-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlb.c
1653 lines (1376 loc) · 32.7 KB
/
lb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <netdb.h>
#include <signal.h>
#include <poll.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <linux/ipv6.h>
#include <linux/if_packet.h>
#include "bpf/libbpf.h"
#include "bpf/xsk.h"
#include "bpf/bpf.h"
#include "liburing.h"
#include "glue_hdr.h"
#define MAX_SOCKS 4
#define BATCH_SIZE 64
#define err_with(rc, ...) do { \
errno = -rc; \
err(1, __VA_ARGS__); \
} while (0)
struct umem {
struct xsk_ring_prod fq;
struct xsk_ring_cons cq;
struct xsk_umem *lib_umem;
void *buffer;
unsigned *frame;
int frame_count;
int fq_refill;
};
struct xsk {
struct xsk_ring_cons rx;
struct xsk_ring_prod tx;
struct umem *umem;
struct xsk_socket *lib_xsk;
int fd;
unsigned long tx_queued;
unsigned long tx_wakeup;
unsigned long tx_npkts;
unsigned long rx_npkts;
};
struct node {
int family;
int socktype;
int protocol;
struct ether_addr mac_addr;
socklen_t addrlen;
struct sockaddr_storage addr;
};
struct {
bool stop;
unsigned prog_id;
int socktype;
struct xsk *xsk[MAX_SOCKS];
struct io_uring ring;
int fd;
char *buf;
} run;
enum transport_type {
TRANSPORT_TCP,
TRANSPORT_UDP,
TRANSPORT_XDP,
TRANSPORT_IOU,
};
enum rw_type {
RW_BASIC,
RW_MSG,
RW_FIXED,
};
struct {
const char *iface;
bool poll;
bool nonblock;
bool use_wakeup;
bool dual;
bool end_summary;
enum rw_type rw_api;
enum transport_type transport;
int ifindex;
int frame_size;
int num_frames;
unsigned xdp_flags;
unsigned bind_flags;
unsigned mmap_flags;
unsigned umem_flags;
int num_xsks;
int queue;
int timeout;
int port;
int iou_entries;
} opt = {
.iface = "eth0",
.nonblock = true,
.use_wakeup = true,
.poll = false,
.transport = TRANSPORT_TCP,
.num_frames = (16 * 1024),
.frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
.num_xsks = 1,
.timeout = 1000,
.queue = 32,
.port = 7777,
.iou_entries = 64,
};
static struct node self;
static struct node peer;
static const char *transport_name[] = {
[TRANSPORT_TCP] = "TCP",
[TRANSPORT_UDP] = "UDP",
[TRANSPORT_XDP] = "XDP",
[TRANSPORT_IOU] = "IOU",
};
static void statistics(void);
static void progress(void);
static void
handle_signal(int sig)
{
run.stop = true;
}
static void
set_nonblocking(int fd, bool on)
{
int rc, flag;
flag = fcntl(fd, F_GETFL);
if (flag == -1)
err(1, "fcntl(F_GETFL)");
if (on)
flag |= O_NONBLOCK;
else
flag &= ~O_NONBLOCK;
rc = fcntl(fd, F_SETFL, flag);
if (rc == -1)
err(1, "fcntl(F_SETFL)");
}
void
show(int family, struct sockaddr *addr)
{
char host[NI_MAXHOST];
int rc;
rc = getnameinfo(addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (rc != 0)
errx(1, "getnameinfo: %s", gai_strerror(rc));
printf("hostname: %s\n", host);
}
static inline bool
fd_read_ready(int fd)
{
struct pollfd pfd = {
.fd = fd,
.events = POLLIN,
};
int n;
if (!opt.poll)
return true;
for (;;) {
n = poll(&pfd, 1, opt.timeout);
if (n == 1)
return true;
if (n == -1) {
run.stop = true;
return false;
}
}
}
/*---------------------------------------------------------------------------*/
static void
normalize_tcp_link(int fd)
{
int rc;
int one = 1;
struct timeval tv = { };
set_nonblocking(fd, opt.nonblock);
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(TCP_NODELAY)");
rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (rc == -1)
err(1, "setsockopt(SO_RCVTIMEO)");
rc = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if (rc == -1)
err(1, "setsockopt(SO_SNDTIMEO)");
}
static int
tcp_wait_connect(int server)
{
struct sockaddr_storage ss;
socklen_t addrlen;
int fd;
fd = accept(server, (struct sockaddr *)&ss, &addrlen);
if (fd == -1)
err(1, "accept");
normalize_tcp_link(fd);
return fd;
}
static int
tcp_server_setup(struct node *node)
{
int fd, rc;
int one = 1;
fd = socket(node->family, node->socktype, node->protocol);
if (fd == -1)
err(1, "socket");
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(REUSEADDR)");
rc = bind(fd, (struct sockaddr *)&node->addr, node->addrlen);
if (rc == -1)
err(1, "bind");
rc = listen(fd, 1);
if (rc == -1)
err(1, "listen");
return fd;
}
static void
tcp_server_pingpong(int c_send, int c_recv)
{
char buf[8];
int n;
while (!run.stop) {
n = read(c_recv, buf, 1);
if (n == 1) {
n = write(c_send, buf, 1);
if (n != 1)
break;
continue;
}
if (n == 0)
break;
if (errno == EAGAIN || errno == EWOULDBLOCK)
continue;
break;
}
}
static void
tcp_server(void)
{
int server, c_send, c_recv;
server = tcp_server_setup(&self);
while (!run.stop) {
c_send = c_recv = tcp_wait_connect(server);
if (opt.dual)
c_send = tcp_wait_connect(server);
printf("new connection, [%d,%d]\n", c_recv, c_send);
tcp_server_pingpong(c_send, c_recv);
close(c_send);
if (opt.dual)
close(c_recv);
printf("client done\n");
}
}
static int
tcp_client_setup(struct node *node)
{
int fd, rc;
int one = 1;
fd = socket(node->family, node->socktype, node->protocol);
if (fd == -1)
err(1, "socket");
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(REUSEADDR)");
rc = connect(fd, (struct sockaddr *)&node->addr, node->addrlen);
if (rc == -1)
err(1, "connect");
normalize_tcp_link(fd);
return fd;
}
static void
tcp_client_pingpong(int c_send, int c_recv)
{
unsigned long start;
char buf[8];
int n;
while (!run.stop) {
start = nsec();
n = write(c_send, buf, 1);
if (n != 1)
break;
while (!run.stop) {
if (!fd_read_ready(c_recv))
continue;
n = read(c_recv, buf, 1);
if (n == 1) {
if (!stat_add(elapsed(start)))
run.stop = true;
progress();
break;
}
if (errno == EAGAIN || errno == EWOULDBLOCK)
continue;
run.stop = true;
}
}
}
static void
tcp_client(void)
{
int c_send, c_recv;
c_send = c_recv = tcp_client_setup(&peer);
if (opt.dual)
c_recv = tcp_client_setup(&peer);
tcp_client_pingpong(c_send, c_recv);
close(c_send);
if (opt.dual)
close(c_recv);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
static int
udp_server_setup(struct node *node)
{
int fd, rc;
int one = 1;
fd = socket(node->family, node->socktype, node->protocol);
if (fd == -1)
err(1, "socket");
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(REUSEADDR)");
rc = bind(fd, (struct sockaddr *)&node->addr, node->addrlen);
if (rc == -1)
err(1, "bind");
return fd;
}
static void
udp_server_pingpong(int fd)
{
char buf[8];
int n;
while (!run.stop) {
peer.addrlen = sizeof(peer.addr);
n = recvfrom(fd, buf, 1, 0,
(struct sockaddr *)&peer.addr, &peer.addrlen);
if (n == 1) {
n = sendto(fd, buf, 1, 0,
(struct sockaddr *)&peer.addr, peer.addrlen);
if (n != 1) {
err(1, "sendto");
break;
}
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK)
continue;
break;
}
}
static void
udp_server(void)
{
int local;
local = udp_server_setup(&self);
udp_server_pingpong(local);
}
static int
udp_client_setup(struct node *node)
{
int fd, rc;
int one = 1;
fd = socket(node->family, node->socktype, node->protocol);
if (fd == -1)
err(1, "socket");
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(REUSEADDR)");
rc = connect(fd, (struct sockaddr *)&node->addr, node->addrlen);
if (rc == -1)
err(1, "connect");
return fd;
}
static void
udp_client_pingpong(int fd)
{
unsigned long start;
char buf[8];
int n;
/* running in busypoll mode (non-blocking) results in packet drops & stalls */
while (!run.stop) {
start = nsec();
n = send(fd, buf, 1, 0);
if (n != 1)
break;
while (!run.stop) {
if (!fd_read_ready(fd))
continue;
n = recv(fd, buf, 1, 0);
if (n == 1) {
if (!stat_add(elapsed(start)))
run.stop = true;
progress();
break;
}
if (errno == EAGAIN || errno == EWOULDBLOCK)
continue;
run.stop = true;
}
}
}
static void
udp_client(void)
{
int fd;
fd = udp_client_setup(&peer);
udp_client_pingpong(fd);
close(fd);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* IO_URING */
static void
iou_setup(void)
{
unsigned flags = 0;
struct iovec iov;
int fd, rc, size;
#if 0
/* This is incompatible with IORING_OP_POLL */
if (opt.poll)
flags |= IORING_SETUP_IOPOLL;
#endif
#if 0
flags |= IORING_SETUP_SQPOLL; /* submission polling kernel thread */
flags |= IORING_SETUP_SQ_AFF; /* bind poll thread to specific cpu */
#endif
/* simplified api that only sets flags. */
fd = io_uring_queue_init(opt.iou_entries, &run.ring, flags);
if (fd == -1)
err(1, "iou_queue_init");
run.fd = fd;
if (opt.rw_api == RW_FIXED) {
size = 4096;
run.buf = mmap(NULL, size, PROT_READ|PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (run.buf == MAP_FAILED)
err(1, "mmap");
iov.iov_base = run.buf;
iov.iov_len = size;
rc = io_uring_register_buffers(&run.ring, &iov, 1);
if (rc == -1)
err(1, "io_uring_register_buffers");
}
}
static int
iou_wait_recv(int fd, char *buf, unsigned len)
{
struct io_uring *ring = &run.ring;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
struct iovec iov = {
.iov_base = buf,
.iov_len = len,
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_namelen = peer.addrlen,
};
int count, head, nr, rc;
retry:
nr = 1;
if (opt.poll) {
sqe = io_uring_get_sqe(ring);
io_uring_prep_poll_add(sqe, fd, POLLIN);
io_uring_sqe_set_flags(sqe, IOSQE_IO_LINK);
io_uring_sqe_set_data(sqe, (void *)1);
nr++;
}
sqe = io_uring_get_sqe(ring);
if (opt.rw_api == RW_BASIC)
io_uring_prep_readv(sqe, fd, &iov, 1, 0);
else if (opt.rw_api == RW_MSG)
io_uring_prep_recvmsg(sqe, fd, &msg, 0);
else
io_uring_prep_read_fixed(sqe, fd, buf, 1, 0, 0);
rc = io_uring_submit_and_wait(ring, nr);
if (rc <= 0)
err(1, "recv submit: %d", rc);
io_uring_for_each_cqe(ring, head, cqe) {
if (io_uring_cqe_get_data(cqe) == (void *)1) {
if (cqe->res < 0)
err_with(cqe->res, "cqe(POLLIN) failed");
if ((cqe->res & POLLIN) == 0)
errx(1, "linked POLL returns 0x%x", cqe->res);
continue;
}
count = cqe->res;
}
io_uring_cq_advance(ring, nr);
if (count < 0) {
if (!opt.poll)
goto retry;
statistics();
err_with(count, "cqe(READV) failed");
}
return count;
}
void
iou_send(int fd, char *buf, unsigned len)
{
struct io_uring *ring = &run.ring;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
struct iovec iov = {
.iov_base = buf,
.iov_len = len,
};
int nr, rc;
nr = 1;
#if 0
if (opt.poll) {
sqe = io_uring_get_sqe(ring);
io_uring_prep_poll_add(sqe, fd, POLLOUT);
io_uring_sqe_set_flags(sqe, IOSQE_IO_LINK);
nr++;
}
#endif
sqe = io_uring_get_sqe(ring);
io_uring_prep_writev(sqe, fd, &iov, 1, 0);
io_uring_sqe_set_data(sqe, 0xdead);
rc = io_uring_submit_and_wait(ring, nr);
if (rc <= 0)
err(1, "send submit: %d", rc);
/* XXX io_uring_for_each_cqe(ring, int head, cqe) { */
rc = io_uring_wait_cqe_nr(ring, &cqe, nr);
if (rc < 0)
err_with(rc, "iou_send, wait_cqe");
if (opt.poll && nr > 1) {
if (cqe->res < 0)
err_with(cqe->res, "cqe(POLLOUT) failed");
if ((cqe->res & POLLOUT) == 0)
errx(1, "linked POLLOUT returns 0x%x", cqe->res);
cqe++;
}
if (cqe->res < 0)
err_with(cqe->res, "iou_send, cqe failed");
if (cqe->res != 1)
errx(1, "short write, res %d", cqe->res);
io_uring_cq_advance(ring, nr);
}
static void
iou_server_pingpong(int c_send, int c_recv)
{
int n;
char buf[8];
while (!run.stop) {
n = iou_wait_recv(c_recv, buf, 1); //sizeof(buf));
if (n == 0)
break;
iou_send(c_send, buf, n);
}
}
static void
iou_server(void)
{
int server, c_send, c_recv;
iou_setup();
server = tcp_server_setup(&self);
while (!run.stop) {
c_send = c_recv = tcp_wait_connect(server);
if (opt.dual)
c_send = tcp_wait_connect(server);
printf("new connection, [%d,%d]\n", c_recv, c_send);
iou_server_pingpong(c_send, c_recv);
close(c_send);
if (opt.dual)
close(c_recv);
printf("client done\n");
}
}
static void
iou_client_pingpong(int c_send, int c_recv)
{
unsigned long start;
char buffer[8], *buf;
int n;
buf = buffer;
if (opt.rw_api == RW_FIXED)
buf = run.buf;
while (!run.stop) {
start = nsec();
iou_send(c_send, buf, 1);
n = iou_wait_recv(c_recv, buf, 1);
if (n == 0)
errx(1, "wait_recv returned 0");
if (!stat_add(elapsed(start)))
run.stop = true;
progress();
}
}
static void
iou_client(void)
{
int c_send, c_recv;
iou_setup();
c_send = c_recv = tcp_client_setup(&peer);
if (opt.dual)
c_recv = tcp_client_setup(&peer);
iou_client_pingpong(c_send, c_recv);
close(c_send);
if (opt.dual)
close(c_recv);
}
/*---------------------------------------------------------------------------*/
/* AF_XDP */
static u64
umem_pop_frame(struct umem *umem)
{
if (umem->frame_count == 0)
errx(1, "out of frames");
return umem->frame[--umem->frame_count];
}
static void
umem_push_frame(struct umem *umem, u64 addr)
{
if (umem->frame_count == opt.num_frames)
errx(1, "too many frames");
umem->frame[umem->frame_count++] = addr;
}
static void
populate_fill_ring(struct umem *umem, int count)
{
int rc, i;
u64 addr;
u32 idx;
rc = xsk_ring_prod__reserve(&umem->fq, count, &idx);
if (rc != count)
err_with(rc, "ring_prod_reserve");
for (i = 0; i < count; i++) {
addr = umem_pop_frame(umem);
*xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr;
}
xsk_ring_prod__submit(&umem->fq, count);
printf("reload fill ring with %d, left %d\n", count, umem->frame_count);
}
static struct umem *
configure_umem(void *buffer, u64 size)
{
struct xsk_umem_config cfg = {
.fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
.comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
.frame_size = opt.frame_size,
.frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
.flags = opt.umem_flags
};
struct umem *umem;
int i, rc;
umem = calloc(1, sizeof(*umem));
if (!umem)
err(1, "calloc");
rc = xsk_umem__create(&umem->lib_umem, buffer, size,
&umem->fq, &umem->cq, &cfg);
if (rc)
err_with(rc, "xsk_umem__create");
umem->buffer = buffer;
umem->fq_refill = cfg.fill_size / 2;
umem->frame = calloc(opt.num_frames, sizeof(unsigned));
for (i = 0; i < opt.num_frames; i++)
umem->frame[i] = i * opt.frame_size;
umem->frame_count = opt.num_frames;
if (cfg.fill_size)
populate_fill_ring(umem, cfg.fill_size);
return umem;
}
static struct xsk *
configure_socket(struct umem *umem, int queue, bool rx, bool tx)
{
struct xsk_socket_config cfg = {
.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
.libbpf_flags = 0,
.xdp_flags = opt.xdp_flags,
.bind_flags = opt.bind_flags,
};
struct xsk_ring_cons *rxr;
struct xsk_ring_prod *txr;
struct xsk *xsk;
int rc;
xsk = calloc(1, sizeof(*xsk));
if (!xsk)
err(1, "calloc");
rxr = rx ? &xsk->rx : NULL;
txr = tx ? &xsk->tx : NULL;
rc = xsk_socket__create(&xsk->lib_xsk, opt.iface, queue,
umem->lib_umem, &xsk->rx, &xsk->tx, &cfg);
if (rc)
err_with(rc, "xsk_socket__create");
xsk->umem = umem;
xsk->fd = xsk_socket__fd(xsk->lib_xsk);
rc = bpf_get_link_xdp_id(opt.ifindex, &run.prog_id, opt.xdp_flags);
if (rc)
err_with(rc, "bpf_get_link_xdp_id");
return xsk;
}
static void
xdp_setup(void)
{
struct umem *umem;
void *buffer;
size_t size;
int i;
size = opt.num_frames * opt.frame_size;
/* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
buffer = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | opt.mmap_flags, -1, 0);
if (buffer == MAP_FAILED)
err(1, "mmap");
/* Create sockets... */
umem = configure_umem(buffer, size);
for (i = 0; i < opt.num_xsks; i++)
run.xsk[i] = configure_socket(umem, opt.queue + i, true, true);
}
static inline void
wakeup_driver(int fd)
{
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
poll(&pfd, 1, 0);
}
static void
kick_tx(struct xsk *xsk)
{
/* don't wake up if already done */
if (xsk->tx_queued == xsk->tx_wakeup)
return;
wakeup_driver(xsk->fd);
xsk->tx_wakeup = xsk->tx_queued;
}
#if 0
set rx need wakeup - on FQ. why on fq?
since this is set only on 'allocation error', aka out of fq space.
needs RX wakeup is a signal that FQ was empty, and driver is idle.
#endif
#if 0
static void
tx_cq_to_fq(struct xsk *xsk, struct pollfd *pfd)
{
struct umem *umem = xsk->umem;
u32 idx_cq = 0, idx_fq = 0;
unsigned int rcvd, ret;
size_t ndescs;
if (!xsk->outstanding_tx)
return;
if (!opt.use_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx))
kick_tx(xsk);
ndescs = (xsk->outstanding_tx > BATCH_SIZE) ? BATCH_SIZE :
xsk->outstanding_tx;
/* re-add completed Tx buffers */
rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
if (rcvd > 0) {
unsigned int i;
ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
while (ret != rcvd) {
printf("could not reserve space in FQ\n");
if (ret < 0)
err_with(ret, "FQ prod reserve");
if (xsk_ring_prod__needs_wakeup(&umem->fq)) {
ret = poll(pfd, 1, 0);
}
ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
}
for (i = 0; i < rcvd; i++)
*xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
*xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
xsk_ring_cons__release(&xsk->umem->cq, rcvd);
xsk->outstanding_tx -= rcvd;
xsk->tx_npkts += rcvd;
}
}
#endif
/* UDP ping from kerneltest001.11.lla2 to kerneltest002.11.lla2:7777 */
static uint8_t pkt_c2s[] = {
0x24, 0x8a, 0x07, 0x8b, 0x00, 0xce, /* dst mac */
0x24, 0x8a, 0x07, 0x8b, 0x00, 0x28, /* src mac */
0x86, 0xdd, /* ethernet */
/* ip6 hdr */
0x60, 0x00,
0x00, 0x00, 0x00, 0x09, 0x11, 0x7f,
/* ip6 src addr */
0x24, 0x01, 0xdb, 0x00, 0x30, 0x20, 0xb2, 0x37,
0xfa, 0xce, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00,
/* ip6 dst addr */
0x24, 0x01, 0xdb, 0x00, 0x30, 0x20, 0xb2, 0x37,
0xfa, 0xce, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00,
/* udp */
0xa9, 0xc9, 0x1e, 0x61, 0x00, 0x09, 0x39, 0xbd,
/* payload */
0x45
};
/* UDP pong from kerneltest002.11.lla2:7777 to kerneltest002.11.lla2 */
static uint8_t pkt_s2c[] = {
0x24, 0x8a, 0x07, 0x8b, 0x00, 0x28, /* src mac */
0x24, 0x8a, 0x07, 0x8b, 0x00, 0xce, /* dst mac */
0x86, 0xdd, /* ethernet */
/* ip6 hdr */
0x60, 0x00,
0x00, 0x00, 0x00, 0x09, 0x11, 0x7f,
/* ip6 src addr */
0x24, 0x01, 0xdb, 0x00, 0x30, 0x20, 0xb2, 0x37,
0xfa, 0xce, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00,
/* ip6 dst addr */
0x24, 0x01, 0xdb, 0x00, 0x30, 0x20, 0xb2, 0x37,
0xfa, 0xce, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00,
/* udp */
0x1e, 0x61, 0xa9, 0xc9, 0x00, 0x09, 0xb9, 0x0e,
/* payload */
0x45
};
static int
xdp_pingpong(char *pkt)
{
memcpy(pkt, pkt_s2c, sizeof(pkt_s2c));
return sizeof(pkt_s2c);
}
#if 0
for the mlx5 driver, when there is an alloction error (fq is depleted)
then it sets 'needs wakeup' on the FQ (and possibly idles the driver).
userspace needs to replenish the FQ, then poke the driver.
Here, driver waits for next scheduled irq/napi before doing more
allocations, so user space could spin/compete with driver.
#endif
/*
* Fill queue may need attention. Provide more buffers and notify the driver.
* Needs some type of hysteresis here so the driver has a chance to clear the
* need wakeup flag.