blob: 6a6692067092abb12bfee93c7ee6b96827e79097 [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 */
KOVACS Krisztian1668e012008-10-01 07:33:10 -070025#include <linux/ip.h>
Julius Volze7ade462008-09-02 15:55:33 +020026#include <linux/ipv6.h> /* for struct ipv6hdr */
27#include <net/ipv6.h> /* for ipv6_addr_copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Julius Volz64aae3c2008-09-02 15:55:34 +020029struct ip_vs_iphdr {
30 int len;
31 __u8 protocol;
32 union nf_inet_addr saddr;
33 union nf_inet_addr daddr;
34};
35
36static inline void
37ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
38{
39#ifdef CONFIG_IP_VS_IPV6
40 if (af == AF_INET6) {
41 const struct ipv6hdr *iph = nh;
42 iphdr->len = sizeof(struct ipv6hdr);
43 iphdr->protocol = iph->nexthdr;
44 ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
45 ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
46 } else
47#endif
48 {
49 const struct iphdr *iph = nh;
50 iphdr->len = iph->ihl * 4;
51 iphdr->protocol = iph->protocol;
52 iphdr->saddr.ip = iph->saddr;
53 iphdr->daddr.ip = iph->daddr;
54 }
55}
56
57static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
58 const union nf_inet_addr *src)
59{
60#ifdef CONFIG_IP_VS_IPV6
61 if (af == AF_INET6)
62 ipv6_addr_copy(&dst->in6, &src->in6);
63 else
64#endif
65 dst->ip = src->ip;
66}
67
68static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
69 const union nf_inet_addr *b)
70{
71#ifdef CONFIG_IP_VS_IPV6
72 if (af == AF_INET6)
73 return ipv6_addr_equal(&a->in6, &b->in6);
74#endif
75 return a->ip == b->ip;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#ifdef CONFIG_IP_VS_DEBUG
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020079#include <linux/net.h>
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081extern int ip_vs_get_debug_level(void);
Julius Volzc842a3a2008-09-02 15:55:35 +020082
83static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
84 const union nf_inet_addr *addr,
85 int *idx)
86{
87 int len;
88#ifdef CONFIG_IP_VS_IPV6
89 if (af == AF_INET6)
Harvey Harrison0c6ce782008-10-28 16:09:23 -070090 len = snprintf(&buf[*idx], buf_len - *idx, "[%p6]",
91 &addr->in6) + 1;
Julius Volzc842a3a2008-09-02 15:55:35 +020092 else
93#endif
94 len = snprintf(&buf[*idx], buf_len - *idx, NIPQUAD_FMT,
95 NIPQUAD(addr->ip)) + 1;
96
97 *idx += len;
98 BUG_ON(*idx > buf_len + 1);
99 return &buf[*idx - len];
100}
101
102#define IP_VS_DBG_BUF(level, msg...) \
103 do { \
104 char ip_vs_dbg_buf[160]; \
105 int ip_vs_dbg_idx = 0; \
106 if (level <= ip_vs_get_debug_level()) \
107 printk(KERN_DEBUG "IPVS: " msg); \
108 } while (0)
109#define IP_VS_ERR_BUF(msg...) \
110 do { \
111 char ip_vs_dbg_buf[160]; \
112 int ip_vs_dbg_idx = 0; \
113 printk(KERN_ERR "IPVS: " msg); \
114 } while (0)
115
116/* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
117#define IP_VS_DBG_ADDR(af, addr) \
118 ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
119 sizeof(ip_vs_dbg_buf), addr, \
120 &ip_vs_dbg_idx)
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define IP_VS_DBG(level, msg...) \
123 do { \
124 if (level <= ip_vs_get_debug_level()) \
125 printk(KERN_DEBUG "IPVS: " msg); \
126 } while (0)
127#define IP_VS_DBG_RL(msg...) \
128 do { \
129 if (net_ratelimit()) \
130 printk(KERN_DEBUG "IPVS: " msg); \
131 } while (0)
132#define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) \
133 do { \
134 if (level <= ip_vs_get_debug_level()) \
135 pp->debug_packet(pp, skb, ofs, msg); \
136 } while (0)
137#define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) \
138 do { \
139 if (level <= ip_vs_get_debug_level() && \
140 net_ratelimit()) \
141 pp->debug_packet(pp, skb, ofs, msg); \
142 } while (0)
143#else /* NO DEBUGGING at ALL */
Julius Volzc842a3a2008-09-02 15:55:35 +0200144#define IP_VS_DBG_BUF(level, msg...) do {} while (0)
145#define IP_VS_ERR_BUF(msg...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define IP_VS_DBG(level, msg...) do {} while (0)
147#define IP_VS_DBG_RL(msg...) do {} while (0)
148#define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) do {} while (0)
149#define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) do {} while (0)
150#endif
151
152#define IP_VS_BUG() BUG()
153#define IP_VS_ERR(msg...) printk(KERN_ERR "IPVS: " msg)
154#define IP_VS_INFO(msg...) printk(KERN_INFO "IPVS: " msg)
155#define IP_VS_WARNING(msg...) \
156 printk(KERN_WARNING "IPVS: " msg)
157#define IP_VS_ERR_RL(msg...) \
158 do { \
159 if (net_ratelimit()) \
160 printk(KERN_ERR "IPVS: " msg); \
161 } while (0)
162
163#ifdef CONFIG_IP_VS_DEBUG
164#define EnterFunction(level) \
165 do { \
166 if (level <= ip_vs_get_debug_level()) \
167 printk(KERN_DEBUG "Enter: %s, %s line %i\n", \
Harvey Harrisond5c003b2008-10-15 22:01:24 -0700168 __func__, __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 } while (0)
170#define LeaveFunction(level) \
171 do { \
172 if (level <= ip_vs_get_debug_level()) \
173 printk(KERN_DEBUG "Leave: %s, %s line %i\n", \
Harvey Harrisond5c003b2008-10-15 22:01:24 -0700174 __func__, __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 } while (0)
176#else
177#define EnterFunction(level) do {} while (0)
178#define LeaveFunction(level) do {} while (0)
179#endif
180
181#define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
182
183
184/*
185 * The port number of FTP service (in network order).
186 */
187#define FTPPORT __constant_htons(21)
188#define FTPDATA __constant_htons(20)
189
190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * TCP State Values
192 */
193enum {
194 IP_VS_TCP_S_NONE = 0,
195 IP_VS_TCP_S_ESTABLISHED,
196 IP_VS_TCP_S_SYN_SENT,
197 IP_VS_TCP_S_SYN_RECV,
198 IP_VS_TCP_S_FIN_WAIT,
199 IP_VS_TCP_S_TIME_WAIT,
200 IP_VS_TCP_S_CLOSE,
201 IP_VS_TCP_S_CLOSE_WAIT,
202 IP_VS_TCP_S_LAST_ACK,
203 IP_VS_TCP_S_LISTEN,
204 IP_VS_TCP_S_SYNACK,
205 IP_VS_TCP_S_LAST
206};
207
208/*
209 * UDP State Values
210 */
211enum {
212 IP_VS_UDP_S_NORMAL,
213 IP_VS_UDP_S_LAST,
214};
215
216/*
217 * ICMP State Values
218 */
219enum {
220 IP_VS_ICMP_S_NORMAL,
221 IP_VS_ICMP_S_LAST,
222};
223
224/*
225 * Delta sequence info structure
226 * Each ip_vs_conn has 2 (output AND input seq. changes).
227 * Only used in the VS/NAT.
228 */
229struct ip_vs_seq {
230 __u32 init_seq; /* Add delta from this seq */
231 __u32 delta; /* Delta in sequence numbers */
232 __u32 previous_delta; /* Delta in sequence numbers
233 before last resized pkt */
234};
235
236
237/*
Sven Wegener3a14a3132008-08-10 18:24:41 +0000238 * IPVS statistics objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
Sven Wegener3a14a3132008-08-10 18:24:41 +0000240struct ip_vs_estimator {
241 struct list_head list;
242
243 u64 last_inbytes;
244 u64 last_outbytes;
245 u32 last_conns;
246 u32 last_inpkts;
247 u32 last_outpkts;
248
249 u32 cps;
250 u32 inpps;
251 u32 outpps;
252 u32 inbps;
253 u32 outbps;
254};
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256struct ip_vs_stats
257{
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200258 struct ip_vs_stats_user ustats; /* statistics */
259 struct ip_vs_estimator est; /* estimator */
Sven Wegener3a14a3132008-08-10 18:24:41 +0000260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 spinlock_t lock; /* spin lock */
262};
263
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200264struct dst_entry;
265struct iphdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266struct ip_vs_conn;
267struct ip_vs_app;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200268struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270struct ip_vs_protocol {
271 struct ip_vs_protocol *next;
272 char *name;
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700273 u16 protocol;
274 u16 num_states;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int dont_defrag;
276 atomic_t appcnt; /* counter of proto app incs */
277 int *timeout_table; /* protocol timeout table */
278
279 void (*init)(struct ip_vs_protocol *pp);
280
281 void (*exit)(struct ip_vs_protocol *pp);
282
Julius Volz51ef3482008-09-02 15:55:40 +0200283 int (*conn_schedule)(int af, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct ip_vs_protocol *pp,
285 int *verdict, struct ip_vs_conn **cpp);
286
287 struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +0200288 (*conn_in_get)(int af,
289 const struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct ip_vs_protocol *pp,
Julius Volz51ef3482008-09-02 15:55:40 +0200291 const struct ip_vs_iphdr *iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 unsigned int proto_off,
293 int inverse);
294
295 struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +0200296 (*conn_out_get)(int af,
297 const struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct ip_vs_protocol *pp,
Julius Volz51ef3482008-09-02 15:55:40 +0200299 const struct ip_vs_iphdr *iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unsigned int proto_off,
301 int inverse);
302
Herbert Xu3db05fe2007-10-15 00:53:15 -0700303 int (*snat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
305
Herbert Xu3db05fe2007-10-15 00:53:15 -0700306 int (*dnat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
308
Julius Volz51ef3482008-09-02 15:55:40 +0200309 int (*csum_check)(int af, struct sk_buff *skb,
310 struct ip_vs_protocol *pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 const char *(*state_name)(int state);
313
314 int (*state_transition)(struct ip_vs_conn *cp, int direction,
315 const struct sk_buff *skb,
316 struct ip_vs_protocol *pp);
317
318 int (*register_app)(struct ip_vs_app *inc);
319
320 void (*unregister_app)(struct ip_vs_app *inc);
321
322 int (*app_conn_bind)(struct ip_vs_conn *cp);
323
324 void (*debug_packet)(struct ip_vs_protocol *pp,
325 const struct sk_buff *skb,
326 int offset,
327 const char *msg);
328
329 void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
330
331 int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
332};
333
334extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
335
336/*
337 * IP_VS structure allocated for each dynamically scheduled connection
338 */
339struct ip_vs_conn {
340 struct list_head c_list; /* hashed list heads */
341
342 /* Protocol, addresses and port numbers */
Julius Volze7ade462008-09-02 15:55:33 +0200343 u16 af; /* address family */
344 union nf_inet_addr caddr; /* client address */
345 union nf_inet_addr vaddr; /* virtual address */
346 union nf_inet_addr daddr; /* destination address */
Al Viro014d7302006-09-28 14:29:52 -0700347 __be16 cport;
348 __be16 vport;
349 __be16 dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 __u16 protocol; /* Which protocol (TCP/UDP) */
351
352 /* counter and timer */
353 atomic_t refcnt; /* reference count */
354 struct timer_list timer; /* Expiration timer */
355 volatile unsigned long timeout; /* timeout */
356
357 /* Flags and state transition */
358 spinlock_t lock; /* lock for state transition */
359 volatile __u16 flags; /* status flags */
360 volatile __u16 state; /* state info */
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -0800361 volatile __u16 old_state; /* old state, to be used for
362 * state transition triggerd
363 * synchronization
364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* Control members */
367 struct ip_vs_conn *control; /* Master control connection */
368 atomic_t n_control; /* Number of controlled ones */
369 struct ip_vs_dest *dest; /* real server */
370 atomic_t in_pkts; /* incoming packet counter */
371
372 /* packet transmitter for different forwarding methods. If it
373 mangles the packet, it must return NF_DROP or better NF_STOLEN,
374 otherwise this must be changed to a sk_buff **.
375 */
376 int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
377 struct ip_vs_protocol *pp);
378
379 /* Note: we can group the following members into a structure,
380 in order to save more space, and the following members are
381 only used in VS/NAT anyway */
382 struct ip_vs_app *app; /* bound ip_vs_app object */
383 void *app_data; /* Application private data */
384 struct ip_vs_seq in_seq; /* incoming seq. struct */
385 struct ip_vs_seq out_seq; /* outgoing seq. struct */
386};
387
388
389/*
Julius Volzc860c6b2008-09-02 15:55:36 +0200390 * Extended internal versions of struct ip_vs_service_user and
391 * ip_vs_dest_user for IPv6 support.
392 *
393 * We need these to conveniently pass around service and destination
394 * options, but unfortunately, we also need to keep the old definitions to
395 * maintain userspace backwards compatibility for the setsockopt interface.
396 */
397struct ip_vs_service_user_kern {
398 /* virtual service addresses */
399 u16 af;
400 u16 protocol;
401 union nf_inet_addr addr; /* virtual ip address */
402 u16 port;
403 u32 fwmark; /* firwall mark of service */
404
405 /* virtual service options */
406 char *sched_name;
407 unsigned flags; /* virtual service flags */
408 unsigned timeout; /* persistent timeout in sec */
409 u32 netmask; /* persistent netmask */
410};
411
412
413struct ip_vs_dest_user_kern {
414 /* destination server address */
415 union nf_inet_addr addr;
416 u16 port;
417
418 /* real server options */
419 unsigned conn_flags; /* connection flags */
420 int weight; /* destination weight */
421
422 /* thresholds for active connections */
423 u32 u_threshold; /* upper threshold */
424 u32 l_threshold; /* lower threshold */
425};
426
427
428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * The information about the virtual service offered to the net
430 * and the forwarding entries
431 */
432struct ip_vs_service {
433 struct list_head s_list; /* for normal service table */
434 struct list_head f_list; /* for fwmark-based service table */
435 atomic_t refcnt; /* reference counter */
436 atomic_t usecnt; /* use counter */
437
Julius Volze7ade462008-09-02 15:55:33 +0200438 u16 af; /* address family */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200440 union nf_inet_addr addr; /* IP address for virtual service */
Al Viro014d7302006-09-28 14:29:52 -0700441 __be16 port; /* port number for the service */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 __u32 fwmark; /* firewall mark of the service */
443 unsigned flags; /* service status flags */
444 unsigned timeout; /* persistent timeout in ticks */
Al Viro014d7302006-09-28 14:29:52 -0700445 __be32 netmask; /* grouping granularity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 struct list_head destinations; /* real server d-linked list */
448 __u32 num_dests; /* number of servers */
449 struct ip_vs_stats stats; /* statistics for the service */
450 struct ip_vs_app *inc; /* bind conns to this app inc */
451
452 /* for scheduling */
453 struct ip_vs_scheduler *scheduler; /* bound scheduler object */
454 rwlock_t sched_lock; /* lock sched_data */
455 void *sched_data; /* scheduler application data */
456};
457
458
459/*
460 * The real server destination forwarding entry
461 * with ip address, port number, and so on.
462 */
463struct ip_vs_dest {
464 struct list_head n_list; /* for the dests in the service */
465 struct list_head d_list; /* for table with all the dests */
466
Julius Volze7ade462008-09-02 15:55:33 +0200467 u16 af; /* address family */
468 union nf_inet_addr addr; /* IP address of the server */
Al Viro014d7302006-09-28 14:29:52 -0700469 __be16 port; /* port number of the server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 volatile unsigned flags; /* dest status flags */
471 atomic_t conn_flags; /* flags to copy to conn */
472 atomic_t weight; /* server weight */
473
474 atomic_t refcnt; /* reference counter */
475 struct ip_vs_stats stats; /* statistics */
476
477 /* connection counters and thresholds */
478 atomic_t activeconns; /* active connections */
479 atomic_t inactconns; /* inactive connections */
480 atomic_t persistconns; /* persistent connections */
481 __u32 u_threshold; /* upper threshold */
482 __u32 l_threshold; /* lower threshold */
483
484 /* for destination cache */
485 spinlock_t dst_lock; /* lock of dst_cache */
486 struct dst_entry *dst_cache; /* destination cache entry */
487 u32 dst_rtos; /* RT_TOS(tos) for dst */
488
489 /* for virtual service */
490 struct ip_vs_service *svc; /* service it belongs to */
491 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200492 union nf_inet_addr vaddr; /* virtual IP address */
Al Viro014d7302006-09-28 14:29:52 -0700493 __be16 vport; /* virtual port number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 __u32 vfwmark; /* firewall mark of service */
495};
496
497
498/*
499 * The scheduler object
500 */
501struct ip_vs_scheduler {
502 struct list_head n_list; /* d-linked list head */
503 char *name; /* scheduler name */
504 atomic_t refcnt; /* reference counter */
505 struct module *module; /* THIS_MODULE/NULL */
Julius Volzb14198f2008-09-02 15:55:39 +0200506#ifdef CONFIG_IP_VS_IPV6
507 int supports_ipv6; /* scheduler has IPv6 support */
508#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* scheduler initializing service */
511 int (*init_service)(struct ip_vs_service *svc);
512 /* scheduling service finish */
513 int (*done_service)(struct ip_vs_service *svc);
514 /* scheduler updating service */
515 int (*update_service)(struct ip_vs_service *svc);
516
517 /* selecting a server from the given service */
518 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
519 const struct sk_buff *skb);
520};
521
522
523/*
524 * The application module object (a.k.a. app incarnation)
525 */
526struct ip_vs_app
527{
528 struct list_head a_list; /* member in app list */
529 int type; /* IP_VS_APP_TYPE_xxx */
530 char *name; /* application module name */
531 __u16 protocol;
532 struct module *module; /* THIS_MODULE/NULL */
533 struct list_head incs_list; /* list of incarnations */
534
535 /* members for application incarnations */
536 struct list_head p_list; /* member in proto app list */
537 struct ip_vs_app *app; /* its real application */
Al Viro014d7302006-09-28 14:29:52 -0700538 __be16 port; /* port number in net order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 atomic_t usecnt; /* usage counter */
540
541 /* output hook: return false if can't linearize. diff set for TCP. */
542 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700543 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* input hook: return false if can't linearize. diff set for TCP. */
546 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700547 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* ip_vs_app initializer */
550 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
551
552 /* ip_vs_app finish */
553 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
554
555
556 /* not used now */
557 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
558 struct ip_vs_protocol *);
559
560 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
561
562 int * timeout_table;
563 int * timeouts;
564 int timeouts_size;
565
566 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
567 int *verdict, struct ip_vs_conn **cpp);
568
569 struct ip_vs_conn *
570 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
571 const struct iphdr *iph, unsigned int proto_off,
572 int inverse);
573
574 struct ip_vs_conn *
575 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
576 const struct iphdr *iph, unsigned int proto_off,
577 int inverse);
578
579 int (*state_transition)(struct ip_vs_conn *cp, int direction,
580 const struct sk_buff *skb,
581 struct ip_vs_app *app);
582
583 void (*timeout_change)(struct ip_vs_app *app, int flags);
584};
585
586
587/*
588 * IPVS core functions
589 * (from ip_vs_core.c)
590 */
591extern const char *ip_vs_proto_name(unsigned proto);
592extern void ip_vs_init_hash_table(struct list_head *table, int rows);
Sven Wegenerafdd6142008-08-10 09:18:01 +0000593#define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#define IP_VS_APP_TYPE_FTP 1
596
597/*
598 * ip_vs_conn handling functions
599 * (from ip_vs_conn.c)
600 */
601
602/*
603 * IPVS connection entry hash table
604 */
605#ifndef CONFIG_IP_VS_TAB_BITS
606#define CONFIG_IP_VS_TAB_BITS 12
607#endif
Sven Wegener2206a3f2008-09-08 13:38:11 +0200608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#define IP_VS_CONN_TAB_BITS CONFIG_IP_VS_TAB_BITS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610#define IP_VS_CONN_TAB_SIZE (1 << IP_VS_CONN_TAB_BITS)
611#define IP_VS_CONN_TAB_MASK (IP_VS_CONN_TAB_SIZE - 1)
612
613enum {
614 IP_VS_DIR_INPUT = 0,
615 IP_VS_DIR_OUTPUT,
616 IP_VS_DIR_INPUT_ONLY,
617 IP_VS_DIR_LAST,
618};
619
620extern struct ip_vs_conn *ip_vs_conn_in_get
Julius Volz28364a52008-09-02 15:55:43 +0200621(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
622 const union nf_inet_addr *d_addr, __be16 d_port);
623
Julian Anastasov87375ab2005-09-14 21:08:51 -0700624extern struct ip_vs_conn *ip_vs_ct_in_get
Julius Volz28364a52008-09-02 15:55:43 +0200625(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
626 const union nf_inet_addr *d_addr, __be16 d_port);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628extern struct ip_vs_conn *ip_vs_conn_out_get
Julius Volz28364a52008-09-02 15:55:43 +0200629(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
630 const union nf_inet_addr *d_addr, __be16 d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/* put back the conn without restarting its timer */
633static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
634{
635 atomic_dec(&cp->refcnt);
636}
637extern void ip_vs_conn_put(struct ip_vs_conn *cp);
Al Viro014d7302006-09-28 14:29:52 -0700638extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640extern struct ip_vs_conn *
Julius Volz28364a52008-09-02 15:55:43 +0200641ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
642 const union nf_inet_addr *vaddr, __be16 vport,
643 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct ip_vs_dest *dest);
645extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
646
647extern const char * ip_vs_state_name(__u16 proto, int state);
648
649extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
650extern int ip_vs_check_template(struct ip_vs_conn *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651extern void ip_vs_random_dropentry(void);
652extern int ip_vs_conn_init(void);
653extern void ip_vs_conn_cleanup(void);
654
655static inline void ip_vs_control_del(struct ip_vs_conn *cp)
656{
657 struct ip_vs_conn *ctl_cp = cp->control;
658 if (!ctl_cp) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200659 IP_VS_ERR_BUF("request control DEL for uncontrolled: "
660 "%s:%d to %s:%d\n",
661 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
662 ntohs(cp->cport),
663 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
664 ntohs(cp->vport));
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667 }
668
Julius Volzcfc78c52008-09-02 15:55:53 +0200669 IP_VS_DBG_BUF(7, "DELeting control for: "
670 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
671 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
672 ntohs(cp->cport),
673 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
674 ntohs(ctl_cp->cport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 cp->control = NULL;
677 if (atomic_read(&ctl_cp->n_control) == 0) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200678 IP_VS_ERR_BUF("BUG control DEL with n=0 : "
679 "%s:%d to %s:%d\n",
680 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
681 ntohs(cp->cport),
682 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
683 ntohs(cp->vport));
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686 }
687 atomic_dec(&ctl_cp->n_control);
688}
689
690static inline void
691ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
692{
693 if (cp->control) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200694 IP_VS_ERR_BUF("request control ADD for already controlled: "
695 "%s:%d to %s:%d\n",
696 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
697 ntohs(cp->cport),
698 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
699 ntohs(cp->vport));
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ip_vs_control_del(cp);
702 }
703
Julius Volzcfc78c52008-09-02 15:55:53 +0200704 IP_VS_DBG_BUF(7, "ADDing control for: "
705 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
706 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
707 ntohs(cp->cport),
708 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
709 ntohs(ctl_cp->cport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 cp->control = ctl_cp;
712 atomic_inc(&ctl_cp->n_control);
713}
714
715
716/*
717 * IPVS application functions
718 * (from ip_vs_app.c)
719 */
720#define IP_VS_APP_MAX_PORTS 8
721extern int register_ip_vs_app(struct ip_vs_app *app);
722extern void unregister_ip_vs_app(struct ip_vs_app *app);
723extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
724extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
725extern int
726register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
727extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
728extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
729
Herbert Xu3db05fe2007-10-15 00:53:15 -0700730extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
731extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
Al Virodd0fc662005-10-07 07:46:04 +0100732extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 char *o_buf, int o_len, char *n_buf, int n_len);
734extern int ip_vs_app_init(void);
735extern void ip_vs_app_cleanup(void);
736
737
738/*
739 * IPVS protocol functions (from ip_vs_proto.c)
740 */
741extern int ip_vs_protocol_init(void);
742extern void ip_vs_protocol_cleanup(void);
743extern void ip_vs_protocol_timeout_change(int flags);
744extern int *ip_vs_create_timeout_table(int *table, int size);
745extern int
746ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
747extern void
748ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
749 int offset, const char *msg);
750
751extern struct ip_vs_protocol ip_vs_protocol_tcp;
752extern struct ip_vs_protocol ip_vs_protocol_udp;
753extern struct ip_vs_protocol ip_vs_protocol_icmp;
754extern struct ip_vs_protocol ip_vs_protocol_esp;
755extern struct ip_vs_protocol ip_vs_protocol_ah;
756
757
758/*
759 * Registering/unregistering scheduler functions
760 * (from ip_vs_sched.c)
761 */
762extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
763extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
764extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
765 struct ip_vs_scheduler *scheduler);
766extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
767extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
768extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
769extern struct ip_vs_conn *
770ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
771extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
772 struct ip_vs_protocol *pp);
773
774
775/*
776 * IPVS control data and functions (from ip_vs_ctl.c)
777 */
778extern int sysctl_ip_vs_cache_bypass;
779extern int sysctl_ip_vs_expire_nodest_conn;
780extern int sysctl_ip_vs_expire_quiescent_template;
781extern int sysctl_ip_vs_sync_threshold[2];
782extern int sysctl_ip_vs_nat_icmp_send;
783extern struct ip_vs_stats ip_vs_stats;
Sven Wegener5587da52008-08-10 18:24:40 +0000784extern const struct ctl_path net_vs_ctl_path[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786extern struct ip_vs_service *
Julius Volz3c2e0502008-09-02 15:55:38 +0200787ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
788 const union nf_inet_addr *vaddr, __be16 vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790static inline void ip_vs_service_put(struct ip_vs_service *svc)
791{
792 atomic_dec(&svc->usecnt);
793}
794
795extern struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200796ip_vs_lookup_real_service(int af, __u16 protocol,
797 const union nf_inet_addr *daddr, __be16 dport);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799extern int ip_vs_use_count_inc(void);
800extern void ip_vs_use_count_dec(void);
801extern int ip_vs_control_init(void);
802extern void ip_vs_control_cleanup(void);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800803extern struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200804ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
805 const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800806extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808
809/*
810 * IPVS sync daemon data and function prototypes
811 * (from ip_vs_sync.c)
812 */
813extern volatile int ip_vs_sync_state;
814extern volatile int ip_vs_master_syncid;
815extern volatile int ip_vs_backup_syncid;
816extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
817extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
818extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
819extern int stop_sync_thread(int state);
820extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
821
822
823/*
824 * IPVS rate estimator prototypes (from ip_vs_est.c)
825 */
Sven Wegenera919cf42008-08-14 00:47:16 +0200826extern int ip_vs_estimator_init(void);
827extern void ip_vs_estimator_cleanup(void);
Sven Wegener3a14a3132008-08-10 18:24:41 +0000828extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
830extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
831
832/*
833 * Various IPVS packet transmitters (from ip_vs_xmit.c)
834 */
835extern int ip_vs_null_xmit
836(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
837extern int ip_vs_bypass_xmit
838(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
839extern int ip_vs_nat_xmit
840(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
841extern int ip_vs_tunnel_xmit
842(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
843extern int ip_vs_dr_xmit
844(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
845extern int ip_vs_icmp_xmit
846(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
847extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
848
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200849#ifdef CONFIG_IP_VS_IPV6
850extern int ip_vs_bypass_xmit_v6
851(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
852extern int ip_vs_nat_xmit_v6
853(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
854extern int ip_vs_tunnel_xmit_v6
855(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
856extern int ip_vs_dr_xmit_v6
857(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
858extern int ip_vs_icmp_xmit_v6
859(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
860 int offset);
861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863/*
864 * This is a simple mechanism to ignore packets when
865 * we are loaded. Just set ip_vs_drop_rate to 'n' and
866 * we start to drop 1/rate of the packets
867 */
868extern int ip_vs_drop_rate;
869extern int ip_vs_drop_counter;
870
871static __inline__ int ip_vs_todrop(void)
872{
873 if (!ip_vs_drop_rate) return 0;
874 if (--ip_vs_drop_counter > 0) return 0;
875 ip_vs_drop_counter = ip_vs_drop_rate;
876 return 1;
877}
878
879/*
880 * ip_vs_fwd_tag returns the forwarding tag of the connection
881 */
882#define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
883
Adrian Bunk732db652005-09-01 17:40:26 -0700884static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 char fwd;
887
888 switch (IP_VS_FWD_METHOD(cp)) {
889 case IP_VS_CONN_F_MASQ:
890 fwd = 'M'; break;
891 case IP_VS_CONN_F_LOCALNODE:
892 fwd = 'L'; break;
893 case IP_VS_CONN_F_TUNNEL:
894 fwd = 'T'; break;
895 case IP_VS_CONN_F_DROUTE:
896 fwd = 'R'; break;
897 case IP_VS_CONN_F_BYPASS:
898 fwd = 'B'; break;
899 default:
900 fwd = '?'; break;
901 }
902 return fwd;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200906 struct ip_vs_conn *cp, int dir);
907
908#ifdef CONFIG_IP_VS_IPV6
909extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
910 struct ip_vs_conn *cp, int dir);
911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Al Virob1550f22006-11-14 21:37:50 -0800913extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Al Virof9214b22006-11-16 02:41:18 -0800915static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Al Virof9214b22006-11-16 02:41:18 -0800917 __be32 diff[2] = { ~old, new };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Al Virof9214b22006-11-16 02:41:18 -0800919 return csum_partial((char *) diff, sizeof(diff), oldsum);
920}
921
Julius Volz0bbdd422008-09-02 15:55:42 +0200922#ifdef CONFIG_IP_VS_IPV6
923static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
924 __wsum oldsum)
925{
926 __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
927 new[3], new[2], new[1], new[0] };
928
929 return csum_partial((char *) diff, sizeof(diff), oldsum);
930}
931#endif
932
Al Virof9214b22006-11-16 02:41:18 -0800933static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
934{
935 __be16 diff[2] = { ~old, new };
936
937 return csum_partial((char *) diff, sizeof(diff), oldsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940#endif /* __KERNEL__ */
941
Julius Volzbc4768e2008-07-31 20:45:24 -0700942#endif /* _NET_IP_VS_H */