ip_vs.h 27 KB

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