-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathformat_linux_xdp.c
1880 lines (1546 loc) · 57 KB
/
format_linux_xdp.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
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "config.h"
#include "libtrace.h"
#include "libtrace_int.h"
#include "format_linux_xdp.h"
#include "format_linux_helpers.h"
#include "format_linux_common.h"
#include "hash_toeplitz.h"
#include <bpf/libbpf.h>
#include <xdp/xsk.h>
#include <bpf/bpf.h>
#include <poll.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <assert.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <pthread.h>
#include <linux/ethtool.h>
#include <linux/if_xdp.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/if_link.h>
#define XDP_FORMAT_DATA ((xdp_format_data_t *)(libtrace->format_data))
#define PACKET_META ((libtrace_xdp_meta_t *)(packet->header))
#ifndef SOL_XDP
# define SOL_XDP 283
#endif
#define FRAME_HEADROOM sizeof(libtrace_xdp_meta_t)
#define NUM_FRAMES (hw_rings + xdp_rings)
#define MIN_FREE_FRAMES 64
#define FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE
#define XDP_BUSY_RETRY 5
int hw_rings = 2048;
int xdp_rings = 2048;
typedef struct libtrace_xdp_meta {
uint64_t timestamp;
uint32_t packet_len;
uint32_t cap_len;
} PACKED libtrace_xdp_meta_t;
struct xsk_config {
uint32_t xdp_flags;
uint32_t libbpf_flags;
uint16_t xsk_bind_flags;
int ifindex;
char ifname[IF_NAMESIZE];
/* 0 = not set by user,
* 1 = promisc off by user,
* 2 = promisc on by user
*/
int promisc;
int promisc_sock;
char *bpf_filename;
char *bpf_progname;
struct bpf_object *bpf_obj;
struct bpf_program *bpf_prg;
uint32_t bpf_prg_fd;
uint32_t xdp_prog_id;
struct bpf_map *xsks_map;
int xsks_map_fd;
struct bpf_map *libtrace_map;
int libtrace_map_fd;
struct bpf_map *libtrace_ctrl_map;
int libtrace_ctrl_map_fd;
/* initial interface statistics */
struct linux_dev_stats stats;
};
struct xsk_umem_info {
struct xsk_ring_cons cq; // frames the kernel has transmitted
struct xsk_ring_prod
fq; // frames the kernel can use to insert received packets
struct xsk_umem *umem;
void *buffer;
};
struct xsk_socket_info {
struct xsk_ring_cons rx; // frames that have been received
struct xsk_ring_prod tx; // frames to be sent
struct xsk_umem_info *umem;
struct xsk_socket *xsk;
int if_queue;
};
struct xsk_per_stream {
struct xsk_socket_info *xsk;
/* previous timestamp a packet was received for this stream */
uint64_t prev_sys_time;
// ring buffer to hold addrs to be released back to the fill queue
libtrace_ringbuffer_t addr_free_ring;
pthread_t thread_id;
};
typedef struct xdp_format_data {
struct xsk_config cfg;
libtrace_list_t *per_stream;
enum hasher_types hasher_type;
xdp_state state;
int snaplen;
} xdp_format_data_t;
static struct bpf_object *load_bpf_and_xdp_attach(struct xsk_config *cfg);
static int xdp_link_detach(struct xsk_config *cfg);
static int linux_xdp_prepare_packet(libtrace_t *libtrace,
libtrace_packet_t *packet, void *buffer,
libtrace_rt_types_t rt_type,
uint32_t flags);
static int linux_xdp_start_stream(struct xsk_config *cfg,
struct xsk_per_stream *stream, int ifqueue,
int dir);
static int xsk_populate_fill_ring(struct xsk_umem_info *umem);
static bool linux_xdp_can_write(libtrace_packet_t *packet)
{
/* Get the linktype */
libtrace_linktype_t ltype = trace_get_link_type(packet);
if (ltype == TRACE_TYPE_CONTENT_INVALID) {
return false;
}
if (ltype == TRACE_TYPE_NONDATA) {
return false;
}
if (ltype == TRACE_TYPE_PCAPNG_META) {
return false;
}
if (ltype == TRACE_TYPE_ERF_META) {
return false;
}
return true;
}
static struct xsk_umem_info *configure_xsk_umem(void *buffer, uint64_t size)
{
struct xsk_umem_info *umem;
struct xsk_umem_config umem_cfg;
int ret = 1;
umem = calloc(1, sizeof(*umem));
if (umem == NULL) {
return NULL;
}
/* We recommend that you set the fill ring size >= HW RX ring size +
* AF_XDP RX ring size. Make sure you fill up the fill ring
* with buffers at regular intervals, and you will with this setting
* avoid allocation failures in the driver. These are usually quite
* expensive since drivers have not been written to assume that
* allocation failures are common. For regular sockets, kernel
* allocated memory is used that only runs out in OOM situations
* that should be rare.
*/
umem_cfg.fill_size = xdp_rings + hw_rings;
umem_cfg.comp_size = xdp_rings;
umem_cfg.frame_size = FRAME_SIZE;
umem_cfg.frame_headroom = FRAME_HEADROOM;
umem_cfg.flags = XSK_UMEM__DEFAULT_FLAGS;
ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
&umem_cfg);
if (ret) {
errno = -ret;
return NULL;
}
umem->buffer = buffer;
return umem;
}
static struct xsk_socket_info *xsk_configure_socket(struct xsk_config *cfg,
struct xsk_umem_info *umem,
int if_queue, int dir)
{
struct xsk_socket_config xsk_cfg;
struct xsk_socket_info *xsk_info;
int ret = 1;
int i;
xsk_info = calloc(1, sizeof(*xsk_info));
if (xsk_info == NULL) {
return NULL;
}
xsk_info->umem = umem;
xsk_info->if_queue = if_queue;
xsk_cfg.rx_size = xdp_rings;
xsk_cfg.tx_size = xdp_rings;
// stop libbpf from loading the default BPF program
xsk_cfg.libbpf_flags =
cfg->libbpf_flags | XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
xsk_cfg.xdp_flags = cfg->xdp_flags;
xsk_cfg.bind_flags = cfg->xsk_bind_flags;
for (i = 0; i < XDP_BUSY_RETRY; i++) {
/* inbound */
if (dir == 0) {
ret = xsk_socket__create(&xsk_info->xsk, cfg->ifname, if_queue,
umem->umem, &xsk_info->rx, NULL, &xsk_cfg);
/* outbound */
} else if (dir == 1) {
ret = xsk_socket__create(&xsk_info->xsk, cfg->ifname, if_queue,
umem->umem, NULL, &xsk_info->tx, &xsk_cfg);
}
/* If busy wait and try again */
if (ret == -EBUSY) {
usleep(1000);
} else {
break;
}
}
if (ret) {
errno = -ret;
return NULL;
}
return xsk_info;
}
static int xsk_populate_fill_ring(struct xsk_umem_info *umem)
{
int ret, i;
uint32_t idx;
ret = xsk_ring_prod__reserve(&umem->fq, NUM_FRAMES, &idx);
if (ret != NUM_FRAMES) {
return -1;
}
for (i = 0; i < NUM_FRAMES; i++) {
*xsk_ring_prod__fill_addr(&umem->fq, idx++) = i * FRAME_SIZE;
}
xsk_ring_prod__submit(&umem->fq, NUM_FRAMES);
return 0;
}
static void linux_xdp_complete_tx(struct xsk_socket_info *xsk)
{
unsigned int rcvd;
uint32_t idx;
/* does the socket need a wakeup? */
if (xsk_ring_prod__needs_wakeup(&xsk->tx)) {
sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
}
/* free completed TX buffers */
rcvd = xsk_ring_cons__peek(&xsk->umem->cq, xdp_rings, &idx);
if (rcvd > 0) {
/* release the number of sent frames */
xsk_ring_cons__release(&xsk->umem->cq, rcvd);
}
}
static inline int linux_xdp_release_addr(libtrace_t *trace,
struct xsk_socket_info *xsk,
uint64_t addr)
{
uint32_t idx;
int ret;
struct pollfd fds = {};
ret = xsk_ring_prod__reserve(&xsk->umem->fq, 1, &idx);
while (ret != 1) {
if (ret < 0) {
trace_set_err(trace, TRACE_ERR_BAD_IO,
"Linux XDP fin packet: unable to "
"reserve fill queue space");
return -1;
}
if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
fds.fd = xsk_socket__fd(xsk->xsk);
fds.events = POLLIN;
ret = poll(&fds, 1, 500);
}
ret = xsk_ring_prod__reserve(&xsk->umem->fq, 1, &idx);
}
*xsk_ring_prod__fill_addr(&xsk->umem->fq, idx) = addr;
xsk_ring_prod__submit(&xsk->umem->fq, 1);
return 1;
}
static uint64_t linux_xdp_get_time()
{
uint64_t sys_time;
#if USE_CLOCK_GETTIME
struct timespec ts = {0};
clock_gettime(CLOCK_REALTIME, &ts);
sys_time = ((uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec);
#else
struct timeval tv = {0};
gettimeofday(&tv, NULL);
sys_time =
((uint64_t)tv.tv_sec * 1000000000ull + (uint64_t)tv.tv_usec * 1000ull);
#endif
return sys_time;
}
static int linux_xdp_init_control_map(libtrace_t *libtrace)
{
libtrace_ctrl_map_t ctrl_map;
int key = 0;
if (XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd <= 0) {
return -1;
}
/* if the trace has a dedicated hasher there is only a single input queue */
if (trace_has_dedicated_hasher(libtrace)) {
ctrl_map.max_queues = 1;
} else {
ctrl_map.max_queues = libtrace->perpkt_thread_count;
}
ctrl_map.state = XDP_NOT_STARTED;
if (bpf_map_update_elem(XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd, &key,
&ctrl_map, BPF_ANY) != 0) {
return -1;
}
return 0;
}
static int linux_xdp_update_state(libtrace_t *libtrace, xdp_state state)
{
libtrace_ctrl_map_t ctrl_map;
int key = 0;
/* update libtrace state */
XDP_FORMAT_DATA->state = state;
if (XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd <= 0) {
return -1;
}
/* get data from libtrace control map */
if ((bpf_map_lookup_elem(XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd, &key,
&ctrl_map)) != 0) {
return -1;
}
/* update state */
ctrl_map.state = state;
/* push changes back to the control map */
if (bpf_map_update_elem(XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd, &key,
&ctrl_map, BPF_ANY) != 0) {
return -1;
}
return 0;
}
static int linux_xdp_init_input(libtrace_t *libtrace)
{
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable "
"to setrlimit(RLIMIT_MEMLOCK) in linux_xdp_init_input");
return -1;
}
// allocate space for the format data
libtrace->format_data =
(xdp_format_data_t *)calloc(1, sizeof(xdp_format_data_t));
if (libtrace->format_data == NULL) {
trace_set_err(
libtrace, TRACE_ERR_INIT_FAILED,
"Unable "
"to allocate memory for format data in linux_xdp_init_input()");
return -1;
}
XDP_FORMAT_DATA->hasher_type = HASHER_BALANCE;
XDP_FORMAT_DATA->state = XDP_NOT_STARTED;
XDP_FORMAT_DATA->snaplen = LIBTRACE_PACKET_BUFSIZE;
/* supported URIs
* interface, "%[^:]"
* kern:prog:interface, "%[^:]:%[^:]:%[^:]:"
*/
char kernel[200], program[200], interface[200];
// int core = -1;
int matches;
int hw_rx;
if ((matches = sscanf(libtrace->uridata, "%[^:]:%[^:]:%[^:]:", kernel,
program, interface)) == 3) {
XDP_FORMAT_DATA->cfg.bpf_filename = strdup(kernel);
XDP_FORMAT_DATA->cfg.bpf_progname = strdup(program);
memcpy(XDP_FORMAT_DATA->cfg.ifname, interface, strlen(interface));
} else if ((matches = sscanf(libtrace->uridata, "%[^:]", interface)) == 1) {
XDP_FORMAT_DATA->cfg.bpf_filename = NULL;
XDP_FORMAT_DATA->cfg.bpf_progname = NULL;
memcpy(XDP_FORMAT_DATA->cfg.ifname, interface, strlen(interface));
} else {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Invalid libtrace XDP URI");
return -1;
}
// If the user did not supply a custom BPF kernel try locate the Libtrace
// one
if (XDP_FORMAT_DATA->cfg.bpf_filename == NULL) {
for (uint32_t i = 0;
i < sizeof(libtrace_xdp_kern) / sizeof(libtrace_xdp_kern[0]);
i++) {
if (access(libtrace_xdp_kern[i], F_OK) != -1) {
XDP_FORMAT_DATA->cfg.bpf_filename =
strdup(libtrace_xdp_kern[i]);
XDP_FORMAT_DATA->cfg.bpf_progname = strdup(libtrace_xdp_prog);
break;
}
}
}
// was a kernel found?
if (XDP_FORMAT_DATA->cfg.bpf_filename == NULL) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to locate Libtrace BPF program");
return -1;
}
// check interface is correct
XDP_FORMAT_DATA->cfg.ifindex = if_nametoindex(XDP_FORMAT_DATA->cfg.ifname);
if (XDP_FORMAT_DATA->cfg.ifindex == 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Invalid XDP input interface "
"name: %s",
XDP_FORMAT_DATA->cfg.ifname);
return -1;
}
// try set number of RX rings to match xdp rings
if (linux_set_nic_rx_tx_rings(xdp_rings, xdp_rings,
XDP_FORMAT_DATA->cfg.ifname) < 0) {
// failed to set, lets see if we can get the current values and set the
// xdp rings to match
if ((hw_rx = linux_get_nic_rx_rings(XDP_FORMAT_DATA->cfg.ifname)) > 0) {
xdp_rings = hw_rx;
hw_rings = hw_rx;
}
}
return 0;
}
static int linux_xdp_setup_xdp(libtrace_t *libtrace)
{
// load BPF program
if (load_bpf_and_xdp_attach(&XDP_FORMAT_DATA->cfg) == NULL) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to load BPF program");
return -1;
}
// locate the xsk map
XDP_FORMAT_DATA->cfg.xsks_map =
bpf_object__find_map_by_name(XDP_FORMAT_DATA->cfg.bpf_obj, "xsks_map");
XDP_FORMAT_DATA->cfg.xsks_map_fd =
bpf_map__fd(XDP_FORMAT_DATA->cfg.xsks_map);
if (XDP_FORMAT_DATA->cfg.xsks_map_fd < 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to load xsks map from BPF");
return -1;
}
// locate the libtrace map
XDP_FORMAT_DATA->cfg.libtrace_map = bpf_object__find_map_by_name(
XDP_FORMAT_DATA->cfg.bpf_obj, "libtrace_map");
XDP_FORMAT_DATA->cfg.libtrace_map_fd =
bpf_map__fd(XDP_FORMAT_DATA->cfg.libtrace_map);
if (XDP_FORMAT_DATA->cfg.libtrace_map_fd < 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to load libtrace XDP map");
return -1;
}
// locate and init the libtrace control map
XDP_FORMAT_DATA->cfg.libtrace_ctrl_map = bpf_object__find_map_by_name(
XDP_FORMAT_DATA->cfg.bpf_obj, "libtrace_ctrl_map");
XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd =
bpf_map__fd(XDP_FORMAT_DATA->cfg.libtrace_ctrl_map);
if (XDP_FORMAT_DATA->cfg.libtrace_ctrl_map_fd < 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to load libtrace XDP contron map");
return -1;
} else {
if (linux_xdp_init_control_map(libtrace) == -1) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to init libtrace XDP control map");
return -1;
}
}
// setup list to hold the streams
XDP_FORMAT_DATA->per_stream =
libtrace_list_init(sizeof(struct xsk_per_stream));
if (XDP_FORMAT_DATA->per_stream == NULL) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to create list "
"for stream data in linux_xdp_init_input()");
return -1;
}
// get the initial device stats
if (linux_get_dev_statistics(XDP_FORMAT_DATA->cfg.ifname,
&XDP_FORMAT_DATA->cfg.stats) != 0) {
XDP_FORMAT_DATA->cfg.stats.if_name[0] = 0;
}
/* cfg.promisc will be 1 if the user has explicity set promisc to off,
* in all other cases we want to keep promisc on so all packets are
* processed.
*/
// create socket used to hold interface promisc setting
XDP_FORMAT_DATA->cfg.promisc_sock =
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (XDP_FORMAT_DATA->cfg.promisc != 1) {
if (linux_set_nic_promisc(XDP_FORMAT_DATA->cfg.promisc_sock,
XDP_FORMAT_DATA->cfg.ifindex, 1) < 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to enable promisc mode "
"on NIC - linux_xdp_init_input()");
return -1;
}
} else {
if (linux_set_nic_promisc(XDP_FORMAT_DATA->cfg.promisc_sock,
XDP_FORMAT_DATA->cfg.ifindex, 0) < 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to disable promisc mode "
"on NIC - linux_xdp_init_input()");
return -1;
}
}
return 0;
}
static int linux_xdp_init_output(libtrace_out_t *libtrace)
{
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
char *scan = NULL;
int hw_tx;
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
trace_set_err_out(
libtrace, TRACE_ERR_INIT_FAILED,
"Unable "
"to setrlimit(RLIMIT_MEMLOCK) in linux_xdp_init_output()");
return -1;
}
/* allocate space for the format data */
libtrace->format_data =
(xdp_format_data_t *)calloc(1, sizeof(xdp_format_data_t));
if (libtrace->format_data == NULL) {
trace_set_err_out(
libtrace, TRACE_ERR_INIT_FAILED,
"Unable "
"to allocate memory for format data in linux_xdp_init_output()");
return -1;
}
/* setup XDP config */
scan = strchr(libtrace->uridata, ':');
if (scan == NULL) {
memcpy(XDP_FORMAT_DATA->cfg.ifname, libtrace->uridata,
strlen(libtrace->uridata));
} else {
memcpy(XDP_FORMAT_DATA->cfg.ifname, scan + 1, strlen(scan + 1));
}
XDP_FORMAT_DATA->cfg.ifindex = if_nametoindex(XDP_FORMAT_DATA->cfg.ifname);
if (XDP_FORMAT_DATA->cfg.ifindex == 0) {
trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED,
"Invalid XDP output interface "
"name.");
return -1;
}
/* setup list to hold the streams */
XDP_FORMAT_DATA->per_stream =
libtrace_list_init(sizeof(struct xsk_per_stream));
if (XDP_FORMAT_DATA->per_stream == NULL) {
trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to create list "
"for stream data in linux_xdp_init_input()");
return -1;
}
// try set number of RX rings to match xdp rings
if (linux_set_nic_rx_tx_rings(xdp_rings, xdp_rings,
XDP_FORMAT_DATA->cfg.ifname) < 0) {
// failed to set, lets see if we can get the current values and set the
// xdp rings to match
if ((hw_tx = linux_get_nic_tx_rings(XDP_FORMAT_DATA->cfg.ifname)) > 0) {
xdp_rings = hw_tx;
hw_rings = hw_tx;
}
}
return 0;
}
static int linux_xdp_pstart_input(libtrace_t *libtrace)
{
int i;
struct xsk_per_stream empty_stream = {NULL, 0, {0}, 0};
struct xsk_per_stream *stream;
int max_nic_queues;
int ret;
switch (XDP_FORMAT_DATA->state) {
case XDP_PAUSED:
/* update state and return */
linux_xdp_update_state(libtrace, XDP_RUNNING);
return 0;
case XDP_RUNNING:
return 0;
case XDP_NOT_STARTED:
if (linux_xdp_setup_xdp(libtrace) < 0)
return -1;
}
/* get the maximum number of supported nic queues */
max_nic_queues = linux_get_nic_max_queues(XDP_FORMAT_DATA->cfg.ifname);
/* if the number of processing threads is greater than the max supported NIC
* queues reduce the number of threads to match */
if (libtrace->perpkt_thread_count > max_nic_queues) {
libtrace->perpkt_thread_count = max_nic_queues;
}
/* set the number of nic queues to match number of threads */
if (linux_set_nic_queues(XDP_FORMAT_DATA->cfg.ifname,
libtrace->perpkt_thread_count) !=
libtrace->perpkt_thread_count) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to set number of NIC queues "
"to match the number of processing threads %d",
libtrace->perpkt_thread_count);
return -1;
}
/* create a stream for each processing thread */
for (i = 0; i < libtrace->perpkt_thread_count; i++) {
libtrace_list_push_back(XDP_FORMAT_DATA->per_stream, &empty_stream);
stream = libtrace_list_get_index(XDP_FORMAT_DATA->per_stream, i)->data;
/* start the stream */
if ((ret = linux_xdp_start_stream(&XDP_FORMAT_DATA->cfg, stream, i,
0)) != 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to start input stream: %s", strerror(ret));
return -1;
}
}
/* update state to running */
linux_xdp_update_state(libtrace, XDP_RUNNING);
return 0;
}
static int linux_xdp_start_input(libtrace_t *libtrace)
{
struct xsk_per_stream empty_stream = {NULL, 0, {0}, 0};
struct xsk_per_stream *stream;
int c_nic_queues;
int ret;
switch (XDP_FORMAT_DATA->state) {
case XDP_PAUSED:
/* update state and return */
linux_xdp_update_state(libtrace, XDP_RUNNING);
return 0;
case XDP_RUNNING:
return 0;
case XDP_NOT_STARTED:
if (linux_xdp_setup_xdp(libtrace) < 0)
return -1;
}
/* single threaded operation, make sure the number of nic queues is 1 or
* packets will be lost */
c_nic_queues = linux_get_nic_queues(XDP_FORMAT_DATA->cfg.ifname);
if (c_nic_queues != 1) {
/* set the number of nic queues to 1 */
if (linux_set_nic_queues(XDP_FORMAT_DATA->cfg.ifname, 1) < 0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to set number "
"of NIC queues to 1");
return -1;
}
}
/* insert empty stream into the list */
libtrace_list_push_back(XDP_FORMAT_DATA->per_stream, &empty_stream);
/* get the stream from the list */
stream = libtrace_list_get_index(XDP_FORMAT_DATA->per_stream, 0)->data;
/* start the stream */
if ((ret = linux_xdp_start_stream(&XDP_FORMAT_DATA->cfg, stream, 0, 0)) !=
0) {
trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to start input stream: %s", strerror(ret));
return -1;
}
/* update state to running */
linux_xdp_update_state(libtrace, XDP_RUNNING);
return 0;
}
static int linux_xdp_pause_input(libtrace_t *libtrace)
{
int ret;
if (XDP_FORMAT_DATA->state == XDP_NOT_STARTED) {
trace_set_err(libtrace, TRACE_ERR_BAD_STATE,
"Call trace_start() before "
"calling trace_pause()");
return -1;
}
ret = linux_xdp_update_state(libtrace, XDP_PAUSED);
/* linux_xdp_update_state will return 0 on success.
* If the control map cannot be found -1 is returned.
*/
return ret;
}
static int linux_xdp_start_output(libtrace_out_t *libtrace)
{
struct xsk_per_stream empty_stream = {NULL, 0, {0}, 0};
struct xsk_per_stream *stream;
int ret;
/* insert empty stream into the list */
libtrace_list_push_back(XDP_FORMAT_DATA->per_stream, &empty_stream);
/* get the stream from the list */
stream = libtrace_list_get_index(XDP_FORMAT_DATA->per_stream, 0)->data;
/* start the stream */
if ((ret = linux_xdp_start_stream(&XDP_FORMAT_DATA->cfg, stream, 0, 1)) !=
0) {
trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED,
"Unable to start output stream: %s", strerror(ret));
return -1;
}
return 0;
}
static int linux_xdp_start_stream(struct xsk_config *cfg,
struct xsk_per_stream *stream, int ifqueue,
int dir)
{
uint64_t pkt_buf_size;
void *pkt_buf;
int ret, sock_fd;
struct xsk_umem_info *umem;
// Allocate memory for NUM_FRAMES of default XDP frame size
pkt_buf_size = NUM_FRAMES * FRAME_SIZE;
pkt_buf = mmap(NULL, pkt_buf_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// setup umem
umem = configure_xsk_umem(pkt_buf, pkt_buf_size);
if (umem == NULL) {
return errno;
}
// populate fill ring (only Rx)
if (dir == 0) {
if (xsk_populate_fill_ring(umem) < 0) {
return -1;
}
}
// configure socket
stream->xsk = xsk_configure_socket(cfg, umem, ifqueue, dir);
if (stream->xsk == NULL) {
return errno;
}
// insert socket into xsks map (only RX)
if (dir == 0) {
sock_fd = xsk_socket__fd(stream->xsk->xsk);
ret = bpf_map_update_elem(cfg->xsks_map_fd, &ifqueue, &sock_fd, 0);
if (ret)
return -ret;
}
// init addr free ring buffer
libtrace_ringbuffer_init(&stream->addr_free_ring, NUM_FRAMES,
LIBTRACE_RINGBUFFER_BLOCKING);
return 0;
}
static int linux_xdp_can_hold_packet(libtrace_packet_t *packet)
{
struct xsk_per_stream *stream;
if (packet->fmtdata == NULL) {
return -1;
}
stream = (struct xsk_per_stream *)packet->fmtdata;
// allow user to hold onto this frame if we have more than MIN_FREE_FRAMES
// remaining
if (NUM_FRAMES - xsk_prod_nb_free(&stream->xsk->umem->fq, 1) >
MIN_FREE_FRAMES) {
return 0;
}
return -1;
}
static void linux_xdp_fin_packet(libtrace_packet_t *packet)
{
struct xsk_per_stream *stream;
uint64_t addr;
if (packet->buffer == NULL)
return;
if (!packet->trace) {
fprintf(stderr, "Linux xdp packet is not attached to a valid trace."
"Unable to release it in linux_xdp_fin_packet\n");
return;
}
/* If we own the packet, we need to free it */
if (packet->buf_control == TRACE_CTRL_EXTERNAL && packet->fmtdata) {
stream = (struct xsk_per_stream *)packet->fmtdata;
packet->fmtdata = NULL;
// offset into the umem to give back to the fill queue
addr = xsk_umem__extract_addr((uint64_t)packet->buffer -
(uint64_t)stream->xsk->umem->buffer +
FRAME_HEADROOM);
// The addr needs to be released by the thread that allocated it. If
// this is the thread release back as normal, otherwise push the
// addr to the ringbuffer for the correct thread to release back to
// the fill queue on its next read. thread_id will be 0 in
// non-parallel mode
if (stream->thread_id == pthread_self() || stream->thread_id == 0) {
if (linux_xdp_release_addr(packet->trace, stream->xsk, addr) < 0) {
return;
}
} else {
libtrace_ringbuffer_swrite(&stream->addr_free_ring, (void *)addr);
}
}
}
static int linux_xdp_read_stream(libtrace_t *libtrace,
libtrace_packet_t *packet[],
libtrace_message_queue_t *msg,
struct xsk_per_stream *stream,
size_t nb_packets)
{
unsigned int rcvd = 0;
uint32_t idx_rx = 0;
uint32_t pkt_len;
uint64_t pkt_addr;
uint8_t *pkt_buffer;
unsigned int i;
libtrace_xdp_meta_t *meta;
struct pollfd fds;
int ret;
uint64_t sys_time;
uint64_t release_addr;
if (libtrace->format_data == NULL) {
trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT,
"Trace format data missing, "
"call trace_create() before calling trace_read_packet()");
return -1;
}
fds.fd = xsk_socket__fd(stream->xsk->xsk);
fds.events = POLLIN;
/* try get nb_packets */
while (rcvd < 1) {
// check for any addrs to be released back to the fill queue
while (libtrace_ringbuffer_try_read(&stream->addr_free_ring,
(void **)&release_addr) == 1) {
if (linux_xdp_release_addr(libtrace, stream->xsk, release_addr) < 0)
return -1;
}
rcvd = xsk_ring_cons__peek(&stream->xsk->rx, nb_packets, &idx_rx);
/* check if libtrace has halted */
if ((ret = is_halted(libtrace)) != -1) {
return ret;
}
if (rcvd < 1) {
/* poll will return 0 on timeout or a positive on a event */
ret = poll(&fds, 1, 500);
/* if we have access to the message queue check for a message
* otherwise we need to return and let libtrace check for a message
*/
if ((msg && libtrace_message_queue_count(msg) > 0) || !msg) {
#if ENABLE_DTRACE
DTRACE_PROBE(libtrace, xdp_read_message);
#endif
return READ_MESSAGE;
}
/* poll encountered a error */
if (ret < 0) {
trace_set_err(libtrace, errno, "poll error() XDP");
return -1;
}
}
}
#if ENABLE_DTRACE