blob: c91e91362a0c644dcead128fcd43abb43d1d8f73 [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;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020080static int opt_poll;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020081static int opt_interval = 1;
Magnus Karlsson46738f72019-08-14 09:27:21 +020082static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
Kevin Laatzc543f542019-08-27 02:25:28 +000083static u32 opt_umem_flags;
84static int opt_unaligned_chunks;
Kevin Laatz3945b372019-08-27 02:25:30 +000085static int opt_mmap_flags;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +030086static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
Magnus Karlsson46738f72019-08-14 09:27:21 +020087static int opt_timeout = 1000;
88static bool opt_need_wakeup = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +010089static u32 opt_num_xsks = 1;
90static u32 prog_id;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020091
Magnus Karlsson248c7f92019-02-21 10:21:27 +010092struct xsk_umem_info {
93 struct xsk_ring_prod fq;
94 struct xsk_ring_cons cq;
95 struct xsk_umem *umem;
96 void *buffer;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +020097};
98
Magnus Karlsson248c7f92019-02-21 10:21:27 +010099struct xsk_socket_info {
100 struct xsk_ring_cons rx;
101 struct xsk_ring_prod tx;
102 struct xsk_umem_info *umem;
103 struct xsk_socket *xsk;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200104 unsigned long rx_npkts;
105 unsigned long tx_npkts;
106 unsigned long prev_rx_npkts;
107 unsigned long prev_tx_npkts;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100108 u32 outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200109};
110
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200111static int num_socks;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100112struct xsk_socket_info *xsks[MAX_SOCKS];
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200113
114static unsigned long get_nsecs(void)
115{
116 struct timespec ts;
117
118 clock_gettime(CLOCK_MONOTONIC, &ts);
119 return ts.tv_sec * 1000000000UL + ts.tv_nsec;
120}
121
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200122static void print_benchmark(bool running)
123{
124 const char *bench_str = "INVALID";
125
126 if (opt_bench == BENCH_RXDROP)
127 bench_str = "rxdrop";
128 else if (opt_bench == BENCH_TXONLY)
129 bench_str = "txonly";
130 else if (opt_bench == BENCH_L2FWD)
131 bench_str = "l2fwd";
132
133 printf("%s:%d %s ", opt_if, opt_queue, bench_str);
134 if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
135 printf("xdp-skb ");
136 else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
137 printf("xdp-drv ");
138 else
139 printf(" ");
140
141 if (opt_poll)
142 printf("poll() ");
143
144 if (running) {
145 printf("running...");
146 fflush(stdout);
147 }
148}
149
150static void dump_stats(void)
151{
152 unsigned long now = get_nsecs();
153 long dt = now - prev_time;
154 int i;
155
156 prev_time = now;
157
Prashant Bhole11c3f512018-08-31 10:00:49 +0900158 for (i = 0; i < num_socks && xsks[i]; i++) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200159 char *fmt = "%-15s %'-11.0f %'-11lu\n";
160 double rx_pps, tx_pps;
161
162 rx_pps = (xsks[i]->rx_npkts - xsks[i]->prev_rx_npkts) *
163 1000000000. / dt;
164 tx_pps = (xsks[i]->tx_npkts - xsks[i]->prev_tx_npkts) *
165 1000000000. / dt;
166
167 printf("\n sock%d@", i);
168 print_benchmark(false);
169 printf("\n");
170
171 printf("%-15s %-11s %-11s %-11.2f\n", "", "pps", "pkts",
172 dt / 1000000000.);
173 printf(fmt, "rx", rx_pps, xsks[i]->rx_npkts);
174 printf(fmt, "tx", tx_pps, xsks[i]->tx_npkts);
175
176 xsks[i]->prev_rx_npkts = xsks[i]->rx_npkts;
177 xsks[i]->prev_tx_npkts = xsks[i]->tx_npkts;
178 }
179}
180
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530181static bool is_benchmark_done(void)
182{
183 if (opt_duration > 0) {
184 unsigned long dt = (get_nsecs() - start_time);
185
186 if (dt >= opt_duration)
187 benchmark_done = true;
188 }
189 return benchmark_done;
190}
191
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200192static void *poller(void *arg)
193{
194 (void)arg;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530195 while (!is_benchmark_done()) {
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200196 sleep(opt_interval);
197 dump_stats();
198 }
199
200 return NULL;
201}
202
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100203static void remove_xdp_program(void)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200204{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100205 u32 curr_prog_id = 0;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100206
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100207 if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
208 printf("bpf_get_link_xdp_id failed\n");
209 exit(EXIT_FAILURE);
210 }
211 if (prog_id == curr_prog_id)
212 bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
213 else if (!curr_prog_id)
214 printf("couldn't find a prog id on a given interface\n");
215 else
216 printf("program on interface changed, not removing\n");
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100217}
218
219static void int_exit(int sig)
220{
Jay Jayatheerthan69525582019-12-20 14:25:26 +0530221 benchmark_done = true;
222}
223
224static void xdpsock_cleanup(void)
225{
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100226 struct xsk_umem *umem = xsks[0]->umem->umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100227 int i;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100228
229 dump_stats();
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100230 for (i = 0; i < num_socks; i++)
231 xsk_socket__delete(xsks[i]->xsk);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100232 (void)xsk_umem__delete(umem);
233 remove_xdp_program();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200234}
235
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100236static void __exit_with_error(int error, const char *file, const char *func,
237 int line)
238{
239 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
240 line, error, strerror(error));
241 dump_stats();
242 remove_xdp_program();
243 exit(EXIT_FAILURE);
244}
245
246#define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, \
247 __LINE__)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100248static void swap_mac_addresses(void *data)
249{
250 struct ether_header *eth = (struct ether_header *)data;
251 struct ether_addr *src_addr = (struct ether_addr *)&eth->ether_shost;
252 struct ether_addr *dst_addr = (struct ether_addr *)&eth->ether_dhost;
253 struct ether_addr tmp;
254
255 tmp = *src_addr;
256 *src_addr = *dst_addr;
257 *dst_addr = tmp;
258}
259
260static void hex_dump(void *pkt, size_t length, u64 addr)
261{
262 const unsigned char *address = (unsigned char *)pkt;
263 const unsigned char *line = address;
264 size_t line_size = 32;
265 unsigned char c;
266 char buf[32];
267 int i = 0;
268
269 if (!DEBUG_HEXDUMP)
270 return;
271
272 sprintf(buf, "addr=%llu", addr);
273 printf("length = %zu\n", length);
274 printf("%s | ", buf);
275 while (length-- > 0) {
276 printf("%02X ", *address++);
277 if (!(++i % line_size) || (length == 0 && i % line_size)) {
278 if (length == 0) {
279 while (i++ % line_size)
280 printf("__ ");
281 }
282 printf(" | "); /* right close */
283 while (line < address) {
284 c = *line++;
285 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
286 }
287 printf("\n");
288 if (length > 0)
289 printf("%s | ", buf);
290 }
291 }
292 printf("\n");
293}
294
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530295static void *memset32_htonl(void *dest, u32 val, u32 size)
296{
297 u32 *ptr = (u32 *)dest;
298 int i;
299
300 val = htonl(val);
301
302 for (i = 0; i < (size & (~0x3)); i += 4)
303 ptr[i >> 2] = val;
304
305 for (; i < size; i++)
306 ((char *)dest)[i] = ((char *)&val)[i & 3];
307
308 return dest;
309}
310
311/*
312 * This function code has been taken from
313 * Linux kernel lib/checksum.c
314 */
315static inline unsigned short from32to16(unsigned int x)
316{
317 /* add up 16-bit and 16-bit for 16+c bit */
318 x = (x & 0xffff) + (x >> 16);
319 /* add up carry.. */
320 x = (x & 0xffff) + (x >> 16);
321 return x;
322}
323
324/*
325 * This function code has been taken from
326 * Linux kernel lib/checksum.c
327 */
328static unsigned int do_csum(const unsigned char *buff, int len)
329{
330 unsigned int result = 0;
331 int odd;
332
333 if (len <= 0)
334 goto out;
335 odd = 1 & (unsigned long)buff;
336 if (odd) {
337#ifdef __LITTLE_ENDIAN
338 result += (*buff << 8);
339#else
340 result = *buff;
341#endif
342 len--;
343 buff++;
344 }
345 if (len >= 2) {
346 if (2 & (unsigned long)buff) {
347 result += *(unsigned short *)buff;
348 len -= 2;
349 buff += 2;
350 }
351 if (len >= 4) {
352 const unsigned char *end = buff +
353 ((unsigned int)len & ~3);
354 unsigned int carry = 0;
355
356 do {
357 unsigned int w = *(unsigned int *)buff;
358
359 buff += 4;
360 result += carry;
361 result += w;
362 carry = (w > result);
363 } while (buff < end);
364 result += carry;
365 result = (result & 0xffff) + (result >> 16);
366 }
367 if (len & 2) {
368 result += *(unsigned short *)buff;
369 buff += 2;
370 }
371 }
372 if (len & 1)
373#ifdef __LITTLE_ENDIAN
374 result += *buff;
375#else
376 result += (*buff << 8);
377#endif
378 result = from32to16(result);
379 if (odd)
380 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
381out:
382 return result;
383}
384
385__sum16 ip_fast_csum(const void *iph, unsigned int ihl);
386
387/*
388 * This is a version of ip_compute_csum() optimized for IP headers,
389 * which always checksum on 4 octet boundaries.
390 * This function code has been taken from
391 * Linux kernel lib/checksum.c
392 */
393__sum16 ip_fast_csum(const void *iph, unsigned int ihl)
394{
395 return (__force __sum16)~do_csum(iph, ihl * 4);
396}
397
398/*
399 * Fold a partial checksum
400 * This function code has been taken from
401 * Linux kernel include/asm-generic/checksum.h
402 */
403static inline __sum16 csum_fold(__wsum csum)
404{
405 u32 sum = (__force u32)csum;
406
407 sum = (sum & 0xffff) + (sum >> 16);
408 sum = (sum & 0xffff) + (sum >> 16);
409 return (__force __sum16)~sum;
410}
411
412/*
413 * This function code has been taken from
414 * Linux kernel lib/checksum.c
415 */
416static inline u32 from64to32(u64 x)
417{
418 /* add up 32-bit and 32-bit for 32+c bit */
419 x = (x & 0xffffffff) + (x >> 32);
420 /* add up carry.. */
421 x = (x & 0xffffffff) + (x >> 32);
422 return (u32)x;
423}
424
425__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
426 __u32 len, __u8 proto, __wsum sum);
427
428/*
429 * This function code has been taken from
430 * Linux kernel lib/checksum.c
431 */
432__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
433 __u32 len, __u8 proto, __wsum sum)
434{
435 unsigned long long s = (__force u32)sum;
436
437 s += (__force u32)saddr;
438 s += (__force u32)daddr;
439#ifdef __BIG_ENDIAN__
440 s += proto + len;
441#else
442 s += (proto + len) << 8;
443#endif
444 return (__force __wsum)from64to32(s);
445}
446
447/*
448 * This function has been taken from
449 * Linux kernel include/asm-generic/checksum.h
450 */
451static inline __sum16
452csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
453 __u8 proto, __wsum sum)
454{
455 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
456}
457
458static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
459 u8 proto, u16 *udp_pkt)
460{
461 u32 csum = 0;
462 u32 cnt = 0;
463
464 /* udp hdr and data */
465 for (; cnt < len; cnt += 2)
466 csum += udp_pkt[cnt >> 1];
467
468 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
469}
470
471#define ETH_FCS_SIZE 4
472
473#define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
474 sizeof(struct udphdr))
475
476#define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
477#define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
478#define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
479#define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
480
481static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
482
483static void gen_eth_hdr_data(void)
484{
485 struct udphdr *udp_hdr = (struct udphdr *)(pkt_data +
486 sizeof(struct ethhdr) +
487 sizeof(struct iphdr));
488 struct iphdr *ip_hdr = (struct iphdr *)(pkt_data +
489 sizeof(struct ethhdr));
490 struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
491
492 /* ethernet header */
493 memcpy(eth_hdr->h_dest, "\x3c\xfd\xfe\x9e\x7f\x71", ETH_ALEN);
494 memcpy(eth_hdr->h_source, "\xec\xb1\xd7\x98\x3a\xc0", ETH_ALEN);
495 eth_hdr->h_proto = htons(ETH_P_IP);
496
497 /* IP header */
498 ip_hdr->version = IPVERSION;
499 ip_hdr->ihl = 0x5; /* 20 byte header */
500 ip_hdr->tos = 0x0;
501 ip_hdr->tot_len = htons(IP_PKT_SIZE);
502 ip_hdr->id = 0;
503 ip_hdr->frag_off = 0;
504 ip_hdr->ttl = IPDEFTTL;
505 ip_hdr->protocol = IPPROTO_UDP;
506 ip_hdr->saddr = htonl(0x0a0a0a10);
507 ip_hdr->daddr = htonl(0x0a0a0a20);
508
509 /* IP header checksum */
510 ip_hdr->check = 0;
511 ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
512
513 /* UDP header */
514 udp_hdr->source = htons(0x1000);
515 udp_hdr->dest = htons(0x1000);
516 udp_hdr->len = htons(UDP_PKT_SIZE);
517
518 /* UDP data */
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530519 memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530520 UDP_PKT_DATA_SIZE);
521
522 /* UDP header checksum */
523 udp_hdr->check = 0;
524 udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
525 IPPROTO_UDP, (u16 *)udp_hdr);
526}
527
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530528static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100529{
530 memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530531 PKT_SIZE);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100532}
533
534static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size)
535{
536 struct xsk_umem_info *umem;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300537 struct xsk_umem_config cfg = {
538 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
539 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
540 .frame_size = opt_xsk_frame_size,
541 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
Kevin Laatzc543f542019-08-27 02:25:28 +0000542 .flags = opt_umem_flags
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300543 };
Magnus Karlsson661842c2019-11-07 18:47:39 +0100544 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100545
546 umem = calloc(1, sizeof(*umem));
547 if (!umem)
548 exit_with_error(errno);
549
550 ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300551 &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100552 if (ret)
553 exit_with_error(-ret);
554
Magnus Karlsson661842c2019-11-07 18:47:39 +0100555 umem->buffer = buffer;
556 return umem;
557}
558
559static void xsk_populate_fill_ring(struct xsk_umem_info *umem)
560{
561 int ret, i;
562 u32 idx;
563
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100564 ret = xsk_ring_prod__reserve(&umem->fq,
565 XSK_RING_PROD__DEFAULT_NUM_DESCS, &idx);
566 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS)
567 exit_with_error(-ret);
568 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS; i++)
569 *xsk_ring_prod__fill_addr(&umem->fq, idx++) =
570 i * opt_xsk_frame_size;
571 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100572}
573
Magnus Karlsson661842c2019-11-07 18:47:39 +0100574static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
575 bool rx, bool tx)
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100576{
577 struct xsk_socket_config cfg;
578 struct xsk_socket_info *xsk;
Magnus Karlsson661842c2019-11-07 18:47:39 +0100579 struct xsk_ring_cons *rxr;
580 struct xsk_ring_prod *txr;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100581 int ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100582
583 xsk = calloc(1, sizeof(*xsk));
584 if (!xsk)
585 exit_with_error(errno);
586
587 xsk->umem = umem;
588 cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
589 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100590 if (opt_num_xsks > 1)
591 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
592 else
593 cfg.libbpf_flags = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100594 cfg.xdp_flags = opt_xdp_flags;
595 cfg.bind_flags = opt_xdp_bind_flags;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100596
Magnus Karlsson661842c2019-11-07 18:47:39 +0100597 rxr = rx ? &xsk->rx : NULL;
598 txr = tx ? &xsk->tx : NULL;
599 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem,
600 rxr, txr, &cfg);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100601 if (ret)
602 exit_with_error(-ret);
603
604 ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
605 if (ret)
606 exit_with_error(-ret);
607
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100608 return xsk;
609}
610
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200611static struct option long_options[] = {
612 {"rxdrop", no_argument, 0, 'r'},
613 {"txonly", no_argument, 0, 't'},
614 {"l2fwd", no_argument, 0, 'l'},
615 {"interface", required_argument, 0, 'i'},
616 {"queue", required_argument, 0, 'q'},
617 {"poll", no_argument, 0, 'p'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200618 {"xdp-skb", no_argument, 0, 'S'},
619 {"xdp-native", no_argument, 0, 'N'},
620 {"interval", required_argument, 0, 'n'},
Björn Töpel58c50ae2018-08-28 14:44:35 +0200621 {"zero-copy", no_argument, 0, 'z'},
622 {"copy", no_argument, 0, 'c'},
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300623 {"frame-size", required_argument, 0, 'f'},
Magnus Karlsson46738f72019-08-14 09:27:21 +0200624 {"no-need-wakeup", no_argument, 0, 'm'},
Kevin Laatzc543f542019-08-27 02:25:28 +0000625 {"unaligned", no_argument, 0, 'u'},
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100626 {"shared-umem", no_argument, 0, 'M'},
Andre Guedesb3133322019-11-14 08:28:47 -0800627 {"force", no_argument, 0, 'F'},
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530628 {"duration", required_argument, 0, 'd'},
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530629 {"batch-size", required_argument, 0, 'b'},
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530630 {"tx-pkt-count", required_argument, 0, 'C'},
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530631 {"tx-pkt-size", required_argument, 0, 's'},
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530632 {"tx-pkt-pattern", required_argument, 0, 'P'},
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200633 {0, 0, 0, 0}
634};
635
636static void usage(const char *prog)
637{
638 const char *str =
639 " Usage: %s [OPTIONS]\n"
640 " Options:\n"
641 " -r, --rxdrop Discard all incoming packets (default)\n"
642 " -t, --txonly Only send packets\n"
643 " -l, --l2fwd MAC swap L2 forwarding\n"
644 " -i, --interface=n Run on interface n\n"
645 " -q, --queue=n Use queue n (default 0)\n"
646 " -p, --poll Use poll syscall\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200647 " -S, --xdp-skb=n Use XDP skb-mod\n"
Anton Ivanov4564a8bb2019-10-07 09:26:36 +0100648 " -N, --xdp-native=n Enforce XDP native mode\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200649 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
Björn Töpel58c50ae2018-08-28 14:44:35 +0200650 " -z, --zero-copy Force zero-copy mode.\n"
651 " -c, --copy Force copy mode.\n"
Magnus Karlsson46738f72019-08-14 09:27:21 +0200652 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
Kevin Laatzc543f542019-08-27 02:25:28 +0000653 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
654 " -u, --unaligned Enable unaligned chunk placement\n"
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100655 " -M, --shared-umem Enable XDP_SHARED_UMEM\n"
Andre Guedesb3133322019-11-14 08:28:47 -0800656 " -F, --force Force loading the XDP prog\n"
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530657 " -d, --duration=n Duration in secs to run command.\n"
658 " Default: forever.\n"
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530659 " -b, --batch-size=n Batch size for sending or receiving\n"
660 " packets. Default: %d\n"
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530661 " -C, --tx-pkt-count=n Number of packets to send.\n"
662 " Default: Continuous packets.\n"
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530663 " -s, --tx-pkt-size=n Transmit packet size.\n"
664 " (Default: %d bytes)\n"
665 " Min size: %d, Max size %d.\n"
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530666 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200667 "\n";
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530668 fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE,
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530669 opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530670 XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530671
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200672 exit(EXIT_FAILURE);
673}
674
675static void parse_command_line(int argc, char **argv)
676{
677 int option_index, c;
678
679 opterr = 0;
680
681 for (;;) {
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530682 c = getopt_long(argc, argv, "Frtli:q:pSNn:czf:muMd:b:C:s:P:",
Magnus Karlsson46738f72019-08-14 09:27:21 +0200683 long_options, &option_index);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200684 if (c == -1)
685 break;
686
687 switch (c) {
688 case 'r':
689 opt_bench = BENCH_RXDROP;
690 break;
691 case 't':
692 opt_bench = BENCH_TXONLY;
693 break;
694 case 'l':
695 opt_bench = BENCH_L2FWD;
696 break;
697 case 'i':
698 opt_if = optarg;
699 break;
700 case 'q':
701 opt_queue = atoi(optarg);
702 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200703 case 'p':
704 opt_poll = 1;
705 break;
706 case 'S':
707 opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
Björn Töpel9f5232c2018-06-04 14:06:01 +0200708 opt_xdp_bind_flags |= XDP_COPY;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200709 break;
710 case 'N':
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100711 /* default, set below */
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200712 break;
713 case 'n':
714 opt_interval = atoi(optarg);
715 break;
Björn Töpel58c50ae2018-08-28 14:44:35 +0200716 case 'z':
717 opt_xdp_bind_flags |= XDP_ZEROCOPY;
718 break;
719 case 'c':
720 opt_xdp_bind_flags |= XDP_COPY;
721 break;
Kevin Laatzc543f542019-08-27 02:25:28 +0000722 case 'u':
723 opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
724 opt_unaligned_chunks = 1;
Kevin Laatz3945b372019-08-27 02:25:30 +0000725 opt_mmap_flags = MAP_HUGETLB;
Kevin Laatzc543f542019-08-27 02:25:28 +0000726 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100727 case 'F':
728 opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
729 break;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300730 case 'f':
731 opt_xsk_frame_size = atoi(optarg);
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100732 break;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200733 case 'm':
734 opt_need_wakeup = false;
735 opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300736 break;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100737 case 'M':
738 opt_num_xsks = MAX_SOCKS;
739 break;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530740 case 'd':
741 opt_duration = atoi(optarg);
742 opt_duration *= 1000000000;
743 break;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530744 case 'b':
745 opt_batch_size = atoi(optarg);
746 break;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530747 case 'C':
748 opt_pkt_count = atoi(optarg);
749 break;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530750 case 's':
751 opt_pkt_size = atoi(optarg);
752 if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
753 opt_pkt_size < MIN_PKT_SIZE) {
754 fprintf(stderr,
755 "ERROR: Invalid frame size %d\n",
756 opt_pkt_size);
757 usage(basename(argv[0]));
758 }
759 break;
Jay Jayatheerthan46e32682019-12-20 14:25:30 +0530760 case 'P':
761 opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
762 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200763 default:
764 usage(basename(argv[0]));
765 }
766 }
767
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100768 if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
769 opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
770
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200771 opt_ifindex = if_nametoindex(opt_if);
772 if (!opt_ifindex) {
773 fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
774 opt_if);
775 usage(basename(argv[0]));
776 }
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100777
Kevin Laatzc543f542019-08-27 02:25:28 +0000778 if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
779 !opt_unaligned_chunks) {
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +0300780 fprintf(stderr, "--frame-size=%d is not a power of two\n",
781 opt_xsk_frame_size);
782 usage(basename(argv[0]));
783 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200784}
785
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100786static void kick_tx(struct xsk_socket_info *xsk)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200787{
788 int ret;
789
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100790 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
Maciej Fijalkowski8ed47e12020-02-05 05:58:34 +0100791 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
792 errno == EBUSY || errno == ENETDOWN)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200793 return;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100794 exit_with_error(errno);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200795}
796
Magnus Karlsson46738f72019-08-14 09:27:21 +0200797static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk,
798 struct pollfd *fds)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200799{
Kevin Laatz03895e62019-08-27 02:25:29 +0000800 struct xsk_umem_info *umem = xsk->umem;
Yonghong Songb74e21a2019-02-28 22:19:41 -0800801 u32 idx_cq = 0, idx_fq = 0;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200802 unsigned int rcvd;
803 size_t ndescs;
804
805 if (!xsk->outstanding_tx)
806 return;
807
Magnus Karlsson46738f72019-08-14 09:27:21 +0200808 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx))
809 kick_tx(xsk);
810
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530811 ndescs = (xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100812 xsk->outstanding_tx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200813
814 /* re-add completed Tx buffers */
Kevin Laatz03895e62019-08-27 02:25:29 +0000815 rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200816 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100817 unsigned int i;
818 int ret;
819
Kevin Laatz03895e62019-08-27 02:25:29 +0000820 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100821 while (ret != rcvd) {
822 if (ret < 0)
823 exit_with_error(-ret);
Kevin Laatz03895e62019-08-27 02:25:29 +0000824 if (xsk_ring_prod__needs_wakeup(&umem->fq))
Magnus Karlsson46738f72019-08-14 09:27:21 +0200825 ret = poll(fds, num_socks, opt_timeout);
Kevin Laatz03895e62019-08-27 02:25:29 +0000826 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100827 }
Kevin Laatz03895e62019-08-27 02:25:29 +0000828
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100829 for (i = 0; i < rcvd; i++)
Kevin Laatz03895e62019-08-27 02:25:29 +0000830 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
831 *xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100832
833 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
834 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200835 xsk->outstanding_tx -= rcvd;
836 xsk->tx_npkts += rcvd;
837 }
838}
839
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530840static inline void complete_tx_only(struct xsk_socket_info *xsk,
841 int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200842{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200843 unsigned int rcvd;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100844 u32 idx;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200845
846 if (!xsk->outstanding_tx)
847 return;
848
Magnus Karlsson46738f72019-08-14 09:27:21 +0200849 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx))
850 kick_tx(xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200851
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530852 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200853 if (rcvd > 0) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100854 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200855 xsk->outstanding_tx -= rcvd;
856 xsk->tx_npkts += rcvd;
857 }
858}
859
Magnus Karlsson46738f72019-08-14 09:27:21 +0200860static void rx_drop(struct xsk_socket_info *xsk, struct pollfd *fds)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200861{
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200862 unsigned int rcvd, i;
Yonghong Songb74e21a2019-02-28 22:19:41 -0800863 u32 idx_rx = 0, idx_fq = 0;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100864 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200865
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530866 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +0200867 if (!rcvd) {
868 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
869 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200870 return;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200871 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200872
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100873 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
874 while (ret != rcvd) {
875 if (ret < 0)
876 exit_with_error(-ret);
Magnus Karlsson46738f72019-08-14 09:27:21 +0200877 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
878 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100879 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200880 }
881
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100882 for (i = 0; i < rcvd; i++) {
883 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
884 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Kevin Laatz03895e62019-08-27 02:25:29 +0000885 u64 orig = xsk_umem__extract_addr(addr);
886
887 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100888 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200889
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100890 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +0000891 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100892 }
893
894 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
895 xsk_ring_cons__release(&xsk->rx, rcvd);
896 xsk->rx_npkts += rcvd;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200897}
898
899static void rx_drop_all(void)
900{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100901 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +0200902 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200903
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200904 for (i = 0; i < num_socks; i++) {
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100905 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200906 fds[i].events = POLLIN;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200907 }
908
909 for (;;) {
910 if (opt_poll) {
Magnus Karlsson46738f72019-08-14 09:27:21 +0200911 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200912 if (ret <= 0)
913 continue;
914 }
915
916 for (i = 0; i < num_socks; i++)
Magnus Karlsson46738f72019-08-14 09:27:21 +0200917 rx_drop(xsks[i], fds);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +0530918
919 if (benchmark_done)
920 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200921 }
922}
923
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530924static void tx_only(struct xsk_socket_info *xsk, u32 frame_nb, int batch_size)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200925{
Magnus Karlsson46738f72019-08-14 09:27:21 +0200926 u32 idx;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530927 unsigned int i;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200928
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530929 while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
930 batch_size) {
931 complete_tx_only(xsk, batch_size);
Magnus Karlsson46738f72019-08-14 09:27:21 +0200932 }
933
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530934 for (i = 0; i < batch_size; i++) {
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530935 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
936 idx + i);
937 tx_desc->addr = (frame_nb + i) << XSK_UMEM__DEFAULT_FRAME_SHIFT;
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +0530938 tx_desc->len = PKT_SIZE;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530939 }
940
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530941 xsk_ring_prod__submit(&xsk->tx, batch_size);
942 xsk->outstanding_tx += batch_size;
943 frame_nb += batch_size;
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +0530944 frame_nb %= NUM_FRAMES;
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530945 complete_tx_only(xsk, batch_size);
946}
947
948static inline int get_batch_size(int pkt_cnt)
949{
950 if (!opt_pkt_count)
951 return opt_batch_size;
952
953 if (pkt_cnt + opt_batch_size <= opt_pkt_count)
954 return opt_batch_size;
955
956 return opt_pkt_count - pkt_cnt;
957}
958
959static void complete_tx_only_all(void)
960{
961 bool pending;
962 int i;
963
964 do {
965 pending = false;
966 for (i = 0; i < num_socks; i++) {
967 if (xsks[i]->outstanding_tx) {
968 complete_tx_only(xsks[i], opt_batch_size);
969 pending = !!xsks[i]->outstanding_tx;
970 }
971 }
972 } while (pending);
Magnus Karlsson46738f72019-08-14 09:27:21 +0200973}
974
975static void tx_only_all(void)
976{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +0100977 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +0200978 u32 frame_nb[MAX_SOCKS] = {};
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530979 int pkt_cnt = 0;
Magnus Karlsson46738f72019-08-14 09:27:21 +0200980 int i, ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200981
Magnus Karlsson46738f72019-08-14 09:27:21 +0200982 for (i = 0; i < num_socks; i++) {
983 fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
984 fds[0].events = POLLOUT;
985 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200986
Jay Jayatheerthanece6e962019-12-20 14:25:28 +0530987 while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
988 int batch_size = get_batch_size(pkt_cnt);
989
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200990 if (opt_poll) {
Magnus Karlsson46738f72019-08-14 09:27:21 +0200991 ret = poll(fds, num_socks, opt_timeout);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200992 if (ret <= 0)
993 continue;
994
Magnus Karlsson248c7f92019-02-21 10:21:27 +0100995 if (!(fds[0].revents & POLLOUT))
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +0200996 continue;
997 }
998
Magnus Karlsson46738f72019-08-14 09:27:21 +0200999 for (i = 0; i < num_socks; i++)
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301000 tx_only(xsks[i], frame_nb[i], batch_size);
1001
1002 pkt_cnt += batch_size;
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301003
1004 if (benchmark_done)
1005 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001006 }
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301007
1008 if (opt_pkt_count)
1009 complete_tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001010}
1011
Magnus Karlsson46738f72019-08-14 09:27:21 +02001012static void l2fwd(struct xsk_socket_info *xsk, struct pollfd *fds)
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001013{
Magnus Karlsson46738f72019-08-14 09:27:21 +02001014 unsigned int rcvd, i;
1015 u32 idx_rx = 0, idx_tx = 0;
1016 int ret;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001017
Magnus Karlsson46738f72019-08-14 09:27:21 +02001018 complete_tx_l2fwd(xsk, fds);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001019
Jay Jayatheerthancd9e72b62019-12-20 14:25:27 +05301020 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001021 if (!rcvd) {
1022 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq))
1023 ret = poll(fds, num_socks, opt_timeout);
1024 return;
1025 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001026
Magnus Karlsson46738f72019-08-14 09:27:21 +02001027 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1028 while (ret != rcvd) {
1029 if (ret < 0)
1030 exit_with_error(-ret);
1031 if (xsk_ring_prod__needs_wakeup(&xsk->tx))
1032 kick_tx(xsk);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001033 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001034 }
1035
1036 for (i = 0; i < rcvd; i++) {
1037 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1038 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
Ciara Loftus5a712e12019-09-13 10:39:48 +00001039 u64 orig = addr;
Kevin Laatz03895e62019-08-27 02:25:29 +00001040
1041 addr = xsk_umem__add_offset_to_addr(addr);
Magnus Karlsson46738f72019-08-14 09:27:21 +02001042 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1043
1044 swap_mac_addresses(pkt);
1045
1046 hex_dump(pkt, len, addr);
Kevin Laatz03895e62019-08-27 02:25:29 +00001047 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = orig;
Magnus Karlsson46738f72019-08-14 09:27:21 +02001048 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
1049 }
1050
1051 xsk_ring_prod__submit(&xsk->tx, rcvd);
1052 xsk_ring_cons__release(&xsk->rx, rcvd);
1053
1054 xsk->rx_npkts += rcvd;
1055 xsk->outstanding_tx += rcvd;
1056}
1057
1058static void l2fwd_all(void)
1059{
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001060 struct pollfd fds[MAX_SOCKS] = {};
Magnus Karlsson46738f72019-08-14 09:27:21 +02001061 int i, ret;
1062
Magnus Karlsson46738f72019-08-14 09:27:21 +02001063 for (i = 0; i < num_socks; i++) {
1064 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1065 fds[i].events = POLLOUT | POLLIN;
1066 }
1067
1068 for (;;) {
1069 if (opt_poll) {
1070 ret = poll(fds, num_socks, opt_timeout);
1071 if (ret <= 0)
1072 continue;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001073 }
1074
Magnus Karlsson46738f72019-08-14 09:27:21 +02001075 for (i = 0; i < num_socks; i++)
1076 l2fwd(xsks[i], fds);
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301077
1078 if (benchmark_done)
1079 break;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001080 }
1081}
1082
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001083static void load_xdp_program(char **argv, struct bpf_object **obj)
1084{
1085 struct bpf_prog_load_attr prog_load_attr = {
1086 .prog_type = BPF_PROG_TYPE_XDP,
1087 };
1088 char xdp_filename[256];
1089 int prog_fd;
1090
1091 snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
1092 prog_load_attr.file = xdp_filename;
1093
1094 if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
1095 exit(EXIT_FAILURE);
1096 if (prog_fd < 0) {
1097 fprintf(stderr, "ERROR: no program found: %s\n",
1098 strerror(prog_fd));
1099 exit(EXIT_FAILURE);
1100 }
1101
1102 if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
1103 fprintf(stderr, "ERROR: link set xdp fd failed\n");
1104 exit(EXIT_FAILURE);
1105 }
1106}
1107
1108static void enter_xsks_into_map(struct bpf_object *obj)
1109{
1110 struct bpf_map *map;
1111 int i, xsks_map;
1112
1113 map = bpf_object__find_map_by_name(obj, "xsks_map");
1114 xsks_map = bpf_map__fd(map);
1115 if (xsks_map < 0) {
1116 fprintf(stderr, "ERROR: no xsks map found: %s\n",
1117 strerror(xsks_map));
1118 exit(EXIT_FAILURE);
1119 }
1120
1121 for (i = 0; i < num_socks; i++) {
1122 int fd = xsk_socket__fd(xsks[i]->xsk);
1123 int key, ret;
1124
1125 key = i;
1126 ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
1127 if (ret) {
1128 fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
1129 exit(EXIT_FAILURE);
1130 }
1131 }
1132}
1133
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001134int main(int argc, char **argv)
1135{
1136 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Magnus Karlsson661842c2019-11-07 18:47:39 +01001137 bool rx = false, tx = false;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001138 struct xsk_umem_info *umem;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001139 struct bpf_object *obj;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001140 pthread_t pt;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001141 int i, ret;
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001142 void *bufs;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001143
1144 parse_command_line(argc, argv);
1145
1146 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
1147 fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1148 strerror(errno));
1149 exit(EXIT_FAILURE);
1150 }
1151
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001152 if (opt_num_xsks > 1)
1153 load_xdp_program(argv, &obj);
1154
Kevin Laatz3945b372019-08-27 02:25:30 +00001155 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1156 bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size,
1157 PROT_READ | PROT_WRITE,
1158 MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
1159 if (bufs == MAP_FAILED) {
1160 printf("ERROR: mmap failed\n");
1161 exit(EXIT_FAILURE);
1162 }
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001163
1164 /* Create sockets... */
Maxim Mikityanskiy123e8da12019-06-26 17:35:27 +03001165 umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size);
Magnus Karlsson661842c2019-11-07 18:47:39 +01001166 if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
1167 rx = true;
1168 xsk_populate_fill_ring(umem);
1169 }
1170 if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
1171 tx = true;
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001172 for (i = 0; i < opt_num_xsks; i++)
Magnus Karlsson661842c2019-11-07 18:47:39 +01001173 xsks[num_socks++] = xsk_configure_socket(umem, rx, tx);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001174
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301175 if (opt_bench == BENCH_TXONLY) {
1176 gen_eth_hdr_data();
1177
Magnus Karlsson661842c2019-11-07 18:47:39 +01001178 for (i = 0; i < NUM_FRAMES; i++)
1179 gen_eth_frame(umem, i * opt_xsk_frame_size);
Jay Jayatheerthan4a3c23a2019-12-20 14:25:29 +05301180 }
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001181
Magnus Karlsson2e5d72c2019-11-07 18:47:37 +01001182 if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
1183 enter_xsks_into_map(obj);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001184
1185 signal(SIGINT, int_exit);
1186 signal(SIGTERM, int_exit);
1187 signal(SIGABRT, int_exit);
1188
1189 setlocale(LC_ALL, "");
1190
1191 ret = pthread_create(&pt, NULL, poller, NULL);
Magnus Karlsson248c7f92019-02-21 10:21:27 +01001192 if (ret)
1193 exit_with_error(ret);
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001194
1195 prev_time = get_nsecs();
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301196 start_time = prev_time;
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001197
1198 if (opt_bench == BENCH_RXDROP)
1199 rx_drop_all();
1200 else if (opt_bench == BENCH_TXONLY)
Magnus Karlsson46738f72019-08-14 09:27:21 +02001201 tx_only_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001202 else
Magnus Karlsson46738f72019-08-14 09:27:21 +02001203 l2fwd_all();
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001204
Jay Jayatheerthanece6e962019-12-20 14:25:28 +05301205 benchmark_done = true;
1206
Jay Jayatheerthand3f11b02019-12-20 14:25:25 +05301207 pthread_join(pt, NULL);
1208
Jay Jayatheerthan69525582019-12-20 14:25:26 +05301209 xdpsock_cleanup();
1210
Magnus Karlssonb4b8faa2018-05-02 13:01:36 +02001211 return 0;
1212}