-
Notifications
You must be signed in to change notification settings - Fork 108
/
dpdk.cpp
2262 lines (2105 loc) · 67.7 KB
/
dpdk.cpp
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 <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/resource.h>
#include <string.h>
#include "pstat.h"
#include "tools.h"
#include "tools_global.h"
#include "sync.h"
#include "dpdk.h"
#if HAVE_LIBDPDK
#define ALLOW_EXPERIMENTAL_API 1
#include <rte_config.h>
#include <rte_common.h>
#include <rte_errno.h>
#include <rte_log.h>
#include <rte_malloc.h>
#include <rte_memory.h>
#include <rte_eal.h>
#include <rte_launch.h>
#include <rte_atomic.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_per_lcore.h>
#include <rte_branch_prediction.h>
#include <rte_interrupts.h>
#include <rte_random.h>
#include <rte_debug.h>
#include <rte_ether.h>
#include <rte_ethdev.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_bus.h>
#if HAVE_LIBDPDK_VDEV
#include <rte_bus_vdev.h>
#endif
extern string opt_dpdk_cpu_cores;
extern string opt_dpdk_cpu_cores_map;
extern int opt_dpdk_main_thread_lcore;
extern string opt_dpdk_read_thread_lcore;
extern string opt_dpdk_worker_thread_lcore;
extern string opt_dpdk_worker2_thread_lcore;
extern int opt_dpdk_memory_channels;
extern string opt_dpdk_pci_device;
extern int opt_dpdk_force_max_simd_bitwidth;
extern int opt_dpdk_nb_mbufs;
extern bool opt_dpdk_nb_mbufs_strictly;
extern int opt_dpdk_pkt_burst;
extern int opt_dpdk_ring_size;
extern int opt_dpdk_mempool_cache_size;
extern int opt_dpdk_batch_read;
extern int opt_dpdk_mbufs_in_packetbuffer;
extern int opt_dpdk_timer_reset_interval;
extern vector<string> opt_dpdk_vdev;
#define MAXIMUM_SNAPLEN 262144
#define DPDK_ARGC_MAX 64
#define DPDK_DEF_LOG_LEV RTE_LOG_ERR
#define DPDK_LIB_NAME ((char*)"vm_dpdk")
#if DPDK_ENV_CFG
#define DPDK_CFG_ENV_NAME "DPDK_CFG"
#define DPDK_DEF_CFG "--log-level=error -l0 -dlibrte_pmd_e1000.so -dlibrte_pmd_ixgbe.so -dlibrte_mempool_ring.so"
#define DPDK_CFG_MAX_LEN 1024
#endif
#define DPDK_PORTID_MAX (64 * 1024U - 1)
#define DPDK_NB_MBUFS ((opt_dpdk_nb_mbufs ? opt_dpdk_nb_mbufs : 1024) * (opt_dpdk_nb_mbufs_strictly ? 1 : 1024))
#define DPDK_DEF_MAC_ADDR "00:00:00:00:00:00"
#define DPDK_TX_BUF_NAME "tx_buffer"
#define DPDK_PREFIX "dpdk:"
#define DPDK_MAC_ADDR_SIZE 32
#define DPDK_PCI_ADDR_SIZE 16
#define MAX_PKT_BURST (opt_dpdk_pkt_burst ? opt_dpdk_pkt_burst : 32)
#define RING_SIZE (opt_dpdk_ring_size ? opt_dpdk_ring_size * 1024 : DPDK_NB_MBUFS)
#define MBUF_POOL_NAME "mbuf_pool"
#define MEMPOOL_CACHE_SIZE (opt_dpdk_mempool_cache_size ? opt_dpdk_mempool_cache_size : 512)
#define DPDK_ERR_PERM_MSG "permission denied, DPDK needs root permission"
// #ifdef HAVE_STRUCT_RTE_ETHER_ADDR
#define ETHER_ADDR_TYPE struct rte_ether_addr
// #else
// #define ETHER_ADDR_TYPE struct ether_addr
// #endif
#define DPDK_ENV_CFG 0
#define DPDK_TIMESTAMP_IN_MBUF 1
#define DPDK_WAIT_FOR_EMPTY_RING_IF_FULL 1
#define WORKER2_THREAD_SUPPORT 0
#define DPDK_ZC_SUPPORT 0
#define DEBUG_CYCLES false
#define DEBUG_CYCLES_MAX_LT_MS 100
#define DEBUG_EXT_STAT false
#if DEBUG_CYCLES
struct sDpdk_cycles {
u_int64_t count;
u_int64_t sum;
u_int64_t min;
u_int64_t max;
u_int64_t begin;
u_int64_t end;
bool reset;
inline void setBegin() {
if(reset) {
memset(this, 0, sizeof(*this));
reset = false;
}
begin = rte_get_timer_cycles();
}
inline void setEnd() {
end = rte_get_timer_cycles();
u_int64_t diff = end - begin;
++count;
sum += diff;
if(!min || diff < min) min = diff;
if(diff > max) max = diff;
}
};
#endif
struct dpdk_ts_helper{
uint64_t start_time;
uint64_t start_cycles;
uint64_t hz;
};
class cDpdkTools {
public:
enum eTypeLcore {
_tlc_read,
_tlc_worker,
_tlc_worker2
};
public:
cDpdkTools();
void init();
void setLCoresMap();
int getFreeLcore(eTypeLcore type, int numa_node);
void setUseLcore(int lcore);
void setFreeLcore(int lcore);
string getAllCores(bool without_main, bool detect_ht);
string getCoresMap();
int getMainThreadLcore();
private:
int getFreeLcore(map<int, bool> &main_map, int numa_node);
bool lcoreIsUsed(int lcore);
bool lcoreIsInAny(int lcore);
private:
int main_thread_lcore;
map<int, bool> read_lcores;
map<int, bool> worker_lcores;
map<int, bool> worker2_lcores;
map<int, bool> used_lcores;
string lcores_map_str;
map<int, list<int> > lcores_map;
volatile int _sync_lcore;
};
struct sDpdk {
uint16_t portid;
bool portid_set;
int must_clear_promisc;
rte_mempool *pktmbuf_pool;
rte_ring *rx_to_worker_ring;
#if WORKER2_THREAD_SUPPORT
rte_ring *worker_to_worker2_ring;
#endif
u_int64_t prev_ts_us;
u_int64_t curr_ts_us;
rte_eth_stats prev_stats;
rte_eth_stats curr_stats;
dpdk_ts_helper ts_helper;
ETHER_ADDR_TYPE eth_addr;
char mac_addr[DPDK_MAC_ADDR_SIZE];
char pci_addr[DPDK_PCI_ADDR_SIZE];
sDpdkConfig config;
uint64_t pps;
uint64_t bps;
uint64_t bpf_drop;
uint64_t ring_full_drop;
#if WORKER2_THREAD_SUPPORT
uint64_t ring2_full_drop;
#endif
bool terminating;
int rte_read_thread_pid;
int rte_worker_thread_pid;
#if WORKER2_THREAD_SUPPORT
int rte_worker2_thread_pid;
#endif
pstat_data rte_read_thread_pstat_data[2];
pstat_data rte_worker_thread_pstat_data[2];
#if WORKER2_THREAD_SUPPORT
pstat_data rte_worker2_thread_pstat_data[2];
#endif
volatile bool initialized;
#if DEBUG_CYCLES
sDpdk_cycles cycles[10];
#endif
bool cycles_reset;
sDpdk() {
memset((void*)this, 0, sizeof(*this));
}
};
static int rte_read_thread(void *arg);
static int rte_worker_thread(void *arg);
#if WORKER2_THREAD_SUPPORT
static int rte_worker2_thread(void *arg);
#endif
static inline uint32_t dpdk_gather_data(unsigned char *data, uint32_t len, struct rte_mbuf *mbuf);
static inline void dpdk_process_packet(sDpdk *dpdk, rte_mbuf *mbuff, u_int64_t timestamp_us);
static inline void dpdk_process_packet_2__std(sDpdk *dpdk, rte_mbuf *mbuff, u_int64_t timestamp_us
#if WORKER2_THREAD_SUPPORT
,bool free_mbuff
#endif
);
static inline void dpdk_process_packet_2__mbufs_in_packetbuffer(sDpdk *dpdk, rte_mbuf *mbuff, u_int64_t timestamp_us
#if WORKER2_THREAD_SUPPORT
,bool free_mbuff
#endif
);
static inline u_int64_t get_timestamp_us(sDpdk *dpdk);
static int dpdk_pre_init(string *error_str);
static uint16_t portid_by_device(const char * device);
#if DPDK_ENV_CFG
static int parse_dpdk_cfg(char* dpdk_cfg,char** dargv);
#endif
static void dpdk_eval_res(int res_no, const char *cust_error, int syslog_print, string *error_str, const char *fmt, ...);
static int dpdk_init_timer(sDpdk *dpdk);
static void eth_addr_str(ETHER_ADDR_TYPE *addrp, char* mac_str, int len);
static int check_link_status(uint16_t portid, struct rte_eth_link *plink);
static int is_dpdk_pre_inited = 0;
#if DPDK_ENV_CFG
static char dpdk_cfg_buf[DPDK_CFG_MAX_LEN];
#endif
static rte_eth_conf port_conf;
#if DPDK_TIMESTAMP_IN_MBUF == 1
static rte_mbuf_dynfield timestamp_dynfield_desc;
static int timestamp_dynfield_offset = -1;
#endif
static cDpdkTools *dpdk_tools;
sDpdkHandle *create_dpdk_handle() {
return(new sDpdk);
}
void destroy_dpdk_handle(sDpdkHandle *dpdk) {
if(dpdk->portid_set) {
if(dpdk->must_clear_promisc) {
rte_eth_promiscuous_disable(dpdk->portid);
}
rte_eth_dev_stop(dpdk->portid);
rte_eth_dev_close(dpdk->portid);
}
delete dpdk;
}
int dpdk_activate(sDpdkConfig *config, sDpdk *dpdk, std::string *error) {
int ret = PCAP_ERROR;
uint16_t nb_ports = 0;
uint16_t portid = DPDK_PORTID_MAX;
struct rte_eth_rxconf rxq_conf;
struct rte_eth_txconf txq_conf;
// port_conf.rxmode.split_hdr_size = 0; // obsolete
port_conf.txmode.mq_mode = RTE_ETH_MQ_TX_NONE;
struct rte_eth_conf local_port_conf = port_conf;
struct rte_eth_dev_info dev_info;
int is_port_up = 0;
struct rte_eth_link link;
//init EAL; fail if we have insufficient permission
int is_dpdk_pre_inited_old = is_dpdk_pre_inited;
ret = dpdk_pre_init(error);
if(ret > 0 && is_dpdk_pre_inited_old <= 0 && is_dpdk_pre_inited > 0) {
config->init_in_activate = true;
} else if(ret < 0) {
return(PCAP_ERROR);
} else if(ret == 0) {
*error = "DPDK is not available on this machine";
return(PCAP_ERROR_NO_SUCH_DEVICE);
}
ret = dpdk_init_timer(dpdk);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - dpdk_init_timer",
config->device);
if(ret < 0) {
return(PCAP_ERROR);
}
nb_ports = rte_eth_dev_count_avail();
if(nb_ports == 0) {
dpdk_eval_res(0, "no ethernet ports", 2, error,
"dpdk_activate(%s) - rte_eth_dev_count_avail",
config->device);
return(PCAP_ERROR);
}
if(opt_dpdk_vdev.size()) {
ret = rte_eth_dev_get_port_by_name(config->device, &portid);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_get_port_by_name(%s)",
config->device,
config->device);
} else {
ret = -1;
}
if(ret < 0) {
portid = portid_by_device(config->device);
if(portid == DPDK_PORTID_MAX) {
dpdk_eval_res(0, "portid is invalid", 2, error,
"dpdk_activate(%s) - portid_by_device(%s)",
config->device,
config->device);
return(PCAP_ERROR_NO_SUCH_DEVICE);
}
}
int numa_node = rte_eth_dev_socket_id(portid);
dpdk_eval_res(numa_node < 0 ? -rte_errno : 0, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_socket_id(%i) - rslt (numa_node): %i",
config->device,
portid,
numa_node);
dpdk->portid = portid;
dpdk->portid_set = true;
if(config->snapshot <= 0 || config->snapshot > MAXIMUM_SNAPLEN) {
config->snapshot = MAXIMUM_SNAPLEN;
}
// create the mbuf pool
dpdk->pktmbuf_pool = rte_pktmbuf_pool_create((string(MBUF_POOL_NAME) + "_" + config->device).c_str(), DPDK_NB_MBUFS,
MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
rte_socket_id());
dpdk_eval_res(dpdk->pktmbuf_pool == NULL ? -rte_errno : 0, dpdk->pktmbuf_pool == NULL && !rte_errno ? "failed allocation mbuf pool" : NULL, 2, error,
"dpdk_activate(%s) - rte_pktmbuf_pool_create",
config->device);
if(dpdk->pktmbuf_pool == NULL) {
return(PCAP_ERROR);
}
// config dev
ret = rte_eth_dev_info_get(portid, &dev_info);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_info_get(%i)",
config->device,
portid);
if(ret < 0) {
return(PCAP_ERROR);
}
if(dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE) {
local_port_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
}
// only support 1 queue
ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_configure(%i)",
config->device,
portid);
if(ret < 0) {
return(PCAP_ERROR);
}
// adjust rx tx
extern int opt_dpdk_nb_rx;
extern int opt_dpdk_nb_tx;
uint16_t nb_rx = opt_dpdk_nb_rx;
uint16_t nb_tx = opt_dpdk_nb_tx;
ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rx, &nb_tx);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_adjust_nb_rx_tx_desc(%i)",
config->device,
portid);
if(ret < 0) {
return(PCAP_ERROR);
}
// get MAC addr
rte_eth_macaddr_get(portid, &(dpdk->eth_addr));
eth_addr_str(&(dpdk->eth_addr), dpdk->mac_addr, DPDK_MAC_ADDR_SIZE-1);
// init one RX queue
rxq_conf = dev_info.default_rxconf;
rxq_conf.offloads = local_port_conf.rxmode.offloads;
rxq_conf.rx_free_thresh = MAX_PKT_BURST;
ret = rte_eth_rx_queue_setup(portid, 0, nb_rx,
rte_eth_dev_socket_id(portid),
&rxq_conf,
dpdk->pktmbuf_pool);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_rx_queue_setup(%i)",
config->device,
portid);
if(ret < 0) {
return(PCAP_ERROR);
}
// init one TX queue
txq_conf = dev_info.default_txconf;
txq_conf.offloads = local_port_conf.txmode.offloads;
ret = rte_eth_tx_queue_setup(portid, 0, nb_tx,
rte_eth_dev_socket_id(portid),
&txq_conf);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_tx_queue_setup(%i)",
config->device,
portid);
if(ret < 0) {
return(PCAP_ERROR);
}
// Initialize TX buffers
rte_eth_dev_tx_buffer *tx_buffer;
tx_buffer = (rte_eth_dev_tx_buffer*)rte_zmalloc_socket(DPDK_TX_BUF_NAME,
RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
rte_eth_dev_socket_id(portid));
dpdk_eval_res(tx_buffer == NULL ? -rte_errno : 0, NULL, 2, error,
"dpdk_activate(%s) - rte_zmalloc_socket",
config->device);
if(tx_buffer == NULL) {
return(PCAP_ERROR);
}
ret = rte_eth_tx_buffer_init(tx_buffer, MAX_PKT_BURST);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_tx_buffer_init",
config->device);
if(ret < 0) {
return(PCAP_ERROR);
}
if(config->type_worker_thread != _dpdk_twt_na) {
dpdk->rx_to_worker_ring = rte_ring_create((string("rx_to_worker") + "_" + config->device).c_str(), RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
dpdk_eval_res(dpdk->rx_to_worker_ring == NULL ? -rte_errno : 0, NULL, 2, error,
"dpdk_activate(%s) - rte_ring_create(rx_to_worker)",
config->device);
if(dpdk->rx_to_worker_ring == NULL) {
return(PCAP_ERROR);
}
}
#if WORKER2_THREAD_SUPPORT
if(config->type_worker_thread == _dpdk_twt_rte && config->type_worker2_thread == _dpdk_tw2t_rte) {
dpdk->worker_to_worker2_ring = rte_ring_create((string("worker_to_worker2") + "_" + config->device).c_str(), RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
dpdk_eval_res(dpdk->worker_to_worker2_ring == NULL ? -rte_errno : 0, NULL, 2, error,
"dpdk_activate(%s) - rte_ring_create(worker_to_worker2)",
config->device);
if(dpdk->rx_to_worker_ring == NULL) {
return(PCAP_ERROR);
}
}
#endif
dpdk->config = *config;
if(config->type_worker_thread == _dpdk_twt_rte) {
int lcore_id = dpdk_tools->getFreeLcore(cDpdkTools::_tlc_worker, numa_node);
dpdk_eval_res(lcore_id, lcore_id < 0 ? "not available free lcore for worker thread" : NULL, 2, error,
"dpdk_activate(%s) - getFreeLcore(cDpdkTools::_tlc_worker)",
config->device);
if(lcore_id < 0) {
return(PCAP_ERROR);
} else {
ret = rte_eal_remote_launch(rte_worker_thread, dpdk, lcore_id);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eal_remote_launch(%i)",
config->device,
lcore_id);
if(ret < 0) {
return(PCAP_ERROR);
}
dpdk_tools->setUseLcore(lcore_id);
}
}
#if WORKER2_THREAD_SUPPORT
if(config->type_worker2_thread == _dpdk_tw2t_rte) {
int lcore_id = dpdk->tools->getFreeLcore(cDpdkTools::_tlc_worker2, numa_node);
dpdk_eval_res(lcore_id, lcore_id < 0 ? "not available free lcore for worker2 thread" : NULL, 2, error,
"dpdk_activate(%s) - getFreeLcore(cDpdkTools::_tlc_worker2)",
config->device);
if(lcore_id < 0) {
return(PCAP_ERROR);
} else {
ret = rte_eal_remote_launch(rte_worker2_thread, dpdk, lcore_id);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eal_remote_launch(%i)",
config->device,
lcore_id);
if(ret < 0) {
return(PCAP_ERROR);
}
dpdk->tools->setUseLcore(lcore_id);
}
}
#endif
if(config->type_read_thread == _dpdk_trt_rte) {
int lcore_id = dpdk_tools->getFreeLcore(cDpdkTools::_tlc_read, numa_node);
dpdk_eval_res(lcore_id, lcore_id < 0 ? "not available free lcore for read thread" : NULL, 2, error,
"dpdk_activate(%s) - getFreeLcore(cDpdkTools::_tlc_read)",
config->device);
if(lcore_id < 0) {
return(PCAP_ERROR);
} else {
ret = rte_eal_remote_launch(rte_read_thread, dpdk, lcore_id);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eal_remote_launch(%i)",
config->device,
lcore_id);
if(ret < 0) {
return(PCAP_ERROR);
}
dpdk_tools->setUseLcore(lcore_id);
}
}
// Start device
ret = rte_eth_dev_start(portid);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_start(%i)",
config->device,
portid);
if(ret < 0) {
return(PCAP_ERROR);
}
// set promiscuous mode
if(config->promisc) {
dpdk->must_clear_promisc=1;
rte_eth_promiscuous_enable(portid);
}
// check link status
for(int i = 0; i < (opt_dpdk_vdev.size() ? 2 : 1); i++) {
if(i == 1) {
if(opt_dpdk_vdev.size()) {
ret = rte_eth_dev_set_link_up(portid);
dpdk_eval_res(ret, NULL, 2, error,
"dpdk_activate(%s) - rte_eth_dev_set_link_up(%i)",
config->device,
portid);
} else {
break;
}
}
is_port_up = check_link_status(portid, &link);
dpdk_eval_res(is_port_up, is_port_up == 0 ? "link is down" : NULL, 2, error,
"dpdk_activate(%s) - check_link_status(%i)",
config->device,
portid);
if(is_port_up) {
break;
}
}
if(!is_port_up) {
return(PCAP_ERROR_IFACE_NOT_UP);
}
// reset statistics
dpdk_reset_statistics(dpdk, true);
/*
// format pcap_t
pd->portid = portid;
p->fd = pd->portid;
if(p->snapshot <=0 || p->snapshot> MAXIMUM_SNAPLEN)
{
p->snapshot = MAXIMUM_SNAPLEN;
}
p->linktype = DLT_EN10MB; // Ethernet, the 10MB is historical.
p->selectable_fd = p->fd;
p->read_op = pcap_dpdk_dispatch;
p->inject_op = pcap_dpdk_inject;
// using pcap_filter currently, though DPDK provides their own BPF function. Because DPDK BPF needs load a ELF file as a filter.
p->setfilter_op = install_bpf_program;
p->setdirection_op = NULL;
p->set_datalink_op = NULL;
p->getnonblock_op = pcap_dpdk_getnonblock;
p->setnonblock_op = pcap_dpdk_setnonblock;
p->stats_op = pcap_dpdk_stats;
p->cleanup_op = pcap_dpdk_close;
p->breakloop_op = pcap_breakloop_common;
// set default timeout
pd->required_select_timeout.tv_sec = 0;
pd->required_select_timeout.tv_usec = DPDK_DEF_MIN_SLEEP_MS*1000;
p->required_select_timeout = &pd->required_select_timeout;
*/
rte_eth_dev_get_name_by_port(portid,dpdk->pci_addr);
syslog(LOG_INFO, "DPDK - Port %d device: %s, MAC:%s, PCI:%s\n",
portid, config->device, dpdk->mac_addr, dpdk->pci_addr);
syslog(LOG_INFO, "DPDK - Port %d Link Up. Speed %u Mbps - %s\n",
portid, link.link_speed,
(link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ? ("full-duplex") : ("half-duplex\n"));
return(0);
}
int dpdk_do_pre_init(std::string *error) {
return(dpdk_pre_init(error));
}
void dpdk_set_initialized(sDpdkHandle *dpdk) {
dpdk->initialized = true;
}
void dpdk_reset_statistics(sDpdkHandle *dpdk, bool flush_buffer) {
if(flush_buffer) {
u_int16_t nb_rx;
rte_mbuf *pkts_burst[MAX_PKT_BURST];
while((nb_rx = rte_eth_rx_burst(dpdk->portid, 0, pkts_burst, MAX_PKT_BURST)) > 0) {
for(u_int16_t i = 0; i < nb_rx; i++) {
rte_pktmbuf_free(pkts_burst[i]);
}
}
}
rte_eth_stats_reset(dpdk->portid);
dpdk->prev_ts_us = get_timestamp_us(dpdk);
rte_eth_stats_get(dpdk->portid, &(dpdk->prev_stats));
}
int dpdk_read_proc(sDpdk *dpdk) {
rte_mbuf *pkts_burst[MAX_PKT_BURST];
u_int64_t timestamp_us;
if(dpdk->rx_to_worker_ring) {
#if 0
while(true) {
u_int16_t nb_rx = rte_eth_rx_burst(dpdk->portid, 0, pkts_burst, MAX_PKT_BURST);
if(nb_rx > 0) {
for(u_int16_t i = 0; i < nb_rx; i++) {
rte_pktmbuf_free(pkts_burst[i]);
}
//printf(" * %i\n", nb_rx);
}
}
return(0);
#else
int nb_rx_sum = 0;
for(int i = 0; i < dpdk->config.iterations_per_call; i++) {
int nb_rx = (int)rte_eth_rx_burst(dpdk->portid, 0, pkts_burst, MAX_PKT_BURST);
if(likely(nb_rx)) {
#if DPDK_TIMESTAMP_IN_MBUF
timestamp_us = get_timestamp_us(dpdk);
for(u_int16_t i = 0; i < nb_rx; i++) {
#if DPDK_TIMESTAMP_IN_MBUF == 1
*RTE_MBUF_DYNFIELD(pkts_burst[i], timestamp_dynfield_offset, u_int64_t*) = timestamp_us;
#elif DPDK_TIMESTAMP_IN_MBUF == 2
*(u_int64_t*)&pkts_burst[i]->dynfield1[0] = timestamp_us;
#endif
}
#endif
u_int16_t nb_rx_enqueue = rte_ring_enqueue_burst(dpdk->rx_to_worker_ring, (void *const *)pkts_burst, nb_rx, NULL);
if(nb_rx_enqueue < nb_rx) {
for(u_int16_t i = nb_rx_enqueue; i < nb_rx; i++) {
rte_pktmbuf_free(pkts_burst[i]);
}
dpdk->ring_full_drop += nb_rx - nb_rx_enqueue;
}
nb_rx_sum += nb_rx;
} else if(dpdk->config.read_usleep_if_no_packet) {
if(dpdk->config.read_usleep_type == _dpdk_usleep_type_rte) {
rte_delay_us_block(dpdk->config.read_usleep_if_no_packet);
} else if(dpdk->config.read_usleep_type == _dpdk_usleep_type_rte_pause) {
rte_pause();
} else {
USLEEP(dpdk->config.read_usleep_if_no_packet);
}
}
}
return(nb_rx_sum);
#endif
} else {
int nb_rx = (int)rte_eth_rx_burst(dpdk->portid, 0, pkts_burst, MAX_PKT_BURST);
if(likely(nb_rx)) {
timestamp_us = get_timestamp_us(dpdk);
for(int i = 0; i < nb_rx; i++) {
dpdk_process_packet(dpdk, pkts_burst[i], timestamp_us);
}
}
return(nb_rx);
}
}
int dpdk_worker_proc(sDpdk *dpdk) {
#if DEBUG_CYCLES
dpdk->cycles[9].setBegin();
#endif
rte_mbuf *pkts_burst[MAX_PKT_BURST];
#if not DPDK_TIMESTAMP_IN_MBUF
u_int64_t timestamp_us;
#endif
int nb_rx = (int)rte_ring_dequeue_burst(dpdk->rx_to_worker_ring, (void**)pkts_burst, MAX_PKT_BURST, NULL);
if(likely(nb_rx)) {
#if not DPDK_TIMESTAMP_IN_MBUF
timestamp_us = get_timestamp_us(dpdk);
#endif
for(int i = 0; i < nb_rx; i++) {
dpdk_process_packet(dpdk, pkts_burst[i],
#if DPDK_TIMESTAMP_IN_MBUF == 1
*RTE_MBUF_DYNFIELD(pkts_burst[i], timestamp_dynfield_offset, u_int64_t*)
#elif DPDK_TIMESTAMP_IN_MBUF == 2
*(u_int64_t*)&pkts_burst[i]->dynfield1[0]
#else
timestamp_us
#endif
);
}
}
#if DEBUG_CYCLES
dpdk->cycles[9].setEnd();
#endif
return(nb_rx);
}
int pcap_dpdk_stats(sDpdk *dpdk, pcap_stat *ps, string *str_out) {
dpdk->curr_ts_us = get_timestamp_us(dpdk);
rte_eth_stats_get(dpdk->portid,&(dpdk->curr_stats));
if(ps) {
ps->ps_recv = dpdk->curr_stats.ipackets;
ps->ps_drop = dpdk->curr_stats.ierrors;
ps->ps_drop += dpdk->bpf_drop;
ps->ps_ifdrop = dpdk->curr_stats.imissed;
}
uint64_t delta_pkt = dpdk->curr_stats.ipackets - dpdk->prev_stats.ipackets;
uint64_t delta_usec = dpdk->curr_ts_us - dpdk->prev_ts_us;
uint64_t delta_bit = (dpdk->curr_stats.ibytes-dpdk->prev_stats.ibytes)*8;
dpdk->pps = (uint64_t)(delta_pkt*1e6f/delta_usec);
dpdk->bps = (uint64_t)(delta_bit*1e6f/delta_usec);
if(str_out) {
ostringstream outStr;
outStr << fixed
<< "DPDK "
<< dpdk->config.device
<< " portid " << dpdk->portid
<< " ["
<< setprecision(2) << dpdk->bps/1e6f << "Mb/s"
<< "; packets: " << dpdk->curr_stats.ipackets
<< "; errors: " << dpdk->curr_stats.ierrors
<< "; imissed: " << dpdk->curr_stats.imissed
<< "; nombuf: " << dpdk->curr_stats.rx_nombuf;
if(dpdk->rx_to_worker_ring) {
outStr << "; ring count: " << rte_ring_count(dpdk->rx_to_worker_ring);
outStr << "; ring full: " << dpdk->ring_full_drop;
}
#if WORKER2_THREAD_SUPPORT
if(dpdk->worker_to_worker2_ring) {
outStr << "; ring2 count: " << rte_ring_count(dpdk->worker_to_worker2_ring);
outStr << "; ring2 full: " << dpdk->ring2_full_drop;
}
#endif
#if DEBUG_EXT_STAT
int len = rte_eth_xstats_get(dpdk->portid, NULL, 0);
if(len < 0) {
outStr << "; error: " << "rte_eth_xstats_get failed";
} else {
struct rte_eth_xstat *xstats = (rte_eth_xstat*)calloc(len, sizeof(*xstats));
if(xstats == NULL) {
outStr << "; error: " << "failed to calloc memory for xstats";
} else {
int ret = rte_eth_xstats_get(portid, xstats, len);
if(ret < 0 || ret > len) {
outStr << "; error: " << "rte_eth_xstats_get failed";
} else {
rte_eth_xstat_name *xstats_names = (rte_eth_xstat_name*)calloc(len, sizeof(*xstats_names));
if(xstats_names == NULL) {
outStr << "; error: " << "failed to calloc memory for xstats_names";
} else {
ret = rte_eth_xstats_get_names(portid, xstats_names, len);
if(ret < 0 || ret > len) {
outStr << "; error: " << "rte_eth_xstats_get_names failed";
} else {
for(int i = 0; i < len; i++) {
if(xstats[i].value > 0) {
outStr << "; " << xstats_names[i].name << ": " << xstats[i].value;
}
}
}
free(xstats_names);
}
}
free(xstats);
}
}
#endif
outStr << "]";
#if DEBUG_CYCLES
for(unsigned i = 0; i < sizeof(dpdk->cycles) / sizeof(dpdk->cycles[0]); i++) {
if(dpdk->cycles[i].count &&
(!DEBUG_CYCLES_MAX_LT_MS ||
dpdk->cycles[i].max * 1000000000 / dpdk->ts_helper.hz > DEBUG_CYCLES_MAX_LT_MS * 1000000ul)) {
outStr << " * C" << i
<< " " << dpdk->cycles[i].sum / dpdk->cycles[i].count * 1000000000 / dpdk->ts_helper.hz
<< " " << dpdk->cycles[i].min * 1000000000 / dpdk->ts_helper.hz
<< " " << dpdk->cycles[i].max * 1000000000 / dpdk->ts_helper.hz
<< endl;
}
dpdk->cycles[i].reset = true;
}
#endif
*str_out = outStr.str();
}
dpdk->prev_stats = dpdk->curr_stats;
dpdk->prev_ts_us = get_timestamp_us(dpdk);
return 0;
}
sDpdkConfig *dpdk_config(sDpdk *dpdk) {
return(&dpdk->config);
}
void dpdk_terminating(sDpdk *dpdk) {
dpdk->terminating = true;
}
double rte_read_thread_cpu_usage(sDpdk *dpdk) {
if(!dpdk->rte_read_thread_pid) {
return(-1);
}
if(dpdk->rte_read_thread_pstat_data[0].cpu_total_time) {
dpdk->rte_read_thread_pstat_data[1] = dpdk->rte_read_thread_pstat_data[0];
}
pstat_get_data(dpdk->rte_read_thread_pid, dpdk->rte_read_thread_pstat_data);
double ucpu_usage, scpu_usage;
if(dpdk->rte_read_thread_pstat_data[0].cpu_total_time && dpdk->rte_read_thread_pstat_data[1].cpu_total_time) {
pstat_calc_cpu_usage_pct(
&dpdk->rte_read_thread_pstat_data[0], &dpdk->rte_read_thread_pstat_data[1],
&ucpu_usage, &scpu_usage);
return(ucpu_usage + scpu_usage);
}
return(-1);
}
double rte_worker_thread_cpu_usage(sDpdk *dpdk) {
if(!dpdk->rte_worker_thread_pid) {
return(-1);
}
if(dpdk->rte_worker_thread_pstat_data[0].cpu_total_time) {
dpdk->rte_worker_thread_pstat_data[1] = dpdk->rte_worker_thread_pstat_data[0];
}
pstat_get_data(dpdk->rte_worker_thread_pid, dpdk->rte_worker_thread_pstat_data);
double ucpu_usage, scpu_usage;
if(dpdk->rte_worker_thread_pstat_data[0].cpu_total_time && dpdk->rte_worker_thread_pstat_data[1].cpu_total_time) {
pstat_calc_cpu_usage_pct(
&dpdk->rte_worker_thread_pstat_data[0], &dpdk->rte_worker_thread_pstat_data[1],
&ucpu_usage, &scpu_usage);
return(ucpu_usage + scpu_usage);
}
return(-1);
}
double rte_worker2_thread_cpu_usage(sDpdk *dpdk) {
#if WORKER2_THREAD_SUPPORT
if(!dpdk->rte_worker2_thread_pid) {
return(-1);
}
if(dpdk->rte_worker2_thread_pstat_data[0].cpu_total_time) {
dpdk->rte_worker2_thread_pstat_data[1] = dpdk->rte_worker2_thread_pstat_data[0];
}
pstat_get_data(dpdk->rte_worker2_thread_pid, dpdk->rte_worker2_thread_pstat_data);
double ucpu_usage, scpu_usage;
if(dpdk->rte_worker2_thread_pstat_data[0].cpu_total_time && dpdk->rte_worker2_thread_pstat_data[1].cpu_total_time) {
pstat_calc_cpu_usage_pct(
&dpdk->rte_worker2_thread_pstat_data[0], &dpdk->rte_worker2_thread_pstat_data[1],
&ucpu_usage, &scpu_usage);
return(ucpu_usage + scpu_usage);
}
#endif
return(-1);
}
string get_dpdk_cpu_cores(bool without_main, bool detect_ht) {
if(!without_main && !opt_dpdk_cpu_cores.empty()) {
return(opt_dpdk_cpu_cores);
}
cDpdkTools tools;
return(tools.getAllCores(without_main, detect_ht));
}
static int rte_read_thread(void *arg) {
sDpdk *dpdk = (sDpdk*)arg;
dpdk->rte_read_thread_pid = get_unix_tid();
setpriority(PRIO_PROCESS, dpdk->rte_read_thread_pid, -19);
syslog(LOG_INFO, "DPDK - READ (rte) THREAD %i\n", dpdk->rte_read_thread_pid);
while(!dpdk->initialized) {
USLEEP(1000);
}
dpdk_reset_statistics(dpdk, true);
void (*dpdk_process_packet_2)(sDpdk *dpdk, rte_mbuf *mbuff, u_int64_t timestamp_us
#if WORKER2_THREAD_SUPPORT
,bool free_mbuff
#endif
) =
opt_dpdk_mbufs_in_packetbuffer ?
dpdk_process_packet_2__mbufs_in_packetbuffer :
dpdk_process_packet_2__std;
#if DPDK_ZC_SUPPORT
extern int opt_dpdk_zc;
if(opt_dpdk_zc && dpdk->rx_to_worker_ring) {
rte_ring_zc_data zcd;
uint16_t nb_zcd, nb_rx;
while(!dpdk->terminating) {
nb_zcd = rte_ring_enqueue_zc_burst_start(dpdk->rx_to_worker_ring, MAX_PKT_BURST, &zcd, NULL);
if(nb_zcd > 0) {
nb_rx = rte_eth_rx_burst(dpdk->portid, 0, (rte_mbuf**)zcd.ptr1, zcd.n1);
if(nb_rx == zcd.n1 && nb_zcd > zcd.n1) {
nb_rx += rte_eth_rx_burst(dpdk->portid, 0, (rte_mbuf**)zcd.ptr2, nb_zcd - zcd.n1);
}
rte_ring_enqueue_zc_finish(dpdk->rx_to_worker_ring, nb_rx);
if(!nb_rx && dpdk->config.read_usleep_if_no_packet) {
if(dpdk->config.read_usleep_type == _dpdk_usleep_type_rte) {
rte_delay_us_block(dpdk->config.read_usleep_if_no_packet);
} else if(dpdk->config.read_usleep_type == _dpdk_usleep_type_rte_pause) {
rte_pause();
} else {
USLEEP(dpdk->config.read_usleep_if_no_packet);
}
}
} else {
++dpdk->ring_full_drop;
}
}
return 0;
}
#endif
if(opt_dpdk_batch_read > 1) {
unsigned pkts_burst_cnt;
rte_mbuf *pkts_burst[opt_dpdk_batch_read][MAX_PKT_BURST];
uint16_t nb_rx[opt_dpdk_batch_read];
u_int64_t timestamp_us[opt_dpdk_batch_read];
uint16_t nb_rx_enqueue;
if(dpdk->rx_to_worker_ring) {
while(!dpdk->terminating) {
pkts_burst_cnt = 0;
while(pkts_burst_cnt < (unsigned)opt_dpdk_batch_read) {
nb_rx[pkts_burst_cnt] = rte_eth_rx_burst(dpdk->portid, 0, pkts_burst[pkts_burst_cnt], MAX_PKT_BURST);
if(!nb_rx[pkts_burst_cnt]) {
break;
}
#if DPDK_TIMESTAMP_IN_MBUF
timestamp_us[pkts_burst_cnt] = get_timestamp_us(dpdk);
#endif
++pkts_burst_cnt;
}
if(likely(pkts_burst_cnt)) {
for(unsigned i = 0; i < pkts_burst_cnt; i++) {
#if DPDK_TIMESTAMP_IN_MBUF
for(u_int16_t j = 0; j < nb_rx[i]; j++) {
#if DPDK_TIMESTAMP_IN_MBUF == 1
*RTE_MBUF_DYNFIELD(pkts_burst[i][j], timestamp_dynfield_offset, u_int64_t*) = timestamp_us[i];
#elif DPDK_TIMESTAMP_IN_MBUF == 2
*(u_int64_t*)&pkts_burst[i][j]->dynfield1[0] = timestamp_us[i];
#endif
}
#endif
nb_rx_enqueue = rte_ring_enqueue_burst(dpdk->rx_to_worker_ring, (void *const *)pkts_burst[i], nb_rx[i], NULL);
if(unlikely(nb_rx_enqueue < nb_rx[i])) {
for(u_int16_t j = nb_rx_enqueue; j < nb_rx[i]; j++) {
rte_pktmbuf_free(pkts_burst[i][j]);
}
dpdk->ring_full_drop += nb_rx[i] - nb_rx_enqueue;
}
}
} else if(dpdk->config.read_usleep_if_no_packet) {
if(dpdk->config.read_usleep_type == _dpdk_usleep_type_rte) {
rte_delay_us_block(dpdk->config.read_usleep_if_no_packet);
} else if(dpdk->config.read_usleep_type == _dpdk_usleep_type_rte_pause) {
rte_pause();
} else {
USLEEP(dpdk->config.read_usleep_if_no_packet);
}
}
}
} else {
while(!dpdk->terminating) {
pkts_burst_cnt = 0;
while(pkts_burst_cnt < (unsigned)opt_dpdk_batch_read) {
nb_rx[pkts_burst_cnt] = rte_eth_rx_burst(dpdk->portid, 0, pkts_burst[pkts_burst_cnt], MAX_PKT_BURST);
if(!nb_rx[pkts_burst_cnt]) {
break;
}
timestamp_us[pkts_burst_cnt] = get_timestamp_us(dpdk);
++pkts_burst_cnt;
}