ip_vs.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. * TCP State Values
  270. */
  271. enum {
  272. IP_VS_TCP_S_NONE = 0,
  273. IP_VS_TCP_S_ESTABLISHED,
  274. IP_VS_TCP_S_SYN_SENT,
  275. IP_VS_TCP_S_SYN_RECV,
  276. IP_VS_TCP_S_FIN_WAIT,
  277. IP_VS_TCP_S_TIME_WAIT,
  278. IP_VS_TCP_S_CLOSE,
  279. IP_VS_TCP_S_CLOSE_WAIT,
  280. IP_VS_TCP_S_LAST_ACK,
  281. IP_VS_TCP_S_LISTEN,
  282. IP_VS_TCP_S_SYNACK,
  283. IP_VS_TCP_S_LAST
  284. };
  285. /*
  286. * UDP State Values
  287. */
  288. enum {
  289. IP_VS_UDP_S_NORMAL,
  290. IP_VS_UDP_S_LAST,
  291. };
  292. /*
  293. * ICMP State Values
  294. */
  295. enum {
  296. IP_VS_ICMP_S_NORMAL,
  297. IP_VS_ICMP_S_LAST,
  298. };
  299. /*
  300. * Delta sequence info structure
  301. * Each ip_vs_conn has 2 (output AND input seq. changes).
  302. * Only used in the VS/NAT.
  303. */
  304. struct ip_vs_seq {
  305. __u32 init_seq; /* Add delta from this seq */
  306. __u32 delta; /* Delta in sequence numbers */
  307. __u32 previous_delta; /* Delta in sequence numbers
  308. before last resized pkt */
  309. };
  310. /*
  311. * IPVS statistics object
  312. */
  313. struct ip_vs_stats
  314. {
  315. __u32 conns; /* connections scheduled */
  316. __u32 inpkts; /* incoming packets */
  317. __u32 outpkts; /* outgoing packets */
  318. __u64 inbytes; /* incoming bytes */
  319. __u64 outbytes; /* outgoing bytes */
  320. __u32 cps; /* current connection rate */
  321. __u32 inpps; /* current in packet rate */
  322. __u32 outpps; /* current out packet rate */
  323. __u32 inbps; /* current in byte rate */
  324. __u32 outbps; /* current out byte rate */
  325. spinlock_t lock; /* spin lock */
  326. };
  327. struct dst_entry;
  328. struct iphdr;
  329. struct ip_vs_conn;
  330. struct ip_vs_app;
  331. struct sk_buff;
  332. struct ip_vs_protocol {
  333. struct ip_vs_protocol *next;
  334. char *name;
  335. __u16 protocol;
  336. int dont_defrag;
  337. atomic_t appcnt; /* counter of proto app incs */
  338. int *timeout_table; /* protocol timeout table */
  339. void (*init)(struct ip_vs_protocol *pp);
  340. void (*exit)(struct ip_vs_protocol *pp);
  341. int (*conn_schedule)(struct sk_buff *skb,
  342. struct ip_vs_protocol *pp,
  343. int *verdict, struct ip_vs_conn **cpp);
  344. struct ip_vs_conn *
  345. (*conn_in_get)(const struct sk_buff *skb,
  346. struct ip_vs_protocol *pp,
  347. const struct iphdr *iph,
  348. unsigned int proto_off,
  349. int inverse);
  350. struct ip_vs_conn *
  351. (*conn_out_get)(const struct sk_buff *skb,
  352. struct ip_vs_protocol *pp,
  353. const struct iphdr *iph,
  354. unsigned int proto_off,
  355. int inverse);
  356. int (*snat_handler)(struct sk_buff *skb,
  357. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  358. int (*dnat_handler)(struct sk_buff *skb,
  359. struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
  360. int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
  361. const char *(*state_name)(int state);
  362. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  363. const struct sk_buff *skb,
  364. struct ip_vs_protocol *pp);
  365. int (*register_app)(struct ip_vs_app *inc);
  366. void (*unregister_app)(struct ip_vs_app *inc);
  367. int (*app_conn_bind)(struct ip_vs_conn *cp);
  368. void (*debug_packet)(struct ip_vs_protocol *pp,
  369. const struct sk_buff *skb,
  370. int offset,
  371. const char *msg);
  372. void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
  373. int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
  374. };
  375. extern struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto);
  376. /*
  377. * IP_VS structure allocated for each dynamically scheduled connection
  378. */
  379. struct ip_vs_conn {
  380. struct list_head c_list; /* hashed list heads */
  381. /* Protocol, addresses and port numbers */
  382. __be32 caddr; /* client address */
  383. __be32 vaddr; /* virtual address */
  384. __be32 daddr; /* destination address */
  385. __be16 cport;
  386. __be16 vport;
  387. __be16 dport;
  388. __u16 protocol; /* Which protocol (TCP/UDP) */
  389. /* counter and timer */
  390. atomic_t refcnt; /* reference count */
  391. struct timer_list timer; /* Expiration timer */
  392. volatile unsigned long timeout; /* timeout */
  393. /* Flags and state transition */
  394. spinlock_t lock; /* lock for state transition */
  395. volatile __u16 flags; /* status flags */
  396. volatile __u16 state; /* state info */
  397. volatile __u16 old_state; /* old state, to be used for
  398. * state transition triggerd
  399. * synchronization
  400. */
  401. /* Control members */
  402. struct ip_vs_conn *control; /* Master control connection */
  403. atomic_t n_control; /* Number of controlled ones */
  404. struct ip_vs_dest *dest; /* real server */
  405. atomic_t in_pkts; /* incoming packet counter */
  406. /* packet transmitter for different forwarding methods. If it
  407. mangles the packet, it must return NF_DROP or better NF_STOLEN,
  408. otherwise this must be changed to a sk_buff **.
  409. */
  410. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  411. struct ip_vs_protocol *pp);
  412. /* Note: we can group the following members into a structure,
  413. in order to save more space, and the following members are
  414. only used in VS/NAT anyway */
  415. struct ip_vs_app *app; /* bound ip_vs_app object */
  416. void *app_data; /* Application private data */
  417. struct ip_vs_seq in_seq; /* incoming seq. struct */
  418. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  419. };
  420. /*
  421. * The information about the virtual service offered to the net
  422. * and the forwarding entries
  423. */
  424. struct ip_vs_service {
  425. struct list_head s_list; /* for normal service table */
  426. struct list_head f_list; /* for fwmark-based service table */
  427. atomic_t refcnt; /* reference counter */
  428. atomic_t usecnt; /* use counter */
  429. __u16 protocol; /* which protocol (TCP/UDP) */
  430. __be32 addr; /* IP address for virtual service */
  431. __be16 port; /* port number for the service */
  432. __u32 fwmark; /* firewall mark of the service */
  433. unsigned flags; /* service status flags */
  434. unsigned timeout; /* persistent timeout in ticks */
  435. __be32 netmask; /* grouping granularity */
  436. struct list_head destinations; /* real server d-linked list */
  437. __u32 num_dests; /* number of servers */
  438. struct ip_vs_stats stats; /* statistics for the service */
  439. struct ip_vs_app *inc; /* bind conns to this app inc */
  440. /* for scheduling */
  441. struct ip_vs_scheduler *scheduler; /* bound scheduler object */
  442. rwlock_t sched_lock; /* lock sched_data */
  443. void *sched_data; /* scheduler application data */
  444. };
  445. /*
  446. * The real server destination forwarding entry
  447. * with ip address, port number, and so on.
  448. */
  449. struct ip_vs_dest {
  450. struct list_head n_list; /* for the dests in the service */
  451. struct list_head d_list; /* for table with all the dests */
  452. __be32 addr; /* IP address of the server */
  453. __be16 port; /* port number of the server */
  454. volatile unsigned flags; /* dest status flags */
  455. atomic_t conn_flags; /* flags to copy to conn */
  456. atomic_t weight; /* server weight */
  457. atomic_t refcnt; /* reference counter */
  458. struct ip_vs_stats stats; /* statistics */
  459. /* connection counters and thresholds */
  460. atomic_t activeconns; /* active connections */
  461. atomic_t inactconns; /* inactive connections */
  462. atomic_t persistconns; /* persistent connections */
  463. __u32 u_threshold; /* upper threshold */
  464. __u32 l_threshold; /* lower threshold */
  465. /* for destination cache */
  466. spinlock_t dst_lock; /* lock of dst_cache */
  467. struct dst_entry *dst_cache; /* destination cache entry */
  468. u32 dst_rtos; /* RT_TOS(tos) for dst */
  469. /* for virtual service */
  470. struct ip_vs_service *svc; /* service it belongs to */
  471. __u16 protocol; /* which protocol (TCP/UDP) */
  472. __be32 vaddr; /* virtual IP address */
  473. __be16 vport; /* virtual port number */
  474. __u32 vfwmark; /* firewall mark of service */
  475. };
  476. /*
  477. * The scheduler object
  478. */
  479. struct ip_vs_scheduler {
  480. struct list_head n_list; /* d-linked list head */
  481. char *name; /* scheduler name */
  482. atomic_t refcnt; /* reference counter */
  483. struct module *module; /* THIS_MODULE/NULL */
  484. /* scheduler initializing service */
  485. int (*init_service)(struct ip_vs_service *svc);
  486. /* scheduling service finish */
  487. int (*done_service)(struct ip_vs_service *svc);
  488. /* scheduler updating service */
  489. int (*update_service)(struct ip_vs_service *svc);
  490. /* selecting a server from the given service */
  491. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  492. const struct sk_buff *skb);
  493. };
  494. /*
  495. * The application module object (a.k.a. app incarnation)
  496. */
  497. struct ip_vs_app
  498. {
  499. struct list_head a_list; /* member in app list */
  500. int type; /* IP_VS_APP_TYPE_xxx */
  501. char *name; /* application module name */
  502. __u16 protocol;
  503. struct module *module; /* THIS_MODULE/NULL */
  504. struct list_head incs_list; /* list of incarnations */
  505. /* members for application incarnations */
  506. struct list_head p_list; /* member in proto app list */
  507. struct ip_vs_app *app; /* its real application */
  508. __be16 port; /* port number in net order */
  509. atomic_t usecnt; /* usage counter */
  510. /* output hook: return false if can't linearize. diff set for TCP. */
  511. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  512. struct sk_buff *, int *diff);
  513. /* input hook: return false if can't linearize. diff set for TCP. */
  514. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  515. struct sk_buff *, int *diff);
  516. /* ip_vs_app initializer */
  517. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  518. /* ip_vs_app finish */
  519. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  520. /* not used now */
  521. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  522. struct ip_vs_protocol *);
  523. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  524. int * timeout_table;
  525. int * timeouts;
  526. int timeouts_size;
  527. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  528. int *verdict, struct ip_vs_conn **cpp);
  529. struct ip_vs_conn *
  530. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  531. const struct iphdr *iph, unsigned int proto_off,
  532. int inverse);
  533. struct ip_vs_conn *
  534. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  535. const struct iphdr *iph, unsigned int proto_off,
  536. int inverse);
  537. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  538. const struct sk_buff *skb,
  539. struct ip_vs_app *app);
  540. void (*timeout_change)(struct ip_vs_app *app, int flags);
  541. };
  542. /*
  543. * IPVS core functions
  544. * (from ip_vs_core.c)
  545. */
  546. extern const char *ip_vs_proto_name(unsigned proto);
  547. extern void ip_vs_init_hash_table(struct list_head *table, int rows);
  548. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table(t, sizeof(t)/sizeof(t[0]))
  549. #define IP_VS_APP_TYPE_UNSPEC 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_secure_tcp_set(int on);
  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 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 */