Skip to contentSkip to conversation

Add bpf_link based TC-BPF API

PATCH In review Kumar Kartikeya Dwivedi 8 participants 36 messages Jun 4, 2021 – Jun 11, 2022

Subsystems (9)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

v2 cover Add bpf_link based TC-BPF API

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

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

* Avoid overwriting other members of union in bpf_attr (Andrii)
* Set link to NULL after bpf_link_cleanup to avoid double free (Andrii)
* Use __be16 to store the result of htons (Kernel Test Robot)
* Make assignment of tcf_exts::net conditional on CONFIG_NET_CLS_ACT (Kernel Test Robot)

Kumar Kartikeya Dwivedi (7):
net: sched: refactor cls_bpf creation code
bpf: export bpf_link functions for modules
net: sched: add bpf_link API for bpf classifier
net: sched: add lightweight update path for cls_bpf
tools: bpf.h: sync with kernel sources
libbpf: add bpf_link based TC-BPF management API
libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 139 ++++++-
 net/sched/cls_bpf.c                           | 389 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   8 +-
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 940 insertions(+), 45 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v2 1/7 net: sched: refactor cls_bpf creation code

Move parts of the code that are independent and need to be reused into their own helpers. These will be needed for adding a bpf_link creation path in a subsequent patch.

---
 net/sched/cls_bpf.c | 84 ++++++++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 31 deletions(-)
net/sched/cls_bpf.c +53 -31
index 6e3e63db0e01..360b97ab8646 100644
@@ -455,6 +455,57 @@ static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp,
 	return 0;
 }
 
+static int __cls_bpf_alloc_idr(struct cls_bpf_head *head, u32 handle,
+			       struct cls_bpf_prog *prog,
+			       struct cls_bpf_prog *oldprog)
+{
+	int ret = 0;
+
+	if (oldprog) {
+		if (handle && oldprog->handle != handle)
+			return -EINVAL;
+	}
+
+	if (handle == 0) {
+		handle = 1;
+		ret = idr_alloc_u32(&head->handle_idr, prog, &handle, INT_MAX,
+				    GFP_KERNEL);
+	} else if (!oldprog) {
+		ret = idr_alloc_u32(&head->handle_idr, prog, &handle, handle,
+				    GFP_KERNEL);
+	}
+
+	prog->handle = handle;
+	return ret;
+}
+
+static int __cls_bpf_change(struct cls_bpf_head *head, struct tcf_proto *tp,
+			    struct cls_bpf_prog *prog,
+			    struct cls_bpf_prog *oldprog,
+			    struct netlink_ext_ack *extack)
+{
+	int ret;
+
+	ret = cls_bpf_offload(tp, prog, oldprog, extack);
+	if (ret)
+		return ret;
+
+	if (!tc_in_hw(prog->gen_flags))
+		prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
+
+	if (oldprog) {
+		idr_replace(&head->handle_idr, prog, prog->handle);
+		list_replace_rcu(&oldprog->link, &prog->link);
+		tcf_unbind_filter(tp, &oldprog->res);
+		tcf_exts_get_net(&oldprog->exts);
+		tcf_queue_work(&oldprog->rwork, cls_bpf_delete_prog_work);
+	} else {
+		list_add_rcu(&prog->link, &head->plist);
+	}
+
+	return 0;
+}
+
 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
 			  struct tcf_proto *tp, unsigned long base,
 			  u32 handle, struct nlattr **tca,
@@ -483,48 +534,19 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
 	if (ret < 0)
 		goto errout;
 
-	if (oldprog) {
-		if (handle && oldprog->handle != handle) {
-			ret = -EINVAL;
-			goto errout;
-		}
-	}
-
-	if (handle == 0) {
-		handle = 1;
-		ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
-				    INT_MAX, GFP_KERNEL);
-	} else if (!oldprog) {
-		ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
-				    handle, GFP_KERNEL);
-	}
-
+	ret = __cls_bpf_alloc_idr(head, handle, prog, oldprog);
 	if (ret)
 		goto errout;
-	prog->handle = handle;
 
 	ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr,
 				extack);
 	if (ret < 0)
 		goto errout_idr;
 
-	ret = cls_bpf_offload(tp, prog, oldprog, extack);
+	ret = __cls_bpf_change(head, tp, prog, oldprog, extack);
 	if (ret)
 		goto errout_parms;
 
-	if (!tc_in_hw(prog->gen_flags))
-		prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
-
-	if (oldprog) {
-		idr_replace(&head->handle_idr, prog, handle);
-		list_replace_rcu(&oldprog->link, &prog->link);
-		tcf_unbind_filter(tp, &oldprog->res);
-		tcf_exts_get_net(&oldprog->exts);
-		tcf_queue_work(&oldprog->rwork, cls_bpf_delete_prog_work);
-	} else {
-		list_add_rcu(&prog->link, &head->plist);
-	}
-
 	*arg = prog;
 	return 0;
 
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH v2 2/7 bpf: export bpf_link functions for modules

In reply to Kumar Kartikeya Dwivedi

These are required in a subsequent patch to implement the bpf_link command for cls_bpf. Since the bpf_link object is tied to the cls_bpf_prog object, it has to be initialized and managed from inside the module.

---
 kernel/bpf/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/syscall.c +4 -0
index 50457019da27..e5934b748ced 100644
@@ -2338,6 +2338,7 @@ void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
 	link->ops = ops;
 	link->prog = prog;
 }
+EXPORT_SYMBOL_GPL(bpf_link_init);
 
 static void bpf_link_free_id(int id)
 {
@@ -2363,6 +2364,7 @@ void bpf_link_cleanup(struct bpf_link_primer *primer)
 	fput(primer->file);
 	put_unused_fd(primer->fd);
 }
+EXPORT_SYMBOL_GPL(bpf_link_cleanup);
 
 void bpf_link_inc(struct bpf_link *link)
 {
@@ -2510,6 +2512,7 @@ int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
 	primer->id = id;
 	return 0;
 }
+EXPORT_SYMBOL_GPL(bpf_link_prime);
 
 int bpf_link_settle(struct bpf_link_primer *primer)
 {
@@ -2522,6 +2525,7 @@ int bpf_link_settle(struct bpf_link_primer *primer)
 	/* pass through installed FD */
 	return primer->fd;
 }
+EXPORT_SYMBOL_GPL(bpf_link_settle);
 
 int bpf_link_new_fd(struct bpf_link *link)
 {
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH v2 3/7 net: sched: add bpf_link API for bpf classifier

In reply to Kumar Kartikeya Dwivedi

This commit introduces a bpf_link based kernel API for creating tc filters and using the cls_bpf classifier. Only a subset of what netlink API offers is supported, things like TCA_BPF_POLICE, TCA_RATE and embedded actions are unsupported.

The kernel API and the libbpf wrapper added in a subsequent patch are more opinionated and mirror the semantics of low level netlink based TC-BPF API, i.e. always setting direct action mode, always setting protocol to ETH_P_ALL, and only exposing handle and priority as the variables the user can control. We add an additional gen_flags parameter though to allow for offloading use cases. It would be trivial to extend the current API to support specifying other attributes in the future, but for now I'm sticking how we want to push usage.

The semantics around bpf_link support are as follows:

A user can create a classifier attached to a filter using the bpf_link API, after which changing it and deleting it only happens through the bpf_link API. It is not possible to bind the bpf_link to existing filter, and any such attempt will fail with EEXIST. Hence EEXIST can be returned in two cases, when existing bpf_link owned filter exists, or existing netlink owned filter exists.

Removing bpf_link owned filter from netlink returns EPERM, denoting that netlink is locked out from filter manipulation when bpf_link is involved.

Whenever a filter is detached due to chain removal, or qdisc tear down, or net_device shutdown, the bpf_link becomes automatically detached.

In this way, the netlink API and bpf_link creation path are exclusive and don't stomp over one another. Filters created using bpf_link API cannot be replaced by netlink API, and filters created by netlink API are never replaced by bpf_link. Netfilter also cannot detach bpf_link filters.

We serialize all changes dover rtnl_lock as cls_bpf API doesn't support the unlocked classifier API.

---
 include/linux/bpf_types.h |   3 +
 include/net/pkt_cls.h     |  13 ++
 include/net/sch_generic.h |   6 +-
 include/uapi/linux/bpf.h  |  15 +++
 kernel/bpf/syscall.c      |  10 +-
 net/sched/cls_api.c       | 139 ++++++++++++++++++++-
 net/sched/cls_bpf.c       | 250 +++++++++++++++++++++++++++++++++++++-
 7 files changed, 430 insertions(+), 6 deletions(-)
include/linux/bpf_types.h +3 -0
index a9db1eae6796..b1aaf7680917 100644
@@ -135,3 +135,6 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_ITER, iter)
 #ifdef CONFIG_NET
 BPF_LINK_TYPE(BPF_LINK_TYPE_NETNS, netns)
 #endif
+#if IS_ENABLED(CONFIG_NET_CLS_BPF)
+BPF_LINK_TYPE(BPF_LINK_TYPE_TC, tc)
+#endif
include/net/pkt_cls.h +13 -0
index 255e4f4b521f..c36c5d79db6b 100644
@@ -2,6 +2,7 @@
 #ifndef __NET_PKT_CLS_H
 #define __NET_PKT_CLS_H
 
+#include <linux/bpf.h>
 #include <linux/pkt_cls.h>
 #include <linux/workqueue.h>
 #include <net/sch_generic.h>
@@ -45,6 +46,9 @@ bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
 				       u32 chain_index);
 void tcf_chain_put_by_act(struct tcf_chain *chain);
+void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
+			       struct tcf_proto *tp, bool rtnl_held,
+			       struct netlink_ext_ack *extack);
 struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
 				     struct tcf_chain *chain);
 struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
@@ -1004,4 +1008,13 @@ struct tc_fifo_qopt_offload {
 	};
 };
 
+#if IS_ENABLED(CONFIG_NET_CLS_BPF)
+int bpf_tc_link_attach(union bpf_attr *attr, struct bpf_prog *prog);
+#else
+static inline int bpf_tc_link_attach(union bpf_attr *attr, struct bpf_prog *prog)
+{
+	return -EOPNOTSUPP;
+}
+#endif
+
 #endif
include/net/sch_generic.h +5 -1
index f7a6e14491fb..bacd70bfc5ed 100644
@@ -341,7 +341,11 @@ struct tcf_proto_ops {
 	int			(*tmplt_dump)(struct sk_buff *skb,
 					      struct net *net,
 					      void *tmplt_priv);
-
+#if IS_ENABLED(CONFIG_NET_CLS_BPF)
+	int			(*bpf_link_change)(struct net *net, struct tcf_proto *tp,
+						   struct bpf_prog *filter, void **arg, u32 handle,
+						   u32 gen_flags);
+#endif
 	struct module		*owner;
 	int			flags;
 };
include/uapi/linux/bpf.h +15 -0
index 2c1ba70abbf1..a3488463d145 100644
@@ -994,6 +994,7 @@ enum bpf_attach_type {
 	BPF_SK_LOOKUP,
 	BPF_XDP,
 	BPF_SK_SKB_VERDICT,
+	BPF_TC,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -1007,6 +1008,7 @@ enum bpf_link_type {
 	BPF_LINK_TYPE_ITER = 4,
 	BPF_LINK_TYPE_NETNS = 5,
 	BPF_LINK_TYPE_XDP = 6,
+	BPF_LINK_TYPE_TC = 7,
 
 	MAX_BPF_LINK_TYPE,
 };
@@ -1447,6 +1449,12 @@ union bpf_attr {
 				__aligned_u64	iter_info;	/* extra bpf_iter_link_info */
 				__u32		iter_info_len;	/* iter_info length */
 			};
+			struct { /* used by BPF_TC */
+				__u32 parent;
+				__u32 handle;
+				__u32 gen_flags;
+				__u16 priority;
+			} tc;
 		};
 	} link_create;
 
@@ -5519,6 +5527,13 @@ struct bpf_link_info {
 		struct {
 			__u32 ifindex;
 		} xdp;
+		struct {
+			__u32 ifindex;
+			__u32 parent;
+			__u32 handle;
+			__u32 gen_flags;
+			__u16 priority;
+		} tc;
 	};
 } __attribute__((aligned(8)));
 
kernel/bpf/syscall.c +9 -1
index e5934b748ced..ce7c00ea135c 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  */
+#include <net/pkt_cls.h>
 #include <linux/bpf.h>
 #include <linux/bpf_trace.h>
 #include <linux/bpf_lirc.h>
@@ -3027,6 +3028,8 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)
 		return BPF_PROG_TYPE_SK_LOOKUP;
 	case BPF_XDP:
 		return BPF_PROG_TYPE_XDP;
+	case BPF_TC:
+		return BPF_PROG_TYPE_SCHED_CLS;
 	default:
 		return BPF_PROG_TYPE_UNSPEC;
 	}
@@ -4085,7 +4088,7 @@ static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
 	return -EINVAL;
 }
 
-#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
+#define BPF_LINK_CREATE_LAST_FIELD link_create.tc.priority
 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 {
 	enum bpf_prog_type ptype;
@@ -4136,6 +4139,11 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
 	case BPF_PROG_TYPE_XDP:
 		ret = bpf_xdp_link_attach(attr, prog);
 		break;
+#endif
+#if IS_ENABLED(CONFIG_NET_CLS_BPF)
+	case BPF_PROG_TYPE_SCHED_CLS:
+		ret = bpf_tc_link_attach(attr, prog);
+		break;
 #endif
 	default:
 		ret = -EINVAL;
net/sched/cls_api.c +136 -3

160 lines not on this page · raw message

net/sched/cls_bpf.c +249 -1

299 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH bpf-next v2 3/7] net: sched: add bpf_link API for bpf classifier

Thank you for the patch! Perhaps something to improve:

Hi Kumar,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Kumar-Kartikeya-Dwivedi/Add-bpf_link-based-TC-BPF-API/20210604-143611
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: i386-randconfig-s001-20210603 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-341-g8af24329-dirty
        # https://github.com/0day-ci/linux/commit/a8da2c7297ab4c27511723367a5679b51bd5af7c
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Kumar-Kartikeya-Dwivedi/Add-bpf_link-based-TC-BPF-API/20210604-143611
        git checkout a8da2c7297ab4c27511723367a5679b51bd5af7c
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)
net/sched/cls_api.c:270:22: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __be16 [usertype] protocol @@ got unsigned int [usertype] protocol @@
net/sched/cls_api.c:270:22: sparse: expected restricted __be16 [usertype] protocol
net/sched/cls_api.c:270:22: sparse: got unsigned int [usertype] protocol
net/sched/cls_api.c:1675:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
net/sched/cls_api.c:1675:16: sparse: struct tcf_proto *
net/sched/cls_api.c:1675:16: sparse: struct tcf_proto [noderef] __rcu *
net/sched/cls_api.c:1776:20: sparse: sparse: incompatible types in comparison expression (different address spaces):
net/sched/cls_api.c:1776:20: sparse: struct tcf_proto [noderef] __rcu *
net/sched/cls_api.c:1776:20: sparse: struct tcf_proto *
net/sched/cls_api.c:1737:25: sparse: sparse: incompatible types in comparison expression (different address spaces):
net/sched/cls_api.c:1737:25: sparse: struct tcf_proto [noderef] __rcu *
net/sched/cls_api.c:1737:25: sparse: struct tcf_proto *
net/sched/cls_api.c:1757:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
net/sched/cls_api.c:1757:16: sparse: struct tcf_proto *
net/sched/cls_api.c:1757:16: sparse: struct tcf_proto [noderef] __rcu *
net/sched/cls_api.c:1823:25: sparse: sparse: restricted __be16 degrades to integer net/sched/cls_api.c:2497:50: sparse: sparse: restricted __be16 degrades to integer

>> net/sched/cls_api.c:3976:52: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected unsigned int [usertype] protocol @@     got restricted __be16 [assigned] [usertype] protocol @@

net/sched/cls_api.c:3976:52: sparse: expected unsigned int [usertype] protocol
net/sched/cls_api.c:3976:52: sparse: got restricted __be16 [assigned] [usertype] protocol
net/sched/cls_api.c:3998:50: sparse: sparse: incorrect type in argument 2 (different base types) @@ expected unsigned int [usertype] protocol @@ got restricted __be16 [assigned] [usertype] protocol @@
net/sched/cls_api.c:3998:50: sparse: expected unsigned int [usertype] protocol
net/sched/cls_api.c:3998:50: sparse: got restricted __be16 [assigned] [usertype] protocol
net/sched/cls_api.c:4006:64: sparse: sparse: incorrect type in argument 3 (different base types) @@ expected unsigned int [usertype] protocol @@ got restricted __be16 [assigned] [usertype] protocol @@
net/sched/cls_api.c:4006:64: sparse: expected unsigned int [usertype] protocol
net/sched/cls_api.c:4006:64: sparse: got restricted __be16 [assigned] [usertype] protocol

vim +3976 net/sched/cls_api.c

3924
3925 int bpf_tc_link_attach(union bpf_attr *attr, struct bpf_prog *prog)
3926 {
3927 struct net *net = current->nsproxy->net_ns;
3928 struct tcf_chain_info chain_info;
3929 u32 chain_index, prio, parent;
3930 struct tcf_block *block;
3931 struct tcf_chain *chain;
3932 struct tcf_proto *tp;
3933 int err, tp_created;
3934 unsigned long cl;
3935 struct Qdisc *q;
3936 __be16 protocol;
3937 void *fh;
3938
3939 /* Caller already checks bpf_capable */
3940 if (!ns_capable(current->nsproxy->net_ns->user_ns, CAP_NET_ADMIN))
3941 return -EPERM;
3942
3943 if (attr->link_create.flags ||
3944 !attr->link_create.target_ifindex ||
3945 !tc_flags_valid(attr->link_create.tc.gen_flags))
3946 return -EINVAL;
3947
3948 replay:
3949 parent = attr->link_create.tc.parent;
3950 prio = attr->link_create.tc.priority;
3951 protocol = htons(ETH_P_ALL);
3952 chain_index = 0;
3953 tp_created = 0;
3954 prio <<= 16;
3955 cl = 0;
3956
3957 /* Address this when cls_bpf switches to RTNL_FLAG_DOIT_UNLOCKED */
3958 rtnl_lock();
3959
3960 block = tcf_block_find(net, &q, &parent, &cl,
3961 attr->link_create.target_ifindex, parent, NULL);
3962 if (IS_ERR(block)) {
3963 err = PTR_ERR(block);
3964 goto out_unlock;
3965 }
3966 block->classid = parent;
3967
3968 chain = tcf_chain_get(block, chain_index, true);
3969 if (!chain) {
3970 err = -ENOMEM;
3971 goto out_block;
3972 }
3973
3974 mutex_lock(&chain->filter_chain_lock);
3975

> 3976		tp = tcf_chain_tp_find(chain, &chain_info, protocol,

3977 prio ?: TC_H_MAKE(0x80000000U, 0U),
3978 !prio);
3979 if (IS_ERR(tp)) {
3980 err = PTR_ERR(tp);
3981 goto out_chain_unlock;
3982 }
3983
3984 if (!tp) {
3985 struct tcf_proto *tp_new = NULL;
3986
3987 if (chain->flushing) {
3988 err = -EAGAIN;
3989 goto out_chain_unlock;
3990 }
3991
3992 if (!prio)
3993 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
3994 &chain_info));
3995
3996 mutex_unlock(&chain->filter_chain_lock);
3997
3998 tp_new = tcf_proto_create("bpf", protocol, prio, chain, true,
3999 NULL);
4000 if (IS_ERR(tp_new)) {
4001 err = PTR_ERR(tp_new);
4002 goto out_chain;
4003 }
4004
4005 tp_created = 1;
4006 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
4007 true);
4008 if (IS_ERR(tp)) {
4009 err = PTR_ERR(tp);
4010 goto out_chain;
4011 }
4012 } else {
4013 mutex_unlock(&chain->filter_chain_lock);
4014 }
4015
4016 fh = tp->ops->get(tp, attr->link_create.tc.handle);
4017
4018 if (!tp->ops->bpf_link_change)
4019 err = -EDEADLK;
4020 else
4021 err = tp->ops->bpf_link_change(net, tp, prog, &fh,
4022 attr->link_create.tc.handle,
4023 attr->link_create.tc.gen_flags);
4024 if (err >= 0 && q)
4025 q->flags &= ~TCQ_F_CAN_BYPASS;
4026
4027 out:
4028 if (err < 0 && tp_created)
4029 tcf_chain_tp_delete_empty(chain, tp, true, NULL);
4030 out_chain:
4031 if (chain) {
4032 if (!IS_ERR_OR_NULL(tp))
4033 tcf_proto_put(tp, true, NULL);
4034 /* Chain reference only kept for tp creation
4035 * to pair with tcf_chain_put from tcf_proto_destroy
4036 */
4037 if (!tp_created)
4038 tcf_chain_put(chain);
4039 }
4040 out_block:
4041 tcf_block_release(q, block, true);
4042 out_unlock:
4043 rtnl_unlock();
4044 if (err == -EAGAIN)
4045 goto replay;
4046 return err;
4047 out_chain_unlock:
4048 mutex_unlock(&chain->filter_chain_lock);
4049 goto out;
4050 }
4051

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw

Re: [PATCH bpf-next v2 3/7] net: sched: add bpf_link API for bpf classifier

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

This commit introduces a bpf_link based kernel API for creating tc filters and using the cls_bpf classifier. Only a subset of what netlink API offers is supported, things like TCA_BPF_POLICE, TCA_RATE and embedded actions are unsupported.

The kernel API and the libbpf wrapper added in a subsequent patch are more opinionated and mirror the semantics of low level netlink based TC-BPF API, i.e. always setting direct action mode, always setting protocol to ETH_P_ALL, and only exposing handle and priority as the variables the user can control. We add an additional gen_flags parameter though to allow for offloading use cases. It would be trivial to extend the current API to support specifying other attributes in the future, but for now I'm sticking how we want to push usage.

The semantics around bpf_link support are as follows:

A user can create a classifier attached to a filter using the bpf_link API, after which changing it and deleting it only happens through the bpf_link API. It is not possible to bind the bpf_link to existing filter, and any such attempt will fail with EEXIST. Hence EEXIST can be returned in two cases, when existing bpf_link owned filter exists, or existing netlink owned filter exists.

Removing bpf_link owned filter from netlink returns EPERM, denoting that netlink is locked out from filter manipulation when bpf_link is involved.

Whenever a filter is detached due to chain removal, or qdisc tear down, or net_device shutdown, the bpf_link becomes automatically detached.

Quoted 6 lines

In this way, the netlink API and bpf_link creation path are exclusive and don't stomp over one another. Filters created using bpf_link API cannot be replaced by netlink API, and filters created by netlink API are never replaced by bpf_link. Netfilter also cannot detach bpf_link filters.

We serialize all changes dover rtnl_lock as cls_bpf API doesn't support the

dover => over?

Quoted 14 lines

unlocked classifier API.

---
  include/linux/bpf_types.h |   3 +
  include/net/pkt_cls.h     |  13 ++
  include/net/sch_generic.h |   6 +-
  include/uapi/linux/bpf.h  |  15 +++
  kernel/bpf/syscall.c      |  10 +-
  net/sched/cls_api.c       | 139 ++++++++++++++++++++-
  net/sched/cls_bpf.c       | 250 +++++++++++++++++++++++++++++++++++++-
  7 files changed, 430 insertions(+), 6 deletions(-)

[...]

Quoted 10 lines
  subsys_initcall(tc_filter_init);
net/sched/cls_api.c +9 -0
+
+#if IS_ENABLED(CONFIG_NET_CLS_BPF)
+
+int bpf_tc_link_attach(union bpf_attr *attr, struct bpf_prog *prog)
+{
+	struct net *net = current->nsproxy->net_ns;
+	struct tcf_chain_info chain_info;
+	u32 chain_index, prio, parent;
+	struct tcf_block *block;

net/sched/cls_api.c static int __init tc_filter_init(void) · line 3940

Quoted 10 lines
net/sched/cls_api.c +10 -0
+	struct tcf_chain *chain;
+	struct tcf_proto *tp;
+	int err, tp_created;
+	unsigned long cl;
+	struct Qdisc *q;
+	__be16 protocol;
+	void *fh;
+
+	/* Caller already checks bpf_capable */
+	if (!ns_capable(current->nsproxy->net_ns->user_ns, CAP_NET_ADMIN))

net->user_ns?

Quoted 7 lines
net/sched/cls_api.c +7 -0
+		return -EPERM;
+
+	if (attr->link_create.flags ||
+	    !attr->link_create.target_ifindex ||
+	    !tc_flags_valid(attr->link_create.tc.gen_flags))
+		return -EINVAL;
+

[...]

author maintainer
#6 lore raw

Re: [PATCH bpf-next v2 3/7] net: sched: add bpf_link API for bpf classifier

Quoted 33 lines · Yonghong Song
Quoted 30 lines · Kumar Kartikeya Dwivedi

This commit introduces a bpf_link based kernel API for creating tc filters and using the cls_bpf classifier. Only a subset of what netlink API offers is supported, things like TCA_BPF_POLICE, TCA_RATE and embedded actions are unsupported.

The kernel API and the libbpf wrapper added in a subsequent patch are more opinionated and mirror the semantics of low level netlink based TC-BPF API, i.e. always setting direct action mode, always setting protocol to ETH_P_ALL, and only exposing handle and priority as the variables the user can control. We add an additional gen_flags parameter though to allow for offloading use cases. It would be trivial to extend the current API to support specifying other attributes in the future, but for now I'm sticking how we want to push usage.

The semantics around bpf_link support are as follows:

A user can create a classifier attached to a filter using the bpf_link API, after which changing it and deleting it only happens through the bpf_link API. It is not possible to bind the bpf_link to existing filter, and any such attempt will fail with EEXIST. Hence EEXIST can be returned in two cases, when existing bpf_link owned filter exists, or existing netlink owned filter exists.

Removing bpf_link owned filter from netlink returns EPERM, denoting that netlink is locked out from filter manipulation when bpf_link is involved.

Whenever a filter is detached due to chain removal, or qdisc tear down, or net_device shutdown, the bpf_link becomes automatically detached.

Quoted 9 lines
Quoted 6 lines

In this way, the netlink API and bpf_link creation path are exclusive and don't stomp over one another. Filters created using bpf_link API cannot be replaced by netlink API, and filters created by netlink API are never replaced by bpf_link. Netfilter also cannot detach bpf_link filters.

We serialize all changes dover rtnl_lock as cls_bpf API doesn't support the

dover => over?

Thanks, will fix.

Quoted 29 lines
Quoted 14 lines

unlocked classifier API.

---
  include/linux/bpf_types.h |   3 +
  include/net/pkt_cls.h     |  13 ++
  include/net/sch_generic.h |   6 +-
  include/uapi/linux/bpf.h  |  15 +++
  kernel/bpf/syscall.c      |  10 +-
  net/sched/cls_api.c       | 139 ++++++++++++++++++++-
  net/sched/cls_bpf.c       | 250 +++++++++++++++++++++++++++++++++++++-
  7 files changed, 430 insertions(+), 6 deletions(-)

[...]

Quoted 14 lines
  subsys_initcall(tc_filter_init);
net/sched/cls_api.c +13 -0
+
+#if IS_ENABLED(CONFIG_NET_CLS_BPF)
+
+int bpf_tc_link_attach(union bpf_attr *attr, struct bpf_prog *prog)
+{
+	struct net *net = current->nsproxy->net_ns;
+	struct tcf_chain_info chain_info;
+	u32 chain_index, prio, parent;
+	struct tcf_block *block;
+	struct tcf_chain *chain;
+	struct tcf_proto *tp;
+	int err, tp_created;
+	unsigned long cl;
Quoted 9 lines

net/sched/cls_api.c static int __init tc_filter_init(void) · line 3940

Quoted 6 lines
net/sched/cls_api.c +6 -0
+	struct Qdisc *q;
+	__be16 protocol;
+	void *fh;
+
+	/* Caller already checks bpf_capable */
+	if (!ns_capable(current->nsproxy->net_ns->user_ns, CAP_NET_ADMIN))

net->user_ns?

True, will fix.

Quoted 8 lines
Quoted 7 lines
net/sched/cls_api.c +7 -0
+		return -EPERM;
+
+	if (attr->link_create.flags ||
+	    !attr->link_create.target_ifindex ||
+	    !tc_flags_valid(attr->link_create.tc.gen_flags))
+		return -EINVAL;
+

[...]

--
Kartikeya

#7 lore raw

Re: [PATCH bpf-next v2 3/7] net: sched: add bpf_link API for bpf classifier

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

This commit introduces a bpf_link based kernel API for creating tc filters and using the cls_bpf classifier. Only a subset of what netlink API offers is supported, things like TCA_BPF_POLICE, TCA_RATE and embedded actions are unsupported.

The kernel API and the libbpf wrapper added in a subsequent patch are more opinionated and mirror the semantics of low level netlink based TC-BPF API, i.e. always setting direct action mode, always setting protocol to ETH_P_ALL, and only exposing handle and priority as the variables the user can control. We add an additional gen_flags parameter though to allow for offloading use cases. It would be trivial to extend the current API to support specifying other attributes in the future, but for now I'm sticking how we want to push usage.

The semantics around bpf_link support are as follows:

A user can create a classifier attached to a filter using the bpf_link API, after which changing it and deleting it only happens through the bpf_link API. It is not possible to bind the bpf_link to existing filter, and any such attempt will fail with EEXIST. Hence EEXIST can be returned in two cases, when existing bpf_link owned filter exists, or existing netlink owned filter exists.

Removing bpf_link owned filter from netlink returns EPERM, denoting that netlink is locked out from filter manipulation when bpf_link is involved.

Whenever a filter is detached due to chain removal, or qdisc tear down, or net_device shutdown, the bpf_link becomes automatically detached.

In this way, the netlink API and bpf_link creation path are exclusive and don't stomp over one another. Filters created using bpf_link API cannot be replaced by netlink API, and filters created by netlink API are never replaced by bpf_link. Netfilter also cannot detach bpf_link filters.

We serialize all changes dover rtnl_lock as cls_bpf API doesn't support the unlocked classifier API.

---
 include/linux/bpf_types.h |   3 +
 include/net/pkt_cls.h     |  13 ++
 include/net/sch_generic.h |   6 +-
 include/uapi/linux/bpf.h  |  15 +++
 kernel/bpf/syscall.c      |  10 +-
 net/sched/cls_api.c       | 139 ++++++++++++++++++++-
 net/sched/cls_bpf.c       | 250 +++++++++++++++++++++++++++++++++++++-
 7 files changed, 430 insertions(+), 6 deletions(-)

[...]

include/uapi/linux/bpf.h union bpf_attr { · line 1455

Quoted 8 lines
include/uapi/linux/bpf.h +4 -0
@@ -1447,6 +1449,12 @@ union bpf_attr {
                                __aligned_u64   iter_info;      /* extra bpf_iter_link_info */
                                __u32           iter_info_len;  /* iter_info length */
                        };
+                       struct { /* used by BPF_TC */
+                               __u32 parent;
+                               __u32 handle;
+                               __u32 gen_flags;

There is already link_create.flags that's totally up to a specific type of bpf_link. E.g., cgroup bpf_link doesn't accept any flags, while xdp bpf_link uses it for passing XDP-specific flags. Is there a need to have both gen_flags and flags for TC link?

Review union bpf_attr { · line 1456

Quoted 1 line
(quoted diff) +1 -0
+                               __u16 priority;

No strong preference, but we typically try to not have unnecessary padding in UAPI bpf_attr, so I wonder if using __u32 for this would make sense?

Quoted 18 lines
include/uapi/linux/bpf.h +8 -0
+                       } tc;
                };
        } link_create;
 
@@ -5519,6 +5527,13 @@ struct bpf_link_info {
                struct {
                        __u32 ifindex;
                } xdp;
+               struct {
+                       __u32 ifindex;
+                       __u32 parent;
+                       __u32 handle;
+                       __u32 gen_flags;
+                       __u16 priority;
+               } tc;
        };
 } __attribute__((aligned(8)));
 

[...]

author maintainer
#8 lore raw

PATCH v2 4/7 net: sched: add lightweight update path for cls_bpf

In reply to Kumar Kartikeya Dwivedi

This is used by BPF_LINK_UPDATE to replace the attach SCHED_CLS bpf prog effectively changing the classifier implementation for a given filter owned by a bpf_link.

Note that READ_ONCE suffices in this case as the ordering for loads from the filter are implicitly provided by the data dependency on BPF prog pointer.

On the writer side we can just use a relaxed WRITE_ONCE store to make sure one or the other value is visible to a reader in cls_bpf_classify. Lifetime is managed using RCU so bpf_prog_put path should wait until readers are done for old_prog.

All other parties accessing the BPF prog are under RTNL protection, so need no changes.

---
 net/sched/cls_bpf.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +53 -2
index bf61ffbb7fd0..f23304685c48 100644
@@ -9,6 +9,7 @@
  * (C) 2013 Daniel Borkmann <[email protected]>
  */
 
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
@@ -104,11 +105,11 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 			/* It is safe to push/pull even if skb_shared() */
 			__skb_push(skb, skb->mac_len);
 			bpf_compute_data_pointers(skb);
-			filter_res = BPF_PROG_RUN(prog->filter, skb);
+			filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
 			__skb_pull(skb, skb->mac_len);
 		} else {
 			bpf_compute_data_pointers(skb);
-			filter_res = BPF_PROG_RUN(prog->filter, skb);
+			filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
 		}
 
 		if (prog->exts_integrated) {
@@ -775,6 +776,55 @@ static int cls_bpf_link_detach(struct bpf_link *link)
 	return 0;
 }
 
+static int cls_bpf_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
+			       struct bpf_prog *old_prog)
+{
+	struct cls_bpf_link *cls_link;
+	struct cls_bpf_prog cls_prog;
+	struct cls_bpf_prog *prog;
+	int ret;
+
+	rtnl_lock();
+
+	cls_link = container_of(link, struct cls_bpf_link, link);
+	if (!cls_link->prog) {
+		ret = -ENOLINK;
+		goto out;
+	}
+
+	prog = cls_link->prog;
+
+	/* BPF_F_REPLACEing? */
+	if (old_prog && prog->filter != old_prog) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	old_prog = prog->filter;
+
+	if (new_prog == old_prog) {
+		ret = 0;
+		goto out;
+	}
+
+	cls_prog = *prog;
+	cls_prog.filter = new_prog;
+
+	ret = cls_bpf_offload(prog->tp, &cls_prog, prog, NULL);
+	if (ret < 0)
+		goto out;
+
+	WRITE_ONCE(prog->filter, new_prog);
+
+	bpf_prog_inc(new_prog);
+	/* release our reference */
+	bpf_prog_put(old_prog);
+
+out:
+	rtnl_unlock();
+	return ret;
+}
+
 static void __bpf_fill_link_info(struct cls_bpf_link *link,
 				 struct bpf_link_info *info)
 {
@@ -859,6 +909,7 @@ static const struct bpf_link_ops cls_bpf_link_ops = {
 	.show_fdinfo = cls_bpf_link_show_fdinfo,
 #endif
 	.fill_link_info = cls_bpf_link_fill_link_info,
+	.update_prog = cls_bpf_link_update,
 };
 
 static inline char *cls_bpf_link_name(u32 prog_id, const char *name)
Signature · 1 line
2.31.1
#9 lore raw

Re: [PATCH bpf-next v2 4/7] net: sched: add lightweight update path for cls_bpf

Quoted 4 lines · Kumar Kartikeya Dwivedi

This is used by BPF_LINK_UPDATE to replace the attach SCHED_CLS bpf prog effectively changing the classifier implementation for a given filter owned by a bpf_link.

Quoted 8 lines

Note that READ_ONCE suffices in this case as the ordering for loads from the filter are implicitly provided by the data dependency on BPF prog pointer.

On the writer side we can just use a relaxed WRITE_ONCE store to make sure one or the other value is visible to a reader in cls_bpf_classify. Lifetime is managed using RCU so bpf_prog_put path should wait until readers are done for old_prog.

Should those be rcu_deref and rcu_assign_pointer ? Typically the pointer would be __rcu annotated which would be another small change in struct cls_bpf_prog.
That would make the life time easier to follow?

Quoted 50 lines

All other parties accessing the BPF prog are under RTNL protection, so need no changes.

---
 net/sched/cls_bpf.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +14 -2
index bf61ffbb7fd0..f23304685c48 100644
@@ -9,6 +9,7 @@
  * (C) 2013 Daniel Borkmann <[email protected]>
  */
 
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
@@ -104,11 +105,11 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 			/* It is safe to push/pull even if skb_shared() */
 			__skb_push(skb, skb->mac_len);
 			bpf_compute_data_pointers(skb);
-			filter_res = BPF_PROG_RUN(prog->filter, skb);
+			filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
 			__skb_pull(skb, skb->mac_len);
 		} else {
 			bpf_compute_data_pointers(skb);
-			filter_res = BPF_PROG_RUN(prog->filter, skb);
+			filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
 		}
 
 		if (prog->exts_integrated) {
@@ -775,6 +776,55 @@ static int cls_bpf_link_detach(struct bpf_link *link)
 	return 0;
 }
 
+static int cls_bpf_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
+			       struct bpf_prog *old_prog)
+{
+	struct cls_bpf_link *cls_link;
+	struct cls_bpf_prog cls_prog;
+	struct cls_bpf_prog *prog;
+	int ret;
+
+	rtnl_lock();
+
+	cls_link = container_of(link, struct cls_bpf_link, link);

net/sched/cls_bpf.c static int cls_bpf_link_detach(struct bpf_link *link) · line 799

Quoted 10 lines
net/sched/cls_bpf.c +10 -0
+	if (!cls_link->prog) {
+		ret = -ENOLINK;
+		goto out;
+	}
+
+	prog = cls_link->prog;
+
+	/* BPF_F_REPLACEing? */
+	if (old_prog && prog->filter != old_prog) {
+		ret = -EINVAL;

Other places like cgroup_bpf_replace and bpf_iter_link_replace return -EPERM in such case.

Quoted 42 lines
net/sched/cls_bpf.c +29 -0
+		goto out;
+	}
+
+	old_prog = prog->filter;
+
+	if (new_prog == old_prog) {
+		ret = 0;
+		goto out;
+	}
+
+	cls_prog = *prog;
+	cls_prog.filter = new_prog;
+
+	ret = cls_bpf_offload(prog->tp, &cls_prog, prog, NULL);
+	if (ret < 0)
+		goto out;
+
+	WRITE_ONCE(prog->filter, new_prog);
+
+	bpf_prog_inc(new_prog);
+	/* release our reference */
+	bpf_prog_put(old_prog);
+
+out:
+	rtnl_unlock();
+	return ret;
+}
+
 static void __bpf_fill_link_info(struct cls_bpf_link *link,
 				 struct bpf_link_info *info)
 {
@@ -859,6 +909,7 @@ static const struct bpf_link_ops cls_bpf_link_ops = {
 	.show_fdinfo = cls_bpf_link_show_fdinfo,
 #endif
 	.fill_link_info = cls_bpf_link_fill_link_info,
+	.update_prog = cls_bpf_link_update,
 };
 
 static inline char *cls_bpf_link_name(u32 prog_id, const char *name)
Signature · 1 line
2.31.1
author maintainer
#10 lore raw

Re: [PATCH bpf-next v2 4/7] net: sched: add lightweight update path for cls_bpf

Quoted 14 lines · Alexei Starovoitov
Quoted 4 lines · Kumar Kartikeya Dwivedi

This is used by BPF_LINK_UPDATE to replace the attach SCHED_CLS bpf prog effectively changing the classifier implementation for a given filter owned by a bpf_link.

Quoted 8 lines

Note that READ_ONCE suffices in this case as the ordering for loads from the filter are implicitly provided by the data dependency on BPF prog pointer.

On the writer side we can just use a relaxed WRITE_ONCE store to make sure one or the other value is visible to a reader in cls_bpf_classify. Lifetime is managed using RCU so bpf_prog_put path should wait until readers are done for old_prog.

Quoted 5 lines

Should those be rcu_deref and rcu_assign_pointer ? Typically the pointer would be __rcu annotated which would be another small change in struct cls_bpf_prog.
That would make the life time easier to follow?

True, I'll make that change.

Quoted 55 lines
Quoted 55 lines

All other parties accessing the BPF prog are under RTNL protection, so need no changes.

---
 net/sched/cls_bpf.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +19 -2
index bf61ffbb7fd0..f23304685c48 100644
@@ -9,6 +9,7 @@
  * (C) 2013 Daniel Borkmann <[email protected]>
  */
 
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
@@ -104,11 +105,11 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
 			/* It is safe to push/pull even if skb_shared() */
 			__skb_push(skb, skb->mac_len);
 			bpf_compute_data_pointers(skb);
-			filter_res = BPF_PROG_RUN(prog->filter, skb);
+			filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
 			__skb_pull(skb, skb->mac_len);
 		} else {
 			bpf_compute_data_pointers(skb);
-			filter_res = BPF_PROG_RUN(prog->filter, skb);
+			filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
 		}
 
 		if (prog->exts_integrated) {
@@ -775,6 +776,55 @@ static int cls_bpf_link_detach(struct bpf_link *link)
 	return 0;
 }
 
+static int cls_bpf_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
+			       struct bpf_prog *old_prog)
+{
+	struct cls_bpf_link *cls_link;
+	struct cls_bpf_prog cls_prog;
+	struct cls_bpf_prog *prog;
+	int ret;
+
+	rtnl_lock();
+
+	cls_link = container_of(link, struct cls_bpf_link, link);
+	if (!cls_link->prog) {
+		ret = -ENOLINK;
+		goto out;
+	}
+
Quoted 9 lines

net/sched/cls_bpf.c static int cls_bpf_link_detach(struct bpf_link *link) · line 799

Quoted 5 lines
net/sched/cls_bpf.c +5 -0
+	prog = cls_link->prog;
+
+	/* BPF_F_REPLACEing? */
+	if (old_prog && prog->filter != old_prog) {
+		ret = -EINVAL;

Other places like cgroup_bpf_replace and bpf_iter_link_replace return -EPERM in such case.

Ok, will change.

Quoted 38 lines
Quoted 38 lines
net/sched/cls_bpf.c +29 -0
+		goto out;
+	}
+
+	old_prog = prog->filter;
+
+	if (new_prog == old_prog) {
+		ret = 0;
+		goto out;
+	}
+
+	cls_prog = *prog;
+	cls_prog.filter = new_prog;
+
+	ret = cls_bpf_offload(prog->tp, &cls_prog, prog, NULL);
+	if (ret < 0)
+		goto out;
+
+	WRITE_ONCE(prog->filter, new_prog);
+
+	bpf_prog_inc(new_prog);
+	/* release our reference */
+	bpf_prog_put(old_prog);
+
+out:
+	rtnl_unlock();
+	return ret;
+}
+
 static void __bpf_fill_link_info(struct cls_bpf_link *link,
 				 struct bpf_link_info *info)
 {
@@ -859,6 +909,7 @@ static const struct bpf_link_ops cls_bpf_link_ops = {
 	.show_fdinfo = cls_bpf_link_show_fdinfo,
 #endif
 	.fill_link_info = cls_bpf_link_fill_link_info,
+	.update_prog = cls_bpf_link_update,
 };
 
Quoted 6 lines
Quoted 4 lines
net/sched/cls_bpf.c +0 -1
 static inline char *cls_bpf_link_name(u32 prog_id, const char *name)
--

2.31.1

--

--
Kartikeya

#11 lore raw

Re: [PATCH bpf-next v2 4/7] net: sched: add lightweight update path for cls_bpf

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

This is used by BPF_LINK_UPDATE to replace the attach SCHED_CLS bpf prog effectively changing the classifier implementation for a given filter owned by a bpf_link.

Note that READ_ONCE suffices in this case as the ordering for loads from the filter are implicitly provided by the data dependency on BPF prog pointer.

On the writer side we can just use a relaxed WRITE_ONCE store to make sure one or the other value is visible to a reader in cls_bpf_classify. Lifetime is managed using RCU so bpf_prog_put path should wait until readers are done for old_prog.

All other parties accessing the BPF prog are under RTNL protection, so need no changes.

---
 net/sched/cls_bpf.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +21 -2
index bf61ffbb7fd0..f23304685c48 100644
@@ -9,6 +9,7 @@
  * (C) 2013 Daniel Borkmann <[email protected]>
  */
 
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
@@ -104,11 +105,11 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
                        /* It is safe to push/pull even if skb_shared() */
                        __skb_push(skb, skb->mac_len);
                        bpf_compute_data_pointers(skb);
-                       filter_res = BPF_PROG_RUN(prog->filter, skb);
+                       filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
                        __skb_pull(skb, skb->mac_len);
                } else {
                        bpf_compute_data_pointers(skb);
-                       filter_res = BPF_PROG_RUN(prog->filter, skb);
+                       filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
                }
 
                if (prog->exts_integrated) {
@@ -775,6 +776,55 @@ static int cls_bpf_link_detach(struct bpf_link *link)
        return 0;
 }
 
+static int cls_bpf_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
+                              struct bpf_prog *old_prog)
+{
+       struct cls_bpf_link *cls_link;
+       struct cls_bpf_prog cls_prog;
+       struct cls_bpf_prog *prog;
+       int ret;
+
+       rtnl_lock();
+
+       cls_link = container_of(link, struct cls_bpf_link, link);
+       if (!cls_link->prog) {
+               ret = -ENOLINK;
+               goto out;
+       }
+
+       prog = cls_link->prog;
+

net/sched/cls_bpf.c static int cls_bpf_link_detach(struct bpf_link *link) · line 806

Quoted 10 lines
net/sched/cls_bpf.c +10 -0
+       /* BPF_F_REPLACEing? */
+       if (old_prog && prog->filter != old_prog) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       old_prog = prog->filter;
+
+       if (new_prog == old_prog) {
+               ret = 0;

So the contract is that if update is successful, new_prog's refcount taken by link_update() in kernel/bpf/syscall.c is transferred here. On error, it will be bpf_prog_put() by link_update(). So here you don't need extra refcnt, but it's also not an error, so you need to bpf_prog_put(new_prog) explicitly to balance out refcnt. See how it's done for XDP, for example.

net/sched/cls_bpf.c static int cls_bpf_link_detach(struct bpf_link *link) · line 819

Quoted 13 lines
net/sched/cls_bpf.c +13 -0
+               goto out;
+       }
+
+       cls_prog = *prog;
+       cls_prog.filter = new_prog;
+
+       ret = cls_bpf_offload(prog->tp, &cls_prog, prog, NULL);
+       if (ret < 0)
+               goto out;
+
+       WRITE_ONCE(prog->filter, new_prog);
+
+       bpf_prog_inc(new_prog);

and you don't need this, you already got the reference from link_update()

Quoted 22 lines
net/sched/cls_bpf.c +9 -1
+       /* release our reference */
+       bpf_prog_put(old_prog);
+
+out:
+       rtnl_unlock();
+       return ret;
+}
+
 static void __bpf_fill_link_info(struct cls_bpf_link *link,
                                 struct bpf_link_info *info)
 {
@@ -859,6 +909,7 @@ static const struct bpf_link_ops cls_bpf_link_ops = {
        .show_fdinfo = cls_bpf_link_show_fdinfo,
 #endif
        .fill_link_info = cls_bpf_link_fill_link_info,
+       .update_prog = cls_bpf_link_update,
 };
 
 static inline char *cls_bpf_link_name(u32 prog_id, const char *name)
--

2.31.1

author maintainer
#12 lore raw

Re: [PATCH bpf-next v2 4/7] net: sched: add lightweight update path for cls_bpf

Quoted 82 lines · Andrii Nakryiko
Quoted 80 lines · Kumar Kartikeya Dwivedi

This is used by BPF_LINK_UPDATE to replace the attach SCHED_CLS bpf prog effectively changing the classifier implementation for a given filter owned by a bpf_link.

Note that READ_ONCE suffices in this case as the ordering for loads from the filter are implicitly provided by the data dependency on BPF prog pointer.

On the writer side we can just use a relaxed WRITE_ONCE store to make sure one or the other value is visible to a reader in cls_bpf_classify. Lifetime is managed using RCU so bpf_prog_put path should wait until readers are done for old_prog.

All other parties accessing the BPF prog are under RTNL protection, so need no changes.

---
 net/sched/cls_bpf.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
net/sched/cls_bpf.c +30 -2
index bf61ffbb7fd0..f23304685c48 100644
@@ -9,6 +9,7 @@
  * (C) 2013 Daniel Borkmann <[email protected]>
  */
 
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
@@ -104,11 +105,11 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
                        /* It is safe to push/pull even if skb_shared() */
                        __skb_push(skb, skb->mac_len);
                        bpf_compute_data_pointers(skb);
-                       filter_res = BPF_PROG_RUN(prog->filter, skb);
+                       filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
                        __skb_pull(skb, skb->mac_len);
                } else {
                        bpf_compute_data_pointers(skb);
-                       filter_res = BPF_PROG_RUN(prog->filter, skb);
+                       filter_res = BPF_PROG_RUN(READ_ONCE(prog->filter), skb);
                }
 
                if (prog->exts_integrated) {
@@ -775,6 +776,55 @@ static int cls_bpf_link_detach(struct bpf_link *link)
        return 0;
 }
 
+static int cls_bpf_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
+                              struct bpf_prog *old_prog)
+{
+       struct cls_bpf_link *cls_link;
+       struct cls_bpf_prog cls_prog;
+       struct cls_bpf_prog *prog;
+       int ret;
+
+       rtnl_lock();
+
+       cls_link = container_of(link, struct cls_bpf_link, link);
+       if (!cls_link->prog) {
+               ret = -ENOLINK;
+               goto out;
+       }
+
+       prog = cls_link->prog;
+
+       /* BPF_F_REPLACEing? */
+       if (old_prog && prog->filter != old_prog) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       old_prog = prog->filter;
+
+       if (new_prog == old_prog) {
Quoted 9 lines

net/sched/cls_bpf.c static int cls_bpf_link_detach(struct bpf_link *link) · line 806

Quoted 1 line
net/sched/cls_bpf.c +1 -0
+               ret = 0;

So the contract is that if update is successful, new_prog's refcount taken by link_update() in kernel/bpf/syscall.c is transferred here. On error, it will be bpf_prog_put() by link_update(). So here you don't need extra refcnt, but it's also not an error, so you need to bpf_prog_put(new_prog) explicitly to balance out refcnt. See how it's done for XDP, for example.

Yes, thanks for spotting this.

Quoted 17 lines
Quoted 4 lines
net/sched/cls_bpf.c +3 -0
+               goto out;
+       }
+

net/sched/cls_bpf.c static int cls_bpf_link_detach(struct bpf_link *link) · line 819

Quoted 10 lines
net/sched/cls_bpf.c +10 -0
+       cls_prog = *prog;
+       cls_prog.filter = new_prog;
+
+       ret = cls_bpf_offload(prog->tp, &cls_prog, prog, NULL);
+       if (ret < 0)
+               goto out;
+
+       WRITE_ONCE(prog->filter, new_prog);
+
+       bpf_prog_inc(new_prog);

and you don't need this, you already got the reference from link_update()

So the reason I still keep an extra refcount is because the existing code on the netlink side assumes that. Even though the link itself holds a refcount for us, the actual freeing of cls_bpf_prog may happen independent of bpf_link.

I'll add a comment for this.

Quoted 18 lines
Quoted 18 lines
net/sched/cls_bpf.c +9 -0
+       /* release our reference */
+       bpf_prog_put(old_prog);
+
+out:
+       rtnl_unlock();
+       return ret;
+}
+
 static void __bpf_fill_link_info(struct cls_bpf_link *link,
                                 struct bpf_link_info *info)
 {
@@ -859,6 +909,7 @@ static const struct bpf_link_ops cls_bpf_link_ops = {
        .show_fdinfo = cls_bpf_link_show_fdinfo,
 #endif
        .fill_link_info = cls_bpf_link_fill_link_info,
+       .update_prog = cls_bpf_link_update,
 };
 
Quoted 4 lines
Quoted 4 lines
net/sched/cls_bpf.c +0 -1
 static inline char *cls_bpf_link_name(u32 prog_id, const char *name)
--

2.31.1

--
Kartikeya

author maintainer
#13 lore raw

PATCH v2 5/7 tools: bpf.h: sync with kernel sources

In reply to Kumar Kartikeya Dwivedi

This will be used to expose bpf_link based libbpf API to users.

---
 tools/include/uapi/linux/bpf.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/include/uapi/linux/bpf.h +15 -0
index 2c1ba70abbf1..a3488463d145 100644
@@ -994,6 +994,7 @@ enum bpf_attach_type {
 	BPF_SK_LOOKUP,
 	BPF_XDP,
 	BPF_SK_SKB_VERDICT,
+	BPF_TC,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -1007,6 +1008,7 @@ enum bpf_link_type {
 	BPF_LINK_TYPE_ITER = 4,
 	BPF_LINK_TYPE_NETNS = 5,
 	BPF_LINK_TYPE_XDP = 6,
+	BPF_LINK_TYPE_TC = 7,
 
 	MAX_BPF_LINK_TYPE,
 };
@@ -1447,6 +1449,12 @@ union bpf_attr {
 				__aligned_u64	iter_info;	/* extra bpf_iter_link_info */
 				__u32		iter_info_len;	/* iter_info length */
 			};
+			struct { /* used by BPF_TC */
+				__u32 parent;
+				__u32 handle;
+				__u32 gen_flags;
+				__u16 priority;
+			} tc;
 		};
 	} link_create;
 
@@ -5519,6 +5527,13 @@ struct bpf_link_info {
 		struct {
 			__u32 ifindex;
 		} xdp;
+		struct {
+			__u32 ifindex;
+			__u32 parent;
+			__u32 handle;
+			__u32 gen_flags;
+			__u16 priority;
+		} tc;
 	};
 } __attribute__((aligned(8)));
 
Signature · 1 line
2.31.1
author maintainer
#14 lore raw

PATCH v2 6/7 libbpf: add bpf_link based TC-BPF management API

In reply to Kumar Kartikeya Dwivedi

This adds userspace TC-BPF management API based on bpf_link.

---
 tools/lib/bpf/bpf.c      |  8 +++++-
 tools/lib/bpf/bpf.h      |  8 +++++-
 tools/lib/bpf/libbpf.c   | 59 ++++++++++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.h   | 17 ++++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/netlink.c  |  5 ++--
 tools/lib/bpf/netlink.h  |  8 ++++++
 7 files changed, 100 insertions(+), 6 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
tools/lib/bpf/bpf.c +7 -1
index 86dcac44f32f..bebccea9bfd7 100644
@@ -28,6 +28,7 @@
 #include <asm/unistd.h>
 #include <errno.h>
 #include <linux/bpf.h>
+#include <arpa/inet.h>
 #include "bpf.h"
 #include "libbpf.h"
 #include "libbpf_internal.h"
@@ -693,7 +694,12 @@ int bpf_link_create(int prog_fd, int target_fd,
 	attr.link_create.attach_type = attach_type;
 	attr.link_create.flags = OPTS_GET(opts, flags, 0);
 
-	if (iter_info_len) {
+	if (attach_type == BPF_TC) {
+		attr.link_create.tc.parent = OPTS_GET(opts, tc.parent, 0);
+		attr.link_create.tc.handle = OPTS_GET(opts, tc.handle, 0);
+		attr.link_create.tc.priority = OPTS_GET(opts, tc.priority, 0);
+		attr.link_create.tc.gen_flags = OPTS_GET(opts, tc.gen_flags, 0);
+	} else if (iter_info_len) {
 		attr.link_create.iter_info =
 			ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
 		attr.link_create.iter_info_len = iter_info_len;
tools/lib/bpf/bpf.h +7 -1
index 4f758f8f50cd..f2178309e9ea 100644
@@ -177,8 +177,14 @@ struct bpf_link_create_opts {
 	union bpf_iter_link_info *iter_info;
 	__u32 iter_info_len;
 	__u32 target_btf_id;
+	struct {
+		__u32 parent;
+		__u32 handle;
+		__u32 priority;
+		__u32 gen_flags;
+	} tc;
 };
-#define bpf_link_create_opts__last_field target_btf_id
+#define bpf_link_create_opts__last_field tc.gen_flags
 
 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
 			       enum bpf_attach_type attach_type,
tools/lib/bpf/libbpf.c +57 -2

83 lines not on this page · raw message

tools/lib/bpf/libbpf.h +17 -0
index 6e61342ba56c..284a446c6513 100644
@@ -282,6 +282,23 @@ LIBBPF_API struct bpf_link *
 bpf_program__attach_iter(struct bpf_program *prog,
 			 const struct bpf_iter_attach_opts *opts);
 
+/* TC bpf_link related API */
+struct bpf_tc_hook;
+
+struct bpf_tc_link_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 priority;
+	__u32 gen_flags;
+	size_t :0;
+};
+#define bpf_tc_link_opts__last_field gen_flags
+
+LIBBPF_API struct bpf_link *
+bpf_program__attach_tc(struct bpf_program *prog,
+		       const struct bpf_tc_hook *hook,
+		       const struct bpf_tc_link_opts *opts);
+
 struct bpf_insn;
 
 /*
tools/lib/bpf/libbpf.map +1 -0
index 944c99d1ded3..5aa2e62b9fc2 100644
@@ -373,5 +373,6 @@ LIBBPF_0.5.0 {
 		bpf_map__initial_value;
 		bpf_map_lookup_and_delete_elem_flags;
 		bpf_object__gen_loader;
+		bpf_program__attach_tc;
 		libbpf_set_strict_mode;
 } LIBBPF_0.4.0;
tools/lib/bpf/netlink.c +3 -2
index d743c8721aa7..b7ac36fc9c1a 100644
@@ -17,6 +17,7 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 #include "nlattr.h"
+#include "netlink.h"
 
 #ifndef SOL_NETLINK
 #define SOL_NETLINK 270
@@ -405,8 +406,8 @@ static int attach_point_to_config(struct bpf_tc_hook *hook,
 	}
 }
 
-static int tc_get_tcm_parent(enum bpf_tc_attach_point attach_point,
-			     __u32 *parent)
+int tc_get_tcm_parent(enum bpf_tc_attach_point attach_point,
+		      __u32 *parent)
 {
 	switch (attach_point) {
 	case BPF_TC_INGRESS:
tools/lib/bpf/netlink.h new file +8 -0
new file mode 100644
index 000000000000..c89133d56eb4
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+#pragma once
+
+#include <linux/types.h>
+#include "libbpf.h"
+
+int tc_get_tcm_parent(enum bpf_tc_attach_point attach_point,
+		      __u32 *parent);
Signature · 1 line
2.31.1
#15 lore raw

Re: [PATCH bpf-next v2 6/7] libbpf: add bpf_link based TC-BPF management API

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 294

Quoted 10 lines · Kumar Kartikeya Dwivedi
tools/lib/bpf/libbpf.h +10 -0
+/* TC bpf_link related API */
+struct bpf_tc_hook;
+
+struct bpf_tc_link_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 priority;
+	__u32 gen_flags;
+	size_t :0;
+};

Did you think of a way to share struct bpf_tc_opts with above? Or use bpf_tc_link_opts inside bpf_tc_opts?
Some other way?
gen_flags here and flags there are the same?

author maintainer
#16 lore raw

Re: [PATCH bpf-next v2 6/7] libbpf: add bpf_link based TC-BPF management API

Quoted 14 lines · Alexei Starovoitov

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 294

Quoted 10 lines · Kumar Kartikeya Dwivedi
tools/lib/bpf/libbpf.h +10 -0
+/* TC bpf_link related API */
+struct bpf_tc_hook;
+
+struct bpf_tc_link_opts {
+	size_t sz;
+	__u32 handle;
+	__u32 priority;
+	__u32 gen_flags;
+	size_t :0;
+};

Did you think of a way to share struct bpf_tc_opts with above? Or use bpf_tc_link_opts inside bpf_tc_opts?

A couple of fields in bpf_tc_opts aren't really relevant here (prog_fd, prog_id) and will always be unused, so I thought it would be cleaner to give this its own opts struct. It still reuses the hook abstraction that was added, though.

Quoted 2 lines

Some other way?
gen_flags here and flags there are the same?

No, it was an oversight that I missed adding gen_flags there, I'll send a patch separately with some other assorted things. It's used when offloading to HW.

We don't really support any other flags (e.g. BPF_TC_F_REPLACE) for this.

--
Kartikeya

#17 lore raw

Re: [PATCH bpf-next v2 6/7] libbpf: add bpf_link based TC-BPF management API

Quoted 17 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Alexei Starovoitov

tools/lib/bpf/libbpf.h LIBBPF_API struct bpf_link * · line 294

Quoted 10 lines · Kumar Kartikeya Dwivedi
tools/lib/bpf/libbpf.h +10 -0
+/* TC bpf_link related API */
+struct bpf_tc_hook;
+
+struct bpf_tc_link_opts {
+   size_t sz;
+   __u32 handle;
+   __u32 priority;
+   __u32 gen_flags;
+   size_t :0;
+};

Did you think of a way to share struct bpf_tc_opts with above? Or use bpf_tc_link_opts inside bpf_tc_opts?

Quoted 3 lines

A couple of fields in bpf_tc_opts aren't really relevant here (prog_fd, prog_id) and will always be unused, so I thought it would be cleaner to give this its own opts struct. It still reuses the hook abstraction that was added, though.

Overall probably it will be less confusing to have one _opts struct across both APIs, even if some of the fields are not used. Just enforce that they are always NULL (and document in a comment that for bpf_link-based API it's not expected).

Quoted 11 lines
Quoted 3 lines

Some other way?
gen_flags here and flags there are the same?

No, it was an oversight that I missed adding gen_flags there, I'll send a patch separately with some other assorted things. It's used when offloading to HW.

We don't really support any other flags (e.g. BPF_TC_F_REPLACE) for this.

--
Kartikeya

#18 lore raw

Re: [PATCH bpf-next v2 6/7] libbpf: add bpf_link based TC-BPF management API

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

This adds userspace TC-BPF management API based on bpf_link.

---
  tools/lib/bpf/bpf.c      |  8 +++++-
  tools/lib/bpf/bpf.h      |  8 +++++-
  tools/lib/bpf/libbpf.c   | 59 ++++++++++++++++++++++++++++++++++++++--
  tools/lib/bpf/libbpf.h   | 17 ++++++++++++
  tools/lib/bpf/libbpf.map |  1 +
  tools/lib/bpf/netlink.c  |  5 ++--
  tools/lib/bpf/netlink.h  |  8 ++++++
  7 files changed, 100 insertions(+), 6 deletions(-)

create mode 100644 tools/lib/bpf/netlink.h

tools/lib/bpf/bpf.c +7 -1
index 86dcac44f32f..bebccea9bfd7 100644
@@ -28,6 +28,7 @@
  #include <asm/unistd.h>
  #include <errno.h>
  #include <linux/bpf.h>
+#include <arpa/inet.h>
  #include "bpf.h"
  #include "libbpf.h"
  #include "libbpf_internal.h"
@@ -693,7 +694,12 @@ int bpf_link_create(int prog_fd, int target_fd,
  	attr.link_create.attach_type = attach_type;
  	attr.link_create.flags = OPTS_GET(opts, flags, 0);
 
-	if (iter_info_len) {
+	if (attach_type == BPF_TC) {
+		attr.link_create.tc.parent = OPTS_GET(opts, tc.parent, 0);
+		attr.link_create.tc.handle = OPTS_GET(opts, tc.handle, 0);
+		attr.link_create.tc.priority = OPTS_GET(opts, tc.priority, 0);
+		attr.link_create.tc.gen_flags = OPTS_GET(opts, tc.gen_flags, 0);
+	} else if (iter_info_len) {
  		attr.link_create.iter_info =
  			ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
  		attr.link_create.iter_info_len = iter_info_len;
tools/lib/bpf/bpf.h +7 -1
index 4f758f8f50cd..f2178309e9ea 100644
@@ -177,8 +177,14 @@ struct bpf_link_create_opts {
  	union bpf_iter_link_info *iter_info;
  	__u32 iter_info_len;
  	__u32 target_btf_id;
+	struct {
+		__u32 parent;
+		__u32 handle;
+		__u32 priority;
+		__u32 gen_flags;
+	} tc;
  };
-#define bpf_link_create_opts__last_field target_btf_id
+#define bpf_link_create_opts__last_field tc.gen_flags
 
  LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
  			       enum bpf_attach_type attach_type,
tools/lib/bpf/libbpf.c +6 -2
index 1c4e20e75237..7809536980b1 100644
@@ -55,6 +55,7 @@
  #include "libbpf_internal.h"
  #include "hashmap.h"
  #include "bpf_gen_internal.h"
+#include "netlink.h"
 
  #ifndef BPF_FS_MAGIC
  #define BPF_FS_MAGIC		0xcafe4a11
@@ -7185,7 +7186,7 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
 
  	for (i = 0; i < obj->nr_programs; i++) {
  		struct bpf_program *p = &obj->programs[i];
-		
+
  		if (!p->nr_reloc)
  			continue;
 
@@ -10005,7 +10006,7 @@ struct bpf_link {
  int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
  {
  	int ret;
-	
+
  	ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
  	return libbpf_err_errno(ret);
  }
@@ -10613,6 +10614,60 @@ struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
  	return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
  }
 
+struct bpf_link *bpf_program__attach_tc(struct bpf_program *prog,
+					const struct bpf_tc_hook *hook,
+					const struct bpf_tc_link_opts *opts)

tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex) · line 10629

Quoted 10 lines
tools/lib/bpf/libbpf.c +10 -0
+{
+	DECLARE_LIBBPF_OPTS(bpf_link_create_opts, lopts, 0);
+	char errmsg[STRERR_BUFSIZE];
+	int prog_fd, link_fd, ret;
+	struct bpf_link *link;
+	__u32 parent;
+
+	if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
+	    !OPTS_VALID(opts, bpf_tc_link_opts))
+		return ERR_PTR(-EINVAL);

For libbpf API functions, the libbpf proposed 1.0 change

    https://lore.kernel.org/bpf/[email protected]

will return NULL and set errno properly.

So the above code probably should be

	return errno = EINVAL, NULL;
Quoted 41 lines
tools/lib/bpf/libbpf.c +41 -0
+
+	if (OPTS_GET(hook, ifindex, 0) <= 0 ||
+	    OPTS_GET(opts, priority, 0) > UINT16_MAX)
+		return ERR_PTR(-EINVAL);
+
+	parent = OPTS_GET(hook, parent, 0);
+
+	ret = tc_get_tcm_parent(OPTS_GET(hook, attach_point, 0),
+				&parent);
+	if (ret < 0)
+		return ERR_PTR(ret);
+
+	lopts.tc.parent = parent;
+	lopts.tc.handle = OPTS_GET(opts, handle, 0);
+	lopts.tc.priority = OPTS_GET(opts, priority, 0);
+	lopts.tc.gen_flags = OPTS_GET(opts, gen_flags, 0);
+
+	prog_fd = bpf_program__fd(prog);
+	if (prog_fd < 0) {
+		pr_warn("prog '%s': can't attach before loaded\n", prog->name);
+		return ERR_PTR(-EINVAL);
+	}
+
+	link = calloc(1, sizeof(*link));
+	if (!link)
+		return ERR_PTR(-ENOMEM);
+	link->detach = &bpf_link__detach_fd;
+
+	link_fd = bpf_link_create(prog_fd, OPTS_GET(hook, ifindex, 0), BPF_TC, &lopts);
+	if (link_fd < 0) {
+		link_fd = -errno;
+		free(link);
+		pr_warn("prog '%s': failed to attach tc filter: %s\n",
+			prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
+		return ERR_PTR(link_fd);
+	}
+	link->fd = link_fd;
+
+	return link;
+}
+

[...]

#19 lore raw

Re: [PATCH bpf-next v2 6/7] libbpf: add bpf_link based TC-BPF management API

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

This adds userspace TC-BPF management API based on bpf_link.

---
 tools/lib/bpf/bpf.c      |  8 +++++-
 tools/lib/bpf/bpf.h      |  8 +++++-
 tools/lib/bpf/libbpf.c   | 59 ++++++++++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.h   | 17 ++++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 tools/lib/bpf/netlink.c  |  5 ++--
 tools/lib/bpf/netlink.h  |  8 ++++++
 7 files changed, 100 insertions(+), 6 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h

[...]

tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex) · line 10664

Quoted 12 lines
tools/lib/bpf/libbpf.c +12 -0
+       link = calloc(1, sizeof(*link));
+       if (!link)
+               return ERR_PTR(-ENOMEM);
+       link->detach = &bpf_link__detach_fd;
+
+       link_fd = bpf_link_create(prog_fd, OPTS_GET(hook, ifindex, 0), BPF_TC, &lopts);
+       if (link_fd < 0) {
+               link_fd = -errno;
+               free(link);
+               pr_warn("prog '%s': failed to attach tc filter: %s\n",
+                       prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
+               return ERR_PTR(link_fd);

like Yonghong mentioned, all these error returns have to be either return libbpf_err_ptr(err); or, given it's a new API, we can do just direct return errno = err, NULL;. I'd probably use libbpf_err_ptr() for now for consistency.

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
+       }
+       link->fd = link_fd;
+
+       return link;
+}
+
 struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
                                              int target_fd,
                                              const char *attach_func_name)

[...]

tools/lib/bpf/netlink.h line 2

Quoted 8 lines
tools/lib/bpf/netlink.h new file +2 -0
new file mode 100644
index 000000000000..c89133d56eb4
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+#pragma once

we don't use "#pragma once" in libbpf. And I saw some kernel discussions discouraging its use. So please just stick to the classic #ifdef/#define/#endif combo.

Quoted 9 lines
tools/lib/bpf/netlink.h new file +6 -1
+
+#include <linux/types.h>
+#include "libbpf.h"
+
+int tc_get_tcm_parent(enum bpf_tc_attach_point attach_point,
+                     __u32 *parent);
--

2.31.1

author maintainer
#20 lore raw

PATCH v2 7/7 libbpf: add selftest for bpf_link based TC-BPF management API

In reply to Kumar Kartikeya Dwivedi

This covers basic attach/detach/update, and tests interaction with the netlink API. It also exercises the bpf_link_info and fdinfo codepaths.

---
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 ++++++++++++++++++
 1 file changed, 285 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +285 -0
new file mode 100644
index 000000000000..beaf06e0557c
@@ -0,0 +1,285 @@
+// 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 int test_tc_bpf_link_basic(struct bpf_tc_hook *hook,
+				  struct bpf_program *prog)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+	DECLARE_LIBBPF_OPTS(bpf_tc_opts, qopts, .handle = 1, .priority = 1);
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	struct bpf_link *link, *invl;
+	int ret;
+
+	link = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
+		return PTR_ERR(link);
+
+	ret = bpf_obj_get_info_by_fd(bpf_program__fd(prog), &info, &info_len);
+	if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd"))
+		goto end;
+
+	ret = bpf_tc_query(hook, &qopts);
+	if (!ASSERT_OK(ret, "bpf_tc_query"))
+		goto end;
+
+	if (!ASSERT_EQ(qopts.prog_id, info.id, "prog_id match"))
+		goto end;
+
+	opts.gen_flags = ~0u;
+	invl = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_ERR_PTR(invl, "bpf_program__attach_tc with invalid flags")) {
+		bpf_link__destroy(invl);
+		ret = -EINVAL;
+	}
+
+end:
+	bpf_link__destroy(link);
+	return ret;
+}
+
+static int test_tc_bpf_link_netlink_interaction(struct bpf_tc_hook *hook,
+						struct bpf_program *prog)
+{
+	DECLARE_LIBBPF_OPTS(bpf_link_update_opts, lopts,
+			    .old_prog_fd = bpf_program__fd(prog));
+	DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+	DECLARE_LIBBPF_OPTS(bpf_tc_opts, nopts, .handle = 1, .priority = 1);
+	DECLARE_LIBBPF_OPTS(bpf_tc_opts, dopts, .handle = 1, .priority = 1);
+	struct bpf_link *link;
+	int ret;
+
+	/* We need to test the following cases:
+	 *	1. BPF link owned filter cannot be replaced by netlink
+	 *	2. Netlink owned filter cannot be replaced by BPF link
+	 *	3. Netlink cannot do targeted delete of BPF link owned filter
+	 *	4. Filter is actually deleted (with chain cleanup)
+	 *	   We actually (ab)use the kernel behavior of returning EINVAL when
+	 *	   target chain doesn't exist on tc_get_tfilter (which maps to
+	 *	   bpf_tc_query) here, to know if the chain was really cleaned
+	 *	   up on tcf_proto destruction. Our setup is so that there is
+	 *	   only one reference to the chain.
+	 *
+	 *	   So on query, chain ? (filter ?: ENOENT) : EINVAL
+	 */
+
+	link = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
+		return PTR_ERR(link);
+
+	nopts.prog_fd = bpf_program__fd(prog);
+	ret = bpf_tc_attach(hook, &nopts);
+	if (!ASSERT_EQ(ret, -EEXIST, "bpf_tc_attach without replace"))
+		goto end;
+
+	nopts.flags = BPF_TC_F_REPLACE;
+	ret = bpf_tc_attach(hook, &nopts);
+	if (!ASSERT_EQ(ret, -EPERM, "bpf_tc_attach with replace"))
+		goto end;
+
+	ret = bpf_tc_detach(hook, &dopts);
+	if (!ASSERT_EQ(ret, -EPERM, "bpf_tc_detach"))
+		goto end;
+
+	lopts.flags = BPF_F_REPLACE;
+	ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+			      &lopts);
+	ASSERT_OK(ret, "bpf_link_update");
+	ret = ret < 0 ? -errno : ret;
+
+end:
+	bpf_link__destroy(link);
+	if (!ret && !ASSERT_EQ(bpf_tc_query(hook, &dopts), -EINVAL,
+			       "chain empty delete"))
+		ret = -EINVAL;
+	return ret;
+}
+
+static int test_tc_bpf_link_update_ways(struct bpf_tc_hook *hook,
+					struct bpf_program *prog)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+	DECLARE_LIBBPF_OPTS(bpf_link_update_opts, uopts, 0);
+	struct test_tc_bpf *skel;
+	struct bpf_link *link;
+	int ret;
+
+	skel = test_tc_bpf__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
+		return PTR_ERR(skel);
+
+	link = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc")) {
+		ret = PTR_ERR(link);
+		goto end;
+	}
+
+	ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+			      &uopts);
+	if (!ASSERT_OK(ret, "bpf_link_update no old prog"))
+		goto end;
+
+	uopts.old_prog_fd = bpf_program__fd(prog);
+	ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+			      &uopts);
+	if (!ASSERT_TRUE(ret < 0 && errno == EINVAL,
+			 "bpf_link_update with old prog without BPF_F_REPLACE")) {
+		ret = -EINVAL;
+		goto end;
+	}
+
+	uopts.flags = BPF_F_REPLACE;
+	ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+			      &uopts);
+	if (!ASSERT_OK(ret, "bpf_link_update with old prog with BPF_F_REPLACE"))
+		goto end;
+
+	uopts.old_prog_fd = bpf_program__fd(skel->progs.cls);
+	ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+			      &uopts);
+	if (!ASSERT_TRUE(ret < 0 && errno == EINVAL,
+			 "bpf_link_update with wrong old prog")) {
+		ret = -EINVAL;
+		goto end;
+	}
+	ret = 0;
+
+end:
+	test_tc_bpf__destroy(skel);
+	return ret;
+}
+
+static int test_tc_bpf_link_info_api(struct bpf_tc_hook *hook,
+				     struct bpf_program *prog)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+	__u32 ifindex, parent, handle, gen_flags, priority;
+	char buf[4096], path[256], *begin;
+	struct bpf_link_info info = {};
+	__u32 info_len = sizeof(info);
+	struct bpf_link *link;
+	int ret, fdinfo;
+
+	link = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
+		return PTR_ERR(link);
+
+	ret = bpf_obj_get_info_by_fd(bpf_link__fd(link), &info, &info_len);
+	if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd"))
+		goto end;
+
+	ret = snprintf(path, sizeof(path), "/proc/self/fdinfo/%d",
+		       bpf_link__fd(link));
+	if (!ASSERT_TRUE(!ret || ret < sizeof(path), "snprintf pathname"))
+		goto end;
+
+	fdinfo = open(path, O_RDONLY);
+	if (!ASSERT_GT(fdinfo, -1, "open fdinfo"))
+		goto end;
+
+	ret = read(fdinfo, buf, sizeof(buf));
+	if (!ASSERT_GT(ret, 0, "read fdinfo")) {
+		ret = -EINVAL;
+		goto end_file;
+	}
+
+	begin = strstr(buf, "ifindex");
+	if (!ASSERT_OK_PTR(begin, "find beginning of fdinfo info")) {
+		ret = -EINVAL;
+		goto end_file;
+	}
+
+	ret = sscanf(begin, "ifindex:\t%u\n"
+			    "parent:\t%u\n"
+			    "handle:\t%u\n"
+			    "priority:\t%u\n"
+			    "gen_flags:\t%u\n",
+			    &ifindex, &parent, &handle, &priority, &gen_flags);
+	if (!ASSERT_EQ(ret, 5, "sscanf fdinfo")) {
+		ret = -EINVAL;
+		goto end_file;
+	}
+
+	ret = -EINVAL;
+
+#define X(a, b, c) (!ASSERT_EQ(a, b, #a " == " #b) || !ASSERT_EQ(b, c, #b " == " #c))
+	if (X(info.tc.ifindex, ifindex, 1) ||
+	    X(info.tc.parent, parent,
+	      TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)) ||
+	    X(info.tc.handle, handle, 1) ||
+	    X(info.tc.gen_flags, gen_flags, TCA_CLS_FLAGS_NOT_IN_HW) ||
+	    X(info.tc.priority, priority, 1))
+#undef X
+		goto end_file;
+
+	ret = 0;
+
+end_file:
+	close(fdinfo);
+end:
+	bpf_link__destroy(link);
+	return ret;
+}
+
+void test_tc_bpf_link(void)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
+			    .attach_point = BPF_TC_INGRESS);
+	struct test_tc_bpf *skel = NULL;
+	bool hook_created = false;
+	int ret;
+
+	skel = test_tc_bpf__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
+		return;
+
+	ret = bpf_tc_hook_create(&hook);
+	if (ret == 0)
+		hook_created = true;
+
+	ret = ret == -EEXIST ? 0 : ret;
+	if (!ASSERT_OK(ret, "bpf_tc_hook_create(BPF_TC_INGRESS)"))
+		goto end;
+
+	ret = test_tc_bpf_link_basic(&hook, skel->progs.cls);
+	if (!ASSERT_OK(ret, "test_tc_bpf_link_basic"))
+		goto end;
+
+	bpf_tc_hook_destroy(&hook);
+
+	hook.attach_point = BPF_TC_EGRESS;
+	ret = test_tc_bpf_link_basic(&hook, skel->progs.cls);
+	if (!ASSERT_OK(ret, "test_tc_bpf_link_basic"))
+		goto end;
+
+	bpf_tc_hook_destroy(&hook);
+
+	ret = test_tc_bpf_link_netlink_interaction(&hook, skel->progs.cls);
+	if (!ASSERT_OK(ret, "test_tc_bpf_link_netlink_interaction"))
+		goto end;
+
+	bpf_tc_hook_destroy(&hook);
+
+	ret = test_tc_bpf_link_update_ways(&hook, skel->progs.cls);
+	if (!ASSERT_OK(ret, "test_tc_bpf_link_update_ways"))
+		goto end;
+
+	bpf_tc_hook_destroy(&hook);
+
+	ret = test_tc_bpf_link_info_api(&hook, skel->progs.cls);
+	if (!ASSERT_OK(ret, "test_tc_bpf_link_info_api"))
+		goto end;
+
+end:
+	if (hook_created) {
+		hook.attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS;
+		bpf_tc_hook_destroy(&hook);
+	}
+	test_tc_bpf__destroy(skel);
+}
Signature · 1 line
2.31.1
#21 lore raw

Re: [PATCH bpf-next v2 7/7] libbpf: add selftest for bpf_link based TC-BPF management API

Quoted 28 lines · Kumar Kartikeya Dwivedi

This covers basic attach/detach/update, and tests interaction with the netlink API. It also exercises the bpf_link_info and fdinfo codepaths.

---
  .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 ++++++++++++++++++
  1 file changed, 285 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +12 -0
new file mode 100644
index 000000000000..beaf06e0557c
@@ -0,0 +1,285 @@
+// 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 int test_tc_bpf_link_basic(struct bpf_tc_hook *hook,
+				  struct bpf_program *prog)
+{

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 22

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+	DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+	DECLARE_LIBBPF_OPTS(bpf_tc_opts, qopts, .handle = 1, .priority = 1);
+	struct bpf_prog_info info = {};
+	__u32 info_len = sizeof(info);
+	struct bpf_link *link, *invl;
+	int ret;
+
+	link = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
+		return PTR_ERR(link);

If we changed the bpf_program__attach_tc return semantics such that the return "link" value can only be NULL or a valid pointer, you can do

	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
		return -EINVAL;

The true error code should have been printed inside ASSERT_OK_PTR.

The same for a few other cases below.

Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +24 -0
+
+	ret = bpf_obj_get_info_by_fd(bpf_program__fd(prog), &info, &info_len);
+	if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd"))
+		goto end;
+
+	ret = bpf_tc_query(hook, &qopts);
+	if (!ASSERT_OK(ret, "bpf_tc_query"))
+		goto end;
+
+	if (!ASSERT_EQ(qopts.prog_id, info.id, "prog_id match"))
+		goto end;
+
+	opts.gen_flags = ~0u;
+	invl = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_ERR_PTR(invl, "bpf_program__attach_tc with invalid flags")) {
+		bpf_link__destroy(invl);
+		ret = -EINVAL;
+	}
+
+end:
+	bpf_link__destroy(link);
+	return ret;
+}
+

[...]

Quoted 53 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +53 -0
+
+static int test_tc_bpf_link_info_api(struct bpf_tc_hook *hook,
+				     struct bpf_program *prog)
+{
+	DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+	__u32 ifindex, parent, handle, gen_flags, priority;
+	char buf[4096], path[256], *begin;
+	struct bpf_link_info info = {};
+	__u32 info_len = sizeof(info);
+	struct bpf_link *link;
+	int ret, fdinfo;
+
+	link = bpf_program__attach_tc(prog, hook, &opts);
+	if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
+		return PTR_ERR(link);
+
+	ret = bpf_obj_get_info_by_fd(bpf_link__fd(link), &info, &info_len);
+	if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd"))
+		goto end;
+
+	ret = snprintf(path, sizeof(path), "/proc/self/fdinfo/%d",
+		       bpf_link__fd(link));
+	if (!ASSERT_TRUE(!ret || ret < sizeof(path), "snprintf pathname"))
+		goto end;
+
+	fdinfo = open(path, O_RDONLY);
+	if (!ASSERT_GT(fdinfo, -1, "open fdinfo"))
+		goto end;
+
+	ret = read(fdinfo, buf, sizeof(buf));
+	if (!ASSERT_GT(ret, 0, "read fdinfo")) {
+		ret = -EINVAL;
+		goto end_file;
+	}
+
+	begin = strstr(buf, "ifindex");
+	if (!ASSERT_OK_PTR(begin, "find beginning of fdinfo info")) {
+		ret = -EINVAL;
+		goto end_file;
+	}
+
+	ret = sscanf(begin, "ifindex:\t%u\n"
+			    "parent:\t%u\n"
+			    "handle:\t%u\n"
+			    "priority:\t%u\n"
+			    "gen_flags:\t%u\n",
+			    &ifindex, &parent, &handle, &priority, &gen_flags);
+	if (!ASSERT_EQ(ret, 5, "sscanf fdinfo")) {
+		ret = -EINVAL;
+		goto end_file;
+	}
+
+	ret = -EINVAL;

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 219

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+
+#define X(a, b, c) (!ASSERT_EQ(a, b, #a " == " #b) || !ASSERT_EQ(b, c, #b " == " #c))
+	if (X(info.tc.ifindex, ifindex, 1) ||
+	    X(info.tc.parent, parent,
+	      TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)) ||
+	    X(info.tc.handle, handle, 1) ||
+	    X(info.tc.gen_flags, gen_flags, TCA_CLS_FLAGS_NOT_IN_HW) ||
+	    X(info.tc.priority, priority, 1))
+#undef X
+		goto end_file;

Maybe put "#undef X" after "goto end_file" is a little bit better?

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+
+	ret = 0;
+
+end_file:
+	close(fdinfo);
+end:
+	bpf_link__destroy(link);
+	return ret;
+}
+

[...]

#22 lore raw

Re: [PATCH bpf-next v2 7/7] libbpf: add selftest for bpf_link based TC-BPF management API

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

This covers basic attach/detach/update, and tests interaction with the netlink API. It also exercises the bpf_link_info and fdinfo codepaths.

---
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 ++++++++++++++++++
 1 file changed, 285 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

[...]

Quoted 38 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +38 -0
+static int test_tc_bpf_link_netlink_interaction(struct bpf_tc_hook *hook,
+                                               struct bpf_program *prog)
+{
+       DECLARE_LIBBPF_OPTS(bpf_link_update_opts, lopts,
+                           .old_prog_fd = bpf_program__fd(prog));
+       DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+       DECLARE_LIBBPF_OPTS(bpf_tc_opts, nopts, .handle = 1, .priority = 1);
+       DECLARE_LIBBPF_OPTS(bpf_tc_opts, dopts, .handle = 1, .priority = 1);
+       struct bpf_link *link;
+       int ret;
+
+       /* We need to test the following cases:
+        *      1. BPF link owned filter cannot be replaced by netlink
+        *      2. Netlink owned filter cannot be replaced by BPF link
+        *      3. Netlink cannot do targeted delete of BPF link owned filter
+        *      4. Filter is actually deleted (with chain cleanup)
+        *         We actually (ab)use the kernel behavior of returning EINVAL when
+        *         target chain doesn't exist on tc_get_tfilter (which maps to
+        *         bpf_tc_query) here, to know if the chain was really cleaned
+        *         up on tcf_proto destruction. Our setup is so that there is
+        *         only one reference to the chain.
+        *
+        *         So on query, chain ? (filter ?: ENOENT) : EINVAL
+        */
+
+       link = bpf_program__attach_tc(prog, hook, &opts);
+       if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))
+               return PTR_ERR(link);
+
+       nopts.prog_fd = bpf_program__fd(prog);
+       ret = bpf_tc_attach(hook, &nopts);
+       if (!ASSERT_EQ(ret, -EEXIST, "bpf_tc_attach without replace"))
+               goto end;
+
+       nopts.flags = BPF_TC_F_REPLACE;
+       ret = bpf_tc_attach(hook, &nopts);
+       if (!ASSERT_EQ(ret, -EPERM, "bpf_tc_attach with replace"))
+               goto end;

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 94

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+
+       ret = bpf_tc_detach(hook, &dopts);
+       if (!ASSERT_EQ(ret, -EPERM, "bpf_tc_detach"))
+               goto end;
+
+       lopts.flags = BPF_F_REPLACE;
+       ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+                             &lopts);
+       ASSERT_OK(ret, "bpf_link_update");
+       ret = ret < 0 ? -errno : ret;

all selftests run in libbpf 1.0 mode, so you get actual error directly, so no need to deal with -errno here.

Quoted 26 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +26 -0
+
+end:
+       bpf_link__destroy(link);
+       if (!ret && !ASSERT_EQ(bpf_tc_query(hook, &dopts), -EINVAL,
+                              "chain empty delete"))
+               ret = -EINVAL;
+       return ret;
+}
+
+static int test_tc_bpf_link_update_ways(struct bpf_tc_hook *hook,
+                                       struct bpf_program *prog)
+{
+       DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+       DECLARE_LIBBPF_OPTS(bpf_link_update_opts, uopts, 0);
+       struct test_tc_bpf *skel;
+       struct bpf_link *link;
+       int ret;
+
+       skel = test_tc_bpf__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
+               return PTR_ERR(skel);
+
+       link = bpf_program__attach_tc(prog, hook, &opts);
+       if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc")) {
+               ret = PTR_ERR(link);
+               goto end;

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 130

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+       }
+
+       ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+                             &uopts);
+       if (!ASSERT_OK(ret, "bpf_link_update no old prog"))
+               goto end;
+
+       uopts.old_prog_fd = bpf_program__fd(prog);
+       ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+                             &uopts);

please keep all such calls single-line, they aren't excessively long at all

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 132

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +2 -0
+       if (!ASSERT_TRUE(ret < 0 && errno == EINVAL,
+                        "bpf_link_update with old prog without BPF_F_REPLACE")) {

same as above, ret should already be -EINVAL, so just check directly

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +5 -0
+               ret = -EINVAL;
+               goto end;
+       }
+
+       uopts.flags = BPF_F_REPLACE;

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 147

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+       ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+                             &uopts);
+       if (!ASSERT_OK(ret, "bpf_link_update with old prog with BPF_F_REPLACE"))
+               goto end;
+
+       uopts.old_prog_fd = bpf_program__fd(skel->progs.cls);
+       ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog),
+                             &uopts);
+       if (!ASSERT_TRUE(ret < 0 && errno == EINVAL,
+                        "bpf_link_update with wrong old prog")) {

and here

Quoted 23 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +23 -0
+               ret = -EINVAL;
+               goto end;
+       }
+       ret = 0;
+
+end:
+       test_tc_bpf__destroy(skel);
+       return ret;
+}
+
+static int test_tc_bpf_link_info_api(struct bpf_tc_hook *hook,
+                                    struct bpf_program *prog)
+{
+       DECLARE_LIBBPF_OPTS(bpf_tc_link_opts, opts, .handle = 1, .priority = 1);
+       __u32 ifindex, parent, handle, gen_flags, priority;
+       char buf[4096], path[256], *begin;
+       struct bpf_link_info info = {};
+       __u32 info_len = sizeof(info);
+       struct bpf_link *link;
+       int ret, fdinfo;
+
+       link = bpf_program__attach_tc(prog, hook, &opts);
+       if (!ASSERT_OK_PTR(link, "bpf_program__attach_tc"))

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 180

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+               return PTR_ERR(link);
+
+       ret = bpf_obj_get_info_by_fd(bpf_link__fd(link), &info, &info_len);
+       if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd"))
+               goto end;
+
+       ret = snprintf(path, sizeof(path), "/proc/self/fdinfo/%d",
+                      bpf_link__fd(link));
+       if (!ASSERT_TRUE(!ret || ret < sizeof(path), "snprintf pathname"))
+               goto end;

ASSERT_TRUE is very generic, it's better to do ASSERT_LT(ret, sizeof(path), "snprintf") here

not sure why !ret is allowed?..

Quoted 28 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +28 -0
+
+       fdinfo = open(path, O_RDONLY);
+       if (!ASSERT_GT(fdinfo, -1, "open fdinfo"))
+               goto end;
+
+       ret = read(fdinfo, buf, sizeof(buf));
+       if (!ASSERT_GT(ret, 0, "read fdinfo")) {
+               ret = -EINVAL;
+               goto end_file;
+       }
+
+       begin = strstr(buf, "ifindex");
+       if (!ASSERT_OK_PTR(begin, "find beginning of fdinfo info")) {
+               ret = -EINVAL;
+               goto end_file;
+       }
+
+       ret = sscanf(begin, "ifindex:\t%u\n"
+                           "parent:\t%u\n"
+                           "handle:\t%u\n"
+                           "priority:\t%u\n"
+                           "gen_flags:\t%u\n",
+                           &ifindex, &parent, &handle, &priority, &gen_flags);
+       if (!ASSERT_EQ(ret, 5, "sscanf fdinfo")) {
+               ret = -EINVAL;
+               goto end_file;
+       }
+

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 218

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+       ret = -EINVAL;
+
+#define X(a, b, c) (!ASSERT_EQ(a, b, #a " == " #b) || !ASSERT_EQ(b, c, #b " == " #c))
+       if (X(info.tc.ifindex, ifindex, 1) ||
+           X(info.tc.parent, parent,
+             TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)) ||
+           X(info.tc.handle, handle, 1) ||
+           X(info.tc.gen_flags, gen_flags, TCA_CLS_FLAGS_NOT_IN_HW) ||
+           X(info.tc.priority, priority, 1))
+#undef X

This seems to be a bit too convoluted and over-engineered. Just validate all the equalities unconditionally.

ASSERT_EQ(info.tc.ifindex, 1, "info.tc.ifindex"); ASSERT_EQ(ifindex, 1, "fdinfo.ifindex"); ASSERT_EQ(info.tc.parent, TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS), "info.tc.parent");

and so on.

Then, you don't really need to propagate errors from test_tc_bpf_link_info_api, because each ASSERT_EQ() marks the test (or subtest) as failed, so you don't have to do that below in test_tc_bpf_link.

Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +48 -0
+               goto end_file;
+
+       ret = 0;
+
+end_file:
+       close(fdinfo);
+end:
+       bpf_link__destroy(link);
+       return ret;
+}
+
+void test_tc_bpf_link(void)
+{
+       DECLARE_LIBBPF_OPTS(bpf_tc_hook, hook, .ifindex = LO_IFINDEX,
+                           .attach_point = BPF_TC_INGRESS);
+       struct test_tc_bpf *skel = NULL;
+       bool hook_created = false;
+       int ret;
+
+       skel = test_tc_bpf__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "test_tc_bpf__open_and_load"))
+               return;
+
+       ret = bpf_tc_hook_create(&hook);
+       if (ret == 0)
+               hook_created = true;
+
+       ret = ret == -EEXIST ? 0 : ret;
+       if (!ASSERT_OK(ret, "bpf_tc_hook_create(BPF_TC_INGRESS)"))
+               goto end;
+
+       ret = test_tc_bpf_link_basic(&hook, skel->progs.cls);
+       if (!ASSERT_OK(ret, "test_tc_bpf_link_basic"))
+               goto end;
+
+       bpf_tc_hook_destroy(&hook);
+
+       hook.attach_point = BPF_TC_EGRESS;
+       ret = test_tc_bpf_link_basic(&hook, skel->progs.cls);
+       if (!ASSERT_OK(ret, "test_tc_bpf_link_basic"))
+               goto end;
+
+       bpf_tc_hook_destroy(&hook);
+
+       ret = test_tc_bpf_link_netlink_interaction(&hook, skel->progs.cls);
+       if (!ASSERT_OK(ret, "test_tc_bpf_link_netlink_interaction"))
+               goto end;
+

tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c line 276

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +10 -0
+       bpf_tc_hook_destroy(&hook);
+
+       ret = test_tc_bpf_link_update_ways(&hook, skel->progs.cls);
+       if (!ASSERT_OK(ret, "test_tc_bpf_link_update_ways"))
+               goto end;
+
+       bpf_tc_hook_destroy(&hook);
+
+       ret = test_tc_bpf_link_info_api(&hook, skel->progs.cls);
+       if (!ASSERT_OK(ret, "test_tc_bpf_link_info_api"))

I was talking about this above, it's completely unnecessary and redundant. Just complicates everything.

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c new file +9 -1
+               goto end;
+
+end:
+       if (hook_created) {
+               hook.attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS;
+               bpf_tc_hook_destroy(&hook);
+       }
+       test_tc_bpf__destroy(skel);
+}
--

2.31.1

#23 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

The reason I'm asking is because there are a few networking teams within Meta that have been requesting this feature :)

Thanks,
Joanne

Quoted 43 lines

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

* Avoid overwriting other members of union in bpf_attr (Andrii)
* Set link to NULL after bpf_link_cleanup to avoid double free (Andrii)
* Use __be16 to store the result of htons (Kernel Test Robot)
* Make assignment of tcf_exts::net conditional on CONFIG_NET_CLS_ACT (Kernel Test Robot)

Kumar Kartikeya Dwivedi (7):
net: sched: refactor cls_bpf creation code
bpf: export bpf_link functions for modules
net: sched: add bpf_link API for bpf classifier
net: sched: add lightweight update path for cls_bpf
tools: bpf.h: sync with kernel sources
libbpf: add bpf_link based TC-BPF management API
libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 139 ++++++-
 net/sched/cls_bpf.c                           | 389 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   8 +-
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 940 insertions(+), 45 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

--
2.31.1

author maintainer
#24 lore raw
Quoted 94 lines · Joanne Koong
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Quoted 7 lines

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

[0]: https://lore.kernel.org/bpf/[email protected]

Quoted 49 lines

The reason I'm asking is because there are a few networking teams within Meta that have been requesting this feature :)

Thanks,
Joanne

Quoted 43 lines

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

* Avoid overwriting other members of union in bpf_attr (Andrii)
* Set link to NULL after bpf_link_cleanup to avoid double free (Andrii)
* Use __be16 to store the result of htons (Kernel Test Robot)
* Make assignment of tcf_exts::net conditional on CONFIG_NET_CLS_ACT (Kernel Test Robot)

Kumar Kartikeya Dwivedi (7):
net: sched: refactor cls_bpf creation code
bpf: export bpf_link functions for modules
net: sched: add bpf_link API for bpf classifier
net: sched: add lightweight update path for cls_bpf
tools: bpf.h: sync with kernel sources
libbpf: add bpf_link based TC-BPF management API
libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 139 ++++++-
 net/sched/cls_bpf.c                           | 389 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   8 +-
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 940 insertions(+), 45 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

--
2.31.1

--
Kartikeya

#25 lore raw
Quoted 104 lines · Kumar Kartikeya Dwivedi
Quoted 94 lines · Joanne Koong
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Quoted 7 lines

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

Quoted 55 lines

[0]: https://lore.kernel.org/bpf/[email protected]

Quoted 49 lines

The reason I'm asking is because there are a few networking teams within Meta that have been requesting this feature :)

Thanks,
Joanne

Quoted 43 lines

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

* Avoid overwriting other members of union in bpf_attr (Andrii)
* Set link to NULL after bpf_link_cleanup to avoid double free (Andrii)
* Use __be16 to store the result of htons (Kernel Test Robot)
* Make assignment of tcf_exts::net conditional on CONFIG_NET_CLS_ACT (Kernel Test Robot)

Kumar Kartikeya Dwivedi (7):
net: sched: refactor cls_bpf creation code
bpf: export bpf_link functions for modules
net: sched: add bpf_link API for bpf classifier
net: sched: add lightweight update path for cls_bpf
tools: bpf.h: sync with kernel sources
libbpf: add bpf_link based TC-BPF management API
libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 139 ++++++-
 net/sched/cls_bpf.c                           | 389 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   8 +-
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 940 insertions(+), 45 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

--
2.31.1

--
Kartikeya

#26 lore raw
Quoted 107 lines · Joanne Koong
Quoted 104 lines · Kumar Kartikeya Dwivedi
Quoted 101 lines · Joanne Koong
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

Quoted 8 lines
Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

After reading the linked thread (which I should have done before submitting my previous reply :)), if I'm understanding it correctly, it seems then that the work needed for tc bpf_link will be in a new direction that's not based on the code in this v2 patchset. I'm interested in learning more about bpf link and tc - I can pick this up to work on. But if this was something you wanted to work on though, please don't hesitate to let me know; I can find some other bpf link thing to work on instead if that's the case.

Quoted 55 lines
Quoted 55 lines

[0]: https://lore.kernel.org/bpf/[email protected]

Quoted 49 lines

The reason I'm asking is because there are a few networking teams within Meta that have been requesting this feature :)

Thanks,
Joanne

Quoted 43 lines

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

* Avoid overwriting other members of union in bpf_attr (Andrii)
* Set link to NULL after bpf_link_cleanup to avoid double free (Andrii)
* Use __be16 to store the result of htons (Kernel Test Robot)
* Make assignment of tcf_exts::net conditional on CONFIG_NET_CLS_ACT (Kernel Test Robot)

Kumar Kartikeya Dwivedi (7):
net: sched: refactor cls_bpf creation code
bpf: export bpf_link functions for modules
net: sched: add bpf_link API for bpf classifier
net: sched: add lightweight update path for cls_bpf
tools: bpf.h: sync with kernel sources
libbpf: add bpf_link based TC-BPF management API
libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 139 ++++++-
 net/sched/cls_bpf.c                           | 389 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   8 +-
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 940 insertions(+), 45 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c

--
2.31.1

--
Kartikeya

author maintainer
#27 lore raw
Quoted 116 lines · Joanne Koong
Quoted 107 lines · Joanne Koong
Quoted 104 lines · Kumar Kartikeya Dwivedi
Quoted 101 lines · Joanne Koong
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

Quoted 8 lines
Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

Quoted 9 lines

After reading the linked thread (which I should have done before submitting my previous reply :)), if I'm understanding it correctly, it seems then that the work needed for tc bpf_link will be in a new direction that's not based on the code in this v2 patchset. I'm interested in learning more about bpf link and tc - I can pick this up to work on. But if this was something you wanted to work on though, please don't hesitate to let me know; I can find some other bpf link thing to work on instead if that's the case.

Feel free to take it. And yes, it's going to be much simpler than this. I think you can just add two bpf_prog pointers in struct net_device, use rtnl_lock to protect the updates, and invoke using bpf_prog_run in sch_handle_ingress and sch_handle_egress. You could also split the old and new path so that there are less branches when the ingress/egress static key is enabled only for bpf_link mode (as clsact will become redundant after this). It should be similar to how XDP's bpf_link works. Auto detach can be handled similarly by unlinking dev from the link when net_device teardown occurs, same as XDP's bpf_link, to sever it.

The other thing to keep in mind is that not all return codes are permitted in direct action mode, so you may want to lift cls_bpf_exec_opcode out into a header as a helper and then repurpose it for the switch statement in sch_handle_ingress/egress to handle only those. Also need to document for the user that only direct action mode is supported for TC bpf_link mode.

You can look at the cls_bpf_classify function in net/sched/cls_bpf.c. The branches protected by prog->exts_integrated is for direct action mode, so the handling for bpf_link will be similar. Also need to handle mono_delivery_time unsetting that Martin added recently.

That's all from my notes from that time. Best of luck! :)

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines

[0]: https://lore.kernel.org/bpf/[email protected]

Quoted 46 lines

The reason I'm asking is because there are a few networking teams within Meta that have been requesting this feature :)

Thanks,
Joanne

Quoted 40 lines

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

* Avoid overwriting other members of union in bpf_attr (Andrii)
* Set link to NULL after bpf_link_cleanup to avoid double free (Andrii)
* Use __be16 to store the result of htons (Kernel Test Robot)
* Make assignment of tcf_exts::net conditional on CONFIG_NET_CLS_ACT (Kernel Test Robot)

Kumar Kartikeya Dwivedi (7):
net: sched: refactor cls_bpf creation code
bpf: export bpf_link functions for modules
net: sched: add bpf_link API for bpf classifier
net: sched: add lightweight update path for cls_bpf
tools: bpf.h: sync with kernel sources
libbpf: add bpf_link based TC-BPF management API
libbpf: add selftest for bpf_link based TC-BPF management API

 include/linux/bpf_types.h                     |   3 +
 include/net/pkt_cls.h                         |  13 +
 include/net/sch_generic.h                     |   6 +-
 include/uapi/linux/bpf.h                      |  15 +
 kernel/bpf/syscall.c                          |  14 +-
 net/sched/cls_api.c                           | 139 ++++++-
 net/sched/cls_bpf.c                           | 389 ++++++++++++++++--
 tools/include/uapi/linux/bpf.h                |  15 +
 tools/lib/bpf/bpf.c                           |   8 +-
 tools/lib/bpf/bpf.h                           |   8 +-
 tools/lib/bpf/libbpf.c                        |  59 ++-
 tools/lib/bpf/libbpf.h                        |  17 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/netlink.c                       |   5 +-
 tools/lib/bpf/netlink.h                       |   8 +
 .../selftests/bpf/prog_tests/tc_bpf_link.c    | 285 +++++++++++++
 16 files changed, 940 insertions(+), 45 deletions(-)
 create mode 100644 tools/lib/bpf/netlink.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_bpf_link.c
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

--
2.31.1

--
Kartikeya

--
Kartikeya

maintainer
#28 lore raw

Hi Joanne, hi Kumar,

Quoted 7 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Joanne Koong
Quoted 5 lines · Joanne Koong
Quoted 3 lines · Kumar Kartikeya Dwivedi
Quoted 2 lines · Joanne Koong

On Thu, Jun 3, 2021 at 11:31 PM Kumar Kartikeya Dwivedi <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

Quoted 8 lines
Quoted 8 lines

After reading the linked thread (which I should have done before submitting my previous reply :)), if I'm understanding it correctly, it seems then that the work needed for tc bpf_link will be in a new direction that's not based on the code in this v2 patchset. I'm interested in learning more about bpf link and tc - I can pick this up to work on. But if this was something you wanted to work on though, please don't hesitate to let me know; I can find some other bpf link thing to work on instead if that's the case.

The tc ingress/egress overhaul we also discussed at lsf/mm/bpf in our session with John and pretty much is along the lines as in the earlier link you sent. We need it from Cilium & Tetragon as well, so it's wip from our side at the moment, modulo the bpf link part. Would you be okay if I pinged you once something that is plateable is ready?

Thanks,
Daniel

#29 lore raw
Quoted 29 lines · Daniel Borkmann

Hi Joanne, hi Kumar,

Quoted 7 lines · Kumar Kartikeya Dwivedi
Quoted 6 lines · Joanne Koong
Quoted 5 lines · Joanne Koong
Quoted 3 lines · Kumar Kartikeya Dwivedi
Quoted 2 lines · Joanne Koong

On Thu, Jun 3, 2021 at 11:31 PM Kumar Kartikeya Dwivedi <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

Quoted 8 lines
Quoted 8 lines

After reading the linked thread (which I should have done before submitting my previous reply :)), if I'm understanding it correctly, it seems then that the work needed for tc bpf_link will be in a new direction that's not based on the code in this v2 patchset. I'm interested in learning more about bpf link and tc - I can pick this up to work on. But if this was something you wanted to work on though, please don't hesitate to let me know; I can find some other bpf link thing to work on instead if that's the case.

Quoted 5 lines

The tc ingress/egress overhaul we also discussed at lsf/mm/bpf in our session with John and pretty much is along the lines as in the earlier link you sent. We need it from Cilium & Tetragon as well, so it's wip from our side at the moment, modulo the bpf link part. Would you be okay if I pinged you once something that is plateable is ready?

Yeah definitely! Thanks for letting me know!

Quoted 3 lines

Thanks,
Daniel

#30 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 127 lines · Kumar Kartikeya Dwivedi
Quoted 116 lines · Joanne Koong
Quoted 107 lines · Joanne Koong
Quoted 104 lines · Kumar Kartikeya Dwivedi
Quoted 101 lines · Joanne Koong
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                            ┌─────┐
                                                                            │     │
                                                                            │ BPF │
                                                                            program
                                                                            │     │
                                                                            └──▲──┘
                                                      ┌───────┐                │
                                                      │       │         ┌──────┴───────┐
                                                      │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
         │          ┌─────────────┐                       │                ┌─▼───┴──┐
         └──────────►  tcf_chain  │                       │                │bpf_link│
                    └───────┬─────┘                       │                └────────┘
                            │          ┌─────────────┐    │
                            └──────────►  tcf_proto  ├────┘
                                       └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

Quoted 8 lines
Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

Quoted 9 lines

After reading the linked thread (which I should have done before submitting my previous reply :)), if I'm understanding it correctly, it seems then that the work needed for tc bpf_link will be in a new direction that's not based on the code in this v2 patchset. I'm interested in learning more about bpf link and tc - I can pick this up to work on. But if this was something you wanted to work on though, please don't hesitate to let me know; I can find some other bpf link thing to work on instead if that's the case.

Quoted 4 lines

Feel free to take it. And yes, it's going to be much simpler than this. I think you can just add two bpf_prog pointers in struct net_device, use rtnl_lock to protect the updates, and invoke using bpf_prog_run in sch_handle_ingress and sch_handle_egress.

Except we'd want to also support multiple programs on different priorities? I don't think requiring a libxdp-like dispatcher to achieve this is a good idea if we can just have it be part of the API from the get-go...

-Toke

maintainer
#31 lore raw
Quoted 133 lines · Toke Høiland-Jørgensen
Quoted 126 lines · Kumar Kartikeya Dwivedi
Quoted 116 lines · Joanne Koong
Quoted 107 lines · Joanne Koong
Quoted 104 lines · Kumar Kartikeya Dwivedi
Quoted 101 lines · Joanne Koong
Quoted 87 lines · Kumar Kartikeya Dwivedi

This is the second (non-RFC) version.

This adds a bpf_link path to create TC filters tied to cls_bpf classifier, and introduces fd based ownership for such TC filters. Netlink cannot delete or replace such filters, but the bpf_link is severed on indirect destruction of the filter (backing qdisc being deleted, or chain being flushed, etc.). To ensure that filters remain attached beyond process lifetime, the usual bpf_link fd pinning approach can be used.

The individual patches contain more details and comments, but the overall kernel API and libbpf helper mirrors the semantics of the netlink based TC-BPF API merged recently. This means that we start by always setting direct action mode, protocol to ETH_P_ALL, chain_index as 0, etc. If there is a need for more options in the future, they can be easily exposed through the bpf_link API in the future.

Patch 1 refactors cls_bpf change function to extract two helpers that will be reused in bpf_link creation.

Patch 2 exports some bpf_link management functions to modules. This is needed because our bpf_link object is tied to the cls_bpf_prog object. Tying it to tcf_proto would be weird, because the update path has to replace offloaded bpf prog, which happens using internal cls_bpf helpers, and would in general be more code to abstract over an operation that is unlikely to be implemented for other filter types.

Patch 3 adds the main bpf_link API. A function in cls_api takes care of obtaining block reference, creating the filter object, and then calls the bpf_link_change tcf_proto op (only supported by cls_bpf) that returns a fd after setting up the internal structures. An optimization is made to not keep around resources for extended actions, which is explained in a code comment as it wasn't immediately obvious.

Patch 4 adds an update path for bpf_link. Since bpf_link_update only supports replacing the bpf_prog, we can skip tc filter's change path by reusing the filter object but swapping its bpf_prog. This takes care of replacing the offloaded prog as well (if that fails, update is aborted). So far however, tcf_classify could do normal load (possibly torn) as the cls_bpf_prog->filter would never be modified concurrently. This is no longer true, and to not penalize the classify hot path, we also cannot impose serialization around its load. Hence the load is changed to READ_ONCE, so that the pointer value is always consistent. Due to invocation in a RCU critical section, the lifetime of the prog is guaranteed for the duration of the call.

Patch 5, 6 take care of updating the userspace bits and add a bpf_link returning function to libbpf.

Patch 7 adds a selftest that exercises all possible problematic interactions that I could think of.

Design:

This is where in the object hierarchy our bpf_link object is attached.

                                                                             ┌─────┐
                                                                             │     │
                                                                             │ BPF │
                                                                             program
                                                                             │     │
                                                                             └──▲──┘
                                                       ┌───────┐                │
                                                       │       │         ┌──────┴───────┐
                                                       │  mod  ├─────────► cls_bpf_prog │
┌────────────────┐                                    │cls_bpf│         └────┬───▲─────┘
│    tcf_block   │                                    │       │              │   │
└────────┬───────┘                                    └───▲───┘              │   │
          │          ┌─────────────┐                       │                ┌─▼───┴──┐
          └──────────►  tcf_chain  │                       │                │bpf_link│
                     └───────┬─────┘                       │                └────────┘
                             │          ┌─────────────┐    │
                             └──────────►  tcf_proto  ├────┘
                                        └─────────────┘

The bpf_link is detached on destruction of the cls_bpf_prog. Doing it this way allows us to implement update in a lightweight manner without having to recreate a new filter, where we can just replace the BPF prog attached to cls_bpf_prog.

The other way to do it would be to link the bpf_link to tcf_proto, there are numerous downsides to this:

1. All filters have to embed the pointer even though they won't be using it when cls_bpf is compiled in.
2. This probably won't make sense to be extended to other filter types anyway.
3. We aren't able to optimize the update case without adding another bpf_link specific update operation to tcf_proto ops.

Quoted 5 lines

The downside with tying this to the module is having to export bpf_link management functions and introducing a tcf_proto op. Hopefully the cost of another operation func pointer is not big enough (as there is only one ops struct per module).

Hi Kumar,

Do you have any plans / bandwidth to land this feature upstream? If so, do you have a tentative estimation for when you'll be able to work on this? And if not, are you okay with someone else working on this to get it merged in?

Quoted 8 lines
Quoted 5 lines

I can have a look at resurrecting it later this month, if you're ok with waiting until then, otherwise if someone else wants to pick this up before that it's fine by me, just let me know so we avoid duplicated effort. Note that the approach in v2 is dead/unlikely to get accepted by the TC maintainers, so we'd have to implement the way Daniel mentioned in [0].

Sounds great! We'll wait and check back in with you later this month.

Quoted 8 lines

After reading the linked thread (which I should have done before submitting my previous reply :)), if I'm understanding it correctly, it seems then that the work needed for tc bpf_link will be in a new direction that's not based on the code in this v2 patchset. I'm interested in learning more about bpf link and tc - I can pick this up to work on. But if this was something you wanted to work on though, please don't hesitate to let me know; I can find some other bpf link thing to work on instead if that's the case.

Quoted 4 lines

Feel free to take it. And yes, it's going to be much simpler than this. I think you can just add two bpf_prog pointers in struct net_device, use rtnl_lock to protect the updates, and invoke using bpf_prog_run in sch_handle_ingress and sch_handle_egress.

Quoted 4 lines

Except we'd want to also support multiple programs on different priorities? I don't think requiring a libxdp-like dispatcher to achieve this is a good idea if we can just have it be part of the API from the get-go...

Yes, it will be multi-prog to avoid a situation where dispatcher is needed.

#32 lore raw
Quoted 6 lines
Quoted 4 lines

Except we'd want to also support multiple programs on different priorities? I don't think requiring a libxdp-like dispatcher to achieve this is a good idea if we can just have it be part of the API from the get-go...

Yes, it will be multi-prog to avoid a situation where dispatcher is needed.

Awesome! :)

-Toke

#33 lore raw
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 7 lines
Quoted 5 lines

Except we'd want to also support multiple programs on different priorities? I don't think requiring a libxdp-like dispatcher to achieve this is a good idea if we can just have it be part of the API from the get-go...

Yes, it will be multi-prog to avoid a situation where dispatcher is needed.

Awesome! :)

Let's keep it simple to start.
Priorities or anything fancy can be added later if really necessary. Otherwise, I'm afraid, we will go into endless bikeshedding or the best priority scheme.

A link list of bpf progs like cls_bpf with the same semantics as cls_bpf_classify.
With prog->exts_integrated always true and no classid, since this concept doesn't apply.

maintainer
#34 lore raw
Quoted 16 lines · Alexei Starovoitov
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 7 lines
Quoted 5 lines

Except we'd want to also support multiple programs on different priorities? I don't think requiring a libxdp-like dispatcher to achieve this is a good idea if we can just have it be part of the API from the get-go...

Yes, it will be multi-prog to avoid a situation where dispatcher is needed.

Awesome! :)

Let's keep it simple to start.
Priorities or anything fancy can be added later if really necessary. Otherwise, I'm afraid, we will go into endless bikeshedding or the best priority scheme.

Quoted 4 lines

A link list of bpf progs like cls_bpf with the same semantics as cls_bpf_classify.
With prog->exts_integrated always true and no classid, since this concept doesn't apply.

Yes, semantics must be that TC_ACT_UNSPEC continues in the list and everything else as return code would terminate the evaluation.

#35 lore raw
Quoted 17 lines · Daniel Borkmann
Quoted 16 lines · Alexei Starovoitov
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 7 lines
Quoted 5 lines

Except we'd want to also support multiple programs on different priorities? I don't think requiring a libxdp-like dispatcher to achieve this is a good idea if we can just have it be part of the API from the get-go...

Yes, it will be multi-prog to avoid a situation where dispatcher is needed.

Awesome! :)

Let's keep it simple to start.
Priorities or anything fancy can be added later if really necessary. Otherwise, I'm afraid, we will go into endless bikeshedding or the best priority scheme.

Quoted 6 lines
Quoted 4 lines

A link list of bpf progs like cls_bpf with the same semantics as cls_bpf_classify.
With prog->exts_integrated always true and no classid, since this concept doesn't apply.

Yes, semantics must be that TC_ACT_UNSPEC continues in the list and everything else as return code would terminate the evaluation.

Sure, SGTM!

-Toke