diff mbox series

[bpf-next,v1,11/13] libbpf: Support creating and destroying qdisc

Message ID 20241213232958.2388301-12-amery.hung@bytedance.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf qdisc | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/apply fail Patch does not apply to bpf-next-0
bpf/vmtest-bpf-net-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-net-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-net-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-net-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-net-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-net-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-10 success Logs for aarch64-gcc / veristat-kernel
bpf/vmtest-bpf-net-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-11 success Logs for aarch64-gcc / veristat-meta
bpf/vmtest-bpf-net-VM_Test-13 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-net-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-12 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-net-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-net-VM_Test-17 success Logs for s390x-gcc / veristat-kernel
bpf/vmtest-bpf-net-VM_Test-18 success Logs for s390x-gcc / veristat-meta
bpf/vmtest-bpf-net-VM_Test-19 success Logs for set-matrix
bpf/vmtest-bpf-net-VM_Test-20 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-21 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-net-VM_Test-22 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-26 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-27 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-28 success Logs for x86_64-gcc / veristat-kernel / x86_64-gcc veristat_kernel
bpf/vmtest-bpf-net-VM_Test-29 fail Logs for x86_64-gcc / veristat-meta / x86_64-gcc veristat_meta
bpf/vmtest-bpf-net-VM_Test-30 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-31 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-net-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-34 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-35 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-net-VM_Test-36 success Logs for x86_64-llvm-17 / veristat-kernel
bpf/vmtest-bpf-net-VM_Test-37 success Logs for x86_64-llvm-17 / veristat-meta
bpf/vmtest-bpf-net-VM_Test-38 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-39 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-net-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-44 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-45 success Logs for x86_64-llvm-18 / veristat-kernel
bpf/vmtest-bpf-net-VM_Test-46 success Logs for x86_64-llvm-18 / veristat-meta
bpf/vmtest-bpf-net-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-net-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-net-VM_Test-23 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-net-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-42 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-net-VM_Test-43 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-net-PR fail PR summary
bpf/vmtest-bpf-net-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc

Commit Message

Amery Hung Dec. 13, 2024, 11:29 p.m. UTC
Extend struct bpf_tc_hook with handle, qdisc name and a new attach type,
BPF_TC_QDISC, to allow users to add or remove any qdisc specified in
addition to clsact.

Signed-off-by: Amery Hung <amery.hung@bytedance.com>
---
 tools/lib/bpf/libbpf.h  |  5 ++++-
 tools/lib/bpf/netlink.c | 20 +++++++++++++++++---
 2 files changed, 21 insertions(+), 4 deletions(-)

Comments

Andrii Nakryiko Dec. 17, 2024, 6:32 p.m. UTC | #1
On Fri, Dec 13, 2024 at 3:30 PM Amery Hung <amery.hung@bytedance.com> wrote:
>
> Extend struct bpf_tc_hook with handle, qdisc name and a new attach type,
> BPF_TC_QDISC, to allow users to add or remove any qdisc specified in
> addition to clsact.
>
> Signed-off-by: Amery Hung <amery.hung@bytedance.com>
> ---
>  tools/lib/bpf/libbpf.h  |  5 ++++-
>  tools/lib/bpf/netlink.c | 20 +++++++++++++++++---
>  2 files changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index b2ce3a72b11d..b05d95814776 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -1268,6 +1268,7 @@ enum bpf_tc_attach_point {
>         BPF_TC_INGRESS = 1 << 0,
>         BPF_TC_EGRESS  = 1 << 1,
>         BPF_TC_CUSTOM  = 1 << 2,
> +       BPF_TC_QDISC   = 1 << 3,
>  };
>
>  #define BPF_TC_PARENT(a, b)    \
> @@ -1282,9 +1283,11 @@ struct bpf_tc_hook {
>         int ifindex;
>         enum bpf_tc_attach_point attach_point;
>         __u32 parent;
> +       __u32 handle;
> +       char *qdisc;

const char *?

>         size_t :0;
>  };
> -#define bpf_tc_hook__last_field parent
> +#define bpf_tc_hook__last_field qdisc
>
>  struct bpf_tc_opts {
>         size_t sz;
> diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
> index 68a2def17175..72db8c0add21 100644
> --- a/tools/lib/bpf/netlink.c
> +++ b/tools/lib/bpf/netlink.c
> @@ -529,9 +529,9 @@ int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id)
>  }
>
>
> -typedef int (*qdisc_config_t)(struct libbpf_nla_req *req);
> +typedef int (*qdisc_config_t)(struct libbpf_nla_req *req, struct bpf_tc_hook *hook);

should hook pointer be const?

>
> -static int clsact_config(struct libbpf_nla_req *req)
> +static int clsact_config(struct libbpf_nla_req *req, struct bpf_tc_hook *hook)

const?

>  {
>         req->tc.tcm_parent = TC_H_CLSACT;
>         req->tc.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
> @@ -539,6 +539,16 @@ static int clsact_config(struct libbpf_nla_req *req)
>         return nlattr_add(req, TCA_KIND, "clsact", sizeof("clsact"));
>  }
>
> +static int qdisc_config(struct libbpf_nla_req *req, struct bpf_tc_hook *hook)

same, const, it's not written into, right?

> +{
> +       char *qdisc = OPTS_GET(hook, qdisc, NULL);
> +
> +       req->tc.tcm_parent = OPTS_GET(hook, parent, TC_H_ROOT);
> +       req->tc.tcm_handle = OPTS_GET(hook, handle, 0);
> +
> +       return nlattr_add(req, TCA_KIND, qdisc, strlen(qdisc) + 1);
> +}
> +
>  static int attach_point_to_config(struct bpf_tc_hook *hook,
>                                   qdisc_config_t *config)
>  {
> @@ -552,6 +562,9 @@ static int attach_point_to_config(struct bpf_tc_hook *hook,
>                 return 0;
>         case BPF_TC_CUSTOM:
>                 return -EOPNOTSUPP;
> +       case BPF_TC_QDISC:
> +               *config = &qdisc_config;
> +               return 0;
>         default:
>                 return -EINVAL;
>         }
> @@ -596,7 +609,7 @@ static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags)
>         req.tc.tcm_family  = AF_UNSPEC;
>         req.tc.tcm_ifindex = OPTS_GET(hook, ifindex, 0);
>
> -       ret = config(&req);
> +       ret = config(&req, hook);
>         if (ret < 0)
>                 return ret;
>
> @@ -639,6 +652,7 @@ int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
>         case BPF_TC_INGRESS:
>         case BPF_TC_EGRESS:
>                 return libbpf_err(__bpf_tc_detach(hook, NULL, true));
> +       case BPF_TC_QDISC:
>         case BPF_TC_INGRESS | BPF_TC_EGRESS:
>                 return libbpf_err(tc_qdisc_delete(hook));
>         case BPF_TC_CUSTOM:
> --
> 2.20.1
>
Amery Hung Dec. 17, 2024, 7:08 p.m. UTC | #2
You are right. I will add const to the hook argument and bpf_tc_hook->qdisc.

Thanks,
Amery

On Tue, Dec 17, 2024 at 10:32 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Dec 13, 2024 at 3:30 PM Amery Hung <amery.hung@bytedance.com> wrote:
> >
> > Extend struct bpf_tc_hook with handle, qdisc name and a new attach type,
> > BPF_TC_QDISC, to allow users to add or remove any qdisc specified in
> > addition to clsact.
> >
> > Signed-off-by: Amery Hung <amery.hung@bytedance.com>
> > ---
> >  tools/lib/bpf/libbpf.h  |  5 ++++-
> >  tools/lib/bpf/netlink.c | 20 +++++++++++++++++---
> >  2 files changed, 21 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> > index b2ce3a72b11d..b05d95814776 100644
> > --- a/tools/lib/bpf/libbpf.h
> > +++ b/tools/lib/bpf/libbpf.h
> > @@ -1268,6 +1268,7 @@ enum bpf_tc_attach_point {
> >         BPF_TC_INGRESS = 1 << 0,
> >         BPF_TC_EGRESS  = 1 << 1,
> >         BPF_TC_CUSTOM  = 1 << 2,
> > +       BPF_TC_QDISC   = 1 << 3,
> >  };
> >
> >  #define BPF_TC_PARENT(a, b)    \
> > @@ -1282,9 +1283,11 @@ struct bpf_tc_hook {
> >         int ifindex;
> >         enum bpf_tc_attach_point attach_point;
> >         __u32 parent;
> > +       __u32 handle;
> > +       char *qdisc;
>
> const char *?
>
> >         size_t :0;
> >  };
> > -#define bpf_tc_hook__last_field parent
> > +#define bpf_tc_hook__last_field qdisc
> >
> >  struct bpf_tc_opts {
> >         size_t sz;
> > diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
> > index 68a2def17175..72db8c0add21 100644
> > --- a/tools/lib/bpf/netlink.c
> > +++ b/tools/lib/bpf/netlink.c
> > @@ -529,9 +529,9 @@ int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id)
> >  }
> >
> >
> > -typedef int (*qdisc_config_t)(struct libbpf_nla_req *req);
> > +typedef int (*qdisc_config_t)(struct libbpf_nla_req *req, struct bpf_tc_hook *hook);
>
> should hook pointer be const?
>
> >
> > -static int clsact_config(struct libbpf_nla_req *req)
> > +static int clsact_config(struct libbpf_nla_req *req, struct bpf_tc_hook *hook)
>
> const?
>
> >  {
> >         req->tc.tcm_parent = TC_H_CLSACT;
> >         req->tc.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
> > @@ -539,6 +539,16 @@ static int clsact_config(struct libbpf_nla_req *req)
> >         return nlattr_add(req, TCA_KIND, "clsact", sizeof("clsact"));
> >  }
> >
> > +static int qdisc_config(struct libbpf_nla_req *req, struct bpf_tc_hook *hook)
>
> same, const, it's not written into, right?
>
> > +{
> > +       char *qdisc = OPTS_GET(hook, qdisc, NULL);
> > +
> > +       req->tc.tcm_parent = OPTS_GET(hook, parent, TC_H_ROOT);
> > +       req->tc.tcm_handle = OPTS_GET(hook, handle, 0);
> > +
> > +       return nlattr_add(req, TCA_KIND, qdisc, strlen(qdisc) + 1);
> > +}
> > +
> >  static int attach_point_to_config(struct bpf_tc_hook *hook,
> >                                   qdisc_config_t *config)
> >  {
> > @@ -552,6 +562,9 @@ static int attach_point_to_config(struct bpf_tc_hook *hook,
> >                 return 0;
> >         case BPF_TC_CUSTOM:
> >                 return -EOPNOTSUPP;
> > +       case BPF_TC_QDISC:
> > +               *config = &qdisc_config;
> > +               return 0;
> >         default:
> >                 return -EINVAL;
> >         }
> > @@ -596,7 +609,7 @@ static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags)
> >         req.tc.tcm_family  = AF_UNSPEC;
> >         req.tc.tcm_ifindex = OPTS_GET(hook, ifindex, 0);
> >
> > -       ret = config(&req);
> > +       ret = config(&req, hook);
> >         if (ret < 0)
> >                 return ret;
> >
> > @@ -639,6 +652,7 @@ int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
> >         case BPF_TC_INGRESS:
> >         case BPF_TC_EGRESS:
> >                 return libbpf_err(__bpf_tc_detach(hook, NULL, true));
> > +       case BPF_TC_QDISC:
> >         case BPF_TC_INGRESS | BPF_TC_EGRESS:
> >                 return libbpf_err(tc_qdisc_delete(hook));
> >         case BPF_TC_CUSTOM:
> > --
> > 2.20.1
> >
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index b2ce3a72b11d..b05d95814776 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1268,6 +1268,7 @@  enum bpf_tc_attach_point {
 	BPF_TC_INGRESS = 1 << 0,
 	BPF_TC_EGRESS  = 1 << 1,
 	BPF_TC_CUSTOM  = 1 << 2,
+	BPF_TC_QDISC   = 1 << 3,
 };
 
 #define BPF_TC_PARENT(a, b) 	\
@@ -1282,9 +1283,11 @@  struct bpf_tc_hook {
 	int ifindex;
 	enum bpf_tc_attach_point attach_point;
 	__u32 parent;
+	__u32 handle;
+	char *qdisc;
 	size_t :0;
 };
-#define bpf_tc_hook__last_field parent
+#define bpf_tc_hook__last_field qdisc
 
 struct bpf_tc_opts {
 	size_t sz;
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 68a2def17175..72db8c0add21 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -529,9 +529,9 @@  int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id)
 }
 
 
-typedef int (*qdisc_config_t)(struct libbpf_nla_req *req);
+typedef int (*qdisc_config_t)(struct libbpf_nla_req *req, struct bpf_tc_hook *hook);
 
-static int clsact_config(struct libbpf_nla_req *req)
+static int clsact_config(struct libbpf_nla_req *req, struct bpf_tc_hook *hook)
 {
 	req->tc.tcm_parent = TC_H_CLSACT;
 	req->tc.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
@@ -539,6 +539,16 @@  static int clsact_config(struct libbpf_nla_req *req)
 	return nlattr_add(req, TCA_KIND, "clsact", sizeof("clsact"));
 }
 
+static int qdisc_config(struct libbpf_nla_req *req, struct bpf_tc_hook *hook)
+{
+	char *qdisc = OPTS_GET(hook, qdisc, NULL);
+
+	req->tc.tcm_parent = OPTS_GET(hook, parent, TC_H_ROOT);
+	req->tc.tcm_handle = OPTS_GET(hook, handle, 0);
+
+	return nlattr_add(req, TCA_KIND, qdisc, strlen(qdisc) + 1);
+}
+
 static int attach_point_to_config(struct bpf_tc_hook *hook,
 				  qdisc_config_t *config)
 {
@@ -552,6 +562,9 @@  static int attach_point_to_config(struct bpf_tc_hook *hook,
 		return 0;
 	case BPF_TC_CUSTOM:
 		return -EOPNOTSUPP;
+	case BPF_TC_QDISC:
+		*config = &qdisc_config;
+		return 0;
 	default:
 		return -EINVAL;
 	}
@@ -596,7 +609,7 @@  static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags)
 	req.tc.tcm_family  = AF_UNSPEC;
 	req.tc.tcm_ifindex = OPTS_GET(hook, ifindex, 0);
 
-	ret = config(&req);
+	ret = config(&req, hook);
 	if (ret < 0)
 		return ret;
 
@@ -639,6 +652,7 @@  int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
 	case BPF_TC_INGRESS:
 	case BPF_TC_EGRESS:
 		return libbpf_err(__bpf_tc_detach(hook, NULL, true));
+	case BPF_TC_QDISC:
 	case BPF_TC_INGRESS | BPF_TC_EGRESS:
 		return libbpf_err(tc_qdisc_delete(hook));
 	case BPF_TC_CUSTOM: