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;
324 fd = socket(AF_INET, SOCK_DGRAM, 0);
328 channels.cmd = ETHTOOL_GCHANNELS;
329 ifr.ifr_data = (void *)&channels;
330 strncpy(ifr.ifr_name, xsk->ifname, IFNAMSIZ - 1);
331 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
332 err = ioctl(fd, SIOCETHTOOL, &ifr);
333 if (err && errno != EOPNOTSUPP) {
338 if (channels.max_combined == 0 || errno == EOPNOTSUPP)
339 /* If the device says it has no channels, then all traffic
340 * is sent to a single stream, so max queues = 1.
344 ret = channels.max_combined;
351 static int xsk_create_bpf_maps(struct xsk_socket *xsk)
356 max_queues = xsk_get_max_queues(xsk);
360 fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map",
361 sizeof(int), sizeof(int), max_queues, 0);
365 xsk->xsks_map_fd = fd;
370 static void xsk_delete_bpf_maps(struct xsk_socket *xsk)
372 bpf_map_delete_elem(xsk->xsks_map_fd, &xsk->queue_id);
373 close(xsk->xsks_map_fd);
376 static int xsk_lookup_bpf_maps(struct xsk_socket *xsk)
378 __u32 i, *map_ids, num_maps, prog_len = sizeof(struct bpf_prog_info);
379 __u32 map_len = sizeof(struct bpf_map_info);
380 struct bpf_prog_info prog_info = {};
381 struct bpf_map_info map_info;
384 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
388 num_maps = prog_info.nr_map_ids;
390 map_ids = calloc(prog_info.nr_map_ids, sizeof(*map_ids));
394 memset(&prog_info, 0, prog_len);
395 prog_info.nr_map_ids = num_maps;
396 prog_info.map_ids = (__u64)(unsigned long)map_ids;
398 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
402 xsk->xsks_map_fd = -1;
404 for (i = 0; i < prog_info.nr_map_ids; i++) {
405 fd = bpf_map_get_fd_by_id(map_ids[i]);
409 err = bpf_obj_get_info_by_fd(fd, &map_info, &map_len);
415 if (!strcmp(map_info.name, "xsks_map")) {
416 xsk->xsks_map_fd = fd;
424 if (xsk->xsks_map_fd == -1)
432 static int xsk_set_bpf_maps(struct xsk_socket *xsk)
434 return bpf_map_update_elem(xsk->xsks_map_fd, &xsk->queue_id,
438 static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
443 err = bpf_get_link_xdp_id(xsk->ifindex, &prog_id,
444 xsk->config.xdp_flags);
449 err = xsk_create_bpf_maps(xsk);
453 err = xsk_load_xdp_prog(xsk);
455 xsk_delete_bpf_maps(xsk);
459 xsk->prog_fd = bpf_prog_get_fd_by_id(prog_id);
460 err = xsk_lookup_bpf_maps(xsk);
467 err = xsk_set_bpf_maps(xsk);
469 xsk_delete_bpf_maps(xsk);
477 int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
478 __u32 queue_id, struct xsk_umem *umem,
479 struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
480 const struct xsk_socket_config *usr_config)
482 void *rx_map = NULL, *tx_map = NULL;
483 struct sockaddr_xdp sxdp = {};
484 struct xdp_mmap_offsets off;
485 struct xdp_options opts;
486 struct xsk_socket *xsk;
490 if (!umem || !xsk_ptr || !rx || !tx)
493 if (umem->refcount) {
494 pr_warning("Error: shared umems not supported by libbpf.\n");
498 xsk = calloc(1, sizeof(*xsk));
502 if (umem->refcount++ > 0) {
503 xsk->fd = socket(AF_XDP, SOCK_RAW, 0);
512 xsk->outstanding_tx = 0;
513 xsk->queue_id = queue_id;
515 xsk->ifindex = if_nametoindex(ifname);
520 strncpy(xsk->ifname, ifname, IFNAMSIZ - 1);
521 xsk->ifname[IFNAMSIZ - 1] = '\0';
523 err = xsk_set_xdp_socket_config(&xsk->config, usr_config);
528 err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING,
529 &xsk->config.rx_size,
530 sizeof(xsk->config.rx_size));
537 err = setsockopt(xsk->fd, SOL_XDP, XDP_TX_RING,
538 &xsk->config.tx_size,
539 sizeof(xsk->config.tx_size));
546 optlen = sizeof(off);
547 err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
554 rx_map = xsk_mmap(NULL, off.rx.desc +
555 xsk->config.rx_size * sizeof(struct xdp_desc),
556 PROT_READ | PROT_WRITE,
557 MAP_SHARED | MAP_POPULATE,
558 xsk->fd, XDP_PGOFF_RX_RING);
559 if (rx_map == MAP_FAILED) {
564 rx->mask = xsk->config.rx_size - 1;
565 rx->size = xsk->config.rx_size;
566 rx->producer = rx_map + off.rx.producer;
567 rx->consumer = rx_map + off.rx.consumer;
568 rx->ring = rx_map + off.rx.desc;
573 tx_map = xsk_mmap(NULL, off.tx.desc +
574 xsk->config.tx_size * sizeof(struct xdp_desc),
575 PROT_READ | PROT_WRITE,
576 MAP_SHARED | MAP_POPULATE,
577 xsk->fd, XDP_PGOFF_TX_RING);
578 if (tx_map == MAP_FAILED) {
583 tx->mask = xsk->config.tx_size - 1;
584 tx->size = xsk->config.tx_size;
585 tx->producer = tx_map + off.tx.producer;
586 tx->consumer = tx_map + off.tx.consumer;
587 tx->ring = tx_map + off.tx.desc;
588 tx->cached_cons = xsk->config.tx_size;
592 sxdp.sxdp_family = PF_XDP;
593 sxdp.sxdp_ifindex = xsk->ifindex;
594 sxdp.sxdp_queue_id = xsk->queue_id;
595 sxdp.sxdp_flags = xsk->config.bind_flags;
597 err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp));
605 optlen = sizeof(opts);
606 err = getsockopt(xsk->fd, SOL_XDP, XDP_OPTIONS, &opts, &optlen);
612 xsk->zc = opts.flags & XDP_OPTIONS_ZEROCOPY;
614 if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
615 err = xsk_setup_xdp_prog(xsk);
625 munmap(tx_map, off.tx.desc +
626 xsk->config.tx_size * sizeof(struct xdp_desc));
629 munmap(rx_map, off.rx.desc +
630 xsk->config.rx_size * sizeof(struct xdp_desc));
632 if (--umem->refcount)
639 int xsk_umem__delete(struct xsk_umem *umem)
641 struct xdp_mmap_offsets off;
651 optlen = sizeof(off);
652 err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
654 munmap(umem->fill->ring - off.fr.desc,
655 off.fr.desc + umem->config.fill_size * sizeof(__u64));
656 munmap(umem->comp->ring - off.cr.desc,
657 off.cr.desc + umem->config.comp_size * sizeof(__u64));
666 void xsk_socket__delete(struct xsk_socket *xsk)
668 size_t desc_sz = sizeof(struct xdp_desc);
669 struct xdp_mmap_offsets off;
676 if (xsk->prog_fd != -1) {
677 xsk_delete_bpf_maps(xsk);
681 optlen = sizeof(off);
682 err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
685 munmap(xsk->rx->ring - off.rx.desc,
686 off.rx.desc + xsk->config.rx_size * desc_sz);
689 munmap(xsk->tx->ring - off.tx.desc,
690 off.tx.desc + xsk->config.tx_size * desc_sz);
695 xsk->umem->refcount--;
696 /* Do not close an fd that also has an associated umem connected
699 if (xsk->fd != xsk->umem->fd)