1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define KMSG_COMPONENT "IPVS"
26#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
27
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>
35#include <linux/seq_file.h>
36#include <linux/jhash.h>
37#include <linux/random.h>
38
39#include <net/net_namespace.h>
40#include <net/ip_vs.h>
41
42
43
44
45
46static struct list_head *ip_vs_conn_tab;
47
48
49static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
50
51
52static atomic_t ip_vs_conn_count = ATOMIC_INIT(0);
53
54
55static atomic_t ip_vs_conn_no_cport_cnt = ATOMIC_INIT(0);
56
57
58static unsigned int ip_vs_conn_rnd;
59
60
61
62
63#define CT_LOCKARRAY_BITS 4
64#define CT_LOCKARRAY_SIZE (1<<CT_LOCKARRAY_BITS)
65#define CT_LOCKARRAY_MASK (CT_LOCKARRAY_SIZE-1)
66
67struct ip_vs_aligned_lock
68{
69 rwlock_t l;
70} __attribute__((__aligned__(SMP_CACHE_BYTES)));
71
72
73static struct ip_vs_aligned_lock
74__ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
75
76static inline void ct_read_lock(unsigned key)
77{
78 read_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
79}
80
81static inline void ct_read_unlock(unsigned key)
82{
83 read_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
84}
85
86static inline void ct_write_lock(unsigned key)
87{
88 write_lock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
89}
90
91static inline void ct_write_unlock(unsigned key)
92{
93 write_unlock(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
94}
95
96static inline void ct_read_lock_bh(unsigned key)
97{
98 read_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
99}
100
101static inline void ct_read_unlock_bh(unsigned key)
102{
103 read_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
104}
105
106static inline void ct_write_lock_bh(unsigned key)
107{
108 write_lock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
109}
110
111static inline void ct_write_unlock_bh(unsigned key)
112{
113 write_unlock_bh(&__ip_vs_conntbl_lock_array[key&CT_LOCKARRAY_MASK].l);
114}
115
116
117
118
119
120static unsigned int ip_vs_conn_hashkey(int af, unsigned proto,
121 const union nf_inet_addr *addr,
122 __be16 port)
123{
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;
133}
134
135
136
137
138
139
140static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
141{
142 unsigned hash;
143 int ret;
144
145
146 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
147
148 ct_write_lock(hash);
149
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;
159 }
160
161 ct_write_unlock(hash);
162
163 return ret;
164}
165
166
167
168
169
170
171static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
172{
173 unsigned hash;
174 int ret;
175
176
177 hash = ip_vs_conn_hashkey(cp->af, cp->protocol, &cp->caddr, cp->cport);
178
179 ct_write_lock(hash);
180
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;
188
189 ct_write_unlock(hash);
190
191 return ret;
192}
193
194
195
196
197
198
199
200
201static 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)
204{
205 unsigned hash;
206 struct ip_vs_conn *cp;
207
208 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
209
210 ct_read_lock(hash);
211
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
220 atomic_inc(&cp->refcnt);
221 ct_read_unlock(hash);
222 return cp;
223 }
224 }
225
226 ct_read_unlock(hash);
227
228 return NULL;
229}
230
231struct 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)
234{
235 struct ip_vs_conn *cp;
236
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);
241
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");
247
248 return cp;
249}
250
251
252struct 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)
255{
256 unsigned hash;
257 struct ip_vs_conn *cp;
258
259 hash = ip_vs_conn_hashkey(af, protocol, s_addr, s_port);
260
261 ct_read_lock(hash);
262
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
267
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
274 atomic_inc(&cp->refcnt);
275 goto out;
276 }
277 }
278 cp = NULL;
279
280 out:
281 ct_read_unlock(hash);
282
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");
288
289 return cp;
290}
291
292
293
294
295
296
297
298struct 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)
301{
302 unsigned hash;
303 struct ip_vs_conn *cp, *ret=NULL;
304
305
306
307
308 hash = ip_vs_conn_hashkey(af, protocol, d_addr, d_port);
309
310 ct_read_lock(hash);
311
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
319 atomic_inc(&cp->refcnt);
320 ret = cp;
321 break;
322 }
323 }
324
325 ct_read_unlock(hash);
326
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");
332
333 return ret;
334}
335
336
337
338
339
340void ip_vs_conn_put(struct ip_vs_conn *cp)
341{
342
343 mod_timer(&cp->timer, jiffies+cp->timeout);
344
345 __ip_vs_conn_put(cp);
346}
347
348
349
350
351
352void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
353{
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;
360 }
361 spin_unlock(&cp->lock);
362
363
364 ip_vs_conn_hash(cp);
365 }
366}
367
368
369
370
371
372
373static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
374{
375 switch (IP_VS_FWD_METHOD(cp)) {
376 case IP_VS_CONN_F_MASQ:
377 cp->packet_xmit = ip_vs_nat_xmit;
378 break;
379
380 case IP_VS_CONN_F_TUNNEL:
381 cp->packet_xmit = ip_vs_tunnel_xmit;
382 break;
383
384 case IP_VS_CONN_F_DROUTE:
385 cp->packet_xmit = ip_vs_dr_xmit;
386 break;
387
388 case IP_VS_CONN_F_LOCALNODE:
389 cp->packet_xmit = ip_vs_null_xmit;
390 break;
391
392 case IP_VS_CONN_F_BYPASS:
393 cp->packet_xmit = ip_vs_bypass_xmit;
394 break;
395 }
396}
397
398#ifdef CONFIG_IP_VS_IPV6
399static inline void ip_vs_bind_xmit_v6(struct ip_vs_conn *cp)
400{
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;
405
406 case IP_VS_CONN_F_TUNNEL:
407 cp->packet_xmit = ip_vs_tunnel_xmit_v6;
408 break;
409
410 case IP_VS_CONN_F_DROUTE:
411 cp->packet_xmit = ip_vs_dr_xmit_v6;
412 break;
413
414 case IP_VS_CONN_F_LOCALNODE:
415 cp->packet_xmit = ip_vs_null_xmit;
416 break;
417
418 case IP_VS_CONN_F_BYPASS:
419 cp->packet_xmit = ip_vs_bypass_xmit_v6;
420 break;
421 }
422}
423#endif
424
425
426static inline int ip_vs_dest_totalconns(struct ip_vs_dest *dest)
427{
428 return atomic_read(&dest->activeconns)
429 + atomic_read(&dest->inactconns);
430}
431
432
433
434
435
436static inline void
437ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
438{
439
440 if (!dest)
441 return;
442
443
444 atomic_inc(&dest->refcnt);
445
446
447 if ((cp->flags & IP_VS_CONN_F_SYNC) &&
448 (!(cp->flags & IP_VS_CONN_F_TEMPLATE)))
449
450
451
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;
457
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));
468
469
470 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
471
472
473
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
481
482 atomic_inc(&dest->persistconns);
483 }
484
485 if (dest->u_threshold != 0 &&
486 ip_vs_dest_totalconns(dest) >= dest->u_threshold)
487 dest->flags |= IP_VS_DEST_F_OVERLOAD;
488}
489
490
491
492
493
494
495struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp)
496{
497 struct ip_vs_dest *dest;
498
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;
507}
508
509
510
511
512
513
514static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
515{
516 struct ip_vs_dest *dest = cp->dest;
517
518 if (!dest)
519 return;
520
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));
531
532
533 if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
534
535
536 if (cp->flags & IP_VS_CONN_F_INACTIVE) {
537 atomic_dec(&dest->inactconns);
538 } else {
539 atomic_dec(&dest->activeconns);
540 }
541 } else {
542
543
544 atomic_dec(&dest->persistconns);
545 }
546
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;
556 }
557
558
559
560
561
562
563 atomic_dec(&dest->refcnt);
564}
565
566
567
568
569
570
571
572int ip_vs_check_template(struct ip_vs_conn *ct)
573{
574 struct ip_vs_dest *dest = ct->dest;
575
576
577
578
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));
593
594
595
596
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);
603 }
604 }
605
606
607
608
609
610 atomic_dec(&ct->refcnt);
611 return 0;
612 }
613 return 1;
614}
615
616static void ip_vs_conn_expire(unsigned long data)
617{
618 struct ip_vs_conn *cp = (struct ip_vs_conn *)data;
619
620 cp->timeout = 60*HZ;
621
622
623
624
625 atomic_inc(&cp->refcnt);
626
627
628
629
630 if (atomic_read(&cp->n_control))
631 goto expire_later;
632
633
634
635
636 if (!ip_vs_conn_unhash(cp))
637 goto expire_later;
638
639
640
641
642 if (likely(atomic_read(&cp->refcnt) == 1)) {
643
644 if (timer_pending(&cp->timer))
645 del_timer(&cp->timer);
646
647
648 if (cp->control)
649 ip_vs_control_del(cp);
650
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);
657
658 kmem_cache_free(ip_vs_conn_cachep, cp);
659 return;
660 }
661
662
663 ip_vs_conn_hash(cp);
664
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));
669
670 ip_vs_conn_put(cp);
671}
672
673
674void ip_vs_conn_expire_now(struct ip_vs_conn *cp)
675{
676 if (del_timer(&cp->timer))
677 mod_timer(&cp->timer, jiffies);
678}
679
680
681
682
683
684struct ip_vs_conn *
685ip_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)
689{
690 struct ip_vs_conn *cp;
691 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
692
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;
697 }
698
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
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);
713
714
715
716
717
718
719 atomic_set(&cp->refcnt, 1);
720
721 atomic_set(&cp->n_control, 0);
722 atomic_set(&cp->in_pkts, 0);
723
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);
727
728
729 ip_vs_bind_dest(cp, dest);
730
731
732 cp->state = 0;
733 cp->timeout = 3*HZ;
734
735
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);
742
743 if (unlikely(pp && atomic_read(&pp->appcnt)))
744 ip_vs_bind_app(cp, pp);
745
746
747 ip_vs_conn_hash(cp);
748
749 return cp;
750}
751
752
753
754
755
756#ifdef CONFIG_PROC_FS
757
758static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
759{
760 int idx;
761 struct ip_vs_conn *cp;
762
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;
769 }
770 }
771 ct_read_unlock_bh(idx);
772 }
773
774 return NULL;
775}
776
777static void *ip_vs_conn_seq_start(struct seq_file *seq, loff_t *pos)
778{
779 seq->private = NULL;
780 return *pos ? ip_vs_conn_array(seq, *pos - 1) :SEQ_START_TOKEN;
781}
782
783static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
784{
785 struct ip_vs_conn *cp = v;
786 struct list_head *e, *l = seq->private;
787 int idx;
788
789 ++*pos;
790 if (v == SEQ_START_TOKEN)
791 return ip_vs_conn_array(seq, 0);
792
793
794 if ((e = cp->c_list.next) != l)
795 return list_entry(e, struct ip_vs_conn, c_list);
796
797 idx = l - ip_vs_conn_tab;
798 ct_read_unlock_bh(idx);
799
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;
805 }
806 ct_read_unlock_bh(idx);
807 }
808 seq->private = NULL;
809 return NULL;
810}
811
812static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
813{
814 struct list_head *l = seq->private;
815
816 if (l)
817 ct_read_unlock_bh(l - ip_vs_conn_tab);
818}
819
820static int ip_vs_conn_seq_show(struct seq_file *seq, void *v)
821{
822
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;
828
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);
849 }
850 return 0;
851}
852
853static 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,
858};
859
860static int ip_vs_conn_open(struct inode *inode, struct file *file)
861{
862 return seq_open(file, &ip_vs_conn_seq_ops);
863}
864
865static 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,
871};
872
873static const char *ip_vs_origin_name(unsigned flags)
874{
875 if (flags & IP_VS_CONN_F_SYNC)
876 return "SYNC";
877 else
878 return "LOCAL";
879}
880
881static int ip_vs_conn_sync_seq_show(struct seq_file *seq, void *v)
882{
883
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;
889
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);
912 }
913 return 0;
914}
915
916static 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,
921};
922
923static int ip_vs_conn_sync_open(struct inode *inode, struct file *file)
924{
925 return seq_open(file, &ip_vs_conn_sync_seq_ops);
926}
927
928static 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,
934};
935
936#endif
937
938
939
940
941
942static inline int todrop_entry(struct ip_vs_conn *cp)
943{
944
945
946
947
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;
951
952
953
954
955 if (time_before(cp->timeout + jiffies, cp->timer.expires + 60*HZ))
956 return 0;
957
958
959
960 i = atomic_read(&cp->in_pkts);
961 if (i > 8 || i < 0) return 0;
962
963 if (!todrop_rate[i]) return 0;
964 if (--todrop_counter[i] > 0) return 0;
965
966 todrop_counter[i] = todrop_rate[i];
967 return 1;
968}
969
970
971void ip_vs_random_dropentry(void)
972{
973 int idx;
974 struct ip_vs_conn *cp;
975
976
977
978
979 for (idx = 0; idx < (IP_VS_CONN_TAB_SIZE>>5); idx++) {
980 unsigned hash = net_random() & IP_VS_CONN_TAB_MASK;
981
982
983
984
985 ct_write_lock_bh(hash);
986
987 list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
988 if (cp->flags & IP_VS_CONN_F_TEMPLATE)
989
990 continue;
991
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;
997
998 case IP_VS_TCP_S_ESTABLISHED:
999 if (todrop_entry(cp))
1000 break;
1001 continue;
1002
1003 default:
1004 continue;
1005 }
1006 } else {
1007 if (!todrop_entry(cp))
1008 continue;
1009 }
1010
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);
1016 }
1017 }
1018 ct_write_unlock_bh(hash);
1019 }
1020}
1021
1022
1023
1024
1025
1026static void ip_vs_conn_flush(void)
1027{
1028 int idx;
1029 struct ip_vs_conn *cp;
1030
1031 flush_again:
1032 for (idx=0; idx<IP_VS_CONN_TAB_SIZE; idx++) {
1033
1034
1035
1036 ct_write_lock_bh(idx);
1037
1038 list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1039
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);
1045 }
1046 }
1047 ct_write_unlock_bh(idx);
1048 }
1049
1050
1051
1052 if (atomic_read(&ip_vs_conn_count) != 0) {
1053 schedule();
1054 goto flush_again;
1055 }
1056}
1057
1058
1059int __init ip_vs_conn_init(void)
1060{
1061 int idx;
1062
1063
1064
1065
1066 ip_vs_conn_tab = vmalloc(IP_VS_CONN_TAB_SIZE*sizeof(struct list_head));
1067 if (!ip_vs_conn_tab)
1068 return -ENOMEM;
1069
1070
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;
1077 }
1078
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));
1085
1086 for (idx = 0; idx < IP_VS_CONN_TAB_SIZE; idx++) {
1087 INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1088 }
1089
1090 for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
1091 rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);
1092 }
1093
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);
1096
1097
1098 get_random_bytes(&ip_vs_conn_rnd, sizeof(ip_vs_conn_rnd));
1099
1100 return 0;
1101}
1102
1103
1104void ip_vs_conn_cleanup(void)
1105{
1106
1107 ip_vs_conn_flush();
1108
1109
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);
1114}
1115