ip_vs_conn.c 24 KB

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