Skip to content

Commit 46738f7

Browse files
magnus-karlssonborkmann
authored andcommitted
samples/bpf: add use of need_wakeup flag in xdpsock
This commit adds using the need_wakeup flag to the xdpsock sample application. It is turned on by default as we think it is a feature that seems to always produce a performance benefit, if the application has been written taking advantage of it. It can be turned off in the sample app by using the '-m' command line option. The txpush and l2fwd sub applications have also been updated to support poll() with multiple sockets. Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> Acked-by: Jonathan Lemon <jonathan.lemon@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent a450043 commit 46738f7

1 file changed

Lines changed: 120 additions & 72 deletions

File tree

‎samples/bpf/xdpsock_user.c‎

Lines changed: 120 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ static int opt_ifindex;
6767
static int opt_queue;
6868
static int opt_poll;
6969
static int opt_interval = 1;
70-
static u32 opt_xdp_bind_flags;
70+
static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
7171
static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
72+
static int opt_timeout = 1000;
73+
static bool opt_need_wakeup = true;
7274
static __u32 prog_id;
7375

7476
struct xsk_umem_info {
@@ -352,6 +354,7 @@ static struct option long_options[] = {
352354
{"zero-copy", no_argument, 0, 'z'},
353355
{"copy", no_argument, 0, 'c'},
354356
{"frame-size", required_argument, 0, 'f'},
357+
{"no-need-wakeup", no_argument, 0, 'm'},
355358
{0, 0, 0, 0}
356359
};
357360

@@ -372,6 +375,7 @@ static void usage(const char *prog)
372375
" -z, --zero-copy Force zero-copy mode.\n"
373376
" -c, --copy Force copy mode.\n"
374377
" -f, --frame-size=n Set the frame size (must be a power of two, default is %d).\n"
378+
" -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
375379
"\n";
376380
fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE);
377381
exit(EXIT_FAILURE);
@@ -384,8 +388,9 @@ static void parse_command_line(int argc, char **argv)
384388
opterr = 0;
385389

386390
for (;;) {
387-
c = getopt_long(argc, argv, "Frtli:q:psSNn:czf:", long_options,
388-
&option_index);
391+
392+
c = getopt_long(argc, argv, "Frtli:q:psSNn:czf:m",
393+
long_options, &option_index);
389394
if (c == -1)
390395
break;
391396

@@ -429,6 +434,9 @@ static void parse_command_line(int argc, char **argv)
429434
break;
430435
case 'f':
431436
opt_xsk_frame_size = atoi(optarg);
437+
case 'm':
438+
opt_need_wakeup = false;
439+
opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
432440
break;
433441
default:
434442
usage(basename(argv[0]));
@@ -459,7 +467,8 @@ static void kick_tx(struct xsk_socket_info *xsk)
459467
exit_with_error(errno);
460468
}
461469

462-
static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk)
470+
static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk,
471+
struct pollfd *fds)
463472
{
464473
u32 idx_cq = 0, idx_fq = 0;
465474
unsigned int rcvd;
@@ -468,7 +477,9 @@ static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk)
468477
if (!xsk->outstanding_tx)
469478
return;
470479

471-
kick_tx(xsk);
480+
if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx))
481+
kick_tx(xsk);
482+
472483
ndescs = (xsk->outstanding_tx > BATCH_SIZE) ? BATCH_SIZE :
473484
xsk->outstanding_tx;
474485

@@ -482,6 +493,8 @@ static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk)
482493
while (ret != rcvd) {
483494
if (ret < 0)
484495
exit_with_error(-ret);
496+
if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
497+
ret = poll(fds, num_socks, opt_timeout);
485498
ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd,
486499
&idx_fq);
487500
}
@@ -505,7 +518,8 @@ static inline void complete_tx_only(struct xsk_socket_info *xsk)
505518
if (!xsk->outstanding_tx)
506519
return;
507520

508-
kick_tx(xsk);
521+
if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx))
522+
kick_tx(xsk);
509523

510524
rcvd = xsk_ring_cons__peek(&xsk->umem->cq, BATCH_SIZE, &idx);
511525
if (rcvd > 0) {
@@ -515,20 +529,25 @@ static inline void complete_tx_only(struct xsk_socket_info *xsk)
515529
}
516530
}
517531

518-
static void rx_drop(struct xsk_socket_info *xsk)
532+
static void rx_drop(struct xsk_socket_info *xsk, struct pollfd *fds)
519533
{
520534
unsigned int rcvd, i;
521535
u32 idx_rx = 0, idx_fq = 0;
522536
int ret;
523537

524538
rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx);
525-
if (!rcvd)
539+
if (!rcvd) {
540+
if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
541+
ret = poll(fds, num_socks, opt_timeout);
526542
return;
543+
}
527544

528545
ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
529546
while (ret != rcvd) {
530547
if (ret < 0)
531548
exit_with_error(-ret);
549+
if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
550+
ret = poll(fds, num_socks, opt_timeout);
532551
ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
533552
}
534553

@@ -549,112 +568,141 @@ static void rx_drop(struct xsk_socket_info *xsk)
549568
static void rx_drop_all(void)
550569
{
551570
struct pollfd fds[MAX_SOCKS + 1];
552-
int i, ret, timeout, nfds = 1;
571+
int i, ret;
553572

554573
memset(fds, 0, sizeof(fds));
555574

556575
for (i = 0; i < num_socks; i++) {
557576
fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
558577
fds[i].events = POLLIN;
559-
timeout = 1000; /* 1sn */
560578
}
561579

562580
for (;;) {
563581
if (opt_poll) {
564-
ret = poll(fds, nfds, timeout);
582+
ret = poll(fds, num_socks, opt_timeout);
565583
if (ret <= 0)
566584
continue;
567585
}
568586

569587
for (i = 0; i < num_socks; i++)
570-
rx_drop(xsks[i]);
588+
rx_drop(xsks[i], fds);
589+
}
590+
}
591+
592+
static void tx_only(struct xsk_socket_info *xsk, u32 frame_nb)
593+
{
594+
u32 idx;
595+
596+
if (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) == BATCH_SIZE) {
597+
unsigned int i;
598+
599+
for (i = 0; i < BATCH_SIZE; i++) {
600+
xsk_ring_prod__tx_desc(&xsk->tx, idx + i)->addr =
601+
(frame_nb + i) << XSK_UMEM__DEFAULT_FRAME_SHIFT;
602+
xsk_ring_prod__tx_desc(&xsk->tx, idx + i)->len =
603+
sizeof(pkt_data) - 1;
604+
}
605+
606+
xsk_ring_prod__submit(&xsk->tx, BATCH_SIZE);
607+
xsk->outstanding_tx += BATCH_SIZE;
608+
frame_nb += BATCH_SIZE;
609+
frame_nb %= NUM_FRAMES;
571610
}
611+
612+
complete_tx_only(xsk);
572613
}
573614

574-
static void tx_only(struct xsk_socket_info *xsk)
615+
static void tx_only_all(void)
575616
{
576-
int timeout, ret, nfds = 1;
577-
struct pollfd fds[nfds + 1];
578-
u32 idx, frame_nb = 0;
617+
struct pollfd fds[MAX_SOCKS];
618+
u32 frame_nb[MAX_SOCKS] = {};
619+
int i, ret;
579620

580621
memset(fds, 0, sizeof(fds));
581-
fds[0].fd = xsk_socket__fd(xsk->xsk);
582-
fds[0].events = POLLOUT;
583-
timeout = 1000; /* 1sn */
622+
for (i = 0; i < num_socks; i++) {
623+
fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
624+
fds[0].events = POLLOUT;
625+
}
584626

585627
for (;;) {
586628
if (opt_poll) {
587-
ret = poll(fds, nfds, timeout);
629+
ret = poll(fds, num_socks, opt_timeout);
588630
if (ret <= 0)
589631
continue;
590632

591633
if (!(fds[0].revents & POLLOUT))
592634
continue;
593635
}
594636

595-
if (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) ==
596-
BATCH_SIZE) {
597-
unsigned int i;
598-
599-
for (i = 0; i < BATCH_SIZE; i++) {
600-
xsk_ring_prod__tx_desc(&xsk->tx, idx + i)->addr
601-
= (frame_nb + i) * opt_xsk_frame_size;
602-
xsk_ring_prod__tx_desc(&xsk->tx, idx + i)->len =
603-
sizeof(pkt_data) - 1;
604-
}
605-
606-
xsk_ring_prod__submit(&xsk->tx, BATCH_SIZE);
607-
xsk->outstanding_tx += BATCH_SIZE;
608-
frame_nb += BATCH_SIZE;
609-
frame_nb %= NUM_FRAMES;
610-
}
611-
612-
complete_tx_only(xsk);
637+
for (i = 0; i < num_socks; i++)
638+
tx_only(xsks[i], frame_nb[i]);
613639
}
614640
}
615641

616-
static void l2fwd(struct xsk_socket_info *xsk)
642+
static void l2fwd(struct xsk_socket_info *xsk, struct pollfd *fds)
617643
{
618-
for (;;) {
619-
unsigned int rcvd, i;
620-
u32 idx_rx = 0, idx_tx = 0;
621-
int ret;
644+
unsigned int rcvd, i;
645+
u32 idx_rx = 0, idx_tx = 0;
646+
int ret;
622647

623-
for (;;) {
624-
complete_tx_l2fwd(xsk);
648+
complete_tx_l2fwd(xsk, fds);
625649

626-
rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE,
627-
&idx_rx);
628-
if (rcvd > 0)
629-
break;
630-
}
650+
rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx);
651+
if (!rcvd) {
652+
if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
653+
ret = poll(fds, num_socks, opt_timeout);
654+
return;
655+
}
631656

657+
ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
658+
while (ret != rcvd) {
659+
if (ret < 0)
660+
exit_with_error(-ret);
661+
if (xsk_ring_prod__needs_wakeup(&xsk->tx))
662+
kick_tx(xsk);
632663
ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
633-
while (ret != rcvd) {
634-
if (ret < 0)
635-
exit_with_error(-ret);
636-
ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
637-
}
664+
}
665+
666+
for (i = 0; i < rcvd; i++) {
667+
u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
668+
u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
669+
char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
670+
671+
swap_mac_addresses(pkt);
638672

639-
for (i = 0; i < rcvd; i++) {
640-
u64 addr = xsk_ring_cons__rx_desc(&xsk->rx,
641-
idx_rx)->addr;
642-
u32 len = xsk_ring_cons__rx_desc(&xsk->rx,
643-
idx_rx++)->len;
644-
char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
673+
hex_dump(pkt, len, addr);
674+
xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = addr;
675+
xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
676+
}
645677

646-
swap_mac_addresses(pkt);
678+
xsk_ring_prod__submit(&xsk->tx, rcvd);
679+
xsk_ring_cons__release(&xsk->rx, rcvd);
647680

648-
hex_dump(pkt, len, addr);
649-
xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = addr;
650-
xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
651-
}
681+
xsk->rx_npkts += rcvd;
682+
xsk->outstanding_tx += rcvd;
683+
}
684+
685+
static void l2fwd_all(void)
686+
{
687+
struct pollfd fds[MAX_SOCKS];
688+
int i, ret;
689+
690+
memset(fds, 0, sizeof(fds));
691+
692+
for (i = 0; i < num_socks; i++) {
693+
fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
694+
fds[i].events = POLLOUT | POLLIN;
695+
}
652696

653-
xsk_ring_prod__submit(&xsk->tx, rcvd);
654-
xsk_ring_cons__release(&xsk->rx, rcvd);
697+
for (;;) {
698+
if (opt_poll) {
699+
ret = poll(fds, num_socks, opt_timeout);
700+
if (ret <= 0)
701+
continue;
702+
}
655703

656-
xsk->rx_npkts += rcvd;
657-
xsk->outstanding_tx += rcvd;
704+
for (i = 0; i < num_socks; i++)
705+
l2fwd(xsks[i], fds);
658706
}
659707
}
660708

@@ -705,9 +753,9 @@ int main(int argc, char **argv)
705753
if (opt_bench == BENCH_RXDROP)
706754
rx_drop_all();
707755
else if (opt_bench == BENCH_TXONLY)
708-
tx_only(xsks[0]);
756+
tx_only_all();
709757
else
710-
l2fwd(xsks[0]);
758+
l2fwd_all();
711759

712760
return 0;
713761
}

0 commit comments

Comments
 (0)