ip_vs.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * IP Virtual Server
  3. * data structure and functionality definitions
  4. */
  5. #ifndef _NET_IP_VS_H
  6. #define _NET_IP_VS_H
  7. #include <linux/ip_vs.h> /* definitions shared with userland */
  8. /* old ipvsadm versions still include this file directly */
  9. #ifdef __KERNEL__
  10. #include <asm/types.h> /* for __uXX types */
  11. #include <linux/sysctl.h> /* for ctl_path */
  12. #include <linux/list.h> /* for struct list_head */
  13. #include <linux/spinlock.h> /* for struct rwlock_t */
  14. #include <asm/atomic.h> /* for struct atomic_t */
  15. #include <linux/compiler.h>
  16. #include <linux/timer.h>
  17. #include <net/checksum.h>
  18. #include <linux/netfilter.h> /* for union nf_inet_addr */
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h> /* for struct ipv6hdr */
  21. #include <net/ipv6.h> /* for ipv6_addr_copy */
  22. struct ip_vs_iphdr {
  23. int len;
  24. __u8 protocol;
  25. union nf_inet_addr saddr;
  26. union nf_inet_addr daddr;
  27. };
  28. static inline void
  29. ip_vs_fill_iphdr(int af, const void *nh, struct ip_vs_iphdr *iphdr)
  30. {
  31. #ifdef CONFIG_IP_VS_IPV6
  32. if (af == AF_INET6) {
  33. const struct ipv6hdr *iph = nh;
  34. iphdr->len = sizeof(struct ipv6hdr);
  35. iphdr->protocol = iph->nexthdr;
  36. ipv6_addr_copy(&iphdr->saddr.in6, &iph->saddr);
  37. ipv6_addr_copy(&iphdr->daddr.in6, &iph->daddr);
  38. } else
  39. #endif
  40. {
  41. const struct iphdr *iph = nh;
  42. iphdr->len = iph->ihl * 4;
  43. iphdr->protocol = iph->protocol;
  44. iphdr->saddr.ip = iph->saddr;
  45. iphdr->daddr.ip = iph->daddr;
  46. }
  47. }
  48. static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
  49. const union nf_inet_addr *src)
  50. {
  51. #ifdef CONFIG_IP_VS_IPV6
  52. if (af == AF_INET6)
  53. ipv6_addr_copy(&dst->in6, &src->in6);
  54. else
  55. #endif
  56. dst->ip = src->ip;
  57. }
  58. static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
  59. const union nf_inet_addr *b)
  60. {
  61. #ifdef CONFIG_IP_VS_IPV6
  62. if (af == AF_INET6)
  63. return ipv6_addr_equal(&a->in6, &b->in6);
  64. #endif
  65. return a->ip == b->ip;
  66. }
  67. #ifdef CONFIG_IP_VS_DEBUG
  68. #include <linux/net.h>
  69. extern int ip_vs_get_debug_level(void);
  70. static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
  71. const union nf_inet_addr *addr,
  72. int *idx)
  73. {
  74. int len;
  75. #ifdef CONFIG_IP_VS_IPV6
  76. if (af == AF_INET6)
  77. len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6]",
  78. &addr->in6) + 1;
  79. else
  80. #endif
  81. len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
  82. &addr->ip) + 1;
  83. *idx += len;
  84. BUG_ON(*idx > buf_len + 1);
  85. return &buf[*idx - len];
  86. }
  87. #define IP_VS_DBG_BUF(level, msg...) \
  88. do { \
  89. char ip_vs_dbg_buf[160]; \
  90. int ip_vs_dbg_idx = 0; \
  91. if (level <= ip_vs_get_debug_level()) \
  92. printk(KERN_DEBUG "IPVS: " msg); \
  93. } while (0)
  94. #define IP_VS_ERR_BUF(msg...) \
  95. do { \
  96. char ip_vs_dbg_buf[160]; \
  97. int ip_vs_dbg_idx = 0; \
  98. printk(KERN_ERR "IPVS: " msg); \
  99. } while (0)
  100. /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
  101. #define IP_VS_DBG_ADDR(af, addr) \
  102. ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
  103. sizeof(ip_vs_dbg_buf), addr, \
  104. &ip_vs_dbg_idx)
  105. #define IP_VS_DBG(level, msg...) \
  106. do { \
  107. if (level <= ip_vs_get_debug_level()) \
  108. printk(KERN_DEBUG "IPVS: " msg); \
  109. } while (0)
  110. #define IP_VS_DBG_RL(msg...) \
  111. do { \
  112. if (net_ratelimit()) \
  113. printk(KERN_DEBUG "IPVS: " msg); \
  114. } while (0)
  115. #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) \
  116. do { \
  117. if (level <= ip_vs_get_debug_level()) \
  118. pp->debug_packet(pp, skb, ofs, msg); \
  119. } while (0)
  120. #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) \
  121. do { \
  122. if (level <= ip_vs_get_debug_level() && \
  123. net_ratelimit()) \
  124. pp->debug_packet(pp, skb, ofs, msg); \
  125. } while (0)
  126. #else /* NO DEBUGGING at ALL */
  127. #define IP_VS_DBG_BUF(level, msg...) do {} while (0)
  128. #define IP_VS_ERR_BUF(msg...) do {} while (0)
  129. #define IP_VS_DBG(level, msg...) do {} while (0)
  130. #define IP_VS_DBG_RL(msg...) do {} while (0)
  131. #define IP_VS_DBG_PKT(level, pp, skb, ofs, msg) do {} while (0)
  132. #define IP_VS_DBG_RL_PKT(level, pp, skb, ofs, msg) do {} while (0)
  133. #endif
  134. #define IP_VS_BUG() BUG()
  135. #define IP_VS_ERR(msg...) printk(KERN_ERR "IPVS: " msg)
  136. #define IP_VS_INFO(msg...) printk(KERN_INFO "IPVS: " msg)
  137. #define IP_VS_WARNING(msg...) \
  138. printk(KERN_WARNING "IPVS: " msg)
  139. #define IP_VS_ERR_RL(msg...) \
  140. do { \
  141. if (net_ratelimit()) \
  142. printk(KERN_ERR "IPVS: " msg); \
  143. } while (0)
  144. #ifdef CONFIG_IP_VS_DEBUG
  145. #define EnterFunction(level) \
  146. do { \
  147. if (level <= ip_vs_get_debug_level()) \
  148. printk(KERN_DEBUG "Enter: %s, %s line %i\n", \
  149. __func__, __FILE__, __LINE__); \
  150. } while (0)
  151. #define LeaveFunction(level) \
  152. do { \
  153. if (level <= ip_vs_get_debug_level()) \
  154. printk(KERN_DEBUG "Leave: %s, %s line %i\n", \
  155. __func__, __FILE__, __LINE__); \
  156. } while (0)
  157. #else
  158. #define EnterFunction(level) do {} while (0)
  159. #define LeaveFunction(level) do {} while (0)
  160. #endif
  161. #define IP_VS_WAIT_WHILE(expr) while (expr) { cpu_relax(); }
  162. /*
  163. * The port number of FTP service (in network order).
  164. */
  165. #define FTPPORT cpu_to_be16(21)
  166. #define FTPDATA cpu_to_be16(20)
  167. /*
  168. * TCP State Values
  169. */
  170. enum {
  171. IP_VS_TCP_S_NONE = 0,
  172. IP_VS_TCP_S_ESTABLISHED,
  173. IP_VS_TCP_S_SYN_SENT,
  174. IP_VS_TCP_S_SYN_RECV,
  175. IP_VS_TCP_S_FIN_WAIT,
  176. IP_VS_TCP_S_TIME_WAIT,
  177. IP_VS_TCP_S_CLOSE,
  178. IP_VS_TCP_S_CLOSE_WAIT,
  179. IP_VS_TCP_S_LAST_ACK,
  180. IP_VS_TCP_S_LISTEN,
  181. IP_VS_TCP_S_SYNACK,
  182. IP_VS_TCP_S_LAST
  183. };
  184. /*
  185. * UDP State Values
  186. */
  187. enum {
  188. IP_VS_UDP_S_NORMAL,
  189. IP_VS_UDP_S_LAST,
  190. };
  191. /*
  192. * ICMP State Values
  193. */
  194. enum {
  195. IP_VS_ICMP_S_NORMAL,
  196. IP_VS_ICMP_S_LAST,
  197. };
  198. /*
  199. * Delta sequence info structure
  200. * Each ip_vs_conn has 2 (output AND input seq. changes).
  201. * Only used in the VS/NAT.
  202. */
  203. struct ip_vs_seq {
  204. __u32 init_seq; /* Add delta from this seq */
  205. __u32 delta; /* Delta in sequence numbers */
  206. __u32 previous_delta; /* Delta in sequence numbers
  207. before last resized pkt */
  208. };
  209. /*
  210. * IPVS statistics objects
  211. */
  212. struct ip_vs_estimator {
  213. struct list_head list;
  214. u64 last_inbytes;
  215. u64 last_outbytes;
  216. u32 last_conns;
  217. u32 last_inpkts;
  218. u32 last_outpkts;
  219. u32 cps;
  220. u32 inpps;
  221. u32 outpps;
  222. u32 inbps;
  223. u32 outbps;
  224. };
  225. struct ip_vs_stats
  226. {
  227. struct ip_vs_stats_user ustats; /* statistics */
  228. struct ip_vs_estimator est; /* estimator */
  229. spinlock_t lock; /* spin lock */
  230. };
  231. struct dst_entry;
  232. struct iphdr;
  233. struct ip_vs_conn;
  234. struct ip_vs_app;
  235. struct sk_buff;
  236. struct ip_vs_protocol {
  237. struct ip_vs_protocol *next;
  238. char *name;
  239. u16 protocol;
  240. u16 num_states;
  241. int dont_defrag;
  242. atomic_t appcnt; /* counter of proto app incs */
  243. int *timeout_table; /* protocol timeout table */
  244. void (*init)(struct ip_vs_protocol *pp);
  245. void (*exit)(struct ip_vs_protocol *pp);
  246. int (*conn_schedule)(int af, struct sk_buff *skb,
  247. struct ip_vs_protocol *pp,
  248. int *verdict, struct ip_vs_conn **cpp);
  249. struct ip_vs_conn *
  250. (*conn_in_get)(int af,
  251. const struct sk_buff *skb,
  252. struct ip_vs_protocol *pp,
  253. const struct ip_vs_iphdr *iph,
  254. unsigned int proto_off,
  255. int inverse);
  256. struct ip_vs_conn *
  257. (*conn_out_get)(int af,
  258. const struct sk_buff *skb,
  259. struct ip_vs_protocol *pp,
  260. const struct ip_vs_iphdr *iph,
  261. unsigned int proto_off,
  262. int inverse);
  263. int (*snat_handler)(struct sk_buff *skb,
  264. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  265. int (*dnat_handler)(struct sk_buff *skb,
  266. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  267. int (*csum_check)(int af, struct sk_buff *skb,
  268. struct ip_vs_protocol *pp);
  269. const char *(*state_name)(int state);
  270. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  271. const struct sk_buff *skb,
  272. struct ip_vs_protocol *pp);
  273. int (*register_app)(struct ip_vs_app *inc);
  274. void (*unregister_app)(struct ip_vs_app *inc);
  275. int (*app_conn_bind)(struct ip_vs_conn *cp);
  276. void (*debug_packet)(struct ip_vs_protocol *pp,
  277. const struct sk_buff *skb,
  278. int offset,
  279. const char *msg);
  280. void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
  281. int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
  282. };
  283. extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
  284. /*
  285. * IP_VS structure allocated for each dynamically scheduled connection
  286. */
  287. struct ip_vs_conn {
  288. struct list_head c_list; /* hashed list heads */
  289. /* Protocol, addresses and port numbers */
  290. u16 af; /* address family */
  291. union nf_inet_addr caddr; /* client address */
  292. union nf_inet_addr vaddr; /* virtual address */
  293. union nf_inet_addr daddr; /* destination address */
  294. __be16 cport;
  295. __be16 vport;
  296. __be16 dport;
  297. __u16 protocol; /* Which protocol (TCP/UDP) */
  298. /* counter and timer */
  299. atomic_t refcnt; /* reference count */
  300. struct timer_list timer; /* Expiration timer */
  301. volatile unsigned long timeout; /* timeout */
  302. /* Flags and state transition */
  303. spinlock_t lock; /* lock for state transition */
  304. volatile __u16 flags; /* status flags */
  305. volatile __u16 state; /* state info */
  306. volatile __u16 old_state; /* old state, to be used for
  307. * state transition triggerd
  308. * synchronization
  309. */
  310. /* Control members */
  311. struct ip_vs_conn *control; /* Master control connection */
  312. atomic_t n_control; /* Number of controlled ones */
  313. struct ip_vs_dest *dest; /* real server */
  314. atomic_t in_pkts; /* incoming packet counter */
  315. /* packet transmitter for different forwarding methods. If it
  316. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  317. otherwise this must be changed to a sk_buff **.
  318. */
  319. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  320. struct ip_vs_protocol *pp);
  321. /* Note: we can group the following members into a structure,
  322. in order to save more space, and the following members are
  323. only used in VS/NAT anyway */
  324. struct ip_vs_app *app; /* bound ip_vs_app object */
  325. void *app_data; /* Application private data */
  326. struct ip_vs_seq in_seq; /* incoming seq. struct */
  327. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  328. };
  329. /*
  330. * Extended internal versions of struct ip_vs_service_user and
  331. * ip_vs_dest_user for IPv6 support.
  332. *
  333. * We need these to conveniently pass around service and destination
  334. * options, but unfortunately, we also need to keep the old definitions to
  335. * maintain userspace backwards compatibility for the setsockopt interface.
  336. */
  337. struct ip_vs_service_user_kern {
  338. /* virtual service addresses */
  339. u16 af;
  340. u16 protocol;
  341. union nf_inet_addr addr; /* virtual ip address */
  342. u16 port;
  343. u32 fwmark; /* firwall mark of service */
  344. /* virtual service options */
  345. char *sched_name;
  346. unsigned flags; /* virtual service flags */
  347. unsigned timeout; /* persistent timeout in sec */
  348. u32 netmask; /* persistent netmask */
  349. };
  350. struct ip_vs_dest_user_kern {
  351. /* destination server address */
  352. union nf_inet_addr addr;
  353. u16 port;
  354. /* real server options */
  355. unsigned conn_flags; /* connection flags */
  356. int weight; /* destination weight */
  357. /* thresholds for active connections */
  358. u32 u_threshold; /* upper threshold */
  359. u32 l_threshold; /* lower threshold */
  360. };
  361. /*
  362. * The information about the virtual service offered to the net
  363. * and the forwarding entries
  364. */
  365. struct ip_vs_service {
  366. struct list_head s_list; /* for normal service table */
  367. struct list_head f_list; /* for fwmark-based service table */
  368. atomic_t refcnt; /* reference counter */
  369. atomic_t usecnt; /* use counter */
  370. u16 af; /* address family */
  371. __u16 protocol; /* which protocol (TCP/UDP) */
  372. union nf_inet_addr addr; /* IP address for virtual service */
  373. __be16 port; /* port number for the service */
  374. __u32 fwmark; /* firewall mark of the service */
  375. unsigned flags; /* service status flags */
  376. unsigned timeout; /* persistent timeout in ticks */
  377. __be32 netmask; /* grouping granularity */
  378. struct list_head destinations; /* real server d-linked list */
  379. __u32 num_dests; /* number of servers */
  380. struct ip_vs_stats stats; /* statistics for the service */
  381. struct ip_vs_app *inc; /* bind conns to this app inc */
  382. /* for scheduling */
  383. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  384. rwlock_t sched_lock; /* lock sched_data */
  385. void *sched_data; /* scheduler application data */
  386. };
  387. /*
  388. * The real server destination forwarding entry
  389. * with ip address, port number, and so on.
  390. */
  391. struct ip_vs_dest {
  392. struct list_head n_list; /* for the dests in the service */
  393. struct list_head d_list; /* for table with all the dests */
  394. u16 af; /* address family */
  395. union nf_inet_addr addr; /* IP address of the server */
  396. __be16 port; /* port number of the server */
  397. volatile unsigned flags; /* dest status flags */
  398. atomic_t conn_flags; /* flags to copy to conn */
  399. atomic_t weight; /* server weight */
  400. atomic_t refcnt; /* reference counter */
  401. struct ip_vs_stats stats; /* statistics */
  402. /* connection counters and thresholds */
  403. atomic_t activeconns; /* active connections */
  404. atomic_t inactconns; /* inactive connections */
  405. atomic_t persistconns; /* persistent connections */
  406. __u32 u_threshold; /* upper threshold */
  407. __u32 l_threshold; /* lower threshold */
  408. /* for destination cache */
  409. spinlock_t dst_lock; /* lock of dst_cache */
  410. struct dst_entry *dst_cache; /* destination cache entry */
  411. u32 dst_rtos; /* RT_TOS(tos) for dst */
  412. /* for virtual service */
  413. struct ip_vs_service *svc; /* service it belongs to */
  414. __u16 protocol; /* which protocol (TCP/UDP) */
  415. union nf_inet_addr vaddr; /* virtual IP address */
  416. __be16 vport; /* virtual port number */
  417. __u32 vfwmark; /* firewall mark of service */
  418. };
  419. /*
  420. * The scheduler object
  421. */
  422. struct ip_vs_scheduler {
  423. struct list_head n_list; /* d-linked list head */
  424. char *name; /* scheduler name */
  425. atomic_t refcnt; /* reference counter */
  426. struct module *module; /* THIS_MODULE/NULL */
  427. /* scheduler initializing service */
  428. int (*init_service)(struct ip_vs_service *svc);
  429. /* scheduling service finish */
  430. int (*done_service)(struct ip_vs_service *svc);
  431. /* scheduler updating service */
  432. int (*update_service)(struct ip_vs_service *svc);
  433. /* selecting a server from the given service */
  434. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  435. const struct sk_buff *skb);
  436. };
  437. /*
  438. * The application module object (a.k.a. app incarnation)
  439. */
  440. struct ip_vs_app
  441. {
  442. struct list_head a_list; /* member in app list */
  443. int type; /* IP_VS_APP_TYPE_xxx */
  444. char *name; /* application module name */
  445. __u16 protocol;
  446. struct module *module; /* THIS_MODULE/NULL */
  447. struct list_head incs_list; /* list of incarnations */
  448. /* members for application incarnations */
  449. struct list_head p_list; /* member in proto app list */
  450. struct ip_vs_app *app; /* its real application */
  451. __be16 port; /* port number in net order */
  452. atomic_t usecnt; /* usage counter */
  453. /* output hook: return false if can't linearize. diff set for TCP. */
  454. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  455. struct sk_buff *, int *diff);
  456. /* input hook: return false if can't linearize. diff set for TCP. */
  457. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  458. struct sk_buff *, int *diff);
  459. /* ip_vs_app initializer */
  460. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  461. /* ip_vs_app finish */
  462. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  463. /* not used now */
  464. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  465. struct ip_vs_protocol *);
  466. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  467. int * timeout_table;
  468. int * timeouts;
  469. int timeouts_size;
  470. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  471. int *verdict, struct ip_vs_conn **cpp);
  472. struct ip_vs_conn *
  473. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  474. const struct iphdr *iph, unsigned int proto_off,
  475. int inverse);
  476. struct ip_vs_conn *
  477. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  478. const struct iphdr *iph, unsigned int proto_off,
  479. int inverse);
  480. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  481. const struct sk_buff *skb,
  482. struct ip_vs_app *app);
  483. void (*timeout_change)(struct ip_vs_app *app, int flags);
  484. };
  485. /*
  486. * IPVS core functions
  487. * (from ip_vs_core.c)
  488. */
  489. extern const char *ip_vs_proto_name(unsigned proto);
  490. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  491. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  492. #define IP_VS_APP_TYPE_FTP 1
  493. /*
  494. * ip_vs_conn handling functions
  495. * (from ip_vs_conn.c)
  496. */
  497. /*
  498. * IPVS connection entry hash table
  499. */
  500. #ifndef CONFIG_IP_VS_TAB_BITS
  501. #define CONFIG_IP_VS_TAB_BITS 12
  502. #endif
  503. #define IP_VS_CONN_TAB_BITS CONFIG_IP_VS_TAB_BITS
  504. #define IP_VS_CONN_TAB_SIZE (1 << IP_VS_CONN_TAB_BITS)
  505. #define IP_VS_CONN_TAB_MASK (IP_VS_CONN_TAB_SIZE - 1)
  506. enum {
  507. IP_VS_DIR_INPUT = 0,
  508. IP_VS_DIR_OUTPUT,
  509. IP_VS_DIR_INPUT_ONLY,
  510. IP_VS_DIR_LAST,
  511. };
  512. extern struct ip_vs_conn *ip_vs_conn_in_get
  513. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  514. const union nf_inet_addr *d_addr, __be16 d_port);
  515. extern struct ip_vs_conn *ip_vs_ct_in_get
  516. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  517. const union nf_inet_addr *d_addr, __be16 d_port);
  518. extern struct ip_vs_conn *ip_vs_conn_out_get
  519. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  520. const union nf_inet_addr *d_addr, __be16 d_port);
  521. /* put back the conn without restarting its timer */
  522. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  523. {
  524. atomic_dec(&cp->refcnt);
  525. }
  526. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  527. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  528. extern struct ip_vs_conn *
  529. ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
  530. const union nf_inet_addr *vaddr, __be16 vport,
  531. const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
  532. struct ip_vs_dest *dest);
  533. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  534. extern const char * ip_vs_state_name(__u16 proto, int state);
  535. extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  536. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  537. extern void ip_vs_random_dropentry(void);
  538. extern int ip_vs_conn_init(void);
  539. extern void ip_vs_conn_cleanup(void);
  540. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  541. {
  542. struct ip_vs_conn *ctl_cp = cp->control;
  543. if (!ctl_cp) {
  544. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  545. "%s:%d to %s:%d\n",
  546. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  547. ntohs(cp->cport),
  548. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  549. ntohs(cp->vport));
  550. return;
  551. }
  552. IP_VS_DBG_BUF(7, "DELeting control for: "
  553. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  554. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  555. ntohs(cp->cport),
  556. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  557. ntohs(ctl_cp->cport));
  558. cp->control = NULL;
  559. if (atomic_read(&ctl_cp->n_control) == 0) {
  560. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  561. "%s:%d to %s:%d\n",
  562. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  563. ntohs(cp->cport),
  564. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  565. ntohs(cp->vport));
  566. return;
  567. }
  568. atomic_dec(&ctl_cp->n_control);
  569. }
  570. static inline void
  571. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  572. {
  573. if (cp->control) {
  574. IP_VS_ERR_BUF("request control ADD for already controlled: "
  575. "%s:%d to %s:%d\n",
  576. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  577. ntohs(cp->cport),
  578. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  579. ntohs(cp->vport));
  580. ip_vs_control_del(cp);
  581. }
  582. IP_VS_DBG_BUF(7, "ADDing control for: "
  583. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  584. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  585. ntohs(cp->cport),
  586. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  587. ntohs(ctl_cp->cport));
  588. cp->control = ctl_cp;
  589. atomic_inc(&ctl_cp->n_control);
  590. }
  591. /*
  592. * IPVS application functions
  593. * (from ip_vs_app.c)
  594. */
  595. #define IP_VS_APP_MAX_PORTS 8
  596. extern int register_ip_vs_app(struct ip_vs_app *app);
  597. extern void unregister_ip_vs_app(struct ip_vs_app *app);
  598. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  599. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  600. extern int
  601. register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
  602. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  603. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  604. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  605. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  606. extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
  607. char *o_buf, int o_len, char *n_buf, int n_len);
  608. extern int ip_vs_app_init(void);
  609. extern void ip_vs_app_cleanup(void);
  610. /*
  611. * IPVS protocol functions (from ip_vs_proto.c)
  612. */
  613. extern int ip_vs_protocol_init(void);
  614. extern void ip_vs_protocol_cleanup(void);
  615. extern void ip_vs_protocol_timeout_change(int flags);
  616. extern int *ip_vs_create_timeout_table(int *table, int size);
  617. extern int
  618. ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
  619. extern void
  620. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  621. int offset, const char *msg);
  622. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  623. extern struct ip_vs_protocol ip_vs_protocol_udp;
  624. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  625. extern struct ip_vs_protocol ip_vs_protocol_esp;
  626. extern struct ip_vs_protocol ip_vs_protocol_ah;
  627. /*
  628. * Registering/unregistering scheduler functions
  629. * (from ip_vs_sched.c)
  630. */
  631. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  632. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  633. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  634. struct ip_vs_scheduler *scheduler);
  635. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  636. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  637. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  638. extern struct ip_vs_conn *
  639. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
  640. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  641. struct ip_vs_protocol *pp);
  642. /*
  643. * IPVS control data and functions (from ip_vs_ctl.c)
  644. */
  645. extern int sysctl_ip_vs_cache_bypass;
  646. extern int sysctl_ip_vs_expire_nodest_conn;
  647. extern int sysctl_ip_vs_expire_quiescent_template;
  648. extern int sysctl_ip_vs_sync_threshold[2];
  649. extern int sysctl_ip_vs_nat_icmp_send;
  650. extern struct ip_vs_stats ip_vs_stats;
  651. extern const struct ctl_path net_vs_ctl_path[];
  652. extern struct ip_vs_service *
  653. ip_vs_service_get(int af, __u32 fwmark, __u16 protocol,
  654. const union nf_inet_addr *vaddr, __be16 vport);
  655. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  656. {
  657. atomic_dec(&svc->usecnt);
  658. }
  659. extern struct ip_vs_dest *
  660. ip_vs_lookup_real_service(int af, __u16 protocol,
  661. const union nf_inet_addr *daddr, __be16 dport);
  662. extern int ip_vs_use_count_inc(void);
  663. extern void ip_vs_use_count_dec(void);
  664. extern int ip_vs_control_init(void);
  665. extern void ip_vs_control_cleanup(void);
  666. extern struct ip_vs_dest *
  667. ip_vs_find_dest(int af, const union nf_inet_addr *daddr, __be16 dport,
  668. const union nf_inet_addr *vaddr, __be16 vport, __u16 protocol);
  669. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  670. /*
  671. * IPVS sync daemon data and function prototypes
  672. * (from ip_vs_sync.c)
  673. */
  674. extern volatile int ip_vs_sync_state;
  675. extern volatile int ip_vs_master_syncid;
  676. extern volatile int ip_vs_backup_syncid;
  677. extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  678. extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  679. extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
  680. extern int stop_sync_thread(int state);
  681. extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
  682. /*
  683. * IPVS rate estimator prototypes (from ip_vs_est.c)
  684. */
  685. extern int ip_vs_estimator_init(void);
  686. extern void ip_vs_estimator_cleanup(void);
  687. extern void ip_vs_new_estimator(struct ip_vs_stats *stats);
  688. extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
  689. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  690. /*
  691. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  692. */
  693. extern int ip_vs_null_xmit
  694. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  695. extern int ip_vs_bypass_xmit
  696. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  697. extern int ip_vs_nat_xmit
  698. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  699. extern int ip_vs_tunnel_xmit
  700. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  701. extern int ip_vs_dr_xmit
  702. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  703. extern int ip_vs_icmp_xmit
  704. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
  705. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  706. #ifdef CONFIG_IP_VS_IPV6
  707. extern int ip_vs_bypass_xmit_v6
  708. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  709. extern int ip_vs_nat_xmit_v6
  710. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  711. extern int ip_vs_tunnel_xmit_v6
  712. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  713. extern int ip_vs_dr_xmit_v6
  714. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  715. extern int ip_vs_icmp_xmit_v6
  716. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp,
  717. int offset);
  718. #endif
  719. /*
  720. * This is a simple mechanism to ignore packets when
  721. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  722. * we start to drop 1/rate of the packets
  723. */
  724. extern int ip_vs_drop_rate;
  725. extern int ip_vs_drop_counter;
  726. static __inline__ int ip_vs_todrop(void)
  727. {
  728. if (!ip_vs_drop_rate) return 0;
  729. if (--ip_vs_drop_counter > 0) return 0;
  730. ip_vs_drop_counter = ip_vs_drop_rate;
  731. return 1;
  732. }
  733. /*
  734. * ip_vs_fwd_tag returns the forwarding tag of the connection
  735. */
  736. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  737. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  738. {
  739. char fwd;
  740. switch (IP_VS_FWD_METHOD(cp)) {
  741. case IP_VS_CONN_F_MASQ:
  742. fwd = 'M'; break;
  743. case IP_VS_CONN_F_LOCALNODE:
  744. fwd = 'L'; break;
  745. case IP_VS_CONN_F_TUNNEL:
  746. fwd = 'T'; break;
  747. case IP_VS_CONN_F_DROUTE:
  748. fwd = 'R'; break;
  749. case IP_VS_CONN_F_BYPASS:
  750. fwd = 'B'; break;
  751. default:
  752. fwd = '?'; break;
  753. }
  754. return fwd;
  755. }
  756. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  757. struct ip_vs_conn *cp, int dir);
  758. #ifdef CONFIG_IP_VS_IPV6
  759. extern void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  760. struct ip_vs_conn *cp, int dir);
  761. #endif
  762. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  763. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  764. {
  765. __be32 diff[2] = { ~old, new };
  766. return csum_partial(diff, sizeof(diff), oldsum);
  767. }
  768. #ifdef CONFIG_IP_VS_IPV6
  769. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  770. __wsum oldsum)
  771. {
  772. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  773. new[3], new[2], new[1], new[0] };
  774. return csum_partial(diff, sizeof(diff), oldsum);
  775. }
  776. #endif
  777. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  778. {
  779. __be16 diff[2] = { ~old, new };
  780. return csum_partial(diff, sizeof(diff), oldsum);
  781. }
  782. #endif /* __KERNEL__ */
  783. #endif /* _NET_IP_VS_H */