linux/tools/lib/bpf/xsk.c
<<
>>
Prefs
   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#ifndef SOL_XDP
  36 #define SOL_XDP 283
  37#endif
  38
  39#ifndef AF_XDP
  40 #define AF_XDP 44
  41#endif
  42
  43#ifndef PF_XDP
  44 #define PF_XDP AF_XDP
  45#endif
  46
  47struct xsk_umem {
  48        struct xsk_ring_prod *fill;
  49        struct xsk_ring_cons *comp;
  50        char *umem_area;
  51        struct xsk_umem_config config;
  52        int fd;
  53        int refcount;
  54};
  55
  56struct xsk_socket {
  57        struct xsk_ring_cons *rx;
  58        struct xsk_ring_prod *tx;
  59        __u64 outstanding_tx;
  60        struct xsk_umem *umem;
  61        struct xsk_socket_config config;
  62        int fd;
  63        int ifindex;
  64        int prog_fd;
  65        int xsks_map_fd;
  66        __u32 queue_id;
  67        char ifname[IFNAMSIZ];
  68};
  69
  70struct xsk_nl_info {
  71        bool xdp_prog_attached;
  72        int ifindex;
  73        int fd;
  74};
  75
  76int xsk_umem__fd(const struct xsk_umem *umem)
  77{
  78        return umem ? umem->fd : -EINVAL;
  79}
  80
  81int xsk_socket__fd(const struct xsk_socket *xsk)
  82{
  83        return xsk ? xsk->fd : -EINVAL;
  84}
  85
  86static bool xsk_page_aligned(void *buffer)
  87{
  88        unsigned long addr = (unsigned long)buffer;
  89
  90        return !(addr & (getpagesize() - 1));
  91}
  92
  93static void xsk_set_umem_config(struct xsk_umem_config *cfg,
  94                                const struct xsk_umem_config *usr_cfg)
  95{
  96        if (!usr_cfg) {
  97                cfg->fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
  98                cfg->comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
  99                cfg->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
 100                cfg->frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM;
 101                cfg->flags = XSK_UMEM__DEFAULT_FLAGS;
 102                return;
 103        }
 104
 105        cfg->fill_size = usr_cfg->fill_size;
 106        cfg->comp_size = usr_cfg->comp_size;
 107        cfg->frame_size = usr_cfg->frame_size;
 108        cfg->frame_headroom = usr_cfg->frame_headroom;
 109        cfg->flags = usr_cfg->flags;
 110}
 111
 112static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg,
 113                                     const struct xsk_socket_config *usr_cfg)
 114{
 115        if (!usr_cfg) {
 116                cfg->rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
 117                cfg->tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
 118                cfg->libbpf_flags = 0;
 119                cfg->xdp_flags = 0;
 120                cfg->bind_flags = 0;
 121                return 0;
 122        }
 123
 124        if (usr_cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)
 125                return -EINVAL;
 126
 127        cfg->rx_size = usr_cfg->rx_size;
 128        cfg->tx_size = usr_cfg->tx_size;
 129        cfg->libbpf_flags = usr_cfg->libbpf_flags;
 130        cfg->xdp_flags = usr_cfg->xdp_flags;
 131        cfg->bind_flags = usr_cfg->bind_flags;
 132
 133        return 0;
 134}
 135
 136int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,
 137                            __u64 size, struct xsk_ring_prod *fill,
 138                            struct xsk_ring_cons *comp,
 139                            const struct xsk_umem_config *usr_config)
 140{
 141        struct xdp_mmap_offsets off;
 142        struct xdp_umem_reg mr;
 143        struct xsk_umem *umem;
 144        socklen_t optlen;
 145        void *map;
 146        int err;
 147
 148        if (!umem_area || !umem_ptr || !fill || !comp)
 149                return -EFAULT;
 150        if (!size && !xsk_page_aligned(umem_area))
 151                return -EINVAL;
 152
 153        umem = calloc(1, sizeof(*umem));
 154        if (!umem)
 155                return -ENOMEM;
 156
 157        umem->fd = socket(AF_XDP, SOCK_RAW, 0);
 158        if (umem->fd < 0) {
 159                err = -errno;
 160                goto out_umem_alloc;
 161        }
 162
 163        umem->umem_area = umem_area;
 164        xsk_set_umem_config(&umem->config, usr_config);
 165
 166        mr.addr = (uintptr_t)umem_area;
 167        mr.len = size;
 168        mr.chunk_size = umem->config.frame_size;
 169        mr.headroom = umem->config.frame_headroom;
 170        mr.flags = umem->config.flags;
 171
 172        err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_REG, &mr, sizeof(mr));
 173        if (err) {
 174                err = -errno;
 175                goto out_socket;
 176        }
 177        err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_FILL_RING,
 178                         &umem->config.fill_size,
 179                         sizeof(umem->config.fill_size));
 180        if (err) {
 181                err = -errno;
 182                goto out_socket;
 183        }
 184        err = setsockopt(umem->fd, SOL_XDP, XDP_UMEM_COMPLETION_RING,
 185                         &umem->config.comp_size,
 186                         sizeof(umem->config.comp_size));
 187        if (err) {
 188                err = -errno;
 189                goto out_socket;
 190        }
 191
 192        optlen = sizeof(off);
 193        err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
 194        if (err) {
 195                err = -errno;
 196                goto out_socket;
 197        }
 198
 199        map = mmap(NULL, off.fr.desc + umem->config.fill_size * sizeof(__u64),
 200                   PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, umem->fd,
 201                   XDP_UMEM_PGOFF_FILL_RING);
 202        if (map == MAP_FAILED) {
 203                err = -errno;
 204                goto out_socket;
 205        }
 206
 207        umem->fill = fill;
 208        fill->mask = umem->config.fill_size - 1;
 209        fill->size = umem->config.fill_size;
 210        fill->producer = map + off.fr.producer;
 211        fill->consumer = map + off.fr.consumer;
 212        fill->flags = map + off.fr.flags;
 213        fill->ring = map + off.fr.desc;
 214        fill->cached_cons = umem->config.fill_size;
 215
 216        map = mmap(NULL, off.cr.desc + umem->config.comp_size * sizeof(__u64),
 217                   PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, umem->fd,
 218                   XDP_UMEM_PGOFF_COMPLETION_RING);
 219        if (map == MAP_FAILED) {
 220                err = -errno;
 221                goto out_mmap;
 222        }
 223
 224        umem->comp = comp;
 225        comp->mask = umem->config.comp_size - 1;
 226        comp->size = umem->config.comp_size;
 227        comp->producer = map + off.cr.producer;
 228        comp->consumer = map + off.cr.consumer;
 229        comp->flags = map + off.cr.flags;
 230        comp->ring = map + off.cr.desc;
 231
 232        *umem_ptr = umem;
 233        return 0;
 234
 235out_mmap:
 236        munmap(map, off.fr.desc + umem->config.fill_size * sizeof(__u64));
 237out_socket:
 238        close(umem->fd);
 239out_umem_alloc:
 240        free(umem);
 241        return err;
 242}
 243
 244struct xsk_umem_config_v1 {
 245        __u32 fill_size;
 246        __u32 comp_size;
 247        __u32 frame_size;
 248        __u32 frame_headroom;
 249};
 250
 251int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,
 252                            __u64 size, struct xsk_ring_prod *fill,
 253                            struct xsk_ring_cons *comp,
 254                            const struct xsk_umem_config *usr_config)
 255{
 256        struct xsk_umem_config config;
 257
 258        memcpy(&config, usr_config, sizeof(struct xsk_umem_config_v1));
 259        config.flags = 0;
 260
 261        return xsk_umem__create_v0_0_4(umem_ptr, umem_area, size, fill, comp,
 262                                        &config);
 263}
 264COMPAT_VERSION(xsk_umem__create_v0_0_2, xsk_umem__create, LIBBPF_0.0.2)
 265DEFAULT_VERSION(xsk_umem__create_v0_0_4, xsk_umem__create, LIBBPF_0.0.4)
 266
 267static int xsk_load_xdp_prog(struct xsk_socket *xsk)
 268{
 269        static const int log_buf_size = 16 * 1024;
 270        char log_buf[log_buf_size];
 271        int err, prog_fd;
 272
 273        /* This is the C-program:
 274         * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
 275         * {
 276         *     int index = ctx->rx_queue_index;
 277         *
 278         *     // A set entry here means that the correspnding queue_id
 279         *     // has an active AF_XDP socket bound to it.
 280         *     if (bpf_map_lookup_elem(&xsks_map, &index))
 281         *         return bpf_redirect_map(&xsks_map, index, 0);
 282         *
 283         *     return XDP_PASS;
 284         * }
 285         */
 286        struct bpf_insn prog[] = {
 287                /* r1 = *(u32 *)(r1 + 16) */
 288                BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1, 16),
 289                /* *(u32 *)(r10 - 4) = r1 */
 290                BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_1, -4),
 291                BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
 292                BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
 293                BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
 294                BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
 295                BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
 296                BPF_MOV32_IMM(BPF_REG_0, 2),
 297                /* if r1 == 0 goto +5 */
 298                BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 5),
 299                /* r2 = *(u32 *)(r10 - 4) */
 300                BPF_LD_MAP_FD(BPF_REG_1, xsk->xsks_map_fd),
 301                BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_10, -4),
 302                BPF_MOV32_IMM(BPF_REG_3, 0),
 303                BPF_EMIT_CALL(BPF_FUNC_redirect_map),
 304                /* The jumps are to this instruction */
 305                BPF_EXIT_INSN(),
 306        };
 307        size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
 308
 309        prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, prog, insns_cnt,
 310                                   "LGPL-2.1 or BSD-2-Clause", 0, log_buf,
 311                                   log_buf_size);
 312        if (prog_fd < 0) {
 313                pr_warning("BPF log buffer:\n%s", log_buf);
 314                return prog_fd;
 315        }
 316
 317        err = bpf_set_link_xdp_fd(xsk->ifindex, prog_fd, xsk->config.xdp_flags);
 318        if (err) {
 319                close(prog_fd);
 320                return err;
 321        }
 322
 323        xsk->prog_fd = prog_fd;
 324        return 0;
 325}
 326
 327static int xsk_get_max_queues(struct xsk_socket *xsk)
 328{
 329        struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
 330        struct ifreq ifr = {};
 331        int fd, err, ret;
 332
 333        fd = socket(AF_INET, SOCK_DGRAM, 0);
 334        if (fd < 0)
 335                return -errno;
 336
 337        ifr.ifr_data = (void *)&channels;
 338        memcpy(ifr.ifr_name, xsk->ifname, IFNAMSIZ - 1);
 339        ifr.ifr_name[IFNAMSIZ - 1] = '\0';
 340        err = ioctl(fd, SIOCETHTOOL, &ifr);
 341        if (err && errno != EOPNOTSUPP) {
 342                ret = -errno;
 343                goto out;
 344        }
 345
 346        if (err || channels.max_combined == 0)
 347                /* If the device says it has no channels, then all traffic
 348                 * is sent to a single stream, so max queues = 1.
 349                 */
 350                ret = 1;
 351        else
 352                ret = channels.max_combined;
 353
 354out:
 355        close(fd);
 356        return ret;
 357}
 358
 359static int xsk_create_bpf_maps(struct xsk_socket *xsk)
 360{
 361        int max_queues;
 362        int fd;
 363
 364        max_queues = xsk_get_max_queues(xsk);
 365        if (max_queues < 0)
 366                return max_queues;
 367
 368        fd = bpf_create_map_name(BPF_MAP_TYPE_XSKMAP, "xsks_map",
 369                                 sizeof(int), sizeof(int), max_queues, 0);
 370        if (fd < 0)
 371                return fd;
 372
 373        xsk->xsks_map_fd = fd;
 374
 375        return 0;
 376}
 377
 378static void xsk_delete_bpf_maps(struct xsk_socket *xsk)
 379{
 380        bpf_map_delete_elem(xsk->xsks_map_fd, &xsk->queue_id);
 381        close(xsk->xsks_map_fd);
 382}
 383
 384static int xsk_lookup_bpf_maps(struct xsk_socket *xsk)
 385{
 386        __u32 i, *map_ids, num_maps, prog_len = sizeof(struct bpf_prog_info);
 387        __u32 map_len = sizeof(struct bpf_map_info);
 388        struct bpf_prog_info prog_info = {};
 389        struct bpf_map_info map_info;
 390        int fd, err;
 391
 392        err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
 393        if (err)
 394                return err;
 395
 396        num_maps = prog_info.nr_map_ids;
 397
 398        map_ids = calloc(prog_info.nr_map_ids, sizeof(*map_ids));
 399        if (!map_ids)
 400                return -ENOMEM;
 401
 402        memset(&prog_info, 0, prog_len);
 403        prog_info.nr_map_ids = num_maps;
 404        prog_info.map_ids = (__u64)(unsigned long)map_ids;
 405
 406        err = bpf_obj_get_info_by_fd(xsk->prog_fd, &prog_info, &prog_len);
 407        if (err)
 408                goto out_map_ids;
 409
 410        xsk->xsks_map_fd = -1;
 411
 412        for (i = 0; i < prog_info.nr_map_ids; i++) {
 413                fd = bpf_map_get_fd_by_id(map_ids[i]);
 414                if (fd < 0)
 415                        continue;
 416
 417                err = bpf_obj_get_info_by_fd(fd, &map_info, &map_len);
 418                if (err) {
 419                        close(fd);
 420                        continue;
 421                }
 422
 423                if (!strcmp(map_info.name, "xsks_map")) {
 424                        xsk->xsks_map_fd = fd;
 425                        continue;
 426                }
 427
 428                close(fd);
 429        }
 430
 431        err = 0;
 432        if (xsk->xsks_map_fd == -1)
 433                err = -ENOENT;
 434
 435out_map_ids:
 436        free(map_ids);
 437        return err;
 438}
 439
 440static int xsk_set_bpf_maps(struct xsk_socket *xsk)
 441{
 442        return bpf_map_update_elem(xsk->xsks_map_fd, &xsk->queue_id,
 443                                   &xsk->fd, 0);
 444}
 445
 446static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
 447{
 448        __u32 prog_id = 0;
 449        int err;
 450
 451        err = bpf_get_link_xdp_id(xsk->ifindex, &prog_id,
 452                                  xsk->config.xdp_flags);
 453        if (err)
 454                return err;
 455
 456        if (!prog_id) {
 457                err = xsk_create_bpf_maps(xsk);
 458                if (err)
 459                        return err;
 460
 461                err = xsk_load_xdp_prog(xsk);
 462                if (err) {
 463                        xsk_delete_bpf_maps(xsk);
 464                        return err;
 465                }
 466        } else {
 467                xsk->prog_fd = bpf_prog_get_fd_by_id(prog_id);
 468                err = xsk_lookup_bpf_maps(xsk);
 469                if (err) {
 470                        close(xsk->prog_fd);
 471                        return err;
 472                }
 473        }
 474
 475        err = xsk_set_bpf_maps(xsk);
 476        if (err) {
 477                xsk_delete_bpf_maps(xsk);
 478                close(xsk->prog_fd);
 479                return err;
 480        }
 481
 482        return 0;
 483}
 484
 485int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,
 486                       __u32 queue_id, struct xsk_umem *umem,
 487                       struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
 488                       const struct xsk_socket_config *usr_config)
 489{
 490        void *rx_map = NULL, *tx_map = NULL;
 491        struct sockaddr_xdp sxdp = {};
 492        struct xdp_mmap_offsets off;
 493        struct xsk_socket *xsk;
 494        socklen_t optlen;
 495        int err;
 496
 497        if (!umem || !xsk_ptr || !rx || !tx)
 498                return -EFAULT;
 499
 500        if (umem->refcount) {
 501                pr_warning("Error: shared umems not supported by libbpf.\n");
 502                return -EBUSY;
 503        }
 504
 505        xsk = calloc(1, sizeof(*xsk));
 506        if (!xsk)
 507                return -ENOMEM;
 508
 509        if (umem->refcount++ > 0) {
 510                xsk->fd = socket(AF_XDP, SOCK_RAW, 0);
 511                if (xsk->fd < 0) {
 512                        err = -errno;
 513                        goto out_xsk_alloc;
 514                }
 515        } else {
 516                xsk->fd = umem->fd;
 517        }
 518
 519        xsk->outstanding_tx = 0;
 520        xsk->queue_id = queue_id;
 521        xsk->umem = umem;
 522        xsk->ifindex = if_nametoindex(ifname);
 523        if (!xsk->ifindex) {
 524                err = -errno;
 525                goto out_socket;
 526        }
 527        memcpy(xsk->ifname, ifname, IFNAMSIZ - 1);
 528        xsk->ifname[IFNAMSIZ - 1] = '\0';
 529
 530        err = xsk_set_xdp_socket_config(&xsk->config, usr_config);
 531        if (err)
 532                goto out_socket;
 533
 534        if (rx) {
 535                err = setsockopt(xsk->fd, SOL_XDP, XDP_RX_RING,
 536                                 &xsk->config.rx_size,
 537                                 sizeof(xsk->config.rx_size));
 538                if (err) {
 539                        err = -errno;
 540                        goto out_socket;
 541                }
 542        }
 543        if (tx) {
 544                err = setsockopt(xsk->fd, SOL_XDP, XDP_TX_RING,
 545                                 &xsk->config.tx_size,
 546                                 sizeof(xsk->config.tx_size));
 547                if (err) {
 548                        err = -errno;
 549                        goto out_socket;
 550                }
 551        }
 552
 553        optlen = sizeof(off);
 554        err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
 555        if (err) {
 556                err = -errno;
 557                goto out_socket;
 558        }
 559
 560        if (rx) {
 561                rx_map = mmap(NULL, off.rx.desc +
 562                              xsk->config.rx_size * sizeof(struct xdp_desc),
 563                              PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
 564                              xsk->fd, XDP_PGOFF_RX_RING);
 565                if (rx_map == MAP_FAILED) {
 566                        err = -errno;
 567                        goto out_socket;
 568                }
 569
 570                rx->mask = xsk->config.rx_size - 1;
 571                rx->size = xsk->config.rx_size;
 572                rx->producer = rx_map + off.rx.producer;
 573                rx->consumer = rx_map + off.rx.consumer;
 574                rx->flags = rx_map + off.rx.flags;
 575                rx->ring = rx_map + off.rx.desc;
 576        }
 577        xsk->rx = rx;
 578
 579        if (tx) {
 580                tx_map = mmap(NULL, off.tx.desc +
 581                              xsk->config.tx_size * sizeof(struct xdp_desc),
 582                              PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
 583                              xsk->fd, XDP_PGOFF_TX_RING);
 584                if (tx_map == MAP_FAILED) {
 585                        err = -errno;
 586                        goto out_mmap_rx;
 587                }
 588
 589                tx->mask = xsk->config.tx_size - 1;
 590                tx->size = xsk->config.tx_size;
 591                tx->producer = tx_map + off.tx.producer;
 592                tx->consumer = tx_map + off.tx.consumer;
 593                tx->flags = tx_map + off.tx.flags;
 594                tx->ring = tx_map + off.tx.desc;
 595                tx->cached_cons = xsk->config.tx_size;
 596        }
 597        xsk->tx = tx;
 598
 599        sxdp.sxdp_family = PF_XDP;
 600        sxdp.sxdp_ifindex = xsk->ifindex;
 601        sxdp.sxdp_queue_id = xsk->queue_id;
 602        sxdp.sxdp_flags = xsk->config.bind_flags;
 603
 604        err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp));
 605        if (err) {
 606                err = -errno;
 607                goto out_mmap_tx;
 608        }
 609
 610        xsk->prog_fd = -1;
 611
 612        if (!(xsk->config.libbpf_flags & XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD)) {
 613                err = xsk_setup_xdp_prog(xsk);
 614                if (err)
 615                        goto out_mmap_tx;
 616        }
 617
 618        *xsk_ptr = xsk;
 619        return 0;
 620
 621out_mmap_tx:
 622        if (tx)
 623                munmap(tx_map, off.tx.desc +
 624                       xsk->config.tx_size * sizeof(struct xdp_desc));
 625out_mmap_rx:
 626        if (rx)
 627                munmap(rx_map, off.rx.desc +
 628                       xsk->config.rx_size * sizeof(struct xdp_desc));
 629out_socket:
 630        if (--umem->refcount)
 631                close(xsk->fd);
 632out_xsk_alloc:
 633        free(xsk);
 634        return err;
 635}
 636
 637int xsk_umem__delete(struct xsk_umem *umem)
 638{
 639        struct xdp_mmap_offsets off;
 640        socklen_t optlen;
 641        int err;
 642
 643        if (!umem)
 644                return 0;
 645
 646        if (umem->refcount)
 647                return -EBUSY;
 648
 649        optlen = sizeof(off);
 650        err = getsockopt(umem->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
 651        if (!err) {
 652                munmap(umem->fill->ring - off.fr.desc,
 653                       off.fr.desc + umem->config.fill_size * sizeof(__u64));
 654                munmap(umem->comp->ring - off.cr.desc,
 655                       off.cr.desc + umem->config.comp_size * sizeof(__u64));
 656        }
 657
 658        close(umem->fd);
 659        free(umem);
 660
 661        return 0;
 662}
 663
 664void xsk_socket__delete(struct xsk_socket *xsk)
 665{
 666        size_t desc_sz = sizeof(struct xdp_desc);
 667        struct xdp_mmap_offsets off;
 668        socklen_t optlen;
 669        int err;
 670
 671        if (!xsk)
 672                return;
 673
 674        if (xsk->prog_fd != -1) {
 675                xsk_delete_bpf_maps(xsk);
 676                close(xsk->prog_fd);
 677        }
 678
 679        optlen = sizeof(off);
 680        err = getsockopt(xsk->fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen);
 681        if (!err) {
 682                if (xsk->rx) {
 683                        munmap(xsk->rx->ring - off.rx.desc,
 684                               off.rx.desc + xsk->config.rx_size * desc_sz);
 685                }
 686                if (xsk->tx) {
 687                        munmap(xsk->tx->ring - off.tx.desc,
 688                               off.tx.desc + xsk->config.tx_size * desc_sz);
 689                }
 690
 691        }
 692
 693        xsk->umem->refcount--;
 694        /* Do not close an fd that also has an associated umem connected
 695         * to it.
 696         */
 697        if (xsk->fd != xsk->umem->fd)
 698                close(xsk->fd);
 699        free(xsk);
 700}
 701