blob: 8dc3296b7bea9919ca7d43b149331ce25ecc8ce7 [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 Harrison5b095d9892008-10-29 12:52:50 -070090 len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
Harvey Harrison0c6ce782008-10-28 16:09:23 -070091 &addr->in6) + 1;
Julius Volzc842a3a2008-09-02 15:55:35 +020092 else
93#endif
Harvey Harrison3685f25d2008-10-31 00:56:49 -070094 len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
95 &addr->ip) + 1;
Julius Volzc842a3a2008-09-02 15:55:35 +020096
97 *idx += len;
98 BUG_ON(*idx > buf_len + 1);
99 return &buf[*idx - len];
100}
101
Hannes Eder9aada7a2009-07-30 14:29:44 -0700102#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 pr_fmt(msg), ##__VA_ARGS__); \
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 pr_err(msg); \
114 } while (0)
Julius Volzc842a3a2008-09-02 15:55:35 +0200115
116/* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
Hannes Eder9aada7a2009-07-30 14:29:44 -0700117#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)
Julius Volzc842a3a2008-09-02 15:55:35 +0200121
Hannes Eder9aada7a2009-07-30 14:29:44 -0700122#define IP_VS_DBG(level, msg, ...) \
123 do { \
124 if (level <= ip_vs_get_debug_level()) \
125 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
126 } while (0)
127#define IP_VS_DBG_RL(msg, ...) \
128 do { \
129 if (net_ratelimit()) \
130 printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#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()
Hannes Eder1e3e2382009-08-02 11:05:41 +0000153#define IP_VS_ERR_RL(msg, ...) \
Hannes Eder9aada7a2009-07-30 14:29:44 -0700154 do { \
155 if (net_ratelimit()) \
Hannes Eder1e3e2382009-08-02 11:05:41 +0000156 pr_err(msg, ##__VA_ARGS__); \
Hannes Eder9aada7a2009-07-30 14:29:44 -0700157 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159#ifdef CONFIG_IP_VS_DEBUG
160#define EnterFunction(level) \
Hannes Eder9aada7a2009-07-30 14:29:44 -0700161 do { \
162 if (level <= ip_vs_get_debug_level()) \
163 printk(KERN_DEBUG \
164 pr_fmt("Enter: %s, %s line %i\n"), \
165 __func__, __FILE__, __LINE__); \
166 } while (0)
167#define LeaveFunction(level) \
168 do { \
169 if (level <= ip_vs_get_debug_level()) \
170 printk(KERN_DEBUG \
171 pr_fmt("Leave: %s, %s line %i\n"), \
172 __func__, __FILE__, __LINE__); \
173 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#else
175#define EnterFunction(level) do {} while (0)
176#define LeaveFunction(level) do {} while (0)
177#endif
178
179#define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
180
181
182/*
183 * The port number of FTP service (in network order).
184 */
Harvey Harrisonf3a7c662009-02-14 22:58:35 -0800185#define FTPPORT cpu_to_be16(21)
186#define FTPDATA cpu_to_be16(20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * TCP State Values
190 */
191enum {
192 IP_VS_TCP_S_NONE = 0,
193 IP_VS_TCP_S_ESTABLISHED,
194 IP_VS_TCP_S_SYN_SENT,
195 IP_VS_TCP_S_SYN_RECV,
196 IP_VS_TCP_S_FIN_WAIT,
197 IP_VS_TCP_S_TIME_WAIT,
198 IP_VS_TCP_S_CLOSE,
199 IP_VS_TCP_S_CLOSE_WAIT,
200 IP_VS_TCP_S_LAST_ACK,
201 IP_VS_TCP_S_LISTEN,
202 IP_VS_TCP_S_SYNACK,
203 IP_VS_TCP_S_LAST
204};
205
206/*
207 * UDP State Values
208 */
209enum {
210 IP_VS_UDP_S_NORMAL,
211 IP_VS_UDP_S_LAST,
212};
213
214/*
215 * ICMP State Values
216 */
217enum {
218 IP_VS_ICMP_S_NORMAL,
219 IP_VS_ICMP_S_LAST,
220};
221
222/*
223 * Delta sequence info structure
224 * Each ip_vs_conn has 2 (output AND input seq. changes).
225 * Only used in the VS/NAT.
226 */
227struct ip_vs_seq {
228 __u32 init_seq; /* Add delta from this seq */
229 __u32 delta; /* Delta in sequence numbers */
230 __u32 previous_delta; /* Delta in sequence numbers
231 before last resized pkt */
232};
233
234
235/*
Sven Wegener3a14a3132008-08-10 18:24:41 +0000236 * IPVS statistics objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Sven Wegener3a14a3132008-08-10 18:24:41 +0000238struct ip_vs_estimator {
239 struct list_head list;
240
241 u64 last_inbytes;
242 u64 last_outbytes;
243 u32 last_conns;
244 u32 last_inpkts;
245 u32 last_outpkts;
246
247 u32 cps;
248 u32 inpps;
249 u32 outpps;
250 u32 inbps;
251 u32 outbps;
252};
253
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000254struct ip_vs_stats {
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200255 struct ip_vs_stats_user ustats; /* statistics */
256 struct ip_vs_estimator est; /* estimator */
Sven Wegener3a14a3132008-08-10 18:24:41 +0000257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spinlock_t lock; /* spin lock */
259};
260
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200261struct dst_entry;
262struct iphdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263struct ip_vs_conn;
264struct ip_vs_app;
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -0200265struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267struct ip_vs_protocol {
268 struct ip_vs_protocol *next;
269 char *name;
Julian Anastasov2ad17de2008-04-29 03:21:23 -0700270 u16 protocol;
271 u16 num_states;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int dont_defrag;
273 atomic_t appcnt; /* counter of proto app incs */
274 int *timeout_table; /* protocol timeout table */
275
276 void (*init)(struct ip_vs_protocol *pp);
277
278 void (*exit)(struct ip_vs_protocol *pp);
279
Julius Volz51ef3482008-09-02 15:55:40 +0200280 int (*conn_schedule)(int af, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct ip_vs_protocol *pp,
282 int *verdict, struct ip_vs_conn **cpp);
283
284 struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +0200285 (*conn_in_get)(int af,
286 const struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct ip_vs_protocol *pp,
Julius Volz51ef3482008-09-02 15:55:40 +0200288 const struct ip_vs_iphdr *iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned int proto_off,
290 int inverse);
291
292 struct ip_vs_conn *
Julius Volz51ef3482008-09-02 15:55:40 +0200293 (*conn_out_get)(int af,
294 const struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct ip_vs_protocol *pp,
Julius Volz51ef3482008-09-02 15:55:40 +0200296 const struct ip_vs_iphdr *iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned int proto_off,
298 int inverse);
299
Herbert Xu3db05fe2007-10-15 00:53:15 -0700300 int (*snat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
302
Herbert Xu3db05fe2007-10-15 00:53:15 -0700303 int (*dnat_handler)(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
305
Julius Volz51ef3482008-09-02 15:55:40 +0200306 int (*csum_check)(int af, struct sk_buff *skb,
307 struct ip_vs_protocol *pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 const char *(*state_name)(int state);
310
311 int (*state_transition)(struct ip_vs_conn *cp, int direction,
312 const struct sk_buff *skb,
313 struct ip_vs_protocol *pp);
314
315 int (*register_app)(struct ip_vs_app *inc);
316
317 void (*unregister_app)(struct ip_vs_app *inc);
318
319 int (*app_conn_bind)(struct ip_vs_conn *cp);
320
321 void (*debug_packet)(struct ip_vs_protocol *pp,
322 const struct sk_buff *skb,
323 int offset,
324 const char *msg);
325
326 void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
327
328 int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
329};
330
331extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
332
333/*
334 * IP_VS structure allocated for each dynamically scheduled connection
335 */
336struct ip_vs_conn {
337 struct list_head c_list; /* hashed list heads */
338
339 /* Protocol, addresses and port numbers */
Julius Volze7ade462008-09-02 15:55:33 +0200340 u16 af; /* address family */
341 union nf_inet_addr caddr; /* client address */
342 union nf_inet_addr vaddr; /* virtual address */
343 union nf_inet_addr daddr; /* destination address */
Al Viro014d7302006-09-28 14:29:52 -0700344 __be16 cport;
345 __be16 vport;
346 __be16 dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 __u16 protocol; /* Which protocol (TCP/UDP) */
348
349 /* counter and timer */
350 atomic_t refcnt; /* reference count */
351 struct timer_list timer; /* Expiration timer */
352 volatile unsigned long timeout; /* timeout */
353
354 /* Flags and state transition */
355 spinlock_t lock; /* lock for state transition */
356 volatile __u16 flags; /* status flags */
357 volatile __u16 state; /* state info */
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -0800358 volatile __u16 old_state; /* old state, to be used for
359 * state transition triggerd
360 * synchronization
361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Control members */
364 struct ip_vs_conn *control; /* Master control connection */
365 atomic_t n_control; /* Number of controlled ones */
366 struct ip_vs_dest *dest; /* real server */
367 atomic_t in_pkts; /* incoming packet counter */
368
369 /* packet transmitter for different forwarding methods. If it
370 mangles the packet, it must return NF_DROP or better NF_STOLEN,
371 otherwise this must be changed to a sk_buff **.
372 */
373 int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
374 struct ip_vs_protocol *pp);
375
376 /* Note: we can group the following members into a structure,
377 in order to save more space, and the following members are
378 only used in VS/NAT anyway */
379 struct ip_vs_app *app; /* bound ip_vs_app object */
380 void *app_data; /* Application private data */
381 struct ip_vs_seq in_seq; /* incoming seq. struct */
382 struct ip_vs_seq out_seq; /* outgoing seq. struct */
383};
384
385
386/*
Julius Volzc860c6b2008-09-02 15:55:36 +0200387 * Extended internal versions of struct ip_vs_service_user and
388 * ip_vs_dest_user for IPv6 support.
389 *
390 * We need these to conveniently pass around service and destination
391 * options, but unfortunately, we also need to keep the old definitions to
392 * maintain userspace backwards compatibility for the setsockopt interface.
393 */
394struct ip_vs_service_user_kern {
395 /* virtual service addresses */
396 u16 af;
397 u16 protocol;
398 union nf_inet_addr addr; /* virtual ip address */
399 u16 port;
400 u32 fwmark; /* firwall mark of service */
401
402 /* virtual service options */
403 char *sched_name;
404 unsigned flags; /* virtual service flags */
405 unsigned timeout; /* persistent timeout in sec */
406 u32 netmask; /* persistent netmask */
407};
408
409
410struct ip_vs_dest_user_kern {
411 /* destination server address */
412 union nf_inet_addr addr;
413 u16 port;
414
415 /* real server options */
416 unsigned conn_flags; /* connection flags */
417 int weight; /* destination weight */
418
419 /* thresholds for active connections */
420 u32 u_threshold; /* upper threshold */
421 u32 l_threshold; /* lower threshold */
422};
423
424
425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * The information about the virtual service offered to the net
427 * and the forwarding entries
428 */
429struct ip_vs_service {
430 struct list_head s_list; /* for normal service table */
431 struct list_head f_list; /* for fwmark-based service table */
432 atomic_t refcnt; /* reference counter */
433 atomic_t usecnt; /* use counter */
434
Julius Volze7ade462008-09-02 15:55:33 +0200435 u16 af; /* address family */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200437 union nf_inet_addr addr; /* IP address for virtual service */
Al Viro014d7302006-09-28 14:29:52 -0700438 __be16 port; /* port number for the service */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 __u32 fwmark; /* firewall mark of the service */
440 unsigned flags; /* service status flags */
441 unsigned timeout; /* persistent timeout in ticks */
Al Viro014d7302006-09-28 14:29:52 -0700442 __be32 netmask; /* grouping granularity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 struct list_head destinations; /* real server d-linked list */
445 __u32 num_dests; /* number of servers */
446 struct ip_vs_stats stats; /* statistics for the service */
447 struct ip_vs_app *inc; /* bind conns to this app inc */
448
449 /* for scheduling */
450 struct ip_vs_scheduler *scheduler; /* bound scheduler object */
451 rwlock_t sched_lock; /* lock sched_data */
452 void *sched_data; /* scheduler application data */
453};
454
455
456/*
457 * The real server destination forwarding entry
458 * with ip address, port number, and so on.
459 */
460struct ip_vs_dest {
461 struct list_head n_list; /* for the dests in the service */
462 struct list_head d_list; /* for table with all the dests */
463
Julius Volze7ade462008-09-02 15:55:33 +0200464 u16 af; /* address family */
465 union nf_inet_addr addr; /* IP address of the server */
Al Viro014d7302006-09-28 14:29:52 -0700466 __be16 port; /* port number of the server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 volatile unsigned flags; /* dest status flags */
468 atomic_t conn_flags; /* flags to copy to conn */
469 atomic_t weight; /* server weight */
470
471 atomic_t refcnt; /* reference counter */
472 struct ip_vs_stats stats; /* statistics */
473
474 /* connection counters and thresholds */
475 atomic_t activeconns; /* active connections */
476 atomic_t inactconns; /* inactive connections */
477 atomic_t persistconns; /* persistent connections */
478 __u32 u_threshold; /* upper threshold */
479 __u32 l_threshold; /* lower threshold */
480
481 /* for destination cache */
482 spinlock_t dst_lock; /* lock of dst_cache */
483 struct dst_entry *dst_cache; /* destination cache entry */
484 u32 dst_rtos; /* RT_TOS(tos) for dst */
485
486 /* for virtual service */
487 struct ip_vs_service *svc; /* service it belongs to */
488 __u16 protocol; /* which protocol (TCP/UDP) */
Julius Volze7ade462008-09-02 15:55:33 +0200489 union nf_inet_addr vaddr; /* virtual IP address */
Al Viro014d7302006-09-28 14:29:52 -0700490 __be16 vport; /* virtual port number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 __u32 vfwmark; /* firewall mark of service */
492};
493
494
495/*
496 * The scheduler object
497 */
498struct ip_vs_scheduler {
499 struct list_head n_list; /* d-linked list head */
500 char *name; /* scheduler name */
501 atomic_t refcnt; /* reference counter */
502 struct module *module; /* THIS_MODULE/NULL */
503
504 /* scheduler initializing service */
505 int (*init_service)(struct ip_vs_service *svc);
506 /* scheduling service finish */
507 int (*done_service)(struct ip_vs_service *svc);
508 /* scheduler updating service */
509 int (*update_service)(struct ip_vs_service *svc);
510
511 /* selecting a server from the given service */
512 struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
513 const struct sk_buff *skb);
514};
515
516
517/*
518 * The application module object (a.k.a. app incarnation)
519 */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000520struct ip_vs_app {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct list_head a_list; /* member in app list */
522 int type; /* IP_VS_APP_TYPE_xxx */
523 char *name; /* application module name */
524 __u16 protocol;
525 struct module *module; /* THIS_MODULE/NULL */
526 struct list_head incs_list; /* list of incarnations */
527
528 /* members for application incarnations */
529 struct list_head p_list; /* member in proto app list */
530 struct ip_vs_app *app; /* its real application */
Al Viro014d7302006-09-28 14:29:52 -0700531 __be16 port; /* port number in net order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 atomic_t usecnt; /* usage counter */
533
534 /* output hook: return false if can't linearize. diff set for TCP. */
535 int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700536 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* input hook: return false if can't linearize. diff set for TCP. */
539 int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700540 struct sk_buff *, int *diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* ip_vs_app initializer */
543 int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
544
545 /* ip_vs_app finish */
546 int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
547
548
549 /* not used now */
550 int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
551 struct ip_vs_protocol *);
552
553 void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
554
555 int * timeout_table;
556 int * timeouts;
557 int timeouts_size;
558
559 int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
560 int *verdict, struct ip_vs_conn **cpp);
561
562 struct ip_vs_conn *
563 (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
564 const struct iphdr *iph, unsigned int proto_off,
565 int inverse);
566
567 struct ip_vs_conn *
568 (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
569 const struct iphdr *iph, unsigned int proto_off,
570 int inverse);
571
572 int (*state_transition)(struct ip_vs_conn *cp, int direction,
573 const struct sk_buff *skb,
574 struct ip_vs_app *app);
575
576 void (*timeout_change)(struct ip_vs_app *app, int flags);
577};
578
579
580/*
581 * IPVS core functions
582 * (from ip_vs_core.c)
583 */
584extern const char *ip_vs_proto_name(unsigned proto);
585extern void ip_vs_init_hash_table(struct list_head *table, int rows);
Sven Wegenerafdd6142008-08-10 09:18:01 +0000586#define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#define IP_VS_APP_TYPE_FTP 1
589
590/*
591 * ip_vs_conn handling functions
592 * (from ip_vs_conn.c)
593 */
594
595/*
596 * IPVS connection entry hash table
597 */
598#ifndef CONFIG_IP_VS_TAB_BITS
599#define CONFIG_IP_VS_TAB_BITS 12
600#endif
Sven Wegener2206a3f2008-09-08 13:38:11 +0200601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#define IP_VS_CONN_TAB_BITS CONFIG_IP_VS_TAB_BITS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603#define IP_VS_CONN_TAB_SIZE (1 << IP_VS_CONN_TAB_BITS)
604#define IP_VS_CONN_TAB_MASK (IP_VS_CONN_TAB_SIZE - 1)
605
606enum {
607 IP_VS_DIR_INPUT = 0,
608 IP_VS_DIR_OUTPUT,
609 IP_VS_DIR_INPUT_ONLY,
610 IP_VS_DIR_LAST,
611};
612
613extern struct ip_vs_conn *ip_vs_conn_in_get
Julius Volz28364a52008-09-02 15:55:43 +0200614(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
615 const union nf_inet_addr *d_addr, __be16 d_port);
616
Julian Anastasov87375ab2005-09-14 21:08:51 -0700617extern struct ip_vs_conn *ip_vs_ct_in_get
Julius Volz28364a52008-09-02 15:55:43 +0200618(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
619 const union nf_inet_addr *d_addr, __be16 d_port);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621extern struct ip_vs_conn *ip_vs_conn_out_get
Julius Volz28364a52008-09-02 15:55:43 +0200622(int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
623 const union nf_inet_addr *d_addr, __be16 d_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625/* put back the conn without restarting its timer */
626static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
627{
628 atomic_dec(&cp->refcnt);
629}
630extern void ip_vs_conn_put(struct ip_vs_conn *cp);
Al Viro014d7302006-09-28 14:29:52 -0700631extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633extern struct ip_vs_conn *
Julius Volz28364a52008-09-02 15:55:43 +0200634ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
635 const union nf_inet_addr *vaddr, __be16 vport,
636 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct ip_vs_dest *dest);
638extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
639
640extern const char * ip_vs_state_name(__u16 proto, int state);
641
642extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
643extern int ip_vs_check_template(struct ip_vs_conn *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644extern void ip_vs_random_dropentry(void);
645extern int ip_vs_conn_init(void);
646extern void ip_vs_conn_cleanup(void);
647
648static inline void ip_vs_control_del(struct ip_vs_conn *cp)
649{
650 struct ip_vs_conn *ctl_cp = cp->control;
651 if (!ctl_cp) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200652 IP_VS_ERR_BUF("request control DEL for uncontrolled: "
653 "%s:%d to %s:%d\n",
654 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
655 ntohs(cp->cport),
656 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
657 ntohs(cp->vport));
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660 }
661
Julius Volzcfc78c52008-09-02 15:55:53 +0200662 IP_VS_DBG_BUF(7, "DELeting control for: "
663 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
664 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
665 ntohs(cp->cport),
666 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
667 ntohs(ctl_cp->cport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 cp->control = NULL;
670 if (atomic_read(&ctl_cp->n_control) == 0) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200671 IP_VS_ERR_BUF("BUG control DEL with n=0 : "
672 "%s:%d to %s:%d\n",
673 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
674 ntohs(cp->cport),
675 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
676 ntohs(cp->vport));
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return;
679 }
680 atomic_dec(&ctl_cp->n_control);
681}
682
683static inline void
684ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
685{
686 if (cp->control) {
Julius Volzcfc78c52008-09-02 15:55:53 +0200687 IP_VS_ERR_BUF("request control ADD for already controlled: "
688 "%s:%d to %s:%d\n",
689 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
690 ntohs(cp->cport),
691 IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
692 ntohs(cp->vport));
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ip_vs_control_del(cp);
695 }
696
Julius Volzcfc78c52008-09-02 15:55:53 +0200697 IP_VS_DBG_BUF(7, "ADDing control for: "
698 "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
699 IP_VS_DBG_ADDR(cp->af, &cp->caddr),
700 ntohs(cp->cport),
701 IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
702 ntohs(ctl_cp->cport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 cp->control = ctl_cp;
705 atomic_inc(&ctl_cp->n_control);
706}
707
708
709/*
710 * IPVS application functions
711 * (from ip_vs_app.c)
712 */
713#define IP_VS_APP_MAX_PORTS 8
714extern int register_ip_vs_app(struct ip_vs_app *app);
715extern void unregister_ip_vs_app(struct ip_vs_app *app);
716extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
717extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
718extern int
719register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
720extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
721extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
722
Herbert Xu3db05fe2007-10-15 00:53:15 -0700723extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
724extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
Al Virodd0fc662005-10-07 07:46:04 +0100725extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 char *o_buf, int o_len, char *n_buf, int n_len);
727extern int ip_vs_app_init(void);
728extern void ip_vs_app_cleanup(void);
729
730
731/*
732 * IPVS protocol functions (from ip_vs_proto.c)
733 */
734extern int ip_vs_protocol_init(void);
735extern void ip_vs_protocol_cleanup(void);
736extern void ip_vs_protocol_timeout_change(int flags);
737extern int *ip_vs_create_timeout_table(int *table, int size);
738extern int
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700739ip_vs_set_state_timeout(int *table, int num, const char *const *names,
740 const char *name, int to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741extern void
742ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
743 int offset, const char *msg);
744
745extern struct ip_vs_protocol ip_vs_protocol_tcp;
746extern struct ip_vs_protocol ip_vs_protocol_udp;
747extern struct ip_vs_protocol ip_vs_protocol_icmp;
748extern struct ip_vs_protocol ip_vs_protocol_esp;
749extern struct ip_vs_protocol ip_vs_protocol_ah;
750
751
752/*
753 * Registering/unregistering scheduler functions
754 * (from ip_vs_sched.c)
755 */
756extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
757extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
758extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
759 struct ip_vs_scheduler *scheduler);
760extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
761extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
762extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
763extern struct ip_vs_conn *
764ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
765extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
766 struct ip_vs_protocol *pp);
767
768
769/*
770 * IPVS control data and functions (from ip_vs_ctl.c)
771 */
772extern int sysctl_ip_vs_cache_bypass;
773extern int sysctl_ip_vs_expire_nodest_conn;
774extern int sysctl_ip_vs_expire_quiescent_template;
775extern int sysctl_ip_vs_sync_threshold[2];
776extern int sysctl_ip_vs_nat_icmp_send;
777extern struct ip_vs_stats ip_vs_stats;
Sven Wegener5587da52008-08-10 18:24:40 +0000778extern const struct ctl_path net_vs_ctl_path[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780extern struct ip_vs_service *
Julius Volz3c2e0502008-09-02 15:55:38 +0200781ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
782 const union nf_inet_addr *vaddr, __be16 vport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784static inline void ip_vs_service_put(struct ip_vs_service *svc)
785{
786 atomic_dec(&svc->usecnt);
787}
788
789extern struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200790ip_vs_lookup_real_service(int af, __u16 protocol,
791 const union nf_inet_addr *daddr, __be16 dport);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793extern int ip_vs_use_count_inc(void);
794extern void ip_vs_use_count_dec(void);
795extern int ip_vs_control_init(void);
796extern void ip_vs_control_cleanup(void);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800797extern struct ip_vs_dest *
Julius Volz7937df12008-09-02 15:55:48 +0200798ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
799 const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
Rumen G. Bogdanovski1e356f92007-11-07 02:35:54 -0800800extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802
803/*
804 * IPVS sync daemon data and function prototypes
805 * (from ip_vs_sync.c)
806 */
807extern volatile int ip_vs_sync_state;
808extern volatile int ip_vs_master_syncid;
809extern volatile int ip_vs_backup_syncid;
810extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
811extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
812extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
813extern int stop_sync_thread(int state);
814extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
815
816
817/*
818 * IPVS rate estimator prototypes (from ip_vs_est.c)
819 */
Sven Wegenera919cf42008-08-14 00:47:16 +0200820extern int ip_vs_estimator_init(void);
821extern void ip_vs_estimator_cleanup(void);
Sven Wegener3a14a3132008-08-10 18:24:41 +0000822extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
824extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
825
826/*
827 * Various IPVS packet transmitters (from ip_vs_xmit.c)
828 */
829extern int ip_vs_null_xmit
830(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
831extern int ip_vs_bypass_xmit
832(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
833extern int ip_vs_nat_xmit
834(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
835extern int ip_vs_tunnel_xmit
836(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
837extern int ip_vs_dr_xmit
838(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
839extern int ip_vs_icmp_xmit
840(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
841extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
842
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200843#ifdef CONFIG_IP_VS_IPV6
844extern int ip_vs_bypass_xmit_v6
845(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
846extern int ip_vs_nat_xmit_v6
847(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
848extern int ip_vs_tunnel_xmit_v6
849(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
850extern int ip_vs_dr_xmit_v6
851(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
852extern int ip_vs_icmp_xmit_v6
853(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
854 int offset);
855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857/*
858 * This is a simple mechanism to ignore packets when
859 * we are loaded. Just set ip_vs_drop_rate to 'n' and
860 * we start to drop 1/rate of the packets
861 */
862extern int ip_vs_drop_rate;
863extern int ip_vs_drop_counter;
864
865static __inline__ int ip_vs_todrop(void)
866{
867 if (!ip_vs_drop_rate) return 0;
868 if (--ip_vs_drop_counter > 0) return 0;
869 ip_vs_drop_counter = ip_vs_drop_rate;
870 return 1;
871}
872
873/*
874 * ip_vs_fwd_tag returns the forwarding tag of the connection
875 */
876#define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
877
Adrian Bunk732db652005-09-01 17:40:26 -0700878static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 char fwd;
881
882 switch (IP_VS_FWD_METHOD(cp)) {
883 case IP_VS_CONN_F_MASQ:
884 fwd = 'M'; break;
885 case IP_VS_CONN_F_LOCALNODE:
886 fwd = 'L'; break;
887 case IP_VS_CONN_F_TUNNEL:
888 fwd = 'T'; break;
889 case IP_VS_CONN_F_DROUTE:
890 fwd = 'R'; break;
891 case IP_VS_CONN_F_BYPASS:
892 fwd = 'B'; break;
893 default:
894 fwd = '?'; break;
895 }
896 return fwd;
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200900 struct ip_vs_conn *cp, int dir);
901
902#ifdef CONFIG_IP_VS_IPV6
903extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
904 struct ip_vs_conn *cp, int dir);
905#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Al Virob1550f22006-11-14 21:37:50 -0800907extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Al Virof9214b22006-11-16 02:41:18 -0800909static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Al Virof9214b22006-11-16 02:41:18 -0800911 __be32 diff[2] = { ~old, new };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Joe Perches07f07572008-11-19 15:44:53 -0800913 return csum_partial(diff, sizeof(diff), oldsum);
Al Virof9214b22006-11-16 02:41:18 -0800914}
915
Julius Volz0bbdd422008-09-02 15:55:42 +0200916#ifdef CONFIG_IP_VS_IPV6
917static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
918 __wsum oldsum)
919{
920 __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
921 new[3], new[2], new[1], new[0] };
922
Joe Perches07f07572008-11-19 15:44:53 -0800923 return csum_partial(diff, sizeof(diff), oldsum);
Julius Volz0bbdd422008-09-02 15:55:42 +0200924}
925#endif
926
Al Virof9214b22006-11-16 02:41:18 -0800927static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
928{
929 __be16 diff[2] = { ~old, new };
930
Joe Perches07f07572008-11-19 15:44:53 -0800931 return csum_partial(diff, sizeof(diff), oldsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934#endif /* __KERNEL__ */
935
Julius Volzbc4768e2008-07-31 20:45:24 -0700936#endif /* _NET_IP_VS_H */