forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netif.c
4601 lines (3952 loc) · 145 KB
/
netif.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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include "dpdk.h"
#include "common.h"
#include "netif.h"
#include "netif_addr.h"
#include "vlan.h"
#include "ctrl.h"
#include "list.h"
#include "kni.h"
#include <rte_version.h>
#include "conf/netif.h"
#include "timer.h"
#include "parser/parser.h"
#define NETIF_PKTPOOL_NB_MBUF_DEF 65535
#define NETIF_PKTPOOL_NB_MBUF_MIN 1023
#define NETIF_PKTPOOL_NB_MBUF_MAX 134217727
static int netif_pktpool_nb_mbuf = NETIF_PKTPOOL_NB_MBUF_DEF;
#define NETIF_PKTPOOL_MBUF_CACHE_DEF 256
#define NETIF_PKTPOOL_MBUF_CACHE_MIN 32
#define NETIF_PKTPOOL_MBUF_CACHE_MAX 8192
static int netif_pktpool_mbuf_cache = NETIF_PKTPOOL_MBUF_CACHE_DEF;
#define NETIF_NB_RX_DESC_DEF 256
#define NETIF_NB_RX_DESC_MIN 16
#define NETIF_NB_RX_DESC_MAX 8192
#define NETIF_NB_TX_DESC_DEF 512
#define NETIF_NB_TX_DESC_MIN 16
#define NETIF_NB_TX_DESC_MAX 8192
#define NETIF_PKT_PREFETCH_OFFSET 3
#define NETIF_ISOL_RXQ_RING_SZ_DEF 1048576 // 1M bytes
/* physical nic id = phy_pid_base + index */
static portid_t phy_pid_base = 0;
static portid_t phy_pid_end = -1; // not inclusive
/* bond device id = bond_pid_base + index */
static portid_t bond_pid_base = -1;
static portid_t bond_pid_end = -1; // not inclusive
static portid_t port_id_end = 0;
static uint8_t g_nports;
static uint64_t cycles_per_sec;
#define NETIF_BOND_MODE_DEF BONDING_MODE_ROUND_ROBIN
struct port_conf_stream {
int port_id;
char name[32];
char kni_name[32];
int rx_queue_nb;
int rx_desc_nb;
char rss[32];
int tx_queue_nb;
int tx_desc_nb;
bool promisc_mode;
struct list_head port_list_node;
};
struct bond_conf_stream {
int port_id;
char name[32];
char kni_name[32];
int mode;
char primary[32];
char slaves[NETIF_MAX_BOND_SLAVES][32];
struct list_head bond_list_node;
};
struct queue_conf_stream {
char port_name[32];
int rx_queues[NETIF_MAX_QUEUES];
int tx_queues[NETIF_MAX_QUEUES];
int isol_rxq_lcore_ids[NETIF_MAX_QUEUES];
int isol_rxq_ring_sz;
struct list_head queue_list_node;
};
struct worker_conf_stream {
int cpu_id;
char name[32];
char type[32];
struct list_head port_list;
struct list_head worker_list_node;
};
static struct list_head port_list; /* device configurations from cfgfile */
static struct list_head bond_list; /* bonding configurations from cfgfile */
static struct list_head worker_list; /* lcore configurations from cfgfile */
#define NETIF_PORT_TABLE_BITS 8
#define NETIF_PORT_TABLE_BUCKETS (1 << NETIF_PORT_TABLE_BITS)
#define NETIF_PORT_TABLE_MASK (NETIF_PORT_TABLE_BUCKETS - 1)
static struct list_head port_tab[NETIF_PORT_TABLE_BUCKETS]; /* hashed by id */
static struct list_head port_ntab[NETIF_PORT_TABLE_BUCKETS]; /* hashed by name */
/* Note: Lockless, NIC can only be registered on initialization stage and
* unregistered on cleanup stage
*/
static inline struct port_conf_stream *get_port_conf_stream(const char *name)
{
struct port_conf_stream *current_cfg;
list_for_each_entry(current_cfg, &port_list, port_list_node) {
if (!strcmp(name, current_cfg->name))
return current_cfg;
}
return NULL;
}
static void netif_defs_handler(vector_t tokens)
{
struct port_conf_stream *port_cfg, *port_cfg_next;
struct bond_conf_stream *bond_cfg, *bond_cfg_next;
list_for_each_entry_safe(port_cfg, port_cfg_next, &port_list, port_list_node) {
list_del(&port_cfg->port_list_node);
rte_free(port_cfg);
}
list_for_each_entry_safe(bond_cfg, bond_cfg_next, &bond_list, bond_list_node) {
list_del(&bond_cfg->bond_list_node);
rte_free(bond_cfg);
}
}
static void pktpool_size_handler(vector_t tokens)
{
char *str = set_value(tokens);
int pktpool_size;
assert(str);
pktpool_size = atoi(str);
if (pktpool_size < NETIF_PKTPOOL_NB_MBUF_MIN ||
pktpool_size > NETIF_PKTPOOL_NB_MBUF_MAX) {
RTE_LOG(WARNING, NETIF, "invalid pktpool_size %s, using default %d\n",
str, NETIF_PKTPOOL_NB_MBUF_DEF);
netif_pktpool_nb_mbuf = NETIF_PKTPOOL_NB_MBUF_DEF;
} else {
is_power2(pktpool_size, 1, &pktpool_size);
RTE_LOG(INFO, NETIF, "pktpool_size = %d (round to 2^n-1)\n", pktpool_size - 1);
netif_pktpool_nb_mbuf = pktpool_size - 1;
}
FREE_PTR(str);
}
static void pktpool_cache_handler(vector_t tokens)
{
char *str = set_value(tokens);
int cache_size;
assert(str);
cache_size = atoi(str);
if (cache_size < NETIF_PKTPOOL_MBUF_CACHE_MIN ||
cache_size > NETIF_PKTPOOL_MBUF_CACHE_MAX) {
RTE_LOG(WARNING, NETIF, "invalid pktpool_cache_size %s, using default %d\n",
str, NETIF_PKTPOOL_MBUF_CACHE_DEF);
netif_pktpool_mbuf_cache = NETIF_PKTPOOL_MBUF_CACHE_DEF;
} else {
is_power2(cache_size, 0, &cache_size);
RTE_LOG(INFO, NETIF, "pktpool_cache_size = %d (round to 2^n)\n", cache_size);
netif_pktpool_mbuf_cache = cache_size;
}
FREE_PTR(str);
}
static void device_handler(vector_t tokens)
{
assert(VECTOR_SIZE(tokens) >= 1);
char *str;
struct port_conf_stream *port_cfg =
rte_zmalloc(NULL, sizeof(struct port_conf_stream), RTE_CACHE_LINE_SIZE);
if (!port_cfg) {
RTE_LOG(ERR, NETIF, "%s: no memory\n", __func__);
return;
}
port_cfg->port_id = -1;
str = VECTOR_SLOT(tokens, 1);
RTE_LOG(INFO, NETIF, "netif device config: %s\n", str);
strncpy(port_cfg->name, str, sizeof(port_cfg->name));
port_cfg->rx_queue_nb = -1;
port_cfg->tx_queue_nb = -1;
port_cfg->rx_desc_nb = NETIF_NB_RX_DESC_DEF;
port_cfg->tx_desc_nb = NETIF_NB_TX_DESC_DEF;
port_cfg->promisc_mode = false;
strncpy(port_cfg->rss, "tcp", sizeof(port_cfg->rss));
list_add(&port_cfg->port_list_node, &port_list);
}
static void rx_queue_number_handler(vector_t tokens)
{
char *str = set_value(tokens);
int rx_queues;
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
assert(str);
rx_queues = atoi(str);
if (rx_queues < 0 || rx_queues > NETIF_MAX_QUEUES) {
RTE_LOG(WARNING, NETIF, "invalid %s:rx_queue_number %s, using default %d\n",
current_device->name, str, NETIF_MAX_QUEUES);
current_device->rx_queue_nb = NETIF_MAX_QUEUES;
} else {
RTE_LOG(WARNING, NETIF, "%s:rx_queue_number = %d\n",
current_device->name, rx_queues);
current_device->rx_queue_nb = rx_queues;
}
FREE_PTR(str);
}
static void rx_desc_nb_handler(vector_t tokens)
{
char *str = set_value(tokens);
int desc_nb;
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
assert(str);
desc_nb = atoi(str);
if (desc_nb < NETIF_NB_RX_DESC_MIN || desc_nb > NETIF_NB_RX_DESC_MAX) {
RTE_LOG(WARNING, NETIF, "invalid %s:nb_rx_desc %s, using default %d\n",
current_device->name, str, NETIF_NB_RX_DESC_DEF);
current_device->rx_desc_nb = NETIF_NB_RX_DESC_DEF;
} else {
is_power2(desc_nb, 0, &desc_nb);
RTE_LOG(INFO, NETIF, "%s:nb_rx_desc = %d (round to 2^n)\n",
current_device->name, desc_nb);
current_device->rx_desc_nb = desc_nb;
}
FREE_PTR(str);
}
static void rss_handler(vector_t tokens)
{
char *str = set_value(tokens);
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
assert(str);
if (!strcmp(str, "tcp") || !strcmp(str, "ip")) {
RTE_LOG(INFO, NETIF, "%s:rss = %s\n", current_device->name, str);
strncpy(current_device->rss, str, sizeof(current_device->rss));
} else {
RTE_LOG(WARNING, NETIF, "invalid %s:rss %s, using default rss_tcp\n",
current_device->name, str);
strncpy(current_device->rss, "tcp", sizeof(current_device->rss));
}
FREE_PTR(str);
}
static void tx_queue_number_handler(vector_t tokens)
{
char *str = set_value(tokens);
int tx_queues;
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
assert(str);
tx_queues = atoi(str);
if (tx_queues < 0 || tx_queues > NETIF_MAX_QUEUES) {
RTE_LOG(WARNING, NETIF, "invalid %s:tx_queue_number %s, using default %d\n",
current_device->name, str, NETIF_MAX_QUEUES);
current_device->tx_queue_nb = NETIF_MAX_QUEUES;
} else {
RTE_LOG(INFO, NETIF, "%s:tx_queue_number = %d\n",
current_device->name, tx_queues);
current_device->tx_queue_nb = tx_queues;
}
FREE_PTR(str);
}
static void tx_desc_nb_handler(vector_t tokens)
{
char *str = set_value(tokens);
int desc_nb;
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
assert(str);
desc_nb = atoi(str);
if (desc_nb < NETIF_NB_TX_DESC_MIN || desc_nb > NETIF_NB_TX_DESC_MAX) {
RTE_LOG(WARNING, NETIF, "invalid nb_tx_desc %s, using default %d\n",
str, NETIF_NB_TX_DESC_DEF);
current_device->tx_desc_nb = NETIF_NB_TX_DESC_DEF;
} else {
is_power2(desc_nb, 0, &desc_nb);
RTE_LOG(INFO, NETIF, "%s:nb_tx_desc = %d (round to 2^n)\n",
current_device->name, desc_nb);
current_device->tx_desc_nb = desc_nb;
}
FREE_PTR(str);
}
static void promisc_mode_handler(vector_t tokens)
{
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
current_device->promisc_mode = true;
}
static void kni_name_handler(vector_t tokens)
{
char *str = set_value(tokens);
struct port_conf_stream *current_device = list_entry(port_list.next,
struct port_conf_stream, port_list_node);
assert(str);
RTE_LOG(INFO, NETIF, "%s: kni_name = %s\n",current_device->name, str);
strncpy(current_device->kni_name, str, sizeof(current_device->kni_name));
FREE_PTR(str);
}
static void bonding_handler(vector_t tokens)
{
assert(VECTOR_SIZE(tokens) >= 1);
char *str;
struct bond_conf_stream *bond_cfg =
rte_zmalloc(NULL, sizeof(struct bond_conf_stream), RTE_CACHE_LINE_SIZE);
if (!bond_cfg) {
RTE_LOG(ERR, NETIF, "%s: no memory\n", __func__);
return;
}
bond_cfg->port_id = -1;
str = VECTOR_SLOT(tokens, 1);
RTE_LOG(INFO, NETIF, "netif bonding config: %s\n", str);
strncpy(bond_cfg->name, str, sizeof(bond_cfg->name));
bond_cfg->mode = NETIF_BOND_MODE_DEF;
list_add(&bond_cfg->bond_list_node, &bond_list);
}
static void bonding_mode_handler(vector_t tokens)
{
char *str = set_value(tokens);
int mode;
struct bond_conf_stream *current_bond = list_entry(bond_list.next,
struct bond_conf_stream, bond_list_node);
assert(str);
mode = atoi(str);
switch (mode) {
case BONDING_MODE_ROUND_ROBIN:
case BONDING_MODE_ACTIVE_BACKUP:
case BONDING_MODE_BALANCE:
case BONDING_MODE_BROADCAST:
case BONDING_MODE_8023AD:
case BONDING_MODE_TLB:
case BONDING_MODE_ALB:
RTE_LOG(INFO, NETIF, "bonding %s:mode=%d\n", current_bond->name, mode);
current_bond->mode = mode;
break;
default:
RTE_LOG(WARNING, NETIF, "invalid bonding %s:mode %d, using default %d\n",
current_bond->name, mode, NETIF_BOND_MODE_DEF);
current_bond->mode = NETIF_BOND_MODE_DEF;
}
FREE_PTR(str);
}
static void bonding_slave_handler(vector_t tokens)
{
int ii;
char *str = set_value(tokens);
struct bond_conf_stream *current_bond = list_entry(bond_list.next,
struct bond_conf_stream, bond_list_node);
assert(str);
if (get_port_conf_stream(str)) {
for (ii = 0; ii < NETIF_MAX_BOND_SLAVES; ii++) {
if (ii >= NETIF_MAX_BOND_SLAVES) {
RTE_LOG(ERR, NETIF, "bonding %s's slaves exceed maximum supported: %d",
current_bond->name, NETIF_MAX_BOND_SLAVES);
break;
}
if (!current_bond->slaves[ii][0]) {
strncpy(current_bond->slaves[ii], str, sizeof(current_bond->slaves[ii]));
RTE_LOG(INFO, NETIF, "bonding %s:slave%d=%s\n", current_bond->name, ii, str);
break;
}
}
} else {
RTE_LOG(ERR, NETIF, "invalid bonding %s:salve %s, skip ...\n",
current_bond->name, str);
}
FREE_PTR(str);
}
static void bonding_primary_handler(vector_t tokens)
{
char *str = set_value(tokens);
struct bond_conf_stream *current_bond = list_entry(bond_list.next,
struct bond_conf_stream, bond_list_node);
assert(str);
if (get_port_conf_stream(str)) {
RTE_LOG(INFO, NETIF, "bonding %s:primary=%s\n", current_bond->name, str);
strncpy(current_bond->primary, str, sizeof(current_bond->primary));
} else {
RTE_LOG(WARNING, NETIF, "invalid bonding %s:primary %s, skip ...\n",
current_bond->name, str);
}
FREE_PTR(str);
}
static void bonding_kni_name_handler(vector_t tokens)
{
char *str = set_value(tokens);
struct bond_conf_stream *current_bond = list_entry(bond_list.next,
struct bond_conf_stream, bond_list_node);
assert(str);
RTE_LOG(INFO, NETIF, "bonding %s:kni_name=%s\n", current_bond->name, str);
strncpy(current_bond->kni_name, str, sizeof(current_bond->kni_name));
FREE_PTR(str);
}
static void worker_defs_handler(vector_t tokens)
{
struct worker_conf_stream *worker_cfg, *worker_cfg_next;
struct queue_conf_stream *queue_cfg, *queue_cfg_next;
list_for_each_entry_safe(worker_cfg, worker_cfg_next, &worker_list,
worker_list_node) {
list_del(&worker_cfg->worker_list_node);
list_for_each_entry_safe(queue_cfg, queue_cfg_next, &worker_cfg->port_list,
queue_list_node) {
list_del(&queue_cfg->queue_list_node);
rte_free(queue_cfg);
}
rte_free(worker_cfg);
}
}
static void worker_handler(vector_t tokens)
{
assert(VECTOR_SIZE(tokens) >= 1);
char *str;
struct worker_conf_stream *worker_cfg = rte_malloc(NULL,
sizeof(struct worker_conf_stream), RTE_CACHE_LINE_SIZE);
if (!worker_cfg) {
RTE_LOG(ERR, NETIF, "%s: no memory\n", __func__);
return;
}
INIT_LIST_HEAD(&worker_cfg->port_list);
str = VECTOR_SLOT(tokens, 1);
RTE_LOG(INFO, NETIF, "netif worker config: %s\n", str);
strncpy(worker_cfg->name, str, sizeof(worker_cfg->name));
list_add(&worker_cfg->worker_list_node, &worker_list);
}
static void worker_type_handler(vector_t tokens)
{
char *str = set_value(tokens);
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
assert(str);
if (!strcmp(str, "master") || !strcmp(str, "slave")) {
RTE_LOG(INFO, NETIF, "%s:type = %s\n", current_worker->name, str);
strncpy(current_worker->type, str, sizeof(current_worker->type));
} else {
RTE_LOG(WARNING, NETIF, "invalid %s:type %s, using default %s\n",
current_worker->name, str, "slave");
strncpy(current_worker->type, "slave", sizeof(current_worker->type));
}
FREE_PTR(str);
}
static void cpu_id_handler(vector_t tokens)
{
char *str = set_value(tokens);
int cpu_id;
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
assert(str);
if (strspn(str, "0123456789") != strlen(str)) {
RTE_LOG(WARNING, NETIF, "invalid %s:cpu_id %s, using default 0\n",
current_worker->name, str);
current_worker->cpu_id = 0;
} else {
cpu_id = atoi(str);
RTE_LOG(INFO, NETIF, "%s:cpu_id = %d\n", current_worker->name, cpu_id);
current_worker->cpu_id = cpu_id;
}
FREE_PTR(str);
}
static void worker_port_handler(vector_t tokens)
{
assert(VECTOR_SIZE(tokens) >= 1);
char *str;
int ii;
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
struct queue_conf_stream *queue_cfg = rte_malloc(NULL,
sizeof(struct queue_conf_stream), RTE_CACHE_LINE_SIZE);
if (!queue_cfg) {
RTE_LOG(ERR, NETIF, "%s: no memory\n", __func__);
return;
}
for (ii = 0; ii < NETIF_MAX_QUEUES; ii++)
queue_cfg->isol_rxq_lcore_ids[ii] = NETIF_LCORE_ID_INVALID;
queue_cfg->isol_rxq_ring_sz = NETIF_ISOL_RXQ_RING_SZ_DEF;
str = VECTOR_SLOT(tokens, 1);
RTE_LOG(INFO, NETIF, "worker %s:%s queue config\n", current_worker->name, str);
strncpy(queue_cfg->port_name, str, sizeof(queue_cfg->port_name));
list_add(&queue_cfg->queue_list_node, ¤t_worker->port_list);
}
static void rx_queue_ids_handler(vector_t tokens)
{
int ii, qid;
char *str;
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
struct queue_conf_stream *current_port = list_entry(current_worker->port_list.next,
struct queue_conf_stream, queue_list_node);
for (ii = 0; ii < VECTOR_SIZE(tokens) - 1; ii++) {
str = VECTOR_SLOT(tokens, ii + 1);
qid = atoi(str);
if (qid < 0 || qid >= NETIF_MAX_QUEUES) {
RTE_LOG(WARNING, NETIF, "invalid worker %s:%s rx_queue_id %s, using "
"default 0\n", current_worker->name, current_port->port_name, str);
current_port->rx_queues[ii] = 0; /* using default worker config array */
} else {
RTE_LOG(WARNING, NETIF, "worker %s:%s rx_queue_id += %d\n",
current_worker->name, current_port->port_name, qid);
current_port->rx_queues[ii] = qid;
}
}
for ( ; ii < NETIF_MAX_QUEUES; ii++) /* unused space */
current_port->rx_queues[ii] = NETIF_MAX_QUEUES;
}
static void tx_queue_ids_handler(vector_t tokens)
{
int ii, qid;
char *str;
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
struct queue_conf_stream *current_port = list_entry(current_worker->port_list.next,
struct queue_conf_stream, queue_list_node);
for (ii = 0; ii < VECTOR_SIZE(tokens) - 1; ii++) {
str = VECTOR_SLOT(tokens, ii + 1);
qid = atoi(str);
if (qid < 0 || qid >= NETIF_MAX_QUEUES) {
RTE_LOG(WARNING, NETIF, "invalid worker %s:%s tx_queue_id %s, uisng "
"default 0\n", current_worker->name, current_port->port_name, str);
current_port->tx_queues[ii] = 0; /* using default worker config array */
} else {
RTE_LOG(WARNING, NETIF, "worker %s:%s tx_queue_id += %d\n",
current_worker->name, current_port->port_name, qid);
current_port->tx_queues[ii] = qid;
}
}
for ( ; ii < NETIF_MAX_QUEUES; ii++) /* unused space */
current_port->tx_queues[ii] = NETIF_MAX_QUEUES;
}
static void isol_rx_cpu_ids_handler(vector_t tokens)
{
int ii, cid;
char *str = set_value(tokens);
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
struct queue_conf_stream *current_port = list_entry(current_worker->port_list.next,
struct queue_conf_stream, queue_list_node);
for (ii = 0; ii < VECTOR_SIZE(tokens) - 1; ii++) {
str = VECTOR_SLOT(tokens, ii + 1);
cid = atoi(str);
if (cid <= 0 || cid >= NETIF_MAX_LCORES) {
RTE_LOG(WARNING, NETIF, "invalid worker %s:%s:isol_rx_cpu_ids[%d] %s\n",
current_worker->name, current_port->port_name, ii, str);
current_port->isol_rxq_lcore_ids[ii] = NETIF_LCORE_ID_INVALID;
} else {
RTE_LOG(INFO, NETIF, "worker %s:%s:isol_rx_cpu_ids[%d] = %d\n",
current_worker->name, current_port->port_name, ii, cid);
current_port->isol_rxq_lcore_ids[ii] = cid;
}
}
}
static void isol_rxq_ring_sz_handler(vector_t tokens)
{
int isol_rxq_ring_sz;
char *str = set_value(tokens);
struct worker_conf_stream *current_worker = list_entry(worker_list.next,
struct worker_conf_stream, worker_list_node);
struct queue_conf_stream *current_port = list_entry(current_worker->port_list.next,
struct queue_conf_stream, queue_list_node);
assert(str);
if (strspn(str, "0123456789") != strlen(str)) {
RTE_LOG(WARNING, NETIF, "invalid worker %s:%s:isol_rxq_ring_sz %s,"
" using default %d\n", current_worker->name, current_port->port_name,
str, NETIF_ISOL_RXQ_RING_SZ_DEF);
current_port->isol_rxq_ring_sz = NETIF_ISOL_RXQ_RING_SZ_DEF;
} else {
isol_rxq_ring_sz = atoi(str);
RTE_LOG(INFO, NETIF, "worker %s:%s:isol_rxq_ring_sz = %d\n",
current_worker->name, current_port->port_name, isol_rxq_ring_sz);
current_port->isol_rxq_ring_sz = isol_rxq_ring_sz;
}
FREE_PTR(str);
}
void netif_keyword_value_init(void)
{
if (dpvs_state_get() == DPVS_STATE_INIT) {
/* KW_TYPE_INIT keyword */
netif_pktpool_nb_mbuf = NETIF_PKTPOOL_NB_MBUF_DEF;
netif_pktpool_mbuf_cache = NETIF_PKTPOOL_MBUF_CACHE_DEF;
}
/* KW_TYPE_NORMAL keyword */
}
void install_netif_keywords(void)
{
install_keyword_root("netif_defs", netif_defs_handler);
install_keyword("pktpool_size", pktpool_size_handler, KW_TYPE_INIT);
install_keyword("pktpool_cache", pktpool_cache_handler, KW_TYPE_INIT);
install_keyword("device", device_handler, KW_TYPE_INIT);
install_sublevel();
install_keyword("rx", NULL, KW_TYPE_INIT);
install_sublevel();
install_keyword("queue_number", rx_queue_number_handler, KW_TYPE_INIT);
install_keyword("descriptor_number", rx_desc_nb_handler, KW_TYPE_INIT);
install_keyword("rss", rss_handler, KW_TYPE_INIT);
install_sublevel_end();
install_keyword("tx", NULL, KW_TYPE_INIT);
install_sublevel();
install_keyword("queue_number", tx_queue_number_handler, KW_TYPE_INIT);
install_keyword("descriptor_number", tx_desc_nb_handler, KW_TYPE_INIT);
install_sublevel_end();
install_keyword("promisc_mode", promisc_mode_handler, KW_TYPE_INIT);
install_keyword("kni_name", kni_name_handler, KW_TYPE_INIT);
install_sublevel_end();
install_keyword("bonding", bonding_handler, KW_TYPE_INIT);
install_sublevel();
install_keyword("mode", bonding_mode_handler, KW_TYPE_INIT);
install_keyword("slave", bonding_slave_handler, KW_TYPE_INIT);
install_keyword("primary", bonding_primary_handler, KW_TYPE_INIT);
install_keyword("kni_name", bonding_kni_name_handler, KW_TYPE_INIT);
install_sublevel_end();
install_keyword_root("worker_defs", worker_defs_handler);
install_keyword("worker", worker_handler, KW_TYPE_INIT);
install_sublevel();
install_keyword("type", worker_type_handler, KW_TYPE_INIT);
install_keyword("cpu_id", cpu_id_handler, KW_TYPE_INIT);
install_keyword("port", worker_port_handler, KW_TYPE_INIT);
install_sublevel();
install_keyword("rx_queue_ids", rx_queue_ids_handler, KW_TYPE_INIT);
install_keyword("tx_queue_ids", tx_queue_ids_handler, KW_TYPE_INIT);
install_keyword("isol_rx_cpu_ids", isol_rx_cpu_ids_handler, KW_TYPE_INIT);
install_keyword("isol_rxq_ring_sz", isol_rxq_ring_sz_handler, KW_TYPE_INIT);
install_sublevel_end();
install_sublevel_end();
}
void netif_cfgfile_init(void)
{
INIT_LIST_HEAD(&port_list);
INIT_LIST_HEAD(&bond_list);
INIT_LIST_HEAD(&worker_list);
}
static void netif_cfgfile_term(void)
{
struct port_conf_stream *port_cfg, *port_cfg_next;
struct bond_conf_stream *bond_cfg, *bond_cfg_next;
struct worker_conf_stream *worker_cfg, *worker_cfg_next;
struct queue_conf_stream *queue_cfg, *queue_cfg_next;
list_for_each_entry_safe(port_cfg, port_cfg_next, &port_list, port_list_node) {
list_del(&port_cfg->port_list_node);
rte_free(port_cfg);
}
list_for_each_entry_safe(bond_cfg, bond_cfg_next, &bond_list, bond_list_node) {
list_del(&bond_cfg->bond_list_node);
rte_free(bond_cfg);
}
list_for_each_entry_safe(worker_cfg, worker_cfg_next, &worker_list,
worker_list_node) {
list_del(&worker_cfg->worker_list_node);
list_for_each_entry_safe(queue_cfg, queue_cfg_next, &worker_cfg->port_list,
queue_list_node) {
list_del(&queue_cfg->queue_list_node);
rte_free(queue_cfg);
}
rte_free(worker_cfg);
}
}
#ifdef CONFIG_DPVS_NETIF_DEBUG
#include <arpa/inet.h>
#include <netinet/in.h>
static inline int parse_ether_hdr(struct rte_mbuf *mbuf, uint16_t port, uint16_t queue) {
struct ether_hdr *eth_hdr;
char saddr[18], daddr[18];
eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
ether_format_addr(saddr, sizeof(saddr), ð_hdr->s_addr);
ether_format_addr(daddr, sizeof(daddr), ð_hdr->d_addr);
RTE_LOG(INFO, NETIF, "[%s] lcore=%u port=%u queue=%u ethtype=%0x saddr=%s daddr=%s\n",
__func__, rte_lcore_id(), port, queue, rte_be_to_cpu_16(eth_hdr->ether_type),
saddr, daddr);
return EDPVS_OK;
}
static inline int is_ipv4_pkt_valid(struct ipv4_hdr *iph, uint32_t link_len)
{
if (((iph->version_ihl) >> 4) != 4)
return EDPVS_INVAL;
if ((iph->version_ihl & 0xf) < 5)
return EDPVS_INVAL;
if (rte_cpu_to_be_16(iph->total_length) < sizeof(struct ipv4_hdr))
return EDPVS_INVAL;
return EDPVS_OK;
}
static void parse_ipv4_hdr(struct rte_mbuf *mbuf, uint16_t port, uint16_t queue)
{
char saddr[16], daddr[16];
uint16_t lcore;
struct ipv4_hdr *iph;
struct udp_hdr *uh;
iph = rte_pktmbuf_mtod_offset(mbuf, struct ipv4_hdr *, sizeof(struct ether_hdr));
if (is_ipv4_pkt_valid(iph, mbuf->pkt_len) < 0)
return;
uh = rte_pktmbuf_mtod_offset(mbuf, struct udp_hdr *, sizeof(struct ether_hdr) +
(IPV4_HDR_IHL_MASK & iph->version_ihl) * sizeof(uint32_t));
lcore = rte_lcore_id();
if (!inet_ntop(AF_INET, &iph->src_addr, saddr, sizeof(saddr)))
return;
if (!inet_ntop(AF_INET, &iph->dst_addr, daddr, sizeof(daddr)))
return;
RTE_LOG(INFO, NETIF, "[%s] lcore=%u port=%u queue=%u ipv4_hl=%u tos=%u tot=%u "
"id=%u ttl=%u prot=%u src=%s dst=%s sport=%04x|%u dport=%04x|%u\n",
__func__, lcore, port, queue, IPV4_HDR_IHL_MASK & iph->version_ihl,
iph->type_of_service, ntohs(iph->total_length),
ntohs(iph->packet_id), iph->time_to_live, iph->next_proto_id, saddr, daddr,
uh->src_port, ntohs(uh->src_port), uh->dst_port, ntohs(uh->dst_port));
return;
}
__rte_unused static void pkt_send_back(struct rte_mbuf *mbuf, struct netif_port *port)
{
struct ether_hdr *ehdr;
struct ether_addr eaddr;
ehdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr*);
ether_addr_copy(&ehdr->s_addr, &eaddr);
ether_addr_copy(&ehdr->d_addr, &ehdr->s_addr);
ether_addr_copy(&eaddr, &ehdr->d_addr);
netif_xmit(mbuf, port);
}
#endif
/********************************************* mbufpool *******************************************/
static struct rte_mempool *pktmbuf_pool[NETIF_MAX_SOCKETS];
static inline void netif_pktmbuf_pool_init(void)
{
int i;
char poolname[32];
for (i = 0; i < NETIF_MAX_SOCKETS; i++) {
snprintf(poolname, sizeof(poolname), "mbuf_pool_%d", i);
pktmbuf_pool[i] = rte_pktmbuf_pool_create(poolname, netif_pktpool_nb_mbuf,
netif_pktpool_mbuf_cache, 0, RTE_MBUF_DEFAULT_BUF_SIZE, i);
if (!pktmbuf_pool[i])
rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d", i);
}
}
/******************************************* pkt-type *********************************************/
#define NETIF_PKT_TYPE_TABLE_BITS 8
#define NETIF_PKT_TYPE_TABLE_BUCKETS (1 << NETIF_PKT_TYPE_TABLE_BITS)
#define NETIF_PKT_TYPE_TABLE_MASK (NETIF_PKT_TYPE_TABLE_BUCKETS - 1)
/* Note: Lockless. pkt_type can only be registered on initialization stage,
* and unregistered on cleanup stage. Otherwise uncertain behavior may arise.
*/
static struct list_head pkt_type_tab[NETIF_PKT_TYPE_TABLE_BUCKETS];
static inline int pkt_type_tab_hashkey(uint16_t type)
{
return type & NETIF_PKT_TYPE_TABLE_MASK;
}
static inline void netif_pkt_type_tab_init(void)
{
int i;
for (i = 0; i < NETIF_PKT_TYPE_TABLE_BUCKETS; i++)
INIT_LIST_HEAD(&pkt_type_tab[i]);
}
int netif_register_pkt(struct pkt_type *pt)
{
struct pkt_type *cur;
int hash;
if (unlikely(NULL == pt))
return EDPVS_INVAL;
hash = pkt_type_tab_hashkey(pt->type);
list_for_each_entry(cur, &pkt_type_tab[hash], list) {
if (cur->type == pt->type) {
return EDPVS_EXIST;
}
}
list_add_tail(&pt->list, &pkt_type_tab[hash]);
return EDPVS_OK;
}
int netif_unregister_pkt(struct pkt_type *pt)
{
struct pkt_type *cur;
int hash;
if (unlikely(NULL == pt))
return EDPVS_INVAL;
hash = pkt_type_tab_hashkey(pt->type);
list_for_each_entry(cur, &pkt_type_tab[hash], list) {
if (cur->type == pt->type) {
list_del_init(&pt->list);
return EDPVS_OK;
}
}
return EDPVS_NOTEXIST;
}
static struct pkt_type *pkt_type_get(uint16_t type, struct netif_port *port)
{
struct pkt_type *pt;
int hash;
hash = pkt_type_tab_hashkey(type);
list_for_each_entry(pt, &pkt_type_tab[hash], list) {
if (pt->type == type && ((pt->port == NULL) || pt->port == port)) {
return pt;
}
}
return NULL;
}
/****************************************** lcore job *********************************************/
/* Note: lockless, lcore_job can only be register on initialization stage and
* unregistered on cleanup stage.
*/
struct list_head netif_lcore_jobs[NETIF_LCORE_JOB_TYPE_MAX];
static inline void netif_lcore_jobs_init(void)
{
int ii;
for (ii = 0; ii < NETIF_LCORE_JOB_TYPE_MAX; ii++) {
INIT_LIST_HEAD(&netif_lcore_jobs[ii]);
}
}
int netif_lcore_loop_job_register(struct netif_lcore_loop_job *lcore_job)
{
struct netif_lcore_loop_job *cur;
if (unlikely(NULL == lcore_job))
return EDPVS_INVAL;
list_for_each_entry(cur, &netif_lcore_jobs[lcore_job->type], list) {
if (cur == lcore_job) {
return EDPVS_EXIST;
}
}
if (unlikely(NETIF_LCORE_JOB_SLOW == lcore_job->type && lcore_job->skip_loops <= 0))
return EDPVS_INVAL;
list_add_tail(&lcore_job->list, &netif_lcore_jobs[lcore_job->type]);
return EDPVS_OK;
}
int netif_lcore_loop_job_unregister(struct netif_lcore_loop_job *lcore_job)
{
struct netif_lcore_loop_job *cur;
if (unlikely(NULL == lcore_job))
return EDPVS_INVAL;
list_for_each_entry(cur, &netif_lcore_jobs[lcore_job->type], list) {
if (cur == lcore_job) {
list_del_init(&cur->list);
return EDPVS_OK;
}
}
return EDPVS_NOTEXIST;
}
/*************************** function declared for kni ********************************************/
static void kni_ingress(struct rte_mbuf *mbuf, struct netif_port *dev,
struct netif_queue_conf *qconf);
static void kni_send2kern_loop(uint8_t port_id, struct netif_queue_conf *qconf);
/****************************************** lcore conf ********************************************/
/* per-lcore statistics */
static struct netif_lcore_stats lcore_stats[NETIF_MAX_LCORES];
/* per-lcore isolated reception queues */
static struct list_head isol_rxq_tab[NETIF_MAX_LCORES];
/* worker configuration array */
static struct netif_lcore_conf lcore_conf[NETIF_MAX_LCORES + 1];
/* Note: Lockless, lcore_conf is set on initialization stage by cfgfile /etc/dpvs.conf.
config sample:
static struct netif_lcore_conf lcore_conf[NETIF_MAX_LCORES + 1] = {
{.id = 1, .nports = 2, .pqs = {
{.id = 0, .nrxq = 1, .ntxq = 1, .rxqs = {{.id = 0, }, }, .txqs = {{.id = 0, }, }, },
{.id = 1, .nrxq = 0, .ntxq = 1, .txqs = {{.id = 0, }, }, }, },
},
{.id = 2, .nports = 2, .pqs = {
{.id = 0, .nrxq = 1, .ntxq = 1, .rxqs = {{.id = 1, }, }, .txqs = {{.id = 1, }, }, },
{.id = 1, .nrxq = 1, .ntxq = 2, .rxqs = {{.id = 0, }, }, .txqs = {{.id = 1, }, {.id = 4, }}, }, },
},
{.id = 3, .nports = 2, .pqs = {
{.id = 0, .nrxq = 2, .ntxq = 1, .rxqs = {{.id = 2, }, {.id = 3, }, }, .txqs = {{.id = 2, }, }, },
{.id = 1, .nrxq = 1, .ntxq = 2, .rxqs = {{.id = 1, }, }, .txqs = {{.id = 2, }, {.id = 3, }, }, }, },
},
};
*/
static int isol_rxq_add(lcoreid_t cid, portid_t pid, queueid_t qid,
unsigned rb_sz, struct netif_queue_conf *rxq);
static void isol_rxq_del(struct rx_partner *isol_rxq, bool force);