ip_vs.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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_TO_ES=7,
  277. NET_IPV4_VS_TO_SS=8,
  278. NET_IPV4_VS_TO_SR=9,
  279. NET_IPV4_VS_TO_FW=10,
  280. NET_IPV4_VS_TO_TW=11,
  281. NET_IPV4_VS_TO_CL=12,
  282. NET_IPV4_VS_TO_CW=13,
  283. NET_IPV4_VS_TO_LA=14,
  284. NET_IPV4_VS_TO_LI=15,
  285. NET_IPV4_VS_TO_SA=16,
  286. NET_IPV4_VS_TO_UDP=17,
  287. NET_IPV4_VS_TO_ICMP=18,
  288. NET_IPV4_VS_CACHE_BYPASS=22,
  289. NET_IPV4_VS_EXPIRE_NODEST_CONN=23,
  290. NET_IPV4_VS_NAT_ICMP_SEND=25,
  291. NET_IPV4_VS_EXPIRE_QUIESCENT_TEMPLATE=26,
  292. NET_IPV4_VS_LAST
  293. };
  294. /*
  295. * TCP State Values
  296. */
  297. enum {
  298. IP_VS_TCP_S_NONE = 0,
  299. IP_VS_TCP_S_ESTABLISHED,
  300. IP_VS_TCP_S_SYN_SENT,
  301. IP_VS_TCP_S_SYN_RECV,
  302. IP_VS_TCP_S_FIN_WAIT,
  303. IP_VS_TCP_S_TIME_WAIT,
  304. IP_VS_TCP_S_CLOSE,
  305. IP_VS_TCP_S_CLOSE_WAIT,
  306. IP_VS_TCP_S_LAST_ACK,
  307. IP_VS_TCP_S_LISTEN,
  308. IP_VS_TCP_S_SYNACK,
  309. IP_VS_TCP_S_LAST
  310. };
  311. /*
  312. * UDP State Values
  313. */
  314. enum {
  315. IP_VS_UDP_S_NORMAL,
  316. IP_VS_UDP_S_LAST,
  317. };
  318. /*
  319. * ICMP State Values
  320. */
  321. enum {
  322. IP_VS_ICMP_S_NORMAL,
  323. IP_VS_ICMP_S_LAST,
  324. };
  325. /*
  326. * Delta sequence info structure
  327. * Each ip_vs_conn has 2 (output AND input seq. changes).
  328. * Only used in the VS/NAT.
  329. */
  330. struct ip_vs_seq {
  331. __u32 init_seq; /* Add delta from this seq */
  332. __u32 delta; /* Delta in sequence numbers */
  333. __u32 previous_delta; /* Delta in sequence numbers
  334. before last resized pkt */
  335. };
  336. /*
  337. * IPVS statistics object
  338. */
  339. struct ip_vs_stats
  340. {
  341. __u32 conns; /* connections scheduled */
  342. __u32 inpkts; /* incoming packets */
  343. __u32 outpkts; /* outgoing packets */
  344. __u64 inbytes; /* incoming bytes */
  345. __u64 outbytes; /* outgoing bytes */
  346. __u32 cps; /* current connection rate */
  347. __u32 inpps; /* current in packet rate */
  348. __u32 outpps; /* current out packet rate */
  349. __u32 inbps; /* current in byte rate */
  350. __u32 outbps; /* current out byte rate */
  351. spinlock_t lock; /* spin lock */
  352. };
  353. struct dst_entry;
  354. struct iphdr;
  355. struct ip_vs_conn;
  356. struct ip_vs_app;
  357. struct sk_buff;
  358. struct ip_vs_protocol {
  359. struct ip_vs_protocol *next;
  360. char *name;
  361. __u16 protocol;
  362. int dont_defrag;
  363. atomic_t appcnt; /* counter of proto app incs */
  364. int *timeout_table; /* protocol timeout table */
  365. void (*init)(struct ip_vs_protocol *pp);
  366. void (*exit)(struct ip_vs_protocol *pp);
  367. int (*conn_schedule)(struct sk_buff *skb,
  368. struct ip_vs_protocol *pp,
  369. int *verdict, struct ip_vs_conn **cpp);
  370. struct ip_vs_conn *
  371. (*conn_in_get)(const struct sk_buff *skb,
  372. struct ip_vs_protocol *pp,
  373. const struct iphdr *iph,
  374. unsigned int proto_off,
  375. int inverse);
  376. struct ip_vs_conn *
  377. (*conn_out_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. int (*snat_handler)(struct sk_buff *skb,
  383. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  384. int (*dnat_handler)(struct sk_buff *skb,
  385. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  386. int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
  387. const char *(*state_name)(int state);
  388. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  389. const struct sk_buff *skb,
  390. struct ip_vs_protocol *pp);
  391. int (*register_app)(struct ip_vs_app *inc);
  392. void (*unregister_app)(struct ip_vs_app *inc);
  393. int (*app_conn_bind)(struct ip_vs_conn *cp);
  394. void (*debug_packet)(struct ip_vs_protocol *pp,
  395. const struct sk_buff *skb,
  396. int offset,
  397. const char *msg);
  398. void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
  399. int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
  400. };
  401. extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
  402. /*
  403. * IP_VS structure allocated for each dynamically scheduled connection
  404. */
  405. struct ip_vs_conn {
  406. struct list_head c_list; /* hashed list heads */
  407. /* Protocol, addresses and port numbers */
  408. __be32 caddr; /* client address */
  409. __be32 vaddr; /* virtual address */
  410. __be32 daddr; /* destination address */
  411. __be16 cport;
  412. __be16 vport;
  413. __be16 dport;
  414. __u16 protocol; /* Which protocol (TCP/UDP) */
  415. /* counter and timer */
  416. atomic_t refcnt; /* reference count */
  417. struct timer_list timer; /* Expiration timer */
  418. volatile unsigned long timeout; /* timeout */
  419. /* Flags and state transition */
  420. spinlock_t lock; /* lock for state transition */
  421. volatile __u16 flags; /* status flags */
  422. volatile __u16 state; /* state info */
  423. volatile __u16 old_state; /* old state, to be used for
  424. * state transition triggerd
  425. * synchronization
  426. */
  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. __be32 addr; /* IP address for virtual service */
  457. __be16 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. __be32 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. __be32 addr; /* IP address of the server */
  479. __be16 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. __be32 vaddr; /* virtual IP address */
  499. __be16 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. __be16 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, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
  607. extern struct ip_vs_conn *ip_vs_ct_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_conn_out_get
  610. (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
  611. /* put back the conn without restarting its timer */
  612. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  613. {
  614. atomic_dec(&cp->refcnt);
  615. }
  616. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  617. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  618. extern struct ip_vs_conn *
  619. ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
  620. __be32 daddr, __be16 dport, unsigned flags,
  621. struct ip_vs_dest *dest);
  622. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  623. extern const char * ip_vs_state_name(__u16 proto, int state);
  624. extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  625. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  626. extern void ip_vs_secure_tcp_set(int on);
  627. extern void ip_vs_random_dropentry(void);
  628. extern int ip_vs_conn_init(void);
  629. extern void ip_vs_conn_cleanup(void);
  630. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  631. {
  632. struct ip_vs_conn *ctl_cp = cp->control;
  633. if (!ctl_cp) {
  634. IP_VS_ERR("request control DEL for uncontrolled: "
  635. "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
  636. NIPQUAD(cp->caddr),ntohs(cp->cport),
  637. NIPQUAD(cp->vaddr),ntohs(cp->vport));
  638. return;
  639. }
  640. IP_VS_DBG(7, "DELeting control for: "
  641. "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
  642. NIPQUAD(cp->caddr),ntohs(cp->cport),
  643. NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
  644. cp->control = NULL;
  645. if (atomic_read(&ctl_cp->n_control) == 0) {
  646. IP_VS_ERR("BUG control DEL with n=0 : "
  647. "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
  648. NIPQUAD(cp->caddr),ntohs(cp->cport),
  649. NIPQUAD(cp->vaddr),ntohs(cp->vport));
  650. return;
  651. }
  652. atomic_dec(&ctl_cp->n_control);
  653. }
  654. static inline void
  655. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  656. {
  657. if (cp->control) {
  658. IP_VS_ERR("request control ADD for already controlled: "
  659. "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
  660. NIPQUAD(cp->caddr),ntohs(cp->cport),
  661. NIPQUAD(cp->vaddr),ntohs(cp->vport));
  662. ip_vs_control_del(cp);
  663. }
  664. IP_VS_DBG(7, "ADDing control for: "
  665. "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
  666. NIPQUAD(cp->caddr),ntohs(cp->cport),
  667. NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
  668. cp->control = ctl_cp;
  669. atomic_inc(&ctl_cp->n_control);
  670. }
  671. /*
  672. * IPVS application functions
  673. * (from ip_vs_app.c)
  674. */
  675. #define IP_VS_APP_MAX_PORTS 8
  676. extern int register_ip_vs_app(struct ip_vs_app *app);
  677. extern void unregister_ip_vs_app(struct ip_vs_app *app);
  678. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  679. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  680. extern int
  681. register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
  682. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  683. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  684. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  685. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  686. extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
  687. char *o_buf, int o_len, char *n_buf, int n_len);
  688. extern int ip_vs_app_init(void);
  689. extern void ip_vs_app_cleanup(void);
  690. /*
  691. * IPVS protocol functions (from ip_vs_proto.c)
  692. */
  693. extern int ip_vs_protocol_init(void);
  694. extern void ip_vs_protocol_cleanup(void);
  695. extern void ip_vs_protocol_timeout_change(int flags);
  696. extern int *ip_vs_create_timeout_table(int *table, int size);
  697. extern int
  698. ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
  699. extern void
  700. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  701. int offset, const char *msg);
  702. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  703. extern struct ip_vs_protocol ip_vs_protocol_udp;
  704. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  705. extern struct ip_vs_protocol ip_vs_protocol_esp;
  706. extern struct ip_vs_protocol ip_vs_protocol_ah;
  707. /*
  708. * Registering/unregistering scheduler functions
  709. * (from ip_vs_sched.c)
  710. */
  711. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  712. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  713. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  714. struct ip_vs_scheduler *scheduler);
  715. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  716. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  717. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  718. extern struct ip_vs_conn *
  719. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
  720. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  721. struct ip_vs_protocol *pp);
  722. /*
  723. * IPVS control data and functions (from ip_vs_ctl.c)
  724. */
  725. extern int sysctl_ip_vs_cache_bypass;
  726. extern int sysctl_ip_vs_expire_nodest_conn;
  727. extern int sysctl_ip_vs_expire_quiescent_template;
  728. extern int sysctl_ip_vs_sync_threshold[2];
  729. extern int sysctl_ip_vs_nat_icmp_send;
  730. extern struct ip_vs_stats ip_vs_stats;
  731. extern struct ip_vs_service *
  732. ip_vs_service_get(__u32 fwmark, __u16 protocol, __be32 vaddr, __be16 vport);
  733. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  734. {
  735. atomic_dec(&svc->usecnt);
  736. }
  737. extern struct ip_vs_dest *
  738. ip_vs_lookup_real_service(__u16 protocol, __be32 daddr, __be16 dport);
  739. extern int ip_vs_use_count_inc(void);
  740. extern void ip_vs_use_count_dec(void);
  741. extern int ip_vs_control_init(void);
  742. extern void ip_vs_control_cleanup(void);
  743. extern struct ip_vs_dest *
  744. ip_vs_find_dest(__be32 daddr, __be16 dport,
  745. __be32 vaddr, __be16 vport, __u16 protocol);
  746. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  747. /*
  748. * IPVS sync daemon data and function prototypes
  749. * (from ip_vs_sync.c)
  750. */
  751. extern volatile int ip_vs_sync_state;
  752. extern volatile int ip_vs_master_syncid;
  753. extern volatile int ip_vs_backup_syncid;
  754. extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  755. extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  756. extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
  757. extern int stop_sync_thread(int state);
  758. extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
  759. /*
  760. * IPVS rate estimator prototypes (from ip_vs_est.c)
  761. */
  762. extern int ip_vs_new_estimator(struct ip_vs_stats *stats);
  763. extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
  764. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  765. /*
  766. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  767. */
  768. extern int ip_vs_null_xmit
  769. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  770. extern int ip_vs_bypass_xmit
  771. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  772. extern int ip_vs_nat_xmit
  773. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  774. extern int ip_vs_tunnel_xmit
  775. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  776. extern int ip_vs_dr_xmit
  777. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  778. extern int ip_vs_icmp_xmit
  779. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
  780. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  781. /*
  782. * This is a simple mechanism to ignore packets when
  783. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  784. * we start to drop 1/rate of the packets
  785. */
  786. extern int ip_vs_drop_rate;
  787. extern int ip_vs_drop_counter;
  788. static __inline__ int ip_vs_todrop(void)
  789. {
  790. if (!ip_vs_drop_rate) return 0;
  791. if (--ip_vs_drop_counter > 0) return 0;
  792. ip_vs_drop_counter = ip_vs_drop_rate;
  793. return 1;
  794. }
  795. /*
  796. * ip_vs_fwd_tag returns the forwarding tag of the connection
  797. */
  798. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  799. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  800. {
  801. char fwd;
  802. switch (IP_VS_FWD_METHOD(cp)) {
  803. case IP_VS_CONN_F_MASQ:
  804. fwd = 'M'; break;
  805. case IP_VS_CONN_F_LOCALNODE:
  806. fwd = 'L'; break;
  807. case IP_VS_CONN_F_TUNNEL:
  808. fwd = 'T'; break;
  809. case IP_VS_CONN_F_DROUTE:
  810. fwd = 'R'; break;
  811. case IP_VS_CONN_F_BYPASS:
  812. fwd = 'B'; break;
  813. default:
  814. fwd = '?'; break;
  815. }
  816. return fwd;
  817. }
  818. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  819. struct ip_vs_conn *cp, int dir);
  820. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  821. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  822. {
  823. __be32 diff[2] = { ~old, new };
  824. return csum_partial((char *) diff, sizeof(diff), oldsum);
  825. }
  826. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  827. {
  828. __be16 diff[2] = { ~old, new };
  829. return csum_partial((char *) diff, sizeof(diff), oldsum);
  830. }
  831. #endif /* __KERNEL__ */
  832. #endif /* _IP_VS_H */