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