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 #include <linux/interrupt.h>
27 #include <linux/net.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/vmalloc.h>
31 #include <linux/proc_fs.h> /* for proc_net_* */
32 #include <linux/seq_file.h>
33 #include <linux/jhash.h>
34 #include <linux/random.h>
36 #include <net/net_namespace.h>
37 #include <net/ip_vs.h>
41 * Connection hash table: for input and output packets lookups of IPVS
43 static struct list_head *ip_vs_conn_tab;
45 /* SLAB cache for IPVS connections */
46 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
48 /* counter for current IPVS connections */
49 static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
51 /* counter for no client port connections */
52 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
54 /* random value for IPVS connection hash */
55 static unsigned int ip_vs_conn_rnd;
58 * Fine locking granularity for big connection hash table
60 #define CT_LOCKARRAY_BITS 4
61 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
62 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
64 struct ip_vs_aligned_lock
67 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
69 /* lock array for conn table */
70 static struct ip_vs_aligned_lock
71 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
73 static inline void ct_read_lock(unsigned key)
75 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
78 static inline void ct_read_unlock(unsigned key)
80 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
83 static inline void ct_write_lock(unsigned key)
85 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
88 static inline void ct_write_unlock(unsigned key)
90 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
93 static inline void ct_read_lock_bh(unsigned key)
95 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
98 static inline void ct_read_unlock_bh(unsigned key)
100 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
103 static inline void ct_write_lock_bh(unsigned key)
105 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
108 static inline void ct_write_unlock_bh(unsigned key)
110 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
115 * Returns hash value for IPVS connection entry
117 static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
118 const union nf_inet_addr *addr,
121 #ifdef CONFIG_IP_VS_IPV6
123 return jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
124 (__force u32)port, proto, ip_vs_conn_rnd)
125 & IP_VS_CONN_TAB_MASK;
127 return jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
129 & IP_VS_CONN_TAB_MASK;
134 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
135 * returns bool success.
137 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
142 /* Hash by protocol, client address and port */
143 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
147 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
148 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
149 cp->flags |= IP_VS_CONN_F_HASHED;
150 atomic_inc(&cp->refcnt);
153 IP_VS_ERR("ip_vs_conn_hash(): request for already hashed, "
154 "called from %p\n", __builtin_return_address(0));
158 ct_write_unlock(hash);
165 * UNhashes ip_vs_conn from ip_vs_conn_tab.
166 * returns bool success.
168 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
173 /* unhash it and decrease its reference counter */
174 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
178 if (cp->flags & IP_VS_CONN_F_HASHED) {
179 list_del(&cp->c_list);
180 cp->flags &= ~IP_VS_CONN_F_HASHED;
181 atomic_dec(&cp->refcnt);
186 ct_write_unlock(hash);
193 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
194 * Called for pkts coming from OUTside-to-INside.
195 * s_addr, s_port: pkt source address (foreign host)
196 * d_addr, d_port: pkt dest address (load balancer)
198 static inline struct ip_vs_conn *__ip_vs_conn_in_get
199 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
200 const union nf_inet_addr *d_addr, __be16 d_port)
203 struct ip_vs_conn *cp;
205 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
209 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
211 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
212 ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
213 s_port == cp->cport && d_port == cp->vport &&
214 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
215 protocol == cp->protocol) {
217 atomic_inc(&cp->refcnt);
218 ct_read_unlock(hash);
223 ct_read_unlock(hash);
228 struct ip_vs_conn *ip_vs_conn_in_get
229 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
230 const union nf_inet_addr *d_addr, __be16 d_port)
232 struct ip_vs_conn *cp;
234 cp = __ip_vs_conn_in_get(af, protocol, s_addr, s_port, d_addr, d_port);
235 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
236 cp = __ip_vs_conn_in_get(af, protocol, s_addr, 0, d_addr,
239 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
240 ip_vs_proto_name(protocol),
241 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
242 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
243 cp ? "hit" : "not hit");
248 /* Get reference to connection template */
249 struct ip_vs_conn *ip_vs_ct_in_get
250 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
251 const union nf_inet_addr *d_addr, __be16 d_port)
254 struct ip_vs_conn *cp;
256 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
260 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
262 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
263 ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
264 s_port == cp->cport && d_port == cp->vport &&
265 cp->flags & IP_VS_CONN_F_TEMPLATE &&
266 protocol == cp->protocol) {
268 atomic_inc(&cp->refcnt);
275 ct_read_unlock(hash);
277 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
278 ip_vs_proto_name(protocol),
279 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
280 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
281 cp ? "hit" : "not hit");
287 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
288 * Called for pkts coming from inside-to-OUTside.
289 * s_addr, s_port: pkt source address (inside host)
290 * d_addr, d_port: pkt dest address (foreign host)
292 struct ip_vs_conn *ip_vs_conn_out_get
293 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
294 const union nf_inet_addr *d_addr, __be16 d_port)
297 struct ip_vs_conn *cp, *ret=NULL;
300 * Check for "full" addressed entries
302 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
306 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
308 ip_vs_addr_equal(af, d_addr, &cp->caddr) &&
309 ip_vs_addr_equal(af, s_addr, &cp->daddr) &&
310 d_port == cp->cport && s_port == cp->dport &&
311 protocol == cp->protocol) {
313 atomic_inc(&cp->refcnt);
319 ct_read_unlock(hash);
321 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
322 ip_vs_proto_name(protocol),
323 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
324 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
325 ret ? "hit" : "not hit");
332 * Put back the conn and restart its timer with its timeout
334 void ip_vs_conn_put(struct ip_vs_conn *cp)
336 /* reset it expire in its timeout */
337 mod_timer(&cp->timer, jiffies+cp->timeout);
339 __ip_vs_conn_put(cp);
344 * Fill a no_client_port connection with a client port number
346 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
348 if (ip_vs_conn_unhash(cp)) {
349 spin_lock(&cp->lock);
350 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
351 atomic_dec(&ip_vs_conn_no_cport_cnt);
352 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
355 spin_unlock(&cp->lock);
357 /* hash on new dport */
364 * Bind a connection entry with the corresponding packet_xmit.
365 * Called by ip_vs_conn_new.
367 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
369 switch (IP_VS_FWD_METHOD(cp)) {
370 case IP_VS_CONN_F_MASQ:
371 cp->packet_xmit = ip_vs_nat_xmit;
374 case IP_VS_CONN_F_TUNNEL:
375 cp->packet_xmit = ip_vs_tunnel_xmit;
378 case IP_VS_CONN_F_DROUTE:
379 cp->packet_xmit = ip_vs_dr_xmit;
382 case IP_VS_CONN_F_LOCALNODE:
383 cp->packet_xmit = ip_vs_null_xmit;
386 case IP_VS_CONN_F_BYPASS:
387 cp->packet_xmit = ip_vs_bypass_xmit;
392 #ifdef CONFIG_IP_VS_IPV6
393 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
395 switch (IP_VS_FWD_METHOD(cp)) {
396 case IP_VS_CONN_F_MASQ:
397 cp->packet_xmit = ip_vs_nat_xmit_v6;
400 case IP_VS_CONN_F_TUNNEL:
401 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
404 case IP_VS_CONN_F_DROUTE:
405 cp->packet_xmit = ip_vs_dr_xmit_v6;
408 case IP_VS_CONN_F_LOCALNODE:
409 cp->packet_xmit = ip_vs_null_xmit;
412 case IP_VS_CONN_F_BYPASS:
413 cp->packet_xmit = ip_vs_bypass_xmit_v6;
420 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
422 return atomic_read(&dest->activeconns)
423 + atomic_read(&dest->inactconns);
427 * Bind a connection entry with a virtual service destination
428 * Called just after a new connection entry is created.
431 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
433 /* if dest is NULL, then return directly */
437 /* Increase the refcnt counter of the dest */
438 atomic_inc(&dest->refcnt);
440 /* Bind with the destination and its corresponding transmitter */
441 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
442 (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
443 /* if the connection is not template and is created
444 * by sync, preserve the activity flag.
446 cp->flags |= atomic_read(&dest->conn_flags) &
447 (~IP_VS_CONN_F_INACTIVE);
449 cp->flags |= atomic_read(&dest->conn_flags);
452 IP_VS_DBG(7, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
453 "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
455 ip_vs_proto_name(cp->protocol),
456 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
457 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
458 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
459 ip_vs_fwd_tag(cp), cp->state,
460 cp->flags, atomic_read(&cp->refcnt),
461 atomic_read(&dest->refcnt));
463 /* Update the connection counters */
464 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
465 /* It is a normal connection, so increase the inactive
466 connection counter because it is in TCP SYNRECV
467 state (inactive) or other protocol inacive state */
468 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
469 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
470 atomic_inc(&dest->activeconns);
472 atomic_inc(&dest->inactconns);
474 /* It is a persistent connection/template, so increase
475 the peristent connection counter */
476 atomic_inc(&dest->persistconns);
479 if (dest->u_threshold != 0 &&
480 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
481 dest->flags |= IP_VS_DEST_F_OVERLOAD;
486 * Check if there is a destination for the connection, if so
487 * bind the connection to the destination.
489 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
491 struct ip_vs_dest *dest;
493 if ((cp) && (!cp->dest)) {
494 dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport,
495 &cp->vaddr, cp->vport,
497 ip_vs_bind_dest(cp, dest);
505 * Unbind a connection entry with its VS destination
506 * Called by the ip_vs_conn_expire function.
508 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
510 struct ip_vs_dest *dest = cp->dest;
515 IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
516 "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
518 ip_vs_proto_name(cp->protocol),
519 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
520 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
521 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
522 ip_vs_fwd_tag(cp), cp->state,
523 cp->flags, atomic_read(&cp->refcnt),
524 atomic_read(&dest->refcnt));
526 /* Update the connection counters */
527 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
528 /* It is a normal connection, so decrease the inactconns
529 or activeconns counter */
530 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
531 atomic_dec(&dest->inactconns);
533 atomic_dec(&dest->activeconns);
536 /* It is a persistent connection/template, so decrease
537 the peristent connection counter */
538 atomic_dec(&dest->persistconns);
541 if (dest->l_threshold != 0) {
542 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
543 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
544 } else if (dest->u_threshold != 0) {
545 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
546 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
548 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
549 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
553 * Simply decrease the refcnt of the dest, because the
554 * dest will be either in service's destination list
557 atomic_dec(&dest->refcnt);
562 * Checking if the destination of a connection template is available.
563 * If available, return 1, otherwise invalidate this connection
564 * template and return 0.
566 int ip_vs_check_template(struct ip_vs_conn *ct)
568 struct ip_vs_dest *dest = ct->dest;
571 * Checking the dest server status.
573 if ((dest == NULL) ||
574 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
575 (sysctl_ip_vs_expire_quiescent_template &&
576 (atomic_read(&dest->weight) == 0))) {
577 IP_VS_DBG(9, "check_template: dest not available for "
578 "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
579 "-> d:%u.%u.%u.%u:%d\n",
580 ip_vs_proto_name(ct->protocol),
581 NIPQUAD(ct->caddr.ip), ntohs(ct->cport),
582 NIPQUAD(ct->vaddr.ip), ntohs(ct->vport),
583 NIPQUAD(ct->daddr.ip), ntohs(ct->dport));
586 * Invalidate the connection template
588 if (ct->vport != htons(0xffff)) {
589 if (ip_vs_conn_unhash(ct)) {
590 ct->dport = htons(0xffff);
591 ct->vport = htons(0xffff);
598 * Simply decrease the refcnt of the template,
599 * don't restart its timer.
601 atomic_dec(&ct->refcnt);
607 static void ip_vs_conn_expire(unsigned long data)
609 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
616 atomic_inc(&cp->refcnt);
619 * do I control anybody?
621 if (atomic_read(&cp->n_control))
625 * unhash it if it is hashed in the conn table
627 if (!ip_vs_conn_unhash(cp))
631 * refcnt==1 implies I'm the only one referrer
633 if (likely(atomic_read(&cp->refcnt) == 1)) {
634 /* delete the timer if it is activated by other users */
635 if (timer_pending(&cp->timer))
636 del_timer(&cp->timer);
638 /* does anybody control me? */
640 ip_vs_control_del(cp);
642 if (unlikely(cp->app != NULL))
643 ip_vs_unbind_app(cp);
644 ip_vs_unbind_dest(cp);
645 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
646 atomic_dec(&ip_vs_conn_no_cport_cnt);
647 atomic_dec(&ip_vs_conn_count);
649 kmem_cache_free(ip_vs_conn_cachep, cp);
653 /* hash it back to the table */
657 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
658 atomic_read(&cp->refcnt)-1,
659 atomic_read(&cp->n_control));
665 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
667 if (del_timer(&cp->timer))
668 mod_timer(&cp->timer, jiffies);
673 * Create a new connection entry and hash it into the ip_vs_conn_tab
676 ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
677 const union nf_inet_addr *vaddr, __be16 vport,
678 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
679 struct ip_vs_dest *dest)
681 struct ip_vs_conn *cp;
682 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
684 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
686 IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
690 INIT_LIST_HEAD(&cp->c_list);
691 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
693 cp->protocol = proto;
694 ip_vs_addr_copy(af, &cp->caddr, caddr);
696 ip_vs_addr_copy(af, &cp->vaddr, vaddr);
698 ip_vs_addr_copy(af, &cp->daddr, daddr);
701 spin_lock_init(&cp->lock);
704 * Set the entry is referenced by the current thread before hashing
705 * it in the table, so that other thread run ip_vs_random_dropentry
706 * but cannot drop this entry.
708 atomic_set(&cp->refcnt, 1);
710 atomic_set(&cp->n_control, 0);
711 atomic_set(&cp->in_pkts, 0);
713 atomic_inc(&ip_vs_conn_count);
714 if (flags & IP_VS_CONN_F_NO_CPORT)
715 atomic_inc(&ip_vs_conn_no_cport_cnt);
717 /* Bind the connection with a destination server */
718 ip_vs_bind_dest(cp, dest);
720 /* Set its state and timeout */
724 /* Bind its packet transmitter */
725 #ifdef CONFIG_IP_VS_IPV6
727 ip_vs_bind_xmit_v6(cp);
732 if (unlikely(pp && atomic_read(&pp->appcnt)))
733 ip_vs_bind_app(cp, pp);
735 /* Hash it in the ip_vs_conn_tab finally */
743 * /proc/net/ip_vs_conn entries
745 #ifdef CONFIG_PROC_FS
747 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
750 struct ip_vs_conn *cp;
752 for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
753 ct_read_lock_bh(idx);
754 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
756 seq->private = &ip_vs_conn_tab[idx];
760 ct_read_unlock_bh(idx);
766 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
769 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
772 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
774 struct ip_vs_conn *cp = v;
775 struct list_head *e, *l = seq->private;
779 if (v == SEQ_START_TOKEN)
780 return ip_vs_conn_array(seq, 0);
782 /* more on same hash chain? */
783 if ((e = cp->c_list.next) != l)
784 return list_entry(e, struct ip_vs_conn, c_list);
786 idx = l - ip_vs_conn_tab;
787 ct_read_unlock_bh(idx);
789 while (++idx < IP_VS_CONN_TAB_SIZE) {
790 ct_read_lock_bh(idx);
791 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
792 seq->private = &ip_vs_conn_tab[idx];
795 ct_read_unlock_bh(idx);
801 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
803 struct list_head *l = seq->private;
806 ct_read_unlock_bh(l - ip_vs_conn_tab);
809 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
812 if (v == SEQ_START_TOKEN)
814 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
816 const struct ip_vs_conn *cp = v;
818 #ifdef CONFIG_IP_VS_IPV6
819 if (cp->af == AF_INET6)
821 "%-3s " NIP6_FMT " %04X " NIP6_FMT
822 " %04X " NIP6_FMT " %04X %-11s %7lu\n",
823 ip_vs_proto_name(cp->protocol),
824 NIP6(cp->caddr.in6), ntohs(cp->cport),
825 NIP6(cp->vaddr.in6), ntohs(cp->vport),
826 NIP6(cp->daddr.in6), ntohs(cp->dport),
827 ip_vs_state_name(cp->protocol, cp->state),
828 (cp->timer.expires-jiffies)/HZ);
832 "%-3s %08X %04X %08X %04X"
833 " %08X %04X %-11s %7lu\n",
834 ip_vs_proto_name(cp->protocol),
835 ntohl(cp->caddr.ip), ntohs(cp->cport),
836 ntohl(cp->vaddr.ip), ntohs(cp->vport),
837 ntohl(cp->daddr.ip), ntohs(cp->dport),
838 ip_vs_state_name(cp->protocol, cp->state),
839 (cp->timer.expires-jiffies)/HZ);
844 static const struct seq_operations ip_vs_conn_seq_ops = {
845 .start = ip_vs_conn_seq_start,
846 .next = ip_vs_conn_seq_next,
847 .stop = ip_vs_conn_seq_stop,
848 .show = ip_vs_conn_seq_show,
851 static int ip_vs_conn_open(struct inode *inode, struct file *file)
853 return seq_open(file, &ip_vs_conn_seq_ops);
856 static const struct file_operations ip_vs_conn_fops = {
857 .owner = THIS_MODULE,
858 .open = ip_vs_conn_open,
861 .release = seq_release,
864 static const char *ip_vs_origin_name(unsigned flags)
866 if (flags & IP_VS_CONN_F_SYNC)
872 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
875 if (v == SEQ_START_TOKEN)
877 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
879 const struct ip_vs_conn *cp = v;
881 #ifdef CONFIG_IP_VS_IPV6
882 if (cp->af == AF_INET6)
884 "%-3s " NIP6_FMT " %04X " NIP6_FMT
885 " %04X " NIP6_FMT " %04X %-11s %-6s %7lu\n",
886 ip_vs_proto_name(cp->protocol),
887 NIP6(cp->caddr.in6), ntohs(cp->cport),
888 NIP6(cp->vaddr.in6), ntohs(cp->vport),
889 NIP6(cp->daddr.in6), ntohs(cp->dport),
890 ip_vs_state_name(cp->protocol, cp->state),
891 ip_vs_origin_name(cp->flags),
892 (cp->timer.expires-jiffies)/HZ);
896 "%-3s %08X %04X %08X %04X "
897 "%08X %04X %-11s %-6s %7lu\n",
898 ip_vs_proto_name(cp->protocol),
899 ntohl(cp->caddr.ip), ntohs(cp->cport),
900 ntohl(cp->vaddr.ip), ntohs(cp->vport),
901 ntohl(cp->daddr.ip), ntohs(cp->dport),
902 ip_vs_state_name(cp->protocol, cp->state),
903 ip_vs_origin_name(cp->flags),
904 (cp->timer.expires-jiffies)/HZ);
909 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
910 .start = ip_vs_conn_seq_start,
911 .next = ip_vs_conn_seq_next,
912 .stop = ip_vs_conn_seq_stop,
913 .show = ip_vs_conn_sync_seq_show,
916 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
918 return seq_open(file, &ip_vs_conn_sync_seq_ops);
921 static const struct file_operations ip_vs_conn_sync_fops = {
922 .owner = THIS_MODULE,
923 .open = ip_vs_conn_sync_open,
926 .release = seq_release,
933 * Randomly drop connection entries before running out of memory
935 static inline int todrop_entry(struct ip_vs_conn *cp)
938 * The drop rate array needs tuning for real environments.
939 * Called from timer bh only => no locking
941 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
942 static char todrop_counter[9] = {0};
945 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
946 This will leave enough time for normal connection to get
948 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
951 /* Don't drop the entry if its number of incoming packets is not
953 i = atomic_read(&cp->in_pkts);
954 if (i > 8 || i < 0) return 0;
956 if (!todrop_rate[i]) return 0;
957 if (--todrop_counter[i] > 0) return 0;
959 todrop_counter[i] = todrop_rate[i];
963 /* Called from keventd and must protect itself from softirqs */
964 void ip_vs_random_dropentry(void)
967 struct ip_vs_conn *cp;
970 * Randomly scan 1/32 of the whole table every second
972 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
973 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
976 * Lock is actually needed in this loop.
978 ct_write_lock_bh(hash);
980 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
981 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
982 /* connection template */
985 if (cp->protocol == IPPROTO_TCP) {
987 case IP_VS_TCP_S_SYN_RECV:
988 case IP_VS_TCP_S_SYNACK:
991 case IP_VS_TCP_S_ESTABLISHED:
992 if (todrop_entry(cp))
1000 if (!todrop_entry(cp))
1004 IP_VS_DBG(4, "del connection\n");
1005 ip_vs_conn_expire_now(cp);
1007 IP_VS_DBG(4, "del conn template\n");
1008 ip_vs_conn_expire_now(cp->control);
1011 ct_write_unlock_bh(hash);
1017 * Flush all the connection entries in the ip_vs_conn_tab
1019 static void ip_vs_conn_flush(void)
1022 struct ip_vs_conn *cp;
1025 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
1027 * Lock is actually needed in this loop.
1029 ct_write_lock_bh(idx);
1031 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1033 IP_VS_DBG(4, "del connection\n");
1034 ip_vs_conn_expire_now(cp);
1036 IP_VS_DBG(4, "del conn template\n");
1037 ip_vs_conn_expire_now(cp->control);
1040 ct_write_unlock_bh(idx);
1043 /* the counter may be not NULL, because maybe some conn entries
1044 are run by slow timer handler or unhashed but still referred */
1045 if (atomic_read(&ip_vs_conn_count) != 0) {
1052 int __init ip_vs_conn_init(void)
1057 * Allocate the connection hash table and initialize its list heads
1059 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
1060 if (!ip_vs_conn_tab)
1063 /* Allocate ip_vs_conn slab cache */
1064 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1065 sizeof(struct ip_vs_conn), 0,
1066 SLAB_HWCACHE_ALIGN, NULL);
1067 if (!ip_vs_conn_cachep) {
1068 vfree(ip_vs_conn_tab);
1072 IP_VS_INFO("Connection hash table configured "
1073 "(size=%d, memory=%ldKbytes)\n",
1074 IP_VS_CONN_TAB_SIZE,
1075 (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
1076 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1077 sizeof(struct ip_vs_conn));
1079 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
1080 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1083 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1084 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1087 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
1088 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1090 /* calculate the random value for connection hash */
1091 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1097 void ip_vs_conn_cleanup(void)
1099 /* flush all the connection entries first */
1102 /* Release the empty cache */
1103 kmem_cache_destroy(ip_vs_conn_cachep);
1104 proc_net_remove(&init_net, "ip_vs_conn");
1105 proc_net_remove(&init_net, "ip_vs_conn_sync");
1106 vfree(ip_vs_conn_tab);