tree-wide: fix typos "ammount" -> "amount"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / ipvs / ip_vs_conn.c
blob27c30cf933daa9092def2dfc342f8f05ef036d5f
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);
150 if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
151 list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
152 cp->flags |= IP_VS_CONN_F_HASHED;
153 atomic_inc(&cp->refcnt);
154 ret = 1;
155 } else {
156 pr_err("%s(): request for already hashed, called from %pF\n",
157 __func__, __builtin_return_address(0));
158 ret = 0;
161 ct_write_unlock(hash);
163 return ret;
168 * UNhashes ip_vs_conn from ip_vs_conn_tab.
169 * returns bool success.
171 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
173 unsigned hash;
174 int ret;
176 /* unhash it and decrease its reference counter */
177 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
179 ct_write_lock(hash);
181 if (cp->flags & IP_VS_CONN_F_HASHED) {
182 list_del(&cp->c_list);
183 cp->flags &= ~IP_VS_CONN_F_HASHED;
184 atomic_dec(&cp->refcnt);
185 ret = 1;
186 } else
187 ret = 0;
189 ct_write_unlock(hash);
191 return ret;
196 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
197 * Called for pkts coming from OUTside-to-INside.
198 * s_addr, s_port: pkt source address (foreign host)
199 * d_addr, d_port: pkt dest address (load balancer)
201 static inline struct ip_vs_conn *__ip_vs_conn_in_get
202 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
203 const union nf_inet_addr *d_addr, __be16 d_port)
205 unsigned hash;
206 struct ip_vs_conn *cp;
208 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
210 ct_read_lock(hash);
212 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
213 if (cp->af == af &&
214 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
215 ip_vs_addr_equal(af, d_addr, &cp->vaddr) &&
216 s_port == cp->cport && d_port == cp->vport &&
217 ((!s_port) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
218 protocol == cp->protocol) {
219 /* HIT */
220 atomic_inc(&cp->refcnt);
221 ct_read_unlock(hash);
222 return cp;
226 ct_read_unlock(hash);
228 return NULL;
231 struct ip_vs_conn *ip_vs_conn_in_get
232 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
233 const union nf_inet_addr *d_addr, __be16 d_port)
235 struct ip_vs_conn *cp;
237 cp = __ip_vs_conn_in_get(af, protocol, s_addr, s_port, d_addr, d_port);
238 if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt))
239 cp = __ip_vs_conn_in_get(af, protocol, s_addr, 0, d_addr,
240 d_port);
242 IP_VS_DBG_BUF(9, "lookup/in %s %s:%d->%s:%d %s\n",
243 ip_vs_proto_name(protocol),
244 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
245 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
246 cp ? "hit" : "not hit");
248 return cp;
251 /* Get reference to connection template */
252 struct ip_vs_conn *ip_vs_ct_in_get
253 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
254 const union nf_inet_addr *d_addr, __be16 d_port)
256 unsigned hash;
257 struct ip_vs_conn *cp;
259 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
261 ct_read_lock(hash);
263 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
264 if (cp->af == af &&
265 ip_vs_addr_equal(af, s_addr, &cp->caddr) &&
266 /* protocol should only be IPPROTO_IP if
267 * d_addr is a fwmark */
268 ip_vs_addr_equal(protocol == IPPROTO_IP ? AF_UNSPEC : af,
269 d_addr, &cp->vaddr) &&
270 s_port == cp->cport && d_port == cp->vport &&
271 cp->flags & IP_VS_CONN_F_TEMPLATE &&
272 protocol == cp->protocol) {
273 /* HIT */
274 atomic_inc(&cp->refcnt);
275 goto out;
278 cp = NULL;
280 out:
281 ct_read_unlock(hash);
283 IP_VS_DBG_BUF(9, "template lookup/in %s %s:%d->%s:%d %s\n",
284 ip_vs_proto_name(protocol),
285 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
286 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
287 cp ? "hit" : "not hit");
289 return cp;
293 * Gets ip_vs_conn associated with supplied parameters in the ip_vs_conn_tab.
294 * Called for pkts coming from inside-to-OUTside.
295 * s_addr, s_port: pkt source address (inside host)
296 * d_addr, d_port: pkt dest address (foreign host)
298 struct ip_vs_conn *ip_vs_conn_out_get
299 (int af, int protocol, const union nf_inet_addr *s_addr, __be16 s_port,
300 const union nf_inet_addr *d_addr, __be16 d_port)
302 unsigned hash;
303 struct ip_vs_conn *cp, *ret=NULL;
306 * Check for "full" addressed entries
308 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
310 ct_read_lock(hash);
312 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
313 if (cp->af == af &&
314 ip_vs_addr_equal(af, d_addr, &cp->caddr) &&
315 ip_vs_addr_equal(af, s_addr, &cp->daddr) &&
316 d_port == cp->cport && s_port == cp->dport &&
317 protocol == cp->protocol) {
318 /* HIT */
319 atomic_inc(&cp->refcnt);
320 ret = cp;
321 break;
325 ct_read_unlock(hash);
327 IP_VS_DBG_BUF(9, "lookup/out %s %s:%d->%s:%d %s\n",
328 ip_vs_proto_name(protocol),
329 IP_VS_DBG_ADDR(af, s_addr), ntohs(s_port),
330 IP_VS_DBG_ADDR(af, d_addr), ntohs(d_port),
331 ret ? "hit" : "not hit");
333 return ret;
338 * Put back the conn and restart its timer with its timeout
340 void ip_vs_conn_put(struct ip_vs_conn *cp)
342 /* reset it expire in its timeout */
343 mod_timer(&cp->timer, jiffies+cp->timeout);
345 __ip_vs_conn_put(cp);
350 * Fill a no_client_port connection with a client port number
352 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
354 if (ip_vs_conn_unhash(cp)) {
355 spin_lock(&cp->lock);
356 if (cp->flags & IP_VS_CONN_F_NO_CPORT) {
357 atomic_dec(&ip_vs_conn_no_cport_cnt);
358 cp->flags &= ~IP_VS_CONN_F_NO_CPORT;
359 cp->cport = cport;
361 spin_unlock(&cp->lock);
363 /* hash on new dport */
364 ip_vs_conn_hash(cp);
370 * Bind a connection entry with the corresponding packet_xmit.
371 * Called by ip_vs_conn_new.
373 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
375 switch (IP_VS_FWD_METHOD(cp)) {
376 case IP_VS_CONN_F_MASQ:
377 cp->packet_xmit = ip_vs_nat_xmit;
378 break;
380 case IP_VS_CONN_F_TUNNEL:
381 cp->packet_xmit = ip_vs_tunnel_xmit;
382 break;
384 case IP_VS_CONN_F_DROUTE:
385 cp->packet_xmit = ip_vs_dr_xmit;
386 break;
388 case IP_VS_CONN_F_LOCALNODE:
389 cp->packet_xmit = ip_vs_null_xmit;
390 break;
392 case IP_VS_CONN_F_BYPASS:
393 cp->packet_xmit = ip_vs_bypass_xmit;
394 break;
398 #ifdef CONFIG_IP_VS_IPV6
399 static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
401 switch (IP_VS_FWD_METHOD(cp)) {
402 case IP_VS_CONN_F_MASQ:
403 cp->packet_xmit = ip_vs_nat_xmit_v6;
404 break;
406 case IP_VS_CONN_F_TUNNEL:
407 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
408 break;
410 case IP_VS_CONN_F_DROUTE:
411 cp->packet_xmit = ip_vs_dr_xmit_v6;
412 break;
414 case IP_VS_CONN_F_LOCALNODE:
415 cp->packet_xmit = ip_vs_null_xmit;
416 break;
418 case IP_VS_CONN_F_BYPASS:
419 cp->packet_xmit = ip_vs_bypass_xmit_v6;
420 break;
423 #endif
426 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
428 return atomic_read(&dest->activeconns)
429 + atomic_read(&dest->inactconns);
433 * Bind a connection entry with a virtual service destination
434 * Called just after a new connection entry is created.
436 static inline void
437 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
439 /* if dest is NULL, then return directly */
440 if (!dest)
441 return;
443 /* Increase the refcnt counter of the dest */
444 atomic_inc(&dest->refcnt);
446 /* Bind with the destination and its corresponding transmitter */
447 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
448 (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
449 /* if the connection is not template and is created
450 * by sync, preserve the activity flag.
452 cp->flags |= atomic_read(&dest->conn_flags) &
453 (~IP_VS_CONN_F_INACTIVE);
454 else
455 cp->flags |= atomic_read(&dest->conn_flags);
456 cp->dest = dest;
458 IP_VS_DBG_BUF(7, "Bind-dest %s c:%s:%d v:%s:%d "
459 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
460 "dest->refcnt:%d\n",
461 ip_vs_proto_name(cp->protocol),
462 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
463 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
464 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
465 ip_vs_fwd_tag(cp), cp->state,
466 cp->flags, atomic_read(&cp->refcnt),
467 atomic_read(&dest->refcnt));
469 /* Update the connection counters */
470 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
471 /* It is a normal connection, so increase the inactive
472 connection counter because it is in TCP SYNRECV
473 state (inactive) or other protocol inacive state */
474 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
475 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
476 atomic_inc(&dest->activeconns);
477 else
478 atomic_inc(&dest->inactconns);
479 } else {
480 /* It is a persistent connection/template, so increase
481 the peristent connection counter */
482 atomic_inc(&dest->persistconns);
485 if (dest->u_threshold != 0 &&
486 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
487 dest->flags |= IP_VS_DEST_F_OVERLOAD;
492 * Check if there is a destination for the connection, if so
493 * bind the connection to the destination.
495 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
497 struct ip_vs_dest *dest;
499 if ((cp) && (!cp->dest)) {
500 dest = ip_vs_find_dest(cp->af, &cp->daddr, cp->dport,
501 &cp->vaddr, cp->vport,
502 cp->protocol);
503 ip_vs_bind_dest(cp, dest);
504 return dest;
505 } else
506 return NULL;
511 * Unbind a connection entry with its VS destination
512 * Called by the ip_vs_conn_expire function.
514 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
516 struct ip_vs_dest *dest = cp->dest;
518 if (!dest)
519 return;
521 IP_VS_DBG_BUF(7, "Unbind-dest %s c:%s:%d v:%s:%d "
522 "d:%s:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
523 "dest->refcnt:%d\n",
524 ip_vs_proto_name(cp->protocol),
525 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
526 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
527 IP_VS_DBG_ADDR(cp->af, &cp->daddr), ntohs(cp->dport),
528 ip_vs_fwd_tag(cp), cp->state,
529 cp->flags, atomic_read(&cp->refcnt),
530 atomic_read(&dest->refcnt));
532 /* Update the connection counters */
533 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
534 /* It is a normal connection, so decrease the inactconns
535 or activeconns counter */
536 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
537 atomic_dec(&dest->inactconns);
538 } else {
539 atomic_dec(&dest->activeconns);
541 } else {
542 /* It is a persistent connection/template, so decrease
543 the peristent connection counter */
544 atomic_dec(&dest->persistconns);
547 if (dest->l_threshold != 0) {
548 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
549 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
550 } else if (dest->u_threshold != 0) {
551 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
552 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
553 } else {
554 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
555 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
559 * Simply decrease the refcnt of the dest, because the
560 * dest will be either in service's destination list
561 * or in the trash.
563 atomic_dec(&dest->refcnt);
568 * Checking if the destination of a connection template is available.
569 * If available, return 1, otherwise invalidate this connection
570 * template and return 0.
572 int ip_vs_check_template(struct ip_vs_conn *ct)
574 struct ip_vs_dest *dest = ct->dest;
577 * Checking the dest server status.
579 if ((dest == NULL) ||
580 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
581 (sysctl_ip_vs_expire_quiescent_template &&
582 (atomic_read(&dest->weight) == 0))) {
583 IP_VS_DBG_BUF(9, "check_template: dest not available for "
584 "protocol %s s:%s:%d v:%s:%d "
585 "-> d:%s:%d\n",
586 ip_vs_proto_name(ct->protocol),
587 IP_VS_DBG_ADDR(ct->af, &ct->caddr),
588 ntohs(ct->cport),
589 IP_VS_DBG_ADDR(ct->af, &ct->vaddr),
590 ntohs(ct->vport),
591 IP_VS_DBG_ADDR(ct->af, &ct->daddr),
592 ntohs(ct->dport));
595 * Invalidate the connection template
597 if (ct->vport != htons(0xffff)) {
598 if (ip_vs_conn_unhash(ct)) {
599 ct->dport = htons(0xffff);
600 ct->vport = htons(0xffff);
601 ct->cport = 0;
602 ip_vs_conn_hash(ct);
607 * Simply decrease the refcnt of the template,
608 * don't restart its timer.
610 atomic_dec(&ct->refcnt);
611 return 0;
613 return 1;
616 static void ip_vs_conn_expire(unsigned long data)
618 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
620 cp->timeout = 60*HZ;
623 * hey, I'm using it
625 atomic_inc(&cp->refcnt);
628 * do I control anybody?
630 if (atomic_read(&cp->n_control))
631 goto expire_later;
634 * unhash it if it is hashed in the conn table
636 if (!ip_vs_conn_unhash(cp))
637 goto expire_later;
640 * refcnt==1 implies I'm the only one referrer
642 if (likely(atomic_read(&cp->refcnt) == 1)) {
643 /* delete the timer if it is activated by other users */
644 if (timer_pending(&cp->timer))
645 del_timer(&cp->timer);
647 /* does anybody control me? */
648 if (cp->control)
649 ip_vs_control_del(cp);
651 if (unlikely(cp->app != NULL))
652 ip_vs_unbind_app(cp);
653 ip_vs_unbind_dest(cp);
654 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
655 atomic_dec(&ip_vs_conn_no_cport_cnt);
656 atomic_dec(&ip_vs_conn_count);
658 kmem_cache_free(ip_vs_conn_cachep, cp);
659 return;
662 /* hash it back to the table */
663 ip_vs_conn_hash(cp);
665 expire_later:
666 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
667 atomic_read(&cp->refcnt)-1,
668 atomic_read(&cp->n_control));
670 ip_vs_conn_put(cp);
674 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
676 if (del_timer(&cp->timer))
677 mod_timer(&cp->timer, jiffies);
682 * Create a new connection entry and hash it into the ip_vs_conn_tab
684 struct ip_vs_conn *
685 ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
686 const union nf_inet_addr *vaddr, __be16 vport,
687 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
688 struct ip_vs_dest *dest)
690 struct ip_vs_conn *cp;
691 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
693 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
694 if (cp == NULL) {
695 IP_VS_ERR_RL("%s(): no memory\n", __func__);
696 return NULL;
699 INIT_LIST_HEAD(&cp->c_list);
700 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
701 cp->af = af;
702 cp->protocol = proto;
703 ip_vs_addr_copy(af, &cp->caddr, caddr);
704 cp->cport = cport;
705 ip_vs_addr_copy(af, &cp->vaddr, vaddr);
706 cp->vport = vport;
707 /* proto should only be IPPROTO_IP if d_addr is a fwmark */
708 ip_vs_addr_copy(proto == IPPROTO_IP ? AF_UNSPEC : af,
709 &cp->daddr, daddr);
710 cp->dport = dport;
711 cp->flags = flags;
712 spin_lock_init(&cp->lock);
715 * Set the entry is referenced by the current thread before hashing
716 * it in the table, so that other thread run ip_vs_random_dropentry
717 * but cannot drop this entry.
719 atomic_set(&cp->refcnt, 1);
721 atomic_set(&cp->n_control, 0);
722 atomic_set(&cp->in_pkts, 0);
724 atomic_inc(&ip_vs_conn_count);
725 if (flags & IP_VS_CONN_F_NO_CPORT)
726 atomic_inc(&ip_vs_conn_no_cport_cnt);
728 /* Bind the connection with a destination server */
729 ip_vs_bind_dest(cp, dest);
731 /* Set its state and timeout */
732 cp->state = 0;
733 cp->timeout = 3*HZ;
735 /* Bind its packet transmitter */
736 #ifdef CONFIG_IP_VS_IPV6
737 if (af == AF_INET6)
738 ip_vs_bind_xmit_v6(cp);
739 else
740 #endif
741 ip_vs_bind_xmit(cp);
743 if (unlikely(pp && atomic_read(&pp->appcnt)))
744 ip_vs_bind_app(cp, pp);
746 /* Hash it in the ip_vs_conn_tab finally */
747 ip_vs_conn_hash(cp);
749 return cp;
754 * /proc/net/ip_vs_conn entries
756 #ifdef CONFIG_PROC_FS
758 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
760 int idx;
761 struct ip_vs_conn *cp;
763 for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
764 ct_read_lock_bh(idx);
765 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
766 if (pos-- == 0) {
767 seq->private = &ip_vs_conn_tab[idx];
768 return cp;
771 ct_read_unlock_bh(idx);
774 return NULL;
777 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
779 seq->private = NULL;
780 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
783 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
785 struct ip_vs_conn *cp = v;
786 struct list_head *e, *l = seq->private;
787 int idx;
789 ++*pos;
790 if (v == SEQ_START_TOKEN)
791 return ip_vs_conn_array(seq, 0);
793 /* more on same hash chain? */
794 if ((e = cp->c_list.next) != l)
795 return list_entry(e, struct ip_vs_conn, c_list);
797 idx = l - ip_vs_conn_tab;
798 ct_read_unlock_bh(idx);
800 while (++idx < IP_VS_CONN_TAB_SIZE) {
801 ct_read_lock_bh(idx);
802 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
803 seq->private = &ip_vs_conn_tab[idx];
804 return cp;
806 ct_read_unlock_bh(idx);
808 seq->private = NULL;
809 return NULL;
812 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
814 struct list_head *l = seq->private;
816 if (l)
817 ct_read_unlock_bh(l - ip_vs_conn_tab);
820 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
823 if (v == SEQ_START_TOKEN)
824 seq_puts(seq,
825 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
826 else {
827 const struct ip_vs_conn *cp = v;
829 #ifdef CONFIG_IP_VS_IPV6
830 if (cp->af == AF_INET6)
831 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %7lu\n",
832 ip_vs_proto_name(cp->protocol),
833 &cp->caddr.in6, ntohs(cp->cport),
834 &cp->vaddr.in6, ntohs(cp->vport),
835 &cp->daddr.in6, ntohs(cp->dport),
836 ip_vs_state_name(cp->protocol, cp->state),
837 (cp->timer.expires-jiffies)/HZ);
838 else
839 #endif
840 seq_printf(seq,
841 "%-3s %08X %04X %08X %04X"
842 " %08X %04X %-11s %7lu\n",
843 ip_vs_proto_name(cp->protocol),
844 ntohl(cp->caddr.ip), ntohs(cp->cport),
845 ntohl(cp->vaddr.ip), ntohs(cp->vport),
846 ntohl(cp->daddr.ip), ntohs(cp->dport),
847 ip_vs_state_name(cp->protocol, cp->state),
848 (cp->timer.expires-jiffies)/HZ);
850 return 0;
853 static const struct seq_operations ip_vs_conn_seq_ops = {
854 .start = ip_vs_conn_seq_start,
855 .next = ip_vs_conn_seq_next,
856 .stop = ip_vs_conn_seq_stop,
857 .show = ip_vs_conn_seq_show,
860 static int ip_vs_conn_open(struct inode *inode, struct file *file)
862 return seq_open(file, &ip_vs_conn_seq_ops);
865 static const struct file_operations ip_vs_conn_fops = {
866 .owner = THIS_MODULE,
867 .open = ip_vs_conn_open,
868 .read = seq_read,
869 .llseek = seq_lseek,
870 .release = seq_release,
873 static const char *ip_vs_origin_name(unsigned flags)
875 if (flags & IP_VS_CONN_F_SYNC)
876 return "SYNC";
877 else
878 return "LOCAL";
881 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
884 if (v == SEQ_START_TOKEN)
885 seq_puts(seq,
886 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
887 else {
888 const struct ip_vs_conn *cp = v;
890 #ifdef CONFIG_IP_VS_IPV6
891 if (cp->af == AF_INET6)
892 seq_printf(seq, "%-3s %pI6 %04X %pI6 %04X %pI6 %04X %-11s %-6s %7lu\n",
893 ip_vs_proto_name(cp->protocol),
894 &cp->caddr.in6, ntohs(cp->cport),
895 &cp->vaddr.in6, ntohs(cp->vport),
896 &cp->daddr.in6, ntohs(cp->dport),
897 ip_vs_state_name(cp->protocol, cp->state),
898 ip_vs_origin_name(cp->flags),
899 (cp->timer.expires-jiffies)/HZ);
900 else
901 #endif
902 seq_printf(seq,
903 "%-3s %08X %04X %08X %04X "
904 "%08X %04X %-11s %-6s %7lu\n",
905 ip_vs_proto_name(cp->protocol),
906 ntohl(cp->caddr.ip), ntohs(cp->cport),
907 ntohl(cp->vaddr.ip), ntohs(cp->vport),
908 ntohl(cp->daddr.ip), ntohs(cp->dport),
909 ip_vs_state_name(cp->protocol, cp->state),
910 ip_vs_origin_name(cp->flags),
911 (cp->timer.expires-jiffies)/HZ);
913 return 0;
916 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
917 .start = ip_vs_conn_seq_start,
918 .next = ip_vs_conn_seq_next,
919 .stop = ip_vs_conn_seq_stop,
920 .show = ip_vs_conn_sync_seq_show,
923 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
925 return seq_open(file, &ip_vs_conn_sync_seq_ops);
928 static const struct file_operations ip_vs_conn_sync_fops = {
929 .owner = THIS_MODULE,
930 .open = ip_vs_conn_sync_open,
931 .read = seq_read,
932 .llseek = seq_lseek,
933 .release = seq_release,
936 #endif
940 * Randomly drop connection entries before running out of memory
942 static inline int todrop_entry(struct ip_vs_conn *cp)
945 * The drop rate array needs tuning for real environments.
946 * Called from timer bh only => no locking
948 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
949 static char todrop_counter[9] = {0};
950 int i;
952 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
953 This will leave enough time for normal connection to get
954 through. */
955 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
956 return 0;
958 /* Don't drop the entry if its number of incoming packets is not
959 located in [0, 8] */
960 i = atomic_read(&cp->in_pkts);
961 if (i > 8 || i < 0) return 0;
963 if (!todrop_rate[i]) return 0;
964 if (--todrop_counter[i] > 0) return 0;
966 todrop_counter[i] = todrop_rate[i];
967 return 1;
970 /* Called from keventd and must protect itself from softirqs */
971 void ip_vs_random_dropentry(void)
973 int idx;
974 struct ip_vs_conn *cp;
977 * Randomly scan 1/32 of the whole table every second
979 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
980 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
983 * Lock is actually needed in this loop.
985 ct_write_lock_bh(hash);
987 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
988 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
989 /* connection template */
990 continue;
992 if (cp->protocol == IPPROTO_TCP) {
993 switch(cp->state) {
994 case IP_VS_TCP_S_SYN_RECV:
995 case IP_VS_TCP_S_SYNACK:
996 break;
998 case IP_VS_TCP_S_ESTABLISHED:
999 if (todrop_entry(cp))
1000 break;
1001 continue;
1003 default:
1004 continue;
1006 } else {
1007 if (!todrop_entry(cp))
1008 continue;
1011 IP_VS_DBG(4, "del connection\n");
1012 ip_vs_conn_expire_now(cp);
1013 if (cp->control) {
1014 IP_VS_DBG(4, "del conn template\n");
1015 ip_vs_conn_expire_now(cp->control);
1018 ct_write_unlock_bh(hash);
1024 * Flush all the connection entries in the ip_vs_conn_tab
1026 static void ip_vs_conn_flush(void)
1028 int idx;
1029 struct ip_vs_conn *cp;
1031 flush_again:
1032 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
1034 * Lock is actually needed in this loop.
1036 ct_write_lock_bh(idx);
1038 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1040 IP_VS_DBG(4, "del connection\n");
1041 ip_vs_conn_expire_now(cp);
1042 if (cp->control) {
1043 IP_VS_DBG(4, "del conn template\n");
1044 ip_vs_conn_expire_now(cp->control);
1047 ct_write_unlock_bh(idx);
1050 /* the counter may be not NULL, because maybe some conn entries
1051 are run by slow timer handler or unhashed but still referred */
1052 if (atomic_read(&ip_vs_conn_count) != 0) {
1053 schedule();
1054 goto flush_again;
1059 int __init ip_vs_conn_init(void)
1061 int idx;
1064 * Allocate the connection hash table and initialize its list heads
1066 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
1067 if (!ip_vs_conn_tab)
1068 return -ENOMEM;
1070 /* Allocate ip_vs_conn slab cache */
1071 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1072 sizeof(struct ip_vs_conn), 0,
1073 SLAB_HWCACHE_ALIGN, NULL);
1074 if (!ip_vs_conn_cachep) {
1075 vfree(ip_vs_conn_tab);
1076 return -ENOMEM;
1079 pr_info("Connection hash table configured "
1080 "(size=%d, memory=%ldKbytes)\n",
1081 IP_VS_CONN_TAB_SIZE,
1082 (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
1083 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1084 sizeof(struct ip_vs_conn));
1086 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
1087 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1090 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1091 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1094 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
1095 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1097 /* calculate the random value for connection hash */
1098 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1100 return 0;
1104 void ip_vs_conn_cleanup(void)
1106 /* flush all the connection entries first */
1107 ip_vs_conn_flush();
1109 /* Release the empty cache */
1110 kmem_cache_destroy(ip_vs_conn_cachep);
1111 proc_net_remove(&init_net, "ip_vs_conn");
1112 proc_net_remove(&init_net, "ip_vs_conn_sync");
1113 vfree(ip_vs_conn_tab);