ip_vs.h 25 KB

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