ip_vs.h 30 KB

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