ip_vs_conn.c 25 KB

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