USB: Add USB-ID for Multiplex RC serial adapter to cp210x.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / ipvs / ip_vs_conn.c
blob95682e56902398486bec7fe20432db6c98cc9535
1 /*
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
6 * cluster of servers.
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.
21 * Changes:
25 #define KMSG_COMPONENT "IPVS"
26 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28 #include <linux/interrupt.h>
29 #include <linux/in.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>
44 * Connection hash table: for input and output packets lookups of IPVS
46 static struct list_head *ip_vs_conn_tab;
48 /* SLAB cache for IPVS connections */
49 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
51 /* counter for current IPVS connections */
52 static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
54 /* counter for no client port connections */
55 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
57 /* random value for IPVS connection hash */
58 static unsigned int ip_vs_conn_rnd;
61 * Fine locking granularity for big connection hash table
63 #define CT_LOCKARRAY_BITS 4
64 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
65 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
67 struct ip_vs_aligned_lock
69 rwlock_t l;
70 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
72 /* lock array for conn table */
73 static struct ip_vs_aligned_lock
74 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
76 static inline void ct_read_lock(unsigned key)
78 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
81 static inline void ct_read_unlock(unsigned key)
83 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
86 static inline void ct_write_lock(unsigned key)
88 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
91 static inline void ct_write_unlock(unsigned key)
93 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
96 static inline void ct_read_lock_bh(unsigned key)
98 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
101 static inline void ct_read_unlock_bh(unsigned key)
103 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
106 static inline void ct_write_lock_bh(unsigned key)
108 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
111 static inline void ct_write_unlock_bh(unsigned key)
113 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
118 * Returns hash value for IPVS connection entry
120 static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
121 const union nf_inet_addr *addr,
122 __be16 port)
124 #ifdef CONFIG_IP_VS_IPV6
125 if (af == AF_INET6)
126 return jhash_3words(jhash(addr, 16, ip_vs_conn_rnd),
127 (__force u32)port, proto, ip_vs_conn_rnd)
128 & IP_VS_CONN_TAB_MASK;
129 #endif
130 return jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
131 ip_vs_conn_rnd)
132 & IP_VS_CONN_TAB_MASK;
137 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
138 * returns bool success.
140 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
142 unsigned hash;
143 int ret;
145 /* Hash by protocol, client address and port */
146 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
148 ct_write_lock(hash);
149 spin_lock(&cp->lock);
151 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
152 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
153 cp->flags |= IP_VS_CONN_F_HASHED;
154 atomic_inc(&cp->refcnt);
155 ret = 1;
156 } else {
157 pr_err("%s(): request for already hashed, called from %pF\n",
158 __func__, __builtin_return_address(0));
159 ret = 0;
162 spin_unlock(&cp->lock);
163 ct_write_unlock(hash);
165 return ret;
170 * UNhashes ip_vs_conn from ip_vs_conn_tab.
171 * returns bool success.
173 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
175 unsigned hash;
176 int ret;
178 /* unhash it and decrease its reference counter */
179 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
181 ct_write_lock(hash);
182 spin_lock(&cp->lock);
184 if (cp->flags & IP_VS_CONN_F_HASHED) {
185 list_del(&cp->c_list);
186 cp->flags &= ~IP_VS_CONN_F_HASHED;
187 atomic_dec(&cp->refcnt);
188 ret = 1;
189 } else
190 ret = 0;
192 spin_unlock(&cp->lock);
193 ct_write_unlock(hash);
195 return ret;
200 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
201 * Called for pkts coming from OUTside-to-INside.
202 * s_addr, s_port: pkt source address (foreign host)
203 * d_addr, d_port: pkt dest address (load balancer)
205 static inline struct ip_vs_conn *__ip_vs_conn_in_get
206 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
207 const union nf_inet_addr *d_addr, __be16 d_port)
209 unsigned hash;
210 struct ip_vs_conn *cp;
212 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
214 ct_read_lock(hash);
216 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
217 if (cp->af == af &&
218 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
219 ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
220 s_port == cp->cport && d_port == cp->vport &&
221 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
222 protocol == cp->protocol) {
223 /* HIT */
224 atomic_inc(&cp->refcnt);
225 ct_read_unlock(hash);
226 return cp;
230 ct_read_unlock(hash);
232 return NULL;
235 struct ip_vs_conn *ip_vs_conn_in_get
236 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
237 const union nf_inet_addr *d_addr, __be16 d_port)
239 struct ip_vs_conn *cp;
241 cp = __ip_vs_conn_in_get(af, protocol, s_addr, s_port, d_addr, d_port);
242 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
243 cp = __ip_vs_conn_in_get(af, protocol, s_addr, 0, d_addr,
244 d_port);
246 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
247 ip_vs_proto_name(protocol),
248 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
249 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
250 cp ? "hit" : "not hit");
252 return cp;
255 /* Get reference to connection template */
256 struct ip_vs_conn *ip_vs_ct_in_get
257 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
258 const union nf_inet_addr *d_addr, __be16 d_port)
260 unsigned hash;
261 struct ip_vs_conn *cp;
263 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
265 ct_read_lock(hash);
267 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
268 if (cp->af == af &&
269 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
270 /* protocol should only be IPPROTO_IP if
271 * d_addr is a fwmark */
272 ip_vs_addr_equal(protocol == IPPROTO_IP ? AF_UNSPEC : af,
273 d_addr, &cp->vaddr) &&
274 s_port == cp->cport && d_port == cp->vport &&
275 cp->flags & IP_VS_CONN_F_TEMPLATE &&
276 protocol == cp->protocol) {
277 /* HIT */
278 atomic_inc(&cp->refcnt);
279 goto out;
282 cp = NULL;
284 out:
285 ct_read_unlock(hash);
287 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
288 ip_vs_proto_name(protocol),
289 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
290 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
291 cp ? "hit" : "not hit");
293 return cp;
297 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
298 * Called for pkts coming from inside-to-OUTside.
299 * s_addr, s_port: pkt source address (inside host)
300 * d_addr, d_port: pkt dest address (foreign host)
302 struct ip_vs_conn *ip_vs_conn_out_get
303 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
304 const union nf_inet_addr *d_addr, __be16 d_port)
306 unsigned hash;
307 struct ip_vs_conn *cp, *ret=NULL;
310 * Check for "full" addressed entries
312 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
314 ct_read_lock(hash);
316 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
317 if (cp->af == af &&
318 ip_vs_addr_equal(af, d_addr, &cp->caddr) &&
319 ip_vs_addr_equal(af, s_addr, &cp->daddr) &&
320 d_port == cp->cport && s_port == cp->dport &&
321 protocol == cp->protocol) {
322 /* HIT */
323 atomic_inc(&cp->refcnt);
324 ret = cp;
325 break;
329 ct_read_unlock(hash);
331 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
332 ip_vs_proto_name(protocol),
333 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
334 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
335 ret ? "hit" : "not hit");
337 return ret;
342 * Put back the conn and restart its timer with its timeout
344 void ip_vs_conn_put(struct ip_vs_conn *cp)
346 /* reset it expire in its timeout */
347 mod_timer(&cp->timer, jiffies+cp->timeout);
349 __ip_vs_conn_put(cp);
354 * Fill a no_client_port connection with a client port number
356 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
358 if (ip_vs_conn_unhash(cp)) {
359 spin_lock(&cp->lock);
360 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
361 atomic_dec(&ip_vs_conn_no_cport_cnt);
362 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
363 cp->cport = cport;
365 spin_unlock(&cp->lock);
367 /* hash on new dport */
368 ip_vs_conn_hash(cp);
374 * Bind a connection entry with the corresponding packet_xmit.
375 * Called by ip_vs_conn_new.
377 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
379 switch (IP_VS_FWD_METHOD(cp)) {
380 case IP_VS_CONN_F_MASQ:
381 cp->packet_xmit = ip_vs_nat_xmit;
382 break;
384 case IP_VS_CONN_F_TUNNEL:
385 cp->packet_xmit = ip_vs_tunnel_xmit;
386 break;
388 case IP_VS_CONN_F_DROUTE:
389 cp->packet_xmit = ip_vs_dr_xmit;
390 break;
392 case IP_VS_CONN_F_LOCALNODE:
393 cp->packet_xmit = ip_vs_null_xmit;
394 break;
396 case IP_VS_CONN_F_BYPASS:
397 cp->packet_xmit = ip_vs_bypass_xmit;
398 break;
402 #ifdef CONFIG_IP_VS_IPV6
403 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
405 switch (IP_VS_FWD_METHOD(cp)) {
406 case IP_VS_CONN_F_MASQ:
407 cp->packet_xmit = ip_vs_nat_xmit_v6;
408 break;
410 case IP_VS_CONN_F_TUNNEL:
411 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
412 break;
414 case IP_VS_CONN_F_DROUTE:
415 cp->packet_xmit = ip_vs_dr_xmit_v6;
416 break;
418 case IP_VS_CONN_F_LOCALNODE:
419 cp->packet_xmit = ip_vs_null_xmit;
420 break;
422 case IP_VS_CONN_F_BYPASS:
423 cp->packet_xmit = ip_vs_bypass_xmit_v6;
424 break;
427 #endif
430 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
432 return atomic_read(&dest->activeconns)
433 + atomic_read(&dest->inactconns);
437 * Bind a connection entry with a virtual service destination
438 * Called just after a new connection entry is created.
440 static inline void
441 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
443 /* if dest is NULL, then return directly */
444 if (!dest)
445 return;
447 /* Increase the refcnt counter of the dest */
448 atomic_inc(&dest->refcnt);
450 /* Bind with the destination and its corresponding transmitter */
451 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
452 (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
453 /* if the connection is not template and is created
454 * by sync, preserve the activity flag.
456 cp->flags |= atomic_read(&dest->conn_flags) &
457 (~IP_VS_CONN_F_INACTIVE);
458 else
459 cp->flags |= atomic_read(&dest->conn_flags);
460 cp->dest = dest;
462 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
463 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
464 "dest->refcnt:%d\n",
465 ip_vs_proto_name(cp->protocol),
466 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
467 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
468 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
469 ip_vs_fwd_tag(cp), cp->state,
470 cp->flags, atomic_read(&cp->refcnt),
471 atomic_read(&dest->refcnt));
473 /* Update the connection counters */
474 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
475 /* It is a normal connection, so increase the inactive
476 connection counter because it is in TCP SYNRECV
477 state (inactive) or other protocol inacive state */
478 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
479 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
480 atomic_inc(&dest->activeconns);
481 else
482 atomic_inc(&dest->inactconns);
483 } else {
484 /* It is a persistent connection/template, so increase
485 the peristent connection counter */
486 atomic_inc(&dest->persistconns);
489 if (dest->u_threshold != 0 &&
490 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
491 dest->flags |= IP_VS_DEST_F_OVERLOAD;
496 * Check if there is a destination for the connection, if so
497 * bind the connection to the destination.
499 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
501 struct ip_vs_dest *dest;
503 if ((cp) && (!cp->dest)) {
504 dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport,
505 &cp->vaddr, cp->vport,
506 cp->protocol);
507 ip_vs_bind_dest(cp, dest);
508 return dest;
509 } else
510 return NULL;
515 * Unbind a connection entry with its VS destination
516 * Called by the ip_vs_conn_expire function.
518 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
520 struct ip_vs_dest *dest = cp->dest;
522 if (!dest)
523 return;
525 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
526 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
527 "dest->refcnt:%d\n",
528 ip_vs_proto_name(cp->protocol),
529 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
530 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
531 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
532 ip_vs_fwd_tag(cp), cp->state,
533 cp->flags, atomic_read(&cp->refcnt),
534 atomic_read(&dest->refcnt));
536 /* Update the connection counters */
537 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
538 /* It is a normal connection, so decrease the inactconns
539 or activeconns counter */
540 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
541 atomic_dec(&dest->inactconns);
542 } else {
543 atomic_dec(&dest->activeconns);
545 } else {
546 /* It is a persistent connection/template, so decrease
547 the peristent connection counter */
548 atomic_dec(&dest->persistconns);
551 if (dest->l_threshold != 0) {
552 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
553 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
554 } else if (dest->u_threshold != 0) {
555 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
556 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
557 } else {
558 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
559 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
563 * Simply decrease the refcnt of the dest, because the
564 * dest will be either in service's destination list
565 * or in the trash.
567 atomic_dec(&dest->refcnt);
572 * Checking if the destination of a connection template is available.
573 * If available, return 1, otherwise invalidate this connection
574 * template and return 0.
576 int ip_vs_check_template(struct ip_vs_conn *ct)
578 struct ip_vs_dest *dest = ct->dest;
581 * Checking the dest server status.
583 if ((dest == NULL) ||
584 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
585 (sysctl_ip_vs_expire_quiescent_template &&
586 (atomic_read(&dest->weight) == 0))) {
587 IP_VS_DBG_BUF(9, "check_template: dest not available for "
588 "protocol %s s:%s:%d v:%s:%d "
589 "-> d:%s:%d\n",
590 ip_vs_proto_name(ct->protocol),
591 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
592 ntohs(ct->cport),
593 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
594 ntohs(ct->vport),
595 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
596 ntohs(ct->dport));
599 * Invalidate the connection template
601 if (ct->vport != htons(0xffff)) {
602 if (ip_vs_conn_unhash(ct)) {
603 ct->dport = htons(0xffff);
604 ct->vport = htons(0xffff);
605 ct->cport = 0;
606 ip_vs_conn_hash(ct);
611 * Simply decrease the refcnt of the template,
612 * don't restart its timer.
614 atomic_dec(&ct->refcnt);
615 return 0;
617 return 1;
620 static void ip_vs_conn_expire(unsigned long data)
622 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
624 cp->timeout = 60*HZ;
627 * hey, I'm using it
629 atomic_inc(&cp->refcnt);
632 * do I control anybody?
634 if (atomic_read(&cp->n_control))
635 goto expire_later;
638 * unhash it if it is hashed in the conn table
640 if (!ip_vs_conn_unhash(cp))
641 goto expire_later;
644 * refcnt==1 implies I'm the only one referrer
646 if (likely(atomic_read(&cp->refcnt) == 1)) {
647 /* delete the timer if it is activated by other users */
648 if (timer_pending(&cp->timer))
649 del_timer(&cp->timer);
651 /* does anybody control me? */
652 if (cp->control)
653 ip_vs_control_del(cp);
655 if (unlikely(cp->app != NULL))
656 ip_vs_unbind_app(cp);
657 ip_vs_unbind_dest(cp);
658 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
659 atomic_dec(&ip_vs_conn_no_cport_cnt);
660 atomic_dec(&ip_vs_conn_count);
662 kmem_cache_free(ip_vs_conn_cachep, cp);
663 return;
666 /* hash it back to the table */
667 ip_vs_conn_hash(cp);
669 expire_later:
670 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
671 atomic_read(&cp->refcnt)-1,
672 atomic_read(&cp->n_control));
674 ip_vs_conn_put(cp);
678 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
680 if (del_timer(&cp->timer))
681 mod_timer(&cp->timer, jiffies);
686 * Create a new connection entry and hash it into the ip_vs_conn_tab
688 struct ip_vs_conn *
689 ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
690 const union nf_inet_addr *vaddr, __be16 vport,
691 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
692 struct ip_vs_dest *dest)
694 struct ip_vs_conn *cp;
695 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
697 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
698 if (cp == NULL) {
699 IP_VS_ERR_RL("%s(): no memory\n", __func__);
700 return NULL;
703 INIT_LIST_HEAD(&cp->c_list);
704 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
705 cp->af = af;
706 cp->protocol = proto;
707 ip_vs_addr_copy(af, &cp->caddr, caddr);
708 cp->cport = cport;
709 ip_vs_addr_copy(af, &cp->vaddr, vaddr);
710 cp->vport = vport;
711 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
712 ip_vs_addr_copy(proto == IPPROTO_IP ? AF_UNSPEC : af,
713 &cp->daddr, daddr);
714 cp->dport = dport;
715 cp->flags = flags;
716 spin_lock_init(&cp->lock);
719 * Set the entry is referenced by the current thread before hashing
720 * it in the table, so that other thread run ip_vs_random_dropentry
721 * but cannot drop this entry.
723 atomic_set(&cp->refcnt, 1);
725 atomic_set(&cp->n_control, 0);
726 atomic_set(&cp->in_pkts, 0);
728 atomic_inc(&ip_vs_conn_count);
729 if (flags & IP_VS_CONN_F_NO_CPORT)
730 atomic_inc(&ip_vs_conn_no_cport_cnt);
732 /* Bind the connection with a destination server */
733 ip_vs_bind_dest(cp, dest);
735 /* Set its state and timeout */
736 cp->state = 0;
737 cp->timeout = 3*HZ;
739 /* Bind its packet transmitter */
740 #ifdef CONFIG_IP_VS_IPV6
741 if (af == AF_INET6)
742 ip_vs_bind_xmit_v6(cp);
743 else
744 #endif
745 ip_vs_bind_xmit(cp);
747 if (unlikely(pp && atomic_read(&pp->appcnt)))
748 ip_vs_bind_app(cp, pp);
750 /* Hash it in the ip_vs_conn_tab finally */
751 ip_vs_conn_hash(cp);
753 return cp;
758 * /proc/net/ip_vs_conn entries
760 #ifdef CONFIG_PROC_FS
762 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
764 int idx;
765 struct ip_vs_conn *cp;
767 for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
768 ct_read_lock_bh(idx);
769 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
770 if (pos-- == 0) {
771 seq->private = &ip_vs_conn_tab[idx];
772 return cp;
775 ct_read_unlock_bh(idx);
778 return NULL;
781 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
783 seq->private = NULL;
784 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
787 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
789 struct ip_vs_conn *cp = v;
790 struct list_head *e, *l = seq->private;
791 int idx;
793 ++*pos;
794 if (v == SEQ_START_TOKEN)
795 return ip_vs_conn_array(seq, 0);
797 /* more on same hash chain? */
798 if ((e = cp->c_list.next) != l)
799 return list_entry(e, struct ip_vs_conn, c_list);
801 idx = l - ip_vs_conn_tab;
802 ct_read_unlock_bh(idx);
804 while (++idx < IP_VS_CONN_TAB_SIZE) {
805 ct_read_lock_bh(idx);
806 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
807 seq->private = &ip_vs_conn_tab[idx];
808 return cp;
810 ct_read_unlock_bh(idx);
812 seq->private = NULL;
813 return NULL;
816 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
818 struct list_head *l = seq->private;
820 if (l)
821 ct_read_unlock_bh(l - ip_vs_conn_tab);
824 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
827 if (v == SEQ_START_TOKEN)
828 seq_puts(seq,
829 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
830 else {
831 const struct ip_vs_conn *cp = v;
833 #ifdef CONFIG_IP_VS_IPV6
834 if (cp->af == AF_INET6)
835 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n",
836 ip_vs_proto_name(cp->protocol),
837 &cp->caddr.in6, ntohs(cp->cport),
838 &cp->vaddr.in6, ntohs(cp->vport),
839 &cp->daddr.in6, ntohs(cp->dport),
840 ip_vs_state_name(cp->protocol, cp->state),
841 (cp->timer.expires-jiffies)/HZ);
842 else
843 #endif
844 seq_printf(seq,
845 "%-3s %08X %04X %08X %04X"
846 " %08X %04X %-11s %7lu\n",
847 ip_vs_proto_name(cp->protocol),
848 ntohl(cp->caddr.ip), ntohs(cp->cport),
849 ntohl(cp->vaddr.ip), ntohs(cp->vport),
850 ntohl(cp->daddr.ip), ntohs(cp->dport),
851 ip_vs_state_name(cp->protocol, cp->state),
852 (cp->timer.expires-jiffies)/HZ);
854 return 0;
857 static const struct seq_operations ip_vs_conn_seq_ops = {
858 .start = ip_vs_conn_seq_start,
859 .next = ip_vs_conn_seq_next,
860 .stop = ip_vs_conn_seq_stop,
861 .show = ip_vs_conn_seq_show,
864 static int ip_vs_conn_open(struct inode *inode, struct file *file)
866 return seq_open(file, &ip_vs_conn_seq_ops);
869 static const struct file_operations ip_vs_conn_fops = {
870 .owner = THIS_MODULE,
871 .open = ip_vs_conn_open,
872 .read = seq_read,
873 .llseek = seq_lseek,
874 .release = seq_release,
877 static const char *ip_vs_origin_name(unsigned flags)
879 if (flags & IP_VS_CONN_F_SYNC)
880 return "SYNC";
881 else
882 return "LOCAL";
885 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
888 if (v == SEQ_START_TOKEN)
889 seq_puts(seq,
890 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
891 else {
892 const struct ip_vs_conn *cp = v;
894 #ifdef CONFIG_IP_VS_IPV6
895 if (cp->af == AF_INET6)
896 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
897 ip_vs_proto_name(cp->protocol),
898 &cp->caddr.in6, ntohs(cp->cport),
899 &cp->vaddr.in6, ntohs(cp->vport),
900 &cp->daddr.in6, ntohs(cp->dport),
901 ip_vs_state_name(cp->protocol, cp->state),
902 ip_vs_origin_name(cp->flags),
903 (cp->timer.expires-jiffies)/HZ);
904 else
905 #endif
906 seq_printf(seq,
907 "%-3s %08X %04X %08X %04X "
908 "%08X %04X %-11s %-6s %7lu\n",
909 ip_vs_proto_name(cp->protocol),
910 ntohl(cp->caddr.ip), ntohs(cp->cport),
911 ntohl(cp->vaddr.ip), ntohs(cp->vport),
912 ntohl(cp->daddr.ip), ntohs(cp->dport),
913 ip_vs_state_name(cp->protocol, cp->state),
914 ip_vs_origin_name(cp->flags),
915 (cp->timer.expires-jiffies)/HZ);
917 return 0;
920 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
921 .start = ip_vs_conn_seq_start,
922 .next = ip_vs_conn_seq_next,
923 .stop = ip_vs_conn_seq_stop,
924 .show = ip_vs_conn_sync_seq_show,
927 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
929 return seq_open(file, &ip_vs_conn_sync_seq_ops);
932 static const struct file_operations ip_vs_conn_sync_fops = {
933 .owner = THIS_MODULE,
934 .open = ip_vs_conn_sync_open,
935 .read = seq_read,
936 .llseek = seq_lseek,
937 .release = seq_release,
940 #endif
944 * Randomly drop connection entries before running out of memory
946 static inline int todrop_entry(struct ip_vs_conn *cp)
949 * The drop rate array needs tuning for real environments.
950 * Called from timer bh only => no locking
952 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
953 static char todrop_counter[9] = {0};
954 int i;
956 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
957 This will leave enough time for normal connection to get
958 through. */
959 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
960 return 0;
962 /* Don't drop the entry if its number of incoming packets is not
963 located in [0, 8] */
964 i = atomic_read(&cp->in_pkts);
965 if (i > 8 || i < 0) return 0;
967 if (!todrop_rate[i]) return 0;
968 if (--todrop_counter[i] > 0) return 0;
970 todrop_counter[i] = todrop_rate[i];
971 return 1;
974 /* Called from keventd and must protect itself from softirqs */
975 void ip_vs_random_dropentry(void)
977 int idx;
978 struct ip_vs_conn *cp;
981 * Randomly scan 1/32 of the whole table every second
983 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
984 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
987 * Lock is actually needed in this loop.
989 ct_write_lock_bh(hash);
991 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
992 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
993 /* connection template */
994 continue;
996 if (cp->protocol == IPPROTO_TCP) {
997 switch(cp->state) {
998 case IP_VS_TCP_S_SYN_RECV:
999 case IP_VS_TCP_S_SYNACK:
1000 break;
1002 case IP_VS_TCP_S_ESTABLISHED:
1003 if (todrop_entry(cp))
1004 break;
1005 continue;
1007 default:
1008 continue;
1010 } else {
1011 if (!todrop_entry(cp))
1012 continue;
1015 IP_VS_DBG(4, "del connection\n");
1016 ip_vs_conn_expire_now(cp);
1017 if (cp->control) {
1018 IP_VS_DBG(4, "del conn template\n");
1019 ip_vs_conn_expire_now(cp->control);
1022 ct_write_unlock_bh(hash);
1028 * Flush all the connection entries in the ip_vs_conn_tab
1030 static void ip_vs_conn_flush(void)
1032 int idx;
1033 struct ip_vs_conn *cp;
1035 flush_again:
1036 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
1038 * Lock is actually needed in this loop.
1040 ct_write_lock_bh(idx);
1042 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1044 IP_VS_DBG(4, "del connection\n");
1045 ip_vs_conn_expire_now(cp);
1046 if (cp->control) {
1047 IP_VS_DBG(4, "del conn template\n");
1048 ip_vs_conn_expire_now(cp->control);
1051 ct_write_unlock_bh(idx);
1054 /* the counter may be not NULL, because maybe some conn entries
1055 are run by slow timer handler or unhashed but still referred */
1056 if (atomic_read(&ip_vs_conn_count) != 0) {
1057 schedule();
1058 goto flush_again;
1063 int __init ip_vs_conn_init(void)
1065 int idx;
1068 * Allocate the connection hash table and initialize its list heads
1070 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
1071 if (!ip_vs_conn_tab)
1072 return -ENOMEM;
1074 /* Allocate ip_vs_conn slab cache */
1075 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1076 sizeof(struct ip_vs_conn), 0,
1077 SLAB_HWCACHE_ALIGN, NULL);
1078 if (!ip_vs_conn_cachep) {
1079 vfree(ip_vs_conn_tab);
1080 return -ENOMEM;
1083 pr_info("Connection hash table configured "
1084 "(size=%d, memory=%ldKbytes)\n",
1085 IP_VS_CONN_TAB_SIZE,
1086 (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
1087 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1088 sizeof(struct ip_vs_conn));
1090 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
1091 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1094 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1095 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1098 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
1099 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1101 /* calculate the random value for connection hash */
1102 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1104 return 0;
1108 void ip_vs_conn_cleanup(void)
1110 /* flush all the connection entries first */
1111 ip_vs_conn_flush();
1113 /* Release the empty cache */
1114 kmem_cache_destroy(ip_vs_conn_cachep);
1115 proc_net_remove(&init_net, "ip_vs_conn");
1116 proc_net_remove(&init_net, "ip_vs_conn_sync");
1117 vfree(ip_vs_conn_tab);