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_prod
= *fill
->producer
;
284 /* cached_cons is "size" bigger than the real consumer pointer
285 * See xsk_prod_nb_free
287 fill
->cached_cons
= *fill
->consumer
+ umem
->config
.fill_size
;
289 map
= mmap(NULL
, off
.cr
.desc
+ umem
->config
.comp_size
* sizeof(__u64
),
290 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_POPULATE
, umem
->fd
,
291 XDP_UMEM_PGOFF_COMPLETION_RING
);
292 if (map
== MAP_FAILED
) {
298 comp
->mask
= umem
->config
.comp_size
- 1;
299 comp
->size
= umem
->config
.comp_size
;
300 comp
->producer
= map
+ off
.cr
.producer
;
301 comp
->consumer
= map
+ off
.cr
.consumer
;
302 comp
->flags
= map
+ off
.cr
.flags
;
303 comp
->ring
= map
+ off
.cr
.desc
;
304 comp
->cached_prod
= *comp
->producer
;
305 comp
->cached_cons
= *comp
->consumer
;
311 munmap(map
, off
.fr
.desc
+ umem
->config
.fill_size
* sizeof(__u64
));
319 struct xsk_umem_config_v1
{
323 __u32 frame_headroom
;
326 int xsk_umem__create_v0_0_2(struct xsk_umem
**umem_ptr
, void *umem_area
,
327 __u64 size
, struct xsk_ring_prod
*fill
,
328 struct xsk_ring_cons
*comp
,
329 const struct xsk_umem_config
*usr_config
)
331 struct xsk_umem_config config
;
333 memcpy(&config
, usr_config
, sizeof(struct xsk_umem_config_v1
));
336 return xsk_umem__create_v0_0_4(umem_ptr
, umem_area
, size
, fill
, comp
,
339 COMPAT_VERSION(xsk_umem__create_v0_0_2
, xsk_umem__create
, LIBBPF_0
.0
.2)
340 DEFAULT_VERSION(xsk_umem__create_v0_0_4
, xsk_umem__create
, LIBBPF_0
.0
.4)
342 static int xsk_load_xdp_prog(struct xsk_socket
*xsk
)
344 static const int log_buf_size
= 16 * 1024;
345 char log_buf
[log_buf_size
];
348 /* This is the C-program:
349 * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
351 * int ret, index = ctx->rx_queue_index;
353 * // A set entry here means that the correspnding queue_id
354 * // has an active AF_XDP socket bound to it.
355 * ret = bpf_redirect_map(&xsks_map, index, XDP_PASS);
359 * // Fallback for pre-5.3 kernels, not supporting default
360 * // action in the flags parameter.
361 * if (bpf_map_lookup_elem(&xsks_map, &index))
362 * return bpf_redirect_map(&xsks_map, index, 0);
366 struct bpf_insn prog
[] = {
367 /* r2 = *(u32 *)(r1 + 16) */
368 BPF_LDX_MEM(BPF_W
, BPF_REG_2
, BPF_REG_1
, 16),
369 /* *(u32 *)(r10 - 4) = r2 */
370 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_2
, -4),
372 BPF_LD_MAP_FD(BPF_REG_1
, xsk
->xsks_map_fd
),
374 BPF_MOV64_IMM(BPF_REG_3
, 2),
375 /* call bpf_redirect_map */
376 BPF_EMIT_CALL(BPF_FUNC_redirect_map
),
377 /* if w0 != 0 goto pc+13 */
378 BPF_JMP32_IMM(BPF_JSGT
, BPF_REG_0
, 0, 13),
380 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
382 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4),
384 BPF_LD_MAP_FD(BPF_REG_1
, xsk
->xsks_map_fd
),
385 /* call bpf_map_lookup_elem */
386 BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem
),
388 BPF_MOV64_REG(BPF_REG_1
, BPF_REG_0
),
390 BPF_MOV64_IMM(BPF_REG_0
, 2),
391 /* if r1 == 0 goto pc+5 */
392 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_1
, 0, 5),
393 /* r2 = *(u32 *)(r10 - 4) */
394 BPF_LDX_MEM(BPF_W
, BPF_REG_2
, BPF_REG_10
, -4),
396 BPF_LD_MAP_FD(BPF_REG_1
, xsk
->xsks_map_fd
),
398 BPF_MOV64_IMM(BPF_REG_3
, 0),
399 /* call bpf_redirect_map */
400 BPF_EMIT_CALL(BPF_FUNC_redirect_map
),
401 /* The jumps are to this instruction */
404 size_t insns_cnt
= sizeof(prog
) / sizeof(struct bpf_insn
);
406 prog_fd
= bpf_load_program(BPF_PROG_TYPE_XDP
, prog
, insns_cnt
,
407 "LGPL-2.1 or BSD-2-Clause", 0, log_buf
,
410 pr_warn("BPF log buffer:\n%s", log_buf
);
414 err
= bpf_set_link_xdp_fd(xsk
->ifindex
, prog_fd
, xsk
->config
.xdp_flags
);
420 xsk
->prog_fd
= prog_fd
;
424 static int xsk_get_max_queues(struct xsk_socket
*xsk
)
426 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
427 struct ifreq ifr
= {};
430 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
434 ifr
.ifr_data
= (void *)&channels
;
435 memcpy(ifr
.ifr_name
, xsk
->ifname
, IFNAMSIZ
- 1);
436 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
437 err
= ioctl(fd
, SIOCETHTOOL
, &ifr
);
438 if (err
&& errno
!= EOPNOTSUPP
) {
444 /* If the device says it has no channels, then all traffic
445 * is sent to a single stream, so max queues = 1.
449 /* Take the max of rx, tx, combined. Drivers return
450 * the number of channels in different ways.
452 ret
= max(channels
.max_rx
, channels
.max_tx
);
453 ret
= max(ret
, (int)channels
.max_combined
);
461 static int xsk_create_bpf_maps(struct xsk_socket
*xsk
)
466 max_queues
= xsk_get_max_queues(xsk
);
470 fd
= bpf_create_map_name(BPF_MAP_TYPE_XSKMAP
, "xsks_map",
471 sizeof(int), sizeof(int), max_queues
, 0);
475 xsk
->xsks_map_fd
= fd
;
480 static void xsk_delete_bpf_maps(struct xsk_socket
*xsk
)
482 bpf_map_delete_elem(xsk
->xsks_map_fd
, &xsk
->queue_id
);
483 close(xsk
->xsks_map_fd
);
486 static int xsk_lookup_bpf_maps(struct xsk_socket
*xsk
)
488 __u32 i
, *map_ids
, num_maps
, prog_len
= sizeof(struct bpf_prog_info
);
489 __u32 map_len
= sizeof(struct bpf_map_info
);
490 struct bpf_prog_info prog_info
= {};
491 struct bpf_map_info map_info
;
494 err
= bpf_obj_get_info_by_fd(xsk
->prog_fd
, &prog_info
, &prog_len
);
498 num_maps
= prog_info
.nr_map_ids
;
500 map_ids
= calloc(prog_info
.nr_map_ids
, sizeof(*map_ids
));
504 memset(&prog_info
, 0, prog_len
);
505 prog_info
.nr_map_ids
= num_maps
;
506 prog_info
.map_ids
= (__u64
)(unsigned long)map_ids
;
508 err
= bpf_obj_get_info_by_fd(xsk
->prog_fd
, &prog_info
, &prog_len
);
512 xsk
->xsks_map_fd
= -1;
514 for (i
= 0; i
< prog_info
.nr_map_ids
; i
++) {
515 fd
= bpf_map_get_fd_by_id(map_ids
[i
]);
519 err
= bpf_obj_get_info_by_fd(fd
, &map_info
, &map_len
);
525 if (!strcmp(map_info
.name
, "xsks_map")) {
526 xsk
->xsks_map_fd
= fd
;
534 if (xsk
->xsks_map_fd
== -1)
542 static int xsk_set_bpf_maps(struct xsk_socket
*xsk
)
544 return bpf_map_update_elem(xsk
->xsks_map_fd
, &xsk
->queue_id
,
548 static int xsk_setup_xdp_prog(struct xsk_socket
*xsk
)
553 err
= bpf_get_link_xdp_id(xsk
->ifindex
, &prog_id
,
554 xsk
->config
.xdp_flags
);
559 err
= xsk_create_bpf_maps(xsk
);
563 err
= xsk_load_xdp_prog(xsk
);
565 xsk_delete_bpf_maps(xsk
);
569 xsk
->prog_fd
= bpf_prog_get_fd_by_id(prog_id
);
570 if (xsk
->prog_fd
< 0)
572 err
= xsk_lookup_bpf_maps(xsk
);
580 err
= xsk_set_bpf_maps(xsk
);
582 xsk_delete_bpf_maps(xsk
);
590 int xsk_socket__create(struct xsk_socket
**xsk_ptr
, const char *ifname
,
591 __u32 queue_id
, struct xsk_umem
*umem
,
592 struct xsk_ring_cons
*rx
, struct xsk_ring_prod
*tx
,
593 const struct xsk_socket_config
*usr_config
)
595 void *rx_map
= NULL
, *tx_map
= NULL
;
596 struct sockaddr_xdp sxdp
= {};
597 struct xdp_mmap_offsets off
;
598 struct xsk_socket
*xsk
;
601 if (!umem
|| !xsk_ptr
|| !(rx
|| tx
))
604 xsk
= calloc(1, sizeof(*xsk
));
608 err
= xsk_set_xdp_socket_config(&xsk
->config
, usr_config
);
612 if (umem
->refcount
&&
613 !(xsk
->config
.libbpf_flags
& XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD
)) {
614 pr_warn("Error: shared umems not supported by libbpf supplied XDP program.\n");
619 if (umem
->refcount
++ > 0) {
620 xsk
->fd
= socket(AF_XDP
, SOCK_RAW
, 0);
629 xsk
->outstanding_tx
= 0;
630 xsk
->queue_id
= queue_id
;
632 xsk
->ifindex
= if_nametoindex(ifname
);
637 memcpy(xsk
->ifname
, ifname
, IFNAMSIZ
- 1);
638 xsk
->ifname
[IFNAMSIZ
- 1] = '\0';
641 err
= setsockopt(xsk
->fd
, SOL_XDP
, XDP_RX_RING
,
642 &xsk
->config
.rx_size
,
643 sizeof(xsk
->config
.rx_size
));
650 err
= setsockopt(xsk
->fd
, SOL_XDP
, XDP_TX_RING
,
651 &xsk
->config
.tx_size
,
652 sizeof(xsk
->config
.tx_size
));
659 err
= xsk_get_mmap_offsets(xsk
->fd
, &off
);
666 rx_map
= mmap(NULL
, off
.rx
.desc
+
667 xsk
->config
.rx_size
* sizeof(struct xdp_desc
),
668 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_POPULATE
,
669 xsk
->fd
, XDP_PGOFF_RX_RING
);
670 if (rx_map
== MAP_FAILED
) {
675 rx
->mask
= xsk
->config
.rx_size
- 1;
676 rx
->size
= xsk
->config
.rx_size
;
677 rx
->producer
= rx_map
+ off
.rx
.producer
;
678 rx
->consumer
= rx_map
+ off
.rx
.consumer
;
679 rx
->flags
= rx_map
+ off
.rx
.flags
;
680 rx
->ring
= rx_map
+ off
.rx
.desc
;
681 rx
->cached_prod
= *rx
->producer
;
682 rx
->cached_cons
= *rx
->consumer
;
687 tx_map
= mmap(NULL
, off
.tx
.desc
+
688 xsk
->config
.tx_size
* sizeof(struct xdp_desc
),
689 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_POPULATE
,
690 xsk
->fd
, XDP_PGOFF_TX_RING
);
691 if (tx_map
== MAP_FAILED
) {
696 tx
->mask
= xsk
->config
.tx_size
- 1;
697 tx
->size
= xsk
->config
.tx_size
;
698 tx
->producer
= tx_map
+ off
.tx
.producer
;
699 tx
->consumer
= tx_map
+ off
.tx
.consumer
;
700 tx
->flags
= tx_map
+ off
.tx
.flags
;
701 tx
->ring
= tx_map
+ off
.tx
.desc
;
702 tx
->cached_prod
= *tx
->producer
;
703 /* cached_cons is r->size bigger than the real consumer pointer
704 * See xsk_prod_nb_free
706 tx
->cached_cons
= *tx
->consumer
+ xsk
->config
.tx_size
;
710 sxdp
.sxdp_family
= PF_XDP
;
711 sxdp
.sxdp_ifindex
= xsk
->ifindex
;
712 sxdp
.sxdp_queue_id
= xsk
->queue_id
;
713 if (umem
->refcount
> 1) {
714 sxdp
.sxdp_flags
= XDP_SHARED_UMEM
;
715 sxdp
.sxdp_shared_umem_fd
= umem
->fd
;
717 sxdp
.sxdp_flags
= xsk
->config
.bind_flags
;
720 err
= bind(xsk
->fd
, (struct sockaddr
*)&sxdp
, sizeof(sxdp
));
728 if (!(xsk
->config
.libbpf_flags
& XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD
)) {
729 err
= xsk_setup_xdp_prog(xsk
);
739 munmap(tx_map
, off
.tx
.desc
+
740 xsk
->config
.tx_size
* sizeof(struct xdp_desc
));
743 munmap(rx_map
, off
.rx
.desc
+
744 xsk
->config
.rx_size
* sizeof(struct xdp_desc
));
746 if (--umem
->refcount
)
753 int xsk_umem__delete(struct xsk_umem
*umem
)
755 struct xdp_mmap_offsets off
;
764 err
= xsk_get_mmap_offsets(umem
->fd
, &off
);
766 munmap(umem
->fill
->ring
- off
.fr
.desc
,
767 off
.fr
.desc
+ umem
->config
.fill_size
* sizeof(__u64
));
768 munmap(umem
->comp
->ring
- off
.cr
.desc
,
769 off
.cr
.desc
+ umem
->config
.comp_size
* sizeof(__u64
));
778 void xsk_socket__delete(struct xsk_socket
*xsk
)
780 size_t desc_sz
= sizeof(struct xdp_desc
);
781 struct xdp_mmap_offsets off
;
787 if (xsk
->prog_fd
!= -1) {
788 xsk_delete_bpf_maps(xsk
);
792 err
= xsk_get_mmap_offsets(xsk
->fd
, &off
);
795 munmap(xsk
->rx
->ring
- off
.rx
.desc
,
796 off
.rx
.desc
+ xsk
->config
.rx_size
* desc_sz
);
799 munmap(xsk
->tx
->ring
- off
.tx
.desc
,
800 off
.tx
.desc
+ xsk
->config
.tx_size
* desc_sz
);
805 xsk
->umem
->refcount
--;
806 /* Do not close an fd that also has an associated umem connected
809 if (xsk
->fd
!= xsk
->umem
->fd
)