blob: aa50864e4415a508d96ca82aa1bd14dcbc066bf9 [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>
Ong Boon Leong6440a6c2021-12-30 11:54:42 +080017#include <netinet/ether.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020018#include <net/if.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010019#include <poll.h>
20#include <pthread.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020021#include <signal.h>
22#include <stdbool.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Mariusz Dudek3627d972020-12-03 10:05:46 +010026#include <sys/capability.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010027#include <sys/mman.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020028#include <sys/resource.h>
29#include <sys/socket.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010030#include <sys/types.h>
Mariusz Dudek3627d972020-12-03 10:05:46 +010031#include <sys/un.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020032#include <time.h>
33#include <unistd.h>
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +080034#include <sched.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020035
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010036#include <bpf/libbpf.h>
37#include <bpf/xsk.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070038#include <bpf/bpf.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010039#include "xdpsock.h"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020040
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080041/* libbpf APIs for AF_XDP are deprecated starting from v0.7 */
42#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
43
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020044#ifndef SOL_XDP
45#define SOL_XDP 283
46#endif
47
48#ifndef AF_XDP
49#define AF_XDP 44
50#endif
51
52#ifndef PF_XDP
53#define PF_XDP AF_XDP
54#endif
55
Magnus Karlsson248c7f92019-02-21 10:21:27 +010056#define NUM_FRAMES (4 * 1024)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053057#define MIN_PKT_SIZE 64
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020058
59#define DEBUG_HEXDUMP 0
60
Ong Boon Leong2741a042021-12-30 11:54:41 +080061#define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
62#define VLAN_PRIO_SHIFT 13
63#define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
64#define VLAN_VID__DEFAULT 1
65#define VLAN_PRI__DEFAULT 0
66
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +080067#define NSEC_PER_SEC 1000000000UL
68#define NSEC_PER_USEC 1000
69
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +080070#define SCHED_PRI__DEFAULT 0
71
Björn Töpela412ef52018-06-04 13:57:14 +020072typedef __u64 u64;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020073typedef __u32 u32;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053074typedef __u16 u16;
75typedef __u8 u8;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020076
77static unsigned long prev_time;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +080078static long tx_cycle_diff_min;
79static long tx_cycle_diff_max;
80static double tx_cycle_diff_ave;
81static long tx_cycle_cnt;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020082
83enum benchmark_type {
84 BENCH_RXDROP = 0,
85 BENCH_TXONLY = 1,
86 BENCH_L2FWD = 2,
87};
88
89static enum benchmark_type opt_bench = BENCH_RXDROP;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010090static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020091static const char *opt_if = "";
92static int opt_ifindex;
93static int opt_queue;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +053094static unsigned long opt_duration;
95static unsigned long start_time;
96static bool benchmark_done;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +053097static u32 opt_batch_size = 64;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +053098static int opt_pkt_count;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053099static u16 opt_pkt_size = MIN_PKT_SIZE;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530100static u32 opt_pkt_fill_pattern = 0x12345678;
Ong Boon Leong2741a042021-12-30 11:54:41 +0800101static bool opt_vlan_tag;
102static u16 opt_pkt_vlan_id = VLAN_VID__DEFAULT;
103static u16 opt_pkt_vlan_pri = VLAN_PRI__DEFAULT;
Ong Boon Leong6440a6c2021-12-30 11:54:42 +0800104static struct ether_addr opt_txdmac = {{ 0x3c, 0xfd, 0xfe,
105 0x9e, 0x7f, 0x71 }};
106static struct ether_addr opt_txsmac = {{ 0xec, 0xb1, 0xd7,
107 0x98, 0x3a, 0xc0 }};
Ciara Loftusb36c3202020-07-08 07:28:34 +0000108static bool opt_extra_stats;
Magnus Karlsson74e00672020-09-10 10:31:06 +0200109static bool opt_quiet;
Ciara Loftus60dc6092020-10-02 13:36:11 +0000110static bool opt_app_stats;
Ciara Loftus67ed3752020-10-02 13:36:12 +0000111static const char *opt_irq_str = "";
112static u32 irq_no;
113static int irqs_at_init = -1;
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800114static u32 sequence;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200115static int opt_poll;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200116static int opt_interval = 1;
Ong Boon Leong8121e782021-12-30 11:54:46 +0800117static int opt_retries = 3;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200118static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
Kevin Laatzc543f542019-08-27 02:25:28 +0000119static u32 opt_umem_flags;
120static int opt_unaligned_chunks;
Kevin Laatz3945b372019-08-27 02:25:30 +0000121static int opt_mmap_flags;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300122static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200123static int opt_timeout = 1000;
124static bool opt_need_wakeup = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100125static u32 opt_num_xsks = 1;
Wang Hai2620e922021-06-28 17:18:15 +0800126static u32 prog_id;
Björn Töpelb35fc142020-11-30 19:52:04 +0100127static bool opt_busy_poll;
Mariusz Dudek3627d972020-12-03 10:05:46 +0100128static bool opt_reduced_cap;
Ong Boon Leong5a388252021-12-30 11:54:43 +0800129static clockid_t opt_clock = CLOCK_MONOTONIC;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +0800130static unsigned long opt_tx_cycle_ns;
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +0800131static int opt_schpolicy = SCHED_OTHER;
132static int opt_schprio = SCHED_PRI__DEFAULT;
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800133static bool opt_tstamp;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200134
Ong Boon Leong2741a042021-12-30 11:54:41 +0800135struct vlan_ethhdr {
136 unsigned char h_dest[6];
137 unsigned char h_source[6];
138 __be16 h_vlan_proto;
139 __be16 h_vlan_TCI;
140 __be16 h_vlan_encapsulated_proto;
141};
142
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800143#define PKTGEN_MAGIC 0xbe9be955
144struct pktgen_hdr {
145 __be32 pgh_magic;
146 __be32 seq_num;
147 __be32 tv_sec;
148 __be32 tv_usec;
149};
150
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000151struct xsk_ring_stats {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200152 unsigned long rx_npkts;
153 unsigned long tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000154 unsigned long rx_dropped_npkts;
155 unsigned long rx_invalid_npkts;
156 unsigned long tx_invalid_npkts;
157 unsigned long rx_full_npkts;
158 unsigned long rx_fill_empty_npkts;
159 unsigned long tx_empty_npkts;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200160 unsigned long prev_rx_npkts;
161 unsigned long prev_tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000162 unsigned long prev_rx_dropped_npkts;
163 unsigned long prev_rx_invalid_npkts;
164 unsigned long prev_tx_invalid_npkts;
165 unsigned long prev_rx_full_npkts;
166 unsigned long prev_rx_fill_empty_npkts;
167 unsigned long prev_tx_empty_npkts;
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000168};
169
Ciara Loftus67ed3752020-10-02 13:36:12 +0000170struct xsk_driver_stats {
171 unsigned long intrs;
172 unsigned long prev_intrs;
173};
174
Ciara Loftus60dc6092020-10-02 13:36:11 +0000175struct xsk_app_stats {
176 unsigned long rx_empty_polls;
177 unsigned long fill_fail_polls;
178 unsigned long copy_tx_sendtos;
179 unsigned long tx_wakeup_sendtos;
180 unsigned long opt_polls;
181 unsigned long prev_rx_empty_polls;
182 unsigned long prev_fill_fail_polls;
183 unsigned long prev_copy_tx_sendtos;
184 unsigned long prev_tx_wakeup_sendtos;
185 unsigned long prev_opt_polls;
186};
187
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000188struct xsk_umem_info {
189 struct xsk_ring_prod fq;
190 struct xsk_ring_cons cq;
191 struct xsk_umem *umem;
192 void *buffer;
193};
194
195struct xsk_socket_info {
196 struct xsk_ring_cons rx;
197 struct xsk_ring_prod tx;
198 struct xsk_umem_info *umem;
199 struct xsk_socket *xsk;
200 struct xsk_ring_stats ring_stats;
Ciara Loftus60dc6092020-10-02 13:36:11 +0000201 struct xsk_app_stats app_stats;
Ciara Loftus67ed3752020-10-02 13:36:12 +0000202 struct xsk_driver_stats drv_stats;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100203 u32 outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200204};
205
Ong Boon Leong5a388252021-12-30 11:54:43 +0800206static const struct clockid_map {
207 const char *name;
208 clockid_t clockid;
209} clockids_map[] = {
210 { "REALTIME", CLOCK_REALTIME },
211 { "TAI", CLOCK_TAI },
212 { "BOOTTIME", CLOCK_BOOTTIME },
213 { "MONOTONIC", CLOCK_MONOTONIC },
214 { NULL }
215};
216
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +0800217static const struct sched_map {
218 const char *name;
219 int policy;
220} schmap[] = {
221 { "OTHER", SCHED_OTHER },
222 { "FIFO", SCHED_FIFO },
223 { NULL }
224};
225
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200226static int num_socks;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100227struct xsk_socket_info *xsks[MAX_SOCKS];
Mariusz Dudek3627d972020-12-03 10:05:46 +0100228int sock;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200229
Ong Boon Leong5a388252021-12-30 11:54:43 +0800230static int get_clockid(clockid_t *id, const char *name)
231{
232 const struct clockid_map *clk;
233
234 for (clk = clockids_map; clk->name; clk++) {
235 if (strcasecmp(clk->name, name) == 0) {
236 *id = clk->clockid;
237 return 0;
238 }
239 }
240
241 return -1;
242}
243
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +0800244static int get_schpolicy(int *policy, const char *name)
245{
246 const struct sched_map *sch;
247
248 for (sch = schmap; sch->name; sch++) {
249 if (strcasecmp(sch->name, name) == 0) {
250 *policy = sch->policy;
251 return 0;
252 }
253 }
254
255 return -1;
256}
257
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200258static unsigned long get_nsecs(void)
259{
260 struct timespec ts;
261
Ong Boon Leong5a388252021-12-30 11:54:43 +0800262 clock_gettime(opt_clock, &ts);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200263 return ts.tv_sec * 1000000000UL + ts.tv_nsec;
264}
265
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200266static void print_benchmark(bool running)
267{
268 const char *bench_str = "INVALID";
269
270 if (opt_bench == BENCH_RXDROP)
271 bench_str = "rxdrop";
272 else if (opt_bench == BENCH_TXONLY)
273 bench_str = "txonly";
274 else if (opt_bench == BENCH_L2FWD)
275 bench_str = "l2fwd";
276
277 printf("%s:%d %s ", opt_if, opt_queue, bench_str);
278 if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
279 printf("xdp-skb ");
280 else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
281 printf("xdp-drv ");
282 else
283 printf(" ");
284
285 if (opt_poll)
286 printf("poll() ");
287
288 if (running) {
289 printf("running...");
290 fflush(stdout);
291 }
292}
293
Ciara Loftusb36c3202020-07-08 07:28:34 +0000294static int xsk_get_xdp_stats(int fd, struct xsk_socket_info *xsk)
295{
296 struct xdp_statistics stats;
297 socklen_t optlen;
298 int err;
299
300 optlen = sizeof(stats);
301 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
302 if (err)
303 return err;
304
305 if (optlen == sizeof(struct xdp_statistics)) {
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000306 xsk->ring_stats.rx_dropped_npkts = stats.rx_dropped;
307 xsk->ring_stats.rx_invalid_npkts = stats.rx_invalid_descs;
308 xsk->ring_stats.tx_invalid_npkts = stats.tx_invalid_descs;
309 xsk->ring_stats.rx_full_npkts = stats.rx_ring_full;
310 xsk->ring_stats.rx_fill_empty_npkts = stats.rx_fill_ring_empty_descs;
311 xsk->ring_stats.tx_empty_npkts = stats.tx_ring_empty_descs;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000312 return 0;
313 }
314
315 return -EINVAL;
316}
317
Ciara Loftus60dc6092020-10-02 13:36:11 +0000318static void dump_app_stats(long dt)
319{
320 int i;
321
322 for (i = 0; i < num_socks && xsks[i]; i++) {
323 char *fmt = "%-18s %'-14.0f %'-14lu\n";
324 double rx_empty_polls_ps, fill_fail_polls_ps, copy_tx_sendtos_ps,
325 tx_wakeup_sendtos_ps, opt_polls_ps;
326
327 rx_empty_polls_ps = (xsks[i]->app_stats.rx_empty_polls -
328 xsks[i]->app_stats.prev_rx_empty_polls) * 1000000000. / dt;
329 fill_fail_polls_ps = (xsks[i]->app_stats.fill_fail_polls -
330 xsks[i]->app_stats.prev_fill_fail_polls) * 1000000000. / dt;
331 copy_tx_sendtos_ps = (xsks[i]->app_stats.copy_tx_sendtos -
332 xsks[i]->app_stats.prev_copy_tx_sendtos) * 1000000000. / dt;
333 tx_wakeup_sendtos_ps = (xsks[i]->app_stats.tx_wakeup_sendtos -
334 xsks[i]->app_stats.prev_tx_wakeup_sendtos)
335 * 1000000000. / dt;
336 opt_polls_ps = (xsks[i]->app_stats.opt_polls -
337 xsks[i]->app_stats.prev_opt_polls) * 1000000000. / dt;
338
339 printf("\n%-18s %-14s %-14s\n", "", "calls/s", "count");
340 printf(fmt, "rx empty polls", rx_empty_polls_ps, xsks[i]->app_stats.rx_empty_polls);
341 printf(fmt, "fill fail polls", fill_fail_polls_ps,
342 xsks[i]->app_stats.fill_fail_polls);
343 printf(fmt, "copy tx sendtos", copy_tx_sendtos_ps,
344 xsks[i]->app_stats.copy_tx_sendtos);
345 printf(fmt, "tx wakeup sendtos", tx_wakeup_sendtos_ps,
346 xsks[i]->app_stats.tx_wakeup_sendtos);
347 printf(fmt, "opt polls", opt_polls_ps, xsks[i]->app_stats.opt_polls);
348
349 xsks[i]->app_stats.prev_rx_empty_polls = xsks[i]->app_stats.rx_empty_polls;
350 xsks[i]->app_stats.prev_fill_fail_polls = xsks[i]->app_stats.fill_fail_polls;
351 xsks[i]->app_stats.prev_copy_tx_sendtos = xsks[i]->app_stats.copy_tx_sendtos;
352 xsks[i]->app_stats.prev_tx_wakeup_sendtos = xsks[i]->app_stats.tx_wakeup_sendtos;
353 xsks[i]->app_stats.prev_opt_polls = xsks[i]->app_stats.opt_polls;
354 }
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +0800355
356 if (opt_tx_cycle_ns) {
357 printf("\n%-18s %-10s %-10s %-10s %-10s %-10s\n",
358 "", "period", "min", "ave", "max", "cycle");
359 printf("%-18s %-10lu %-10lu %-10lu %-10lu %-10lu\n",
360 "Cyclic TX", opt_tx_cycle_ns, tx_cycle_diff_min,
361 (long)(tx_cycle_diff_ave / tx_cycle_cnt),
362 tx_cycle_diff_max, tx_cycle_cnt);
363 }
Ciara Loftus60dc6092020-10-02 13:36:11 +0000364}
365
Ciara Loftus67ed3752020-10-02 13:36:12 +0000366static bool get_interrupt_number(void)
367{
368 FILE *f_int_proc;
369 char line[4096];
370 bool found = false;
371
372 f_int_proc = fopen("/proc/interrupts", "r");
373 if (f_int_proc == NULL) {
374 printf("Failed to open /proc/interrupts.\n");
375 return found;
376 }
377
378 while (!feof(f_int_proc) && !found) {
379 /* Make sure to read a full line at a time */
380 if (fgets(line, sizeof(line), f_int_proc) == NULL ||
381 line[strlen(line) - 1] != '\n') {
382 printf("Error reading from interrupts file\n");
383 break;
384 }
385
386 /* Extract interrupt number from line */
387 if (strstr(line, opt_irq_str) != NULL) {
388 irq_no = atoi(line);
389 found = true;
390 break;
391 }
392 }
393
394 fclose(f_int_proc);
395
396 return found;
397}
398
399static int get_irqs(void)
400{
401 char count_path[PATH_MAX];
402 int total_intrs = -1;
403 FILE *f_count_proc;
404 char line[4096];
405
406 snprintf(count_path, sizeof(count_path),
407 "/sys/kernel/irq/%i/per_cpu_count", irq_no);
408 f_count_proc = fopen(count_path, "r");
409 if (f_count_proc == NULL) {
410 printf("Failed to open %s\n", count_path);
411 return total_intrs;
412 }
413
414 if (fgets(line, sizeof(line), f_count_proc) == NULL ||
415 line[strlen(line) - 1] != '\n') {
416 printf("Error reading from %s\n", count_path);
417 } else {
418 static const char com[2] = ",";
419 char *token;
420
421 total_intrs = 0;
422 token = strtok(line, com);
423 while (token != NULL) {
424 /* sum up interrupts across all cores */
425 total_intrs += atoi(token);
426 token = strtok(NULL, com);
427 }
428 }
429
430 fclose(f_count_proc);
431
432 return total_intrs;
433}
434
435static void dump_driver_stats(long dt)
436{
437 int i;
438
439 for (i = 0; i < num_socks && xsks[i]; i++) {
440 char *fmt = "%-18s %'-14.0f %'-14lu\n";
441 double intrs_ps;
442 int n_ints = get_irqs();
443
444 if (n_ints < 0) {
445 printf("error getting intr info for intr %i\n", irq_no);
446 return;
447 }
448 xsks[i]->drv_stats.intrs = n_ints - irqs_at_init;
449
450 intrs_ps = (xsks[i]->drv_stats.intrs - xsks[i]->drv_stats.prev_intrs) *
451 1000000000. / dt;
452
453 printf("\n%-18s %-14s %-14s\n", "", "intrs/s", "count");
454 printf(fmt, "irqs", intrs_ps, xsks[i]->drv_stats.intrs);
455
456 xsks[i]->drv_stats.prev_intrs = xsks[i]->drv_stats.intrs;
457 }
458}
459
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200460static void dump_stats(void)
461{
462 unsigned long now = get_nsecs();
463 long dt = now - prev_time;
464 int i;
465
466 prev_time = now;
467
Prashant Bhole11c3f512018-08-31 10:00:49 +0900468 for (i = 0; i < num_socks && xsks[i]; i++) {
Ciara Loftus60dc6092020-10-02 13:36:11 +0000469 char *fmt = "%-18s %'-14.0f %'-14lu\n";
Ciara Loftusb36c3202020-07-08 07:28:34 +0000470 double rx_pps, tx_pps, dropped_pps, rx_invalid_pps, full_pps, fill_empty_pps,
471 tx_invalid_pps, tx_empty_pps;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200472
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000473 rx_pps = (xsks[i]->ring_stats.rx_npkts - xsks[i]->ring_stats.prev_rx_npkts) *
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200474 1000000000. / dt;
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000475 tx_pps = (xsks[i]->ring_stats.tx_npkts - xsks[i]->ring_stats.prev_tx_npkts) *
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200476 1000000000. / dt;
477
478 printf("\n sock%d@", i);
479 print_benchmark(false);
480 printf("\n");
481
Ciara Loftus60dc6092020-10-02 13:36:11 +0000482 printf("%-18s %-14s %-14s %-14.2f\n", "", "pps", "pkts",
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200483 dt / 1000000000.);
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000484 printf(fmt, "rx", rx_pps, xsks[i]->ring_stats.rx_npkts);
485 printf(fmt, "tx", tx_pps, xsks[i]->ring_stats.tx_npkts);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200486
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000487 xsks[i]->ring_stats.prev_rx_npkts = xsks[i]->ring_stats.rx_npkts;
488 xsks[i]->ring_stats.prev_tx_npkts = xsks[i]->ring_stats.tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000489
490 if (opt_extra_stats) {
491 if (!xsk_get_xdp_stats(xsk_socket__fd(xsks[i]->xsk), xsks[i])) {
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000492 dropped_pps = (xsks[i]->ring_stats.rx_dropped_npkts -
493 xsks[i]->ring_stats.prev_rx_dropped_npkts) *
494 1000000000. / dt;
495 rx_invalid_pps = (xsks[i]->ring_stats.rx_invalid_npkts -
496 xsks[i]->ring_stats.prev_rx_invalid_npkts) *
497 1000000000. / dt;
498 tx_invalid_pps = (xsks[i]->ring_stats.tx_invalid_npkts -
499 xsks[i]->ring_stats.prev_tx_invalid_npkts) *
500 1000000000. / dt;
501 full_pps = (xsks[i]->ring_stats.rx_full_npkts -
502 xsks[i]->ring_stats.prev_rx_full_npkts) *
503 1000000000. / dt;
504 fill_empty_pps = (xsks[i]->ring_stats.rx_fill_empty_npkts -
505 xsks[i]->ring_stats.prev_rx_fill_empty_npkts) *
506 1000000000. / dt;
507 tx_empty_pps = (xsks[i]->ring_stats.tx_empty_npkts -
508 xsks[i]->ring_stats.prev_tx_empty_npkts) *
509 1000000000. / dt;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000510
511 printf(fmt, "rx dropped", dropped_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000512 xsks[i]->ring_stats.rx_dropped_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000513 printf(fmt, "rx invalid", rx_invalid_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000514 xsks[i]->ring_stats.rx_invalid_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000515 printf(fmt, "tx invalid", tx_invalid_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000516 xsks[i]->ring_stats.tx_invalid_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000517 printf(fmt, "rx queue full", full_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000518 xsks[i]->ring_stats.rx_full_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000519 printf(fmt, "fill ring empty", fill_empty_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000520 xsks[i]->ring_stats.rx_fill_empty_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000521 printf(fmt, "tx ring empty", tx_empty_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000522 xsks[i]->ring_stats.tx_empty_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000523
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000524 xsks[i]->ring_stats.prev_rx_dropped_npkts =
525 xsks[i]->ring_stats.rx_dropped_npkts;
526 xsks[i]->ring_stats.prev_rx_invalid_npkts =
527 xsks[i]->ring_stats.rx_invalid_npkts;
528 xsks[i]->ring_stats.prev_tx_invalid_npkts =
529 xsks[i]->ring_stats.tx_invalid_npkts;
530 xsks[i]->ring_stats.prev_rx_full_npkts =
531 xsks[i]->ring_stats.rx_full_npkts;
532 xsks[i]->ring_stats.prev_rx_fill_empty_npkts =
533 xsks[i]->ring_stats.rx_fill_empty_npkts;
534 xsks[i]->ring_stats.prev_tx_empty_npkts =
535 xsks[i]->ring_stats.tx_empty_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000536 } else {
537 printf("%-15s\n", "Error retrieving extra stats");
538 }
539 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200540 }
Ciara Loftus60dc6092020-10-02 13:36:11 +0000541
542 if (opt_app_stats)
543 dump_app_stats(dt);
Ciara Loftus67ed3752020-10-02 13:36:12 +0000544 if (irq_no)
545 dump_driver_stats(dt);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200546}
547
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530548static bool is_benchmark_done(void)
549{
550 if (opt_duration > 0) {
551 unsigned long dt = (get_nsecs() - start_time);
552
553 if (dt >= opt_duration)
554 benchmark_done = true;
555 }
556 return benchmark_done;
557}
558
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200559static void *poller(void *arg)
560{
561 (void)arg;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530562 while (!is_benchmark_done()) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200563 sleep(opt_interval);
564 dump_stats();
565 }
566
567 return NULL;
568}
569
Wang Hai2620e922021-06-28 17:18:15 +0800570static void remove_xdp_program(void)
571{
572 u32 curr_prog_id = 0;
573
574 if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
575 printf("bpf_get_link_xdp_id failed\n");
576 exit(EXIT_FAILURE);
577 }
578
579 if (prog_id == curr_prog_id)
580 bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
581 else if (!curr_prog_id)
582 printf("couldn't find a prog id on a given interface\n");
583 else
584 printf("program on interface changed, not removing\n");
585}
586
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100587static void int_exit(int sig)
588{
Jay Jayatheerthan69525582019-12-20 14:25:26 +0530589 benchmark_done = true;
590}
591
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100592static void __exit_with_error(int error, const char *file, const char *func,
593 int line)
594{
595 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
596 line, error, strerror(error));
Wang Hai2620e922021-06-28 17:18:15 +0800597
598 if (opt_num_xsks > 1)
599 remove_xdp_program();
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100600 exit(EXIT_FAILURE);
601}
602
Maciej Fijalkowskic9d27c92021-03-30 00:43:06 +0200603#define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
604
605static void xdpsock_cleanup(void)
606{
607 struct xsk_umem *umem = xsks[0]->umem->umem;
608 int i, cmd = CLOSE_CONN;
609
610 dump_stats();
611 for (i = 0; i < num_socks; i++)
612 xsk_socket__delete(xsks[i]->xsk);
613 (void)xsk_umem__delete(umem);
614
615 if (opt_reduced_cap) {
616 if (write(sock, &cmd, sizeof(int)) < 0)
617 exit_with_error(errno);
618 }
Wang Hai2620e922021-06-28 17:18:15 +0800619
620 if (opt_num_xsks > 1)
621 remove_xdp_program();
Maciej Fijalkowskic9d27c92021-03-30 00:43:06 +0200622}
623
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100624static void swap_mac_addresses(void *data)
625{
626 struct ether_header *eth = (struct ether_header *)data;
627 struct ether_addr *src_addr = (struct ether_addr *)&eth->ether_shost;
628 struct ether_addr *dst_addr = (struct ether_addr *)&eth->ether_dhost;
629 struct ether_addr tmp;
630
631 tmp = *src_addr;
632 *src_addr = *dst_addr;
633 *dst_addr = tmp;
634}
635
636static void hex_dump(void *pkt, size_t length, u64 addr)
637{
638 const unsigned char *address = (unsigned char *)pkt;
639 const unsigned char *line = address;
640 size_t line_size = 32;
641 unsigned char c;
642 char buf[32];
643 int i = 0;
644
645 if (!DEBUG_HEXDUMP)
646 return;
647
648 sprintf(buf, "addr=%llu", addr);
649 printf("length = %zu\n", length);
650 printf("%s | ", buf);
651 while (length-- > 0) {
652 printf("%02X ", *address++);
653 if (!(++i % line_size) || (length == 0 && i % line_size)) {
654 if (length == 0) {
655 while (i++ % line_size)
656 printf("__ ");
657 }
658 printf(" | "); /* right close */
659 while (line < address) {
660 c = *line++;
661 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
662 }
663 printf("\n");
664 if (length > 0)
665 printf("%s | ", buf);
666 }
667 }
668 printf("\n");
669}
670
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530671static void *memset32_htonl(void *dest, u32 val, u32 size)
672{
673 u32 *ptr = (u32 *)dest;
674 int i;
675
676 val = htonl(val);
677
678 for (i = 0; i < (size & (~0x3)); i += 4)
679 ptr[i >> 2] = val;
680
681 for (; i < size; i++)
682 ((char *)dest)[i] = ((char *)&val)[i & 3];
683
684 return dest;
685}
686
687/*
688 * This function code has been taken from
689 * Linux kernel lib/checksum.c
690 */
691static inline unsigned short from32to16(unsigned int x)
692{
693 /* add up 16-bit and 16-bit for 16+c bit */
694 x = (x & 0xffff) + (x >> 16);
695 /* add up carry.. */
696 x = (x & 0xffff) + (x >> 16);
697 return x;
698}
699
700/*
701 * This function code has been taken from
702 * Linux kernel lib/checksum.c
703 */
704static unsigned int do_csum(const unsigned char *buff, int len)
705{
706 unsigned int result = 0;
707 int odd;
708
709 if (len <= 0)
710 goto out;
711 odd = 1 & (unsigned long)buff;
712 if (odd) {
713#ifdef __LITTLE_ENDIAN
714 result += (*buff << 8);
715#else
716 result = *buff;
717#endif
718 len--;
719 buff++;
720 }
721 if (len >= 2) {
722 if (2 & (unsigned long)buff) {
723 result += *(unsigned short *)buff;
724 len -= 2;
725 buff += 2;
726 }
727 if (len >= 4) {
728 const unsigned char *end = buff +
729 ((unsigned int)len & ~3);
730 unsigned int carry = 0;
731
732 do {
733 unsigned int w = *(unsigned int *)buff;
734
735 buff += 4;
736 result += carry;
737 result += w;
738 carry = (w > result);
739 } while (buff < end);
740 result += carry;
741 result = (result & 0xffff) + (result >> 16);
742 }
743 if (len & 2) {
744 result += *(unsigned short *)buff;
745 buff += 2;
746 }
747 }
748 if (len & 1)
749#ifdef __LITTLE_ENDIAN
750 result += *buff;
751#else
752 result += (*buff << 8);
753#endif
754 result = from32to16(result);
755 if (odd)
756 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
757out:
758 return result;
759}
760
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530761/*
762 * This is a version of ip_compute_csum() optimized for IP headers,
763 * which always checksum on 4 octet boundaries.
764 * This function code has been taken from
765 * Linux kernel lib/checksum.c
766 */
Niklas Söderlundf4700a62021-08-06 14:28:55 +0200767static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530768{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200769 return (__sum16)~do_csum(iph, ihl * 4);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530770}
771
772/*
773 * Fold a partial checksum
774 * This function code has been taken from
775 * Linux kernel include/asm-generic/checksum.h
776 */
777static inline __sum16 csum_fold(__wsum csum)
778{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200779 u32 sum = (u32)csum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530780
781 sum = (sum & 0xffff) + (sum >> 16);
782 sum = (sum & 0xffff) + (sum >> 16);
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200783 return (__sum16)~sum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530784}
785
786/*
787 * This function code has been taken from
788 * Linux kernel lib/checksum.c
789 */
790static inline u32 from64to32(u64 x)
791{
792 /* add up 32-bit and 32-bit for 32+c bit */
793 x = (x & 0xffffffff) + (x >> 32);
794 /* add up carry.. */
795 x = (x & 0xffffffff) + (x >> 32);
796 return (u32)x;
797}
798
799__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
800 __u32 len, __u8 proto, __wsum sum);
801
802/*
803 * This function code has been taken from
804 * Linux kernel lib/checksum.c
805 */
806__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
807 __u32 len, __u8 proto, __wsum sum)
808{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200809 unsigned long long s = (u32)sum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530810
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200811 s += (u32)saddr;
812 s += (u32)daddr;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530813#ifdef __BIG_ENDIAN__
814 s += proto + len;
815#else
816 s += (proto + len) << 8;
817#endif
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200818 return (__wsum)from64to32(s);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530819}
820
821/*
822 * This function has been taken from
823 * Linux kernel include/asm-generic/checksum.h
824 */
825static inline __sum16
826csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
827 __u8 proto, __wsum sum)
828{
829 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
830}
831
832static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
833 u8 proto, u16 *udp_pkt)
834{
835 u32 csum = 0;
836 u32 cnt = 0;
837
838 /* udp hdr and data */
839 for (; cnt < len; cnt += 2)
840 csum += udp_pkt[cnt >> 1];
841
842 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
843}
844
845#define ETH_FCS_SIZE 4
846
Ong Boon Leong2741a042021-12-30 11:54:41 +0800847#define ETH_HDR_SIZE (opt_vlan_tag ? sizeof(struct vlan_ethhdr) : \
848 sizeof(struct ethhdr))
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800849#define PKTGEN_HDR_SIZE (opt_tstamp ? sizeof(struct pktgen_hdr) : 0)
Ong Boon Leong2741a042021-12-30 11:54:41 +0800850#define PKT_HDR_SIZE (ETH_HDR_SIZE + sizeof(struct iphdr) + \
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800851 sizeof(struct udphdr) + PKTGEN_HDR_SIZE)
852#define PKTGEN_HDR_OFFSET (ETH_HDR_SIZE + sizeof(struct iphdr) + \
853 sizeof(struct udphdr))
854#define PKTGEN_SIZE_MIN (PKTGEN_HDR_OFFSET + sizeof(struct pktgen_hdr) + \
855 ETH_FCS_SIZE)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530856
857#define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
Ong Boon Leong2741a042021-12-30 11:54:41 +0800858#define IP_PKT_SIZE (PKT_SIZE - ETH_HDR_SIZE)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530859#define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800860#define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - \
861 (sizeof(struct udphdr) + PKTGEN_HDR_SIZE))
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530862
863static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
864
865static void gen_eth_hdr_data(void)
866{
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800867 struct pktgen_hdr *pktgen_hdr;
Ong Boon Leong2741a042021-12-30 11:54:41 +0800868 struct udphdr *udp_hdr;
869 struct iphdr *ip_hdr;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530870
Ong Boon Leong2741a042021-12-30 11:54:41 +0800871 if (opt_vlan_tag) {
872 struct vlan_ethhdr *veth_hdr = (struct vlan_ethhdr *)pkt_data;
873 u16 vlan_tci = 0;
874
875 udp_hdr = (struct udphdr *)(pkt_data +
876 sizeof(struct vlan_ethhdr) +
877 sizeof(struct iphdr));
878 ip_hdr = (struct iphdr *)(pkt_data +
879 sizeof(struct vlan_ethhdr));
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800880 pktgen_hdr = (struct pktgen_hdr *)(pkt_data +
881 sizeof(struct vlan_ethhdr) +
882 sizeof(struct iphdr) +
883 sizeof(struct udphdr));
Ong Boon Leong2741a042021-12-30 11:54:41 +0800884 /* ethernet & VLAN header */
Ong Boon Leong6440a6c2021-12-30 11:54:42 +0800885 memcpy(veth_hdr->h_dest, &opt_txdmac, ETH_ALEN);
886 memcpy(veth_hdr->h_source, &opt_txsmac, ETH_ALEN);
Ong Boon Leong2741a042021-12-30 11:54:41 +0800887 veth_hdr->h_vlan_proto = htons(ETH_P_8021Q);
888 vlan_tci = opt_pkt_vlan_id & VLAN_VID_MASK;
889 vlan_tci |= (opt_pkt_vlan_pri << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
890 veth_hdr->h_vlan_TCI = htons(vlan_tci);
891 veth_hdr->h_vlan_encapsulated_proto = htons(ETH_P_IP);
892 } else {
893 struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
894
895 udp_hdr = (struct udphdr *)(pkt_data +
896 sizeof(struct ethhdr) +
897 sizeof(struct iphdr));
898 ip_hdr = (struct iphdr *)(pkt_data +
899 sizeof(struct ethhdr));
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800900 pktgen_hdr = (struct pktgen_hdr *)(pkt_data +
901 sizeof(struct ethhdr) +
902 sizeof(struct iphdr) +
903 sizeof(struct udphdr));
Ong Boon Leong2741a042021-12-30 11:54:41 +0800904 /* ethernet header */
Ong Boon Leong6440a6c2021-12-30 11:54:42 +0800905 memcpy(eth_hdr->h_dest, &opt_txdmac, ETH_ALEN);
906 memcpy(eth_hdr->h_source, &opt_txsmac, ETH_ALEN);
Ong Boon Leong2741a042021-12-30 11:54:41 +0800907 eth_hdr->h_proto = htons(ETH_P_IP);
908 }
909
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530910
911 /* IP header */
912 ip_hdr->version = IPVERSION;
913 ip_hdr->ihl = 0x5; /* 20 byte header */
914 ip_hdr->tos = 0x0;
915 ip_hdr->tot_len = htons(IP_PKT_SIZE);
916 ip_hdr->id = 0;
917 ip_hdr->frag_off = 0;
918 ip_hdr->ttl = IPDEFTTL;
919 ip_hdr->protocol = IPPROTO_UDP;
920 ip_hdr->saddr = htonl(0x0a0a0a10);
921 ip_hdr->daddr = htonl(0x0a0a0a20);
922
923 /* IP header checksum */
924 ip_hdr->check = 0;
925 ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
926
927 /* UDP header */
928 udp_hdr->source = htons(0x1000);
929 udp_hdr->dest = htons(0x1000);
930 udp_hdr->len = htons(UDP_PKT_SIZE);
931
Ong Boon Leongeb68db42021-12-30 11:54:47 +0800932 if (opt_tstamp)
933 pktgen_hdr->pgh_magic = htonl(PKTGEN_MAGIC);
934
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530935 /* UDP data */
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530936 memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530937 UDP_PKT_DATA_SIZE);
938
939 /* UDP header checksum */
940 udp_hdr->check = 0;
941 udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
942 IPPROTO_UDP, (u16 *)udp_hdr);
943}
944
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530945static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100946{
947 memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530948 PKT_SIZE);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100949}
950
951static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size)
952{
953 struct xsk_umem_info *umem;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300954 struct xsk_umem_config cfg = {
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200955 /* We recommend that you set the fill ring size >= HW RX ring size +
956 * AF_XDP RX ring size. Make sure you fill up the fill ring
957 * with buffers at regular intervals, and you will with this setting
958 * avoid allocation failures in the driver. These are usually quite
959 * expensive since drivers have not been written to assume that
960 * allocation failures are common. For regular sockets, kernel
961 * allocated memory is used that only runs out in OOM situations
962 * that should be rare.
963 */
964 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300965 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
966 .frame_size = opt_xsk_frame_size,
967 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
Kevin Laatzc543f542019-08-27 02:25:28 +0000968 .flags = opt_umem_flags
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300969 };
Magnus Karlsson661842c2019-11-07 18:47:39 +0100970 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100971
972 umem = calloc(1, sizeof(*umem));
973 if (!umem)
974 exit_with_error(errno);
975
976 ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300977 &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100978 if (ret)
979 exit_with_error(-ret);
980
Magnus Karlsson661842c2019-11-07 18:47:39 +0100981 umem->buffer = buffer;
982 return umem;
983}
984
985static void xsk_populate_fill_ring(struct xsk_umem_info *umem)
986{
987 int ret, i;
988 u32 idx;
989
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100990 ret = xsk_ring_prod__reserve(&umem->fq,
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200991 XSK_RING_PROD__DEFAULT_NUM_DESCS * 2, &idx);
992 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS * 2)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100993 exit_with_error(-ret);
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200994 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS * 2; i++)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100995 *xsk_ring_prod__fill_addr(&umem->fq, idx++) =
996 i * opt_xsk_frame_size;
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200997 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS * 2);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100998}
999
Magnus Karlsson661842c2019-11-07 18:47:39 +01001000static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
1001 bool rx, bool tx)
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001002{
1003 struct xsk_socket_config cfg;
1004 struct xsk_socket_info *xsk;
Magnus Karlsson661842c2019-11-07 18:47:39 +01001005 struct xsk_ring_cons *rxr;
1006 struct xsk_ring_prod *txr;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001007 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001008
1009 xsk = calloc(1, sizeof(*xsk));
1010 if (!xsk)
1011 exit_with_error(errno);
1012
1013 xsk->umem = umem;
1014 cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
1015 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001016 if (opt_num_xsks > 1 || opt_reduced_cap)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001017 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
1018 else
1019 cfg.libbpf_flags = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001020 cfg.xdp_flags = opt_xdp_flags;
1021 cfg.bind_flags = opt_xdp_bind_flags;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001022
Magnus Karlsson661842c2019-11-07 18:47:39 +01001023 rxr = rx ? &xsk->rx : NULL;
1024 txr = tx ? &xsk->tx : NULL;
1025 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem,
1026 rxr, txr, &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001027 if (ret)
1028 exit_with_error(-ret);
1029
Wang Hai2620e922021-06-28 17:18:15 +08001030 ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
1031 if (ret)
1032 exit_with_error(-ret);
1033
Ciara Loftus60dc6092020-10-02 13:36:11 +00001034 xsk->app_stats.rx_empty_polls = 0;
1035 xsk->app_stats.fill_fail_polls = 0;
1036 xsk->app_stats.copy_tx_sendtos = 0;
1037 xsk->app_stats.tx_wakeup_sendtos = 0;
1038 xsk->app_stats.opt_polls = 0;
1039 xsk->app_stats.prev_rx_empty_polls = 0;
1040 xsk->app_stats.prev_fill_fail_polls = 0;
1041 xsk->app_stats.prev_copy_tx_sendtos = 0;
1042 xsk->app_stats.prev_tx_wakeup_sendtos = 0;
1043 xsk->app_stats.prev_opt_polls = 0;
1044
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001045 return xsk;
1046}
1047
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001048static struct option long_options[] = {
1049 {"rxdrop", no_argument, 0, 'r'},
1050 {"txonly", no_argument, 0, 't'},
1051 {"l2fwd", no_argument, 0, 'l'},
1052 {"interface", required_argument, 0, 'i'},
1053 {"queue", required_argument, 0, 'q'},
1054 {"poll", no_argument, 0, 'p'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001055 {"xdp-skb", no_argument, 0, 'S'},
1056 {"xdp-native", no_argument, 0, 'N'},
1057 {"interval", required_argument, 0, 'n'},
Ong Boon Leong8121e782021-12-30 11:54:46 +08001058 {"retries", required_argument, 0, 'O'},
Björn Töpel58c50ae2018-08-28 14:44:35 +02001059 {"zero-copy", no_argument, 0, 'z'},
1060 {"copy", no_argument, 0, 'c'},
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001061 {"frame-size", required_argument, 0, 'f'},
Magnus Karlsson46738f72019-08-14 09:27:21 +02001062 {"no-need-wakeup", no_argument, 0, 'm'},
Kevin Laatzc543f542019-08-27 02:25:28 +00001063 {"unaligned", no_argument, 0, 'u'},
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001064 {"shared-umem", no_argument, 0, 'M'},
Andre Guedesb3133322019-11-14 08:28:47 -08001065 {"force", no_argument, 0, 'F'},
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301066 {"duration", required_argument, 0, 'd'},
Ong Boon Leong5a388252021-12-30 11:54:43 +08001067 {"clock", required_argument, 0, 'w'},
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301068 {"batch-size", required_argument, 0, 'b'},
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301069 {"tx-pkt-count", required_argument, 0, 'C'},
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301070 {"tx-pkt-size", required_argument, 0, 's'},
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301071 {"tx-pkt-pattern", required_argument, 0, 'P'},
Ong Boon Leong2741a042021-12-30 11:54:41 +08001072 {"tx-vlan", no_argument, 0, 'V'},
1073 {"tx-vlan-id", required_argument, 0, 'J'},
1074 {"tx-vlan-pri", required_argument, 0, 'K'},
Ong Boon Leong6440a6c2021-12-30 11:54:42 +08001075 {"tx-dmac", required_argument, 0, 'G'},
1076 {"tx-smac", required_argument, 0, 'H'},
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001077 {"tx-cycle", required_argument, 0, 'T'},
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001078 {"tstamp", no_argument, 0, 'y'},
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001079 {"policy", required_argument, 0, 'W'},
1080 {"schpri", required_argument, 0, 'U'},
Ciara Loftusb36c3202020-07-08 07:28:34 +00001081 {"extra-stats", no_argument, 0, 'x'},
Magnus Karlsson74e00672020-09-10 10:31:06 +02001082 {"quiet", no_argument, 0, 'Q'},
Ciara Loftus60dc6092020-10-02 13:36:11 +00001083 {"app-stats", no_argument, 0, 'a'},
Ciara Loftus67ed3752020-10-02 13:36:12 +00001084 {"irq-string", no_argument, 0, 'I'},
Björn Töpelb35fc142020-11-30 19:52:04 +01001085 {"busy-poll", no_argument, 0, 'B'},
Mariusz Dudek3627d972020-12-03 10:05:46 +01001086 {"reduce-cap", no_argument, 0, 'R'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001087 {0, 0, 0, 0}
1088};
1089
1090static void usage(const char *prog)
1091{
1092 const char *str =
1093 " Usage: %s [OPTIONS]\n"
1094 " Options:\n"
1095 " -r, --rxdrop Discard all incoming packets (default)\n"
1096 " -t, --txonly Only send packets\n"
1097 " -l, --l2fwd MAC swap L2 forwarding\n"
1098 " -i, --interface=n Run on interface n\n"
1099 " -q, --queue=n Use queue n (default 0)\n"
1100 " -p, --poll Use poll syscall\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001101 " -S, --xdp-skb=n Use XDP skb-mod\n"
Anton Ivanov4564a8bb2019-10-07 09:26:36 +01001102 " -N, --xdp-native=n Enforce XDP native mode\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001103 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
Ong Boon Leong8121e782021-12-30 11:54:46 +08001104 " -O, --retries=n Specify time-out retries (1s interval) attempt (default 3).\n"
Björn Töpel58c50ae2018-08-28 14:44:35 +02001105 " -z, --zero-copy Force zero-copy mode.\n"
1106 " -c, --copy Force copy mode.\n"
Magnus Karlsson46738f72019-08-14 09:27:21 +02001107 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
Kevin Laatzc543f542019-08-27 02:25:28 +00001108 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
1109 " -u, --unaligned Enable unaligned chunk placement\n"
Mariusz Dudek3627d972020-12-03 10:05:46 +01001110 " -M, --shared-umem Enable XDP_SHARED_UMEM (cannot be used with -R)\n"
Andre Guedesb3133322019-11-14 08:28:47 -08001111 " -F, --force Force loading the XDP prog\n"
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301112 " -d, --duration=n Duration in secs to run command.\n"
1113 " Default: forever.\n"
Ong Boon Leong5a388252021-12-30 11:54:43 +08001114 " -w, --clock=CLOCK Clock NAME (default MONOTONIC).\n"
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301115 " -b, --batch-size=n Batch size for sending or receiving\n"
1116 " packets. Default: %d\n"
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301117 " -C, --tx-pkt-count=n Number of packets to send.\n"
1118 " Default: Continuous packets.\n"
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301119 " -s, --tx-pkt-size=n Transmit packet size.\n"
1120 " (Default: %d bytes)\n"
1121 " Min size: %d, Max size %d.\n"
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301122 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
Ong Boon Leong2741a042021-12-30 11:54:41 +08001123 " -V, --tx-vlan Send VLAN tagged packets (For -t|--txonly)\n"
1124 " -J, --tx-vlan-id=n Tx VLAN ID [1-4095]. Default: %d (For -V|--tx-vlan)\n"
1125 " -K, --tx-vlan-pri=n Tx VLAN Priority [0-7]. Default: %d (For -V|--tx-vlan)\n"
Ong Boon Leong6440a6c2021-12-30 11:54:42 +08001126 " -G, --tx-dmac=<MAC> Dest MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n"
1127 " -H, --tx-smac=<MAC> Src MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n"
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001128 " -T, --tx-cycle=n Tx cycle time in micro-seconds (For -t|--txonly).\n"
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001129 " -y, --tstamp Add time-stamp to packet (For -t|--txonly).\n"
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001130 " -W, --policy=POLICY Schedule policy. Default: SCHED_OTHER\n"
1131 " -U, --schpri=n Schedule priority. Default: %d\n"
Ciara Loftusb36c3202020-07-08 07:28:34 +00001132 " -x, --extra-stats Display extra statistics.\n"
Magnus Karlsson74e00672020-09-10 10:31:06 +02001133 " -Q, --quiet Do not display any stats.\n"
Ciara Loftus60dc6092020-10-02 13:36:11 +00001134 " -a, --app-stats Display application (syscall) statistics.\n"
Ciara Loftus67ed3752020-10-02 13:36:12 +00001135 " -I, --irq-string Display driver interrupt statistics for interface associated with irq-string.\n"
Björn Töpelb35fc142020-11-30 19:52:04 +01001136 " -B, --busy-poll Busy poll.\n"
Mariusz Dudek3627d972020-12-03 10:05:46 +01001137 " -R, --reduce-cap Use reduced capabilities (cannot be used with -M)\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001138 "\n";
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301139 fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301140 opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
Ong Boon Leong2741a042021-12-30 11:54:41 +08001141 XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern,
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001142 VLAN_VID__DEFAULT, VLAN_PRI__DEFAULT,
1143 SCHED_PRI__DEFAULT);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301144
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001145 exit(EXIT_FAILURE);
1146}
1147
1148static void parse_command_line(int argc, char **argv)
1149{
1150 int option_index, c;
1151
1152 opterr = 0;
1153
1154 for (;;) {
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001155 c = getopt_long(argc, argv,
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001156 "Frtli:q:pSNn:w:O:czf:muMd:b:C:s:P:VJ:K:G:H:T:yW:U:xQaI:BR",
Magnus Karlsson46738f72019-08-14 09:27:21 +02001157 long_options, &option_index);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001158 if (c == -1)
1159 break;
1160
1161 switch (c) {
1162 case 'r':
1163 opt_bench = BENCH_RXDROP;
1164 break;
1165 case 't':
1166 opt_bench = BENCH_TXONLY;
1167 break;
1168 case 'l':
1169 opt_bench = BENCH_L2FWD;
1170 break;
1171 case 'i':
1172 opt_if = optarg;
1173 break;
1174 case 'q':
1175 opt_queue = atoi(optarg);
1176 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001177 case 'p':
1178 opt_poll = 1;
1179 break;
1180 case 'S':
1181 opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
Björn Töpel9f5232c2018-06-04 14:06:01 +02001182 opt_xdp_bind_flags |= XDP_COPY;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001183 break;
1184 case 'N':
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +01001185 /* default, set below */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001186 break;
1187 case 'n':
1188 opt_interval = atoi(optarg);
1189 break;
Ong Boon Leong5a388252021-12-30 11:54:43 +08001190 case 'w':
1191 if (get_clockid(&opt_clock, optarg)) {
1192 fprintf(stderr,
1193 "ERROR: Invalid clock %s. Default to CLOCK_MONOTONIC.\n",
1194 optarg);
1195 opt_clock = CLOCK_MONOTONIC;
1196 }
1197 break;
Ong Boon Leong8121e782021-12-30 11:54:46 +08001198 case 'O':
1199 opt_retries = atoi(optarg);
1200 break;
Björn Töpel58c50ae2018-08-28 14:44:35 +02001201 case 'z':
1202 opt_xdp_bind_flags |= XDP_ZEROCOPY;
1203 break;
1204 case 'c':
1205 opt_xdp_bind_flags |= XDP_COPY;
1206 break;
Kevin Laatzc543f542019-08-27 02:25:28 +00001207 case 'u':
1208 opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
1209 opt_unaligned_chunks = 1;
Kevin Laatz3945b372019-08-27 02:25:30 +00001210 opt_mmap_flags = MAP_HUGETLB;
Kevin Laatzc543f542019-08-27 02:25:28 +00001211 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +01001212 case 'F':
1213 opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
1214 break;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001215 case 'f':
1216 opt_xsk_frame_size = atoi(optarg);
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001217 break;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001218 case 'm':
1219 opt_need_wakeup = false;
1220 opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001221 break;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001222 case 'M':
1223 opt_num_xsks = MAX_SOCKS;
1224 break;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301225 case 'd':
1226 opt_duration = atoi(optarg);
1227 opt_duration *= 1000000000;
1228 break;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301229 case 'b':
1230 opt_batch_size = atoi(optarg);
1231 break;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301232 case 'C':
1233 opt_pkt_count = atoi(optarg);
1234 break;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301235 case 's':
1236 opt_pkt_size = atoi(optarg);
1237 if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
1238 opt_pkt_size < MIN_PKT_SIZE) {
1239 fprintf(stderr,
1240 "ERROR: Invalid frame size %d\n",
1241 opt_pkt_size);
1242 usage(basename(argv[0]));
1243 }
1244 break;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301245 case 'P':
1246 opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
1247 break;
Ong Boon Leong2741a042021-12-30 11:54:41 +08001248 case 'V':
1249 opt_vlan_tag = true;
1250 break;
1251 case 'J':
1252 opt_pkt_vlan_id = atoi(optarg);
1253 break;
1254 case 'K':
1255 opt_pkt_vlan_pri = atoi(optarg);
1256 break;
Ong Boon Leong6440a6c2021-12-30 11:54:42 +08001257 case 'G':
1258 if (!ether_aton_r(optarg,
1259 (struct ether_addr *)&opt_txdmac)) {
1260 fprintf(stderr, "Invalid dmac address:%s\n",
1261 optarg);
1262 usage(basename(argv[0]));
1263 }
1264 break;
1265 case 'H':
1266 if (!ether_aton_r(optarg,
1267 (struct ether_addr *)&opt_txsmac)) {
1268 fprintf(stderr, "Invalid smac address:%s\n",
1269 optarg);
1270 usage(basename(argv[0]));
1271 }
1272 break;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001273 case 'T':
1274 opt_tx_cycle_ns = atoi(optarg);
1275 opt_tx_cycle_ns *= NSEC_PER_USEC;
1276 break;
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001277 case 'y':
1278 opt_tstamp = 1;
1279 break;
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001280 case 'W':
1281 if (get_schpolicy(&opt_schpolicy, optarg)) {
1282 fprintf(stderr,
1283 "ERROR: Invalid policy %s. Default to SCHED_OTHER.\n",
1284 optarg);
1285 opt_schpolicy = SCHED_OTHER;
1286 }
1287 break;
1288 case 'U':
1289 opt_schprio = atoi(optarg);
1290 break;
Ciara Loftusb36c3202020-07-08 07:28:34 +00001291 case 'x':
1292 opt_extra_stats = 1;
1293 break;
Magnus Karlsson74e00672020-09-10 10:31:06 +02001294 case 'Q':
1295 opt_quiet = 1;
1296 break;
Ciara Loftus60dc6092020-10-02 13:36:11 +00001297 case 'a':
1298 opt_app_stats = 1;
1299 break;
Ciara Loftus67ed3752020-10-02 13:36:12 +00001300 case 'I':
1301 opt_irq_str = optarg;
1302 if (get_interrupt_number())
1303 irqs_at_init = get_irqs();
1304 if (irqs_at_init < 0) {
1305 fprintf(stderr, "ERROR: Failed to get irqs for %s\n", opt_irq_str);
1306 usage(basename(argv[0]));
1307 }
Björn Töpelb35fc142020-11-30 19:52:04 +01001308 break;
1309 case 'B':
1310 opt_busy_poll = 1;
Ciara Loftus67ed3752020-10-02 13:36:12 +00001311 break;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001312 case 'R':
1313 opt_reduced_cap = true;
1314 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001315 default:
1316 usage(basename(argv[0]));
1317 }
1318 }
1319
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +01001320 if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
1321 opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
1322
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001323 opt_ifindex = if_nametoindex(opt_if);
1324 if (!opt_ifindex) {
1325 fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
1326 opt_if);
1327 usage(basename(argv[0]));
1328 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001329
Kevin Laatzc543f542019-08-27 02:25:28 +00001330 if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
1331 !opt_unaligned_chunks) {
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001332 fprintf(stderr, "--frame-size=%d is not a power of two\n",
1333 opt_xsk_frame_size);
1334 usage(basename(argv[0]));
1335 }
Mariusz Dudek3627d972020-12-03 10:05:46 +01001336
1337 if (opt_reduced_cap && opt_num_xsks > 1) {
1338 fprintf(stderr, "ERROR: -M and -R cannot be used together\n");
1339 usage(basename(argv[0]));
1340 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001341}
1342
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001343static void kick_tx(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001344{
1345 int ret;
1346
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001347 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
Maciej Fijalkowski8ed47e12020-02-05 05:58:34 +01001348 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
1349 errno == EBUSY || errno == ENETDOWN)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001350 return;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001351 exit_with_error(errno);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001352}
1353
Björn Töpel284cbc62020-11-30 19:52:03 +01001354static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001355{
Kevin Laatz03895e62019-08-27 02:25:29 +00001356 struct xsk_umem_info *umem = xsk->umem;
Yonghong Songb74e21a2019-02-28 22:19:41 -08001357 u32 idx_cq = 0, idx_fq = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001358 unsigned int rcvd;
1359 size_t ndescs;
1360
1361 if (!xsk->outstanding_tx)
1362 return;
1363
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001364 /* In copy mode, Tx is driven by a syscall so we need to use e.g. sendto() to
1365 * really send the packets. In zero-copy mode we do not have to do this, since Tx
1366 * is driven by the NAPI loop. So as an optimization, we do not have to call
1367 * sendto() all the time in zero-copy mode for l2fwd.
1368 */
Ciara Loftus60dc6092020-10-02 13:36:11 +00001369 if (opt_xdp_bind_flags & XDP_COPY) {
1370 xsk->app_stats.copy_tx_sendtos++;
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001371 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001372 }
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001373
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301374 ndescs = (xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001375 xsk->outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001376
1377 /* re-add completed Tx buffers */
Kevin Laatz03895e62019-08-27 02:25:29 +00001378 rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001379 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001380 unsigned int i;
1381 int ret;
1382
Kevin Laatz03895e62019-08-27 02:25:29 +00001383 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001384 while (ret != rcvd) {
1385 if (ret < 0)
1386 exit_with_error(-ret);
Björn Töpelb35fc142020-11-30 19:52:04 +01001387 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001388 xsk->app_stats.fill_fail_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001389 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL,
1390 NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001391 }
Kevin Laatz03895e62019-08-27 02:25:29 +00001392 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001393 }
Kevin Laatz03895e62019-08-27 02:25:29 +00001394
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001395 for (i = 0; i < rcvd; i++)
Kevin Laatz03895e62019-08-27 02:25:29 +00001396 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
1397 *xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001398
1399 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1400 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001401 xsk->outstanding_tx -= rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001402 }
1403}
1404
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301405static inline void complete_tx_only(struct xsk_socket_info *xsk,
1406 int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001407{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001408 unsigned int rcvd;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001409 u32 idx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001410
1411 if (!xsk->outstanding_tx)
1412 return;
1413
Ciara Loftus60dc6092020-10-02 13:36:11 +00001414 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
1415 xsk->app_stats.tx_wakeup_sendtos++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001416 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001417 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001418
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301419 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001420 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001421 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001422 xsk->outstanding_tx -= rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001423 }
1424}
1425
Björn Töpelf2d27282020-11-30 19:52:02 +01001426static void rx_drop(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001427{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001428 unsigned int rcvd, i;
Yonghong Songb74e21a2019-02-28 22:19:41 -08001429 u32 idx_rx = 0, idx_fq = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001430 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001431
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301432 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001433 if (!rcvd) {
Björn Töpelb35fc142020-11-30 19:52:04 +01001434 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001435 xsk->app_stats.rx_empty_polls++;
Björn Töpelf2d27282020-11-30 19:52:02 +01001436 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001437 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001438 return;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001439 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001440
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001441 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
1442 while (ret != rcvd) {
1443 if (ret < 0)
1444 exit_with_error(-ret);
Björn Töpelb35fc142020-11-30 19:52:04 +01001445 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001446 xsk->app_stats.fill_fail_polls++;
Björn Töpelf2d27282020-11-30 19:52:02 +01001447 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001448 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001449 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001450 }
1451
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001452 for (i = 0; i < rcvd; i++) {
1453 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1454 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Kevin Laatz03895e62019-08-27 02:25:29 +00001455 u64 orig = xsk_umem__extract_addr(addr);
1456
1457 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001458 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001459
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001460 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001461 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001462 }
1463
1464 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1465 xsk_ring_cons__release(&xsk->rx, rcvd);
Ciara Loftus2e8806f2020-10-02 13:36:10 +00001466 xsk->ring_stats.rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001467}
1468
1469static void rx_drop_all(void)
1470{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001471 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001472 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001473
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001474 for (i = 0; i < num_socks; i++) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001475 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001476 fds[i].events = POLLIN;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001477 }
1478
1479 for (;;) {
1480 if (opt_poll) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001481 for (i = 0; i < num_socks; i++)
1482 xsks[i]->app_stats.opt_polls++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001483 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001484 if (ret <= 0)
1485 continue;
1486 }
1487
1488 for (i = 0; i < num_socks; i++)
Björn Töpelf2d27282020-11-30 19:52:02 +01001489 rx_drop(xsks[i]);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301490
1491 if (benchmark_done)
1492 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001493 }
1494}
1495
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001496static int tx_only(struct xsk_socket_info *xsk, u32 *frame_nb,
1497 int batch_size, unsigned long tx_ns)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001498{
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001499 u32 idx, tv_sec, tv_usec;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301500 unsigned int i;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001501
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301502 while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
1503 batch_size) {
1504 complete_tx_only(xsk, batch_size);
Magnus Karlsson092fde02020-12-10 17:34:07 +01001505 if (benchmark_done)
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001506 return 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001507 }
1508
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001509 if (opt_tstamp) {
1510 tv_sec = (u32)(tx_ns / NSEC_PER_SEC);
1511 tv_usec = (u32)((tx_ns % NSEC_PER_SEC) / 1000);
1512 }
1513
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301514 for (i = 0; i < batch_size; i++) {
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301515 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
1516 idx + i);
Magnus Karlsson3b80d102021-05-06 14:43:49 +02001517 tx_desc->addr = (*frame_nb + i) * opt_xsk_frame_size;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301518 tx_desc->len = PKT_SIZE;
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001519
1520 if (opt_tstamp) {
1521 struct pktgen_hdr *pktgen_hdr;
1522 u64 addr = tx_desc->addr;
1523 char *pkt;
1524
1525 pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1526 pktgen_hdr = (struct pktgen_hdr *)(pkt + PKTGEN_HDR_OFFSET);
1527
1528 pktgen_hdr->seq_num = htonl(sequence++);
1529 pktgen_hdr->tv_sec = htonl(tv_sec);
1530 pktgen_hdr->tv_usec = htonl(tv_usec);
1531
1532 hex_dump(pkt, PKT_SIZE, addr);
1533 }
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301534 }
1535
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301536 xsk_ring_prod__submit(&xsk->tx, batch_size);
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001537 xsk->ring_stats.tx_npkts += batch_size;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301538 xsk->outstanding_tx += batch_size;
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001539 *frame_nb += batch_size;
1540 *frame_nb %= NUM_FRAMES;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301541 complete_tx_only(xsk, batch_size);
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001542
1543 return batch_size;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301544}
1545
1546static inline int get_batch_size(int pkt_cnt)
1547{
1548 if (!opt_pkt_count)
1549 return opt_batch_size;
1550
1551 if (pkt_cnt + opt_batch_size <= opt_pkt_count)
1552 return opt_batch_size;
1553
1554 return opt_pkt_count - pkt_cnt;
1555}
1556
1557static void complete_tx_only_all(void)
1558{
1559 bool pending;
1560 int i;
1561
1562 do {
1563 pending = false;
1564 for (i = 0; i < num_socks; i++) {
1565 if (xsks[i]->outstanding_tx) {
1566 complete_tx_only(xsks[i], opt_batch_size);
1567 pending = !!xsks[i]->outstanding_tx;
1568 }
1569 }
Ong Boon Leong8121e782021-12-30 11:54:46 +08001570 sleep(1);
1571 } while (pending && opt_retries-- > 0);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001572}
1573
1574static void tx_only_all(void)
1575{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001576 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001577 u32 frame_nb[MAX_SOCKS] = {};
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001578 unsigned long next_tx_ns = 0;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301579 int pkt_cnt = 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001580 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001581
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001582 if (opt_poll && opt_tx_cycle_ns) {
1583 fprintf(stderr,
1584 "Error: --poll and --tx-cycles are both set\n");
1585 return;
1586 }
1587
Magnus Karlsson46738f72019-08-14 09:27:21 +02001588 for (i = 0; i < num_socks; i++) {
1589 fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
1590 fds[0].events = POLLOUT;
1591 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001592
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001593 if (opt_tx_cycle_ns) {
1594 /* Align Tx time to micro-second boundary */
1595 next_tx_ns = (get_nsecs() / NSEC_PER_USEC + 1) *
1596 NSEC_PER_USEC;
1597 next_tx_ns += opt_tx_cycle_ns;
1598
1599 /* Initialize periodic Tx scheduling variance */
1600 tx_cycle_diff_min = 1000000000;
1601 tx_cycle_diff_max = 0;
1602 tx_cycle_diff_ave = 0.0;
1603 }
1604
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301605 while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
1606 int batch_size = get_batch_size(pkt_cnt);
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001607 unsigned long tx_ns = 0;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001608 struct timespec next;
1609 int tx_cnt = 0;
1610 long diff;
1611 int err;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301612
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001613 if (opt_poll) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001614 for (i = 0; i < num_socks; i++)
1615 xsks[i]->app_stats.opt_polls++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001616 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001617 if (ret <= 0)
1618 continue;
1619
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001620 if (!(fds[0].revents & POLLOUT))
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001621 continue;
1622 }
1623
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001624 if (opt_tx_cycle_ns) {
1625 next.tv_sec = next_tx_ns / NSEC_PER_SEC;
1626 next.tv_nsec = next_tx_ns % NSEC_PER_SEC;
1627 err = clock_nanosleep(opt_clock, TIMER_ABSTIME, &next, NULL);
1628 if (err) {
1629 if (err != EINTR)
1630 fprintf(stderr,
1631 "clock_nanosleep failed. Err:%d errno:%d\n",
1632 err, errno);
1633 break;
1634 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301635
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001636 /* Measure periodic Tx scheduling variance */
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001637 tx_ns = get_nsecs();
1638 diff = tx_ns - next_tx_ns;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001639 if (diff < tx_cycle_diff_min)
1640 tx_cycle_diff_min = diff;
1641
1642 if (diff > tx_cycle_diff_max)
1643 tx_cycle_diff_max = diff;
1644
1645 tx_cycle_diff_ave += (double)diff;
1646 tx_cycle_cnt++;
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001647 } else if (opt_tstamp) {
1648 tx_ns = get_nsecs();
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001649 }
1650
1651 for (i = 0; i < num_socks; i++)
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001652 tx_cnt += tx_only(xsks[i], &frame_nb[i], batch_size, tx_ns);
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001653
1654 pkt_cnt += tx_cnt;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301655
1656 if (benchmark_done)
1657 break;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001658
1659 if (opt_tx_cycle_ns)
1660 next_tx_ns += opt_tx_cycle_ns;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001661 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301662
1663 if (opt_pkt_count)
1664 complete_tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001665}
1666
Björn Töpel284cbc62020-11-30 19:52:03 +01001667static void l2fwd(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001668{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001669 unsigned int rcvd, i;
1670 u32 idx_rx = 0, idx_tx = 0;
1671 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001672
Björn Töpel284cbc62020-11-30 19:52:03 +01001673 complete_tx_l2fwd(xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001674
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301675 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001676 if (!rcvd) {
Björn Töpelb35fc142020-11-30 19:52:04 +01001677 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001678 xsk->app_stats.rx_empty_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001679 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001680 }
Magnus Karlsson46738f72019-08-14 09:27:21 +02001681 return;
1682 }
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001683 xsk->ring_stats.rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001684
Magnus Karlsson46738f72019-08-14 09:27:21 +02001685 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1686 while (ret != rcvd) {
1687 if (ret < 0)
1688 exit_with_error(-ret);
Björn Töpel284cbc62020-11-30 19:52:03 +01001689 complete_tx_l2fwd(xsk);
Björn Töpelb35fc142020-11-30 19:52:04 +01001690 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001691 xsk->app_stats.tx_wakeup_sendtos++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001692 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001693 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001694 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001695 }
1696
1697 for (i = 0; i < rcvd; i++) {
1698 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1699 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Ciara Loftus5a712e12019-09-13 10:39:48 +00001700 u64 orig = addr;
Kevin Laatz03895e62019-08-27 02:25:29 +00001701
1702 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001703 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1704
1705 swap_mac_addresses(pkt);
1706
1707 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001708 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = orig;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001709 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
1710 }
1711
1712 xsk_ring_prod__submit(&xsk->tx, rcvd);
1713 xsk_ring_cons__release(&xsk->rx, rcvd);
1714
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001715 xsk->ring_stats.tx_npkts += rcvd;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001716 xsk->outstanding_tx += rcvd;
1717}
1718
1719static void l2fwd_all(void)
1720{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001721 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001722 int i, ret;
1723
Magnus Karlsson46738f72019-08-14 09:27:21 +02001724 for (;;) {
1725 if (opt_poll) {
Björn Töpel284cbc62020-11-30 19:52:03 +01001726 for (i = 0; i < num_socks; i++) {
1727 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1728 fds[i].events = POLLOUT | POLLIN;
Ciara Loftus60dc6092020-10-02 13:36:11 +00001729 xsks[i]->app_stats.opt_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001730 }
Magnus Karlsson46738f72019-08-14 09:27:21 +02001731 ret = poll(fds, num_socks, opt_timeout);
1732 if (ret <= 0)
1733 continue;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001734 }
1735
Magnus Karlsson46738f72019-08-14 09:27:21 +02001736 for (i = 0; i < num_socks; i++)
Björn Töpel284cbc62020-11-30 19:52:03 +01001737 l2fwd(xsks[i]);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301738
1739 if (benchmark_done)
1740 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001741 }
1742}
1743
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001744static void load_xdp_program(char **argv, struct bpf_object **obj)
1745{
1746 struct bpf_prog_load_attr prog_load_attr = {
1747 .prog_type = BPF_PROG_TYPE_XDP,
1748 };
1749 char xdp_filename[256];
1750 int prog_fd;
1751
1752 snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
1753 prog_load_attr.file = xdp_filename;
1754
1755 if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
1756 exit(EXIT_FAILURE);
1757 if (prog_fd < 0) {
1758 fprintf(stderr, "ERROR: no program found: %s\n",
1759 strerror(prog_fd));
1760 exit(EXIT_FAILURE);
1761 }
1762
1763 if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
1764 fprintf(stderr, "ERROR: link set xdp fd failed\n");
1765 exit(EXIT_FAILURE);
1766 }
1767}
1768
1769static void enter_xsks_into_map(struct bpf_object *obj)
1770{
1771 struct bpf_map *map;
1772 int i, xsks_map;
1773
1774 map = bpf_object__find_map_by_name(obj, "xsks_map");
1775 xsks_map = bpf_map__fd(map);
1776 if (xsks_map < 0) {
1777 fprintf(stderr, "ERROR: no xsks map found: %s\n",
1778 strerror(xsks_map));
1779 exit(EXIT_FAILURE);
1780 }
1781
1782 for (i = 0; i < num_socks; i++) {
1783 int fd = xsk_socket__fd(xsks[i]->xsk);
1784 int key, ret;
1785
1786 key = i;
1787 ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
1788 if (ret) {
1789 fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
1790 exit(EXIT_FAILURE);
1791 }
1792 }
1793}
1794
Björn Töpelb35fc142020-11-30 19:52:04 +01001795static void apply_setsockopt(struct xsk_socket_info *xsk)
1796{
1797 int sock_opt;
1798
1799 if (!opt_busy_poll)
1800 return;
1801
1802 sock_opt = 1;
1803 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL,
1804 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1805 exit_with_error(errno);
1806
1807 sock_opt = 20;
1808 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL,
1809 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1810 exit_with_error(errno);
Björn Töpel41bf9002020-11-30 19:52:05 +01001811
1812 sock_opt = opt_batch_size;
1813 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1814 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1815 exit_with_error(errno);
Björn Töpelb35fc142020-11-30 19:52:04 +01001816}
1817
Mariusz Dudek3627d972020-12-03 10:05:46 +01001818static int recv_xsks_map_fd_from_ctrl_node(int sock, int *_fd)
1819{
1820 char cms[CMSG_SPACE(sizeof(int))];
1821 struct cmsghdr *cmsg;
1822 struct msghdr msg;
1823 struct iovec iov;
1824 int value;
1825 int len;
1826
1827 iov.iov_base = &value;
1828 iov.iov_len = sizeof(int);
1829
1830 msg.msg_name = 0;
1831 msg.msg_namelen = 0;
1832 msg.msg_iov = &iov;
1833 msg.msg_iovlen = 1;
1834 msg.msg_flags = 0;
1835 msg.msg_control = (caddr_t)cms;
1836 msg.msg_controllen = sizeof(cms);
1837
1838 len = recvmsg(sock, &msg, 0);
1839
1840 if (len < 0) {
1841 fprintf(stderr, "Recvmsg failed length incorrect.\n");
1842 return -EINVAL;
1843 }
1844
1845 if (len == 0) {
1846 fprintf(stderr, "Recvmsg failed no data\n");
1847 return -EINVAL;
1848 }
1849
1850 cmsg = CMSG_FIRSTHDR(&msg);
1851 *_fd = *(int *)CMSG_DATA(cmsg);
1852
1853 return 0;
1854}
1855
1856static int
1857recv_xsks_map_fd(int *xsks_map_fd)
1858{
1859 struct sockaddr_un server;
1860 int err;
1861
1862 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1863 if (sock < 0) {
1864 fprintf(stderr, "Error opening socket stream: %s", strerror(errno));
1865 return errno;
1866 }
1867
1868 server.sun_family = AF_UNIX;
1869 strcpy(server.sun_path, SOCKET_NAME);
1870
1871 if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) < 0) {
1872 close(sock);
1873 fprintf(stderr, "Error connecting stream socket: %s", strerror(errno));
1874 return errno;
1875 }
1876
1877 err = recv_xsks_map_fd_from_ctrl_node(sock, xsks_map_fd);
1878 if (err) {
Colin Ian King2faa7322020-12-03 11:44:52 +00001879 fprintf(stderr, "Error %d receiving fd\n", err);
Mariusz Dudek3627d972020-12-03 10:05:46 +01001880 return err;
1881 }
1882 return 0;
1883}
1884
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001885int main(int argc, char **argv)
1886{
Mariusz Dudek3627d972020-12-03 10:05:46 +01001887 struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 };
1888 struct __user_cap_data_struct data[2] = { { 0 } };
1889 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Magnus Karlsson661842c2019-11-07 18:47:39 +01001890 bool rx = false, tx = false;
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001891 struct sched_param schparam;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001892 struct xsk_umem_info *umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001893 struct bpf_object *obj;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001894 int xsks_map_fd = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001895 pthread_t pt;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001896 int i, ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001897 void *bufs;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001898
1899 parse_command_line(argc, argv);
1900
Mariusz Dudek3627d972020-12-03 10:05:46 +01001901 if (opt_reduced_cap) {
1902 if (capget(&hdr, data) < 0)
1903 fprintf(stderr, "Error getting capabilities\n");
1904
1905 data->effective &= CAP_TO_MASK(CAP_NET_RAW);
1906 data->permitted &= CAP_TO_MASK(CAP_NET_RAW);
1907
1908 if (capset(&hdr, data) < 0)
1909 fprintf(stderr, "Setting capabilities failed\n");
1910
1911 if (capget(&hdr, data) < 0) {
1912 fprintf(stderr, "Error getting capabilities\n");
1913 } else {
1914 fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
1915 data[0].effective, data[0].inheritable, data[0].permitted);
1916 fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
1917 data[1].effective, data[1].inheritable, data[1].permitted);
1918 }
1919 } else {
1920 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
1921 fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1922 strerror(errno));
1923 exit(EXIT_FAILURE);
1924 }
1925
1926 if (opt_num_xsks > 1)
1927 load_xdp_program(argv, &obj);
1928 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001929
Kevin Laatz3945b372019-08-27 02:25:30 +00001930 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1931 bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size,
1932 PROT_READ | PROT_WRITE,
1933 MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
1934 if (bufs == MAP_FAILED) {
1935 printf("ERROR: mmap failed\n");
1936 exit(EXIT_FAILURE);
1937 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001938
1939 /* Create sockets... */
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001940 umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size);
Magnus Karlsson661842c2019-11-07 18:47:39 +01001941 if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
1942 rx = true;
1943 xsk_populate_fill_ring(umem);
1944 }
1945 if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
1946 tx = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001947 for (i = 0; i < opt_num_xsks; i++)
Magnus Karlsson661842c2019-11-07 18:47:39 +01001948 xsks[num_socks++] = xsk_configure_socket(umem, rx, tx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001949
Björn Töpelb35fc142020-11-30 19:52:04 +01001950 for (i = 0; i < opt_num_xsks; i++)
1951 apply_setsockopt(xsks[i]);
1952
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301953 if (opt_bench == BENCH_TXONLY) {
Ong Boon Leongeb68db42021-12-30 11:54:47 +08001954 if (opt_tstamp && opt_pkt_size < PKTGEN_SIZE_MIN)
1955 opt_pkt_size = PKTGEN_SIZE_MIN;
1956
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301957 gen_eth_hdr_data();
1958
Magnus Karlsson661842c2019-11-07 18:47:39 +01001959 for (i = 0; i < NUM_FRAMES; i++)
1960 gen_eth_frame(umem, i * opt_xsk_frame_size);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301961 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001962
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001963 if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
1964 enter_xsks_into_map(obj);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001965
Mariusz Dudek3627d972020-12-03 10:05:46 +01001966 if (opt_reduced_cap) {
1967 ret = recv_xsks_map_fd(&xsks_map_fd);
1968 if (ret) {
1969 fprintf(stderr, "Error %d receiving xsks_map_fd\n", ret);
1970 exit_with_error(ret);
1971 }
1972 if (xsks[0]->xsk) {
1973 ret = xsk_socket__update_xskmap(xsks[0]->xsk, xsks_map_fd);
1974 if (ret) {
1975 fprintf(stderr, "Update of BPF map failed(%d)\n", ret);
1976 exit_with_error(ret);
1977 }
1978 }
1979 }
1980
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001981 signal(SIGINT, int_exit);
1982 signal(SIGTERM, int_exit);
1983 signal(SIGABRT, int_exit);
1984
1985 setlocale(LC_ALL, "");
1986
Magnus Karlsson74e00672020-09-10 10:31:06 +02001987 if (!opt_quiet) {
1988 ret = pthread_create(&pt, NULL, poller, NULL);
1989 if (ret)
1990 exit_with_error(ret);
1991 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001992
1993 prev_time = get_nsecs();
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301994 start_time = prev_time;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001995
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08001996 /* Configure sched priority for better wake-up accuracy */
1997 memset(&schparam, 0, sizeof(schparam));
1998 schparam.sched_priority = opt_schprio;
1999 ret = sched_setscheduler(0, opt_schpolicy, &schparam);
2000 if (ret) {
2001 fprintf(stderr, "Error(%d) in setting priority(%d): %s\n",
2002 errno, opt_schprio, strerror(errno));
2003 goto out;
2004 }
2005
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02002006 if (opt_bench == BENCH_RXDROP)
2007 rx_drop_all();
2008 else if (opt_bench == BENCH_TXONLY)
Magnus Karlsson46738f72019-08-14 09:27:21 +02002009 tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02002010 else
Magnus Karlsson46738f72019-08-14 09:27:21 +02002011 l2fwd_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02002012
Ong Boon Leongfa24d0b2021-12-30 11:54:45 +08002013out:
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05302014 benchmark_done = true;
2015
Magnus Karlsson74e00672020-09-10 10:31:06 +02002016 if (!opt_quiet)
2017 pthread_join(pt, NULL);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05302018
Jay Jayatheerthan69525582019-12-20 14:25:26 +05302019 xdpsock_cleanup();
2020
Maciej Fijalkowski6bc66992021-03-03 19:56:35 +01002021 munmap(bufs, NUM_FRAMES * opt_xsk_frame_size);
2022
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02002023 return 0;
2024}