1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * AF_XDP user-space access library.
6 * Copyright(c) 2018 - 2019 Intel Corporation.
8 * Author(s): Magnus Karlsson <magnus.karlsson@intel.com>
15 #include <arpa/inet.h>
16 #include <asm/barrier.h>
17 #include <linux/compiler.h>
18 #include <linux/ethtool.h>
19 #include <linux/filter.h>
20 #include <linux/if_ether.h>
21 #include <linux/if_packet.h>
22 #include <linux/if_xdp.h>
23 #include <linux/sockios.h>
25 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include "libbpf_internal.h"
35 /* make sure libbpf doesn't use kernel-only integer typedefs */
36 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
51 struct xsk_ring_prod *fill;
52 struct xsk_ring_cons *comp;
54 struct xsk_umem_config config;
60 struct xsk_ring_cons *rx;
61 struct xsk_ring_prod *tx;
63 struct xsk_umem *umem;
64 struct xsk_socket_config config;
70 char ifname[IFNAMSIZ];
74 bool xdp_prog_attached;
79 /* Up until and including Linux 5.3 */
80 struct xdp_ring_offset_v1 {
86 /* Up until and including Linux 5.3 */
87 struct xdp_mmap_offsets_v1 {
88 struct xdp_ring_offset_v1 rx;
89 struct xdp_ring_offset_v1 tx;
90 struct xdp_ring_offset_v1 fr;
91 struct xdp_ring_offset_v1 cr;
94 int xsk_umem__fd(const struct xsk_umem *umem)
96 return umem ? umem->fd : -EINVAL;
99 int xsk_socket__fd(const struct xsk_socket *xsk)
101 return xsk ? xsk->fd : -EINVAL;
104 static bool xsk_page_aligned(void *buffer)
106 unsigned long addr = (unsigned long)buffer;
108 return !(addr & (getpagesize() - 1));
111 static void xsk_set_umem_config(struct xsk_umem_config *cfg,
112 const struct xsk_umem_config *usr_cfg)
115 cfg->fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
116 cfg->comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
117 cfg->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
118 cfg->frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM;
119 cfg->flags = XSK_UMEM__DEFAULT_FLAGS;
123 cfg->fill_size = usr_cfg->fill_size;
124 cfg->comp_size = usr_cfg->comp_size;
125 cfg->frame_size = usr_cfg->frame_size;
126 cfg->frame_headroom = usr_cfg->frame_headroom;
127 cfg->flags = usr_cfg->flags;
130 static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg,
131 const struct xsk_socket_config *usr_cfg)
134 cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
135 cfg->tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
136 cfg->libbpf_flags = 0;
142 if (usr_cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)
145 cfg->rx_size = usr_cfg->rx_size;
146 cfg->tx_size = usr_cfg->tx_size;
147 cfg->libbpf_flags = usr_cfg->libbpf_flags;
148 cfg->xdp_flags = usr_cfg->xdp_flags;
149 cfg->bind_flags = usr_cfg->bind_flags;
154 static void xsk_mmap_offsets_v1(struct xdp_mmap_offsets *off)
156 struct xdp_mmap_offsets_v1 off_v1;
158 /* getsockopt on a kernel <= 5.3 has no flags fields.
159 * Copy over the offsets to the correct places in the >=5.4 format
160 * and put the flags where they would have been on that kernel.
162 memcpy(&off_v1, off, sizeof(off_v1));
164 off->rx.producer = off_v1.rx.producer;
165 off->rx.consumer = off_v1.rx.consumer;
166 off->rx.desc = off_v1.rx.desc;
167 off->rx.flags = off_v1.rx.consumer + sizeof(__u32);
169 off->tx.producer = off_v1.tx.producer;
170 off->tx.consumer = off_v1.tx.consumer;
171 off->tx.desc = off_v1.tx.desc;
172 off->tx.flags = off_v1.tx.consumer + sizeof(__u32);
174 off->fr.producer = off_v1.fr.producer;
175 off->fr.consumer = off_v1.fr.consumer;
176 off->fr.desc = off_v1.fr.desc;
177 off->fr.flags = off_v1.fr.consumer + sizeof(__u32);
179 off->cr.producer = off_v1.cr.producer;
180 off->cr.consumer = off_v1.cr.consumer;
181 off->cr.desc = off_v1.cr.desc;
182 off->cr.flags = off_v1.cr.consumer + sizeof(__u32);
185 static int xsk_get_mmap_offsets(int fd, struct xdp_mmap_offsets *off)
190 optlen = sizeof(*off);
191 err = getsockopt(fd, SOL_XDP, XDP_MMAP_OFFSETS, off, &optlen);
195 if (optlen == sizeof(*off))
198 if (optlen == sizeof(struct xdp_mmap_offsets_v1)) {
199 xsk_mmap_offsets_v1(off);
206 int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,
207 __u64 size, struct xsk_ring_prod *fill,
208 struct xsk_ring_cons *comp,
209 const struct xsk_umem_config *usr_config)
211 struct xdp_mmap_offsets off;
212 struct xdp_umem_reg mr;
213 struct xsk_umem *umem;
217 if (!umem_area || !umem_ptr || !fill || !comp)
219 if (!size && !xsk_page_aligned(umem_area))
222 umem = calloc(1, sizeof(*umem));
226 umem->fd = socket(AF_XDP, SOCK_RAW, 0);
232 umem->umem_area = umem_area;
233 xsk_set_umem_config(&umem->config, usr_config);
235 memset(&mr, 0, sizeof(mr));
236 mr.addr = (uintptr_t)umem_area;
238 mr.chunk_size = umem->config.frame_size;
239 mr.headroom = umem->config.frame_headroom;
240 mr.flags = umem->config.flags;
242 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr));
247 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_FILL_RING,
248 &umem->config.fill_size,
249 sizeof(umem->config.fill_size));
254 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_COMPLETION_RING,
255 &umem->config.comp_size,
256 sizeof(umem->config.comp_size));
262 err = xsk_get_mmap_offsets(umem->fd, &off);
268 map = mmap(NULL, off.fr.desc + umem->config.fill_size * sizeof(__u64),
269 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, umem->fd,
270 XDP_UMEM_PGOFF_FILL_RING);
271 if (map == MAP_FAILED) {
277 fill->mask = umem->config.fill_size - 1;
278 fill->size = umem->config.fill_size;
279 fill->producer = map + off.fr.producer;
280 fill->consumer = map + off.fr.consumer;
281 fill->flags = map + off.fr.flags;
282 fill->ring = map + off.fr.desc;
283 fill->cached_cons = umem->config.fill_size;
285 map = mmap(NULL, off.cr.desc + umem->config.comp_size * sizeof(__u64),
286 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, umem->fd,
287 XDP_UMEM_PGOFF_COMPLETION_RING);
288 if (map == MAP_FAILED) {
294 comp->mask = umem->config.comp_size - 1;
295 comp->size = umem->config.comp_size;
296 comp->producer = map + off.cr.producer;
297 comp->consumer = map + off.cr.consumer;
298 comp->flags = map + off.cr.flags;
299 comp->ring = map + off.cr.desc;
305 munmap(map, off.fr.desc + umem->config.fill_size * sizeof(__u64));
313 struct xsk_umem_config_v1 {
317 __u32 frame_headroom;
320 int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,
321 __u64 size, struct xsk_ring_prod *fill,
322 struct xsk_ring_cons *comp,
323 const struct xsk_umem_config *usr_config)
325 struct xsk_umem_config config;
327 memcpy(&config, usr_config, sizeof(struct xsk_umem_config_v1));
330 return xsk_umem__create_v0_0_4(umem_ptr, umem_area, size, fill, comp,
333 COMPAT_VERSION(xsk_umem__create_v0_0_2, xsk_umem__create, LIBBPF_0.0.2)
334 DEFAULT_VERSION(xsk_umem__create_v0_0_4, xsk_umem__create, LIBBPF_0.0.4)
336 static int xsk_load_xdp_prog(struct xsk_socket *xsk)
338 static const int log_buf_size = 16 * 1024;
339 char log_buf[log_buf_size];
342 /* This is the C-program:
343 * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
345 * int ret, index = ctx->rx_queue_index;
347 * // A set entry here means that the correspnding queue_id
348 * // has an active AF_XDP socket bound to it.
349 * ret = bpf_redirect_map(&xsks_map, index, XDP_PASS);
353 * // Fallback for pre-5.3 kernels, not supporting default
354 * // action in the flags parameter.
355 * if (bpf_map_lookup_elem(&xsks_map, &index))
356 * return bpf_redirect_map(&xsks_map, index, 0);
360 struct bpf_insn prog[] = {
361 /* r2 = *(u32 *)(r1 + 16) */
362 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 16),
363 /* *(u32 *)(r10 - 4) = r2 */
364 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -4),
366 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
368 BPF_MOV64_IMM(BPF_REG_3, 2),
369 /* call bpf_redirect_map */
370 BPF_EMIT_CALL(BPF_FUNC_redirect_map),
371 /* if w0 != 0 goto pc+13 */
372 BPF_JMP32_IMM(BPF_JSGT, BPF_REG_0, 0, 13),
374 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
376 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
378 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
379 /* call bpf_map_lookup_elem */
380 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
382 BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
384 BPF_MOV64_IMM(BPF_REG_0, 2),
385 /* if r1 == 0 goto pc+5 */
386 BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 5),
387 /* r2 = *(u32 *)(r10 - 4) */
388 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, -4),
390 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
392 BPF_MOV64_IMM(BPF_REG_3, 0),
393 /* call bpf_redirect_map */
394 BPF_EMIT_CALL(BPF_FUNC_redirect_map),
395 /* The jumps are to this instruction */
398 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
400 prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, prog, insns_cnt,
401 "LGPL-2.1 or BSD-2-Clause", 0, log_buf,
404 pr_warn("BPF log buffer:\n%s", log_buf);
408 err = bpf_set_link_xdp_fd(xsk->ifindex, prog_fd, xsk->config.xdp_flags);
414 xsk->prog_fd = prog_fd;
418 static int xsk_get_max_queues(struct xsk_socket *xsk)
420 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
421 struct ifreq ifr = {};
424 fd = socket(AF_INET, SOCK_DGRAM, 0);
428 ifr.ifr_data = (void *)&channels;
429 memcpy(ifr.ifr_name, xsk->ifname, IFNAMSIZ - 1);
430 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
431 err = ioctl(fd, SIOCETHTOOL, &ifr);
432 if (err && errno != EOPNOTSUPP) {
438 /* If the device says it has no channels, then all traffic
439 * is sent to a single stream, so max queues = 1.
443 /* Take the max of rx, tx, combined. Drivers return
444 * the number of channels in different ways.
446 ret = max(channels.max_rx, channels.max_tx);
447 ret = max(ret, (int)channels.max_combined);
455 static int xsk_create_bpf_maps(struct xsk_socket *xsk)
460 max_queues = xsk_get_max_queues(xsk);
464 fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map",
465 sizeof(int), sizeof(int), max_queues, 0);
469 xsk->xsks_map_fd = fd;
474 static void xsk_delete_bpf_maps(struct xsk_socket *xsk)
476 bpf_map_delete_elem(xsk->xsks_map_fd, &xsk->queue_id);
477 close(xsk->xsks_map_fd);
480 static int xsk_lookup_bpf_maps(struct xsk_socket *xsk)
482 __u32 i, *map_ids, num_maps, prog_len = sizeof(struct bpf_prog_info);
483 __u32 map_len = sizeof(struct bpf_map_info);
484 struct bpf_prog_info prog_info = {};
485 struct bpf_map_info map_info;
488 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
492 num_maps = prog_info.nr_map_ids;
494 map_ids = calloc(prog_info.nr_map_ids, sizeof(*map_ids));
498 memset(&prog_info, 0, prog_len);
499 prog_info.nr_map_ids = num_maps;
500 prog_info.map_ids = (__u64)(unsigned long)map_ids;
502 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
506 xsk->xsks_map_fd = -1;
508 for (i = 0; i < prog_info.nr_map_ids; i++) {
509 fd = bpf_map_get_fd_by_id(map_ids[i]);
513 err = bpf_obj_get_info_by_fd(fd, &map_info, &map_len);
519 if (!strcmp(map_info.name, "xsks_map")) {
520 xsk->xsks_map_fd = fd;
528 if (xsk->xsks_map_fd == -1)
536 static int xsk_set_bpf_maps(struct xsk_socket *xsk)
538 return bpf_map_update_elem(xsk->xsks_map_fd, &xsk->queue_id,
542 static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
547 err = bpf_get_link_xdp_id(xsk->ifindex, &prog_id,
548 xsk->config.xdp_flags);
553 err = xsk_create_bpf_maps(xsk);
557 err = xsk_load_xdp_prog(xsk);
559 xsk_delete_bpf_maps(xsk);
563 xsk->prog_fd = bpf_prog_get_fd_by_id(prog_id);
564 if (xsk->prog_fd < 0)
566 err = xsk_lookup_bpf_maps(xsk);
574 err = xsk_set_bpf_maps(xsk);
576 xsk_delete_bpf_maps(xsk);
584 int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
585 __u32 queue_id, struct xsk_umem *umem,
586 struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
587 const struct xsk_socket_config *usr_config)
589 void *rx_map = NULL, *tx_map = NULL;
590 struct sockaddr_xdp sxdp = {};
591 struct xdp_mmap_offsets off;
592 struct xsk_socket *xsk;
595 if (!umem || !xsk_ptr || !(rx || tx))
598 xsk = calloc(1, sizeof(*xsk));
602 err = xsk_set_xdp_socket_config(&xsk->config, usr_config);
606 if (umem->refcount &&
607 !(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
608 pr_warn("Error: shared umems not supported by libbpf supplied XDP program.\n");
613 if (umem->refcount++ > 0) {
614 xsk->fd = socket(AF_XDP, SOCK_RAW, 0);
623 xsk->outstanding_tx = 0;
624 xsk->queue_id = queue_id;
626 xsk->ifindex = if_nametoindex(ifname);
631 memcpy(xsk->ifname, ifname, IFNAMSIZ - 1);
632 xsk->ifname[IFNAMSIZ - 1] = '\0';
635 err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING,
636 &xsk->config.rx_size,
637 sizeof(xsk->config.rx_size));
644 err = setsockopt(xsk->fd, SOL_XDP, XDP_TX_RING,
645 &xsk->config.tx_size,
646 sizeof(xsk->config.tx_size));
653 err = xsk_get_mmap_offsets(xsk->fd, &off);
660 rx_map = mmap(NULL, off.rx.desc +
661 xsk->config.rx_size * sizeof(struct xdp_desc),
662 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
663 xsk->fd, XDP_PGOFF_RX_RING);
664 if (rx_map == MAP_FAILED) {
669 rx->mask = xsk->config.rx_size - 1;
670 rx->size = xsk->config.rx_size;
671 rx->producer = rx_map + off.rx.producer;
672 rx->consumer = rx_map + off.rx.consumer;
673 rx->flags = rx_map + off.rx.flags;
674 rx->ring = rx_map + off.rx.desc;
679 tx_map = mmap(NULL, off.tx.desc +
680 xsk->config.tx_size * sizeof(struct xdp_desc),
681 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
682 xsk->fd, XDP_PGOFF_TX_RING);
683 if (tx_map == MAP_FAILED) {
688 tx->mask = xsk->config.tx_size - 1;
689 tx->size = xsk->config.tx_size;
690 tx->producer = tx_map + off.tx.producer;
691 tx->consumer = tx_map + off.tx.consumer;
692 tx->flags = tx_map + off.tx.flags;
693 tx->ring = tx_map + off.tx.desc;
694 tx->cached_cons = xsk->config.tx_size;
698 sxdp.sxdp_family = PF_XDP;
699 sxdp.sxdp_ifindex = xsk->ifindex;
700 sxdp.sxdp_queue_id = xsk->queue_id;
701 if (umem->refcount > 1) {
702 sxdp.sxdp_flags = XDP_SHARED_UMEM;
703 sxdp.sxdp_shared_umem_fd = umem->fd;
705 sxdp.sxdp_flags = xsk->config.bind_flags;
708 err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp));
716 if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
717 err = xsk_setup_xdp_prog(xsk);
727 munmap(tx_map, off.tx.desc +
728 xsk->config.tx_size * sizeof(struct xdp_desc));
731 munmap(rx_map, off.rx.desc +
732 xsk->config.rx_size * sizeof(struct xdp_desc));
734 if (--umem->refcount)
741 int xsk_umem__delete(struct xsk_umem *umem)
743 struct xdp_mmap_offsets off;
752 err = xsk_get_mmap_offsets(umem->fd, &off);
754 munmap(umem->fill->ring - off.fr.desc,
755 off.fr.desc + umem->config.fill_size * sizeof(__u64));
756 munmap(umem->comp->ring - off.cr.desc,
757 off.cr.desc + umem->config.comp_size * sizeof(__u64));
766 void xsk_socket__delete(struct xsk_socket *xsk)
768 size_t desc_sz = sizeof(struct xdp_desc);
769 struct xdp_mmap_offsets off;
775 if (xsk->prog_fd != -1) {
776 xsk_delete_bpf_maps(xsk);
780 err = xsk_get_mmap_offsets(xsk->fd, &off);
783 munmap(xsk->rx->ring - off.rx.desc,
784 off.rx.desc + xsk->config.rx_size * desc_sz);
787 munmap(xsk->tx->ring - off.tx.desc,
788 off.tx.desc + xsk->config.tx_size * desc_sz);
793 xsk->umem->refcount--;
794 /* Do not close an fd that also has an associated umem connected
797 if (xsk->fd != xsk->umem->fd)