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/net.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/vmalloc.h>
34 #include <linux/proc_fs.h> /* for proc_net_* */
35 #include <linux/slab.h>
36 #include <linux/seq_file.h>
37 #include <linux/jhash.h>
38 #include <linux/random.h>
40 #include <net/net_namespace.h>
41 #include <net/ip_vs.h>
44 #ifndef CONFIG_IP_VS_TAB_BITS
45 #define CONFIG_IP_VS_TAB_BITS 12
49 * Connection hash size. Default is what was selected at compile time.
51 static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
52 module_param_named(conn_tab_bits, ip_vs_conn_tab_bits, int, 0444);
53 MODULE_PARM_DESC(conn_tab_bits, "Set connections' hash size");
55 /* size and mask values */
56 int ip_vs_conn_tab_size __read_mostly;
57 static int ip_vs_conn_tab_mask __read_mostly;
60 * Connection hash table: for input and output packets lookups of IPVS
62 static struct hlist_head *ip_vs_conn_tab __read_mostly;
64 /* SLAB cache for IPVS connections */
65 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
67 /* counter for no client port connections */
68 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
70 /* random value for IPVS connection hash */
71 static unsigned int ip_vs_conn_rnd __read_mostly;
74 * Fine locking granularity for big connection hash table
76 #define CT_LOCKARRAY_BITS 5
77 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
78 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
80 struct ip_vs_aligned_lock
83 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
85 /* lock array for conn table */
86 static struct ip_vs_aligned_lock
87 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
89 static inline void ct_write_lock(unsigned int key)
91 spin_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
94 static inline void ct_write_unlock(unsigned int key)
96 spin_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
101 * Returns hash value for IPVS connection entry
103 static unsigned int ip_vs_conn_hashkey(struct net *net, int af, unsigned int proto,
104 const union nf_inet_addr *addr,
107 #ifdef CONFIG_IP_VS_IPV6
109 return (jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
110 (__force u32)port, proto, ip_vs_conn_rnd) ^
111 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
113 return (jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
115 ((size_t)net>>8)) & ip_vs_conn_tab_mask;
118 static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p,
121 const union nf_inet_addr *addr;
124 if (p->pe_data && p->pe->hashkey_raw)
125 return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) &
128 if (likely(!inverse)) {
136 return ip_vs_conn_hashkey(p->net, p->af, p->protocol, addr, port);
139 static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
141 struct ip_vs_conn_param p;
143 ip_vs_conn_fill_param(ip_vs_conn_net(cp), cp->af, cp->protocol,
144 &cp->caddr, cp->cport, NULL, 0, &p);
148 p.pe_data = cp->pe_data;
149 p.pe_data_len = cp->pe_data_len;
152 return ip_vs_conn_hashkey_param(&p, false);
156 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
157 * returns bool success.
159 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
164 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
167 /* Hash by protocol, client address and port */
168 hash = ip_vs_conn_hashkey_conn(cp);
171 spin_lock(&cp->lock);
173 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
174 cp->flags |= IP_VS_CONN_F_HASHED;
175 atomic_inc(&cp->refcnt);
176 hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
179 pr_err("%s(): request for already hashed, called from %pF\n",
180 __func__, __builtin_return_address(0));
184 spin_unlock(&cp->lock);
185 ct_write_unlock(hash);
192 * UNhashes ip_vs_conn from ip_vs_conn_tab.
193 * returns bool success. Caller should hold conn reference.
195 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
200 /* unhash it and decrease its reference counter */
201 hash = ip_vs_conn_hashkey_conn(cp);
204 spin_lock(&cp->lock);
206 if (cp->flags & IP_VS_CONN_F_HASHED) {
207 hlist_del_rcu(&cp->c_list);
208 cp->flags &= ~IP_VS_CONN_F_HASHED;
209 atomic_dec(&cp->refcnt);
214 spin_unlock(&cp->lock);
215 ct_write_unlock(hash);
220 /* Try to unlink ip_vs_conn from ip_vs_conn_tab.
221 * returns bool success.
223 static inline bool ip_vs_conn_unlink(struct ip_vs_conn *cp)
228 hash = ip_vs_conn_hashkey_conn(cp);
231 spin_lock(&cp->lock);
233 if (cp->flags & IP_VS_CONN_F_HASHED) {
235 /* Decrease refcnt and unlink conn only if we are last user */
236 if (atomic_cmpxchg(&cp->refcnt, 1, 0) == 1) {
237 hlist_del_rcu(&cp->c_list);
238 cp->flags &= ~IP_VS_CONN_F_HASHED;
242 ret = atomic_read(&cp->refcnt) ? false : true;
244 spin_unlock(&cp->lock);
245 ct_write_unlock(hash);
252 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
253 * Called for pkts coming from OUTside-to-INside.
254 * p->caddr, p->cport: pkt source address (foreign host)
255 * p->vaddr, p->vport: pkt dest address (load balancer)
257 static inline struct ip_vs_conn *
258 __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
261 struct ip_vs_conn *cp;
263 hash = ip_vs_conn_hashkey_param(p, false);
267 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
268 if (cp->af == p->af &&
269 p->cport == cp->cport && p->vport == cp->vport &&
270 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
271 ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
272 ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
273 p->protocol == cp->protocol &&
274 ip_vs_conn_net_eq(cp, p->net)) {
275 if (!__ip_vs_conn_get(cp))
288 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
290 struct ip_vs_conn *cp;
292 cp = __ip_vs_conn_in_get(p);
293 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
294 struct ip_vs_conn_param cport_zero_p = *p;
295 cport_zero_p.cport = 0;
296 cp = __ip_vs_conn_in_get(&cport_zero_p);
299 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
300 ip_vs_proto_name(p->protocol),
301 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
302 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
303 cp ? "hit" : "not hit");
309 ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
310 const struct ip_vs_iphdr *iph,
311 int inverse, struct ip_vs_conn_param *p)
313 __be16 _ports[2], *pptr;
314 struct net *net = skb_net(skb);
316 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
320 if (likely(!inverse))
321 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->saddr,
322 pptr[0], &iph->daddr, pptr[1], p);
324 ip_vs_conn_fill_param(net, af, iph->protocol, &iph->daddr,
325 pptr[1], &iph->saddr, pptr[0], p);
330 ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
331 const struct ip_vs_iphdr *iph, int inverse)
333 struct ip_vs_conn_param p;
335 if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
338 return ip_vs_conn_in_get(&p);
340 EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto);
342 /* Get reference to connection template */
343 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
346 struct ip_vs_conn *cp;
348 hash = ip_vs_conn_hashkey_param(p, false);
352 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
353 if (!ip_vs_conn_net_eq(cp, p->net))
355 if (p->pe_data && p->pe->ct_match) {
356 if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
357 if (__ip_vs_conn_get(cp))
363 if (cp->af == p->af &&
364 ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
365 /* protocol should only be IPPROTO_IP if
366 * p->vaddr is a fwmark */
367 ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC :
368 p->af, p->vaddr, &cp->vaddr) &&
369 p->cport == cp->cport && p->vport == cp->vport &&
370 cp->flags & IP_VS_CONN_F_TEMPLATE &&
371 p->protocol == cp->protocol) {
372 if (__ip_vs_conn_get(cp))
381 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
382 ip_vs_proto_name(p->protocol),
383 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
384 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
385 cp ? "hit" : "not hit");
390 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
391 * Called for pkts coming from inside-to-OUTside.
392 * p->caddr, p->cport: pkt source address (inside host)
393 * p->vaddr, p->vport: pkt dest address (foreign host) */
394 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
397 struct ip_vs_conn *cp, *ret=NULL;
400 * Check for "full" addressed entries
402 hash = ip_vs_conn_hashkey_param(p, true);
406 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
407 if (cp->af == p->af &&
408 p->vport == cp->cport && p->cport == cp->dport &&
409 ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
410 ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) &&
411 p->protocol == cp->protocol &&
412 ip_vs_conn_net_eq(cp, p->net)) {
413 if (!__ip_vs_conn_get(cp))
423 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
424 ip_vs_proto_name(p->protocol),
425 IP_VS_DBG_ADDR(p->af, p->caddr), ntohs(p->cport),
426 IP_VS_DBG_ADDR(p->af, p->vaddr), ntohs(p->vport),
427 ret ? "hit" : "not hit");
433 ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
434 const struct ip_vs_iphdr *iph, int inverse)
436 struct ip_vs_conn_param p;
438 if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
441 return ip_vs_conn_out_get(&p);
443 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto);
446 * Put back the conn and restart its timer with its timeout
448 void ip_vs_conn_put(struct ip_vs_conn *cp)
450 unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ?
452 mod_timer(&cp->timer, jiffies+t);
454 __ip_vs_conn_put(cp);
459 * Fill a no_client_port connection with a client port number
461 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
463 if (ip_vs_conn_unhash(cp)) {
464 spin_lock(&cp->lock);
465 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
466 atomic_dec(&ip_vs_conn_no_cport_cnt);
467 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
470 spin_unlock(&cp->lock);
472 /* hash on new dport */
479 * Bind a connection entry with the corresponding packet_xmit.
480 * Called by ip_vs_conn_new.
482 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
484 switch (IP_VS_FWD_METHOD(cp)) {
485 case IP_VS_CONN_F_MASQ:
486 cp->packet_xmit = ip_vs_nat_xmit;
489 case IP_VS_CONN_F_TUNNEL:
490 cp->packet_xmit = ip_vs_tunnel_xmit;
493 case IP_VS_CONN_F_DROUTE:
494 cp->packet_xmit = ip_vs_dr_xmit;
497 case IP_VS_CONN_F_LOCALNODE:
498 cp->packet_xmit = ip_vs_null_xmit;
501 case IP_VS_CONN_F_BYPASS:
502 cp->packet_xmit = ip_vs_bypass_xmit;
507 #ifdef CONFIG_IP_VS_IPV6
508 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
510 switch (IP_VS_FWD_METHOD(cp)) {
511 case IP_VS_CONN_F_MASQ:
512 cp->packet_xmit = ip_vs_nat_xmit_v6;
515 case IP_VS_CONN_F_TUNNEL:
516 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
519 case IP_VS_CONN_F_DROUTE:
520 cp->packet_xmit = ip_vs_dr_xmit_v6;
523 case IP_VS_CONN_F_LOCALNODE:
524 cp->packet_xmit = ip_vs_null_xmit;
527 case IP_VS_CONN_F_BYPASS:
528 cp->packet_xmit = ip_vs_bypass_xmit_v6;
535 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
537 return atomic_read(&dest->activeconns)
538 + atomic_read(&dest->inactconns);
542 * Bind a connection entry with a virtual service destination
543 * Called just after a new connection entry is created.
546 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
548 unsigned int conn_flags;
551 /* if dest is NULL, then return directly */
555 /* Increase the refcnt counter of the dest */
556 atomic_inc(&dest->refcnt);
558 conn_flags = atomic_read(&dest->conn_flags);
559 if (cp->protocol != IPPROTO_UDP)
560 conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
562 /* Bind with the destination and its corresponding transmitter */
563 if (flags & IP_VS_CONN_F_SYNC) {
564 /* if the connection is not template and is created
565 * by sync, preserve the activity flag.
567 if (!(flags & IP_VS_CONN_F_TEMPLATE))
568 conn_flags &= ~IP_VS_CONN_F_INACTIVE;
569 /* connections inherit forwarding method from dest */
570 flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
576 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
577 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
579 ip_vs_proto_name(cp->protocol),
580 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
581 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
582 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
583 ip_vs_fwd_tag(cp), cp->state,
584 cp->flags, atomic_read(&cp->refcnt),
585 atomic_read(&dest->refcnt));
587 /* Update the connection counters */
588 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
589 /* It is a normal connection, so modify the counters
590 * according to the flags, later the protocol can
591 * update them on state change
593 if (!(flags & IP_VS_CONN_F_INACTIVE))
594 atomic_inc(&dest->activeconns);
596 atomic_inc(&dest->inactconns);
598 /* It is a persistent connection/template, so increase
599 the persistent connection counter */
600 atomic_inc(&dest->persistconns);
603 if (dest->u_threshold != 0 &&
604 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
605 dest->flags |= IP_VS_DEST_F_OVERLOAD;
610 * Check if there is a destination for the connection, if so
611 * bind the connection to the destination.
613 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
615 struct ip_vs_dest *dest;
617 dest = ip_vs_find_dest(ip_vs_conn_net(cp), cp->af, &cp->daddr,
618 cp->dport, &cp->vaddr, cp->vport,
619 cp->protocol, cp->fwmark, cp->flags);
621 struct ip_vs_proto_data *pd;
623 spin_lock(&cp->lock);
625 spin_unlock(&cp->lock);
629 /* Applications work depending on the forwarding method
630 * but better to reassign them always when binding dest */
632 ip_vs_unbind_app(cp);
634 ip_vs_bind_dest(cp, dest);
635 spin_unlock(&cp->lock);
637 /* Update its packet transmitter */
638 cp->packet_xmit = NULL;
639 #ifdef CONFIG_IP_VS_IPV6
640 if (cp->af == AF_INET6)
641 ip_vs_bind_xmit_v6(cp);
646 pd = ip_vs_proto_data_get(ip_vs_conn_net(cp), cp->protocol);
647 if (pd && atomic_read(&pd->appcnt))
648 ip_vs_bind_app(cp, pd->pp);
655 * Unbind a connection entry with its VS destination
656 * Called by the ip_vs_conn_expire function.
658 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
660 struct ip_vs_dest *dest = cp->dest;
665 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
666 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
668 ip_vs_proto_name(cp->protocol),
669 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
670 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
671 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
672 ip_vs_fwd_tag(cp), cp->state,
673 cp->flags, atomic_read(&cp->refcnt),
674 atomic_read(&dest->refcnt));
676 /* Update the connection counters */
677 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
678 /* It is a normal connection, so decrease the inactconns
679 or activeconns counter */
680 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
681 atomic_dec(&dest->inactconns);
683 atomic_dec(&dest->activeconns);
686 /* It is a persistent connection/template, so decrease
687 the persistent connection counter */
688 atomic_dec(&dest->persistconns);
691 if (dest->l_threshold != 0) {
692 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
693 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
694 } else if (dest->u_threshold != 0) {
695 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
696 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
698 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
699 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
703 * Simply decrease the refcnt of the dest, because the
704 * dest will be either in service's destination list
707 atomic_dec(&dest->refcnt);
710 static int expire_quiescent_template(struct netns_ipvs *ipvs,
711 struct ip_vs_dest *dest)
714 return ipvs->sysctl_expire_quiescent_template &&
715 (atomic_read(&dest->weight) == 0);
722 * Checking if the destination of a connection template is available.
723 * If available, return 1, otherwise invalidate this connection
724 * template and return 0.
726 int ip_vs_check_template(struct ip_vs_conn *ct)
728 struct ip_vs_dest *dest = ct->dest;
729 struct netns_ipvs *ipvs = net_ipvs(ip_vs_conn_net(ct));
732 * Checking the dest server status.
734 if ((dest == NULL) ||
735 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
736 expire_quiescent_template(ipvs, dest)) {
737 IP_VS_DBG_BUF(9, "check_template: dest not available for "
738 "protocol %s s:%s:%d v:%s:%d "
740 ip_vs_proto_name(ct->protocol),
741 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
743 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
745 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
749 * Invalidate the connection template
751 if (ct->vport != htons(0xffff)) {
752 if (ip_vs_conn_unhash(ct)) {
753 ct->dport = htons(0xffff);
754 ct->vport = htons(0xffff);
761 * Simply decrease the refcnt of the template,
762 * don't restart its timer.
764 __ip_vs_conn_put(ct);
770 static void ip_vs_conn_rcu_free(struct rcu_head *head)
772 struct ip_vs_conn *cp = container_of(head, struct ip_vs_conn,
775 ip_vs_pe_put(cp->pe);
777 kmem_cache_free(ip_vs_conn_cachep, cp);
780 static void ip_vs_conn_expire(unsigned long data)
782 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
783 struct net *net = ip_vs_conn_net(cp);
784 struct netns_ipvs *ipvs = net_ipvs(net);
787 * do I control anybody?
789 if (atomic_read(&cp->n_control))
792 /* Unlink conn if not referenced anymore */
793 if (likely(ip_vs_conn_unlink(cp))) {
794 /* delete the timer if it is activated by other users */
795 del_timer(&cp->timer);
797 /* does anybody control me? */
799 ip_vs_control_del(cp);
801 if (cp->flags & IP_VS_CONN_F_NFCT) {
802 ip_vs_conn_drop_conntrack(cp);
803 /* Do not access conntracks during subsys cleanup
804 * because nf_conntrack_find_get can not be used after
805 * conntrack cleanup for the net.
809 ip_vs_conn_drop_conntrack(cp);
812 if (unlikely(cp->app != NULL))
813 ip_vs_unbind_app(cp);
814 ip_vs_unbind_dest(cp);
815 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
816 atomic_dec(&ip_vs_conn_no_cport_cnt);
817 call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
818 atomic_dec(&ipvs->conn_count);
823 IP_VS_DBG(7, "delayed: conn->refcnt=%d conn->n_control=%d\n",
824 atomic_read(&cp->refcnt),
825 atomic_read(&cp->n_control));
827 atomic_inc(&cp->refcnt);
830 if (ipvs->sync_state & IP_VS_STATE_MASTER)
831 ip_vs_sync_conn(net, cp, sysctl_sync_threshold(ipvs));
836 /* Modify timer, so that it expires as soon as possible.
837 * Can be called without reference only if under RCU lock.
839 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
841 /* Using mod_timer_pending will ensure the timer is not
842 * modified after the final del_timer in ip_vs_conn_expire.
844 if (timer_pending(&cp->timer) &&
845 time_after(cp->timer.expires, jiffies))
846 mod_timer_pending(&cp->timer, jiffies);
851 * Create a new connection entry and hash it into the ip_vs_conn_tab
854 ip_vs_conn_new(const struct ip_vs_conn_param *p,
855 const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
856 struct ip_vs_dest *dest, __u32 fwmark)
858 struct ip_vs_conn *cp;
859 struct netns_ipvs *ipvs = net_ipvs(p->net);
860 struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->net,
863 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
865 IP_VS_ERR_RL("%s(): no memory\n", __func__);
869 INIT_HLIST_NODE(&cp->c_list);
870 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
871 ip_vs_conn_net_set(cp, p->net);
873 cp->protocol = p->protocol;
874 ip_vs_addr_copy(p->af, &cp->caddr, p->caddr);
875 cp->cport = p->cport;
876 ip_vs_addr_copy(p->af, &cp->vaddr, p->vaddr);
877 cp->vport = p->vport;
878 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
879 ip_vs_addr_copy(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af,
884 if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
887 cp->pe_data = p->pe_data;
888 cp->pe_data_len = p->pe_data_len;
890 spin_lock_init(&cp->lock);
893 * Set the entry is referenced by the current thread before hashing
894 * it in the table, so that other thread run ip_vs_random_dropentry
895 * but cannot drop this entry.
897 atomic_set(&cp->refcnt, 1);
899 atomic_set(&cp->n_control, 0);
900 atomic_set(&cp->in_pkts, 0);
902 atomic_inc(&ipvs->conn_count);
903 if (flags & IP_VS_CONN_F_NO_CPORT)
904 atomic_inc(&ip_vs_conn_no_cport_cnt);
906 /* Bind the connection with a destination server */
907 ip_vs_bind_dest(cp, dest);
909 /* Set its state and timeout */
912 cp->sync_endtime = jiffies & ~3UL;
914 /* Bind its packet transmitter */
915 #ifdef CONFIG_IP_VS_IPV6
916 if (p->af == AF_INET6)
917 ip_vs_bind_xmit_v6(cp);
922 if (unlikely(pd && atomic_read(&pd->appcnt)))
923 ip_vs_bind_app(cp, pd->pp);
926 * Allow conntrack to be preserved. By default, conntrack
927 * is created and destroyed for every packet.
928 * Sometimes keeping conntrack can be useful for
929 * IP_VS_CONN_F_ONE_PACKET too.
932 if (ip_vs_conntrack_enabled(ipvs))
933 cp->flags |= IP_VS_CONN_F_NFCT;
935 /* Hash it in the ip_vs_conn_tab finally */
942 * /proc/net/ip_vs_conn entries
944 #ifdef CONFIG_PROC_FS
945 struct ip_vs_iter_state {
946 struct seq_net_private p;
947 struct hlist_head *l;
950 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
953 struct ip_vs_conn *cp;
954 struct ip_vs_iter_state *iter = seq->private;
956 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
958 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
959 /* __ip_vs_conn_get() is not needed by
960 * ip_vs_conn_seq_show and ip_vs_conn_sync_seq_show
963 iter->l = &ip_vs_conn_tab[idx];
973 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
975 struct ip_vs_iter_state *iter = seq->private;
978 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
981 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
983 struct ip_vs_conn *cp = v;
984 struct ip_vs_iter_state *iter = seq->private;
985 struct hlist_node *e;
986 struct hlist_head *l = iter->l;
990 if (v == SEQ_START_TOKEN)
991 return ip_vs_conn_array(seq, 0);
993 /* more on same hash chain? */
994 e = rcu_dereference(hlist_next_rcu(&cp->c_list));
996 return hlist_entry(e, struct ip_vs_conn, c_list);
999 idx = l - ip_vs_conn_tab;
1000 while (++idx < ip_vs_conn_tab_size) {
1002 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1003 iter->l = &ip_vs_conn_tab[idx];
1012 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
1014 struct ip_vs_iter_state *iter = seq->private;
1015 struct hlist_head *l = iter->l;
1021 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
1024 if (v == SEQ_START_TOKEN)
1026 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1028 const struct ip_vs_conn *cp = v;
1029 struct net *net = seq_file_net(seq);
1030 char pe_data[IP_VS_PENAME_MAXLEN + IP_VS_PEDATA_MAXLEN + 3];
1033 if (!ip_vs_conn_net_eq(cp, net))
1037 len = strlen(cp->pe->name);
1038 memcpy(pe_data + 1, cp->pe->name, len);
1039 pe_data[len + 1] = ' ';
1041 len += cp->pe->show_pe_data(cp, pe_data + len);
1043 pe_data[len] = '\0';
1045 #ifdef CONFIG_IP_VS_IPV6
1046 if (cp->af == AF_INET6)
1047 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X "
1048 "%pI6 %04X %-11s %7lu%s\n",
1049 ip_vs_proto_name(cp->protocol),
1050 &cp->caddr.in6, ntohs(cp->cport),
1051 &cp->vaddr.in6, ntohs(cp->vport),
1052 &cp->daddr.in6, ntohs(cp->dport),
1053 ip_vs_state_name(cp->protocol, cp->state),
1054 (cp->timer.expires-jiffies)/HZ, pe_data);
1058 "%-3s %08X %04X %08X %04X"
1059 " %08X %04X %-11s %7lu%s\n",
1060 ip_vs_proto_name(cp->protocol),
1061 ntohl(cp->caddr.ip), ntohs(cp->cport),
1062 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1063 ntohl(cp->daddr.ip), ntohs(cp->dport),
1064 ip_vs_state_name(cp->protocol, cp->state),
1065 (cp->timer.expires-jiffies)/HZ, pe_data);
1070 static const struct seq_operations ip_vs_conn_seq_ops = {
1071 .start = ip_vs_conn_seq_start,
1072 .next = ip_vs_conn_seq_next,
1073 .stop = ip_vs_conn_seq_stop,
1074 .show = ip_vs_conn_seq_show,
1077 static int ip_vs_conn_open(struct inode *inode, struct file *file)
1079 return seq_open_net(inode, file, &ip_vs_conn_seq_ops,
1080 sizeof(struct ip_vs_iter_state));
1083 static const struct file_operations ip_vs_conn_fops = {
1084 .owner = THIS_MODULE,
1085 .open = ip_vs_conn_open,
1087 .llseek = seq_lseek,
1088 .release = seq_release_net,
1091 static const char *ip_vs_origin_name(unsigned int flags)
1093 if (flags & IP_VS_CONN_F_SYNC)
1099 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
1102 if (v == SEQ_START_TOKEN)
1104 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1106 const struct ip_vs_conn *cp = v;
1107 struct net *net = seq_file_net(seq);
1109 if (!ip_vs_conn_net_eq(cp, net))
1112 #ifdef CONFIG_IP_VS_IPV6
1113 if (cp->af == AF_INET6)
1114 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
1115 ip_vs_proto_name(cp->protocol),
1116 &cp->caddr.in6, ntohs(cp->cport),
1117 &cp->vaddr.in6, ntohs(cp->vport),
1118 &cp->daddr.in6, ntohs(cp->dport),
1119 ip_vs_state_name(cp->protocol, cp->state),
1120 ip_vs_origin_name(cp->flags),
1121 (cp->timer.expires-jiffies)/HZ);
1125 "%-3s %08X %04X %08X %04X "
1126 "%08X %04X %-11s %-6s %7lu\n",
1127 ip_vs_proto_name(cp->protocol),
1128 ntohl(cp->caddr.ip), ntohs(cp->cport),
1129 ntohl(cp->vaddr.ip), ntohs(cp->vport),
1130 ntohl(cp->daddr.ip), ntohs(cp->dport),
1131 ip_vs_state_name(cp->protocol, cp->state),
1132 ip_vs_origin_name(cp->flags),
1133 (cp->timer.expires-jiffies)/HZ);
1138 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
1139 .start = ip_vs_conn_seq_start,
1140 .next = ip_vs_conn_seq_next,
1141 .stop = ip_vs_conn_seq_stop,
1142 .show = ip_vs_conn_sync_seq_show,
1145 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
1147 return seq_open_net(inode, file, &ip_vs_conn_sync_seq_ops,
1148 sizeof(struct ip_vs_iter_state));
1151 static const struct file_operations ip_vs_conn_sync_fops = {
1152 .owner = THIS_MODULE,
1153 .open = ip_vs_conn_sync_open,
1155 .llseek = seq_lseek,
1156 .release = seq_release_net,
1163 * Randomly drop connection entries before running out of memory
1165 static inline int todrop_entry(struct ip_vs_conn *cp)
1168 * The drop rate array needs tuning for real environments.
1169 * Called from timer bh only => no locking
1171 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1172 static char todrop_counter[9] = {0};
1175 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1176 This will leave enough time for normal connection to get
1178 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
1181 /* Don't drop the entry if its number of incoming packets is not
1182 located in [0, 8] */
1183 i = atomic_read(&cp->in_pkts);
1184 if (i > 8 || i < 0) return 0;
1186 if (!todrop_rate[i]) return 0;
1187 if (--todrop_counter[i] > 0) return 0;
1189 todrop_counter[i] = todrop_rate[i];
1193 /* Called from keventd and must protect itself from softirqs */
1194 void ip_vs_random_dropentry(struct net *net)
1197 struct ip_vs_conn *cp, *cp_c;
1200 * Randomly scan 1/32 of the whole table every second
1202 for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
1203 unsigned int hash = net_random() & ip_vs_conn_tab_mask;
1206 * Lock is actually needed in this loop.
1210 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
1211 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
1212 /* connection template */
1214 if (!ip_vs_conn_net_eq(cp, net))
1216 if (cp->protocol == IPPROTO_TCP) {
1218 case IP_VS_TCP_S_SYN_RECV:
1219 case IP_VS_TCP_S_SYNACK:
1222 case IP_VS_TCP_S_ESTABLISHED:
1223 if (todrop_entry(cp))
1231 if (!todrop_entry(cp))
1235 IP_VS_DBG(4, "del connection\n");
1236 ip_vs_conn_expire_now(cp);
1238 /* cp->control is valid only with reference to cp */
1239 if (cp_c && __ip_vs_conn_get(cp)) {
1240 IP_VS_DBG(4, "del conn template\n");
1241 ip_vs_conn_expire_now(cp_c);
1242 __ip_vs_conn_put(cp);
1251 * Flush all the connection entries in the ip_vs_conn_tab
1253 static void ip_vs_conn_flush(struct net *net)
1256 struct ip_vs_conn *cp, *cp_c;
1257 struct netns_ipvs *ipvs = net_ipvs(net);
1260 for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1262 * Lock is actually needed in this loop.
1266 hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[idx], c_list) {
1267 if (!ip_vs_conn_net_eq(cp, net))
1269 IP_VS_DBG(4, "del connection\n");
1270 ip_vs_conn_expire_now(cp);
1272 /* cp->control is valid only with reference to cp */
1273 if (cp_c && __ip_vs_conn_get(cp)) {
1274 IP_VS_DBG(4, "del conn template\n");
1275 ip_vs_conn_expire_now(cp_c);
1276 __ip_vs_conn_put(cp);
1282 /* the counter may be not NULL, because maybe some conn entries
1283 are run by slow timer handler or unhashed but still referred */
1284 if (atomic_read(&ipvs->conn_count) != 0) {
1290 * per netns init and exit
1292 int __net_init ip_vs_conn_net_init(struct net *net)
1294 struct netns_ipvs *ipvs = net_ipvs(net);
1296 atomic_set(&ipvs->conn_count, 0);
1298 proc_create("ip_vs_conn", 0, net->proc_net, &ip_vs_conn_fops);
1299 proc_create("ip_vs_conn_sync", 0, net->proc_net, &ip_vs_conn_sync_fops);
1303 void __net_exit ip_vs_conn_net_cleanup(struct net *net)
1305 /* flush all the connection entries first */
1306 ip_vs_conn_flush(net);
1307 remove_proc_entry("ip_vs_conn", net->proc_net);
1308 remove_proc_entry("ip_vs_conn_sync", net->proc_net);
1311 int __init ip_vs_conn_init(void)
1315 /* Compute size and mask */
1316 ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
1317 ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
1320 * Allocate the connection hash table and initialize its list heads
1322 ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
1323 if (!ip_vs_conn_tab)
1326 /* Allocate ip_vs_conn slab cache */
1327 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1328 sizeof(struct ip_vs_conn), 0,
1329 SLAB_HWCACHE_ALIGN, NULL);
1330 if (!ip_vs_conn_cachep) {
1331 vfree(ip_vs_conn_tab);
1335 pr_info("Connection hash table configured "
1336 "(size=%d, memory=%ldKbytes)\n",
1337 ip_vs_conn_tab_size,
1338 (long)(ip_vs_conn_tab_size*sizeof(struct list_head))/1024);
1339 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1340 sizeof(struct ip_vs_conn));
1342 for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1343 INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
1345 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1346 spin_lock_init(&__ip_vs_conntbl_lock_array[idx].l);
1349 /* calculate the random value for connection hash */
1350 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1355 void ip_vs_conn_cleanup(void)
1357 /* Wait all ip_vs_conn_rcu_free() callbacks to complete */
1359 /* Release the empty cache */
1360 kmem_cache_destroy(ip_vs_conn_cachep);
1361 vfree(ip_vs_conn_tab);