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);
109 * Returns hash value for IPVS connection entry
111 static unsigned int ip_vs_conn_hashkey(struct netns_ipvs *ipvs, int af, unsigned int proto,
112 const union nf_inet_addr *addr,
115 #ifdef CONFIG_IP_VS_IPV6
117 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
118 (__force u32)port, proto, ip_vs_conn_rnd) ^
119 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
121 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
123 ((size_t)ipvs>>8)) & ip_vs_conn_tab_mask;
126 static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
129 const union nf_inet_addr *addr;
132 if (p->pe_data && p->pe->hashkey_raw)
133 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
136 if (likely(!inverse)) {
144 return ip_vs_conn_hashkey(p->ipvs, p->af, p->protocol, addr, port);
147 static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
149 struct ip_vs_conn_param p;
151 ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol,
152 &cp->caddr, cp->cport, NULL, 0, &p);
156 p.pe_data = cp->pe_data;
157 p.pe_data_len = cp->pe_data_len;
160 return ip_vs_conn_hashkey_param(&p, false);
164 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
165 * returns bool success.
167 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
172 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
175 /* Hash by protocol, client address and port */
176 hash = ip_vs_conn_hashkey_conn(cp);
178 ct_write_lock_bh(hash);
179 spin_lock(&cp->lock);
181 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
182 cp->flags |= IP_VS_CONN_F_HASHED;
183 atomic_inc(&cp->refcnt);
184 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
187 pr_err("%s(): request for already hashed, called from %pF\n",
188 __func__, __builtin_return_address(0));
192 spin_unlock(&cp->lock);
193 ct_write_unlock_bh(hash);
200 * UNhashes ip_vs_conn from ip_vs_conn_tab.
201 * returns bool success. Caller should hold conn reference.
203 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
208 /* unhash it and decrease its reference counter */
209 hash = ip_vs_conn_hashkey_conn(cp);
211 ct_write_lock_bh(hash);
212 spin_lock(&cp->lock);
214 if (cp->flags & IP_VS_CONN_F_HASHED) {
215 hlist_del_rcu(&cp->c_list);
216 cp->flags &= ~IP_VS_CONN_F_HASHED;
217 atomic_dec(&cp->refcnt);
222 spin_unlock(&cp->lock);
223 ct_write_unlock_bh(hash);
228 /* Try to unlink ip_vs_conn from ip_vs_conn_tab.
229 * returns bool success.
231 static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
236 hash = ip_vs_conn_hashkey_conn(cp);
238 ct_write_lock_bh(hash);
239 spin_lock(&cp->lock);
241 if (cp->flags & IP_VS_CONN_F_HASHED) {
243 /* Decrease refcnt and unlink conn only if we are last user */
244 if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) {
245 hlist_del_rcu(&cp->c_list);
246 cp->flags &= ~IP_VS_CONN_F_HASHED;
250 ret = atomic_read(&cp->refcnt) ? false : true;
252 spin_unlock(&cp->lock);
253 ct_write_unlock_bh(hash);
260 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
261 * Called for pkts coming from OUTside-to-INside.
262 * p->caddr, p->cport: pkt source address (foreign host)
263 * p->vaddr, p->vport: pkt dest address (load balancer)
265 static inline struct ip_vs_conn *
266 __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
269 struct ip_vs_conn *cp;
271 hash = ip_vs_conn_hashkey_param(p, false);
275 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
276 if (p->cport == cp->cport && p->vport == cp->vport &&
278 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
279 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
280 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
281 p->protocol == cp->protocol &&
282 cp->ipvs == p->ipvs) {
283 if (!__ip_vs_conn_get(cp))
296 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
298 struct ip_vs_conn *cp;
300 cp = __ip_vs_conn_in_get(p);
301 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
302 struct ip_vs_conn_param cport_zero_p = *p;
303 cport_zero_p.cport = 0;
304 cp = __ip_vs_conn_in_get(&cport_zero_p);
307 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
308 ip_vs_proto_name(p->protocol),
309 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
310 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
311 cp ? "hit" : "not hit");
317 ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs,
318 int af, const struct sk_buff *skb,
319 const struct ip_vs_iphdr *iph,
320 struct ip_vs_conn_param *p)
322 __be16 _ports[2], *pptr;
324 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
328 if (likely(!ip_vs_iph_inverse(iph)))
329 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr,
330 pptr[0], &iph->daddr, pptr[1], p);
332 ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr,
333 pptr[1], &iph->saddr, pptr[0], p);
338 ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
339 const struct sk_buff *skb,
340 const struct ip_vs_iphdr *iph)
342 struct ip_vs_conn_param p;
344 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
347 return ip_vs_conn_in_get(&p);
349 EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
351 /* Get reference to connection template */
352 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
355 struct ip_vs_conn *cp;
357 hash = ip_vs_conn_hashkey_param(p, false);
361 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
362 if (unlikely(p->pe_data && p->pe->ct_match)) {
363 if (cp->ipvs != p->ipvs)
365 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
366 if (__ip_vs_conn_get(cp))
372 if (cp->af == p->af &&
373 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
374 /* protocol should only be IPPROTO_IP if
375 * p->vaddr is a fwmark */
376 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
377 p->af, p->vaddr, &cp->vaddr) &&
378 p->vport == cp->vport && p->cport == cp->cport &&
379 cp->flags & IP_VS_CONN_F_TEMPLATE &&
380 p->protocol == cp->protocol &&
381 cp->ipvs == p->ipvs) {
382 if (__ip_vs_conn_get(cp))
391 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
392 ip_vs_proto_name(p->protocol),
393 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
394 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
395 cp ? "hit" : "not hit");
400 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
401 * Called for pkts coming from inside-to-OUTside.
402 * p->caddr, p->cport: pkt source address (inside host)
403 * p->vaddr, p->vport: pkt dest address (foreign host) */
404 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
407 struct ip_vs_conn *cp, *ret=NULL;
410 * Check for "full" addressed entries
412 hash = ip_vs_conn_hashkey_param(p, true);
416 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
417 if (p->vport == cp->cport && p->cport == cp->dport &&
419 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
420 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
421 p->protocol == cp->protocol &&
422 cp->ipvs == p->ipvs) {
423 if (!__ip_vs_conn_get(cp))
433 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
434 ip_vs_proto_name(p->protocol),
435 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
436 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
437 ret ? "hit" : "not hit");
443 ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
444 const struct sk_buff *skb,
445 const struct ip_vs_iphdr *iph)
447 struct ip_vs_conn_param p;
449 if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
452 return ip_vs_conn_out_get(&p);
454 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
457 * Put back the conn and restart its timer with its timeout
459 void ip_vs_conn_put(struct ip_vs_conn *cp)
461 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
463 mod_timer(&cp->timer, jiffies+t);
465 __ip_vs_conn_put(cp);
470 * Fill a no_client_port connection with a client port number
472 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
474 if (ip_vs_conn_unhash(cp)) {
475 spin_lock_bh(&cp->lock);
476 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
477 atomic_dec(&ip_vs_conn_no_cport_cnt);
478 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
481 spin_unlock_bh(&cp->lock);
483 /* hash on new dport */
490 * Bind a connection entry with the corresponding packet_xmit.
491 * Called by ip_vs_conn_new.
493 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
495 switch (IP_VS_FWD_METHOD(cp)) {
496 case IP_VS_CONN_F_MASQ:
497 cp->packet_xmit = ip_vs_nat_xmit;
500 case IP_VS_CONN_F_TUNNEL:
501 #ifdef CONFIG_IP_VS_IPV6
502 if (cp->daf == AF_INET6)
503 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
506 cp->packet_xmit = ip_vs_tunnel_xmit;
509 case IP_VS_CONN_F_DROUTE:
510 cp->packet_xmit = ip_vs_dr_xmit;
513 case IP_VS_CONN_F_LOCALNODE:
514 cp->packet_xmit = ip_vs_null_xmit;
517 case IP_VS_CONN_F_BYPASS:
518 cp->packet_xmit = ip_vs_bypass_xmit;
523 #ifdef CONFIG_IP_VS_IPV6
524 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
526 switch (IP_VS_FWD_METHOD(cp)) {
527 case IP_VS_CONN_F_MASQ:
528 cp->packet_xmit = ip_vs_nat_xmit_v6;
531 case IP_VS_CONN_F_TUNNEL:
532 if (cp->daf == AF_INET6)
533 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
535 cp->packet_xmit = ip_vs_tunnel_xmit;
538 case IP_VS_CONN_F_DROUTE:
539 cp->packet_xmit = ip_vs_dr_xmit_v6;
542 case IP_VS_CONN_F_LOCALNODE:
543 cp->packet_xmit = ip_vs_null_xmit;
546 case IP_VS_CONN_F_BYPASS:
547 cp->packet_xmit = ip_vs_bypass_xmit_v6;
554 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
556 return atomic_read(&dest->activeconns)
557 + atomic_read(&dest->inactconns);
561 * Bind a connection entry with a virtual service destination
562 * Called just after a new connection entry is created.
565 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
567 unsigned int conn_flags;
570 /* if dest is NULL, then return directly */
574 /* Increase the refcnt counter of the dest */
575 ip_vs_dest_hold(dest);
577 conn_flags = atomic_read(&dest->conn_flags);
578 if (cp->protocol != IPPROTO_UDP)
579 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
581 /* Bind with the destination and its corresponding transmitter */
582 if (flags & IP_VS_CONN_F_SYNC) {
583 /* if the connection is not template and is created
584 * by sync, preserve the activity flag.
586 if (!(flags & IP_VS_CONN_F_TEMPLATE))
587 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
588 /* connections inherit forwarding method from dest */
589 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
595 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
596 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
598 ip_vs_proto_name(cp->protocol),
599 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
600 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
601 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
602 ip_vs_fwd_tag(cp), cp->state,
603 cp->flags, atomic_read(&cp->refcnt),
604 atomic_read(&dest->refcnt));
606 /* Update the connection counters */
607 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
608 /* It is a normal connection, so modify the counters
609 * according to the flags, later the protocol can
610 * update them on state change
612 if (!(flags & IP_VS_CONN_F_INACTIVE))
613 atomic_inc(&dest->activeconns);
615 atomic_inc(&dest->inactconns);
617 /* It is a persistent connection/template, so increase
618 the persistent connection counter */
619 atomic_inc(&dest->persistconns);
622 if (dest->u_threshold != 0 &&
623 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
624 dest->flags |= IP_VS_DEST_F_OVERLOAD;
629 * Check if there is a destination for the connection, if so
630 * bind the connection to the destination.
632 void ip_vs_try_bind_dest(struct ip_vs_conn *cp)
634 struct ip_vs_dest *dest;
638 /* This function is only invoked by the synchronization code. We do
639 * not currently support heterogeneous pools with synchronization,
640 * so we can make the assumption that the svc_af is the same as the
643 dest = ip_vs_find_dest(cp->ipvs, cp->af, cp->af, &cp->daddr,
644 cp->dport, &cp->vaddr, cp->vport,
645 cp->protocol, cp->fwmark, cp->flags);
647 struct ip_vs_proto_data *pd;
649 spin_lock_bh(&cp->lock);
651 spin_unlock_bh(&cp->lock);
656 /* Applications work depending on the forwarding method
657 * but better to reassign them always when binding dest */
659 ip_vs_unbind_app(cp);
661 ip_vs_bind_dest(cp, dest);
662 spin_unlock_bh(&cp->lock);
664 /* Update its packet transmitter */
665 cp->packet_xmit = NULL;
666 #ifdef CONFIG_IP_VS_IPV6
667 if (cp->af == AF_INET6)
668 ip_vs_bind_xmit_v6(cp);
673 pd = ip_vs_proto_data_get(cp->ipvs, cp->protocol);
674 if (pd && atomic_read(&pd->appcnt))
675 ip_vs_bind_app(cp, pd->pp);
682 * Unbind a connection entry with its VS destination
683 * Called by the ip_vs_conn_expire function.
685 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
687 struct ip_vs_dest *dest = cp->dest;
692 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
693 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
695 ip_vs_proto_name(cp->protocol),
696 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
697 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
698 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
699 ip_vs_fwd_tag(cp), cp->state,
700 cp->flags, atomic_read(&cp->refcnt),
701 atomic_read(&dest->refcnt));
703 /* Update the connection counters */
704 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
705 /* It is a normal connection, so decrease the inactconns
706 or activeconns counter */
707 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
708 atomic_dec(&dest->inactconns);
710 atomic_dec(&dest->activeconns);
713 /* It is a persistent connection/template, so decrease
714 the persistent connection counter */
715 atomic_dec(&dest->persistconns);
718 if (dest->l_threshold != 0) {
719 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
720 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
721 } else if (dest->u_threshold != 0) {
722 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
723 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
725 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
726 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
729 ip_vs_dest_put(dest);
732 static int expire_quiescent_template(struct netns_ipvs *ipvs,
733 struct ip_vs_dest *dest)
736 return ipvs->sysctl_expire_quiescent_template &&
737 (atomic_read(&dest->weight) == 0);
744 * Checking if the destination of a connection template is available.
745 * If available, return 1, otherwise invalidate this connection
746 * template and return 0.
748 int ip_vs_check_template(struct ip_vs_conn *ct)
750 struct ip_vs_dest *dest = ct->dest;
751 struct netns_ipvs *ipvs = ct->ipvs;
754 * Checking the dest server status.
756 if ((dest == NULL) ||
757 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
758 expire_quiescent_template(ipvs, dest)) {
759 IP_VS_DBG_BUF(9, "check_template: dest not available for "
760 "protocol %s s:%s:%d v:%s:%d "
762 ip_vs_proto_name(ct->protocol),
763 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
765 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
767 IP_VS_DBG_ADDR(ct->daf, &ct->daddr),
771 * Invalidate the connection template
773 if (ct->vport != htons(0xffff)) {
774 if (ip_vs_conn_unhash(ct)) {
775 ct->dport = htons(0xffff);
776 ct->vport = htons(0xffff);
783 * Simply decrease the refcnt of the template,
784 * don't restart its timer.
786 __ip_vs_conn_put(ct);
792 static void ip_vs_conn_rcu_free(struct rcu_head *head)
794 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
797 ip_vs_pe_put(cp->pe);
799 kmem_cache_free(ip_vs_conn_cachep, cp);
802 static void ip_vs_conn_expire(unsigned long data)
804 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
805 struct netns_ipvs *ipvs = cp->ipvs;
808 * do I control anybody?
810 if (atomic_read(&cp->n_control))
813 /* Unlink conn if not referenced anymore */
814 if (likely(ip_vs_conn_unlink(cp))) {
815 /* delete the timer if it is activated by other users */
816 del_timer(&cp->timer);
818 /* does anybody control me? */
820 ip_vs_control_del(cp);
822 if (cp->flags & IP_VS_CONN_F_NFCT) {
823 /* Do not access conntracks during subsys cleanup
824 * because nf_conntrack_find_get can not be used after
825 * conntrack cleanup for the net.
829 ip_vs_conn_drop_conntrack(cp);
832 if (unlikely(cp->app != NULL))
833 ip_vs_unbind_app(cp);
834 ip_vs_unbind_dest(cp);
835 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
836 atomic_dec(&ip_vs_conn_no_cport_cnt);
837 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
838 atomic_dec(&ipvs->conn_count);
843 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
844 atomic_read(&cp->refcnt),
845 atomic_read(&cp->n_control));
847 atomic_inc(&cp->refcnt);
850 if (ipvs->sync_state & IP_VS_STATE_MASTER)
851 ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
856 /* Modify timer, so that it expires as soon as possible.
857 * Can be called without reference only if under RCU lock.
859 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
861 /* Using mod_timer_pending will ensure the timer is not
862 * modified after the final del_timer in ip_vs_conn_expire.
864 if (timer_pending(&cp->timer) &&
865 time_after(cp->timer.expires, jiffies))
866 mod_timer_pending(&cp->timer, jiffies);
871 * Create a new connection entry and hash it into the ip_vs_conn_tab
874 ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
875 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
876 struct ip_vs_dest *dest, __u32 fwmark)
878 struct ip_vs_conn *cp;
879 struct netns_ipvs *ipvs = p->ipvs;
880 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs,
883 cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
885 IP_VS_ERR_RL("%s(): no memory\n", __func__);
889 INIT_HLIST_NODE(&cp->c_list);
890 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
894 cp->protocol = p->protocol;
895 ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
896 cp->cport = p->cport;
897 /* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
898 ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
899 &cp->vaddr, p->vaddr);
900 cp->vport = p->vport;
901 ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
905 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
908 cp->pe_data = p->pe_data;
909 cp->pe_data_len = p->pe_data_len;
915 spin_lock_init(&cp->lock);
918 * Set the entry is referenced by the current thread before hashing
919 * it in the table, so that other thread run ip_vs_random_dropentry
920 * but cannot drop this entry.
922 atomic_set(&cp->refcnt, 1);
925 atomic_set(&cp->n_control, 0);
926 atomic_set(&cp->in_pkts, 0);
928 cp->packet_xmit = NULL;
931 /* reset struct ip_vs_seq */
932 cp->in_seq.delta = 0;
933 cp->out_seq.delta = 0;
935 atomic_inc(&ipvs->conn_count);
936 if (flags & IP_VS_CONN_F_NO_CPORT)
937 atomic_inc(&ip_vs_conn_no_cport_cnt);
939 /* Bind the connection with a destination server */
941 ip_vs_bind_dest(cp, dest);
943 /* Set its state and timeout */
947 cp->sync_endtime = jiffies & ~3UL;
949 /* Bind its packet transmitter */
950 #ifdef CONFIG_IP_VS_IPV6
951 if (p->af == AF_INET6)
952 ip_vs_bind_xmit_v6(cp);
957 if (unlikely(pd && atomic_read(&pd->appcnt)))
958 ip_vs_bind_app(cp, pd->pp);
961 * Allow conntrack to be preserved. By default, conntrack
962 * is created and destroyed for every packet.
963 * Sometimes keeping conntrack can be useful for
964 * IP_VS_CONN_F_ONE_PACKET too.
967 if (ip_vs_conntrack_enabled(ipvs))
968 cp->flags |= IP_VS_CONN_F_NFCT;
970 /* Hash it in the ip_vs_conn_tab finally */
977 * /proc/net/ip_vs_conn entries
979 #ifdef CONFIG_PROC_FS
980 struct ip_vs_iter_state {
981 struct seq_net_private p;
982 struct hlist_head *l;
985 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
988 struct ip_vs_conn *cp;
989 struct ip_vs_iter_state *iter = seq->private;
991 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
992 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
993 /* __ip_vs_conn_get() is not needed by
994 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
997 iter->l = &ip_vs_conn_tab[idx];
1007 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
1010 struct ip_vs_iter_state *iter = seq->private;
1014 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
1017 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1019 struct ip_vs_conn *cp = v;
1020 struct ip_vs_iter_state *iter = seq->private;
1021 struct hlist_node *e;
1022 struct hlist_head *l = iter->l;
1026 if (v == SEQ_START_TOKEN)
1027 return ip_vs_conn_array(seq, 0);
1029 /* more on same hash chain? */
1030 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
1032 return hlist_entry(e, struct ip_vs_conn, c_list);
1034 idx = l - ip_vs_conn_tab;
1035 while (++idx < ip_vs_conn_tab_size) {
1036 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1037 iter->l = &ip_vs_conn_tab[idx];
1046 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1052 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1055 if (v == SEQ_START_TOKEN)
1057 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1059 const struct ip_vs_conn *cp = v;
1060 struct net *net = seq_file_net(seq);
1061 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1063 char dbuf[IP_VS_ADDRSTRLEN];
1065 if (!net_eq(cp->ipvs->net, net))
1069 len = strlen(cp->pe->name);
1070 memcpy(pe_data + 1, cp->pe->name, len);
1071 pe_data[len + 1] = ' ';
1073 len += cp->pe->show_pe_data(cp, pe_data + len);
1075 pe_data[len] = '\0';
1077 #ifdef CONFIG_IP_VS_IPV6
1078 if (cp->daf == AF_INET6)
1079 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1082 snprintf(dbuf, sizeof(dbuf), "%08X",
1083 ntohl(cp->daddr.ip));
1085 #ifdef CONFIG_IP_VS_IPV6
1086 if (cp->af == AF_INET6)
1087 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1088 "%s %04X %-11s %7lu%s\n",
1089 ip_vs_proto_name(cp->protocol),
1090 &cp->caddr.in6, ntohs(cp->cport),
1091 &cp->vaddr.in6, ntohs(cp->vport),
1092 dbuf, ntohs(cp->dport),
1093 ip_vs_state_name(cp->protocol, cp->state),
1094 (cp->timer.expires-jiffies)/HZ, pe_data);
1098 "%-3s %08X %04X %08X %04X"
1099 " %s %04X %-11s %7lu%s\n",
1100 ip_vs_proto_name(cp->protocol),
1101 ntohl(cp->caddr.ip), ntohs(cp->cport),
1102 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1103 dbuf, ntohs(cp->dport),
1104 ip_vs_state_name(cp->protocol, cp->state),
1105 (cp->timer.expires-jiffies)/HZ, pe_data);
1110 static const struct seq_operations ip_vs_conn_seq_ops = {
1111 .start = ip_vs_conn_seq_start,
1112 .next = ip_vs_conn_seq_next,
1113 .stop = ip_vs_conn_seq_stop,
1114 .show = ip_vs_conn_seq_show,
1117 static int ip_vs_conn_open(struct inode *inode, struct file *file)
1119 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1120 sizeof(struct ip_vs_iter_state));
1123 static const struct file_operations ip_vs_conn_fops = {
1124 .owner = THIS_MODULE,
1125 .open = ip_vs_conn_open,
1127 .llseek = seq_lseek,
1128 .release = seq_release_net,
1131 static const char *ip_vs_origin_name(unsigned int flags)
1133 if (flags & IP_VS_CONN_F_SYNC)
1139 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1141 char dbuf[IP_VS_ADDRSTRLEN];
1143 if (v == SEQ_START_TOKEN)
1145 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1147 const struct ip_vs_conn *cp = v;
1148 struct net *net = seq_file_net(seq);
1150 if (!net_eq(cp->ipvs->net, net))
1153 #ifdef CONFIG_IP_VS_IPV6
1154 if (cp->daf == AF_INET6)
1155 snprintf(dbuf, sizeof(dbuf), "%pI6", &cp->daddr.in6);
1158 snprintf(dbuf, sizeof(dbuf), "%08X",
1159 ntohl(cp->daddr.ip));
1161 #ifdef CONFIG_IP_VS_IPV6
1162 if (cp->af == AF_INET6)
1163 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1164 "%s %04X %-11s %-6s %7lu\n",
1165 ip_vs_proto_name(cp->protocol),
1166 &cp->caddr.in6, ntohs(cp->cport),
1167 &cp->vaddr.in6, ntohs(cp->vport),
1168 dbuf, ntohs(cp->dport),
1169 ip_vs_state_name(cp->protocol, cp->state),
1170 ip_vs_origin_name(cp->flags),
1171 (cp->timer.expires-jiffies)/HZ);
1175 "%-3s %08X %04X %08X %04X "
1176 "%s %04X %-11s %-6s %7lu\n",
1177 ip_vs_proto_name(cp->protocol),
1178 ntohl(cp->caddr.ip), ntohs(cp->cport),
1179 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1180 dbuf, ntohs(cp->dport),
1181 ip_vs_state_name(cp->protocol, cp->state),
1182 ip_vs_origin_name(cp->flags),
1183 (cp->timer.expires-jiffies)/HZ);
1188 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1189 .start = ip_vs_conn_seq_start,
1190 .next = ip_vs_conn_seq_next,
1191 .stop = ip_vs_conn_seq_stop,
1192 .show = ip_vs_conn_sync_seq_show,
1195 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1197 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1198 sizeof(struct ip_vs_iter_state));
1201 static const struct file_operations ip_vs_conn_sync_fops = {
1202 .owner = THIS_MODULE,
1203 .open = ip_vs_conn_sync_open,
1205 .llseek = seq_lseek,
1206 .release = seq_release_net,
1213 * Randomly drop connection entries before running out of memory
1215 static inline int todrop_entry(struct ip_vs_conn *cp)
1218 * The drop rate array needs tuning for real environments.
1219 * Called from timer bh only => no locking
1221 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1222 static char todrop_counter[9] = {0};
1225 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1226 This will leave enough time for normal connection to get
1228 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1231 /* Don't drop the entry if its number of incoming packets is not
1232 located in [0, 8] */
1233 i = atomic_read(&cp->in_pkts);
1234 if (i > 8 || i < 0) return 0;
1236 if (!todrop_rate[i]) return 0;
1237 if (--todrop_counter[i] > 0) return 0;
1239 todrop_counter[i] = todrop_rate[i];
1243 /* Called from keventd and must protect itself from softirqs */
1244 void ip_vs_random_dropentry(struct netns_ipvs *ipvs)
1247 struct ip_vs_conn *cp, *cp_c;
1251 * Randomly scan 1/32 of the whole table every second
1253 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1254 unsigned int hash = prandom_u32() & ip_vs_conn_tab_mask;
1256 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
1257 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
1258 /* connection template */
1260 if (cp->ipvs != ipvs)
1262 if (cp->protocol == IPPROTO_TCP) {
1264 case IP_VS_TCP_S_SYN_RECV:
1265 case IP_VS_TCP_S_SYNACK:
1268 case IP_VS_TCP_S_ESTABLISHED:
1269 if (todrop_entry(cp))
1276 } else if (cp->protocol == IPPROTO_SCTP) {
1277 switch (cp->state) {
1278 case IP_VS_SCTP_S_INIT1:
1279 case IP_VS_SCTP_S_INIT:
1281 case IP_VS_SCTP_S_ESTABLISHED:
1282 if (todrop_entry(cp))
1289 if (!todrop_entry(cp))
1293 IP_VS_DBG(4, "del connection\n");
1294 ip_vs_conn_expire_now(cp);
1296 /* cp->control is valid only with reference to cp */
1297 if (cp_c && __ip_vs_conn_get(cp)) {
1298 IP_VS_DBG(4, "del conn template\n");
1299 ip_vs_conn_expire_now(cp_c);
1300 __ip_vs_conn_put(cp);
1310 * Flush all the connection entries in the ip_vs_conn_tab
1312 static void ip_vs_conn_flush(struct netns_ipvs *ipvs)
1315 struct ip_vs_conn *cp, *cp_c;
1319 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1321 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1322 if (cp->ipvs != ipvs)
1324 IP_VS_DBG(4, "del connection\n");
1325 ip_vs_conn_expire_now(cp);
1327 /* cp->control is valid only with reference to cp */
1328 if (cp_c && __ip_vs_conn_get(cp)) {
1329 IP_VS_DBG(4, "del conn template\n");
1330 ip_vs_conn_expire_now(cp_c);
1331 __ip_vs_conn_put(cp);
1338 /* the counter may be not NULL, because maybe some conn entries
1339 are run by slow timer handler or unhashed but still referred */
1340 if (atomic_read(&ipvs->conn_count) != 0) {
1346 * per netns init and exit
1348 int __net_init ip_vs_conn_net_init(struct netns_ipvs *ipvs)
1350 atomic_set(&ipvs->conn_count, 0);
1352 proc_create("ip_vs_conn", 0, ipvs->net->proc_net, &ip_vs_conn_fops);
1353 proc_create("ip_vs_conn_sync", 0, ipvs->net->proc_net,
1354 &ip_vs_conn_sync_fops);
1358 void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)
1360 /* flush all the connection entries first */
1361 ip_vs_conn_flush(ipvs);
1362 remove_proc_entry("ip_vs_conn", ipvs->net->proc_net);
1363 remove_proc_entry("ip_vs_conn_sync", ipvs->net->proc_net);
1366 int __init ip_vs_conn_init(void)
1370 /* Compute size and mask */
1371 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1372 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1375 * Allocate the connection hash table and initialize its list heads
1377 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
1378 if (!ip_vs_conn_tab)
1381 /* Allocate ip_vs_conn slab cache */
1382 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1383 sizeof(struct ip_vs_conn), 0,
1384 SLAB_HWCACHE_ALIGN, NULL);
1385 if (!ip_vs_conn_cachep) {
1386 vfree(ip_vs_conn_tab);
1390 pr_info("Connection hash table configured "
1391 "(size=%d, memory=%ldKbytes)\n",
1392 ip_vs_conn_tab_size,
1393 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
1394 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1395 sizeof(struct ip_vs_conn));
1397 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1398 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
1400 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1401 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
1404 /* calculate the random value for connection hash */
1405 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1410 void ip_vs_conn_cleanup(void)
1412 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1414 /* Release the empty cache */
1415 kmem_cache_destroy(ip_vs_conn_cachep);
1416 vfree(ip_vs_conn_tab);