IPVS: Extend functions for getting/creating connections
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / ipvs / ip_vs_conn.c
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.
7 *
8 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
11 *
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.
16 *
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.
20 *
21 * Changes:
22 *
23 */
24
25 #include <linux/interrupt.h>
26 #include <linux/in.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>
35
36 #include <net/net_namespace.h>
37 #include <net/ip_vs.h>
38
39
40 /*
41 * Connection hash table: for input and output packets lookups of IPVS
42 */
43 static struct list_head *ip_vs_conn_tab;
44
45 /* SLAB cache for IPVS connections */
46 static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
47
48 /* counter for current IPVS connections */
49 static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
50
51 /* counter for no client port connections */
52 static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
53
54 /* random value for IPVS connection hash */
55 static unsigned int ip_vs_conn_rnd;
56
57 /*
58 * Fine locking granularity for big connection hash table
59 */
60 #define CT_LOCKARRAY_BITS 4
61 #define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
62 #define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
63
64 struct ip_vs_aligned_lock
65 {
66 rwlock_t l;
67 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
68
69 /* lock array for conn table */
70 static struct ip_vs_aligned_lock
71 __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
72
73 static inline void ct_read_lock(unsigned key)
74 {
75 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
76 }
77
78 static inline void ct_read_unlock(unsigned key)
79 {
80 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
81 }
82
83 static inline void ct_write_lock(unsigned key)
84 {
85 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
86 }
87
88 static inline void ct_write_unlock(unsigned key)
89 {
90 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
91 }
92
93 static inline void ct_read_lock_bh(unsigned key)
94 {
95 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
96 }
97
98 static inline void ct_read_unlock_bh(unsigned key)
99 {
100 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
101 }
102
103 static inline void ct_write_lock_bh(unsigned key)
104 {
105 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
106 }
107
108 static inline void ct_write_unlock_bh(unsigned key)
109 {
110 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
111 }
112
113
114 /*
115 * Returns hash value for IPVS connection entry
116 */
117 static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
118 const union nf_inet_addr *addr,
119 __be16 port)
120 {
121 #ifdef CONFIG_IP_VS_IPV6
122 if (af == AF_INET6)
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;
126 #endif
127 return jhash_3words((__force u32)addr->ip, (__force u32)port, proto,
128 ip_vs_conn_rnd)
129 & IP_VS_CONN_TAB_MASK;
130 }
131
132
133 /*
134 * Hashes ip_vs_conn in ip_vs_conn_tab by proto,addr,port.
135 * returns bool success.
136 */
137 static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
138 {
139 unsigned hash;
140 int ret;
141
142 /* Hash by protocol, client address and port */
143 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
144
145 ct_write_lock(hash);
146
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);
151 ret = 1;
152 } else {
153 IP_VS_ERR("ip_vs_conn_hash(): request for already hashed, "
154 "called from %p\n", __builtin_return_address(0));
155 ret = 0;
156 }
157
158 ct_write_unlock(hash);
159
160 return ret;
161 }
162
163
164 /*
165 * UNhashes ip_vs_conn from ip_vs_conn_tab.
166 * returns bool success.
167 */
168 static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
169 {
170 unsigned hash;
171 int ret;
172
173 /* unhash it and decrease its reference counter */
174 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
175
176 ct_write_lock(hash);
177
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);
182 ret = 1;
183 } else
184 ret = 0;
185
186 ct_write_unlock(hash);
187
188 return ret;
189 }
190
191
192 /*
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)
197 */
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)
201 {
202 unsigned hash;
203 struct ip_vs_conn *cp;
204
205 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
206
207 ct_read_lock(hash);
208
209 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
210 if (cp->af == af &&
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) {
216 /* HIT */
217 atomic_inc(&cp->refcnt);
218 ct_read_unlock(hash);
219 return cp;
220 }
221 }
222
223 ct_read_unlock(hash);
224
225 return NULL;
226 }
227
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)
231 {
232 struct ip_vs_conn *cp;
233
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,
237 d_port);
238
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");
244
245 return cp;
246 }
247
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)
252 {
253 unsigned hash;
254 struct ip_vs_conn *cp;
255
256 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
257
258 ct_read_lock(hash);
259
260 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
261 if (cp->af == af &&
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) {
267 /* HIT */
268 atomic_inc(&cp->refcnt);
269 goto out;
270 }
271 }
272 cp = NULL;
273
274 out:
275 ct_read_unlock(hash);
276
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");
282
283 return cp;
284 }
285
286 /*
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)
291 */
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)
295 {
296 unsigned hash;
297 struct ip_vs_conn *cp, *ret=NULL;
298
299 /*
300 * Check for "full" addressed entries
301 */
302 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
303
304 ct_read_lock(hash);
305
306 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
307 if (cp->af == af &&
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) {
312 /* HIT */
313 atomic_inc(&cp->refcnt);
314 ret = cp;
315 break;
316 }
317 }
318
319 ct_read_unlock(hash);
320
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");
326
327 return ret;
328 }
329
330
331 /*
332 * Put back the conn and restart its timer with its timeout
333 */
334 void ip_vs_conn_put(struct ip_vs_conn *cp)
335 {
336 /* reset it expire in its timeout */
337 mod_timer(&cp->timer, jiffies+cp->timeout);
338
339 __ip_vs_conn_put(cp);
340 }
341
342
343 /*
344 * Fill a no_client_port connection with a client port number
345 */
346 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
347 {
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;
353 cp->cport = cport;
354 }
355 spin_unlock(&cp->lock);
356
357 /* hash on new dport */
358 ip_vs_conn_hash(cp);
359 }
360 }
361
362
363 /*
364 * Bind a connection entry with the corresponding packet_xmit.
365 * Called by ip_vs_conn_new.
366 */
367 static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
368 {
369 switch (IP_VS_FWD_METHOD(cp)) {
370 case IP_VS_CONN_F_MASQ:
371 cp->packet_xmit = ip_vs_nat_xmit;
372 break;
373
374 case IP_VS_CONN_F_TUNNEL:
375 cp->packet_xmit = ip_vs_tunnel_xmit;
376 break;
377
378 case IP_VS_CONN_F_DROUTE:
379 cp->packet_xmit = ip_vs_dr_xmit;
380 break;
381
382 case IP_VS_CONN_F_LOCALNODE:
383 cp->packet_xmit = ip_vs_null_xmit;
384 break;
385
386 case IP_VS_CONN_F_BYPASS:
387 cp->packet_xmit = ip_vs_bypass_xmit;
388 break;
389 }
390 }
391
392
393 static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
394 {
395 return atomic_read(&dest->activeconns)
396 + atomic_read(&dest->inactconns);
397 }
398
399 /*
400 * Bind a connection entry with a virtual service destination
401 * Called just after a new connection entry is created.
402 */
403 static inline void
404 ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
405 {
406 /* if dest is NULL, then return directly */
407 if (!dest)
408 return;
409
410 /* Increase the refcnt counter of the dest */
411 atomic_inc(&dest->refcnt);
412
413 /* Bind with the destination and its corresponding transmitter */
414 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
415 (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
416 /* if the connection is not template and is created
417 * by sync, preserve the activity flag.
418 */
419 cp->flags |= atomic_read(&dest->conn_flags) &
420 (~IP_VS_CONN_F_INACTIVE);
421 else
422 cp->flags |= atomic_read(&dest->conn_flags);
423 cp->dest = dest;
424
425 IP_VS_DBG(7, "Bind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
426 "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
427 "dest->refcnt:%d\n",
428 ip_vs_proto_name(cp->protocol),
429 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
430 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
431 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
432 ip_vs_fwd_tag(cp), cp->state,
433 cp->flags, atomic_read(&cp->refcnt),
434 atomic_read(&dest->refcnt));
435
436 /* Update the connection counters */
437 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
438 /* It is a normal connection, so increase the inactive
439 connection counter because it is in TCP SYNRECV
440 state (inactive) or other protocol inacive state */
441 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
442 (!(cp->flags & IP_VS_CONN_F_INACTIVE)))
443 atomic_inc(&dest->activeconns);
444 else
445 atomic_inc(&dest->inactconns);
446 } else {
447 /* It is a persistent connection/template, so increase
448 the peristent connection counter */
449 atomic_inc(&dest->persistconns);
450 }
451
452 if (dest->u_threshold != 0 &&
453 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
454 dest->flags |= IP_VS_DEST_F_OVERLOAD;
455 }
456
457
458 /*
459 * Check if there is a destination for the connection, if so
460 * bind the connection to the destination.
461 */
462 struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
463 {
464 struct ip_vs_dest *dest;
465
466 if ((cp) && (!cp->dest)) {
467 dest = ip_vs_find_dest(cp->daddr.ip, cp->dport,
468 cp->vaddr.ip, cp->vport, cp->protocol);
469 ip_vs_bind_dest(cp, dest);
470 return dest;
471 } else
472 return NULL;
473 }
474
475
476 /*
477 * Unbind a connection entry with its VS destination
478 * Called by the ip_vs_conn_expire function.
479 */
480 static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
481 {
482 struct ip_vs_dest *dest = cp->dest;
483
484 if (!dest)
485 return;
486
487 IP_VS_DBG(7, "Unbind-dest %s c:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
488 "d:%u.%u.%u.%u:%d fwd:%c s:%u conn->flags:%X conn->refcnt:%d "
489 "dest->refcnt:%d\n",
490 ip_vs_proto_name(cp->protocol),
491 NIPQUAD(cp->caddr.ip), ntohs(cp->cport),
492 NIPQUAD(cp->vaddr.ip), ntohs(cp->vport),
493 NIPQUAD(cp->daddr.ip), ntohs(cp->dport),
494 ip_vs_fwd_tag(cp), cp->state,
495 cp->flags, atomic_read(&cp->refcnt),
496 atomic_read(&dest->refcnt));
497
498 /* Update the connection counters */
499 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
500 /* It is a normal connection, so decrease the inactconns
501 or activeconns counter */
502 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
503 atomic_dec(&dest->inactconns);
504 } else {
505 atomic_dec(&dest->activeconns);
506 }
507 } else {
508 /* It is a persistent connection/template, so decrease
509 the peristent connection counter */
510 atomic_dec(&dest->persistconns);
511 }
512
513 if (dest->l_threshold != 0) {
514 if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
515 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
516 } else if (dest->u_threshold != 0) {
517 if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
518 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
519 } else {
520 if (dest->flags & IP_VS_DEST_F_OVERLOAD)
521 dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
522 }
523
524 /*
525 * Simply decrease the refcnt of the dest, because the
526 * dest will be either in service's destination list
527 * or in the trash.
528 */
529 atomic_dec(&dest->refcnt);
530 }
531
532
533 /*
534 * Checking if the destination of a connection template is available.
535 * If available, return 1, otherwise invalidate this connection
536 * template and return 0.
537 */
538 int ip_vs_check_template(struct ip_vs_conn *ct)
539 {
540 struct ip_vs_dest *dest = ct->dest;
541
542 /*
543 * Checking the dest server status.
544 */
545 if ((dest == NULL) ||
546 !(dest->flags & IP_VS_DEST_F_AVAILABLE) ||
547 (sysctl_ip_vs_expire_quiescent_template &&
548 (atomic_read(&dest->weight) == 0))) {
549 IP_VS_DBG(9, "check_template: dest not available for "
550 "protocol %s s:%u.%u.%u.%u:%d v:%u.%u.%u.%u:%d "
551 "-> d:%u.%u.%u.%u:%d\n",
552 ip_vs_proto_name(ct->protocol),
553 NIPQUAD(ct->caddr.ip), ntohs(ct->cport),
554 NIPQUAD(ct->vaddr.ip), ntohs(ct->vport),
555 NIPQUAD(ct->daddr.ip), ntohs(ct->dport));
556
557 /*
558 * Invalidate the connection template
559 */
560 if (ct->vport != htons(0xffff)) {
561 if (ip_vs_conn_unhash(ct)) {
562 ct->dport = htons(0xffff);
563 ct->vport = htons(0xffff);
564 ct->cport = 0;
565 ip_vs_conn_hash(ct);
566 }
567 }
568
569 /*
570 * Simply decrease the refcnt of the template,
571 * don't restart its timer.
572 */
573 atomic_dec(&ct->refcnt);
574 return 0;
575 }
576 return 1;
577 }
578
579 static void ip_vs_conn_expire(unsigned long data)
580 {
581 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
582
583 cp->timeout = 60*HZ;
584
585 /*
586 * hey, I'm using it
587 */
588 atomic_inc(&cp->refcnt);
589
590 /*
591 * do I control anybody?
592 */
593 if (atomic_read(&cp->n_control))
594 goto expire_later;
595
596 /*
597 * unhash it if it is hashed in the conn table
598 */
599 if (!ip_vs_conn_unhash(cp))
600 goto expire_later;
601
602 /*
603 * refcnt==1 implies I'm the only one referrer
604 */
605 if (likely(atomic_read(&cp->refcnt) == 1)) {
606 /* delete the timer if it is activated by other users */
607 if (timer_pending(&cp->timer))
608 del_timer(&cp->timer);
609
610 /* does anybody control me? */
611 if (cp->control)
612 ip_vs_control_del(cp);
613
614 if (unlikely(cp->app != NULL))
615 ip_vs_unbind_app(cp);
616 ip_vs_unbind_dest(cp);
617 if (cp->flags & IP_VS_CONN_F_NO_CPORT)
618 atomic_dec(&ip_vs_conn_no_cport_cnt);
619 atomic_dec(&ip_vs_conn_count);
620
621 kmem_cache_free(ip_vs_conn_cachep, cp);
622 return;
623 }
624
625 /* hash it back to the table */
626 ip_vs_conn_hash(cp);
627
628 expire_later:
629 IP_VS_DBG(7, "delayed: conn->refcnt-1=%d conn->n_control=%d\n",
630 atomic_read(&cp->refcnt)-1,
631 atomic_read(&cp->n_control));
632
633 ip_vs_conn_put(cp);
634 }
635
636
637 void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
638 {
639 if (del_timer(&cp->timer))
640 mod_timer(&cp->timer, jiffies);
641 }
642
643
644 /*
645 * Create a new connection entry and hash it into the ip_vs_conn_tab
646 */
647 struct ip_vs_conn *
648 ip_vs_conn_new(int af, int proto, const union nf_inet_addr *caddr, __be16 cport,
649 const union nf_inet_addr *vaddr, __be16 vport,
650 const union nf_inet_addr *daddr, __be16 dport, unsigned flags,
651 struct ip_vs_dest *dest)
652 {
653 struct ip_vs_conn *cp;
654 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
655
656 cp = kmem_cache_zalloc(ip_vs_conn_cachep, GFP_ATOMIC);
657 if (cp == NULL) {
658 IP_VS_ERR_RL("ip_vs_conn_new: no memory available.\n");
659 return NULL;
660 }
661
662 INIT_LIST_HEAD(&cp->c_list);
663 setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
664 cp->af = af;
665 cp->protocol = proto;
666 ip_vs_addr_copy(af, &cp->caddr, caddr);
667 cp->cport = cport;
668 ip_vs_addr_copy(af, &cp->vaddr, vaddr);
669 cp->vport = vport;
670 ip_vs_addr_copy(af, &cp->daddr, daddr);
671 cp->dport = dport;
672 cp->flags = flags;
673 spin_lock_init(&cp->lock);
674
675 /*
676 * Set the entry is referenced by the current thread before hashing
677 * it in the table, so that other thread run ip_vs_random_dropentry
678 * but cannot drop this entry.
679 */
680 atomic_set(&cp->refcnt, 1);
681
682 atomic_set(&cp->n_control, 0);
683 atomic_set(&cp->in_pkts, 0);
684
685 atomic_inc(&ip_vs_conn_count);
686 if (flags & IP_VS_CONN_F_NO_CPORT)
687 atomic_inc(&ip_vs_conn_no_cport_cnt);
688
689 /* Bind the connection with a destination server */
690 ip_vs_bind_dest(cp, dest);
691
692 /* Set its state and timeout */
693 cp->state = 0;
694 cp->timeout = 3*HZ;
695
696 /* Bind its packet transmitter */
697 ip_vs_bind_xmit(cp);
698
699 if (unlikely(pp && atomic_read(&pp->appcnt)))
700 ip_vs_bind_app(cp, pp);
701
702 /* Hash it in the ip_vs_conn_tab finally */
703 ip_vs_conn_hash(cp);
704
705 return cp;
706 }
707
708
709 /*
710 * /proc/net/ip_vs_conn entries
711 */
712 #ifdef CONFIG_PROC_FS
713
714 static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
715 {
716 int idx;
717 struct ip_vs_conn *cp;
718
719 for(idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
720 ct_read_lock_bh(idx);
721 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
722 if (pos-- == 0) {
723 seq->private = &ip_vs_conn_tab[idx];
724 return cp;
725 }
726 }
727 ct_read_unlock_bh(idx);
728 }
729
730 return NULL;
731 }
732
733 static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
734 {
735 seq->private = NULL;
736 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
737 }
738
739 static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
740 {
741 struct ip_vs_conn *cp = v;
742 struct list_head *e, *l = seq->private;
743 int idx;
744
745 ++*pos;
746 if (v == SEQ_START_TOKEN)
747 return ip_vs_conn_array(seq, 0);
748
749 /* more on same hash chain? */
750 if ((e = cp->c_list.next) != l)
751 return list_entry(e, struct ip_vs_conn, c_list);
752
753 idx = l - ip_vs_conn_tab;
754 ct_read_unlock_bh(idx);
755
756 while (++idx < IP_VS_CONN_TAB_SIZE) {
757 ct_read_lock_bh(idx);
758 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
759 seq->private = &ip_vs_conn_tab[idx];
760 return cp;
761 }
762 ct_read_unlock_bh(idx);
763 }
764 seq->private = NULL;
765 return NULL;
766 }
767
768 static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
769 {
770 struct list_head *l = seq->private;
771
772 if (l)
773 ct_read_unlock_bh(l - ip_vs_conn_tab);
774 }
775
776 static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
777 {
778
779 if (v == SEQ_START_TOKEN)
780 seq_puts(seq,
781 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Expires\n");
782 else {
783 const struct ip_vs_conn *cp = v;
784
785 seq_printf(seq,
786 "%-3s %08X %04X %08X %04X %08X %04X %-11s %7lu\n",
787 ip_vs_proto_name(cp->protocol),
788 ntohl(cp->caddr.ip), ntohs(cp->cport),
789 ntohl(cp->vaddr.ip), ntohs(cp->vport),
790 ntohl(cp->daddr.ip), ntohs(cp->dport),
791 ip_vs_state_name(cp->protocol, cp->state),
792 (cp->timer.expires-jiffies)/HZ);
793 }
794 return 0;
795 }
796
797 static const struct seq_operations ip_vs_conn_seq_ops = {
798 .start = ip_vs_conn_seq_start,
799 .next = ip_vs_conn_seq_next,
800 .stop = ip_vs_conn_seq_stop,
801 .show = ip_vs_conn_seq_show,
802 };
803
804 static int ip_vs_conn_open(struct inode *inode, struct file *file)
805 {
806 return seq_open(file, &ip_vs_conn_seq_ops);
807 }
808
809 static const struct file_operations ip_vs_conn_fops = {
810 .owner = THIS_MODULE,
811 .open = ip_vs_conn_open,
812 .read = seq_read,
813 .llseek = seq_lseek,
814 .release = seq_release,
815 };
816
817 static const char *ip_vs_origin_name(unsigned flags)
818 {
819 if (flags & IP_VS_CONN_F_SYNC)
820 return "SYNC";
821 else
822 return "LOCAL";
823 }
824
825 static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
826 {
827
828 if (v == SEQ_START_TOKEN)
829 seq_puts(seq,
830 "Pro FromIP FPrt ToIP TPrt DestIP DPrt State Origin Expires\n");
831 else {
832 const struct ip_vs_conn *cp = v;
833
834 seq_printf(seq,
835 "%-3s %08X %04X %08X %04X %08X %04X %-11s %-6s %7lu\n",
836 ip_vs_proto_name(cp->protocol),
837 ntohl(cp->caddr.ip), ntohs(cp->cport),
838 ntohl(cp->vaddr.ip), ntohs(cp->vport),
839 ntohl(cp->daddr.ip), ntohs(cp->dport),
840 ip_vs_state_name(cp->protocol, cp->state),
841 ip_vs_origin_name(cp->flags),
842 (cp->timer.expires-jiffies)/HZ);
843 }
844 return 0;
845 }
846
847 static const struct seq_operations ip_vs_conn_sync_seq_ops = {
848 .start = ip_vs_conn_seq_start,
849 .next = ip_vs_conn_seq_next,
850 .stop = ip_vs_conn_seq_stop,
851 .show = ip_vs_conn_sync_seq_show,
852 };
853
854 static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
855 {
856 return seq_open(file, &ip_vs_conn_sync_seq_ops);
857 }
858
859 static const struct file_operations ip_vs_conn_sync_fops = {
860 .owner = THIS_MODULE,
861 .open = ip_vs_conn_sync_open,
862 .read = seq_read,
863 .llseek = seq_lseek,
864 .release = seq_release,
865 };
866
867 #endif
868
869
870 /*
871 * Randomly drop connection entries before running out of memory
872 */
873 static inline int todrop_entry(struct ip_vs_conn *cp)
874 {
875 /*
876 * The drop rate array needs tuning for real environments.
877 * Called from timer bh only => no locking
878 */
879 static const char todrop_rate[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
880 static char todrop_counter[9] = {0};
881 int i;
882
883 /* if the conn entry hasn't lasted for 60 seconds, don't drop it.
884 This will leave enough time for normal connection to get
885 through. */
886 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
887 return 0;
888
889 /* Don't drop the entry if its number of incoming packets is not
890 located in [0, 8] */
891 i = atomic_read(&cp->in_pkts);
892 if (i > 8 || i < 0) return 0;
893
894 if (!todrop_rate[i]) return 0;
895 if (--todrop_counter[i] > 0) return 0;
896
897 todrop_counter[i] = todrop_rate[i];
898 return 1;
899 }
900
901 /* Called from keventd and must protect itself from softirqs */
902 void ip_vs_random_dropentry(void)
903 {
904 int idx;
905 struct ip_vs_conn *cp;
906
907 /*
908 * Randomly scan 1/32 of the whole table every second
909 */
910 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
911 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
912
913 /*
914 * Lock is actually needed in this loop.
915 */
916 ct_write_lock_bh(hash);
917
918 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
919 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
920 /* connection template */
921 continue;
922
923 if (cp->protocol == IPPROTO_TCP) {
924 switch(cp->state) {
925 case IP_VS_TCP_S_SYN_RECV:
926 case IP_VS_TCP_S_SYNACK:
927 break;
928
929 case IP_VS_TCP_S_ESTABLISHED:
930 if (todrop_entry(cp))
931 break;
932 continue;
933
934 default:
935 continue;
936 }
937 } else {
938 if (!todrop_entry(cp))
939 continue;
940 }
941
942 IP_VS_DBG(4, "del connection\n");
943 ip_vs_conn_expire_now(cp);
944 if (cp->control) {
945 IP_VS_DBG(4, "del conn template\n");
946 ip_vs_conn_expire_now(cp->control);
947 }
948 }
949 ct_write_unlock_bh(hash);
950 }
951 }
952
953
954 /*
955 * Flush all the connection entries in the ip_vs_conn_tab
956 */
957 static void ip_vs_conn_flush(void)
958 {
959 int idx;
960 struct ip_vs_conn *cp;
961
962 flush_again:
963 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
964 /*
965 * Lock is actually needed in this loop.
966 */
967 ct_write_lock_bh(idx);
968
969 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
970
971 IP_VS_DBG(4, "del connection\n");
972 ip_vs_conn_expire_now(cp);
973 if (cp->control) {
974 IP_VS_DBG(4, "del conn template\n");
975 ip_vs_conn_expire_now(cp->control);
976 }
977 }
978 ct_write_unlock_bh(idx);
979 }
980
981 /* the counter may be not NULL, because maybe some conn entries
982 are run by slow timer handler or unhashed but still referred */
983 if (atomic_read(&ip_vs_conn_count) != 0) {
984 schedule();
985 goto flush_again;
986 }
987 }
988
989
990 int __init ip_vs_conn_init(void)
991 {
992 int idx;
993
994 /*
995 * Allocate the connection hash table and initialize its list heads
996 */
997 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
998 if (!ip_vs_conn_tab)
999 return -ENOMEM;
1000
1001 /* Allocate ip_vs_conn slab cache */
1002 ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn",
1003 sizeof(struct ip_vs_conn), 0,
1004 SLAB_HWCACHE_ALIGN, NULL);
1005 if (!ip_vs_conn_cachep) {
1006 vfree(ip_vs_conn_tab);
1007 return -ENOMEM;
1008 }
1009
1010 IP_VS_INFO("Connection hash table configured "
1011 "(size=%d, memory=%ldKbytes)\n",
1012 IP_VS_CONN_TAB_SIZE,
1013 (long)(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head))/1024);
1014 IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
1015 sizeof(struct ip_vs_conn));
1016
1017 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
1018 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1019 }
1020
1021 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1022 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1023 }
1024
1025 proc_net_fops_create(&init_net, "ip_vs_conn", 0, &ip_vs_conn_fops);
1026 proc_net_fops_create(&init_net, "ip_vs_conn_sync", 0, &ip_vs_conn_sync_fops);
1027
1028 /* calculate the random value for connection hash */
1029 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1030
1031 return 0;
1032 }
1033
1034
1035 void ip_vs_conn_cleanup(void)
1036 {
1037 /* flush all the connection entries first */
1038 ip_vs_conn_flush();
1039
1040 /* Release the empty cache */
1041 kmem_cache_destroy(ip_vs_conn_cachep);
1042 proc_net_remove(&init_net, "ip_vs_conn");
1043 proc_net_remove(&init_net, "ip_vs_conn_sync");
1044 vfree(ip_vs_conn_tab);
1045 }