ip_vs.h 30 KB

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