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"
48 struct xsk_ring_prod *fill;
49 struct xsk_ring_cons *comp;
51 struct xsk_umem_config config;
57 struct xsk_ring_cons *rx;
58 struct xsk_ring_prod *tx;
60 struct xsk_umem *umem;
61 struct xsk_socket_config config;
67 char ifname[IFNAMSIZ];
72 bool xdp_prog_attached;
77 /* For 32-bit systems, we need to use mmap2 as the offsets are 64-bit.
78 * Unfortunately, it is not part of glibc.
80 static inline void *xsk_mmap(void *addr, size_t length, int prot, int flags,
84 unsigned int page_shift = __builtin_ffs(getpagesize()) - 1;
85 long ret = syscall(__NR_mmap2, addr, length, prot, flags, fd,
86 (off_t)(offset >> page_shift));
90 return mmap(addr, length, prot, flags, fd, offset);
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;
122 cfg->fill_size = usr_cfg->fill_size;
123 cfg->comp_size = usr_cfg->comp_size;
124 cfg->frame_size = usr_cfg->frame_size;
125 cfg->frame_headroom = usr_cfg->frame_headroom;
128 static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg,
129 const struct xsk_socket_config *usr_cfg)
132 cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
133 cfg->tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
134 cfg->libbpf_flags = 0;
140 if (usr_cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)
143 cfg->rx_size = usr_cfg->rx_size;
144 cfg->tx_size = usr_cfg->tx_size;
145 cfg->libbpf_flags = usr_cfg->libbpf_flags;
146 cfg->xdp_flags = usr_cfg->xdp_flags;
147 cfg->bind_flags = usr_cfg->bind_flags;
152 int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area, __u64 size,
153 struct xsk_ring_prod *fill, struct xsk_ring_cons *comp,
154 const struct xsk_umem_config *usr_config)
156 struct xdp_mmap_offsets off;
157 struct xdp_umem_reg mr;
158 struct xsk_umem *umem;
163 if (!umem_area || !umem_ptr || !fill || !comp)
165 if (!size && !xsk_page_aligned(umem_area))
168 umem = calloc(1, sizeof(*umem));
172 umem->fd = socket(AF_XDP, SOCK_RAW, 0);
178 umem->umem_area = umem_area;
179 xsk_set_umem_config(&umem->config, usr_config);
181 mr.addr = (uintptr_t)umem_area;
183 mr.chunk_size = umem->config.frame_size;
184 mr.headroom = umem->config.frame_headroom;
186 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr));
191 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_FILL_RING,
192 &umem->config.fill_size,
193 sizeof(umem->config.fill_size));
198 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_COMPLETION_RING,
199 &umem->config.comp_size,
200 sizeof(umem->config.comp_size));
206 optlen = sizeof(off);
207 err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
213 map = xsk_mmap(NULL, off.fr.desc +
214 umem->config.fill_size * sizeof(__u64),
215 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
216 umem->fd, XDP_UMEM_PGOFF_FILL_RING);
217 if (map == MAP_FAILED) {
223 fill->mask = umem->config.fill_size - 1;
224 fill->size = umem->config.fill_size;
225 fill->producer = map + off.fr.producer;
226 fill->consumer = map + off.fr.consumer;
227 fill->ring = map + off.fr.desc;
228 fill->cached_cons = umem->config.fill_size;
231 off.cr.desc + umem->config.comp_size * sizeof(__u64),
232 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
233 umem->fd, XDP_UMEM_PGOFF_COMPLETION_RING);
234 if (map == MAP_FAILED) {
240 comp->mask = umem->config.comp_size - 1;
241 comp->size = umem->config.comp_size;
242 comp->producer = map + off.cr.producer;
243 comp->consumer = map + off.cr.consumer;
244 comp->ring = map + off.cr.desc;
250 munmap(map, off.fr.desc + umem->config.fill_size * sizeof(__u64));
258 static int xsk_load_xdp_prog(struct xsk_socket *xsk)
260 static const int log_buf_size = 16 * 1024;
261 char log_buf[log_buf_size];
264 /* This is the C-program:
265 * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
267 * int index = ctx->rx_queue_index;
269 * // A set entry here means that the correspnding queue_id
270 * // has an active AF_XDP socket bound to it.
271 * if (bpf_map_lookup_elem(&xsks_map, &index))
272 * return bpf_redirect_map(&xsks_map, index, 0);
277 struct bpf_insn prog[] = {
278 /* r1 = *(u32 *)(r1 + 16) */
279 BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1, 16),
280 /* *(u32 *)(r10 - 4) = r1 */
281 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_1, -4),
282 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
283 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
284 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
285 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
286 BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
287 BPF_MOV32_IMM(BPF_REG_0, 2),
288 /* if r1 == 0 goto +5 */
289 BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 5),
290 /* r2 = *(u32 *)(r10 - 4) */
291 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
292 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, -4),
293 BPF_MOV32_IMM(BPF_REG_3, 0),
294 BPF_EMIT_CALL(BPF_FUNC_redirect_map),
295 /* The jumps are to this instruction */
298 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
300 prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, prog, insns_cnt,
301 "LGPL-2.1 or BSD-2-Clause", 0, log_buf,
304 pr_warning("BPF log buffer:\n%s", log_buf);
308 err = bpf_set_link_xdp_fd(xsk->ifindex, prog_fd, xsk->config.xdp_flags);
314 xsk->prog_fd = prog_fd;
318 static int xsk_get_max_queues(struct xsk_socket *xsk)
320 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
321 struct ifreq ifr = {};
324 fd = socket(AF_INET, SOCK_DGRAM, 0);
328 ifr.ifr_data = (void *)&channels;
329 memcpy(ifr.ifr_name, xsk->ifname, IFNAMSIZ - 1);
330 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
331 err = ioctl(fd, SIOCETHTOOL, &ifr);
332 if (err && errno != EOPNOTSUPP) {
337 if (err || channels.max_combined == 0)
338 /* If the device says it has no channels, then all traffic
339 * is sent to a single stream, so max queues = 1.
343 ret = channels.max_combined;
350 static int xsk_create_bpf_maps(struct xsk_socket *xsk)
355 max_queues = xsk_get_max_queues(xsk);
359 fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map",
360 sizeof(int), sizeof(int), max_queues, 0);
364 xsk->xsks_map_fd = fd;
369 static void xsk_delete_bpf_maps(struct xsk_socket *xsk)
371 bpf_map_delete_elem(xsk->xsks_map_fd, &xsk->queue_id);
372 close(xsk->xsks_map_fd);
375 static int xsk_lookup_bpf_maps(struct xsk_socket *xsk)
377 __u32 i, *map_ids, num_maps, prog_len = sizeof(struct bpf_prog_info);
378 __u32 map_len = sizeof(struct bpf_map_info);
379 struct bpf_prog_info prog_info = {};
380 struct bpf_map_info map_info;
383 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
387 num_maps = prog_info.nr_map_ids;
389 map_ids = calloc(prog_info.nr_map_ids, sizeof(*map_ids));
393 memset(&prog_info, 0, prog_len);
394 prog_info.nr_map_ids = num_maps;
395 prog_info.map_ids = (__u64)(unsigned long)map_ids;
397 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
401 xsk->xsks_map_fd = -1;
403 for (i = 0; i < prog_info.nr_map_ids; i++) {
404 fd = bpf_map_get_fd_by_id(map_ids[i]);
408 err = bpf_obj_get_info_by_fd(fd, &map_info, &map_len);
414 if (!strcmp(map_info.name, "xsks_map")) {
415 xsk->xsks_map_fd = fd;
423 if (xsk->xsks_map_fd == -1)
431 static int xsk_set_bpf_maps(struct xsk_socket *xsk)
433 return bpf_map_update_elem(xsk->xsks_map_fd, &xsk->queue_id,
437 static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
442 err = bpf_get_link_xdp_id(xsk->ifindex, &prog_id,
443 xsk->config.xdp_flags);
448 err = xsk_create_bpf_maps(xsk);
452 err = xsk_load_xdp_prog(xsk);
454 xsk_delete_bpf_maps(xsk);
458 xsk->prog_fd = bpf_prog_get_fd_by_id(prog_id);
459 err = xsk_lookup_bpf_maps(xsk);
466 err = xsk_set_bpf_maps(xsk);
468 xsk_delete_bpf_maps(xsk);
476 int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
477 __u32 queue_id, struct xsk_umem *umem,
478 struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
479 const struct xsk_socket_config *usr_config)
481 void *rx_map = NULL, *tx_map = NULL;
482 struct sockaddr_xdp sxdp = {};
483 struct xdp_mmap_offsets off;
484 struct xdp_options opts;
485 struct xsk_socket *xsk;
489 if (!umem || !xsk_ptr || !rx || !tx)
492 if (umem->refcount) {
493 pr_warning("Error: shared umems not supported by libbpf.\n");
497 xsk = calloc(1, sizeof(*xsk));
501 if (umem->refcount++ > 0) {
502 xsk->fd = socket(AF_XDP, SOCK_RAW, 0);
511 xsk->outstanding_tx = 0;
512 xsk->queue_id = queue_id;
514 xsk->ifindex = if_nametoindex(ifname);
519 memcpy(xsk->ifname, ifname, IFNAMSIZ - 1);
520 xsk->ifname[IFNAMSIZ - 1] = '\0';
522 err = xsk_set_xdp_socket_config(&xsk->config, usr_config);
527 err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING,
528 &xsk->config.rx_size,
529 sizeof(xsk->config.rx_size));
536 err = setsockopt(xsk->fd, SOL_XDP, XDP_TX_RING,
537 &xsk->config.tx_size,
538 sizeof(xsk->config.tx_size));
545 optlen = sizeof(off);
546 err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
553 rx_map = xsk_mmap(NULL, off.rx.desc +
554 xsk->config.rx_size * sizeof(struct xdp_desc),
555 PROT_READ | PROT_WRITE,
556 MAP_SHARED | MAP_POPULATE,
557 xsk->fd, XDP_PGOFF_RX_RING);
558 if (rx_map == MAP_FAILED) {
563 rx->mask = xsk->config.rx_size - 1;
564 rx->size = xsk->config.rx_size;
565 rx->producer = rx_map + off.rx.producer;
566 rx->consumer = rx_map + off.rx.consumer;
567 rx->ring = rx_map + off.rx.desc;
572 tx_map = xsk_mmap(NULL, off.tx.desc +
573 xsk->config.tx_size * sizeof(struct xdp_desc),
574 PROT_READ | PROT_WRITE,
575 MAP_SHARED | MAP_POPULATE,
576 xsk->fd, XDP_PGOFF_TX_RING);
577 if (tx_map == MAP_FAILED) {
582 tx->mask = xsk->config.tx_size - 1;
583 tx->size = xsk->config.tx_size;
584 tx->producer = tx_map + off.tx.producer;
585 tx->consumer = tx_map + off.tx.consumer;
586 tx->ring = tx_map + off.tx.desc;
587 tx->cached_cons = xsk->config.tx_size;
591 sxdp.sxdp_family = PF_XDP;
592 sxdp.sxdp_ifindex = xsk->ifindex;
593 sxdp.sxdp_queue_id = xsk->queue_id;
594 sxdp.sxdp_flags = xsk->config.bind_flags;
596 err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp));
604 optlen = sizeof(opts);
605 err = getsockopt(xsk->fd, SOL_XDP, XDP_OPTIONS, &opts, &optlen);
611 xsk->zc = opts.flags & XDP_OPTIONS_ZEROCOPY;
613 if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
614 err = xsk_setup_xdp_prog(xsk);
624 munmap(tx_map, off.tx.desc +
625 xsk->config.tx_size * sizeof(struct xdp_desc));
628 munmap(rx_map, off.rx.desc +
629 xsk->config.rx_size * sizeof(struct xdp_desc));
631 if (--umem->refcount)
638 int xsk_umem__delete(struct xsk_umem *umem)
640 struct xdp_mmap_offsets off;
650 optlen = sizeof(off);
651 err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
653 munmap(umem->fill->ring - off.fr.desc,
654 off.fr.desc + umem->config.fill_size * sizeof(__u64));
655 munmap(umem->comp->ring - off.cr.desc,
656 off.cr.desc + umem->config.comp_size * sizeof(__u64));
665 void xsk_socket__delete(struct xsk_socket *xsk)
667 size_t desc_sz = sizeof(struct xdp_desc);
668 struct xdp_mmap_offsets off;
675 if (xsk->prog_fd != -1) {
676 xsk_delete_bpf_maps(xsk);
680 optlen = sizeof(off);
681 err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
684 munmap(xsk->rx->ring - off.rx.desc,
685 off.rx.desc + xsk->config.rx_size * desc_sz);
688 munmap(xsk->tx->ring - off.tx.desc,
689 off.tx.desc + xsk->config.tx_size * desc_sz);
694 xsk->umem->refcount--;
695 /* Do not close an fd that also has an associated umem connected
698 if (xsk->fd != xsk->umem->fd)