blob: 616d663d55aa4bfb788142a4c5a60f966d1a1601 [file] [log] [blame]
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001// SPDX-License-Identifier: GPL-2.0
Björn Töpeldac091492018-05-18 14:00:21 +02002/* Copyright(c) 2017 - 2018 Intel Corporation. */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02003
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02004#include <errno.h>
5#include <getopt.h>
6#include <libgen.h>
7#include <linux/bpf.h>
8#include <linux/if_link.h>
9#include <linux/if_xdp.h>
10#include <linux/if_ether.h>
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053011#include <linux/ip.h>
Ciara Loftus67ed3752020-10-02 13:36:12 +000012#include <linux/limits.h>
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053013#include <linux/udp.h>
14#include <arpa/inet.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010015#include <locale.h>
16#include <net/ethernet.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020017#include <net/if.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010018#include <poll.h>
19#include <pthread.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020020#include <signal.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Mariusz Dudek3627d972020-12-03 10:05:46 +010025#include <sys/capability.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010026#include <sys/mman.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020027#include <sys/resource.h>
28#include <sys/socket.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010029#include <sys/types.h>
Mariusz Dudek3627d972020-12-03 10:05:46 +010030#include <sys/un.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020031#include <time.h>
32#include <unistd.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020033
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010034#include <bpf/libbpf.h>
35#include <bpf/xsk.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070036#include <bpf/bpf.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010037#include "xdpsock.h"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020038
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080039/* libbpf APIs for AF_XDP are deprecated starting from v0.7 */
40#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020042#ifndef SOL_XDP
43#define SOL_XDP 283
44#endif
45
46#ifndef AF_XDP
47#define AF_XDP 44
48#endif
49
50#ifndef PF_XDP
51#define PF_XDP AF_XDP
52#endif
53
Magnus Karlsson248c7f92019-02-21 10:21:27 +010054#define NUM_FRAMES (4 * 1024)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053055#define MIN_PKT_SIZE 64
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020056
57#define DEBUG_HEXDUMP 0
58
Björn Töpela412ef52018-06-04 13:57:14 +020059typedef __u64 u64;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020060typedef __u32 u32;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053061typedef __u16 u16;
62typedef __u8 u8;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020063
64static unsigned long prev_time;
65
66enum benchmark_type {
67 BENCH_RXDROP = 0,
68 BENCH_TXONLY = 1,
69 BENCH_L2FWD = 2,
70};
71
72static enum benchmark_type opt_bench = BENCH_RXDROP;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010073static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020074static const char *opt_if = "";
75static int opt_ifindex;
76static int opt_queue;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +053077static unsigned long opt_duration;
78static unsigned long start_time;
79static bool benchmark_done;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +053080static u32 opt_batch_size = 64;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +053081static int opt_pkt_count;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053082static u16 opt_pkt_size = MIN_PKT_SIZE;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +053083static u32 opt_pkt_fill_pattern = 0x12345678;
Ciara Loftusb36c3202020-07-08 07:28:34 +000084static bool opt_extra_stats;
Magnus Karlsson74e00672020-09-10 10:31:06 +020085static bool opt_quiet;
Ciara Loftus60dc6092020-10-02 13:36:11 +000086static bool opt_app_stats;
Ciara Loftus67ed3752020-10-02 13:36:12 +000087static const char *opt_irq_str = "";
88static u32 irq_no;
89static int irqs_at_init = -1;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020090static int opt_poll;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020091static int opt_interval = 1;
Magnus Karlsson46738f72019-08-14 09:27:21 +020092static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
Kevin Laatzc543f542019-08-27 02:25:28 +000093static u32 opt_umem_flags;
94static int opt_unaligned_chunks;
Kevin Laatz3945b372019-08-27 02:25:30 +000095static int opt_mmap_flags;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +030096static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
Magnus Karlsson46738f72019-08-14 09:27:21 +020097static int opt_timeout = 1000;
98static bool opt_need_wakeup = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +010099static u32 opt_num_xsks = 1;
Wang Hai2620e922021-06-28 17:18:15 +0800100static u32 prog_id;
Björn Töpelb35fc142020-11-30 19:52:04 +0100101static bool opt_busy_poll;
Mariusz Dudek3627d972020-12-03 10:05:46 +0100102static bool opt_reduced_cap;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200103
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000104struct xsk_ring_stats {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200105 unsigned long rx_npkts;
106 unsigned long tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000107 unsigned long rx_dropped_npkts;
108 unsigned long rx_invalid_npkts;
109 unsigned long tx_invalid_npkts;
110 unsigned long rx_full_npkts;
111 unsigned long rx_fill_empty_npkts;
112 unsigned long tx_empty_npkts;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200113 unsigned long prev_rx_npkts;
114 unsigned long prev_tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000115 unsigned long prev_rx_dropped_npkts;
116 unsigned long prev_rx_invalid_npkts;
117 unsigned long prev_tx_invalid_npkts;
118 unsigned long prev_rx_full_npkts;
119 unsigned long prev_rx_fill_empty_npkts;
120 unsigned long prev_tx_empty_npkts;
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000121};
122
Ciara Loftus67ed3752020-10-02 13:36:12 +0000123struct xsk_driver_stats {
124 unsigned long intrs;
125 unsigned long prev_intrs;
126};
127
Ciara Loftus60dc6092020-10-02 13:36:11 +0000128struct xsk_app_stats {
129 unsigned long rx_empty_polls;
130 unsigned long fill_fail_polls;
131 unsigned long copy_tx_sendtos;
132 unsigned long tx_wakeup_sendtos;
133 unsigned long opt_polls;
134 unsigned long prev_rx_empty_polls;
135 unsigned long prev_fill_fail_polls;
136 unsigned long prev_copy_tx_sendtos;
137 unsigned long prev_tx_wakeup_sendtos;
138 unsigned long prev_opt_polls;
139};
140
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000141struct xsk_umem_info {
142 struct xsk_ring_prod fq;
143 struct xsk_ring_cons cq;
144 struct xsk_umem *umem;
145 void *buffer;
146};
147
148struct xsk_socket_info {
149 struct xsk_ring_cons rx;
150 struct xsk_ring_prod tx;
151 struct xsk_umem_info *umem;
152 struct xsk_socket *xsk;
153 struct xsk_ring_stats ring_stats;
Ciara Loftus60dc6092020-10-02 13:36:11 +0000154 struct xsk_app_stats app_stats;
Ciara Loftus67ed3752020-10-02 13:36:12 +0000155 struct xsk_driver_stats drv_stats;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100156 u32 outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200157};
158
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200159static int num_socks;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100160struct xsk_socket_info *xsks[MAX_SOCKS];
Mariusz Dudek3627d972020-12-03 10:05:46 +0100161int sock;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200162
163static unsigned long get_nsecs(void)
164{
165 struct timespec ts;
166
167 clock_gettime(CLOCK_MONOTONIC, &ts);
168 return ts.tv_sec * 1000000000UL + ts.tv_nsec;
169}
170
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200171static void print_benchmark(bool running)
172{
173 const char *bench_str = "INVALID";
174
175 if (opt_bench == BENCH_RXDROP)
176 bench_str = "rxdrop";
177 else if (opt_bench == BENCH_TXONLY)
178 bench_str = "txonly";
179 else if (opt_bench == BENCH_L2FWD)
180 bench_str = "l2fwd";
181
182 printf("%s:%d %s ", opt_if, opt_queue, bench_str);
183 if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
184 printf("xdp-skb ");
185 else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
186 printf("xdp-drv ");
187 else
188 printf(" ");
189
190 if (opt_poll)
191 printf("poll() ");
192
193 if (running) {
194 printf("running...");
195 fflush(stdout);
196 }
197}
198
Ciara Loftusb36c3202020-07-08 07:28:34 +0000199static int xsk_get_xdp_stats(int fd, struct xsk_socket_info *xsk)
200{
201 struct xdp_statistics stats;
202 socklen_t optlen;
203 int err;
204
205 optlen = sizeof(stats);
206 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
207 if (err)
208 return err;
209
210 if (optlen == sizeof(struct xdp_statistics)) {
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000211 xsk->ring_stats.rx_dropped_npkts = stats.rx_dropped;
212 xsk->ring_stats.rx_invalid_npkts = stats.rx_invalid_descs;
213 xsk->ring_stats.tx_invalid_npkts = stats.tx_invalid_descs;
214 xsk->ring_stats.rx_full_npkts = stats.rx_ring_full;
215 xsk->ring_stats.rx_fill_empty_npkts = stats.rx_fill_ring_empty_descs;
216 xsk->ring_stats.tx_empty_npkts = stats.tx_ring_empty_descs;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000217 return 0;
218 }
219
220 return -EINVAL;
221}
222
Ciara Loftus60dc6092020-10-02 13:36:11 +0000223static void dump_app_stats(long dt)
224{
225 int i;
226
227 for (i = 0; i < num_socks && xsks[i]; i++) {
228 char *fmt = "%-18s %'-14.0f %'-14lu\n";
229 double rx_empty_polls_ps, fill_fail_polls_ps, copy_tx_sendtos_ps,
230 tx_wakeup_sendtos_ps, opt_polls_ps;
231
232 rx_empty_polls_ps = (xsks[i]->app_stats.rx_empty_polls -
233 xsks[i]->app_stats.prev_rx_empty_polls) * 1000000000. / dt;
234 fill_fail_polls_ps = (xsks[i]->app_stats.fill_fail_polls -
235 xsks[i]->app_stats.prev_fill_fail_polls) * 1000000000. / dt;
236 copy_tx_sendtos_ps = (xsks[i]->app_stats.copy_tx_sendtos -
237 xsks[i]->app_stats.prev_copy_tx_sendtos) * 1000000000. / dt;
238 tx_wakeup_sendtos_ps = (xsks[i]->app_stats.tx_wakeup_sendtos -
239 xsks[i]->app_stats.prev_tx_wakeup_sendtos)
240 * 1000000000. / dt;
241 opt_polls_ps = (xsks[i]->app_stats.opt_polls -
242 xsks[i]->app_stats.prev_opt_polls) * 1000000000. / dt;
243
244 printf("\n%-18s %-14s %-14s\n", "", "calls/s", "count");
245 printf(fmt, "rx empty polls", rx_empty_polls_ps, xsks[i]->app_stats.rx_empty_polls);
246 printf(fmt, "fill fail polls", fill_fail_polls_ps,
247 xsks[i]->app_stats.fill_fail_polls);
248 printf(fmt, "copy tx sendtos", copy_tx_sendtos_ps,
249 xsks[i]->app_stats.copy_tx_sendtos);
250 printf(fmt, "tx wakeup sendtos", tx_wakeup_sendtos_ps,
251 xsks[i]->app_stats.tx_wakeup_sendtos);
252 printf(fmt, "opt polls", opt_polls_ps, xsks[i]->app_stats.opt_polls);
253
254 xsks[i]->app_stats.prev_rx_empty_polls = xsks[i]->app_stats.rx_empty_polls;
255 xsks[i]->app_stats.prev_fill_fail_polls = xsks[i]->app_stats.fill_fail_polls;
256 xsks[i]->app_stats.prev_copy_tx_sendtos = xsks[i]->app_stats.copy_tx_sendtos;
257 xsks[i]->app_stats.prev_tx_wakeup_sendtos = xsks[i]->app_stats.tx_wakeup_sendtos;
258 xsks[i]->app_stats.prev_opt_polls = xsks[i]->app_stats.opt_polls;
259 }
260}
261
Ciara Loftus67ed3752020-10-02 13:36:12 +0000262static bool get_interrupt_number(void)
263{
264 FILE *f_int_proc;
265 char line[4096];
266 bool found = false;
267
268 f_int_proc = fopen("/proc/interrupts", "r");
269 if (f_int_proc == NULL) {
270 printf("Failed to open /proc/interrupts.\n");
271 return found;
272 }
273
274 while (!feof(f_int_proc) && !found) {
275 /* Make sure to read a full line at a time */
276 if (fgets(line, sizeof(line), f_int_proc) == NULL ||
277 line[strlen(line) - 1] != '\n') {
278 printf("Error reading from interrupts file\n");
279 break;
280 }
281
282 /* Extract interrupt number from line */
283 if (strstr(line, opt_irq_str) != NULL) {
284 irq_no = atoi(line);
285 found = true;
286 break;
287 }
288 }
289
290 fclose(f_int_proc);
291
292 return found;
293}
294
295static int get_irqs(void)
296{
297 char count_path[PATH_MAX];
298 int total_intrs = -1;
299 FILE *f_count_proc;
300 char line[4096];
301
302 snprintf(count_path, sizeof(count_path),
303 "/sys/kernel/irq/%i/per_cpu_count", irq_no);
304 f_count_proc = fopen(count_path, "r");
305 if (f_count_proc == NULL) {
306 printf("Failed to open %s\n", count_path);
307 return total_intrs;
308 }
309
310 if (fgets(line, sizeof(line), f_count_proc) == NULL ||
311 line[strlen(line) - 1] != '\n') {
312 printf("Error reading from %s\n", count_path);
313 } else {
314 static const char com[2] = ",";
315 char *token;
316
317 total_intrs = 0;
318 token = strtok(line, com);
319 while (token != NULL) {
320 /* sum up interrupts across all cores */
321 total_intrs += atoi(token);
322 token = strtok(NULL, com);
323 }
324 }
325
326 fclose(f_count_proc);
327
328 return total_intrs;
329}
330
331static void dump_driver_stats(long dt)
332{
333 int i;
334
335 for (i = 0; i < num_socks && xsks[i]; i++) {
336 char *fmt = "%-18s %'-14.0f %'-14lu\n";
337 double intrs_ps;
338 int n_ints = get_irqs();
339
340 if (n_ints < 0) {
341 printf("error getting intr info for intr %i\n", irq_no);
342 return;
343 }
344 xsks[i]->drv_stats.intrs = n_ints - irqs_at_init;
345
346 intrs_ps = (xsks[i]->drv_stats.intrs - xsks[i]->drv_stats.prev_intrs) *
347 1000000000. / dt;
348
349 printf("\n%-18s %-14s %-14s\n", "", "intrs/s", "count");
350 printf(fmt, "irqs", intrs_ps, xsks[i]->drv_stats.intrs);
351
352 xsks[i]->drv_stats.prev_intrs = xsks[i]->drv_stats.intrs;
353 }
354}
355
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200356static void dump_stats(void)
357{
358 unsigned long now = get_nsecs();
359 long dt = now - prev_time;
360 int i;
361
362 prev_time = now;
363
Prashant Bhole11c3f512018-08-31 10:00:49 +0900364 for (i = 0; i < num_socks && xsks[i]; i++) {
Ciara Loftus60dc6092020-10-02 13:36:11 +0000365 char *fmt = "%-18s %'-14.0f %'-14lu\n";
Ciara Loftusb36c3202020-07-08 07:28:34 +0000366 double rx_pps, tx_pps, dropped_pps, rx_invalid_pps, full_pps, fill_empty_pps,
367 tx_invalid_pps, tx_empty_pps;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200368
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000369 rx_pps = (xsks[i]->ring_stats.rx_npkts - xsks[i]->ring_stats.prev_rx_npkts) *
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200370 1000000000. / dt;
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000371 tx_pps = (xsks[i]->ring_stats.tx_npkts - xsks[i]->ring_stats.prev_tx_npkts) *
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200372 1000000000. / dt;
373
374 printf("\n sock%d@", i);
375 print_benchmark(false);
376 printf("\n");
377
Ciara Loftus60dc6092020-10-02 13:36:11 +0000378 printf("%-18s %-14s %-14s %-14.2f\n", "", "pps", "pkts",
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200379 dt / 1000000000.);
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000380 printf(fmt, "rx", rx_pps, xsks[i]->ring_stats.rx_npkts);
381 printf(fmt, "tx", tx_pps, xsks[i]->ring_stats.tx_npkts);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200382
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000383 xsks[i]->ring_stats.prev_rx_npkts = xsks[i]->ring_stats.rx_npkts;
384 xsks[i]->ring_stats.prev_tx_npkts = xsks[i]->ring_stats.tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000385
386 if (opt_extra_stats) {
387 if (!xsk_get_xdp_stats(xsk_socket__fd(xsks[i]->xsk), xsks[i])) {
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000388 dropped_pps = (xsks[i]->ring_stats.rx_dropped_npkts -
389 xsks[i]->ring_stats.prev_rx_dropped_npkts) *
390 1000000000. / dt;
391 rx_invalid_pps = (xsks[i]->ring_stats.rx_invalid_npkts -
392 xsks[i]->ring_stats.prev_rx_invalid_npkts) *
393 1000000000. / dt;
394 tx_invalid_pps = (xsks[i]->ring_stats.tx_invalid_npkts -
395 xsks[i]->ring_stats.prev_tx_invalid_npkts) *
396 1000000000. / dt;
397 full_pps = (xsks[i]->ring_stats.rx_full_npkts -
398 xsks[i]->ring_stats.prev_rx_full_npkts) *
399 1000000000. / dt;
400 fill_empty_pps = (xsks[i]->ring_stats.rx_fill_empty_npkts -
401 xsks[i]->ring_stats.prev_rx_fill_empty_npkts) *
402 1000000000. / dt;
403 tx_empty_pps = (xsks[i]->ring_stats.tx_empty_npkts -
404 xsks[i]->ring_stats.prev_tx_empty_npkts) *
405 1000000000. / dt;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000406
407 printf(fmt, "rx dropped", dropped_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000408 xsks[i]->ring_stats.rx_dropped_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000409 printf(fmt, "rx invalid", rx_invalid_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000410 xsks[i]->ring_stats.rx_invalid_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000411 printf(fmt, "tx invalid", tx_invalid_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000412 xsks[i]->ring_stats.tx_invalid_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000413 printf(fmt, "rx queue full", full_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000414 xsks[i]->ring_stats.rx_full_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000415 printf(fmt, "fill ring empty", fill_empty_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000416 xsks[i]->ring_stats.rx_fill_empty_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000417 printf(fmt, "tx ring empty", tx_empty_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000418 xsks[i]->ring_stats.tx_empty_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000419
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000420 xsks[i]->ring_stats.prev_rx_dropped_npkts =
421 xsks[i]->ring_stats.rx_dropped_npkts;
422 xsks[i]->ring_stats.prev_rx_invalid_npkts =
423 xsks[i]->ring_stats.rx_invalid_npkts;
424 xsks[i]->ring_stats.prev_tx_invalid_npkts =
425 xsks[i]->ring_stats.tx_invalid_npkts;
426 xsks[i]->ring_stats.prev_rx_full_npkts =
427 xsks[i]->ring_stats.rx_full_npkts;
428 xsks[i]->ring_stats.prev_rx_fill_empty_npkts =
429 xsks[i]->ring_stats.rx_fill_empty_npkts;
430 xsks[i]->ring_stats.prev_tx_empty_npkts =
431 xsks[i]->ring_stats.tx_empty_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000432 } else {
433 printf("%-15s\n", "Error retrieving extra stats");
434 }
435 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200436 }
Ciara Loftus60dc6092020-10-02 13:36:11 +0000437
438 if (opt_app_stats)
439 dump_app_stats(dt);
Ciara Loftus67ed3752020-10-02 13:36:12 +0000440 if (irq_no)
441 dump_driver_stats(dt);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200442}
443
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530444static bool is_benchmark_done(void)
445{
446 if (opt_duration > 0) {
447 unsigned long dt = (get_nsecs() - start_time);
448
449 if (dt >= opt_duration)
450 benchmark_done = true;
451 }
452 return benchmark_done;
453}
454
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200455static void *poller(void *arg)
456{
457 (void)arg;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530458 while (!is_benchmark_done()) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200459 sleep(opt_interval);
460 dump_stats();
461 }
462
463 return NULL;
464}
465
Wang Hai2620e922021-06-28 17:18:15 +0800466static void remove_xdp_program(void)
467{
468 u32 curr_prog_id = 0;
469
470 if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
471 printf("bpf_get_link_xdp_id failed\n");
472 exit(EXIT_FAILURE);
473 }
474
475 if (prog_id == curr_prog_id)
476 bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
477 else if (!curr_prog_id)
478 printf("couldn't find a prog id on a given interface\n");
479 else
480 printf("program on interface changed, not removing\n");
481}
482
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100483static void int_exit(int sig)
484{
Jay Jayatheerthan69525582019-12-20 14:25:26 +0530485 benchmark_done = true;
486}
487
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100488static void __exit_with_error(int error, const char *file, const char *func,
489 int line)
490{
491 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
492 line, error, strerror(error));
Wang Hai2620e922021-06-28 17:18:15 +0800493
494 if (opt_num_xsks > 1)
495 remove_xdp_program();
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100496 exit(EXIT_FAILURE);
497}
498
Maciej Fijalkowskic9d27c92021-03-30 00:43:06 +0200499#define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
500
501static void xdpsock_cleanup(void)
502{
503 struct xsk_umem *umem = xsks[0]->umem->umem;
504 int i, cmd = CLOSE_CONN;
505
506 dump_stats();
507 for (i = 0; i < num_socks; i++)
508 xsk_socket__delete(xsks[i]->xsk);
509 (void)xsk_umem__delete(umem);
510
511 if (opt_reduced_cap) {
512 if (write(sock, &cmd, sizeof(int)) < 0)
513 exit_with_error(errno);
514 }
Wang Hai2620e922021-06-28 17:18:15 +0800515
516 if (opt_num_xsks > 1)
517 remove_xdp_program();
Maciej Fijalkowskic9d27c92021-03-30 00:43:06 +0200518}
519
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100520static void swap_mac_addresses(void *data)
521{
522 struct ether_header *eth = (struct ether_header *)data;
523 struct ether_addr *src_addr = (struct ether_addr *)&eth->ether_shost;
524 struct ether_addr *dst_addr = (struct ether_addr *)&eth->ether_dhost;
525 struct ether_addr tmp;
526
527 tmp = *src_addr;
528 *src_addr = *dst_addr;
529 *dst_addr = tmp;
530}
531
532static void hex_dump(void *pkt, size_t length, u64 addr)
533{
534 const unsigned char *address = (unsigned char *)pkt;
535 const unsigned char *line = address;
536 size_t line_size = 32;
537 unsigned char c;
538 char buf[32];
539 int i = 0;
540
541 if (!DEBUG_HEXDUMP)
542 return;
543
544 sprintf(buf, "addr=%llu", addr);
545 printf("length = %zu\n", length);
546 printf("%s | ", buf);
547 while (length-- > 0) {
548 printf("%02X ", *address++);
549 if (!(++i % line_size) || (length == 0 && i % line_size)) {
550 if (length == 0) {
551 while (i++ % line_size)
552 printf("__ ");
553 }
554 printf(" | "); /* right close */
555 while (line < address) {
556 c = *line++;
557 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
558 }
559 printf("\n");
560 if (length > 0)
561 printf("%s | ", buf);
562 }
563 }
564 printf("\n");
565}
566
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530567static void *memset32_htonl(void *dest, u32 val, u32 size)
568{
569 u32 *ptr = (u32 *)dest;
570 int i;
571
572 val = htonl(val);
573
574 for (i = 0; i < (size & (~0x3)); i += 4)
575 ptr[i >> 2] = val;
576
577 for (; i < size; i++)
578 ((char *)dest)[i] = ((char *)&val)[i & 3];
579
580 return dest;
581}
582
583/*
584 * This function code has been taken from
585 * Linux kernel lib/checksum.c
586 */
587static inline unsigned short from32to16(unsigned int x)
588{
589 /* add up 16-bit and 16-bit for 16+c bit */
590 x = (x & 0xffff) + (x >> 16);
591 /* add up carry.. */
592 x = (x & 0xffff) + (x >> 16);
593 return x;
594}
595
596/*
597 * This function code has been taken from
598 * Linux kernel lib/checksum.c
599 */
600static unsigned int do_csum(const unsigned char *buff, int len)
601{
602 unsigned int result = 0;
603 int odd;
604
605 if (len <= 0)
606 goto out;
607 odd = 1 & (unsigned long)buff;
608 if (odd) {
609#ifdef __LITTLE_ENDIAN
610 result += (*buff << 8);
611#else
612 result = *buff;
613#endif
614 len--;
615 buff++;
616 }
617 if (len >= 2) {
618 if (2 & (unsigned long)buff) {
619 result += *(unsigned short *)buff;
620 len -= 2;
621 buff += 2;
622 }
623 if (len >= 4) {
624 const unsigned char *end = buff +
625 ((unsigned int)len & ~3);
626 unsigned int carry = 0;
627
628 do {
629 unsigned int w = *(unsigned int *)buff;
630
631 buff += 4;
632 result += carry;
633 result += w;
634 carry = (w > result);
635 } while (buff < end);
636 result += carry;
637 result = (result & 0xffff) + (result >> 16);
638 }
639 if (len & 2) {
640 result += *(unsigned short *)buff;
641 buff += 2;
642 }
643 }
644 if (len & 1)
645#ifdef __LITTLE_ENDIAN
646 result += *buff;
647#else
648 result += (*buff << 8);
649#endif
650 result = from32to16(result);
651 if (odd)
652 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
653out:
654 return result;
655}
656
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530657/*
658 * This is a version of ip_compute_csum() optimized for IP headers,
659 * which always checksum on 4 octet boundaries.
660 * This function code has been taken from
661 * Linux kernel lib/checksum.c
662 */
Niklas Söderlundf4700a62021-08-06 14:28:55 +0200663static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530664{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200665 return (__sum16)~do_csum(iph, ihl * 4);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530666}
667
668/*
669 * Fold a partial checksum
670 * This function code has been taken from
671 * Linux kernel include/asm-generic/checksum.h
672 */
673static inline __sum16 csum_fold(__wsum csum)
674{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200675 u32 sum = (u32)csum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530676
677 sum = (sum & 0xffff) + (sum >> 16);
678 sum = (sum & 0xffff) + (sum >> 16);
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200679 return (__sum16)~sum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530680}
681
682/*
683 * This function code has been taken from
684 * Linux kernel lib/checksum.c
685 */
686static inline u32 from64to32(u64 x)
687{
688 /* add up 32-bit and 32-bit for 32+c bit */
689 x = (x & 0xffffffff) + (x >> 32);
690 /* add up carry.. */
691 x = (x & 0xffffffff) + (x >> 32);
692 return (u32)x;
693}
694
695__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
696 __u32 len, __u8 proto, __wsum sum);
697
698/*
699 * This function code has been taken from
700 * Linux kernel lib/checksum.c
701 */
702__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
703 __u32 len, __u8 proto, __wsum sum)
704{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200705 unsigned long long s = (u32)sum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530706
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200707 s += (u32)saddr;
708 s += (u32)daddr;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530709#ifdef __BIG_ENDIAN__
710 s += proto + len;
711#else
712 s += (proto + len) << 8;
713#endif
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200714 return (__wsum)from64to32(s);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530715}
716
717/*
718 * This function has been taken from
719 * Linux kernel include/asm-generic/checksum.h
720 */
721static inline __sum16
722csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
723 __u8 proto, __wsum sum)
724{
725 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
726}
727
728static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
729 u8 proto, u16 *udp_pkt)
730{
731 u32 csum = 0;
732 u32 cnt = 0;
733
734 /* udp hdr and data */
735 for (; cnt < len; cnt += 2)
736 csum += udp_pkt[cnt >> 1];
737
738 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
739}
740
741#define ETH_FCS_SIZE 4
742
743#define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
744 sizeof(struct udphdr))
745
746#define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
747#define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
748#define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
749#define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
750
751static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
752
753static void gen_eth_hdr_data(void)
754{
755 struct udphdr *udp_hdr = (struct udphdr *)(pkt_data +
756 sizeof(struct ethhdr) +
757 sizeof(struct iphdr));
758 struct iphdr *ip_hdr = (struct iphdr *)(pkt_data +
759 sizeof(struct ethhdr));
760 struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
761
762 /* ethernet header */
763 memcpy(eth_hdr->h_dest, "\x3c\xfd\xfe\x9e\x7f\x71", ETH_ALEN);
764 memcpy(eth_hdr->h_source, "\xec\xb1\xd7\x98\x3a\xc0", ETH_ALEN);
765 eth_hdr->h_proto = htons(ETH_P_IP);
766
767 /* IP header */
768 ip_hdr->version = IPVERSION;
769 ip_hdr->ihl = 0x5; /* 20 byte header */
770 ip_hdr->tos = 0x0;
771 ip_hdr->tot_len = htons(IP_PKT_SIZE);
772 ip_hdr->id = 0;
773 ip_hdr->frag_off = 0;
774 ip_hdr->ttl = IPDEFTTL;
775 ip_hdr->protocol = IPPROTO_UDP;
776 ip_hdr->saddr = htonl(0x0a0a0a10);
777 ip_hdr->daddr = htonl(0x0a0a0a20);
778
779 /* IP header checksum */
780 ip_hdr->check = 0;
781 ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
782
783 /* UDP header */
784 udp_hdr->source = htons(0x1000);
785 udp_hdr->dest = htons(0x1000);
786 udp_hdr->len = htons(UDP_PKT_SIZE);
787
788 /* UDP data */
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530789 memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530790 UDP_PKT_DATA_SIZE);
791
792 /* UDP header checksum */
793 udp_hdr->check = 0;
794 udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
795 IPPROTO_UDP, (u16 *)udp_hdr);
796}
797
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530798static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100799{
800 memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530801 PKT_SIZE);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100802}
803
804static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size)
805{
806 struct xsk_umem_info *umem;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300807 struct xsk_umem_config cfg = {
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200808 /* We recommend that you set the fill ring size >= HW RX ring size +
809 * AF_XDP RX ring size. Make sure you fill up the fill ring
810 * with buffers at regular intervals, and you will with this setting
811 * avoid allocation failures in the driver. These are usually quite
812 * expensive since drivers have not been written to assume that
813 * allocation failures are common. For regular sockets, kernel
814 * allocated memory is used that only runs out in OOM situations
815 * that should be rare.
816 */
817 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300818 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
819 .frame_size = opt_xsk_frame_size,
820 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
Kevin Laatzc543f542019-08-27 02:25:28 +0000821 .flags = opt_umem_flags
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300822 };
Magnus Karlsson661842c2019-11-07 18:47:39 +0100823 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100824
825 umem = calloc(1, sizeof(*umem));
826 if (!umem)
827 exit_with_error(errno);
828
829 ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300830 &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100831 if (ret)
832 exit_with_error(-ret);
833
Magnus Karlsson661842c2019-11-07 18:47:39 +0100834 umem->buffer = buffer;
835 return umem;
836}
837
838static void xsk_populate_fill_ring(struct xsk_umem_info *umem)
839{
840 int ret, i;
841 u32 idx;
842
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100843 ret = xsk_ring_prod__reserve(&umem->fq,
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200844 XSK_RING_PROD__DEFAULT_NUM_DESCS * 2, &idx);
845 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS * 2)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100846 exit_with_error(-ret);
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200847 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS * 2; i++)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100848 *xsk_ring_prod__fill_addr(&umem->fq, idx++) =
849 i * opt_xsk_frame_size;
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200850 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS * 2);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100851}
852
Magnus Karlsson661842c2019-11-07 18:47:39 +0100853static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
854 bool rx, bool tx)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100855{
856 struct xsk_socket_config cfg;
857 struct xsk_socket_info *xsk;
Magnus Karlsson661842c2019-11-07 18:47:39 +0100858 struct xsk_ring_cons *rxr;
859 struct xsk_ring_prod *txr;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100860 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100861
862 xsk = calloc(1, sizeof(*xsk));
863 if (!xsk)
864 exit_with_error(errno);
865
866 xsk->umem = umem;
867 cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
868 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
Mariusz Dudek3627d972020-12-03 10:05:46 +0100869 if (opt_num_xsks > 1 || opt_reduced_cap)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100870 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
871 else
872 cfg.libbpf_flags = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100873 cfg.xdp_flags = opt_xdp_flags;
874 cfg.bind_flags = opt_xdp_bind_flags;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100875
Magnus Karlsson661842c2019-11-07 18:47:39 +0100876 rxr = rx ? &xsk->rx : NULL;
877 txr = tx ? &xsk->tx : NULL;
878 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem,
879 rxr, txr, &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100880 if (ret)
881 exit_with_error(-ret);
882
Wang Hai2620e922021-06-28 17:18:15 +0800883 ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
884 if (ret)
885 exit_with_error(-ret);
886
Ciara Loftus60dc6092020-10-02 13:36:11 +0000887 xsk->app_stats.rx_empty_polls = 0;
888 xsk->app_stats.fill_fail_polls = 0;
889 xsk->app_stats.copy_tx_sendtos = 0;
890 xsk->app_stats.tx_wakeup_sendtos = 0;
891 xsk->app_stats.opt_polls = 0;
892 xsk->app_stats.prev_rx_empty_polls = 0;
893 xsk->app_stats.prev_fill_fail_polls = 0;
894 xsk->app_stats.prev_copy_tx_sendtos = 0;
895 xsk->app_stats.prev_tx_wakeup_sendtos = 0;
896 xsk->app_stats.prev_opt_polls = 0;
897
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100898 return xsk;
899}
900
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200901static struct option long_options[] = {
902 {"rxdrop", no_argument, 0, 'r'},
903 {"txonly", no_argument, 0, 't'},
904 {"l2fwd", no_argument, 0, 'l'},
905 {"interface", required_argument, 0, 'i'},
906 {"queue", required_argument, 0, 'q'},
907 {"poll", no_argument, 0, 'p'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200908 {"xdp-skb", no_argument, 0, 'S'},
909 {"xdp-native", no_argument, 0, 'N'},
910 {"interval", required_argument, 0, 'n'},
Björn Töpel58c50ae2018-08-28 14:44:35 +0200911 {"zero-copy", no_argument, 0, 'z'},
912 {"copy", no_argument, 0, 'c'},
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300913 {"frame-size", required_argument, 0, 'f'},
Magnus Karlsson46738f72019-08-14 09:27:21 +0200914 {"no-need-wakeup", no_argument, 0, 'm'},
Kevin Laatzc543f542019-08-27 02:25:28 +0000915 {"unaligned", no_argument, 0, 'u'},
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100916 {"shared-umem", no_argument, 0, 'M'},
Andre Guedesb3133322019-11-14 08:28:47 -0800917 {"force", no_argument, 0, 'F'},
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530918 {"duration", required_argument, 0, 'd'},
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530919 {"batch-size", required_argument, 0, 'b'},
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530920 {"tx-pkt-count", required_argument, 0, 'C'},
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530921 {"tx-pkt-size", required_argument, 0, 's'},
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530922 {"tx-pkt-pattern", required_argument, 0, 'P'},
Ciara Loftusb36c3202020-07-08 07:28:34 +0000923 {"extra-stats", no_argument, 0, 'x'},
Magnus Karlsson74e00672020-09-10 10:31:06 +0200924 {"quiet", no_argument, 0, 'Q'},
Ciara Loftus60dc6092020-10-02 13:36:11 +0000925 {"app-stats", no_argument, 0, 'a'},
Ciara Loftus67ed3752020-10-02 13:36:12 +0000926 {"irq-string", no_argument, 0, 'I'},
Björn Töpelb35fc142020-11-30 19:52:04 +0100927 {"busy-poll", no_argument, 0, 'B'},
Mariusz Dudek3627d972020-12-03 10:05:46 +0100928 {"reduce-cap", no_argument, 0, 'R'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200929 {0, 0, 0, 0}
930};
931
932static void usage(const char *prog)
933{
934 const char *str =
935 " Usage: %s [OPTIONS]\n"
936 " Options:\n"
937 " -r, --rxdrop Discard all incoming packets (default)\n"
938 " -t, --txonly Only send packets\n"
939 " -l, --l2fwd MAC swap L2 forwarding\n"
940 " -i, --interface=n Run on interface n\n"
941 " -q, --queue=n Use queue n (default 0)\n"
942 " -p, --poll Use poll syscall\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200943 " -S, --xdp-skb=n Use XDP skb-mod\n"
Anton Ivanov4564a8bb2019-10-07 09:26:36 +0100944 " -N, --xdp-native=n Enforce XDP native mode\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200945 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
Björn Töpel58c50ae2018-08-28 14:44:35 +0200946 " -z, --zero-copy Force zero-copy mode.\n"
947 " -c, --copy Force copy mode.\n"
Magnus Karlsson46738f72019-08-14 09:27:21 +0200948 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
Kevin Laatzc543f542019-08-27 02:25:28 +0000949 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
950 " -u, --unaligned Enable unaligned chunk placement\n"
Mariusz Dudek3627d972020-12-03 10:05:46 +0100951 " -M, --shared-umem Enable XDP_SHARED_UMEM (cannot be used with -R)\n"
Andre Guedesb3133322019-11-14 08:28:47 -0800952 " -F, --force Force loading the XDP prog\n"
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530953 " -d, --duration=n Duration in secs to run command.\n"
954 " Default: forever.\n"
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530955 " -b, --batch-size=n Batch size for sending or receiving\n"
956 " packets. Default: %d\n"
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530957 " -C, --tx-pkt-count=n Number of packets to send.\n"
958 " Default: Continuous packets.\n"
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530959 " -s, --tx-pkt-size=n Transmit packet size.\n"
960 " (Default: %d bytes)\n"
961 " Min size: %d, Max size %d.\n"
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530962 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
Ciara Loftusb36c3202020-07-08 07:28:34 +0000963 " -x, --extra-stats Display extra statistics.\n"
Magnus Karlsson74e00672020-09-10 10:31:06 +0200964 " -Q, --quiet Do not display any stats.\n"
Ciara Loftus60dc6092020-10-02 13:36:11 +0000965 " -a, --app-stats Display application (syscall) statistics.\n"
Ciara Loftus67ed3752020-10-02 13:36:12 +0000966 " -I, --irq-string Display driver interrupt statistics for interface associated with irq-string.\n"
Björn Töpelb35fc142020-11-30 19:52:04 +0100967 " -B, --busy-poll Busy poll.\n"
Mariusz Dudek3627d972020-12-03 10:05:46 +0100968 " -R, --reduce-cap Use reduced capabilities (cannot be used with -M)\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200969 "\n";
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530970 fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530971 opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530972 XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530973
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200974 exit(EXIT_FAILURE);
975}
976
977static void parse_command_line(int argc, char **argv)
978{
979 int option_index, c;
980
981 opterr = 0;
982
983 for (;;) {
Mariusz Dudek3627d972020-12-03 10:05:46 +0100984 c = getopt_long(argc, argv, "Frtli:q:pSNn:czf:muMd:b:C:s:P:xQaI:BR",
Magnus Karlsson46738f72019-08-14 09:27:21 +0200985 long_options, &option_index);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200986 if (c == -1)
987 break;
988
989 switch (c) {
990 case 'r':
991 opt_bench = BENCH_RXDROP;
992 break;
993 case 't':
994 opt_bench = BENCH_TXONLY;
995 break;
996 case 'l':
997 opt_bench = BENCH_L2FWD;
998 break;
999 case 'i':
1000 opt_if = optarg;
1001 break;
1002 case 'q':
1003 opt_queue = atoi(optarg);
1004 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001005 case 'p':
1006 opt_poll = 1;
1007 break;
1008 case 'S':
1009 opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
Björn Töpel9f5232c2018-06-04 14:06:01 +02001010 opt_xdp_bind_flags |= XDP_COPY;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001011 break;
1012 case 'N':
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +01001013 /* default, set below */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001014 break;
1015 case 'n':
1016 opt_interval = atoi(optarg);
1017 break;
Björn Töpel58c50ae2018-08-28 14:44:35 +02001018 case 'z':
1019 opt_xdp_bind_flags |= XDP_ZEROCOPY;
1020 break;
1021 case 'c':
1022 opt_xdp_bind_flags |= XDP_COPY;
1023 break;
Kevin Laatzc543f542019-08-27 02:25:28 +00001024 case 'u':
1025 opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
1026 opt_unaligned_chunks = 1;
Kevin Laatz3945b372019-08-27 02:25:30 +00001027 opt_mmap_flags = MAP_HUGETLB;
Kevin Laatzc543f542019-08-27 02:25:28 +00001028 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +01001029 case 'F':
1030 opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
1031 break;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001032 case 'f':
1033 opt_xsk_frame_size = atoi(optarg);
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001034 break;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001035 case 'm':
1036 opt_need_wakeup = false;
1037 opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001038 break;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001039 case 'M':
1040 opt_num_xsks = MAX_SOCKS;
1041 break;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301042 case 'd':
1043 opt_duration = atoi(optarg);
1044 opt_duration *= 1000000000;
1045 break;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301046 case 'b':
1047 opt_batch_size = atoi(optarg);
1048 break;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301049 case 'C':
1050 opt_pkt_count = atoi(optarg);
1051 break;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301052 case 's':
1053 opt_pkt_size = atoi(optarg);
1054 if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
1055 opt_pkt_size < MIN_PKT_SIZE) {
1056 fprintf(stderr,
1057 "ERROR: Invalid frame size %d\n",
1058 opt_pkt_size);
1059 usage(basename(argv[0]));
1060 }
1061 break;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301062 case 'P':
1063 opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
1064 break;
Ciara Loftusb36c3202020-07-08 07:28:34 +00001065 case 'x':
1066 opt_extra_stats = 1;
1067 break;
Magnus Karlsson74e00672020-09-10 10:31:06 +02001068 case 'Q':
1069 opt_quiet = 1;
1070 break;
Ciara Loftus60dc6092020-10-02 13:36:11 +00001071 case 'a':
1072 opt_app_stats = 1;
1073 break;
Ciara Loftus67ed3752020-10-02 13:36:12 +00001074 case 'I':
1075 opt_irq_str = optarg;
1076 if (get_interrupt_number())
1077 irqs_at_init = get_irqs();
1078 if (irqs_at_init < 0) {
1079 fprintf(stderr, "ERROR: Failed to get irqs for %s\n", opt_irq_str);
1080 usage(basename(argv[0]));
1081 }
Björn Töpelb35fc142020-11-30 19:52:04 +01001082 break;
1083 case 'B':
1084 opt_busy_poll = 1;
Ciara Loftus67ed3752020-10-02 13:36:12 +00001085 break;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001086 case 'R':
1087 opt_reduced_cap = true;
1088 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001089 default:
1090 usage(basename(argv[0]));
1091 }
1092 }
1093
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +01001094 if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
1095 opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
1096
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001097 opt_ifindex = if_nametoindex(opt_if);
1098 if (!opt_ifindex) {
1099 fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
1100 opt_if);
1101 usage(basename(argv[0]));
1102 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001103
Kevin Laatzc543f542019-08-27 02:25:28 +00001104 if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
1105 !opt_unaligned_chunks) {
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001106 fprintf(stderr, "--frame-size=%d is not a power of two\n",
1107 opt_xsk_frame_size);
1108 usage(basename(argv[0]));
1109 }
Mariusz Dudek3627d972020-12-03 10:05:46 +01001110
1111 if (opt_reduced_cap && opt_num_xsks > 1) {
1112 fprintf(stderr, "ERROR: -M and -R cannot be used together\n");
1113 usage(basename(argv[0]));
1114 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001115}
1116
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001117static void kick_tx(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001118{
1119 int ret;
1120
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001121 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
Maciej Fijalkowski8ed47e12020-02-05 05:58:34 +01001122 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
1123 errno == EBUSY || errno == ENETDOWN)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001124 return;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001125 exit_with_error(errno);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001126}
1127
Björn Töpel284cbc62020-11-30 19:52:03 +01001128static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001129{
Kevin Laatz03895e62019-08-27 02:25:29 +00001130 struct xsk_umem_info *umem = xsk->umem;
Yonghong Songb74e21a2019-02-28 22:19:41 -08001131 u32 idx_cq = 0, idx_fq = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001132 unsigned int rcvd;
1133 size_t ndescs;
1134
1135 if (!xsk->outstanding_tx)
1136 return;
1137
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001138 /* In copy mode, Tx is driven by a syscall so we need to use e.g. sendto() to
1139 * really send the packets. In zero-copy mode we do not have to do this, since Tx
1140 * is driven by the NAPI loop. So as an optimization, we do not have to call
1141 * sendto() all the time in zero-copy mode for l2fwd.
1142 */
Ciara Loftus60dc6092020-10-02 13:36:11 +00001143 if (opt_xdp_bind_flags & XDP_COPY) {
1144 xsk->app_stats.copy_tx_sendtos++;
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001145 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001146 }
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001147
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301148 ndescs = (xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001149 xsk->outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001150
1151 /* re-add completed Tx buffers */
Kevin Laatz03895e62019-08-27 02:25:29 +00001152 rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001153 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001154 unsigned int i;
1155 int ret;
1156
Kevin Laatz03895e62019-08-27 02:25:29 +00001157 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001158 while (ret != rcvd) {
1159 if (ret < 0)
1160 exit_with_error(-ret);
Björn Töpelb35fc142020-11-30 19:52:04 +01001161 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001162 xsk->app_stats.fill_fail_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001163 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL,
1164 NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001165 }
Kevin Laatz03895e62019-08-27 02:25:29 +00001166 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001167 }
Kevin Laatz03895e62019-08-27 02:25:29 +00001168
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001169 for (i = 0; i < rcvd; i++)
Kevin Laatz03895e62019-08-27 02:25:29 +00001170 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
1171 *xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001172
1173 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1174 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001175 xsk->outstanding_tx -= rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001176 }
1177}
1178
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301179static inline void complete_tx_only(struct xsk_socket_info *xsk,
1180 int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001181{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001182 unsigned int rcvd;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001183 u32 idx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001184
1185 if (!xsk->outstanding_tx)
1186 return;
1187
Ciara Loftus60dc6092020-10-02 13:36:11 +00001188 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
1189 xsk->app_stats.tx_wakeup_sendtos++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001190 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001191 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001192
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301193 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001194 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001195 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001196 xsk->outstanding_tx -= rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001197 }
1198}
1199
Björn Töpelf2d27282020-11-30 19:52:02 +01001200static void rx_drop(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001201{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001202 unsigned int rcvd, i;
Yonghong Songb74e21a2019-02-28 22:19:41 -08001203 u32 idx_rx = 0, idx_fq = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001204 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001205
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301206 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001207 if (!rcvd) {
Björn Töpelb35fc142020-11-30 19:52:04 +01001208 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001209 xsk->app_stats.rx_empty_polls++;
Björn Töpelf2d27282020-11-30 19:52:02 +01001210 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001211 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001212 return;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001213 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001214
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001215 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
1216 while (ret != rcvd) {
1217 if (ret < 0)
1218 exit_with_error(-ret);
Björn Töpelb35fc142020-11-30 19:52:04 +01001219 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001220 xsk->app_stats.fill_fail_polls++;
Björn Töpelf2d27282020-11-30 19:52:02 +01001221 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001222 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001223 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001224 }
1225
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001226 for (i = 0; i < rcvd; i++) {
1227 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1228 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Kevin Laatz03895e62019-08-27 02:25:29 +00001229 u64 orig = xsk_umem__extract_addr(addr);
1230
1231 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001232 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001233
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001234 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001235 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001236 }
1237
1238 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1239 xsk_ring_cons__release(&xsk->rx, rcvd);
Ciara Loftus2e8806f2020-10-02 13:36:10 +00001240 xsk->ring_stats.rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001241}
1242
1243static void rx_drop_all(void)
1244{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001245 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001246 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001247
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001248 for (i = 0; i < num_socks; i++) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001249 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001250 fds[i].events = POLLIN;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001251 }
1252
1253 for (;;) {
1254 if (opt_poll) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001255 for (i = 0; i < num_socks; i++)
1256 xsks[i]->app_stats.opt_polls++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001257 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001258 if (ret <= 0)
1259 continue;
1260 }
1261
1262 for (i = 0; i < num_socks; i++)
Björn Töpelf2d27282020-11-30 19:52:02 +01001263 rx_drop(xsks[i]);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301264
1265 if (benchmark_done)
1266 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001267 }
1268}
1269
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001270static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001271{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001272 u32 idx;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301273 unsigned int i;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001274
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301275 while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
1276 batch_size) {
1277 complete_tx_only(xsk, batch_size);
Magnus Karlsson092fde02020-12-10 17:34:07 +01001278 if (benchmark_done)
1279 return;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001280 }
1281
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301282 for (i = 0; i < batch_size; i++) {
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301283 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
1284 idx + i);
Magnus Karlsson3b80d102021-05-06 14:43:49 +02001285 tx_desc->addr = (*frame_nb + i) * opt_xsk_frame_size;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301286 tx_desc->len = PKT_SIZE;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301287 }
1288
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301289 xsk_ring_prod__submit(&xsk->tx, batch_size);
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001290 xsk->ring_stats.tx_npkts += batch_size;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301291 xsk->outstanding_tx += batch_size;
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001292 *frame_nb += batch_size;
1293 *frame_nb %= NUM_FRAMES;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301294 complete_tx_only(xsk, batch_size);
1295}
1296
1297static inline int get_batch_size(int pkt_cnt)
1298{
1299 if (!opt_pkt_count)
1300 return opt_batch_size;
1301
1302 if (pkt_cnt + opt_batch_size <= opt_pkt_count)
1303 return opt_batch_size;
1304
1305 return opt_pkt_count - pkt_cnt;
1306}
1307
1308static void complete_tx_only_all(void)
1309{
1310 bool pending;
1311 int i;
1312
1313 do {
1314 pending = false;
1315 for (i = 0; i < num_socks; i++) {
1316 if (xsks[i]->outstanding_tx) {
1317 complete_tx_only(xsks[i], opt_batch_size);
1318 pending = !!xsks[i]->outstanding_tx;
1319 }
1320 }
1321 } while (pending);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001322}
1323
1324static void tx_only_all(void)
1325{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001326 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001327 u32 frame_nb[MAX_SOCKS] = {};
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301328 int pkt_cnt = 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001329 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001330
Magnus Karlsson46738f72019-08-14 09:27:21 +02001331 for (i = 0; i < num_socks; i++) {
1332 fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
1333 fds[0].events = POLLOUT;
1334 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001335
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301336 while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
1337 int batch_size = get_batch_size(pkt_cnt);
1338
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001339 if (opt_poll) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001340 for (i = 0; i < num_socks; i++)
1341 xsks[i]->app_stats.opt_polls++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001342 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001343 if (ret <= 0)
1344 continue;
1345
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001346 if (!(fds[0].revents & POLLOUT))
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001347 continue;
1348 }
1349
Magnus Karlsson46738f72019-08-14 09:27:21 +02001350 for (i = 0; i < num_socks; i++)
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001351 tx_only(xsks[i], &frame_nb[i], batch_size);
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301352
1353 pkt_cnt += batch_size;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301354
1355 if (benchmark_done)
1356 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001357 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301358
1359 if (opt_pkt_count)
1360 complete_tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001361}
1362
Björn Töpel284cbc62020-11-30 19:52:03 +01001363static void l2fwd(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001364{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001365 unsigned int rcvd, i;
1366 u32 idx_rx = 0, idx_tx = 0;
1367 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001368
Björn Töpel284cbc62020-11-30 19:52:03 +01001369 complete_tx_l2fwd(xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001370
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301371 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001372 if (!rcvd) {
Björn Töpelb35fc142020-11-30 19:52:04 +01001373 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001374 xsk->app_stats.rx_empty_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001375 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001376 }
Magnus Karlsson46738f72019-08-14 09:27:21 +02001377 return;
1378 }
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001379 xsk->ring_stats.rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001380
Magnus Karlsson46738f72019-08-14 09:27:21 +02001381 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1382 while (ret != rcvd) {
1383 if (ret < 0)
1384 exit_with_error(-ret);
Björn Töpel284cbc62020-11-30 19:52:03 +01001385 complete_tx_l2fwd(xsk);
Björn Töpelb35fc142020-11-30 19:52:04 +01001386 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001387 xsk->app_stats.tx_wakeup_sendtos++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001388 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001389 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001390 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001391 }
1392
1393 for (i = 0; i < rcvd; i++) {
1394 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1395 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Ciara Loftus5a712e12019-09-13 10:39:48 +00001396 u64 orig = addr;
Kevin Laatz03895e62019-08-27 02:25:29 +00001397
1398 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001399 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1400
1401 swap_mac_addresses(pkt);
1402
1403 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001404 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = orig;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001405 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
1406 }
1407
1408 xsk_ring_prod__submit(&xsk->tx, rcvd);
1409 xsk_ring_cons__release(&xsk->rx, rcvd);
1410
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001411 xsk->ring_stats.tx_npkts += rcvd;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001412 xsk->outstanding_tx += rcvd;
1413}
1414
1415static void l2fwd_all(void)
1416{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001417 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001418 int i, ret;
1419
Magnus Karlsson46738f72019-08-14 09:27:21 +02001420 for (;;) {
1421 if (opt_poll) {
Björn Töpel284cbc62020-11-30 19:52:03 +01001422 for (i = 0; i < num_socks; i++) {
1423 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1424 fds[i].events = POLLOUT | POLLIN;
Ciara Loftus60dc6092020-10-02 13:36:11 +00001425 xsks[i]->app_stats.opt_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001426 }
Magnus Karlsson46738f72019-08-14 09:27:21 +02001427 ret = poll(fds, num_socks, opt_timeout);
1428 if (ret <= 0)
1429 continue;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001430 }
1431
Magnus Karlsson46738f72019-08-14 09:27:21 +02001432 for (i = 0; i < num_socks; i++)
Björn Töpel284cbc62020-11-30 19:52:03 +01001433 l2fwd(xsks[i]);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301434
1435 if (benchmark_done)
1436 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001437 }
1438}
1439
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001440static void load_xdp_program(char **argv, struct bpf_object **obj)
1441{
1442 struct bpf_prog_load_attr prog_load_attr = {
1443 .prog_type = BPF_PROG_TYPE_XDP,
1444 };
1445 char xdp_filename[256];
1446 int prog_fd;
1447
1448 snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
1449 prog_load_attr.file = xdp_filename;
1450
1451 if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
1452 exit(EXIT_FAILURE);
1453 if (prog_fd < 0) {
1454 fprintf(stderr, "ERROR: no program found: %s\n",
1455 strerror(prog_fd));
1456 exit(EXIT_FAILURE);
1457 }
1458
1459 if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
1460 fprintf(stderr, "ERROR: link set xdp fd failed\n");
1461 exit(EXIT_FAILURE);
1462 }
1463}
1464
1465static void enter_xsks_into_map(struct bpf_object *obj)
1466{
1467 struct bpf_map *map;
1468 int i, xsks_map;
1469
1470 map = bpf_object__find_map_by_name(obj, "xsks_map");
1471 xsks_map = bpf_map__fd(map);
1472 if (xsks_map < 0) {
1473 fprintf(stderr, "ERROR: no xsks map found: %s\n",
1474 strerror(xsks_map));
1475 exit(EXIT_FAILURE);
1476 }
1477
1478 for (i = 0; i < num_socks; i++) {
1479 int fd = xsk_socket__fd(xsks[i]->xsk);
1480 int key, ret;
1481
1482 key = i;
1483 ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
1484 if (ret) {
1485 fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
1486 exit(EXIT_FAILURE);
1487 }
1488 }
1489}
1490
Björn Töpelb35fc142020-11-30 19:52:04 +01001491static void apply_setsockopt(struct xsk_socket_info *xsk)
1492{
1493 int sock_opt;
1494
1495 if (!opt_busy_poll)
1496 return;
1497
1498 sock_opt = 1;
1499 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL,
1500 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1501 exit_with_error(errno);
1502
1503 sock_opt = 20;
1504 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL,
1505 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1506 exit_with_error(errno);
Björn Töpel41bf9002020-11-30 19:52:05 +01001507
1508 sock_opt = opt_batch_size;
1509 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1510 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1511 exit_with_error(errno);
Björn Töpelb35fc142020-11-30 19:52:04 +01001512}
1513
Mariusz Dudek3627d972020-12-03 10:05:46 +01001514static int recv_xsks_map_fd_from_ctrl_node(int sock, int *_fd)
1515{
1516 char cms[CMSG_SPACE(sizeof(int))];
1517 struct cmsghdr *cmsg;
1518 struct msghdr msg;
1519 struct iovec iov;
1520 int value;
1521 int len;
1522
1523 iov.iov_base = &value;
1524 iov.iov_len = sizeof(int);
1525
1526 msg.msg_name = 0;
1527 msg.msg_namelen = 0;
1528 msg.msg_iov = &iov;
1529 msg.msg_iovlen = 1;
1530 msg.msg_flags = 0;
1531 msg.msg_control = (caddr_t)cms;
1532 msg.msg_controllen = sizeof(cms);
1533
1534 len = recvmsg(sock, &msg, 0);
1535
1536 if (len < 0) {
1537 fprintf(stderr, "Recvmsg failed length incorrect.\n");
1538 return -EINVAL;
1539 }
1540
1541 if (len == 0) {
1542 fprintf(stderr, "Recvmsg failed no data\n");
1543 return -EINVAL;
1544 }
1545
1546 cmsg = CMSG_FIRSTHDR(&msg);
1547 *_fd = *(int *)CMSG_DATA(cmsg);
1548
1549 return 0;
1550}
1551
1552static int
1553recv_xsks_map_fd(int *xsks_map_fd)
1554{
1555 struct sockaddr_un server;
1556 int err;
1557
1558 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1559 if (sock < 0) {
1560 fprintf(stderr, "Error opening socket stream: %s", strerror(errno));
1561 return errno;
1562 }
1563
1564 server.sun_family = AF_UNIX;
1565 strcpy(server.sun_path, SOCKET_NAME);
1566
1567 if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) < 0) {
1568 close(sock);
1569 fprintf(stderr, "Error connecting stream socket: %s", strerror(errno));
1570 return errno;
1571 }
1572
1573 err = recv_xsks_map_fd_from_ctrl_node(sock, xsks_map_fd);
1574 if (err) {
Colin Ian King2faa7322020-12-03 11:44:52 +00001575 fprintf(stderr, "Error %d receiving fd\n", err);
Mariusz Dudek3627d972020-12-03 10:05:46 +01001576 return err;
1577 }
1578 return 0;
1579}
1580
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001581int main(int argc, char **argv)
1582{
Mariusz Dudek3627d972020-12-03 10:05:46 +01001583 struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 };
1584 struct __user_cap_data_struct data[2] = { { 0 } };
1585 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Magnus Karlsson661842c2019-11-07 18:47:39 +01001586 bool rx = false, tx = false;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001587 struct xsk_umem_info *umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001588 struct bpf_object *obj;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001589 int xsks_map_fd = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001590 pthread_t pt;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001591 int i, ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001592 void *bufs;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001593
1594 parse_command_line(argc, argv);
1595
Mariusz Dudek3627d972020-12-03 10:05:46 +01001596 if (opt_reduced_cap) {
1597 if (capget(&hdr, data) < 0)
1598 fprintf(stderr, "Error getting capabilities\n");
1599
1600 data->effective &= CAP_TO_MASK(CAP_NET_RAW);
1601 data->permitted &= CAP_TO_MASK(CAP_NET_RAW);
1602
1603 if (capset(&hdr, data) < 0)
1604 fprintf(stderr, "Setting capabilities failed\n");
1605
1606 if (capget(&hdr, data) < 0) {
1607 fprintf(stderr, "Error getting capabilities\n");
1608 } else {
1609 fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
1610 data[0].effective, data[0].inheritable, data[0].permitted);
1611 fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
1612 data[1].effective, data[1].inheritable, data[1].permitted);
1613 }
1614 } else {
1615 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
1616 fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1617 strerror(errno));
1618 exit(EXIT_FAILURE);
1619 }
1620
1621 if (opt_num_xsks > 1)
1622 load_xdp_program(argv, &obj);
1623 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001624
Kevin Laatz3945b372019-08-27 02:25:30 +00001625 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1626 bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size,
1627 PROT_READ | PROT_WRITE,
1628 MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
1629 if (bufs == MAP_FAILED) {
1630 printf("ERROR: mmap failed\n");
1631 exit(EXIT_FAILURE);
1632 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001633
1634 /* Create sockets... */
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001635 umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size);
Magnus Karlsson661842c2019-11-07 18:47:39 +01001636 if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
1637 rx = true;
1638 xsk_populate_fill_ring(umem);
1639 }
1640 if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
1641 tx = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001642 for (i = 0; i < opt_num_xsks; i++)
Magnus Karlsson661842c2019-11-07 18:47:39 +01001643 xsks[num_socks++] = xsk_configure_socket(umem, rx, tx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001644
Björn Töpelb35fc142020-11-30 19:52:04 +01001645 for (i = 0; i < opt_num_xsks; i++)
1646 apply_setsockopt(xsks[i]);
1647
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301648 if (opt_bench == BENCH_TXONLY) {
1649 gen_eth_hdr_data();
1650
Magnus Karlsson661842c2019-11-07 18:47:39 +01001651 for (i = 0; i < NUM_FRAMES; i++)
1652 gen_eth_frame(umem, i * opt_xsk_frame_size);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301653 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001654
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001655 if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
1656 enter_xsks_into_map(obj);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001657
Mariusz Dudek3627d972020-12-03 10:05:46 +01001658 if (opt_reduced_cap) {
1659 ret = recv_xsks_map_fd(&xsks_map_fd);
1660 if (ret) {
1661 fprintf(stderr, "Error %d receiving xsks_map_fd\n", ret);
1662 exit_with_error(ret);
1663 }
1664 if (xsks[0]->xsk) {
1665 ret = xsk_socket__update_xskmap(xsks[0]->xsk, xsks_map_fd);
1666 if (ret) {
1667 fprintf(stderr, "Update of BPF map failed(%d)\n", ret);
1668 exit_with_error(ret);
1669 }
1670 }
1671 }
1672
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001673 signal(SIGINT, int_exit);
1674 signal(SIGTERM, int_exit);
1675 signal(SIGABRT, int_exit);
1676
1677 setlocale(LC_ALL, "");
1678
Magnus Karlsson74e00672020-09-10 10:31:06 +02001679 if (!opt_quiet) {
1680 ret = pthread_create(&pt, NULL, poller, NULL);
1681 if (ret)
1682 exit_with_error(ret);
1683 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001684
1685 prev_time = get_nsecs();
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301686 start_time = prev_time;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001687
1688 if (opt_bench == BENCH_RXDROP)
1689 rx_drop_all();
1690 else if (opt_bench == BENCH_TXONLY)
Magnus Karlsson46738f72019-08-14 09:27:21 +02001691 tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001692 else
Magnus Karlsson46738f72019-08-14 09:27:21 +02001693 l2fwd_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001694
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301695 benchmark_done = true;
1696
Magnus Karlsson74e00672020-09-10 10:31:06 +02001697 if (!opt_quiet)
1698 pthread_join(pt, NULL);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301699
Jay Jayatheerthan69525582019-12-20 14:25:26 +05301700 xdpsock_cleanup();
1701
Maciej Fijalkowski6bc66992021-03-03 19:56:35 +01001702 munmap(bufs, NUM_FRAMES * opt_xsk_frame_size);
1703
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001704 return 0;
1705}