Skip to content

Commit 8c52471

Browse files
Amery HungMartin KaFai Lau
authored andcommitted
libbpf: Support creating and destroying qdisc
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 <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Toke Høiland-Jørgensen <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 0ddc97a commit 8c52471

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

tools/lib/bpf/libbpf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ enum bpf_tc_attach_point {
12831283
BPF_TC_INGRESS = 1 << 0,
12841284
BPF_TC_EGRESS = 1 << 1,
12851285
BPF_TC_CUSTOM = 1 << 2,
1286+
BPF_TC_QDISC = 1 << 3,
12861287
};
12871288

12881289
#define BPF_TC_PARENT(a, b) \
@@ -1297,9 +1298,11 @@ struct bpf_tc_hook {
12971298
int ifindex;
12981299
enum bpf_tc_attach_point attach_point;
12991300
__u32 parent;
1301+
__u32 handle;
1302+
const char *qdisc;
13001303
size_t :0;
13011304
};
1302-
#define bpf_tc_hook__last_field parent
1305+
#define bpf_tc_hook__last_field qdisc
13031306

13041307
struct bpf_tc_opts {
13051308
size_t sz;

tools/lib/bpf/netlink.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,16 +529,26 @@ int bpf_xdp_query_id(int ifindex, int flags, __u32 *prog_id)
529529
}
530530

531531

532-
typedef int (*qdisc_config_t)(struct libbpf_nla_req *req);
532+
typedef int (*qdisc_config_t)(struct libbpf_nla_req *req, const struct bpf_tc_hook *hook);
533533

534-
static int clsact_config(struct libbpf_nla_req *req)
534+
static int clsact_config(struct libbpf_nla_req *req, const struct bpf_tc_hook *hook)
535535
{
536536
req->tc.tcm_parent = TC_H_CLSACT;
537537
req->tc.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
538538

539539
return nlattr_add(req, TCA_KIND, "clsact", sizeof("clsact"));
540540
}
541541

542+
static int qdisc_config(struct libbpf_nla_req *req, const struct bpf_tc_hook *hook)
543+
{
544+
const char *qdisc = OPTS_GET(hook, qdisc, NULL);
545+
546+
req->tc.tcm_parent = OPTS_GET(hook, parent, TC_H_ROOT);
547+
req->tc.tcm_handle = OPTS_GET(hook, handle, 0);
548+
549+
return nlattr_add(req, TCA_KIND, qdisc, strlen(qdisc) + 1);
550+
}
551+
542552
static int attach_point_to_config(struct bpf_tc_hook *hook,
543553
qdisc_config_t *config)
544554
{
@@ -552,6 +562,9 @@ static int attach_point_to_config(struct bpf_tc_hook *hook,
552562
return 0;
553563
case BPF_TC_CUSTOM:
554564
return -EOPNOTSUPP;
565+
case BPF_TC_QDISC:
566+
*config = &qdisc_config;
567+
return 0;
555568
default:
556569
return -EINVAL;
557570
}
@@ -596,7 +609,7 @@ static int tc_qdisc_modify(struct bpf_tc_hook *hook, int cmd, int flags)
596609
req.tc.tcm_family = AF_UNSPEC;
597610
req.tc.tcm_ifindex = OPTS_GET(hook, ifindex, 0);
598611

599-
ret = config(&req);
612+
ret = config(&req, hook);
600613
if (ret < 0)
601614
return ret;
602615

@@ -639,6 +652,7 @@ int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
639652
case BPF_TC_INGRESS:
640653
case BPF_TC_EGRESS:
641654
return libbpf_err(__bpf_tc_detach(hook, NULL, true));
655+
case BPF_TC_QDISC:
642656
case BPF_TC_INGRESS | BPF_TC_EGRESS:
643657
return libbpf_err(tc_qdisc_delete(hook));
644658
case BPF_TC_CUSTOM:

0 commit comments

Comments
 (0)