Skip to content

Commit 0d2ec5b

Browse files
author
Alexei Starovoitov
committed
Merge branch 'share-umem'
Magnus Karlsson says: ==================== This patch set extends libbpf and the xdpsock sample program to demonstrate the shared umem mode (XDP_SHARED_UMEM) as well as Rx-only and Tx-only sockets. This in order for users to have an example to use as a blue print and also so that these modes will be exercised more frequently. Note that the user needs to supply an XDP program with the XDP_SHARED_UMEM mode that distributes the packets over the sockets according to some policy. There is an example supplied with the xdpsock program, but there is no default one in libbpf similarly to when XDP_SHARED_UMEM is not used. The reason for this is that I felt that supplying one that would work for all users in this mode is futile. There are just tons of ways to distribute packets, so whatever I come up with and build into libbpf would be wrong in most cases. This patch has been applied against commit 30ee348 ("Merge branch 'bpf-libbpf-fixes'") Structure of the patch set: Patch 1: Adds shared umem support to libbpf Patch 2: Shared umem support and example XPD program added to xdpsock sample Patch 3: Adds Rx-only and Tx-only support to libbpf Patch 4: Uses Rx-only sockets for rxdrop and Tx-only sockets for txpush in the xdpsock sample Patch 5: Add documentation entries for these two features ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 472aeb3 + 57afa8b commit 0d2ec5b

File tree

6 files changed

+195
-59
lines changed

6 files changed

+195
-59
lines changed

Documentation/networking/af_xdp.rst

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ round-robin example of distributing packets is shown below:
295295
{
296296
rr = (rr + 1) & (MAX_SOCKS - 1);
297297
298-
return bpf_redirect_map(&xsks_map, rr, 0);
298+
return bpf_redirect_map(&xsks_map, rr, XDP_DROP);
299299
}
300300
301301
Note, that since there is only a single set of FILL and COMPLETION
@@ -304,6 +304,12 @@ to make sure that multiple processes or threads do not use these rings
304304
concurrently. There are no synchronization primitives in the
305305
libbpf code that protects multiple users at this point in time.
306306

307+
Libbpf uses this mode if you create more than one socket tied to the
308+
same umem. However, note that you need to supply the
309+
XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD libbpf_flag with the
310+
xsk_socket__create calls and load your own XDP program as there is no
311+
built in one in libbpf that will route the traffic for you.
312+
307313
XDP_USE_NEED_WAKEUP bind flag
308314
-----------------------------
309315

@@ -355,10 +361,22 @@ to set the size of at least one of the RX and TX rings. If you set
355361
both, you will be able to both receive and send traffic from your
356362
application, but if you only want to do one of them, you can save
357363
resources by only setting up one of them. Both the FILL ring and the
358-
COMPLETION ring are mandatory if you have a UMEM tied to your socket,
359-
which is the normal case. But if the XDP_SHARED_UMEM flag is used, any
360-
socket after the first one does not have a UMEM and should in that
361-
case not have any FILL or COMPLETION rings created.
364+
COMPLETION ring are mandatory as you need to have a UMEM tied to your
365+
socket. But if the XDP_SHARED_UMEM flag is used, any socket after the
366+
first one does not have a UMEM and should in that case not have any
367+
FILL or COMPLETION rings created as the ones from the shared umem will
368+
be used. Note, that the rings are single-producer single-consumer, so
369+
do not try to access them from multiple processes at the same
370+
time. See the XDP_SHARED_UMEM section.
371+
372+
In libbpf, you can create Rx-only and Tx-only sockets by supplying
373+
NULL to the rx and tx arguments, respectively, to the
374+
xsk_socket__create function.
375+
376+
If you create a Tx-only socket, we recommend that you do not put any
377+
packets on the fill ring. If you do this, drivers might think you are
378+
going to receive something when you in fact will not, and this can
379+
negatively impact performance.
362380

363381
XDP_UMEM_REG setsockopt
364382
-----------------------

samples/bpf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ always += xdp_sample_pkts_kern.o
167167
always += ibumad_kern.o
168168
always += hbm_out_kern.o
169169
always += hbm_edt_kern.o
170+
always += xdpsock_kern.o
170171

171172
ifeq ($(ARCH), arm)
172173
# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux

samples/bpf/xdpsock.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
*
3+
* Copyright(c) 2019 Intel Corporation.
4+
*/
5+
6+
#ifndef XDPSOCK_H_
7+
#define XDPSOCK_H_
8+
9+
#define MAX_SOCKS 4
10+
11+
#endif /* XDPSOCK_H */

samples/bpf/xdpsock_kern.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include "bpf_helpers.h"
4+
#include "xdpsock.h"
5+
6+
/* This XDP program is only needed for the XDP_SHARED_UMEM mode.
7+
* If you do not use this mode, libbpf can supply an XDP program for you.
8+
*/
9+
10+
struct {
11+
__uint(type, BPF_MAP_TYPE_XSKMAP);
12+
__uint(max_entries, MAX_SOCKS);
13+
__uint(key_size, sizeof(int));
14+
__uint(value_size, sizeof(int));
15+
} xsks_map SEC(".maps");
16+
17+
static unsigned int rr;
18+
19+
SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
20+
{
21+
rr = (rr + 1) & (MAX_SOCKS - 1);
22+
23+
return bpf_redirect_map(&xsks_map, rr, XDP_DROP);
24+
}

0 commit comments

Comments
 (0)