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_read_lock(unsigned int key
)
91 read_lock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
94 static inline void ct_read_unlock(unsigned int key
)
96 read_unlock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
99 static inline void ct_write_lock(unsigned int key
)
101 write_lock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
104 static inline void ct_write_unlock(unsigned int key
)
106 write_unlock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
109 static inline void ct_read_lock_bh(unsigned int key
)
111 read_lock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
114 static inline void ct_read_unlock_bh(unsigned int key
)
116 read_unlock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
119 static inline void ct_write_lock_bh(unsigned int key
)
121 write_lock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
124 static inline void ct_write_unlock_bh(unsigned int key
)
126 write_unlock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
131 * Returns hash value for IPVS connection entry
133 static unsigned int ip_vs_conn_hashkey(struct net
*net
, int af
, unsigned int proto
,
134 const union nf_inet_addr
*addr
,
137 #ifdef CONFIG_IP_VS_IPV6
139 return (jhash_3words(jhash(addr
, 16, ip_vs_conn_rnd
),
140 (__force u32
)port
, proto
, ip_vs_conn_rnd
) ^
141 ((size_t)net
>>8)) & ip_vs_conn_tab_mask
;
143 return (jhash_3words((__force u32
)addr
->ip
, (__force u32
)port
, proto
,
145 ((size_t)net
>>8)) & ip_vs_conn_tab_mask
;
148 static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param
*p
,
151 const union nf_inet_addr
*addr
;
154 if (p
->pe_data
&& p
->pe
->hashkey_raw
)
155 return p
->pe
->hashkey_raw(p
, ip_vs_conn_rnd
, inverse
) &
158 if (likely(!inverse
)) {
166 return ip_vs_conn_hashkey(p
->net
, p
->af
, p
->protocol
, addr
, port
);
169 static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn
*cp
)
171 struct ip_vs_conn_param p
;
173 ip_vs_conn_fill_param(ip_vs_conn_net(cp
), cp
->af
, cp
->protocol
,
174 &cp
->caddr
, cp
->cport
, NULL
, 0, &p
);
178 p
.pe_data
= cp
->pe_data
;
179 p
.pe_data_len
= cp
->pe_data_len
;
182 return ip_vs_conn_hashkey_param(&p
, false);
186 * Hashes ip_vs_conn in ip_vs_conn_tab by netns,proto,addr,port.
187 * returns bool success.
189 static inline int ip_vs_conn_hash(struct ip_vs_conn
*cp
)
194 if (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
)
197 /* Hash by protocol, client address and port */
198 hash
= ip_vs_conn_hashkey_conn(cp
);
201 spin_lock(&cp
->lock
);
203 if (!(cp
->flags
& IP_VS_CONN_F_HASHED
)) {
204 hlist_add_head(&cp
->c_list
, &ip_vs_conn_tab
[hash
]);
205 cp
->flags
|= IP_VS_CONN_F_HASHED
;
206 atomic_inc(&cp
->refcnt
);
209 pr_err("%s(): request for already hashed, called from %pF\n",
210 __func__
, __builtin_return_address(0));
214 spin_unlock(&cp
->lock
);
215 ct_write_unlock(hash
);
222 * UNhashes ip_vs_conn from ip_vs_conn_tab.
223 * returns bool success.
225 static inline int ip_vs_conn_unhash(struct ip_vs_conn
*cp
)
230 /* unhash it and decrease its reference counter */
231 hash
= ip_vs_conn_hashkey_conn(cp
);
234 spin_lock(&cp
->lock
);
236 if (cp
->flags
& IP_VS_CONN_F_HASHED
) {
237 hlist_del(&cp
->c_list
);
238 cp
->flags
&= ~IP_VS_CONN_F_HASHED
;
239 atomic_dec(&cp
->refcnt
);
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
;
262 struct hlist_node
*n
;
264 hash
= ip_vs_conn_hashkey_param(p
, false);
268 hlist_for_each_entry(cp
, n
, &ip_vs_conn_tab
[hash
], c_list
) {
269 if (cp
->af
== p
->af
&&
270 p
->cport
== cp
->cport
&& p
->vport
== cp
->vport
&&
271 ip_vs_addr_equal(p
->af
, p
->caddr
, &cp
->caddr
) &&
272 ip_vs_addr_equal(p
->af
, p
->vaddr
, &cp
->vaddr
) &&
273 ((!p
->cport
) ^ (!(cp
->flags
& IP_VS_CONN_F_NO_CPORT
))) &&
274 p
->protocol
== cp
->protocol
&&
275 ip_vs_conn_net_eq(cp
, p
->net
)) {
277 atomic_inc(&cp
->refcnt
);
278 ct_read_unlock(hash
);
283 ct_read_unlock(hash
);
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
;
347 struct hlist_node
*n
;
349 hash
= ip_vs_conn_hashkey_param(p
, false);
353 hlist_for_each_entry(cp
, n
, &ip_vs_conn_tab
[hash
], c_list
) {
354 if (!ip_vs_conn_net_eq(cp
, p
->net
))
356 if (p
->pe_data
&& p
->pe
->ct_match
) {
357 if (p
->pe
== cp
->pe
&& p
->pe
->ct_match(p
, cp
))
362 if (cp
->af
== p
->af
&&
363 ip_vs_addr_equal(p
->af
, p
->caddr
, &cp
->caddr
) &&
364 /* protocol should only be IPPROTO_IP if
365 * p->vaddr is a fwmark */
366 ip_vs_addr_equal(p
->protocol
== IPPROTO_IP
? AF_UNSPEC
:
367 p
->af
, p
->vaddr
, &cp
->vaddr
) &&
368 p
->cport
== cp
->cport
&& p
->vport
== cp
->vport
&&
369 cp
->flags
& IP_VS_CONN_F_TEMPLATE
&&
370 p
->protocol
== cp
->protocol
)
377 atomic_inc(&cp
->refcnt
);
378 ct_read_unlock(hash
);
380 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
381 ip_vs_proto_name(p
->protocol
),
382 IP_VS_DBG_ADDR(p
->af
, p
->caddr
), ntohs(p
->cport
),
383 IP_VS_DBG_ADDR(p
->af
, p
->vaddr
), ntohs(p
->vport
),
384 cp
? "hit" : "not hit");
389 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
390 * Called for pkts coming from inside-to-OUTside.
391 * p->caddr, p->cport: pkt source address (inside host)
392 * p->vaddr, p->vport: pkt dest address (foreign host) */
393 struct ip_vs_conn
*ip_vs_conn_out_get(const struct ip_vs_conn_param
*p
)
396 struct ip_vs_conn
*cp
, *ret
=NULL
;
397 struct hlist_node
*n
;
400 * Check for "full" addressed entries
402 hash
= ip_vs_conn_hashkey_param(p
, true);
406 hlist_for_each_entry(cp
, n
, &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
)) {
414 atomic_inc(&cp
->refcnt
);
420 ct_read_unlock(hash
);
422 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
423 ip_vs_proto_name(p
->protocol
),
424 IP_VS_DBG_ADDR(p
->af
, p
->caddr
), ntohs(p
->cport
),
425 IP_VS_DBG_ADDR(p
->af
, p
->vaddr
), ntohs(p
->vport
),
426 ret
? "hit" : "not hit");
432 ip_vs_conn_out_get_proto(int af
, const struct sk_buff
*skb
,
433 const struct ip_vs_iphdr
*iph
, int inverse
)
435 struct ip_vs_conn_param p
;
437 if (ip_vs_conn_fill_param_proto(af
, skb
, iph
, inverse
, &p
))
440 return ip_vs_conn_out_get(&p
);
442 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto
);
445 * Put back the conn and restart its timer with its timeout
447 void ip_vs_conn_put(struct ip_vs_conn
*cp
)
449 unsigned long t
= (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
) ?
451 mod_timer(&cp
->timer
, jiffies
+t
);
453 __ip_vs_conn_put(cp
);
458 * Fill a no_client_port connection with a client port number
460 void ip_vs_conn_fill_cport(struct ip_vs_conn
*cp
, __be16 cport
)
462 if (ip_vs_conn_unhash(cp
)) {
463 spin_lock(&cp
->lock
);
464 if (cp
->flags
& IP_VS_CONN_F_NO_CPORT
) {
465 atomic_dec(&ip_vs_conn_no_cport_cnt
);
466 cp
->flags
&= ~IP_VS_CONN_F_NO_CPORT
;
469 spin_unlock(&cp
->lock
);
471 /* hash on new dport */
478 * Bind a connection entry with the corresponding packet_xmit.
479 * Called by ip_vs_conn_new.
481 static inline void ip_vs_bind_xmit(struct ip_vs_conn
*cp
)
483 switch (IP_VS_FWD_METHOD(cp
)) {
484 case IP_VS_CONN_F_MASQ
:
485 cp
->packet_xmit
= ip_vs_nat_xmit
;
488 case IP_VS_CONN_F_TUNNEL
:
489 cp
->packet_xmit
= ip_vs_tunnel_xmit
;
492 case IP_VS_CONN_F_DROUTE
:
493 cp
->packet_xmit
= ip_vs_dr_xmit
;
496 case IP_VS_CONN_F_LOCALNODE
:
497 cp
->packet_xmit
= ip_vs_null_xmit
;
500 case IP_VS_CONN_F_BYPASS
:
501 cp
->packet_xmit
= ip_vs_bypass_xmit
;
506 #ifdef CONFIG_IP_VS_IPV6
507 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn
*cp
)
509 switch (IP_VS_FWD_METHOD(cp
)) {
510 case IP_VS_CONN_F_MASQ
:
511 cp
->packet_xmit
= ip_vs_nat_xmit_v6
;
514 case IP_VS_CONN_F_TUNNEL
:
515 cp
->packet_xmit
= ip_vs_tunnel_xmit_v6
;
518 case IP_VS_CONN_F_DROUTE
:
519 cp
->packet_xmit
= ip_vs_dr_xmit_v6
;
522 case IP_VS_CONN_F_LOCALNODE
:
523 cp
->packet_xmit
= ip_vs_null_xmit
;
526 case IP_VS_CONN_F_BYPASS
:
527 cp
->packet_xmit
= ip_vs_bypass_xmit_v6
;
534 static inline int ip_vs_dest_totalconns(struct ip_vs_dest
*dest
)
536 return atomic_read(&dest
->activeconns
)
537 + atomic_read(&dest
->inactconns
);
541 * Bind a connection entry with a virtual service destination
542 * Called just after a new connection entry is created.
545 ip_vs_bind_dest(struct ip_vs_conn
*cp
, struct ip_vs_dest
*dest
)
547 unsigned int conn_flags
;
550 /* if dest is NULL, then return directly */
554 /* Increase the refcnt counter of the dest */
555 atomic_inc(&dest
->refcnt
);
557 conn_flags
= atomic_read(&dest
->conn_flags
);
558 if (cp
->protocol
!= IPPROTO_UDP
)
559 conn_flags
&= ~IP_VS_CONN_F_ONE_PACKET
;
561 /* Bind with the destination and its corresponding transmitter */
562 if (flags
& IP_VS_CONN_F_SYNC
) {
563 /* if the connection is not template and is created
564 * by sync, preserve the activity flag.
566 if (!(flags
& IP_VS_CONN_F_TEMPLATE
))
567 conn_flags
&= ~IP_VS_CONN_F_INACTIVE
;
568 /* connections inherit forwarding method from dest */
569 flags
&= ~(IP_VS_CONN_F_FWD_MASK
| IP_VS_CONN_F_NOOUTPUT
);
575 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
576 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
578 ip_vs_proto_name(cp
->protocol
),
579 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
580 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
581 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
582 ip_vs_fwd_tag(cp
), cp
->state
,
583 cp
->flags
, atomic_read(&cp
->refcnt
),
584 atomic_read(&dest
->refcnt
));
586 /* Update the connection counters */
587 if (!(flags
& IP_VS_CONN_F_TEMPLATE
)) {
588 /* It is a normal connection, so modify the counters
589 * according to the flags, later the protocol can
590 * update them on state change
592 if (!(flags
& IP_VS_CONN_F_INACTIVE
))
593 atomic_inc(&dest
->activeconns
);
595 atomic_inc(&dest
->inactconns
);
597 /* It is a persistent connection/template, so increase
598 the persistent connection counter */
599 atomic_inc(&dest
->persistconns
);
602 if (dest
->u_threshold
!= 0 &&
603 ip_vs_dest_totalconns(dest
) >= dest
->u_threshold
)
604 dest
->flags
|= IP_VS_DEST_F_OVERLOAD
;
609 * Check if there is a destination for the connection, if so
610 * bind the connection to the destination.
612 struct ip_vs_dest
*ip_vs_try_bind_dest(struct ip_vs_conn
*cp
)
614 struct ip_vs_dest
*dest
;
616 dest
= ip_vs_find_dest(ip_vs_conn_net(cp
), cp
->af
, &cp
->daddr
,
617 cp
->dport
, &cp
->vaddr
, cp
->vport
,
618 cp
->protocol
, cp
->fwmark
, cp
->flags
);
620 struct ip_vs_proto_data
*pd
;
622 spin_lock(&cp
->lock
);
624 spin_unlock(&cp
->lock
);
628 /* Applications work depending on the forwarding method
629 * but better to reassign them always when binding dest */
631 ip_vs_unbind_app(cp
);
633 ip_vs_bind_dest(cp
, dest
);
634 spin_unlock(&cp
->lock
);
636 /* Update its packet transmitter */
637 cp
->packet_xmit
= NULL
;
638 #ifdef CONFIG_IP_VS_IPV6
639 if (cp
->af
== AF_INET6
)
640 ip_vs_bind_xmit_v6(cp
);
645 pd
= ip_vs_proto_data_get(ip_vs_conn_net(cp
), cp
->protocol
);
646 if (pd
&& atomic_read(&pd
->appcnt
))
647 ip_vs_bind_app(cp
, pd
->pp
);
654 * Unbind a connection entry with its VS destination
655 * Called by the ip_vs_conn_expire function.
657 static inline void ip_vs_unbind_dest(struct ip_vs_conn
*cp
)
659 struct ip_vs_dest
*dest
= cp
->dest
;
664 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
665 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
667 ip_vs_proto_name(cp
->protocol
),
668 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
669 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
670 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
671 ip_vs_fwd_tag(cp
), cp
->state
,
672 cp
->flags
, atomic_read(&cp
->refcnt
),
673 atomic_read(&dest
->refcnt
));
675 /* Update the connection counters */
676 if (!(cp
->flags
& IP_VS_CONN_F_TEMPLATE
)) {
677 /* It is a normal connection, so decrease the inactconns
678 or activeconns counter */
679 if (cp
->flags
& IP_VS_CONN_F_INACTIVE
) {
680 atomic_dec(&dest
->inactconns
);
682 atomic_dec(&dest
->activeconns
);
685 /* It is a persistent connection/template, so decrease
686 the persistent connection counter */
687 atomic_dec(&dest
->persistconns
);
690 if (dest
->l_threshold
!= 0) {
691 if (ip_vs_dest_totalconns(dest
) < dest
->l_threshold
)
692 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
693 } else if (dest
->u_threshold
!= 0) {
694 if (ip_vs_dest_totalconns(dest
) * 4 < dest
->u_threshold
* 3)
695 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
697 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
698 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
702 * Simply decrease the refcnt of the dest, because the
703 * dest will be either in service's destination list
706 atomic_dec(&dest
->refcnt
);
709 static int expire_quiescent_template(struct netns_ipvs
*ipvs
,
710 struct ip_vs_dest
*dest
)
713 return ipvs
->sysctl_expire_quiescent_template
&&
714 (atomic_read(&dest
->weight
) == 0);
721 * Checking if the destination of a connection template is available.
722 * If available, return 1, otherwise invalidate this connection
723 * template and return 0.
725 int ip_vs_check_template(struct ip_vs_conn
*ct
)
727 struct ip_vs_dest
*dest
= ct
->dest
;
728 struct netns_ipvs
*ipvs
= net_ipvs(ip_vs_conn_net(ct
));
731 * Checking the dest server status.
733 if ((dest
== NULL
) ||
734 !(dest
->flags
& IP_VS_DEST_F_AVAILABLE
) ||
735 expire_quiescent_template(ipvs
, dest
)) {
736 IP_VS_DBG_BUF(9, "check_template: dest not available for "
737 "protocol %s s:%s:%d v:%s:%d "
739 ip_vs_proto_name(ct
->protocol
),
740 IP_VS_DBG_ADDR(ct
->af
, &ct
->caddr
),
742 IP_VS_DBG_ADDR(ct
->af
, &ct
->vaddr
),
744 IP_VS_DBG_ADDR(ct
->af
, &ct
->daddr
),
748 * Invalidate the connection template
750 if (ct
->vport
!= htons(0xffff)) {
751 if (ip_vs_conn_unhash(ct
)) {
752 ct
->dport
= htons(0xffff);
753 ct
->vport
= htons(0xffff);
760 * Simply decrease the refcnt of the template,
761 * don't restart its timer.
763 atomic_dec(&ct
->refcnt
);
769 static void ip_vs_conn_expire(unsigned long data
)
771 struct ip_vs_conn
*cp
= (struct ip_vs_conn
*)data
;
772 struct net
*net
= ip_vs_conn_net(cp
);
773 struct netns_ipvs
*ipvs
= net_ipvs(net
);
780 atomic_inc(&cp
->refcnt
);
783 * do I control anybody?
785 if (atomic_read(&cp
->n_control
))
789 * unhash it if it is hashed in the conn table
791 if (!ip_vs_conn_unhash(cp
) && !(cp
->flags
& IP_VS_CONN_F_ONE_PACKET
))
795 * refcnt==1 implies I'm the only one referrer
797 if (likely(atomic_read(&cp
->refcnt
) == 1)) {
798 /* delete the timer if it is activated by other users */
799 del_timer(&cp
->timer
);
801 /* does anybody control me? */
803 ip_vs_control_del(cp
);
805 if (cp
->flags
& IP_VS_CONN_F_NFCT
) {
806 ip_vs_conn_drop_conntrack(cp
);
807 /* Do not access conntracks during subsys cleanup
808 * because nf_conntrack_find_get can not be used after
809 * conntrack cleanup for the net.
813 ip_vs_conn_drop_conntrack(cp
);
816 ip_vs_pe_put(cp
->pe
);
818 if (unlikely(cp
->app
!= NULL
))
819 ip_vs_unbind_app(cp
);
820 ip_vs_unbind_dest(cp
);
821 if (cp
->flags
& IP_VS_CONN_F_NO_CPORT
)
822 atomic_dec(&ip_vs_conn_no_cport_cnt
);
823 atomic_dec(&ipvs
->conn_count
);
825 kmem_cache_free(ip_vs_conn_cachep
, cp
);
829 /* hash it back to the table */
833 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
834 atomic_read(&cp
->refcnt
)-1,
835 atomic_read(&cp
->n_control
));
837 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
838 ip_vs_sync_conn(net
, cp
, sysctl_sync_threshold(ipvs
));
844 void ip_vs_conn_expire_now(struct ip_vs_conn
*cp
)
846 if (del_timer(&cp
->timer
))
847 mod_timer(&cp
->timer
, jiffies
);
852 * Create a new connection entry and hash it into the ip_vs_conn_tab
855 ip_vs_conn_new(const struct ip_vs_conn_param
*p
,
856 const union nf_inet_addr
*daddr
, __be16 dport
, unsigned int flags
,
857 struct ip_vs_dest
*dest
, __u32 fwmark
)
859 struct ip_vs_conn
*cp
;
860 struct netns_ipvs
*ipvs
= net_ipvs(p
->net
);
861 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(p
->net
,
864 cp
= kmem_cache_zalloc(ip_vs_conn_cachep
, GFP_ATOMIC
);
866 IP_VS_ERR_RL("%s(): no memory\n", __func__
);
870 INIT_HLIST_NODE(&cp
->c_list
);
871 setup_timer(&cp
->timer
, ip_vs_conn_expire
, (unsigned long)cp
);
872 ip_vs_conn_net_set(cp
, p
->net
);
874 cp
->protocol
= p
->protocol
;
875 ip_vs_addr_copy(p
->af
, &cp
->caddr
, p
->caddr
);
876 cp
->cport
= p
->cport
;
877 ip_vs_addr_copy(p
->af
, &cp
->vaddr
, p
->vaddr
);
878 cp
->vport
= p
->vport
;
879 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
880 ip_vs_addr_copy(p
->protocol
== IPPROTO_IP
? AF_UNSPEC
: p
->af
,
885 if (flags
& IP_VS_CONN_F_TEMPLATE
&& p
->pe
) {
888 cp
->pe_data
= p
->pe_data
;
889 cp
->pe_data_len
= p
->pe_data_len
;
891 spin_lock_init(&cp
->lock
);
894 * Set the entry is referenced by the current thread before hashing
895 * it in the table, so that other thread run ip_vs_random_dropentry
896 * but cannot drop this entry.
898 atomic_set(&cp
->refcnt
, 1);
900 atomic_set(&cp
->n_control
, 0);
901 atomic_set(&cp
->in_pkts
, 0);
903 atomic_inc(&ipvs
->conn_count
);
904 if (flags
& IP_VS_CONN_F_NO_CPORT
)
905 atomic_inc(&ip_vs_conn_no_cport_cnt
);
907 /* Bind the connection with a destination server */
908 ip_vs_bind_dest(cp
, dest
);
910 /* Set its state and timeout */
913 cp
->sync_endtime
= jiffies
& ~3UL;
915 /* Bind its packet transmitter */
916 #ifdef CONFIG_IP_VS_IPV6
917 if (p
->af
== AF_INET6
)
918 ip_vs_bind_xmit_v6(cp
);
923 if (unlikely(pd
&& atomic_read(&pd
->appcnt
)))
924 ip_vs_bind_app(cp
, pd
->pp
);
927 * Allow conntrack to be preserved. By default, conntrack
928 * is created and destroyed for every packet.
929 * Sometimes keeping conntrack can be useful for
930 * IP_VS_CONN_F_ONE_PACKET too.
933 if (ip_vs_conntrack_enabled(ipvs
))
934 cp
->flags
|= IP_VS_CONN_F_NFCT
;
936 /* Hash it in the ip_vs_conn_tab finally */
943 * /proc/net/ip_vs_conn entries
945 #ifdef CONFIG_PROC_FS
946 struct ip_vs_iter_state
{
947 struct seq_net_private p
;
948 struct hlist_head
*l
;
951 static void *ip_vs_conn_array(struct seq_file
*seq
, loff_t pos
)
954 struct ip_vs_conn
*cp
;
955 struct ip_vs_iter_state
*iter
= seq
->private;
956 struct hlist_node
*n
;
958 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
959 ct_read_lock_bh(idx
);
960 hlist_for_each_entry(cp
, n
, &ip_vs_conn_tab
[idx
], c_list
) {
962 iter
->l
= &ip_vs_conn_tab
[idx
];
966 ct_read_unlock_bh(idx
);
972 static void *ip_vs_conn_seq_start(struct seq_file
*seq
, loff_t
*pos
)
974 struct ip_vs_iter_state
*iter
= seq
->private;
977 return *pos
? ip_vs_conn_array(seq
, *pos
- 1) :SEQ_START_TOKEN
;
980 static void *ip_vs_conn_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
982 struct ip_vs_conn
*cp
= v
;
983 struct ip_vs_iter_state
*iter
= seq
->private;
984 struct hlist_node
*e
;
985 struct hlist_head
*l
= iter
->l
;
989 if (v
== SEQ_START_TOKEN
)
990 return ip_vs_conn_array(seq
, 0);
992 /* more on same hash chain? */
993 if ((e
= cp
->c_list
.next
))
994 return hlist_entry(e
, struct ip_vs_conn
, c_list
);
996 idx
= l
- ip_vs_conn_tab
;
997 ct_read_unlock_bh(idx
);
999 while (++idx
< ip_vs_conn_tab_size
) {
1000 ct_read_lock_bh(idx
);
1001 hlist_for_each_entry(cp
, e
, &ip_vs_conn_tab
[idx
], c_list
) {
1002 iter
->l
= &ip_vs_conn_tab
[idx
];
1005 ct_read_unlock_bh(idx
);
1011 static void ip_vs_conn_seq_stop(struct seq_file
*seq
, void *v
)
1013 struct ip_vs_iter_state
*iter
= seq
->private;
1014 struct hlist_head
*l
= iter
->l
;
1017 ct_read_unlock_bh(l
- ip_vs_conn_tab
);
1020 static int ip_vs_conn_seq_show(struct seq_file
*seq
, void *v
)
1023 if (v
== SEQ_START_TOKEN
)
1025 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1027 const struct ip_vs_conn
*cp
= v
;
1028 struct net
*net
= seq_file_net(seq
);
1029 char pe_data
[IP_VS_PENAME_MAXLEN
+ IP_VS_PEDATA_MAXLEN
+ 3];
1032 if (!ip_vs_conn_net_eq(cp
, net
))
1036 len
= strlen(cp
->pe
->name
);
1037 memcpy(pe_data
+ 1, cp
->pe
->name
, len
);
1038 pe_data
[len
+ 1] = ' ';
1040 len
+= cp
->pe
->show_pe_data(cp
, pe_data
+ len
);
1042 pe_data
[len
] = '\0';
1044 #ifdef CONFIG_IP_VS_IPV6
1045 if (cp
->af
== AF_INET6
)
1046 seq_printf(seq
, "%-3s %pI6 %04X %pI6 %04X "
1047 "%pI6 %04X %-11s %7lu%s\n",
1048 ip_vs_proto_name(cp
->protocol
),
1049 &cp
->caddr
.in6
, ntohs(cp
->cport
),
1050 &cp
->vaddr
.in6
, ntohs(cp
->vport
),
1051 &cp
->daddr
.in6
, ntohs(cp
->dport
),
1052 ip_vs_state_name(cp
->protocol
, cp
->state
),
1053 (cp
->timer
.expires
-jiffies
)/HZ
, pe_data
);
1057 "%-3s %08X %04X %08X %04X"
1058 " %08X %04X %-11s %7lu%s\n",
1059 ip_vs_proto_name(cp
->protocol
),
1060 ntohl(cp
->caddr
.ip
), ntohs(cp
->cport
),
1061 ntohl(cp
->vaddr
.ip
), ntohs(cp
->vport
),
1062 ntohl(cp
->daddr
.ip
), ntohs(cp
->dport
),
1063 ip_vs_state_name(cp
->protocol
, cp
->state
),
1064 (cp
->timer
.expires
-jiffies
)/HZ
, pe_data
);
1069 static const struct seq_operations ip_vs_conn_seq_ops
= {
1070 .start
= ip_vs_conn_seq_start
,
1071 .next
= ip_vs_conn_seq_next
,
1072 .stop
= ip_vs_conn_seq_stop
,
1073 .show
= ip_vs_conn_seq_show
,
1076 static int ip_vs_conn_open(struct inode
*inode
, struct file
*file
)
1078 return seq_open_net(inode
, file
, &ip_vs_conn_seq_ops
,
1079 sizeof(struct ip_vs_iter_state
));
1082 static const struct file_operations ip_vs_conn_fops
= {
1083 .owner
= THIS_MODULE
,
1084 .open
= ip_vs_conn_open
,
1086 .llseek
= seq_lseek
,
1087 .release
= seq_release_net
,
1090 static const char *ip_vs_origin_name(unsigned int flags
)
1092 if (flags
& IP_VS_CONN_F_SYNC
)
1098 static int ip_vs_conn_sync_seq_show(struct seq_file
*seq
, void *v
)
1101 if (v
== SEQ_START_TOKEN
)
1103 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1105 const struct ip_vs_conn
*cp
= v
;
1106 struct net
*net
= seq_file_net(seq
);
1108 if (!ip_vs_conn_net_eq(cp
, net
))
1111 #ifdef CONFIG_IP_VS_IPV6
1112 if (cp
->af
== AF_INET6
)
1113 seq_printf(seq
, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
1114 ip_vs_proto_name(cp
->protocol
),
1115 &cp
->caddr
.in6
, ntohs(cp
->cport
),
1116 &cp
->vaddr
.in6
, ntohs(cp
->vport
),
1117 &cp
->daddr
.in6
, ntohs(cp
->dport
),
1118 ip_vs_state_name(cp
->protocol
, cp
->state
),
1119 ip_vs_origin_name(cp
->flags
),
1120 (cp
->timer
.expires
-jiffies
)/HZ
);
1124 "%-3s %08X %04X %08X %04X "
1125 "%08X %04X %-11s %-6s %7lu\n",
1126 ip_vs_proto_name(cp
->protocol
),
1127 ntohl(cp
->caddr
.ip
), ntohs(cp
->cport
),
1128 ntohl(cp
->vaddr
.ip
), ntohs(cp
->vport
),
1129 ntohl(cp
->daddr
.ip
), ntohs(cp
->dport
),
1130 ip_vs_state_name(cp
->protocol
, cp
->state
),
1131 ip_vs_origin_name(cp
->flags
),
1132 (cp
->timer
.expires
-jiffies
)/HZ
);
1137 static const struct seq_operations ip_vs_conn_sync_seq_ops
= {
1138 .start
= ip_vs_conn_seq_start
,
1139 .next
= ip_vs_conn_seq_next
,
1140 .stop
= ip_vs_conn_seq_stop
,
1141 .show
= ip_vs_conn_sync_seq_show
,
1144 static int ip_vs_conn_sync_open(struct inode
*inode
, struct file
*file
)
1146 return seq_open_net(inode
, file
, &ip_vs_conn_sync_seq_ops
,
1147 sizeof(struct ip_vs_iter_state
));
1150 static const struct file_operations ip_vs_conn_sync_fops
= {
1151 .owner
= THIS_MODULE
,
1152 .open
= ip_vs_conn_sync_open
,
1154 .llseek
= seq_lseek
,
1155 .release
= seq_release_net
,
1162 * Randomly drop connection entries before running out of memory
1164 static inline int todrop_entry(struct ip_vs_conn
*cp
)
1167 * The drop rate array needs tuning for real environments.
1168 * Called from timer bh only => no locking
1170 static const char todrop_rate
[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1171 static char todrop_counter
[9] = {0};
1174 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1175 This will leave enough time for normal connection to get
1177 if (time_before(cp
->timeout
+ jiffies
, cp
->timer
.expires
+ 60*HZ
))
1180 /* Don't drop the entry if its number of incoming packets is not
1181 located in [0, 8] */
1182 i
= atomic_read(&cp
->in_pkts
);
1183 if (i
> 8 || i
< 0) return 0;
1185 if (!todrop_rate
[i
]) return 0;
1186 if (--todrop_counter
[i
] > 0) return 0;
1188 todrop_counter
[i
] = todrop_rate
[i
];
1192 /* Called from keventd and must protect itself from softirqs */
1193 void ip_vs_random_dropentry(struct net
*net
)
1196 struct ip_vs_conn
*cp
;
1199 * Randomly scan 1/32 of the whole table every second
1201 for (idx
= 0; idx
< (ip_vs_conn_tab_size
>>5); idx
++) {
1202 unsigned int hash
= net_random() & ip_vs_conn_tab_mask
;
1203 struct hlist_node
*n
;
1206 * Lock is actually needed in this loop.
1208 ct_write_lock_bh(hash
);
1210 hlist_for_each_entry(cp
, n
, &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 IP_VS_DBG(4, "del conn template\n");
1239 ip_vs_conn_expire_now(cp
->control
);
1242 ct_write_unlock_bh(hash
);
1248 * Flush all the connection entries in the ip_vs_conn_tab
1250 static void ip_vs_conn_flush(struct net
*net
)
1253 struct ip_vs_conn
*cp
;
1254 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1257 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
1258 struct hlist_node
*n
;
1261 * Lock is actually needed in this loop.
1263 ct_write_lock_bh(idx
);
1265 hlist_for_each_entry(cp
, n
, &ip_vs_conn_tab
[idx
], c_list
) {
1266 if (!ip_vs_conn_net_eq(cp
, net
))
1268 IP_VS_DBG(4, "del connection\n");
1269 ip_vs_conn_expire_now(cp
);
1271 IP_VS_DBG(4, "del conn template\n");
1272 ip_vs_conn_expire_now(cp
->control
);
1275 ct_write_unlock_bh(idx
);
1278 /* the counter may be not NULL, because maybe some conn entries
1279 are run by slow timer handler or unhashed but still referred */
1280 if (atomic_read(&ipvs
->conn_count
) != 0) {
1286 * per netns init and exit
1288 int __net_init
ip_vs_conn_net_init(struct net
*net
)
1290 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1292 atomic_set(&ipvs
->conn_count
, 0);
1294 proc_create("ip_vs_conn", 0, net
->proc_net
, &ip_vs_conn_fops
);
1295 proc_create("ip_vs_conn_sync", 0, net
->proc_net
, &ip_vs_conn_sync_fops
);
1299 void __net_exit
ip_vs_conn_net_cleanup(struct net
*net
)
1301 /* flush all the connection entries first */
1302 ip_vs_conn_flush(net
);
1303 remove_proc_entry("ip_vs_conn", net
->proc_net
);
1304 remove_proc_entry("ip_vs_conn_sync", net
->proc_net
);
1307 int __init
ip_vs_conn_init(void)
1311 /* Compute size and mask */
1312 ip_vs_conn_tab_size
= 1 << ip_vs_conn_tab_bits
;
1313 ip_vs_conn_tab_mask
= ip_vs_conn_tab_size
- 1;
1316 * Allocate the connection hash table and initialize its list heads
1318 ip_vs_conn_tab
= vmalloc(ip_vs_conn_tab_size
* sizeof(*ip_vs_conn_tab
));
1319 if (!ip_vs_conn_tab
)
1322 /* Allocate ip_vs_conn slab cache */
1323 ip_vs_conn_cachep
= kmem_cache_create("ip_vs_conn",
1324 sizeof(struct ip_vs_conn
), 0,
1325 SLAB_HWCACHE_ALIGN
, NULL
);
1326 if (!ip_vs_conn_cachep
) {
1327 vfree(ip_vs_conn_tab
);
1331 pr_info("Connection hash table configured "
1332 "(size=%d, memory=%ldKbytes)\n",
1333 ip_vs_conn_tab_size
,
1334 (long)(ip_vs_conn_tab_size
*sizeof(struct list_head
))/1024);
1335 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1336 sizeof(struct ip_vs_conn
));
1338 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++)
1339 INIT_HLIST_HEAD(&ip_vs_conn_tab
[idx
]);
1341 for (idx
= 0; idx
< CT_LOCKARRAY_SIZE
; idx
++) {
1342 rwlock_init(&__ip_vs_conntbl_lock_array
[idx
].l
);
1345 /* calculate the random value for connection hash */
1346 get_random_bytes(&ip_vs_conn_rnd
, sizeof(ip_vs_conn_rnd
));
1351 void ip_vs_conn_cleanup(void)
1353 /* Release the empty cache */
1354 kmem_cache_destroy(ip_vs_conn_cachep
);
1355 vfree(ip_vs_conn_tab
);