Skip to contentSkip to conversation

Add TC-BPF API

PATCH Superseded Kumar Kartikeya Dwivedi 4 participants 18 messages Apr 23, 2021 – Apr 27, 2021

Subsystems (6)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

v4 cover Add TC-BPF API

This is the fourth version of the TC-BPF series.

It adds a simple API that uses netlink to attach the tc filter and its bpf classifier. Currently, a user needs to shell out to the tc command line to be able to create filters and attach SCHED_CLS programs as classifiers. With the help of this API, it will be possible to use libbpf for doing all parts of bpf program setup and attach.

Changelog contains details of patchset evolution.

In an effort to keep discussion focused, this series doesn't have the high level TC-BPF API. It was clear that there is a need for a bpf_link API in the kernel, hence that will be submitted as a separate patchset.

The individual commit messages contain more details, and also a brief summary of the API.

Changelog:
----------
v3 -> v4
v3: https://lore.kernel.org/bpf/[email protected]

* We add a concept of bpf_tc_ctx context structure representing the attach point. The qdisc setup and delete is tied to this object's lifetime if it succeeds in creating the clsact qdisc when the attach point is BPF_TC_INGRESS or BPF_TC_EGRESS. Qdisc is only deleted when there are no filters attached to it. The struct itself is opaque to the user.
* Refactor all API functions to take ctx.
* Remove bpf_tc_info, bpf_tc_attach_id, instead reuse bpf_tc_opts for filling in attributes in various API functions (including query).
* Explicitly document the expectation of each function regarding the opts fields that must be set/unset. Add some small notes for the defaults chosen by the API.
* Rename bpf_tc_get_info to bpf_tc_query
* Keep the netlink socket open in the context structure to save on open/close cycles for each operation.
* Miscellaneous adjustments due to keeping the socket open.
* Rewrite the tests, and also add tests for verifying all preconditions of the TC-BPF API.
* Use bpf skeleton API in examples and tests.

v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]

* bpf_tc_cls_* -> bpf_tc_* rename
* bpf_tc_attach_id now only consists of handle and priority, the two variables that user may or may not set.
* bpf_tc_replace has been dropped, instead a replace bool is introduced in bpf_tc_opts for the same purpose.
* bpf_tc_get_info now takes attach_id for filling in filter details during lookup instead of requiring user to do so. This also allows us to remove the fd parameter, as no matching is needed as long as we have all attributes necessary to identify a specific filter.
* A little bit of code simplification taking into account the change above.
* priority and protocol are now __u16 members in user facing API structs to reflect actual size.
* Patch updating pkt_cls.h header has been removed, as it is unused now.
* protocol and chain_index options have been dropped in bpf_tc_opts, protocol is always set to ETH_P_ALL, while chain_index is set as 0 by default in the kernel. This also means removal of chain_index from bpf_tc_attach_id, as it is unconditionally always 0.
* bpf_tc_cls_change has been dropped
* selftest now uses ASSERT_* macros

v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]

* netlink helpers have been renamed to object_action style.
* attach_id now only contains attributes that are not explicitly set. Only the bare minimum info is kept in it.
* protocol is now an optional and always set to ETH_P_ALL.
* direct-action mode is always set.
* skip_sw and skip_hw options have also been removed.
* bpf_tc_cls_info struct now also returns the bpf program tag and id, as available in the netlink response. This came up as a requirement during discussion with people wanting to use this functionality.
* support for attaching SCHED_ACT programs has been dropped, as it isn't useful without any support for binding loaded actions to a classifier.
* the distinction between dev and block API has been dropped, there is now a single set of functions and user has to pass the special ifindex value to indicate operation on a shared filter block on their own.
* The high level API returning a bpf_link is gone. This was already non- functional for pinning and typical ownership semantics. Instead, a separate patchset will be sent adding a bpf_link API for attaching SCHED_CLS progs to the kernel, and its corresponding libbpf API.
* The clsact qdisc is now setup automatically in a best-effort fashion whenever user passes in the clsact ingress or egress parent id. This is done with exclusive mode, such that if an ingress or clsact qdisc is already set up, we skip the setup and move on with filter creation.
* Other minor changes that came up during the course of discussion and rework.

Kumar Kartikeya Dwivedi (3):
libbpf: add helpers for preparing netlink attributes
libbpf: add low level TC-BPF API
libbpf: add selftests for TC-BPF API

 tools/lib/bpf/libbpf.h                        |  92 ++++
 tools/lib/bpf/libbpf.map                      |   5 +
 tools/lib/bpf/netlink.c                       | 515 +++++++++++++++++-
 tools/lib/bpf/nlattr.h                        |  48 ++
 .../testing/selftests/bpf/prog_tests/tc_bpf.c | 204 +++++++
 .../testing/selftests/bpf/progs/test_tc_bpf.c |  12 +
 6 files changed, 854 insertions(+), 22 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf.c

--
2.30.2

author maintainer
#1 lore raw

PATCH v4 1/3 libbpf: add helpers for preparing netlink attributes

This change introduces a few helpers to wrap open coded attribute preparation in netlink.c.

Every nested attribute's closure must happen using the helper nlattr_end_nested, which sets its length properly. NLA_F_NESTED is enforced using nlattr_begin_nested helper. Other simple attributes can be added directly.

The maxsz parameter corresponds to the size of the request structure which is being filled in, so for instance with req being:

struct {
	struct nlmsghdr nh;
	struct tcmsg t;
	char buf[4096];
} req;

Then, maxsz should be sizeof(req).

This change also converts the open coded attribute preparation with the helpers. Note that the only failure the internal call to nlattr_add could result in the nested helper would be -EMSGSIZE, hence that is what we return to our caller.

---
 tools/lib/bpf/netlink.c | 37 ++++++++++++++-----------------
 tools/lib/bpf/nlattr.h  | 48 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 21 deletions(-)
tools/lib/bpf/netlink.c +16 -21
index d2cb28e9ef52..c79e30484e81 100644
@@ -135,7 +135,7 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
 					 __u32 flags)
 {
 	int sock, seq = 0, ret;
-	struct nlattr *nla, *nla_xdp;
+	struct nlattr *nla;
 	struct {
 		struct nlmsghdr  nh;
 		struct ifinfomsg ifinfo;
@@ -157,36 +157,31 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
 	req.ifinfo.ifi_index = ifindex;
 
 	/* started nested attribute for XDP */
-	nla = (struct nlattr *)(((char *)&req)
-				+ NLMSG_ALIGN(req.nh.nlmsg_len));
-	nla->nla_type = NLA_F_NESTED | IFLA_XDP;
-	nla->nla_len = NLA_HDRLEN;
+	nla = nlattr_begin_nested(&req.nh, sizeof(req), IFLA_XDP);
+	if (!nla) {
+		ret = -EMSGSIZE;
+		goto cleanup;
+	}
 
 	/* add XDP fd */
-	nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
-	nla_xdp->nla_type = IFLA_XDP_FD;
-	nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
-	memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
-	nla->nla_len += nla_xdp->nla_len;
+	ret = nlattr_add(&req.nh, sizeof(req), IFLA_XDP_FD, &fd, sizeof(fd));
+	if (ret < 0)
+		goto cleanup;
 
 	/* if user passed in any flags, add those too */
 	if (flags) {
-		nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
-		nla_xdp->nla_type = IFLA_XDP_FLAGS;
-		nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
-		memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
-		nla->nla_len += nla_xdp->nla_len;
+		ret = nlattr_add(&req.nh, sizeof(req), IFLA_XDP_FLAGS, &flags, sizeof(flags));
+		if (ret < 0)
+			goto cleanup;
 	}
 
 	if (flags & XDP_FLAGS_REPLACE) {
-		nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
-		nla_xdp->nla_type = IFLA_XDP_EXPECTED_FD;
-		nla_xdp->nla_len = NLA_HDRLEN + sizeof(old_fd);
-		memcpy((char *)nla_xdp + NLA_HDRLEN, &old_fd, sizeof(old_fd));
-		nla->nla_len += nla_xdp->nla_len;
+		ret = nlattr_add(&req.nh, sizeof(req), IFLA_XDP_EXPECTED_FD, &flags, sizeof(flags));
+		if (ret < 0)
+			goto cleanup;
 	}
 
-	req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
+	nlattr_end_nested(&req.nh, nla);
 
 	if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
 		ret = -errno;
tools/lib/bpf/nlattr.h +48 -0

59 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH v4 2/3 libbpf: add low level TC-BPF API

In reply to Kumar Kartikeya Dwivedi

This adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters.

An API summary:

bpf_tc_ctx_init may be used to create a context structure represented by the opaque struct bpf_tc_ctx pointer returned to the caller. A bpf context represents the attach point for a filter, which means it takes the ifindex of the device user is attaching to, and an attach point that translates to the parent of the filter. When specifying BPF_TC_INGRESS or BPF_TC_EGRESS, the ctx initialization will automatically setup the clsact qdisc if possible, and also clean it up if it was created during ctx_init and there are no more remaining filters. A custom parent value can be set in opts during filter attach and indicated by setting BPF_TC_CUSTOM_PARENT during ctx initialization.

bpf_tc_ctx_destroy is used to release a bpf_tc_ctx object and remove any qdiscs owned by it. Note that there is a small race between the checking of existing filters and removal of qdisc. The chances of this happening are quite slim however, as we only remove a qdisc we set up.

bpf_tc_attach may be used to attach, and replace a filter and bind a SCHED_CLS prog as its bpf classifier. Direct action mode is always enabled, and protocol is always set to ETH_P_ALL. The filter is attached to the main chain with index 0.

bpf_tc_detach may be used to delete existing filter and detach the SCHED_CLS filter.

Example (using bpf skeleton infrastructure):

BPF program (test_tc_bpf.c):

	#include <linux/bpf.h>
	#include <bpf/bpf_helpers.h>

	SEC("classifier")
	int cls(struct __sk_buff *skb)
	{
		return 0;
	}

Userspace loader:

	DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, 0);
	struct test_tc_bpf *skel;
	struct bpf_tc_ctx *ctx;
	int fd, r;

	skel = test_tc_bpf__open_and_load();
	if (!skel)
		return -ENOMEM;

	fd = bpf_program__fd(skel->progs.cls);

	ctx = bpf_tc_ctx_init(if_nametoindex("lo"),
			      BPF_TC_INGRESS, NULL);
	if (!ctx)
		return -ENOMEM;

	r = bpf_tc_attach(ctx, fd, &opts);
	if (r < 0)
		return r;

... which is roughly equivalent to:

  # tc qdisc add dev lo clsact
  # tc filter add dev lo ingress bpf obj foo.o sec classifier da

To replace an existing filter (e.g. the one we just created):

	/* opts.{handle, parent, priority, prog_id} was filled
	 * in by attach.
	 */
	opts.replace = true;
	/* clear fields that must be unset, we must also clear parent as
	   our attach point is ingress hook */
	opts.parent = opts.prog_id = 0;
	r = bpf_tc_attach(ctx, fd, &opts);
	if (r < 0)
		return r;

To obtain info of a particular filter:

	/* Find info for filter with handle 1 and priority 50 */
	DECLARE_LIBBPF_OPTS(bpf_tc_opts, info_opts, .handle = 1,
			    .priority = 50, .parent = opts.parent)
	r = bpf_tc_query(ctx, &info_opts);
	if (r < 0 && r == -ENOENT)
		printf("Filter not found\n");
	else
		return r;
---
 tools/lib/bpf/libbpf.h   |  92 ++++++++
 tools/lib/bpf/libbpf.map |   5 +
 tools/lib/bpf/netlink.c  | 478 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 574 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.h +92 -0
index bec4e6a6e31d..1c717c07b66e 100644
@@ -775,6 +775,98 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen
 LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
 LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
+enum bpf_tc_attach_point {
+	BPF_TC_INGRESS,
+	BPF_TC_EGRESS,
+	BPF_TC_CUSTOM_PARENT,
+	_BPF_TC_PARENT_MAX,
+};
+
+/* The opts structure is also used to return the created filters attributes
+ * (e.g. in case the user left them unset). Some of the options that were left
+ * out default to a reasonable value, documented below.
+ *
+ *	protocol - ETH_P_ALL
+ *	chain index - 0
+ *	class_id - 0 (can be set by bpf program using skb->tc_classid)
+ *	bpf_flags - TCA_BPF_FLAG_ACT_DIRECT (direct action mode)
+ *	bpf_flags_gen - 0
+ *
+ *	The user must fulfill documented requirements for each function.
+ */
+struct bpf_tc_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 parent;
+	__u16 priority;
+	__u32 prog_id;
+	bool replace;
+	size_t :0;
+};
+
+#define bpf_tc_opts__last_field replace
+
+struct bpf_tc_ctx;
+
+struct bpf_tc_ctx_opts {
+	size_t sz;
+};
+
+#define bpf_tc_ctx_opts__last_field sz
+
+/* Requirements */
+/*
+ * @ifindex: Must be > 0.
+ * @parent: Must be one of the enum constants < _BPF_TC_PARENT_MAX
+ * @opts: Can be NULL, currently no options are supported.
+ */
+LIBBPF_API struct bpf_tc_ctx *bpf_tc_ctx_init(__u32 ifindex,
+					      enum bpf_tc_attach_point parent,
+					      struct bpf_tc_ctx_opts *opts);
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *	 If the qdisc was attached during ctx_init, it will be deleted if no
+ *	 filters are attached to it.
+ *	 When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *	  optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+			     struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ */
+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx,
+			     const struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id
+ */
+LIBBPF_API int bpf_tc_query(struct bpf_tc_ctx *ctx,
+			    struct bpf_tc_opts *opts);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
tools/lib/bpf/libbpf.map +5 -0
index b9b29baf1df8..f6490d521601 100644
@@ -361,4 +361,9 @@ LIBBPF_0.4.0 {
 		bpf_linker__new;
 		bpf_map__inner_map;
 		bpf_object__set_kversion;
+		bpf_tc_ctx_init;
+		bpf_tc_ctx_destroy;
+		bpf_tc_attach;
+		bpf_tc_detach;
+		bpf_tc_query;
 } LIBBPF_0.3.0;
tools/lib/bpf/netlink.c +477 -1

513 lines not on this page · raw message

--
2.30.2

author maintainer
#16 lore raw

PATCH v4 3/3 libbpf: add selftests for TC-BPF API

In reply to Kumar Kartikeya Dwivedi

This adds some basic tests for the low level bpf_tc_* API.

---
 .../testing/selftests/bpf/prog_tests/tc_bpf.c | 204 ++++++++++++++++++
 .../testing/selftests/bpf/progs/test_tc_bpf.c |  12 ++
 2 files changed, 216 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf.c
tools/testing/selftests/bpf/prog_tests/tc_bpf.c new file +204 -0
new file mode 100644
index 000000000000..47505b92e50a
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include <linux/pkt_cls.h>
+
+#include "test_tc_bpf.skel.h"
+
+#define LO_IFINDEX 1
+
+static const __u32 tcm_parent[2] = {
+	[BPF_TC_INGRESS] = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS),
+	[BPF_TC_EGRESS] = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS),
+};
+
+static int test_tc_internal(struct bpf_tc_ctx *ctx, int fd,
+			    enum bpf_tc_attach_point parent)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10);
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	int ret;
+
+	ret = bpf_obj_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_EQ(ret, 0, "bpf_obj_get_info_by_fd"))
+		return ret;
+
+	ret = bpf_tc_attach(ctx, fd, &opts);
+	if (!ASSERT_EQ(ret, 0, "bpf_tc_attach"))
+		return ret;
+
+	if (!ASSERT_EQ(opts.handle, 1, "handle set") ||
+	    !ASSERT_EQ(opts.priority, 10, "priority set") ||
+	    !ASSERT_EQ(opts.parent, tcm_parent[parent], "parent set") ||
+	    !ASSERT_NEQ(opts.prog_id, 0, "prog_id set"))
+		goto end;
+
+	opts.prog_id = 0;
+	ret = bpf_tc_query(ctx, &opts);
+	if (!ASSERT_EQ(ret, 0, "bpf_tc_query"))
+		goto end;
+
+	if (!ASSERT_NEQ(opts.prog_id, 0, "prog_id set") ||
+	    !ASSERT_EQ(info.id, opts.prog_id, "prog_id matching"))
+		goto end;
+
+	/* Atomic replace */
+	opts.replace = true;
+	opts.parent = opts.prog_id = 0;
+	ret = bpf_tc_attach(ctx, fd, &opts);
+	if (!ASSERT_EQ(ret, 0, "bpf_tc_attach replace mode"))
+		return ret;
+	opts.replace = false;
+
+end:
+	opts.prog_id = 0;
+	ret = bpf_tc_detach(ctx, &opts);
+	ASSERT_EQ(ret, 0, "bpf_tc_detach");
+	return ret;
+}
+
+int test_tc_invalid(struct bpf_tc_ctx *ctx, int fd)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10,
+			    .parent = tcm_parent[BPF_TC_INGRESS]);
+	struct bpf_tc_ctx *inv_ctx;
+	int ret, saved_errno;
+
+	inv_ctx = bpf_tc_ctx_init(0, BPF_TC_INGRESS, NULL);
+	saved_errno = errno;
+	if (!ASSERT_EQ(inv_ctx, NULL, "bpf_tc_ctx_init invalid ifindex = 0"))
+		return -EINVAL;
+
+	ASSERT_EQ(saved_errno, EINVAL, "errno");
+
+	inv_ctx = bpf_tc_ctx_init(LO_IFINDEX, 0xdeadc0de, NULL);
+	saved_errno = errno;
+	if (!ASSERT_EQ(inv_ctx, NULL,
+		       "bpf_tc_ctx_init invalid parent >= _BPF_TC_PARENT_MAX"))
+		return -EINVAL;
+
+	ASSERT_EQ(saved_errno, EINVAL, "errno");
+
+	ret = bpf_tc_ctx_destroy(NULL);
+	if (!ASSERT_EQ(ret, 0, "bpf_tc_ctx_destroy ctx = NULL"))
+		return -EINVAL;
+
+	ret = bpf_tc_detach(NULL, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid ctx = NULL"))
+		return -EINVAL;
+
+	ret = bpf_tc_detach(ctx, NULL);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid opts = NULL"))
+		return -EINVAL;
+
+	ret = bpf_tc_query(NULL, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid ctx = NULL"))
+		return -EINVAL;
+
+	ret = bpf_tc_query(ctx, NULL);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid opts = NULL"))
+		return -EINVAL;
+
+	opts.replace = true;
+	ret = bpf_tc_detach(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid replace set"))
+		return -EINVAL;
+	ret = bpf_tc_query(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid replace set"))
+		return -EINVAL;
+	opts.replace = false;
+
+	opts.prog_id = 42;
+	ret = bpf_tc_detach(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid prog_id set"))
+		return -EINVAL;
+	ret = bpf_tc_query(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid prog_id set"))
+		return -EINVAL;
+	opts.prog_id = 0;
+
+	opts.handle = 0;
+	ret = bpf_tc_detach(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid handle unset"))
+		return -EINVAL;
+	ret = bpf_tc_query(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid handle unset"))
+		return -EINVAL;
+	opts.handle = 1;
+
+	opts.priority = 0;
+	ret = bpf_tc_detach(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid priority unset"))
+		return -EINVAL;
+	ret = bpf_tc_query(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid priority unset"))
+		return -EINVAL;
+	opts.priority = 10;
+
+	opts.parent = 0;
+	ret = bpf_tc_detach(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_detach invalid parent unset"))
+		return -EINVAL;
+	ret = bpf_tc_query(ctx, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_query invalid parent unset"))
+		return -EINVAL;
+
+	ret = bpf_tc_attach(NULL, fd, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid ctx = NULL"))
+		return -EINVAL;
+
+	ret = bpf_tc_attach(ctx, -1, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid fd < 0"))
+		return -EINVAL;
+
+	ret = bpf_tc_attach(ctx, fd, NULL);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid opts = NULL"))
+		return -EINVAL;
+
+	opts.prog_id = 42;
+	ret = bpf_tc_attach(ctx, fd, &opts);
+	if (!ASSERT_EQ(ret, -EINVAL, "bpf_tc_attach invalid prog_id set"))
+		return -EINVAL;
+	opts.prog_id = 0;
+
+	return 0;
+}
+
+void test_tc_bpf(void)
+{
+	struct bpf_tc_ctx *ctx_ing = NULL, *ctx_eg = NULL;
+	struct test_tc_bpf *skel = NULL;
+	int cls_fd, ret;
+
+	skel = test_tc_bpf__open_and_load();
+	if (!ASSERT_NEQ(skel, NULL, "test_tc_bpf skeleton"))
+		goto end;
+
+	cls_fd = bpf_program__fd(skel->progs.cls);
+
+	ctx_ing = bpf_tc_ctx_init(LO_IFINDEX, BPF_TC_INGRESS, NULL);
+	if (!ASSERT_NEQ(ctx_ing, NULL, "bpf_tc_ctx_init(BPF_TC_INGRESS)"))
+		goto end;
+
+	ctx_eg = bpf_tc_ctx_init(LO_IFINDEX, BPF_TC_EGRESS, NULL);
+	if (!ASSERT_NEQ(ctx_eg, NULL, "bpf_tc_ctx_init(BPF_TC_EGRESS)"))
+		goto end;
+
+	ret = test_tc_internal(ctx_ing, cls_fd, BPF_TC_INGRESS);
+	if (!ASSERT_EQ(ret, 0, "test_tc_internal ingress"))
+		goto end;
+
+	ret = test_tc_internal(ctx_eg, cls_fd, BPF_TC_EGRESS);
+	if (!ASSERT_EQ(ret, 0, "test_tc_internal egress"))
+		goto end;
+
+	ret = test_tc_invalid(ctx_ing, cls_fd);
+	if (!ASSERT_EQ(ret, 0, "test_tc_invalid"))
+		goto end;
+
+end:
+	bpf_tc_ctx_destroy(ctx_eg);
+	bpf_tc_ctx_destroy(ctx_ing);
+	test_tc_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/test_tc_bpf.c new file +12 -0
new file mode 100644
index 000000000000..18a3a7ed924a
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* Dummy prog to test TC-BPF API */
+
+SEC("classifier")
+int cls(struct __sk_buff *skb)
+{
+	return 0;
+}
Signature · 1 line
2.30.2
maintainer
#3 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

In reply to Kumar Kartikeya Dwivedi

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:
[...]

Quoted 10 lines
  tools/lib/bpf/libbpf.h   |  92 ++++++++
  tools/lib/bpf/libbpf.map |   5 +
  tools/lib/bpf/netlink.c  | 478 ++++++++++++++++++++++++++++++++++++++-
  3 files changed, 574 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.h +0 -0
index bec4e6a6e31d..1c717c07b66e 100644

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782

Quoted 8 lines
tools/lib/bpf/libbpf.h +5 -0
@@ -775,6 +775,98 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen
  LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
+enum bpf_tc_attach_point {
+	BPF_TC_INGRESS,
+	BPF_TC_EGRESS,
+	BPF_TC_CUSTOM_PARENT,
+	_BPF_TC_PARENT_MAX,

I don't think we need to expose _BPF_TC_PARENT_MAX as part of the API, I would drop the latter.

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 795

Quoted 13 lines
tools/lib/bpf/libbpf.h +13 -0
+};
+
+/* The opts structure is also used to return the created filters attributes
+ * (e.g. in case the user left them unset). Some of the options that were left
+ * out default to a reasonable value, documented below.
+ *
+ *	protocol - ETH_P_ALL
+ *	chain index - 0
+ *	class_id - 0 (can be set by bpf program using skb->tc_classid)
+ *	bpf_flags - TCA_BPF_FLAG_ACT_DIRECT (direct action mode)
+ *	bpf_flags_gen - 0
+ *
+ *	The user must fulfill documented requirements for each function.

Not sure if this is overly relevant as part of the bpf_tc_opts in here. For the 2nd part, I would probably just mention that libbpf internally attaches the bpf programs with direct action mode. The hw offload may be future todo, and the other bits are little used anyway; mentioning them here, what value does it have to libbpf users? I'd rather just drop the 2nd part and/or simplify this paragraph just stating that the progs are attached in direct action mode.

Quoted 17 lines
tools/lib/bpf/libbpf.h +17 -0
+ */
+struct bpf_tc_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 parent;
+	__u16 priority;
+	__u32 prog_id;
+	bool replace;
+	size_t :0;
+};
+
+#define bpf_tc_opts__last_field replace
+
+struct bpf_tc_ctx;
+
+struct bpf_tc_ctx_opts {
+	size_t sz;

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 822

Quoted 10 lines
tools/lib/bpf/libbpf.h +10 -0
+};
+
+#define bpf_tc_ctx_opts__last_field sz
+
+/* Requirements */
+/*
+ * @ifindex: Must be > 0.
+ * @parent: Must be one of the enum constants < _BPF_TC_PARENT_MAX
+ * @opts: Can be NULL, currently no options are supported.
+ */

Up to Andrii, but we don't have such API doc in general inside libbpf.h, I would drop it for the time being to be consistent with the rest (same for others below).

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 823

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+LIBBPF_API struct bpf_tc_ctx *bpf_tc_ctx_init(__u32 ifindex,

nit: in user space s/__u32 ifindex/int ifindex/

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 825

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+					      enum bpf_tc_attach_point parent,
+					      struct bpf_tc_ctx_opts *opts);

Should we enforce opts being NULL or non-NULL here, or drop the arg from here for now altogether? (And if later versions of the functions show up this could be mapped to the right one?)

Quoted 20 lines
tools/lib/bpf/libbpf.h +20 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *	 If the qdisc was attached during ctx_init, it will be deleted if no
+ *	 filters are attached to it.
+ *	 When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *	  optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 855

Quoted 10 lines
tools/lib/bpf/libbpf.h +10 -0
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+			     struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ */
+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx,
+			     const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Could we add a BPF_TC_UNSPEC to enum bpf_tc_attach_point, which the user would pass to bpf_tc_ctx_init(), so that opts.direction = BPF_TC_INGRESS with subsequent bpf_tc_attach() can be called, and same opts.direction = BPF_TC_EGRESS with bpf_tc_attach() for different fd. The only thing we cared about in bpf_tc_ctx_init() resp. the ctx was that qdisc was ready.

Quoted 17 lines
tools/lib/bpf/libbpf.h +14 -0
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id
+ */
+LIBBPF_API int bpf_tc_query(struct bpf_tc_ctx *ctx,
+			    struct bpf_tc_opts *opts);
+
  #ifdef __cplusplus
  } /* extern "C" */
  #endif
#4 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 36 lines · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:
[...]

Quoted 10 lines
  tools/lib/bpf/libbpf.h   |  92 ++++++++
  tools/lib/bpf/libbpf.map |   5 +
  tools/lib/bpf/netlink.c  | 478 ++++++++++++++++++++++++++++++++++++++-
  3 files changed, 574 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.h +0 -0
index bec4e6a6e31d..1c717c07b66e 100644

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782

Quoted 8 lines
tools/lib/bpf/libbpf.h +5 -0
@@ -775,6 +775,98 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen
  LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
+enum bpf_tc_attach_point {
+	BPF_TC_INGRESS,
+	BPF_TC_EGRESS,
+	BPF_TC_CUSTOM_PARENT,
+	_BPF_TC_PARENT_MAX,

I don't think we need to expose _BPF_TC_PARENT_MAX as part of the API, I would drop the latter.

Quoted 12 lines
tools/lib/bpf/libbpf.h +12 -0
+};
+
+/* The opts structure is also used to return the created filters attributes
+ * (e.g. in case the user left them unset). Some of the options that were left
+ * out default to a reasonable value, documented below.
+ *
+ *	protocol - ETH_P_ALL
+ *	chain index - 0
+ *	class_id - 0 (can be set by bpf program using skb->tc_classid)
+ *	bpf_flags - TCA_BPF_FLAG_ACT_DIRECT (direct action mode)
+ *	bpf_flags_gen - 0
+ *
Quoted 8 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 795

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+ *	The user must fulfill documented requirements for each function.

Not sure if this is overly relevant as part of the bpf_tc_opts in here. For the 2nd part, I would probably just mention that libbpf internally attaches the bpf programs with direct action mode. The hw offload may be future todo, and the other bits are little used anyway; mentioning them here, what value does it have to libbpf users? I'd rather just drop the 2nd part and/or simplify this paragraph just stating that the progs are attached in direct action mode.

The idea was that this would be for the benefit of people familiar with TC concepts. Maybe simplify it to "Programs are attached in direct action mode with a protocol value of 'all', and all other parameters that the 'tc' binary supports will be set to 0"?

Quoted 34 lines
Quoted 17 lines
tools/lib/bpf/libbpf.h +17 -0
+ */
+struct bpf_tc_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 parent;
+	__u16 priority;
+	__u32 prog_id;
+	bool replace;
+	size_t :0;
+};
+
+#define bpf_tc_opts__last_field replace
+
+struct bpf_tc_ctx;
+
+struct bpf_tc_ctx_opts {
+	size_t sz;

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 822

Quoted 10 lines
tools/lib/bpf/libbpf.h +10 -0
+};
+
+#define bpf_tc_ctx_opts__last_field sz
+
+/* Requirements */
+/*
+ * @ifindex: Must be > 0.
+ * @parent: Must be one of the enum constants < _BPF_TC_PARENT_MAX
+ * @opts: Can be NULL, currently no options are supported.
+ */

Up to Andrii, but we don't have such API doc in general inside libbpf.h, I would drop it for the time being to be consistent with the rest (same for others below).

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 823

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+LIBBPF_API struct bpf_tc_ctx *bpf_tc_ctx_init(__u32 ifindex,
Quoted 8 lines

nit: in user space s/__u32 ifindex/int ifindex/

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 825

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+					      enum bpf_tc_attach_point parent,
+					      struct bpf_tc_ctx_opts *opts);

Should we enforce opts being NULL or non-NULL here, or drop the arg from here for now altogether? (And if later versions of the functions show up this could be mapped to the right one?)

Hmm, extending later is easier if there's already an opts parameter. But it could be declared 'void *' and enforced as always 0 for now?

Quoted 37 lines
Quoted 20 lines
tools/lib/bpf/libbpf.h +20 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *	 If the qdisc was attached during ctx_init, it will be deleted if no
+ *	 filters are attached to it.
+ *	 When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *	  optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 855

Quoted 10 lines
tools/lib/bpf/libbpf.h +10 -0
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+			     struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ */
+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx,
+			     const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Quoted 5 lines

Could we add a BPF_TC_UNSPEC to enum bpf_tc_attach_point, which the user would pass to bpf_tc_ctx_init(), so that opts.direction = BPF_TC_INGRESS with subsequent bpf_tc_attach() can be called, and same opts.direction = BPF_TC_EGRESS with bpf_tc_attach() for different fd. The only thing we cared about in bpf_tc_ctx_init() resp. the ctx was that qdisc was ready.

This sounds workable - no objections from me :)

-Toke

author maintainer
#5 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

In reply to Daniel Borkmann
Quoted 15 lines · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:
[...]

Quoted 13 lines
  tools/lib/bpf/libbpf.h   |  92 ++++++++
  tools/lib/bpf/libbpf.map |   5 +
  tools/lib/bpf/netlink.c  | 478 ++++++++++++++++++++++++++++++++++++++-
  3 files changed, 574 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.h +0 -0
index bec4e6a6e31d..1c717c07b66e 100644
@@ -775,6 +775,98 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen
  LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782

Quoted 5 lines
tools/lib/bpf/libbpf.h +5 -0
+enum bpf_tc_attach_point {
+	BPF_TC_INGRESS,
+	BPF_TC_EGRESS,
+	BPF_TC_CUSTOM_PARENT,
+	_BPF_TC_PARENT_MAX,

I don't think we need to expose _BPF_TC_PARENT_MAX as part of the API, I would drop the latter.

Ok, will drop.

Quoted 12 lines
Quoted 12 lines
tools/lib/bpf/libbpf.h +12 -0
+};
+
+/* The opts structure is also used to return the created filters attributes
+ * (e.g. in case the user left them unset). Some of the options that were left
+ * out default to a reasonable value, documented below.
+ *
+ *	protocol - ETH_P_ALL
+ *	chain index - 0
+ *	class_id - 0 (can be set by bpf program using skb->tc_classid)
+ *	bpf_flags - TCA_BPF_FLAG_ACT_DIRECT (direct action mode)
+ *	bpf_flags_gen - 0
+ *
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 795

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+ *	The user must fulfill documented requirements for each function.

Not sure if this is overly relevant as part of the bpf_tc_opts in here. For the 2nd part, I would probably just mention that libbpf internally attaches the bpf programs with direct action mode. The hw offload may be future todo, and the other bits are little used anyway; mentioning them here, what value does it have to libbpf users? I'd rather just drop the 2nd part and/or simplify this paragraph just stating that the progs are attached in direct action mode.

The goal was to just document whatever attributes were set to by default, but I can see your point. I'll trim it.

Quoted 23 lines
Quoted 23 lines
tools/lib/bpf/libbpf.h +23 -0
+ */
+struct bpf_tc_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 parent;
+	__u16 priority;
+	__u32 prog_id;
+	bool replace;
+	size_t :0;
+};
+
+#define bpf_tc_opts__last_field replace
+
+struct bpf_tc_ctx;
+
+struct bpf_tc_ctx_opts {
+	size_t sz;
+};
+
+#define bpf_tc_ctx_opts__last_field sz
+
+/* Requirements */
+/*
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 822

Quoted 4 lines
tools/lib/bpf/libbpf.h +4 -0
+ * @ifindex: Must be > 0.
+ * @parent: Must be one of the enum constants < _BPF_TC_PARENT_MAX
+ * @opts: Can be NULL, currently no options are supported.
+ */

Up to Andrii, but we don't have such API doc in general inside libbpf.h, I would drop it for the time being to be consistent with the rest (same for others below).

I think we need to keep this somewhere. We dropped bpf_tc_info since it wasn't really serving any purpose, but that meant we would put the only extra thing it returned (prog_id) into bpf_tc_opts. That means we also need to take care that it is unset (along with replace) in functions where it isn't used, to allow for reuse for some future purpose. If we don't document that the user needs to unset them whenever working with bpf_tc_query and bpf_tc_detach, how are they supposed to know?

Maybe a man page and/or a blog post would be better? Just putting it above the function seemed best for now.

Quoted 4 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 823

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+LIBBPF_API struct bpf_tc_ctx *bpf_tc_ctx_init(__u32 ifindex,

nit: in user space s/__u32 ifindex/int ifindex/

Ok.

Quoted 7 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 825

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+					      enum bpf_tc_attach_point parent,
+					      struct bpf_tc_ctx_opts *opts);

Should we enforce opts being NULL or non-NULL here, or drop the arg from here for now altogether? (And if later versions of the functions show up this could be mapped to the right one?)

I can enforce it to be NULL for now, but I'd rather keep it this way than add another with opts later. The way this works, you could extend it to attach other kinds of qdiscs (by taking a simple config in opts), which I think can be very useful.

Quoted 28 lines
Quoted 28 lines
tools/lib/bpf/libbpf.h +28 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *	 If the qdisc was attached during ctx_init, it will be deleted if no
+ *	 filters are attached to it.
+ *	 When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *	  optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+			     struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ */
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 855

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx,
+			     const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Currently I check that opts->parent is unset when BPF_TC_INGRESS or BPF_TC_EGRESS is set as attach point. But since both map to clsact, we could allow the user to specify opts->parent as BPF_TC_INGRESS or BPF_TC_EGRESS (no need to use TC_H_MAKE, we can detect it from ctx->parent that it won't be a parent id). This would mean that by default attach point is what you set for ctx, but for bpf_tc_attach you can temporarily override to be some other attach point (for the same qdisc). You still won't be able to set anything other than the two though.

Quoted 15 lines

Could we add a BPF_TC_UNSPEC to enum bpf_tc_attach_point, which the user would pass to bpf_tc_ctx_init(), so that opts.direction = BPF_TC_INGRESS with subsequent bpf_tc_attach() can be called, and same opts.direction = BPF_TC_EGRESS with bpf_tc_attach() for different fd. The only thing we cared about in bpf_tc_ctx_init() resp. the ctx was that qdisc was ready.

Quoted 9 lines
tools/lib/bpf/libbpf.h +9 -0
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
Quoted 8 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 872

Quoted 8 lines
tools/lib/bpf/libbpf.h +5 -0
+ *	prog_id
+ */
+LIBBPF_API int bpf_tc_query(struct bpf_tc_ctx *ctx,
+			    struct bpf_tc_opts *opts);
+
  #ifdef __cplusplus
  } /* extern "C" */
  #endif

--
Kartikeya

#6 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 94 lines · Kumar Kartikeya Dwivedi
Quoted 15 lines · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:
[...]

Quoted 13 lines
  tools/lib/bpf/libbpf.h   |  92 ++++++++
  tools/lib/bpf/libbpf.map |   5 +
  tools/lib/bpf/netlink.c  | 478 ++++++++++++++++++++++++++++++++++++++-
  3 files changed, 574 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.h +0 -0
index bec4e6a6e31d..1c717c07b66e 100644
@@ -775,6 +775,98 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen
  LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782

Quoted 5 lines
tools/lib/bpf/libbpf.h +5 -0
+enum bpf_tc_attach_point {
+	BPF_TC_INGRESS,
+	BPF_TC_EGRESS,
+	BPF_TC_CUSTOM_PARENT,
+	_BPF_TC_PARENT_MAX,

I don't think we need to expose _BPF_TC_PARENT_MAX as part of the API, I would drop the latter.

Ok, will drop.

Quoted 12 lines
Quoted 12 lines
tools/lib/bpf/libbpf.h +12 -0
+};
+
+/* The opts structure is also used to return the created filters attributes
+ * (e.g. in case the user left them unset). Some of the options that were left
+ * out default to a reasonable value, documented below.
+ *
+ *	protocol - ETH_P_ALL
+ *	chain index - 0
+ *	class_id - 0 (can be set by bpf program using skb->tc_classid)
+ *	bpf_flags - TCA_BPF_FLAG_ACT_DIRECT (direct action mode)
+ *	bpf_flags_gen - 0
+ *
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 795

Quoted 1 line
tools/lib/bpf/libbpf.h +1 -0
+ *	The user must fulfill documented requirements for each function.

Not sure if this is overly relevant as part of the bpf_tc_opts in here. For the 2nd part, I would probably just mention that libbpf internally attaches the bpf programs with direct action mode. The hw offload may be future todo, and the other bits are little used anyway; mentioning them here, what value does it have to libbpf users? I'd rather just drop the 2nd part and/or simplify this paragraph just stating that the progs are attached in direct action mode.

The goal was to just document whatever attributes were set to by default, but I can see your point. I'll trim it.

Quoted 23 lines
Quoted 23 lines
tools/lib/bpf/libbpf.h +23 -0
+ */
+struct bpf_tc_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 parent;
+	__u16 priority;
+	__u32 prog_id;
+	bool replace;
+	size_t :0;
+};
+
+#define bpf_tc_opts__last_field replace
+
+struct bpf_tc_ctx;
+
+struct bpf_tc_ctx_opts {
+	size_t sz;
+};
+
+#define bpf_tc_ctx_opts__last_field sz
+
+/* Requirements */
+/*
Quoted 9 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 822

Quoted 4 lines
tools/lib/bpf/libbpf.h +4 -0
+ * @ifindex: Must be > 0.
+ * @parent: Must be one of the enum constants < _BPF_TC_PARENT_MAX
+ * @opts: Can be NULL, currently no options are supported.
+ */

Up to Andrii, but we don't have such API doc in general inside libbpf.h, I would drop it for the time being to be consistent with the rest (same for others below).

I think we need to keep this somewhere. We dropped bpf_tc_info since it wasn't really serving any purpose, but that meant we would put the only extra thing it returned (prog_id) into bpf_tc_opts. That means we also need to take care that it is unset (along with replace) in functions where it isn't used, to allow for reuse for some future purpose. If we don't document that the user needs to unset them whenever working with bpf_tc_query and bpf_tc_detach, how are they supposed to know?

Quoted 2 lines

Maybe a man page and/or a blog post would be better? Just putting it above the function seemed best for now.

You could document it together with the struct definition instead of for each function? See the inline comments in bpf_object_open_opts for instance...

-Toke

#15 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

In reply to Daniel Borkmann
Quoted 69 lines · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:
[...]

Quoted 10 lines
  tools/lib/bpf/libbpf.h   |  92 ++++++++
  tools/lib/bpf/libbpf.map |   5 +
  tools/lib/bpf/netlink.c  | 478 ++++++++++++++++++++++++++++++++++++++-
  3 files changed, 574 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.h +0 -0
index bec4e6a6e31d..1c717c07b66e 100644

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782

Quoted 8 lines
tools/lib/bpf/libbpf.h +5 -0
@@ -775,6 +775,98 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen
  LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker);
  LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
 
+enum bpf_tc_attach_point {
+     BPF_TC_INGRESS,
+     BPF_TC_EGRESS,
+     BPF_TC_CUSTOM_PARENT,
+     _BPF_TC_PARENT_MAX,

I don't think we need to expose _BPF_TC_PARENT_MAX as part of the API, I would drop the latter.

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 795

Quoted 13 lines
tools/lib/bpf/libbpf.h +13 -0
+};
+
+/* The opts structure is also used to return the created filters attributes
+ * (e.g. in case the user left them unset). Some of the options that were left
+ * out default to a reasonable value, documented below.
+ *
+ *   protocol - ETH_P_ALL
+ *   chain index - 0
+ *   class_id - 0 (can be set by bpf program using skb->tc_classid)
+ *   bpf_flags - TCA_BPF_FLAG_ACT_DIRECT (direct action mode)
+ *   bpf_flags_gen - 0
+ *
+ *   The user must fulfill documented requirements for each function.

Not sure if this is overly relevant as part of the bpf_tc_opts in here. For the 2nd part, I would probably just mention that libbpf internally attaches the bpf programs with direct action mode. The hw offload may be future todo, and the other bits are little used anyway; mentioning them here, what value does it have to libbpf users? I'd rather just drop the 2nd part and/or simplify this paragraph just stating that the progs are attached in direct action mode.

Quoted 23 lines
tools/lib/bpf/libbpf.h +23 -0
+ */
+struct bpf_tc_opts {
+     size_t sz;
+     __u32 handle;
+     __u32 parent;
+     __u16 priority;
+     __u32 prog_id;
+     bool replace;
+     size_t :0;
+};
+
+#define bpf_tc_opts__last_field replace
+
+struct bpf_tc_ctx;
+
+struct bpf_tc_ctx_opts {
+     size_t sz;
+};
+
+#define bpf_tc_ctx_opts__last_field sz
+
+/* Requirements */
+/*
Quoted 8 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 822

Quoted 4 lines
tools/lib/bpf/libbpf.h +4 -0
+ * @ifindex: Must be > 0.
+ * @parent: Must be one of the enum constants < _BPF_TC_PARENT_MAX
+ * @opts: Can be NULL, currently no options are supported.
+ */

Up to Andrii, but we don't have such API doc in general inside libbpf.h, I would drop it for the time being to be consistent with the rest (same for others below).

+1

Quoted 12 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 823

Quoted 2 lines
tools/lib/bpf/libbpf.h +1 -0
+LIBBPF_API struct bpf_tc_ctx *bpf_tc_ctx_init(__u32 ifindex,

nit: in user space s/__u32 ifindex/int ifindex/

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 825

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+                                           enum bpf_tc_attach_point parent,
+                                           struct bpf_tc_ctx_opts *opts);

Should we enforce opts being NULL or non-NULL here, or drop the arg from here for now altogether? (And if later versions of the functions show up this could be mapped to the right one?)

OPTS_VALID check handles all the cases. Opts are always allowed to be NULL. If it's not null, all the bytes beyond what current libbpf version supports should be zero. All that is handled by OPTS_VALID, so I don't think anything extra needs to be checked.

Quoted 60 lines
Quoted 20 lines
tools/lib/bpf/libbpf.h +20 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *    If the qdisc was attached during ctx_init, it will be deleted if no
+ *    filters are attached to it.
+ *    When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *     optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *   handle
+ *   parent
+ *   priority
+ *   prog_id

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 855

Quoted 10 lines
tools/lib/bpf/libbpf.h +10 -0
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+                          struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *     must be set.
+ */
+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx,
+                          const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Could we add a BPF_TC_UNSPEC to enum bpf_tc_attach_point, which the user would pass to bpf_tc_ctx_init(), so that opts.direction = BPF_TC_INGRESS with subsequent bpf_tc_attach() can be called, and same opts.direction = BPF_TC_EGRESS with bpf_tc_attach() for different fd. The only thing we cared about in bpf_tc_ctx_init() resp. the ctx was that qdisc was ready.

Quoted 17 lines
tools/lib/bpf/libbpf.h +14 -0
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *     must be set.
+ *
+ * Fills the following fields in opts:
+ *   handle
+ *   parent
+ *   priority
+ *   prog_id
+ */
+LIBBPF_API int bpf_tc_query(struct bpf_tc_ctx *ctx,
+                         struct bpf_tc_opts *opts);
+
  #ifdef __cplusplus
  } /* extern "C" */
  #endif
#17 lore raw

Re: [PATCH bpf-next v4 3/3] libbpf: add selftests for TC-BPF API

In reply to Kumar Kartikeya Dwivedi
Quoted 12 lines · Kumar Kartikeya Dwivedi

This adds some basic tests for the low level bpf_tc_* API.

---
 .../testing/selftests/bpf/prog_tests/tc_bpf.c | 204 ++++++++++++++++++
 .../testing/selftests/bpf/progs/test_tc_bpf.c |  12 ++
 2 files changed, 216 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf.c

[...]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf.c new file +5 -0
+
+void test_tc_bpf(void)
+{
+       struct bpf_tc_ctx *ctx_ing = NULL, *ctx_eg = NULL;
+       struct test_tc_bpf *skel = NULL;

tools/testing/selftests/bpf/prog_tests/tc_bpf.c line 181

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf.c new file +10 -0
+       int cls_fd, ret;
+
+       skel = test_tc_bpf__open_and_load();
+       if (!ASSERT_NEQ(skel, NULL, "test_tc_bpf skeleton"))
+               goto end;
+
+       cls_fd = bpf_program__fd(skel->progs.cls);
+
+       ctx_ing = bpf_tc_ctx_init(LO_IFINDEX, BPF_TC_INGRESS, NULL);
+       if (!ASSERT_NEQ(ctx_ing, NULL, "bpf_tc_ctx_init(BPF_TC_INGRESS)"))

please use ASSERT_OK_PTR() and ASSERT_ERR_PTR() for pointer checks. They handle both NULL/non-NULL cases and ERR_PTR() errors.

Quoted 44 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf.c new file +23 -0
+               goto end;
+
+       ctx_eg = bpf_tc_ctx_init(LO_IFINDEX, BPF_TC_EGRESS, NULL);
+       if (!ASSERT_NEQ(ctx_eg, NULL, "bpf_tc_ctx_init(BPF_TC_EGRESS)"))
+               goto end;
+
+       ret = test_tc_internal(ctx_ing, cls_fd, BPF_TC_INGRESS);
+       if (!ASSERT_EQ(ret, 0, "test_tc_internal ingress"))
+               goto end;
+
+       ret = test_tc_internal(ctx_eg, cls_fd, BPF_TC_EGRESS);
+       if (!ASSERT_EQ(ret, 0, "test_tc_internal egress"))
+               goto end;
+
+       ret = test_tc_invalid(ctx_ing, cls_fd);
+       if (!ASSERT_EQ(ret, 0, "test_tc_invalid"))
+               goto end;
+
+end:
+       bpf_tc_ctx_destroy(ctx_eg);
+       bpf_tc_ctx_destroy(ctx_ing);
+       test_tc_bpf__destroy(skel);
+}
tools/testing/selftests/bpf/progs/test_tc_bpf.c new file +12 -1
new file mode 100644
index 000000000000..18a3a7ed924a
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* Dummy prog to test TC-BPF API */
+
+SEC("classifier")
+int cls(struct __sk_buff *skb)
+{
+       return 0;
+}
--

2.30.2

maintainer
#7 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

In reply to Kumar Kartikeya Dwivedi
Quoted 2 lines · Kumar Kartikeya Dwivedi
Quoted 1 line · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:

[...]

Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
tools/lib/bpf/libbpf.h +28 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *	 If the qdisc was attached during ctx_init, it will be deleted if no
+ *	 filters are attached to it.
+ *	 When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *	  optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *	handle
+ *	parent
+ *	priority
+ *	prog_id
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+			     struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *	  must be set.
+ */
Quoted 8 lines

tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 855

Quoted 2 lines
tools/lib/bpf/libbpf.h +2 -0
+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx,
+			     const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Quoted 8 lines

Currently I check that opts->parent is unset when BPF_TC_INGRESS or BPF_TC_EGRESS is set as attach point. But since both map to clsact, we could allow the user to specify opts->parent as BPF_TC_INGRESS or BPF_TC_EGRESS (no need to use TC_H_MAKE, we can detect it from ctx->parent that it won't be a parent id). This would mean that by default attach point is what you set for ctx, but for bpf_tc_attach you can temporarily override to be some other attach point (for the same qdisc). You still won't be able to set anything other than the two though.

I think the assumption on auto-detecting the parent id in that case might not hold given major number could very well be 0. Wrt BPF_TC_UNSPEC ... maybe it's not even needed, back to drawing board ...

Here's how the whole API could look like, usage examples below:

  enum bpf_tc_attach_point {
	BPF_TC_INGRESS = 1 << 0,
	BPF_TC_EGRESS  = 1 << 1,
	BPF_TC_CUSTOM  = 1 << 2,

};

  enum bpf_tc_attach_flags {
	BPF_TC_F_REPLACE = 1 << 0,

};

  struct bpf_tc_hook {
	size_t sz;
	int    ifindex;
	enum bpf_tc_attach_point which;
	__u32  parent;
	size_t :0;

};

  struct bpf_tc_opts {
	size_t sz;
	__u32  handle;
	__u16  priority;
	union {
		int   prog_fd;
		__u32 prog_id;
	};
	size_t :0;

};

  LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook);
  LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);

  LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts, int flags);
  LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts);
  LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts);

So a user could do just:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS);
  DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);

  err = bpf_tc_attach(&hook, &opts, BPF_TC_F_REPLACE);
  [...]

If it's not known whether the hook exists, then a preceding call to:

  err = bpf_tc_hook_create(&hook);
  [...]

The bpf_tc_query() would look like:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_EGRESS);
  DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

  err = bpf_tc_query(&hook, &opts);
  if (!err) {
         [...]  // gives access to: opts.prog_id
  }

The bpf_tc_detach():

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS);
  DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

  err = bpf_tc_detach(&hook, &opts);
  [...]

The nice thing would be that hook and opts are kept semantically separate, meaning with hook you can iterate though a bunch of devs and ingress/egress locations without changing opts, whereas with opts you could iterate on the cls_bpf instance itself w/o changing hook. Both are kept extensible via DECLARE_LIBBPF_OPTS().

Now the bpf_tc_hook_destroy() one:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

  err = bpf_tc_hook_destroy(&hook, &opts);
  [...]

For triggering a remove of the clsact qdisc on the device, both directions are passed in. Combining both is only ever allowed for bpf_tc_hook_destroy().

If /only/ BPF_TC_INGRESS or only BPF_TC_EGRESS is passed, it could flush their lists (aka equivalent of tc filter del dev eth0 ingress and tc filter del dev eth0 egress command).

For bpf_tc_hook_{create,destroy}() with BPF_TC_CUSTOM, we just return -EINVAL or -EOPNOTSUPP.

I think the above interface would work nicely and feels intuitive while being extensible. Thoughts?

Thanks,
Daniel

maintainer
#8 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 130 lines · Daniel Borkmann
Quoted 2 lines · Kumar Kartikeya Dwivedi
Quoted 1 line · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:

[...]

Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
tools/lib/bpf/libbpf.h +28 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *     If the qdisc was attached during ctx_init, it will be deleted if no
+ *     filters are attached to it.
+ *     When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *      optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *    handle
+ *    parent
+ *    priority
+ *    prog_id
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+                 struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *      must be set.
+ */
Quoted 8 lines
Quoted 2 lines

+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx, +                 const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Quoted 8 lines

Currently I check that opts->parent is unset when BPF_TC_INGRESS or BPF_TC_EGRESS is set as attach point. But since both map to clsact, we could allow the user to specify opts->parent as BPF_TC_INGRESS or BPF_TC_EGRESS (no need to use TC_H_MAKE, we can detect it from ctx->parent that it won't be a parent id). This would mean that by default attach point is what you set for ctx, but for bpf_tc_attach you can temporarily override to be some other attach point (for the same qdisc). You still won't be able to set anything other than the two though.

I think the assumption on auto-detecting the parent id in that case might not hold given major number could very well be 0. Wrt BPF_TC_UNSPEC ... maybe it's not even needed, back to drawing board ...

Here's how the whole API could look like, usage examples below:

  enum bpf_tc_attach_point {
    BPF_TC_INGRESS = 1 << 0,
    BPF_TC_EGRESS  = 1 << 1,
    BPF_TC_CUSTOM  = 1 << 2,
  };

  enum bpf_tc_attach_flags {
    BPF_TC_F_REPLACE = 1 << 0,
  };

  struct bpf_tc_hook {
    size_t sz;
    int    ifindex;
    enum bpf_tc_attach_point which;
    __u32  parent;
    size_t :0;
  };

  struct bpf_tc_opts {
    size_t sz;
    __u32  handle;
    __u16  priority;
    union {
        int   prog_fd;
        __u32 prog_id;
    };
    size_t :0;
  };

  LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);

  LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts, int flags); LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts); LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts);

So a user could do just:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);

  err = bpf_tc_attach(&hook, &opts, BPF_TC_F_REPLACE); [...]

If it's not known whether the hook exists, then a preceding call to:

  err = bpf_tc_hook_create(&hook);
  [...]

The bpf_tc_query() would look like:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_EGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

  err = bpf_tc_query(&hook, &opts);
  if (!err) {
         [...]  // gives access to: opts.prog_id
  }

The bpf_tc_detach():

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

  err = bpf_tc_detach(&hook, &opts);
  [...]

The nice thing would be that hook and opts are kept semantically separate, meaning with hook you can iterate though a bunch of devs and ingress/egress locations without changing opts, whereas with opts you could iterate on the cls_bpf instance itself w/o changing hook. Both are kept extensible via DECLARE_LIBBPF_OPTS().

Now the bpf_tc_hook_destroy() one:

Quoted 7 lines

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

  err = bpf_tc_hook_destroy(&hook);
  [...]

For triggering a remove of the clsact qdisc on the device, both directions are passed in. Combining both is only ever allowed for bpf_tc_hook_destroy().

Small addendum:

    DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

    err = bpf_tc_hook_create(&hook);
    [...]

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

Quoted 10 lines

If /only/ BPF_TC_INGRESS or only BPF_TC_EGRESS is passed, it could flush their lists (aka equivalent of tc filter del dev eth0 ingress and tc filter del dev eth0 egress command).

For bpf_tc_hook_{create,destroy}() with BPF_TC_CUSTOM, we just return -EINVAL or -EOPNOTSUPP.

I think the above interface would work nicely and feels intuitive while being extensible. Thoughts?

Thanks,
Daniel

maintainer
#9 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 51 lines · Daniel Borkmann
Quoted 50 lines · Daniel Borkmann
Quoted 2 lines · Kumar Kartikeya Dwivedi
Quoted 1 line · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:

[...]

Quoted 45 lines
Quoted 28 lines
Quoted 28 lines
tools/lib/bpf/libbpf.h +28 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *     If the qdisc was attached during ctx_init, it will be deleted if no
+ *     filters are attached to it.
+ *     When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *      optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *    handle
+ *    parent
+ *    priority
+ *    prog_id
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+                 struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *      must be set.
+ */
Quoted 8 lines
Quoted 2 lines

+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx, +                 const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Currently I check that opts->parent is unset when BPF_TC_INGRESS or BPF_TC_EGRESS is set as attach point. But since both map to clsact, we could allow the user to specify opts->parent as BPF_TC_INGRESS or BPF_TC_EGRESS (no need to use TC_H_MAKE, we can detect it from ctx->parent that it won't be a parent id). This would mean that by default attach point is what you set for ctx, but for bpf_tc_attach you can temporarily override to be some other attach point (for the same qdisc). You still won't be able to set anything other than the two though.

Quoted 5 lines
Quoted 5 lines

I think the assumption on auto-detecting the parent id in that case might not hold given major number could very well be 0. Wrt BPF_TC_UNSPEC ... maybe it's not even needed, back to drawing board ...

Here's how the whole API could look like, usage examples below:

And one last follow-up thought:

Quoted 18 lines
Quoted 18 lines

   enum bpf_tc_attach_point {
     BPF_TC_INGRESS = 1 << 0,
     BPF_TC_EGRESS  = 1 << 1,
     BPF_TC_CUSTOM  = 1 << 2,
   };

   enum bpf_tc_attach_flags {
     BPF_TC_F_REPLACE = 1 << 0,
   };

   struct bpf_tc_hook {
     size_t sz;
     int    ifindex;
     enum bpf_tc_attach_point which;
     __u32  parent;
     size_t :0;
   };

Quoted 4 lines
Quoted 4 lines

   struct bpf_tc_opts {
     size_t sz;
     __u32  handle;
     __u16  priority;

To avoid gaps, priority could be __u32 as well, but we need to enforce [0, u16max].

Quoted 4 lines
Quoted 4 lines

     union {
         int   prog_fd;
         __u32 prog_id;
     };

We should rather remove the union to make it less error-prone.

Quoted 7 lines
Quoted 7 lines

     size_t :0;
   };

   LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);

   LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts, int flags);

Changing s/const struct bpf_tc_opts *opts/struct bpf_tc_opts *opts/ so that if a user did:

    DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS);
    DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .prog_fd = fd);

    err = bpf_tc_attach(&hook, &opts, 0);
    [...]

Then we'd rely on the kernel (cls_bpf) to auto-allocate handle/prio. libbpf in that case would populate opts.handle and opts.priority upon success, which can then later be used again for bpf_tc_detach() / bpf_tc_query() calls.

Quoted 68 lines
Quoted 41 lines

   LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts); LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts);

So a user could do just:

   DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);

   err = bpf_tc_attach(&hook, &opts, BPF_TC_F_REPLACE); [...]

If it's not known whether the hook exists, then a preceding call to:

   err = bpf_tc_hook_create(&hook);
   [...]

The bpf_tc_query() would look like:

   DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_EGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

   err = bpf_tc_query(&hook, &opts);
   if (!err) {
          [...]  // gives access to: opts.prog_id
   }

The bpf_tc_detach():

   DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

   err = bpf_tc_detach(&hook, &opts);
   [...]

The nice thing would be that hook and opts are kept semantically separate, meaning with hook you can iterate though a bunch of devs and ingress/egress locations without changing opts, whereas with opts you could iterate on the cls_bpf instance itself w/o changing hook. Both are kept extensible via DECLARE_LIBBPF_OPTS().

Now the bpf_tc_hook_destroy() one:

Quoted 7 lines

   DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

   err = bpf_tc_hook_destroy(&hook);
   [...]

For triggering a remove of the clsact qdisc on the device, both directions are passed in. Combining both is only ever allowed for bpf_tc_hook_destroy().

Small addendum:

    DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

    err = bpf_tc_hook_create(&hook);
    [...]

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

Quoted 10 lines

If /only/ BPF_TC_INGRESS or only BPF_TC_EGRESS is passed, it could flush their lists (aka equivalent of tc filter del dev eth0 ingress and tc filter del dev eth0 egress command).

For bpf_tc_hook_{create,destroy}() with BPF_TC_CUSTOM, we just return -EINVAL or -EOPNOTSUPP.

I think the above interface would work nicely and feels intuitive while being extensible. Thoughts?

Thanks,
Daniel

#10 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

In reply to Daniel Borkmann
Quoted 139 lines · Daniel Borkmann
Quoted 130 lines · Daniel Borkmann
Quoted 2 lines · Kumar Kartikeya Dwivedi
Quoted 1 line · Daniel Borkmann

On 4/23/21 5:05 PM, Kumar Kartikeya Dwivedi wrote:

[...]

Quoted 37 lines
Quoted 28 lines
Quoted 28 lines
tools/lib/bpf/libbpf.h +28 -0
+/*
+ * @ctx: Can be NULL, if not, must point to a valid object.
+ *     If the qdisc was attached during ctx_init, it will be deleted if no
+ *     filters are attached to it.
+ *     When ctx == NULL, this is a no-op.
+ */
+LIBBPF_API int bpf_tc_ctx_destroy(struct bpf_tc_ctx *ctx);
+/*
+ * @ctx: Cannot be NULL.
+ * @fd: Must be >= 0.
+ * @opts: Cannot be NULL, prog_id must be unset, all other fields can be
+ *      optionally set. All fields except replace  will be set as per created
+ *        filter's attributes. parent must only be set when attach_point of ctx is
+ *        BPF_TC_CUSTOM_PARENT, otherwise parent must be unset.
+ *
+ * Fills the following fields in opts:
+ *    handle
+ *    parent
+ *    priority
+ *    prog_id
+ */
+LIBBPF_API int bpf_tc_attach(struct bpf_tc_ctx *ctx, int fd,
+                 struct bpf_tc_opts *opts);
+/*
+ * @ctx: Cannot be NULL.
+ * @opts: Cannot be NULL, replace and prog_id must be unset, all other fields
+ *      must be set.
+ */
Quoted 8 lines
Quoted 2 lines

+LIBBPF_API int bpf_tc_detach(struct bpf_tc_ctx *ctx, +                 const struct bpf_tc_opts *opts);

One thing that I find a bit odd from this API is that BPF_TC_INGRESS / BPF_TC_EGRESS needs to be set each time via bpf_tc_ctx_init(). So whenever a specific program would be attached to both we need to 're-init' in between just to change from hook a to b, whereas when you have BPF_TC_CUSTOM_PARENT, you could just use a different opts->parent without going this detour (unless the clsact wasn't loaded there in the first place).

Quoted 8 lines

Currently I check that opts->parent is unset when BPF_TC_INGRESS or BPF_TC_EGRESS is set as attach point. But since both map to clsact, we could allow the user to specify opts->parent as BPF_TC_INGRESS or BPF_TC_EGRESS (no need to use TC_H_MAKE, we can detect it from ctx->parent that it won't be a parent id). This would mean that by default attach point is what you set for ctx, but for bpf_tc_attach you can temporarily override to be some other attach point (for the same qdisc). You still won't be able to set anything other than the two though.

I think the assumption on auto-detecting the parent id in that case might not hold given major number could very well be 0. Wrt BPF_TC_UNSPEC ... maybe it's not even needed, back to drawing board ...

Here's how the whole API could look like, usage examples below:

  enum bpf_tc_attach_point {
    BPF_TC_INGRESS = 1 << 0,
    BPF_TC_EGRESS  = 1 << 1,
    BPF_TC_CUSTOM  = 1 << 2,
  };

  enum bpf_tc_attach_flags {
    BPF_TC_F_REPLACE = 1 << 0,
  };

  struct bpf_tc_hook {
    size_t sz;
    int    ifindex;
    enum bpf_tc_attach_point which;
    __u32  parent;
    size_t :0;
  };

  struct bpf_tc_opts {
    size_t sz;
    __u32  handle;
    __u16  priority;
    union {
        int   prog_fd;
        __u32 prog_id;
    };
    size_t :0;
  };

  LIBBPF_API int bpf_tc_hook_create(struct bpf_tc_hook *hook); LIBBPF_API int bpf_tc_hook_destroy(struct bpf_tc_hook *hook);

  LIBBPF_API int bpf_tc_attach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts, int flags); LIBBPF_API int bpf_tc_detach(const struct bpf_tc_hook *hook, const struct bpf_tc_opts *opts); LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts);

So a user could do just:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);

  err = bpf_tc_attach(&hook, &opts, BPF_TC_F_REPLACE); [...]

If it's not known whether the hook exists, then a preceding call to:

  err = bpf_tc_hook_create(&hook);
  [...]

The bpf_tc_query() would look like:

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_EGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

  err = bpf_tc_query(&hook, &opts);
  if (!err) {
         [...]  // gives access to: opts.prog_id
  }

The bpf_tc_detach():

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS); DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1);

  err = bpf_tc_detach(&hook, &opts);
  [...]

The nice thing would be that hook and opts are kept semantically separate, meaning with hook you can iterate though a bunch of devs and ingress/egress locations without changing opts, whereas with opts you could iterate on the cls_bpf instance itself w/o changing hook. Both are kept extensible via DECLARE_LIBBPF_OPTS().

Now the bpf_tc_hook_destroy() one:

Quoted 7 lines

  DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

  err = bpf_tc_hook_destroy(&hook);
  [...]

For triggering a remove of the clsact qdisc on the device, both directions are passed in. Combining both is only ever allowed for bpf_tc_hook_destroy().

Quoted 8 lines

Small addendum:

     DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

     err = bpf_tc_hook_create(&hook);
     [...]

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

It should be allowed, but it wouldn't actually make any difference which combination of TC_INGRESS and TC_EGRESS you specify, as long as one of them is set, right? I.e., we just attach the clsact qdisc in both cases...

-Toke

maintainer
#11 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 1 line · Toke Høiland-Jørgensen

Daniel Borkmann <[email protected]> writes:

[...]

Quoted 7 lines
Quoted 7 lines

Small addendum:

      DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

      err = bpf_tc_hook_create(&hook);
      [...]
Quoted 6 lines
Quoted 1 line

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

It should be allowed, but it wouldn't actually make any difference which combination of TC_INGRESS and TC_EGRESS you specify, as long as one of them is set, right? I.e., we just attach the clsact qdisc in both cases...

Yes, that is correct, for the bpf_tc_hook_create() whether you pass in BPF_TC_INGRESS, BPF_TC_EGRESS or BPF_TC_INGRESS|BPF_TC_EGRESS, you'll end up creating clsact qdisc in either of the three cases. Only the bpf_tc_hook_destroy() differs between all of them.

Thanks,
Daniel

#12 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 17 lines · Daniel Borkmann
Quoted 1 line · Toke Høiland-Jørgensen

Daniel Borkmann <[email protected]> writes:

[...]

Quoted 7 lines
Quoted 7 lines

Small addendum:

      DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

      err = bpf_tc_hook_create(&hook);
      [...]
Quoted 6 lines
Quoted 1 line

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

It should be allowed, but it wouldn't actually make any difference which combination of TC_INGRESS and TC_EGRESS you specify, as long as one of them is set, right? I.e., we just attach the clsact qdisc in both cases...

Quoted 4 lines

Yes, that is correct, for the bpf_tc_hook_create() whether you pass in BPF_TC_INGRESS, BPF_TC_EGRESS or BPF_TC_INGRESS|BPF_TC_EGRESS, you'll end up creating clsact qdisc in either of the three cases. Only the bpf_tc_hook_destroy() differs between all of them.

Right, just checking. Other than that, I like your proposal; it loses the "automatic removal of qdisc if we added it" feature, but that's probably OK: less magic is good. And as long as bpf_tc_hook_create() returns EEXIST if the qdisc already exists, the caller can do the same thing if they want.

-Toke

maintainer
#13 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 23 lines · Toke Høiland-Jørgensen
Quoted 17 lines · Daniel Borkmann
Quoted 1 line · Toke Høiland-Jørgensen

Daniel Borkmann <[email protected]> writes:

[...]

Quoted 7 lines
Quoted 7 lines

Small addendum:

       DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

       err = bpf_tc_hook_create(&hook);
       [...]
Quoted 6 lines
Quoted 1 line

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

It should be allowed, but it wouldn't actually make any difference which combination of TC_INGRESS and TC_EGRESS you specify, as long as one of them is set, right? I.e., we just attach the clsact qdisc in both cases...

Quoted 4 lines

Yes, that is correct, for the bpf_tc_hook_create() whether you pass in BPF_TC_INGRESS, BPF_TC_EGRESS or BPF_TC_INGRESS|BPF_TC_EGRESS, you'll end up creating clsact qdisc in either of the three cases. Only the bpf_tc_hook_destroy() differs between all of them.

Quoted 5 lines

Right, just checking. Other than that, I like your proposal; it loses the "automatic removal of qdisc if we added it" feature, but that's probably OK: less magic is good. And as long as bpf_tc_hook_create() returns EEXIST if the qdisc already exists, the caller can do the same thing if they want.

Yes exactly. Less magic the better, especially given this has global effect.

Thanks,
Daniel

author maintainer
#14 lore raw

Re: [PATCH bpf-next v4 2/3] libbpf: add low level TC-BPF API

Quoted 24 lines · Daniel Borkmann
Quoted 23 lines · Toke Høiland-Jørgensen
Quoted 21 lines · Daniel Borkmann
Quoted 1 line · Toke Høiland-Jørgensen

Daniel Borkmann <[email protected]> writes:

[...]

Quoted 7 lines
Quoted 7 lines

Small addendum:

       DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = 42, .which = BPF_TC_INGRESS|BPF_TC_EGRESS);

       err = bpf_tc_hook_create(&hook);
       [...]
Quoted 6 lines
Quoted 1 line

... is also possible, of course, and then both bpf_tc_hook_{create,destroy}() are symmetric.

It should be allowed, but it wouldn't actually make any difference which combination of TC_INGRESS and TC_EGRESS you specify, as long as one of them is set, right? I.e., we just attach the clsact qdisc in both cases...

Yes, that is correct, for the bpf_tc_hook_create() whether you pass in BPF_TC_INGRESS, BPF_TC_EGRESS or BPF_TC_INGRESS|BPF_TC_EGRESS, you'll end up creating clsact qdisc in either of the three cases. Only the bpf_tc_hook_destroy() differs between all of them.

Quoted 8 lines
Quoted 5 lines

Right, just checking. Other than that, I like your proposal; it loses the "automatic removal of qdisc if we added it" feature, but that's probably OK: less magic is good. And as long as bpf_tc_hook_create() returns EEXIST if the qdisc already exists, the caller can do the same thing if they want.

Yes exactly. Less magic the better, especially given this has global effect.

Everything sounds good. I'll do a resend.

Quoted 2 lines

Thanks,
Daniel

--
Kartikeya