ip_vs_conn.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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->af, &cp->daddr, cp->dport,
  407. &cp->vaddr, cp->vport,
  408. cp->protocol);
  409. ip_vs_bind_dest(cp, dest);
  410. return dest;
  411. } else
  412. return NULL;
  413. }
  414. /*
  415. * Unbind a connection entry with its VS destination
  416. * Called by the ip_vs_conn_expire function.
  417. */
  418. static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
  419. {
  420. struct ip_vs_dest *dest = cp->dest;
  421. if (!dest)
  422. return;
  423. IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  424. "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  425. "dest->refcnt:%d\n",
  426. ip_vs_proto_name(cp->protocol),
  427. NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
  428. NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
  429. NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
  430. ip_vs_fwd_tag(cp), cp->state,
  431. cp->flags, atomic_read(&cp->refcnt),
  432. atomic_read(&dest->refcnt));
  433. /* Update the connection counters */
  434. if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
  435. /* It is a normal connection, so decrease the inactconns
  436. or activeconns counter */
  437. if (cp->flags & IP_VS_CONN_F_INACTIVE) {
  438. atomic_dec(&dest->inactconns);
  439. } else {
  440. atomic_dec(&dest->activeconns);
  441. }
  442. } else {
  443. /* It is a persistent connection/template, so decrease
  444. the peristent connection counter */
  445. atomic_dec(&dest->persistconns);
  446. }
  447. if (dest->l_threshold != 0) {
  448. if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
  449. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  450. } else if (dest->u_threshold != 0) {
  451. if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
  452. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  453. } else {
  454. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  455. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  456. }
  457. /*
  458. * Simply decrease the refcnt of the dest, because the
  459. * dest will be either in service's destination list
  460. * or in the trash.
  461. */
  462. atomic_dec(&dest->refcnt);
  463. }
  464. /*
  465. * Checking if the destination of a connection template is available.
  466. * If available, return 1, otherwise invalidate this connection
  467. * template and return 0.
  468. */
  469. int ip_vs_check_template(struct ip_vs_conn *ct)
  470. {
  471. struct ip_vs_dest *dest = ct->dest;
  472. /*
  473. * Checking the dest server status.
  474. */
  475. if ((dest == NULL) ||
  476. !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
  477. (sysctl_ip_vs_expire_quiescent_template &&
  478. (atomic_read(&dest->weight) == 0))) {
  479. IP_VS_DBG(9, "check_template: dest not available for "
  480. "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  481. "-> d:%u.%u.%u.%u:%d\n",
  482. ip_vs_proto_name(ct->protocol),
  483. NIPQUAD(ct->caddr.ip), ntohs(ct->cport),
  484. NIPQUAD(ct->vaddr.ip), ntohs(ct->vport),
  485. NIPQUAD(ct->daddr.ip), ntohs(ct->dport));
  486. /*
  487. * Invalidate the connection template
  488. */
  489. if (ct->vport != htons(0xffff)) {
  490. if (ip_vs_conn_unhash(ct)) {
  491. ct->dport = htons(0xffff);
  492. ct->vport = htons(0xffff);
  493. ct->cport = 0;
  494. ip_vs_conn_hash(ct);
  495. }
  496. }
  497. /*
  498. * Simply decrease the refcnt of the template,
  499. * don't restart its timer.
  500. */
  501. atomic_dec(&ct->refcnt);
  502. return 0;
  503. }
  504. return 1;
  505. }
  506. static void ip_vs_conn_expire(unsigned long data)
  507. {
  508. struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
  509. cp->timeout = 60*HZ;
  510. /*
  511. * hey, I'm using it
  512. */
  513. atomic_inc(&cp->refcnt);
  514. /*
  515. * do I control anybody?
  516. */
  517. if (atomic_read(&cp->n_control))
  518. goto expire_later;
  519. /*
  520. * unhash it if it is hashed in the conn table
  521. */
  522. if (!ip_vs_conn_unhash(cp))
  523. goto expire_later;
  524. /*
  525. * refcnt==1 implies I'm the only one referrer
  526. */
  527. if (likely(atomic_read(&cp->refcnt) == 1)) {
  528. /* delete the timer if it is activated by other users */
  529. if (timer_pending(&cp->timer))
  530. del_timer(&cp->timer);
  531. /* does anybody control me? */
  532. if (cp->control)
  533. ip_vs_control_del(cp);
  534. if (unlikely(cp->app != NULL))
  535. ip_vs_unbind_app(cp);
  536. ip_vs_unbind_dest(cp);
  537. if (cp->flags & IP_VS_CONN_F_NO_CPORT)
  538. atomic_dec(&ip_vs_conn_no_cport_cnt);
  539. atomic_dec(&ip_vs_conn_count);
  540. kmem_cache_free(ip_vs_conn_cachep, cp);
  541. return;
  542. }
  543. /* hash it back to the table */
  544. ip_vs_conn_hash(cp);
  545. expire_later:
  546. IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
  547. atomic_read(&cp->refcnt)-1,
  548. atomic_read(&cp->n_control));
  549. ip_vs_conn_put(cp);
  550. }
  551. void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
  552. {
  553. if (del_timer(&cp->timer))
  554. mod_timer(&cp->timer, jiffies);
  555. }
  556. /*
  557. * Create a new connection entry and hash it into the ip_vs_conn_tab
  558. */
  559. struct ip_vs_conn *
  560. ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
  561. const union nf_inet_addr *vaddr, __be16 vport,
  562. const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
  563. struct ip_vs_dest *dest)
  564. {
  565. struct ip_vs_conn *cp;
  566. struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
  567. cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
  568. if (cp == NULL) {
  569. IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
  570. return NULL;
  571. }
  572. INIT_LIST_HEAD(&cp->c_list);
  573. setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
  574. cp->af = af;
  575. cp->protocol = proto;
  576. ip_vs_addr_copy(af, &cp->caddr, caddr);
  577. cp->cport = cport;
  578. ip_vs_addr_copy(af, &cp->vaddr, vaddr);
  579. cp->vport = vport;
  580. ip_vs_addr_copy(af, &cp->daddr, daddr);
  581. cp->dport = dport;
  582. cp->flags = flags;
  583. spin_lock_init(&cp->lock);
  584. /*
  585. * Set the entry is referenced by the current thread before hashing
  586. * it in the table, so that other thread run ip_vs_random_dropentry
  587. * but cannot drop this entry.
  588. */
  589. atomic_set(&cp->refcnt, 1);
  590. atomic_set(&cp->n_control, 0);
  591. atomic_set(&cp->in_pkts, 0);
  592. atomic_inc(&ip_vs_conn_count);
  593. if (flags & IP_VS_CONN_F_NO_CPORT)
  594. atomic_inc(&ip_vs_conn_no_cport_cnt);
  595. /* Bind the connection with a destination server */
  596. ip_vs_bind_dest(cp, dest);
  597. /* Set its state and timeout */
  598. cp->state = 0;
  599. cp->timeout = 3*HZ;
  600. /* Bind its packet transmitter */
  601. #ifdef CONFIG_IP_VS_IPV6
  602. if (af == AF_INET6)
  603. ip_vs_bind_xmit_v6(cp);
  604. else
  605. #endif
  606. ip_vs_bind_xmit(cp);
  607. if (unlikely(pp && atomic_read(&pp->appcnt)))
  608. ip_vs_bind_app(cp, pp);
  609. /* Hash it in the ip_vs_conn_tab finally */
  610. ip_vs_conn_hash(cp);
  611. return cp;
  612. }
  613. /*
  614. * /proc/net/ip_vs_conn entries
  615. */
  616. #ifdef CONFIG_PROC_FS
  617. static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
  618. {
  619. int idx;
  620. struct ip_vs_conn *cp;
  621. for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
  622. ct_read_lock_bh(idx);
  623. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  624. if (pos-- == 0) {
  625. seq->private = &ip_vs_conn_tab[idx];
  626. return cp;
  627. }
  628. }
  629. ct_read_unlock_bh(idx);
  630. }
  631. return NULL;
  632. }
  633. static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
  634. {
  635. seq->private = NULL;
  636. return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
  637. }
  638. static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  639. {
  640. struct ip_vs_conn *cp = v;
  641. struct list_head *e, *l = seq->private;
  642. int idx;
  643. ++*pos;
  644. if (v == SEQ_START_TOKEN)
  645. return ip_vs_conn_array(seq, 0);
  646. /* more on same hash chain? */
  647. if ((e = cp->c_list.next) != l)
  648. return list_entry(e, struct ip_vs_conn, c_list);
  649. idx = l - ip_vs_conn_tab;
  650. ct_read_unlock_bh(idx);
  651. while (++idx < IP_VS_CONN_TAB_SIZE) {
  652. ct_read_lock_bh(idx);
  653. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  654. seq->private = &ip_vs_conn_tab[idx];
  655. return cp;
  656. }
  657. ct_read_unlock_bh(idx);
  658. }
  659. seq->private = NULL;
  660. return NULL;
  661. }
  662. static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
  663. {
  664. struct list_head *l = seq->private;
  665. if (l)
  666. ct_read_unlock_bh(l - ip_vs_conn_tab);
  667. }
  668. static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
  669. {
  670. if (v == SEQ_START_TOKEN)
  671. seq_puts(seq,
  672. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
  673. else {
  674. const struct ip_vs_conn *cp = v;
  675. seq_printf(seq,
  676. "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
  677. ip_vs_proto_name(cp->protocol),
  678. ntohl(cp->caddr.ip), ntohs(cp->cport),
  679. ntohl(cp->vaddr.ip), ntohs(cp->vport),
  680. ntohl(cp->daddr.ip), ntohs(cp->dport),
  681. ip_vs_state_name(cp->protocol, cp->state),
  682. (cp->timer.expires-jiffies)/HZ);
  683. }
  684. return 0;
  685. }
  686. static const struct seq_operations ip_vs_conn_seq_ops = {
  687. .start = ip_vs_conn_seq_start,
  688. .next = ip_vs_conn_seq_next,
  689. .stop = ip_vs_conn_seq_stop,
  690. .show = ip_vs_conn_seq_show,
  691. };
  692. static int ip_vs_conn_open(struct inode *inode, struct file *file)
  693. {
  694. return seq_open(file, &ip_vs_conn_seq_ops);
  695. }
  696. static const struct file_operations ip_vs_conn_fops = {
  697. .owner = THIS_MODULE,
  698. .open = ip_vs_conn_open,
  699. .read = seq_read,
  700. .llseek = seq_lseek,
  701. .release = seq_release,
  702. };
  703. static const char *ip_vs_origin_name(unsigned flags)
  704. {
  705. if (flags & IP_VS_CONN_F_SYNC)
  706. return "SYNC";
  707. else
  708. return "LOCAL";
  709. }
  710. static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
  711. {
  712. if (v == SEQ_START_TOKEN)
  713. seq_puts(seq,
  714. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
  715. else {
  716. const struct ip_vs_conn *cp = v;
  717. seq_printf(seq,
  718. "%-3s %08X %04X %08X %04X %08X %04X %-11s %-6s %7lu\n",
  719. ip_vs_proto_name(cp->protocol),
  720. ntohl(cp->caddr.ip), ntohs(cp->cport),
  721. ntohl(cp->vaddr.ip), ntohs(cp->vport),
  722. ntohl(cp->daddr.ip), ntohs(cp->dport),
  723. ip_vs_state_name(cp->protocol, cp->state),
  724. ip_vs_origin_name(cp->flags),
  725. (cp->timer.expires-jiffies)/HZ);
  726. }
  727. return 0;
  728. }
  729. static const struct seq_operations ip_vs_conn_sync_seq_ops = {
  730. .start = ip_vs_conn_seq_start,
  731. .next = ip_vs_conn_seq_next,
  732. .stop = ip_vs_conn_seq_stop,
  733. .show = ip_vs_conn_sync_seq_show,
  734. };
  735. static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
  736. {
  737. return seq_open(file, &ip_vs_conn_sync_seq_ops);
  738. }
  739. static const struct file_operations ip_vs_conn_sync_fops = {
  740. .owner = THIS_MODULE,
  741. .open = ip_vs_conn_sync_open,
  742. .read = seq_read,
  743. .llseek = seq_lseek,
  744. .release = seq_release,
  745. };
  746. #endif
  747. /*
  748. * Randomly drop connection entries before running out of memory
  749. */
  750. static inline int todrop_entry(struct ip_vs_conn *cp)
  751. {
  752. /*
  753. * The drop rate array needs tuning for real environments.
  754. * Called from timer bh only => no locking
  755. */
  756. static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  757. static char todrop_counter[9] = {0};
  758. int i;
  759. /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
  760. This will leave enough time for normal connection to get
  761. through. */
  762. if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
  763. return 0;
  764. /* Don't drop the entry if its number of incoming packets is not
  765. located in [0, 8] */
  766. i = atomic_read(&cp->in_pkts);
  767. if (i > 8 || i < 0) return 0;
  768. if (!todrop_rate[i]) return 0;
  769. if (--todrop_counter[i] > 0) return 0;
  770. todrop_counter[i] = todrop_rate[i];
  771. return 1;
  772. }
  773. /* Called from keventd and must protect itself from softirqs */
  774. void ip_vs_random_dropentry(void)
  775. {
  776. int idx;
  777. struct ip_vs_conn *cp;
  778. /*
  779. * Randomly scan 1/32 of the whole table every second
  780. */
  781. for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
  782. unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
  783. /*
  784. * Lock is actually needed in this loop.
  785. */
  786. ct_write_lock_bh(hash);
  787. list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
  788. if (cp->flags & IP_VS_CONN_F_TEMPLATE)
  789. /* connection template */
  790. continue;
  791. if (cp->protocol == IPPROTO_TCP) {
  792. switch(cp->state) {
  793. case IP_VS_TCP_S_SYN_RECV:
  794. case IP_VS_TCP_S_SYNACK:
  795. break;
  796. case IP_VS_TCP_S_ESTABLISHED:
  797. if (todrop_entry(cp))
  798. break;
  799. continue;
  800. default:
  801. continue;
  802. }
  803. } else {
  804. if (!todrop_entry(cp))
  805. continue;
  806. }
  807. IP_VS_DBG(4, "del connection\n");
  808. ip_vs_conn_expire_now(cp);
  809. if (cp->control) {
  810. IP_VS_DBG(4, "del conn template\n");
  811. ip_vs_conn_expire_now(cp->control);
  812. }
  813. }
  814. ct_write_unlock_bh(hash);
  815. }
  816. }
  817. /*
  818. * Flush all the connection entries in the ip_vs_conn_tab
  819. */
  820. static void ip_vs_conn_flush(void)
  821. {
  822. int idx;
  823. struct ip_vs_conn *cp;
  824. flush_again:
  825. for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
  826. /*
  827. * Lock is actually needed in this loop.
  828. */
  829. ct_write_lock_bh(idx);
  830. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  831. IP_VS_DBG(4, "del connection\n");
  832. ip_vs_conn_expire_now(cp);
  833. if (cp->control) {
  834. IP_VS_DBG(4, "del conn template\n");
  835. ip_vs_conn_expire_now(cp->control);
  836. }
  837. }
  838. ct_write_unlock_bh(idx);
  839. }
  840. /* the counter may be not NULL, because maybe some conn entries
  841. are run by slow timer handler or unhashed but still referred */
  842. if (atomic_read(&ip_vs_conn_count) != 0) {
  843. schedule();
  844. goto flush_again;
  845. }
  846. }
  847. int __init ip_vs_conn_init(void)
  848. {
  849. int idx;
  850. /*
  851. * Allocate the connection hash table and initialize its list heads
  852. */
  853. ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
  854. if (!ip_vs_conn_tab)
  855. return -ENOMEM;
  856. /* Allocate ip_vs_conn slab cache */
  857. ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
  858. sizeof(struct ip_vs_conn), 0,
  859. SLAB_HWCACHE_ALIGN, NULL);
  860. if (!ip_vs_conn_cachep) {
  861. vfree(ip_vs_conn_tab);
  862. return -ENOMEM;
  863. }
  864. IP_VS_INFO("Connection hash table configured "
  865. "(size=%d, memory=%ldKbytes)\n",
  866. IP_VS_CONN_TAB_SIZE,
  867. (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
  868. IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
  869. sizeof(struct ip_vs_conn));
  870. for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
  871. INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
  872. }
  873. for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
  874. rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
  875. }
  876. proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
  877. proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
  878. /* calculate the random value for connection hash */
  879. get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
  880. return 0;
  881. }
  882. void ip_vs_conn_cleanup(void)
  883. {
  884. /* flush all the connection entries first */
  885. ip_vs_conn_flush();
  886. /* Release the empty cache */
  887. kmem_cache_destroy(ip_vs_conn_cachep);
  888. proc_net_remove(&init_net, "ip_vs_conn");
  889. proc_net_remove(&init_net, "ip_vs_conn_sync");
  890. vfree(ip_vs_conn_tab);
  891. }