Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.6 800 lines 18 kB view raw
1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 3/* 4 * AF_XDP user-space access library. 5 * 6 * Copyright(c) 2018 - 2019 Intel Corporation. 7 * 8 * Author(s): Magnus Karlsson <magnus.karlsson@intel.com> 9 */ 10 11#include <errno.h> 12#include <stdlib.h> 13#include <string.h> 14#include <unistd.h> 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> 24#include <net/if.h> 25#include <sys/ioctl.h> 26#include <sys/mman.h> 27#include <sys/socket.h> 28#include <sys/types.h> 29 30#include "bpf.h" 31#include "libbpf.h" 32#include "libbpf_internal.h" 33#include "xsk.h" 34 35/* make sure libbpf doesn't use kernel-only integer typedefs */ 36#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 37 38#ifndef SOL_XDP 39 #define SOL_XDP 283 40#endif 41 42#ifndef AF_XDP 43 #define AF_XDP 44 44#endif 45 46#ifndef PF_XDP 47 #define PF_XDP AF_XDP 48#endif 49 50struct xsk_umem { 51 struct xsk_ring_prod *fill; 52 struct xsk_ring_cons *comp; 53 char *umem_area; 54 struct xsk_umem_config config; 55 int fd; 56 int refcount; 57}; 58 59struct xsk_socket { 60 struct xsk_ring_cons *rx; 61 struct xsk_ring_prod *tx; 62 __u64 outstanding_tx; 63 struct xsk_umem *umem; 64 struct xsk_socket_config config; 65 int fd; 66 int ifindex; 67 int prog_fd; 68 int xsks_map_fd; 69 __u32 queue_id; 70 char ifname[IFNAMSIZ]; 71}; 72 73struct xsk_nl_info { 74 bool xdp_prog_attached; 75 int ifindex; 76 int fd; 77}; 78 79/* Up until and including Linux 5.3 */ 80struct xdp_ring_offset_v1 { 81 __u64 producer; 82 __u64 consumer; 83 __u64 desc; 84}; 85 86/* Up until and including Linux 5.3 */ 87struct 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; 92}; 93 94int xsk_umem__fd(const struct xsk_umem *umem) 95{ 96 return umem ? umem->fd : -EINVAL; 97} 98 99int xsk_socket__fd(const struct xsk_socket *xsk) 100{ 101 return xsk ? xsk->fd : -EINVAL; 102} 103 104static bool xsk_page_aligned(void *buffer) 105{ 106 unsigned long addr = (unsigned long)buffer; 107 108 return !(addr & (getpagesize() - 1)); 109} 110 111static void xsk_set_umem_config(struct xsk_umem_config *cfg, 112 const struct xsk_umem_config *usr_cfg) 113{ 114 if (!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; 120 return; 121 } 122 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; 128} 129 130static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg, 131 const struct xsk_socket_config *usr_cfg) 132{ 133 if (!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; 137 cfg->xdp_flags = 0; 138 cfg->bind_flags = 0; 139 return 0; 140 } 141 142 if (usr_cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD) 143 return -EINVAL; 144 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; 150 151 return 0; 152} 153 154static void xsk_mmap_offsets_v1(struct xdp_mmap_offsets *off) 155{ 156 struct xdp_mmap_offsets_v1 off_v1; 157 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. 161 */ 162 memcpy(&off_v1, off, sizeof(off_v1)); 163 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); 168 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); 173 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); 178 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); 183} 184 185static int xsk_get_mmap_offsets(int fd, struct xdp_mmap_offsets *off) 186{ 187 socklen_t optlen; 188 int err; 189 190 optlen = sizeof(*off); 191 err = getsockopt(fd, SOL_XDP, XDP_MMAP_OFFSETS, off, &optlen); 192 if (err) 193 return err; 194 195 if (optlen == sizeof(*off)) 196 return 0; 197 198 if (optlen == sizeof(struct xdp_mmap_offsets_v1)) { 199 xsk_mmap_offsets_v1(off); 200 return 0; 201 } 202 203 return -EINVAL; 204} 205 206int 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) 210{ 211 struct xdp_mmap_offsets off; 212 struct xdp_umem_reg mr; 213 struct xsk_umem *umem; 214 void *map; 215 int err; 216 217 if (!umem_area || !umem_ptr || !fill || !comp) 218 return -EFAULT; 219 if (!size && !xsk_page_aligned(umem_area)) 220 return -EINVAL; 221 222 umem = calloc(1, sizeof(*umem)); 223 if (!umem) 224 return -ENOMEM; 225 226 umem->fd = socket(AF_XDP, SOCK_RAW, 0); 227 if (umem->fd < 0) { 228 err = -errno; 229 goto out_umem_alloc; 230 } 231 232 umem->umem_area = umem_area; 233 xsk_set_umem_config(&umem->config, usr_config); 234 235 memset(&mr, 0, sizeof(mr)); 236 mr.addr = (uintptr_t)umem_area; 237 mr.len = size; 238 mr.chunk_size = umem->config.frame_size; 239 mr.headroom = umem->config.frame_headroom; 240 mr.flags = umem->config.flags; 241 242 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr)); 243 if (err) { 244 err = -errno; 245 goto out_socket; 246 } 247 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_FILL_RING, 248 &umem->config.fill_size, 249 sizeof(umem->config.fill_size)); 250 if (err) { 251 err = -errno; 252 goto out_socket; 253 } 254 err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_COMPLETION_RING, 255 &umem->config.comp_size, 256 sizeof(umem->config.comp_size)); 257 if (err) { 258 err = -errno; 259 goto out_socket; 260 } 261 262 err = xsk_get_mmap_offsets(umem->fd, &off); 263 if (err) { 264 err = -errno; 265 goto out_socket; 266 } 267 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) { 272 err = -errno; 273 goto out_socket; 274 } 275 276 umem->fill = fill; 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; 284 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) { 289 err = -errno; 290 goto out_mmap; 291 } 292 293 umem->comp = comp; 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; 300 301 *umem_ptr = umem; 302 return 0; 303 304out_mmap: 305 munmap(map, off.fr.desc + umem->config.fill_size * sizeof(__u64)); 306out_socket: 307 close(umem->fd); 308out_umem_alloc: 309 free(umem); 310 return err; 311} 312 313struct xsk_umem_config_v1 { 314 __u32 fill_size; 315 __u32 comp_size; 316 __u32 frame_size; 317 __u32 frame_headroom; 318}; 319 320int 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) 324{ 325 struct xsk_umem_config config; 326 327 memcpy(&config, usr_config, sizeof(struct xsk_umem_config_v1)); 328 config.flags = 0; 329 330 return xsk_umem__create_v0_0_4(umem_ptr, umem_area, size, fill, comp, 331 &config); 332} 333COMPAT_VERSION(xsk_umem__create_v0_0_2, xsk_umem__create, LIBBPF_0.0.2) 334DEFAULT_VERSION(xsk_umem__create_v0_0_4, xsk_umem__create, LIBBPF_0.0.4) 335 336static int xsk_load_xdp_prog(struct xsk_socket *xsk) 337{ 338 static const int log_buf_size = 16 * 1024; 339 char log_buf[log_buf_size]; 340 int err, prog_fd; 341 342 /* This is the C-program: 343 * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx) 344 * { 345 * int ret, index = ctx->rx_queue_index; 346 * 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); 350 * if (ret > 0) 351 * return ret; 352 * 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); 357 * return XDP_PASS; 358 * } 359 */ 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), 365 /* r1 = xskmap[] */ 366 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd), 367 /* r3 = XDP_PASS */ 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), 373 /* r2 = r10 */ 374 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 375 /* r2 += -4 */ 376 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), 377 /* r1 = xskmap[] */ 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), 381 /* r1 = r0 */ 382 BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), 383 /* r0 = XDP_PASS */ 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), 389 /* r1 = xskmap[] */ 390 BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd), 391 /* r3 = 0 */ 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 */ 396 BPF_EXIT_INSN(), 397 }; 398 size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); 399 400 prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, prog, insns_cnt, 401 "LGPL-2.1 or BSD-2-Clause", 0, log_buf, 402 log_buf_size); 403 if (prog_fd < 0) { 404 pr_warn("BPF log buffer:\n%s", log_buf); 405 return prog_fd; 406 } 407 408 err = bpf_set_link_xdp_fd(xsk->ifindex, prog_fd, xsk->config.xdp_flags); 409 if (err) { 410 close(prog_fd); 411 return err; 412 } 413 414 xsk->prog_fd = prog_fd; 415 return 0; 416} 417 418static int xsk_get_max_queues(struct xsk_socket *xsk) 419{ 420 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; 421 struct ifreq ifr = {}; 422 int fd, err, ret; 423 424 fd = socket(AF_INET, SOCK_DGRAM, 0); 425 if (fd < 0) 426 return -errno; 427 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) { 433 ret = -errno; 434 goto out; 435 } 436 437 if (err) { 438 /* If the device says it has no channels, then all traffic 439 * is sent to a single stream, so max queues = 1. 440 */ 441 ret = 1; 442 } else { 443 /* Take the max of rx, tx, combined. Drivers return 444 * the number of channels in different ways. 445 */ 446 ret = max(channels.max_rx, channels.max_tx); 447 ret = max(ret, (int)channels.max_combined); 448 } 449 450out: 451 close(fd); 452 return ret; 453} 454 455static int xsk_create_bpf_maps(struct xsk_socket *xsk) 456{ 457 int max_queues; 458 int fd; 459 460 max_queues = xsk_get_max_queues(xsk); 461 if (max_queues < 0) 462 return max_queues; 463 464 fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map", 465 sizeof(int), sizeof(int), max_queues, 0); 466 if (fd < 0) 467 return fd; 468 469 xsk->xsks_map_fd = fd; 470 471 return 0; 472} 473 474static void xsk_delete_bpf_maps(struct xsk_socket *xsk) 475{ 476 bpf_map_delete_elem(xsk->xsks_map_fd, &xsk->queue_id); 477 close(xsk->xsks_map_fd); 478} 479 480static int xsk_lookup_bpf_maps(struct xsk_socket *xsk) 481{ 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; 486 int fd, err; 487 488 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len); 489 if (err) 490 return err; 491 492 num_maps = prog_info.nr_map_ids; 493 494 map_ids = calloc(prog_info.nr_map_ids, sizeof(*map_ids)); 495 if (!map_ids) 496 return -ENOMEM; 497 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; 501 502 err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len); 503 if (err) 504 goto out_map_ids; 505 506 xsk->xsks_map_fd = -1; 507 508 for (i = 0; i < prog_info.nr_map_ids; i++) { 509 fd = bpf_map_get_fd_by_id(map_ids[i]); 510 if (fd < 0) 511 continue; 512 513 err = bpf_obj_get_info_by_fd(fd, &map_info, &map_len); 514 if (err) { 515 close(fd); 516 continue; 517 } 518 519 if (!strcmp(map_info.name, "xsks_map")) { 520 xsk->xsks_map_fd = fd; 521 continue; 522 } 523 524 close(fd); 525 } 526 527 err = 0; 528 if (xsk->xsks_map_fd == -1) 529 err = -ENOENT; 530 531out_map_ids: 532 free(map_ids); 533 return err; 534} 535 536static int xsk_set_bpf_maps(struct xsk_socket *xsk) 537{ 538 return bpf_map_update_elem(xsk->xsks_map_fd, &xsk->queue_id, 539 &xsk->fd, 0); 540} 541 542static int xsk_setup_xdp_prog(struct xsk_socket *xsk) 543{ 544 __u32 prog_id = 0; 545 int err; 546 547 err = bpf_get_link_xdp_id(xsk->ifindex, &prog_id, 548 xsk->config.xdp_flags); 549 if (err) 550 return err; 551 552 if (!prog_id) { 553 err = xsk_create_bpf_maps(xsk); 554 if (err) 555 return err; 556 557 err = xsk_load_xdp_prog(xsk); 558 if (err) { 559 xsk_delete_bpf_maps(xsk); 560 return err; 561 } 562 } else { 563 xsk->prog_fd = bpf_prog_get_fd_by_id(prog_id); 564 if (xsk->prog_fd < 0) 565 return -errno; 566 err = xsk_lookup_bpf_maps(xsk); 567 if (err) { 568 close(xsk->prog_fd); 569 return err; 570 } 571 } 572 573 if (xsk->rx) 574 err = xsk_set_bpf_maps(xsk); 575 if (err) { 576 xsk_delete_bpf_maps(xsk); 577 close(xsk->prog_fd); 578 return err; 579 } 580 581 return 0; 582} 583 584int 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) 588{ 589 void *rx_map = NULL, *tx_map = NULL; 590 struct sockaddr_xdp sxdp = {}; 591 struct xdp_mmap_offsets off; 592 struct xsk_socket *xsk; 593 int err; 594 595 if (!umem || !xsk_ptr || !(rx || tx)) 596 return -EFAULT; 597 598 xsk = calloc(1, sizeof(*xsk)); 599 if (!xsk) 600 return -ENOMEM; 601 602 err = xsk_set_xdp_socket_config(&xsk->config, usr_config); 603 if (err) 604 goto out_xsk_alloc; 605 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"); 609 err = -EBUSY; 610 goto out_xsk_alloc; 611 } 612 613 if (umem->refcount++ > 0) { 614 xsk->fd = socket(AF_XDP, SOCK_RAW, 0); 615 if (xsk->fd < 0) { 616 err = -errno; 617 goto out_xsk_alloc; 618 } 619 } else { 620 xsk->fd = umem->fd; 621 } 622 623 xsk->outstanding_tx = 0; 624 xsk->queue_id = queue_id; 625 xsk->umem = umem; 626 xsk->ifindex = if_nametoindex(ifname); 627 if (!xsk->ifindex) { 628 err = -errno; 629 goto out_socket; 630 } 631 memcpy(xsk->ifname, ifname, IFNAMSIZ - 1); 632 xsk->ifname[IFNAMSIZ - 1] = '\0'; 633 634 if (rx) { 635 err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING, 636 &xsk->config.rx_size, 637 sizeof(xsk->config.rx_size)); 638 if (err) { 639 err = -errno; 640 goto out_socket; 641 } 642 } 643 if (tx) { 644 err = setsockopt(xsk->fd, SOL_XDP, XDP_TX_RING, 645 &xsk->config.tx_size, 646 sizeof(xsk->config.tx_size)); 647 if (err) { 648 err = -errno; 649 goto out_socket; 650 } 651 } 652 653 err = xsk_get_mmap_offsets(xsk->fd, &off); 654 if (err) { 655 err = -errno; 656 goto out_socket; 657 } 658 659 if (rx) { 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) { 665 err = -errno; 666 goto out_socket; 667 } 668 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; 675 } 676 xsk->rx = rx; 677 678 if (tx) { 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) { 684 err = -errno; 685 goto out_mmap_rx; 686 } 687 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; 695 } 696 xsk->tx = tx; 697 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; 704 } else { 705 sxdp.sxdp_flags = xsk->config.bind_flags; 706 } 707 708 err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp)); 709 if (err) { 710 err = -errno; 711 goto out_mmap_tx; 712 } 713 714 xsk->prog_fd = -1; 715 716 if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) { 717 err = xsk_setup_xdp_prog(xsk); 718 if (err) 719 goto out_mmap_tx; 720 } 721 722 *xsk_ptr = xsk; 723 return 0; 724 725out_mmap_tx: 726 if (tx) 727 munmap(tx_map, off.tx.desc + 728 xsk->config.tx_size * sizeof(struct xdp_desc)); 729out_mmap_rx: 730 if (rx) 731 munmap(rx_map, off.rx.desc + 732 xsk->config.rx_size * sizeof(struct xdp_desc)); 733out_socket: 734 if (--umem->refcount) 735 close(xsk->fd); 736out_xsk_alloc: 737 free(xsk); 738 return err; 739} 740 741int xsk_umem__delete(struct xsk_umem *umem) 742{ 743 struct xdp_mmap_offsets off; 744 int err; 745 746 if (!umem) 747 return 0; 748 749 if (umem->refcount) 750 return -EBUSY; 751 752 err = xsk_get_mmap_offsets(umem->fd, &off); 753 if (!err) { 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)); 758 } 759 760 close(umem->fd); 761 free(umem); 762 763 return 0; 764} 765 766void xsk_socket__delete(struct xsk_socket *xsk) 767{ 768 size_t desc_sz = sizeof(struct xdp_desc); 769 struct xdp_mmap_offsets off; 770 int err; 771 772 if (!xsk) 773 return; 774 775 if (xsk->prog_fd != -1) { 776 xsk_delete_bpf_maps(xsk); 777 close(xsk->prog_fd); 778 } 779 780 err = xsk_get_mmap_offsets(xsk->fd, &off); 781 if (!err) { 782 if (xsk->rx) { 783 munmap(xsk->rx->ring - off.rx.desc, 784 off.rx.desc + xsk->config.rx_size * desc_sz); 785 } 786 if (xsk->tx) { 787 munmap(xsk->tx->ring - off.tx.desc, 788 off.tx.desc + xsk->config.tx_size * desc_sz); 789 } 790 791 } 792 793 xsk->umem->refcount--; 794 /* Do not close an fd that also has an associated umem connected 795 * to it. 796 */ 797 if (xsk->fd != xsk->umem->fd) 798 close(xsk->fd); 799 free(xsk); 800}