-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwb.c
executable file
·1890 lines (1782 loc) · 54.2 KB
/
mwb.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/stddef.h>
#include <linux/types.h>
#include <linux/ctype.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <net/netfilter/nf_conntrack.h>
#include <linux/rculist.h>
#include <linux/list.h>
#include <net/net_namespace.h>
#include <linux/jhash.h>
#include <linux/version.h>
/*for auto route*/
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <net/ip_fib.h>
#include <net/rtnetlink.h>
#include <net/route.h>
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[3]
#define LE_NIPQUAD(addr) \
((unsigned char *)&addr)[3], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[0]
#define MWB_LOG_LEVEL 2
#define MWB_LOG(level, fmt, ...) do { \
if ((level) <= MWB_LOG_LEVEL) { \
printk("*MWB* " fmt "\n", ##__VA_ARGS__); \
} \
} while (0)
#define MWB_LOG_IF(level, cond, fmt, ...) do { \
if ((level) <= MWB_LOG_LEVEL) { \
if (cond) { \
printk("*MWB* " fmt "\n", ##__VA_ARGS__); \
} \
} \
} while (0)
#define MWB_ASSERT(cond) BUG_ON(!(cond))
#define MWB_ASSERT_MSG(cond, fmt, ...) do { \
if (unlikely(!(cond))) { \
printk(fmt "\n", ##__VA_ARGS__); \
BUG(); \
} \
} while (0)
#define MWB_ERROR(...) MWB_LOG(0, ##__VA_ARGS__)
#define MWB_ERROR_IF(cond, ...) MWB_LOG_IF(0, cond, ##__VA_ARGS__)
#define MWB_WARN(...) MWB_LOG(1, ##__VA_ARGS__)
#define MWB_WARN_IF(cond, ...) MWB_LOG_IF(1, cond, ##__VA_ARGS__)
#define MWB_INFO(...) MWB_LOG(2, ##__VA_ARGS__)
#define MWB_INFO_IF(cond, ...) MWB_LOG_IF(2, cond, ##__VA_ARGS__)
#define MWB_DEBUG(...) MWB_LOG(3, ##__VA_ARGS__)
#define MWB_DEBUG_IF(cond, ...) MWB_LOG_IF(3, cond, ##__VA_ARGS__)
#define MWB_LINE_MASK_CHECK(mask, i) (mask & (1u << i))
#define MWB_LINE_MASK_SET(mask, i) ((void)(mask |= 1u << i))
#define MWB_LINE_MASK_CLR(mask, i) ((void)(mask &= ~(1u << i)))
#define MWB_LINE_MASK_FOR_EACH(i, mask) \
for ( ; (i = __builtin_ffs(mask) - 1) != -1; MWB_LINE_MASK_CLR(mask, i))
struct pair_info{
char sip[20];
char dip[20];
unsigned int mark;
char dst_dev[2][20];
}g_pair_info;
#define MLINES_MAX (8)
//每秒计算流量次数64*16=16*4*16=1024, COUNT_TIMES * 4 *MWB_RATE_CHECK_INTVL
#define COUNT_TIMES (64)
//产生流量估计需要的时间ms
#define MWB_RATE_CHECK_INTVL 16
//定期检查entry 是否超时间隔
#define CHECK_ENTRY_INTVL (2*60*HZ)
#define MWB_MAX_CHECK_ENTRY 256
//没有连接引用后,超时期限
#define MWB_ENTRY_TIMEOUT_MAX (10*HZ)
#define DEV_NAME_SIZE 20
struct mwb_cpu_dev_stats {
u64 rx_bytes;
u64 tx_bytes;
struct u64_stats_sync syncp;
};
struct line_info_st{
uint8_t mark;
char dev_name[DEV_NAME_SIZE];
struct net_device *dev;
struct mwb_cpu_dev_stats __percpu *stats;
unsigned long down_bandwidth;
unsigned long up_bandwidth;
unsigned long n;
u64 down_bytes;
unsigned int down[COUNT_TIMES];
u64 up_bytes;
unsigned int up[COUNT_TIMES];
unsigned int up_rate;
unsigned int down_rate;
};
struct mwb_mline_st {
uint8_t type;
uint8_t line_mask;
uint8_t line_alive_cnt;
spinlock_t line_lock;
struct timer_list timer;
unsigned long timestamp;
atomic_t last_chose_line_id;
struct line_info_st line_info[MLINES_MAX]; //0x1, 2, 3
};
#define MLINES_NONE (0)
struct mwb_mline_st mwb_lines;
uint8_t mwb_type = MLINES_NONE;
unsigned long g_line_change_jiffies = 0;
struct mwb_key_info{
unsigned int ipsaddr;
unsigned int ipdaddr;
};
struct mwb_entry{
struct hlist_node hlist;
struct list_head ct_list;
spinlock_t ct_list_lock;
struct rcu_head rcu;
int deleted;
atomic_t refcnt;
struct mwb_key_info mki;
unsigned int mark;
unsigned long timestamp;
unsigned long dst[IP_CT_DIR_MAX];
spinlock_t dst_lock[IP_CT_DIR_MAX];
uint8_t line_mask;
};
typedef int (*match_keys)( struct mwb_key_info *mki1, struct mwb_key_info *mki2);
#define MWB_HASH_SIZE 2048
struct ht_table {
struct hlist_head hhead;
spinlock_t lock;
};
struct HashTable{
struct ht_table *table;
int alloc_by_vmalloc;
unsigned int hash_mask;
unsigned int hash_salt;
struct kmem_cache *hcache;
int (*node_hash_key)(struct mwb_key_info *mki);
match_keys node_match_key;
spinlock_t lock;
struct timer_list ktimer;
atomic_t counter;
unsigned int entry_timeout;
unsigned int timer_max_check_cnt;
unsigned int timer_slot_check;
unsigned int timer_check_intvl;
};
struct HashTable *mwbHashTable = NULL;
#define LOADPOLICY_MARK 0x10
struct load_policy_t {
struct list_head mask_list;
unsigned int hash_salt;
unsigned int cnt;
spinlock_t lp_lock;
};
struct load_policy_t LoadPolicy;
#define LP_HASH_SIZE 32
struct load_policy_list_node {
struct list_head list;
struct rcu_head rcu;
//unsigned int mask_len;
unsigned int mask;
unsigned int hlist_node_cnt;
struct hlist_head table[LP_HASH_SIZE];
};
struct load_policy_hlist_node {
struct hlist_node hlist;
unsigned int network;
unsigned int mark;
struct rcu_head rcu;
};
static int lp_add_network(unsigned int network, unsigned int mask, unsigned int mark);
static int lp_del_network(unsigned int network, unsigned int mask);
static void mwb_entry_timeout_check(unsigned long data);
static unsigned int mwb_hash_salt(void);
static unsigned int mwb_ht_mask(void);
void me_ct_attach(struct nf_conn *ct, struct mwb_entry *me);
static int mwb_rate_handle(void);
static int mwb_iprule_del(int line_no);
static int mwb_iprule_set(int line_no);
static inline struct dst_entry *mwb_entry_dst(struct mwb_entry *me, int dir);
static inline bool ipv4_is_lgroup(__be32 addr)
{
return (addr & htonl(0x000000ff)) == htonl(0x000000ff);
}
int mwb_hash1(struct mwb_key_info *mki)
{
return jhash_1word(mki->ipsaddr, mwb_hash_salt()) & mwb_ht_mask();
}
int mwb_hash2(struct mwb_key_info *mki)
{
return jhash_2words(mki->ipsaddr, mki->ipdaddr, mwb_hash_salt()) & mwb_ht_mask();
}
int mwb_match_key1(struct mwb_key_info *mki1, struct mwb_key_info *mki2)
{
return mki1->ipsaddr == mki2->ipsaddr ;
}
int mwb_match_key2(struct mwb_key_info *mki1, struct mwb_key_info *mki2)
{
return ((mki1->ipdaddr ^ mki2->ipdaddr) | (mki1->ipsaddr ^ mki2->ipsaddr) ) == 0;
}
void ht_tbpool_destroy(struct HashTable *ht)
{
if(ht->alloc_by_vmalloc)
vfree(ht->table);
else
kfree(ht->table);
}
static int ht_tbpool_create(struct HashTable *ht, int slot_num)
{
uint32_t size = sizeof(*ht->table) * slot_num;
int i;
if(size <= KMALLOC_MAX_SIZE) {
ht->table = kmalloc(size, GFP_KERNEL);
ht->alloc_by_vmalloc = 0;
} else {
ht->table = vmalloc(size);
ht->alloc_by_vmalloc = 1;
}
if(!ht->table) {
MWB_ERROR("slot_num=%d,size=%u,ht_tbpool_create fail", slot_num, size);
return -1;
}
memset(ht->table, 0, size);
for (i = 0; i < slot_num; i++){
INIT_HLIST_HEAD(&ht->table[i].hhead);
spin_lock_init(&ht->table[i].lock);
}
MWB_INFO("slot_num=%d,size=%u,ht_tbpool_create success", slot_num, size);
return 0;
}
static struct HashTable *mwb_ht_init(void){
int slot_num;
struct HashTable *ht = NULL;
ht = (struct HashTable *)kzalloc(sizeof(struct HashTable), GFP_ATOMIC);
if (!ht){
MWB_ERROR(" mwbHashTable kzalloc fail");
return NULL;
}
slot_num = rounddown_pow_of_two(MWB_HASH_SIZE);
ht->hash_mask = slot_num -1;
if(ht_tbpool_create(ht, slot_num))
return NULL;
get_random_bytes(&ht->hash_salt, sizeof(ht->hash_salt));
ht->hcache = kmem_cache_create("mwb_hcache", sizeof(struct mwb_entry), 0,
SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
if(!ht->hcache){
MWB_ERROR("kmem_cache_create fail \n");
ht_tbpool_destroy(ht);
return NULL;
}
atomic_set(&ht->counter, 0);
spin_lock_init(&ht->lock);
ht->node_hash_key = mwb_hash2;
ht->node_match_key = mwb_match_key2;
ht->timer_max_check_cnt = MWB_MAX_CHECK_ENTRY;
ht->timer_slot_check = 0;
ht->entry_timeout = MWB_ENTRY_TIMEOUT_MAX;
ht->timer_check_intvl = CHECK_ENTRY_INTVL;
setup_timer(&ht->ktimer, mwb_entry_timeout_check, (unsigned long)ht);
mod_timer(&ht->ktimer, jiffies + ht->entry_timeout);
return ht;
}
static inline void mwb_lock(void)
{
spin_lock(&mwbHashTable->lock);
}
static inline void mwb_unlock(void)
{
spin_unlock(&mwbHashTable->lock);
}
static inline void mwb_ht_head_lock(struct hlist_head *head)
{
struct ht_table *tb = (struct ht_table *)head;
spin_lock(&tb->lock);
}
void mwb_ht_head_unlock(struct hlist_head *head)
{
struct ht_table *tb = (struct ht_table *)head;
spin_unlock(&tb->lock);
}
static inline void mwb_ht_inc(void)
{
atomic_inc(&mwbHashTable->counter);
}
static inline void mwb_ht_dec(void)
{
atomic_dec(&mwbHashTable->counter);
}
static inline void *mwb_malloc(int size)
{
return kmem_cache_alloc(mwbHashTable->hcache, GFP_ATOMIC);
}
static void mwb_free(void *p)
{
if(p){
kmem_cache_free(mwbHashTable->hcache, p);
mwb_ht_dec();
}
}
static inline unsigned int mwb_ht_mask(void)
{
return mwbHashTable->hash_mask;
}
static inline unsigned int mwb_hash_salt(void)
{
return mwbHashTable->hash_salt;
}
static inline int mwb_hash(struct mwb_key_info *mki){
return mwbHashTable->node_hash_key(mki);
}
static struct hlist_head *hash_head_get(struct mwb_key_info *mki)
{
int hash_index = mwb_hash(mki);
return &mwbHashTable->table[hash_index].hhead;
}
static inline int mwb_entry_match(struct mwb_key_info *mki1, struct mwb_key_info *mki2)
{
return mwbHashTable->node_match_key(mki1, mki2);
}
struct mwb_entry *mwb_entry_each_match(struct hlist_head *h, struct mwb_key_info *mki)
{
struct mwb_entry *entry;
hlist_for_each_entry_rcu(entry, h, hlist){
if(mwb_entry_match(&entry->mki, mki))
return entry;
}
return NULL;
}
struct mwb_entry *mwb_entry_find(struct mwb_key_info *mki)
{
struct hlist_head *h = hash_head_get(mki);
return mwb_entry_each_match(h, mki);
}
static struct mwb_entry *mwb_entry_create(void)
{
struct mwb_entry *entry = NULL;
entry = mwb_malloc(sizeof(*entry));
if (!entry){
MWB_WARN("wmb_malloc memory fail");
return NULL;
}
mwb_ht_inc();
memset(entry, 0, sizeof(struct mwb_entry));
atomic_set(&entry->refcnt, 0);
INIT_HLIST_NODE(&entry->hlist);
INIT_LIST_HEAD(&entry->ct_list);
spin_lock_init(&entry->ct_list_lock);
spin_lock_init(&entry->dst_lock[IP_CT_DIR_ORIGINAL]);
spin_lock_init(&entry->dst_lock[IP_CT_DIR_REPLY]);
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36))
init_rcu_head(&entry->rcu);
#else
INIT_RCU_HEAD(&entry->rcu);
#endif
entry->timestamp = jiffies;
return entry;
}
static struct mwb_entry *mwb_entry_add(struct mwb_key_info *mki, struct sk_buff *skb)
{
struct hlist_head *h;
struct mwb_entry *entry = NULL, *me = NULL;
MWB_ASSERT(skb->mark);
MWB_ASSERT(mki);
if(!(entry = mwb_entry_create()))
return NULL;
entry->mki = *mki;
entry->mark = skb->mark;
h = hash_head_get(mki);
mwb_ht_head_lock(h);
if((me = mwb_entry_each_match(h, mki))){
mwb_free(entry);
mwb_ht_head_unlock(h);
skb->mark = me->mark;
/*the same pair of sip and dip, but not the same ct , so this ct should be marked and attach, return me for sure*/
((struct nf_conn *)skb->nfct)->mark = me->mark;
//MWB_INFO("entry exist , mwb_entry_add return");
return me;
}
hlist_add_head_rcu(&entry->hlist, h);
mwb_ht_head_unlock(h);
return entry;
}
static inline void mwb_entry_dst_drop(struct mwb_entry *me, int dir)
{
if (me->dst[dir]) {
refdst_drop(me->dst[dir]);
me->dst[dir] = 0UL;
}
}
static void mwb_entry_dst_drop_all(struct mwb_entry *me)
{
mwb_entry_dst_drop(me, IP_CT_DIR_ORIGINAL);
mwb_entry_dst_drop(me, IP_CT_DIR_REPLY);
}
static void mwb_entry_free(struct rcu_head *rh)
{
struct mwb_entry *e = container_of(rh, struct mwb_entry, rcu);
mwb_entry_dst_drop_all(e);
mwb_free(e);
}
static void mwb_entry_detach_all_ct(struct mwb_entry *entry)
{
struct nf_conn *ct,*tmp;
list_for_each_entry_safe(ct, tmp, &entry->ct_list, list){
/*TODO:
if ct has free, here may be dangerous , so have to spin_lock to make sure list_del_init correct,
and then rcu_assign_pointer(mwb_ct_detach, NULL)
*/
list_del_init(&ct->list);
atomic_dec(&entry->refcnt);
ct->mwb_entry = NULL;
}
}
static void mwb_entry_del(struct hlist_head *h, struct mwb_entry *entry)
{
mwb_ht_head_lock(h);
/* 目前只有timer 一个地方有删除操作,暂时不需要判断deleted
if (entry->deleted){
mwb_ht_head_unlock(h);
return;
}
entry->deleted = 1;
*/
hlist_del_rcu(&entry->hlist);
mwb_ht_head_unlock(h);
/*here , maybe ct just attach entry this moment, so detach ct*/
spin_lock(&entry->ct_list_lock);
mwb_entry_detach_all_ct(entry);
/*set deleted flag, make sure ct cannot attch this entry*/
entry->deleted = 1;
spin_unlock(&entry->ct_list_lock);
/*here, entry->refcnt==0 is certainly*/
MWB_ASSERT(atomic_read(&entry->refcnt) == 0);
call_rcu(&entry->rcu, mwb_entry_free);
}
static void mwb_entry_timeout_check(unsigned long data)
{
int i, checked_cnt = 0, jiffies_intvl;
unsigned int check_times_around;
//unsigned long jiffies_fly;
struct mwb_entry *entry;
struct HashTable *ht = (struct HashTable *)data;
rcu_read_lock();
for (i = ht->timer_slot_check & ht->hash_mask; i < ht->hash_mask + 1; i++){
hlist_for_each_entry_rcu(entry, &ht->table[i].hhead, hlist){
/*
jiffies_fly = jiffies - entry->timestamp;
if(jiffies_fly > ht->entry_timeout){
mwb_entry_del(&ht->table[i].hhead, entry);
}
*/
if(atomic_read(&entry->refcnt) == 0 && time_after(jiffies, entry->timestamp + ht->entry_timeout)){
mwb_entry_del(&ht->table[i].hhead, entry);
}
checked_cnt++;
}
ht->timer_slot_check = i + 1;
if(checked_cnt >= ht->timer_max_check_cnt)
break;
}
rcu_read_unlock();
//TODO:
check_times_around = (atomic_read(&ht->counter) + ht->timer_max_check_cnt -1)/ht->timer_max_check_cnt;
if (!check_times_around)
check_times_around = 1;
MWB_INFO("----ht->counter=%d, hash_size=%u, max_check_cnt=%u, check_times_around=%d, timer_check_intvl=%u ,entry_timeout=%ds-----",
atomic_read(&ht->counter), ht->hash_mask+1, ht->timer_max_check_cnt, check_times_around, ht->timer_check_intvl/HZ, ht->entry_timeout/HZ);
if(ht->timer_slot_check == ht->hash_mask+1)
MWB_INFO("\n-----------------check around over--------------------\n");
jiffies_intvl = ht->timer_check_intvl/check_times_around;
mod_timer(&ht->ktimer, jiffies + (jiffies_intvl>10?jiffies_intvl:10));
}
/* make sure mwb_hook_fn and ct can not reference entry any more*/
void hash_table_cleanup(struct HashTable *ht){
int i;
struct mwb_entry *entry;
struct hlist_node *n;
for (i = 0; i < ht->hash_mask + 1; i++){
hlist_for_each_entry_safe(entry, n, &ht->table[i].hhead, hlist){
hlist_del_init(&entry->hlist);
spin_lock_bh(&entry->ct_list_lock);
mwb_entry_detach_all_ct(entry);
spin_unlock_bh(&entry->ct_list_lock);
MWB_ASSERT(atomic_read(&entry->refcnt) == 0);
//mwb_free(entry);
call_rcu(&entry->rcu, mwb_entry_free);/*1. maybe ct also reference this entry, 2. need to drop dst*/
}
}
}
static unsigned int mwb_get_dev_rate(unsigned int *arry)
{
int i;
unsigned int rate = 0;
for(i = 0; i < COUNT_TIMES; i++){
rate += arry[i];
}
return rate;
}
static unsigned int mwb_chose_routine_by_traffic(void)
{
int i, lid = 0, min_up_id = 0;
unsigned int min_down_occup_rate = ~0u, down_occup_rate/*, down_rate = 0*/;
unsigned int min_up_occup_rate = ~0u, up_occup_rate/*, up_rate = 0*/;
struct line_info_st *li;
uint8_t mask = mwb_lines.line_mask;
if(time_after(jiffies, mwb_lines.timestamp + (MWB_RATE_CHECK_INTVL >> 1))){
mwb_lines.timestamp = jiffies;
CHECK_ALL_LINE_RATE:
MWB_LINE_MASK_FOR_EACH(i, mask){
li = &mwb_lines.line_info[i];
//down_rate = mwb_get_dev_rate(li->down);
if(!li->down_rate){
lid = i;
goto chose_it;
}
//up_rate = mwb_get_dev_rate(li->up);
up_occup_rate = li->up_rate/li->down_bandwidth;
if (up_occup_rate > 850){
if (up_occup_rate < min_up_occup_rate){
min_up_occup_rate = up_occup_rate;
min_up_id = i;
}
continue;
}
down_occup_rate = li->down_rate/li->down_bandwidth;
if( down_occup_rate < min_down_occup_rate){
min_down_occup_rate = down_occup_rate;
lid = i;
}
}
if (min_down_occup_rate == ~0u)
lid = min_up_id;
chose_it:
atomic_set(&mwb_lines.last_chose_line_id, lid);
}else{
lid = atomic_read(&mwb_lines.last_chose_line_id);
if(!MWB_LINE_MASK_CHECK(mask, lid))
goto CHECK_ALL_LINE_RATE;
#if 0
MWB_LINE_MASK_CLR(mask, lid);
MWB_LINE_MASK_FOR_EACH(i, mask){
li = &mwb_lines.line_info[i];
//down_rate = mwb_get_dev_rate(li->down);
if(!li->down_rate)
lid = i;
}
#endif
}
return mwb_lines.line_info[lid].mark;
}
#if 0
/*
dev_get_stats make a bug:
dev_get_stats -->ndo_get_stats64 = igb_get_stats64 ->spin_lock/spin_unlock,
timer_fn will preempt when netlink handle igb_get_stats64 in a process context
*/
static struct mwb_dev_stat mwb_dev_stats_get(struct net_device **dev, char *devname)
{
struct mwb_dev_stat mds;
struct rtnl_link_stats64 stats;
if (*dev){
dev_get_stats(*dev, &stats);
}else {
*dev = dev_get_by_name(&init_net, devname);
if (*dev){
dev_get_stats(*dev, &stats);
dev_put(*dev);
}
}
mds.rx_bytes = stats.rx_bytes;
mds.tx_bytes = stats.tx_bytes;
return mds;
}
#endif
static void mwb_stats_seqlock_init(struct mwb_cpu_dev_stats *stats)
{
unsigned int cpu;
struct mwb_cpu_dev_stats *s;
if (!stats)
return;
for_each_possible_cpu(cpu) {
s = per_cpu_ptr(stats, cpu);
u64_stats_init(s->syncp);
}
}
static struct mwb_cpu_dev_stats mwb_dev_stats_get(struct mwb_cpu_dev_stats *stats)
{
struct mwb_cpu_dev_stats tmp, sum = {0};
unsigned int cpu;
if(!stats)
return sum;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36))
for_each_possible_cpu(cpu) {
unsigned int start;
const struct mwb_cpu_dev_stats *bstats = per_cpu_ptr(stats, cpu);
do {
start = u64_stats_fetch_begin_irq(&bstats->syncp);
memcpy(&tmp, bstats, sizeof(tmp));
} while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
#else
for_each_possible_cpu(cpu) {
unsigned int start;
const struct mwb_cpu_dev_stats *bstats = per_cpu_ptr(stats, cpu);
do {
start = u64_stats_fetch_begin(&bstats->syncp);
memcpy(&tmp, bstats, sizeof(tmp));
} while (u64_stats_fetch_retry(&bstats->syncp, start));
#endif
sum.tx_bytes += tmp.tx_bytes;
sum.rx_bytes += tmp.rx_bytes;
}
return sum;
}
static int mwb_rate_handle(void)
{
int i, idx;
struct mwb_cpu_dev_stats mds;
struct line_info_st *li;
uint8_t mask = mwb_lines.line_mask;
spin_lock_bh(&mwb_lines.line_lock);// rcu_read_lock();
MWB_LINE_MASK_FOR_EACH(i, mask){
li = &mwb_lines.line_info[i];
mds = mwb_dev_stats_get(li->stats);
idx = li->n & (COUNT_TIMES-1);
li->down_rate -= li->down[idx];
if (mds.rx_bytes > li->down_bytes){
li->down[idx] = mds.rx_bytes - li->down_bytes;
li->down_rate += li->down[idx];
}else{
li->down[idx]=0;
}
li->up_rate -= li->up[idx];
if (mds.tx_bytes > li->up_bytes){
li->up[idx] = mds.tx_bytes - li->up_bytes;
li->up_rate += li->up[idx];
}else{
li->up[idx]=0;
}
li->n ++;
li->down_bytes = mds.rx_bytes;
li->up_bytes = mds.tx_bytes;
}
spin_unlock_bh(&mwb_lines.line_lock);// rcu_read_unlock();
return 1;
}
void mwb_rate_timer_fun(unsigned long data)
{
#if 0
if(time_after(jiffies, mwb_lines.timestamp + MWB_RATE_CHECK_INTVL))
mwb_rate_handle();
mod_timer(&mwb_lines.timer, jiffies + HZ/COUNT_TIMES);
#endif
mwb_rate_handle();
mod_timer(&mwb_lines.timer, jiffies + 16);
}
static void mwb_lines_init(void)
{
memset(&mwb_lines, 0, sizeof(mwb_lines));
spin_lock_init(&mwb_lines.line_lock);
setup_timer(&mwb_lines.timer, mwb_rate_timer_fun, 0);
mwb_lines.timestamp = jiffies;
atomic_set(&mwb_lines.last_chose_line_id, 0);
mod_timer(&mwb_lines.timer, jiffies + 1);
}
int mwb_mlines_reset(int line_no, int line_alive, char *line_devname , unsigned int line_down_bw, unsigned int line_up_bw)
{
struct mwb_cpu_dev_stats __percpu *stats_free = NULL;
struct line_info_st *li;
if(line_no <0 || line_no > MLINES_MAX-1){
printk("line_no =%d out of range [0-%d]\n", line_no, MLINES_MAX-1);
return 0;
}
spin_lock_bh(&mwb_lines.line_lock);
li = &mwb_lines.line_info[line_no];
if (!line_alive){
if (MWB_LINE_MASK_CHECK(mwb_lines.line_mask, line_no)){
mwb_lines.line_alive_cnt--;
MWB_LINE_MASK_CLR(mwb_lines.line_mask, line_no);
stats_free = li->stats;
//li->stats = NULL;
memset(li, 0, sizeof(struct line_info_st));
mwb_iprule_del(line_no);
}
goto out;
}
li->mark = line_no + 1;
li->down_bandwidth = line_down_bw;
li->up_bandwidth = line_up_bw;
li->n = 0;
memset(li->up, 0, sizeof(li->up));
memset(li->down, 0, sizeof(li->down));
li->up_rate = 0;
li->down_rate = 0;
if(strlen(line_devname)){
memset(li->dev_name, 0 ,sizeof(li->dev_name));
strncpy(li->dev_name, line_devname, DEV_NAME_SIZE-1);
li->dev_name[DEV_NAME_SIZE-1] = '\0';
}
li->dev = dev_get_by_name(&init_net, li->dev_name);
if (!li->dev){
MWB_WARN("dev_get_by_name %s fail", li->dev_name);
goto out;
}
dev_put(li->dev);
stats_free = li->stats;
li->stats = alloc_percpu(struct mwb_cpu_dev_stats);
if(!li->stats){
MWB_WARN("alloc_percpu stats %s fail", li->dev_name);
goto out;
}
mwb_stats_seqlock_init(li->stats);
if (MWB_LINE_MASK_CHECK(mwb_lines.line_mask, line_no))
goto out;
MWB_LINE_MASK_SET(mwb_lines.line_mask, line_no);
mwb_iprule_set(line_no);
mwb_lines.line_alive_cnt++;
g_line_change_jiffies = jiffies;
out:
spin_unlock_bh(&mwb_lines.line_lock);
if(stats_free){
synchronize_net();
free_percpu(stats_free);
}
return 0;
}
static ssize_t mline_sysfs_attr_show(
struct module_attribute *mattr,
struct module_kobject *mod,
char *buf)
{
int i, th = 0;
ssize_t ret = 0;
struct line_info_st *li;
uint8_t mask = mwb_lines.line_mask;
struct load_policy_list_node *pos, *tmp;
struct load_policy_hlist_node *net_entry;
struct hlist_node *n;
ret = sprintf(buf, "last find:sip=%s, dip=%s, mark=%u, dst_dev =(%s)->(%s) \n", g_pair_info.sip, g_pair_info.dip, g_pair_info.mark, g_pair_info.dst_dev[0], g_pair_info.dst_dev[1]);
ret += sprintf(buf + ret, "[type:%d] [line_flag:%02x][live_cnt:%d]\n", mwb_lines.type, mwb_lines.line_mask, mwb_lines.line_alive_cnt);
MWB_LINE_MASK_FOR_EACH(i, mask){
li = &mwb_lines.line_info[i];
ret += sprintf(buf + ret, "\t i =%d, mark=%x, devname=%s, down_bw=%luKB, up_bw=%luKB, down_rate =%u,%u bytes, up_rate =%u,%u bytes\n", i
, li->mark, li->dev_name, li->down_bandwidth, li->up_bandwidth, li->down_rate, mwb_get_dev_rate(li->down), li->up_rate, mwb_get_dev_rate(li->up));
}
ret += sprintf(buf + ret, "=======network cnt=%u============\n", LoadPolicy.cnt);
list_for_each_entry_safe(pos, tmp, &LoadPolicy.mask_list, list){
for (i = 0; i < sizeof(pos->table)/sizeof(pos->table[0]); i++){
hlist_for_each_entry_safe(net_entry, n, &pos->table[i], hlist){
ret += sprintf(buf + ret, "%d(hash_id=%d)\t%u.%u.%u.%u/%u.%u.%u.%u\tmark=%u\n", ++th, i
, LE_NIPQUAD(net_entry->network), LE_NIPQUAD(pos->mask), net_entry->mark);
}
}
ret += sprintf(buf + ret, "------------------\n");
}
return ret;
}
static int ip_str_to_num(const char *ipstr, unsigned int *ip)
{
unsigned int a,b,c,d;
char tmp;
if (sscanf(ipstr, "%u.%u.%u.%u %c", &a, &b, &c, &d, &tmp) != 4 ||
a > 255 || b > 255 || c > 255 || d > 255) {
*ip = 0;
return -1;
}
*ip = (a << 24) | (b << 16) | (c << 8) | d;
return 0;
}
static ssize_t mline_sysfs_attr_store(
struct module_attribute *mattr,
struct module_kobject *mod,
const char *buf,
size_t count)
{
int line_no, line_alive, mark, type;
unsigned int line_down_bw, line_up_bw;
char line_devname[20];
char nw_op[8];
char network_str[20], sip_str[20], dip_str[20];
char mask_str[20];
char tmp;
unsigned int network, mask, sip, dip;
struct mwb_key_info mki;
struct mwb_entry *me;
memset(line_devname, 0, sizeof(line_devname));
memset(network_str, 0, sizeof(network_str));
memset(mask_str, 0, sizeof(mask_str));
//"line_no,line_alive,devname, daikuan; ; ; ;"
printk("count =%lu ,buf = %s \n", count, buf);
if (strncmp(buf, "type", strlen("type")) == 0){
if(sscanf(buf, "type %d %c", &type, &tmp) != 1){
MWB_ERROR("like: type 1");
return count;
}
mwb_lines.type = type;
return count;
}
if (strncmp(buf, "find", strlen("find")) == 0){
memset(sip_str, 0, sizeof(sip_str));
memset(dip_str, 0, sizeof(dip_str));
if(sscanf(buf, "find %s %s %c", sip_str, dip_str, &tmp) != 2){
MWB_ERROR("error :find sip = %s , dip=%s", sip_str, dip_str);
MWB_ERROR("like: find 192.168.1.2 8.8.8.8");
return count;
}
if(ip_str_to_num(sip_str, &sip) || ip_str_to_num(dip_str, &dip)){
MWB_ERROR("error :find sip = %s , dip=%s", sip_str, dip_str);
return count;
}
mki.ipsaddr = htonl(sip);
mki.ipdaddr = htonl(dip);
memset(&g_pair_info, 0, sizeof(g_pair_info));
rcu_read_lock();
me = mwb_entry_find(&mki);
if (me){
g_pair_info.mark = me->mark;
if(mwb_entry_dst(me, 0))
strcpy(g_pair_info.dst_dev[0], mwb_entry_dst(me, 0)->dev->name);
if(mwb_entry_dst(me, 1))
strcpy(g_pair_info.dst_dev[1], mwb_entry_dst(me, 1)->dev->name);
MWB_INFO("yes find %s -> %s ,mark =%u, dev: (%s) ->(%s) .", sip_str, dip_str, me->mark, g_pair_info.dst_dev[0], g_pair_info.dst_dev[1]);
}
rcu_read_unlock();
strcpy(g_pair_info.sip, sip_str);
strcpy(g_pair_info.dip, dip_str);
return count;
}
if (strncmp(buf, "network", strlen("network")) == 0){
/*like: network [add|del] 192.168.1.0 255.255.255.0*/
if(sscanf(buf, "network %s %s %s %d %c", nw_op, network_str, mask_str, &mark, &tmp) != 4){
MWB_ERROR("nw_op =%s, network_str=%s, mask_str=%s, mark=%d", nw_op, network_str, mask_str, mark);
MWB_ERROR("like: network [add|del] 192.168.1.0 255.255.255.0 1");
return count;
}
if(ip_str_to_num(network_str, &network) || ip_str_to_num(mask_str, &mask) || mark <1 || mark > MLINES_MAX){
MWB_ERROR("nw_op =%s, network_str=%s, mask_str=%s, mark=%d", nw_op, network_str, mask_str, mark);
return count;
}else{
if(((mask-1) | mask) != 0xFFFFFFFF){
MWB_ERROR("it is invalidate mask_str =%s, mask=%u\n", mask_str, mask);
return count;
}
spin_lock(&LoadPolicy.lp_lock);
if (strcmp(nw_op, "add") == 0)
lp_add_network(network, mask, mark|LOADPOLICY_MARK);
else if (strcmp(nw_op, "del") == 0)
lp_del_network(network, mask);
else
MWB_ERROR("nw_op =%s, must be 'add' or 'del' ", nw_op);
spin_unlock(&LoadPolicy.lp_lock);
}
}else{
sscanf(buf, "%d %d %19s %u %u", &line_no, &line_alive, line_devname, &line_down_bw, &line_up_bw);
printk("kernel :%d,%d,%s,%u,%u\n", line_no, line_alive, line_devname, line_down_bw, line_up_bw);
mwb_mlines_reset(line_no, line_alive, line_devname ,line_down_bw, line_up_bw);
}
return count;
}
static struct module_attribute mline_sysfs_attr =
__ATTR(mline, 0644, mline_sysfs_attr_show, mline_sysfs_attr_store);
static int mline_sysfs_register(void)
{
return sysfs_create_file(&THIS_MODULE->mkobj.kobj, &mline_sysfs_attr.attr);
}
static void mline_sysfs_unregister(void)
{
return sysfs_remove_file(&THIS_MODULE->mkobj.kobj, &mline_sysfs_attr.attr);
}
static inline unsigned int check_line_alive_by_mark(unsigned int mark)
{
MWB_ASSERT(mark);
return mwb_lines.line_mask & (1u << (mark-1));
}
static void mwb_key_info_get(struct sk_buff *skb, struct mwb_key_info *mki)
{
struct iphdr *iph;
iph = ip_hdr(skb);
mki->ipsaddr = iph->saddr;
mki->ipdaddr = iph->daddr;
}
static void LoadPolicy_init(void)
{
INIT_LIST_HEAD(&LoadPolicy.mask_list);
get_random_bytes(&LoadPolicy.hash_salt, sizeof(LoadPolicy.hash_salt));
LoadPolicy.cnt = 0;
spin_lock_init(&LoadPolicy.lp_lock);
}
static inline void lp_network_entry_inc(void)
{
LoadPolicy.cnt++;
}
static inline void lp_network_entry_dec(void)
{
LoadPolicy.cnt--;
}
static inline unsigned int lp_hash(unsigned int net_key)
{
return jhash_1word(net_key, LoadPolicy.hash_salt) & (LP_HASH_SIZE-1);
}
static void lp_hlist_node_free(struct load_policy_hlist_node *hnode)
{
kfree(hnode);
}
void lp_list_node_free(struct load_policy_list_node *node)
{
MWB_ASSERT(node->hlist_node_cnt == 0);
kfree(node);
}
static void lp_hlist_node_free_rcu(struct rcu_head *rh)
{
struct load_policy_hlist_node *hnode = container_of(rh, struct load_policy_hlist_node, rcu);
lp_hlist_node_free(hnode);
}
static void lp_list_node_free_rcu(struct rcu_head *rh)
{
struct load_policy_list_node *node = container_of(rh, struct load_policy_list_node, rcu);
lp_list_node_free(node);
}
static inline void lp_list_cnt_inc(struct load_policy_list_node *node)
{
lp_network_entry_inc();
node->hlist_node_cnt++;
}
static void lp_list_cnt_dec(struct load_policy_list_node *node)
{
lp_network_entry_dec();
if(--node->hlist_node_cnt == 0){
list_del_init(&node->list);