ip_vs_conn.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. if ((cp->flags & IP_VS_CONN_F_SYNC) &&
  320. (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
  321. /* if the connection is not template and is created
  322. * by sync, preserve the activity flag.
  323. */
  324. cp->flags |= atomic_read(&dest->conn_flags) &
  325. (~IP_VS_CONN_F_INACTIVE);
  326. else
  327. cp->flags |= atomic_read(&dest->conn_flags);
  328. cp->dest = dest;
  329. IP_VS_DBG(7, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  330. "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  331. "dest->refcnt:%d\n",
  332. ip_vs_proto_name(cp->protocol),
  333. NIPQUAD(cp->caddr), ntohs(cp->cport),
  334. NIPQUAD(cp->vaddr), ntohs(cp->vport),
  335. NIPQUAD(cp->daddr), ntohs(cp->dport),
  336. ip_vs_fwd_tag(cp), cp->state,
  337. cp->flags, atomic_read(&cp->refcnt),
  338. atomic_read(&dest->refcnt));
  339. /* Update the connection counters */
  340. if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
  341. /* It is a normal connection, so increase the inactive
  342. connection counter because it is in TCP SYNRECV
  343. state (inactive) or other protocol inacive state */
  344. if ((cp->flags & IP_VS_CONN_F_SYNC) &&
  345. (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
  346. atomic_inc(&dest->activeconns);
  347. else
  348. atomic_inc(&dest->inactconns);
  349. } else {
  350. /* It is a persistent connection/template, so increase
  351. the peristent connection counter */
  352. atomic_inc(&dest->persistconns);
  353. }
  354. if (dest->u_threshold != 0 &&
  355. ip_vs_dest_totalconns(dest) >= dest->u_threshold)
  356. dest->flags |= IP_VS_DEST_F_OVERLOAD;
  357. }
  358. /*
  359. * Check if there is a destination for the connection, if so
  360. * bind the connection to the destination.
  361. */
  362. struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
  363. {
  364. struct ip_vs_dest *dest;
  365. if ((cp) && (!cp->dest)) {
  366. dest = ip_vs_find_dest(cp->daddr, cp->dport,
  367. cp->vaddr, cp->vport, cp->protocol);
  368. ip_vs_bind_dest(cp, dest);
  369. return dest;
  370. } else
  371. return NULL;
  372. }
  373. /*
  374. * Unbind a connection entry with its VS destination
  375. * Called by the ip_vs_conn_expire function.
  376. */
  377. static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
  378. {
  379. struct ip_vs_dest *dest = cp->dest;
  380. if (!dest)
  381. return;
  382. IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  383. "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  384. "dest->refcnt:%d\n",
  385. ip_vs_proto_name(cp->protocol),
  386. NIPQUAD(cp->caddr), ntohs(cp->cport),
  387. NIPQUAD(cp->vaddr), ntohs(cp->vport),
  388. NIPQUAD(cp->daddr), ntohs(cp->dport),
  389. ip_vs_fwd_tag(cp), cp->state,
  390. cp->flags, atomic_read(&cp->refcnt),
  391. atomic_read(&dest->refcnt));
  392. /* Update the connection counters */
  393. if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
  394. /* It is a normal connection, so decrease the inactconns
  395. or activeconns counter */
  396. if (cp->flags & IP_VS_CONN_F_INACTIVE) {
  397. atomic_dec(&dest->inactconns);
  398. } else {
  399. atomic_dec(&dest->activeconns);
  400. }
  401. } else {
  402. /* It is a persistent connection/template, so decrease
  403. the peristent connection counter */
  404. atomic_dec(&dest->persistconns);
  405. }
  406. if (dest->l_threshold != 0) {
  407. if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
  408. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  409. } else if (dest->u_threshold != 0) {
  410. if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
  411. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  412. } else {
  413. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  414. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  415. }
  416. /*
  417. * Simply decrease the refcnt of the dest, because the
  418. * dest will be either in service's destination list
  419. * or in the trash.
  420. */
  421. atomic_dec(&dest->refcnt);
  422. }
  423. /*
  424. * Checking if the destination of a connection template is available.
  425. * If available, return 1, otherwise invalidate this connection
  426. * template and return 0.
  427. */
  428. int ip_vs_check_template(struct ip_vs_conn *ct)
  429. {
  430. struct ip_vs_dest *dest = ct->dest;
  431. /*
  432. * Checking the dest server status.
  433. */
  434. if ((dest == NULL) ||
  435. !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
  436. (sysctl_ip_vs_expire_quiescent_template &&
  437. (atomic_read(&dest->weight) == 0))) {
  438. IP_VS_DBG(9, "check_template: dest not available for "
  439. "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
  440. "-> d:%u.%u.%u.%u:%d\n",
  441. ip_vs_proto_name(ct->protocol),
  442. NIPQUAD(ct->caddr), ntohs(ct->cport),
  443. NIPQUAD(ct->vaddr), ntohs(ct->vport),
  444. NIPQUAD(ct->daddr), ntohs(ct->dport));
  445. /*
  446. * Invalidate the connection template
  447. */
  448. if (ct->vport != htons(0xffff)) {
  449. if (ip_vs_conn_unhash(ct)) {
  450. ct->dport = htons(0xffff);
  451. ct->vport = htons(0xffff);
  452. ct->cport = 0;
  453. ip_vs_conn_hash(ct);
  454. }
  455. }
  456. /*
  457. * Simply decrease the refcnt of the template,
  458. * don't restart its timer.
  459. */
  460. atomic_dec(&ct->refcnt);
  461. return 0;
  462. }
  463. return 1;
  464. }
  465. static void ip_vs_conn_expire(unsigned long data)
  466. {
  467. struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
  468. cp->timeout = 60*HZ;
  469. /*
  470. * hey, I'm using it
  471. */
  472. atomic_inc(&cp->refcnt);
  473. /*
  474. * do I control anybody?
  475. */
  476. if (atomic_read(&cp->n_control))
  477. goto expire_later;
  478. /*
  479. * unhash it if it is hashed in the conn table
  480. */
  481. if (!ip_vs_conn_unhash(cp))
  482. goto expire_later;
  483. /*
  484. * refcnt==1 implies I'm the only one referrer
  485. */
  486. if (likely(atomic_read(&cp->refcnt) == 1)) {
  487. /* delete the timer if it is activated by other users */
  488. if (timer_pending(&cp->timer))
  489. del_timer(&cp->timer);
  490. /* does anybody control me? */
  491. if (cp->control)
  492. ip_vs_control_del(cp);
  493. if (unlikely(cp->app != NULL))
  494. ip_vs_unbind_app(cp);
  495. ip_vs_unbind_dest(cp);
  496. if (cp->flags & IP_VS_CONN_F_NO_CPORT)
  497. atomic_dec(&ip_vs_conn_no_cport_cnt);
  498. atomic_dec(&ip_vs_conn_count);
  499. kmem_cache_free(ip_vs_conn_cachep, cp);
  500. return;
  501. }
  502. /* hash it back to the table */
  503. ip_vs_conn_hash(cp);
  504. expire_later:
  505. IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
  506. atomic_read(&cp->refcnt)-1,
  507. atomic_read(&cp->n_control));
  508. ip_vs_conn_put(cp);
  509. }
  510. void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
  511. {
  512. if (del_timer(&cp->timer))
  513. mod_timer(&cp->timer, jiffies);
  514. }
  515. /*
  516. * Create a new connection entry and hash it into the ip_vs_conn_tab
  517. */
  518. struct ip_vs_conn *
  519. ip_vs_conn_new(int proto, __be32 caddr, __be16 cport, __be32 vaddr, __be16 vport,
  520. __be32 daddr, __be16 dport, unsigned flags,
  521. struct ip_vs_dest *dest)
  522. {
  523. struct ip_vs_conn *cp;
  524. struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
  525. cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
  526. if (cp == NULL) {
  527. IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
  528. return NULL;
  529. }
  530. INIT_LIST_HEAD(&cp->c_list);
  531. setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
  532. cp->protocol = proto;
  533. cp->caddr = caddr;
  534. cp->cport = cport;
  535. cp->vaddr = vaddr;
  536. cp->vport = vport;
  537. cp->daddr = daddr;
  538. cp->dport = dport;
  539. cp->flags = flags;
  540. spin_lock_init(&cp->lock);
  541. /*
  542. * Set the entry is referenced by the current thread before hashing
  543. * it in the table, so that other thread run ip_vs_random_dropentry
  544. * but cannot drop this entry.
  545. */
  546. atomic_set(&cp->refcnt, 1);
  547. atomic_set(&cp->n_control, 0);
  548. atomic_set(&cp->in_pkts, 0);
  549. atomic_inc(&ip_vs_conn_count);
  550. if (flags & IP_VS_CONN_F_NO_CPORT)
  551. atomic_inc(&ip_vs_conn_no_cport_cnt);
  552. /* Bind the connection with a destination server */
  553. ip_vs_bind_dest(cp, dest);
  554. /* Set its state and timeout */
  555. cp->state = 0;
  556. cp->timeout = 3*HZ;
  557. /* Bind its packet transmitter */
  558. ip_vs_bind_xmit(cp);
  559. if (unlikely(pp && atomic_read(&pp->appcnt)))
  560. ip_vs_bind_app(cp, pp);
  561. /* Hash it in the ip_vs_conn_tab finally */
  562. ip_vs_conn_hash(cp);
  563. return cp;
  564. }
  565. /*
  566. * /proc/net/ip_vs_conn entries
  567. */
  568. #ifdef CONFIG_PROC_FS
  569. static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
  570. {
  571. int idx;
  572. struct ip_vs_conn *cp;
  573. for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
  574. ct_read_lock_bh(idx);
  575. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  576. if (pos-- == 0) {
  577. seq->private = &ip_vs_conn_tab[idx];
  578. return cp;
  579. }
  580. }
  581. ct_read_unlock_bh(idx);
  582. }
  583. return NULL;
  584. }
  585. static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
  586. {
  587. seq->private = NULL;
  588. return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
  589. }
  590. static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  591. {
  592. struct ip_vs_conn *cp = v;
  593. struct list_head *e, *l = seq->private;
  594. int idx;
  595. ++*pos;
  596. if (v == SEQ_START_TOKEN)
  597. return ip_vs_conn_array(seq, 0);
  598. /* more on same hash chain? */
  599. if ((e = cp->c_list.next) != l)
  600. return list_entry(e, struct ip_vs_conn, c_list);
  601. idx = l - ip_vs_conn_tab;
  602. ct_read_unlock_bh(idx);
  603. while (++idx < IP_VS_CONN_TAB_SIZE) {
  604. ct_read_lock_bh(idx);
  605. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  606. seq->private = &ip_vs_conn_tab[idx];
  607. return cp;
  608. }
  609. ct_read_unlock_bh(idx);
  610. }
  611. seq->private = NULL;
  612. return NULL;
  613. }
  614. static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
  615. {
  616. struct list_head *l = seq->private;
  617. if (l)
  618. ct_read_unlock_bh(l - ip_vs_conn_tab);
  619. }
  620. static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
  621. {
  622. if (v == SEQ_START_TOKEN)
  623. seq_puts(seq,
  624. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
  625. else {
  626. const struct ip_vs_conn *cp = v;
  627. seq_printf(seq,
  628. "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
  629. ip_vs_proto_name(cp->protocol),
  630. ntohl(cp->caddr), ntohs(cp->cport),
  631. ntohl(cp->vaddr), ntohs(cp->vport),
  632. ntohl(cp->daddr), ntohs(cp->dport),
  633. ip_vs_state_name(cp->protocol, cp->state),
  634. (cp->timer.expires-jiffies)/HZ);
  635. }
  636. return 0;
  637. }
  638. static const struct seq_operations ip_vs_conn_seq_ops = {
  639. .start = ip_vs_conn_seq_start,
  640. .next = ip_vs_conn_seq_next,
  641. .stop = ip_vs_conn_seq_stop,
  642. .show = ip_vs_conn_seq_show,
  643. };
  644. static int ip_vs_conn_open(struct inode *inode, struct file *file)
  645. {
  646. return seq_open(file, &ip_vs_conn_seq_ops);
  647. }
  648. static const struct file_operations ip_vs_conn_fops = {
  649. .owner = THIS_MODULE,
  650. .open = ip_vs_conn_open,
  651. .read = seq_read,
  652. .llseek = seq_lseek,
  653. .release = seq_release,
  654. };
  655. static const char *ip_vs_origin_name(unsigned flags)
  656. {
  657. if (flags & IP_VS_CONN_F_SYNC)
  658. return "SYNC";
  659. else
  660. return "LOCAL";
  661. }
  662. static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
  663. {
  664. if (v == SEQ_START_TOKEN)
  665. seq_puts(seq,
  666. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
  667. else {
  668. const struct ip_vs_conn *cp = v;
  669. seq_printf(seq,
  670. "%-3s %08X %04X %08X %04X %08X %04X %-11s %-6s %7lu\n",
  671. ip_vs_proto_name(cp->protocol),
  672. ntohl(cp->caddr), ntohs(cp->cport),
  673. ntohl(cp->vaddr), ntohs(cp->vport),
  674. ntohl(cp->daddr), ntohs(cp->dport),
  675. ip_vs_state_name(cp->protocol, cp->state),
  676. ip_vs_origin_name(cp->flags),
  677. (cp->timer.expires-jiffies)/HZ);
  678. }
  679. return 0;
  680. }
  681. static const struct seq_operations ip_vs_conn_sync_seq_ops = {
  682. .start = ip_vs_conn_seq_start,
  683. .next = ip_vs_conn_seq_next,
  684. .stop = ip_vs_conn_seq_stop,
  685. .show = ip_vs_conn_sync_seq_show,
  686. };
  687. static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
  688. {
  689. return seq_open(file, &ip_vs_conn_sync_seq_ops);
  690. }
  691. static const struct file_operations ip_vs_conn_sync_fops = {
  692. .owner = THIS_MODULE,
  693. .open = ip_vs_conn_sync_open,
  694. .read = seq_read,
  695. .llseek = seq_lseek,
  696. .release = seq_release,
  697. };
  698. #endif
  699. /*
  700. * Randomly drop connection entries before running out of memory
  701. */
  702. static inline int todrop_entry(struct ip_vs_conn *cp)
  703. {
  704. /*
  705. * The drop rate array needs tuning for real environments.
  706. * Called from timer bh only => no locking
  707. */
  708. static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  709. static char todrop_counter[9] = {0};
  710. int i;
  711. /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
  712. This will leave enough time for normal connection to get
  713. through. */
  714. if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
  715. return 0;
  716. /* Don't drop the entry if its number of incoming packets is not
  717. located in [0, 8] */
  718. i = atomic_read(&cp->in_pkts);
  719. if (i > 8 || i < 0) return 0;
  720. if (!todrop_rate[i]) return 0;
  721. if (--todrop_counter[i] > 0) return 0;
  722. todrop_counter[i] = todrop_rate[i];
  723. return 1;
  724. }
  725. /* Called from keventd and must protect itself from softirqs */
  726. void ip_vs_random_dropentry(void)
  727. {
  728. int idx;
  729. struct ip_vs_conn *cp;
  730. /*
  731. * Randomly scan 1/32 of the whole table every second
  732. */
  733. for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
  734. unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
  735. /*
  736. * Lock is actually needed in this loop.
  737. */
  738. ct_write_lock_bh(hash);
  739. list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
  740. if (cp->flags & IP_VS_CONN_F_TEMPLATE)
  741. /* connection template */
  742. continue;
  743. if (cp->protocol == IPPROTO_TCP) {
  744. switch(cp->state) {
  745. case IP_VS_TCP_S_SYN_RECV:
  746. case IP_VS_TCP_S_SYNACK:
  747. break;
  748. case IP_VS_TCP_S_ESTABLISHED:
  749. if (todrop_entry(cp))
  750. break;
  751. continue;
  752. default:
  753. continue;
  754. }
  755. } else {
  756. if (!todrop_entry(cp))
  757. continue;
  758. }
  759. IP_VS_DBG(4, "del connection\n");
  760. ip_vs_conn_expire_now(cp);
  761. if (cp->control) {
  762. IP_VS_DBG(4, "del conn template\n");
  763. ip_vs_conn_expire_now(cp->control);
  764. }
  765. }
  766. ct_write_unlock_bh(hash);
  767. }
  768. }
  769. /*
  770. * Flush all the connection entries in the ip_vs_conn_tab
  771. */
  772. static void ip_vs_conn_flush(void)
  773. {
  774. int idx;
  775. struct ip_vs_conn *cp;
  776. flush_again:
  777. for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
  778. /*
  779. * Lock is actually needed in this loop.
  780. */
  781. ct_write_lock_bh(idx);
  782. list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
  783. IP_VS_DBG(4, "del connection\n");
  784. ip_vs_conn_expire_now(cp);
  785. if (cp->control) {
  786. IP_VS_DBG(4, "del conn template\n");
  787. ip_vs_conn_expire_now(cp->control);
  788. }
  789. }
  790. ct_write_unlock_bh(idx);
  791. }
  792. /* the counter may be not NULL, because maybe some conn entries
  793. are run by slow timer handler or unhashed but still referred */
  794. if (atomic_read(&ip_vs_conn_count) != 0) {
  795. schedule();
  796. goto flush_again;
  797. }
  798. }
  799. int ip_vs_conn_init(void)
  800. {
  801. int idx;
  802. /*
  803. * Allocate the connection hash table and initialize its list heads
  804. */
  805. ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
  806. if (!ip_vs_conn_tab)
  807. return -ENOMEM;
  808. /* Allocate ip_vs_conn slab cache */
  809. ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
  810. sizeof(struct ip_vs_conn), 0,
  811. SLAB_HWCACHE_ALIGN, NULL);
  812. if (!ip_vs_conn_cachep) {
  813. vfree(ip_vs_conn_tab);
  814. return -ENOMEM;
  815. }
  816. IP_VS_INFO("Connection hash table configured "
  817. "(size=%d, memory=%ldKbytes)\n",
  818. IP_VS_CONN_TAB_SIZE,
  819. (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
  820. IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
  821. sizeof(struct ip_vs_conn));
  822. for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
  823. INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
  824. }
  825. for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
  826. rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
  827. }
  828. proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
  829. proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
  830. /* calculate the random value for connection hash */
  831. get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
  832. return 0;
  833. }
  834. void ip_vs_conn_cleanup(void)
  835. {
  836. /* flush all the connection entries first */
  837. ip_vs_conn_flush();
  838. /* Release the empty cache */
  839. kmem_cache_destroy(ip_vs_conn_cachep);
  840. proc_net_remove(&init_net, "ip_vs_conn");
  841. proc_net_remove(&init_net, "ip_vs_conn_sync");
  842. vfree(ip_vs_conn_tab);
  843. }