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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
|
/// 物理设备管理器
#include <assert.h>
#include <stdio.h>
#include <sys/queue.h>
#include <unistd.h>
#include <rte_bus_pci.h>
#include <rte_config.h>
#include <rte_debug.h>
#include <rte_ethdev.h>
#include <rte_ether.h>
#include <rte_flow.h>
#include <rte_kni.h>
#include <rte_malloc.h>
#include <rte_mbuf.h>
#include <rte_pci.h>
#include <rte_string_fns.h>
#include <MESA_prof_load.h>
#include <cJSON.h>
#include <common.h>
#include <sc_common.h>
#include <sc_mrb.h>
#include <sc_phydev.h>
static struct rte_eth_conf eth_conf_default = {
.rxmode =
{
.mq_mode = ETH_MQ_RX_RSS,
.split_hdr_size = 0,
},
.txmode =
{
.mq_mode = ETH_MQ_TX_NONE,
},
};
static uint8_t default_sym_rss_key[40] = {0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a};
TAILQ_HEAD(phydev_list, phydev);
TAILQ_HEAD(phydev_whitelist_list, phydev_whitelist);
struct phydev_main
{
struct phydev_list device_list;
struct phydev * rte_device[RTE_MAX_ETHPORTS];
struct cJSON * j_hwfile;
struct phydev_whitelist_list device_whitelist_list;
struct phydev * whitelist_device[RTE_MAX_ETHPORTS];
unsigned int en_early_scan;
unsigned int en_load_hwfile;
unsigned int en_load_g_cfg;
};
const char * __str_rssmode(unsigned int rssmode)
{
switch (rssmode)
{
case MR_DEV_RSSMODE_DEFAULT:
return "Default";
case MR_DEV_RSSMODE_2TUPLE_SYM:
return "SAddr/DAddr(Sym)";
case MR_DEV_RSSMODE_4TUPLE_SYM:
return "SAddr/DAddr/SPort/DPort(Sym)";
case MR_DEV_RSSMODE_4TUPLE_ASYM:
return "SAddr/DAddr/SPort/DPort(Asym)";
default:
return "Unknown";
}
}
const char * __str_enable_or_disable(unsigned int value)
{
if (value)
return "ENABLE";
else
return "DISABLE";
}
static int phydev_info_dump(struct phydev * dev)
{
char str_phy_addr[MR_SYMBOL_MAX];
rte_ether_format_addr(str_phy_addr, sizeof(str_phy_addr), &dev->info.phy_ether_addr);
MR_INFO(" ");
MR_INFO("Physical Device %s: PortID = %d", dev->symbol, dev->port_id);
MR_INFO(" Physical Hwaddr : %s", str_phy_addr);
MR_INFO(" Maximum Transmission Unit : %u", dev->mtu);
MR_INFO(" Promisc : %s", __str_enable_or_disable(dev->promisc));
MR_INFO(" VLAN-Filter : %s", __str_enable_or_disable(dev->en_vlan_filter));
MR_INFO(" VLAN-Strip : %s", __str_enable_or_disable(dev->en_vlan_strip));
MR_INFO(" Drop-En : %s", __str_enable_or_disable(dev->en_drop));
MR_INFO(" RSSMode : %s", __str_rssmode(dev->info.rssmode));
return 0;
}
/* 虚设备用户配置查询 */
static int phydev_query_device_list(struct sc_main * sc, const char * str_vdev_type,
char vdevsyms[MR_SYMBOL_MAX][MR_PHYDEV_MAX], unsigned int * nr_vdevsyms)
{
char str_direct_devices[MR_STRING_MAX];
int ret = MESA_load_profile_string_nodef(sc->local_cfgfile, "device", str_vdev_type, str_direct_devices,
sizeof(str_direct_devices));
if (ret < 0)
{
*nr_vdevsyms = 0;
return RT_SUCCESS;
}
char * str_vdev_symbol[MR_TOKENS_MAX];
int nr_str_tokens =
rte_strsplit(str_direct_devices, sizeof(str_direct_devices), str_vdev_symbol, MR_TOKENS_MAX, ',');
for (int i = 0; i < nr_str_tokens; i++)
{
MR_VERIFY(i < MR_PHYDEV_MAX);
snprintf(vdevsyms[i], sizeof(vdevsyms[i]), "%s", str_vdev_symbol[i]);
}
*nr_vdevsyms = nr_str_tokens;
return RT_SUCCESS;
}
/* 用户参数解析:网卡队列参数设置 */
static int gen_phydev_qconf(struct phydev * dev, struct rte_eth_rxconf * rxconf, struct rte_eth_txconf * txconf,
unsigned int * nr_rxdesc, unsigned int * nr_txdesc)
{
// 获取默认的RX、TX队列设置
struct rte_eth_dev_info rte_dev_info;
rte_eth_dev_info_get(dev->port_id, &rte_dev_info);
// 复制默认配置
*rxconf = rte_dev_info.default_rxconf;
*txconf = rte_dev_info.default_txconf;
// Drop-En开关设置,丢弃处理不过来的包
rxconf->rx_drop_en = dev->info.en_drop;
dev->en_drop = rxconf->rx_drop_en;
*nr_rxdesc = dev->info.nr_rx_desc;
*nr_txdesc = dev->info.nr_tx_desc;
return 0;
}
/* 用户参数解析:网卡参数设置 */
static int gen_phydev_ethconf(struct phydev * dev, struct rte_eth_conf * out_eth_conf)
{
struct rte_eth_conf eth_conf = eth_conf_default;
if (dev->info.type == __PHYDEV_INFO_TYPE_PCI)
{
/* only PCI devices can run at RSS mode. */
eth_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
/* setup how nics distributes packets */
if (dev->info.rssmode == MR_DEV_RSSMODE_2TUPLE_SYM)
{
eth_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP;
eth_conf.rx_adv_conf.rss_conf.rss_key = default_sym_rss_key;
eth_conf.rx_adv_conf.rss_conf.rss_key_len = sizeof(default_sym_rss_key);
}
else if (dev->info.rssmode == MR_DEV_RSSMODE_4TUPLE_SYM)
{
eth_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV6_TCP |
ETH_RSS_NONFRAG_IPV4_UDP | ETH_RSS_NONFRAG_IPV6_UDP;
eth_conf.rx_adv_conf.rss_conf.rss_key = default_sym_rss_key;
eth_conf.rx_adv_conf.rss_conf.rss_key_len = sizeof(default_sym_rss_key);
}
else if (dev->info.rssmode == MR_DEV_RSSMODE_4TUPLE_ASYM)
{
eth_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV6_TCP |
ETH_RSS_NONFRAG_IPV4_UDP | ETH_RSS_NONFRAG_IPV6_UDP;
eth_conf.rx_adv_conf.rss_conf.rss_key = NULL;
}
}
else if (dev->info.type == __PHYDEV_INFO_TYPE_VDEV)
{
/* the vdevs is not support multi-queues loadbalance by hardware */
eth_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
}
if (dev->info.en_vlan_strip)
{
eth_conf.txmode.offloads |= DEV_TX_OFFLOAD_VLAN_INSERT;
}
*out_eth_conf = eth_conf;
return 0;
}
static int calc_phydev_queue(struct phydev * dev, unsigned int * out_rxq, unsigned int * out_txq)
{
unsigned int rxq_use = 0;
unsigned int txq_use = 0;
#define _(_type, _nr_q, _dir, _counter) \
do \
{ \
dev->qgroup[_type].qgroup_type = _type; \
dev->qgroup[_type].nr_q_max = _nr_q; \
dev->qgroup[_type].dir = _dir; \
\
for (int i = 0; i < _nr_q; i++) \
dev->qgroup[_type].qinfo[i].queue_id = _counter++; \
} while (0)
_(QGROUP_TYPE_RSS_RX, dev->nr_rxq, QINFO_DIR_RX, rxq_use);
_(QGROUP_TYPE_RSS_TX, dev->nr_txq, QINFO_DIR_TX, txq_use);
_(QGROUP_TYPE_CTX_RX, dev->nr_ctx_rxq, QINFO_DIR_RX, rxq_use);
_(QGROUP_TYPE_CTX_TX, dev->nr_ctx_txq, QINFO_DIR_TX, txq_use);
_(QGROUP_TYPE_HAIRPIN_RX, dev->nr_hairpin_q, QINFO_DIR_RX, rxq_use);
_(QGROUP_TYPE_HAIRPIN_TX, dev->nr_hairpin_q, QINFO_DIR_TX, txq_use);
#undef _
// 后面可以继续添加别的类型的队列,物理队列序号顺序排布
// 至少要有一个收、发队列,否则无法通过初始化
*out_rxq = rxq_use > 0 ? rxq_use : 1;
*out_txq = txq_use > 0 ? txq_use : 1;
return 0;
}
static int phydev_queue_setup_rss_or_ctx(struct phydev * dev, socket_id_t socket_id, struct rte_mempool * pool,
unsigned int nr_rxq_use, unsigned int nr_txq_use, unsigned int rxq_index_begin,
unsigned int txq_index_begin)
{
int retval = 0;
unsigned int nr_rxdesc;
unsigned int nr_txdesc;
struct rte_eth_rxconf rxconf;
struct rte_eth_txconf txconf;
gen_phydev_qconf(dev, &rxconf, &txconf, &nr_rxdesc, &nr_txdesc);
for (unsigned int rxq = rxq_index_begin; rxq < rxq_index_begin + nr_rxq_use; rxq++)
{
retval = rte_eth_rx_queue_setup(dev->port_id, rxq, nr_rxdesc, socket_id, &rxconf, pool);
if (retval < 0)
{
MR_ERROR("Physical device %s RXQ %d setup failed, errno = %d", dev->symbol, rxq, retval);
goto err;
}
}
for (unsigned int txq = txq_index_begin; txq < txq_index_begin + nr_txq_use; txq++)
{
retval = rte_eth_tx_queue_setup(dev->port_id, txq, nr_txdesc, socket_id, &txconf);
if (retval < 0)
{
MR_ERROR("Physical device %s TXQ %d setup failed, errno = %d", dev->symbol, txq, retval);
goto err;
}
}
return 0;
err:
return retval;
}
static int phydev_queue_setup_hairpin(struct phydev * dev, unsigned int nr_hairpin_q, unsigned int rxq_index_begin,
unsigned int txq_index_begin)
{
/* create hairpin queues on both ports*/
unsigned int q_index_hairpin = 0;
unsigned int q_index_hairpin_peer = 0;
struct rte_eth_hairpin_conf hairpin_conf = {
.peer_count = 1,
.manual_bind = 0,
.tx_explicit = 0,
};
int ret = 0;
for (q_index_hairpin = rxq_index_begin, q_index_hairpin_peer = txq_index_begin;
q_index_hairpin < rxq_index_begin + nr_hairpin_q; q_index_hairpin++, q_index_hairpin_peer++)
{
hairpin_conf.peers[0].port = dev->port_id;
hairpin_conf.peers[0].queue = q_index_hairpin_peer;
MR_DEBUG("Prepare to setup rx hairpin for device %s, hairpin_queue_id = %d, peer_hairpin_queue_id = %d",
dev->symbol, q_index_hairpin, q_index_hairpin_peer);
ret = rte_eth_rx_hairpin_queue_setup(dev->port_id, q_index_hairpin, dev->info.nr_rx_desc, &hairpin_conf);
if (unlikely(ret != 0))
{
MR_ERROR("Failed at setup rx hairpin queue at port = %d, queue = %d, peer_queue = %d, ret = %d: %s",
dev->port_id, q_index_hairpin, q_index_hairpin_peer, ret, rte_strerror(errno));
return ret;
}
}
for (q_index_hairpin = txq_index_begin, q_index_hairpin_peer = rxq_index_begin;
q_index_hairpin < txq_index_begin + nr_hairpin_q; q_index_hairpin++, q_index_hairpin_peer++)
{
hairpin_conf.peers[0].port = dev->port_id;
hairpin_conf.peers[0].queue = q_index_hairpin_peer;
MR_DEBUG("Prepare to setup tx hairpin for device %s, hairpin_queue_id = %d, peer_hairpin_queue_id = %d",
dev->symbol, q_index_hairpin, q_index_hairpin_peer);
ret = rte_eth_tx_hairpin_queue_setup(dev->port_id, q_index_hairpin, dev->info.nr_rx_desc, &hairpin_conf);
if (unlikely(ret != 0))
{
MR_ERROR("Failed at setup tx hairpin queue at port = %d, queue = %d, peer_queue = %d, ret = %d: %s",
dev->port_id, q_index_hairpin, q_index_hairpin_peer, ret, rte_strerror(errno));
return ret;
}
dev->hairpin_q = q_index_hairpin;
}
MR_INFO("device %s hairpin setup successfully.", dev->symbol);
return 0;
}
/* A matrix can be used to do symmetric hash */
static const uint8_t symmetric_rss_key[] = {
0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A,
0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A,
0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A, 0x6D, 0x5A,
};
static int phydev_setup_default_flows(struct sc_main * sc, struct phydev_main * ctx, struct phydev * dev)
{
/* ----------------- GROUP 0 -------------------- */
struct rte_flow * flow = NULL;
struct rte_flow_attr attr = {
.group = 0, /* set the rule on the main group. */
.ingress = 1, /* Rx flow. */
.priority = 3,
};
struct rte_flow_error flow_error = {};
/* Define the pattern to match the packet */
struct rte_flow_item pattern[] = {
[0] = {.type = RTE_FLOW_ITEM_TYPE_ETH},
[1] = {.type = RTE_FLOW_ITEM_TYPE_END},
};
/* Jump parameters */
struct rte_flow_action_jump jump_to_group_1 = {.group = 1};
/* Jump actions */
struct rte_flow_action actions[] = {
[0] =
{
.type = RTE_FLOW_ACTION_TYPE_JUMP,
.conf = &jump_to_group_1,
},
[1] =
{
.type = RTE_FLOW_ACTION_TYPE_END,
.conf = NULL,
},
};
/* FLOW 0 */
flow = rte_flow_create(dev->port_id, &attr, pattern, actions, &flow_error);
if (unlikely(flow == NULL))
{
MR_ERROR("Failed at install default rule for device %s: %s", dev->symbol, flow_error.message);
goto err;
}
dev->default_flow_handles[dev->nr_default_flow_handles++] = flow;
/* ------------------ GROUP 1 ----------------------- */
attr.group = 1;
attr.ingress = 1;
attr.priority = 3;
memset(&flow_error, 0, sizeof(flow_error));
memset(&pattern, 0, sizeof(pattern));
memset(&actions, 0, sizeof(actions));
uint16_t target_queue_id[__QGROUP_MAX_QUEUE] = {};
for (unsigned int i = 0; i < dev->nr_rxq; i++)
{
target_queue_id[i] = i;
}
/* TODO: maybe we have much more better way to do the symmetric hash */
struct rte_flow_action_rss rss = {
.level = 0,
.queue = &target_queue_id,
.queue_num = dev->nr_rxq,
.types = ETH_RSS_IP,
.key = symmetric_rss_key,
.key_len = 40,
};
actions[0].type = RTE_FLOW_ACTION_TYPE_RSS;
actions[0].conf = &rss;
actions[1].type = RTE_FLOW_ACTION_TYPE_END;
actions[1].conf = NULL;
flow = rte_flow_create(dev->port_id, &attr, pattern, actions, &flow_error);
if (unlikely(flow == NULL))
{
MR_ERROR("Failed at install RSS flow at device %s: %s", dev->symbol, flow_error.message);
return RT_ERR;
}
/* Save the default flows, these rules should be destroy when the device is stop */
dev->default_flow_handles[dev->nr_default_flow_handles++] = flow;
return RT_SUCCESS;
err:
return RT_ERR;
}
static int phydev_setup(struct sc_main * sc, struct phydev_main * ctx, struct phydev * dev)
{
int retval = 0;
socket_id_t socket_id = rte_eth_dev_socket_id(dev->port_id);
struct rte_mempool * direct_pool = mrb_direct_mempool_locate(sc->mrb_pool_main, dev->str_direct_pool, socket_id, 0);
struct rte_mempool * indirect_pool =
mrb_indirect_mempool_locate(sc->mrb_pool_main, dev->str_indirect_pool, socket_id, 0);
if (direct_pool == NULL || indirect_pool == NULL)
{
MR_ERROR("Physical device %s setup failed: Cannot find suitable mempool"
" on socket %d, core %d",
dev->symbol, socket_id, 0);
return RT_ERR;
}
// 配置端口信息
struct rte_eth_conf local_eth_conf;
gen_phydev_ethconf(dev, &local_eth_conf);
unsigned nr_rxq_use = 0;
unsigned nr_txq_use = 0;
// TODO: 目前实现按每个服务线程处理一个队列考虑
dev->nr_rxq = CPU_COUNT(&sc->cpu_set_io);
dev->nr_txq = CPU_COUNT(&sc->cpu_set_io);
/* SmartOffload CPUSETS */
dev->nr_ctx_rxq = CPU_COUNT(&sc->cpu_set_offload);
dev->nr_ctx_txq = CPU_COUNT(&sc->cpu_set_offload);
/*
Check this NIC can support hairpin or not.
The SmartOffload can not use when hairpin mode is unavailable.
*/
if (dev->en_smartoffload )
{
struct rte_eth_hairpin_cap hairpin_cap = {};
retval = rte_eth_dev_hairpin_capability_get(dev->port_id, &hairpin_cap);
if (unlikely(retval != 0))
{
MR_ERROR("device %s hairpin mode is unavliable, SmartOffload can not be enabled: retval = %d, %s", dev->symbol,
retval, rte_strerror(retval));
return RT_ERR;
}
}
/*
Need to setup haripin when enable smartoffload,
For now, for each port, there's only one rx queue and tx queue for hairpin.
*/
dev->nr_hairpin_q = dev->en_smartoffload ? 1 : 0;
calc_phydev_queue(dev, &nr_rxq_use, &nr_txq_use);
retval = rte_eth_dev_configure(dev->port_id, nr_rxq_use, nr_txq_use, &local_eth_conf);
if (retval != 0)
{
MR_ERROR("Physical device %s configure error: %s, errno = %d", dev->symbol, strerror(retval), retval);
return retval;
}
unsigned int nr_rxq_index = 0;
unsigned int nr_txq_index = 0;
/* Configure the RX, TX queues */
if (dev->nr_rxq > 0 || dev->nr_txq > 0)
{
retval = phydev_queue_setup_rss_or_ctx(dev, socket_id, direct_pool, dev->nr_rxq, dev->nr_txq, nr_rxq_index,
nr_txq_index);
if (retval)
return RT_ERR;
}
nr_rxq_index += dev->nr_rxq;
nr_txq_index += dev->nr_txq;
/* Configure the RX, TX queues for control and exception path */
if (dev->nr_ctx_rxq > 0 || dev->nr_ctx_txq > 0)
{
retval = phydev_queue_setup_rss_or_ctx(dev, socket_id, direct_pool, dev->nr_ctx_rxq, dev->nr_ctx_txq,
nr_rxq_index, nr_txq_index);
if (retval)
return RT_ERR;
}
nr_rxq_index += dev->nr_ctx_rxq;
nr_txq_index += dev->nr_ctx_txq;
/* Configure the RX, TX queues for same port hairpin */
if (dev->nr_hairpin_q > 0)
{
retval = phydev_queue_setup_hairpin(dev, dev->nr_hairpin_q, nr_rxq_index, nr_txq_index);
if (retval)
return RT_ERR;
}
/* MTU */
if (dev->mtu != 0 && (retval = rte_eth_dev_set_mtu(dev->port_id, dev->mtu)) < 0)
{
MR_WARNING("Physical device %s MTU setup failed : %s", dev->symbol, strerror(-retval));
}
/* Multicast */
if (dev->allmulticast)
{
rte_eth_allmulticast_enable(dev->port_id);
}
else
{
rte_eth_allmulticast_disable(dev->port_id);
}
/* 混杂模式设置 */
if (dev->promisc)
{
rte_eth_promiscuous_enable(dev->port_id);
}
else
{
rte_eth_promiscuous_disable(dev->port_id);
}
retval = rte_eth_dev_start(dev->port_id);
if (retval < 0)
{
MR_ERROR("Physical device %s Start Error, Errno = %d(%s)", dev->symbol, retval, strerror(-retval));
return retval;
}
/* setup default flows, the default flows need to install after the device start */
/* for now, only devices which smartoffload is enabled will be install default flows,
because the vf interfaces does not support jump action */
if (dev->en_smartoffload)
{
retval = phydev_setup_default_flows(sc, ctx, dev);
if (retval)
return RT_ERR;
}
dev->nr_rxq = nr_rxq_use;
dev->nr_txq = nr_txq_use;
rte_eth_dev_get_mtu(dev->port_id, &dev->mtu);
rte_eth_macaddr_get(dev->port_id, &dev->ether_addr);
dev->promisc = rte_eth_promiscuous_get(dev->port_id);
// 配置VLAN相关功能
dev->en_vlan_strip = dev->info.en_vlan_strip;
dev->en_vlan_filter = dev->info.en_vlan_filter;
int vlan_offload_mask = 0;
if (dev->en_vlan_strip)
{
vlan_offload_mask |= ETH_VLAN_STRIP_OFFLOAD;
}
if (dev->en_vlan_filter)
{
vlan_offload_mask |= ETH_VLAN_FILTER_OFFLOAD;
}
if (vlan_offload_mask != 0)
{
if ((retval = rte_eth_dev_set_vlan_offload(dev->port_id, vlan_offload_mask)) < 0)
{
MR_WARNING("Physical device %s VLAN offload mask setup failed: mask=%x, %s", dev->symbol, vlan_offload_mask,
strerror(-retval));
}
}
/* VLAN Filter设置 */
if (dev->en_vlan_filter)
{
for (unsigned int i = 0; i < dev->info.nr_vlan_filter_allow_ids; i++)
{
uint16_t vlan_id = (uint16_t)(dev->info.vlan_filter_allow_ids[i]);
if ((retval = rte_eth_dev_vlan_filter(dev->port_id, vlan_id, 1)) < 0)
{
MR_WARNING("Physical device %s VLAN Filter allow vlan-id %d setup failed: %s", dev->symbol, vlan_id,
strerror(-retval));
}
}
for (unsigned int i = 0; i < dev->info.nr_vlan_filter_deny_ids; i++)
{
uint16_t vlan_id = (uint16_t)(dev->info.vlan_filter_deny_ids[i]);
if ((retval = rte_eth_dev_vlan_filter(dev->port_id, vlan_id, 0)) < 0)
{
MR_WARNING("Physical device %s VLAN Filter deny vlan-id %d setup failed: %s", dev->symbol, vlan_id,
strerror(-retval));
}
}
}
if (dev->info.pvidmode == MR_DEV_PVID_BY_HW_SETUP && dev->info.pvid > 0)
{
rte_eth_dev_set_vlan_pvid(dev->port_id, dev->info.pvid, 1);
}
phydev_info_dump(dev);
dev->inited = 1;
dev->enable = 1;
return 0;
}
int phydev_list(struct phydev_main * ctx, struct phydev * devs[], int nr_max_devs)
{
struct phydev * dev_iter;
int nr_in_use = 0;
TAILQ_FOREACH(dev_iter, &ctx->device_list, next)
{
if (nr_in_use >= nr_max_devs)
break;
if (dev_iter->enable && dev_iter->inited)
devs[nr_in_use++] = dev_iter;
}
return nr_in_use;
}
int phydev_iterate(struct phydev_main * ctx, struct phydev ** phydev)
{
// 迭代器为空,从头开始迭代,否则查找迭代器下一个对象
if (*phydev == NULL)
{
*phydev = TAILQ_FIRST(&ctx->device_list);
}
else
{
*phydev = TAILQ_NEXT(*phydev, next);
}
// 迭代到尾部,返回错误码
if (*phydev == NULL)
return -ENOENT;
return 0;
}
int whitelist_phydev_iterate(struct phydev_main * ctx, struct phydev_whitelist ** phydev_wh)
{
// 迭代器为空,从头开始迭代,否则查找迭代器下一个对象
if (*phydev_wh == NULL)
{
*phydev_wh = TAILQ_FIRST(&ctx->device_whitelist_list);
}
else
{
*phydev_wh = TAILQ_NEXT(*phydev_wh, next);
}
// 迭代到尾部,返回错误码
if (*phydev_wh == NULL) return -ENOENT;
return 0;
}
struct phydev * phydev_lookup(struct phydev_main * ctx, const char * devsym)
{
struct phydev * dev_iter = NULL;
struct phydev * dev_ret = NULL;
TAILQ_FOREACH(dev_iter, &ctx->device_list, next)
{
if (strncmp(dev_iter->symbol, devsym, sizeof(dev_iter->symbol)) != 0)
continue;
dev_ret = dev_iter;
break;
}
return dev_ret;
}
struct phydev * phydev_lookup_by_pci_addr(struct phydev_main * ctx, struct rte_pci_addr pci_addr)
{
struct phydev * phydev_iter;
TAILQ_FOREACH(phydev_iter, &ctx->device_list, next)
{
if (phydev_iter->info.type == __PHYDEV_INFO_TYPE_PCI &&
rte_pci_addr_cmp(&phydev_iter->info.pci_addr, &pci_addr) == 0)
{
return phydev_iter;
}
}
return NULL;
}
int phydev_use_queue(struct phydev * dev, enum phydev_qgroup_type q_type, queue_id_t * queue_out)
{
struct phydev_qgroup * qgroup = &dev->qgroup[q_type];
for (int i = 0; i < qgroup->nr_q_max; i++)
{
if (qgroup->qinfo[i].in_use != 0)
continue;
qgroup->qinfo[i].in_use = 1;
*queue_out = qgroup->qinfo[i].queue_id;
goto success;
}
MR_ERROR("Phydev %s out of free queues(type = %d, total = %d), attach failed.\n", dev->symbol, q_type,
qgroup->nr_q_max);
return RT_ERR;
success:
return RT_SUCCESS;
}
void phydev_unuse_queue(struct phydev * dev, enum phydev_qgroup_type q_type, queue_id_t queue)
{
struct phydev_qgroup * qgroup = &dev->qgroup[q_type];
qgroup->qinfo[queue].in_use = 1;
}
/* \brief 物理设备名称数据解析
* 从预配置文件中解析启用的物理设备对应的符号名称和自定义参数
*/
#ifndef PHYDEV_DEFAULT_NR_RX_DESC
#define PHYDEV_DEFAULT_NR_RX_DESC 2048
#endif
#ifndef PHYDEV_DEFAULT_NR_TX_DESC
#define PHYDEV_DEFAULT_NR_TX_DESC 2048
#endif
#ifndef PHYDEV_DEFAULT_EN_VLAN_FILTER
#define PHYDEV_DEFAULT_EN_VLAN_FILTER 0
#endif
#ifndef PHYDEV_DEFAULT_EN_VLAN_STRIP
#define PHYDEV_DEFAULT_EN_VLAN_STRIP 0
#endif
#ifndef PHYDEV_DEFAULT_EN_DROP
#define PHYDEV_DEFAULT_EN_DROP 0
#endif
#ifndef PHYDEV_DEFAULT_PROMISC
#define PHYDEV_DEFAULT_PROMISC 1
#endif
#ifndef PHYDEV_DEFAULT_RSS_MODE
#define PHYDEV_DEFAULT_RSS_MODE MR_DEV_RSSMODE_2TUPLE_SYM
#endif
#ifndef PHYDEV_DEFAULT_EN_EARLY_SCAN
#define PHYDEV_DEFAULT_EN_EARLY_SCAN 1
#endif
#ifndef PHYDEV_DEFAULT_EN_LOAD_HWFILE
#define PHYDEV_DEFAULT_EN_LOAD_HWFILE 1
#endif
#ifndef PHYDEV_DEFAULT_EN_LOAD_G_CFG
#define PHYDEV_DEFAULT_EN_LOAD_G_CFG 1
#endif
struct phydev * phydev_alloc(struct phydev_main * phydev_main, unsigned int port_id)
{
struct phydev * phydev = ZMALLOC(sizeof(struct phydev));
MR_VERIFY_MALLOC(phydev);
snprintf(phydev->symbol, sizeof(phydev->symbol), "meth%d", port_id);
phydev->info.en_drop = PHYDEV_DEFAULT_EN_DROP;
phydev->info.en_vlan_filter = PHYDEV_DEFAULT_EN_VLAN_FILTER;
phydev->info.en_vlan_strip = PHYDEV_DEFAULT_EN_VLAN_STRIP;
phydev->info.nr_rx_desc = PHYDEV_DEFAULT_NR_RX_DESC;
phydev->info.nr_tx_desc = PHYDEV_DEFAULT_NR_RX_DESC;
phydev->info.rssmode = PHYDEV_DEFAULT_RSS_MODE;
phydev->promisc = PHYDEV_DEFAULT_PROMISC;
TAILQ_INSERT_TAIL(&phydev_main->device_list, phydev, next);
return phydev;
}
struct phydev_whitelist * whitelist_phydev_alloc(struct phydev_main * phydev_main)
{
struct phydev_whitelist * phydev_wh = LIBC_MALLOC(sizeof(struct phydev_whitelist));
MR_VERIFY_MALLOC(phydev_wh);
memset(phydev_wh, 0, sizeof(struct phydev_whitelist));
TAILQ_INSERT_TAIL(&phydev_main->device_whitelist_list, phydev_wh, next);
return phydev_wh;
}
/* 从全局配置文件读硬件配置信息 */
void phydev_config_by_g_cfg(struct phydev * phydev, const char * cfg)
{
char str_section[MR_SYMBOL_MAX];
snprintf(str_section, sizeof(str_section), "device:%s", phydev->symbol);
struct phydev_info * phydev_info = &phydev->info;
// 读VLAN-Strip选项,默认不开启
MESA_load_profile_uint_def(cfg, str_section, "vlan-strip", &phydev_info->en_vlan_strip, phydev_info->en_vlan_strip);
// 读VLAN-Filter选项,默认不开启
MESA_load_profile_uint_def(cfg, str_section, "vlan-filter", &phydev_info->en_vlan_filter,
phydev_info->en_vlan_filter);
// 默认PVID设置模式,
// 所有出方向的报文全部打vlan标签
MESA_load_profile_uint_def(cfg, str_section, "vlan-pvid-mode", &phydev_info->pvidmode, MR_DEV_PVID_BY_HW_SETUP);
MESA_load_profile_uint_def(cfg, str_section, "vlan-pvid", &phydev_info->pvid, 0);
// 允许通过的VLAN ID列表,仅当vlan-filter开启后有效
int ret =
MESA_load_profile_uint_range(cfg, str_section, "vlan-id-allow", RTE_DIM(phydev_info->vlan_filter_allow_ids),
phydev_info->vlan_filter_allow_ids);
if (ret >= 0)
{
phydev_info->nr_vlan_filter_allow_ids = ret;
}
// 禁止通过的VLAN ID列表,仅当vlan-filter开启后有效
ret = MESA_load_profile_uint_range(cfg, str_section, "vlan-id-deny", RTE_DIM(phydev_info->vlan_filter_deny_ids),
phydev_info->vlan_filter_deny_ids);
if (ret >= 0)
{
phydev_info->nr_vlan_filter_deny_ids = ret;
}
// 丢包选项
MESA_load_profile_uint_def(cfg, str_section, "drop_en", &phydev_info->en_drop, phydev_info->en_drop);
// 分流模式
MESA_load_profile_uint_def(cfg, str_section, "rssmode", &phydev_info->rssmode, phydev_info->rssmode);
// RX描述符数量
MESA_load_profile_uint_def(cfg, str_section, "nr_rxdesc", &phydev_info->nr_rx_desc, phydev_info->nr_rx_desc);
// TX描述符数量
MESA_load_profile_uint_def(cfg, str_section, "nr_txdesc", &phydev_info->nr_tx_desc, phydev_info->nr_tx_desc);
// 硬件CRC剥离
MESA_load_profile_uint_def(cfg, str_section, "hw_strip_crc", &phydev_info->en_hw_strip_crc, 0);
// 读MTU,网卡自适应。
MESA_load_profile_short_def(cfg, str_section, "mtu", (short *)&phydev->mtu, (short)phydev->mtu);
// 读混杂模式
MESA_load_profile_uint_def(cfg, str_section, "promisc", &phydev->promisc, phydev->promisc);
// MULTICAST
MESA_load_profile_uint_def(cfg, str_section, "allmulticast", &phydev->allmulticast, 0);
// SMARTOFFLOAD
MESA_load_profile_uint_def(cfg, str_section, "en_smartoffload", &phydev->en_smartoffload, 0);
// 读报文缓冲区名称
memset(phydev->str_direct_pool, 0, sizeof(phydev->str_direct_pool));
memset(phydev->str_indirect_pool, 0, sizeof(phydev->str_indirect_pool));
MESA_load_profile_string_def(cfg, str_section, "direct-pool", phydev->str_direct_pool,
sizeof(phydev->str_direct_pool), phydev->symbol);
MESA_load_profile_string_def(cfg, str_section, "indirect-pool", phydev->str_indirect_pool,
sizeof(phydev->str_indirect_pool), phydev->str_direct_pool);
return;
}
/* 通过网卡名从hwfile中提取PCI号 */
static int phydev_get_pci_info_by_hwfile(struct phydev_main * phydev_main, struct phydev_whitelist * phydev_wh)
{
cJSON * j_hwfile = phydev_main->j_hwfile;
if (j_hwfile == NULL)
{
MR_ERROR("j_hwfile is NULL \n");
return RT_SUCCESS;
}
/* 在JSON文件中查找设备,根据网卡名称查找 */
cJSON * j_phydev;
cJSON_ArrayForEach(j_phydev, j_hwfile)
{
/* 先获取设备名称 */
cJSON * j_interface = cJSON_GetObjectItem(j_phydev, "Interface");
MR_VERIFY(j_interface != NULL);
/* 如果设备名不匹配则跳过 */
if (strcmp(j_interface->valuestring,phydev_wh->symbol) !=0)
continue;
/* 获取PCI号 */
cJSON * j_slot = cJSON_GetObjectItem(j_phydev, "Slot");
MR_VERIFY(j_slot != NULL);
snprintf(phydev_wh->pci_addr, sizeof(phydev_wh->pci_addr), "%s", j_slot->valuestring);
return RT_SUCCESS;
}
MR_ERROR("Phydev Not Found in hwfile : %s \n", phydev_wh->symbol);
return RT_ERR;
}
/* 早期设备扫描,从HWFILE中获取设备定义的信息 */
static int phydev_early_scan(struct sc_main * sc, struct phydev_main * phydev_main)
{
struct phydev_whitelist * phydev_wh = NULL;
char str_device[MR_STRING_MAX];
char * str_dev_symbol[MR_TOKENS_MAX];
/* 从配置文件中提取网卡名列表 */
MESA_load_profile_string_nodef(sc->local_cfgfile, "device","device", str_device, sizeof(str_device));
/* 从列表中抽取每一个网卡名 */
int nr_str_tokens = rte_strsplit(str_device, sizeof(str_device),str_dev_symbol, MR_TOKENS_MAX, ',');
if (nr_str_tokens < 0)
{
return RT_ERR;
}
/* 遍历所有dev设备 */
for (int i = 0; i < nr_str_tokens; i++)
{
/* 每一个设备对应创建一个phydev_wh */
phydev_wh = whitelist_phydev_alloc(phydev_main);
MR_VERIFY_MALLOC(phydev_wh);
snprintf(phydev_wh->symbol, sizeof(phydev_wh->symbol), "%s", str_dev_symbol[i]);
/* 根据设备名称提取对应的PCI号 */
int ret = phydev_get_pci_info_by_hwfile(phydev_main,phydev_wh);
if (ret == RT_ERR)
{
return RT_ERR;
}
}
return RT_SUCCESS;
}
static int phydev_adjust_info_by_hwfile(struct phydev_main * phydev_main, struct phydev * phydev)
{
cJSON * j_hwfile = phydev_main->j_hwfile;
if (j_hwfile == NULL)
return RT_SUCCESS;
/* 在JSON文件中查找设备,根据PCI地址查找 */
cJSON * j_phydev;
cJSON_ArrayForEach(j_phydev, j_hwfile)
{
cJSON * j_slot = cJSON_GetObjectItem(j_phydev, "slot");
MR_VERIFY(j_slot != NULL);
struct rte_pci_addr _local_pci_addr;
int ret = rte_pci_addr_parse(j_slot->valuestring, &_local_pci_addr);
MR_VERIFY(ret == 0);
if (rte_pci_addr_cmp(&phydev->info.pci_addr, &_local_pci_addr) != 0)
continue;
/* 读内核名称、内核模块 */
cJSON * j_interface = cJSON_GetObjectItem(j_phydev, "Interface");
MR_VERIFY(j_interface != NULL);
/* 内核名称为空,说明在内核启用,跳过 */
if (strlen(j_interface->valuestring) == 0)
continue;
snprintf(phydev->symbol, sizeof(phydev->symbol), "%s", j_interface->valuestring);
snprintf(phydev->info.symbol, sizeof(phydev->info.symbol), "%s", j_interface->valuestring);
}
return RT_SUCCESS;
}
#if 0
/* 原始套接字设备扫描,根据用户配置 */
static int phydev_scan_raw_socket(struct sc_main * sc, struct phydev_main * phydev_main)
{
/* 加载原始套接字设备配置 */
char raw_devsyms[MR_SYMBOL_MAX][MR_PHYDEV_MAX] = {{0}};
unsigned int nr_rawsyms = 0;
phydev_query_device_list(sc, "rawsocket", raw_devsyms, &nr_rawsyms);
/* 用户没有配置原始套接字物理设备 */
if (nr_rawsyms <= 0) return RT_SUCCESS;
for (int i = 0; i < nr_rawsyms; i++)
{
uint16_t port_id;
char str_attach_args[MR_STRING_MAX];
snprintf(str_attach_args, sizeof(str_attach_args),
"net_af_packet%d,iface=%s,qpairs=%d", i, raw_devsyms[i], sc->nr_serv_thread);
int ret = rte_eth_dev_attach(str_attach_args, &port_id);
if (ret < 0)
{
MR_ERROR("Attaching raw socket device %s failed, Errno = %d",
raw_devsyms[i], ret);
return RT_ERR;
}
struct phydev * phydev = phydev_alloc(phydev_main, port_id);
snprintf(phydev->symbol, sizeof(phydev->symbol), "%s", raw_devsyms[i]);
snprintf(phydev->info.symbol, sizeof(phydev->info.symbol), "%s", raw_devsyms[i]);
phydev->port_id = port_id;
}
return RT_SUCCESS;
}
#endif
/* VIRTIO Devices */
static int phydev_scan_virtio_user(struct sc_main * sc, struct phydev_main * phydev_main)
{
char virtio_syms[MR_SYMBOL_MAX][MR_PHYDEV_MAX] = {{0}};
unsigned int nr_virtio_syms = 0;
phydev_query_device_list(sc, "virtio_user", virtio_syms, &nr_virtio_syms);
if (nr_virtio_syms < 0)
{
return RT_SUCCESS;
}
for (int i = 0; i < nr_virtio_syms; i++)
{
uint16_t port_id;
char vdev_name[MR_STRING_MAX];
/* dpdk会根据vdev_name获取对应的驱动,所以针对virtio类型的设备,此处命名必须为virtio_user开头 */
snprintf(vdev_name, sizeof(vdev_name) - 1, "virtio_user%d", i);
char vdev_args[MR_STRING_MAX];
snprintf(vdev_args, sizeof(vdev_args) - 1, "queues=%d,queue_size=1024,path=/dev/vhost-net,iface=%s", sc->nr_io_thread, virtio_syms[i]);
int ret = rte_eal_hotplug_add("vdev", vdev_name, vdev_args);
if (ret < 0)
{
MR_ERROR("Attaching virtio-user device %s failed, Errno = %d", virtio_syms[i], ret);
return RT_ERR;
}
ret = rte_eth_dev_get_port_by_name(vdev_name, &port_id);
if (ret < 0)
{
rte_eal_hotplug_remove("vdev", vdev_name);
MR_ERROR("Cannot find added vdev %s failed, Errno = %d", virtio_syms[i], ret);
return RT_ERR;
}
struct phydev * phydev = phydev_alloc(phydev_main, port_id);
snprintf(phydev->symbol, sizeof(phydev->symbol), "%s", virtio_syms[i]);
snprintf(phydev->info.symbol, sizeof(phydev->info.symbol), "%s", virtio_syms[i]);
phydev->info.type = __PHYDEV_INFO_TYPE_VDEV;
phydev->port_id = port_id;
}
return RT_SUCCESS;
}
static struct rte_pci_device * __dev_info_to_pci_dev(struct rte_eth_dev_info * dev_info)
{
struct rte_bus * bus = rte_bus_find_by_device(dev_info->device);
if (bus && !strcmp(bus->name, "pci"))
return RTE_DEV_TO_PCI(dev_info->device);
return NULL;
}
/* 设备扫描,根据目前的设备启用情况修正物理设备链 */
static int phydev_scan_uio(struct phydev_main * phydev_main)
{
unsigned int eth_dev_counts = rte_eth_dev_count_total();
/* 校验PCI设备 */
for (port_id_t port_id = 0; port_id < eth_dev_counts; port_id++)
{
// 从网卡取硬件信息,包括最大收、发队列数
struct rte_eth_dev_info dev_info;
struct phydev * phydev = NULL;
rte_eth_dev_info_get(port_id, &dev_info);
struct rte_pci_device * pci_dev = __dev_info_to_pci_dev(&dev_info);
if (pci_dev != NULL)
{
phydev = phydev_lookup_by_pci_addr(phydev_main, pci_dev->addr);
}
/* HWFILE里面没有定义,或虚拟设备 */
if (phydev == NULL)
phydev = phydev_alloc(phydev_main, port_id);
MR_VERIFY_MALLOC(phydev);
phydev->port_id = port_id;
phydev->info.nr_rxq_max = dev_info.max_rx_queues;
phydev->info.nr_txq_max = dev_info.max_tx_queues;
rte_eth_macaddr_get(phydev->port_id, &phydev->info.phy_ether_addr);
rte_eth_dev_get_mtu(phydev->port_id, &phydev->info.default_mtu);
// TODO: 处理DPDK的虚设备
if (pci_dev != NULL)
{
phydev->info.pci_addr = pci_dev->addr;
}
char str_macaddr[MR_SYMBOL_MAX];
rte_ether_format_addr(str_macaddr, sizeof(str_macaddr), &phydev->info.phy_ether_addr);
phydev_adjust_info_by_hwfile(phydev_main, phydev);
MR_DEBUG("PCI device scan: physical device %s, PORT_ID=%d, MAC: %s", phydev->symbol, port_id, str_macaddr);
}
return RT_SUCCESS;
}
static int phydev_hwfile_load(struct phydev_main * phydev_main, const char * hwfile)
{
FILE * f = fopen(hwfile, "rb");
if (f == NULL)
{
MR_ERROR("Cannot open hardware file %s: %s", hwfile, strerror(errno));
return RT_ERR;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); // same as rewind(f);
char * string = malloc(fsize + 1);
MR_VERIFY_MALLOC(string);
/* 读文件 */
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
cJSON * j_hwfile = cJSON_Parse(string);
if (j_hwfile == NULL)
{
MR_ERROR("Hardware file %s parse failed. ", hwfile);
return RT_ERR;
}
phydev_main->j_hwfile = j_hwfile;
return RT_SUCCESS;
}
/* 在EAL启动前调用,生成ARGC, ARGV等参数 */
int phydev_early_init(struct sc_main * sc)
{
assert(sc->phydev_main == NULL);
/* EAL环境还没有初始化,用malloc申请内存 */
sc->phydev_main = malloc(sizeof(struct phydev_main));
MR_VERIFY_MALLOC(sc->phydev_main);
struct phydev_main * phydev_main = sc->phydev_main;
memset(phydev_main, 0, sizeof(struct phydev_main));
TAILQ_INIT(&sc->phydev_main->device_list);
TAILQ_INIT(&sc->phydev_main->device_whitelist_list);
/* 选项:是否启用早期设备扫描 */
MESA_load_profile_uint_def(sc->local_cfgfile, "phydev", "early_scan_enable", &phydev_main->en_early_scan,
PHYDEV_DEFAULT_EN_EARLY_SCAN);
/* 选项:是否加载硬件信息文件 */
MESA_load_profile_uint_def(sc->local_cfgfile, "phydev", "load_hwfile_enable", &phydev_main->en_load_hwfile,
PHYDEV_DEFAULT_EN_LOAD_HWFILE);
/* 选项:是否从全局配置文件中读取物理设备配置 */
MESA_load_profile_uint_def(sc->local_cfgfile, "phydev", "load_global_cfg_enable", &phydev_main->en_load_g_cfg,
PHYDEV_DEFAULT_EN_LOAD_G_CFG);
if (sc->phydev_main->en_load_hwfile)
phydev_hwfile_load(sc->phydev_main, sc->local_hwfile);
return phydev_main->en_early_scan ? phydev_early_scan(sc, phydev_main) : 0;
}
void phydev_deinit(struct sc_main * sc)
{
for (unsigned int port_id = 0; port_id < rte_eth_dev_count_total(); port_id++)
{
rte_eth_dev_stop(port_id);
rte_eth_dev_close(port_id);
MR_INFO("Port %d closed.", port_id);
}
return;
}
void * phydev_link_state_update_thread(void * arg);
/* 初始化 */
int phydev_init(struct sc_main * sc)
{
if (phydev_scan_uio(sc->phydev_main) < 0)
{
MR_ERROR("Physical device scan failed. ");
return RT_ERR;
}
#if 0
if (phydev_scan_raw_socket(sc, sc->phydev_main) < 0)
{
MR_ERROR("Physical device in raw socket scan failed.");
return RT_ERR;
}
#endif
if (phydev_scan_virtio_user(sc, sc->phydev_main) < 0)
{
MR_ERROR("Physical device in virtio-user scan failed.");
return RT_ERR;
}
struct phydev * phydev_iter;
TAILQ_FOREACH(phydev_iter, &sc->phydev_main->device_list, next)
{
if (sc->phydev_main->en_load_g_cfg)
phydev_config_by_g_cfg(phydev_iter, sc->local_cfgfile);
if (phydev_setup(sc, sc->phydev_main, phydev_iter) == RT_SUCCESS)
continue;
return RT_ERR;
}
/* 检查物理设备列表是否为空。如果为空,告警 */
if (TAILQ_EMPTY(&sc->phydev_main->device_list))
{
MR_WARNING("No physical devices found.");
}
/* 启动物理设备状态更新线程 */
pthread_t __pid_phydev_link_update;
int ret = pthread_create(&__pid_phydev_link_update, NULL, phydev_link_state_update_thread, sc->phydev_main);
if (ret < 0)
{
MR_ERROR("PHYDEV link state update thread create failed: %s", strerror(errno));
return RT_ERR;
}
return RT_SUCCESS;
}
void phydev_stat_get(struct phydev * phydev, struct phydev_stat * phydev_stat)
{
rte_eth_stats_get(phydev->port_id, &phydev->stat.rte);
*phydev_stat = phydev->stat;
}
void phydev_stat_last_save(struct phydev * phydev, struct phydev_stat * phydev_stat_last)
{
phydev->stat_last = *phydev_stat_last;
}
void phydev_stat_last_get(struct phydev * phydev, struct phydev_stat * phydev_stat_last)
{
*phydev_stat_last = phydev->stat_last;
}
void * phydev_link_state_update_thread(void * arg)
{
struct phydev_main * phydev_main = (struct phydev_main *)arg;
pthread_detach(pthread_self());
mr_thread_setname(pthread_self(), "MRSRV_PHYDEV_LINK_UPDATE");
while (g_keep_running)
{
struct phydev * phydev_iter;
TAILQ_FOREACH(phydev_iter, &phydev_main->device_list, next)
{
struct rte_eth_link _eth_link;
rte_eth_link_get_nowait(phydev_iter->port_id, &_eth_link);
phydev_iter->link_status.link_speed = _eth_link.link_speed;
phydev_iter->link_status.link_autoneg = _eth_link.link_autoneg;
phydev_iter->link_status.link_duplex = _eth_link.link_duplex;
phydev_iter->link_status.link_status = _eth_link.link_status;
}
sleep(1);
}
return (void *)0;
}
|