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/kernel.h>
24 #include <linux/list.h>
25 #include <linux/sockios.h>
27 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
34 #include "libbpf_internal.h"
50 struct xsk_ring_prod *fill_save;
51 struct xsk_ring_cons *comp_save;
53 struct xsk_umem_config config;
56 struct list_head ctx_list;
60 struct xsk_ring_prod *fill;
61 struct xsk_ring_cons *comp;
63 struct xsk_umem *umem;
66 struct list_head list;
69 char ifname[IFNAMSIZ];
73 struct xsk_ring_cons *rx;
74 struct xsk_ring_prod *tx;
77 struct xsk_socket_config config;
82 bool xdp_prog_attached;
87 /* Up until and including Linux 5.3 */
88 struct xdp_ring_offset_v1 {
94 /* Up until and including Linux 5.3 */
95 struct xdp_mmap_offsets_v1 {
96 struct xdp_ring_offset_v1 rx;
97 struct xdp_ring_offset_v1 tx;
98 struct xdp_ring_offset_v1 fr;
99 struct xdp_ring_offset_v1 cr;
102 int xsk_umem__fd(const struct xsk_umem *umem)
104 return umem ? umem->fd : -EINVAL;
107 int xsk_socket__fd(const struct xsk_socket *xsk)
109 return xsk ? xsk->fd : -EINVAL;
112 static bool xsk_page_aligned(void *buffer)
114 unsigned long addr = (unsigned long)buffer;
116 return !(addr & (getpagesize() - 1));
119 static void xsk_set_umem_config(struct xsk_umem_config *cfg,
120 const struct xsk_umem_config *usr_cfg)
123 cfg->fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
124 cfg->comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
125 cfg->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
126 cfg->frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM;
127 cfg->flags = XSK_UMEM__DEFAULT_FLAGS;
131 cfg->fill_size = usr_cfg->fill_size;
132 cfg->comp_size = usr_cfg->comp_size;
133 cfg->frame_size = usr_cfg->frame_size;
134 cfg->frame_headroom = usr_cfg->frame_headroom;
135 cfg->flags = usr_cfg->flags;
138 static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg,
139 const struct xsk_socket_config *usr_cfg)
142 cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
143 cfg->tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
144 cfg->libbpf_flags = 0;
150 if (usr_cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)
153 cfg->rx_size = usr_cfg->rx_size;
154 cfg->tx_size = usr_cfg->tx_size;
155 cfg->libbpf_flags = usr_cfg->libbpf_flags;
156 cfg->xdp_flags = usr_cfg->xdp_flags;
157 cfg->bind_flags = usr_cfg->bind_flags;
162 static void xsk_mmap_offsets_v1(struct xdp_mmap_offsets *off)
164 struct xdp_mmap_offsets_v1 off_v1;
166 /* getsockopt on a kernel <= 5.3 has no flags fields.
167 * Copy over the offsets to the correct places in the >=5.4 format
168 * and put the flags where they would have been on that kernel.
170 memcpy(&off_v1, off, sizeof(off_v1));
172 off->rx.producer = off_v1.rx.producer;
173 off->rx.consumer = off_v1.rx.consumer;
174 off->rx.desc = off_v1.rx.desc;
175 off->rx.flags = off_v1.rx.consumer + sizeof(__u32);
177 off->tx.producer = off_v1.tx.producer;
178 off->tx.consumer = off_v1.tx.consumer;
179 off->tx.desc = off_v1.tx.desc;
180 off->tx.flags = off_v1.tx.consumer + sizeof(__u32);
182 off->fr.producer = off_v1.fr.producer;
183 off->fr.consumer = off_v1.fr.consumer;
184 off->fr.desc = off_v1.fr.desc;
185 off->fr.flags = off_v1.fr.consumer + sizeof(__u32);
187 off->cr.producer = off_v1.cr.producer;
188 off->cr.consumer = off_v1.cr.consumer;
189 off->cr.desc = off_v1.cr.desc;
190 off->cr.flags = off_v1.cr.consumer + sizeof(__u32);
193 static int xsk_get_mmap_offsets(int fd, struct xdp_mmap_offsets *off)
198 optlen = sizeof(*off);
199 err = getsockopt(fd, SOL_XDP, XDP_MMAP_OFFSETS, off, &optlen);
203 if (optlen == sizeof(*off))
206 if (optlen == sizeof(struct xdp_mmap_offsets_v1)) {
207 xsk_mmap_offsets_v1(off);
214 static int xsk_create_umem_rings(struct xsk_umem *umem, int fd,
215 struct xsk_ring_prod *fill,
216 struct xsk_ring_cons *comp)
218 struct xdp_mmap_offsets off;
222 err = setsockopt(fd, SOL_XDP, XDP_UMEM_FILL_RING,
223 &umem->config.fill_size,
224 sizeof(umem->config.fill_size));
228 err = setsockopt(fd, SOL_XDP, XDP_UMEM_COMPLETION_RING,
229 &umem->config.comp_size,
230 sizeof(umem->config.comp_size));
234 err = xsk_get_mmap_offsets(fd, &off);
238 map = mmap(NULL, off.fr.desc + umem->config.fill_size * sizeof(__u64),
239 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
240 XDP_UMEM_PGOFF_FILL_RING);
241 if (map == MAP_FAILED)
244 fill->mask = umem->config.fill_size - 1;
245 fill->size = umem->config.fill_size;
246 fill->producer = map + off.fr.producer;
247 fill->consumer = map + off.fr.consumer;
248 fill->flags = map + off.fr.flags;
249 fill->ring = map + off.fr.desc;
250 fill->cached_cons = umem->config.fill_size;
252 map = mmap(NULL, off.cr.desc + umem->config.comp_size * sizeof(__u64),
253 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
254 XDP_UMEM_PGOFF_COMPLETION_RING);
255 if (map == MAP_FAILED) {
260 comp->mask = umem->config.comp_size - 1;
261 comp->size = umem->config.comp_size;
262 comp->producer = map + off.cr.producer;
263 comp->consumer = map + off.cr.consumer;
264 comp->flags = map + off.cr.flags;
265 comp->ring = map + off.cr.desc;
270 munmap(map, off.fr.desc + umem->config.fill_size * sizeof(__u64));
274 int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,
275 __u64 size, struct xsk_ring_prod *fill,
276 struct xsk_ring_cons *comp,
277 const struct xsk_umem_config *usr_config)
279 struct xdp_umem_reg mr;
280 struct xsk_umem *umem;
283 if (!umem_area || !umem_ptr || !fill || !comp)
285 if (!size && !xsk_page_aligned(umem_area))
288 umem = calloc(1, sizeof(*umem));
292 umem->fd = socket(AF_XDP, SOCK_RAW, 0);
298 umem->umem_area = umem_area;
299 INIT_LIST_HEAD(&umem->ctx_list);
300 xsk_set_umem_config(&umem->config, usr_config);
302 memset(&mr, 0, sizeof(mr));
303 mr.addr = (uintptr_t)umem_area;
305 mr.chunk_size = umem->config.frame_size;
306 mr.headroom = umem->config.frame_headroom;
307 mr.flags = umem->config.flags;
309 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr));
315 err = xsk_create_umem_rings(umem, umem->fd, fill, comp);
319 umem->fill_save = fill;
320 umem->comp_save = comp;
331 struct xsk_umem_config_v1 {
335 __u32 frame_headroom;
338 int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,
339 __u64 size, struct xsk_ring_prod *fill,
340 struct xsk_ring_cons *comp,
341 const struct xsk_umem_config *usr_config)
343 struct xsk_umem_config config;
345 memcpy(&config, usr_config, sizeof(struct xsk_umem_config_v1));
348 return xsk_umem__create_v0_0_4(umem_ptr, umem_area, size, fill, comp,
351 COMPAT_VERSION(xsk_umem__create_v0_0_2, xsk_umem__create, LIBBPF_0.0.2)
352 DEFAULT_VERSION(xsk_umem__create_v0_0_4, xsk_umem__create, LIBBPF_0.0.4)
354 static int xsk_load_xdp_prog(struct xsk_socket *xsk)
356 static const int log_buf_size = 16 * 1024;
357 struct xsk_ctx *ctx = xsk->ctx;
358 char log_buf[log_buf_size];
361 /* This is the C-program:
362 * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
364 * int ret, index = ctx->rx_queue_index;
366 * // A set entry here means that the correspnding queue_id
367 * // has an active AF_XDP socket bound to it.
368 * ret = bpf_redirect_map(&xsks_map, index, XDP_PASS);
372 * // Fallback for pre-5.3 kernels, not supporting default
373 * // action in the flags parameter.
374 * if (bpf_map_lookup_elem(&xsks_map, &index))
375 * return bpf_redirect_map(&xsks_map, index, 0);
379 struct bpf_insn prog[] = {
380 /* r2 = *(u32 *)(r1 + 16) */
381 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 16),
382 /* *(u32 *)(r10 - 4) = r2 */
383 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -4),
385 BPF_LD_MAP_FD(BPF_REG_1, ctx->xsks_map_fd),
387 BPF_MOV64_IMM(BPF_REG_3, 2),
388 /* call bpf_redirect_map */
389 BPF_EMIT_CALL(BPF_FUNC_redirect_map),
390 /* if w0 != 0 goto pc+13 */
391 BPF_JMP32_IMM(BPF_JSGT, BPF_REG_0, 0, 13),
393 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
395 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
397 BPF_LD_MAP_FD(BPF_REG_1, ctx->xsks_map_fd),
398 /* call bpf_map_lookup_elem */
399 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
401 BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
403 BPF_MOV64_IMM(BPF_REG_0, 2),
404 /* if r1 == 0 goto pc+5 */
405 BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 5),
406 /* r2 = *(u32 *)(r10 - 4) */
407 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, -4),
409 BPF_LD_MAP_FD(BPF_REG_1, ctx->xsks_map_fd),
411 BPF_MOV64_IMM(BPF_REG_3, 0),
412 /* call bpf_redirect_map */
413 BPF_EMIT_CALL(BPF_FUNC_redirect_map),
414 /* The jumps are to this instruction */
417 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
419 prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, prog, insns_cnt,
420 "LGPL-2.1 or BSD-2-Clause", 0, log_buf,
423 pr_warn("BPF log buffer:\n%s", log_buf);
427 err = bpf_set_link_xdp_fd(xsk->ctx->ifindex, prog_fd,
428 xsk->config.xdp_flags);
434 ctx->prog_fd = prog_fd;
438 static int xsk_get_max_queues(struct xsk_socket *xsk)
440 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
441 struct xsk_ctx *ctx = xsk->ctx;
442 struct ifreq ifr = {};
445 fd = socket(AF_INET, SOCK_DGRAM, 0);
449 ifr.ifr_data = (void *)&channels;
450 memcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ - 1);
451 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
452 err = ioctl(fd, SIOCETHTOOL, &ifr);
453 if (err && errno != EOPNOTSUPP) {
459 /* If the device says it has no channels, then all traffic
460 * is sent to a single stream, so max queues = 1.
464 /* Take the max of rx, tx, combined. Drivers return
465 * the number of channels in different ways.
467 ret = max(channels.max_rx, channels.max_tx);
468 ret = max(ret, (int)channels.max_combined);
476 static int xsk_create_bpf_maps(struct xsk_socket *xsk)
478 struct xsk_ctx *ctx = xsk->ctx;
482 max_queues = xsk_get_max_queues(xsk);
486 fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map",
487 sizeof(int), sizeof(int), max_queues, 0);
491 ctx->xsks_map_fd = fd;
496 static void xsk_delete_bpf_maps(struct xsk_socket *xsk)
498 struct xsk_ctx *ctx = xsk->ctx;
500 bpf_map_delete_elem(ctx->xsks_map_fd, &ctx->queue_id);
501 close(ctx->xsks_map_fd);
504 static int xsk_lookup_bpf_maps(struct xsk_socket *xsk)
506 __u32 i, *map_ids, num_maps, prog_len = sizeof(struct bpf_prog_info);
507 __u32 map_len = sizeof(struct bpf_map_info);
508 struct bpf_prog_info prog_info = {};
509 struct xsk_ctx *ctx = xsk->ctx;
510 struct bpf_map_info map_info;
513 err = bpf_obj_get_info_by_fd(ctx->prog_fd, &prog_info, &prog_len);
517 num_maps = prog_info.nr_map_ids;
519 map_ids = calloc(prog_info.nr_map_ids, sizeof(*map_ids));
523 memset(&prog_info, 0, prog_len);
524 prog_info.nr_map_ids = num_maps;
525 prog_info.map_ids = (__u64)(unsigned long)map_ids;
527 err = bpf_obj_get_info_by_fd(ctx->prog_fd, &prog_info, &prog_len);
531 ctx->xsks_map_fd = -1;
533 for (i = 0; i < prog_info.nr_map_ids; i++) {
534 fd = bpf_map_get_fd_by_id(map_ids[i]);
538 err = bpf_obj_get_info_by_fd(fd, &map_info, &map_len);
544 if (!strcmp(map_info.name, "xsks_map")) {
545 ctx->xsks_map_fd = fd;
553 if (ctx->xsks_map_fd == -1)
561 static int xsk_set_bpf_maps(struct xsk_socket *xsk)
563 struct xsk_ctx *ctx = xsk->ctx;
565 return bpf_map_update_elem(ctx->xsks_map_fd, &ctx->queue_id,
569 static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
571 struct xsk_ctx *ctx = xsk->ctx;
575 err = bpf_get_link_xdp_id(ctx->ifindex, &prog_id,
576 xsk->config.xdp_flags);
581 err = xsk_create_bpf_maps(xsk);
585 err = xsk_load_xdp_prog(xsk);
587 xsk_delete_bpf_maps(xsk);
591 ctx->prog_fd = bpf_prog_get_fd_by_id(prog_id);
592 if (ctx->prog_fd < 0)
594 err = xsk_lookup_bpf_maps(xsk);
602 err = xsk_set_bpf_maps(xsk);
604 xsk_delete_bpf_maps(xsk);
612 static struct xsk_ctx *xsk_get_ctx(struct xsk_umem *umem, int ifindex,
617 if (list_empty(&umem->ctx_list))
620 list_for_each_entry(ctx, &umem->ctx_list, list) {
621 if (ctx->ifindex == ifindex && ctx->queue_id == queue_id) {
630 static void xsk_put_ctx(struct xsk_ctx *ctx)
632 struct xsk_umem *umem = ctx->umem;
633 struct xdp_mmap_offsets off;
636 if (--ctx->refcount == 0) {
637 err = xsk_get_mmap_offsets(umem->fd, &off);
639 munmap(ctx->fill->ring - off.fr.desc,
640 off.fr.desc + umem->config.fill_size *
642 munmap(ctx->comp->ring - off.cr.desc,
643 off.cr.desc + umem->config.comp_size *
647 list_del(&ctx->list);
652 static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
653 struct xsk_umem *umem, int ifindex,
654 const char *ifname, __u32 queue_id,
655 struct xsk_ring_prod *fill,
656 struct xsk_ring_cons *comp)
661 ctx = calloc(1, sizeof(*ctx));
665 if (!umem->fill_save) {
666 err = xsk_create_umem_rings(umem, xsk->fd, fill, comp);
671 } else if (umem->fill_save != fill || umem->comp_save != comp) {
672 /* Copy over rings to new structs. */
673 memcpy(fill, umem->fill_save, sizeof(*fill));
674 memcpy(comp, umem->comp_save, sizeof(*comp));
677 ctx->ifindex = ifindex;
680 ctx->queue_id = queue_id;
681 memcpy(ctx->ifname, ifname, IFNAMSIZ - 1);
682 ctx->ifname[IFNAMSIZ - 1] = '\0';
684 umem->fill_save = NULL;
685 umem->comp_save = NULL;
688 list_add(&ctx->list, &umem->ctx_list);
692 int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
694 __u32 queue_id, struct xsk_umem *umem,
695 struct xsk_ring_cons *rx,
696 struct xsk_ring_prod *tx,
697 struct xsk_ring_prod *fill,
698 struct xsk_ring_cons *comp,
699 const struct xsk_socket_config *usr_config)
701 void *rx_map = NULL, *tx_map = NULL;
702 struct sockaddr_xdp sxdp = {};
703 struct xdp_mmap_offsets off;
704 struct xsk_socket *xsk;
708 if (!umem || !xsk_ptr || !(rx || tx))
711 xsk = calloc(1, sizeof(*xsk));
715 err = xsk_set_xdp_socket_config(&xsk->config, usr_config);
719 xsk->outstanding_tx = 0;
720 ifindex = if_nametoindex(ifname);
726 if (umem->refcount++ > 0) {
727 xsk->fd = socket(AF_XDP, SOCK_RAW, 0);
736 ctx = xsk_get_ctx(umem, ifindex, queue_id);
738 if (!fill || !comp) {
743 ctx = xsk_create_ctx(xsk, umem, ifindex, ifname, queue_id,
753 err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING,
754 &xsk->config.rx_size,
755 sizeof(xsk->config.rx_size));
762 err = setsockopt(xsk->fd, SOL_XDP, XDP_TX_RING,
763 &xsk->config.tx_size,
764 sizeof(xsk->config.tx_size));
771 err = xsk_get_mmap_offsets(xsk->fd, &off);
778 rx_map = mmap(NULL, off.rx.desc +
779 xsk->config.rx_size * sizeof(struct xdp_desc),
780 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
781 xsk->fd, XDP_PGOFF_RX_RING);
782 if (rx_map == MAP_FAILED) {
787 rx->mask = xsk->config.rx_size - 1;
788 rx->size = xsk->config.rx_size;
789 rx->producer = rx_map + off.rx.producer;
790 rx->consumer = rx_map + off.rx.consumer;
791 rx->flags = rx_map + off.rx.flags;
792 rx->ring = rx_map + off.rx.desc;
793 rx->cached_prod = *rx->producer;
794 rx->cached_cons = *rx->consumer;
799 tx_map = mmap(NULL, off.tx.desc +
800 xsk->config.tx_size * sizeof(struct xdp_desc),
801 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
802 xsk->fd, XDP_PGOFF_TX_RING);
803 if (tx_map == MAP_FAILED) {
808 tx->mask = xsk->config.tx_size - 1;
809 tx->size = xsk->config.tx_size;
810 tx->producer = tx_map + off.tx.producer;
811 tx->consumer = tx_map + off.tx.consumer;
812 tx->flags = tx_map + off.tx.flags;
813 tx->ring = tx_map + off.tx.desc;
814 tx->cached_prod = *tx->producer;
815 /* cached_cons is r->size bigger than the real consumer pointer
816 * See xsk_prod_nb_free
818 tx->cached_cons = *tx->consumer + xsk->config.tx_size;
822 sxdp.sxdp_family = PF_XDP;
823 sxdp.sxdp_ifindex = ctx->ifindex;
824 sxdp.sxdp_queue_id = ctx->queue_id;
825 if (umem->refcount > 1) {
826 sxdp.sxdp_flags |= XDP_SHARED_UMEM;
827 sxdp.sxdp_shared_umem_fd = umem->fd;
829 sxdp.sxdp_flags = xsk->config.bind_flags;
832 err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp));
840 if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
841 err = xsk_setup_xdp_prog(xsk);
851 munmap(tx_map, off.tx.desc +
852 xsk->config.tx_size * sizeof(struct xdp_desc));
855 munmap(rx_map, off.rx.desc +
856 xsk->config.rx_size * sizeof(struct xdp_desc));
860 if (--umem->refcount)
867 int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
868 __u32 queue_id, struct xsk_umem *umem,
869 struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
870 const struct xsk_socket_config *usr_config)
872 return xsk_socket__create_shared(xsk_ptr, ifname, queue_id, umem,
873 rx, tx, umem->fill_save,
874 umem->comp_save, usr_config);
877 int xsk_umem__delete(struct xsk_umem *umem)
891 void xsk_socket__delete(struct xsk_socket *xsk)
893 size_t desc_sz = sizeof(struct xdp_desc);
894 struct xsk_ctx *ctx = xsk->ctx;
895 struct xdp_mmap_offsets off;
901 if (ctx->prog_fd != -1) {
902 xsk_delete_bpf_maps(xsk);
906 err = xsk_get_mmap_offsets(xsk->fd, &off);
909 munmap(xsk->rx->ring - off.rx.desc,
910 off.rx.desc + xsk->config.rx_size * desc_sz);
913 munmap(xsk->tx->ring - off.tx.desc,
914 off.tx.desc + xsk->config.tx_size * desc_sz);
920 ctx->umem->refcount--;
921 /* Do not close an fd that also has an associated umem connected
924 if (xsk->fd != ctx->umem->fd)