ip_vs_conn.c 22 KB

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