Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/net-2.6
[pandora-kernel.git] / include / net / ip_vs.h
1 /*
2  *      IP Virtual Server
3  *      data structure and functionality definitions
4  */
5
6 #ifndef _IP_VS_H
7 #define _IP_VS_H
8
9 #include <asm/types.h>          /* For __uXX types */
10 #include <linux/types.h>        /* For __beXX types in userland */
11
12 #define IP_VS_VERSION_CODE      0x010201
13 #define NVERSION(version)                       \
14         (version >> 16) & 0xFF,                 \
15         (version >> 8) & 0xFF,                  \
16         version & 0xFF
17
18 /*
19  *      Virtual Service Flags
20  */
21 #define IP_VS_SVC_F_PERSISTENT  0x0001          /* persistent port */
22 #define IP_VS_SVC_F_HASHED      0x0002          /* hashed entry */
23
24 /*
25  *      Destination Server Flags
26  */
27 #define IP_VS_DEST_F_AVAILABLE  0x0001          /* server is available */
28 #define IP_VS_DEST_F_OVERLOAD   0x0002          /* server is overloaded */
29
30 /*
31  *      IPVS sync daemon states
32  */
33 #define IP_VS_STATE_NONE        0x0000          /* daemon is stopped */
34 #define IP_VS_STATE_MASTER      0x0001          /* started as master */
35 #define IP_VS_STATE_BACKUP      0x0002          /* started as backup */
36
37 /*
38  *      IPVS socket options
39  */
40 #define IP_VS_BASE_CTL          (64+1024+64)            /* base */
41
42 #define IP_VS_SO_SET_NONE       IP_VS_BASE_CTL          /* just peek */
43 #define IP_VS_SO_SET_INSERT     (IP_VS_BASE_CTL+1)
44 #define IP_VS_SO_SET_ADD        (IP_VS_BASE_CTL+2)
45 #define IP_VS_SO_SET_EDIT       (IP_VS_BASE_CTL+3)
46 #define IP_VS_SO_SET_DEL        (IP_VS_BASE_CTL+4)
47 #define IP_VS_SO_SET_FLUSH      (IP_VS_BASE_CTL+5)
48 #define IP_VS_SO_SET_LIST       (IP_VS_BASE_CTL+6)
49 #define IP_VS_SO_SET_ADDDEST    (IP_VS_BASE_CTL+7)
50 #define IP_VS_SO_SET_DELDEST    (IP_VS_BASE_CTL+8)
51 #define IP_VS_SO_SET_EDITDEST   (IP_VS_BASE_CTL+9)
52 #define IP_VS_SO_SET_TIMEOUT    (IP_VS_BASE_CTL+10)
53 #define IP_VS_SO_SET_STARTDAEMON (IP_VS_BASE_CTL+11)
54 #define IP_VS_SO_SET_STOPDAEMON (IP_VS_BASE_CTL+12)
55 #define IP_VS_SO_SET_RESTORE    (IP_VS_BASE_CTL+13)
56 #define IP_VS_SO_SET_SAVE       (IP_VS_BASE_CTL+14)
57 #define IP_VS_SO_SET_ZERO       (IP_VS_BASE_CTL+15)
58 #define IP_VS_SO_SET_MAX        IP_VS_SO_SET_ZERO
59
60 #define IP_VS_SO_GET_VERSION    IP_VS_BASE_CTL
61 #define IP_VS_SO_GET_INFO       (IP_VS_BASE_CTL+1)
62 #define IP_VS_SO_GET_SERVICES   (IP_VS_BASE_CTL+2)
63 #define IP_VS_SO_GET_SERVICE    (IP_VS_BASE_CTL+3)
64 #define IP_VS_SO_GET_DESTS      (IP_VS_BASE_CTL+4)
65 #define IP_VS_SO_GET_DEST       (IP_VS_BASE_CTL+5)      /* not used now */
66 #define IP_VS_SO_GET_TIMEOUT    (IP_VS_BASE_CTL+6)
67 #define IP_VS_SO_GET_DAEMON     (IP_VS_BASE_CTL+7)
68 #define IP_VS_SO_GET_MAX        IP_VS_SO_GET_DAEMON
69
70
71 /*
72  *      IPVS Connection Flags
73  */
74 #define IP_VS_CONN_F_FWD_MASK   0x0007          /* mask for the fwd methods */
75 #define IP_VS_CONN_F_MASQ       0x0000          /* masquerading/NAT */
76 #define IP_VS_CONN_F_LOCALNODE  0x0001          /* local node */
77 #define IP_VS_CONN_F_TUNNEL     0x0002          /* tunneling */
78 #define IP_VS_CONN_F_DROUTE     0x0003          /* direct routing */
79 #define IP_VS_CONN_F_BYPASS     0x0004          /* cache bypass */
80 #define IP_VS_CONN_F_SYNC       0x0020          /* entry created by sync */
81 #define IP_VS_CONN_F_HASHED     0x0040          /* hashed entry */
82 #define IP_VS_CONN_F_NOOUTPUT   0x0080          /* no output packets */
83 #define IP_VS_CONN_F_INACTIVE   0x0100          /* not established */
84 #define IP_VS_CONN_F_OUT_SEQ    0x0200          /* must do output seq adjust */
85 #define IP_VS_CONN_F_IN_SEQ     0x0400          /* must do input seq adjust */
86 #define IP_VS_CONN_F_SEQ_MASK   0x0600          /* in/out sequence mask */
87 #define IP_VS_CONN_F_NO_CPORT   0x0800          /* no client port set yet */
88 #define IP_VS_CONN_F_TEMPLATE   0x1000          /* template, not connection */
89
90 /* Move it to better place one day, for now keep it unique */
91 #define NFC_IPVS_PROPERTY       0x10000
92
93 #define IP_VS_SCHEDNAME_MAXLEN  16
94 #define IP_VS_IFNAME_MAXLEN     16
95
96
97 /*
98  *      The struct ip_vs_service_user and struct ip_vs_dest_user are
99  *      used to set IPVS rules through setsockopt.
100  */
101 struct ip_vs_service_user {
102         /* virtual service addresses */
103         u_int16_t               protocol;
104         __be32                  addr;           /* virtual ip address */
105         __be16                  port;
106         u_int32_t               fwmark;         /* firwall mark of service */
107
108         /* virtual service options */
109         char                    sched_name[IP_VS_SCHEDNAME_MAXLEN];
110         unsigned                flags;          /* virtual service flags */
111         unsigned                timeout;        /* persistent timeout in sec */
112         __be32                  netmask;        /* persistent netmask */
113 };
114
115
116 struct ip_vs_dest_user {
117         /* destination server address */
118         __be32                  addr;
119         __be16                  port;
120
121         /* real server options */
122         unsigned                conn_flags;     /* connection flags */
123         int                     weight;         /* destination weight */
124
125         /* thresholds for active connections */
126         u_int32_t               u_threshold;    /* upper threshold */
127         u_int32_t               l_threshold;    /* lower threshold */
128 };
129
130
131 /*
132  *      IPVS statistics object (for user space)
133  */
134 struct ip_vs_stats_user
135 {
136         __u32                   conns;          /* connections scheduled */
137         __u32                   inpkts;         /* incoming packets */
138         __u32                   outpkts;        /* outgoing packets */
139         __u64                   inbytes;        /* incoming bytes */
140         __u64                   outbytes;       /* outgoing bytes */
141
142         __u32                   cps;            /* current connection rate */
143         __u32                   inpps;          /* current in packet rate */
144         __u32                   outpps;         /* current out packet rate */
145         __u32                   inbps;          /* current in byte rate */
146         __u32                   outbps;         /* current out byte rate */
147 };
148
149
150 /* The argument to IP_VS_SO_GET_INFO */
151 struct ip_vs_getinfo {
152         /* version number */
153         unsigned int            version;
154
155         /* size of connection hash table */
156         unsigned int            size;
157
158         /* number of virtual services */
159         unsigned int            num_services;
160 };
161
162
163 /* The argument to IP_VS_SO_GET_SERVICE */
164 struct ip_vs_service_entry {
165         /* which service: user fills in these */
166         u_int16_t               protocol;
167         __be32                  addr;           /* virtual address */
168         __be16                  port;
169         u_int32_t               fwmark;         /* firwall mark of service */
170
171         /* service options */
172         char                    sched_name[IP_VS_SCHEDNAME_MAXLEN];
173         unsigned                flags;          /* virtual service flags */
174         unsigned                timeout;        /* persistent timeout */
175         __be32                  netmask;        /* persistent netmask */
176
177         /* number of real servers */
178         unsigned int            num_dests;
179
180         /* statistics */
181         struct ip_vs_stats_user stats;
182 };
183
184
185 struct ip_vs_dest_entry {
186         __be32                  addr;           /* destination address */
187         __be16                  port;
188         unsigned                conn_flags;     /* connection flags */
189         int                     weight;         /* destination weight */
190
191         u_int32_t               u_threshold;    /* upper threshold */
192         u_int32_t               l_threshold;    /* lower threshold */
193
194         u_int32_t               activeconns;    /* active connections */
195         u_int32_t               inactconns;     /* inactive connections */
196         u_int32_t               persistconns;   /* persistent connections */
197
198         /* statistics */
199         struct ip_vs_stats_user stats;
200 };
201
202
203 /* The argument to IP_VS_SO_GET_DESTS */
204 struct ip_vs_get_dests {
205         /* which service: user fills in these */
206         u_int16_t               protocol;
207         __be32                  addr;           /* virtual address */
208         __be16                  port;
209         u_int32_t               fwmark;         /* firwall mark of service */
210
211         /* number of real servers */
212         unsigned int            num_dests;
213
214         /* the real servers */
215         struct ip_vs_dest_entry entrytable[0];
216 };
217
218
219 /* The argument to IP_VS_SO_GET_SERVICES */
220 struct ip_vs_get_services {
221         /* number of virtual services */
222         unsigned int            num_services;
223
224         /* service table */
225         struct ip_vs_service_entry entrytable[0];
226 };
227
228
229 /* The argument to IP_VS_SO_GET_TIMEOUT */
230 struct ip_vs_timeout_user {
231         int                     tcp_timeout;
232         int                     tcp_fin_timeout;
233         int                     udp_timeout;
234 };
235
236
237 /* The argument to IP_VS_SO_GET_DAEMON */
238 struct ip_vs_daemon_user {
239         /* sync daemon state (master/backup) */
240         int                     state;
241
242         /* multicast interface name */
243         char                    mcast_ifn[IP_VS_IFNAME_MAXLEN];
244
245         /* SyncID we belong to */
246         int                     syncid;
247 };
248
249
250 #ifdef __KERNEL__
251
252 #include <linux/list.h>                 /* for struct list_head */
253 #include <linux/spinlock.h>             /* for struct rwlock_t */
254 #include <asm/atomic.h>                 /* for struct atomic_t */
255 #include <linux/compiler.h>
256 #include <linux/timer.h>
257
258 #include <net/checksum.h>
259
260 #ifdef CONFIG_IP_VS_DEBUG
261 #include <linux/net.h>
262
263 extern int ip_vs_get_debug_level(void);
264 #define IP_VS_DBG(level, msg...)                        \
265     do {                                                \
266             if (level <= ip_vs_get_debug_level())       \
267                     printk(KERN_DEBUG "IPVS: " msg);    \
268     } while (0)
269 #define IP_VS_DBG_RL(msg...)                            \
270     do {                                                \
271             if (net_ratelimit())                        \
272                     printk(KERN_DEBUG "IPVS: " msg);    \
273     } while (0)
274 #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg)         \
275     do {                                                \
276             if (level <= ip_vs_get_debug_level())       \
277                 pp->debug_packet(pp, skb, ofs, msg);    \
278     } while (0)
279 #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg)      \
280     do {                                                \
281             if (level <= ip_vs_get_debug_level() &&     \
282                 net_ratelimit())                        \
283                 pp->debug_packet(pp, skb, ofs, msg);    \
284     } while (0)
285 #else   /* NO DEBUGGING at ALL */
286 #define IP_VS_DBG(level, msg...)  do {} while (0)
287 #define IP_VS_DBG_RL(msg...)  do {} while (0)
288 #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg)         do {} while (0)
289 #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg)      do {} while (0)
290 #endif
291
292 #define IP_VS_BUG() BUG()
293 #define IP_VS_ERR(msg...) printk(KERN_ERR "IPVS: " msg)
294 #define IP_VS_INFO(msg...) printk(KERN_INFO "IPVS: " msg)
295 #define IP_VS_WARNING(msg...) \
296         printk(KERN_WARNING "IPVS: " msg)
297 #define IP_VS_ERR_RL(msg...)                            \
298     do {                                                \
299             if (net_ratelimit())                        \
300                     printk(KERN_ERR "IPVS: " msg);      \
301     } while (0)
302
303 #ifdef CONFIG_IP_VS_DEBUG
304 #define EnterFunction(level)                                            \
305     do {                                                                \
306             if (level <= ip_vs_get_debug_level())                       \
307                     printk(KERN_DEBUG "Enter: %s, %s line %i\n",        \
308                            __FUNCTION__, __FILE__, __LINE__);           \
309     } while (0)
310 #define LeaveFunction(level)                                            \
311     do {                                                                \
312             if (level <= ip_vs_get_debug_level())                       \
313                         printk(KERN_DEBUG "Leave: %s, %s line %i\n",    \
314                                __FUNCTION__, __FILE__, __LINE__);       \
315     } while (0)
316 #else
317 #define EnterFunction(level)   do {} while (0)
318 #define LeaveFunction(level)   do {} while (0)
319 #endif
320
321 #define IP_VS_WAIT_WHILE(expr)  while (expr) { cpu_relax(); }
322
323
324 /*
325  *      The port number of FTP service (in network order).
326  */
327 #define FTPPORT  __constant_htons(21)
328 #define FTPDATA  __constant_htons(20)
329
330 /*
331  *      TCP State Values
332  */
333 enum {
334         IP_VS_TCP_S_NONE = 0,
335         IP_VS_TCP_S_ESTABLISHED,
336         IP_VS_TCP_S_SYN_SENT,
337         IP_VS_TCP_S_SYN_RECV,
338         IP_VS_TCP_S_FIN_WAIT,
339         IP_VS_TCP_S_TIME_WAIT,
340         IP_VS_TCP_S_CLOSE,
341         IP_VS_TCP_S_CLOSE_WAIT,
342         IP_VS_TCP_S_LAST_ACK,
343         IP_VS_TCP_S_LISTEN,
344         IP_VS_TCP_S_SYNACK,
345         IP_VS_TCP_S_LAST
346 };
347
348 /*
349  *      UDP State Values
350  */
351 enum {
352         IP_VS_UDP_S_NORMAL,
353         IP_VS_UDP_S_LAST,
354 };
355
356 /*
357  *      ICMP State Values
358  */
359 enum {
360         IP_VS_ICMP_S_NORMAL,
361         IP_VS_ICMP_S_LAST,
362 };
363
364 /*
365  *      Delta sequence info structure
366  *      Each ip_vs_conn has 2 (output AND input seq. changes).
367  *      Only used in the VS/NAT.
368  */
369 struct ip_vs_seq {
370         __u32                   init_seq;       /* Add delta from this seq */
371         __u32                   delta;          /* Delta in sequence numbers */
372         __u32                   previous_delta; /* Delta in sequence numbers
373                                                    before last resized pkt */
374 };
375
376
377 /*
378  *      IPVS statistics object
379  */
380 struct ip_vs_stats
381 {
382         __u32                   conns;          /* connections scheduled */
383         __u32                   inpkts;         /* incoming packets */
384         __u32                   outpkts;        /* outgoing packets */
385         __u64                   inbytes;        /* incoming bytes */
386         __u64                   outbytes;       /* outgoing bytes */
387
388         __u32                   cps;            /* current connection rate */
389         __u32                   inpps;          /* current in packet rate */
390         __u32                   outpps;         /* current out packet rate */
391         __u32                   inbps;          /* current in byte rate */
392         __u32                   outbps;         /* current out byte rate */
393
394         spinlock_t              lock;           /* spin lock */
395 };
396
397 struct dst_entry;
398 struct iphdr;
399 struct ip_vs_conn;
400 struct ip_vs_app;
401 struct sk_buff;
402
403 struct ip_vs_protocol {
404         struct ip_vs_protocol   *next;
405         char                    *name;
406         __u16                   protocol;
407         int                     dont_defrag;
408         atomic_t                appcnt;         /* counter of proto app incs */
409         int                     *timeout_table; /* protocol timeout table */
410
411         void (*init)(struct ip_vs_protocol *pp);
412
413         void (*exit)(struct ip_vs_protocol *pp);
414
415         int (*conn_schedule)(struct sk_buff *skb,
416                              struct ip_vs_protocol *pp,
417                              int *verdict, struct ip_vs_conn **cpp);
418
419         struct ip_vs_conn *
420         (*conn_in_get)(const struct sk_buff *skb,
421                        struct ip_vs_protocol *pp,
422                        const struct iphdr *iph,
423                        unsigned int proto_off,
424                        int inverse);
425
426         struct ip_vs_conn *
427         (*conn_out_get)(const struct sk_buff *skb,
428                         struct ip_vs_protocol *pp,
429                         const struct iphdr *iph,
430                         unsigned int proto_off,
431                         int inverse);
432
433         int (*snat_handler)(struct sk_buff *skb,
434                             struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
435
436         int (*dnat_handler)(struct sk_buff *skb,
437                             struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
438
439         int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
440
441         const char *(*state_name)(int state);
442
443         int (*state_transition)(struct ip_vs_conn *cp, int direction,
444                                 const struct sk_buff *skb,
445                                 struct ip_vs_protocol *pp);
446
447         int (*register_app)(struct ip_vs_app *inc);
448
449         void (*unregister_app)(struct ip_vs_app *inc);
450
451         int (*app_conn_bind)(struct ip_vs_conn *cp);
452
453         void (*debug_packet)(struct ip_vs_protocol *pp,
454                              const struct sk_buff *skb,
455                              int offset,
456                              const char *msg);
457
458         void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
459
460         int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
461 };
462
463 extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
464
465 /*
466  *      IP_VS structure allocated for each dynamically scheduled connection
467  */
468 struct ip_vs_conn {
469         struct list_head        c_list;         /* hashed list heads */
470
471         /* Protocol, addresses and port numbers */
472         __be32                   caddr;          /* client address */
473         __be32                   vaddr;          /* virtual address */
474         __be32                   daddr;          /* destination address */
475         __be16                   cport;
476         __be16                   vport;
477         __be16                   dport;
478         __u16                   protocol;       /* Which protocol (TCP/UDP) */
479
480         /* counter and timer */
481         atomic_t                refcnt;         /* reference count */
482         struct timer_list       timer;          /* Expiration timer */
483         volatile unsigned long  timeout;        /* timeout */
484
485         /* Flags and state transition */
486         spinlock_t              lock;           /* lock for state transition */
487         volatile __u16          flags;          /* status flags */
488         volatile __u16          state;          /* state info */
489         volatile __u16          old_state;      /* old state, to be used for
490                                                  * state transition triggerd
491                                                  * synchronization
492                                                  */
493
494         /* Control members */
495         struct ip_vs_conn       *control;       /* Master control connection */
496         atomic_t                n_control;      /* Number of controlled ones */
497         struct ip_vs_dest       *dest;          /* real server */
498         atomic_t                in_pkts;        /* incoming packet counter */
499
500         /* packet transmitter for different forwarding methods.  If it
501            mangles the packet, it must return NF_DROP or better NF_STOLEN,
502            otherwise this must be changed to a sk_buff **.
503          */
504         int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
505                            struct ip_vs_protocol *pp);
506
507         /* Note: we can group the following members into a structure,
508            in order to save more space, and the following members are
509            only used in VS/NAT anyway */
510         struct ip_vs_app        *app;           /* bound ip_vs_app object */
511         void                    *app_data;      /* Application private data */
512         struct ip_vs_seq        in_seq;         /* incoming seq. struct */
513         struct ip_vs_seq        out_seq;        /* outgoing seq. struct */
514 };
515
516
517 /*
518  *      The information about the virtual service offered to the net
519  *      and the forwarding entries
520  */
521 struct ip_vs_service {
522         struct list_head        s_list;   /* for normal service table */
523         struct list_head        f_list;   /* for fwmark-based service table */
524         atomic_t                refcnt;   /* reference counter */
525         atomic_t                usecnt;   /* use counter */
526
527         __u16                   protocol; /* which protocol (TCP/UDP) */
528         __be32                  addr;     /* IP address for virtual service */
529         __be16                  port;     /* port number for the service */
530         __u32                   fwmark;   /* firewall mark of the service */
531         unsigned                flags;    /* service status flags */
532         unsigned                timeout;  /* persistent timeout in ticks */
533         __be32                  netmask;  /* grouping granularity */
534
535         struct list_head        destinations;  /* real server d-linked list */
536         __u32                   num_dests;     /* number of servers */
537         struct ip_vs_stats      stats;         /* statistics for the service */
538         struct ip_vs_app        *inc;     /* bind conns to this app inc */
539
540         /* for scheduling */
541         struct ip_vs_scheduler  *scheduler;    /* bound scheduler object */
542         rwlock_t                sched_lock;    /* lock sched_data */
543         void                    *sched_data;   /* scheduler application data */
544 };
545
546
547 /*
548  *      The real server destination forwarding entry
549  *      with ip address, port number, and so on.
550  */
551 struct ip_vs_dest {
552         struct list_head        n_list;   /* for the dests in the service */
553         struct list_head        d_list;   /* for table with all the dests */
554
555         __be32                  addr;           /* IP address of the server */
556         __be16                  port;           /* port number of the server */
557         volatile unsigned       flags;          /* dest status flags */
558         atomic_t                conn_flags;     /* flags to copy to conn */
559         atomic_t                weight;         /* server weight */
560
561         atomic_t                refcnt;         /* reference counter */
562         struct ip_vs_stats      stats;          /* statistics */
563
564         /* connection counters and thresholds */
565         atomic_t                activeconns;    /* active connections */
566         atomic_t                inactconns;     /* inactive connections */
567         atomic_t                persistconns;   /* persistent connections */
568         __u32                   u_threshold;    /* upper threshold */
569         __u32                   l_threshold;    /* lower threshold */
570
571         /* for destination cache */
572         spinlock_t              dst_lock;       /* lock of dst_cache */
573         struct dst_entry        *dst_cache;     /* destination cache entry */
574         u32                     dst_rtos;       /* RT_TOS(tos) for dst */
575
576         /* for virtual service */
577         struct ip_vs_service    *svc;           /* service it belongs to */
578         __u16                   protocol;       /* which protocol (TCP/UDP) */
579         __be32                  vaddr;          /* virtual IP address */
580         __be16                  vport;          /* virtual port number */
581         __u32                   vfwmark;        /* firewall mark of service */
582 };
583
584
585 /*
586  *      The scheduler object
587  */
588 struct ip_vs_scheduler {
589         struct list_head        n_list;         /* d-linked list head */
590         char                    *name;          /* scheduler name */
591         atomic_t                refcnt;         /* reference counter */
592         struct module           *module;        /* THIS_MODULE/NULL */
593
594         /* scheduler initializing service */
595         int (*init_service)(struct ip_vs_service *svc);
596         /* scheduling service finish */
597         int (*done_service)(struct ip_vs_service *svc);
598         /* scheduler updating service */
599         int (*update_service)(struct ip_vs_service *svc);
600
601         /* selecting a server from the given service */
602         struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
603                                        const struct sk_buff *skb);
604 };
605
606
607 /*
608  *      The application module object (a.k.a. app incarnation)
609  */
610 struct ip_vs_app
611 {
612         struct list_head        a_list;         /* member in app list */
613         int                     type;           /* IP_VS_APP_TYPE_xxx */
614         char                    *name;          /* application module name */
615         __u16                   protocol;
616         struct module           *module;        /* THIS_MODULE/NULL */
617         struct list_head        incs_list;      /* list of incarnations */
618
619         /* members for application incarnations */
620         struct list_head        p_list;         /* member in proto app list */
621         struct ip_vs_app        *app;           /* its real application */
622         __be16                  port;           /* port number in net order */
623         atomic_t                usecnt;         /* usage counter */
624
625         /* output hook: return false if can't linearize. diff set for TCP.  */
626         int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
627                        struct sk_buff *, int *diff);
628
629         /* input hook: return false if can't linearize. diff set for TCP. */
630         int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
631                       struct sk_buff *, int *diff);
632
633         /* ip_vs_app initializer */
634         int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
635
636         /* ip_vs_app finish */
637         int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
638
639
640         /* not used now */
641         int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
642                          struct ip_vs_protocol *);
643
644         void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
645
646         int *                   timeout_table;
647         int *                   timeouts;
648         int                     timeouts_size;
649
650         int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
651                              int *verdict, struct ip_vs_conn **cpp);
652
653         struct ip_vs_conn *
654         (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
655                        const struct iphdr *iph, unsigned int proto_off,
656                        int inverse);
657
658         struct ip_vs_conn *
659         (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
660                         const struct iphdr *iph, unsigned int proto_off,
661                         int inverse);
662
663         int (*state_transition)(struct ip_vs_conn *cp, int direction,
664                                 const struct sk_buff *skb,
665                                 struct ip_vs_app *app);
666
667         void (*timeout_change)(struct ip_vs_app *app, int flags);
668 };
669
670
671 /*
672  *      IPVS core functions
673  *      (from ip_vs_core.c)
674  */
675 extern const char *ip_vs_proto_name(unsigned proto);
676 extern void ip_vs_init_hash_table(struct list_head *table, int rows);
677 #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table(t, sizeof(t)/sizeof(t[0]))
678
679 #define IP_VS_APP_TYPE_UNSPEC   0
680 #define IP_VS_APP_TYPE_FTP      1
681
682 /*
683  *     ip_vs_conn handling functions
684  *     (from ip_vs_conn.c)
685  */
686
687 /*
688  *     IPVS connection entry hash table
689  */
690 #ifndef CONFIG_IP_VS_TAB_BITS
691 #define CONFIG_IP_VS_TAB_BITS   12
692 #endif
693 /* make sure that IP_VS_CONN_TAB_BITS is located in [8, 20] */
694 #if CONFIG_IP_VS_TAB_BITS < 8
695 #define IP_VS_CONN_TAB_BITS     8
696 #endif
697 #if CONFIG_IP_VS_TAB_BITS > 20
698 #define IP_VS_CONN_TAB_BITS     20
699 #endif
700 #if 8 <= CONFIG_IP_VS_TAB_BITS && CONFIG_IP_VS_TAB_BITS <= 20
701 #define IP_VS_CONN_TAB_BITS     CONFIG_IP_VS_TAB_BITS
702 #endif
703 #define IP_VS_CONN_TAB_SIZE     (1 << IP_VS_CONN_TAB_BITS)
704 #define IP_VS_CONN_TAB_MASK     (IP_VS_CONN_TAB_SIZE - 1)
705
706 enum {
707         IP_VS_DIR_INPUT = 0,
708         IP_VS_DIR_OUTPUT,
709         IP_VS_DIR_INPUT_ONLY,
710         IP_VS_DIR_LAST,
711 };
712
713 extern struct ip_vs_conn *ip_vs_conn_in_get
714 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
715 extern struct ip_vs_conn *ip_vs_ct_in_get
716 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
717 extern struct ip_vs_conn *ip_vs_conn_out_get
718 (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
719
720 /* put back the conn without restarting its timer */
721 static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
722 {
723         atomic_dec(&cp->refcnt);
724 }
725 extern void ip_vs_conn_put(struct ip_vs_conn *cp);
726 extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
727
728 extern struct ip_vs_conn *
729 ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
730                __be32 daddr, __be16 dport, unsigned flags,
731                struct ip_vs_dest *dest);
732 extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
733
734 extern const char * ip_vs_state_name(__u16 proto, int state);
735
736 extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
737 extern int ip_vs_check_template(struct ip_vs_conn *ct);
738 extern void ip_vs_secure_tcp_set(int on);
739 extern void ip_vs_random_dropentry(void);
740 extern int ip_vs_conn_init(void);
741 extern void ip_vs_conn_cleanup(void);
742
743 static inline void ip_vs_control_del(struct ip_vs_conn *cp)
744 {
745         struct ip_vs_conn *ctl_cp = cp->control;
746         if (!ctl_cp) {
747                 IP_VS_ERR("request control DEL for uncontrolled: "
748                           "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
749                           NIPQUAD(cp->caddr),ntohs(cp->cport),
750                           NIPQUAD(cp->vaddr),ntohs(cp->vport));
751                 return;
752         }
753
754         IP_VS_DBG(7, "DELeting control for: "
755                   "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
756                   NIPQUAD(cp->caddr),ntohs(cp->cport),
757                   NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
758
759         cp->control = NULL;
760         if (atomic_read(&ctl_cp->n_control) == 0) {
761                 IP_VS_ERR("BUG control DEL with n=0 : "
762                           "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
763                           NIPQUAD(cp->caddr),ntohs(cp->cport),
764                           NIPQUAD(cp->vaddr),ntohs(cp->vport));
765                 return;
766         }
767         atomic_dec(&ctl_cp->n_control);
768 }
769
770 static inline void
771 ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
772 {
773         if (cp->control) {
774                 IP_VS_ERR("request control ADD for already controlled: "
775                           "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
776                           NIPQUAD(cp->caddr),ntohs(cp->cport),
777                           NIPQUAD(cp->vaddr),ntohs(cp->vport));
778                 ip_vs_control_del(cp);
779         }
780
781         IP_VS_DBG(7, "ADDing control for: "
782                   "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
783                   NIPQUAD(cp->caddr),ntohs(cp->cport),
784                   NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
785
786         cp->control = ctl_cp;
787         atomic_inc(&ctl_cp->n_control);
788 }
789
790
791 /*
792  *      IPVS application functions
793  *      (from ip_vs_app.c)
794  */
795 #define IP_VS_APP_MAX_PORTS  8
796 extern int register_ip_vs_app(struct ip_vs_app *app);
797 extern void unregister_ip_vs_app(struct ip_vs_app *app);
798 extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
799 extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
800 extern int
801 register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
802 extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
803 extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
804
805 extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
806 extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
807 extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
808                              char *o_buf, int o_len, char *n_buf, int n_len);
809 extern int ip_vs_app_init(void);
810 extern void ip_vs_app_cleanup(void);
811
812
813 /*
814  *      IPVS protocol functions (from ip_vs_proto.c)
815  */
816 extern int ip_vs_protocol_init(void);
817 extern void ip_vs_protocol_cleanup(void);
818 extern void ip_vs_protocol_timeout_change(int flags);
819 extern int *ip_vs_create_timeout_table(int *table, int size);
820 extern int
821 ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
822 extern void
823 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
824                           int offset, const char *msg);
825
826 extern struct ip_vs_protocol ip_vs_protocol_tcp;
827 extern struct ip_vs_protocol ip_vs_protocol_udp;
828 extern struct ip_vs_protocol ip_vs_protocol_icmp;
829 extern struct ip_vs_protocol ip_vs_protocol_esp;
830 extern struct ip_vs_protocol ip_vs_protocol_ah;
831
832
833 /*
834  *      Registering/unregistering scheduler functions
835  *      (from ip_vs_sched.c)
836  */
837 extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
838 extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
839 extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
840                                 struct ip_vs_scheduler *scheduler);
841 extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
842 extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
843 extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
844 extern struct ip_vs_conn *
845 ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
846 extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
847                         struct ip_vs_protocol *pp);
848
849
850 /*
851  *      IPVS control data and functions (from ip_vs_ctl.c)
852  */
853 extern int sysctl_ip_vs_cache_bypass;
854 extern int sysctl_ip_vs_expire_nodest_conn;
855 extern int sysctl_ip_vs_expire_quiescent_template;
856 extern int sysctl_ip_vs_sync_threshold[2];
857 extern int sysctl_ip_vs_nat_icmp_send;
858 extern struct ip_vs_stats ip_vs_stats;
859
860 extern struct ip_vs_service *
861 ip_vs_service_get(__u32 fwmark, __u16 protocol, __be32 vaddr, __be16 vport);
862
863 static inline void ip_vs_service_put(struct ip_vs_service *svc)
864 {
865         atomic_dec(&svc->usecnt);
866 }
867
868 extern struct ip_vs_dest *
869 ip_vs_lookup_real_service(__u16 protocol, __be32 daddr, __be16 dport);
870 extern int ip_vs_use_count_inc(void);
871 extern void ip_vs_use_count_dec(void);
872 extern int ip_vs_control_init(void);
873 extern void ip_vs_control_cleanup(void);
874 extern struct ip_vs_dest *
875 ip_vs_find_dest(__be32 daddr, __be16 dport,
876                  __be32 vaddr, __be16 vport, __u16 protocol);
877 extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
878
879
880 /*
881  *      IPVS sync daemon data and function prototypes
882  *      (from ip_vs_sync.c)
883  */
884 extern volatile int ip_vs_sync_state;
885 extern volatile int ip_vs_master_syncid;
886 extern volatile int ip_vs_backup_syncid;
887 extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
888 extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
889 extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
890 extern int stop_sync_thread(int state);
891 extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
892
893
894 /*
895  *      IPVS rate estimator prototypes (from ip_vs_est.c)
896  */
897 extern int ip_vs_new_estimator(struct ip_vs_stats *stats);
898 extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
899 extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
900
901 /*
902  *      Various IPVS packet transmitters (from ip_vs_xmit.c)
903  */
904 extern int ip_vs_null_xmit
905 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
906 extern int ip_vs_bypass_xmit
907 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
908 extern int ip_vs_nat_xmit
909 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
910 extern int ip_vs_tunnel_xmit
911 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
912 extern int ip_vs_dr_xmit
913 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
914 extern int ip_vs_icmp_xmit
915 (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
916 extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
917
918
919 /*
920  *      This is a simple mechanism to ignore packets when
921  *      we are loaded. Just set ip_vs_drop_rate to 'n' and
922  *      we start to drop 1/rate of the packets
923  */
924 extern int ip_vs_drop_rate;
925 extern int ip_vs_drop_counter;
926
927 static __inline__ int ip_vs_todrop(void)
928 {
929         if (!ip_vs_drop_rate) return 0;
930         if (--ip_vs_drop_counter > 0) return 0;
931         ip_vs_drop_counter = ip_vs_drop_rate;
932         return 1;
933 }
934
935 /*
936  *      ip_vs_fwd_tag returns the forwarding tag of the connection
937  */
938 #define IP_VS_FWD_METHOD(cp)  (cp->flags & IP_VS_CONN_F_FWD_MASK)
939
940 static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
941 {
942         char fwd;
943
944         switch (IP_VS_FWD_METHOD(cp)) {
945         case IP_VS_CONN_F_MASQ:
946                 fwd = 'M'; break;
947         case IP_VS_CONN_F_LOCALNODE:
948                 fwd = 'L'; break;
949         case IP_VS_CONN_F_TUNNEL:
950                 fwd = 'T'; break;
951         case IP_VS_CONN_F_DROUTE:
952                 fwd = 'R'; break;
953         case IP_VS_CONN_F_BYPASS:
954                 fwd = 'B'; break;
955         default:
956                 fwd = '?'; break;
957         }
958         return fwd;
959 }
960
961 extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
962                 struct ip_vs_conn *cp, int dir);
963
964 extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
965
966 static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
967 {
968         __be32 diff[2] = { ~old, new };
969
970         return csum_partial((char *) diff, sizeof(diff), oldsum);
971 }
972
973 static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
974 {
975         __be16 diff[2] = { ~old, new };
976
977         return csum_partial((char *) diff, sizeof(diff), oldsum);
978 }
979
980 #endif /* __KERNEL__ */
981
982 #endif  /* _IP_VS_H */