ip_vs.h 29 KB

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