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