ip_vs_conn.c 22 KB

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