blob: 0400e590b6a27aa98c77b6534f554709021db75c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP Virtual Server
3 * data structure and functionality definitions
4 */
5
Julius Volzbc4768e2008-07-31 20:45:24 -07006#ifndef _NET_IP_VS_H
7#define _NET_IP_VS_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Julius Volzbc4768e2008-07-31 20:45:24 -07009#include <linux/ip_vs.h> /* definitions shared with userland */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Julius Volzbc4768e2008-07-31 20:45:24 -070011/* old ipvsadm versions still include this file directly */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#ifdef __KERNEL__
13
Julius Volzbc4768e2008-07-31 20:45:24 -070014#include <asm/types.h> /* for __uXX types */
15
16#include <linux/sysctl.h> /* for ctl_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/list.h> /* for struct list_head */
18#include <linux/spinlock.h> /* for struct rwlock_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/atomic.h> /* for struct atomic_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/compiler.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020021#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020023#include <net/checksum.h>
Julius Volze7ade462008-09-02 15:55:33 +020024#include <linux/netfilter.h> /* for union nf_inet_addr */
25#include <linux/ipv6.h> /* for struct ipv6hdr */
26#include <net/ipv6.h> /* for ipv6_addr_copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Julius Volz64aae3c2008-09-02 15:55:34 +020028struct ip_vs_iphdr {
29 int len;
30 __u8 protocol;
31 union nf_inet_addr saddr;
32 union nf_inet_addr daddr;
33};
34
35static inline void
36ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
37{
38#ifdef CONFIG_IP_VS_IPV6
39 if (af == AF_INET6) {
40 const struct ipv6hdr *iph = nh;
41 iphdr->len = sizeof(struct ipv6hdr);
42 iphdr->protocol = iph->nexthdr;
43 ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
44 ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
45 } else
46#endif
47 {
48 const struct iphdr *iph = nh;
49 iphdr->len = iph->ihl * 4;
50 iphdr->protocol = iph->protocol;
51 iphdr->saddr.ip = iph->saddr;
52 iphdr->daddr.ip = iph->daddr;
53 }
54}
55
56static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
57 const union nf_inet_addr *src)
58{
59#ifdef CONFIG_IP_VS_IPV6
60 if (af == AF_INET6)
61 ipv6_addr_copy(&dst->in6, &src->in6);
62 else
63#endif
64 dst->ip = src->ip;
65}
66
67static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
68 const union nf_inet_addr *b)
69{
70#ifdef CONFIG_IP_VS_IPV6
71 if (af == AF_INET6)
72 return ipv6_addr_equal(&a->in6, &b->in6);
73#endif
74 return a->ip == b->ip;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifdef CONFIG_IP_VS_DEBUG
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020078#include <linux/net.h>
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080extern int ip_vs_get_debug_level(void);
Julius Volzc842a3a2008-09-02 15:55:35 +020081
82static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
83 const union nf_inet_addr *addr,
84 int *idx)
85{
86 int len;
87#ifdef CONFIG_IP_VS_IPV6
88 if (af == AF_INET6)
89 len = snprintf(&buf[*idx], buf_len - *idx, "[" NIP6_FMT "]",
90 NIP6(addr->in6)) + 1;
91 else
92#endif
93 len = snprintf(&buf[*idx], buf_len - *idx, NIPQUAD_FMT,
94 NIPQUAD(addr->ip)) + 1;
95
96 *idx += len;
97 BUG_ON(*idx > buf_len + 1);
98 return &buf[*idx - len];
99}
100
101#define IP_VS_DBG_BUF(level, msg...) \
102 do { \
103 char ip_vs_dbg_buf[160]; \
104 int ip_vs_dbg_idx = 0; \
105 if (level <= ip_vs_get_debug_level()) \
106 printk(KERN_DEBUG "IPVS: " msg); \
107 } while (0)
108#define IP_VS_ERR_BUF(msg...) \
109 do { \
110 char ip_vs_dbg_buf[160]; \
111 int ip_vs_dbg_idx = 0; \
112 printk(KERN_ERR "IPVS: " msg); \
113 } while (0)
114
115/* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
116#define IP_VS_DBG_ADDR(af, addr) \
117 ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
118 sizeof(ip_vs_dbg_buf), addr, \
119 &ip_vs_dbg_idx)
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define IP_VS_DBG(level, msg...) \
122 do { \
123 if (level <= ip_vs_get_debug_level()) \
124 printk(KERN_DEBUG "IPVS: " msg); \
125 } while (0)
126#define IP_VS_DBG_RL(msg...) \
127 do { \
128 if (net_ratelimit()) \
129 printk(KERN_DEBUG "IPVS: " msg); \
130 } while (0)
131#define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) \
132 do { \
133 if (level <= ip_vs_get_debug_level()) \
134 pp->debug_packet(pp, skb, ofs, msg); \
135 } while (0)
136#define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) \
137 do { \
138 if (level <= ip_vs_get_debug_level() && \
139 net_ratelimit()) \
140 pp->debug_packet(pp, skb, ofs, msg); \
141 } while (0)
142#else /* NO DEBUGGING at ALL */
Julius Volzc842a3a2008-09-02 15:55:35 +0200143#define IP_VS_DBG_BUF(level, msg...) do {} while (0)
144#define IP_VS_ERR_BUF(msg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define IP_VS_DBG(level, msg...) do {} while (0)
146#define IP_VS_DBG_RL(msg...) do {} while (0)
147#define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) do {} while (0)
148#define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) do {} while (0)
149#endif
150
151#define IP_VS_BUG() BUG()
152#define IP_VS_ERR(msg...) printk(KERN_ERR "IPVS: " msg)
153#define IP_VS_INFO(msg...) printk(KERN_INFO "IPVS: " msg)
154#define IP_VS_WARNING(msg...) \
155 printk(KERN_WARNING "IPVS: " msg)
156#define IP_VS_ERR_RL(msg...) \
157 do { \
158 if (net_ratelimit()) \
159 printk(KERN_ERR "IPVS: " msg); \
160 } while (0)
161
162#ifdef CONFIG_IP_VS_DEBUG
163#define EnterFunction(level) \
164 do { \
165 if (level <= ip_vs_get_debug_level()) \
166 printk(KERN_DEBUG "Enter: %s, %s line %i\n", \
167 __FUNCTION__, __FILE__, __LINE__); \
168 } while (0)
169#define LeaveFunction(level) \
170 do { \
171 if (level <= ip_vs_get_debug_level()) \
172 printk(KERN_DEBUG "Leave: %s, %s line %i\n", \
173 __FUNCTION__, __FILE__, __LINE__); \
174 } while (0)
175#else
176#define EnterFunction(level) do {} while (0)
177#define LeaveFunction(level) do {} while (0)
178#endif
179
180#define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
181
182
183/*
184 * The port number of FTP service (in network order).
185 */
186#define FTPPORT __constant_htons(21)
187#define FTPDATA __constant_htons(20)
188
189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * TCP State Values
191 */
192enum {
193 IP_VS_TCP_S_NONE = 0,
194 IP_VS_TCP_S_ESTABLISHED,
195 IP_VS_TCP_S_SYN_SENT,
196 IP_VS_TCP_S_SYN_RECV,
197 IP_VS_TCP_S_FIN_WAIT,
198 IP_VS_TCP_S_TIME_WAIT,
199 IP_VS_TCP_S_CLOSE,
200 IP_VS_TCP_S_CLOSE_WAIT,
201 IP_VS_TCP_S_LAST_ACK,
202 IP_VS_TCP_S_LISTEN,
203 IP_VS_TCP_S_SYNACK,
204 IP_VS_TCP_S_LAST
205};
206
207/*
208 * UDP State Values
209 */
210enum {
211 IP_VS_UDP_S_NORMAL,
212 IP_VS_UDP_S_LAST,
213};
214
215/*
216 * ICMP State Values
217 */
218enum {
219 IP_VS_ICMP_S_NORMAL,
220 IP_VS_ICMP_S_LAST,
221};
222
223/*
224 * Delta sequence info structure
225 * Each ip_vs_conn has 2 (output AND input seq. changes).
226 * Only used in the VS/NAT.
227 */
228struct ip_vs_seq {
229 __u32 init_seq; /* Add delta from this seq */
230 __u32 delta; /* Delta in sequence numbers */
231 __u32 previous_delta; /* Delta in sequence numbers
232 before last resized pkt */
233};
234
235
236/*
Sven Wegener3a14a312008-08-10 18:24:41 +0000237 * IPVS statistics objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Sven Wegener3a14a312008-08-10 18:24:41 +0000239struct ip_vs_estimator {
240 struct list_head list;
241
242 u64 last_inbytes;
243 u64 last_outbytes;
244 u32 last_conns;
245 u32 last_inpkts;
246 u32 last_outpkts;
247
248 u32 cps;
249 u32 inpps;
250 u32 outpps;
251 u32 inbps;
252 u32 outbps;
253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255struct ip_vs_stats
256{
257 __u32 conns; /* connections scheduled */
258 __u32 inpkts; /* incoming packets */
259 __u32 outpkts; /* outgoing packets */
260 __u64 inbytes; /* incoming bytes */
261 __u64 outbytes; /* outgoing bytes */
262
263 __u32 cps; /* current connection rate */
264 __u32 inpps; /* current in packet rate */
265 __u32 outpps; /* current out packet rate */
266 __u32 inbps; /* current in byte rate */
267 __u32 outbps; /* current out byte rate */
268
Sven Wegener3a14a312008-08-10 18:24:41 +0000269 /*
270 * Don't add anything before the lock, because we use memcpy() to copy
271 * the members before the lock to struct ip_vs_stats_user in
272 * ip_vs_ctl.c.
273 */
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 spinlock_t lock; /* spin lock */
Sven Wegener3a14a312008-08-10 18:24:41 +0000276
277 struct ip_vs_estimator est; /* estimator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200280struct dst_entry;
281struct iphdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282struct ip_vs_conn;
283struct ip_vs_app;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200284struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286struct ip_vs_protocol {
287 struct ip_vs_protocol *next;
288 char *name;
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700289 u16 protocol;
290 u16 num_states;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int dont_defrag;
292 atomic_t appcnt; /* counter of proto app incs */
293 int *timeout_table; /* protocol timeout table */
294
295 void (*init)(struct ip_vs_protocol *pp);
296
297 void (*exit)(struct ip_vs_protocol *pp);
298
299 int (*conn_schedule)(struct sk_buff *skb,
300 struct ip_vs_protocol *pp,
301 int *verdict, struct ip_vs_conn **cpp);
302
303 struct ip_vs_conn *
304 (*conn_in_get)(const struct sk_buff *skb,
305 struct ip_vs_protocol *pp,
306 const struct iphdr *iph,
307 unsigned int proto_off,
308 int inverse);
309
310 struct ip_vs_conn *
311 (*conn_out_get)(const struct sk_buff *skb,
312 struct ip_vs_protocol *pp,
313 const struct iphdr *iph,
314 unsigned int proto_off,
315 int inverse);
316
Herbert Xu3db05fe2007-10-15 00:53:15 -0700317 int (*snat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
319
Herbert Xu3db05fe2007-10-15 00:53:15 -0700320 int (*dnat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
322
323 int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
324
325 const char *(*state_name)(int state);
326
327 int (*state_transition)(struct ip_vs_conn *cp, int direction,
328 const struct sk_buff *skb,
329 struct ip_vs_protocol *pp);
330
331 int (*register_app)(struct ip_vs_app *inc);
332
333 void (*unregister_app)(struct ip_vs_app *inc);
334
335 int (*app_conn_bind)(struct ip_vs_conn *cp);
336
337 void (*debug_packet)(struct ip_vs_protocol *pp,
338 const struct sk_buff *skb,
339 int offset,
340 const char *msg);
341
342 void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
343
344 int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
345};
346
347extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
348
349/*
350 * IP_VS structure allocated for each dynamically scheduled connection
351 */
352struct ip_vs_conn {
353 struct list_head c_list; /* hashed list heads */
354
355 /* Protocol, addresses and port numbers */
Julius Volze7ade462008-09-02 15:55:33 +0200356 u16 af; /* address family */
357 union nf_inet_addr caddr; /* client address */
358 union nf_inet_addr vaddr; /* virtual address */
359 union nf_inet_addr daddr; /* destination address */
Al Viro014d7302006-09-28 14:29:52 -0700360 __be16 cport;
361 __be16 vport;
362 __be16 dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 __u16 protocol; /* Which protocol (TCP/UDP) */
364
365 /* counter and timer */
366 atomic_t refcnt; /* reference count */
367 struct timer_list timer; /* Expiration timer */
368 volatile unsigned long timeout; /* timeout */
369
370 /* Flags and state transition */
371 spinlock_t lock; /* lock for state transition */
372 volatile __u16 flags; /* status flags */
373 volatile __u16 state; /* state info */
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -0800374 volatile __u16 old_state; /* old state, to be used for
375 * state transition triggerd
376 * synchronization
377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Control members */
380 struct ip_vs_conn *control; /* Master control connection */
381 atomic_t n_control; /* Number of controlled ones */
382 struct ip_vs_dest *dest; /* real server */
383 atomic_t in_pkts; /* incoming packet counter */
384
385 /* packet transmitter for different forwarding methods. If it
386 mangles the packet, it must return NF_DROP or better NF_STOLEN,
387 otherwise this must be changed to a sk_buff **.
388 */
389 int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
390 struct ip_vs_protocol *pp);
391
392 /* Note: we can group the following members into a structure,
393 in order to save more space, and the following members are
394 only used in VS/NAT anyway */
395 struct ip_vs_app *app; /* bound ip_vs_app object */
396 void *app_data; /* Application private data */
397 struct ip_vs_seq in_seq; /* incoming seq. struct */
398 struct ip_vs_seq out_seq; /* outgoing seq. struct */
399};
400
401
402/*
403 * The information about the virtual service offered to the net
404 * and the forwarding entries
405 */
406struct ip_vs_service {
407 struct list_head s_list; /* for normal service table */
408 struct list_head f_list; /* for fwmark-based service table */
409 atomic_t refcnt; /* reference counter */
410 atomic_t usecnt; /* use counter */
411
Julius Volze7ade462008-09-02 15:55:33 +0200412 u16 af; /* address family */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200414 union nf_inet_addr addr; /* IP address for virtual service */
Al Viro014d7302006-09-28 14:29:52 -0700415 __be16 port; /* port number for the service */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 __u32 fwmark; /* firewall mark of the service */
417 unsigned flags; /* service status flags */
418 unsigned timeout; /* persistent timeout in ticks */
Al Viro014d7302006-09-28 14:29:52 -0700419 __be32 netmask; /* grouping granularity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 struct list_head destinations; /* real server d-linked list */
422 __u32 num_dests; /* number of servers */
423 struct ip_vs_stats stats; /* statistics for the service */
424 struct ip_vs_app *inc; /* bind conns to this app inc */
425
426 /* for scheduling */
427 struct ip_vs_scheduler *scheduler; /* bound scheduler object */
428 rwlock_t sched_lock; /* lock sched_data */
429 void *sched_data; /* scheduler application data */
430};
431
432
433/*
434 * The real server destination forwarding entry
435 * with ip address, port number, and so on.
436 */
437struct ip_vs_dest {
438 struct list_head n_list; /* for the dests in the service */
439 struct list_head d_list; /* for table with all the dests */
440
Julius Volze7ade462008-09-02 15:55:33 +0200441 u16 af; /* address family */
442 union nf_inet_addr addr; /* IP address of the server */
Al Viro014d7302006-09-28 14:29:52 -0700443 __be16 port; /* port number of the server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 volatile unsigned flags; /* dest status flags */
445 atomic_t conn_flags; /* flags to copy to conn */
446 atomic_t weight; /* server weight */
447
448 atomic_t refcnt; /* reference counter */
449 struct ip_vs_stats stats; /* statistics */
450
451 /* connection counters and thresholds */
452 atomic_t activeconns; /* active connections */
453 atomic_t inactconns; /* inactive connections */
454 atomic_t persistconns; /* persistent connections */
455 __u32 u_threshold; /* upper threshold */
456 __u32 l_threshold; /* lower threshold */
457
458 /* for destination cache */
459 spinlock_t dst_lock; /* lock of dst_cache */
460 struct dst_entry *dst_cache; /* destination cache entry */
461 u32 dst_rtos; /* RT_TOS(tos) for dst */
462
463 /* for virtual service */
464 struct ip_vs_service *svc; /* service it belongs to */
465 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200466 union nf_inet_addr vaddr; /* virtual IP address */
Al Viro014d7302006-09-28 14:29:52 -0700467 __be16 vport; /* virtual port number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 __u32 vfwmark; /* firewall mark of service */
469};
470
471
472/*
473 * The scheduler object
474 */
475struct ip_vs_scheduler {
476 struct list_head n_list; /* d-linked list head */
477 char *name; /* scheduler name */
478 atomic_t refcnt; /* reference counter */
479 struct module *module; /* THIS_MODULE/NULL */
480
481 /* scheduler initializing service */
482 int (*init_service)(struct ip_vs_service *svc);
483 /* scheduling service finish */
484 int (*done_service)(struct ip_vs_service *svc);
485 /* scheduler updating service */
486 int (*update_service)(struct ip_vs_service *svc);
487
488 /* selecting a server from the given service */
489 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
490 const struct sk_buff *skb);
491};
492
493
494/*
495 * The application module object (a.k.a. app incarnation)
496 */
497struct ip_vs_app
498{
499 struct list_head a_list; /* member in app list */
500 int type; /* IP_VS_APP_TYPE_xxx */
501 char *name; /* application module name */
502 __u16 protocol;
503 struct module *module; /* THIS_MODULE/NULL */
504 struct list_head incs_list; /* list of incarnations */
505
506 /* members for application incarnations */
507 struct list_head p_list; /* member in proto app list */
508 struct ip_vs_app *app; /* its real application */
Al Viro014d7302006-09-28 14:29:52 -0700509 __be16 port; /* port number in net order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 atomic_t usecnt; /* usage counter */
511
512 /* output hook: return false if can't linearize. diff set for TCP. */
513 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700514 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* input hook: return false if can't linearize. diff set for TCP. */
517 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700518 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* ip_vs_app initializer */
521 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
522
523 /* ip_vs_app finish */
524 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
525
526
527 /* not used now */
528 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
529 struct ip_vs_protocol *);
530
531 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
532
533 int * timeout_table;
534 int * timeouts;
535 int timeouts_size;
536
537 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
538 int *verdict, struct ip_vs_conn **cpp);
539
540 struct ip_vs_conn *
541 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
542 const struct iphdr *iph, unsigned int proto_off,
543 int inverse);
544
545 struct ip_vs_conn *
546 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
547 const struct iphdr *iph, unsigned int proto_off,
548 int inverse);
549
550 int (*state_transition)(struct ip_vs_conn *cp, int direction,
551 const struct sk_buff *skb,
552 struct ip_vs_app *app);
553
554 void (*timeout_change)(struct ip_vs_app *app, int flags);
555};
556
557
558/*
559 * IPVS core functions
560 * (from ip_vs_core.c)
561 */
562extern const char *ip_vs_proto_name(unsigned proto);
563extern void ip_vs_init_hash_table(struct list_head *table, int rows);
Sven Wegenerafdd6142008-08-10 09:18:01 +0000564#define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#define IP_VS_APP_TYPE_FTP 1
567
568/*
569 * ip_vs_conn handling functions
570 * (from ip_vs_conn.c)
571 */
572
573/*
574 * IPVS connection entry hash table
575 */
576#ifndef CONFIG_IP_VS_TAB_BITS
577#define CONFIG_IP_VS_TAB_BITS 12
578#endif
579/* make sure that IP_VS_CONN_TAB_BITS is located in [8, 20] */
580#if CONFIG_IP_VS_TAB_BITS < 8
581#define IP_VS_CONN_TAB_BITS 8
582#endif
583#if CONFIG_IP_VS_TAB_BITS > 20
584#define IP_VS_CONN_TAB_BITS 20
585#endif
586#if 8 <= CONFIG_IP_VS_TAB_BITS && CONFIG_IP_VS_TAB_BITS <= 20
587#define IP_VS_CONN_TAB_BITS CONFIG_IP_VS_TAB_BITS
588#endif
589#define IP_VS_CONN_TAB_SIZE (1 << IP_VS_CONN_TAB_BITS)
590#define IP_VS_CONN_TAB_MASK (IP_VS_CONN_TAB_SIZE - 1)
591
592enum {
593 IP_VS_DIR_INPUT = 0,
594 IP_VS_DIR_OUTPUT,
595 IP_VS_DIR_INPUT_ONLY,
596 IP_VS_DIR_LAST,
597};
598
599extern struct ip_vs_conn *ip_vs_conn_in_get
Al Viro014d7302006-09-28 14:29:52 -0700600(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
Julian Anastasov87375ab2005-09-14 21:08:51 -0700601extern struct ip_vs_conn *ip_vs_ct_in_get
Al Viro014d7302006-09-28 14:29:52 -0700602(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603extern struct ip_vs_conn *ip_vs_conn_out_get
Al Viro014d7302006-09-28 14:29:52 -0700604(int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/* put back the conn without restarting its timer */
607static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
608{
609 atomic_dec(&cp->refcnt);
610}
611extern void ip_vs_conn_put(struct ip_vs_conn *cp);
Al Viro014d7302006-09-28 14:29:52 -0700612extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614extern struct ip_vs_conn *
Al Viro014d7302006-09-28 14:29:52 -0700615ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
616 __be32 daddr, __be16 dport, unsigned flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct ip_vs_dest *dest);
618extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
619
620extern const char * ip_vs_state_name(__u16 proto, int state);
621
622extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
623extern int ip_vs_check_template(struct ip_vs_conn *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624extern void ip_vs_random_dropentry(void);
625extern int ip_vs_conn_init(void);
626extern void ip_vs_conn_cleanup(void);
627
628static inline void ip_vs_control_del(struct ip_vs_conn *cp)
629{
630 struct ip_vs_conn *ctl_cp = cp->control;
631 if (!ctl_cp) {
632 IP_VS_ERR("request control DEL for uncontrolled: "
633 "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
634 NIPQUAD(cp->caddr),ntohs(cp->cport),
635 NIPQUAD(cp->vaddr),ntohs(cp->vport));
636 return;
637 }
638
639 IP_VS_DBG(7, "DELeting control for: "
640 "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
641 NIPQUAD(cp->caddr),ntohs(cp->cport),
642 NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
643
644 cp->control = NULL;
645 if (atomic_read(&ctl_cp->n_control) == 0) {
646 IP_VS_ERR("BUG control DEL with n=0 : "
647 "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
648 NIPQUAD(cp->caddr),ntohs(cp->cport),
649 NIPQUAD(cp->vaddr),ntohs(cp->vport));
650 return;
651 }
652 atomic_dec(&ctl_cp->n_control);
653}
654
655static inline void
656ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
657{
658 if (cp->control) {
659 IP_VS_ERR("request control ADD for already controlled: "
660 "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
661 NIPQUAD(cp->caddr),ntohs(cp->cport),
662 NIPQUAD(cp->vaddr),ntohs(cp->vport));
663 ip_vs_control_del(cp);
664 }
665
666 IP_VS_DBG(7, "ADDing control for: "
667 "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
668 NIPQUAD(cp->caddr),ntohs(cp->cport),
669 NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
670
671 cp->control = ctl_cp;
672 atomic_inc(&ctl_cp->n_control);
673}
674
675
676/*
677 * IPVS application functions
678 * (from ip_vs_app.c)
679 */
680#define IP_VS_APP_MAX_PORTS 8
681extern int register_ip_vs_app(struct ip_vs_app *app);
682extern void unregister_ip_vs_app(struct ip_vs_app *app);
683extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
684extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
685extern int
686register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
687extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
688extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
689
Herbert Xu3db05fe2007-10-15 00:53:15 -0700690extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
691extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
Al Virodd0fc662005-10-07 07:46:04 +0100692extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 char *o_buf, int o_len, char *n_buf, int n_len);
694extern int ip_vs_app_init(void);
695extern void ip_vs_app_cleanup(void);
696
697
698/*
699 * IPVS protocol functions (from ip_vs_proto.c)
700 */
701extern int ip_vs_protocol_init(void);
702extern void ip_vs_protocol_cleanup(void);
703extern void ip_vs_protocol_timeout_change(int flags);
704extern int *ip_vs_create_timeout_table(int *table, int size);
705extern int
706ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
707extern void
708ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
709 int offset, const char *msg);
710
711extern struct ip_vs_protocol ip_vs_protocol_tcp;
712extern struct ip_vs_protocol ip_vs_protocol_udp;
713extern struct ip_vs_protocol ip_vs_protocol_icmp;
714extern struct ip_vs_protocol ip_vs_protocol_esp;
715extern struct ip_vs_protocol ip_vs_protocol_ah;
716
717
718/*
719 * Registering/unregistering scheduler functions
720 * (from ip_vs_sched.c)
721 */
722extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
723extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
724extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
725 struct ip_vs_scheduler *scheduler);
726extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
727extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
728extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
729extern struct ip_vs_conn *
730ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
731extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
732 struct ip_vs_protocol *pp);
733
734
735/*
736 * IPVS control data and functions (from ip_vs_ctl.c)
737 */
738extern int sysctl_ip_vs_cache_bypass;
739extern int sysctl_ip_vs_expire_nodest_conn;
740extern int sysctl_ip_vs_expire_quiescent_template;
741extern int sysctl_ip_vs_sync_threshold[2];
742extern int sysctl_ip_vs_nat_icmp_send;
743extern struct ip_vs_stats ip_vs_stats;
Sven Wegener5587da52008-08-10 18:24:40 +0000744extern const struct ctl_path net_vs_ctl_path[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746extern struct ip_vs_service *
Al Viro014d7302006-09-28 14:29:52 -0700747ip_vs_service_get(__u32 fwmark, __u16 protocol, __be32 vaddr, __be16 vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749static inline void ip_vs_service_put(struct ip_vs_service *svc)
750{
751 atomic_dec(&svc->usecnt);
752}
753
754extern struct ip_vs_dest *
Al Viro014d7302006-09-28 14:29:52 -0700755ip_vs_lookup_real_service(__u16 protocol, __be32 daddr, __be16 dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756extern int ip_vs_use_count_inc(void);
757extern void ip_vs_use_count_dec(void);
758extern int ip_vs_control_init(void);
759extern void ip_vs_control_cleanup(void);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800760extern struct ip_vs_dest *
761ip_vs_find_dest(__be32 daddr, __be16 dport,
762 __be32 vaddr, __be16 vport, __u16 protocol);
763extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765
766/*
767 * IPVS sync daemon data and function prototypes
768 * (from ip_vs_sync.c)
769 */
770extern volatile int ip_vs_sync_state;
771extern volatile int ip_vs_master_syncid;
772extern volatile int ip_vs_backup_syncid;
773extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
774extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
775extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
776extern int stop_sync_thread(int state);
777extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
778
779
780/*
781 * IPVS rate estimator prototypes (from ip_vs_est.c)
782 */
Sven Wegenera919cf42008-08-14 00:47:16 +0200783extern int ip_vs_estimator_init(void);
784extern void ip_vs_estimator_cleanup(void);
Sven Wegener3a14a312008-08-10 18:24:41 +0000785extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
787extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
788
789/*
790 * Various IPVS packet transmitters (from ip_vs_xmit.c)
791 */
792extern int ip_vs_null_xmit
793(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
794extern int ip_vs_bypass_xmit
795(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
796extern int ip_vs_nat_xmit
797(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
798extern int ip_vs_tunnel_xmit
799(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
800extern int ip_vs_dr_xmit
801(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
802extern int ip_vs_icmp_xmit
803(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
804extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
805
806
807/*
808 * This is a simple mechanism to ignore packets when
809 * we are loaded. Just set ip_vs_drop_rate to 'n' and
810 * we start to drop 1/rate of the packets
811 */
812extern int ip_vs_drop_rate;
813extern int ip_vs_drop_counter;
814
815static __inline__ int ip_vs_todrop(void)
816{
817 if (!ip_vs_drop_rate) return 0;
818 if (--ip_vs_drop_counter > 0) return 0;
819 ip_vs_drop_counter = ip_vs_drop_rate;
820 return 1;
821}
822
823/*
824 * ip_vs_fwd_tag returns the forwarding tag of the connection
825 */
826#define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
827
Adrian Bunk732db652005-09-01 17:40:26 -0700828static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 char fwd;
831
832 switch (IP_VS_FWD_METHOD(cp)) {
833 case IP_VS_CONN_F_MASQ:
834 fwd = 'M'; break;
835 case IP_VS_CONN_F_LOCALNODE:
836 fwd = 'L'; break;
837 case IP_VS_CONN_F_TUNNEL:
838 fwd = 'T'; break;
839 case IP_VS_CONN_F_DROUTE:
840 fwd = 'R'; break;
841 case IP_VS_CONN_F_BYPASS:
842 fwd = 'B'; break;
843 default:
844 fwd = '?'; break;
845 }
846 return fwd;
847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
850 struct ip_vs_conn *cp, int dir);
851
Al Virob1550f22006-11-14 21:37:50 -0800852extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Al Virof9214b22006-11-16 02:41:18 -0800854static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Al Virof9214b22006-11-16 02:41:18 -0800856 __be32 diff[2] = { ~old, new };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Al Virof9214b22006-11-16 02:41:18 -0800858 return csum_partial((char *) diff, sizeof(diff), oldsum);
859}
860
861static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
862{
863 __be16 diff[2] = { ~old, new };
864
865 return csum_partial((char *) diff, sizeof(diff), oldsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868#endif /* __KERNEL__ */
869
Julius Volzbc4768e2008-07-31 20:45:24 -0700870#endif /* _NET_IP_VS_H */