ip_vs_conn.c 22 KB

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