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/seq_file.h>
36 #include <linux/jhash.h>
37 #include <linux/random.h>
39 #include <net/net_namespace.h>
40 #include <net/ip_vs.h>
43 #ifndef CONFIG_IP_VS_TAB_BITS
44 #define CONFIG_IP_VS_TAB_BITS 12
48 * Connection hash size. Default is what was selected at compile time.
50 int ip_vs_conn_tab_bits
= CONFIG_IP_VS_TAB_BITS
;
51 module_param_named(conn_tab_bits
, ip_vs_conn_tab_bits
, int, 0444);
52 MODULE_PARM_DESC(conn_tab_bits
, "Set connections' hash size");
54 /* size and mask values */
55 int ip_vs_conn_tab_size
;
56 int ip_vs_conn_tab_mask
;
59 * Connection hash table: for input and output packets lookups of IPVS
61 static struct list_head
*ip_vs_conn_tab
;
63 /* SLAB cache for IPVS connections */
64 static struct kmem_cache
*ip_vs_conn_cachep __read_mostly
;
66 /* counter for current IPVS connections */
67 static atomic_t ip_vs_conn_count
= ATOMIC_INIT(0);
69 /* counter for no client port connections */
70 static atomic_t ip_vs_conn_no_cport_cnt
= ATOMIC_INIT(0);
72 /* random value for IPVS connection hash */
73 static unsigned int ip_vs_conn_rnd
;
76 * Fine locking granularity for big connection hash table
78 #define CT_LOCKARRAY_BITS 4
79 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
80 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
82 struct ip_vs_aligned_lock
85 } __attribute__((__aligned__(SMP_CACHE_BYTES
)));
87 /* lock array for conn table */
88 static struct ip_vs_aligned_lock
89 __ip_vs_conntbl_lock_array
[CT_LOCKARRAY_SIZE
] __cacheline_aligned
;
91 static inline void ct_read_lock(unsigned key
)
93 read_lock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
96 static inline void ct_read_unlock(unsigned key
)
98 read_unlock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
101 static inline void ct_write_lock(unsigned key
)
103 write_lock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
106 static inline void ct_write_unlock(unsigned key
)
108 write_unlock(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
111 static inline void ct_read_lock_bh(unsigned key
)
113 read_lock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
116 static inline void ct_read_unlock_bh(unsigned key
)
118 read_unlock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
121 static inline void ct_write_lock_bh(unsigned key
)
123 write_lock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
126 static inline void ct_write_unlock_bh(unsigned key
)
128 write_unlock_bh(&__ip_vs_conntbl_lock_array
[key
&CT_LOCKARRAY_MASK
].l
);
133 * Returns hash value for IPVS connection entry
135 static unsigned int ip_vs_conn_hashkey(int af
, unsigned proto
,
136 const union nf_inet_addr
*addr
,
139 #ifdef CONFIG_IP_VS_IPV6
141 return jhash_3words(jhash(addr
, 16, ip_vs_conn_rnd
),
142 (__force u32
)port
, proto
, ip_vs_conn_rnd
)
143 & ip_vs_conn_tab_mask
;
145 return jhash_3words((__force u32
)addr
->ip
, (__force u32
)port
, proto
,
147 & ip_vs_conn_tab_mask
;
152 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
153 * returns bool success.
155 static inline int ip_vs_conn_hash(struct ip_vs_conn
*cp
)
160 /* Hash by protocol, client address and port */
161 hash
= ip_vs_conn_hashkey(cp
->af
, cp
->protocol
, &cp
->caddr
, cp
->cport
);
165 if (!(cp
->flags
& IP_VS_CONN_F_HASHED
)) {
166 list_add(&cp
->c_list
, &ip_vs_conn_tab
[hash
]);
167 cp
->flags
|= IP_VS_CONN_F_HASHED
;
168 atomic_inc(&cp
->refcnt
);
171 pr_err("%s(): request for already hashed, called from %pF\n",
172 __func__
, __builtin_return_address(0));
176 ct_write_unlock(hash
);
183 * UNhashes ip_vs_conn from ip_vs_conn_tab.
184 * returns bool success.
186 static inline int ip_vs_conn_unhash(struct ip_vs_conn
*cp
)
191 /* unhash it and decrease its reference counter */
192 hash
= ip_vs_conn_hashkey(cp
->af
, cp
->protocol
, &cp
->caddr
, cp
->cport
);
196 if (cp
->flags
& IP_VS_CONN_F_HASHED
) {
197 list_del(&cp
->c_list
);
198 cp
->flags
&= ~IP_VS_CONN_F_HASHED
;
199 atomic_dec(&cp
->refcnt
);
204 ct_write_unlock(hash
);
211 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
212 * Called for pkts coming from OUTside-to-INside.
213 * s_addr, s_port: pkt source address (foreign host)
214 * d_addr, d_port: pkt dest address (load balancer)
216 static inline struct ip_vs_conn
*__ip_vs_conn_in_get
217 (int af
, int protocol
, const union nf_inet_addr
*s_addr
, __be16 s_port
,
218 const union nf_inet_addr
*d_addr
, __be16 d_port
)
221 struct ip_vs_conn
*cp
;
223 hash
= ip_vs_conn_hashkey(af
, protocol
, s_addr
, s_port
);
227 list_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
229 ip_vs_addr_equal(af
, s_addr
, &cp
->caddr
) &&
230 ip_vs_addr_equal(af
, d_addr
, &cp
->vaddr
) &&
231 s_port
== cp
->cport
&& d_port
== cp
->vport
&&
232 ((!s_port
) ^ (!(cp
->flags
& IP_VS_CONN_F_NO_CPORT
))) &&
233 protocol
== cp
->protocol
) {
235 atomic_inc(&cp
->refcnt
);
236 ct_read_unlock(hash
);
241 ct_read_unlock(hash
);
246 struct ip_vs_conn
*ip_vs_conn_in_get
247 (int af
, int protocol
, const union nf_inet_addr
*s_addr
, __be16 s_port
,
248 const union nf_inet_addr
*d_addr
, __be16 d_port
)
250 struct ip_vs_conn
*cp
;
252 cp
= __ip_vs_conn_in_get(af
, protocol
, s_addr
, s_port
, d_addr
, d_port
);
253 if (!cp
&& atomic_read(&ip_vs_conn_no_cport_cnt
))
254 cp
= __ip_vs_conn_in_get(af
, protocol
, s_addr
, 0, d_addr
,
257 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
258 ip_vs_proto_name(protocol
),
259 IP_VS_DBG_ADDR(af
, s_addr
), ntohs(s_port
),
260 IP_VS_DBG_ADDR(af
, d_addr
), ntohs(d_port
),
261 cp
? "hit" : "not hit");
266 /* Get reference to connection template */
267 struct ip_vs_conn
*ip_vs_ct_in_get
268 (int af
, int protocol
, const union nf_inet_addr
*s_addr
, __be16 s_port
,
269 const union nf_inet_addr
*d_addr
, __be16 d_port
)
272 struct ip_vs_conn
*cp
;
274 hash
= ip_vs_conn_hashkey(af
, protocol
, s_addr
, s_port
);
278 list_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
280 ip_vs_addr_equal(af
, s_addr
, &cp
->caddr
) &&
281 /* protocol should only be IPPROTO_IP if
282 * d_addr is a fwmark */
283 ip_vs_addr_equal(protocol
== IPPROTO_IP
? AF_UNSPEC
: af
,
284 d_addr
, &cp
->vaddr
) &&
285 s_port
== cp
->cport
&& d_port
== cp
->vport
&&
286 cp
->flags
& IP_VS_CONN_F_TEMPLATE
&&
287 protocol
== cp
->protocol
) {
289 atomic_inc(&cp
->refcnt
);
296 ct_read_unlock(hash
);
298 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
299 ip_vs_proto_name(protocol
),
300 IP_VS_DBG_ADDR(af
, s_addr
), ntohs(s_port
),
301 IP_VS_DBG_ADDR(af
, d_addr
), ntohs(d_port
),
302 cp
? "hit" : "not hit");
308 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
309 * Called for pkts coming from inside-to-OUTside.
310 * s_addr, s_port: pkt source address (inside host)
311 * d_addr, d_port: pkt dest address (foreign host)
313 struct ip_vs_conn
*ip_vs_conn_out_get
314 (int af
, int protocol
, const union nf_inet_addr
*s_addr
, __be16 s_port
,
315 const union nf_inet_addr
*d_addr
, __be16 d_port
)
318 struct ip_vs_conn
*cp
, *ret
=NULL
;
321 * Check for "full" addressed entries
323 hash
= ip_vs_conn_hashkey(af
, protocol
, d_addr
, d_port
);
327 list_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
329 ip_vs_addr_equal(af
, d_addr
, &cp
->caddr
) &&
330 ip_vs_addr_equal(af
, s_addr
, &cp
->daddr
) &&
331 d_port
== cp
->cport
&& s_port
== cp
->dport
&&
332 protocol
== cp
->protocol
) {
334 atomic_inc(&cp
->refcnt
);
340 ct_read_unlock(hash
);
342 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
343 ip_vs_proto_name(protocol
),
344 IP_VS_DBG_ADDR(af
, s_addr
), ntohs(s_port
),
345 IP_VS_DBG_ADDR(af
, d_addr
), ntohs(d_port
),
346 ret
? "hit" : "not hit");
353 * Put back the conn and restart its timer with its timeout
355 void ip_vs_conn_put(struct ip_vs_conn
*cp
)
357 /* reset it expire in its timeout */
358 mod_timer(&cp
->timer
, jiffies
+cp
->timeout
);
360 __ip_vs_conn_put(cp
);
365 * Fill a no_client_port connection with a client port number
367 void ip_vs_conn_fill_cport(struct ip_vs_conn
*cp
, __be16 cport
)
369 if (ip_vs_conn_unhash(cp
)) {
370 spin_lock(&cp
->lock
);
371 if (cp
->flags
& IP_VS_CONN_F_NO_CPORT
) {
372 atomic_dec(&ip_vs_conn_no_cport_cnt
);
373 cp
->flags
&= ~IP_VS_CONN_F_NO_CPORT
;
376 spin_unlock(&cp
->lock
);
378 /* hash on new dport */
385 * Bind a connection entry with the corresponding packet_xmit.
386 * Called by ip_vs_conn_new.
388 static inline void ip_vs_bind_xmit(struct ip_vs_conn
*cp
)
390 switch (IP_VS_FWD_METHOD(cp
)) {
391 case IP_VS_CONN_F_MASQ
:
392 cp
->packet_xmit
= ip_vs_nat_xmit
;
395 case IP_VS_CONN_F_TUNNEL
:
396 cp
->packet_xmit
= ip_vs_tunnel_xmit
;
399 case IP_VS_CONN_F_DROUTE
:
400 cp
->packet_xmit
= ip_vs_dr_xmit
;
403 case IP_VS_CONN_F_LOCALNODE
:
404 cp
->packet_xmit
= ip_vs_null_xmit
;
407 case IP_VS_CONN_F_BYPASS
:
408 cp
->packet_xmit
= ip_vs_bypass_xmit
;
413 #ifdef CONFIG_IP_VS_IPV6
414 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn
*cp
)
416 switch (IP_VS_FWD_METHOD(cp
)) {
417 case IP_VS_CONN_F_MASQ
:
418 cp
->packet_xmit
= ip_vs_nat_xmit_v6
;
421 case IP_VS_CONN_F_TUNNEL
:
422 cp
->packet_xmit
= ip_vs_tunnel_xmit_v6
;
425 case IP_VS_CONN_F_DROUTE
:
426 cp
->packet_xmit
= ip_vs_dr_xmit_v6
;
429 case IP_VS_CONN_F_LOCALNODE
:
430 cp
->packet_xmit
= ip_vs_null_xmit
;
433 case IP_VS_CONN_F_BYPASS
:
434 cp
->packet_xmit
= ip_vs_bypass_xmit_v6
;
441 static inline int ip_vs_dest_totalconns(struct ip_vs_dest
*dest
)
443 return atomic_read(&dest
->activeconns
)
444 + atomic_read(&dest
->inactconns
);
448 * Bind a connection entry with a virtual service destination
449 * Called just after a new connection entry is created.
452 ip_vs_bind_dest(struct ip_vs_conn
*cp
, struct ip_vs_dest
*dest
)
454 /* if dest is NULL, then return directly */
458 /* Increase the refcnt counter of the dest */
459 atomic_inc(&dest
->refcnt
);
461 /* Bind with the destination and its corresponding transmitter */
462 if ((cp
->flags
& IP_VS_CONN_F_SYNC
) &&
463 (!(cp
->flags
& IP_VS_CONN_F_TEMPLATE
)))
464 /* if the connection is not template and is created
465 * by sync, preserve the activity flag.
467 cp
->flags
|= atomic_read(&dest
->conn_flags
) &
468 (~IP_VS_CONN_F_INACTIVE
);
470 cp
->flags
|= atomic_read(&dest
->conn_flags
);
473 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
474 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
476 ip_vs_proto_name(cp
->protocol
),
477 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
478 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
479 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
480 ip_vs_fwd_tag(cp
), cp
->state
,
481 cp
->flags
, atomic_read(&cp
->refcnt
),
482 atomic_read(&dest
->refcnt
));
484 /* Update the connection counters */
485 if (!(cp
->flags
& IP_VS_CONN_F_TEMPLATE
)) {
486 /* It is a normal connection, so increase the inactive
487 connection counter because it is in TCP SYNRECV
488 state (inactive) or other protocol inacive state */
489 if ((cp
->flags
& IP_VS_CONN_F_SYNC
) &&
490 (!(cp
->flags
& IP_VS_CONN_F_INACTIVE
)))
491 atomic_inc(&dest
->activeconns
);
493 atomic_inc(&dest
->inactconns
);
495 /* It is a persistent connection/template, so increase
496 the peristent connection counter */
497 atomic_inc(&dest
->persistconns
);
500 if (dest
->u_threshold
!= 0 &&
501 ip_vs_dest_totalconns(dest
) >= dest
->u_threshold
)
502 dest
->flags
|= IP_VS_DEST_F_OVERLOAD
;
507 * Check if there is a destination for the connection, if so
508 * bind the connection to the destination.
510 struct ip_vs_dest
*ip_vs_try_bind_dest(struct ip_vs_conn
*cp
)
512 struct ip_vs_dest
*dest
;
514 if ((cp
) && (!cp
->dest
)) {
515 dest
= ip_vs_find_dest(cp
->af
, &cp
->daddr
, cp
->dport
,
516 &cp
->vaddr
, cp
->vport
,
518 ip_vs_bind_dest(cp
, dest
);
526 * Unbind a connection entry with its VS destination
527 * Called by the ip_vs_conn_expire function.
529 static inline void ip_vs_unbind_dest(struct ip_vs_conn
*cp
)
531 struct ip_vs_dest
*dest
= cp
->dest
;
536 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
537 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
539 ip_vs_proto_name(cp
->protocol
),
540 IP_VS_DBG_ADDR(cp
->af
, &cp
->caddr
), ntohs(cp
->cport
),
541 IP_VS_DBG_ADDR(cp
->af
, &cp
->vaddr
), ntohs(cp
->vport
),
542 IP_VS_DBG_ADDR(cp
->af
, &cp
->daddr
), ntohs(cp
->dport
),
543 ip_vs_fwd_tag(cp
), cp
->state
,
544 cp
->flags
, atomic_read(&cp
->refcnt
),
545 atomic_read(&dest
->refcnt
));
547 /* Update the connection counters */
548 if (!(cp
->flags
& IP_VS_CONN_F_TEMPLATE
)) {
549 /* It is a normal connection, so decrease the inactconns
550 or activeconns counter */
551 if (cp
->flags
& IP_VS_CONN_F_INACTIVE
) {
552 atomic_dec(&dest
->inactconns
);
554 atomic_dec(&dest
->activeconns
);
557 /* It is a persistent connection/template, so decrease
558 the peristent connection counter */
559 atomic_dec(&dest
->persistconns
);
562 if (dest
->l_threshold
!= 0) {
563 if (ip_vs_dest_totalconns(dest
) < dest
->l_threshold
)
564 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
565 } else if (dest
->u_threshold
!= 0) {
566 if (ip_vs_dest_totalconns(dest
) * 4 < dest
->u_threshold
* 3)
567 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
569 if (dest
->flags
& IP_VS_DEST_F_OVERLOAD
)
570 dest
->flags
&= ~IP_VS_DEST_F_OVERLOAD
;
574 * Simply decrease the refcnt of the dest, because the
575 * dest will be either in service's destination list
578 atomic_dec(&dest
->refcnt
);
583 * Checking if the destination of a connection template is available.
584 * If available, return 1, otherwise invalidate this connection
585 * template and return 0.
587 int ip_vs_check_template(struct ip_vs_conn
*ct
)
589 struct ip_vs_dest
*dest
= ct
->dest
;
592 * Checking the dest server status.
594 if ((dest
== NULL
) ||
595 !(dest
->flags
& IP_VS_DEST_F_AVAILABLE
) ||
596 (sysctl_ip_vs_expire_quiescent_template
&&
597 (atomic_read(&dest
->weight
) == 0))) {
598 IP_VS_DBG_BUF(9, "check_template: dest not available for "
599 "protocol %s s:%s:%d v:%s:%d "
601 ip_vs_proto_name(ct
->protocol
),
602 IP_VS_DBG_ADDR(ct
->af
, &ct
->caddr
),
604 IP_VS_DBG_ADDR(ct
->af
, &ct
->vaddr
),
606 IP_VS_DBG_ADDR(ct
->af
, &ct
->daddr
),
610 * Invalidate the connection template
612 if (ct
->vport
!= htons(0xffff)) {
613 if (ip_vs_conn_unhash(ct
)) {
614 ct
->dport
= htons(0xffff);
615 ct
->vport
= htons(0xffff);
622 * Simply decrease the refcnt of the template,
623 * don't restart its timer.
625 atomic_dec(&ct
->refcnt
);
631 static void ip_vs_conn_expire(unsigned long data
)
633 struct ip_vs_conn
*cp
= (struct ip_vs_conn
*)data
;
640 atomic_inc(&cp
->refcnt
);
643 * do I control anybody?
645 if (atomic_read(&cp
->n_control
))
649 * unhash it if it is hashed in the conn table
651 if (!ip_vs_conn_unhash(cp
))
655 * refcnt==1 implies I'm the only one referrer
657 if (likely(atomic_read(&cp
->refcnt
) == 1)) {
658 /* delete the timer if it is activated by other users */
659 if (timer_pending(&cp
->timer
))
660 del_timer(&cp
->timer
);
662 /* does anybody control me? */
664 ip_vs_control_del(cp
);
666 if (unlikely(cp
->app
!= NULL
))
667 ip_vs_unbind_app(cp
);
668 ip_vs_unbind_dest(cp
);
669 if (cp
->flags
& IP_VS_CONN_F_NO_CPORT
)
670 atomic_dec(&ip_vs_conn_no_cport_cnt
);
671 atomic_dec(&ip_vs_conn_count
);
673 kmem_cache_free(ip_vs_conn_cachep
, cp
);
677 /* hash it back to the table */
681 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
682 atomic_read(&cp
->refcnt
)-1,
683 atomic_read(&cp
->n_control
));
689 void ip_vs_conn_expire_now(struct ip_vs_conn
*cp
)
691 if (del_timer(&cp
->timer
))
692 mod_timer(&cp
->timer
, jiffies
);
697 * Create a new connection entry and hash it into the ip_vs_conn_tab
700 ip_vs_conn_new(int af
, int proto
, const union nf_inet_addr
*caddr
, __be16 cport
,
701 const union nf_inet_addr
*vaddr
, __be16 vport
,
702 const union nf_inet_addr
*daddr
, __be16 dport
, unsigned flags
,
703 struct ip_vs_dest
*dest
)
705 struct ip_vs_conn
*cp
;
706 struct ip_vs_protocol
*pp
= ip_vs_proto_get(proto
);
708 cp
= kmem_cache_zalloc(ip_vs_conn_cachep
, GFP_ATOMIC
);
710 IP_VS_ERR_RL("%s(): no memory\n", __func__
);
714 INIT_LIST_HEAD(&cp
->c_list
);
715 setup_timer(&cp
->timer
, ip_vs_conn_expire
, (unsigned long)cp
);
717 cp
->protocol
= proto
;
718 ip_vs_addr_copy(af
, &cp
->caddr
, caddr
);
720 ip_vs_addr_copy(af
, &cp
->vaddr
, vaddr
);
722 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
723 ip_vs_addr_copy(proto
== IPPROTO_IP
? AF_UNSPEC
: af
,
727 spin_lock_init(&cp
->lock
);
730 * Set the entry is referenced by the current thread before hashing
731 * it in the table, so that other thread run ip_vs_random_dropentry
732 * but cannot drop this entry.
734 atomic_set(&cp
->refcnt
, 1);
736 atomic_set(&cp
->n_control
, 0);
737 atomic_set(&cp
->in_pkts
, 0);
739 atomic_inc(&ip_vs_conn_count
);
740 if (flags
& IP_VS_CONN_F_NO_CPORT
)
741 atomic_inc(&ip_vs_conn_no_cport_cnt
);
743 /* Bind the connection with a destination server */
744 ip_vs_bind_dest(cp
, dest
);
746 /* Set its state and timeout */
750 /* Bind its packet transmitter */
751 #ifdef CONFIG_IP_VS_IPV6
753 ip_vs_bind_xmit_v6(cp
);
758 if (unlikely(pp
&& atomic_read(&pp
->appcnt
)))
759 ip_vs_bind_app(cp
, pp
);
761 /* Hash it in the ip_vs_conn_tab finally */
769 * /proc/net/ip_vs_conn entries
771 #ifdef CONFIG_PROC_FS
773 static void *ip_vs_conn_array(struct seq_file
*seq
, loff_t pos
)
776 struct ip_vs_conn
*cp
;
778 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
779 ct_read_lock_bh(idx
);
780 list_for_each_entry(cp
, &ip_vs_conn_tab
[idx
], c_list
) {
782 seq
->private = &ip_vs_conn_tab
[idx
];
786 ct_read_unlock_bh(idx
);
792 static void *ip_vs_conn_seq_start(struct seq_file
*seq
, loff_t
*pos
)
795 return *pos
? ip_vs_conn_array(seq
, *pos
- 1) :SEQ_START_TOKEN
;
798 static void *ip_vs_conn_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
800 struct ip_vs_conn
*cp
= v
;
801 struct list_head
*e
, *l
= seq
->private;
805 if (v
== SEQ_START_TOKEN
)
806 return ip_vs_conn_array(seq
, 0);
808 /* more on same hash chain? */
809 if ((e
= cp
->c_list
.next
) != l
)
810 return list_entry(e
, struct ip_vs_conn
, c_list
);
812 idx
= l
- ip_vs_conn_tab
;
813 ct_read_unlock_bh(idx
);
815 while (++idx
< ip_vs_conn_tab_size
) {
816 ct_read_lock_bh(idx
);
817 list_for_each_entry(cp
, &ip_vs_conn_tab
[idx
], c_list
) {
818 seq
->private = &ip_vs_conn_tab
[idx
];
821 ct_read_unlock_bh(idx
);
827 static void ip_vs_conn_seq_stop(struct seq_file
*seq
, void *v
)
829 struct list_head
*l
= seq
->private;
832 ct_read_unlock_bh(l
- ip_vs_conn_tab
);
835 static int ip_vs_conn_seq_show(struct seq_file
*seq
, void *v
)
838 if (v
== SEQ_START_TOKEN
)
840 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
842 const struct ip_vs_conn
*cp
= v
;
844 #ifdef CONFIG_IP_VS_IPV6
845 if (cp
->af
== AF_INET6
)
846 seq_printf(seq
, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n",
847 ip_vs_proto_name(cp
->protocol
),
848 &cp
->caddr
.in6
, ntohs(cp
->cport
),
849 &cp
->vaddr
.in6
, ntohs(cp
->vport
),
850 &cp
->daddr
.in6
, ntohs(cp
->dport
),
851 ip_vs_state_name(cp
->protocol
, cp
->state
),
852 (cp
->timer
.expires
-jiffies
)/HZ
);
856 "%-3s %08X %04X %08X %04X"
857 " %08X %04X %-11s %7lu\n",
858 ip_vs_proto_name(cp
->protocol
),
859 ntohl(cp
->caddr
.ip
), ntohs(cp
->cport
),
860 ntohl(cp
->vaddr
.ip
), ntohs(cp
->vport
),
861 ntohl(cp
->daddr
.ip
), ntohs(cp
->dport
),
862 ip_vs_state_name(cp
->protocol
, cp
->state
),
863 (cp
->timer
.expires
-jiffies
)/HZ
);
868 static const struct seq_operations ip_vs_conn_seq_ops
= {
869 .start
= ip_vs_conn_seq_start
,
870 .next
= ip_vs_conn_seq_next
,
871 .stop
= ip_vs_conn_seq_stop
,
872 .show
= ip_vs_conn_seq_show
,
875 static int ip_vs_conn_open(struct inode
*inode
, struct file
*file
)
877 return seq_open(file
, &ip_vs_conn_seq_ops
);
880 static const struct file_operations ip_vs_conn_fops
= {
881 .owner
= THIS_MODULE
,
882 .open
= ip_vs_conn_open
,
885 .release
= seq_release
,
888 static const char *ip_vs_origin_name(unsigned flags
)
890 if (flags
& IP_VS_CONN_F_SYNC
)
896 static int ip_vs_conn_sync_seq_show(struct seq_file
*seq
, void *v
)
899 if (v
== SEQ_START_TOKEN
)
901 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
903 const struct ip_vs_conn
*cp
= v
;
905 #ifdef CONFIG_IP_VS_IPV6
906 if (cp
->af
== AF_INET6
)
907 seq_printf(seq
, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
908 ip_vs_proto_name(cp
->protocol
),
909 &cp
->caddr
.in6
, ntohs(cp
->cport
),
910 &cp
->vaddr
.in6
, ntohs(cp
->vport
),
911 &cp
->daddr
.in6
, ntohs(cp
->dport
),
912 ip_vs_state_name(cp
->protocol
, cp
->state
),
913 ip_vs_origin_name(cp
->flags
),
914 (cp
->timer
.expires
-jiffies
)/HZ
);
918 "%-3s %08X %04X %08X %04X "
919 "%08X %04X %-11s %-6s %7lu\n",
920 ip_vs_proto_name(cp
->protocol
),
921 ntohl(cp
->caddr
.ip
), ntohs(cp
->cport
),
922 ntohl(cp
->vaddr
.ip
), ntohs(cp
->vport
),
923 ntohl(cp
->daddr
.ip
), ntohs(cp
->dport
),
924 ip_vs_state_name(cp
->protocol
, cp
->state
),
925 ip_vs_origin_name(cp
->flags
),
926 (cp
->timer
.expires
-jiffies
)/HZ
);
931 static const struct seq_operations ip_vs_conn_sync_seq_ops
= {
932 .start
= ip_vs_conn_seq_start
,
933 .next
= ip_vs_conn_seq_next
,
934 .stop
= ip_vs_conn_seq_stop
,
935 .show
= ip_vs_conn_sync_seq_show
,
938 static int ip_vs_conn_sync_open(struct inode
*inode
, struct file
*file
)
940 return seq_open(file
, &ip_vs_conn_sync_seq_ops
);
943 static const struct file_operations ip_vs_conn_sync_fops
= {
944 .owner
= THIS_MODULE
,
945 .open
= ip_vs_conn_sync_open
,
948 .release
= seq_release
,
955 * Randomly drop connection entries before running out of memory
957 static inline int todrop_entry(struct ip_vs_conn
*cp
)
960 * The drop rate array needs tuning for real environments.
961 * Called from timer bh only => no locking
963 static const char todrop_rate
[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
964 static char todrop_counter
[9] = {0};
967 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
968 This will leave enough time for normal connection to get
970 if (time_before(cp
->timeout
+ jiffies
, cp
->timer
.expires
+ 60*HZ
))
973 /* Don't drop the entry if its number of incoming packets is not
975 i
= atomic_read(&cp
->in_pkts
);
976 if (i
> 8 || i
< 0) return 0;
978 if (!todrop_rate
[i
]) return 0;
979 if (--todrop_counter
[i
] > 0) return 0;
981 todrop_counter
[i
] = todrop_rate
[i
];
985 /* Called from keventd and must protect itself from softirqs */
986 void ip_vs_random_dropentry(void)
989 struct ip_vs_conn
*cp
;
992 * Randomly scan 1/32 of the whole table every second
994 for (idx
= 0; idx
< (ip_vs_conn_tab_size
>>5); idx
++) {
995 unsigned hash
= net_random() & ip_vs_conn_tab_mask
;
998 * Lock is actually needed in this loop.
1000 ct_write_lock_bh(hash
);
1002 list_for_each_entry(cp
, &ip_vs_conn_tab
[hash
], c_list
) {
1003 if (cp
->flags
& IP_VS_CONN_F_TEMPLATE
)
1004 /* connection template */
1007 if (cp
->protocol
== IPPROTO_TCP
) {
1009 case IP_VS_TCP_S_SYN_RECV
:
1010 case IP_VS_TCP_S_SYNACK
:
1013 case IP_VS_TCP_S_ESTABLISHED
:
1014 if (todrop_entry(cp
))
1022 if (!todrop_entry(cp
))
1026 IP_VS_DBG(4, "del connection\n");
1027 ip_vs_conn_expire_now(cp
);
1029 IP_VS_DBG(4, "del conn template\n");
1030 ip_vs_conn_expire_now(cp
->control
);
1033 ct_write_unlock_bh(hash
);
1039 * Flush all the connection entries in the ip_vs_conn_tab
1041 static void ip_vs_conn_flush(void)
1044 struct ip_vs_conn
*cp
;
1047 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
1049 * Lock is actually needed in this loop.
1051 ct_write_lock_bh(idx
);
1053 list_for_each_entry(cp
, &ip_vs_conn_tab
[idx
], c_list
) {
1055 IP_VS_DBG(4, "del connection\n");
1056 ip_vs_conn_expire_now(cp
);
1058 IP_VS_DBG(4, "del conn template\n");
1059 ip_vs_conn_expire_now(cp
->control
);
1062 ct_write_unlock_bh(idx
);
1065 /* the counter may be not NULL, because maybe some conn entries
1066 are run by slow timer handler or unhashed but still referred */
1067 if (atomic_read(&ip_vs_conn_count
) != 0) {
1074 int __init
ip_vs_conn_init(void)
1078 /* Compute size and mask */
1079 ip_vs_conn_tab_size
= 1 << ip_vs_conn_tab_bits
;
1080 ip_vs_conn_tab_mask
= ip_vs_conn_tab_size
- 1;
1083 * Allocate the connection hash table and initialize its list heads
1085 ip_vs_conn_tab
= vmalloc(ip_vs_conn_tab_size
*
1086 sizeof(struct list_head
));
1087 if (!ip_vs_conn_tab
)
1090 /* Allocate ip_vs_conn slab cache */
1091 ip_vs_conn_cachep
= kmem_cache_create("ip_vs_conn",
1092 sizeof(struct ip_vs_conn
), 0,
1093 SLAB_HWCACHE_ALIGN
, NULL
);
1094 if (!ip_vs_conn_cachep
) {
1095 vfree(ip_vs_conn_tab
);
1099 pr_info("Connection hash table configured "
1100 "(size=%d, memory=%ldKbytes)\n",
1101 ip_vs_conn_tab_size
,
1102 (long)(ip_vs_conn_tab_size
*sizeof(struct list_head
))/1024);
1103 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1104 sizeof(struct ip_vs_conn
));
1106 for (idx
= 0; idx
< ip_vs_conn_tab_size
; idx
++) {
1107 INIT_LIST_HEAD(&ip_vs_conn_tab
[idx
]);
1110 for (idx
= 0; idx
< CT_LOCKARRAY_SIZE
; idx
++) {
1111 rwlock_init(&__ip_vs_conntbl_lock_array
[idx
].l
);
1114 proc_net_fops_create(&init_net
, "ip_vs_conn", 0, &ip_vs_conn_fops
);
1115 proc_net_fops_create(&init_net
, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops
);
1117 /* calculate the random value for connection hash */
1118 get_random_bytes(&ip_vs_conn_rnd
, sizeof(ip_vs_conn_rnd
));
1124 void ip_vs_conn_cleanup(void)
1126 /* flush all the connection entries first */
1129 /* Release the empty cache */
1130 kmem_cache_destroy(ip_vs_conn_cachep
);
1131 proc_net_remove(&init_net
, "ip_vs_conn");
1132 proc_net_remove(&init_net
, "ip_vs_conn_sync");
1133 vfree(ip_vs_conn_tab
);