1 // SPDX-License-Identifier: GPL-2.0-or-later
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
9 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
10 * Peter Kese <peter.kese@ijs.si>
11 * Julian Anastasov <ja@ssi.bg>
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.
20 #define KMSG_COMPONENT "IPVS"
21 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23 #include <linux/interrupt.h>
25 #include <linux/inet.h>
26 #include <linux/net.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/proc_fs.h> /* for proc_net_* */
30 #include <linux/slab.h>
31 #include <linux/seq_file.h>
32 #include <linux/jhash.h>
33 #include <linux/random.h>
35 #include <net/net_namespace.h>
36 #include <net/ip_vs.h>
39 #ifndef CONFIG_IP_VS_TAB_BITS
40 #define CONFIG_IP_VS_TAB_BITS 12
44 * Connection hash size. Default is what was selected at compile time.
46 static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
47 module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
48 MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
50 /* size and mask values */
51 int ip_vs_conn_tab_size __read_mostly;
52 static int ip_vs_conn_tab_mask __read_mostly;
55 * Connection hash table: for input and output packets lookups of IPVS
57 static struct hlist_head *ip_vs_conn_tab __read_mostly;
59 /* SLAB cache for IPVS connections */
60 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
62 /* counter for no client port connections */
63 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
65 /* random value for IPVS connection hash */
66 static unsigned int ip_vs_conn_rnd __read_mostly;
69 * Fine locking granularity for big connection hash table
71 #define CT_LOCKARRAY_BITS 5
72 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
73 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
75 /* We need an addrstrlen that works with or without v6 */
76 #ifdef CONFIG_IP_VS_IPV6
77 #define IP_VS_ADDRSTRLEN INET6_ADDRSTRLEN
79 #define IP_VS_ADDRSTRLEN (8+1)
82 struct ip_vs_aligned_lock
85 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
87 /* lock array for conn table */
88 static struct ip_vs_aligned_lock
89 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
91 static inline void ct_write_lock_bh(unsigned int key)
93 spin_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
96 static inline void ct_write_unlock_bh(unsigned int key)
98 spin_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
101 static void ip_vs_conn_expire(struct timer_list *t);
104 * Returns hash value for IPVS connection entry
106 static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
107 const union nf_inet_addr *addr,
110 #ifdef CONFIG_IP_VS_IPV6
112 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
113 (__force u32)port, proto, ip_vs_conn_rnd) ^
114 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
116 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
118 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
121 static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
124 const union nf_inet_addr *addr;
127 if (p->pe_data && p->pe->hashkey_raw)
128 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
131 if (likely(!inverse)) {
139 return ip_vs_conn_hashkey(p->ipvs, p->af, p->protocol, addr, port);
142 static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
144 struct ip_vs_conn_param p;
146 ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol,
147 &cp->caddr, cp->cport, NULL, 0, &p);
151 p.pe_data = cp->pe_data;
152 p.pe_data_len = cp->pe_data_len;
155 return ip_vs_conn_hashkey_param(&p, false);
159 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
160 * returns bool success.
162 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
167 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
170 /* Hash by protocol, client address and port */
171 hash = ip_vs_conn_hashkey_conn(cp);
173 ct_write_lock_bh(hash);
174 spin_lock(&cp->lock);
176 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
177 cp->flags |= IP_VS_CONN_F_HASHED;
178 refcount_inc(&cp->refcnt);
179 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
182 pr_err("%s(): request for already hashed, called from %pS\n",
183 __func__, __builtin_return_address(0));
187 spin_unlock(&cp->lock);
188 ct_write_unlock_bh(hash);
195 * UNhashes ip_vs_conn from ip_vs_conn_tab.
196 * returns bool success. Caller should hold conn reference.
198 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
203 /* unhash it and decrease its reference counter */
204 hash = ip_vs_conn_hashkey_conn(cp);
206 ct_write_lock_bh(hash);
207 spin_lock(&cp->lock);
209 if (cp->flags & IP_VS_CONN_F_HASHED) {
210 hlist_del_rcu(&cp->c_list);
211 cp->flags &= ~IP_VS_CONN_F_HASHED;
212 refcount_dec(&cp->refcnt);
217 spin_unlock(&cp->lock);
218 ct_write_unlock_bh(hash);
223 /* Try to unlink ip_vs_conn from ip_vs_conn_tab.
224 * returns bool success.
226 static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
231 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
232 return refcount_dec_if_one(&cp->refcnt);
234 hash = ip_vs_conn_hashkey_conn(cp);
236 ct_write_lock_bh(hash);
237 spin_lock(&cp->lock);
239 if (cp->flags & IP_VS_CONN_F_HASHED) {
240 /* Decrease refcnt and unlink conn only if we are last user */
241 if (refcount_dec_if_one(&cp->refcnt)) {
242 hlist_del_rcu(&cp->c_list);
243 cp->flags &= ~IP_VS_CONN_F_HASHED;
248 spin_unlock(&cp->lock);
249 ct_write_unlock_bh(hash);
256 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
257 * Called for pkts coming from OUTside-to-INside.
258 * p->caddr, p->cport: pkt source address (foreign host)
259 * p->vaddr, p->vport: pkt dest address (load balancer)
261 static inline struct ip_vs_conn *
262 __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
265 struct ip_vs_conn *cp;
267 hash = ip_vs_conn_hashkey_param(p, false);
271 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
272 if (p->cport == cp->cport && p->vport == cp->vport &&
274 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
275 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
276 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
277 p->protocol == cp->protocol &&
278 cp->ipvs == p->ipvs) {
279 if (!__ip_vs_conn_get(cp))
292 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
294 struct ip_vs_conn *cp;
296 cp = __ip_vs_conn_in_get(p);
297 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
298 struct ip_vs_conn_param cport_zero_p = *p;
299 cport_zero_p.cport = 0;
300 cp = __ip_vs_conn_in_get(&cport_zero_p);
303 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
304 ip_vs_proto_name(p->protocol),
305 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
306 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
307 cp ? "hit" : "not hit");
313 ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
314 int af, const struct sk_buff *skb,
315 const struct ip_vs_iphdr *iph,
316 struct ip_vs_conn_param *p)
318 __be16 _ports[2], *pptr;
320 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports);
324 if (likely(!ip_vs_iph_inverse(iph)))
325 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr,
326 pptr[0], &iph->daddr, pptr[1], p);
328 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr,
329 pptr[1], &iph->saddr, pptr[0], p);
334 ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
335 const struct sk_buff *skb,
336 const struct ip_vs_iphdr *iph)
338 struct ip_vs_conn_param p;
340 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
343 return ip_vs_conn_in_get(&p);
345 EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
347 /* Get reference to connection template */
348 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
351 struct ip_vs_conn *cp;
353 hash = ip_vs_conn_hashkey_param(p, false);
357 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
358 if (unlikely(p->pe_data && p->pe->ct_match)) {
359 if (cp->ipvs != p->ipvs)
361 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
362 if (__ip_vs_conn_get(cp))
368 if (cp->af == p->af &&
369 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
370 /* protocol should only be IPPROTO_IP if
371 * p->vaddr is a fwmark */
372 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
373 p->af, p->vaddr, &cp->vaddr) &&
374 p->vport == cp->vport && p->cport == cp->cport &&
375 cp->flags & IP_VS_CONN_F_TEMPLATE &&
376 p->protocol == cp->protocol &&
377 cp->ipvs == p->ipvs) {
378 if (__ip_vs_conn_get(cp))
387 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
388 ip_vs_proto_name(p->protocol),
389 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
390 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
391 cp ? "hit" : "not hit");
396 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
397 * Called for pkts coming from inside-to-OUTside.
398 * p->caddr, p->cport: pkt source address (inside host)
399 * p->vaddr, p->vport: pkt dest address (foreign host) */
400 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
403 struct ip_vs_conn *cp, *ret=NULL;
404 const union nf_inet_addr *saddr;
408 * Check for "full" addressed entries
410 hash = ip_vs_conn_hashkey_param(p, true);
414 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
415 if (p->vport != cp->cport)
418 if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) {
426 if (p->cport == sport && cp->af == p->af &&
427 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
428 ip_vs_addr_equal(p->af, p->caddr, saddr) &&
429 p->protocol == cp->protocol &&
430 cp->ipvs == p->ipvs) {
431 if (!__ip_vs_conn_get(cp))
441 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
442 ip_vs_proto_name(p->protocol),
443 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
444 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
445 ret ? "hit" : "not hit");
451 ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
452 const struct sk_buff *skb,
453 const struct ip_vs_iphdr *iph)
455 struct ip_vs_conn_param p;
457 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
460 return ip_vs_conn_out_get(&p);
462 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
465 * Put back the conn and restart its timer with its timeout
467 static void __ip_vs_conn_put_timer(struct ip_vs_conn *cp)
469 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
471 mod_timer(&cp->timer, jiffies+t);
473 __ip_vs_conn_put(cp);
476 void ip_vs_conn_put(struct ip_vs_conn *cp)
478 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) &&
479 (refcount_read(&cp->refcnt) == 1) &&
480 !timer_pending(&cp->timer))
481 /* expire connection immediately */
482 ip_vs_conn_expire(&cp->timer);
484 __ip_vs_conn_put_timer(cp);
488 * Fill a no_client_port connection with a client port number
490 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
492 if (ip_vs_conn_unhash(cp)) {
493 spin_lock_bh(&cp->lock);
494 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
495 atomic_dec(&ip_vs_conn_no_cport_cnt);
496 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
499 spin_unlock_bh(&cp->lock);
501 /* hash on new dport */
508 * Bind a connection entry with the corresponding packet_xmit.
509 * Called by ip_vs_conn_new.
511 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
513 switch (IP_VS_FWD_METHOD(cp)) {
514 case IP_VS_CONN_F_MASQ:
515 cp->packet_xmit = ip_vs_nat_xmit;
518 case IP_VS_CONN_F_TUNNEL:
519 #ifdef CONFIG_IP_VS_IPV6
520 if (cp->daf == AF_INET6)
521 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
524 cp->packet_xmit = ip_vs_tunnel_xmit;
527 case IP_VS_CONN_F_DROUTE:
528 cp->packet_xmit = ip_vs_dr_xmit;
531 case IP_VS_CONN_F_LOCALNODE:
532 cp->packet_xmit = ip_vs_null_xmit;
535 case IP_VS_CONN_F_BYPASS:
536 cp->packet_xmit = ip_vs_bypass_xmit;
541 #ifdef CONFIG_IP_VS_IPV6
542 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
544 switch (IP_VS_FWD_METHOD(cp)) {
545 case IP_VS_CONN_F_MASQ:
546 cp->packet_xmit = ip_vs_nat_xmit_v6;
549 case IP_VS_CONN_F_TUNNEL:
550 if (cp->daf == AF_INET6)
551 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
553 cp->packet_xmit = ip_vs_tunnel_xmit;
556 case IP_VS_CONN_F_DROUTE:
557 cp->packet_xmit = ip_vs_dr_xmit_v6;
560 case IP_VS_CONN_F_LOCALNODE:
561 cp->packet_xmit = ip_vs_null_xmit;
564 case IP_VS_CONN_F_BYPASS:
565 cp->packet_xmit = ip_vs_bypass_xmit_v6;
572 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
574 return atomic_read(&dest->activeconns)
575 + atomic_read(&dest->inactconns);
579 * Bind a connection entry with a virtual service destination
580 * Called just after a new connection entry is created.
583 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
585 unsigned int conn_flags;
588 /* if dest is NULL, then return directly */
592 /* Increase the refcnt counter of the dest */
593 ip_vs_dest_hold(dest);
595 conn_flags = atomic_read(&dest->conn_flags);
596 if (cp->protocol != IPPROTO_UDP)
597 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
599 /* Bind with the destination and its corresponding transmitter */
600 if (flags & IP_VS_CONN_F_SYNC) {
601 /* if the connection is not template and is created
602 * by sync, preserve the activity flag.
604 if (!(flags & IP_VS_CONN_F_TEMPLATE))
605 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
606 /* connections inherit forwarding method from dest */
607 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
613 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
614 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
616 ip_vs_proto_name(cp->protocol),
617 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
618 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
619 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
620 ip_vs_fwd_tag(cp), cp->state,
621 cp->flags, refcount_read(&cp->refcnt),
622 refcount_read(&dest->refcnt));
624 /* Update the connection counters */
625 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
626 /* It is a normal connection, so modify the counters
627 * according to the flags, later the protocol can
628 * update them on state change
630 if (!(flags & IP_VS_CONN_F_INACTIVE))
631 atomic_inc(&dest->activeconns);
633 atomic_inc(&dest->inactconns);
635 /* It is a persistent connection/template, so increase
636 the persistent connection counter */
637 atomic_inc(&dest->persistconns);
640 if (dest->u_threshold != 0 &&
641 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
642 dest->flags |= IP_VS_DEST_F_OVERLOAD;
647 * Check if there is a destination for the connection, if so
648 * bind the connection to the destination.
650 void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
652 struct ip_vs_dest *dest;
656 /* This function is only invoked by the synchronization code. We do
657 * not currently support heterogeneous pools with synchronization,
658 * so we can make the assumption that the svc_af is the same as the
661 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
662 cp->dport, &cp->vaddr, cp->vport,
663 cp->protocol, cp->fwmark, cp->flags);
665 struct ip_vs_proto_data *pd;
667 spin_lock_bh(&cp->lock);
669 spin_unlock_bh(&cp->lock);
674 /* Applications work depending on the forwarding method
675 * but better to reassign them always when binding dest */
677 ip_vs_unbind_app(cp);
679 ip_vs_bind_dest(cp, dest);
680 spin_unlock_bh(&cp->lock);
682 /* Update its packet transmitter */
683 cp->packet_xmit = NULL;
684 #ifdef CONFIG_IP_VS_IPV6
685 if (cp->af == AF_INET6)
686 ip_vs_bind_xmit_v6(cp);
691 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
692 if (pd && atomic_read(&pd->appcnt))
693 ip_vs_bind_app(cp, pd->pp);
700 * Unbind a connection entry with its VS destination
701 * Called by the ip_vs_conn_expire function.
703 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
705 struct ip_vs_dest *dest = cp->dest;
710 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
711 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
713 ip_vs_proto_name(cp->protocol),
714 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
715 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
716 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
717 ip_vs_fwd_tag(cp), cp->state,
718 cp->flags, refcount_read(&cp->refcnt),
719 refcount_read(&dest->refcnt));
721 /* Update the connection counters */
722 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
723 /* It is a normal connection, so decrease the inactconns
724 or activeconns counter */
725 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
726 atomic_dec(&dest->inactconns);
728 atomic_dec(&dest->activeconns);
731 /* It is a persistent connection/template, so decrease
732 the persistent connection counter */
733 atomic_dec(&dest->persistconns);
736 if (dest->l_threshold != 0) {
737 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
738 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
739 } else if (dest->u_threshold != 0) {
740 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
741 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
743 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
744 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
747 ip_vs_dest_put(dest);
750 static int expire_quiescent_template(struct netns_ipvs *ipvs,
751 struct ip_vs_dest *dest)
754 return ipvs->sysctl_expire_quiescent_template &&
755 (atomic_read(&dest->weight) == 0);
762 * Checking if the destination of a connection template is available.
763 * If available, return 1, otherwise invalidate this connection
764 * template and return 0.
766 int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest)
768 struct ip_vs_dest *dest = ct->dest;
769 struct netns_ipvs *ipvs = ct->ipvs;
772 * Checking the dest server status.
774 if ((dest == NULL) ||
775 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
776 expire_quiescent_template(ipvs, dest) ||
777 (cdest && (dest != cdest))) {
778 IP_VS_DBG_BUF(9, "check_template: dest not available for "
779 "protocol %s s:%s:%d v:%s:%d "
781 ip_vs_proto_name(ct->protocol),
782 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
784 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
786 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
790 * Invalidate the connection template
792 if (ct->vport != htons(0xffff)) {
793 if (ip_vs_conn_unhash(ct)) {
794 ct->dport = htons(0xffff);
795 ct->vport = htons(0xffff);
802 * Simply decrease the refcnt of the template,
803 * don't restart its timer.
805 __ip_vs_conn_put(ct);
811 static void ip_vs_conn_rcu_free(struct rcu_head *head)
813 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
816 ip_vs_pe_put(cp->pe);
818 kmem_cache_free(ip_vs_conn_cachep, cp);
821 /* Try to delete connection while not holding reference */
822 static void ip_vs_conn_del(struct ip_vs_conn *cp)
824 if (del_timer(&cp->timer)) {
825 /* Drop cp->control chain too */
828 ip_vs_conn_expire(&cp->timer);
832 /* Try to delete connection while holding reference */
833 static void ip_vs_conn_del_put(struct ip_vs_conn *cp)
835 if (del_timer(&cp->timer)) {
836 /* Drop cp->control chain too */
839 __ip_vs_conn_put(cp);
840 ip_vs_conn_expire(&cp->timer);
842 __ip_vs_conn_put(cp);
846 static void ip_vs_conn_expire(struct timer_list *t)
848 struct ip_vs_conn *cp = from_timer(cp, t, timer);
849 struct netns_ipvs *ipvs = cp->ipvs;
852 * do I control anybody?
854 if (atomic_read(&cp->n_control))
857 /* Unlink conn if not referenced anymore */
858 if (likely(ip_vs_conn_unlink(cp))) {
859 struct ip_vs_conn *ct = cp->control;
861 /* delete the timer if it is activated by other users */
862 del_timer(&cp->timer);
864 /* does anybody control me? */
866 bool has_ref = !cp->timeout && __ip_vs_conn_get(ct);
868 ip_vs_control_del(cp);
869 /* Drop CTL or non-assured TPL if not used anymore */
870 if (has_ref && !atomic_read(&ct->n_control) &&
871 (!(ct->flags & IP_VS_CONN_F_TEMPLATE) ||
872 !(ct->state & IP_VS_CTPL_S_ASSURED))) {
873 IP_VS_DBG(4, "drop controlling connection\n");
874 ip_vs_conn_del_put(ct);
875 } else if (has_ref) {
876 __ip_vs_conn_put(ct);
880 if ((cp->flags & IP_VS_CONN_F_NFCT) &&
881 !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) {
882 /* Do not access conntracks during subsys cleanup
883 * because nf_conntrack_find_get can not be used after
884 * conntrack cleanup for the net.
888 ip_vs_conn_drop_conntrack(cp);
891 if (unlikely(cp->app != NULL))
892 ip_vs_unbind_app(cp);
893 ip_vs_unbind_dest(cp);
894 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
895 atomic_dec(&ip_vs_conn_no_cport_cnt);
896 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
897 ip_vs_conn_rcu_free(&cp->rcu_head);
899 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
900 atomic_dec(&ipvs->conn_count);
905 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
906 refcount_read(&cp->refcnt),
907 atomic_read(&cp->n_control));
909 refcount_inc(&cp->refcnt);
912 if (ipvs->sync_state & IP_VS_STATE_MASTER)
913 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
915 __ip_vs_conn_put_timer(cp);
918 /* Modify timer, so that it expires as soon as possible.
919 * Can be called without reference only if under RCU lock.
920 * We can have such chain of conns linked with ->control: DATA->CTL->TPL
921 * - DATA (eg. FTP) and TPL (persistence) can be present depending on setup
922 * - cp->timeout=0 indicates all conns from chain should be dropped but
923 * TPL is not dropped if in assured state
925 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
927 /* Using mod_timer_pending will ensure the timer is not
928 * modified after the final del_timer in ip_vs_conn_expire.
930 if (timer_pending(&cp->timer) &&
931 time_after(cp->timer.expires, jiffies))
932 mod_timer_pending(&cp->timer, jiffies);
937 * Create a new connection entry and hash it into the ip_vs_conn_tab
940 ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
941 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
942 struct ip_vs_dest *dest, __u32 fwmark)
944 struct ip_vs_conn *cp;
945 struct netns_ipvs *ipvs = p->ipvs;
946 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
949 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
951 IP_VS_ERR_RL("%s(): no memory\n", __func__);
955 INIT_HLIST_NODE(&cp->c_list);
956 timer_setup(&cp->timer, ip_vs_conn_expire, 0);
960 cp->protocol = p->protocol;
961 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
962 cp->cport = p->cport;
963 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
964 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
965 &cp->vaddr, p->vaddr);
966 cp->vport = p->vport;
967 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
971 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
974 cp->pe_data = p->pe_data;
975 cp->pe_data_len = p->pe_data_len;
981 spin_lock_init(&cp->lock);
984 * Set the entry is referenced by the current thread before hashing
985 * it in the table, so that other thread run ip_vs_random_dropentry
986 * but cannot drop this entry.
988 refcount_set(&cp->refcnt, 1);
991 atomic_set(&cp->n_control, 0);
992 atomic_set(&cp->in_pkts, 0);
994 cp->packet_xmit = NULL;
997 /* reset struct ip_vs_seq */
998 cp->in_seq.delta = 0;
999 cp->out_seq.delta = 0;
1001 atomic_inc(&ipvs->conn_count);
1002 if (flags & IP_VS_CONN_F_NO_CPORT)
1003 atomic_inc(&ip_vs_conn_no_cport_cnt);
1005 /* Bind the connection with a destination server */
1007 ip_vs_bind_dest(cp, dest);
1009 /* Set its state and timeout */
1013 cp->sync_endtime = jiffies & ~3UL;
1015 /* Bind its packet transmitter */
1016 #ifdef CONFIG_IP_VS_IPV6
1017 if (p->af == AF_INET6)
1018 ip_vs_bind_xmit_v6(cp);
1021 ip_vs_bind_xmit(cp);
1023 if (unlikely(pd && atomic_read(&pd->appcnt)))
1024 ip_vs_bind_app(cp, pd->pp);
1027 * Allow conntrack to be preserved. By default, conntrack
1028 * is created and destroyed for every packet.
1029 * Sometimes keeping conntrack can be useful for
1030 * IP_VS_CONN_F_ONE_PACKET too.
1033 if (ip_vs_conntrack_enabled(ipvs))
1034 cp->flags |= IP_VS_CONN_F_NFCT;
1036 /* Hash it in the ip_vs_conn_tab finally */
1037 ip_vs_conn_hash(cp);
1043 * /proc/net/ip_vs_conn entries
1045 #ifdef CONFIG_PROC_FS
1046 struct ip_vs_iter_state {
1047 struct seq_net_private p;
1048 struct hlist_head *l;
1051 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
1054 struct ip_vs_conn *cp;
1055 struct ip_vs_iter_state *iter = seq->private;
1057 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1058 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1059 /* __ip_vs_conn_get() is not needed by
1060 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
1063 iter->l = &ip_vs_conn_tab[idx];
1073 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
1076 struct ip_vs_iter_state *iter = seq->private;
1080 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1083 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1085 struct ip_vs_conn *cp = v;
1086 struct ip_vs_iter_state *iter = seq->private;
1087 struct hlist_node *e;
1088 struct hlist_head *l = iter->l;
1092 if (v == SEQ_START_TOKEN)
1093 return ip_vs_conn_array(seq, 0);
1095 /* more on same hash chain? */
1096 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1098 return hlist_entry(e, struct ip_vs_conn, c_list);
1100 idx = l - ip_vs_conn_tab;
1101 while (++idx < ip_vs_conn_tab_size) {
1102 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1103 iter->l = &ip_vs_conn_tab[idx];
1112 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1118 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1121 if (v == SEQ_START_TOKEN)
1123 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1125 const struct ip_vs_conn *cp = v;
1126 struct net *net = seq_file_net(seq);
1127 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1129 char dbuf[IP_VS_ADDRSTRLEN];
1131 if (!net_eq(cp->ipvs->net, net))
1135 len = strlen(cp->pe->name);
1136 memcpy(pe_data + 1, cp->pe->name, len);
1137 pe_data[len + 1] = ' ';
1139 len += cp->pe->show_pe_data(cp, pe_data + len);
1141 pe_data[len] = '\0';
1143 #ifdef CONFIG_IP_VS_IPV6
1144 if (cp->daf == AF_INET6)
1145 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1148 snprintf(dbuf, sizeof(dbuf), "%08X",
1149 ntohl(cp->daddr.ip));
1151 #ifdef CONFIG_IP_VS_IPV6
1152 if (cp->af == AF_INET6)
1153 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1154 "%s %04X %-11s %7u%s\n",
1155 ip_vs_proto_name(cp->protocol),
1156 &cp->caddr.in6, ntohs(cp->cport),
1157 &cp->vaddr.in6, ntohs(cp->vport),
1158 dbuf, ntohs(cp->dport),
1159 ip_vs_state_name(cp),
1160 jiffies_delta_to_msecs(cp->timer.expires -
1166 "%-3s %08X %04X %08X %04X"
1167 " %s %04X %-11s %7u%s\n",
1168 ip_vs_proto_name(cp->protocol),
1169 ntohl(cp->caddr.ip), ntohs(cp->cport),
1170 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1171 dbuf, ntohs(cp->dport),
1172 ip_vs_state_name(cp),
1173 jiffies_delta_to_msecs(cp->timer.expires -
1180 static const struct seq_operations ip_vs_conn_seq_ops = {
1181 .start = ip_vs_conn_seq_start,
1182 .next = ip_vs_conn_seq_next,
1183 .stop = ip_vs_conn_seq_stop,
1184 .show = ip_vs_conn_seq_show,
1187 static const char *ip_vs_origin_name(unsigned int flags)
1189 if (flags & IP_VS_CONN_F_SYNC)
1195 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1197 char dbuf[IP_VS_ADDRSTRLEN];
1199 if (v == SEQ_START_TOKEN)
1201 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1203 const struct ip_vs_conn *cp = v;
1204 struct net *net = seq_file_net(seq);
1206 if (!net_eq(cp->ipvs->net, net))
1209 #ifdef CONFIG_IP_VS_IPV6
1210 if (cp->daf == AF_INET6)
1211 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1214 snprintf(dbuf, sizeof(dbuf), "%08X",
1215 ntohl(cp->daddr.ip));
1217 #ifdef CONFIG_IP_VS_IPV6
1218 if (cp->af == AF_INET6)
1219 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1220 "%s %04X %-11s %-6s %7u\n",
1221 ip_vs_proto_name(cp->protocol),
1222 &cp->caddr.in6, ntohs(cp->cport),
1223 &cp->vaddr.in6, ntohs(cp->vport),
1224 dbuf, ntohs(cp->dport),
1225 ip_vs_state_name(cp),
1226 ip_vs_origin_name(cp->flags),
1227 jiffies_delta_to_msecs(cp->timer.expires -
1232 "%-3s %08X %04X %08X %04X "
1233 "%s %04X %-11s %-6s %7u\n",
1234 ip_vs_proto_name(cp->protocol),
1235 ntohl(cp->caddr.ip), ntohs(cp->cport),
1236 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1237 dbuf, ntohs(cp->dport),
1238 ip_vs_state_name(cp),
1239 ip_vs_origin_name(cp->flags),
1240 jiffies_delta_to_msecs(cp->timer.expires -
1246 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1247 .start = ip_vs_conn_seq_start,
1248 .next = ip_vs_conn_seq_next,
1249 .stop = ip_vs_conn_seq_stop,
1250 .show = ip_vs_conn_sync_seq_show,
1255 /* Randomly drop connection entries before running out of memory
1256 * Can be used for DATA and CTL conns. For TPL conns there are exceptions:
1257 * - traffic for services in OPS mode increases ct->in_pkts, so it is supported
1258 * - traffic for services not in OPS mode does not increase ct->in_pkts in
1259 * all cases, so it is not supported
1261 static inline int todrop_entry(struct ip_vs_conn *cp)
1264 * The drop rate array needs tuning for real environments.
1265 * Called from timer bh only => no locking
1267 static const signed char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1268 static signed char todrop_counter[9] = {0};
1271 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1272 This will leave enough time for normal connection to get
1274 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1277 /* Don't drop the entry if its number of incoming packets is not
1278 located in [0, 8] */
1279 i = atomic_read(&cp->in_pkts);
1280 if (i > 8 || i < 0) return 0;
1282 if (!todrop_rate[i]) return 0;
1283 if (--todrop_counter[i] > 0) return 0;
1285 todrop_counter[i] = todrop_rate[i];
1289 static inline bool ip_vs_conn_ops_mode(struct ip_vs_conn *cp)
1291 struct ip_vs_service *svc;
1295 svc = rcu_dereference(cp->dest->svc);
1296 return svc && (svc->flags & IP_VS_SVC_F_ONEPACKET);
1299 /* Called from keventd and must protect itself from softirqs */
1300 void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
1303 struct ip_vs_conn *cp;
1307 * Randomly scan 1/32 of the whole table every second
1309 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1310 unsigned int hash = get_random_u32() & ip_vs_conn_tab_mask;
1312 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
1313 if (cp->ipvs != ipvs)
1315 if (atomic_read(&cp->n_control))
1317 if (cp->flags & IP_VS_CONN_F_TEMPLATE) {
1318 /* connection template of OPS */
1319 if (ip_vs_conn_ops_mode(cp))
1321 if (!(cp->state & IP_VS_CTPL_S_ASSURED))
1325 if (cp->protocol == IPPROTO_TCP) {
1327 case IP_VS_TCP_S_SYN_RECV:
1328 case IP_VS_TCP_S_SYNACK:
1331 case IP_VS_TCP_S_ESTABLISHED:
1332 if (todrop_entry(cp))
1339 } else if (cp->protocol == IPPROTO_SCTP) {
1340 switch (cp->state) {
1341 case IP_VS_SCTP_S_INIT1:
1342 case IP_VS_SCTP_S_INIT:
1344 case IP_VS_SCTP_S_ESTABLISHED:
1345 if (todrop_entry(cp))
1353 if (!todrop_entry(cp))
1358 IP_VS_DBG(4, "drop connection\n");
1368 * Flush all the connection entries in the ip_vs_conn_tab
1370 static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
1373 struct ip_vs_conn *cp, *cp_c;
1377 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1379 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1380 if (cp->ipvs != ipvs)
1382 if (atomic_read(&cp->n_control))
1385 IP_VS_DBG(4, "del connection\n");
1387 if (cp_c && !atomic_read(&cp_c->n_control)) {
1388 IP_VS_DBG(4, "del controlling connection\n");
1389 ip_vs_conn_del(cp_c);
1396 /* the counter may be not NULL, because maybe some conn entries
1397 are run by slow timer handler or unhashed but still referred */
1398 if (atomic_read(&ipvs->conn_count) != 0) {
1404 #ifdef CONFIG_SYSCTL
1405 void ip_vs_expire_nodest_conn_flush(struct netns_ipvs *ipvs)
1408 struct ip_vs_conn *cp, *cp_c;
1409 struct ip_vs_dest *dest;
1412 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1413 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1414 if (cp->ipvs != ipvs)
1418 if (!dest || (dest->flags & IP_VS_DEST_F_AVAILABLE))
1421 if (atomic_read(&cp->n_control))
1425 IP_VS_DBG(4, "del connection\n");
1427 if (cp_c && !atomic_read(&cp_c->n_control)) {
1428 IP_VS_DBG(4, "del controlling connection\n");
1429 ip_vs_conn_del(cp_c);
1434 /* netns clean up started, abort delayed work */
1443 * per netns init and exit
1445 int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
1447 atomic_set(&ipvs->conn_count, 0);
1449 #ifdef CONFIG_PROC_FS
1450 if (!proc_create_net("ip_vs_conn", 0, ipvs->net->proc_net,
1451 &ip_vs_conn_seq_ops,
1452 sizeof(struct ip_vs_iter_state)))
1455 if (!proc_create_net("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1456 &ip_vs_conn_sync_seq_ops,
1457 sizeof(struct ip_vs_iter_state)))
1463 #ifdef CONFIG_PROC_FS
1465 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1471 void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
1473 /* flush all the connection entries first */
1474 ip_vs_conn_flush(ipvs);
1475 #ifdef CONFIG_PROC_FS
1476 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1477 remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
1481 int __init ip_vs_conn_init(void)
1483 size_t tab_array_size;
1485 #if BITS_PER_LONG > 32
1493 max_avail = order_base_2(totalram_pages()) + PAGE_SHIFT;
1494 max_avail -= 2; /* ~4 in hash row */
1495 max_avail -= 1; /* IPVS up to 1/2 of mem */
1496 max_avail -= order_base_2(sizeof(struct ip_vs_conn));
1497 max = clamp(max, min, max_avail);
1498 ip_vs_conn_tab_bits = clamp_val(ip_vs_conn_tab_bits, min, max);
1499 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1500 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1503 * Allocate the connection hash table and initialize its list heads
1505 tab_array_size = array_size(ip_vs_conn_tab_size,
1506 sizeof(*ip_vs_conn_tab));
1507 ip_vs_conn_tab = kvmalloc_array(ip_vs_conn_tab_size,
1508 sizeof(*ip_vs_conn_tab), GFP_KERNEL);
1509 if (!ip_vs_conn_tab)
1512 /* Allocate ip_vs_conn slab cache */
1513 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1514 sizeof(struct ip_vs_conn), 0,
1515 SLAB_HWCACHE_ALIGN, NULL);
1516 if (!ip_vs_conn_cachep) {
1517 kvfree(ip_vs_conn_tab);
1521 pr_info("Connection hash table configured (size=%d, memory=%zdKbytes)\n",
1522 ip_vs_conn_tab_size, tab_array_size / 1024);
1523 IP_VS_DBG(0, "Each connection entry needs %zd bytes at least\n",
1524 sizeof(struct ip_vs_conn));
1526 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1527 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
1529 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1530 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
1533 /* calculate the random value for connection hash */
1534 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1539 void ip_vs_conn_cleanup(void)
1541 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1543 /* Release the empty cache */
1544 kmem_cache_destroy(ip_vs_conn_cachep);
1545 kvfree(ip_vs_conn_tab);