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
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
18 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
19 * and others. Many code here is taken from IP MASQ code of kernel 2.2.
25 #define KMSG_COMPONENT "IPVS"
26 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28 #include <linux/interrupt.h>
30 #include <linux/inet.h>
31 #include <linux/net.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/vmalloc.h>
35 #include <linux/proc_fs.h> /* for proc_net_* */
36 #include <linux/slab.h>
37 #include <linux/seq_file.h>
38 #include <linux/jhash.h>
39 #include <linux/random.h>
41 #include <net/net_namespace.h>
42 #include <net/ip_vs.h>
45 #ifndef CONFIG_IP_VS_TAB_BITS
46 #define CONFIG_IP_VS_TAB_BITS 12
50 * Connection hash size. Default is what was selected at compile time.
52 static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
53 module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
54 MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
56 /* size and mask values */
57 int ip_vs_conn_tab_size __read_mostly;
58 static int ip_vs_conn_tab_mask __read_mostly;
61 * Connection hash table: for input and output packets lookups of IPVS
63 static struct hlist_head *ip_vs_conn_tab __read_mostly;
65 /* SLAB cache for IPVS connections */
66 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
68 /* counter for no client port connections */
69 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
71 /* random value for IPVS connection hash */
72 static unsigned int ip_vs_conn_rnd __read_mostly;
75 * Fine locking granularity for big connection hash table
77 #define CT_LOCKARRAY_BITS 5
78 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
79 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
81 /* We need an addrstrlen that works with or without v6 */
82 #ifdef CONFIG_IP_VS_IPV6
83 #define IP_VS_ADDRSTRLEN INET6_ADDRSTRLEN
85 #define IP_VS_ADDRSTRLEN (8+1)
88 struct ip_vs_aligned_lock
91 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
93 /* lock array for conn table */
94 static struct ip_vs_aligned_lock
95 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
97 static inline void ct_write_lock_bh(unsigned int key)
99 spin_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
102 static inline void ct_write_unlock_bh(unsigned int key)
104 spin_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
107 static void ip_vs_conn_expire(unsigned long data);
110 * Returns hash value for IPVS connection entry
112 static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
113 const union nf_inet_addr *addr,
116 #ifdef CONFIG_IP_VS_IPV6
118 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
119 (__force u32)port, proto, ip_vs_conn_rnd) ^
120 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
122 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
124 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
127 static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
130 const union nf_inet_addr *addr;
133 if (p->pe_data && p->pe->hashkey_raw)
134 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
137 if (likely(!inverse)) {
145 return ip_vs_conn_hashkey(p->ipvs, p->af, p->protocol, addr, port);
148 static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
150 struct ip_vs_conn_param p;
152 ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol,
153 &cp->caddr, cp->cport, NULL, 0, &p);
157 p.pe_data = cp->pe_data;
158 p.pe_data_len = cp->pe_data_len;
161 return ip_vs_conn_hashkey_param(&p, false);
165 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
166 * returns bool success.
168 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
173 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
176 /* Hash by protocol, client address and port */
177 hash = ip_vs_conn_hashkey_conn(cp);
179 ct_write_lock_bh(hash);
180 spin_lock(&cp->lock);
182 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
183 cp->flags |= IP_VS_CONN_F_HASHED;
184 atomic_inc(&cp->refcnt);
185 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
188 pr_err("%s(): request for already hashed, called from %pF\n",
189 __func__, __builtin_return_address(0));
193 spin_unlock(&cp->lock);
194 ct_write_unlock_bh(hash);
201 * UNhashes ip_vs_conn from ip_vs_conn_tab.
202 * returns bool success. Caller should hold conn reference.
204 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
209 /* unhash it and decrease its reference counter */
210 hash = ip_vs_conn_hashkey_conn(cp);
212 ct_write_lock_bh(hash);
213 spin_lock(&cp->lock);
215 if (cp->flags & IP_VS_CONN_F_HASHED) {
216 hlist_del_rcu(&cp->c_list);
217 cp->flags &= ~IP_VS_CONN_F_HASHED;
218 atomic_dec(&cp->refcnt);
223 spin_unlock(&cp->lock);
224 ct_write_unlock_bh(hash);
229 /* Try to unlink ip_vs_conn from ip_vs_conn_tab.
230 * returns bool success.
232 static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
237 hash = ip_vs_conn_hashkey_conn(cp);
239 ct_write_lock_bh(hash);
240 spin_lock(&cp->lock);
242 if (cp->flags & IP_VS_CONN_F_HASHED) {
244 /* Decrease refcnt and unlink conn only if we are last user */
245 if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) {
246 hlist_del_rcu(&cp->c_list);
247 cp->flags &= ~IP_VS_CONN_F_HASHED;
251 ret = atomic_read(&cp->refcnt) ? false : true;
253 spin_unlock(&cp->lock);
254 ct_write_unlock_bh(hash);
261 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
262 * Called for pkts coming from OUTside-to-INside.
263 * p->caddr, p->cport: pkt source address (foreign host)
264 * p->vaddr, p->vport: pkt dest address (load balancer)
266 static inline struct ip_vs_conn *
267 __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
270 struct ip_vs_conn *cp;
272 hash = ip_vs_conn_hashkey_param(p, false);
276 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
277 if (p->cport == cp->cport && p->vport == cp->vport &&
279 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
280 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
281 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
282 p->protocol == cp->protocol &&
283 cp->ipvs == p->ipvs) {
284 if (!__ip_vs_conn_get(cp))
297 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
299 struct ip_vs_conn *cp;
301 cp = __ip_vs_conn_in_get(p);
302 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
303 struct ip_vs_conn_param cport_zero_p = *p;
304 cport_zero_p.cport = 0;
305 cp = __ip_vs_conn_in_get(&cport_zero_p);
308 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
309 ip_vs_proto_name(p->protocol),
310 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
311 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
312 cp ? "hit" : "not hit");
318 ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
319 int af, const struct sk_buff *skb,
320 const struct ip_vs_iphdr *iph,
321 struct ip_vs_conn_param *p)
323 __be16 _ports[2], *pptr;
325 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
329 if (likely(!ip_vs_iph_inverse(iph)))
330 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr,
331 pptr[0], &iph->daddr, pptr[1], p);
333 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr,
334 pptr[1], &iph->saddr, pptr[0], p);
339 ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
340 const struct sk_buff *skb,
341 const struct ip_vs_iphdr *iph)
343 struct ip_vs_conn_param p;
345 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
348 return ip_vs_conn_in_get(&p);
350 EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
352 /* Get reference to connection template */
353 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
356 struct ip_vs_conn *cp;
358 hash = ip_vs_conn_hashkey_param(p, false);
362 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
363 if (unlikely(p->pe_data && p->pe->ct_match)) {
364 if (cp->ipvs != p->ipvs)
366 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
367 if (__ip_vs_conn_get(cp))
373 if (cp->af == p->af &&
374 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
375 /* protocol should only be IPPROTO_IP if
376 * p->vaddr is a fwmark */
377 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
378 p->af, p->vaddr, &cp->vaddr) &&
379 p->vport == cp->vport && p->cport == cp->cport &&
380 cp->flags & IP_VS_CONN_F_TEMPLATE &&
381 p->protocol == cp->protocol &&
382 cp->ipvs == p->ipvs) {
383 if (__ip_vs_conn_get(cp))
392 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
393 ip_vs_proto_name(p->protocol),
394 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
395 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
396 cp ? "hit" : "not hit");
401 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
402 * Called for pkts coming from inside-to-OUTside.
403 * p->caddr, p->cport: pkt source address (inside host)
404 * p->vaddr, p->vport: pkt dest address (foreign host) */
405 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
408 struct ip_vs_conn *cp, *ret=NULL;
411 * Check for "full" addressed entries
413 hash = ip_vs_conn_hashkey_param(p, true);
417 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
418 if (p->vport == cp->cport && p->cport == cp->dport &&
420 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
421 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
422 p->protocol == cp->protocol &&
423 cp->ipvs == p->ipvs) {
424 if (!__ip_vs_conn_get(cp))
434 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
435 ip_vs_proto_name(p->protocol),
436 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
437 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
438 ret ? "hit" : "not hit");
444 ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
445 const struct sk_buff *skb,
446 const struct ip_vs_iphdr *iph)
448 struct ip_vs_conn_param p;
450 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
453 return ip_vs_conn_out_get(&p);
455 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
457 static void __ip_vs_conn_put_notimer(struct ip_vs_conn *cp)
459 __ip_vs_conn_put(cp);
460 ip_vs_conn_expire((unsigned long)cp);
464 * Put back the conn and restart its timer with its timeout
466 static void __ip_vs_conn_put_timer(struct ip_vs_conn *cp)
468 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
470 mod_timer(&cp->timer, jiffies+t);
472 __ip_vs_conn_put(cp);
475 void ip_vs_conn_put(struct ip_vs_conn *cp)
477 if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) &&
478 (atomic_read(&cp->refcnt) == 1) &&
479 !timer_pending(&cp->timer))
480 /* expire connection immediately */
481 __ip_vs_conn_put_notimer(cp);
483 __ip_vs_conn_put_timer(cp);
487 * Fill a no_client_port connection with a client port number
489 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
491 if (ip_vs_conn_unhash(cp)) {
492 spin_lock_bh(&cp->lock);
493 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
494 atomic_dec(&ip_vs_conn_no_cport_cnt);
495 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
498 spin_unlock_bh(&cp->lock);
500 /* hash on new dport */
507 * Bind a connection entry with the corresponding packet_xmit.
508 * Called by ip_vs_conn_new.
510 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
512 switch (IP_VS_FWD_METHOD(cp)) {
513 case IP_VS_CONN_F_MASQ:
514 cp->packet_xmit = ip_vs_nat_xmit;
517 case IP_VS_CONN_F_TUNNEL:
518 #ifdef CONFIG_IP_VS_IPV6
519 if (cp->daf == AF_INET6)
520 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
523 cp->packet_xmit = ip_vs_tunnel_xmit;
526 case IP_VS_CONN_F_DROUTE:
527 cp->packet_xmit = ip_vs_dr_xmit;
530 case IP_VS_CONN_F_LOCALNODE:
531 cp->packet_xmit = ip_vs_null_xmit;
534 case IP_VS_CONN_F_BYPASS:
535 cp->packet_xmit = ip_vs_bypass_xmit;
540 #ifdef CONFIG_IP_VS_IPV6
541 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
543 switch (IP_VS_FWD_METHOD(cp)) {
544 case IP_VS_CONN_F_MASQ:
545 cp->packet_xmit = ip_vs_nat_xmit_v6;
548 case IP_VS_CONN_F_TUNNEL:
549 if (cp->daf == AF_INET6)
550 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
552 cp->packet_xmit = ip_vs_tunnel_xmit;
555 case IP_VS_CONN_F_DROUTE:
556 cp->packet_xmit = ip_vs_dr_xmit_v6;
559 case IP_VS_CONN_F_LOCALNODE:
560 cp->packet_xmit = ip_vs_null_xmit;
563 case IP_VS_CONN_F_BYPASS:
564 cp->packet_xmit = ip_vs_bypass_xmit_v6;
571 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
573 return atomic_read(&dest->activeconns)
574 + atomic_read(&dest->inactconns);
578 * Bind a connection entry with a virtual service destination
579 * Called just after a new connection entry is created.
582 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
584 unsigned int conn_flags;
587 /* if dest is NULL, then return directly */
591 /* Increase the refcnt counter of the dest */
592 ip_vs_dest_hold(dest);
594 conn_flags = atomic_read(&dest->conn_flags);
595 if (cp->protocol != IPPROTO_UDP)
596 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
598 /* Bind with the destination and its corresponding transmitter */
599 if (flags & IP_VS_CONN_F_SYNC) {
600 /* if the connection is not template and is created
601 * by sync, preserve the activity flag.
603 if (!(flags & IP_VS_CONN_F_TEMPLATE))
604 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
605 /* connections inherit forwarding method from dest */
606 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
612 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
613 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
615 ip_vs_proto_name(cp->protocol),
616 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
617 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
618 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
619 ip_vs_fwd_tag(cp), cp->state,
620 cp->flags, atomic_read(&cp->refcnt),
621 atomic_read(&dest->refcnt));
623 /* Update the connection counters */
624 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
625 /* It is a normal connection, so modify the counters
626 * according to the flags, later the protocol can
627 * update them on state change
629 if (!(flags & IP_VS_CONN_F_INACTIVE))
630 atomic_inc(&dest->activeconns);
632 atomic_inc(&dest->inactconns);
634 /* It is a persistent connection/template, so increase
635 the persistent connection counter */
636 atomic_inc(&dest->persistconns);
639 if (dest->u_threshold != 0 &&
640 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
641 dest->flags |= IP_VS_DEST_F_OVERLOAD;
646 * Check if there is a destination for the connection, if so
647 * bind the connection to the destination.
649 void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
651 struct ip_vs_dest *dest;
655 /* This function is only invoked by the synchronization code. We do
656 * not currently support heterogeneous pools with synchronization,
657 * so we can make the assumption that the svc_af is the same as the
660 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
661 cp->dport, &cp->vaddr, cp->vport,
662 cp->protocol, cp->fwmark, cp->flags);
664 struct ip_vs_proto_data *pd;
666 spin_lock_bh(&cp->lock);
668 spin_unlock_bh(&cp->lock);
673 /* Applications work depending on the forwarding method
674 * but better to reassign them always when binding dest */
676 ip_vs_unbind_app(cp);
678 ip_vs_bind_dest(cp, dest);
679 spin_unlock_bh(&cp->lock);
681 /* Update its packet transmitter */
682 cp->packet_xmit = NULL;
683 #ifdef CONFIG_IP_VS_IPV6
684 if (cp->af == AF_INET6)
685 ip_vs_bind_xmit_v6(cp);
690 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
691 if (pd && atomic_read(&pd->appcnt))
692 ip_vs_bind_app(cp, pd->pp);
699 * Unbind a connection entry with its VS destination
700 * Called by the ip_vs_conn_expire function.
702 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
704 struct ip_vs_dest *dest = cp->dest;
709 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
710 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
712 ip_vs_proto_name(cp->protocol),
713 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
714 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
715 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
716 ip_vs_fwd_tag(cp), cp->state,
717 cp->flags, atomic_read(&cp->refcnt),
718 atomic_read(&dest->refcnt));
720 /* Update the connection counters */
721 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
722 /* It is a normal connection, so decrease the inactconns
723 or activeconns counter */
724 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
725 atomic_dec(&dest->inactconns);
727 atomic_dec(&dest->activeconns);
730 /* It is a persistent connection/template, so decrease
731 the persistent connection counter */
732 atomic_dec(&dest->persistconns);
735 if (dest->l_threshold != 0) {
736 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
737 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
738 } else if (dest->u_threshold != 0) {
739 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
740 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
742 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
743 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
746 ip_vs_dest_put(dest);
749 static int expire_quiescent_template(struct netns_ipvs *ipvs,
750 struct ip_vs_dest *dest)
753 return ipvs->sysctl_expire_quiescent_template &&
754 (atomic_read(&dest->weight) == 0);
761 * Checking if the destination of a connection template is available.
762 * If available, return 1, otherwise invalidate this connection
763 * template and return 0.
765 int ip_vs_check_template(struct ip_vs_conn *ct)
767 struct ip_vs_dest *dest = ct->dest;
768 struct netns_ipvs *ipvs = ct->ipvs;
771 * Checking the dest server status.
773 if ((dest == NULL) ||
774 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
775 expire_quiescent_template(ipvs, dest)) {
776 IP_VS_DBG_BUF(9, "check_template: dest not available for "
777 "protocol %s s:%s:%d v:%s:%d "
779 ip_vs_proto_name(ct->protocol),
780 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
782 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
784 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
788 * Invalidate the connection template
790 if (ct->vport != htons(0xffff)) {
791 if (ip_vs_conn_unhash(ct)) {
792 ct->dport = htons(0xffff);
793 ct->vport = htons(0xffff);
800 * Simply decrease the refcnt of the template,
801 * don't restart its timer.
803 __ip_vs_conn_put(ct);
809 static void ip_vs_conn_rcu_free(struct rcu_head *head)
811 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
814 ip_vs_pe_put(cp->pe);
816 kmem_cache_free(ip_vs_conn_cachep, cp);
819 static void ip_vs_conn_expire(unsigned long data)
821 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
822 struct netns_ipvs *ipvs = cp->ipvs;
825 * do I control anybody?
827 if (atomic_read(&cp->n_control))
830 /* Unlink conn if not referenced anymore */
831 if (likely(ip_vs_conn_unlink(cp))) {
832 /* delete the timer if it is activated by other users */
833 del_timer(&cp->timer);
835 /* does anybody control me? */
837 ip_vs_control_del(cp);
839 if ((cp->flags & IP_VS_CONN_F_NFCT) &&
840 !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) {
841 /* Do not access conntracks during subsys cleanup
842 * because nf_conntrack_find_get can not be used after
843 * conntrack cleanup for the net.
847 ip_vs_conn_drop_conntrack(cp);
850 if (unlikely(cp->app != NULL))
851 ip_vs_unbind_app(cp);
852 ip_vs_unbind_dest(cp);
853 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
854 atomic_dec(&ip_vs_conn_no_cport_cnt);
855 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
856 ip_vs_conn_rcu_free(&cp->rcu_head);
858 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
859 atomic_dec(&ipvs->conn_count);
864 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
865 atomic_read(&cp->refcnt),
866 atomic_read(&cp->n_control));
868 atomic_inc(&cp->refcnt);
871 if (ipvs->sync_state & IP_VS_STATE_MASTER)
872 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
874 __ip_vs_conn_put_timer(cp);
877 /* Modify timer, so that it expires as soon as possible.
878 * Can be called without reference only if under RCU lock.
880 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
882 /* Using mod_timer_pending will ensure the timer is not
883 * modified after the final del_timer in ip_vs_conn_expire.
885 if (timer_pending(&cp->timer) &&
886 time_after(cp->timer.expires, jiffies))
887 mod_timer_pending(&cp->timer, jiffies);
892 * Create a new connection entry and hash it into the ip_vs_conn_tab
895 ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
896 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
897 struct ip_vs_dest *dest, __u32 fwmark)
899 struct ip_vs_conn *cp;
900 struct netns_ipvs *ipvs = p->ipvs;
901 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
904 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
906 IP_VS_ERR_RL("%s(): no memory\n", __func__);
910 INIT_HLIST_NODE(&cp->c_list);
911 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
915 cp->protocol = p->protocol;
916 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
917 cp->cport = p->cport;
918 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
919 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
920 &cp->vaddr, p->vaddr);
921 cp->vport = p->vport;
922 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
926 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
929 cp->pe_data = p->pe_data;
930 cp->pe_data_len = p->pe_data_len;
936 spin_lock_init(&cp->lock);
939 * Set the entry is referenced by the current thread before hashing
940 * it in the table, so that other thread run ip_vs_random_dropentry
941 * but cannot drop this entry.
943 atomic_set(&cp->refcnt, 1);
946 atomic_set(&cp->n_control, 0);
947 atomic_set(&cp->in_pkts, 0);
949 cp->packet_xmit = NULL;
952 /* reset struct ip_vs_seq */
953 cp->in_seq.delta = 0;
954 cp->out_seq.delta = 0;
956 atomic_inc(&ipvs->conn_count);
957 if (flags & IP_VS_CONN_F_NO_CPORT)
958 atomic_inc(&ip_vs_conn_no_cport_cnt);
960 /* Bind the connection with a destination server */
962 ip_vs_bind_dest(cp, dest);
964 /* Set its state and timeout */
968 cp->sync_endtime = jiffies & ~3UL;
970 /* Bind its packet transmitter */
971 #ifdef CONFIG_IP_VS_IPV6
972 if (p->af == AF_INET6)
973 ip_vs_bind_xmit_v6(cp);
978 if (unlikely(pd && atomic_read(&pd->appcnt)))
979 ip_vs_bind_app(cp, pd->pp);
982 * Allow conntrack to be preserved. By default, conntrack
983 * is created and destroyed for every packet.
984 * Sometimes keeping conntrack can be useful for
985 * IP_VS_CONN_F_ONE_PACKET too.
988 if (ip_vs_conntrack_enabled(ipvs))
989 cp->flags |= IP_VS_CONN_F_NFCT;
991 /* Hash it in the ip_vs_conn_tab finally */
998 * /proc/net/ip_vs_conn entries
1000 #ifdef CONFIG_PROC_FS
1001 struct ip_vs_iter_state {
1002 struct seq_net_private p;
1003 struct hlist_head *l;
1006 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
1009 struct ip_vs_conn *cp;
1010 struct ip_vs_iter_state *iter = seq->private;
1012 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1013 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1014 /* __ip_vs_conn_get() is not needed by
1015 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
1018 iter->l = &ip_vs_conn_tab[idx];
1028 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
1031 struct ip_vs_iter_state *iter = seq->private;
1035 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1038 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1040 struct ip_vs_conn *cp = v;
1041 struct ip_vs_iter_state *iter = seq->private;
1042 struct hlist_node *e;
1043 struct hlist_head *l = iter->l;
1047 if (v == SEQ_START_TOKEN)
1048 return ip_vs_conn_array(seq, 0);
1050 /* more on same hash chain? */
1051 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1053 return hlist_entry(e, struct ip_vs_conn, c_list);
1055 idx = l - ip_vs_conn_tab;
1056 while (++idx < ip_vs_conn_tab_size) {
1057 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1058 iter->l = &ip_vs_conn_tab[idx];
1067 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1073 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1076 if (v == SEQ_START_TOKEN)
1078 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1080 const struct ip_vs_conn *cp = v;
1081 struct net *net = seq_file_net(seq);
1082 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1084 char dbuf[IP_VS_ADDRSTRLEN];
1086 if (!net_eq(cp->ipvs->net, net))
1090 len = strlen(cp->pe->name);
1091 memcpy(pe_data + 1, cp->pe->name, len);
1092 pe_data[len + 1] = ' ';
1094 len += cp->pe->show_pe_data(cp, pe_data + len);
1096 pe_data[len] = '\0';
1098 #ifdef CONFIG_IP_VS_IPV6
1099 if (cp->daf == AF_INET6)
1100 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1103 snprintf(dbuf, sizeof(dbuf), "%08X",
1104 ntohl(cp->daddr.ip));
1106 #ifdef CONFIG_IP_VS_IPV6
1107 if (cp->af == AF_INET6)
1108 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1109 "%s %04X %-11s %7lu%s\n",
1110 ip_vs_proto_name(cp->protocol),
1111 &cp->caddr.in6, ntohs(cp->cport),
1112 &cp->vaddr.in6, ntohs(cp->vport),
1113 dbuf, ntohs(cp->dport),
1114 ip_vs_state_name(cp->protocol, cp->state),
1115 (cp->timer.expires-jiffies)/HZ, pe_data);
1119 "%-3s %08X %04X %08X %04X"
1120 " %s %04X %-11s %7lu%s\n",
1121 ip_vs_proto_name(cp->protocol),
1122 ntohl(cp->caddr.ip), ntohs(cp->cport),
1123 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1124 dbuf, ntohs(cp->dport),
1125 ip_vs_state_name(cp->protocol, cp->state),
1126 (cp->timer.expires-jiffies)/HZ, pe_data);
1131 static const struct seq_operations ip_vs_conn_seq_ops = {
1132 .start = ip_vs_conn_seq_start,
1133 .next = ip_vs_conn_seq_next,
1134 .stop = ip_vs_conn_seq_stop,
1135 .show = ip_vs_conn_seq_show,
1138 static int ip_vs_conn_open(struct inode *inode, struct file *file)
1140 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1141 sizeof(struct ip_vs_iter_state));
1144 static const struct file_operations ip_vs_conn_fops = {
1145 .owner = THIS_MODULE,
1146 .open = ip_vs_conn_open,
1148 .llseek = seq_lseek,
1149 .release = seq_release_net,
1152 static const char *ip_vs_origin_name(unsigned int flags)
1154 if (flags & IP_VS_CONN_F_SYNC)
1160 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1162 char dbuf[IP_VS_ADDRSTRLEN];
1164 if (v == SEQ_START_TOKEN)
1166 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1168 const struct ip_vs_conn *cp = v;
1169 struct net *net = seq_file_net(seq);
1171 if (!net_eq(cp->ipvs->net, net))
1174 #ifdef CONFIG_IP_VS_IPV6
1175 if (cp->daf == AF_INET6)
1176 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1179 snprintf(dbuf, sizeof(dbuf), "%08X",
1180 ntohl(cp->daddr.ip));
1182 #ifdef CONFIG_IP_VS_IPV6
1183 if (cp->af == AF_INET6)
1184 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1185 "%s %04X %-11s %-6s %7lu\n",
1186 ip_vs_proto_name(cp->protocol),
1187 &cp->caddr.in6, ntohs(cp->cport),
1188 &cp->vaddr.in6, ntohs(cp->vport),
1189 dbuf, ntohs(cp->dport),
1190 ip_vs_state_name(cp->protocol, cp->state),
1191 ip_vs_origin_name(cp->flags),
1192 (cp->timer.expires-jiffies)/HZ);
1196 "%-3s %08X %04X %08X %04X "
1197 "%s %04X %-11s %-6s %7lu\n",
1198 ip_vs_proto_name(cp->protocol),
1199 ntohl(cp->caddr.ip), ntohs(cp->cport),
1200 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1201 dbuf, ntohs(cp->dport),
1202 ip_vs_state_name(cp->protocol, cp->state),
1203 ip_vs_origin_name(cp->flags),
1204 (cp->timer.expires-jiffies)/HZ);
1209 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1210 .start = ip_vs_conn_seq_start,
1211 .next = ip_vs_conn_seq_next,
1212 .stop = ip_vs_conn_seq_stop,
1213 .show = ip_vs_conn_sync_seq_show,
1216 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1218 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1219 sizeof(struct ip_vs_iter_state));
1222 static const struct file_operations ip_vs_conn_sync_fops = {
1223 .owner = THIS_MODULE,
1224 .open = ip_vs_conn_sync_open,
1226 .llseek = seq_lseek,
1227 .release = seq_release_net,
1234 * Randomly drop connection entries before running out of memory
1236 static inline int todrop_entry(struct ip_vs_conn *cp)
1239 * The drop rate array needs tuning for real environments.
1240 * Called from timer bh only => no locking
1242 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1243 static char todrop_counter[9] = {0};
1246 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1247 This will leave enough time for normal connection to get
1249 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1252 /* Don't drop the entry if its number of incoming packets is not
1253 located in [0, 8] */
1254 i = atomic_read(&cp->in_pkts);
1255 if (i > 8 || i < 0) return 0;
1257 if (!todrop_rate[i]) return 0;
1258 if (--todrop_counter[i] > 0) return 0;
1260 todrop_counter[i] = todrop_rate[i];
1264 static inline bool ip_vs_conn_ops_mode(struct ip_vs_conn *cp)
1266 struct ip_vs_service *svc;
1270 svc = rcu_dereference(cp->dest->svc);
1271 return svc && (svc->flags & IP_VS_SVC_F_ONEPACKET);
1274 /* Called from keventd and must protect itself from softirqs */
1275 void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
1278 struct ip_vs_conn *cp, *cp_c;
1282 * Randomly scan 1/32 of the whole table every second
1284 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1285 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
1287 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
1288 if (cp->ipvs != ipvs)
1290 if (cp->flags & IP_VS_CONN_F_TEMPLATE) {
1291 if (atomic_read(&cp->n_control) ||
1292 !ip_vs_conn_ops_mode(cp))
1295 /* connection template of OPS */
1298 if (cp->protocol == IPPROTO_TCP) {
1300 case IP_VS_TCP_S_SYN_RECV:
1301 case IP_VS_TCP_S_SYNACK:
1304 case IP_VS_TCP_S_ESTABLISHED:
1305 if (todrop_entry(cp))
1312 } else if (cp->protocol == IPPROTO_SCTP) {
1313 switch (cp->state) {
1314 case IP_VS_SCTP_S_INIT1:
1315 case IP_VS_SCTP_S_INIT:
1317 case IP_VS_SCTP_S_ESTABLISHED:
1318 if (todrop_entry(cp))
1326 if (!todrop_entry(cp))
1330 IP_VS_DBG(4, "del connection\n");
1331 ip_vs_conn_expire_now(cp);
1333 /* cp->control is valid only with reference to cp */
1334 if (cp_c && __ip_vs_conn_get(cp)) {
1335 IP_VS_DBG(4, "del conn template\n");
1336 ip_vs_conn_expire_now(cp_c);
1337 __ip_vs_conn_put(cp);
1347 * Flush all the connection entries in the ip_vs_conn_tab
1349 static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
1352 struct ip_vs_conn *cp, *cp_c;
1356 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1358 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1359 if (cp->ipvs != ipvs)
1361 IP_VS_DBG(4, "del connection\n");
1362 ip_vs_conn_expire_now(cp);
1364 /* cp->control is valid only with reference to cp */
1365 if (cp_c && __ip_vs_conn_get(cp)) {
1366 IP_VS_DBG(4, "del conn template\n");
1367 ip_vs_conn_expire_now(cp_c);
1368 __ip_vs_conn_put(cp);
1375 /* the counter may be not NULL, because maybe some conn entries
1376 are run by slow timer handler or unhashed but still referred */
1377 if (atomic_read(&ipvs->conn_count) != 0) {
1383 * per netns init and exit
1385 int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
1387 atomic_set(&ipvs->conn_count, 0);
1389 proc_create("ip_vs_conn", 0, ipvs->net->proc_net, &ip_vs_conn_fops);
1390 proc_create("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1391 &ip_vs_conn_sync_fops);
1395 void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
1397 /* flush all the connection entries first */
1398 ip_vs_conn_flush(ipvs);
1399 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1400 remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
1403 int __init ip_vs_conn_init(void)
1407 /* Compute size and mask */
1408 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1409 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1412 * Allocate the connection hash table and initialize its list heads
1414 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
1415 if (!ip_vs_conn_tab)
1418 /* Allocate ip_vs_conn slab cache */
1419 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1420 sizeof(struct ip_vs_conn), 0,
1421 SLAB_HWCACHE_ALIGN, NULL);
1422 if (!ip_vs_conn_cachep) {
1423 vfree(ip_vs_conn_tab);
1427 pr_info("Connection hash table configured "
1428 "(size=%d, memory=%ldKbytes)\n",
1429 ip_vs_conn_tab_size,
1430 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
1431 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1432 sizeof(struct ip_vs_conn));
1434 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1435 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
1437 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1438 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
1441 /* calculate the random value for connection hash */
1442 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1447 void ip_vs_conn_cleanup(void)
1449 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1451 /* Release the empty cache */
1452 kmem_cache_destroy(ip_vs_conn_cachep);
1453 vfree(ip_vs_conn_tab);