/net/netfilter/ipvs/ip_vs_conn.c

http://github.com/mirrors/linux · C · 1444 lines · 1006 code · 214 blank · 224 comment · 189 complexity · 3dfe6a20c050e4f4a67e5af186019125 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPVS An implementation of the IP virtual server support for the
  4. * LINUX operating system. IPVS is now implemented as a module
  5. * over the Netfilter framework. IPVS can be used to build a
  6. * high-performance and highly available server based on a
  7. * cluster of servers.
  8. *
  9. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  10. * Peter Kese <peter.kese@ijs.si>
  11. * Julian Anastasov <ja@ssi.bg>
  12. *
  13. * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
  14. * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
  15. * and others. Many code here is taken from IP MASQ code of kernel 2.2.
  16. *
  17. * Changes:
  18. */
  19. #define KMSG_COMPONENT "IPVS"
  20. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  21. #include <linux/interrupt.h>
  22. #include <linux/in.h>
  23. #include <linux/inet.h>
  24. #include <linux/net.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/proc_fs.h> /* for proc_net_* */
  29. #include <linux/slab.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/jhash.h>
  32. #include <linux/random.h>
  33. #include <net/net_namespace.h>
  34. #include <net/ip_vs.h>
  35. #ifndef CONFIG_IP_VS_TAB_BITS
  36. #define CONFIG_IP_VS_TAB_BITS 12
  37. #endif
  38. /*
  39. * Connection hash size. Default is what was selected at compile time.
  40. */
  41. static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
  42. module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
  43. MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
  44. /* size and mask values */
  45. int ip_vs_conn_tab_size __read_mostly;
  46. static int ip_vs_conn_tab_mask __read_mostly;
  47. /*
  48. * Connection hash table: for input and output packets lookups of IPVS
  49. */
  50. static struct hlist_head *ip_vs_conn_tab __read_mostly;
  51. /* SLAB cache for IPVS connections */
  52. static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
  53. /* counter for no client port connections */
  54. static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
  55. /* random value for IPVS connection hash */
  56. static unsigned int ip_vs_conn_rnd __read_mostly;
  57. /*
  58. * Fine locking granularity for big connection hash table
  59. */
  60. #define CT_LOCKARRAY_BITS 5
  61. #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
  62. #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
  63. /* We need an addrstrlen that works with or without v6 */
  64. #ifdef CONFIG_IP_VS_IPV6
  65. #define IP_VS_ADDRSTRLEN INET6_ADDRSTRLEN
  66. #else
  67. #define IP_VS_ADDRSTRLEN (8+1)
  68. #endif
  69. struct ip_vs_aligned_lock
  70. {
  71. spinlock_t l;
  72. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  73. /* lock array for conn table */
  74. static struct ip_vs_aligned_lock
  75. __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
  76. static inline void ct_write_lock_bh(unsigned int key)
  77. {
  78. spin_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  79. }
  80. static inline void ct_write_unlock_bh(unsigned int key)
  81. {
  82. spin_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
  83. }
  84. static void ip_vs_conn_expire(struct timer_list *t);
  85. /*
  86. * Returns hash value for IPVS connection entry
  87. */
  88. static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
  89. const union nf_inet_addr *addr,
  90. __be16 port)
  91. {
  92. #ifdef CONFIG_IP_VS_IPV6
  93. if (af == AF_INET6)
  94. return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
  95. (__force u32)port, proto, ip_vs_conn_rnd) ^
  96. ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
  97. #endif
  98. return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
  99. ip_vs_conn_rnd) ^
  100. ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
  101. }
  102. static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
  103. bool inverse)
  104. {
  105. const union nf_inet_addr *addr;
  106. __be16 port;
  107. if (p->pe_data && p->pe->hashkey_raw)
  108. return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
  109. ip_vs_conn_tab_mask;
  110. if (likely(!inverse)) {
  111. addr = p->caddr;
  112. port = p->cport;
  113. } else {
  114. addr = p->vaddr;
  115. port = p->vport;
  116. }
  117. return ip_vs_conn_hashkey(p->ipvs, p->af, p->protocol, addr, port);
  118. }
  119. static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
  120. {
  121. struct ip_vs_conn_param p;
  122. ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol,
  123. &cp->caddr, cp->cport, NULL, 0, &p);
  124. if (cp->pe) {
  125. p.pe = cp->pe;
  126. p.pe_data = cp->pe_data;
  127. p.pe_data_len = cp->pe_data_len;
  128. }
  129. return ip_vs_conn_hashkey_param(&p, false);
  130. }
  131. /*
  132. * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
  133. * returns bool success.
  134. */
  135. static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
  136. {
  137. unsigned int hash;
  138. int ret;
  139. if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
  140. return 0;
  141. /* Hash by protocol, client address and port */
  142. hash = ip_vs_conn_hashkey_conn(cp);
  143. ct_write_lock_bh(hash);
  144. spin_lock(&cp->lock);
  145. if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
  146. cp->flags |= IP_VS_CONN_F_HASHED;
  147. refcount_inc(&cp->refcnt);
  148. hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
  149. ret = 1;
  150. } else {
  151. pr_err("%s(): request for already hashed, called from %pS\n",
  152. __func__, __builtin_return_address(0));
  153. ret = 0;
  154. }
  155. spin_unlock(&cp->lock);
  156. ct_write_unlock_bh(hash);
  157. return ret;
  158. }
  159. /*
  160. * UNhashes ip_vs_conn from ip_vs_conn_tab.
  161. * returns bool success. Caller should hold conn reference.
  162. */
  163. static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
  164. {
  165. unsigned int hash;
  166. int ret;
  167. /* unhash it and decrease its reference counter */
  168. hash = ip_vs_conn_hashkey_conn(cp);
  169. ct_write_lock_bh(hash);
  170. spin_lock(&cp->lock);
  171. if (cp->flags & IP_VS_CONN_F_HASHED) {
  172. hlist_del_rcu(&cp->c_list);
  173. cp->flags &= ~IP_VS_CONN_F_HASHED;
  174. refcount_dec(&cp->refcnt);
  175. ret = 1;
  176. } else
  177. ret = 0;
  178. spin_unlock(&cp->lock);
  179. ct_write_unlock_bh(hash);
  180. return ret;
  181. }
  182. /* Try to unlink ip_vs_conn from ip_vs_conn_tab.
  183. * returns bool success.
  184. */
  185. static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
  186. {
  187. unsigned int hash;
  188. bool ret = false;
  189. if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
  190. return refcount_dec_if_one(&cp->refcnt);
  191. hash = ip_vs_conn_hashkey_conn(cp);
  192. ct_write_lock_bh(hash);
  193. spin_lock(&cp->lock);
  194. if (cp->flags & IP_VS_CONN_F_HASHED) {
  195. /* Decrease refcnt and unlink conn only if we are last user */
  196. if (refcount_dec_if_one(&cp->refcnt)) {
  197. hlist_del_rcu(&cp->c_list);
  198. cp->flags &= ~IP_VS_CONN_F_HASHED;
  199. ret = true;
  200. }
  201. }
  202. spin_unlock(&cp->lock);
  203. ct_write_unlock_bh(hash);
  204. return ret;
  205. }
  206. /*
  207. * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
  208. * Called for pkts coming from OUTside-to-INside.
  209. * p->caddr, p->cport: pkt source address (foreign host)
  210. * p->vaddr, p->vport: pkt dest address (load balancer)
  211. */
  212. static inline struct ip_vs_conn *
  213. __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
  214. {
  215. unsigned int hash;
  216. struct ip_vs_conn *cp;
  217. hash = ip_vs_conn_hashkey_param(p, false);
  218. rcu_read_lock();
  219. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
  220. if (p->cport == cp->cport && p->vport == cp->vport &&
  221. cp->af == p->af &&
  222. ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
  223. ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
  224. ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
  225. p->protocol == cp->protocol &&
  226. cp->ipvs == p->ipvs) {
  227. if (!__ip_vs_conn_get(cp))
  228. continue;
  229. /* HIT */
  230. rcu_read_unlock();
  231. return cp;
  232. }
  233. }
  234. rcu_read_unlock();
  235. return NULL;
  236. }
  237. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
  238. {
  239. struct ip_vs_conn *cp;
  240. cp = __ip_vs_conn_in_get(p);
  241. if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
  242. struct ip_vs_conn_param cport_zero_p = *p;
  243. cport_zero_p.cport = 0;
  244. cp = __ip_vs_conn_in_get(&cport_zero_p);
  245. }
  246. IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
  247. ip_vs_proto_name(p->protocol),
  248. IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
  249. IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
  250. cp ? "hit" : "not hit");
  251. return cp;
  252. }
  253. static int
  254. ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
  255. int af, const struct sk_buff *skb,
  256. const struct ip_vs_iphdr *iph,
  257. struct ip_vs_conn_param *p)
  258. {
  259. __be16 _ports[2], *pptr;
  260. pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
  261. if (pptr == NULL)
  262. return 1;
  263. if (likely(!ip_vs_iph_inverse(iph)))
  264. ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr,
  265. pptr[0], &iph->daddr, pptr[1], p);
  266. else
  267. ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr,
  268. pptr[1], &iph->saddr, pptr[0], p);
  269. return 0;
  270. }
  271. struct ip_vs_conn *
  272. ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
  273. const struct sk_buff *skb,
  274. const struct ip_vs_iphdr *iph)
  275. {
  276. struct ip_vs_conn_param p;
  277. if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
  278. return NULL;
  279. return ip_vs_conn_in_get(&p);
  280. }
  281. EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
  282. /* Get reference to connection template */
  283. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
  284. {
  285. unsigned int hash;
  286. struct ip_vs_conn *cp;
  287. hash = ip_vs_conn_hashkey_param(p, false);
  288. rcu_read_lock();
  289. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
  290. if (unlikely(p->pe_data && p->pe->ct_match)) {
  291. if (cp->ipvs != p->ipvs)
  292. continue;
  293. if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
  294. if (__ip_vs_conn_get(cp))
  295. goto out;
  296. }
  297. continue;
  298. }
  299. if (cp->af == p->af &&
  300. ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
  301. /* protocol should only be IPPROTO_IP if
  302. * p->vaddr is a fwmark */
  303. ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
  304. p->af, p->vaddr, &cp->vaddr) &&
  305. p->vport == cp->vport && p->cport == cp->cport &&
  306. cp->flags & IP_VS_CONN_F_TEMPLATE &&
  307. p->protocol == cp->protocol &&
  308. cp->ipvs == p->ipvs) {
  309. if (__ip_vs_conn_get(cp))
  310. goto out;
  311. }
  312. }
  313. cp = NULL;
  314. out:
  315. rcu_read_unlock();
  316. IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
  317. ip_vs_proto_name(p->protocol),
  318. IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
  319. IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
  320. cp ? "hit" : "not hit");
  321. return cp;
  322. }
  323. /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
  324. * Called for pkts coming from inside-to-OUTside.
  325. * p->caddr, p->cport: pkt source address (inside host)
  326. * p->vaddr, p->vport: pkt dest address (foreign host) */
  327. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
  328. {
  329. unsigned int hash;
  330. struct ip_vs_conn *cp, *ret=NULL;
  331. /*
  332. * Check for "full" addressed entries
  333. */
  334. hash = ip_vs_conn_hashkey_param(p, true);
  335. rcu_read_lock();
  336. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
  337. if (p->vport == cp->cport && p->cport == cp->dport &&
  338. cp->af == p->af &&
  339. ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
  340. ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
  341. p->protocol == cp->protocol &&
  342. cp->ipvs == p->ipvs) {
  343. if (!__ip_vs_conn_get(cp))
  344. continue;
  345. /* HIT */
  346. ret = cp;
  347. break;
  348. }
  349. }
  350. rcu_read_unlock();
  351. IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
  352. ip_vs_proto_name(p->protocol),
  353. IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
  354. IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
  355. ret ? "hit" : "not hit");
  356. return ret;
  357. }
  358. struct ip_vs_conn *
  359. ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
  360. const struct sk_buff *skb,
  361. const struct ip_vs_iphdr *iph)
  362. {
  363. struct ip_vs_conn_param p;
  364. if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
  365. return NULL;
  366. return ip_vs_conn_out_get(&p);
  367. }
  368. EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
  369. /*
  370. * Put back the conn and restart its timer with its timeout
  371. */
  372. static void __ip_vs_conn_put_timer(struct ip_vs_conn *cp)
  373. {
  374. unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
  375. 0 : cp->timeout;
  376. mod_timer(&cp->timer, jiffies+t);
  377. __ip_vs_conn_put(cp);
  378. }
  379. void ip_vs_conn_put(struct ip_vs_conn *cp)
  380. {
  381. if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) &&
  382. (refcount_read(&cp->refcnt) == 1) &&
  383. !timer_pending(&cp->timer))
  384. /* expire connection immediately */
  385. ip_vs_conn_expire(&cp->timer);
  386. else
  387. __ip_vs_conn_put_timer(cp);
  388. }
  389. /*
  390. * Fill a no_client_port connection with a client port number
  391. */
  392. void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
  393. {
  394. if (ip_vs_conn_unhash(cp)) {
  395. spin_lock_bh(&cp->lock);
  396. if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
  397. atomic_dec(&ip_vs_conn_no_cport_cnt);
  398. cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
  399. cp->cport = cport;
  400. }
  401. spin_unlock_bh(&cp->lock);
  402. /* hash on new dport */
  403. ip_vs_conn_hash(cp);
  404. }
  405. }
  406. /*
  407. * Bind a connection entry with the corresponding packet_xmit.
  408. * Called by ip_vs_conn_new.
  409. */
  410. static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
  411. {
  412. switch (IP_VS_FWD_METHOD(cp)) {
  413. case IP_VS_CONN_F_MASQ:
  414. cp->packet_xmit = ip_vs_nat_xmit;
  415. break;
  416. case IP_VS_CONN_F_TUNNEL:
  417. #ifdef CONFIG_IP_VS_IPV6
  418. if (cp->daf == AF_INET6)
  419. cp->packet_xmit = ip_vs_tunnel_xmit_v6;
  420. else
  421. #endif
  422. cp->packet_xmit = ip_vs_tunnel_xmit;
  423. break;
  424. case IP_VS_CONN_F_DROUTE:
  425. cp->packet_xmit = ip_vs_dr_xmit;
  426. break;
  427. case IP_VS_CONN_F_LOCALNODE:
  428. cp->packet_xmit = ip_vs_null_xmit;
  429. break;
  430. case IP_VS_CONN_F_BYPASS:
  431. cp->packet_xmit = ip_vs_bypass_xmit;
  432. break;
  433. }
  434. }
  435. #ifdef CONFIG_IP_VS_IPV6
  436. static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
  437. {
  438. switch (IP_VS_FWD_METHOD(cp)) {
  439. case IP_VS_CONN_F_MASQ:
  440. cp->packet_xmit = ip_vs_nat_xmit_v6;
  441. break;
  442. case IP_VS_CONN_F_TUNNEL:
  443. if (cp->daf == AF_INET6)
  444. cp->packet_xmit = ip_vs_tunnel_xmit_v6;
  445. else
  446. cp->packet_xmit = ip_vs_tunnel_xmit;
  447. break;
  448. case IP_VS_CONN_F_DROUTE:
  449. cp->packet_xmit = ip_vs_dr_xmit_v6;
  450. break;
  451. case IP_VS_CONN_F_LOCALNODE:
  452. cp->packet_xmit = ip_vs_null_xmit;
  453. break;
  454. case IP_VS_CONN_F_BYPASS:
  455. cp->packet_xmit = ip_vs_bypass_xmit_v6;
  456. break;
  457. }
  458. }
  459. #endif
  460. static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
  461. {
  462. return atomic_read(&dest->activeconns)
  463. + atomic_read(&dest->inactconns);
  464. }
  465. /*
  466. * Bind a connection entry with a virtual service destination
  467. * Called just after a new connection entry is created.
  468. */
  469. static inline void
  470. ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
  471. {
  472. unsigned int conn_flags;
  473. __u32 flags;
  474. /* if dest is NULL, then return directly */
  475. if (!dest)
  476. return;
  477. /* Increase the refcnt counter of the dest */
  478. ip_vs_dest_hold(dest);
  479. conn_flags = atomic_read(&dest->conn_flags);
  480. if (cp->protocol != IPPROTO_UDP)
  481. conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
  482. flags = cp->flags;
  483. /* Bind with the destination and its corresponding transmitter */
  484. if (flags & IP_VS_CONN_F_SYNC) {
  485. /* if the connection is not template and is created
  486. * by sync, preserve the activity flag.
  487. */
  488. if (!(flags & IP_VS_CONN_F_TEMPLATE))
  489. conn_flags &= ~IP_VS_CONN_F_INACTIVE;
  490. /* connections inherit forwarding method from dest */
  491. flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
  492. }
  493. flags |= conn_flags;
  494. cp->flags = flags;
  495. cp->dest = dest;
  496. IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
  497. "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  498. "dest->refcnt:%d\n",
  499. ip_vs_proto_name(cp->protocol),
  500. IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
  501. IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
  502. IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
  503. ip_vs_fwd_tag(cp), cp->state,
  504. cp->flags, refcount_read(&cp->refcnt),
  505. refcount_read(&dest->refcnt));
  506. /* Update the connection counters */
  507. if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
  508. /* It is a normal connection, so modify the counters
  509. * according to the flags, later the protocol can
  510. * update them on state change
  511. */
  512. if (!(flags & IP_VS_CONN_F_INACTIVE))
  513. atomic_inc(&dest->activeconns);
  514. else
  515. atomic_inc(&dest->inactconns);
  516. } else {
  517. /* It is a persistent connection/template, so increase
  518. the persistent connection counter */
  519. atomic_inc(&dest->persistconns);
  520. }
  521. if (dest->u_threshold != 0 &&
  522. ip_vs_dest_totalconns(dest) >= dest->u_threshold)
  523. dest->flags |= IP_VS_DEST_F_OVERLOAD;
  524. }
  525. /*
  526. * Check if there is a destination for the connection, if so
  527. * bind the connection to the destination.
  528. */
  529. void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
  530. {
  531. struct ip_vs_dest *dest;
  532. rcu_read_lock();
  533. /* This function is only invoked by the synchronization code. We do
  534. * not currently support heterogeneous pools with synchronization,
  535. * so we can make the assumption that the svc_af is the same as the
  536. * dest_af
  537. */
  538. dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
  539. cp->dport, &cp->vaddr, cp->vport,
  540. cp->protocol, cp->fwmark, cp->flags);
  541. if (dest) {
  542. struct ip_vs_proto_data *pd;
  543. spin_lock_bh(&cp->lock);
  544. if (cp->dest) {
  545. spin_unlock_bh(&cp->lock);
  546. rcu_read_unlock();
  547. return;
  548. }
  549. /* Applications work depending on the forwarding method
  550. * but better to reassign them always when binding dest */
  551. if (cp->app)
  552. ip_vs_unbind_app(cp);
  553. ip_vs_bind_dest(cp, dest);
  554. spin_unlock_bh(&cp->lock);
  555. /* Update its packet transmitter */
  556. cp->packet_xmit = NULL;
  557. #ifdef CONFIG_IP_VS_IPV6
  558. if (cp->af == AF_INET6)
  559. ip_vs_bind_xmit_v6(cp);
  560. else
  561. #endif
  562. ip_vs_bind_xmit(cp);
  563. pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
  564. if (pd && atomic_read(&pd->appcnt))
  565. ip_vs_bind_app(cp, pd->pp);
  566. }
  567. rcu_read_unlock();
  568. }
  569. /*
  570. * Unbind a connection entry with its VS destination
  571. * Called by the ip_vs_conn_expire function.
  572. */
  573. static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
  574. {
  575. struct ip_vs_dest *dest = cp->dest;
  576. if (!dest)
  577. return;
  578. IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
  579. "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
  580. "dest->refcnt:%d\n",
  581. ip_vs_proto_name(cp->protocol),
  582. IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
  583. IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
  584. IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
  585. ip_vs_fwd_tag(cp), cp->state,
  586. cp->flags, refcount_read(&cp->refcnt),
  587. refcount_read(&dest->refcnt));
  588. /* Update the connection counters */
  589. if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
  590. /* It is a normal connection, so decrease the inactconns
  591. or activeconns counter */
  592. if (cp->flags & IP_VS_CONN_F_INACTIVE) {
  593. atomic_dec(&dest->inactconns);
  594. } else {
  595. atomic_dec(&dest->activeconns);
  596. }
  597. } else {
  598. /* It is a persistent connection/template, so decrease
  599. the persistent connection counter */
  600. atomic_dec(&dest->persistconns);
  601. }
  602. if (dest->l_threshold != 0) {
  603. if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
  604. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  605. } else if (dest->u_threshold != 0) {
  606. if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
  607. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  608. } else {
  609. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  610. dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
  611. }
  612. ip_vs_dest_put(dest);
  613. }
  614. static int expire_quiescent_template(struct netns_ipvs *ipvs,
  615. struct ip_vs_dest *dest)
  616. {
  617. #ifdef CONFIG_SYSCTL
  618. return ipvs->sysctl_expire_quiescent_template &&
  619. (atomic_read(&dest->weight) == 0);
  620. #else
  621. return 0;
  622. #endif
  623. }
  624. /*
  625. * Checking if the destination of a connection template is available.
  626. * If available, return 1, otherwise invalidate this connection
  627. * template and return 0.
  628. */
  629. int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest)
  630. {
  631. struct ip_vs_dest *dest = ct->dest;
  632. struct netns_ipvs *ipvs = ct->ipvs;
  633. /*
  634. * Checking the dest server status.
  635. */
  636. if ((dest == NULL) ||
  637. !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
  638. expire_quiescent_template(ipvs, dest) ||
  639. (cdest && (dest != cdest))) {
  640. IP_VS_DBG_BUF(9, "check_template: dest not available for "
  641. "protocol %s s:%s:%d v:%s:%d "
  642. "-> d:%s:%d\n",
  643. ip_vs_proto_name(ct->protocol),
  644. IP_VS_DBG_ADDR(ct->af, &ct->caddr),
  645. ntohs(ct->cport),
  646. IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
  647. ntohs(ct->vport),
  648. IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
  649. ntohs(ct->dport));
  650. /*
  651. * Invalidate the connection template
  652. */
  653. if (ct->vport != htons(0xffff)) {
  654. if (ip_vs_conn_unhash(ct)) {
  655. ct->dport = htons(0xffff);
  656. ct->vport = htons(0xffff);
  657. ct->cport = 0;
  658. ip_vs_conn_hash(ct);
  659. }
  660. }
  661. /*
  662. * Simply decrease the refcnt of the template,
  663. * don't restart its timer.
  664. */
  665. __ip_vs_conn_put(ct);
  666. return 0;
  667. }
  668. return 1;
  669. }
  670. static void ip_vs_conn_rcu_free(struct rcu_head *head)
  671. {
  672. struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
  673. rcu_head);
  674. ip_vs_pe_put(cp->pe);
  675. kfree(cp->pe_data);
  676. kmem_cache_free(ip_vs_conn_cachep, cp);
  677. }
  678. static void ip_vs_conn_expire(struct timer_list *t)
  679. {
  680. struct ip_vs_conn *cp = from_timer(cp, t, timer);
  681. struct netns_ipvs *ipvs = cp->ipvs;
  682. /*
  683. * do I control anybody?
  684. */
  685. if (atomic_read(&cp->n_control))
  686. goto expire_later;
  687. /* Unlink conn if not referenced anymore */
  688. if (likely(ip_vs_conn_unlink(cp))) {
  689. struct ip_vs_conn *ct = cp->control;
  690. /* delete the timer if it is activated by other users */
  691. del_timer(&cp->timer);
  692. /* does anybody control me? */
  693. if (ct) {
  694. ip_vs_control_del(cp);
  695. /* Drop CTL or non-assured TPL if not used anymore */
  696. if (!cp->timeout && !atomic_read(&ct->n_control) &&
  697. (!(ct->flags & IP_VS_CONN_F_TEMPLATE) ||
  698. !(ct->state & IP_VS_CTPL_S_ASSURED))) {
  699. IP_VS_DBG(4, "drop controlling connection\n");
  700. ct->timeout = 0;
  701. ip_vs_conn_expire_now(ct);
  702. }
  703. }
  704. if ((cp->flags & IP_VS_CONN_F_NFCT) &&
  705. !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) {
  706. /* Do not access conntracks during subsys cleanup
  707. * because nf_conntrack_find_get can not be used after
  708. * conntrack cleanup for the net.
  709. */
  710. smp_rmb();
  711. if (ipvs->enable)
  712. ip_vs_conn_drop_conntrack(cp);
  713. }
  714. if (unlikely(cp->app != NULL))
  715. ip_vs_unbind_app(cp);
  716. ip_vs_unbind_dest(cp);
  717. if (cp->flags & IP_VS_CONN_F_NO_CPORT)
  718. atomic_dec(&ip_vs_conn_no_cport_cnt);
  719. if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
  720. ip_vs_conn_rcu_free(&cp->rcu_head);
  721. else
  722. call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
  723. atomic_dec(&ipvs->conn_count);
  724. return;
  725. }
  726. expire_later:
  727. IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
  728. refcount_read(&cp->refcnt),
  729. atomic_read(&cp->n_control));
  730. refcount_inc(&cp->refcnt);
  731. cp->timeout = 60*HZ;
  732. if (ipvs->sync_state & IP_VS_STATE_MASTER)
  733. ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
  734. __ip_vs_conn_put_timer(cp);
  735. }
  736. /* Modify timer, so that it expires as soon as possible.
  737. * Can be called without reference only if under RCU lock.
  738. * We can have such chain of conns linked with ->control: DATA->CTL->TPL
  739. * - DATA (eg. FTP) and TPL (persistence) can be present depending on setup
  740. * - cp->timeout=0 indicates all conns from chain should be dropped but
  741. * TPL is not dropped if in assured state
  742. */
  743. void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
  744. {
  745. /* Using mod_timer_pending will ensure the timer is not
  746. * modified after the final del_timer in ip_vs_conn_expire.
  747. */
  748. if (timer_pending(&cp->timer) &&
  749. time_after(cp->timer.expires, jiffies))
  750. mod_timer_pending(&cp->timer, jiffies);
  751. }
  752. /*
  753. * Create a new connection entry and hash it into the ip_vs_conn_tab
  754. */
  755. struct ip_vs_conn *
  756. ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
  757. const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
  758. struct ip_vs_dest *dest, __u32 fwmark)
  759. {
  760. struct ip_vs_conn *cp;
  761. struct netns_ipvs *ipvs = p->ipvs;
  762. struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
  763. p->protocol);
  764. cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
  765. if (cp == NULL) {
  766. IP_VS_ERR_RL("%s(): no memory\n", __func__);
  767. return NULL;
  768. }
  769. INIT_HLIST_NODE(&cp->c_list);
  770. timer_setup(&cp->timer, ip_vs_conn_expire, 0);
  771. cp->ipvs = ipvs;
  772. cp->af = p->af;
  773. cp->daf = dest_af;
  774. cp->protocol = p->protocol;
  775. ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
  776. cp->cport = p->cport;
  777. /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
  778. ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
  779. &cp->vaddr, p->vaddr);
  780. cp->vport = p->vport;
  781. ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
  782. cp->dport = dport;
  783. cp->flags = flags;
  784. cp->fwmark = fwmark;
  785. if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
  786. ip_vs_pe_get(p->pe);
  787. cp->pe = p->pe;
  788. cp->pe_data = p->pe_data;
  789. cp->pe_data_len = p->pe_data_len;
  790. } else {
  791. cp->pe = NULL;
  792. cp->pe_data = NULL;
  793. cp->pe_data_len = 0;
  794. }
  795. spin_lock_init(&cp->lock);
  796. /*
  797. * Set the entry is referenced by the current thread before hashing
  798. * it in the table, so that other thread run ip_vs_random_dropentry
  799. * but cannot drop this entry.
  800. */
  801. refcount_set(&cp->refcnt, 1);
  802. cp->control = NULL;
  803. atomic_set(&cp->n_control, 0);
  804. atomic_set(&cp->in_pkts, 0);
  805. cp->packet_xmit = NULL;
  806. cp->app = NULL;
  807. cp->app_data = NULL;
  808. /* reset struct ip_vs_seq */
  809. cp->in_seq.delta = 0;
  810. cp->out_seq.delta = 0;
  811. atomic_inc(&ipvs->conn_count);
  812. if (flags & IP_VS_CONN_F_NO_CPORT)
  813. atomic_inc(&ip_vs_conn_no_cport_cnt);
  814. /* Bind the connection with a destination server */
  815. cp->dest = NULL;
  816. ip_vs_bind_dest(cp, dest);
  817. /* Set its state and timeout */
  818. cp->state = 0;
  819. cp->old_state = 0;
  820. cp->timeout = 3*HZ;
  821. cp->sync_endtime = jiffies & ~3UL;
  822. /* Bind its packet transmitter */
  823. #ifdef CONFIG_IP_VS_IPV6
  824. if (p->af == AF_INET6)
  825. ip_vs_bind_xmit_v6(cp);
  826. else
  827. #endif
  828. ip_vs_bind_xmit(cp);
  829. if (unlikely(pd && atomic_read(&pd->appcnt)))
  830. ip_vs_bind_app(cp, pd->pp);
  831. /*
  832. * Allow conntrack to be preserved. By default, conntrack
  833. * is created and destroyed for every packet.
  834. * Sometimes keeping conntrack can be useful for
  835. * IP_VS_CONN_F_ONE_PACKET too.
  836. */
  837. if (ip_vs_conntrack_enabled(ipvs))
  838. cp->flags |= IP_VS_CONN_F_NFCT;
  839. /* Hash it in the ip_vs_conn_tab finally */
  840. ip_vs_conn_hash(cp);
  841. return cp;
  842. }
  843. /*
  844. * /proc/net/ip_vs_conn entries
  845. */
  846. #ifdef CONFIG_PROC_FS
  847. struct ip_vs_iter_state {
  848. struct seq_net_private p;
  849. struct hlist_head *l;
  850. };
  851. static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
  852. {
  853. int idx;
  854. struct ip_vs_conn *cp;
  855. struct ip_vs_iter_state *iter = seq->private;
  856. for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
  857. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
  858. /* __ip_vs_conn_get() is not needed by
  859. * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
  860. */
  861. if (pos-- == 0) {
  862. iter->l = &ip_vs_conn_tab[idx];
  863. return cp;
  864. }
  865. }
  866. cond_resched_rcu();
  867. }
  868. return NULL;
  869. }
  870. static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
  871. __acquires(RCU)
  872. {
  873. struct ip_vs_iter_state *iter = seq->private;
  874. iter->l = NULL;
  875. rcu_read_lock();
  876. return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
  877. }
  878. static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  879. {
  880. struct ip_vs_conn *cp = v;
  881. struct ip_vs_iter_state *iter = seq->private;
  882. struct hlist_node *e;
  883. struct hlist_head *l = iter->l;
  884. int idx;
  885. ++*pos;
  886. if (v == SEQ_START_TOKEN)
  887. return ip_vs_conn_array(seq, 0);
  888. /* more on same hash chain? */
  889. e = rcu_dereference(hlist_next_rcu(&cp->c_list));
  890. if (e)
  891. return hlist_entry(e, struct ip_vs_conn, c_list);
  892. idx = l - ip_vs_conn_tab;
  893. while (++idx < ip_vs_conn_tab_size) {
  894. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
  895. iter->l = &ip_vs_conn_tab[idx];
  896. return cp;
  897. }
  898. cond_resched_rcu();
  899. }
  900. iter->l = NULL;
  901. return NULL;
  902. }
  903. static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
  904. __releases(RCU)
  905. {
  906. rcu_read_unlock();
  907. }
  908. static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
  909. {
  910. if (v == SEQ_START_TOKEN)
  911. seq_puts(seq,
  912. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
  913. else {
  914. const struct ip_vs_conn *cp = v;
  915. struct net *net = seq_file_net(seq);
  916. char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
  917. size_t len = 0;
  918. char dbuf[IP_VS_ADDRSTRLEN];
  919. if (!net_eq(cp->ipvs->net, net))
  920. return 0;
  921. if (cp->pe_data) {
  922. pe_data[0] = ' ';
  923. len = strlen(cp->pe->name);
  924. memcpy(pe_data + 1, cp->pe->name, len);
  925. pe_data[len + 1] = ' ';
  926. len += 2;
  927. len += cp->pe->show_pe_data(cp, pe_data + len);
  928. }
  929. pe_data[len] = '\0';
  930. #ifdef CONFIG_IP_VS_IPV6
  931. if (cp->daf == AF_INET6)
  932. snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
  933. else
  934. #endif
  935. snprintf(dbuf, sizeof(dbuf), "%08X",
  936. ntohl(cp->daddr.ip));
  937. #ifdef CONFIG_IP_VS_IPV6
  938. if (cp->af == AF_INET6)
  939. seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
  940. "%s %04X %-11s %7u%s\n",
  941. ip_vs_proto_name(cp->protocol),
  942. &cp->caddr.in6, ntohs(cp->cport),
  943. &cp->vaddr.in6, ntohs(cp->vport),
  944. dbuf, ntohs(cp->dport),
  945. ip_vs_state_name(cp),
  946. jiffies_delta_to_msecs(cp->timer.expires -
  947. jiffies) / 1000,
  948. pe_data);
  949. else
  950. #endif
  951. seq_printf(seq,
  952. "%-3s %08X %04X %08X %04X"
  953. " %s %04X %-11s %7u%s\n",
  954. ip_vs_proto_name(cp->protocol),
  955. ntohl(cp->caddr.ip), ntohs(cp->cport),
  956. ntohl(cp->vaddr.ip), ntohs(cp->vport),
  957. dbuf, ntohs(cp->dport),
  958. ip_vs_state_name(cp),
  959. jiffies_delta_to_msecs(cp->timer.expires -
  960. jiffies) / 1000,
  961. pe_data);
  962. }
  963. return 0;
  964. }
  965. static const struct seq_operations ip_vs_conn_seq_ops = {
  966. .start = ip_vs_conn_seq_start,
  967. .next = ip_vs_conn_seq_next,
  968. .stop = ip_vs_conn_seq_stop,
  969. .show = ip_vs_conn_seq_show,
  970. };
  971. static const char *ip_vs_origin_name(unsigned int flags)
  972. {
  973. if (flags & IP_VS_CONN_F_SYNC)
  974. return "SYNC";
  975. else
  976. return "LOCAL";
  977. }
  978. static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
  979. {
  980. char dbuf[IP_VS_ADDRSTRLEN];
  981. if (v == SEQ_START_TOKEN)
  982. seq_puts(seq,
  983. "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
  984. else {
  985. const struct ip_vs_conn *cp = v;
  986. struct net *net = seq_file_net(seq);
  987. if (!net_eq(cp->ipvs->net, net))
  988. return 0;
  989. #ifdef CONFIG_IP_VS_IPV6
  990. if (cp->daf == AF_INET6)
  991. snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
  992. else
  993. #endif
  994. snprintf(dbuf, sizeof(dbuf), "%08X",
  995. ntohl(cp->daddr.ip));
  996. #ifdef CONFIG_IP_VS_IPV6
  997. if (cp->af == AF_INET6)
  998. seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
  999. "%s %04X %-11s %-6s %7u\n",
  1000. ip_vs_proto_name(cp->protocol),
  1001. &cp->caddr.in6, ntohs(cp->cport),
  1002. &cp->vaddr.in6, ntohs(cp->vport),
  1003. dbuf, ntohs(cp->dport),
  1004. ip_vs_state_name(cp),
  1005. ip_vs_origin_name(cp->flags),
  1006. jiffies_delta_to_msecs(cp->timer.expires -
  1007. jiffies) / 1000);
  1008. else
  1009. #endif
  1010. seq_printf(seq,
  1011. "%-3s %08X %04X %08X %04X "
  1012. "%s %04X %-11s %-6s %7u\n",
  1013. ip_vs_proto_name(cp->protocol),
  1014. ntohl(cp->caddr.ip), ntohs(cp->cport),
  1015. ntohl(cp->vaddr.ip), ntohs(cp->vport),
  1016. dbuf, ntohs(cp->dport),
  1017. ip_vs_state_name(cp),
  1018. ip_vs_origin_name(cp->flags),
  1019. jiffies_delta_to_msecs(cp->timer.expires -
  1020. jiffies) / 1000);
  1021. }
  1022. return 0;
  1023. }
  1024. static const struct seq_operations ip_vs_conn_sync_seq_ops = {
  1025. .start = ip_vs_conn_seq_start,
  1026. .next = ip_vs_conn_seq_next,
  1027. .stop = ip_vs_conn_seq_stop,
  1028. .show = ip_vs_conn_sync_seq_show,
  1029. };
  1030. #endif
  1031. /* Randomly drop connection entries before running out of memory
  1032. * Can be used for DATA and CTL conns. For TPL conns there are exceptions:
  1033. * - traffic for services in OPS mode increases ct->in_pkts, so it is supported
  1034. * - traffic for services not in OPS mode does not increase ct->in_pkts in
  1035. * all cases, so it is not supported
  1036. */
  1037. static inline int todrop_entry(struct ip_vs_conn *cp)
  1038. {
  1039. /*
  1040. * The drop rate array needs tuning for real environments.
  1041. * Called from timer bh only => no locking
  1042. */
  1043. static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  1044. static char todrop_counter[9] = {0};
  1045. int i;
  1046. /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
  1047. This will leave enough time for normal connection to get
  1048. through. */
  1049. if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
  1050. return 0;
  1051. /* Don't drop the entry if its number of incoming packets is not
  1052. located in [0, 8] */
  1053. i = atomic_read(&cp->in_pkts);
  1054. if (i > 8 || i < 0) return 0;
  1055. if (!todrop_rate[i]) return 0;
  1056. if (--todrop_counter[i] > 0) return 0;
  1057. todrop_counter[i] = todrop_rate[i];
  1058. return 1;
  1059. }
  1060. static inline bool ip_vs_conn_ops_mode(struct ip_vs_conn *cp)
  1061. {
  1062. struct ip_vs_service *svc;
  1063. if (!cp->dest)
  1064. return false;
  1065. svc = rcu_dereference(cp->dest->svc);
  1066. return svc && (svc->flags & IP_VS_SVC_F_ONEPACKET);
  1067. }
  1068. /* Called from keventd and must protect itself from softirqs */
  1069. void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
  1070. {
  1071. int idx;
  1072. struct ip_vs_conn *cp;
  1073. rcu_read_lock();
  1074. /*
  1075. * Randomly scan 1/32 of the whole table every second
  1076. */
  1077. for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
  1078. unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
  1079. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
  1080. if (cp->ipvs != ipvs)
  1081. continue;
  1082. if (atomic_read(&cp->n_control))
  1083. continue;
  1084. if (cp->flags & IP_VS_CONN_F_TEMPLATE) {
  1085. /* connection template of OPS */
  1086. if (ip_vs_conn_ops_mode(cp))
  1087. goto try_drop;
  1088. if (!(cp->state & IP_VS_CTPL_S_ASSURED))
  1089. goto drop;
  1090. continue;
  1091. }
  1092. if (cp->protocol == IPPROTO_TCP) {
  1093. switch(cp->state) {
  1094. case IP_VS_TCP_S_SYN_RECV:
  1095. case IP_VS_TCP_S_SYNACK:
  1096. break;
  1097. case IP_VS_TCP_S_ESTABLISHED:
  1098. if (todrop_entry(cp))
  1099. break;
  1100. continue;
  1101. default:
  1102. continue;
  1103. }
  1104. } else if (cp->protocol == IPPROTO_SCTP) {
  1105. switch (cp->state) {
  1106. case IP_VS_SCTP_S_INIT1:
  1107. case IP_VS_SCTP_S_INIT:
  1108. break;
  1109. case IP_VS_SCTP_S_ESTABLISHED:
  1110. if (todrop_entry(cp))
  1111. break;
  1112. continue;
  1113. default:
  1114. continue;
  1115. }
  1116. } else {
  1117. try_drop:
  1118. if (!todrop_entry(cp))
  1119. continue;
  1120. }
  1121. drop:
  1122. IP_VS_DBG(4, "drop connection\n");
  1123. cp->timeout = 0;
  1124. ip_vs_conn_expire_now(cp);
  1125. }
  1126. cond_resched_rcu();
  1127. }
  1128. rcu_read_unlock();
  1129. }
  1130. /*
  1131. * Flush all the connection entries in the ip_vs_conn_tab
  1132. */
  1133. static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
  1134. {
  1135. int idx;
  1136. struct ip_vs_conn *cp, *cp_c;
  1137. flush_again:
  1138. rcu_read_lock();
  1139. for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
  1140. hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
  1141. if (cp->ipvs != ipvs)
  1142. continue;
  1143. /* As timers are expired in LIFO order, restart
  1144. * the timer of controlling connection first, so
  1145. * that it is expired after us.
  1146. */
  1147. cp_c = cp->control;
  1148. /* cp->control is valid only with reference to cp */
  1149. if (cp_c && __ip_vs_conn_get(cp)) {
  1150. IP_VS_DBG(4, "del controlling connection\n");
  1151. ip_vs_conn_expire_now(cp_c);
  1152. __ip_vs_conn_put(cp);
  1153. }
  1154. IP_VS_DBG(4, "del connection\n");
  1155. ip_vs_conn_expire_now(cp);
  1156. }
  1157. cond_resched_rcu();
  1158. }
  1159. rcu_read_unlock();
  1160. /* the counter may be not NULL, because maybe some conn entries
  1161. are run by slow timer handler or unhashed but still referred */
  1162. if (atomic_read(&ipvs->conn_count) != 0) {
  1163. schedule();
  1164. goto flush_again;
  1165. }
  1166. }
  1167. /*
  1168. * per netns init and exit
  1169. */
  1170. int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
  1171. {
  1172. atomic_set(&ipvs->conn_count, 0);
  1173. proc_create_net("ip_vs_conn", 0, ipvs->net->proc_net,
  1174. &ip_vs_conn_seq_ops, sizeof(struct ip_vs_iter_state));
  1175. proc_create_net("ip_vs_conn_sync", 0, ipvs->net->proc_net,
  1176. &ip_vs_conn_sync_seq_ops,
  1177. sizeof(struct ip_vs_iter_state));
  1178. return 0;
  1179. }
  1180. void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
  1181. {
  1182. /* flush all the connection entries first */
  1183. ip_vs_conn_flush(ipvs);
  1184. remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
  1185. remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
  1186. }
  1187. int __init ip_vs_conn_init(void)
  1188. {
  1189. int idx;
  1190. /* Compute size and mask */
  1191. ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
  1192. ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
  1193. /*
  1194. * Allocate the connection hash table and initialize its list heads
  1195. */
  1196. ip_vs_conn_tab = vmalloc(array_size(ip_vs_conn_tab_size,
  1197. sizeof(*ip_vs_conn_tab)));
  1198. if (!ip_vs_conn_tab)
  1199. return -ENOMEM;
  1200. /* Allocate ip_vs_conn slab cache */
  1201. ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
  1202. sizeof(struct ip_vs_conn), 0,
  1203. SLAB_HWCACHE_ALIGN, NULL);
  1204. if (!ip_vs_conn_cachep) {
  1205. vfree(ip_vs_conn_tab);
  1206. return -ENOMEM;
  1207. }
  1208. pr_info("Connection hash table configured "
  1209. "(size=%d, memory=%ldKbytes)\n",
  1210. ip_vs_conn_tab_size,
  1211. (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
  1212. IP_VS_DBG(0, "Each connection entry needs %zd bytes at least\n",
  1213. sizeof(struct ip_vs_conn));
  1214. for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
  1215. INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
  1216. for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
  1217. spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
  1218. }
  1219. /* calculate the random value for connection hash */
  1220. get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
  1221. return 0;
  1222. }
  1223. void ip_vs_conn_cleanup(void)
  1224. {
  1225. /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
  1226. rcu_barrier();
  1227. /* Release the empty cache */
  1228. kmem_cache_destroy(ip_vs_conn_cachep);
  1229. vfree(ip_vs_conn_tab);
  1230. }