ip_vs_conn.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*
  2. * IPVS An implementation of the IP virtual server support for the
  3. * LINUX operating system. IPVS is now implemented as a module
  4. * over the Netfilter framework. IPVS can be used to build a
  5. * high-performance and highly available server based on a
  6. * cluster of servers.
  7. *
  8. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  9. * Peter Kese <peter.kese@ijs.si>
  10. * Julian Anastasov <ja@ssi.bg>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
  18. * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
  19. * and others. Many code here is taken from IP MASQ code of kernel 2.2.
  20. *
  21. * Changes:
  22. *
  23. */
  24. #include <linux/interrupt.h>
  25. #include <linux/in.h>
  26. #include <linux/net.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/proc_fs.h> /* for proc_net_* */
  31. #include <linux/seq_file.h>
  32. #include <linux/jhash.h>
  33. #include <linux/random.h>
  34. #include <net/net_namespace.h>
  35. #include <net/ip_vs.h>
  36. /*
  37. * Connection hash table: for input and output packets lookups of IPVS
  38. */
  39. static struct list_head *ip_vs_conn_tab;
  40. /* SLAB cache for IPVS connections */
  41. static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
  42. /* counter for current IPVS connections */
  43. static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
  44. /* counter for no client port connections */
  45. static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
  46. /* random value for IPVS connection hash */
  47. static unsigned int ip_vs_conn_rnd;
  48. /*
  49. * Fine locking granularity for big connection hash table
  50. */
  51. #define CT_LOCKARRAY_BITS 4
  52. #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
  53. #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
  54. struct ip_vs_aligned_lock
  55. {
  56. rwlock_t l;
  57. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  58. /* lock array for conn table */
  59. static struct ip_vs_aligned_lock
  60. __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
  61. static inline void ct_read_lock(unsigned key)
  62. {
  63. read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  64. }
  65. static inline void ct_read_unlock(unsigned key)
  66. {
  67. read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  68. }
  69. static inline void ct_write_lock(unsigned key)
  70. {
  71. write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  72. }
  73. static inline void ct_write_unlock(unsigned key)
  74. {
  75. write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  76. }
  77. static inline void ct_read_lock_bh(unsigned key)
  78. {
  79. read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  80. }
  81. static inline void ct_read_unlock_bh(unsigned key)
  82. {
  83. read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  84. }
  85. static inline void ct_write_lock_bh(unsigned key)
  86. {
  87. write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  88. }
  89. static inline void ct_write_unlock_bh(unsigned key)
  90. {
  91. write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  92. }
  93. /*
  94. * Returns hash value for IPVS connection entry
  95. */
  96. static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
  97. const union nf_inet_addr *addr,
  98. __be16 port)
  99. {
  100. #ifdef CONFIG_IP_VS_IPV6
  101. if (af == AF_INET6)
  102. return jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
  103. (__force u32)port, proto, ip_vs_conn_rnd)
  104. & IP_VS_CONN_TAB_MASK;
  105. #endif
  106. return jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
  107. ip_vs_conn_rnd)
  108. & IP_VS_CONN_TAB_MASK;
  109. }
  110. /*
  111. * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
  112. * returns bool success.
  113. */
  114. static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
  115. {
  116. unsigned hash;
  117. int ret;
  118. /* Hash by protocol, client address and port */
  119. hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
  120. ct_write_lock(hash);
  121. if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
  122. list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
  123. cp->flags |= IP_VS_CONN_F_HASHED;
  124. atomic_inc(&cp->refcnt);
  125. ret = 1;
  126. } else {
  127. IP_VS_ERR("ip_vs_conn_hash(): request for already hashed, "
  128. "called from %p\n", __builtin_return_address(0));
  129. ret = 0;
  130. }
  131. ct_write_unlock(hash);
  132. return ret;
  133. }
  134. /*
  135. * UNhashes ip_vs_conn from ip_vs_conn_tab.
  136. * returns bool success.
  137. */
  138. static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
  139. {
  140. unsigned hash;
  141. int ret;
  142. /* unhash it and decrease its reference counter */
  143. hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
  144. ct_write_lock(hash);
  145. if (cp->flags & IP_VS_CONN_F_HASHED) {
  146. list_del(&cp->c_list);
  147. cp->flags &= ~IP_VS_CONN_F_HASHED;
  148. atomic_dec(&cp->refcnt);
  149. ret = 1;
  150. } else
  151. ret = 0;
  152. ct_write_unlock(hash);
  153. return ret;
  154. }
  155. /*
  156. * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
  157. * Called for pkts coming from OUTside-to-INside.
  158. * s_addr, s_port: pkt source address (foreign host)
  159. * d_addr, d_port: pkt dest address (load balancer)
  160. */
  161. static inline struct ip_vs_conn *__ip_vs_conn_in_get
  162. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  163. const union nf_inet_addr *d_addr, __be16 d_port)
  164. {
  165. unsigned hash;
  166. struct ip_vs_conn *cp;
  167. hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
  168. ct_read_lock(hash);
  169. list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
  170. if (cp->af == af &&
  171. ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
  172. ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
  173. s_port == cp->cport && d_port == cp->vport &&
  174. ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
  175. protocol == cp->protocol) {
  176. /* HIT */
  177. atomic_inc(&cp->refcnt);
  178. ct_read_unlock(hash);
  179. return cp;
  180. }
  181. }
  182. ct_read_unlock(hash);
  183. return NULL;
  184. }
  185. struct ip_vs_conn *ip_vs_conn_in_get
  186. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  187. const union nf_inet_addr *d_addr, __be16 d_port)
  188. {
  189. struct ip_vs_conn *cp;
  190. cp = __ip_vs_conn_in_get(af, protocol, s_addr, s_port, d_addr, d_port);
  191. if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
  192. cp = __ip_vs_conn_in_get(af, protocol, s_addr, 0, d_addr,
  193. d_port);
  194. IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
  195. ip_vs_proto_name(protocol),
  196. IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
  197. IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
  198. cp ? "hit" : "not hit");
  199. return cp;
  200. }
  201. /* Get reference to connection template */
  202. struct ip_vs_conn *ip_vs_ct_in_get
  203. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  204. const union nf_inet_addr *d_addr, __be16 d_port)
  205. {
  206. unsigned hash;
  207. struct ip_vs_conn *cp;
  208. hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
  209. ct_read_lock(hash);
  210. list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
  211. if (cp->af == af &&
  212. ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
  213. ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
  214. s_port == cp->cport && d_port == cp->vport &&
  215. cp->flags & IP_VS_CONN_F_TEMPLATE &&
  216. protocol == cp->protocol) {
  217. /* HIT */
  218. atomic_inc(&cp->refcnt);
  219. goto out;
  220. }
  221. }
  222. cp = NULL;
  223. out:
  224. ct_read_unlock(hash);
  225. IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
  226. ip_vs_proto_name(protocol),
  227. IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
  228. IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
  229. cp ? "hit" : "not hit");
  230. return cp;
  231. }
  232. /*
  233. * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
  234. * Called for pkts coming from inside-to-OUTside.
  235. * s_addr, s_port: pkt source address (inside host)
  236. * d_addr, d_port: pkt dest address (foreign host)
  237. */
  238. struct ip_vs_conn *ip_vs_conn_out_get
  239. (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
  240. const union nf_inet_addr *d_addr, __be16 d_port)
  241. {
  242. unsigned hash;
  243. struct ip_vs_conn *cp, *ret=NULL;
  244. /*
  245. * Check for "full" addressed entries
  246. */
  247. hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
  248. ct_read_lock(hash);
  249. list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
  250. if (cp->af == af &&
  251. ip_vs_addr_equal(af, d_addr, &cp->caddr) &&
  252. ip_vs_addr_equal(af, s_addr, &cp->daddr) &&
  253. d_port == cp->cport && s_port == cp->dport &&
  254. protocol == cp->protocol) {
  255. /* HIT */
  256. atomic_inc(&cp->refcnt);
  257. ret = cp;
  258. break;
  259. }
  260. }
  261. ct_read_unlock(hash);
  262. IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
  263. ip_vs_proto_name(protocol),
  264. IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
  265. IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
  266. ret ? "hit" : "not hit");
  267. return ret;
  268. }
  269. /*
  270. * Put back the conn and restart its timer with its timeout
  271. */
  272. void ip_vs_conn_put(struct ip_vs_conn *cp)
  273. {
  274. /* reset it expire in its timeout */
  275. mod_timer(&cp->timer, jiffies+cp->timeout);
  276. __ip_vs_conn_put(cp);
  277. }
  278. /*
  279. * Fill a no_client_port connection with a client port number
  280. */
  281. void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
  282. {
  283. if (ip_vs_conn_unhash(cp)) {
  284. spin_lock(&cp->lock);
  285. if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
  286. atomic_dec(&ip_vs_conn_no_cport_cnt);
  287. cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
  288. cp->cport = cport;
  289. }
  290. spin_unlock(&cp->lock);
  291. /* hash on new dport */
  292. ip_vs_conn_hash(cp);
  293. }
  294. }
  295. /*
  296. * Bind a connection entry with the corresponding packet_xmit.
  297. * Called by ip_vs_conn_new.
  298. */
  299. static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
  300. {
  301. switch (IP_VS_FWD_METHOD(cp)) {
  302. case IP_VS_CONN_F_MASQ:
  303. cp->packet_xmit = ip_vs_nat_xmit;
  304. break;
  305. case IP_VS_CONN_F_TUNNEL:
  306. cp->packet_xmit = ip_vs_tunnel_xmit;
  307. break;
  308. case IP_VS_CONN_F_DROUTE:
  309. cp->packet_xmit = ip_vs_dr_xmit;
  310. break;
  311. case IP_VS_CONN_F_LOCALNODE:
  312. cp->packet_xmit = ip_vs_null_xmit;
  313. break;
  314. case IP_VS_CONN_F_BYPASS:
  315. cp->packet_xmit = ip_vs_bypass_xmit;
  316. break;
  317. }
  318. }
  319. #ifdef CONFIG_IP_VS_IPV6
  320. static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
  321. {
  322. switch (IP_VS_FWD_METHOD(cp)) {
  323. case IP_VS_CONN_F_MASQ:
  324. cp->packet_xmit = ip_vs_nat_xmit_v6;
  325. break;
  326. case IP_VS_CONN_F_TUNNEL:
  327. cp->packet_xmit = ip_vs_tunnel_xmit_v6;
  328. break;
  329. case IP_VS_CONN_F_DROUTE:
  330. cp->packet_xmit = ip_vs_dr_xmit_v6;
  331. break;
  332. case IP_VS_CONN_F_LOCALNODE:
  333. cp->packet_xmit = ip_vs_null_xmit;
  334. break;
  335. case IP_VS_CONN_F_BYPASS:
  336. cp->packet_xmit = ip_vs_bypass_xmit_v6;
  337. break;
  338. }
  339. }
  340. #endif
  341. static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
  342. {
  343. return atomic_read(&dest->activeconns)
  344. + atomic_read(&dest->inactconns);
  345. }
  346. /*
  347. * Bind a connection entry with a virtual service destination
  348. * Called just after a new connection entry is created.
  349. */
  350. static inline void
  351. ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
  352. {
  353. /* if dest is NULL, then return directly */
  354. if (!dest)
  355. return;
  356. /* Increase the refcnt counter of the dest */
  357. atomic_inc(&dest->refcnt);
  358. /* Bind with the destination and its corresponding transmitter */
  359. if ((cp->flags & IP_VS_CONN_F_SYNC) &&
  360. (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
  361. /* if the connection is not template and is created
  362. * by sync, preserve the activity flag.
  363. */
  364. cp->flags |= atomic_read(&dest->conn_flags) &
  365. (~IP_VS_CONN_F_INACTIVE);
  366. else
  367. cp->flags |= atomic_read(&dest->conn_flags);
  368. cp->dest = dest;
  369. IP_VS_DBG(7, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  370. "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  371. "dest->refcnt:%d\n",
  372. ip_vs_proto_name(cp->protocol),
  373. NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
  374. NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
  375. NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
  376. ip_vs_fwd_tag(cp), cp->state,
  377. cp->flags, atomic_read(&cp->refcnt),
  378. atomic_read(&dest->refcnt));
  379. /* Update the connection counters */
  380. if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
  381. /* It is a normal connection, so increase the inactive
  382. connection counter because it is in TCP SYNRECV
  383. state (inactive) or other protocol inacive state */
  384. if ((cp->flags & IP_VS_CONN_F_SYNC) &&
  385. (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
  386. atomic_inc(&dest->activeconns);
  387. else
  388. atomic_inc(&dest->inactconns);
  389. } else {
  390. /* It is a persistent connection/template, so increase
  391. the peristent connection counter */
  392. atomic_inc(&dest->persistconns);
  393. }
  394. if (dest->u_threshold != 0 &&
  395. ip_vs_dest_totalconns(dest) >= dest->u_threshold)
  396. dest->flags |= IP_VS_DEST_F_OVERLOAD;
  397. }
  398. /*
  399. * Check if there is a destination for the connection, if so
  400. * bind the connection to the destination.
  401. */
  402. struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
  403. {
  404. struct ip_vs_dest *dest;
  405. if ((cp) && (!cp->dest)) {
  406. dest = ip_vs_find_dest(cp->daddr.ip, cp->dport,
  407. cp->vaddr.ip, cp->vport, cp->protocol);
  408. ip_vs_bind_dest(cp, dest);
  409. return dest;
  410. } else
  411. return NULL;
  412. }
  413. /*
  414. * Unbind a connection entry with its VS destination
  415. * Called by the ip_vs_conn_expire function.
  416. */
  417. static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
  418. {
  419. struct ip_vs_dest *dest = cp->dest;
  420. if (!dest)
  421. return;
  422. IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  423. "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  424. "dest->refcnt:%d\n",
  425. ip_vs_proto_name(cp->protocol),
  426. NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
  427. NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
  428. NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
  429. ip_vs_fwd_tag(cp), cp->state,
  430. cp->flags, atomic_read(&cp->refcnt),
  431. atomic_read(&dest->refcnt));
  432. /* Update the connection counters */
  433. if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
  434. /* It is a normal connection, so decrease the inactconns
  435. or activeconns counter */
  436. if (cp->flags & IP_VS_CONN_F_INACTIVE) {
  437. atomic_dec(&dest->inactconns);
  438. } else {
  439. atomic_dec(&dest->activeconns);
  440. }
  441. } else {
  442. /* It is a persistent connection/template, so decrease
  443. the peristent connection counter */
  444. atomic_dec(&dest->persistconns);
  445. }
  446. if (dest->l_threshold != 0) {
  447. if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
  448. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  449. } else if (dest->u_threshold != 0) {
  450. if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
  451. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  452. } else {
  453. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  454. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  455. }
  456. /*
  457. * Simply decrease the refcnt of the dest, because the
  458. * dest will be either in service's destination list
  459. * or in the trash.
  460. */
  461. atomic_dec(&dest->refcnt);
  462. }
  463. /*
  464. * Checking if the destination of a connection template is available.
  465. * If available, return 1, otherwise invalidate this connection
  466. * template and return 0.
  467. */
  468. int ip_vs_check_template(struct ip_vs_conn *ct)
  469. {
  470. struct ip_vs_dest *dest = ct->dest;
  471. /*
  472. * Checking the dest server status.
  473. */
  474. if ((dest == NULL) ||
  475. !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
  476. (sysctl_ip_vs_expire_quiescent_template &&
  477. (atomic_read(&dest->weight) == 0))) {
  478. IP_VS_DBG(9, "check_template: dest not available for "
  479. "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  480. "-> d:%u.%u.%u.%u:%d\n",
  481. ip_vs_proto_name(ct->protocol),
  482. NIPQUAD(ct->caddr.ip), ntohs(ct->cport),
  483. NIPQUAD(ct->vaddr.ip), ntohs(ct->vport),
  484. NIPQUAD(ct->daddr.ip), ntohs(ct->dport));
  485. /*
  486. * Invalidate the connection template
  487. */
  488. if (ct->vport != htons(0xffff)) {
  489. if (ip_vs_conn_unhash(ct)) {
  490. ct->dport = htons(0xffff);
  491. ct->vport = htons(0xffff);
  492. ct->cport = 0;
  493. ip_vs_conn_hash(ct);
  494. }
  495. }
  496. /*
  497. * Simply decrease the refcnt of the template,
  498. * don't restart its timer.
  499. */
  500. atomic_dec(&ct->refcnt);
  501. return 0;
  502. }
  503. return 1;
  504. }
  505. static void ip_vs_conn_expire(unsigned long data)
  506. {
  507. struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
  508. cp->timeout = 60*HZ;
  509. /*
  510. * hey, I'm using it
  511. */
  512. atomic_inc(&cp->refcnt);
  513. /*
  514. * do I control anybody?
  515. */
  516. if (atomic_read(&cp->n_control))
  517. goto expire_later;
  518. /*
  519. * unhash it if it is hashed in the conn table
  520. */
  521. if (!ip_vs_conn_unhash(cp))
  522. goto expire_later;
  523. /*
  524. * refcnt==1 implies I'm the only one referrer
  525. */
  526. if (likely(atomic_read(&cp->refcnt) == 1)) {
  527. /* delete the timer if it is activated by other users */
  528. if (timer_pending(&cp->timer))
  529. del_timer(&cp->timer);
  530. /* does anybody control me? */
  531. if (cp->control)
  532. ip_vs_control_del(cp);
  533. if (unlikely(cp->app != NULL))
  534. ip_vs_unbind_app(cp);
  535. ip_vs_unbind_dest(cp);
  536. if (cp->flags & IP_VS_CONN_F_NO_CPORT)
  537. atomic_dec(&ip_vs_conn_no_cport_cnt);
  538. atomic_dec(&ip_vs_conn_count);
  539. kmem_cache_free(ip_vs_conn_cachep, cp);
  540. return;
  541. }
  542. /* hash it back to the table */
  543. ip_vs_conn_hash(cp);
  544. expire_later:
  545. IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
  546. atomic_read(&cp->refcnt)-1,
  547. atomic_read(&cp->n_control));
  548. ip_vs_conn_put(cp);
  549. }
  550. void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
  551. {
  552. if (del_timer(&cp->timer))
  553. mod_timer(&cp->timer, jiffies);
  554. }
  555. /*
  556. * Create a new connection entry and hash it into the ip_vs_conn_tab
  557. */
  558. struct ip_vs_conn *
  559. ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
  560. const union nf_inet_addr *vaddr, __be16 vport,
  561. const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
  562. struct ip_vs_dest *dest)
  563. {
  564. struct ip_vs_conn *cp;
  565. struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
  566. cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
  567. if (cp == NULL) {
  568. IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
  569. return NULL;
  570. }
  571. INIT_LIST_HEAD(&cp->c_list);
  572. setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
  573. cp->af = af;
  574. cp->protocol = proto;
  575. ip_vs_addr_copy(af, &cp->caddr, caddr);
  576. cp->cport = cport;
  577. ip_vs_addr_copy(af, &cp->vaddr, vaddr);
  578. cp->vport = vport;
  579. ip_vs_addr_copy(af, &cp->daddr, daddr);
  580. cp->dport = dport;
  581. cp->flags = flags;
  582. spin_lock_init(&cp->lock);
  583. /*
  584. * Set the entry is referenced by the current thread before hashing
  585. * it in the table, so that other thread run ip_vs_random_dropentry
  586. * but cannot drop this entry.
  587. */
  588. atomic_set(&cp->refcnt, 1);
  589. atomic_set(&cp->n_control, 0);
  590. atomic_set(&cp->in_pkts, 0);
  591. atomic_inc(&ip_vs_conn_count);
  592. if (flags & IP_VS_CONN_F_NO_CPORT)
  593. atomic_inc(&ip_vs_conn_no_cport_cnt);
  594. /* Bind the connection with a destination server */
  595. ip_vs_bind_dest(cp, dest);
  596. /* Set its state and timeout */
  597. cp->state = 0;
  598. cp->timeout = 3*HZ;
  599. /* Bind its packet transmitter */
  600. #ifdef CONFIG_IP_VS_IPV6
  601. if (af == AF_INET6)
  602. ip_vs_bind_xmit_v6(cp);
  603. else
  604. #endif
  605. ip_vs_bind_xmit(cp);
  606. if (unlikely(pp && atomic_read(&pp->appcnt)))
  607. ip_vs_bind_app(cp, pp);
  608. /* Hash it in the ip_vs_conn_tab finally */
  609. ip_vs_conn_hash(cp);
  610. return cp;
  611. }
  612. /*
  613. * /proc/net/ip_vs_conn entries
  614. */
  615. #ifdef CONFIG_PROC_FS
  616. static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
  617. {
  618. int idx;
  619. struct ip_vs_conn *cp;
  620. for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
  621. ct_read_lock_bh(idx);
  622. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  623. if (pos-- == 0) {
  624. seq->private = &ip_vs_conn_tab[idx];
  625. return cp;
  626. }
  627. }
  628. ct_read_unlock_bh(idx);
  629. }
  630. return NULL;
  631. }
  632. static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
  633. {
  634. seq->private = NULL;
  635. return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
  636. }
  637. static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  638. {
  639. struct ip_vs_conn *cp = v;
  640. struct list_head *e, *l = seq->private;
  641. int idx;
  642. ++*pos;
  643. if (v == SEQ_START_TOKEN)
  644. return ip_vs_conn_array(seq, 0);
  645. /* more on same hash chain? */
  646. if ((e = cp->c_list.next) != l)
  647. return list_entry(e, struct ip_vs_conn, c_list);
  648. idx = l - ip_vs_conn_tab;
  649. ct_read_unlock_bh(idx);
  650. while (++idx < IP_VS_CONN_TAB_SIZE) {
  651. ct_read_lock_bh(idx);
  652. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  653. seq->private = &ip_vs_conn_tab[idx];
  654. return cp;
  655. }
  656. ct_read_unlock_bh(idx);
  657. }
  658. seq->private = NULL;
  659. return NULL;
  660. }
  661. static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
  662. {
  663. struct list_head *l = seq->private;
  664. if (l)
  665. ct_read_unlock_bh(l - ip_vs_conn_tab);
  666. }
  667. static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
  668. {
  669. if (v == SEQ_START_TOKEN)
  670. seq_puts(seq,
  671. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
  672. else {
  673. const struct ip_vs_conn *cp = v;
  674. seq_printf(seq,
  675. "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
  676. ip_vs_proto_name(cp->protocol),
  677. ntohl(cp->caddr.ip), ntohs(cp->cport),
  678. ntohl(cp->vaddr.ip), ntohs(cp->vport),
  679. ntohl(cp->daddr.ip), ntohs(cp->dport),
  680. ip_vs_state_name(cp->protocol, cp->state),
  681. (cp->timer.expires-jiffies)/HZ);
  682. }
  683. return 0;
  684. }
  685. static const struct seq_operations ip_vs_conn_seq_ops = {
  686. .start = ip_vs_conn_seq_start,
  687. .next = ip_vs_conn_seq_next,
  688. .stop = ip_vs_conn_seq_stop,
  689. .show = ip_vs_conn_seq_show,
  690. };
  691. static int ip_vs_conn_open(struct inode *inode, struct file *file)
  692. {
  693. return seq_open(file, &ip_vs_conn_seq_ops);
  694. }
  695. static const struct file_operations ip_vs_conn_fops = {
  696. .owner = THIS_MODULE,
  697. .open = ip_vs_conn_open,
  698. .read = seq_read,
  699. .llseek = seq_lseek,
  700. .release = seq_release,
  701. };
  702. static const char *ip_vs_origin_name(unsigned flags)
  703. {
  704. if (flags & IP_VS_CONN_F_SYNC)
  705. return "SYNC";
  706. else
  707. return "LOCAL";
  708. }
  709. static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
  710. {
  711. if (v == SEQ_START_TOKEN)
  712. seq_puts(seq,
  713. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
  714. else {
  715. const struct ip_vs_conn *cp = v;
  716. seq_printf(seq,
  717. "%-3s %08X %04X %08X %04X %08X %04X %-11s %-6s %7lu\n",
  718. ip_vs_proto_name(cp->protocol),
  719. ntohl(cp->caddr.ip), ntohs(cp->cport),
  720. ntohl(cp->vaddr.ip), ntohs(cp->vport),
  721. ntohl(cp->daddr.ip), ntohs(cp->dport),
  722. ip_vs_state_name(cp->protocol, cp->state),
  723. ip_vs_origin_name(cp->flags),
  724. (cp->timer.expires-jiffies)/HZ);
  725. }
  726. return 0;
  727. }
  728. static const struct seq_operations ip_vs_conn_sync_seq_ops = {
  729. .start = ip_vs_conn_seq_start,
  730. .next = ip_vs_conn_seq_next,
  731. .stop = ip_vs_conn_seq_stop,
  732. .show = ip_vs_conn_sync_seq_show,
  733. };
  734. static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
  735. {
  736. return seq_open(file, &ip_vs_conn_sync_seq_ops);
  737. }
  738. static const struct file_operations ip_vs_conn_sync_fops = {
  739. .owner = THIS_MODULE,
  740. .open = ip_vs_conn_sync_open,
  741. .read = seq_read,
  742. .llseek = seq_lseek,
  743. .release = seq_release,
  744. };
  745. #endif
  746. /*
  747. * Randomly drop connection entries before running out of memory
  748. */
  749. static inline int todrop_entry(struct ip_vs_conn *cp)
  750. {
  751. /*
  752. * The drop rate array needs tuning for real environments.
  753. * Called from timer bh only => no locking
  754. */
  755. static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  756. static char todrop_counter[9] = {0};
  757. int i;
  758. /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
  759. This will leave enough time for normal connection to get
  760. through. */
  761. if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
  762. return 0;
  763. /* Don't drop the entry if its number of incoming packets is not
  764. located in [0, 8] */
  765. i = atomic_read(&cp->in_pkts);
  766. if (i > 8 || i < 0) return 0;
  767. if (!todrop_rate[i]) return 0;
  768. if (--todrop_counter[i] > 0) return 0;
  769. todrop_counter[i] = todrop_rate[i];
  770. return 1;
  771. }
  772. /* Called from keventd and must protect itself from softirqs */
  773. void ip_vs_random_dropentry(void)
  774. {
  775. int idx;
  776. struct ip_vs_conn *cp;
  777. /*
  778. * Randomly scan 1/32 of the whole table every second
  779. */
  780. for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
  781. unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
  782. /*
  783. * Lock is actually needed in this loop.
  784. */
  785. ct_write_lock_bh(hash);
  786. list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
  787. if (cp->flags & IP_VS_CONN_F_TEMPLATE)
  788. /* connection template */
  789. continue;
  790. if (cp->protocol == IPPROTO_TCP) {
  791. switch(cp->state) {
  792. case IP_VS_TCP_S_SYN_RECV:
  793. case IP_VS_TCP_S_SYNACK:
  794. break;
  795. case IP_VS_TCP_S_ESTABLISHED:
  796. if (todrop_entry(cp))
  797. break;
  798. continue;
  799. default:
  800. continue;
  801. }
  802. } else {
  803. if (!todrop_entry(cp))
  804. continue;
  805. }
  806. IP_VS_DBG(4, "del connection\n");
  807. ip_vs_conn_expire_now(cp);
  808. if (cp->control) {
  809. IP_VS_DBG(4, "del conn template\n");
  810. ip_vs_conn_expire_now(cp->control);
  811. }
  812. }
  813. ct_write_unlock_bh(hash);
  814. }
  815. }
  816. /*
  817. * Flush all the connection entries in the ip_vs_conn_tab
  818. */
  819. static void ip_vs_conn_flush(void)
  820. {
  821. int idx;
  822. struct ip_vs_conn *cp;
  823. flush_again:
  824. for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
  825. /*
  826. * Lock is actually needed in this loop.
  827. */
  828. ct_write_lock_bh(idx);
  829. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  830. IP_VS_DBG(4, "del connection\n");
  831. ip_vs_conn_expire_now(cp);
  832. if (cp->control) {
  833. IP_VS_DBG(4, "del conn template\n");
  834. ip_vs_conn_expire_now(cp->control);
  835. }
  836. }
  837. ct_write_unlock_bh(idx);
  838. }
  839. /* the counter may be not NULL, because maybe some conn entries
  840. are run by slow timer handler or unhashed but still referred */
  841. if (atomic_read(&ip_vs_conn_count) != 0) {
  842. schedule();
  843. goto flush_again;
  844. }
  845. }
  846. int __init ip_vs_conn_init(void)
  847. {
  848. int idx;
  849. /*
  850. * Allocate the connection hash table and initialize its list heads
  851. */
  852. ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
  853. if (!ip_vs_conn_tab)
  854. return -ENOMEM;
  855. /* Allocate ip_vs_conn slab cache */
  856. ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
  857. sizeof(struct ip_vs_conn), 0,
  858. SLAB_HWCACHE_ALIGN, NULL);
  859. if (!ip_vs_conn_cachep) {
  860. vfree(ip_vs_conn_tab);
  861. return -ENOMEM;
  862. }
  863. IP_VS_INFO("Connection hash table configured "
  864. "(size=%d, memory=%ldKbytes)\n",
  865. IP_VS_CONN_TAB_SIZE,
  866. (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
  867. IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
  868. sizeof(struct ip_vs_conn));
  869. for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
  870. INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
  871. }
  872. for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
  873. rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
  874. }
  875. proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
  876. proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
  877. /* calculate the random value for connection hash */
  878. get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
  879. return 0;
  880. }
  881. void ip_vs_conn_cleanup(void)
  882. {
  883. /* flush all the connection entries first */
  884. ip_vs_conn_flush();
  885. /* Release the empty cache */
  886. kmem_cache_destroy(ip_vs_conn_cachep);
  887. proc_net_remove(&init_net, "ip_vs_conn");
  888. proc_net_remove(&init_net, "ip_vs_conn_sync");
  889. vfree(ip_vs_conn_tab);
  890. }