ip_vs.h 27 KB

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