Add TC-BPF API
Subsystems (6)
Kumar Kartikeya Dwivedi
maintainerv3 cover Add TC-BPF API
This is the third version of the TC-BPF series.
It adds a simple API that uses netlink to attach the tc filter and its bpf classifier. Currently, a user needs to shell out to the tc command line to be able to create filters and attach SCHED_CLS programs as classifiers. With the help of this API, it will be possible to use libbpf for doing all parts of bpf program setup and attach.
Direct action is now always set, and currently no way to disable it has been provided. This also means that SCHED_ACT programs are a lot less useful, so support for them has been removed for now. In the future, if someone comes up with a convincing use case where the direct action mode doesn't serve their needs, a simple extension that allows disabling direct action mode and passing the list of actions that would be bound to the classifier can be added.
In an effort to keep discussion focused, this series doesn't have the high level TC-BPF API. It was clear that there is a need for a bpf_link API in the kernel, hence that will be submitted as a separate patchset.
The individual commit messages contain more details, and also a brief summary of the API.
Changelog:
----------
v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]
* bpf_tc_cls_* -> bpf_tc_* rename
* bpf_tc_attach_id now only consists of handle and priority, the two variables that user may or may not set.
* bpf_tc_replace has been dropped, instead a replace bool is introduced in bpf_tc_opts for the same purpose.
* bpf_tc_get_info now takes attach_id for filling in filter details during lookup instead of requiring user to do so. This also allows us to remove the fd parameter, as no matching is needed as long as we have all attributes necessary to identify a specific filter.
* A little bit of code simplification taking into account the change above.
* priority and protocol are now __u16 members in user facing API structs to reflect actual size.
* Patch updating pkt_cls.h header has been removed, as it is unused now.
* protocol and chain_index options have been dropped in bpf_tc_opts, protocol is always set to ETH_P_ALL, while chain_index is set as 0 by default in the kernel. This also means removal of chain_index from bpf_tc_attach_id, as it is unconditionally always 0.
* bpf_tc_cls_change has been dropped
* selftest now uses ASSERT_* macros
v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* netlink helpers have been renamed to object_action style.
* attach_id now only contains attributes that are not explicitly set. Only the bare minimum info is kept in it.
* protocol is now an optional and always set to ETH_P_ALL.
* direct-action mode is always set.
* skip_sw and skip_hw options have also been removed.
* bpf_tc_cls_info struct now also returns the bpf program tag and id, as available in the netlink response. This came up as a requirement during discussion with people wanting to use this functionality.
* support for attaching SCHED_ACT programs has been dropped, as it isn't useful without any support for binding loaded actions to a classifier.
* the distinction between dev and block API has been dropped, there is now a single set of functions and user has to pass the special ifindex value to indicate operation on a shared filter block on their own.
* The high level API returning a bpf_link is gone. This was already non- functional for pinning and typical ownership semantics. Instead, a separate patchset will be sent adding a bpf_link API for attaching SCHED_CLS progs to the kernel, and its corresponding libbpf API.
* The clsact qdisc is now setup automatically in a best-effort fashion whenever user passes in the clsact ingress or egress parent id. This is done with exclusive mode, such that if an ingress or clsact qdisc is already set up, we skip the setup and move on with filter creation.
* Other minor changes that came up during the course of discussion and rework.
Kumar Kartikeya Dwivedi (3):
libbpf: add helpers for preparing netlink attributes
libbpf: add low level TC-BPF API
libbpf: add selftests for TC-BPF API
tools/lib/bpf/libbpf.h | 44 +++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 356 ++++++++++++++++-- tools/lib/bpf/nlattr.h | 48 +++ .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 +++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 + 6 files changed, 605 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c
--
2.30.2
Kumar Kartikeya Dwivedi
maintainerPATCH v3 1/3 libbpf: add helpers for preparing netlink attributes
This change introduces a few helpers to wrap open coded attribute preparation in netlink.c.
Every nested attribute's closure must happen using the helper nlattr_end_nested, which sets its length properly. NLA_F_NESTED is enforeced using nlattr_begin_nested helper. Other simple attributes can be added directly.
The maxsz parameter corresponds to the size of the request structure which is being filled in, so for instance with req being:
struct {
struct nlmsghdr nh;
struct tcmsg t;
char buf[4096];
} req;Then, maxsz should be sizeof(req).
This change also converts the open coded attribute preparation with the helpers. Note that the only failure the internal call to nlattr_add could result in the nested helper would be -EMSGSIZE, hence that is what we return to our caller.
--- tools/lib/bpf/netlink.c | 37 ++++++++++++++----------------- tools/lib/bpf/nlattr.h | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 21 deletions(-)
tools/lib/bpf/netlink.c +16 -21
@@ -135,7 +135,7 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, __u32 flags) {int sock, seq =0 , ret; -struct nlattr *nla, *nla_xdp; +struct nlattr *nla;struct {struct nlmsghdr nh;struct ifinfomsg ifinfo;
@@ -157,36 +157,31 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, req.ifinfo.ifi_index = ifindex;/* started nested attribute for XDP */ - nla = (struct nlattr *)(((char *)&req) - +NLMSG_ALIGN (req.nh.nlmsg_len)); - nla->nla_type =NLA_F_NESTED |IFLA_XDP ; - nla->nla_len =NLA_HDRLEN ; + nla =nlattr_begin_nested (&req.nh,sizeof (req),IFLA_XDP ); +if (!nla) { + ret = -EMSGSIZE ; + goto cleanup; + }/* add XDP fd */ - nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); - nla_xdp->nla_type =IFLA_XDP_FD ; - nla_xdp->nla_len =NLA_HDRLEN +sizeof (int ); -memcpy ((char *)nla_xdp +NLA_HDRLEN , &fd,sizeof (fd)); - nla->nla_len += nla_xdp->nla_len; + ret =nlattr_add (&req.nh,sizeof (req),IFLA_XDP_FD , &fd,sizeof (fd)); +if (ret <0 ) + goto cleanup;/* if user passed in any flags, add those too */ if (flags) { - nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); - nla_xdp->nla_type =IFLA_XDP_FLAGS ; - nla_xdp->nla_len =NLA_HDRLEN +sizeof (flags); -memcpy ((char *)nla_xdp +NLA_HDRLEN , &flags,sizeof (flags)); - nla->nla_len += nla_xdp->nla_len; + ret =nlattr_add (&req.nh,sizeof (req),IFLA_XDP_FLAGS , &flags,sizeof (flags)); +if (ret <0 ) + goto cleanup; }if (flags &XDP_FLAGS_REPLACE ) { - nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); - nla_xdp->nla_type =IFLA_XDP_EXPECTED_FD ; - nla_xdp->nla_len =NLA_HDRLEN +sizeof (old_fd); -memcpy ((char *)nla_xdp +NLA_HDRLEN , &old_fd,sizeof (old_fd)); - nla->nla_len += nla_xdp->nla_len; + ret =nlattr_add (&req.nh,sizeof (req),IFLA_XDP_EXPECTED_FD , &flags,sizeof (flags)); +if (ret <0 ) + goto cleanup; } - req.nh.nlmsg_len +=NLA_ALIGN (nla->nla_len); +nlattr_end_nested (&req.nh, nla);if (send (sock, &req, req.nh.nlmsg_len,0 ) <0 ) { ret = -errno;
tools/lib/bpf/nlattr.h +48 -0
59 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v3 1/3] libbpf: add helpers for preparing netlink attributes
Quoted 6 lines · Kumar Kartikeya Dwivedi
This change introduces a few helpers to wrap open coded attribute preparation in
netlink.c.Every nested attribute's closure must happen using the helper
nlattr_end_nested, which sets its length properly.NLA_F_NESTEDis enforeced usingnlattr_begin_nestedhelper. Other simple attributes
typo: enforced
Quoted 25 lines
can be added directly.
The maxsz parameter corresponds to the size of the request structure which is being filled in, so for instance with req being:
struct { struct nlmsghdr nh; struct tcmsg t; char buf[4096]; } req;Then, maxsz should be sizeof(req).
This change also converts the open coded attribute preparation with the helpers. Note that the only failure the internal call to
nlattr_addcould result in the nested helper would be-EMSGSIZE, hence that is what we return to our caller.Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/netlink.c | 37 ++++++++++++++----------------- tools/lib/bpf/nlattr.h | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 21 deletions(-)
[...]
Kumar Kartikeya Dwivedi
maintainerPATCH v3 2/3 libbpf: add low level TC-BPF API
In reply to Kumar Kartikeya DwivediThis adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters. These functions operate directly on the loaded prog's fd, and return a handle to the filter using an out parameter named id.
The basic featureset is covered to allow for attaching and removal of filters. Some additional features like TCA_BPF_POLICE and TCA_RATE for the API have been omitted. These can added on top later by extending the bpf_tc_opts struct.
Support for binding actions directly to a classifier by passing them in during filter creation has also been omitted for now. These actions have an auto clean up property because their lifetime is bound to the filter they are attached to. This can be added later, but was omitted for now as direct action mode is a better alternative to it, which is enabled by default.
An API summary:
bpf_tc_attach may be used to attach, and replace SCHED_CLS bpf classifier. The protocol is always set as ETH_P_ALL. The replace option in bpf_tc_opts is used to control replacement behavior. Attachment fails if filter with existing attributes already exists.
bpf_tc_detach may be used to detach existing SCHED_CLS filter. The bpf_tc_attach_id object filled in during attach must be passed in to the detach functions for them to remove the filter and its attached classififer correctly.
bpf_tc_get_info is a helper that can be used to obtain attributes for the filter and classififer.
Examples:
struct bpf_tc_attach_id id = {};
struct bpf_object *obj;
struct bpf_program *p;
int fd, r;
obj = bpf_object_open("foo.o");
if (IS_ERR_OR_NULL(obj))
return PTR_ERR(obj);
p = bpf_object__find_program_by_title(obj, "classifier");
if (IS_ERR_OR_NULL(p))
return PTR_ERR(p);
if (bpf_object__load(obj) < 0)
return -1;
fd = bpf_program__fd(p);
r = bpf_tc_attach(fd, if_nametoindex("lo"),
BPF_TC_CLSACT_INGRESS,
NULL, &id);
if (r < 0)
return r;... which is roughly equivalent to:
# tc qdisc add dev lo clsact # tc filter add dev lo ingress bpf obj foo.o sec classifier da
... as direct action mode is always enabled.
To replace an existing filter:
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = id.handle,
.priority = id.priority, .replace = true);
r = bpf_tc_attach(fd, if_nametoindex("lo"),
BPF_TC_CLSACT_INGRESS,
&opts, &id);
if (r < 0)
return r;To obtain info of a particular filter, the example above can be extended as follows:
struct bpf_tc_info info = {};
r = bpf_tc_get_info(if_nametoindex("lo"),
BPF_TC_CLSACT_INGRESS,
&id, &info);
if (r < 0)
return r;... where id corresponds to the bpf_tc_attach_id filled in during an attach operation.
--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)
tools/lib/bpf/libbpf.h +44 -0
@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h"
@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS) + +struct bpf_tc_opts { +size_t sz; +__u32 handle; +__u32 class_id; +__u16 priority; +bool replace; + size_t :0; +}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id { +__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen; +}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id, +const struct bpf_tc_opts *opts, +struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id, +struct bpf_tc_info *info); + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/libbpf.map +3 -0
@@ -361,4 +361,7 @@ LIBBPF_0.4.0 { bpf_linker__new; bpf_map__inner_map; bpf_object__set_kversion; + bpf_tc_attach; + bpf_tc_detach; + bpf_tc_get_info; } LIBBPF_0.3.0;
tools/lib/bpf/netlink.c +313 -6
338 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 8 lines · Kumar Kartikeya Dwivedi
This adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters. These functions operate directly on the loaded prog's fd, and return a handle to the filter using an out parameter named id.
The basic featureset is covered to allow for attaching and removal of filters. Some additional features like
TCA_BPF_POLICEandTCA_RATEfor the API have been omitted. These can added on top later by extending the
"later" => "layer"?
Quoted 26 lines
bpf_tc_optsstruct.Support for binding actions directly to a classifier by passing them in during filter creation has also been omitted for now. These actions have an auto clean up property because their lifetime is bound to the filter they are attached to. This can be added later, but was omitted for now as direct action mode is a better alternative to it, which is enabled by default.
An API summary:
bpf_tc_attachmay be used to attach, and replaceSCHED_CLSbpf classifier. The protocol is always set asETH_P_ALL. The replace option inbpf_tc_optsis used to control replacement behavior. Attachment fails if filter with existing attributes already exists.
bpf_tc_detachmay be used to detach existingSCHED_CLSfilter. Thebpf_tc_attach_idobject filled in during attach must be passed in to the detach functions for them to remove the filter and its attached classififer correctly.
bpf_tc_get_infois a helper that can be used to obtain attributes for the filter and classififer.Examples:
Quoted 10 lines
struct bpf_tc_attach_id id = {};
struct bpf_object *obj;
struct bpf_program *p;
int fd, r;
obj = bpf_object_open("foo.o");
if (IS_ERR_OR_NULL(obj))
return PTR_ERR(obj);
p = bpf_object__find_program_by_title(obj, "classifier");Please use bpf_object__find_program_by_name() API. bpf_object__find_program_by_title() is not recommended as now libbpf supports multiple programs within the same section.
Quoted 70 lines
if (IS_ERR_OR_NULL(p)) return PTR_ERR(p); if (bpf_object__load(obj) < 0) return -1; fd = bpf_program__fd(p); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, NULL, &id); if (r < 0) return r;... which is roughly equivalent to:
# tc qdisc add dev lo clsact # tc filter add dev lo ingress bpf obj foo.o sec classifier da... as direct action mode is always enabled.
To replace an existing filter:
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = id.handle, .priority = id.priority, .replace = true); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &opts, &id); if (r < 0) return r;To obtain info of a particular filter, the example above can be extended as follows:
struct bpf_tc_info info = {}; r = bpf_tc_get_info(if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &id, &info); if (r < 0) return r;... where id corresponds to the
bpf_tc_attach_idfilled in during an attach operation.Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +3
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS) + +struct bpf_tc_opts { +size_t sz; +__u32 handle; +__u32 class_id; +__u16 priority; +bool replace; + size_t :0;
Did you see any error without "size_t :0"?
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id { +__u32 handle; +__u16 priority; +}; +
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 8 lines · Yonghong Song
Quoted 5 lines · Kumar Kartikeya Dwivedi
This adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters. These functions operate directly on the loaded prog's fd, and return a handle to the filter using an out parameter named id.
Quoted 6 lines
Quoted 3 lines
The basic featureset is covered to allow for attaching and removal of filters. Some additional features like
TCA_BPF_POLICEandTCA_RATEfor the API have been omitted. These can added on top later by extending the"later" => "layer"?
No, I meant that other options can be added on top of this series by someone later. I'll reword it.
Quoted 35 lines
Quoted 35 lines
bpf_tc_optsstruct.Support for binding actions directly to a classifier by passing them in during filter creation has also been omitted for now. These actions have an auto clean up property because their lifetime is bound to the filter they are attached to. This can be added later, but was omitted for now as direct action mode is a better alternative to it, which is enabled by default.
An API summary:
bpf_tc_attachmay be used to attach, and replaceSCHED_CLSbpf classifier. The protocol is always set asETH_P_ALL. The replace option inbpf_tc_optsis used to control replacement behavior. Attachment fails if filter with existing attributes already exists.
bpf_tc_detachmay be used to detach existingSCHED_CLSfilter. Thebpf_tc_attach_idobject filled in during attach must be passed in to the detach functions for them to remove the filter and its attached classififer correctly.
bpf_tc_get_infois a helper that can be used to obtain attributes for the filter and classififer.Examples:
struct bpf_tc_attach_id id = {}; struct bpf_object *obj; struct bpf_program *p; int fd, r; obj = bpf_object_open("foo.o"); if (IS_ERR_OR_NULL(obj)) return PTR_ERR(obj);
Quoted 5 lines
Quoted 1 line
p = bpf_object__find_program_by_title(obj, "classifier");Please use
bpf_object__find_program_by_name()API.bpf_object__find_program_by_title()is not recommended as now libbpf supports multiple programs within the same section.
Thanks, will do.
Quoted 72 lines
Quoted 72 lines
if (IS_ERR_OR_NULL(p)) return PTR_ERR(p); if (bpf_object__load(obj) < 0) return -1; fd = bpf_program__fd(p); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, NULL, &id); if (r < 0) return r;... which is roughly equivalent to:
# tc qdisc add dev lo clsact # tc filter add dev lo ingress bpf obj foo.o sec classifier da... as direct action mode is always enabled.
To replace an existing filter:
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = id.handle, .priority = id.priority, .replace = true); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &opts, &id); if (r < 0) return r;To obtain info of a particular filter, the example above can be extended as follows:
struct bpf_tc_info info = {}; r = bpf_tc_get_info(if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &id, &info); if (r < 0) return r;... where id corresponds to the
bpf_tc_attach_idfilled in during an attach operation.Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +7
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS) + +struct bpf_tc_opts {
Quoted 9 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+size_t sz; +__u32 handle; +__u32 class_id; +__u16 priority; +bool replace; + size_t :0;Did you see any error without "
size_t:0"?
Not really, I just added this considering this recent change:dde7b3f5f2f4 ("libbpf: Add explicit padding to bpf_xdp_set_link_opts")
Quoted 11 lines
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id { +__u32 handle; +__u16 priority; +}; +[...]
--
Kartikeya
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 19 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Yonghong Song
Quoted 5 lines · Kumar Kartikeya Dwivedi
This adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters. These functions operate directly on the loaded prog's fd, and return a handle to the filter using an out parameter named id.
Quoted 6 lines
Quoted 3 lines
The basic featureset is covered to allow for attaching and removal of filters. Some additional features like
TCA_BPF_POLICEandTCA_RATEfor the API have been omitted. These can added on top later by extending the"later" => "layer"?
No, I meant that other options can be added on top of this series by someone later. I'll reword it.
[...]
Quoted 38 lines
Quoted 28 lines
Quoted 28 lines
Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +7
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS) + +struct bpf_tc_opts {Quoted 9 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+size_t sz; +__u32 handle; +__u32 class_id; +__u16 priority; +bool replace; + size_t :0;Did you see any error without "
size_t:0"?
Quoted 2 lines
Not really, I just added this considering this recent change:
dde7b3f5f2f4("libbpf: Add explicit padding tobpf_xdp_set_link_opts")
Thanks. That is fine with me then.
Quoted 16 lines
Quoted 12 lines
Quoted 11 lines
tools/lib/bpf/libbpf.h +10
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id { +__u32 handle; +__u16 priority; +}; +[...]
--
Kartikeya
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
In reply to Kumar Kartikeya DwivediQuoted 102 lines · Kumar Kartikeya Dwivedi
This adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters. These functions operate directly on the loaded prog's fd, and return a handle to the filter using an out parameter named id.
The basic featureset is covered to allow for attaching and removal of filters. Some additional features like
TCA_BPF_POLICEandTCA_RATEfor the API have been omitted. These can added on top later by extending thebpf_tc_optsstruct.Support for binding actions directly to a classifier by passing them in during filter creation has also been omitted for now. These actions have an auto clean up property because their lifetime is bound to the filter they are attached to. This can be added later, but was omitted for now as direct action mode is a better alternative to it, which is enabled by default.
An API summary:
bpf_tc_attachmay be used to attach, and replaceSCHED_CLSbpf classifier. The protocol is always set asETH_P_ALL. The replace option inbpf_tc_optsis used to control replacement behavior. Attachment fails if filter with existing attributes already exists.
bpf_tc_detachmay be used to detach existingSCHED_CLSfilter. Thebpf_tc_attach_idobject filled in during attach must be passed in to the detach functions for them to remove the filter and its attached classififer correctly.
bpf_tc_get_infois a helper that can be used to obtain attributes for the filter and classififer.Examples:
struct bpf_tc_attach_id id = {}; struct bpf_object *obj; struct bpf_program *p; int fd, r; obj = bpf_object_open("foo.o"); if (IS_ERR_OR_NULL(obj)) return PTR_ERR(obj); p = bpf_object__find_program_by_title(obj, "classifier"); if (IS_ERR_OR_NULL(p)) return PTR_ERR(p); if (bpf_object__load(obj) < 0) return -1; fd = bpf_program__fd(p); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, NULL, &id); if (r < 0) return r;... which is roughly equivalent to:
# tc qdisc add dev lo clsact # tc filter add dev lo ingress bpf obj foo.o sec classifier da... as direct action mode is always enabled.
To replace an existing filter:
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = id.handle, .priority = id.priority, .replace = true); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &opts, &id); if (r < 0) return r;To obtain info of a particular filter, the example above can be extended as follows:
struct bpf_tc_info info = {}; r = bpf_tc_get_info(if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &id, &info); if (r < 0) return r;... where id corresponds to the
bpf_tc_attach_idfilled in during an attach operation.Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +0
-0
tools/lib/bpf/libbpf.h line 20
Quoted 5 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h>
apart from those unused macros below, are these needed in public API header?
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 10 lines
tools/lib/bpf/libbpf.h +3
-0#include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)
these seem to be used only internally, why exposing them in public API?
Quoted 7 lines
tools/lib/bpf/libbpf.h +7
-0+ +struct bpf_tc_opts { +size_t sz; +__u32 handle; +__u32 class_id; +__u16 priority; +bool replace;
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 799
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+ size_t :0; +}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id { +__u32 handle; +__u16 priority; +};
what are the chances that we'll need to grow this id struct? If that happens, how do we do that in a backward/forward compatible manner?
if handle/prio are the only two ever necessary, we can actually use bpf_tc_opts to return them back to user (we do that with bpf_test_run_opts API). And then adjust detach/get_info methods to let pass those values.
The whole idea of a struct for id just screams "compatibility problems down the road" at me. Does anyone else has any other opinion on this?
Quoted 4 lines
tools/lib/bpf/libbpf.h +4
-0+ +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol;
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 813
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen; +}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id,
so parent_id is INGRESS|EGRESS, right? Is that an obvious name for this parameter? I had to look at the code to understand what's expected. Is it possible that it will be anything other than INGRESS or EGRESS? If not bool ingress might be an option. Or perhaps enum bpf_tc_direction { BPF_TC_INGRESS, BPF_TC_EGRESS } is better still.
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 818
Quoted 5 lines
tools/lib/bpf/libbpf.h +5
-0+const struct bpf_tc_opts *opts, + struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id,
bpf_tc_query() to be more in line with attach/detach single-word verbs?
Quoted 18 lines
tools/lib/bpf/libbpf.h +3
-0+const struct bpf_tc_attach_id *id, + struct bpf_tc_info *info); + #ifdef __cplusplus }/* extern "C" */ #endif tools/lib/bpf/libbpf.map +3
-0@@ -361,4 +361,7 @@ LIBBPF_0.4.0 { bpf_linker__new; bpf_map__inner_map; bpf_object__set_kversion; + bpf_tc_attach; + bpf_tc_detach; + bpf_tc_get_info; } LIBBPF_0.3.0;
[...]
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 119 lines · Andrii Nakryiko
Quoted 102 lines · Kumar Kartikeya Dwivedi
This adds functions that wrap the netlink API used for adding, manipulating, and removing traffic control filters. These functions operate directly on the loaded prog's fd, and return a handle to the filter using an out parameter named id.
The basic featureset is covered to allow for attaching and removal of filters. Some additional features like
TCA_BPF_POLICEandTCA_RATEfor the API have been omitted. These can added on top later by extending thebpf_tc_optsstruct.Support for binding actions directly to a classifier by passing them in during filter creation has also been omitted for now. These actions have an auto clean up property because their lifetime is bound to the filter they are attached to. This can be added later, but was omitted for now as direct action mode is a better alternative to it, which is enabled by default.
An API summary:
bpf_tc_attachmay be used to attach, and replaceSCHED_CLSbpf classifier. The protocol is always set asETH_P_ALL. The replace option inbpf_tc_optsis used to control replacement behavior. Attachment fails if filter with existing attributes already exists.
bpf_tc_detachmay be used to detach existingSCHED_CLSfilter. Thebpf_tc_attach_idobject filled in during attach must be passed in to the detach functions for them to remove the filter and its attached classififer correctly.
bpf_tc_get_infois a helper that can be used to obtain attributes for the filter and classififer.Examples:
struct bpf_tc_attach_id id = {}; struct bpf_object *obj; struct bpf_program *p; int fd, r; obj = bpf_object_open("foo.o"); if (IS_ERR_OR_NULL(obj)) return PTR_ERR(obj); p = bpf_object__find_program_by_title(obj, "classifier"); if (IS_ERR_OR_NULL(p)) return PTR_ERR(p); if (bpf_object__load(obj) < 0) return -1; fd = bpf_program__fd(p); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, NULL, &id); if (r < 0) return r;... which is roughly equivalent to:
# tc qdisc add dev lo clsact # tc filter add dev lo ingress bpf obj foo.o sec classifier da... as direct action mode is always enabled.
To replace an existing filter:
DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = id.handle, .priority = id.priority, .replace = true); r = bpf_tc_attach(fd, if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &opts, &id); if (r < 0) return r;To obtain info of a particular filter, the example above can be extended as follows:
struct bpf_tc_info info = {}; r = bpf_tc_get_info(if_nametoindex("lo"), BPF_TC_CLSACT_INGRESS, &id, &info); if (r < 0) return r;... where id corresponds to the
bpf_tc_attach_idfilled in during an attach operation.Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +0
-0tools/lib/bpf/libbpf.h line 20
Quoted 5 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> apart from those unused macros below, are these needed in public API header?
Quoted 7 lines
tools/lib/bpf/libbpf.h +0
-0#include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);
Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)these seem to be used only internally, why exposing them in public API?
No they're "aliases" for when you want to attach the filter directly to the interface (and thus install the clsact qdisc as the root). You can also use the filter with an existing qdisc (most commonly HTB), in which case you need to specify the qdisc handle as the root. We have a few examples of this use case:
https://github.com/xdp-project/bpf-examples/tree/master/traffic-pacing-edt
and
https://github.com/xdp-project/xdp-cpumap-tc
Quoted 20 lines
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+struct bpf_tc_opts { +size_t sz; +__u32 handle; +__u32 class_id; +__u16 priority; +bool replace;tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 799
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+ size_t :0; +}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id { +__u32 handle; +__u16 priority; +};what are the chances that we'll need to grow this id struct? If that happens, how do we do that in a backward/forward compatible manner?
Quoted 7 lines
if handle/prio are the only two ever necessary, we can actually use
bpf_tc_optsto return them back to user (we do that withbpf_test_run_optsAPI). And then adjust detach/get_info methods to let pass those values.The whole idea of a struct for id just screams "compatibility problems down the road" at me. Does anyone else has any other opinion on this?
Well, *if* we ever want to extend them (e.g., to support other values of the protocol field, if that ever becomes necessary), we'll probably also want to make it possible to pass the same identifiers as options, so just reusing the opts struct definitely makes sense!
Quoted 11 lines
Quoted 11 lines
tools/lib/bpf/libbpf.h +11
-0+struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen; +}; +
Quoted 8 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 813
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id,so
parent_idis INGRESS|EGRESS, right? Is that an obvious name for this parameter? I had to look at the code to understand what's expected. Is it possible that it will be anything other than INGRESS or EGRESS? If notbool ingressmight be an option. Or perhaps enumbpf_tc_direction{BPF_TC_INGRESS,BPF_TC_EGRESS} is better still.
See above; the parent is the attach point, and you use the defines from above if you just want to attach to the interface.
But maybe documenting this in a comment above the function signature would be good (along with a bit of terminology from the TC world for those coming from there) :)
Quoted 8 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 818
Quoted 5 lines
tools/lib/bpf/libbpf.h +5
-0+const struct bpf_tc_opts *opts, + struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id,
bpf_tc_query()to be more in line with attach/detach single-word verbs?
OK by me!
-Toke
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 3 lines · Toke Høiland-Jørgensen
Quoted 2 lines · Andrii Nakryiko
On Tue, Apr 20, 2021 at 12:37 PM Kumar Kartikeya Dwivedi <[email protected]> wrote:
[...]
Quoted 38 lines
Quoted 25 lines
Quoted 11 lines
--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +0
-0tools/lib/bpf/libbpf.h line 20
Quoted 5 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> apart from those unused macros below, are these needed in public API header?
Quoted 6 lines
tools/lib/bpf/libbpf.h +0
-0#include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)these seem to be used only internally, why exposing them in public API?
No they're "aliases" for when you want to attach the filter directly to the interface (and thus install the clsact qdisc as the root). You can also use the filter with an existing qdisc (most commonly HTB), in which case you need to specify the qdisc handle as the root. We have a few examples of this use case:
Quoted 3 lines
https://github.com/xdp-project/bpf-examples/tree/master/traffic-pacing-edt
and
https://github.com/xdp-project/xdp-cpumap-tc
I'm a bit puzzled, could you elaborate on your use case on why you wouldn't use the tc egress hook for those especially given it's guaranteed to run outside of root qdisc lock?
Some pointers as well:
- http://vger.kernel.org/lpc-bpf2018.html#session-1
- https://netdevconf.info/0x14/session.html?talk-replacing-HTB-with-EDT-and-BPF
- https://cilium.io/blog/2020/11/10/cilium-19#bwmanager
Thanks,
Daniel
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 43 lines · Daniel Borkmann
Quoted 3 lines · Toke Høiland-Jørgensen
Quoted 2 lines · Andrii Nakryiko
On Tue, Apr 20, 2021 at 12:37 PM Kumar Kartikeya Dwivedi <[email protected]> wrote:
[...]
Quoted 38 lines
Quoted 25 lines
Quoted 11 lines
--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +0
-0tools/lib/bpf/libbpf.h line 20
Quoted 5 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> apart from those unused macros below, are these needed in public API header?
Quoted 6 lines
tools/lib/bpf/libbpf.h +0
-0#include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)these seem to be used only internally, why exposing them in public API?
No they're "aliases" for when you want to attach the filter directly to the interface (and thus install the clsact qdisc as the root). You can also use the filter with an existing qdisc (most commonly HTB), in which case you need to specify the qdisc handle as the root. We have a few examples of this use case:
Quoted 7 lines
Quoted 3 lines
https://github.com/xdp-project/bpf-examples/tree/master/traffic-pacing-edt
and
https://github.com/xdp-project/xdp-cpumap-tcI'm a bit puzzled, could you elaborate on your use case on why you wouldn't use the tc egress hook for those especially given it's guaranteed to run outside of root qdisc lock?
Jesper can correct me if I'm wrong, but I think the first one of the links above is basically his implementation of just that EDT-based shaper. And it works reasonably well, except you don't get the nice per-flow scheduling and sparse flow prioritisation like in FQ-CoDel unless you implement that yourself in BPF when you set the timestamps (and that is by no means trivial to implement).
So if you want to use any of the features of the existing qdiscs (I have also been suggesting to people that they use tc_bpf if they want to customise sch_cake's notion of flows or shaping tiers), you need to be able to attach the filter to an existing qdisc. Sure, this means you're still stuck behind the qdisc lock, but for some applications that is fine (not everything is a data centre, some devices don't have that many CPUs anyway; and as the second example above shows, you can get around the qdisc lock by some clever use of partitioning of flows using cpumap).
So what this boils down to is, we should keep the 'parent' parameter not just as an egress/ingress enum, but also as a field the user can fill out. I'm fine with moving the latter into the opts struct, though, so maybe the function parameter can be an enum like:
enum bpf_tc_attach_point {
BPF_TC_CLSACT_INGRESS,
BPF_TC_CLSACT_EGRESS,
BPF_TC_QDISC_PARENT
};
where if you set the last one you have to fill in the parent in opts?
-Toke
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 74 lines · Toke Høiland-Jørgensen
Quoted 43 lines · Daniel Borkmann
Quoted 3 lines · Toke Høiland-Jørgensen
Quoted 2 lines · Andrii Nakryiko
On Tue, Apr 20, 2021 at 12:37 PM Kumar Kartikeya Dwivedi <[email protected]> wrote:
[...]
Quoted 38 lines
Quoted 25 lines
Quoted 11 lines
--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +0
-0tools/lib/bpf/libbpf.h line 20
Quoted 5 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> apart from those unused macros below, are these needed in public API header?
Quoted 6 lines
tools/lib/bpf/libbpf.h +0
-0#include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)these seem to be used only internally, why exposing them in public API?
No they're "aliases" for when you want to attach the filter directly to the interface (and thus install the clsact qdisc as the root). You can also use the filter with an existing qdisc (most commonly HTB), in which case you need to specify the qdisc handle as the root. We have a few examples of this use case:
Quoted 7 lines
Quoted 3 lines
https://github.com/xdp-project/bpf-examples/tree/master/traffic-pacing-edt
and
https://github.com/xdp-project/xdp-cpumap-tcI'm a bit puzzled, could you elaborate on your use case on why you wouldn't use the tc egress hook for those especially given it's guaranteed to run outside of root qdisc lock?
Jesper can correct me if I'm wrong, but I think the first one of the links above is basically his implementation of just that EDT-based shaper. And it works reasonably well, except you don't get the nice per-flow scheduling and sparse flow prioritisation like in FQ-CoDel unless you implement that yourself in BPF when you set the timestamps (and that is by no means trivial to implement).
So if you want to use any of the features of the existing qdiscs (I have also been suggesting to people that they use
tc_bpfif they want to customise sch_cake's notion of flows or shaping tiers), you need to be able to attach the filter to an existing qdisc. Sure, this means you're still stuck behind the qdisc lock, but for some applications that is fine (not everything is a data centre, some devices don't have that many CPUs anyway; and as the second example above shows, you can get around the qdisc lock by some clever use of partitioning of flows using cpumap).So what this boils down to is, we should keep the 'parent' parameter not just as an egress/ingress enum, but also as a field the user can fill out. I'm fine with moving the latter into the opts struct, though, so maybe the function parameter can be an enum like:
Quoted 7 lines
enum
bpf_tc_attach_point{
BPF_TC_CLSACT_INGRESS,
BPF_TC_CLSACT_EGRESS,
BPF_TC_QDISC_PARENT
};where if you set the last one you have to fill in the parent in opts?
Fair enough, I still think this is a bit backwards and should be discouraged given the constraints, but if you have an actual need for it ... I'd rather simplify API naming, the fact that it's clsact is an implementation detail and shouldn't matter to a user, like:
enum bpf_tc_attach_point {
BPF_TC_INGRESS,
BPF_TC_EGRESS,
BPF_TC_CUSTOM_PARENT,
};
For BPF_TC_INGRESS and BPF_TC_EGRESS, I would enforce opts parent parameter to be /zero/ from the API.
Thanks,
Daniel
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 88 lines · Daniel Borkmann
Quoted 74 lines · Toke Høiland-Jørgensen
Quoted 43 lines · Daniel Borkmann
Quoted 3 lines · Toke Høiland-Jørgensen
Quoted 2 lines · Andrii Nakryiko
On Tue, Apr 20, 2021 at 12:37 PM Kumar Kartikeya Dwivedi <[email protected]> wrote:
[...]
Quoted 38 lines
Quoted 25 lines
Quoted 11 lines
--- tools/lib/bpf/libbpf.h | 44 ++++++ tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 319 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 360 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.h +0
-0tools/lib/bpf/libbpf.h line 20
Quoted 5 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> apart from those unused macros below, are these needed in public API header?
Quoted 6 lines
tools/lib/bpf/libbpf.h +0
-0#include "libbpf_common.h" @@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker);Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)these seem to be used only internally, why exposing them in public API?
No they're "aliases" for when you want to attach the filter directly to the interface (and thus install the clsact qdisc as the root). You can also use the filter with an existing qdisc (most commonly HTB), in which case you need to specify the qdisc handle as the root. We have a few examples of this use case:
Quoted 7 lines
Quoted 3 lines
https://github.com/xdp-project/bpf-examples/tree/master/traffic-pacing-edt
and
https://github.com/xdp-project/xdp-cpumap-tcI'm a bit puzzled, could you elaborate on your use case on why you wouldn't use the tc egress hook for those especially given it's guaranteed to run outside of root qdisc lock?
Jesper can correct me if I'm wrong, but I think the first one of the links above is basically his implementation of just that EDT-based shaper. And it works reasonably well, except you don't get the nice per-flow scheduling and sparse flow prioritisation like in FQ-CoDel unless you implement that yourself in BPF when you set the timestamps (and that is by no means trivial to implement).
So if you want to use any of the features of the existing qdiscs (I have also been suggesting to people that they use
tc_bpfif they want to customise sch_cake's notion of flows or shaping tiers), you need to be able to attach the filter to an existing qdisc. Sure, this means you're still stuck behind the qdisc lock, but for some applications that is fine (not everything is a data centre, some devices don't have that many CPUs anyway; and as the second example above shows, you can get around the qdisc lock by some clever use of partitioning of flows using cpumap).So what this boils down to is, we should keep the 'parent' parameter not just as an egress/ingress enum, but also as a field the user can fill out. I'm fine with moving the latter into the opts struct, though, so maybe the function parameter can be an enum like:
Quoted 7 lines
enum
bpf_tc_attach_point{
BPF_TC_CLSACT_INGRESS,
BPF_TC_CLSACT_EGRESS,
BPF_TC_QDISC_PARENT
};where if you set the last one you have to fill in the parent in opts?
Fair enough, I still think this is a bit backwards and should be discouraged given the constraints, but if you have an actual need for it ... I'd rather simplify API naming, the fact that it's clsact is an implementation detail and shouldn't matter to a user, like:
Quoted 8 lines
enum
bpf_tc_attach_point{
BPF_TC_INGRESS,
BPF_TC_EGRESS,
BPF_TC_CUSTOM_PARENT,
};For
BPF_TC_INGRESSandBPF_TC_EGRESS, I would enforce opts parent parameter to be /zero/ from the API.
OK, SGTM :)
-Toke
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
In reply to Kumar Kartikeya DwivediOn 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]
Quoted 14 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h"
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 6 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)
I would abstract those away into an enum, plus avoid having to pull in linux/pkt_sched.h and linux/tc_act/tc_bpf.h from main libbpf.h header.
Just add a enum { BPF_TC_DIR_INGRESS, BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;
Is this set anywhere?
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 786
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;
I'd remove class_id from here as well given in direct-action a BPF prog can set it if needed.
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;
What's the rationale for this padding?
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {
nit: maybe bpf_tc_ctx
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;
Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is prog_id.
The protocol, chain_index, and I would also include tag should be dropped. Similarly class_id given my earlier statement, and flags I would extend once this lib API would support offloading progs.
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 820
Quoted 11 lines
tools/lib/bpf/libbpf.h +11
-0+}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id, +const struct bpf_tc_opts *opts, +struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id, +struct bpf_tc_info *info);
As per above, for parent_id I'd replace with dir enum.
Quoted 4 lines
tools/lib/bpf/libbpf.h +1
-0+ #ifdef __cplusplus }/* extern "C" */ #endif
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 23 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 12 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 781
Quoted 5 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.
Quoted 3 lines
Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.
Ok, will do.
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
This is needed by the OPTS_* infrastructure.
Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.
Ok, makes sense.
Quoted 6 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
dde7b3f5f2f4 ("libbpf: Add explicit padding to bpf_xdp_set_link_opts")
Quoted 9 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctx
Noted.
Quoted 17 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.
Quoted 1 line
The protocol,
chain_index, and I would also include tag should be dropped.
A future user of this API needs to know the tag, so I would like to keep that. The rest we can drop, and probably document the default values explicitly.
Quoted 21 lines
Similarly
class_idgiven my earlier statement, and flags I would extend once this lib API would support offloading progs.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 820
Quoted 11 lines
tools/lib/bpf/libbpf.h +11
-0+}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id, +const struct bpf_tc_opts *opts, +struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id, +struct bpf_tc_info *info);As per above, for
parent_idI'd replace with dir enum.Quoted 4 lines
tools/lib/bpf/libbpf.h +1
-0+ #ifdef __cplusplus }/* extern "C" */ #endif
--
Kartikeya
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 44 lines · Kumar Kartikeya Dwivedi
Quoted 23 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 12 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 781
Quoted 5 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.Quoted 2 lines
Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.Ok, will do.
Quoted 4 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
This is needed by the OPTS_* infrastructure.
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.Ok, makes sense.
Quoted 7 lines
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
dde7b3f5f2f4("libbpf: Add explicit padding tobpf_xdp_set_link_opts")
Hm, fair enough.
Quoted 29 lines
Quoted 9 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctxNoted.
Quoted 17 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.
Quoted 4 lines
Quoted 1 line
The protocol,
chain_index, and I would also include tag should be dropped.A future user of this API needs to know the tag, so I would like to keep that. The rest we can drop, and probably document the default values explicitly.
Couldn't this be added along with the future patch for the [future] user?
The tag should be the tag of the prog itself, so if you have prog_id, you could also retrieve the same tag from the prog. The tag was basically from the early days where we didn't have bpf_prog_get_info_by_fd().
What does that future user need to do different here?
Thanks,
Daniel
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 84 lines · Daniel Borkmann
Quoted 44 lines · Kumar Kartikeya Dwivedi
Quoted 23 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 12 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 781
Quoted 5 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.Quoted 2 lines
Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.Ok, will do.
Quoted 4 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
This is needed by the OPTS_* infrastructure.
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.Ok, makes sense.
Quoted 7 lines
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
dde7b3f5f2f4("libbpf: Add explicit padding tobpf_xdp_set_link_opts")Hm, fair enough.
Quoted 29 lines
Quoted 9 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctxNoted.
Quoted 17 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
The protocol,
chain_index, and I would also include tag should be dropped.A future user of this API needs to know the tag, so I would like to keep that. The rest we can drop, and probably document the default values explicitly.
Couldn't this be added along with the future patch for the [future] user?
True.
Quoted 6 lines
The tag should be the tag of the prog itself, so if you have
prog_id, you could also retrieve the same tag from the prog. The tag was basically from the early days where we didn't havebpf_prog_get_info_by_fd().What does that future user need to do different here?
From Shaun Crampton:
"My particular use case is to load a program, link it with its maps and then check if its tag matches the existing program on the interface (and if so, abort the update)"
Also CC'd, they would be able to elaborate better, and whether or not dropping it is ok.
Quoted 2 lines
Thanks,
Daniel
--
Kartikeya
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 99 lines · Kumar Kartikeya Dwivedi
Quoted 83 lines · Daniel Borkmann
Quoted 44 lines · Kumar Kartikeya Dwivedi
Quoted 23 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 12 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 781
Quoted 5 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.Quoted 2 lines
Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.Ok, will do.
Quoted 4 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
This is needed by the OPTS_* infrastructure.
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.Ok, makes sense.
Quoted 7 lines
Quoted 5 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
dde7b3f5f2f4("libbpf: Add explicit padding tobpf_xdp_set_link_opts")Hm, fair enough.
Quoted 28 lines
Quoted 8 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctxNoted.
Quoted 17 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The protocol,
chain_index, and I would also include tag should be dropped.A future user of this API needs to know the tag, so I would like to keep that. The rest we can drop, and probably document the default values explicitly.
Couldn't this be added along with the future patch for the [future] user?
True.
Quoted 5 lines
The tag should be the tag of the prog itself, so if you have
prog_id, you could also retrieve the same tag from the prog. The tag was basically from the early days where we didn't havebpf_prog_get_info_by_fd().What does that future user need to do different here?
Quoted 7 lines
From Shaun Crampton:
"My particular use case is to load a program, link it with its maps and then check if its tag matches the existing program on the interface (and if so, abort the update)"Also CC'd, they would be able to elaborate better, and whether or not dropping it is ok.
Nope, just get it from the prog itself.
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 1 line
Nope, just get it from the prog itself.
Looks like the API returns the prog ID, so from that we can look up the prog and then get its tag? If so that meets our needs. Alternatively, if the API allows
for atomic replacement of a BPF program with another, that'd also work for us.
The use case is that our daemon is restarted and it doesn't know what BPF program was previously loaded. We want to make sure we end up with the latest version loaded, either by doing a seamless replace with the latest version
or by checking if the loaded version matches the latest.
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 6 lines · Shaun Crampton
Quoted 1 line
Nope, just get it from the prog itself.
Looks like the API returns the prog ID, so from that we can look up the prog and then get its tag? If so that meets our needs. Alternatively, if the API allows
for atomic replacement of a BPF program with another, that'd also work for us.
Both is the case: from prog ID you can already retrieve that same tag, and progs can be atomically replaced with the current API code.
Exposing the tag in here otherwise feels just odd/wrong from a design PoV, explain that to a user of this API on /why/ such field is in the tc API when it already can be retrieved via bpf_prog_get_info_by_fd(), as in, what is so special on this field in the tc API here (aside from legacy reasons when there was no mentioned helper [which we don't need to support given it dates way too far back]) ... I cannot. ;-) Hence lets drop it from there.
Thanks,
Daniel
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
In reply to Daniel BorkmannQuoted 47 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 14 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 6 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 786
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
Quoted 8 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctx
ok, so wait. It seems like apart from INGRESS|EGRESS enum and ifindex, everything else is optional and/or has some sane defaults, right? So this bpf_tc_attach_id or bpf_tc_ctx seems a bit artificial construct and it will cause problems for extending this.
So if my understanding is correct, I'd get rid of it completely. As I said previously, opts allow returning parameters back, so if user didn't specify handle and priority and kernel picks values on user's behalf, we can return them in the same opts fields.
For detach, again, ifindex and INGRESS|EGRESS is sufficient, but if user want to provide more detailed parameters, we should do that through extensible opts. That way we can keep growing this easily, plus simple cases will remain simple.
Similarly bpf_tc_info below, there is no need to have struct bpf_tc_attach_id id; field, just have handle and priority right there. And bpf_tc_info should use OPTS framework for extensibility (though opts name doesn't fit it very well, but it is still nice for extensibility and for doing optional input/output params).
Does this make sense? Am I missing something crucial here?
The general rule with any new structs added to libbpf APIs is to either be 100% (ok, 99.99%) sure that they will never be changed, or do forward/backward compatible OPTS. Any other thing is pain and calls for symbol versioning, which we are trying really hard to avoid.
Quoted 40 lines
Quoted 4 lines
tools/lib/bpf/libbpf.h +3
-0+__u32 handle; +__u16 priority; +};tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 10 lines
tools/lib/bpf/libbpf.h +10
-0+ +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.The protocol,
chain_index, and I would also include tag should be dropped. Similarlyclass_idgiven my earlier statement, and flags I would extend once this lib API would support offloading progs.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 820
Quoted 11 lines
tools/lib/bpf/libbpf.h +11
-0+}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id, +const struct bpf_tc_opts *opts, +struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id, +struct bpf_tc_info *info);As per above, for
parent_idI'd replace with dir enum.Quoted 4 lines
tools/lib/bpf/libbpf.h +1
-0+ #ifdef __cplusplus }/* extern "C" */ #endif
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 71 lines · Andrii Nakryiko
Quoted 46 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 14 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 6 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 786
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
Quoted 8 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctxok, so wait. It seems like apart from INGRESS|EGRESS enum and ifindex, everything else is optional and/or has some sane defaults, right? So this
bpf_tc_attach_idorbpf_tc_ctxseems a bit artificial construct and it will cause problems for extending this.So if my understanding is correct, I'd get rid of it completely. As I said previously, opts allow returning parameters back, so if user didn't specify handle and priority and kernel picks values on user's behalf, we can return them in the same opts fields.
For detach, again, ifindex and INGRESS|EGRESS is sufficient, but if user want to provide more detailed parameters, we should do that through extensible opts. That way we can keep growing this easily, plus simple cases will remain simple.
Quoted 7 lines
Similarly
bpf_tc_infobelow, there is no need to have structbpf_tc_attach_idid; field, just have handle and priority right there. Andbpf_tc_infoshould use OPTS framework for extensibility (though opts name doesn't fit it very well, but it is still nice for extensibility and for doing optional input/output params).Does this make sense? Am I missing something crucial here?
I would probably keep the handle + priority in there; maybe if both are 0, we could fix it to some default value internally, but without those it might be a bit hard if people want to build a 'pipeline' of cls_bpf progs if they need/want to.
Potentially, one could fixate the handle itself, and then allow to specify different priorities for it such that when a BPF prog returns a TC_ACT_UNSPEC, it will exec the next one inside that cls_bpf instance, every other TC_ACT_* opcode will terminate the processing. Technically, only priority would really be needed (unless you combine multiple different classifiers from tc side on the ingress/egress hook which is not great to begin with, tbh).
Quoted 44 lines
The general rule with any new structs added to libbpf APIs is to either be 100% (ok, 99.99%) sure that they will never be changed, or do forward/backward compatible OPTS. Any other thing is pain and calls for symbol versioning, which we are trying really hard to avoid.
Quoted 39 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.The protocol,
chain_index, and I would also include tag should be dropped. Similarlyclass_idgiven my earlier statement, and flags I would extend once this lib API would support offloading progs.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 820
Quoted 11 lines
tools/lib/bpf/libbpf.h +11
-0+}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id, +const struct bpf_tc_opts *opts, +struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id, +struct bpf_tc_info *info);As per above, for
parent_idI'd replace with dir enum.Quoted 4 lines
tools/lib/bpf/libbpf.h +1
-0+ #ifdef __cplusplus }/* extern "C" */ #endif
Re: [PATCH bpf-next v3 2/3] libbpf: add low level TC-BPF API
Quoted 79 lines · Daniel Borkmann
Quoted 77 lines · Andrii Nakryiko
Quoted 46 lines · Daniel Borkmann
On 4/20/21 9:37 PM, Kumar Kartikeya Dwivedi wrote:
[...]Quoted 14 lines
tools/lib/bpf/libbpf.h +2
-0@@ -16,6 +16,8 @@#include <stdbool.h> #include <sys/types.h> // for size_t #include <linux/bpf.h> +#include <linux/pkt_sched.h> +#include <linux/tc_act/tc_bpf.h> #include "libbpf_common.h" tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 782
Quoted 6 lines
tools/lib/bpf/libbpf.h +3
-0@@ -775,6 +777,48 @@ LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen LIBBPF_API int bpf_linker__finalize(struct bpf_linker *linker); LIBBPF_API void bpf_linker__free(struct bpf_linker *linker); +/* Convenience macros for the clsact attach hooks */ +#define BPF_TC_CLSACT_INGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS) +#define BPF_TC_CLSACT_EGRESS TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS)I would abstract those away into an enum, plus avoid having to pull in
linux/pkt_sched.handlinux/tc_act/tc_bpf.hfrom mainlibbpf.hheader.Just add a enum {
BPF_TC_DIR_INGRESS,BPF_TC_DIR_EGRESS, } and then the concrete tc bits (TC_H_MAKE()) can be translated internally.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 785
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+struct bpf_tc_opts { +size_t sz;Is this set anywhere?
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 786
Quoted 2 lines
tools/lib/bpf/libbpf.h +2
-0+__u32 handle; +__u32 class_id;I'd remove
class_idfrom here as well given in direct-action a BPF prog can set it if needed.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 790
Quoted 3 lines
tools/lib/bpf/libbpf.h +3
-0+__u16 priority; +bool replace; + size_t :0;What's the rationale for this padding?
Quoted 8 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 796
Quoted 6 lines
tools/lib/bpf/libbpf.h +6
-0+}; + +#define bpf_tc_opts__last_field replace + +/* Acts as a handle for an attached filter */ +struct bpf_tc_attach_id {nit: maybe
bpf_tc_ctxok, so wait. It seems like apart from INGRESS|EGRESS enum and ifindex, everything else is optional and/or has some sane defaults, right? So this
bpf_tc_attach_idorbpf_tc_ctxseems a bit artificial construct and it will cause problems for extending this.So if my understanding is correct, I'd get rid of it completely. As I said previously, opts allow returning parameters back, so if user didn't specify handle and priority and kernel picks values on user's behalf, we can return them in the same opts fields.
For detach, again, ifindex and INGRESS|EGRESS is sufficient, but if user want to provide more detailed parameters, we should do that through extensible opts. That way we can keep growing this easily, plus simple cases will remain simple.
Similarly
bpf_tc_infobelow, there is no need to have structbpf_tc_attach_idid; field, just have handle and priority right there. Andbpf_tc_infoshould use OPTS framework for extensibility (though opts name doesn't fit it very well, but it is still nice for extensibility and for doing optional input/output params).
Quoted 6 lines
Quoted 1 line
Does this make sense? Am I missing something crucial here?
I would probably keep the handle + priority in there; maybe if both are 0, we could fix it to some default value internally, but without those it might be a bit hard if people want to build a 'pipeline' of
cls_bpfprogs if they need/want to.
Oh, I'm not proposing to drop support for specifying handle and prio. I'm just saying having a fixed UAPI struct bpf_tc_attach_id as an "ID" is problematic from API stability point of view. So instead of pretending we know what "ID" will always be like, pass any extra non-default fields in OPTS struct. And if those are not specified by user (either opts is NULL or handle/prio is 0), use sane defaults, as you are proposing.
Quoted 53 lines
Potentially, one could fixate the handle itself, and then allow to specify different priorities for it such that when a BPF prog returns a
TC_ACT_UNSPEC, it will exec the next one inside thatcls_bpfinstance, every other TC_ACT_* opcode will terminate the processing. Technically, only priority would really be needed (unless you combine multiple different classifiers from tc side on the ingress/egress hook which is not great to begin with, tbh).Quoted 44 lines
The general rule with any new structs added to libbpf APIs is to either be 100% (ok, 99.99%) sure that they will never be changed, or do forward/backward compatible OPTS. Any other thing is pain and calls for symbol versioning, which we are trying really hard to avoid.
Quoted 39 lines
tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 809
Quoted 13 lines
tools/lib/bpf/libbpf.h +13
-0+__u32 handle; +__u16 priority; +}; + +struct bpf_tc_info { +struct bpf_tc_attach_id id; +__u16 protocol; +__u32 chain_index; +__u32 prog_id; +__u8 tag[BPF_TAG_SIZE ]; +__u32 class_id; +__u32 bpf_flags; +__u32 bpf_flags_gen;Given we do not yet have any setters e.g. for offload, etc, the one thing I'd see useful and crucial initially is
prog_id.The protocol,
chain_index, and I would also include tag should be dropped. Similarlyclass_idgiven my earlier statement, and flags I would extend once this lib API would support offloading progs.tools/lib/bpf/libbpf.h LIBBPF_API int bpf_linker__add_file(struct bpf_linker *linker, const char *filen · line 820
Quoted 11 lines
tools/lib/bpf/libbpf.h +11
-0+}; + +/* id is out parameter that will be written to, it must not be NULL */ +LIBBPF_API int bpf_tc_attach(int fd, __u32 ifindex, __u32 parent_id, +const struct bpf_tc_opts *opts, +struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_detach(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id); +LIBBPF_API int bpf_tc_get_info(__u32 ifindex, __u32 parent_id, +const struct bpf_tc_attach_id *id, +struct bpf_tc_info *info);As per above, for
parent_idI'd replace with dir enum.Quoted 4 lines
tools/lib/bpf/libbpf.h +1
-0+ #ifdef __cplusplus }/* extern "C" */ #endif
Kumar Kartikeya Dwivedi
maintainerPATCH v3 3/3 libbpf: add selftests for TC-BPF API
In reply to Kumar Kartikeya DwivediThis adds some basic tests for the low level bpf_tc_* API.
--- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +169 -0
@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/err.h> +#include <linux/limits.h> +#include <bpf/libbpf.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <test_progs.h> +#include <linux/if_ether.h> + +#define LO_IFINDEX 1 + +static int test_tc_internal (int fd,__u32 parent_id) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end; + + opts.replace = true; + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach in replace mode" )) +return ret; + +/* Demonstrate changing attributes */ + opts.class_id =TC_H_MAKE (1UL <<16 ,2 ); + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc attach in replace mode" )) + goto end; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,2 ), +"class_id incorrect after replace" )) + goto end; +if (!ASSERT_EQ (info.bpf_flags &TCA_BPF_FLAG_ACT_DIRECT ,1 , +"direct action mode not set" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX , parent_id, &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +int test_tc_info (int fd) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}, old; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + old = id; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end_old; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end_old; + +/* choose a priority */ + opts.priority =0 ; + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) + goto end_old; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_NEQ (id.priority, old.priority,"filter priority mismatch" )) + goto end; +if (!ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +end_old: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &old); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +void test_test_tc_bpf (void ) +{ +const char *file ="./test_tc_bpf_kern.o" ; +struct bpf_program *clsp; +struct bpf_object *obj; +int cls_fd, ret; + + obj =bpf_object__open (file); +if (!ASSERT_OK_PTR (obj,"bpf_object__open" )) +return ; + + clsp =bpf_object__find_program_by_title (obj,"classifier" ); +if (!ASSERT_OK_PTR (clsp,"bpf_object__find_program_by_title" )) + goto end; + + ret =bpf_object__load (obj); +if (!ASSERT_EQ (ret,0 ,"bpf_object__load" )) + goto end; + + cls_fd =bpf_program__fd (clsp); + +system ("tc qdisc del dev lo clsact" ); + + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_INGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal INGRESS" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_info (cls_fd); +if (!ASSERT_EQ (ret,0 ,"test_tc_info" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_EGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal EGRESS" )) + goto end; + +ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" ); + +end: +bpf_object__close (obj); +}
tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +/* Dummy prog to test TC-BPF API */ + +SEC("classifier") +int cls (struct __sk_buff *skb) +{ +return 0 ; +}
Signature · 1 line
2.30.2
Re: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API
Quoted 31 lines · Kumar Kartikeya Dwivedi
This adds some basic tests for the low level bpf_tc_* API.
Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+)create mode 100644
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.ccreate mode 100644tools/testing/selftests/bpf/progs/test_tc_bpf_kern.ctools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +14
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/err.h> +#include <linux/limits.h> +#include <bpf/libbpf.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <test_progs.h> +#include <linux/if_ether.h> + +#define LO_IFINDEX 1 +
[...]
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +5
-0+ +void test_test_tc_bpf (void ) +{ +const char *file ="./test_tc_bpf_kern.o" ; +struct bpf_program *clsp;
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 134
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +10
-0+struct bpf_object *obj; +int cls_fd, ret; + + obj =bpf_object__open (file); +if (!ASSERT_OK_PTR (obj,"bpf_object__open" )) +return ; + + clsp =bpf_object__find_program_by_title (obj,"classifier" ); +if (!ASSERT_OK_PTR (clsp,"bpf_object__find_program_by_title" )) + goto end;
Please use bpf_object__find_program_by_name() API.
Quoted 54 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +35
-0+ + ret =bpf_object__load (obj); +if (!ASSERT_EQ (ret,0 ,"bpf_object__load" )) + goto end; + + cls_fd =bpf_program__fd (clsp); + +system ("tc qdisc del dev lo clsact" ); + + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_INGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal INGRESS" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_info (cls_fd); +if (!ASSERT_EQ (ret,0 ,"test_tc_info" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_EGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal EGRESS" )) + goto end; + +ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" ); + +end: +bpf_object__close (obj); +}tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c new file +12
-0@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +/* Dummy prog to test TC-BPF API */ + +SEC("classifier") +int cls (struct __sk_buff *skb) +{ +return 0 ; +}
Re: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API
In reply to Kumar Kartikeya DwivediQuoted 10 lines · Kumar Kartikeya Dwivedi
This adds some basic tests for the low level bpf_tc_* API.
Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c
we normally don't call prog_test's files with "test_" prefix, it can be just tc_bpf.c (or just tc.c)
Quoted 1 line
create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c
we also don't typically call BPF source code files with _kern suffix, just test_tc_bpf.c would be more in line with most common case
Quoted 118 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +111
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/err.h> +#include <linux/limits.h> +#include <bpf/libbpf.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <test_progs.h> +#include <linux/if_ether.h> + +#define LO_IFINDEX 1 + +static int test_tc_internal (int fd,__u32 parent_id) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end; + + opts.replace = true; + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach in replace mode" )) +return ret; + +/* Demonstrate changing attributes */ + opts.class_id =TC_H_MAKE (1UL <<16 ,2 ); + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc attach in replace mode" )) + goto end; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,2 ), +"class_id incorrect after replace" )) + goto end; +if (!ASSERT_EQ (info.bpf_flags &TCA_BPF_FLAG_ACT_DIRECT ,1 , +"direct action mode not set" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX , parent_id, &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +int test_tc_info (int fd) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}, old; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + old = id; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end_old; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end_old; + +/* choose a priority */ + opts.priority =0 ; + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) + goto end_old; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_NEQ (id.priority, old.priority,"filter priority mismatch" )) + goto end; +if (!ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" )) + goto end; +
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 121
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +10
-0+end: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +end_old: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &old); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +void test_test_tc_bpf (void )
test_test_ tautology, drop one test?
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 123
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +2
-0+{ +const char *file ="./test_tc_bpf_kern.o" ;
please use BPF skeleton instead, see lots of other selftests doing that already. You won't even need find_program_by_{name,title}, among other things.
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +9
-0+struct bpf_program *clsp; +struct bpf_object *obj; +int cls_fd, ret; + + obj =bpf_object__open (file); +if (!ASSERT_OK_PTR (obj,"bpf_object__open" )) +return ; + + clsp =bpf_object__find_program_by_title (obj,"classifier" );
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 142
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +10
-0+if (!ASSERT_OK_PTR (clsp,"bpf_object__find_program_by_title" )) + goto end; + + ret =bpf_object__load (obj); +if (!ASSERT_EQ (ret,0 ,"bpf_object__load" )) + goto end; + + cls_fd =bpf_program__fd (clsp); + +system ("tc qdisc del dev lo clsact" );
can this fail? also why is this necessary? it's still not possible to do anything with only libbpf APIs?
Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +27
-0+ + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_INGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal INGRESS" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_info (cls_fd); +if (!ASSERT_EQ (ret,0 ,"test_tc_info" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_EGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal EGRESS" )) + goto end; + +ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" ); + +end: +bpf_object__close (obj); +}tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c new file +12
-1@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +/* Dummy prog to test TC-BPF API */ + +SEC("classifier") +int cls (struct __sk_buff *skb) +{ +return 0 ; +} --2.30.2
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API
Quoted 13 lines · Andrii Nakryiko
Quoted 10 lines · Kumar Kartikeya Dwivedi
This adds some basic tests for the low level bpf_tc_* API.
Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c
Quoted 3 lines
we normally don't call prog_test's files with "
test_" prefix, it can be justtc_bpf.c(or justtc.c)
Ok, will rename.
Quoted 5 lines
Quoted 1 line
create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.cwe also don't typically call BPF source code files with
_kernsuffix, justtest_tc_bpf.cwould be more in line with most common case
Will rename.
Quoted 122 lines
Quoted 122 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +115
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/err.h> +#include <linux/limits.h> +#include <bpf/libbpf.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <test_progs.h> +#include <linux/if_ether.h> + +#define LO_IFINDEX 1 + +static int test_tc_internal (int fd,__u32 parent_id) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end; + + opts.replace = true; + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach in replace mode" )) +return ret; + +/* Demonstrate changing attributes */ + opts.class_id =TC_H_MAKE (1UL <<16 ,2 ); + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc attach in replace mode" )) + goto end; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,2 ), +"class_id incorrect after replace" )) + goto end; +if (!ASSERT_EQ (info.bpf_flags &TCA_BPF_FLAG_ACT_DIRECT ,1 , +"direct action mode not set" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX , parent_id, &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +int test_tc_info (int fd) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}, old; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + old = id; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end_old; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end_old; + +/* choose a priority */ + opts.priority =0 ; + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) + goto end_old; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_NEQ (id.priority, old.priority,"filter priority mismatch" )) + goto end; +if (!ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +end_old:
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 121
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +6
-0+ ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &old); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +void test_test_tc_bpf (void )
test_test_tautology, drop one test?
Ok.
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 123
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +2
-0+{ +const char *file ="./test_tc_bpf_kern.o" ;please use BPF skeleton instead, see lots of other selftests doing that already. You won't even need find_program_by_{name,title}, among other things.
Sounds good, will change.
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +13
-0+struct bpf_program *clsp; +struct bpf_object *obj; +int cls_fd, ret; + + obj =bpf_object__open (file); +if (!ASSERT_OK_PTR (obj,"bpf_object__open" )) +return ; + + clsp =bpf_object__find_program_by_title (obj,"classifier" ); +if (!ASSERT_OK_PTR (clsp,"bpf_object__find_program_by_title" )) + goto end; + + ret =bpf_object__load (obj);
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 142
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +6
-0+if (!ASSERT_EQ (ret,0 ,"bpf_object__load" )) + goto end; + + cls_fd =bpf_program__fd (clsp); + +system ("tc qdisc del dev lo clsact" );can this fail? also why is this necessary? it's still not possible to
This is just removing any existing clsact qdisc since it will be setup by the attach call, which is again removed later (where we do check if it fails, if it does clsact qdisc was not setup, and something was wrong in that it returned 0 when the attach point was one of the clsact hooks).
We don't care about failure initially, since if it isn't present we'd just move on to running the test.
Quoted 2 lines
do anything with only libbpf APIs?
I don't think so, I can do the qdisc teardown using netlink in the selftest, but that would mean duplicating a lot of code. I think expecting tc to be present on the system is a reasonable assumption for this test.
Quoted 40 lines
Quoted 40 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +27
-0+ + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_INGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal INGRESS" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_info (cls_fd); +if (!ASSERT_EQ (ret,0 ,"test_tc_info" )) + goto end; + +if (!ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" )) + goto end; + + ret =test_tc_internal (cls_fd,BPF_TC_CLSACT_EGRESS ); +if (!ASSERT_EQ (ret,0 ,"test_tc_internal EGRESS" )) + goto end; + +ASSERT_EQ (system ("tc qdisc del dev lo clsact" ),0 , +"clsact qdisc delete failed" ); + +end: +bpf_object__close (obj); +}tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c new file +7
-0@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +/* Dummy prog to test TC-BPF API */ +
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c new file +5
-1+SEC("classifier") +int cls (struct __sk_buff *skb) +{ +return 0 ; +} --2.30.2
--
Kartikeya
Re: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API
Quoted 202 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Andrii Nakryiko
Quoted 10 lines · Kumar Kartikeya Dwivedi
This adds some basic tests for the low level bpf_tc_* API.
Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.cQuoted 3 lines
we normally don't call prog_test's files with "
test_" prefix, it can be justtc_bpf.c(or justtc.c)Ok, will rename.
Quoted 5 lines
Quoted 1 line
create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.cwe also don't typically call BPF source code files with
_kernsuffix, justtest_tc_bpf.cwould be more in line with most common caseWill rename.
Quoted 122 lines
Quoted 122 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +115
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/err.h> +#include <linux/limits.h> +#include <bpf/libbpf.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <test_progs.h> +#include <linux/if_ether.h> + +#define LO_IFINDEX 1 + +static int test_tc_internal (int fd,__u32 parent_id) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end; + + opts.replace = true; + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach in replace mode" )) +return ret; + +/* Demonstrate changing attributes */ + opts.class_id =TC_H_MAKE (1UL <<16 ,2 ); + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc attach in replace mode" )) + goto end; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,2 ), +"class_id incorrect after replace" )) + goto end; +if (!ASSERT_EQ (info.bpf_flags &TCA_BPF_FLAG_ACT_DIRECT ,1 , +"direct action mode not set" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX , parent_id, &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +int test_tc_info (int fd) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}, old; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + old = id; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end_old; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end_old; + +/* choose a priority */ + opts.priority =0 ; + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) + goto end_old; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_NEQ (id.priority, old.priority,"filter priority mismatch" )) + goto end; +if (!ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +end_old:Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 121
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +6
-0+ ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &old); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +void test_test_tc_bpf (void )
test_test_tautology, drop one test?Ok.
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 123
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +2
-0+{ +const char *file ="./test_tc_bpf_kern.o" ;please use BPF skeleton instead, see lots of other selftests doing that already. You won't even need find_program_by_{name,title}, among other things.
Sounds good, will change.
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +13
-0+struct bpf_program *clsp; +struct bpf_object *obj; +int cls_fd, ret; + + obj =bpf_object__open (file); +if (!ASSERT_OK_PTR (obj,"bpf_object__open" )) +return ; + + clsp =bpf_object__find_program_by_title (obj,"classifier" ); +if (!ASSERT_OK_PTR (clsp,"bpf_object__find_program_by_title" )) + goto end; + + ret =bpf_object__load (obj);Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 142
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +6
-0+if (!ASSERT_EQ (ret,0 ,"bpf_object__load" )) + goto end; + + cls_fd =bpf_program__fd (clsp); + +system ("tc qdisc del dev lo clsact" );can this fail? also why is this necessary? it's still not possible to
This is just removing any existing clsact qdisc since it will be setup by the attach call, which is again removed later (where we do check if it fails, if it does clsact qdisc was not setup, and something was wrong in that it returned 0 when the attach point was one of the clsact hooks).
We don't care about failure initially, since if it isn't present we'd just move on to running the test.
Quoted 6 lines
Quoted 2 lines
do anything with only libbpf APIs?
I don't think so, I can do the qdisc teardown using netlink in the selftest, but that would mean duplicating a lot of code. I think expecting tc to be present on the system is a reasonable assumption for this test.
So this stems from the fact that bpf_tc_detach() doesn't clean up the clsact qdisc that is added by bpf_tc_attach(). I think we should fix this.
Andrii, Kumar and I discussed this, and concluded that the best we can do from userspace right now is query the number of filters before remove and if there's only one, also remove the clsact qdisc. This is racy in that a new filter can be attached between the check and the remove, but to fix that we need a way for the filter to take the ref on the qdisc. Since something like this will be needed for a bpf_link attach mode as well, we figured that can be added as part of such a series, and we'll just do the best-effort thing now. WDYT?
-Toke
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API
Quoted 206 lines · Toke Høiland-Jørgensen
Quoted 204 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Andrii Nakryiko
Quoted 10 lines · Kumar Kartikeya Dwivedi
This adds some basic tests for the low level bpf_tc_* API.
Reviewed-by Toke Høiland-Jørgensen <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+)create mode 100644
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.cQuoted 3 lines
we normally don't call prog_test's files with "
test_" prefix, it can be justtc_bpf.c(or justtc.c)Ok, will rename.
Quoted 5 lines
Quoted 1 line
create mode 100644
tools/testing/selftests/bpf/progs/test_tc_bpf_kern.cwe also don't typically call BPF source code files with
_kernsuffix, justtest_tc_bpf.cwould be more in line with most common caseWill rename.
Quoted 122 lines
Quoted 122 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +115
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/err.h> +#include <linux/limits.h> +#include <bpf/libbpf.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <test_progs.h> +#include <linux/if_ether.h> + +#define LO_IFINDEX 1 + +static int test_tc_internal (int fd,__u32 parent_id) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end; + + opts.replace = true; + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach in replace mode" )) +return ret; + +/* Demonstrate changing attributes */ + opts.class_id =TC_H_MAKE (1UL <<16 ,2 ); + + ret =bpf_tc_attach (fd,LO_IFINDEX , parent_id, &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc attach in replace mode" )) + goto end; + + ret =bpf_tc_get_info (LO_IFINDEX , parent_id, &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,2 ), +"class_id incorrect after replace" )) + goto end; +if (!ASSERT_EQ (info.bpf_flags &TCA_BPF_FLAG_ACT_DIRECT ,1 , +"direct action mode not set" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX , parent_id, &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +int test_tc_info (int fd) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); +struct bpf_tc_attach_id id = {}, old; +struct bpf_tc_info info = {}; +int ret; + + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) +return ret; + old = id; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end_old; + +if (!ASSERT_EQ (info.id.handle, id.handle,"handle mismatch" ) || + !ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" ) || + !ASSERT_EQ (info.id.handle,1 ,"handle incorrect" ) || + !ASSERT_EQ (info.chain_index,0 ,"chain_index incorrect" ) || + !ASSERT_EQ (info.id.priority,10 ,"priority incorrect" ) || + !ASSERT_EQ (info.class_id,TC_H_MAKE (1UL <<16 ,1 ), +"class_id incorrect" ) || + !ASSERT_EQ (info.protocol,ETH_P_ALL ,"protocol incorrect" )) + goto end_old; + +/* choose a priority */ + opts.priority =0 ; + ret =bpf_tc_attach (fd,LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &opts, &id); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_attach" )) + goto end_old; + + ret =bpf_tc_get_info (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id, &info); +if (!ASSERT_EQ (ret,0 ,"bpf_tc_get_info" )) + goto end; + +if (!ASSERT_NEQ (id.priority, old.priority,"filter priority mismatch" )) + goto end; +if (!ASSERT_EQ (info.id.priority, id.priority,"priority mismatch" )) + goto end; + +end: + ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &id); +ASSERT_EQ (ret,0 ,"detach failed" ); +end_old:Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 121
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +6
-0+ ret =bpf_tc_detach (LO_IFINDEX ,BPF_TC_CLSACT_INGRESS , &old); +ASSERT_EQ (ret,0 ,"detach failed" ); +return ret; +} + +void test_test_tc_bpf (void )
test_test_tautology, drop one test?Ok.
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 123
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +2
-0+{ +const char *file ="./test_tc_bpf_kern.o" ;please use BPF skeleton instead, see lots of other selftests doing that already. You won't even need find_program_by_{name,title}, among other things.
Sounds good, will change.
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +13
-0+struct bpf_program *clsp; +struct bpf_object *obj; +int cls_fd, ret; + + obj =bpf_object__open (file); +if (!ASSERT_OK_PTR (obj,"bpf_object__open" )) +return ; + + clsp =bpf_object__find_program_by_title (obj,"classifier" ); +if (!ASSERT_OK_PTR (clsp,"bpf_object__find_program_by_title" )) + goto end; + + ret =bpf_object__load (obj);Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c line 142
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file +6
-0+if (!ASSERT_EQ (ret,0 ,"bpf_object__load" )) + goto end; + + cls_fd =bpf_program__fd (clsp); + +system ("tc qdisc del dev lo clsact" );can this fail? also why is this necessary? it's still not possible to
This is just removing any existing clsact qdisc since it will be setup by the attach call, which is again removed later (where we do check if it fails, if it does clsact qdisc was not setup, and something was wrong in that it returned 0 when the attach point was one of the clsact hooks).
We don't care about failure initially, since if it isn't present we'd just move on to running the test.
Quoted 1 line
do anything with only libbpf APIs?
Quoted 7 lines
Quoted 3 lines
I don't think so, I can do the qdisc teardown using netlink in the selftest, but that would mean duplicating a lot of code. I think expecting tc to be present on the system is a reasonable assumption for this test.
So this stems from the fact that
bpf_tc_detach()doesn't clean up the clsact qdisc that is added bybpf_tc_attach(). I think we should fix this.
I was wondering whether it would make sense to add a bpf_tc_ctx_init() and bpf_tc_ctx_destroy() API which would auto-create the sch_clsact qdisc, plus provide a 'handle' for bpf_tc_attach() and bpf_tc_detach(), and for the other one, it would delete the qdisc. Otoh, if an empty sch_clsact obj is sitting around while not being great (given minor effect on fast-path), it also doesn't harm /overly/ much. Maybe a /poor/ analogy could be that if you open a v6 socket, it pulls in the ipv6 module, but also doesn't remove it when you close() it. Anyway, but for the test itself, given you can define prio etc, I don't think it would even need the system() call?
Thanks,
Daniel