Skip to contentSkip to conversation

Add support for XDP_ATTACH

PATCH In review Björn Töpel 6 participants 20 messages Dec 17, 2018 – Dec 20, 2018

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v2 cover Add support for XDP_ATTACH

From: Björn Töpel <[email protected]>

Hi!

This patch set adds support for a new XDP socket bind option, XDP_ATTACH.

XDP_ATTACH associates an XDP socket to a specific netdev Rx queue. To redirect a packet to an attached socket from XDP, the bpf_xsk_redirect helper is used. The bpf_xsk_redirect helper is also introduced in this series.

Many XDP socket users just need a simple way of creating/binding a socket and receiving frames right away without a complicated XDP program. "Attached" XDP sockets removes the need for the XSKMAP, and allows for a trivial XDP program, e.g.:

  SEC("xdp")
  int xdp_prog(struct xdp_md *ctx)
  {
        return bpf_xsk_redirect(ctx);
  }

An attached XDP socket also has better performance than the XSKMAP based sockets (performance numbers below).

The first three patches of the series add support for the XDP_ATTACH flag and the BPF helper.

Since the trivial XDP program above will be a very common way of using AF_XDP sockets, it makes sense to bundle that XDP program to libbpf.

We call BPF programs that are bundled with libbpf a "builtin program". To access the bpf_object/bpf_program of a builtin program the following libbpf API are introduced:

      LIBBPF_API struct bpf_object *bpf_object__open_builtin(
                      enum bpf_prog_type prog_type);
    
      LIBBPF_API struct bpf_program *
      bpf_object__find_xdp_builtin_program(
                    struct bpf_object* obj,
                      enum libbpf_builtin_xdp_prog prog);

The first function is used to get a handle to the bpf_object containing all builtin programs for a certain program type. The latter is used to access a certain builtin program from the bpf_object. Note that currenty only XDP is supported. When other program types are supported, additional bpf_object__find_PROG_TYPE_builtin_program function are required.

Patch 4 and 5 introduce the "builtin" program to libbpf.

The last two patches adds XDP_ATTACH support and the "builtin" libbpf support to the sample application.

Some questions:

* If the only builtin programs in libbpf will be XDP programs, the libbpf API in this series might be a bit over-engineered. Thoughts?

* Is the idea that users of libbpf should use the IS_ERR* functionality for checking pointers (e.g. __open), or is that just library internal convenience?

Finally, the performance numbers for rxdrop (Intel(R) Xeon(R) Gold 6154 CPU @ 3.00GHz):

XDP_SKB:

XSKMAP:     2.8 Mpps
XDP_ATTACH: 2.9 Mpps

XDP_DRV - copy:

XSKMAP:     8.5 Mpps
XDP_ATTACH: 9.3 Mpps

XDP_DRV - zero-copy:

XSKMAP:     15.1 Mpps
XDP_ATTACH: 17.3 Mpps

Thanks!
Björn

v1->v2: Reworked the "builtin program" concept. The v1 had the builtin
program as part of the kernel, which simply added too much complexity. In v2, the "builtin" was moved to libbpf instead. Alexei suggested that XDP_ATTACH was renamed to to XDP_BUILTIN_ATTACH, but given that the "builtin" functionality was removed, the old name stuck.

Björn Töpel (7):
xsk: simplify AF_XDP socket teardown
xsk: add XDP_ATTACH bind() flag
bpf: add bpf_xsk_redirect function tools/bpf: sync kernel uapi bpf.h to tools directory
libbpf: initial support for builtin BPF programs
samples: bpf: simplify/cleanup xdpsock
samples: bpf: add support for XDP_ATTACH to xdpsock

 include/linux/filter.h         |   4 +
 include/linux/netdevice.h      |   1 +
 include/net/xdp_sock.h         |   3 +
 include/trace/events/xdp.h     |  61 ++++++++++++++
 include/uapi/linux/bpf.h       |  14 +++-
 include/uapi/linux/if_xdp.h    |   1 +
 net/core/filter.c              | 100 +++++++++++++++++++++++
 net/xdp/xsk.c                  |  67 +++++++++------
 samples/bpf/xdpsock_user.c     | 145 +++++++++++++++++++++++----------
 tools/include/uapi/linux/bpf.h |  14 +++-
 tools/lib/bpf/libbpf.c         |  85 +++++++++++++++++++
 tools/lib/bpf/libbpf.h         |  14 ++++
 tools/lib/bpf/libbpf.map       |   2 +
 13 files changed, 441 insertions(+), 70 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH v2 1/7 xsk: simplify AF_XDP socket teardown

From: Björn Töpel <[email protected]>

Prior this commit, when the struct socket object was being released, the UMEM did not have its reference count decreased. Instead, this was done in the struct sock sk_destruct function.

There is no reason to keep the UMEM reference around when the socket is being orphaned, so in this patch the xdp_put_mem is called in the xsk_release function. This results in that the xsk_destruct function can be removed!

Note that, it still holds that a struct xsk_sock reference might still linger in the XSKMAP after the UMEM is released, e.g. if a user does not clear the XSKMAP prior to closing the process. This sock will be in a "released" zombie like state, until the XSKMAP is removed.

---
 net/xdp/xsk.c | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)
net/xdp/xsk.c +1 -15
index 07156f43d295..a03268454a27 100644
@@ -366,6 +366,7 @@ static int xsk_release(struct socket *sock)
 
 	xskq_destroy(xs->rx);
 	xskq_destroy(xs->tx);
+	xdp_put_umem(xs->umem);
 
 	sock_orphan(sk);
 	sock->sk = NULL;
@@ -713,18 +714,6 @@ static const struct proto_ops xsk_proto_ops = {
 	.sendpage	= sock_no_sendpage,
 };
 
-static void xsk_destruct(struct sock *sk)
-{
-	struct xdp_sock *xs = xdp_sk(sk);
-
-	if (!sock_flag(sk, SOCK_DEAD))
-		return;
-
-	xdp_put_umem(xs->umem);
-
-	sk_refcnt_debug_dec(sk);
-}
-
 static int xsk_create(struct net *net, struct socket *sock, int protocol,
 		      int kern)
 {
@@ -751,9 +740,6 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,
 
 	sk->sk_family = PF_XDP;
 
-	sk->sk_destruct = xsk_destruct;
-	sk_refcnt_debug_inc(sk);
-
 	sock_set_flag(sk, SOCK_RCU_FREE);
 
 	xs = xdp_sk(sk);
Signature · 1 line
2.19.1
#2 lore raw

PATCH v2 2/7 xsk: add XDP_ATTACH bind() flag

In reply to Björn Töpel

From: Björn Töpel <[email protected]>

In this commit the XDP_ATTACH bind() flag is introduced. When an XDP socket is bound with this flag set, the socket will be associated with a certain netdev Rx queue. The idea is that the XDP socket users do not have to deal with the XSKMAP. Instead, XDP_ATTACH will "attach" an XDP socket to a queue, and XDP programs simply use bpf_xsk_redirect to redirect XDP frames to an attached socket.

An XDP socket bound with this option performs better, since the BPF program is smaller, and the kernel code-path also has fewer instructions.

This commit only introduces the first part of XDP_ATTACH, namely associating the XDP socket to a netdev Rx queue. The bpf_xsk_redirect function will be introduced in the next commit.

---
 include/linux/netdevice.h   |  1 +
 include/net/xdp_sock.h      |  3 +++
 include/uapi/linux/if_xdp.h |  1 +
 net/xdp/xsk.c               | 51 +++++++++++++++++++++++++++++--------
 4 files changed, 45 insertions(+), 11 deletions(-)
include/linux/netdevice.h +1 -0
index fc6ba71513be..a2d19af6b8dd 100644
@@ -743,6 +743,7 @@ struct netdev_rx_queue {
 	struct xdp_rxq_info		xdp_rxq;
 #ifdef CONFIG_XDP_SOCKETS
 	struct xdp_umem                 *umem;
+	struct xdp_sock			*xsk;
 #endif
 } ____cacheline_aligned_in_smp;
 
include/net/xdp_sock.h +3 -0
index 13acb9803a6d..13975723430c 100644
@@ -61,6 +61,7 @@ struct xdp_sock {
 	struct xsk_queue *tx ____cacheline_aligned_in_smp;
 	struct list_head list;
 	bool zc;
+	bool attached;
 	/* Protects multiple processes in the control path */
 	struct mutex mutex;
 	/* Mutual exclusion of NAPI TX thread and sendmsg error paths
@@ -72,7 +73,9 @@ struct xdp_sock {
 
 struct xdp_buff;
 #ifdef CONFIG_XDP_SOCKETS
+int xsk_generic_attached_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
+int xsk_attached_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
 int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
 void xsk_flush(struct xdp_sock *xs);
 bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs);
include/uapi/linux/if_xdp.h +1 -0
index caed8b1614ff..bd76235c2749 100644
@@ -16,6 +16,7 @@
 #define XDP_SHARED_UMEM	(1 << 0)
 #define XDP_COPY	(1 << 1) /* Force copy-mode */
 #define XDP_ZEROCOPY	(1 << 2) /* Force zero-copy mode */
+#define XDP_ATTACH	(1 << 3)
 
 struct sockaddr_xdp {
 	__u16 sxdp_family;
net/xdp/xsk.c +40 -11

104 lines not on this page · raw message

Signature · 1 line
2.19.1
#3 lore raw

PATCH v2 3/7 bpf: add bpf_xsk_redirect function

In reply to Björn Töpel

From: Björn Töpel <[email protected]>

The bpf_xsk_redirect function is a new redirect bpf function, in addition to bpf_redirect/bpf_redirect_map. If an XDP socket has been attached to a netdev Rx queue via the XDP_ATTACH bind() option and bpf_xsk_redirect is called, the packet will be redirected to the attached socket.

The bpf_xsk_redirect function returns XDP_REDIRECT if there is a socket attached to the originated queue, otherwise XDP_PASS.

This commit also adds the corresponding trace points for the redirect call.

---
 include/linux/filter.h     |   4 ++
 include/trace/events/xdp.h |  61 ++++++++++++++++++++++
 include/uapi/linux/bpf.h   |  14 +++++-
 net/core/filter.c          | 100 +++++++++++++++++++++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)
include/linux/filter.h +4 -0
index 537e9e7c6e6f..ee3efca4477e 100644
@@ -532,6 +532,10 @@ struct bpf_redirect_info {
 	u32 flags;
 	struct bpf_map *map;
 	struct bpf_map *map_to_flush;
+#ifdef CONFIG_XDP_SOCKETS
+	struct xdp_sock *xsk;
+	struct xdp_sock *xsk_to_flush;
+#endif
 	u32 kern_flags;
 };
 
include/trace/events/xdp.h +61 -0

67 lines not on this page · raw message

include/uapi/linux/bpf.h +13 -1
index e7d57e89f25f..2568039b6265 100644
@@ -2319,6 +2319,17 @@ union bpf_attr {
  *		"**y**".
  *	Return
  *		0
+ *
+ * int bpf_xsk_redirect(struct xdp_buff *xdp_md)
+ *	 Description
+ *		Redirect the packet to the attached XDP socket, if any.
+ *		An XDP socket can be attached to a network interface Rx
+ *		queue by passing the XDP_ATTACH option at bind point of
+ *		the socket.
+ *
+ *	Return
+ *		**XDP_REDIRECT** if there is an XDP socket attached to the Rx
+ *		queue receiving the frame, otherwise **XDP_PASS**.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -2413,7 +2424,8 @@ union bpf_attr {
 	FN(map_peek_elem),		\
 	FN(msg_push_data),		\
 	FN(msg_pop_data),		\
-	FN(rc_pointer_rel),
+	FN(rc_pointer_rel),		\
+	FN(xsk_redirect),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
net/core/filter.c +100 -0

148 lines not on this page · raw message

Signature · 1 line
2.19.1
#4 lore raw

PATCH v2 4/7 tools/bpf: sync kernel uapi bpf.h to tools directory

In reply to Björn Töpel

From: Björn Töpel <[email protected]>

Sync kernel uapi bpf.h "BPF_FUNC_xsk_redirect" change to the tools directory.

---
 tools/include/uapi/linux/bpf.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +13 -1
index e7d57e89f25f..2568039b6265 100644
@@ -2319,6 +2319,17 @@ union bpf_attr {
  *		"**y**".
  *	Return
  *		0
+ *
+ * int bpf_xsk_redirect(struct xdp_buff *xdp_md)
+ *	 Description
+ *		Redirect the packet to the attached XDP socket, if any.
+ *		An XDP socket can be attached to a network interface Rx
+ *		queue by passing the XDP_ATTACH option at bind point of
+ *		the socket.
+ *
+ *	Return
+ *		**XDP_REDIRECT** if there is an XDP socket attached to the Rx
+ *		queue receiving the frame, otherwise **XDP_PASS**.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -2413,7 +2424,8 @@ union bpf_attr {
 	FN(map_peek_elem),		\
 	FN(msg_push_data),		\
 	FN(msg_pop_data),		\
-	FN(rc_pointer_rel),
+	FN(rc_pointer_rel),		\
+	FN(xsk_redirect),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
Signature · 1 line
2.19.1
#5 lore raw

PATCH v2 5/7 libbpf: initial support for builtin BPF programs

In reply to Björn Töpel

From: Björn Töpel <[email protected]>

This commit introduces builtin BPF programs to libbpf. A builtin program is simply a BPF program that is bundled with libbpf.

The first builtin program is an XDP program, "xdp_xsk_redirect", which is a trivial program that calls bpf_xsk_redirect for each received packet, and redirects it to the corresponding XDP socket.

Two new functions are added to the libbpf API:
LIBBPF_API struct bpf_object *bpf_object__open_builtin(

                  enum bpf_prog_type prog_type);

LIBBPF_API struct bpf_program * bpf_object__find_xdp_builtin_program(
struct bpf_object* obj,
enum libbpf_builtin_xdp_prog prog);

The first function is used to get a handle to the bpf_object containing all builtin programs for a certain program type. The latter is used to access a certain builtin program from the bpf_object. Note that currenty only XDP is supported. When other program types are supported, additional bpf_object__find_PROG_TYPE_builtin_program function are required.

When/if packet cloning is introduced to XDP, another builtin program candidate would be a program that clones all packets to an XDP socket.

---
 tools/lib/bpf/libbpf.c   | 85 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   | 14 +++++++
 tools/lib/bpf/libbpf.map |  2 +
 3 files changed, 101 insertions(+)
tools/lib/bpf/libbpf.c +85 -0
index e2bc75ee1614..d8551193862b 100644
@@ -242,6 +242,31 @@ struct bpf_object {
 };
 #define obj_elf_valid(o)	((o)->efile.elf)
 
+struct libbpf_builtin_prog {
+	const char *name;
+	const struct bpf_insn *insns;
+	size_t size;
+};
+
+/*
+ * Builtin XDP program: LIBBPF_BUILTIN_XDP__XSK_REDIRECT
+ *
+ * Trivial XDP program that calls bpf_xsk_redirect() on every received
+ * frame.
+ */
+static const struct bpf_insn builtin_xdp_xsk_redirect_insn[] = {
+	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_xsk_redirect),
+	BPF_EXIT_INSN(),
+};
+
+static const struct libbpf_builtin_prog libbpf_builtin_xdp_prog[] = {
+	[LIBBPF_BUILTIN_XDP__XSK_REDIRECT] = {
+		"xdp_xsk_redirect",
+		&builtin_xdp_xsk_redirect_insn[0],
+		sizeof(builtin_xdp_xsk_redirect_insn)
+	},
+};
+
 void bpf_program__unload(struct bpf_program *prog)
 {
 	int i;
@@ -2990,3 +3015,63 @@ bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
 	ring_buffer_write_tail(header, data_tail);
 	return ret;
 }
+
+struct bpf_object *bpf_object__open_builtin(enum bpf_prog_type prog_type)
+{
+	struct bpf_program *prog;
+	struct bpf_object *obj;
+	int err, i;
+
+	/* Right now, only XDP is supported. */
+	if (prog_type != BPF_PROG_TYPE_XDP)
+		return ERR_PTR(-EINVAL);
+
+	obj = bpf_object__new("", NULL, 0);
+	if (IS_ERR(obj))
+		return NULL;
+
+	CHECK_ERR(bpf_object__init_license(obj, (void *)"GPL", sizeof("GPL")),
+		  err, out);
+
+	for (i = 0; i < __LIBBPF_BUILTIN_XDP__END; i++) {
+		err = bpf_object__add_program(
+			obj,
+			(void *)libbpf_builtin_xdp_prog[i].insns,
+			libbpf_builtin_xdp_prog[i].size,
+			(char *)libbpf_builtin_xdp_prog[i].name, i);
+		if (err) {
+			pr_warning("failed to add builtin program %s\n",
+				   libbpf_builtin_xdp_prog[i].name);
+			goto out;
+		}
+	}
+
+	bpf_object__for_each_program(prog, obj) {
+		bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
+
+		prog->name = strdup(libbpf_builtin_xdp_prog[prog->idx].name);
+		if (!prog->name) {
+			pr_warning("failed to allocate memory for name %s\n",
+				   libbpf_builtin_xdp_prog[prog->idx].name);
+			goto out;
+		}
+	}
+
+	return obj;
+out:
+	bpf_object__close(obj);
+	return NULL;
+
+}
+
+struct bpf_program *bpf_object__find_xdp_builtin_program(
+	struct bpf_object *obj, enum libbpf_builtin_xdp_prog prog)
+{
+	if (!obj)
+		return NULL;
+
+	if (prog < 0 || prog >= __LIBBPF_BUILTIN_XDP__END)
+		return NULL;
+
+	return bpf_object__find_prog_by_idx(obj, prog);
+}
tools/lib/bpf/libbpf.h +14 -0
index 5f68d7b75215..0de0cc2da240 100644
@@ -45,6 +45,15 @@ enum libbpf_errno {
 	__LIBBPF_ERRNO__END,
 };
 
+enum libbpf_builtin_xdp_prog {
+	/*
+	 * Trivial XDP program that calls bpf_xsk_redirect
+	 * unconditionally for every received packet.
+	 */
+	LIBBPF_BUILTIN_XDP__XSK_REDIRECT,
+	__LIBBPF_BUILTIN_XDP__END,
+};
+
 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
 
 /*
@@ -75,6 +84,8 @@ struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
 LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
 						      size_t obj_buf_sz,
 						      const char *name);
+LIBBPF_API struct bpf_object *bpf_object__open_builtin(
+	enum bpf_prog_type prog_type);
 LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
 LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
 				      const char *path);
@@ -94,6 +105,9 @@ LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
 
 LIBBPF_API struct bpf_program *
 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
+LIBBPF_API struct bpf_program *
+bpf_object__find_xdp_builtin_program(struct bpf_object *obj,
+				     enum libbpf_builtin_xdp_prog prog);
 
 LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
 #define bpf_object__for_each_safe(pos, tmp)			\
tools/lib/bpf/libbpf.map +2 -0
index cd02cd4e2cc3..58acd84d5ff3 100644
@@ -121,6 +121,8 @@ LIBBPF_0.0.1 {
 		libbpf_prog_type_by_name;
 		libbpf_set_print;
 		libbpf_strerror;
+		bpf_object__open_builtin;
+		bpf_object__find_xdp_builtin_program;
 	local:
 		*;
 };
Signature · 1 line
2.19.1
#6 lore raw

PATCH v2 6/7 samples: bpf: simplify/cleanup xdpsock

In reply to Björn Töpel

From: Björn Töpel <[email protected]>

Break out all map and XDP program setup into a function.

---
 samples/bpf/xdpsock_user.c | 91 +++++++++++++++++++++-----------------
 1 file changed, 50 insertions(+), 41 deletions(-)
samples/bpf/xdpsock_user.c +50 -41
index 57ecadc58403..c72face0da05 100644
@@ -119,7 +119,7 @@ struct xdpsock {
 };
 
 static int num_socks;
-struct xdpsock *xsks[MAX_SOCKS];
+static struct xdpsock *xsks[MAX_SOCKS];
 
 static unsigned long get_nsecs(void)
 {
@@ -897,37 +897,21 @@ static void l2fwd(struct xdpsock *xsk)
 	}
 }
 
-int main(int argc, char **argv)
+static void setup_xdp_xskmap(const char *pathprefix)
 {
-	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
-	struct bpf_prog_load_attr prog_load_attr = {
-		.prog_type	= BPF_PROG_TYPE_XDP,
-	};
-	int prog_fd, qidconf_map, xsks_map;
+	int prog_fd, qidconf_map, xsks_map, i, key = 0, err;
+	struct bpf_prog_load_attr prog_load_attr = {};
+	char bpf_objs_filename[256];
+	struct bpf_program *prog;
 	struct bpf_object *obj;
-	char xdp_filename[256];
 	struct bpf_map *map;
-	int i, ret, key = 0;
-	pthread_t pt;
 
-	parse_command_line(argc, argv);
-
-	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
-		fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
-			strerror(errno));
-		exit(EXIT_FAILURE);
-	}
-
-	snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
-	prog_load_attr.file = xdp_filename;
+	snprintf(bpf_objs_filename, sizeof(bpf_objs_filename), "%s_kern.o",
+		 pathprefix);
+	prog_load_attr.file = bpf_objs_filename;
 
 	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
 		exit(EXIT_FAILURE);
-	if (prog_fd < 0) {
-		fprintf(stderr, "ERROR: no program found: %s\n",
-			strerror(prog_fd));
-		exit(EXIT_FAILURE);
-	}
 
 	map = bpf_object__find_map_by_name(obj, "qidconf_map");
 	qidconf_map = bpf_map__fd(map);
@@ -945,14 +929,51 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	err = bpf_map_update_elem(qidconf_map, &key, &opt_queue, 0);
+	if (err) {
+		fprintf(stderr, "ERROR: bpf_map_update_elem qidconf\n");
+		exit(EXIT_FAILURE);
+	}
+
+	/* Insert sockets into into the XSKMAP. */
+	for (i = 0; i < num_socks; i++) {
+		key = i;
+		err = bpf_map_update_elem(xsks_map, &key, &xsks[i]->sfd, 0);
+		if (err) {
+			fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
+			exit(EXIT_FAILURE);
+		}
+	}
+
+	prog = bpf_object__find_program_by_title(obj, "xdp_sock");
+	prog_fd = bpf_program__fd(prog);
+	if (prog_fd < 0) {
+		fprintf(stderr, "ERROR: no xdp_sock program found: %s\n",
+			strerror(prog_fd));
+		exit(EXIT_FAILURE);
+	}
+
 	if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
 		fprintf(stderr, "ERROR: link set xdp fd failed\n");
 		exit(EXIT_FAILURE);
 	}
+}
 
-	ret = bpf_map_update_elem(qidconf_map, &key, &opt_queue, 0);
-	if (ret) {
-		fprintf(stderr, "ERROR: bpf_map_update_elem qidconf\n");
+int main(int argc, char **argv)
+{
+	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
+	pthread_t pt;
+	int ret;
+
+	signal(SIGINT, int_exit);
+	signal(SIGTERM, int_exit);
+	signal(SIGABRT, int_exit);
+
+	parse_command_line(argc, argv);
+
+	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
+		fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
+			strerror(errno));
 		exit(EXIT_FAILURE);
 	}
 
@@ -964,19 +985,7 @@ int main(int argc, char **argv)
 		xsks[num_socks++] = xsk_configure(xsks[0]->umem);
 #endif
 
-	/* ...and insert them into the map. */
-	for (i = 0; i < num_socks; i++) {
-		key = i;
-		ret = bpf_map_update_elem(xsks_map, &key, &xsks[i]->sfd, 0);
-		if (ret) {
-			fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
-			exit(EXIT_FAILURE);
-		}
-	}
-
-	signal(SIGINT, int_exit);
-	signal(SIGTERM, int_exit);
-	signal(SIGABRT, int_exit);
+	setup_xdp_xskmap(argv[0]);
 
 	setlocale(LC_ALL, "");
 
Signature · 1 line
2.19.1
#7 lore raw

PATCH v2 7/7 samples: bpf: add support for XDP_ATTACH to xdpsock

In reply to Björn Töpel

From: Björn Töpel <[email protected]>

This commit adds support for the XDP_ATTACH bind() flag. If "-a" is passed to the program XDP_ATTACH is passed to bind and the builtin libbpf XDP program is loaded.

---
 samples/bpf/xdpsock_user.c | 56 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)
samples/bpf/xdpsock_user.c +54 -2
index c72face0da05..53f9063bbdd9 100644
@@ -650,6 +650,7 @@ static struct option long_options[] = {
 	{"interval", required_argument, 0, 'n'},
 	{"zero-copy", no_argument, 0, 'z'},
 	{"copy", no_argument, 0, 'c'},
+	{"attach", no_argument, 0, 'a'},
 	{0, 0, 0, 0}
 };
 
@@ -670,6 +671,7 @@ static void usage(const char *prog)
 		"  -n, --interval=n	Specify statistics update interval (default 1 sec).\n"
 		"  -z, --zero-copy      Force zero-copy mode.\n"
 		"  -c, --copy           Force copy mode.\n"
+		"  -a, --attach		XDP_ATTACH mode.\n"
 		"\n";
 	fprintf(stderr, str, prog);
 	exit(EXIT_FAILURE);
@@ -682,7 +684,7 @@ static void parse_command_line(int argc, char **argv)
 	opterr = 0;
 
 	for (;;) {
-		c = getopt_long(argc, argv, "rtli:q:psSNn:cz", long_options,
+		c = getopt_long(argc, argv, "rtli:q:psSNn:cza", long_options,
 				&option_index);
 		if (c == -1)
 			break;
@@ -725,6 +727,9 @@ static void parse_command_line(int argc, char **argv)
 		case 'c':
 			opt_xdp_bind_flags |= XDP_COPY;
 			break;
+		case 'a':
+			opt_xdp_bind_flags |= XDP_ATTACH;
+			break;
 		default:
 			usage(basename(argv[0]));
 		}
@@ -959,6 +964,45 @@ static void setup_xdp_xskmap(const char *pathprefix)
 	}
 }
 
+static void setup_xdp_attach(void)
+{
+	struct bpf_program *prog;
+	struct bpf_object *obj;
+	int prog_fd, err;
+
+	obj = bpf_object__open_builtin(BPF_PROG_TYPE_XDP);
+	if (!obj) {
+		fprintf(stderr, "ERROR: opening builtin XDP\n");
+		exit(EXIT_FAILURE);
+	}
+
+	prog = bpf_object__find_xdp_builtin_program(
+		obj,
+		LIBBPF_BUILTIN_XDP__XSK_REDIRECT);
+	if (!prog) {
+		fprintf(stderr, "ERROR: finding builtin XDP program\n");
+		exit(EXIT_FAILURE);
+	}
+
+	err = bpf_program__load(prog, "GPL", 0);
+	if (err) {
+		fprintf(stderr, "ERROR: error loading XDP program\n");
+		exit(EXIT_FAILURE);
+	}
+
+	prog_fd = bpf_program__fd(prog);
+	if (prog_fd < 0) {
+		fprintf(stderr, "ERROR: no xdp_sock program found: %s\n",
+			strerror(prog_fd));
+		exit(EXIT_FAILURE);
+	}
+
+	if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
+		fprintf(stderr, "ERROR: link set xdp fd failed\n");
+		exit(EXIT_FAILURE);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
@@ -981,11 +1025,19 @@ int main(int argc, char **argv)
 	xsks[num_socks++] = xsk_configure(NULL);
 
 #if RR_LB
+	if (opt_xdp_bind_flags & XDP_ATTACH) {
+		fprintf(stderr, "ERROR: Not supported by application!\n");
+		exit(EXIT_FAILURE);
+	}
+
 	for (i = 0; i < MAX_SOCKS - 1; i++)
 		xsks[num_socks++] = xsk_configure(xsks[0]->umem);
 #endif
 
-	setup_xdp_xskmap(argv[0]);
+	if (opt_xdp_bind_flags & XDP_ATTACH)
+		setup_xdp_attach();
+	else
+		setup_xdp_xskmap(argv[0]);
 
 	setlocale(LC_ALL, "");
 
Signature · 1 line
2.19.1
#8 lore raw
In reply to Björn Töpel
Quoted 10 lines · On Mon, 17 Dec 2018 11:24:54 +0100 Björn Töpel <[email protected]> wrote:

XDP_ATTACH associates an XDP socket to a specific netdev Rx queue. To redirect a packet to an attached socket from XDP, the bpf_xsk_redirect helper is used. The bpf_xsk_redirect helper is also introduced in this series.

Many XDP socket users just need a simple way of creating/binding a socket and receiving frames right away without a complicated XDP program. "Attached" XDP sockets removes the need for the XSKMAP, and allows for a trivial XDP program, e.g.:

Quoted 8 lines
  SEC("xdp")
  int xdp_prog(struct xdp_md *ctx)
  {
        return bpf_xsk_redirect(ctx);
  }

An attached XDP socket also has better performance than the XSKMAP based sockets (performance numbers below).

I still have a general problem with this approach.

The AF_XDP socket is build around (and gets its performance) from being tied to a specific RX-queue. That design begs to have an XDP program per RX-queue.

Patchset-v1 moved towards this goal. But in this patchset-v2 you steer away from this again, and work-around the issue with the current limitations of 1-XDP program per netdev. (Which result in; if a single AF_XDP socket is used in the system, which can ONLY be for a single RX-queue by design, then ALL other XDP_PASS traffic also have to take the overhead of indirect BPF call).

IMHO with this use-case, now is the time to introduce XDP programs per RX-queue. Yes, it will be more work, but I will offer to helpout. This should be generalized as XDP programs per RX-queue can be used by other use-cases too:
In general terms: We can setup a NIC hardware filter to deliver
frame matching some criteria, then we can avoid rechecking these criterias in on the (RX) CPU when/if we can attach an XDP prog to this specific RX-queue directly. This *IS* exactly what AF_XDP does, but it is in general useful for others, like CPUMAP redirect.

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
#9 lore raw
Quoted 19 lines · Jesper Dangaard Brouer
Quoted 16 lines · On Mon, 17 Dec 2018 11:24:54 +0100 Björn Töpel <[email protected]> wrote:

XDP_ATTACH associates an XDP socket to a specific netdev Rx queue. To redirect a packet to an attached socket from XDP, the bpf_xsk_redirect helper is used. The bpf_xsk_redirect helper is also introduced in this series.

Many XDP socket users just need a simple way of creating/binding a socket and receiving frames right away without a complicated XDP program. "Attached" XDP sockets removes the need for the XSKMAP, and allows for a trivial XDP program, e.g.:

   SEC("xdp")
   int xdp_prog(struct xdp_md *ctx)
   {
         return bpf_xsk_redirect(ctx);
   }
Quoted 5 lines
Quoted 2 lines

An attached XDP socket also has better performance than the XSKMAP based sockets (performance numbers below).

I still have a general problem with this approach.

And I appreciate that you have comments on the design/code! :-) Thank you!

Quoted 11 lines

The AF_XDP socket is build around (and gets its performance) from being tied to a specific RX-queue. That design begs to have an XDP program per RX-queue.

Patchset-v1 moved towards this goal. But in this patchset-v2 you steer away from this again, and work-around the issue with the current limitations of 1-XDP program per netdev. (Which result in; if a single AF_XDP socket is used in the system, which can ONLY be for a single RX-queue by design, then ALL other XDP_PASS traffic also have to take the overhead of indirect BPF call).

I agree that a per-queue-program would be a good fit, but I think it still makes sense to add XDP_ATTACH and the helper, to make it easier for the XDP program authors to use AF_XDP. Would you prefer that this functionality was help back, until per-queue programs are introduced?

OTOH the implementation would probably look different if there was a per-q program, because this would open up for more optimizations. One could even imagine that the socket fd was part of the program (part of relocation process) when loading the program. That could get rid of yet another if-statement that check for socket existence. :-)

Quoted 10 lines

IMHO with this use-case, now is the time to introduce XDP programs per RX-queue. Yes, it will be more work, but I will offer to helpout. This should be generalized as XDP programs per RX-queue can be used by other use-cases too:
In general terms: We can setup a NIC hardware filter to deliver
frame matching some criteria, then we can avoid rechecking these criterias in on the (RX) CPU when/if we can attach an XDP prog to this specific RX-queue directly. This *IS* exactly what AF_XDP does, but it is in general useful for others, like CPUMAP redirect.

Fair enough, and thank you for offering to help out. And the fact that *other than AF_XDP* can benefit from that is good. Before we dive down this hole, what are the opinions of the BPF maintainers? Maybe it's better to hack an RFC, and then take the discussion?

Björn

#10 lore raw
Quoted 41 lines · On Mon, 17 Dec 2018 14:39:57 +0100 Björn Töpel <[email protected]> wrote:
Quoted 19 lines · Jesper Dangaard Brouer
Quoted 16 lines · On Mon, 17 Dec 2018 11:24:54 +0100 Björn Töpel <[email protected]> wrote:

XDP_ATTACH associates an XDP socket to a specific netdev Rx queue. To redirect a packet to an attached socket from XDP, the bpf_xsk_redirect helper is used. The bpf_xsk_redirect helper is also introduced in this series.

Many XDP socket users just need a simple way of creating/binding a socket and receiving frames right away without a complicated XDP program. "Attached" XDP sockets removes the need for the XSKMAP, and allows for a trivial XDP program, e.g.:

   SEC("xdp")
   int xdp_prog(struct xdp_md *ctx)
   {
         return bpf_xsk_redirect(ctx);
   }
Quoted 5 lines
Quoted 2 lines

An attached XDP socket also has better performance than the XSKMAP based sockets (performance numbers below).

I still have a general problem with this approach.

And I appreciate that you have comments on the design/code! :-) Thank you!

Quoted 11 lines

The AF_XDP socket is build around (and gets its performance) from being tied to a specific RX-queue. That design begs to have an XDP program per RX-queue.

Patchset-v1 moved towards this goal. But in this patchset-v2 you steer away from this again, and work-around the issue with the current limitations of 1-XDP program per netdev. (Which result in; if a single AF_XDP socket is used in the system, which can ONLY be for a single RX-queue by design, then ALL other XDP_PASS traffic also have to take the overhead of indirect BPF call).

Quoted 4 lines

I agree that a per-queue-program would be a good fit, but I think it still makes sense to add XDP_ATTACH and the helper, to make it easier for the XDP program authors to use AF_XDP. Would you prefer that this functionality was help back, until per-queue programs are introduced?

Yes, for the reasons you yourself listed in next section:

Quoted 5 lines

OTOH the implementation would probably look different if there was a per-q program, because this would open up for more optimizations. One could even imagine that the socket fd was part of the program (part of relocation process) when loading the program. That could get rid of yet another if-statement that check for socket existence. :-)

Yes, exactly. The implementation would probably look different, when we look at it from a more generic angle, with per-q programs.

Quoted 11 lines
Quoted 10 lines

IMHO with this use-case, now is the time to introduce XDP programs per RX-queue. Yes, it will be more work, but I will offer to helpout. This should be generalized as XDP programs per RX-queue can be used by other use-cases too:
In general terms: We can setup a NIC hardware filter to deliver
frame matching some criteria, then we can avoid rechecking these criterias in on the (RX) CPU when/if we can attach an XDP prog to this specific RX-queue directly. This *IS* exactly what AF_XDP does, but it is in general useful for others, like CPUMAP redirect.

Quoted 4 lines

Fair enough, and thank you for offering to help out. And the fact that *other than AF_XDP* can benefit from that is good. Before we dive down this hole, what are the opinions of the BPF maintainers? Maybe it's better to hack an RFC, and then take the discussion?

As XDP grows, and more use-cases are added, then I fear that the single XDP program per netdev is going to be a performance bottleneck. As the single XDP program, will have to perform a lot of common checks before it knows what use-case this packet match. With an XDP program per RX-queue, we can instead leverage the hardware to pre-filter/sort packets, and thus simplify the XDP programs.
And this patchset already do shows a performance advantage of
simplifying the XDP prog and allowing to store info per RX-queue (the xsk-sock) that allows you do take a more direct action (avoiding exec some of the redirect-core code).

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
#11 lore raw
Quoted 77 lines · Jesper Dangaard Brouer
Quoted 41 lines · On Mon, 17 Dec 2018 14:39:57 +0100 Björn Töpel <[email protected]> wrote:
Quoted 19 lines · Jesper Dangaard Brouer
Quoted 16 lines · On Mon, 17 Dec 2018 11:24:54 +0100 Björn Töpel <[email protected]> wrote:

XDP_ATTACH associates an XDP socket to a specific netdev Rx queue. To redirect a packet to an attached socket from XDP, the bpf_xsk_redirect helper is used. The bpf_xsk_redirect helper is also introduced in this series.

Many XDP socket users just need a simple way of creating/binding a socket and receiving frames right away without a complicated XDP program. "Attached" XDP sockets removes the need for the XSKMAP, and allows for a trivial XDP program, e.g.:

   SEC("xdp")
   int xdp_prog(struct xdp_md *ctx)
   {
         return bpf_xsk_redirect(ctx);
   }
Quoted 5 lines
Quoted 2 lines

An attached XDP socket also has better performance than the XSKMAP based sockets (performance numbers below).

I still have a general problem with this approach.

And I appreciate that you have comments on the design/code! :-) Thank you!

Quoted 11 lines

The AF_XDP socket is build around (and gets its performance) from being tied to a specific RX-queue. That design begs to have an XDP program per RX-queue.

Patchset-v1 moved towards this goal. But in this patchset-v2 you steer away from this again, and work-around the issue with the current limitations of 1-XDP program per netdev. (Which result in; if a single AF_XDP socket is used in the system, which can ONLY be for a single RX-queue by design, then ALL other XDP_PASS traffic also have to take the overhead of indirect BPF call).

Quoted 4 lines

I agree that a per-queue-program would be a good fit, but I think it still makes sense to add XDP_ATTACH and the helper, to make it easier for the XDP program authors to use AF_XDP. Would you prefer that this functionality was help back, until per-queue programs are introduced?

Yes, for the reasons you yourself listed in next section:

Quoted 5 lines

OTOH the implementation would probably look different if there was a per-q program, because this would open up for more optimizations. One could even imagine that the socket fd was part of the program (part of relocation process) when loading the program. That could get rid of yet another if-statement that check for socket existence. :-)

Yes, exactly. The implementation would probably look different, when we look at it from a more generic angle, with per-q programs.

Quoted 11 lines
Quoted 10 lines

IMHO with this use-case, now is the time to introduce XDP programs per RX-queue. Yes, it will be more work, but I will offer to helpout. This should be generalized as XDP programs per RX-queue can be used by other use-cases too:
In general terms: We can setup a NIC hardware filter to deliver
frame matching some criteria, then we can avoid rechecking these criterias in on the (RX) CPU when/if we can attach an XDP prog to this specific RX-queue directly. This *IS* exactly what AF_XDP does, but it is in general useful for others, like CPUMAP redirect.

Quoted 4 lines

Fair enough, and thank you for offering to help out. And the fact that *other than AF_XDP* can benefit from that is good. Before we dive down this hole, what are the opinions of the BPF maintainers? Maybe it's better to hack an RFC, and then take the discussion?

Quoted 10 lines

As XDP grows, and more use-cases are added, then I fear that the single XDP program per netdev is going to be a performance bottleneck. As the single XDP program, will have to perform a lot of common checks before it knows what use-case this packet match. With an XDP program per RX-queue, we can instead leverage the hardware to pre-filter/sort packets, and thus simplify the XDP programs.
And this patchset already do shows a performance advantage of
simplifying the XDP prog and allowing to store info per RX-queue (the xsk-sock) that allows you do take a more direct action (avoiding exec some of the redirect-core code).

Instead of introducing the XDP_ATTACH option to the bind call, can we just make this association between rx queue id and xsk every single time we bind? Then it is up to the user via the XDP program if he/she wants to use this by calling xsk_redirect. No new option needed.

We could also make the setup of AF_XDP easier by just hiding the loading and creation of the XSKMAP and the XDP program behind xsk_socket__create in the patch set I am working on. It could take a parameter in the configuration struct stating if libbpf should load a predefined program (with xsk_redirect alternatively XSKMAP that directs all traffic on a queue to a specific AF_XDP socket), or if the user desires to set this up manually. The built-in program would be supplied inside libbpf and would be very small. We could start with the current XSKMAP and just setup everything in the same way the sample program does. If people think xsk_redirect is a good idea, then we could move over to that, as it has higher performance.

Please let me know what you think.

BTW, I like the per RX queue XDP program idea. Could see a number of performance optimizations that could be done given that that existed.

Thanks: Magnus

Quoted 5 lines

--
Best regards,
Jesper Dangaard Brouer
MSc.CS, Principal Kernel Engineer at Red Hat
LinkedIn: http://www.linkedin.com/in/brouer

#12 lore raw
Quoted 90 lines · Magnus Karlsson
Quoted 77 lines · Jesper Dangaard Brouer
Quoted 41 lines · On Mon, 17 Dec 2018 14:39:57 +0100 Björn Töpel <[email protected]> wrote:
Quoted 19 lines · Jesper Dangaard Brouer
Quoted 16 lines · On Mon, 17 Dec 2018 11:24:54 +0100 Björn Töpel <[email protected]> wrote:

XDP_ATTACH associates an XDP socket to a specific netdev Rx queue. To redirect a packet to an attached socket from XDP, the bpf_xsk_redirect helper is used. The bpf_xsk_redirect helper is also introduced in this series.

Many XDP socket users just need a simple way of creating/binding a socket and receiving frames right away without a complicated XDP program. "Attached" XDP sockets removes the need for the XSKMAP, and allows for a trivial XDP program, e.g.:

    SEC("xdp")
    int xdp_prog(struct xdp_md *ctx)
    {
          return bpf_xsk_redirect(ctx);
    }
Quoted 5 lines
Quoted 2 lines

An attached XDP socket also has better performance than the XSKMAP based sockets (performance numbers below).

I still have a general problem with this approach.

And I appreciate that you have comments on the design/code! :-) Thank you!

Quoted 11 lines

The AF_XDP socket is build around (and gets its performance) from being tied to a specific RX-queue. That design begs to have an XDP program per RX-queue.

Patchset-v1 moved towards this goal. But in this patchset-v2 you steer away from this again, and work-around the issue with the current limitations of 1-XDP program per netdev. (Which result in; if a single AF_XDP socket is used in the system, which can ONLY be for a single RX-queue by design, then ALL other XDP_PASS traffic also have to take the overhead of indirect BPF call).

Quoted 4 lines

I agree that a per-queue-program would be a good fit, but I think it still makes sense to add XDP_ATTACH and the helper, to make it easier for the XDP program authors to use AF_XDP. Would you prefer that this functionality was help back, until per-queue programs are introduced?

Yes, for the reasons you yourself listed in next section:

Quoted 5 lines

OTOH the implementation would probably look different if there was a per-q program, because this would open up for more optimizations. One could even imagine that the socket fd was part of the program (part of relocation process) when loading the program. That could get rid of yet another if-statement that check for socket existence. :-)

Yes, exactly. The implementation would probably look different, when we look at it from a more generic angle, with per-q programs.

Quoted 11 lines
Quoted 10 lines

IMHO with this use-case, now is the time to introduce XDP programs per RX-queue. Yes, it will be more work, but I will offer to helpout. This should be generalized as XDP programs per RX-queue can be used by other use-cases too:
In general terms: We can setup a NIC hardware filter to deliver
frame matching some criteria, then we can avoid rechecking these criterias in on the (RX) CPU when/if we can attach an XDP prog to this specific RX-queue directly. This *IS* exactly what AF_XDP does, but it is in general useful for others, like CPUMAP redirect.

Quoted 4 lines

Fair enough, and thank you for offering to help out. And the fact that *other than AF_XDP* can benefit from that is good. Before we dive down this hole, what are the opinions of the BPF maintainers? Maybe it's better to hack an RFC, and then take the discussion?

Quoted 10 lines

As XDP grows, and more use-cases are added, then I fear that the single XDP program per netdev is going to be a performance bottleneck. As the single XDP program, will have to perform a lot of common checks before it knows what use-case this packet match. With an XDP program per RX-queue, we can instead leverage the hardware to pre-filter/sort packets, and thus simplify the XDP programs.
And this patchset already do shows a performance advantage of
simplifying the XDP prog and allowing to store info per RX-queue (the xsk-sock) that allows you do take a more direct action (avoiding exec some of the redirect-core code).

Quoted 5 lines

Instead of introducing the XDP_ATTACH option to the bind call, can we just make this association between rx queue id and xsk every single time we bind? Then it is up to the user via the XDP program if he/she wants to use this by calling xsk_redirect. No new option needed.

Nice! Then it would simply be a matter of adding the helper. Much better than extending the uapi. Thank you for pointing this out!

Quoted 13 lines

We could also make the setup of AF_XDP easier by just hiding the loading and creation of the XSKMAP and the XDP program behind xsk_socket__create in the patch set I am working on. It could take a parameter in the configuration struct stating if libbpf should load a predefined program (with xsk_redirect alternatively XSKMAP that directs all traffic on a queue to a specific AF_XDP socket), or if the user desires to set this up manually. The built-in program would be supplied inside libbpf and would be very small. We could start with the current XSKMAP and just setup everything in the same way the sample program does. If people think xsk_redirect is a good idea, then we could move over to that, as it has higher performance.

Quoted 2 lines

Please let me know what you think.

Hmm, let's start of with this, making sure AF_XDP is simple to use from a libbpf perspective, and then (maybe) move towards the bpf_xsk_redirect, depending on where the per-queue work moves.

Let's drop this series for now, and focus on libbpf and the per-queue XDP programs.

Quoted 3 lines

BTW, I like the per RX queue XDP program idea. Could see a number of performance optimizations that could be done given that that existed.

Yes! It would be really cool do use the socket file descriptor in the BPF program, so that the bpf_xsk_redirect could use the socket directly (analogous to bpf_map in redirect map).

Another idea with per-queue programs is that the socket could be implicity bound to queue/dev when installing the program to an Rx queue.

Björn

Quoted 8 lines

Thanks: Magnus

Quoted 5 lines

--
Best regards,
Jesper Dangaard Brouer
MSc.CS, Principal Kernel Engineer at Red Hat
LinkedIn: http://www.linkedin.com/in/brouer

#17 lore raw
In reply to Björn Töpel
Quoted 5 lines · Björn Töpel

OTOH the implementation would probably look different if there was a per-q program, because this would open up for more optimizations. One could even imagine that the socket fd was part of the program (part of relocation process) when loading the program. That could get rid of yet another if-statement that check for socket existence. :-)

Interesting thought, then we would have to teach the BPF subsystem to express program dependencies (this program can only be used on queue where given XSK is attached), and have the networking subsystem (drivers?) check those constraints when attaching. Did I get this right?

#18 lore raw
Quoted 7 lines · Jakub Kicinski
Quoted 5 lines · Björn Töpel

OTOH the implementation would probably look different if there was a per-q program, because this would open up for more optimizations. One could even imagine that the socket fd was part of the program (part of relocation process) when loading the program. That could get rid of yet another if-statement that check for socket existence. :-)

Quoted 5 lines

Interesting thought, then we would have to teach the BPF subsystem to express program dependencies (this program can only be used on queue where given XSK is attached), and have the networking subsystem (drivers?) check those constraints when attaching. Did I get this right?

Yes. Well, yet another dependency, similar to how a program refers to maps.

The per-queue loading could be similar to passing the prog_ifindex in the prog-loading attribute, used by the HW offloading. I.e. in addition to prog_ifindex, there would be, say, prog_ifqueue.

For the XDP socket constraint checking I see two models. The BPF program refers to an unbound socket(s), and by installing the program to a certain Rx queue, the socket(s) will be implicitly bound. If the socket(s) are bound to certain device/queue at install time, the networking subsystem will scream if the program is installed to a ifindex/queue != prog ifindex/queue.

Is the XDP HW offloading program verified at load or install time, or both?

Again, this is just me thinking out loud... Might be a lousy idea. :-)

Side-note: When we started AF_XDP, we played around with the idea to associate the XSKMAP to a certain ifindex/queue, to be able to reject sockets bound to another device/queue and elide a couple of checks in the fast-path. My ramblings above is somewhat in the same vein. We'd like to be able to set program constraints for netdev/queue target, so that we can remove checks in the fast-path.

Björn

#19 lore raw
Quoted 1 line · Björn Töpel

Is the XDP HW offloading program verified at load or install time, or both?

Load interacts with the ifindex of choice, install checks if the load time device matches.

#13 lore raw
In reply to Björn Töpel
Quoted 9 lines · Björn Töpel
Quoted 6 lines

Instead of introducing the XDP_ATTACH option to the bind call, can we just make this association between rx queue id and xsk every single time we bind? Then it is up to the user via the XDP program if he/she wants to use this by calling xsk_redirect. No new option needed.

Nice! Then it would simply be a matter of adding the helper. Much better than extending the uapi. Thank you for pointing this out!

Magnus's by-default association approach sounds good to me. I'm missing though why extra bpf helper is needed.
This soft-bind rx queue with xsk at the time
of normal bind() should work as-is and no program necessary. No prog -> default assoc. Yet the prog has an ability to redirect to specific xsk via existing bpf_redirect_map
or via faster (future) xsk_redirect.

Also I agree with Jesper that per-queue programs would be good to have sooner than later, but I disagree that it's mandatory for this set. It seems the patches will give us performance boost and simplification of uapi while per-queue prog api is being discussed and implemented.

I like the other idea too (of having libbpf provide canned bpf progs for different use cases). That's better than loading them from the kernel.

#14 lore raw
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Björn Töpel
Quoted 6 lines

Instead of introducing the XDP_ATTACH option to the bind call, can we just make this association between rx queue id and xsk every single time we bind? Then it is up to the user via the XDP program if he/she wants to use this by calling xsk_redirect. No new option needed.

Quoted 7 lines
Quoted 2 lines

Nice! Then it would simply be a matter of adding the helper. Much better than extending the uapi. Thank you for pointing this out!

Magnus's by-default association approach sounds good to me. I'm missing though why extra bpf helper is needed. This soft-bind rx queue with xsk at the time
of normal bind() should work as-is and no program necessary.

Hmm, an XDP program has always been required. It's just whether the program needs an XSKMAP or not. The XDP_ATTACH can be seens as "use an internal map" -- and the mechanism to redirect using that map is via the bpf_xsk_redirect helper. What Magnus proposed was populating the builtin map unconditionally, and therefore removing the need for the XDP_ATTACH option.

If I am interpreting what you're saying correctly, you thought XDP_ATTACH (in v1) was bypassing the XDP path. This is not something that we want. AF_XDP is a proper XDP construct, and require an XDP program to receive frames. What I wanted to improve with this series was to lower the complexity of the minimal XDP program to receive a frame.

Quoted 4 lines

No prog -> default assoc. Yet the prog has an ability to redirect to specific xsk via existing bpf_redirect_map
or via faster (future) xsk_redirect.

Quoted 9 lines

Also I agree with Jesper that per-queue programs would be good to have sooner than later, but I disagree that it's mandatory for this set. It seems the patches will give us performance boost and simplification of uapi while per-queue prog api is being discussed and implemented.

I like the other idea too (of having libbpf provide canned bpf progs for different use cases). That's better than loading them from the kernel.

To summarize; I'll hold off this series for now (except the first cleanup patch which I'll send out today), and spend some time on per-queue programs, iproute2 support and libbpf.

Björn

#15 lore raw
Quoted 3 lines · On Wed, 19 Dec 2018 10:34:28 +0100 Björn Töpel <[email protected]> wrote:

To summarize; I'll hold off this series for now (except the first cleanup patch which I'll send out today), and spend some time on per-queue programs, iproute2 support and libbpf.

As promised I'll be helping out with per-queue programs, and is currentl writing up a design document here [1], and tomorrow we will do a meeting (with Bjørn and Magnus) about this design and next steps.

[1] https://github.com/xdp-project/xdp-project/blob/master/areas/core/xdp_per_rxq01.org

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
#16 lore raw
In reply to Björn Töpel
Quoted 18 lines · Björn Töpel
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Björn Töpel
Quoted 6 lines

Instead of introducing the XDP_ATTACH option to the bind call, can we just make this association between rx queue id and xsk every single time we bind? Then it is up to the user via the XDP program if he/she wants to use this by calling xsk_redirect. No new option needed.

Quoted 7 lines
Quoted 2 lines

Nice! Then it would simply be a matter of adding the helper. Much better than extending the uapi. Thank you for pointing this out!

Magnus's by-default association approach sounds good to me. I'm missing though why extra bpf helper is needed. This soft-bind rx queue with xsk at the time
of normal bind() should work as-is and no program necessary.

Quoted 6 lines

Hmm, an XDP program has always been required. It's just whether the program needs an XSKMAP or not. The XDP_ATTACH can be seens as "use an internal map" -- and the mechanism to redirect using that map is via the bpf_xsk_redirect helper. What Magnus proposed was populating the builtin map unconditionally, and therefore removing the need for the XDP_ATTACH option.

well, I'm actually proposing to make af_xdp program optional if hard coded queue->xsk mapping gives extra performance.