blob: a2a42ec4b0e92b31de6c1f891ca9ddc7290a6381 [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>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020034
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010035#include <bpf/libbpf.h>
36#include <bpf/xsk.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070037#include <bpf/bpf.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010038#include "xdpsock.h"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020039
Andrii Nakryikoc58f9812021-12-01 15:28:23 -080040/* libbpf APIs for AF_XDP are deprecated starting from v0.7 */
41#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
42
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020043#ifndef SOL_XDP
44#define SOL_XDP 283
45#endif
46
47#ifndef AF_XDP
48#define AF_XDP 44
49#endif
50
51#ifndef PF_XDP
52#define PF_XDP AF_XDP
53#endif
54
Magnus Karlsson248c7f92019-02-21 10:21:27 +010055#define NUM_FRAMES (4 * 1024)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053056#define MIN_PKT_SIZE 64
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020057
58#define DEBUG_HEXDUMP 0
59
Ong Boon Leong2741a042021-12-30 11:54:41 +080060#define VLAN_PRIO_MASK 0xe000 /* Priority Code Point */
61#define VLAN_PRIO_SHIFT 13
62#define VLAN_VID_MASK 0x0fff /* VLAN Identifier */
63#define VLAN_VID__DEFAULT 1
64#define VLAN_PRI__DEFAULT 0
65
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +080066#define NSEC_PER_SEC 1000000000UL
67#define NSEC_PER_USEC 1000
68
Björn Töpela412ef52018-06-04 13:57:14 +020069typedef __u64 u64;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020070typedef __u32 u32;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053071typedef __u16 u16;
72typedef __u8 u8;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020073
74static unsigned long prev_time;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +080075static long tx_cycle_diff_min;
76static long tx_cycle_diff_max;
77static double tx_cycle_diff_ave;
78static long tx_cycle_cnt;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020079
80enum benchmark_type {
81 BENCH_RXDROP = 0,
82 BENCH_TXONLY = 1,
83 BENCH_L2FWD = 2,
84};
85
86static enum benchmark_type opt_bench = BENCH_RXDROP;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010087static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020088static const char *opt_if = "";
89static int opt_ifindex;
90static int opt_queue;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +053091static unsigned long opt_duration;
92static unsigned long start_time;
93static bool benchmark_done;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +053094static u32 opt_batch_size = 64;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +053095static int opt_pkt_count;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053096static u16 opt_pkt_size = MIN_PKT_SIZE;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +053097static u32 opt_pkt_fill_pattern = 0x12345678;
Ong Boon Leong2741a042021-12-30 11:54:41 +080098static bool opt_vlan_tag;
99static u16 opt_pkt_vlan_id = VLAN_VID__DEFAULT;
100static u16 opt_pkt_vlan_pri = VLAN_PRI__DEFAULT;
Ong Boon Leong6440a6c2021-12-30 11:54:42 +0800101static struct ether_addr opt_txdmac = {{ 0x3c, 0xfd, 0xfe,
102 0x9e, 0x7f, 0x71 }};
103static struct ether_addr opt_txsmac = {{ 0xec, 0xb1, 0xd7,
104 0x98, 0x3a, 0xc0 }};
Ciara Loftusb36c3202020-07-08 07:28:34 +0000105static bool opt_extra_stats;
Magnus Karlsson74e00672020-09-10 10:31:06 +0200106static bool opt_quiet;
Ciara Loftus60dc6092020-10-02 13:36:11 +0000107static bool opt_app_stats;
Ciara Loftus67ed3752020-10-02 13:36:12 +0000108static const char *opt_irq_str = "";
109static u32 irq_no;
110static int irqs_at_init = -1;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200111static int opt_poll;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200112static int opt_interval = 1;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200113static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
Kevin Laatzc543f542019-08-27 02:25:28 +0000114static u32 opt_umem_flags;
115static int opt_unaligned_chunks;
Kevin Laatz3945b372019-08-27 02:25:30 +0000116static int opt_mmap_flags;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300117static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200118static int opt_timeout = 1000;
119static bool opt_need_wakeup = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100120static u32 opt_num_xsks = 1;
Wang Hai2620e922021-06-28 17:18:15 +0800121static u32 prog_id;
Björn Töpelb35fc142020-11-30 19:52:04 +0100122static bool opt_busy_poll;
Mariusz Dudek3627d972020-12-03 10:05:46 +0100123static bool opt_reduced_cap;
Ong Boon Leong5a388252021-12-30 11:54:43 +0800124static clockid_t opt_clock = CLOCK_MONOTONIC;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +0800125static unsigned long opt_tx_cycle_ns;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200126
Ong Boon Leong2741a042021-12-30 11:54:41 +0800127struct vlan_ethhdr {
128 unsigned char h_dest[6];
129 unsigned char h_source[6];
130 __be16 h_vlan_proto;
131 __be16 h_vlan_TCI;
132 __be16 h_vlan_encapsulated_proto;
133};
134
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000135struct xsk_ring_stats {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200136 unsigned long rx_npkts;
137 unsigned long tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000138 unsigned long rx_dropped_npkts;
139 unsigned long rx_invalid_npkts;
140 unsigned long tx_invalid_npkts;
141 unsigned long rx_full_npkts;
142 unsigned long rx_fill_empty_npkts;
143 unsigned long tx_empty_npkts;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200144 unsigned long prev_rx_npkts;
145 unsigned long prev_tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000146 unsigned long prev_rx_dropped_npkts;
147 unsigned long prev_rx_invalid_npkts;
148 unsigned long prev_tx_invalid_npkts;
149 unsigned long prev_rx_full_npkts;
150 unsigned long prev_rx_fill_empty_npkts;
151 unsigned long prev_tx_empty_npkts;
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000152};
153
Ciara Loftus67ed3752020-10-02 13:36:12 +0000154struct xsk_driver_stats {
155 unsigned long intrs;
156 unsigned long prev_intrs;
157};
158
Ciara Loftus60dc6092020-10-02 13:36:11 +0000159struct xsk_app_stats {
160 unsigned long rx_empty_polls;
161 unsigned long fill_fail_polls;
162 unsigned long copy_tx_sendtos;
163 unsigned long tx_wakeup_sendtos;
164 unsigned long opt_polls;
165 unsigned long prev_rx_empty_polls;
166 unsigned long prev_fill_fail_polls;
167 unsigned long prev_copy_tx_sendtos;
168 unsigned long prev_tx_wakeup_sendtos;
169 unsigned long prev_opt_polls;
170};
171
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000172struct xsk_umem_info {
173 struct xsk_ring_prod fq;
174 struct xsk_ring_cons cq;
175 struct xsk_umem *umem;
176 void *buffer;
177};
178
179struct xsk_socket_info {
180 struct xsk_ring_cons rx;
181 struct xsk_ring_prod tx;
182 struct xsk_umem_info *umem;
183 struct xsk_socket *xsk;
184 struct xsk_ring_stats ring_stats;
Ciara Loftus60dc6092020-10-02 13:36:11 +0000185 struct xsk_app_stats app_stats;
Ciara Loftus67ed3752020-10-02 13:36:12 +0000186 struct xsk_driver_stats drv_stats;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100187 u32 outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200188};
189
Ong Boon Leong5a388252021-12-30 11:54:43 +0800190static const struct clockid_map {
191 const char *name;
192 clockid_t clockid;
193} clockids_map[] = {
194 { "REALTIME", CLOCK_REALTIME },
195 { "TAI", CLOCK_TAI },
196 { "BOOTTIME", CLOCK_BOOTTIME },
197 { "MONOTONIC", CLOCK_MONOTONIC },
198 { NULL }
199};
200
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200201static int num_socks;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100202struct xsk_socket_info *xsks[MAX_SOCKS];
Mariusz Dudek3627d972020-12-03 10:05:46 +0100203int sock;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200204
Ong Boon Leong5a388252021-12-30 11:54:43 +0800205static int get_clockid(clockid_t *id, const char *name)
206{
207 const struct clockid_map *clk;
208
209 for (clk = clockids_map; clk->name; clk++) {
210 if (strcasecmp(clk->name, name) == 0) {
211 *id = clk->clockid;
212 return 0;
213 }
214 }
215
216 return -1;
217}
218
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200219static unsigned long get_nsecs(void)
220{
221 struct timespec ts;
222
Ong Boon Leong5a388252021-12-30 11:54:43 +0800223 clock_gettime(opt_clock, &ts);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200224 return ts.tv_sec * 1000000000UL + ts.tv_nsec;
225}
226
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200227static void print_benchmark(bool running)
228{
229 const char *bench_str = "INVALID";
230
231 if (opt_bench == BENCH_RXDROP)
232 bench_str = "rxdrop";
233 else if (opt_bench == BENCH_TXONLY)
234 bench_str = "txonly";
235 else if (opt_bench == BENCH_L2FWD)
236 bench_str = "l2fwd";
237
238 printf("%s:%d %s ", opt_if, opt_queue, bench_str);
239 if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
240 printf("xdp-skb ");
241 else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
242 printf("xdp-drv ");
243 else
244 printf(" ");
245
246 if (opt_poll)
247 printf("poll() ");
248
249 if (running) {
250 printf("running...");
251 fflush(stdout);
252 }
253}
254
Ciara Loftusb36c3202020-07-08 07:28:34 +0000255static int xsk_get_xdp_stats(int fd, struct xsk_socket_info *xsk)
256{
257 struct xdp_statistics stats;
258 socklen_t optlen;
259 int err;
260
261 optlen = sizeof(stats);
262 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
263 if (err)
264 return err;
265
266 if (optlen == sizeof(struct xdp_statistics)) {
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000267 xsk->ring_stats.rx_dropped_npkts = stats.rx_dropped;
268 xsk->ring_stats.rx_invalid_npkts = stats.rx_invalid_descs;
269 xsk->ring_stats.tx_invalid_npkts = stats.tx_invalid_descs;
270 xsk->ring_stats.rx_full_npkts = stats.rx_ring_full;
271 xsk->ring_stats.rx_fill_empty_npkts = stats.rx_fill_ring_empty_descs;
272 xsk->ring_stats.tx_empty_npkts = stats.tx_ring_empty_descs;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000273 return 0;
274 }
275
276 return -EINVAL;
277}
278
Ciara Loftus60dc6092020-10-02 13:36:11 +0000279static void dump_app_stats(long dt)
280{
281 int i;
282
283 for (i = 0; i < num_socks && xsks[i]; i++) {
284 char *fmt = "%-18s %'-14.0f %'-14lu\n";
285 double rx_empty_polls_ps, fill_fail_polls_ps, copy_tx_sendtos_ps,
286 tx_wakeup_sendtos_ps, opt_polls_ps;
287
288 rx_empty_polls_ps = (xsks[i]->app_stats.rx_empty_polls -
289 xsks[i]->app_stats.prev_rx_empty_polls) * 1000000000. / dt;
290 fill_fail_polls_ps = (xsks[i]->app_stats.fill_fail_polls -
291 xsks[i]->app_stats.prev_fill_fail_polls) * 1000000000. / dt;
292 copy_tx_sendtos_ps = (xsks[i]->app_stats.copy_tx_sendtos -
293 xsks[i]->app_stats.prev_copy_tx_sendtos) * 1000000000. / dt;
294 tx_wakeup_sendtos_ps = (xsks[i]->app_stats.tx_wakeup_sendtos -
295 xsks[i]->app_stats.prev_tx_wakeup_sendtos)
296 * 1000000000. / dt;
297 opt_polls_ps = (xsks[i]->app_stats.opt_polls -
298 xsks[i]->app_stats.prev_opt_polls) * 1000000000. / dt;
299
300 printf("\n%-18s %-14s %-14s\n", "", "calls/s", "count");
301 printf(fmt, "rx empty polls", rx_empty_polls_ps, xsks[i]->app_stats.rx_empty_polls);
302 printf(fmt, "fill fail polls", fill_fail_polls_ps,
303 xsks[i]->app_stats.fill_fail_polls);
304 printf(fmt, "copy tx sendtos", copy_tx_sendtos_ps,
305 xsks[i]->app_stats.copy_tx_sendtos);
306 printf(fmt, "tx wakeup sendtos", tx_wakeup_sendtos_ps,
307 xsks[i]->app_stats.tx_wakeup_sendtos);
308 printf(fmt, "opt polls", opt_polls_ps, xsks[i]->app_stats.opt_polls);
309
310 xsks[i]->app_stats.prev_rx_empty_polls = xsks[i]->app_stats.rx_empty_polls;
311 xsks[i]->app_stats.prev_fill_fail_polls = xsks[i]->app_stats.fill_fail_polls;
312 xsks[i]->app_stats.prev_copy_tx_sendtos = xsks[i]->app_stats.copy_tx_sendtos;
313 xsks[i]->app_stats.prev_tx_wakeup_sendtos = xsks[i]->app_stats.tx_wakeup_sendtos;
314 xsks[i]->app_stats.prev_opt_polls = xsks[i]->app_stats.opt_polls;
315 }
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +0800316
317 if (opt_tx_cycle_ns) {
318 printf("\n%-18s %-10s %-10s %-10s %-10s %-10s\n",
319 "", "period", "min", "ave", "max", "cycle");
320 printf("%-18s %-10lu %-10lu %-10lu %-10lu %-10lu\n",
321 "Cyclic TX", opt_tx_cycle_ns, tx_cycle_diff_min,
322 (long)(tx_cycle_diff_ave / tx_cycle_cnt),
323 tx_cycle_diff_max, tx_cycle_cnt);
324 }
Ciara Loftus60dc6092020-10-02 13:36:11 +0000325}
326
Ciara Loftus67ed3752020-10-02 13:36:12 +0000327static bool get_interrupt_number(void)
328{
329 FILE *f_int_proc;
330 char line[4096];
331 bool found = false;
332
333 f_int_proc = fopen("/proc/interrupts", "r");
334 if (f_int_proc == NULL) {
335 printf("Failed to open /proc/interrupts.\n");
336 return found;
337 }
338
339 while (!feof(f_int_proc) && !found) {
340 /* Make sure to read a full line at a time */
341 if (fgets(line, sizeof(line), f_int_proc) == NULL ||
342 line[strlen(line) - 1] != '\n') {
343 printf("Error reading from interrupts file\n");
344 break;
345 }
346
347 /* Extract interrupt number from line */
348 if (strstr(line, opt_irq_str) != NULL) {
349 irq_no = atoi(line);
350 found = true;
351 break;
352 }
353 }
354
355 fclose(f_int_proc);
356
357 return found;
358}
359
360static int get_irqs(void)
361{
362 char count_path[PATH_MAX];
363 int total_intrs = -1;
364 FILE *f_count_proc;
365 char line[4096];
366
367 snprintf(count_path, sizeof(count_path),
368 "/sys/kernel/irq/%i/per_cpu_count", irq_no);
369 f_count_proc = fopen(count_path, "r");
370 if (f_count_proc == NULL) {
371 printf("Failed to open %s\n", count_path);
372 return total_intrs;
373 }
374
375 if (fgets(line, sizeof(line), f_count_proc) == NULL ||
376 line[strlen(line) - 1] != '\n') {
377 printf("Error reading from %s\n", count_path);
378 } else {
379 static const char com[2] = ",";
380 char *token;
381
382 total_intrs = 0;
383 token = strtok(line, com);
384 while (token != NULL) {
385 /* sum up interrupts across all cores */
386 total_intrs += atoi(token);
387 token = strtok(NULL, com);
388 }
389 }
390
391 fclose(f_count_proc);
392
393 return total_intrs;
394}
395
396static void dump_driver_stats(long dt)
397{
398 int i;
399
400 for (i = 0; i < num_socks && xsks[i]; i++) {
401 char *fmt = "%-18s %'-14.0f %'-14lu\n";
402 double intrs_ps;
403 int n_ints = get_irqs();
404
405 if (n_ints < 0) {
406 printf("error getting intr info for intr %i\n", irq_no);
407 return;
408 }
409 xsks[i]->drv_stats.intrs = n_ints - irqs_at_init;
410
411 intrs_ps = (xsks[i]->drv_stats.intrs - xsks[i]->drv_stats.prev_intrs) *
412 1000000000. / dt;
413
414 printf("\n%-18s %-14s %-14s\n", "", "intrs/s", "count");
415 printf(fmt, "irqs", intrs_ps, xsks[i]->drv_stats.intrs);
416
417 xsks[i]->drv_stats.prev_intrs = xsks[i]->drv_stats.intrs;
418 }
419}
420
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200421static void dump_stats(void)
422{
423 unsigned long now = get_nsecs();
424 long dt = now - prev_time;
425 int i;
426
427 prev_time = now;
428
Prashant Bhole11c3f512018-08-31 10:00:49 +0900429 for (i = 0; i < num_socks && xsks[i]; i++) {
Ciara Loftus60dc6092020-10-02 13:36:11 +0000430 char *fmt = "%-18s %'-14.0f %'-14lu\n";
Ciara Loftusb36c3202020-07-08 07:28:34 +0000431 double rx_pps, tx_pps, dropped_pps, rx_invalid_pps, full_pps, fill_empty_pps,
432 tx_invalid_pps, tx_empty_pps;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200433
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000434 rx_pps = (xsks[i]->ring_stats.rx_npkts - xsks[i]->ring_stats.prev_rx_npkts) *
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200435 1000000000. / dt;
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000436 tx_pps = (xsks[i]->ring_stats.tx_npkts - xsks[i]->ring_stats.prev_tx_npkts) *
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200437 1000000000. / dt;
438
439 printf("\n sock%d@", i);
440 print_benchmark(false);
441 printf("\n");
442
Ciara Loftus60dc6092020-10-02 13:36:11 +0000443 printf("%-18s %-14s %-14s %-14.2f\n", "", "pps", "pkts",
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200444 dt / 1000000000.);
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000445 printf(fmt, "rx", rx_pps, xsks[i]->ring_stats.rx_npkts);
446 printf(fmt, "tx", tx_pps, xsks[i]->ring_stats.tx_npkts);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200447
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000448 xsks[i]->ring_stats.prev_rx_npkts = xsks[i]->ring_stats.rx_npkts;
449 xsks[i]->ring_stats.prev_tx_npkts = xsks[i]->ring_stats.tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000450
451 if (opt_extra_stats) {
452 if (!xsk_get_xdp_stats(xsk_socket__fd(xsks[i]->xsk), xsks[i])) {
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000453 dropped_pps = (xsks[i]->ring_stats.rx_dropped_npkts -
454 xsks[i]->ring_stats.prev_rx_dropped_npkts) *
455 1000000000. / dt;
456 rx_invalid_pps = (xsks[i]->ring_stats.rx_invalid_npkts -
457 xsks[i]->ring_stats.prev_rx_invalid_npkts) *
458 1000000000. / dt;
459 tx_invalid_pps = (xsks[i]->ring_stats.tx_invalid_npkts -
460 xsks[i]->ring_stats.prev_tx_invalid_npkts) *
461 1000000000. / dt;
462 full_pps = (xsks[i]->ring_stats.rx_full_npkts -
463 xsks[i]->ring_stats.prev_rx_full_npkts) *
464 1000000000. / dt;
465 fill_empty_pps = (xsks[i]->ring_stats.rx_fill_empty_npkts -
466 xsks[i]->ring_stats.prev_rx_fill_empty_npkts) *
467 1000000000. / dt;
468 tx_empty_pps = (xsks[i]->ring_stats.tx_empty_npkts -
469 xsks[i]->ring_stats.prev_tx_empty_npkts) *
470 1000000000. / dt;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000471
472 printf(fmt, "rx dropped", dropped_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000473 xsks[i]->ring_stats.rx_dropped_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000474 printf(fmt, "rx invalid", rx_invalid_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000475 xsks[i]->ring_stats.rx_invalid_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000476 printf(fmt, "tx invalid", tx_invalid_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000477 xsks[i]->ring_stats.tx_invalid_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000478 printf(fmt, "rx queue full", full_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000479 xsks[i]->ring_stats.rx_full_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000480 printf(fmt, "fill ring empty", fill_empty_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000481 xsks[i]->ring_stats.rx_fill_empty_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000482 printf(fmt, "tx ring empty", tx_empty_pps,
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000483 xsks[i]->ring_stats.tx_empty_npkts);
Ciara Loftusb36c3202020-07-08 07:28:34 +0000484
Ciara Loftus2e8806f2020-10-02 13:36:10 +0000485 xsks[i]->ring_stats.prev_rx_dropped_npkts =
486 xsks[i]->ring_stats.rx_dropped_npkts;
487 xsks[i]->ring_stats.prev_rx_invalid_npkts =
488 xsks[i]->ring_stats.rx_invalid_npkts;
489 xsks[i]->ring_stats.prev_tx_invalid_npkts =
490 xsks[i]->ring_stats.tx_invalid_npkts;
491 xsks[i]->ring_stats.prev_rx_full_npkts =
492 xsks[i]->ring_stats.rx_full_npkts;
493 xsks[i]->ring_stats.prev_rx_fill_empty_npkts =
494 xsks[i]->ring_stats.rx_fill_empty_npkts;
495 xsks[i]->ring_stats.prev_tx_empty_npkts =
496 xsks[i]->ring_stats.tx_empty_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000497 } else {
498 printf("%-15s\n", "Error retrieving extra stats");
499 }
500 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200501 }
Ciara Loftus60dc6092020-10-02 13:36:11 +0000502
503 if (opt_app_stats)
504 dump_app_stats(dt);
Ciara Loftus67ed3752020-10-02 13:36:12 +0000505 if (irq_no)
506 dump_driver_stats(dt);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200507}
508
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530509static bool is_benchmark_done(void)
510{
511 if (opt_duration > 0) {
512 unsigned long dt = (get_nsecs() - start_time);
513
514 if (dt >= opt_duration)
515 benchmark_done = true;
516 }
517 return benchmark_done;
518}
519
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200520static void *poller(void *arg)
521{
522 (void)arg;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530523 while (!is_benchmark_done()) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200524 sleep(opt_interval);
525 dump_stats();
526 }
527
528 return NULL;
529}
530
Wang Hai2620e922021-06-28 17:18:15 +0800531static void remove_xdp_program(void)
532{
533 u32 curr_prog_id = 0;
534
535 if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
536 printf("bpf_get_link_xdp_id failed\n");
537 exit(EXIT_FAILURE);
538 }
539
540 if (prog_id == curr_prog_id)
541 bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
542 else if (!curr_prog_id)
543 printf("couldn't find a prog id on a given interface\n");
544 else
545 printf("program on interface changed, not removing\n");
546}
547
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100548static void int_exit(int sig)
549{
Jay Jayatheerthan69525582019-12-20 14:25:26 +0530550 benchmark_done = true;
551}
552
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100553static void __exit_with_error(int error, const char *file, const char *func,
554 int line)
555{
556 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
557 line, error, strerror(error));
Wang Hai2620e922021-06-28 17:18:15 +0800558
559 if (opt_num_xsks > 1)
560 remove_xdp_program();
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100561 exit(EXIT_FAILURE);
562}
563
Maciej Fijalkowskic9d27c92021-03-30 00:43:06 +0200564#define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
565
566static void xdpsock_cleanup(void)
567{
568 struct xsk_umem *umem = xsks[0]->umem->umem;
569 int i, cmd = CLOSE_CONN;
570
571 dump_stats();
572 for (i = 0; i < num_socks; i++)
573 xsk_socket__delete(xsks[i]->xsk);
574 (void)xsk_umem__delete(umem);
575
576 if (opt_reduced_cap) {
577 if (write(sock, &cmd, sizeof(int)) < 0)
578 exit_with_error(errno);
579 }
Wang Hai2620e922021-06-28 17:18:15 +0800580
581 if (opt_num_xsks > 1)
582 remove_xdp_program();
Maciej Fijalkowskic9d27c92021-03-30 00:43:06 +0200583}
584
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100585static void swap_mac_addresses(void *data)
586{
587 struct ether_header *eth = (struct ether_header *)data;
588 struct ether_addr *src_addr = (struct ether_addr *)&eth->ether_shost;
589 struct ether_addr *dst_addr = (struct ether_addr *)&eth->ether_dhost;
590 struct ether_addr tmp;
591
592 tmp = *src_addr;
593 *src_addr = *dst_addr;
594 *dst_addr = tmp;
595}
596
597static void hex_dump(void *pkt, size_t length, u64 addr)
598{
599 const unsigned char *address = (unsigned char *)pkt;
600 const unsigned char *line = address;
601 size_t line_size = 32;
602 unsigned char c;
603 char buf[32];
604 int i = 0;
605
606 if (!DEBUG_HEXDUMP)
607 return;
608
609 sprintf(buf, "addr=%llu", addr);
610 printf("length = %zu\n", length);
611 printf("%s | ", buf);
612 while (length-- > 0) {
613 printf("%02X ", *address++);
614 if (!(++i % line_size) || (length == 0 && i % line_size)) {
615 if (length == 0) {
616 while (i++ % line_size)
617 printf("__ ");
618 }
619 printf(" | "); /* right close */
620 while (line < address) {
621 c = *line++;
622 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
623 }
624 printf("\n");
625 if (length > 0)
626 printf("%s | ", buf);
627 }
628 }
629 printf("\n");
630}
631
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530632static void *memset32_htonl(void *dest, u32 val, u32 size)
633{
634 u32 *ptr = (u32 *)dest;
635 int i;
636
637 val = htonl(val);
638
639 for (i = 0; i < (size & (~0x3)); i += 4)
640 ptr[i >> 2] = val;
641
642 for (; i < size; i++)
643 ((char *)dest)[i] = ((char *)&val)[i & 3];
644
645 return dest;
646}
647
648/*
649 * This function code has been taken from
650 * Linux kernel lib/checksum.c
651 */
652static inline unsigned short from32to16(unsigned int x)
653{
654 /* add up 16-bit and 16-bit for 16+c bit */
655 x = (x & 0xffff) + (x >> 16);
656 /* add up carry.. */
657 x = (x & 0xffff) + (x >> 16);
658 return x;
659}
660
661/*
662 * This function code has been taken from
663 * Linux kernel lib/checksum.c
664 */
665static unsigned int do_csum(const unsigned char *buff, int len)
666{
667 unsigned int result = 0;
668 int odd;
669
670 if (len <= 0)
671 goto out;
672 odd = 1 & (unsigned long)buff;
673 if (odd) {
674#ifdef __LITTLE_ENDIAN
675 result += (*buff << 8);
676#else
677 result = *buff;
678#endif
679 len--;
680 buff++;
681 }
682 if (len >= 2) {
683 if (2 & (unsigned long)buff) {
684 result += *(unsigned short *)buff;
685 len -= 2;
686 buff += 2;
687 }
688 if (len >= 4) {
689 const unsigned char *end = buff +
690 ((unsigned int)len & ~3);
691 unsigned int carry = 0;
692
693 do {
694 unsigned int w = *(unsigned int *)buff;
695
696 buff += 4;
697 result += carry;
698 result += w;
699 carry = (w > result);
700 } while (buff < end);
701 result += carry;
702 result = (result & 0xffff) + (result >> 16);
703 }
704 if (len & 2) {
705 result += *(unsigned short *)buff;
706 buff += 2;
707 }
708 }
709 if (len & 1)
710#ifdef __LITTLE_ENDIAN
711 result += *buff;
712#else
713 result += (*buff << 8);
714#endif
715 result = from32to16(result);
716 if (odd)
717 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
718out:
719 return result;
720}
721
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530722/*
723 * This is a version of ip_compute_csum() optimized for IP headers,
724 * which always checksum on 4 octet boundaries.
725 * This function code has been taken from
726 * Linux kernel lib/checksum.c
727 */
Niklas Söderlundf4700a62021-08-06 14:28:55 +0200728static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530729{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200730 return (__sum16)~do_csum(iph, ihl * 4);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530731}
732
733/*
734 * Fold a partial checksum
735 * This function code has been taken from
736 * Linux kernel include/asm-generic/checksum.h
737 */
738static inline __sum16 csum_fold(__wsum csum)
739{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200740 u32 sum = (u32)csum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530741
742 sum = (sum & 0xffff) + (sum >> 16);
743 sum = (sum & 0xffff) + (sum >> 16);
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200744 return (__sum16)~sum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530745}
746
747/*
748 * This function code has been taken from
749 * Linux kernel lib/checksum.c
750 */
751static inline u32 from64to32(u64 x)
752{
753 /* add up 32-bit and 32-bit for 32+c bit */
754 x = (x & 0xffffffff) + (x >> 32);
755 /* add up carry.. */
756 x = (x & 0xffffffff) + (x >> 32);
757 return (u32)x;
758}
759
760__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
761 __u32 len, __u8 proto, __wsum sum);
762
763/*
764 * This function code has been taken from
765 * Linux kernel lib/checksum.c
766 */
767__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
768 __u32 len, __u8 proto, __wsum sum)
769{
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200770 unsigned long long s = (u32)sum;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530771
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200772 s += (u32)saddr;
773 s += (u32)daddr;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530774#ifdef __BIG_ENDIAN__
775 s += proto + len;
776#else
777 s += (proto + len) << 8;
778#endif
Niklas Söderlund29f24c42021-08-06 14:28:54 +0200779 return (__wsum)from64to32(s);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530780}
781
782/*
783 * This function has been taken from
784 * Linux kernel include/asm-generic/checksum.h
785 */
786static inline __sum16
787csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
788 __u8 proto, __wsum sum)
789{
790 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
791}
792
793static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
794 u8 proto, u16 *udp_pkt)
795{
796 u32 csum = 0;
797 u32 cnt = 0;
798
799 /* udp hdr and data */
800 for (; cnt < len; cnt += 2)
801 csum += udp_pkt[cnt >> 1];
802
803 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
804}
805
806#define ETH_FCS_SIZE 4
807
Ong Boon Leong2741a042021-12-30 11:54:41 +0800808#define ETH_HDR_SIZE (opt_vlan_tag ? sizeof(struct vlan_ethhdr) : \
809 sizeof(struct ethhdr))
810#define PKT_HDR_SIZE (ETH_HDR_SIZE + sizeof(struct iphdr) + \
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530811 sizeof(struct udphdr))
812
813#define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
Ong Boon Leong2741a042021-12-30 11:54:41 +0800814#define IP_PKT_SIZE (PKT_SIZE - ETH_HDR_SIZE)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530815#define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
816#define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
817
818static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
819
820static void gen_eth_hdr_data(void)
821{
Ong Boon Leong2741a042021-12-30 11:54:41 +0800822 struct udphdr *udp_hdr;
823 struct iphdr *ip_hdr;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530824
Ong Boon Leong2741a042021-12-30 11:54:41 +0800825 if (opt_vlan_tag) {
826 struct vlan_ethhdr *veth_hdr = (struct vlan_ethhdr *)pkt_data;
827 u16 vlan_tci = 0;
828
829 udp_hdr = (struct udphdr *)(pkt_data +
830 sizeof(struct vlan_ethhdr) +
831 sizeof(struct iphdr));
832 ip_hdr = (struct iphdr *)(pkt_data +
833 sizeof(struct vlan_ethhdr));
834
835 /* ethernet & VLAN header */
Ong Boon Leong6440a6c2021-12-30 11:54:42 +0800836 memcpy(veth_hdr->h_dest, &opt_txdmac, ETH_ALEN);
837 memcpy(veth_hdr->h_source, &opt_txsmac, ETH_ALEN);
Ong Boon Leong2741a042021-12-30 11:54:41 +0800838 veth_hdr->h_vlan_proto = htons(ETH_P_8021Q);
839 vlan_tci = opt_pkt_vlan_id & VLAN_VID_MASK;
840 vlan_tci |= (opt_pkt_vlan_pri << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
841 veth_hdr->h_vlan_TCI = htons(vlan_tci);
842 veth_hdr->h_vlan_encapsulated_proto = htons(ETH_P_IP);
843 } else {
844 struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
845
846 udp_hdr = (struct udphdr *)(pkt_data +
847 sizeof(struct ethhdr) +
848 sizeof(struct iphdr));
849 ip_hdr = (struct iphdr *)(pkt_data +
850 sizeof(struct ethhdr));
851
852 /* ethernet header */
Ong Boon Leong6440a6c2021-12-30 11:54:42 +0800853 memcpy(eth_hdr->h_dest, &opt_txdmac, ETH_ALEN);
854 memcpy(eth_hdr->h_source, &opt_txsmac, ETH_ALEN);
Ong Boon Leong2741a042021-12-30 11:54:41 +0800855 eth_hdr->h_proto = htons(ETH_P_IP);
856 }
857
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530858
859 /* IP header */
860 ip_hdr->version = IPVERSION;
861 ip_hdr->ihl = 0x5; /* 20 byte header */
862 ip_hdr->tos = 0x0;
863 ip_hdr->tot_len = htons(IP_PKT_SIZE);
864 ip_hdr->id = 0;
865 ip_hdr->frag_off = 0;
866 ip_hdr->ttl = IPDEFTTL;
867 ip_hdr->protocol = IPPROTO_UDP;
868 ip_hdr->saddr = htonl(0x0a0a0a10);
869 ip_hdr->daddr = htonl(0x0a0a0a20);
870
871 /* IP header checksum */
872 ip_hdr->check = 0;
873 ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
874
875 /* UDP header */
876 udp_hdr->source = htons(0x1000);
877 udp_hdr->dest = htons(0x1000);
878 udp_hdr->len = htons(UDP_PKT_SIZE);
879
880 /* UDP data */
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530881 memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530882 UDP_PKT_DATA_SIZE);
883
884 /* UDP header checksum */
885 udp_hdr->check = 0;
886 udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
887 IPPROTO_UDP, (u16 *)udp_hdr);
888}
889
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530890static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100891{
892 memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530893 PKT_SIZE);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100894}
895
896static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size)
897{
898 struct xsk_umem_info *umem;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300899 struct xsk_umem_config cfg = {
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200900 /* We recommend that you set the fill ring size >= HW RX ring size +
901 * AF_XDP RX ring size. Make sure you fill up the fill ring
902 * with buffers at regular intervals, and you will with this setting
903 * avoid allocation failures in the driver. These are usually quite
904 * expensive since drivers have not been written to assume that
905 * allocation failures are common. For regular sockets, kernel
906 * allocated memory is used that only runs out in OOM situations
907 * that should be rare.
908 */
909 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300910 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
911 .frame_size = opt_xsk_frame_size,
912 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
Kevin Laatzc543f542019-08-27 02:25:28 +0000913 .flags = opt_umem_flags
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300914 };
Magnus Karlsson661842c2019-11-07 18:47:39 +0100915 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100916
917 umem = calloc(1, sizeof(*umem));
918 if (!umem)
919 exit_with_error(errno);
920
921 ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300922 &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100923 if (ret)
924 exit_with_error(-ret);
925
Magnus Karlsson661842c2019-11-07 18:47:39 +0100926 umem->buffer = buffer;
927 return umem;
928}
929
930static void xsk_populate_fill_ring(struct xsk_umem_info *umem)
931{
932 int ret, i;
933 u32 idx;
934
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100935 ret = xsk_ring_prod__reserve(&umem->fq,
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200936 XSK_RING_PROD__DEFAULT_NUM_DESCS * 2, &idx);
937 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS * 2)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100938 exit_with_error(-ret);
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200939 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS * 2; i++)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100940 *xsk_ring_prod__fill_addr(&umem->fq, idx++) =
941 i * opt_xsk_frame_size;
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200942 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS * 2);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100943}
944
Magnus Karlsson661842c2019-11-07 18:47:39 +0100945static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
946 bool rx, bool tx)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100947{
948 struct xsk_socket_config cfg;
949 struct xsk_socket_info *xsk;
Magnus Karlsson661842c2019-11-07 18:47:39 +0100950 struct xsk_ring_cons *rxr;
951 struct xsk_ring_prod *txr;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100952 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100953
954 xsk = calloc(1, sizeof(*xsk));
955 if (!xsk)
956 exit_with_error(errno);
957
958 xsk->umem = umem;
959 cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
960 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
Mariusz Dudek3627d972020-12-03 10:05:46 +0100961 if (opt_num_xsks > 1 || opt_reduced_cap)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100962 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
963 else
964 cfg.libbpf_flags = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100965 cfg.xdp_flags = opt_xdp_flags;
966 cfg.bind_flags = opt_xdp_bind_flags;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100967
Magnus Karlsson661842c2019-11-07 18:47:39 +0100968 rxr = rx ? &xsk->rx : NULL;
969 txr = tx ? &xsk->tx : NULL;
970 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem,
971 rxr, txr, &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100972 if (ret)
973 exit_with_error(-ret);
974
Wang Hai2620e922021-06-28 17:18:15 +0800975 ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
976 if (ret)
977 exit_with_error(-ret);
978
Ciara Loftus60dc6092020-10-02 13:36:11 +0000979 xsk->app_stats.rx_empty_polls = 0;
980 xsk->app_stats.fill_fail_polls = 0;
981 xsk->app_stats.copy_tx_sendtos = 0;
982 xsk->app_stats.tx_wakeup_sendtos = 0;
983 xsk->app_stats.opt_polls = 0;
984 xsk->app_stats.prev_rx_empty_polls = 0;
985 xsk->app_stats.prev_fill_fail_polls = 0;
986 xsk->app_stats.prev_copy_tx_sendtos = 0;
987 xsk->app_stats.prev_tx_wakeup_sendtos = 0;
988 xsk->app_stats.prev_opt_polls = 0;
989
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100990 return xsk;
991}
992
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200993static struct option long_options[] = {
994 {"rxdrop", no_argument, 0, 'r'},
995 {"txonly", no_argument, 0, 't'},
996 {"l2fwd", no_argument, 0, 'l'},
997 {"interface", required_argument, 0, 'i'},
998 {"queue", required_argument, 0, 'q'},
999 {"poll", no_argument, 0, 'p'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001000 {"xdp-skb", no_argument, 0, 'S'},
1001 {"xdp-native", no_argument, 0, 'N'},
1002 {"interval", required_argument, 0, 'n'},
Björn Töpel58c50ae2018-08-28 14:44:35 +02001003 {"zero-copy", no_argument, 0, 'z'},
1004 {"copy", no_argument, 0, 'c'},
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001005 {"frame-size", required_argument, 0, 'f'},
Magnus Karlsson46738f72019-08-14 09:27:21 +02001006 {"no-need-wakeup", no_argument, 0, 'm'},
Kevin Laatzc543f542019-08-27 02:25:28 +00001007 {"unaligned", no_argument, 0, 'u'},
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001008 {"shared-umem", no_argument, 0, 'M'},
Andre Guedesb3133322019-11-14 08:28:47 -08001009 {"force", no_argument, 0, 'F'},
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301010 {"duration", required_argument, 0, 'd'},
Ong Boon Leong5a388252021-12-30 11:54:43 +08001011 {"clock", required_argument, 0, 'w'},
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301012 {"batch-size", required_argument, 0, 'b'},
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301013 {"tx-pkt-count", required_argument, 0, 'C'},
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301014 {"tx-pkt-size", required_argument, 0, 's'},
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301015 {"tx-pkt-pattern", required_argument, 0, 'P'},
Ong Boon Leong2741a042021-12-30 11:54:41 +08001016 {"tx-vlan", no_argument, 0, 'V'},
1017 {"tx-vlan-id", required_argument, 0, 'J'},
1018 {"tx-vlan-pri", required_argument, 0, 'K'},
Ong Boon Leong6440a6c2021-12-30 11:54:42 +08001019 {"tx-dmac", required_argument, 0, 'G'},
1020 {"tx-smac", required_argument, 0, 'H'},
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001021 {"tx-cycle", required_argument, 0, 'T'},
Ciara Loftusb36c3202020-07-08 07:28:34 +00001022 {"extra-stats", no_argument, 0, 'x'},
Magnus Karlsson74e00672020-09-10 10:31:06 +02001023 {"quiet", no_argument, 0, 'Q'},
Ciara Loftus60dc6092020-10-02 13:36:11 +00001024 {"app-stats", no_argument, 0, 'a'},
Ciara Loftus67ed3752020-10-02 13:36:12 +00001025 {"irq-string", no_argument, 0, 'I'},
Björn Töpelb35fc142020-11-30 19:52:04 +01001026 {"busy-poll", no_argument, 0, 'B'},
Mariusz Dudek3627d972020-12-03 10:05:46 +01001027 {"reduce-cap", no_argument, 0, 'R'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001028 {0, 0, 0, 0}
1029};
1030
1031static void usage(const char *prog)
1032{
1033 const char *str =
1034 " Usage: %s [OPTIONS]\n"
1035 " Options:\n"
1036 " -r, --rxdrop Discard all incoming packets (default)\n"
1037 " -t, --txonly Only send packets\n"
1038 " -l, --l2fwd MAC swap L2 forwarding\n"
1039 " -i, --interface=n Run on interface n\n"
1040 " -q, --queue=n Use queue n (default 0)\n"
1041 " -p, --poll Use poll syscall\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001042 " -S, --xdp-skb=n Use XDP skb-mod\n"
Anton Ivanov4564a8bb2019-10-07 09:26:36 +01001043 " -N, --xdp-native=n Enforce XDP native mode\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001044 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
Björn Töpel58c50ae2018-08-28 14:44:35 +02001045 " -z, --zero-copy Force zero-copy mode.\n"
1046 " -c, --copy Force copy mode.\n"
Magnus Karlsson46738f72019-08-14 09:27:21 +02001047 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
Kevin Laatzc543f542019-08-27 02:25:28 +00001048 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
1049 " -u, --unaligned Enable unaligned chunk placement\n"
Mariusz Dudek3627d972020-12-03 10:05:46 +01001050 " -M, --shared-umem Enable XDP_SHARED_UMEM (cannot be used with -R)\n"
Andre Guedesb3133322019-11-14 08:28:47 -08001051 " -F, --force Force loading the XDP prog\n"
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301052 " -d, --duration=n Duration in secs to run command.\n"
1053 " Default: forever.\n"
Ong Boon Leong5a388252021-12-30 11:54:43 +08001054 " -w, --clock=CLOCK Clock NAME (default MONOTONIC).\n"
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301055 " -b, --batch-size=n Batch size for sending or receiving\n"
1056 " packets. Default: %d\n"
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301057 " -C, --tx-pkt-count=n Number of packets to send.\n"
1058 " Default: Continuous packets.\n"
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301059 " -s, --tx-pkt-size=n Transmit packet size.\n"
1060 " (Default: %d bytes)\n"
1061 " Min size: %d, Max size %d.\n"
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301062 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
Ong Boon Leong2741a042021-12-30 11:54:41 +08001063 " -V, --tx-vlan Send VLAN tagged packets (For -t|--txonly)\n"
1064 " -J, --tx-vlan-id=n Tx VLAN ID [1-4095]. Default: %d (For -V|--tx-vlan)\n"
1065 " -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 +08001066 " -G, --tx-dmac=<MAC> Dest MAC addr of TX frame in aa:bb:cc:dd:ee:ff format (For -V|--tx-vlan)\n"
1067 " -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 +08001068 " -T, --tx-cycle=n Tx cycle time in micro-seconds (For -t|--txonly).\n"
Ciara Loftusb36c3202020-07-08 07:28:34 +00001069 " -x, --extra-stats Display extra statistics.\n"
Magnus Karlsson74e00672020-09-10 10:31:06 +02001070 " -Q, --quiet Do not display any stats.\n"
Ciara Loftus60dc6092020-10-02 13:36:11 +00001071 " -a, --app-stats Display application (syscall) statistics.\n"
Ciara Loftus67ed3752020-10-02 13:36:12 +00001072 " -I, --irq-string Display driver interrupt statistics for interface associated with irq-string.\n"
Björn Töpelb35fc142020-11-30 19:52:04 +01001073 " -B, --busy-poll Busy poll.\n"
Mariusz Dudek3627d972020-12-03 10:05:46 +01001074 " -R, --reduce-cap Use reduced capabilities (cannot be used with -M)\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001075 "\n";
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301076 fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301077 opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
Ong Boon Leong2741a042021-12-30 11:54:41 +08001078 XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern,
1079 VLAN_VID__DEFAULT, VLAN_PRI__DEFAULT);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301080
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001081 exit(EXIT_FAILURE);
1082}
1083
1084static void parse_command_line(int argc, char **argv)
1085{
1086 int option_index, c;
1087
1088 opterr = 0;
1089
1090 for (;;) {
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001091 c = getopt_long(argc, argv, "Frtli:q:pSNn:w:czf:muMd:b:C:s:P:VJ:K:G:H:T:xQaI:BR",
Magnus Karlsson46738f72019-08-14 09:27:21 +02001092 long_options, &option_index);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001093 if (c == -1)
1094 break;
1095
1096 switch (c) {
1097 case 'r':
1098 opt_bench = BENCH_RXDROP;
1099 break;
1100 case 't':
1101 opt_bench = BENCH_TXONLY;
1102 break;
1103 case 'l':
1104 opt_bench = BENCH_L2FWD;
1105 break;
1106 case 'i':
1107 opt_if = optarg;
1108 break;
1109 case 'q':
1110 opt_queue = atoi(optarg);
1111 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001112 case 'p':
1113 opt_poll = 1;
1114 break;
1115 case 'S':
1116 opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
Björn Töpel9f5232c2018-06-04 14:06:01 +02001117 opt_xdp_bind_flags |= XDP_COPY;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001118 break;
1119 case 'N':
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +01001120 /* default, set below */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001121 break;
1122 case 'n':
1123 opt_interval = atoi(optarg);
1124 break;
Ong Boon Leong5a388252021-12-30 11:54:43 +08001125 case 'w':
1126 if (get_clockid(&opt_clock, optarg)) {
1127 fprintf(stderr,
1128 "ERROR: Invalid clock %s. Default to CLOCK_MONOTONIC.\n",
1129 optarg);
1130 opt_clock = CLOCK_MONOTONIC;
1131 }
1132 break;
Björn Töpel58c50ae2018-08-28 14:44:35 +02001133 case 'z':
1134 opt_xdp_bind_flags |= XDP_ZEROCOPY;
1135 break;
1136 case 'c':
1137 opt_xdp_bind_flags |= XDP_COPY;
1138 break;
Kevin Laatzc543f542019-08-27 02:25:28 +00001139 case 'u':
1140 opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
1141 opt_unaligned_chunks = 1;
Kevin Laatz3945b372019-08-27 02:25:30 +00001142 opt_mmap_flags = MAP_HUGETLB;
Kevin Laatzc543f542019-08-27 02:25:28 +00001143 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +01001144 case 'F':
1145 opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
1146 break;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001147 case 'f':
1148 opt_xsk_frame_size = atoi(optarg);
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001149 break;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001150 case 'm':
1151 opt_need_wakeup = false;
1152 opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001153 break;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001154 case 'M':
1155 opt_num_xsks = MAX_SOCKS;
1156 break;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301157 case 'd':
1158 opt_duration = atoi(optarg);
1159 opt_duration *= 1000000000;
1160 break;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301161 case 'b':
1162 opt_batch_size = atoi(optarg);
1163 break;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301164 case 'C':
1165 opt_pkt_count = atoi(optarg);
1166 break;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301167 case 's':
1168 opt_pkt_size = atoi(optarg);
1169 if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
1170 opt_pkt_size < MIN_PKT_SIZE) {
1171 fprintf(stderr,
1172 "ERROR: Invalid frame size %d\n",
1173 opt_pkt_size);
1174 usage(basename(argv[0]));
1175 }
1176 break;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +05301177 case 'P':
1178 opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
1179 break;
Ong Boon Leong2741a042021-12-30 11:54:41 +08001180 case 'V':
1181 opt_vlan_tag = true;
1182 break;
1183 case 'J':
1184 opt_pkt_vlan_id = atoi(optarg);
1185 break;
1186 case 'K':
1187 opt_pkt_vlan_pri = atoi(optarg);
1188 break;
Ong Boon Leong6440a6c2021-12-30 11:54:42 +08001189 case 'G':
1190 if (!ether_aton_r(optarg,
1191 (struct ether_addr *)&opt_txdmac)) {
1192 fprintf(stderr, "Invalid dmac address:%s\n",
1193 optarg);
1194 usage(basename(argv[0]));
1195 }
1196 break;
1197 case 'H':
1198 if (!ether_aton_r(optarg,
1199 (struct ether_addr *)&opt_txsmac)) {
1200 fprintf(stderr, "Invalid smac address:%s\n",
1201 optarg);
1202 usage(basename(argv[0]));
1203 }
1204 break;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001205 case 'T':
1206 opt_tx_cycle_ns = atoi(optarg);
1207 opt_tx_cycle_ns *= NSEC_PER_USEC;
1208 break;
Ciara Loftusb36c3202020-07-08 07:28:34 +00001209 case 'x':
1210 opt_extra_stats = 1;
1211 break;
Magnus Karlsson74e00672020-09-10 10:31:06 +02001212 case 'Q':
1213 opt_quiet = 1;
1214 break;
Ciara Loftus60dc6092020-10-02 13:36:11 +00001215 case 'a':
1216 opt_app_stats = 1;
1217 break;
Ciara Loftus67ed3752020-10-02 13:36:12 +00001218 case 'I':
1219 opt_irq_str = optarg;
1220 if (get_interrupt_number())
1221 irqs_at_init = get_irqs();
1222 if (irqs_at_init < 0) {
1223 fprintf(stderr, "ERROR: Failed to get irqs for %s\n", opt_irq_str);
1224 usage(basename(argv[0]));
1225 }
Björn Töpelb35fc142020-11-30 19:52:04 +01001226 break;
1227 case 'B':
1228 opt_busy_poll = 1;
Ciara Loftus67ed3752020-10-02 13:36:12 +00001229 break;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001230 case 'R':
1231 opt_reduced_cap = true;
1232 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001233 default:
1234 usage(basename(argv[0]));
1235 }
1236 }
1237
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +01001238 if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
1239 opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
1240
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001241 opt_ifindex = if_nametoindex(opt_if);
1242 if (!opt_ifindex) {
1243 fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
1244 opt_if);
1245 usage(basename(argv[0]));
1246 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001247
Kevin Laatzc543f542019-08-27 02:25:28 +00001248 if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
1249 !opt_unaligned_chunks) {
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001250 fprintf(stderr, "--frame-size=%d is not a power of two\n",
1251 opt_xsk_frame_size);
1252 usage(basename(argv[0]));
1253 }
Mariusz Dudek3627d972020-12-03 10:05:46 +01001254
1255 if (opt_reduced_cap && opt_num_xsks > 1) {
1256 fprintf(stderr, "ERROR: -M and -R cannot be used together\n");
1257 usage(basename(argv[0]));
1258 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001259}
1260
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001261static void kick_tx(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001262{
1263 int ret;
1264
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001265 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
Maciej Fijalkowski8ed47e12020-02-05 05:58:34 +01001266 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
1267 errno == EBUSY || errno == ENETDOWN)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001268 return;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001269 exit_with_error(errno);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001270}
1271
Björn Töpel284cbc62020-11-30 19:52:03 +01001272static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001273{
Kevin Laatz03895e62019-08-27 02:25:29 +00001274 struct xsk_umem_info *umem = xsk->umem;
Yonghong Songb74e21a2019-02-28 22:19:41 -08001275 u32 idx_cq = 0, idx_fq = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001276 unsigned int rcvd;
1277 size_t ndescs;
1278
1279 if (!xsk->outstanding_tx)
1280 return;
1281
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001282 /* In copy mode, Tx is driven by a syscall so we need to use e.g. sendto() to
1283 * really send the packets. In zero-copy mode we do not have to do this, since Tx
1284 * is driven by the NAPI loop. So as an optimization, we do not have to call
1285 * sendto() all the time in zero-copy mode for l2fwd.
1286 */
Ciara Loftus60dc6092020-10-02 13:36:11 +00001287 if (opt_xdp_bind_flags & XDP_COPY) {
1288 xsk->app_stats.copy_tx_sendtos++;
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001289 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001290 }
Magnus Karlsson3131cf62020-09-10 10:31:04 +02001291
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301292 ndescs = (xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001293 xsk->outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001294
1295 /* re-add completed Tx buffers */
Kevin Laatz03895e62019-08-27 02:25:29 +00001296 rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001297 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001298 unsigned int i;
1299 int ret;
1300
Kevin Laatz03895e62019-08-27 02:25:29 +00001301 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001302 while (ret != rcvd) {
1303 if (ret < 0)
1304 exit_with_error(-ret);
Björn Töpelb35fc142020-11-30 19:52:04 +01001305 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001306 xsk->app_stats.fill_fail_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001307 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL,
1308 NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001309 }
Kevin Laatz03895e62019-08-27 02:25:29 +00001310 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001311 }
Kevin Laatz03895e62019-08-27 02:25:29 +00001312
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001313 for (i = 0; i < rcvd; i++)
Kevin Laatz03895e62019-08-27 02:25:29 +00001314 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
1315 *xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001316
1317 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1318 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001319 xsk->outstanding_tx -= rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001320 }
1321}
1322
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301323static inline void complete_tx_only(struct xsk_socket_info *xsk,
1324 int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001325{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001326 unsigned int rcvd;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001327 u32 idx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001328
1329 if (!xsk->outstanding_tx)
1330 return;
1331
Ciara Loftus60dc6092020-10-02 13:36:11 +00001332 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
1333 xsk->app_stats.tx_wakeup_sendtos++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001334 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001335 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001336
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301337 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001338 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001339 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001340 xsk->outstanding_tx -= rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001341 }
1342}
1343
Björn Töpelf2d27282020-11-30 19:52:02 +01001344static void rx_drop(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001345{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001346 unsigned int rcvd, i;
Yonghong Songb74e21a2019-02-28 22:19:41 -08001347 u32 idx_rx = 0, idx_fq = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001348 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001349
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301350 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001351 if (!rcvd) {
Björn Töpelb35fc142020-11-30 19:52:04 +01001352 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001353 xsk->app_stats.rx_empty_polls++;
Björn Töpelf2d27282020-11-30 19:52:02 +01001354 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001355 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001356 return;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001357 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001358
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001359 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
1360 while (ret != rcvd) {
1361 if (ret < 0)
1362 exit_with_error(-ret);
Björn Töpelb35fc142020-11-30 19:52:04 +01001363 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001364 xsk->app_stats.fill_fail_polls++;
Björn Töpelf2d27282020-11-30 19:52:02 +01001365 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001366 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001367 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001368 }
1369
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001370 for (i = 0; i < rcvd; i++) {
1371 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1372 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Kevin Laatz03895e62019-08-27 02:25:29 +00001373 u64 orig = xsk_umem__extract_addr(addr);
1374
1375 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001376 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001377
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001378 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001379 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001380 }
1381
1382 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1383 xsk_ring_cons__release(&xsk->rx, rcvd);
Ciara Loftus2e8806f2020-10-02 13:36:10 +00001384 xsk->ring_stats.rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001385}
1386
1387static void rx_drop_all(void)
1388{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001389 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001390 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001391
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001392 for (i = 0; i < num_socks; i++) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001393 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001394 fds[i].events = POLLIN;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001395 }
1396
1397 for (;;) {
1398 if (opt_poll) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001399 for (i = 0; i < num_socks; i++)
1400 xsks[i]->app_stats.opt_polls++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001401 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001402 if (ret <= 0)
1403 continue;
1404 }
1405
1406 for (i = 0; i < num_socks; i++)
Björn Töpelf2d27282020-11-30 19:52:02 +01001407 rx_drop(xsks[i]);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301408
1409 if (benchmark_done)
1410 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001411 }
1412}
1413
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001414static int tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001415{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001416 u32 idx;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301417 unsigned int i;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001418
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301419 while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
1420 batch_size) {
1421 complete_tx_only(xsk, batch_size);
Magnus Karlsson092fde02020-12-10 17:34:07 +01001422 if (benchmark_done)
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001423 return 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001424 }
1425
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301426 for (i = 0; i < batch_size; i++) {
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301427 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
1428 idx + i);
Magnus Karlsson3b80d102021-05-06 14:43:49 +02001429 tx_desc->addr = (*frame_nb + i) * opt_xsk_frame_size;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301430 tx_desc->len = PKT_SIZE;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301431 }
1432
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301433 xsk_ring_prod__submit(&xsk->tx, batch_size);
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001434 xsk->ring_stats.tx_npkts += batch_size;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301435 xsk->outstanding_tx += batch_size;
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001436 *frame_nb += batch_size;
1437 *frame_nb %= NUM_FRAMES;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301438 complete_tx_only(xsk, batch_size);
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001439
1440 return batch_size;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301441}
1442
1443static inline int get_batch_size(int pkt_cnt)
1444{
1445 if (!opt_pkt_count)
1446 return opt_batch_size;
1447
1448 if (pkt_cnt + opt_batch_size <= opt_pkt_count)
1449 return opt_batch_size;
1450
1451 return opt_pkt_count - pkt_cnt;
1452}
1453
1454static void complete_tx_only_all(void)
1455{
1456 bool pending;
1457 int i;
1458
1459 do {
1460 pending = false;
1461 for (i = 0; i < num_socks; i++) {
1462 if (xsks[i]->outstanding_tx) {
1463 complete_tx_only(xsks[i], opt_batch_size);
1464 pending = !!xsks[i]->outstanding_tx;
1465 }
1466 }
1467 } while (pending);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001468}
1469
1470static void tx_only_all(void)
1471{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001472 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001473 u32 frame_nb[MAX_SOCKS] = {};
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001474 unsigned long next_tx_ns = 0;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301475 int pkt_cnt = 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001476 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001477
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001478 if (opt_poll && opt_tx_cycle_ns) {
1479 fprintf(stderr,
1480 "Error: --poll and --tx-cycles are both set\n");
1481 return;
1482 }
1483
Magnus Karlsson46738f72019-08-14 09:27:21 +02001484 for (i = 0; i < num_socks; i++) {
1485 fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
1486 fds[0].events = POLLOUT;
1487 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001488
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001489 if (opt_tx_cycle_ns) {
1490 /* Align Tx time to micro-second boundary */
1491 next_tx_ns = (get_nsecs() / NSEC_PER_USEC + 1) *
1492 NSEC_PER_USEC;
1493 next_tx_ns += opt_tx_cycle_ns;
1494
1495 /* Initialize periodic Tx scheduling variance */
1496 tx_cycle_diff_min = 1000000000;
1497 tx_cycle_diff_max = 0;
1498 tx_cycle_diff_ave = 0.0;
1499 }
1500
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301501 while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
1502 int batch_size = get_batch_size(pkt_cnt);
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001503 struct timespec next;
1504 int tx_cnt = 0;
1505 long diff;
1506 int err;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301507
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001508 if (opt_poll) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001509 for (i = 0; i < num_socks; i++)
1510 xsks[i]->app_stats.opt_polls++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001511 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001512 if (ret <= 0)
1513 continue;
1514
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001515 if (!(fds[0].revents & POLLOUT))
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001516 continue;
1517 }
1518
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001519 if (opt_tx_cycle_ns) {
1520 next.tv_sec = next_tx_ns / NSEC_PER_SEC;
1521 next.tv_nsec = next_tx_ns % NSEC_PER_SEC;
1522 err = clock_nanosleep(opt_clock, TIMER_ABSTIME, &next, NULL);
1523 if (err) {
1524 if (err != EINTR)
1525 fprintf(stderr,
1526 "clock_nanosleep failed. Err:%d errno:%d\n",
1527 err, errno);
1528 break;
1529 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301530
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001531 /* Measure periodic Tx scheduling variance */
1532 diff = get_nsecs() - next_tx_ns;
1533 if (diff < tx_cycle_diff_min)
1534 tx_cycle_diff_min = diff;
1535
1536 if (diff > tx_cycle_diff_max)
1537 tx_cycle_diff_max = diff;
1538
1539 tx_cycle_diff_ave += (double)diff;
1540 tx_cycle_cnt++;
1541 }
1542
1543 for (i = 0; i < num_socks; i++)
1544 tx_cnt += tx_only(xsks[i], &frame_nb[i], batch_size);
1545
1546 pkt_cnt += tx_cnt;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301547
1548 if (benchmark_done)
1549 break;
Ong Boon Leongfa0d27a12021-12-30 11:54:44 +08001550
1551 if (opt_tx_cycle_ns)
1552 next_tx_ns += opt_tx_cycle_ns;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001553 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301554
1555 if (opt_pkt_count)
1556 complete_tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001557}
1558
Björn Töpel284cbc62020-11-30 19:52:03 +01001559static void l2fwd(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001560{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001561 unsigned int rcvd, i;
1562 u32 idx_rx = 0, idx_tx = 0;
1563 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001564
Björn Töpel284cbc62020-11-30 19:52:03 +01001565 complete_tx_l2fwd(xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001566
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301567 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001568 if (!rcvd) {
Björn Töpelb35fc142020-11-30 19:52:04 +01001569 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001570 xsk->app_stats.rx_empty_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001571 recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001572 }
Magnus Karlsson46738f72019-08-14 09:27:21 +02001573 return;
1574 }
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001575 xsk->ring_stats.rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001576
Magnus Karlsson46738f72019-08-14 09:27:21 +02001577 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1578 while (ret != rcvd) {
1579 if (ret < 0)
1580 exit_with_error(-ret);
Björn Töpel284cbc62020-11-30 19:52:03 +01001581 complete_tx_l2fwd(xsk);
Björn Töpelb35fc142020-11-30 19:52:04 +01001582 if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
Ciara Loftus60dc6092020-10-02 13:36:11 +00001583 xsk->app_stats.tx_wakeup_sendtos++;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001584 kick_tx(xsk);
Ciara Loftus60dc6092020-10-02 13:36:11 +00001585 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001586 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001587 }
1588
1589 for (i = 0; i < rcvd; i++) {
1590 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1591 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Ciara Loftus5a712e12019-09-13 10:39:48 +00001592 u64 orig = addr;
Kevin Laatz03895e62019-08-27 02:25:29 +00001593
1594 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001595 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1596
1597 swap_mac_addresses(pkt);
1598
1599 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001600 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = orig;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001601 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
1602 }
1603
1604 xsk_ring_prod__submit(&xsk->tx, rcvd);
1605 xsk_ring_cons__release(&xsk->rx, rcvd);
1606
Magnus Karlsson90da4b32020-11-16 12:12:43 +01001607 xsk->ring_stats.tx_npkts += rcvd;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001608 xsk->outstanding_tx += rcvd;
1609}
1610
1611static void l2fwd_all(void)
1612{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001613 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001614 int i, ret;
1615
Magnus Karlsson46738f72019-08-14 09:27:21 +02001616 for (;;) {
1617 if (opt_poll) {
Björn Töpel284cbc62020-11-30 19:52:03 +01001618 for (i = 0; i < num_socks; i++) {
1619 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1620 fds[i].events = POLLOUT | POLLIN;
Ciara Loftus60dc6092020-10-02 13:36:11 +00001621 xsks[i]->app_stats.opt_polls++;
Björn Töpel284cbc62020-11-30 19:52:03 +01001622 }
Magnus Karlsson46738f72019-08-14 09:27:21 +02001623 ret = poll(fds, num_socks, opt_timeout);
1624 if (ret <= 0)
1625 continue;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001626 }
1627
Magnus Karlsson46738f72019-08-14 09:27:21 +02001628 for (i = 0; i < num_socks; i++)
Björn Töpel284cbc62020-11-30 19:52:03 +01001629 l2fwd(xsks[i]);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301630
1631 if (benchmark_done)
1632 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001633 }
1634}
1635
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001636static void load_xdp_program(char **argv, struct bpf_object **obj)
1637{
1638 struct bpf_prog_load_attr prog_load_attr = {
1639 .prog_type = BPF_PROG_TYPE_XDP,
1640 };
1641 char xdp_filename[256];
1642 int prog_fd;
1643
1644 snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
1645 prog_load_attr.file = xdp_filename;
1646
1647 if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
1648 exit(EXIT_FAILURE);
1649 if (prog_fd < 0) {
1650 fprintf(stderr, "ERROR: no program found: %s\n",
1651 strerror(prog_fd));
1652 exit(EXIT_FAILURE);
1653 }
1654
1655 if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
1656 fprintf(stderr, "ERROR: link set xdp fd failed\n");
1657 exit(EXIT_FAILURE);
1658 }
1659}
1660
1661static void enter_xsks_into_map(struct bpf_object *obj)
1662{
1663 struct bpf_map *map;
1664 int i, xsks_map;
1665
1666 map = bpf_object__find_map_by_name(obj, "xsks_map");
1667 xsks_map = bpf_map__fd(map);
1668 if (xsks_map < 0) {
1669 fprintf(stderr, "ERROR: no xsks map found: %s\n",
1670 strerror(xsks_map));
1671 exit(EXIT_FAILURE);
1672 }
1673
1674 for (i = 0; i < num_socks; i++) {
1675 int fd = xsk_socket__fd(xsks[i]->xsk);
1676 int key, ret;
1677
1678 key = i;
1679 ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
1680 if (ret) {
1681 fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
1682 exit(EXIT_FAILURE);
1683 }
1684 }
1685}
1686
Björn Töpelb35fc142020-11-30 19:52:04 +01001687static void apply_setsockopt(struct xsk_socket_info *xsk)
1688{
1689 int sock_opt;
1690
1691 if (!opt_busy_poll)
1692 return;
1693
1694 sock_opt = 1;
1695 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL,
1696 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1697 exit_with_error(errno);
1698
1699 sock_opt = 20;
1700 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL,
1701 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1702 exit_with_error(errno);
Björn Töpel41bf9002020-11-30 19:52:05 +01001703
1704 sock_opt = opt_batch_size;
1705 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1706 (void *)&sock_opt, sizeof(sock_opt)) < 0)
1707 exit_with_error(errno);
Björn Töpelb35fc142020-11-30 19:52:04 +01001708}
1709
Mariusz Dudek3627d972020-12-03 10:05:46 +01001710static int recv_xsks_map_fd_from_ctrl_node(int sock, int *_fd)
1711{
1712 char cms[CMSG_SPACE(sizeof(int))];
1713 struct cmsghdr *cmsg;
1714 struct msghdr msg;
1715 struct iovec iov;
1716 int value;
1717 int len;
1718
1719 iov.iov_base = &value;
1720 iov.iov_len = sizeof(int);
1721
1722 msg.msg_name = 0;
1723 msg.msg_namelen = 0;
1724 msg.msg_iov = &iov;
1725 msg.msg_iovlen = 1;
1726 msg.msg_flags = 0;
1727 msg.msg_control = (caddr_t)cms;
1728 msg.msg_controllen = sizeof(cms);
1729
1730 len = recvmsg(sock, &msg, 0);
1731
1732 if (len < 0) {
1733 fprintf(stderr, "Recvmsg failed length incorrect.\n");
1734 return -EINVAL;
1735 }
1736
1737 if (len == 0) {
1738 fprintf(stderr, "Recvmsg failed no data\n");
1739 return -EINVAL;
1740 }
1741
1742 cmsg = CMSG_FIRSTHDR(&msg);
1743 *_fd = *(int *)CMSG_DATA(cmsg);
1744
1745 return 0;
1746}
1747
1748static int
1749recv_xsks_map_fd(int *xsks_map_fd)
1750{
1751 struct sockaddr_un server;
1752 int err;
1753
1754 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1755 if (sock < 0) {
1756 fprintf(stderr, "Error opening socket stream: %s", strerror(errno));
1757 return errno;
1758 }
1759
1760 server.sun_family = AF_UNIX;
1761 strcpy(server.sun_path, SOCKET_NAME);
1762
1763 if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) < 0) {
1764 close(sock);
1765 fprintf(stderr, "Error connecting stream socket: %s", strerror(errno));
1766 return errno;
1767 }
1768
1769 err = recv_xsks_map_fd_from_ctrl_node(sock, xsks_map_fd);
1770 if (err) {
Colin Ian King2faa7322020-12-03 11:44:52 +00001771 fprintf(stderr, "Error %d receiving fd\n", err);
Mariusz Dudek3627d972020-12-03 10:05:46 +01001772 return err;
1773 }
1774 return 0;
1775}
1776
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001777int main(int argc, char **argv)
1778{
Mariusz Dudek3627d972020-12-03 10:05:46 +01001779 struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 };
1780 struct __user_cap_data_struct data[2] = { { 0 } };
1781 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Magnus Karlsson661842c2019-11-07 18:47:39 +01001782 bool rx = false, tx = false;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001783 struct xsk_umem_info *umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001784 struct bpf_object *obj;
Mariusz Dudek3627d972020-12-03 10:05:46 +01001785 int xsks_map_fd = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001786 pthread_t pt;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001787 int i, ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001788 void *bufs;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001789
1790 parse_command_line(argc, argv);
1791
Mariusz Dudek3627d972020-12-03 10:05:46 +01001792 if (opt_reduced_cap) {
1793 if (capget(&hdr, data) < 0)
1794 fprintf(stderr, "Error getting capabilities\n");
1795
1796 data->effective &= CAP_TO_MASK(CAP_NET_RAW);
1797 data->permitted &= CAP_TO_MASK(CAP_NET_RAW);
1798
1799 if (capset(&hdr, data) < 0)
1800 fprintf(stderr, "Setting capabilities failed\n");
1801
1802 if (capget(&hdr, data) < 0) {
1803 fprintf(stderr, "Error getting capabilities\n");
1804 } else {
1805 fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
1806 data[0].effective, data[0].inheritable, data[0].permitted);
1807 fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
1808 data[1].effective, data[1].inheritable, data[1].permitted);
1809 }
1810 } else {
1811 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
1812 fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1813 strerror(errno));
1814 exit(EXIT_FAILURE);
1815 }
1816
1817 if (opt_num_xsks > 1)
1818 load_xdp_program(argv, &obj);
1819 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001820
Kevin Laatz3945b372019-08-27 02:25:30 +00001821 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1822 bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size,
1823 PROT_READ | PROT_WRITE,
1824 MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
1825 if (bufs == MAP_FAILED) {
1826 printf("ERROR: mmap failed\n");
1827 exit(EXIT_FAILURE);
1828 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001829
1830 /* Create sockets... */
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001831 umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size);
Magnus Karlsson661842c2019-11-07 18:47:39 +01001832 if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
1833 rx = true;
1834 xsk_populate_fill_ring(umem);
1835 }
1836 if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
1837 tx = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001838 for (i = 0; i < opt_num_xsks; i++)
Magnus Karlsson661842c2019-11-07 18:47:39 +01001839 xsks[num_socks++] = xsk_configure_socket(umem, rx, tx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001840
Björn Töpelb35fc142020-11-30 19:52:04 +01001841 for (i = 0; i < opt_num_xsks; i++)
1842 apply_setsockopt(xsks[i]);
1843
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301844 if (opt_bench == BENCH_TXONLY) {
1845 gen_eth_hdr_data();
1846
Magnus Karlsson661842c2019-11-07 18:47:39 +01001847 for (i = 0; i < NUM_FRAMES; i++)
1848 gen_eth_frame(umem, i * opt_xsk_frame_size);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301849 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001850
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001851 if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
1852 enter_xsks_into_map(obj);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001853
Mariusz Dudek3627d972020-12-03 10:05:46 +01001854 if (opt_reduced_cap) {
1855 ret = recv_xsks_map_fd(&xsks_map_fd);
1856 if (ret) {
1857 fprintf(stderr, "Error %d receiving xsks_map_fd\n", ret);
1858 exit_with_error(ret);
1859 }
1860 if (xsks[0]->xsk) {
1861 ret = xsk_socket__update_xskmap(xsks[0]->xsk, xsks_map_fd);
1862 if (ret) {
1863 fprintf(stderr, "Update of BPF map failed(%d)\n", ret);
1864 exit_with_error(ret);
1865 }
1866 }
1867 }
1868
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001869 signal(SIGINT, int_exit);
1870 signal(SIGTERM, int_exit);
1871 signal(SIGABRT, int_exit);
1872
1873 setlocale(LC_ALL, "");
1874
Magnus Karlsson74e00672020-09-10 10:31:06 +02001875 if (!opt_quiet) {
1876 ret = pthread_create(&pt, NULL, poller, NULL);
1877 if (ret)
1878 exit_with_error(ret);
1879 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001880
1881 prev_time = get_nsecs();
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301882 start_time = prev_time;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001883
1884 if (opt_bench == BENCH_RXDROP)
1885 rx_drop_all();
1886 else if (opt_bench == BENCH_TXONLY)
Magnus Karlsson46738f72019-08-14 09:27:21 +02001887 tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001888 else
Magnus Karlsson46738f72019-08-14 09:27:21 +02001889 l2fwd_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001890
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301891 benchmark_done = true;
1892
Magnus Karlsson74e00672020-09-10 10:31:06 +02001893 if (!opt_quiet)
1894 pthread_join(pt, NULL);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301895
Jay Jayatheerthan69525582019-12-20 14:25:26 +05301896 xdpsock_cleanup();
1897
Maciej Fijalkowski6bc66992021-03-03 19:56:35 +01001898 munmap(bufs, NUM_FRAMES * opt_xsk_frame_size);
1899
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001900 return 0;
1901}