ip_vs.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. int dont_defrag;
  338. atomic_t appcnt; /* counter of proto app incs */
  339. int *timeout_table; /* protocol timeout table */
  340. void (*init)(struct ip_vs_protocol *pp);
  341. void (*exit)(struct ip_vs_protocol *pp);
  342. int (*conn_schedule)(struct sk_buff *skb,
  343. struct ip_vs_protocol *pp,
  344. int *verdict, struct ip_vs_conn **cpp);
  345. struct ip_vs_conn *
  346. (*conn_in_get)(const struct sk_buff *skb,
  347. struct ip_vs_protocol *pp,
  348. const struct iphdr *iph,
  349. unsigned int proto_off,
  350. int inverse);
  351. struct ip_vs_conn *
  352. (*conn_out_get)(const struct sk_buff *skb,
  353. struct ip_vs_protocol *pp,
  354. const struct iphdr *iph,
  355. unsigned int proto_off,
  356. int inverse);
  357. int (*snat_handler)(struct sk_buff *skb,
  358. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  359. int (*dnat_handler)(struct sk_buff *skb,
  360. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  361. int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
  362. const char *(*state_name)(int state);
  363. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  364. const struct sk_buff *skb,
  365. struct ip_vs_protocol *pp);
  366. int (*register_app)(struct ip_vs_app *inc);
  367. void (*unregister_app)(struct ip_vs_app *inc);
  368. int (*app_conn_bind)(struct ip_vs_conn *cp);
  369. void (*debug_packet)(struct ip_vs_protocol *pp,
  370. const struct sk_buff *skb,
  371. int offset,
  372. const char *msg);
  373. void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
  374. int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
  375. };
  376. extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
  377. /*
  378. * IP_VS structure allocated for each dynamically scheduled connection
  379. */
  380. struct ip_vs_conn {
  381. struct list_head c_list; /* hashed list heads */
  382. /* Protocol, addresses and port numbers */
  383. __be32 caddr; /* client address */
  384. __be32 vaddr; /* virtual address */
  385. __be32 daddr; /* destination address */
  386. __be16 cport;
  387. __be16 vport;
  388. __be16 dport;
  389. __u16 protocol; /* Which protocol (TCP/UDP) */
  390. /* counter and timer */
  391. atomic_t refcnt; /* reference count */
  392. struct timer_list timer; /* Expiration timer */
  393. volatile unsigned long timeout; /* timeout */
  394. /* Flags and state transition */
  395. spinlock_t lock; /* lock for state transition */
  396. volatile __u16 flags; /* status flags */
  397. volatile __u16 state; /* state info */
  398. volatile __u16 old_state; /* old state, to be used for
  399. * state transition triggerd
  400. * synchronization
  401. */
  402. /* Control members */
  403. struct ip_vs_conn *control; /* Master control connection */
  404. atomic_t n_control; /* Number of controlled ones */
  405. struct ip_vs_dest *dest; /* real server */
  406. atomic_t in_pkts; /* incoming packet counter */
  407. /* packet transmitter for different forwarding methods. If it
  408. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  409. otherwise this must be changed to a sk_buff **.
  410. */
  411. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  412. struct ip_vs_protocol *pp);
  413. /* Note: we can group the following members into a structure,
  414. in order to save more space, and the following members are
  415. only used in VS/NAT anyway */
  416. struct ip_vs_app *app; /* bound ip_vs_app object */
  417. void *app_data; /* Application private data */
  418. struct ip_vs_seq in_seq; /* incoming seq. struct */
  419. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  420. };
  421. /*
  422. * The information about the virtual service offered to the net
  423. * and the forwarding entries
  424. */
  425. struct ip_vs_service {
  426. struct list_head s_list; /* for normal service table */
  427. struct list_head f_list; /* for fwmark-based service table */
  428. atomic_t refcnt; /* reference counter */
  429. atomic_t usecnt; /* use counter */
  430. __u16 protocol; /* which protocol (TCP/UDP) */
  431. __be32 addr; /* IP address for virtual service */
  432. __be16 port; /* port number for the service */
  433. __u32 fwmark; /* firewall mark of the service */
  434. unsigned flags; /* service status flags */
  435. unsigned timeout; /* persistent timeout in ticks */
  436. __be32 netmask; /* grouping granularity */
  437. struct list_head destinations; /* real server d-linked list */
  438. __u32 num_dests; /* number of servers */
  439. struct ip_vs_stats stats; /* statistics for the service */
  440. struct ip_vs_app *inc; /* bind conns to this app inc */
  441. /* for scheduling */
  442. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  443. rwlock_t sched_lock; /* lock sched_data */
  444. void *sched_data; /* scheduler application data */
  445. };
  446. /*
  447. * The real server destination forwarding entry
  448. * with ip address, port number, and so on.
  449. */
  450. struct ip_vs_dest {
  451. struct list_head n_list; /* for the dests in the service */
  452. struct list_head d_list; /* for table with all the dests */
  453. __be32 addr; /* IP address of the server */
  454. __be16 port; /* port number of the server */
  455. volatile unsigned flags; /* dest status flags */
  456. atomic_t conn_flags; /* flags to copy to conn */
  457. atomic_t weight; /* server weight */
  458. atomic_t refcnt; /* reference counter */
  459. struct ip_vs_stats stats; /* statistics */
  460. /* connection counters and thresholds */
  461. atomic_t activeconns; /* active connections */
  462. atomic_t inactconns; /* inactive connections */
  463. atomic_t persistconns; /* persistent connections */
  464. __u32 u_threshold; /* upper threshold */
  465. __u32 l_threshold; /* lower threshold */
  466. /* for destination cache */
  467. spinlock_t dst_lock; /* lock of dst_cache */
  468. struct dst_entry *dst_cache; /* destination cache entry */
  469. u32 dst_rtos; /* RT_TOS(tos) for dst */
  470. /* for virtual service */
  471. struct ip_vs_service *svc; /* service it belongs to */
  472. __u16 protocol; /* which protocol (TCP/UDP) */
  473. __be32 vaddr; /* virtual IP address */
  474. __be16 vport; /* virtual port number */
  475. __u32 vfwmark; /* firewall mark of service */
  476. };
  477. /*
  478. * The scheduler object
  479. */
  480. struct ip_vs_scheduler {
  481. struct list_head n_list; /* d-linked list head */
  482. char *name; /* scheduler name */
  483. atomic_t refcnt; /* reference counter */
  484. struct module *module; /* THIS_MODULE/NULL */
  485. /* scheduler initializing service */
  486. int (*init_service)(struct ip_vs_service *svc);
  487. /* scheduling service finish */
  488. int (*done_service)(struct ip_vs_service *svc);
  489. /* scheduler updating service */
  490. int (*update_service)(struct ip_vs_service *svc);
  491. /* selecting a server from the given service */
  492. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  493. const struct sk_buff *skb);
  494. };
  495. /*
  496. * The application module object (a.k.a. app incarnation)
  497. */
  498. struct ip_vs_app
  499. {
  500. struct list_head a_list; /* member in app list */
  501. int type; /* IP_VS_APP_TYPE_xxx */
  502. char *name; /* application module name */
  503. __u16 protocol;
  504. struct module *module; /* THIS_MODULE/NULL */
  505. struct list_head incs_list; /* list of incarnations */
  506. /* members for application incarnations */
  507. struct list_head p_list; /* member in proto app list */
  508. struct ip_vs_app *app; /* its real application */
  509. __be16 port; /* port number in net order */
  510. atomic_t usecnt; /* usage counter */
  511. /* output hook: return false if can't linearize. diff set for TCP. */
  512. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  513. struct sk_buff *, int *diff);
  514. /* input hook: return false if can't linearize. diff set for TCP. */
  515. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  516. struct sk_buff *, int *diff);
  517. /* ip_vs_app initializer */
  518. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  519. /* ip_vs_app finish */
  520. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  521. /* not used now */
  522. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  523. struct ip_vs_protocol *);
  524. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  525. int * timeout_table;
  526. int * timeouts;
  527. int timeouts_size;
  528. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  529. int *verdict, struct ip_vs_conn **cpp);
  530. struct ip_vs_conn *
  531. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  532. const struct iphdr *iph, unsigned int proto_off,
  533. int inverse);
  534. struct ip_vs_conn *
  535. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  536. const struct iphdr *iph, unsigned int proto_off,
  537. int inverse);
  538. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  539. const struct sk_buff *skb,
  540. struct ip_vs_app *app);
  541. void (*timeout_change)(struct ip_vs_app *app, int flags);
  542. };
  543. /*
  544. * IPVS core functions
  545. * (from ip_vs_core.c)
  546. */
  547. extern const char *ip_vs_proto_name(unsigned proto);
  548. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  549. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table(t, sizeof(t)/sizeof(t[0]))
  550. #define IP_VS_APP_TYPE_FTP 1
  551. /*
  552. * ip_vs_conn handling functions
  553. * (from ip_vs_conn.c)
  554. */
  555. /*
  556. * IPVS connection entry hash table
  557. */
  558. #ifndef CONFIG_IP_VS_TAB_BITS
  559. #define CONFIG_IP_VS_TAB_BITS 12
  560. #endif
  561. /* make sure that IP_VS_CONN_TAB_BITS is located in [8, 20] */
  562. #if CONFIG_IP_VS_TAB_BITS < 8
  563. #define IP_VS_CONN_TAB_BITS 8
  564. #endif
  565. #if CONFIG_IP_VS_TAB_BITS > 20
  566. #define IP_VS_CONN_TAB_BITS 20
  567. #endif
  568. #if 8 <= CONFIG_IP_VS_TAB_BITS && CONFIG_IP_VS_TAB_BITS <= 20
  569. #define IP_VS_CONN_TAB_BITS CONFIG_IP_VS_TAB_BITS
  570. #endif
  571. #define IP_VS_CONN_TAB_SIZE (1 << IP_VS_CONN_TAB_BITS)
  572. #define IP_VS_CONN_TAB_MASK (IP_VS_CONN_TAB_SIZE - 1)
  573. enum {
  574. IP_VS_DIR_INPUT = 0,
  575. IP_VS_DIR_OUTPUT,
  576. IP_VS_DIR_INPUT_ONLY,
  577. IP_VS_DIR_LAST,
  578. };
  579. extern struct ip_vs_conn *ip_vs_conn_in_get
  580. (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
  581. extern struct ip_vs_conn *ip_vs_ct_in_get
  582. (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
  583. extern struct ip_vs_conn *ip_vs_conn_out_get
  584. (int protocol, __be32 s_addr, __be16 s_port, __be32 d_addr, __be16 d_port);
  585. /* put back the conn without restarting its timer */
  586. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  587. {
  588. atomic_dec(&cp->refcnt);
  589. }
  590. extern void ip_vs_conn_put(struct ip_vs_conn *cp);
  591. extern void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  592. extern struct ip_vs_conn *
  593. ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
  594. __be32 daddr, __be16 dport, unsigned flags,
  595. struct ip_vs_dest *dest);
  596. extern void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  597. extern const char * ip_vs_state_name(__u16 proto, int state);
  598. extern void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  599. extern int ip_vs_check_template(struct ip_vs_conn *ct);
  600. extern void ip_vs_random_dropentry(void);
  601. extern int ip_vs_conn_init(void);
  602. extern void ip_vs_conn_cleanup(void);
  603. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  604. {
  605. struct ip_vs_conn *ctl_cp = cp->control;
  606. if (!ctl_cp) {
  607. IP_VS_ERR("request control DEL for uncontrolled: "
  608. "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
  609. NIPQUAD(cp->caddr),ntohs(cp->cport),
  610. NIPQUAD(cp->vaddr),ntohs(cp->vport));
  611. return;
  612. }
  613. IP_VS_DBG(7, "DELeting control for: "
  614. "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
  615. NIPQUAD(cp->caddr),ntohs(cp->cport),
  616. NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
  617. cp->control = NULL;
  618. if (atomic_read(&ctl_cp->n_control) == 0) {
  619. IP_VS_ERR("BUG control DEL with n=0 : "
  620. "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
  621. NIPQUAD(cp->caddr),ntohs(cp->cport),
  622. NIPQUAD(cp->vaddr),ntohs(cp->vport));
  623. return;
  624. }
  625. atomic_dec(&ctl_cp->n_control);
  626. }
  627. static inline void
  628. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  629. {
  630. if (cp->control) {
  631. IP_VS_ERR("request control ADD for already controlled: "
  632. "%d.%d.%d.%d:%d to %d.%d.%d.%d:%d\n",
  633. NIPQUAD(cp->caddr),ntohs(cp->cport),
  634. NIPQUAD(cp->vaddr),ntohs(cp->vport));
  635. ip_vs_control_del(cp);
  636. }
  637. IP_VS_DBG(7, "ADDing control for: "
  638. "cp.dst=%d.%d.%d.%d:%d ctl_cp.dst=%d.%d.%d.%d:%d\n",
  639. NIPQUAD(cp->caddr),ntohs(cp->cport),
  640. NIPQUAD(ctl_cp->caddr),ntohs(ctl_cp->cport));
  641. cp->control = ctl_cp;
  642. atomic_inc(&ctl_cp->n_control);
  643. }
  644. /*
  645. * IPVS application functions
  646. * (from ip_vs_app.c)
  647. */
  648. #define IP_VS_APP_MAX_PORTS 8
  649. extern int register_ip_vs_app(struct ip_vs_app *app);
  650. extern void unregister_ip_vs_app(struct ip_vs_app *app);
  651. extern int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  652. extern void ip_vs_unbind_app(struct ip_vs_conn *cp);
  653. extern int
  654. register_ip_vs_app_inc(struct ip_vs_app *app, __u16 proto, __u16 port);
  655. extern int ip_vs_app_inc_get(struct ip_vs_app *inc);
  656. extern void ip_vs_app_inc_put(struct ip_vs_app *inc);
  657. extern int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb);
  658. extern int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb);
  659. extern int ip_vs_skb_replace(struct sk_buff *skb, gfp_t pri,
  660. char *o_buf, int o_len, char *n_buf, int n_len);
  661. extern int ip_vs_app_init(void);
  662. extern void ip_vs_app_cleanup(void);
  663. /*
  664. * IPVS protocol functions (from ip_vs_proto.c)
  665. */
  666. extern int ip_vs_protocol_init(void);
  667. extern void ip_vs_protocol_cleanup(void);
  668. extern void ip_vs_protocol_timeout_change(int flags);
  669. extern int *ip_vs_create_timeout_table(int *table, int size);
  670. extern int
  671. ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to);
  672. extern void
  673. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp, const struct sk_buff *skb,
  674. int offset, const char *msg);
  675. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  676. extern struct ip_vs_protocol ip_vs_protocol_udp;
  677. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  678. extern struct ip_vs_protocol ip_vs_protocol_esp;
  679. extern struct ip_vs_protocol ip_vs_protocol_ah;
  680. /*
  681. * Registering/unregistering scheduler functions
  682. * (from ip_vs_sched.c)
  683. */
  684. extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  685. extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  686. extern int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  687. struct ip_vs_scheduler *scheduler);
  688. extern int ip_vs_unbind_scheduler(struct ip_vs_service *svc);
  689. extern struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  690. extern void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  691. extern struct ip_vs_conn *
  692. ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb);
  693. extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  694. struct ip_vs_protocol *pp);
  695. /*
  696. * IPVS control data and functions (from ip_vs_ctl.c)
  697. */
  698. extern int sysctl_ip_vs_cache_bypass;
  699. extern int sysctl_ip_vs_expire_nodest_conn;
  700. extern int sysctl_ip_vs_expire_quiescent_template;
  701. extern int sysctl_ip_vs_sync_threshold[2];
  702. extern int sysctl_ip_vs_nat_icmp_send;
  703. extern struct ip_vs_stats ip_vs_stats;
  704. extern struct ctl_path net_vs_ctl_path[];
  705. extern struct ip_vs_service *
  706. ip_vs_service_get(__u32 fwmark, __u16 protocol, __be32 vaddr, __be16 vport);
  707. static inline void ip_vs_service_put(struct ip_vs_service *svc)
  708. {
  709. atomic_dec(&svc->usecnt);
  710. }
  711. extern struct ip_vs_dest *
  712. ip_vs_lookup_real_service(__u16 protocol, __be32 daddr, __be16 dport);
  713. extern int ip_vs_use_count_inc(void);
  714. extern void ip_vs_use_count_dec(void);
  715. extern int ip_vs_control_init(void);
  716. extern void ip_vs_control_cleanup(void);
  717. extern struct ip_vs_dest *
  718. ip_vs_find_dest(__be32 daddr, __be16 dport,
  719. __be32 vaddr, __be16 vport, __u16 protocol);
  720. extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  721. /*
  722. * IPVS sync daemon data and function prototypes
  723. * (from ip_vs_sync.c)
  724. */
  725. extern volatile int ip_vs_sync_state;
  726. extern volatile int ip_vs_master_syncid;
  727. extern volatile int ip_vs_backup_syncid;
  728. extern char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  729. extern char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  730. extern int start_sync_thread(int state, char *mcast_ifn, __u8 syncid);
  731. extern int stop_sync_thread(int state);
  732. extern void ip_vs_sync_conn(struct ip_vs_conn *cp);
  733. /*
  734. * IPVS rate estimator prototypes (from ip_vs_est.c)
  735. */
  736. extern int ip_vs_new_estimator(struct ip_vs_stats *stats);
  737. extern void ip_vs_kill_estimator(struct ip_vs_stats *stats);
  738. extern void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  739. /*
  740. * Various IPVS packet transmitters (from ip_vs_xmit.c)
  741. */
  742. extern int ip_vs_null_xmit
  743. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  744. extern int ip_vs_bypass_xmit
  745. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  746. extern int ip_vs_nat_xmit
  747. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  748. extern int ip_vs_tunnel_xmit
  749. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  750. extern int ip_vs_dr_xmit
  751. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  752. extern int ip_vs_icmp_xmit
  753. (struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, int offset);
  754. extern void ip_vs_dst_reset(struct ip_vs_dest *dest);
  755. /*
  756. * This is a simple mechanism to ignore packets when
  757. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  758. * we start to drop 1/rate of the packets
  759. */
  760. extern int ip_vs_drop_rate;
  761. extern int ip_vs_drop_counter;
  762. static __inline__ int ip_vs_todrop(void)
  763. {
  764. if (!ip_vs_drop_rate) return 0;
  765. if (--ip_vs_drop_counter > 0) return 0;
  766. ip_vs_drop_counter = ip_vs_drop_rate;
  767. return 1;
  768. }
  769. /*
  770. * ip_vs_fwd_tag returns the forwarding tag of the connection
  771. */
  772. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  773. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  774. {
  775. char fwd;
  776. switch (IP_VS_FWD_METHOD(cp)) {
  777. case IP_VS_CONN_F_MASQ:
  778. fwd = 'M'; break;
  779. case IP_VS_CONN_F_LOCALNODE:
  780. fwd = 'L'; break;
  781. case IP_VS_CONN_F_TUNNEL:
  782. fwd = 'T'; break;
  783. case IP_VS_CONN_F_DROUTE:
  784. fwd = 'R'; break;
  785. case IP_VS_CONN_F_BYPASS:
  786. fwd = 'B'; break;
  787. default:
  788. fwd = '?'; break;
  789. }
  790. return fwd;
  791. }
  792. extern void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  793. struct ip_vs_conn *cp, int dir);
  794. extern __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  795. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  796. {
  797. __be32 diff[2] = { ~old, new };
  798. return csum_partial((char *) diff, sizeof(diff), oldsum);
  799. }
  800. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  801. {
  802. __be16 diff[2] = { ~old, new };
  803. return csum_partial((char *) diff, sizeof(diff), oldsum);
  804. }
  805. #endif /* __KERNEL__ */
  806. #endif /* _IP_VS_H */