ip_vs.h 30 KB

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