blob: b6175cb9a31d1c1bf3c86faf8516629f8a849306 [file] [log] [blame]
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001// SPDX-License-Identifier: GPL-2.0
Björn Töpeldac09142018-05-18 14:00:21 +02002/* Copyright(c) 2017 - 2018 Intel Corporation. */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02003
Magnus Karlsson248c7f92019-02-21 10:21:27 +01004#include <asm/barrier.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02005#include <errno.h>
6#include <getopt.h>
7#include <libgen.h>
8#include <linux/bpf.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +01009#include <linux/compiler.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020010#include <linux/if_link.h>
11#include <linux/if_xdp.h>
12#include <linux/if_ether.h>
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053013#include <linux/ip.h>
14#include <linux/udp.h>
15#include <arpa/inet.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010016#include <locale.h>
17#include <net/ethernet.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>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010026#include <sys/mman.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020027#include <sys/resource.h>
28#include <sys/socket.h>
Magnus Karlsson248c7f92019-02-21 10:21:27 +010029#include <sys/types.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020030#include <time.h>
31#include <unistd.h>
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020032
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010033#include <bpf/libbpf.h>
34#include <bpf/xsk.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070035#include <bpf/bpf.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010036#include "xdpsock.h"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020037
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020038#ifndef SOL_XDP
39#define SOL_XDP 283
40#endif
41
42#ifndef AF_XDP
43#define AF_XDP 44
44#endif
45
46#ifndef PF_XDP
47#define PF_XDP AF_XDP
48#endif
49
Magnus Karlsson248c7f92019-02-21 10:21:27 +010050#define NUM_FRAMES (4 * 1024)
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053051#define MIN_PKT_SIZE 64
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020052
53#define DEBUG_HEXDUMP 0
54
Björn Töpela412ef52018-06-04 13:57:14 +020055typedef __u64 u64;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020056typedef __u32 u32;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053057typedef __u16 u16;
58typedef __u8 u8;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020059
60static unsigned long prev_time;
61
62enum benchmark_type {
63 BENCH_RXDROP = 0,
64 BENCH_TXONLY = 1,
65 BENCH_L2FWD = 2,
66};
67
68static enum benchmark_type opt_bench = BENCH_RXDROP;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010069static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020070static const char *opt_if = "";
71static int opt_ifindex;
72static int opt_queue;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +053073static unsigned long opt_duration;
74static unsigned long start_time;
75static bool benchmark_done;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +053076static u32 opt_batch_size = 64;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +053077static int opt_pkt_count;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +053078static u16 opt_pkt_size = MIN_PKT_SIZE;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +053079static u32 opt_pkt_fill_pattern = 0x12345678;
Ciara Loftusb36c3202020-07-08 07:28:34 +000080static bool opt_extra_stats;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020081static int opt_poll;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020082static int opt_interval = 1;
Magnus Karlsson46738f72019-08-14 09:27:21 +020083static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
Kevin Laatzc543f542019-08-27 02:25:28 +000084static u32 opt_umem_flags;
85static int opt_unaligned_chunks;
Kevin Laatz3945b372019-08-27 02:25:30 +000086static int opt_mmap_flags;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +030087static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
Magnus Karlsson46738f72019-08-14 09:27:21 +020088static int opt_timeout = 1000;
89static bool opt_need_wakeup = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +010090static u32 opt_num_xsks = 1;
91static u32 prog_id;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020092
Magnus Karlsson248c7f92019-02-21 10:21:27 +010093struct xsk_umem_info {
94 struct xsk_ring_prod fq;
95 struct xsk_ring_cons cq;
96 struct xsk_umem *umem;
97 void *buffer;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020098};
99
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100100struct xsk_socket_info {
101 struct xsk_ring_cons rx;
102 struct xsk_ring_prod tx;
103 struct xsk_umem_info *umem;
104 struct xsk_socket *xsk;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200105 unsigned long rx_npkts;
106 unsigned long tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000107 unsigned long rx_dropped_npkts;
108 unsigned long rx_invalid_npkts;
109 unsigned long tx_invalid_npkts;
110 unsigned long rx_full_npkts;
111 unsigned long rx_fill_empty_npkts;
112 unsigned long tx_empty_npkts;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200113 unsigned long prev_rx_npkts;
114 unsigned long prev_tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000115 unsigned long prev_rx_dropped_npkts;
116 unsigned long prev_rx_invalid_npkts;
117 unsigned long prev_tx_invalid_npkts;
118 unsigned long prev_rx_full_npkts;
119 unsigned long prev_rx_fill_empty_npkts;
120 unsigned long prev_tx_empty_npkts;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100121 u32 outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200122};
123
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200124static int num_socks;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100125struct xsk_socket_info *xsks[MAX_SOCKS];
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200126
127static unsigned long get_nsecs(void)
128{
129 struct timespec ts;
130
131 clock_gettime(CLOCK_MONOTONIC, &ts);
132 return ts.tv_sec * 1000000000UL + ts.tv_nsec;
133}
134
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200135static void print_benchmark(bool running)
136{
137 const char *bench_str = "INVALID";
138
139 if (opt_bench == BENCH_RXDROP)
140 bench_str = "rxdrop";
141 else if (opt_bench == BENCH_TXONLY)
142 bench_str = "txonly";
143 else if (opt_bench == BENCH_L2FWD)
144 bench_str = "l2fwd";
145
146 printf("%s:%d %s ", opt_if, opt_queue, bench_str);
147 if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
148 printf("xdp-skb ");
149 else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
150 printf("xdp-drv ");
151 else
152 printf(" ");
153
154 if (opt_poll)
155 printf("poll() ");
156
157 if (running) {
158 printf("running...");
159 fflush(stdout);
160 }
161}
162
Ciara Loftusb36c3202020-07-08 07:28:34 +0000163static int xsk_get_xdp_stats(int fd, struct xsk_socket_info *xsk)
164{
165 struct xdp_statistics stats;
166 socklen_t optlen;
167 int err;
168
169 optlen = sizeof(stats);
170 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
171 if (err)
172 return err;
173
174 if (optlen == sizeof(struct xdp_statistics)) {
175 xsk->rx_dropped_npkts = stats.rx_dropped;
176 xsk->rx_invalid_npkts = stats.rx_invalid_descs;
177 xsk->tx_invalid_npkts = stats.tx_invalid_descs;
178 xsk->rx_full_npkts = stats.rx_ring_full;
179 xsk->rx_fill_empty_npkts = stats.rx_fill_ring_empty_descs;
180 xsk->tx_empty_npkts = stats.tx_ring_empty_descs;
181 return 0;
182 }
183
184 return -EINVAL;
185}
186
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200187static void dump_stats(void)
188{
189 unsigned long now = get_nsecs();
190 long dt = now - prev_time;
191 int i;
192
193 prev_time = now;
194
Prashant Bhole11c3f512018-08-31 10:00:49 +0900195 for (i = 0; i < num_socks && xsks[i]; i++) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200196 char *fmt = "%-15s %'-11.0f %'-11lu\n";
Ciara Loftusb36c3202020-07-08 07:28:34 +0000197 double rx_pps, tx_pps, dropped_pps, rx_invalid_pps, full_pps, fill_empty_pps,
198 tx_invalid_pps, tx_empty_pps;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200199
200 rx_pps = (xsks[i]->rx_npkts - xsks[i]->prev_rx_npkts) *
201 1000000000. / dt;
202 tx_pps = (xsks[i]->tx_npkts - xsks[i]->prev_tx_npkts) *
203 1000000000. / dt;
204
205 printf("\n sock%d@", i);
206 print_benchmark(false);
207 printf("\n");
208
209 printf("%-15s %-11s %-11s %-11.2f\n", "", "pps", "pkts",
210 dt / 1000000000.);
211 printf(fmt, "rx", rx_pps, xsks[i]->rx_npkts);
212 printf(fmt, "tx", tx_pps, xsks[i]->tx_npkts);
213
214 xsks[i]->prev_rx_npkts = xsks[i]->rx_npkts;
215 xsks[i]->prev_tx_npkts = xsks[i]->tx_npkts;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000216
217 if (opt_extra_stats) {
218 if (!xsk_get_xdp_stats(xsk_socket__fd(xsks[i]->xsk), xsks[i])) {
219 dropped_pps = (xsks[i]->rx_dropped_npkts -
220 xsks[i]->prev_rx_dropped_npkts) * 1000000000. / dt;
221 rx_invalid_pps = (xsks[i]->rx_invalid_npkts -
222 xsks[i]->prev_rx_invalid_npkts) * 1000000000. / dt;
223 tx_invalid_pps = (xsks[i]->tx_invalid_npkts -
224 xsks[i]->prev_tx_invalid_npkts) * 1000000000. / dt;
225 full_pps = (xsks[i]->rx_full_npkts -
226 xsks[i]->prev_rx_full_npkts) * 1000000000. / dt;
227 fill_empty_pps = (xsks[i]->rx_fill_empty_npkts -
228 xsks[i]->prev_rx_fill_empty_npkts)
229 * 1000000000. / dt;
230 tx_empty_pps = (xsks[i]->tx_empty_npkts -
231 xsks[i]->prev_tx_empty_npkts) * 1000000000. / dt;
232
233 printf(fmt, "rx dropped", dropped_pps,
234 xsks[i]->rx_dropped_npkts);
235 printf(fmt, "rx invalid", rx_invalid_pps,
236 xsks[i]->rx_invalid_npkts);
237 printf(fmt, "tx invalid", tx_invalid_pps,
238 xsks[i]->tx_invalid_npkts);
239 printf(fmt, "rx queue full", full_pps,
240 xsks[i]->rx_full_npkts);
241 printf(fmt, "fill ring empty", fill_empty_pps,
242 xsks[i]->rx_fill_empty_npkts);
243 printf(fmt, "tx ring empty", tx_empty_pps,
244 xsks[i]->tx_empty_npkts);
245
246 xsks[i]->prev_rx_dropped_npkts = xsks[i]->rx_dropped_npkts;
247 xsks[i]->prev_rx_invalid_npkts = xsks[i]->rx_invalid_npkts;
248 xsks[i]->prev_tx_invalid_npkts = xsks[i]->tx_invalid_npkts;
249 xsks[i]->prev_rx_full_npkts = xsks[i]->rx_full_npkts;
250 xsks[i]->prev_rx_fill_empty_npkts = xsks[i]->rx_fill_empty_npkts;
251 xsks[i]->prev_tx_empty_npkts = xsks[i]->tx_empty_npkts;
252 } else {
253 printf("%-15s\n", "Error retrieving extra stats");
254 }
255 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200256 }
257}
258
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530259static bool is_benchmark_done(void)
260{
261 if (opt_duration > 0) {
262 unsigned long dt = (get_nsecs() - start_time);
263
264 if (dt >= opt_duration)
265 benchmark_done = true;
266 }
267 return benchmark_done;
268}
269
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200270static void *poller(void *arg)
271{
272 (void)arg;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530273 while (!is_benchmark_done()) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200274 sleep(opt_interval);
275 dump_stats();
276 }
277
278 return NULL;
279}
280
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100281static void remove_xdp_program(void)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200282{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100283 u32 curr_prog_id = 0;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100284
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100285 if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
286 printf("bpf_get_link_xdp_id failed\n");
287 exit(EXIT_FAILURE);
288 }
289 if (prog_id == curr_prog_id)
290 bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
291 else if (!curr_prog_id)
292 printf("couldn't find a prog id on a given interface\n");
293 else
294 printf("program on interface changed, not removing\n");
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100295}
296
297static void int_exit(int sig)
298{
Jay Jayatheerthan69525582019-12-20 14:25:26 +0530299 benchmark_done = true;
300}
301
302static void xdpsock_cleanup(void)
303{
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100304 struct xsk_umem *umem = xsks[0]->umem->umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100305 int i;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100306
307 dump_stats();
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100308 for (i = 0; i < num_socks; i++)
309 xsk_socket__delete(xsks[i]->xsk);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100310 (void)xsk_umem__delete(umem);
311 remove_xdp_program();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200312}
313
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100314static void __exit_with_error(int error, const char *file, const char *func,
315 int line)
316{
317 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
318 line, error, strerror(error));
319 dump_stats();
320 remove_xdp_program();
321 exit(EXIT_FAILURE);
322}
323
324#define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, \
325 __LINE__)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100326static void swap_mac_addresses(void *data)
327{
328 struct ether_header *eth = (struct ether_header *)data;
329 struct ether_addr *src_addr = (struct ether_addr *)&eth->ether_shost;
330 struct ether_addr *dst_addr = (struct ether_addr *)&eth->ether_dhost;
331 struct ether_addr tmp;
332
333 tmp = *src_addr;
334 *src_addr = *dst_addr;
335 *dst_addr = tmp;
336}
337
338static void hex_dump(void *pkt, size_t length, u64 addr)
339{
340 const unsigned char *address = (unsigned char *)pkt;
341 const unsigned char *line = address;
342 size_t line_size = 32;
343 unsigned char c;
344 char buf[32];
345 int i = 0;
346
347 if (!DEBUG_HEXDUMP)
348 return;
349
350 sprintf(buf, "addr=%llu", addr);
351 printf("length = %zu\n", length);
352 printf("%s | ", buf);
353 while (length-- > 0) {
354 printf("%02X ", *address++);
355 if (!(++i % line_size) || (length == 0 && i % line_size)) {
356 if (length == 0) {
357 while (i++ % line_size)
358 printf("__ ");
359 }
360 printf(" | "); /* right close */
361 while (line < address) {
362 c = *line++;
363 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
364 }
365 printf("\n");
366 if (length > 0)
367 printf("%s | ", buf);
368 }
369 }
370 printf("\n");
371}
372
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530373static void *memset32_htonl(void *dest, u32 val, u32 size)
374{
375 u32 *ptr = (u32 *)dest;
376 int i;
377
378 val = htonl(val);
379
380 for (i = 0; i < (size & (~0x3)); i += 4)
381 ptr[i >> 2] = val;
382
383 for (; i < size; i++)
384 ((char *)dest)[i] = ((char *)&val)[i & 3];
385
386 return dest;
387}
388
389/*
390 * This function code has been taken from
391 * Linux kernel lib/checksum.c
392 */
393static inline unsigned short from32to16(unsigned int x)
394{
395 /* add up 16-bit and 16-bit for 16+c bit */
396 x = (x & 0xffff) + (x >> 16);
397 /* add up carry.. */
398 x = (x & 0xffff) + (x >> 16);
399 return x;
400}
401
402/*
403 * This function code has been taken from
404 * Linux kernel lib/checksum.c
405 */
406static unsigned int do_csum(const unsigned char *buff, int len)
407{
408 unsigned int result = 0;
409 int odd;
410
411 if (len <= 0)
412 goto out;
413 odd = 1 & (unsigned long)buff;
414 if (odd) {
415#ifdef __LITTLE_ENDIAN
416 result += (*buff << 8);
417#else
418 result = *buff;
419#endif
420 len--;
421 buff++;
422 }
423 if (len >= 2) {
424 if (2 & (unsigned long)buff) {
425 result += *(unsigned short *)buff;
426 len -= 2;
427 buff += 2;
428 }
429 if (len >= 4) {
430 const unsigned char *end = buff +
431 ((unsigned int)len & ~3);
432 unsigned int carry = 0;
433
434 do {
435 unsigned int w = *(unsigned int *)buff;
436
437 buff += 4;
438 result += carry;
439 result += w;
440 carry = (w > result);
441 } while (buff < end);
442 result += carry;
443 result = (result & 0xffff) + (result >> 16);
444 }
445 if (len & 2) {
446 result += *(unsigned short *)buff;
447 buff += 2;
448 }
449 }
450 if (len & 1)
451#ifdef __LITTLE_ENDIAN
452 result += *buff;
453#else
454 result += (*buff << 8);
455#endif
456 result = from32to16(result);
457 if (odd)
458 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
459out:
460 return result;
461}
462
463__sum16 ip_fast_csum(const void *iph, unsigned int ihl);
464
465/*
466 * This is a version of ip_compute_csum() optimized for IP headers,
467 * which always checksum on 4 octet boundaries.
468 * This function code has been taken from
469 * Linux kernel lib/checksum.c
470 */
471__sum16 ip_fast_csum(const void *iph, unsigned int ihl)
472{
473 return (__force __sum16)~do_csum(iph, ihl * 4);
474}
475
476/*
477 * Fold a partial checksum
478 * This function code has been taken from
479 * Linux kernel include/asm-generic/checksum.h
480 */
481static inline __sum16 csum_fold(__wsum csum)
482{
483 u32 sum = (__force u32)csum;
484
485 sum = (sum & 0xffff) + (sum >> 16);
486 sum = (sum & 0xffff) + (sum >> 16);
487 return (__force __sum16)~sum;
488}
489
490/*
491 * This function code has been taken from
492 * Linux kernel lib/checksum.c
493 */
494static inline u32 from64to32(u64 x)
495{
496 /* add up 32-bit and 32-bit for 32+c bit */
497 x = (x & 0xffffffff) + (x >> 32);
498 /* add up carry.. */
499 x = (x & 0xffffffff) + (x >> 32);
500 return (u32)x;
501}
502
503__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
504 __u32 len, __u8 proto, __wsum sum);
505
506/*
507 * This function code has been taken from
508 * Linux kernel lib/checksum.c
509 */
510__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
511 __u32 len, __u8 proto, __wsum sum)
512{
513 unsigned long long s = (__force u32)sum;
514
515 s += (__force u32)saddr;
516 s += (__force u32)daddr;
517#ifdef __BIG_ENDIAN__
518 s += proto + len;
519#else
520 s += (proto + len) << 8;
521#endif
522 return (__force __wsum)from64to32(s);
523}
524
525/*
526 * This function has been taken from
527 * Linux kernel include/asm-generic/checksum.h
528 */
529static inline __sum16
530csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
531 __u8 proto, __wsum sum)
532{
533 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
534}
535
536static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
537 u8 proto, u16 *udp_pkt)
538{
539 u32 csum = 0;
540 u32 cnt = 0;
541
542 /* udp hdr and data */
543 for (; cnt < len; cnt += 2)
544 csum += udp_pkt[cnt >> 1];
545
546 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
547}
548
549#define ETH_FCS_SIZE 4
550
551#define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
552 sizeof(struct udphdr))
553
554#define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
555#define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
556#define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
557#define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
558
559static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
560
561static void gen_eth_hdr_data(void)
562{
563 struct udphdr *udp_hdr = (struct udphdr *)(pkt_data +
564 sizeof(struct ethhdr) +
565 sizeof(struct iphdr));
566 struct iphdr *ip_hdr = (struct iphdr *)(pkt_data +
567 sizeof(struct ethhdr));
568 struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
569
570 /* ethernet header */
571 memcpy(eth_hdr->h_dest, "\x3c\xfd\xfe\x9e\x7f\x71", ETH_ALEN);
572 memcpy(eth_hdr->h_source, "\xec\xb1\xd7\x98\x3a\xc0", ETH_ALEN);
573 eth_hdr->h_proto = htons(ETH_P_IP);
574
575 /* IP header */
576 ip_hdr->version = IPVERSION;
577 ip_hdr->ihl = 0x5; /* 20 byte header */
578 ip_hdr->tos = 0x0;
579 ip_hdr->tot_len = htons(IP_PKT_SIZE);
580 ip_hdr->id = 0;
581 ip_hdr->frag_off = 0;
582 ip_hdr->ttl = IPDEFTTL;
583 ip_hdr->protocol = IPPROTO_UDP;
584 ip_hdr->saddr = htonl(0x0a0a0a10);
585 ip_hdr->daddr = htonl(0x0a0a0a20);
586
587 /* IP header checksum */
588 ip_hdr->check = 0;
589 ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
590
591 /* UDP header */
592 udp_hdr->source = htons(0x1000);
593 udp_hdr->dest = htons(0x1000);
594 udp_hdr->len = htons(UDP_PKT_SIZE);
595
596 /* UDP data */
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530597 memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530598 UDP_PKT_DATA_SIZE);
599
600 /* UDP header checksum */
601 udp_hdr->check = 0;
602 udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
603 IPPROTO_UDP, (u16 *)udp_hdr);
604}
605
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530606static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100607{
608 memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530609 PKT_SIZE);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100610}
611
612static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size)
613{
614 struct xsk_umem_info *umem;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300615 struct xsk_umem_config cfg = {
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200616 /* We recommend that you set the fill ring size >= HW RX ring size +
617 * AF_XDP RX ring size. Make sure you fill up the fill ring
618 * with buffers at regular intervals, and you will with this setting
619 * avoid allocation failures in the driver. These are usually quite
620 * expensive since drivers have not been written to assume that
621 * allocation failures are common. For regular sockets, kernel
622 * allocated memory is used that only runs out in OOM situations
623 * that should be rare.
624 */
625 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300626 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
627 .frame_size = opt_xsk_frame_size,
628 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
Kevin Laatzc543f542019-08-27 02:25:28 +0000629 .flags = opt_umem_flags
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300630 };
Magnus Karlsson661842c2019-11-07 18:47:39 +0100631 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100632
633 umem = calloc(1, sizeof(*umem));
634 if (!umem)
635 exit_with_error(errno);
636
637 ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300638 &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100639 if (ret)
640 exit_with_error(-ret);
641
Magnus Karlsson661842c2019-11-07 18:47:39 +0100642 umem->buffer = buffer;
643 return umem;
644}
645
646static void xsk_populate_fill_ring(struct xsk_umem_info *umem)
647{
648 int ret, i;
649 u32 idx;
650
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100651 ret = xsk_ring_prod__reserve(&umem->fq,
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200652 XSK_RING_PROD__DEFAULT_NUM_DESCS * 2, &idx);
653 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS * 2)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100654 exit_with_error(-ret);
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200655 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS * 2; i++)
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100656 *xsk_ring_prod__fill_addr(&umem->fq, idx++) =
657 i * opt_xsk_frame_size;
Magnus Karlssonc8a039a2020-08-28 14:51:05 +0200658 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS * 2);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100659}
660
Magnus Karlsson661842c2019-11-07 18:47:39 +0100661static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
662 bool rx, bool tx)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100663{
664 struct xsk_socket_config cfg;
665 struct xsk_socket_info *xsk;
Magnus Karlsson661842c2019-11-07 18:47:39 +0100666 struct xsk_ring_cons *rxr;
667 struct xsk_ring_prod *txr;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100668 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100669
670 xsk = calloc(1, sizeof(*xsk));
671 if (!xsk)
672 exit_with_error(errno);
673
674 xsk->umem = umem;
675 cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
676 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100677 if (opt_num_xsks > 1)
678 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
679 else
680 cfg.libbpf_flags = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100681 cfg.xdp_flags = opt_xdp_flags;
682 cfg.bind_flags = opt_xdp_bind_flags;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100683
Magnus Karlsson661842c2019-11-07 18:47:39 +0100684 rxr = rx ? &xsk->rx : NULL;
685 txr = tx ? &xsk->tx : NULL;
686 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem,
687 rxr, txr, &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100688 if (ret)
689 exit_with_error(-ret);
690
691 ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
692 if (ret)
693 exit_with_error(-ret);
694
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100695 return xsk;
696}
697
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200698static struct option long_options[] = {
699 {"rxdrop", no_argument, 0, 'r'},
700 {"txonly", no_argument, 0, 't'},
701 {"l2fwd", no_argument, 0, 'l'},
702 {"interface", required_argument, 0, 'i'},
703 {"queue", required_argument, 0, 'q'},
704 {"poll", no_argument, 0, 'p'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200705 {"xdp-skb", no_argument, 0, 'S'},
706 {"xdp-native", no_argument, 0, 'N'},
707 {"interval", required_argument, 0, 'n'},
Björn Töpel58c50ae2018-08-28 14:44:35 +0200708 {"zero-copy", no_argument, 0, 'z'},
709 {"copy", no_argument, 0, 'c'},
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300710 {"frame-size", required_argument, 0, 'f'},
Magnus Karlsson46738f72019-08-14 09:27:21 +0200711 {"no-need-wakeup", no_argument, 0, 'm'},
Kevin Laatzc543f542019-08-27 02:25:28 +0000712 {"unaligned", no_argument, 0, 'u'},
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100713 {"shared-umem", no_argument, 0, 'M'},
Andre Guedesb3133322019-11-14 08:28:47 -0800714 {"force", no_argument, 0, 'F'},
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530715 {"duration", required_argument, 0, 'd'},
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530716 {"batch-size", required_argument, 0, 'b'},
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530717 {"tx-pkt-count", required_argument, 0, 'C'},
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530718 {"tx-pkt-size", required_argument, 0, 's'},
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530719 {"tx-pkt-pattern", required_argument, 0, 'P'},
Ciara Loftusb36c3202020-07-08 07:28:34 +0000720 {"extra-stats", no_argument, 0, 'x'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200721 {0, 0, 0, 0}
722};
723
724static void usage(const char *prog)
725{
726 const char *str =
727 " Usage: %s [OPTIONS]\n"
728 " Options:\n"
729 " -r, --rxdrop Discard all incoming packets (default)\n"
730 " -t, --txonly Only send packets\n"
731 " -l, --l2fwd MAC swap L2 forwarding\n"
732 " -i, --interface=n Run on interface n\n"
733 " -q, --queue=n Use queue n (default 0)\n"
734 " -p, --poll Use poll syscall\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200735 " -S, --xdp-skb=n Use XDP skb-mod\n"
Anton Ivanov4564a8bb2019-10-07 09:26:36 +0100736 " -N, --xdp-native=n Enforce XDP native mode\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200737 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
Björn Töpel58c50ae2018-08-28 14:44:35 +0200738 " -z, --zero-copy Force zero-copy mode.\n"
739 " -c, --copy Force copy mode.\n"
Magnus Karlsson46738f72019-08-14 09:27:21 +0200740 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
Kevin Laatzc543f542019-08-27 02:25:28 +0000741 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
742 " -u, --unaligned Enable unaligned chunk placement\n"
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100743 " -M, --shared-umem Enable XDP_SHARED_UMEM\n"
Andre Guedesb3133322019-11-14 08:28:47 -0800744 " -F, --force Force loading the XDP prog\n"
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530745 " -d, --duration=n Duration in secs to run command.\n"
746 " Default: forever.\n"
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530747 " -b, --batch-size=n Batch size for sending or receiving\n"
748 " packets. Default: %d\n"
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530749 " -C, --tx-pkt-count=n Number of packets to send.\n"
750 " Default: Continuous packets.\n"
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530751 " -s, --tx-pkt-size=n Transmit packet size.\n"
752 " (Default: %d bytes)\n"
753 " Min size: %d, Max size %d.\n"
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530754 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
Ciara Loftusb36c3202020-07-08 07:28:34 +0000755 " -x, --extra-stats Display extra statistics.\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200756 "\n";
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530757 fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530758 opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530759 XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530760
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200761 exit(EXIT_FAILURE);
762}
763
764static void parse_command_line(int argc, char **argv)
765{
766 int option_index, c;
767
768 opterr = 0;
769
770 for (;;) {
Ciara Loftusb36c3202020-07-08 07:28:34 +0000771 c = getopt_long(argc, argv, "Frtli:q:pSNn:czf:muMd:b:C:s:P:x",
Magnus Karlsson46738f72019-08-14 09:27:21 +0200772 long_options, &option_index);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200773 if (c == -1)
774 break;
775
776 switch (c) {
777 case 'r':
778 opt_bench = BENCH_RXDROP;
779 break;
780 case 't':
781 opt_bench = BENCH_TXONLY;
782 break;
783 case 'l':
784 opt_bench = BENCH_L2FWD;
785 break;
786 case 'i':
787 opt_if = optarg;
788 break;
789 case 'q':
790 opt_queue = atoi(optarg);
791 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200792 case 'p':
793 opt_poll = 1;
794 break;
795 case 'S':
796 opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
Björn Töpel9f5232c2018-06-04 14:06:01 +0200797 opt_xdp_bind_flags |= XDP_COPY;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200798 break;
799 case 'N':
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100800 /* default, set below */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200801 break;
802 case 'n':
803 opt_interval = atoi(optarg);
804 break;
Björn Töpel58c50ae2018-08-28 14:44:35 +0200805 case 'z':
806 opt_xdp_bind_flags |= XDP_ZEROCOPY;
807 break;
808 case 'c':
809 opt_xdp_bind_flags |= XDP_COPY;
810 break;
Kevin Laatzc543f542019-08-27 02:25:28 +0000811 case 'u':
812 opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
813 opt_unaligned_chunks = 1;
Kevin Laatz3945b372019-08-27 02:25:30 +0000814 opt_mmap_flags = MAP_HUGETLB;
Kevin Laatzc543f542019-08-27 02:25:28 +0000815 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100816 case 'F':
817 opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
818 break;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300819 case 'f':
820 opt_xsk_frame_size = atoi(optarg);
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100821 break;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200822 case 'm':
823 opt_need_wakeup = false;
824 opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300825 break;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100826 case 'M':
827 opt_num_xsks = MAX_SOCKS;
828 break;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530829 case 'd':
830 opt_duration = atoi(optarg);
831 opt_duration *= 1000000000;
832 break;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530833 case 'b':
834 opt_batch_size = atoi(optarg);
835 break;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530836 case 'C':
837 opt_pkt_count = atoi(optarg);
838 break;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530839 case 's':
840 opt_pkt_size = atoi(optarg);
841 if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
842 opt_pkt_size < MIN_PKT_SIZE) {
843 fprintf(stderr,
844 "ERROR: Invalid frame size %d\n",
845 opt_pkt_size);
846 usage(basename(argv[0]));
847 }
848 break;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530849 case 'P':
850 opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
851 break;
Ciara Loftusb36c3202020-07-08 07:28:34 +0000852 case 'x':
853 opt_extra_stats = 1;
854 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200855 default:
856 usage(basename(argv[0]));
857 }
858 }
859
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100860 if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
861 opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
862
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200863 opt_ifindex = if_nametoindex(opt_if);
864 if (!opt_ifindex) {
865 fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
866 opt_if);
867 usage(basename(argv[0]));
868 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100869
Kevin Laatzc543f542019-08-27 02:25:28 +0000870 if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
871 !opt_unaligned_chunks) {
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300872 fprintf(stderr, "--frame-size=%d is not a power of two\n",
873 opt_xsk_frame_size);
874 usage(basename(argv[0]));
875 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200876}
877
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100878static void kick_tx(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200879{
880 int ret;
881
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100882 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
Maciej Fijalkowski8ed47e12020-02-05 05:58:34 +0100883 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
884 errno == EBUSY || errno == ENETDOWN)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200885 return;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100886 exit_with_error(errno);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200887}
888
Magnus Karlsson46738f72019-08-14 09:27:21 +0200889static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk,
890 struct pollfd *fds)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200891{
Kevin Laatz03895e62019-08-27 02:25:29 +0000892 struct xsk_umem_info *umem = xsk->umem;
Yonghong Songb74e21a2019-02-28 22:19:41 -0800893 u32 idx_cq = 0, idx_fq = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200894 unsigned int rcvd;
895 size_t ndescs;
896
897 if (!xsk->outstanding_tx)
898 return;
899
Magnus Karlsson3131cf62020-09-10 10:31:04 +0200900 /* In copy mode, Tx is driven by a syscall so we need to use e.g. sendto() to
901 * really send the packets. In zero-copy mode we do not have to do this, since Tx
902 * is driven by the NAPI loop. So as an optimization, we do not have to call
903 * sendto() all the time in zero-copy mode for l2fwd.
904 */
905 if (opt_xdp_bind_flags & XDP_COPY)
906 kick_tx(xsk);
907
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530908 ndescs = (xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100909 xsk->outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200910
911 /* re-add completed Tx buffers */
Kevin Laatz03895e62019-08-27 02:25:29 +0000912 rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200913 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100914 unsigned int i;
915 int ret;
916
Kevin Laatz03895e62019-08-27 02:25:29 +0000917 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100918 while (ret != rcvd) {
919 if (ret < 0)
920 exit_with_error(-ret);
Kevin Laatz03895e62019-08-27 02:25:29 +0000921 if (xsk_ring_prod__needs_wakeup(&umem->fq))
Magnus Karlsson46738f72019-08-14 09:27:21 +0200922 ret = poll(fds, num_socks, opt_timeout);
Kevin Laatz03895e62019-08-27 02:25:29 +0000923 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100924 }
Kevin Laatz03895e62019-08-27 02:25:29 +0000925
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100926 for (i = 0; i < rcvd; i++)
Kevin Laatz03895e62019-08-27 02:25:29 +0000927 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
928 *xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100929
930 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
931 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200932 xsk->outstanding_tx -= rcvd;
933 xsk->tx_npkts += rcvd;
934 }
935}
936
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530937static inline void complete_tx_only(struct xsk_socket_info *xsk,
938 int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200939{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200940 unsigned int rcvd;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100941 u32 idx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200942
943 if (!xsk->outstanding_tx)
944 return;
945
Magnus Karlsson46738f72019-08-14 09:27:21 +0200946 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx))
947 kick_tx(xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200948
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530949 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200950 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100951 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200952 xsk->outstanding_tx -= rcvd;
953 xsk->tx_npkts += rcvd;
954 }
955}
956
Magnus Karlsson46738f72019-08-14 09:27:21 +0200957static void rx_drop(struct xsk_socket_info *xsk, struct pollfd *fds)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200958{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200959 unsigned int rcvd, i;
Yonghong Songb74e21a2019-02-28 22:19:41 -0800960 u32 idx_rx = 0, idx_fq = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100961 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200962
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530963 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +0200964 if (!rcvd) {
965 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
966 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200967 return;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200968 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200969
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100970 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
971 while (ret != rcvd) {
972 if (ret < 0)
973 exit_with_error(-ret);
Magnus Karlsson46738f72019-08-14 09:27:21 +0200974 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
975 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100976 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200977 }
978
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100979 for (i = 0; i < rcvd; i++) {
980 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
981 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Kevin Laatz03895e62019-08-27 02:25:29 +0000982 u64 orig = xsk_umem__extract_addr(addr);
983
984 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100985 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200986
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100987 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +0000988 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100989 }
990
991 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
992 xsk_ring_cons__release(&xsk->rx, rcvd);
993 xsk->rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200994}
995
996static void rx_drop_all(void)
997{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100998 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +0200999 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001000
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001001 for (i = 0; i < num_socks; i++) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001002 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001003 fds[i].events = POLLIN;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001004 }
1005
1006 for (;;) {
1007 if (opt_poll) {
Magnus Karlsson46738f72019-08-14 09:27:21 +02001008 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001009 if (ret <= 0)
1010 continue;
1011 }
1012
1013 for (i = 0; i < num_socks; i++)
Magnus Karlsson46738f72019-08-14 09:27:21 +02001014 rx_drop(xsks[i], fds);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301015
1016 if (benchmark_done)
1017 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001018 }
1019}
1020
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001021static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001022{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001023 u32 idx;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301024 unsigned int i;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001025
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301026 while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
1027 batch_size) {
1028 complete_tx_only(xsk, batch_size);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001029 }
1030
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301031 for (i = 0; i < batch_size; i++) {
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301032 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
1033 idx + i);
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001034 tx_desc->addr = (*frame_nb + i) << XSK_UMEM__DEFAULT_FRAME_SHIFT;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301035 tx_desc->len = PKT_SIZE;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301036 }
1037
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301038 xsk_ring_prod__submit(&xsk->tx, batch_size);
1039 xsk->outstanding_tx += batch_size;
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001040 *frame_nb += batch_size;
1041 *frame_nb %= NUM_FRAMES;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301042 complete_tx_only(xsk, batch_size);
1043}
1044
1045static inline int get_batch_size(int pkt_cnt)
1046{
1047 if (!opt_pkt_count)
1048 return opt_batch_size;
1049
1050 if (pkt_cnt + opt_batch_size <= opt_pkt_count)
1051 return opt_batch_size;
1052
1053 return opt_pkt_count - pkt_cnt;
1054}
1055
1056static void complete_tx_only_all(void)
1057{
1058 bool pending;
1059 int i;
1060
1061 do {
1062 pending = false;
1063 for (i = 0; i < num_socks; i++) {
1064 if (xsks[i]->outstanding_tx) {
1065 complete_tx_only(xsks[i], opt_batch_size);
1066 pending = !!xsks[i]->outstanding_tx;
1067 }
1068 }
1069 } while (pending);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001070}
1071
1072static void tx_only_all(void)
1073{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001074 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001075 u32 frame_nb[MAX_SOCKS] = {};
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301076 int pkt_cnt = 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001077 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001078
Magnus Karlsson46738f72019-08-14 09:27:21 +02001079 for (i = 0; i < num_socks; i++) {
1080 fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
1081 fds[0].events = POLLOUT;
1082 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001083
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301084 while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
1085 int batch_size = get_batch_size(pkt_cnt);
1086
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001087 if (opt_poll) {
Magnus Karlsson46738f72019-08-14 09:27:21 +02001088 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001089 if (ret <= 0)
1090 continue;
1091
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001092 if (!(fds[0].revents & POLLOUT))
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001093 continue;
1094 }
1095
Magnus Karlsson46738f72019-08-14 09:27:21 +02001096 for (i = 0; i < num_socks; i++)
Weqaar Janjuab69e56c2020-08-29 00:17:17 +08001097 tx_only(xsks[i], &frame_nb[i], batch_size);
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301098
1099 pkt_cnt += batch_size;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301100
1101 if (benchmark_done)
1102 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001103 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301104
1105 if (opt_pkt_count)
1106 complete_tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001107}
1108
Magnus Karlsson46738f72019-08-14 09:27:21 +02001109static void l2fwd(struct xsk_socket_info *xsk, struct pollfd *fds)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001110{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001111 unsigned int rcvd, i;
1112 u32 idx_rx = 0, idx_tx = 0;
1113 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001114
Magnus Karlsson46738f72019-08-14 09:27:21 +02001115 complete_tx_l2fwd(xsk, fds);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001116
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301117 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001118 if (!rcvd) {
1119 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
1120 ret = poll(fds, num_socks, opt_timeout);
1121 return;
1122 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001123
Magnus Karlsson46738f72019-08-14 09:27:21 +02001124 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1125 while (ret != rcvd) {
1126 if (ret < 0)
1127 exit_with_error(-ret);
1128 if (xsk_ring_prod__needs_wakeup(&xsk->tx))
1129 kick_tx(xsk);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001130 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001131 }
1132
1133 for (i = 0; i < rcvd; i++) {
1134 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1135 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Ciara Loftus5a712e12019-09-13 10:39:48 +00001136 u64 orig = addr;
Kevin Laatz03895e62019-08-27 02:25:29 +00001137
1138 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001139 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1140
1141 swap_mac_addresses(pkt);
1142
1143 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001144 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = orig;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001145 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
1146 }
1147
1148 xsk_ring_prod__submit(&xsk->tx, rcvd);
1149 xsk_ring_cons__release(&xsk->rx, rcvd);
1150
1151 xsk->rx_npkts += rcvd;
1152 xsk->outstanding_tx += rcvd;
1153}
1154
1155static void l2fwd_all(void)
1156{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001157 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001158 int i, ret;
1159
Magnus Karlsson46738f72019-08-14 09:27:21 +02001160 for (i = 0; i < num_socks; i++) {
1161 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1162 fds[i].events = POLLOUT | POLLIN;
1163 }
1164
1165 for (;;) {
1166 if (opt_poll) {
1167 ret = poll(fds, num_socks, opt_timeout);
1168 if (ret <= 0)
1169 continue;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001170 }
1171
Magnus Karlsson46738f72019-08-14 09:27:21 +02001172 for (i = 0; i < num_socks; i++)
1173 l2fwd(xsks[i], fds);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301174
1175 if (benchmark_done)
1176 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001177 }
1178}
1179
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001180static void load_xdp_program(char **argv, struct bpf_object **obj)
1181{
1182 struct bpf_prog_load_attr prog_load_attr = {
1183 .prog_type = BPF_PROG_TYPE_XDP,
1184 };
1185 char xdp_filename[256];
1186 int prog_fd;
1187
1188 snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
1189 prog_load_attr.file = xdp_filename;
1190
1191 if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
1192 exit(EXIT_FAILURE);
1193 if (prog_fd < 0) {
1194 fprintf(stderr, "ERROR: no program found: %s\n",
1195 strerror(prog_fd));
1196 exit(EXIT_FAILURE);
1197 }
1198
1199 if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
1200 fprintf(stderr, "ERROR: link set xdp fd failed\n");
1201 exit(EXIT_FAILURE);
1202 }
1203}
1204
1205static void enter_xsks_into_map(struct bpf_object *obj)
1206{
1207 struct bpf_map *map;
1208 int i, xsks_map;
1209
1210 map = bpf_object__find_map_by_name(obj, "xsks_map");
1211 xsks_map = bpf_map__fd(map);
1212 if (xsks_map < 0) {
1213 fprintf(stderr, "ERROR: no xsks map found: %s\n",
1214 strerror(xsks_map));
1215 exit(EXIT_FAILURE);
1216 }
1217
1218 for (i = 0; i < num_socks; i++) {
1219 int fd = xsk_socket__fd(xsks[i]->xsk);
1220 int key, ret;
1221
1222 key = i;
1223 ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
1224 if (ret) {
1225 fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
1226 exit(EXIT_FAILURE);
1227 }
1228 }
1229}
1230
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001231int main(int argc, char **argv)
1232{
1233 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Magnus Karlsson661842c2019-11-07 18:47:39 +01001234 bool rx = false, tx = false;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001235 struct xsk_umem_info *umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001236 struct bpf_object *obj;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001237 pthread_t pt;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001238 int i, ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001239 void *bufs;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001240
1241 parse_command_line(argc, argv);
1242
1243 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
1244 fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1245 strerror(errno));
1246 exit(EXIT_FAILURE);
1247 }
1248
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001249 if (opt_num_xsks > 1)
1250 load_xdp_program(argv, &obj);
1251
Kevin Laatz3945b372019-08-27 02:25:30 +00001252 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1253 bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size,
1254 PROT_READ | PROT_WRITE,
1255 MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
1256 if (bufs == MAP_FAILED) {
1257 printf("ERROR: mmap failed\n");
1258 exit(EXIT_FAILURE);
1259 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001260
1261 /* Create sockets... */
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001262 umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size);
Magnus Karlsson661842c2019-11-07 18:47:39 +01001263 if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
1264 rx = true;
1265 xsk_populate_fill_ring(umem);
1266 }
1267 if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
1268 tx = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001269 for (i = 0; i < opt_num_xsks; i++)
Magnus Karlsson661842c2019-11-07 18:47:39 +01001270 xsks[num_socks++] = xsk_configure_socket(umem, rx, tx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001271
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301272 if (opt_bench == BENCH_TXONLY) {
1273 gen_eth_hdr_data();
1274
Magnus Karlsson661842c2019-11-07 18:47:39 +01001275 for (i = 0; i < NUM_FRAMES; i++)
1276 gen_eth_frame(umem, i * opt_xsk_frame_size);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301277 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001278
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001279 if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
1280 enter_xsks_into_map(obj);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001281
1282 signal(SIGINT, int_exit);
1283 signal(SIGTERM, int_exit);
1284 signal(SIGABRT, int_exit);
1285
1286 setlocale(LC_ALL, "");
1287
1288 ret = pthread_create(&pt, NULL, poller, NULL);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001289 if (ret)
1290 exit_with_error(ret);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001291
1292 prev_time = get_nsecs();
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301293 start_time = prev_time;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001294
1295 if (opt_bench == BENCH_RXDROP)
1296 rx_drop_all();
1297 else if (opt_bench == BENCH_TXONLY)
Magnus Karlsson46738f72019-08-14 09:27:21 +02001298 tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001299 else
Magnus Karlsson46738f72019-08-14 09:27:21 +02001300 l2fwd_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001301
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301302 benchmark_done = true;
1303
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301304 pthread_join(pt, NULL);
1305
Jay Jayatheerthan69525582019-12-20 14:25:26 +05301306 xdpsock_cleanup();
1307
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001308 return 0;
1309}