ip_vs.h 30 KB

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