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
;
263 hash
= ip_vs_conn_hashkey_param(p
, false);
267 hlist_for_each_entry(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
)) {
276 atomic_inc(&cp
->refcnt
);
277 ct_read_unlock(hash
);
282 ct_read_unlock(hash
);
287 struct ip_vs_conn
*ip_vs_conn_in_get(const struct ip_vs_conn_param
*p
)
289 struct ip_vs_conn
*cp
;
291 cp
= __ip_vs_conn_in_get(p
);
292 if (!cp
&& atomic_read(&ip_vs_conn_no_cport_cnt
)) {
293 struct ip_vs_conn_param cport_zero_p
= *p
;
294 cport_zero_p
.cport
= 0;
295 cp
= __ip_vs_conn_in_get(&cport_zero_p
);
298 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
299 ip_vs_proto_name(p
->protocol
),
300 IP_VS_DBG_ADDR(p
->af
, p
->caddr
), ntohs(p
->cport
),
301 IP_VS_DBG_ADDR(p
->af
, p
->vaddr
), ntohs(p
->vport
),
302 cp
? "hit" : "not hit");
308 ip_vs_conn_fill_param_proto(int af
, const struct sk_buff
*skb
,
309 const struct ip_vs_iphdr
*iph
,
310 int inverse
, struct ip_vs_conn_param
*p
)
312 __be16 _ports
[2], *pptr
;
313 struct net
*net
= skb_net(skb
);
315 pptr
= frag_safe_skb_hp(skb
, iph
->len
, sizeof(_ports
), _ports
, iph
);
319 if (likely(!inverse
))
320 ip_vs_conn_fill_param(net
, af
, iph
->protocol
, &iph
->saddr
,
321 pptr
[0], &iph
->daddr
, pptr
[1], p
);
323 ip_vs_conn_fill_param(net
, af
, iph
->protocol
, &iph
->daddr
,
324 pptr
[1], &iph
->saddr
, pptr
[0], p
);
329 ip_vs_conn_in_get_proto(int af
, const struct sk_buff
*skb
,
330 const struct ip_vs_iphdr
*iph
, int inverse
)
332 struct ip_vs_conn_param p
;
334 if (ip_vs_conn_fill_param_proto(af
, skb
, iph
, inverse
, &p
))
337 return ip_vs_conn_in_get(&p
);
339 EXPORT_SYMBOL_GPL(ip_vs_conn_in_get_proto
);
341 /* Get reference to connection template */
342 struct ip_vs_conn
*ip_vs_ct_in_get(const struct ip_vs_conn_param
*p
)
345 struct ip_vs_conn
*cp
;
347 hash
= ip_vs_conn_hashkey_param(p
, false);
351 hlist_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
352 if (!ip_vs_conn_net_eq(cp
, p
->net
))
354 if (p
->pe_data
&& p
->pe
->ct_match
) {
355 if (p
->pe
== cp
->pe
&& p
->pe
->ct_match(p
, cp
))
360 if (cp
->af
== p
->af
&&
361 ip_vs_addr_equal(p
->af
, p
->caddr
, &cp
->caddr
) &&
362 /* protocol should only be IPPROTO_IP if
363 * p->vaddr is a fwmark */
364 ip_vs_addr_equal(p
->protocol
== IPPROTO_IP
? AF_UNSPEC
:
365 p
->af
, p
->vaddr
, &cp
->vaddr
) &&
366 p
->cport
== cp
->cport
&& p
->vport
== cp
->vport
&&
367 cp
->flags
& IP_VS_CONN_F_TEMPLATE
&&
368 p
->protocol
== cp
->protocol
)
375 atomic_inc(&cp
->refcnt
);
376 ct_read_unlock(hash
);
378 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
379 ip_vs_proto_name(p
->protocol
),
380 IP_VS_DBG_ADDR(p
->af
, p
->caddr
), ntohs(p
->cport
),
381 IP_VS_DBG_ADDR(p
->af
, p
->vaddr
), ntohs(p
->vport
),
382 cp
? "hit" : "not hit");
387 /* Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
388 * Called for pkts coming from inside-to-OUTside.
389 * p->caddr, p->cport: pkt source address (inside host)
390 * p->vaddr, p->vport: pkt dest address (foreign host) */
391 struct ip_vs_conn
*ip_vs_conn_out_get(const struct ip_vs_conn_param
*p
)
394 struct ip_vs_conn
*cp
, *ret
=NULL
;
397 * Check for "full" addressed entries
399 hash
= ip_vs_conn_hashkey_param(p
, true);
403 hlist_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
404 if (cp
->af
== p
->af
&&
405 p
->vport
== cp
->cport
&& p
->cport
== cp
->dport
&&
406 ip_vs_addr_equal(p
->af
, p
->vaddr
, &cp
->caddr
) &&
407 ip_vs_addr_equal(p
->af
, p
->caddr
, &cp
->daddr
) &&
408 p
->protocol
== cp
->protocol
&&
409 ip_vs_conn_net_eq(cp
, p
->net
)) {
411 atomic_inc(&cp
->refcnt
);
417 ct_read_unlock(hash
);
419 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
420 ip_vs_proto_name(p
->protocol
),
421 IP_VS_DBG_ADDR(p
->af
, p
->caddr
), ntohs(p
->cport
),
422 IP_VS_DBG_ADDR(p
->af
, p
->vaddr
), ntohs(p
->vport
),
423 ret
? "hit" : "not hit");
429 ip_vs_conn_out_get_proto(int af
, const struct sk_buff
*skb
,
430 const struct ip_vs_iphdr
*iph
, int inverse
)
432 struct ip_vs_conn_param p
;
434 if (ip_vs_conn_fill_param_proto(af
, skb
, iph
, inverse
, &p
))
437 return ip_vs_conn_out_get(&p
);
439 EXPORT_SYMBOL_GPL(ip_vs_conn_out_get_proto
);
442 * Put back the conn and restart its timer with its timeout
444 void ip_vs_conn_put(struct ip_vs_conn
*cp
)
446 unsigned long t
= (cp
->flags
& IP_VS_CONN_F_ONE_PACKET
) ?
448 mod_timer(&cp
->timer
, jiffies
+t
);
450 __ip_vs_conn_put(cp
);
455 * Fill a no_client_port connection with a client port number
457 void ip_vs_conn_fill_cport(struct ip_vs_conn
*cp
, __be16 cport
)
459 if (ip_vs_conn_unhash(cp
)) {
460 spin_lock(&cp
->lock
);
461 if (cp
->flags
& IP_VS_CONN_F_NO_CPORT
) {
462 atomic_dec(&ip_vs_conn_no_cport_cnt
);
463 cp
->flags
&= ~IP_VS_CONN_F_NO_CPORT
;
466 spin_unlock(&cp
->lock
);
468 /* hash on new dport */
475 * Bind a connection entry with the corresponding packet_xmit.
476 * Called by ip_vs_conn_new.
478 static inline void ip_vs_bind_xmit(struct ip_vs_conn
*cp
)
480 switch (IP_VS_FWD_METHOD(cp
)) {
481 case IP_VS_CONN_F_MASQ
:
482 cp
->packet_xmit
= ip_vs_nat_xmit
;
485 case IP_VS_CONN_F_TUNNEL
:
486 cp
->packet_xmit
= ip_vs_tunnel_xmit
;
489 case IP_VS_CONN_F_DROUTE
:
490 cp
->packet_xmit
= ip_vs_dr_xmit
;
493 case IP_VS_CONN_F_LOCALNODE
:
494 cp
->packet_xmit
= ip_vs_null_xmit
;
497 case IP_VS_CONN_F_BYPASS
:
498 cp
->packet_xmit
= ip_vs_bypass_xmit
;
503 #ifdef CONFIG_IP_VS_IPV6
504 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn
*cp
)
506 switch (IP_VS_FWD_METHOD(cp
)) {
507 case IP_VS_CONN_F_MASQ
:
508 cp
->packet_xmit
= ip_vs_nat_xmit_v6
;
511 case IP_VS_CONN_F_TUNNEL
:
512 cp
->packet_xmit
= ip_vs_tunnel_xmit_v6
;
515 case IP_VS_CONN_F_DROUTE
:
516 cp
->packet_xmit
= ip_vs_dr_xmit_v6
;
519 case IP_VS_CONN_F_LOCALNODE
:
520 cp
->packet_xmit
= ip_vs_null_xmit
;
523 case IP_VS_CONN_F_BYPASS
:
524 cp
->packet_xmit
= ip_vs_bypass_xmit_v6
;
531 static inline int ip_vs_dest_totalconns(struct ip_vs_dest
*dest
)
533 return atomic_read(&dest
->activeconns
)
534 + atomic_read(&dest
->inactconns
);
538 * Bind a connection entry with a virtual service destination
539 * Called just after a new connection entry is created.
542 ip_vs_bind_dest(struct ip_vs_conn
*cp
, struct ip_vs_dest
*dest
)
544 unsigned int conn_flags
;
547 /* if dest is NULL, then return directly */
551 /* Increase the refcnt counter of the dest */
552 atomic_inc(&dest
->refcnt
);
554 conn_flags
= atomic_read(&dest
->conn_flags
);
555 if (cp
->protocol
!= IPPROTO_UDP
)
556 conn_flags
&= ~IP_VS_CONN_F_ONE_PACKET
;
558 /* Bind with the destination and its corresponding transmitter */
559 if (flags
& IP_VS_CONN_F_SYNC
) {
560 /* if the connection is not template and is created
561 * by sync, preserve the activity flag.
563 if (!(flags
& IP_VS_CONN_F_TEMPLATE
))
564 conn_flags
&= ~IP_VS_CONN_F_INACTIVE
;
565 /* connections inherit forwarding method from dest */
566 flags
&= ~(IP_VS_CONN_F_FWD_MASK
| IP_VS_CONN_F_NOOUTPUT
);
572 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
573 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
575 ip_vs_proto_name(cp
->protocol
),
576 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
577 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
578 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
579 ip_vs_fwd_tag(cp
), cp
->state
,
580 cp
->flags
, atomic_read(&cp
->refcnt
),
581 atomic_read(&dest
->refcnt
));
583 /* Update the connection counters */
584 if (!(flags
& IP_VS_CONN_F_TEMPLATE
)) {
585 /* It is a normal connection, so modify the counters
586 * according to the flags, later the protocol can
587 * update them on state change
589 if (!(flags
& IP_VS_CONN_F_INACTIVE
))
590 atomic_inc(&dest
->activeconns
);
592 atomic_inc(&dest
->inactconns
);
594 /* It is a persistent connection/template, so increase
595 the persistent connection counter */
596 atomic_inc(&dest
->persistconns
);
599 if (dest
->u_threshold
!= 0 &&
600 ip_vs_dest_totalconns(dest
) >= dest
->u_threshold
)
601 dest
->flags
|= IP_VS_DEST_F_OVERLOAD
;
606 * Check if there is a destination for the connection, if so
607 * bind the connection to the destination.
609 struct ip_vs_dest
*ip_vs_try_bind_dest(struct ip_vs_conn
*cp
)
611 struct ip_vs_dest
*dest
;
613 dest
= ip_vs_find_dest(ip_vs_conn_net(cp
), cp
->af
, &cp
->daddr
,
614 cp
->dport
, &cp
->vaddr
, cp
->vport
,
615 cp
->protocol
, cp
->fwmark
, cp
->flags
);
617 struct ip_vs_proto_data
*pd
;
619 spin_lock(&cp
->lock
);
621 spin_unlock(&cp
->lock
);
625 /* Applications work depending on the forwarding method
626 * but better to reassign them always when binding dest */
628 ip_vs_unbind_app(cp
);
630 ip_vs_bind_dest(cp
, dest
);
631 spin_unlock(&cp
->lock
);
633 /* Update its packet transmitter */
634 cp
->packet_xmit
= NULL
;
635 #ifdef CONFIG_IP_VS_IPV6
636 if (cp
->af
== AF_INET6
)
637 ip_vs_bind_xmit_v6(cp
);
642 pd
= ip_vs_proto_data_get(ip_vs_conn_net(cp
), cp
->protocol
);
643 if (pd
&& atomic_read(&pd
->appcnt
))
644 ip_vs_bind_app(cp
, pd
->pp
);
651 * Unbind a connection entry with its VS destination
652 * Called by the ip_vs_conn_expire function.
654 static inline void ip_vs_unbind_dest(struct ip_vs_conn
*cp
)
656 struct ip_vs_dest
*dest
= cp
->dest
;
661 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
662 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
664 ip_vs_proto_name(cp
->protocol
),
665 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
666 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
667 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
668 ip_vs_fwd_tag(cp
), cp
->state
,
669 cp
->flags
, atomic_read(&cp
->refcnt
),
670 atomic_read(&dest
->refcnt
));
672 /* Update the connection counters */
673 if (!(cp
->flags
& IP_VS_CONN_F_TEMPLATE
)) {
674 /* It is a normal connection, so decrease the inactconns
675 or activeconns counter */
676 if (cp
->flags
& IP_VS_CONN_F_INACTIVE
) {
677 atomic_dec(&dest
->inactconns
);
679 atomic_dec(&dest
->activeconns
);
682 /* It is a persistent connection/template, so decrease
683 the persistent connection counter */
684 atomic_dec(&dest
->persistconns
);
687 if (dest
->l_threshold
!= 0) {
688 if (ip_vs_dest_totalconns(dest
) < dest
->l_threshold
)
689 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
690 } else if (dest
->u_threshold
!= 0) {
691 if (ip_vs_dest_totalconns(dest
) * 4 < dest
->u_threshold
* 3)
692 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
694 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
695 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
699 * Simply decrease the refcnt of the dest, because the
700 * dest will be either in service's destination list
703 atomic_dec(&dest
->refcnt
);
706 static int expire_quiescent_template(struct netns_ipvs
*ipvs
,
707 struct ip_vs_dest
*dest
)
710 return ipvs
->sysctl_expire_quiescent_template
&&
711 (atomic_read(&dest
->weight
) == 0);
718 * Checking if the destination of a connection template is available.
719 * If available, return 1, otherwise invalidate this connection
720 * template and return 0.
722 int ip_vs_check_template(struct ip_vs_conn
*ct
)
724 struct ip_vs_dest
*dest
= ct
->dest
;
725 struct netns_ipvs
*ipvs
= net_ipvs(ip_vs_conn_net(ct
));
728 * Checking the dest server status.
730 if ((dest
== NULL
) ||
731 !(dest
->flags
& IP_VS_DEST_F_AVAILABLE
) ||
732 expire_quiescent_template(ipvs
, dest
)) {
733 IP_VS_DBG_BUF(9, "check_template: dest not available for "
734 "protocol %s s:%s:%d v:%s:%d "
736 ip_vs_proto_name(ct
->protocol
),
737 IP_VS_DBG_ADDR(ct
->af
, &ct
->caddr
),
739 IP_VS_DBG_ADDR(ct
->af
, &ct
->vaddr
),
741 IP_VS_DBG_ADDR(ct
->af
, &ct
->daddr
),
745 * Invalidate the connection template
747 if (ct
->vport
!= htons(0xffff)) {
748 if (ip_vs_conn_unhash(ct
)) {
749 ct
->dport
= htons(0xffff);
750 ct
->vport
= htons(0xffff);
757 * Simply decrease the refcnt of the template,
758 * don't restart its timer.
760 atomic_dec(&ct
->refcnt
);
766 static void ip_vs_conn_expire(unsigned long data
)
768 struct ip_vs_conn
*cp
= (struct ip_vs_conn
*)data
;
769 struct net
*net
= ip_vs_conn_net(cp
);
770 struct netns_ipvs
*ipvs
= net_ipvs(net
);
777 atomic_inc(&cp
->refcnt
);
780 * do I control anybody?
782 if (atomic_read(&cp
->n_control
))
786 * unhash it if it is hashed in the conn table
788 if (!ip_vs_conn_unhash(cp
) && !(cp
->flags
& IP_VS_CONN_F_ONE_PACKET
))
792 * refcnt==1 implies I'm the only one referrer
794 if (likely(atomic_read(&cp
->refcnt
) == 1)) {
795 /* delete the timer if it is activated by other users */
796 del_timer(&cp
->timer
);
798 /* does anybody control me? */
800 ip_vs_control_del(cp
);
802 if (cp
->flags
& IP_VS_CONN_F_NFCT
) {
803 ip_vs_conn_drop_conntrack(cp
);
804 /* Do not access conntracks during subsys cleanup
805 * because nf_conntrack_find_get can not be used after
806 * conntrack cleanup for the net.
810 ip_vs_conn_drop_conntrack(cp
);
813 ip_vs_pe_put(cp
->pe
);
815 if (unlikely(cp
->app
!= NULL
))
816 ip_vs_unbind_app(cp
);
817 ip_vs_unbind_dest(cp
);
818 if (cp
->flags
& IP_VS_CONN_F_NO_CPORT
)
819 atomic_dec(&ip_vs_conn_no_cport_cnt
);
820 atomic_dec(&ipvs
->conn_count
);
822 kmem_cache_free(ip_vs_conn_cachep
, cp
);
826 /* hash it back to the table */
830 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
831 atomic_read(&cp
->refcnt
)-1,
832 atomic_read(&cp
->n_control
));
834 if (ipvs
->sync_state
& IP_VS_STATE_MASTER
)
835 ip_vs_sync_conn(net
, cp
, sysctl_sync_threshold(ipvs
));
841 void ip_vs_conn_expire_now(struct ip_vs_conn
*cp
)
843 if (del_timer(&cp
->timer
))
844 mod_timer(&cp
->timer
, jiffies
);
849 * Create a new connection entry and hash it into the ip_vs_conn_tab
852 ip_vs_conn_new(const struct ip_vs_conn_param
*p
,
853 const union nf_inet_addr
*daddr
, __be16 dport
, unsigned int flags
,
854 struct ip_vs_dest
*dest
, __u32 fwmark
)
856 struct ip_vs_conn
*cp
;
857 struct netns_ipvs
*ipvs
= net_ipvs(p
->net
);
858 struct ip_vs_proto_data
*pd
= ip_vs_proto_data_get(p
->net
,
861 cp
= kmem_cache_zalloc(ip_vs_conn_cachep
, GFP_ATOMIC
);
863 IP_VS_ERR_RL("%s(): no memory\n", __func__
);
867 INIT_HLIST_NODE(&cp
->c_list
);
868 setup_timer(&cp
->timer
, ip_vs_conn_expire
, (unsigned long)cp
);
869 ip_vs_conn_net_set(cp
, p
->net
);
871 cp
->protocol
= p
->protocol
;
872 ip_vs_addr_copy(p
->af
, &cp
->caddr
, p
->caddr
);
873 cp
->cport
= p
->cport
;
874 ip_vs_addr_copy(p
->af
, &cp
->vaddr
, p
->vaddr
);
875 cp
->vport
= p
->vport
;
876 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
877 ip_vs_addr_copy(p
->protocol
== IPPROTO_IP
? AF_UNSPEC
: p
->af
,
882 if (flags
& IP_VS_CONN_F_TEMPLATE
&& p
->pe
) {
885 cp
->pe_data
= p
->pe_data
;
886 cp
->pe_data_len
= p
->pe_data_len
;
888 spin_lock_init(&cp
->lock
);
891 * Set the entry is referenced by the current thread before hashing
892 * it in the table, so that other thread run ip_vs_random_dropentry
893 * but cannot drop this entry.
895 atomic_set(&cp
->refcnt
, 1);
897 atomic_set(&cp
->n_control
, 0);
898 atomic_set(&cp
->in_pkts
, 0);
900 atomic_inc(&ipvs
->conn_count
);
901 if (flags
& IP_VS_CONN_F_NO_CPORT
)
902 atomic_inc(&ip_vs_conn_no_cport_cnt
);
904 /* Bind the connection with a destination server */
905 ip_vs_bind_dest(cp
, dest
);
907 /* Set its state and timeout */
910 cp
->sync_endtime
= jiffies
& ~3UL;
912 /* Bind its packet transmitter */
913 #ifdef CONFIG_IP_VS_IPV6
914 if (p
->af
== AF_INET6
)
915 ip_vs_bind_xmit_v6(cp
);
920 if (unlikely(pd
&& atomic_read(&pd
->appcnt
)))
921 ip_vs_bind_app(cp
, pd
->pp
);
924 * Allow conntrack to be preserved. By default, conntrack
925 * is created and destroyed for every packet.
926 * Sometimes keeping conntrack can be useful for
927 * IP_VS_CONN_F_ONE_PACKET too.
930 if (ip_vs_conntrack_enabled(ipvs
))
931 cp
->flags
|= IP_VS_CONN_F_NFCT
;
933 /* Hash it in the ip_vs_conn_tab finally */
940 * /proc/net/ip_vs_conn entries
942 #ifdef CONFIG_PROC_FS
943 struct ip_vs_iter_state
{
944 struct seq_net_private p
;
945 struct hlist_head
*l
;
948 static void *ip_vs_conn_array(struct seq_file
*seq
, loff_t pos
)
951 struct ip_vs_conn
*cp
;
952 struct ip_vs_iter_state
*iter
= seq
->private;
954 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
955 ct_read_lock_bh(idx
);
956 hlist_for_each_entry(cp
, &ip_vs_conn_tab
[idx
], c_list
) {
958 iter
->l
= &ip_vs_conn_tab
[idx
];
962 ct_read_unlock_bh(idx
);
968 static void *ip_vs_conn_seq_start(struct seq_file
*seq
, loff_t
*pos
)
970 struct ip_vs_iter_state
*iter
= seq
->private;
973 return *pos
? ip_vs_conn_array(seq
, *pos
- 1) :SEQ_START_TOKEN
;
976 static void *ip_vs_conn_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
978 struct ip_vs_conn
*cp
= v
;
979 struct ip_vs_iter_state
*iter
= seq
->private;
980 struct hlist_head
*l
= iter
->l
;
984 if (v
== SEQ_START_TOKEN
)
985 return ip_vs_conn_array(seq
, 0);
987 /* more on same hash chain? */
989 return hlist_entry(cp
->c_list
.next
, struct ip_vs_conn
, c_list
);
991 idx
= l
- ip_vs_conn_tab
;
992 ct_read_unlock_bh(idx
);
994 while (++idx
< ip_vs_conn_tab_size
) {
995 ct_read_lock_bh(idx
);
996 hlist_for_each_entry(cp
, &ip_vs_conn_tab
[idx
], c_list
) {
997 iter
->l
= &ip_vs_conn_tab
[idx
];
1000 ct_read_unlock_bh(idx
);
1006 static void ip_vs_conn_seq_stop(struct seq_file
*seq
, void *v
)
1008 struct ip_vs_iter_state
*iter
= seq
->private;
1009 struct hlist_head
*l
= iter
->l
;
1012 ct_read_unlock_bh(l
- ip_vs_conn_tab
);
1015 static int ip_vs_conn_seq_show(struct seq_file
*seq
, void *v
)
1018 if (v
== SEQ_START_TOKEN
)
1020 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires PEName PEData\n");
1022 const struct ip_vs_conn
*cp
= v
;
1023 struct net
*net
= seq_file_net(seq
);
1024 char pe_data
[IP_VS_PENAME_MAXLEN
+ IP_VS_PEDATA_MAXLEN
+ 3];
1027 if (!ip_vs_conn_net_eq(cp
, net
))
1031 len
= strlen(cp
->pe
->name
);
1032 memcpy(pe_data
+ 1, cp
->pe
->name
, len
);
1033 pe_data
[len
+ 1] = ' ';
1035 len
+= cp
->pe
->show_pe_data(cp
, pe_data
+ len
);
1037 pe_data
[len
] = '\0';
1039 #ifdef CONFIG_IP_VS_IPV6
1040 if (cp
->af
== AF_INET6
)
1041 seq_printf(seq
, "%-3s %pI6 %04X %pI6 %04X "
1042 "%pI6 %04X %-11s %7lu%s\n",
1043 ip_vs_proto_name(cp
->protocol
),
1044 &cp
->caddr
.in6
, ntohs(cp
->cport
),
1045 &cp
->vaddr
.in6
, ntohs(cp
->vport
),
1046 &cp
->daddr
.in6
, ntohs(cp
->dport
),
1047 ip_vs_state_name(cp
->protocol
, cp
->state
),
1048 (cp
->timer
.expires
-jiffies
)/HZ
, pe_data
);
1052 "%-3s %08X %04X %08X %04X"
1053 " %08X %04X %-11s %7lu%s\n",
1054 ip_vs_proto_name(cp
->protocol
),
1055 ntohl(cp
->caddr
.ip
), ntohs(cp
->cport
),
1056 ntohl(cp
->vaddr
.ip
), ntohs(cp
->vport
),
1057 ntohl(cp
->daddr
.ip
), ntohs(cp
->dport
),
1058 ip_vs_state_name(cp
->protocol
, cp
->state
),
1059 (cp
->timer
.expires
-jiffies
)/HZ
, pe_data
);
1064 static const struct seq_operations ip_vs_conn_seq_ops
= {
1065 .start
= ip_vs_conn_seq_start
,
1066 .next
= ip_vs_conn_seq_next
,
1067 .stop
= ip_vs_conn_seq_stop
,
1068 .show
= ip_vs_conn_seq_show
,
1071 static int ip_vs_conn_open(struct inode
*inode
, struct file
*file
)
1073 return seq_open_net(inode
, file
, &ip_vs_conn_seq_ops
,
1074 sizeof(struct ip_vs_iter_state
));
1077 static const struct file_operations ip_vs_conn_fops
= {
1078 .owner
= THIS_MODULE
,
1079 .open
= ip_vs_conn_open
,
1081 .llseek
= seq_lseek
,
1082 .release
= seq_release_net
,
1085 static const char *ip_vs_origin_name(unsigned int flags
)
1087 if (flags
& IP_VS_CONN_F_SYNC
)
1093 static int ip_vs_conn_sync_seq_show(struct seq_file
*seq
, void *v
)
1096 if (v
== SEQ_START_TOKEN
)
1098 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
1100 const struct ip_vs_conn
*cp
= v
;
1101 struct net
*net
= seq_file_net(seq
);
1103 if (!ip_vs_conn_net_eq(cp
, net
))
1106 #ifdef CONFIG_IP_VS_IPV6
1107 if (cp
->af
== AF_INET6
)
1108 seq_printf(seq
, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
1109 ip_vs_proto_name(cp
->protocol
),
1110 &cp
->caddr
.in6
, ntohs(cp
->cport
),
1111 &cp
->vaddr
.in6
, ntohs(cp
->vport
),
1112 &cp
->daddr
.in6
, ntohs(cp
->dport
),
1113 ip_vs_state_name(cp
->protocol
, cp
->state
),
1114 ip_vs_origin_name(cp
->flags
),
1115 (cp
->timer
.expires
-jiffies
)/HZ
);
1119 "%-3s %08X %04X %08X %04X "
1120 "%08X %04X %-11s %-6s %7lu\n",
1121 ip_vs_proto_name(cp
->protocol
),
1122 ntohl(cp
->caddr
.ip
), ntohs(cp
->cport
),
1123 ntohl(cp
->vaddr
.ip
), ntohs(cp
->vport
),
1124 ntohl(cp
->daddr
.ip
), ntohs(cp
->dport
),
1125 ip_vs_state_name(cp
->protocol
, cp
->state
),
1126 ip_vs_origin_name(cp
->flags
),
1127 (cp
->timer
.expires
-jiffies
)/HZ
);
1132 static const struct seq_operations ip_vs_conn_sync_seq_ops
= {
1133 .start
= ip_vs_conn_seq_start
,
1134 .next
= ip_vs_conn_seq_next
,
1135 .stop
= ip_vs_conn_seq_stop
,
1136 .show
= ip_vs_conn_sync_seq_show
,
1139 static int ip_vs_conn_sync_open(struct inode
*inode
, struct file
*file
)
1141 return seq_open_net(inode
, file
, &ip_vs_conn_sync_seq_ops
,
1142 sizeof(struct ip_vs_iter_state
));
1145 static const struct file_operations ip_vs_conn_sync_fops
= {
1146 .owner
= THIS_MODULE
,
1147 .open
= ip_vs_conn_sync_open
,
1149 .llseek
= seq_lseek
,
1150 .release
= seq_release_net
,
1157 * Randomly drop connection entries before running out of memory
1159 static inline int todrop_entry(struct ip_vs_conn
*cp
)
1162 * The drop rate array needs tuning for real environments.
1163 * Called from timer bh only => no locking
1165 static const char todrop_rate
[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
1166 static char todrop_counter
[9] = {0};
1169 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
1170 This will leave enough time for normal connection to get
1172 if (time_before(cp
->timeout
+ jiffies
, cp
->timer
.expires
+ 60*HZ
))
1175 /* Don't drop the entry if its number of incoming packets is not
1176 located in [0, 8] */
1177 i
= atomic_read(&cp
->in_pkts
);
1178 if (i
> 8 || i
< 0) return 0;
1180 if (!todrop_rate
[i
]) return 0;
1181 if (--todrop_counter
[i
] > 0) return 0;
1183 todrop_counter
[i
] = todrop_rate
[i
];
1187 /* Called from keventd and must protect itself from softirqs */
1188 void ip_vs_random_dropentry(struct net
*net
)
1191 struct ip_vs_conn
*cp
;
1194 * Randomly scan 1/32 of the whole table every second
1196 for (idx
= 0; idx
< (ip_vs_conn_tab_size
>>5); idx
++) {
1197 unsigned int hash
= net_random() & ip_vs_conn_tab_mask
;
1200 * Lock is actually needed in this loop.
1202 ct_write_lock_bh(hash
);
1204 hlist_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
1205 if (cp
->flags
& IP_VS_CONN_F_TEMPLATE
)
1206 /* connection template */
1208 if (!ip_vs_conn_net_eq(cp
, net
))
1210 if (cp
->protocol
== IPPROTO_TCP
) {
1212 case IP_VS_TCP_S_SYN_RECV
:
1213 case IP_VS_TCP_S_SYNACK
:
1216 case IP_VS_TCP_S_ESTABLISHED
:
1217 if (todrop_entry(cp
))
1225 if (!todrop_entry(cp
))
1229 IP_VS_DBG(4, "del connection\n");
1230 ip_vs_conn_expire_now(cp
);
1232 IP_VS_DBG(4, "del conn template\n");
1233 ip_vs_conn_expire_now(cp
->control
);
1236 ct_write_unlock_bh(hash
);
1242 * Flush all the connection entries in the ip_vs_conn_tab
1244 static void ip_vs_conn_flush(struct net
*net
)
1247 struct ip_vs_conn
*cp
;
1248 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1251 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
1253 * Lock is actually needed in this loop.
1255 ct_write_lock_bh(idx
);
1257 hlist_for_each_entry(cp
, &ip_vs_conn_tab
[idx
], c_list
) {
1258 if (!ip_vs_conn_net_eq(cp
, net
))
1260 IP_VS_DBG(4, "del connection\n");
1261 ip_vs_conn_expire_now(cp
);
1263 IP_VS_DBG(4, "del conn template\n");
1264 ip_vs_conn_expire_now(cp
->control
);
1267 ct_write_unlock_bh(idx
);
1270 /* the counter may be not NULL, because maybe some conn entries
1271 are run by slow timer handler or unhashed but still referred */
1272 if (atomic_read(&ipvs
->conn_count
) != 0) {
1278 * per netns init and exit
1280 int __net_init
ip_vs_conn_net_init(struct net
*net
)
1282 struct netns_ipvs
*ipvs
= net_ipvs(net
);
1284 atomic_set(&ipvs
->conn_count
, 0);
1286 proc_create("ip_vs_conn", 0, net
->proc_net
, &ip_vs_conn_fops
);
1287 proc_create("ip_vs_conn_sync", 0, net
->proc_net
, &ip_vs_conn_sync_fops
);
1291 void __net_exit
ip_vs_conn_net_cleanup(struct net
*net
)
1293 /* flush all the connection entries first */
1294 ip_vs_conn_flush(net
);
1295 remove_proc_entry("ip_vs_conn", net
->proc_net
);
1296 remove_proc_entry("ip_vs_conn_sync", net
->proc_net
);
1299 int __init
ip_vs_conn_init(void)
1303 /* Compute size and mask */
1304 ip_vs_conn_tab_size
= 1 << ip_vs_conn_tab_bits
;
1305 ip_vs_conn_tab_mask
= ip_vs_conn_tab_size
- 1;
1308 * Allocate the connection hash table and initialize its list heads
1310 ip_vs_conn_tab
= vmalloc(ip_vs_conn_tab_size
* sizeof(*ip_vs_conn_tab
));
1311 if (!ip_vs_conn_tab
)
1314 /* Allocate ip_vs_conn slab cache */
1315 ip_vs_conn_cachep
= kmem_cache_create("ip_vs_conn",
1316 sizeof(struct ip_vs_conn
), 0,
1317 SLAB_HWCACHE_ALIGN
, NULL
);
1318 if (!ip_vs_conn_cachep
) {
1319 vfree(ip_vs_conn_tab
);
1323 pr_info("Connection hash table configured "
1324 "(size=%d, memory=%ldKbytes)\n",
1325 ip_vs_conn_tab_size
,
1326 (long)(ip_vs_conn_tab_size
*sizeof(struct list_head
))/1024);
1327 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1328 sizeof(struct ip_vs_conn
));
1330 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++)
1331 INIT_HLIST_HEAD(&ip_vs_conn_tab
[idx
]);
1333 for (idx
= 0; idx
< CT_LOCKARRAY_SIZE
; idx
++) {
1334 rwlock_init(&__ip_vs_conntbl_lock_array
[idx
].l
);
1337 /* calculate the random value for connection hash */
1338 get_random_bytes(&ip_vs_conn_rnd
, sizeof(ip_vs_conn_rnd
));
1343 void ip_vs_conn_cleanup(void)
1345 /* Release the empty cache */
1346 kmem_cache_destroy(ip_vs_conn_cachep
);
1347 vfree(ip_vs_conn_tab
);