bpf qdisc
Subsystems (7)
v7 cover bpf qdisc
Hi all,
This patchset aims to support implementing qdisc using bpf struct_ops. This version takes a step back and only implements the minimum support for bpf qdisc. 1) support of adding skb to bpf_list and bpf_rbtree directly and 2) classful qdisc are deferred to future patchsets. In addition, we only allow attaching bpf qdisc to root or mq for now. This is to prevent accidentally breaking exisiting classful qdiscs that rely on data in a child qdisc. This limit may be lifted in the future after careful inspection.
* Overview *
This series supports implementing qdisc using bpf struct_ops. bpf qdisc aims to be a flexible and easy-to-use infrastructure that allows users to quickly experiment with different scheduling algorithms/policies. It only requires users to implement core qdisc logic using bpf and implements the mundane part for them. In addition, the ability to easily communicate between qdisc and other components will also bring new opportunities for new applications and optimizations.
* Performance of bpf qdisc *
This patchset includes two qdisc examples, bpf_fifo and bpf_fq, for __testing__ purposes. For performance test, we compare selftests and their kernel counterparts to give you a sense of the performance of qdisc implemented in bpf.
The implementation of bpf_fq is fairly complex and slightly different from fq so later we only compare the two fifo qdiscs. bpf_fq implements a scheduling algorithm similar to fq before commit 29f834aa326e ("net_sched:sch_fq: add 3 bands and WRR scheduling") was introduced. bpf_fifo uses a single bpf_list as a queue instead of three queues for different priorities in pfifo_fast. The time complexity of fifo however should be similar since the queue selection time is negligible.
Test setup:
client -> qdisc -------------> server
~~~~~~~~~~~~~~~ ~~~~~~
nested VM1 @ DC1 VM2 @ DC2Throghput: iperf3 -t 600, 5 times
Qdisc Average (GBits/sec)
---------- -------------------
pfifo_fast 12.52 ± 0.26
bpf_fifo 11.72 ± 0.32
fq 10.24 ± 0.13
bpf_fq 11.92 ± 0.64 Latency: sockperf pp --tcp -t 600, 5 times
Qdisc Average (usec)
---------- --------------
pfifo_fast 244.58 ± 7.93
bpf_fifo 244.92 ± 15.22
fq 234.30 ± 19.25
bpf_fq 221.34 ± 10.76Looking at the two fifo qdiscs, the 6.4% drop in throughput in the bpf implementatioin is consistent with previous observation (v8 throughput test on a loopback device). This should be able to be mitigated by supporting adding skb to bpf_list or bpf_rbtree directly in the future.
* Clean up skb in bpf qdisc during reset *
The current implementation relies on bpf qdisc implementors to correctly release skbs in queues (bpf graphs or maps) in .reset, which might not be a safe thing to do. The solution as Martin has suggested would be supporting private data in struct_ops. This can also help simplifying implementation of qdisc that works with mq. For examples, qdiscs in the selftest mostly use global data. Therefore, even if user add multiple qdisc instances under mq, they would still share the same queue.
---
v7:
* Rebase to bpf-next/master and resolve a slight merge conflict due to commit 2eb6c6a34cb1 ("net: move replay logic to tc_modify_qdisc") moving a module_put(). No functional change from v6.
Link to v6: https://lore.kernel.org/bpf/[email protected]/
v6:
* Improve kfunc filter implementation
- Drop old patch 2 that introduces a function to get member offset of struct_ops program
- Include patch 1 from Juntong Deng from [0], which populates prog->aux->st_ops and attach_st_ops_member_off once and early in the verification
- Adopt flag-based kfunc filter proposed in sched_ext [0]
* Refactor bpf_qdisc_btf_struct_access
- Merge old patch 7, which allows writes to Qdisc->qstats with patch 3 (Support implementation of Qdisc_ops in bpf)
- Share checks of bpf_qdisc_sk_buff_access() and bpf_qdisc_qdisc_access() in bpf_qdisc_btf_struct_access()
* Explain what bytecodes in gen_pro/epilogue do in the comment
* Merge old patch 8, which simply allows writes to Qdisc->q.qlen and
Qdisc->limit, with patch 3 (Support implementation of Qdisc_ops in
bpf) * Minor fixes
- Remove bpf_qdisc_get_func_proto that is no longer needed as tail call is disabled universally for struct_ops with __ref args
- Remove .peek's cfi_stub as it is not supported
* Add SPDX license identifier
* Add Acked-by from Toke
[0] https://lore.kernel.org/all/AM6PR03MB50806070E3D56208DDB8131699C22@AM6PR03MB5080.eurprd03.prod.outlook.com/ Link to v5: https://lore.kernel.org/bpf/[email protected]/
v5:
* Rebase to bpf-next/master
* Remove prerequisite bpf patches that has landed
* Add Acked-by from Cong
v4:
* Rebase to bpf-next/master
* Move the root/mq attaching check to bpf_qdisc.c
(now patch 15)
* Replace netdevsim with veth for attaching mq
v3:
* Rebase to bpf-next/master
* Remove the requirement in the verifier that "__ref arguments must acquire ref_obj_id first" by making each prog keeping a copy of arg_ctx_info and saving ref_obj_id in it.
* Generalize prog_ops_moff() to work with any struct_op (now called
bpf_struct_ops_prog_moff())
* Use bpf_struct_ops_prog_moff(prog) instead of
prog->aux->attach_func_name to infer the ops of a program * Limit attach to root and mq for now and add corresponding selftests
* Simplify qdisc selftests with network_helper
* Fix fq_remove_flow() not deleting the stashed flow
v2: Rebase to bpf-next/master
Patch 1-4
Remove the use of ctx_arg_info->ref_obj_id when acquiring referenced kptr from struct_ops arg Improve type comparison when checking kptr return from struct_ops
Simplify selftests with test_loader and nomerge attribute
Patch 5
Remove redundant checks in qdisc_init
Disallow tail_call
Patch 6
Improve kfunc ops availabilty filter by
i) Checking struct_ops->type
ii) Defining op-specific kfunc set
Patch 7
Search and add bpf_kfunc_desc after gen_prologue/epilogue
Patch 8
Use gen_prologue/epilogue to init/cancel watchdog timer
Patch 12
Mark read-only func arg and struct member const in libbpf
v1:
Fix struct_ops referenced kptr acquire/return mechanisms Allow creating dynptr from skb
Add bpf qdisc kfunc filter
Support updating bstats and qstats
Update qdiscs in selftest to update stats
Add gc, handle hash collision and fix bugs in fq_bpf
past RFCs
v9: Drop classful qdisc operations and kfuncs
Drop support of enqueuing skb directly to bpf_rbtree/list
v8: Implement support of bpf qdisc using struct_ops
Allow struct_ops to acquire referenced kptr via argument Allow struct_ops to release and return referenced kptr Support enqueuing sk_buff to bpf_rbtree/list Move examples from samples to selftests
Add a classful qdisc selftest
v7: Reference skb using kptr to sk_buff instead of __sk_buff
Use the new bpf rbtree/link to for skb queues Add reset and init programs
Add a bpf fq qdisc sample
Add a bpf netem qdisc sample
v6: switch to kptr based approach
v5: mv kernel/bpf/skb_map.c net/core/skb_map.c
implement flow map as map-in-map
rename bpf_skb_tc_classify() and move it to net/sched/cls_api.c clean up eBPF qdisc program context
v4: get rid of PIFO, use rbtree directly
v3: move priority queue from sch_bpf to skb map
introduce skb map and its helpers
introduce bpf_skb_classify()
use netdevice notifier to reset skb's
Rebase on latest bpf-next
v2: Rebase on latest net-next
Make the code more complete (but still incomplete)
Amery Hung (10):
bpf: Prepare to reuse get_ctx_arg_idx
bpf: net_sched: Support implementation of Qdisc_ops in bpf
bpf: net_sched: Add basic bpf qdisc kfuncs
bpf: net_sched: Add a qdisc watchdog timer
bpf: net_sched: Support updating bstats
bpf: net_sched: Disable attaching bpf qdisc to non root
libbpf: Support creating and destroying qdisc selftests/bpf: Add a basic fifo qdisc test selftests/bpf: Add a bpf fq qdisc to selftest selftests/bpf: Test attaching bpf qdisc to mq and non root
include/linux/btf.h | 1 + kernel/bpf/btf.c | 6 +- net/sched/Kconfig | 12 + net/sched/Makefile | 1 + net/sched/bpf_qdisc.c | 477 +++++++++++ net/sched/sch_api.c | 7 +- net/sched/sch_generic.c | 3 +- tools/lib/bpf/libbpf.h | 5 +- tools/lib/bpf/netlink.c | 20 +- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 180 +++++ .../selftests/bpf/progs/bpf_qdisc_common.h | 31 + .../selftests/bpf/progs/bpf_qdisc_fifo.c | 117 +++ .../selftests/bpf/progs/bpf_qdisc_fq.c | 750 ++++++++++++++++++ 14 files changed, 1601 insertions(+), 11 deletions(-) create mode 100644 net/sched/bpf_qdisc.c create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_common.h create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c
Signature · 1 line
2.47.1
PATCH v7 1/10 bpf: Prepare to reuse get_ctx_arg_idx
Rename get_ctx_arg_idx to bpf_ctx_arg_idx, and allow others to call it. No functional change.
--- include/linux/btf.h | 1 + kernel/bpf/btf.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-)
include/linux/btf.h +1 -0
@@ -522,6 +522,7 @@ bool btf_param_match_suffix(const struct btf *btf, const char *suffix);int btf_ctx_arg_offset (const struct btf *btf,const struct btf_type *func_proto,u32 arg_no); +u32 btf_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto,int off);struct bpf_verifier_log ;
kernel/bpf/btf.c +3 -3
@@ -6391,8 +6391,8 @@ static bool is_int_ptr(struct btf *btf, const struct btf_type *t)return btf_type_is_int (t); } -static u32 get_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto, -int off) +u32 btf_ctx_arg_idx (struct btf *btf,const struct btf_type *func_proto, +int off) {const struct btf_param *args;const struct btf_type *t;
@@ -6671,7 +6671,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, tname, off);return false; } - arg =get_ctx_arg_idx (btf, t, off); + arg =btf_ctx_arg_idx (btf, t, off); args = (const struct btf_param *)(t +1 );/* if (t == NULL) Fall back to default BPF prog with * MAX_BPF_FUNC_REG_ARGS u64 arguments.
Signature · 1 line
2.47.1
PATCH v7 2/10 bpf: net_sched: Support implementation of Qdisc_ops in bpf
In reply to Amery HungFrom: Amery Hung <[email protected]>
The recent advancement in bpf such as allocated objects, bpf list and bpf rbtree has provided powerful and flexible building blocks to realize sophisticated packet scheduling algorithms. As struct_ops now supports core operators in Qdisc_ops, start allowing qdisc to be implemented using bpf struct_ops with this patch. Users can implement Qdisc_ops.{enqueue, dequeue, init, reset, destroy} in bpf and register the qdisc dynamically into the kernel.
--- net/sched/Kconfig | 12 +++ net/sched/Makefile | 1 + net/sched/bpf_qdisc.c | 232 ++++++++++++++++++++++++++++++++++++++++ net/sched/sch_api.c | 7 +- net/sched/sch_generic.c | 3 +- 5 files changed, 251 insertions(+), 4 deletions(-) create mode 100644 net/sched/bpf_qdisc.c
net/sched/Kconfig +12 -0
@@ -403,6 +403,18 @@ config NET_SCH_ETS If unsure, say N. +config NET_SCH_BPF + bool "BPF-based Qdisc" + depends on BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF + help + This option allows BPF-based queueing disiplines. With BPF struct_ops, + users can implement supported operators in Qdisc_ops using BPF programs. + The queue holding skb can be built with BPF maps or graphs. + + Say Y here if you want to use BPF-based Qdisc. + + If unsure, say N. + menuconfig NET_SCH_DEFAULT bool "Allow override default queue discipline" help
net/sched/Makefile +1 -0
@@ -62,6 +62,7 @@ obj-$(CONFIG_NET_SCH_FQ_PIE) += sch_fq_pie.o obj-$(CONFIG_NET_SCH_CBS) += sch_cbs.o obj-$(CONFIG_NET_SCH_ETF) += sch_etf.o obj-$(CONFIG_NET_SCH_TAPRIO) += sch_taprio.o +obj-$(CONFIG_NET_SCH_BPF) += bpf_qdisc.o obj-$(CONFIG_NET_CLS_U32) += cls_u32.o obj-$(CONFIG_NET_CLS_ROUTE4) += cls_route.o
net/sched/bpf_qdisc.c new file +232 -0
232 lines not on this page · raw message
net/sched/sch_api.c +4 -3
@@ -25,6 +25,7 @@#include <linux/hrtimer.h> #include <linux/slab.h> #include <linux/hashtable.h> +#include <linux/bpf.h> #include <net/netdev_lock.h> #include <net/net_namespace.h>
@@ -359,7 +360,7 @@ static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind)read_lock (&qdisc_mod_lock);for (q = qdisc_base; q; q = q->next) {if (nla_strcmp (kind, q->id) ==0 ) { -if (!try_module_get (q->owner)) +if (!bpf_try_module_get (q, q->owner)) q =NULL ;break ; }
@@ -1370,7 +1371,7 @@ static struct Qdisc *qdisc_create(struct net_device *dev,netdev_put (dev, &sch->dev_tracker);qdisc_free (sch); err_out2: -module_put (ops->owner); +bpf_module_put (ops, ops->owner); err_out: *errp = err;return NULL ;
@@ -1782,7 +1783,7 @@ static void request_qdisc_module(struct nlattr *kind) ops =qdisc_lookup_ops (kind);if (ops) { -module_put (ops->owner); +bpf_module_put (ops, ops->owner);return ; }
net/sched/sch_generic.c +2 -1
@@ -24,6 +24,7 @@#include <linux/if_vlan.h> #include <linux/skb_array.h> #include <linux/if_macvlan.h> +#include <linux/bpf.h> #include <net/sch_generic.h> #include <net/pkt_sched.h> #include <net/dst.h>
@@ -1078,7 +1079,7 @@ static void __qdisc_destroy(struct Qdisc *qdisc) ops->destroy (qdisc);lockdep_unregister_key (&qdisc->root_lock_key); -module_put (ops->owner); +bpf_module_put (ops, ops->owner);netdev_put (dev, &qdisc->dev_tracker);trace_qdisc_destroy (qdisc);
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v7 02/10] bpf: net_sched: Support implementation of Qdisc_ops in bpf
Quoted 20 lines · Amery Hung
net/sched/bpf_qdisc.c new file +20
-0+static int bpf_qdisc_sk_buff_access (struct bpf_verifier_log *log, +const struct bpf_reg_state *reg, +int off,size_t *end) +{ +switch (off) { +case offsetof(struct sk_buff , tstamp): + *end = offsetofend(struct sk_buff, tstamp); +break ; +case offsetof(struct sk_buff , priority): + *end = offsetofend(struct sk_buff, priority); +break ; +case offsetof(struct sk_buff , mark): + *end = offsetofend(struct sk_buff, mark); +break ; +case offsetof(struct sk_buff , queue_mapping): + *end = offsetofend(struct sk_buff, queue_mapping); +break ; +case offsetof(struct sk_buff , cb) + offsetof(struct qdisc_skb_cb , tc_classid): + *end = offsetof(struct sk_buff , cb) + + offsetofend(struct qdisc_skb_cb, tc_classid);
net/sched/bpf_qdisc.c line 97
Quoted 10 lines
net/sched/bpf_qdisc.c new file +10
-0+break ; + case offsetof(struct sk_buff, cb) + offsetof(struct qdisc_skb_cb, data[0]) ... + offsetof(struct sk_buff , cb) + offsetof(struct qdisc_skb_cb , + data[QDISC_CB_PRIV_LEN - 1]): + *end = offsetof(struct sk_buff , cb) + + offsetofend(struct qdisc_skb_cb, data[QDISC_CB_PRIV_LEN - 1]); +break ; +case offsetof(struct sk_buff , tc_index): + *end = offsetofend(struct sk_buff, tc_index); +break ;
I only kept the WRITE access to tstamp and data[]. Removed everything else. It can be revisited when they are really needed in the future.
PATCH v7 3/10 bpf: net_sched: Add basic bpf qdisc kfuncs
In reply to Amery HungFrom: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both bpf_qdisc_skb_drop() and bpf_kfree_skb() can be used to release a reference to an skb. However, bpf_qdisc_skb_drop() can only be called in .enqueue where a to_free skb list is available from kernel to defer the release. bpf_kfree_skb() should be used elsewhere. It is also used in bpf_obj_free_fields() when cleaning up skb in maps and collections.
bpf_skb_get_hash() returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.
Finally, allow users to create read-only dynptr via bpf_dynptr_from_skb().
--- net/sched/bpf_qdisc.c | 111 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-)
net/sched/bpf_qdisc.c +110 -1
@@ -8,6 +8,9 @@#include <net/pkt_sched.h> #include <net/pkt_cls.h> +#define QDISC_OP_IDX (op) (offsetof(struct Qdisc_ops, op) / sizeof(void (*)(void))) +#define QDISC_MOFF_IDX (moff) (moff / sizeof(void (*)(void))) +static struct bpf_struct_ops bpf_Qdisc_ops;struct bpf_sk_buff_ptr {
@@ -139,6 +142,95 @@ static int bpf_qdisc_btf_struct_access(struct bpf_verifier_log *log,return 0 ; } +__bpf_kfunc_start_defs (); + +/* bpf_skb_get_hash - Get the flow hash of an skb. +* @skb: The skb to get the flow hash from. +*/ +__bpf_kfunc u32 bpf_skb_get_hash(struct sk_buff *skb) +{ +return skb_get_hash (skb); +} + +/* bpf_kfree_skb - Release an skb's reference and drop it immediately. +* @skb: The skb whose reference to be released and dropped. +*/ +__bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb) +{ +kfree_skb (skb); +} + +/* bpf_qdisc_skb_drop - Drop an skb by adding it to a deferred free list. +* @skb: The skb whose reference to be released and dropped. +* @to_free_list: The list of skbs to be dropped. +*/ +__bpf_kfunc void bpf_qdisc_skb_drop(struct sk_buff *skb, +struct bpf_sk_buff_ptr *to_free_list) +{ +__qdisc_drop (skb, (struct sk_buff **)to_free_list); +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (qdisc_kfunc_ids) +BTF_ID_FLAGS (func ,bpf_skb_get_hash ,KF_TRUSTED_ARGS ) +BTF_ID_FLAGS (func, bpf_kfree_skb,KF_RELEASE ) +BTF_ID_FLAGS (func, bpf_qdisc_skb_drop,KF_RELEASE ) +BTF_ID_FLAGS (func, bpf_dynptr_from_skb,KF_TRUSTED_ARGS ) +BTF_KFUNCS_END (qdisc_kfunc_ids) + +BTF_SET_START (qdisc_common_kfunc_set) +BTF_ID (func, bpf_skb_get_hash) +BTF_ID (func, bpf_kfree_skb) +BTF_ID (func, bpf_dynptr_from_skb) +BTF_SET_END (qdisc_common_kfunc_set) + +BTF_SET_START (qdisc_enqueue_kfunc_set) +BTF_ID (func, bpf_qdisc_skb_drop) +BTF_SET_END (qdisc_enqueue_kfunc_set) + +enum qdisc_ops_kf_flags { +QDISC_OPS_KF_COMMON =0 , + QDISC_OPS_KF_ENQUEUE = 1 << 0, +}; + +static const u32 qdisc_ops_context_flags[] = { + [QDISC_OP_IDX (enqueue)] =QDISC_OPS_KF_ENQUEUE , + [QDISC_OP_IDX (dequeue)] =QDISC_OPS_KF_COMMON , + [QDISC_OP_IDX (init)] =QDISC_OPS_KF_COMMON , + [QDISC_OP_IDX (reset)] =QDISC_OPS_KF_COMMON , + [QDISC_OP_IDX (destroy)] =QDISC_OPS_KF_COMMON , +}; + +static int bpf_qdisc_kfunc_filter (const struct bpf_prog *prog,u32 kfunc_id) +{ +u32 moff, flags; + +if (!btf_id_set8_contains (&qdisc_kfunc_ids, kfunc_id)) +return 0 ; + +if (prog->aux->st_ops != &bpf_Qdisc_ops) +return -EACCES ; + + moff = prog->aux->attach_st_ops_member_off; + flags = qdisc_ops_context_flags[QDISC_MOFF_IDX (moff)]; + +if ((flags &QDISC_OPS_KF_ENQUEUE ) && +btf_id_set_contains (&qdisc_enqueue_kfunc_set, kfunc_id)) +return 0 ; + +if (btf_id_set_contains (&qdisc_common_kfunc_set, kfunc_id)) +return 0 ; + +return -EACCES ; +} + +static const struct btf_kfunc_id_set bpf_qdisc_kfunc_set = { + .owner =THIS_MODULE , + .set = &qdisc_kfunc_ids, + .filter = bpf_qdisc_kfunc_filter, +}; +static const struct bpf_verifier_ops bpf_qdisc_verifier_ops = { .get_func_proto = bpf_base_func_proto, .is_valid_access = bpf_qdisc_is_valid_access,
@@ -225,8 +317,25 @@ static struct bpf_struct_ops bpf_Qdisc_ops = { .owner = THIS_MODULE, }; +BTF_ID_LIST (bpf_sk_buff_dtor_ids) +BTF_ID (func ,bpf_kfree_skb ) + static int__init bpf_qdisc_kfunc_init (void) { -return register_bpf_struct_ops (&bpf_Qdisc_ops, Qdisc_ops); +int ret; +const struct btf_id_dtor_kfunc skb_kfunc_dtors[] = { + { + .btf_id = bpf_sk_buff_ids[0 ], + .kfunc_btf_id = bpf_sk_buff_dtor_ids[0 ] + }, + }; + + ret =register_btf_kfunc_id_set (BPF_PROG_TYPE_STRUCT_OPS , &bpf_qdisc_kfunc_set); + ret = ret ?:register_btf_id_dtor_kfuncs (skb_kfunc_dtors, +ARRAY_SIZE (skb_kfunc_dtors), +THIS_MODULE ); + ret = ret ?:register_bpf_struct_ops (&bpf_Qdisc_ops, Qdisc_ops); + +return ret; }late_initcall (bpf_qdisc_kfunc_init);
Signature · 1 line
2.47.1
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 11 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
Quoted 8 lines
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after bpf_kfree_skb?
Quoted 1 line
[...]
Re: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.
Quoted 8 lines
Quoted 5 lines
Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after
bpf_kfree_skb?
Good question...
Maybe we can add a ref_obj_id field to bpf_reg_state->dynptr to track the ref_obj_id of the object underlying a dynptr?
Then, in release_reference(), in addition to finding ref_obj_id in registers, verifier will also search stack slots and invalidate all dynptrs with the ref_obj_id.
Does this sound like a feasible solution?
Quoted 1 line
Quoted 1 line
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 30 lines · Amery Hung
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.Quoted 8 lines
Quoted 5 lines
Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after
bpf_kfree_skb?Good question...
Quoted 8 lines
Maybe we can add a
ref_obj_idfield tobpf_reg_state->dynptrto track theref_obj_idof the object underlying a dynptr?Then, in
release_reference(), in addition to findingref_obj_idin registers, verifier will also search stack slots and invalidate all dynptrs with theref_obj_id.Does this sound like a feasible solution?
Yes, though I talked with Andrii and he has better ideas for doing this generically, but for now I think we can make this fix as a stopgap.
I will add a fixes tag, asked the question because I had the same question when implementing a similar pattern for my patch, and was wondering how you solved it.
I made a similar fix to what you described for now.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
[...]
Re: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 37 lines · Amery Hung
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.Quoted 8 lines
Quoted 5 lines
Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after
bpf_kfree_skb?Good question...
Maybe we can add a
ref_obj_idfield tobpf_reg_state->dynptrto track theref_obj_idof the object underlying a dynptr?Then, in
release_reference(), in addition to findingref_obj_idin registers, verifier will also search stack slots and invalidate all dynptrs with theref_obj_id.
Quoted 5 lines
Quoted 1 line
Does this sound like a feasible solution?
Yes, though I talked with Andrii and he has better ideas for doing this generically, but for now I think we can make this fix as a stopgap.
Sounds good. Just making sure I am not doing redundant work, you will send the fix you made, right?
Thanks,
Amery
Quoted 8 lines
I will add a fixes tag, asked the question because I had the same question when implementing a similar pattern for my patch, and was wondering how you solved it.
I made a similar fix to what you described for now.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
[...]
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 42 lines · Amery Hung
Quoted 39 lines · Kumar Kartikeya Dwivedi
Quoted 37 lines · Amery Hung
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.Quoted 8 lines
Quoted 5 lines
Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after
bpf_kfree_skb?Good question...
Maybe we can add a
ref_obj_idfield tobpf_reg_state->dynptrto track theref_obj_idof the object underlying a dynptr?Then, in
release_reference(), in addition to findingref_obj_idin registers, verifier will also search stack slots and invalidate all dynptrs with theref_obj_id.
Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Does this sound like a feasible solution?
Yes, though I talked with Andrii and he has better ideas for doing this generically, but for now I think we can make this fix as a stopgap.
Sounds good. Just making sure I am not doing redundant work, you will send the fix you made, right?
Yes.
Quoted 11 lines
Thanks,
AmeryQuoted 8 lines
I will add a fixes tag, asked the question because I had the same question when implementing a similar pattern for my patch, and was wondering how you solved it.
I made a similar fix to what you described for now.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
[...]
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
In reply to Kumar Kartikeya DwivediQuoted 38 lines · Kumar Kartikeya Dwivedi
Quoted 37 lines · Amery Hung
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.Quoted 8 lines
Quoted 5 lines
Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after
bpf_kfree_skb?Good question...
Maybe we can add a
ref_obj_idfield tobpf_reg_state->dynptrto track theref_obj_idof the object underlying a dynptr?Then, in
release_reference(), in addition to findingref_obj_idin registers, verifier will also search stack slots and invalidate all dynptrs with theref_obj_id.
Quoted 5 lines
Quoted 1 line
Does this sound like a feasible solution?
Yes, though I talked with Andrii and he has better ideas for doing this generically, but for now I think we can make this fix as a stopgap.
In case the better fix will take longer, just want to mention that an option is to remove the bpf_dynptr_from_skb() from bpf qdisc. I don't see an urgent need for the bpf qdisc to be able to directly access the skb->data. btw, I don't think bpf qdisc should write to the skb->data.
The same goes for the bpf_kfree_skb(). I was thinking if it is useful at all considering there is already a bpf_qdisc_skb_drop(). I kept it there because it is a little more intuitive in case the .reset/.destroy wanted to do a "skb = bpf_kptr_xchg(&skbn->skb, NULL);" and then explicitly free the bpf_kfree_skb(skb). However, the bpf prog can also directly do the bpf_obj_drop(skbn) and then bpf_kfree_skb() is not needed, right?
Re: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 51 lines · Martin KaFai Lau
Quoted 38 lines · Kumar Kartikeya Dwivedi
Quoted 37 lines · Amery Hung
Quoted 16 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Amery Hung
From: Amery Hung <[email protected]>
Add basic kfuncs for working on skb in qdisc.
Both
bpf_qdisc_skb_drop()andbpf_kfree_skb()can be used to release a reference to an skb. However,bpf_qdisc_skb_drop()can only be called in .enqueue where ato_freeskb list is available from kernel to defer the release.bpf_kfree_skb()should be used elsewhere. It is also used inbpf_obj_free_fields()when cleaning up skb in maps and collections.
bpf_skb_get_hash()returns the flow hash of an skb, which can be used to build flow-based queueing algorithms.Quoted 8 lines
Quoted 5 lines
Finally, allow users to create read-only dynptr via
bpf_dynptr_from_skb().Signed-off-by Amery Hung <[email protected]>---
How do we prevent UAF when dynptr is accessed after
bpf_kfree_skb?Good question...
Maybe we can add a
ref_obj_idfield tobpf_reg_state->dynptrto track theref_obj_idof the object underlying a dynptr?Then, in
release_reference(), in addition to findingref_obj_idin registers, verifier will also search stack slots and invalidate all dynptrs with theref_obj_id.Quoted 5 lines
Quoted 1 line
Does this sound like a feasible solution?
Yes, though I talked with Andrii and he has better ideas for doing this generically, but for now I think we can make this fix as a stopgap.
In case the better fix will take longer, just want to mention that an option is to remove the
bpf_dynptr_from_skb()from bpf qdisc. I don't see an urgent need for the bpf qdisc to be able to directly access theskb->data. btw, I don't think bpf qdisc should write to theskb->data.
Quoted 8 lines
The same goes for the
bpf_kfree_skb(). I was thinking if it is useful at all considering there is already abpf_qdisc_skb_drop(). I kept it there because it is a little more intuitive in case the .reset/.destroy wanted to do a "skb =bpf_kptr_xchg(&skbn->skb, NULL);" and then explicitly free thebpf_kfree_skb(skb). However, the bpf prog can also directly do thebpf_obj_drop(skbn)and thenbpf_kfree_skb()is not needed, right?
My rationale for keeping two skb releasing kfuncs: bpf_kfree_skb() is the dtor and since dtor can only have one argument, so bpf_qdisc_skb_drop() can not replace it. Since bpf_kfree_skb() is here to stay, I allow users to call it directly for convenience. Only exposing bpf_qdisc_skb_drop() and calling kfree_skb() in bpf_qdisc_skb_drop() when to_free is NULL will also do. I don’t have a strong opinion.
Yes, bpf_kfree_skb() will not be needed if doing bpf_obj_drop(skbn). bpf_obj_drop() internally will call the dtor of a kptr (i.e., in this case, bpf_kfree_skb()) in an allocated object.
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v7 03/10] bpf: net_sched: Add basic bpf qdisc kfuncs
Quoted 17 lines · Amery Hung
Quoted 8 lines
The same goes for the
bpf_kfree_skb(). I was thinking if it is useful at all considering there is already abpf_qdisc_skb_drop(). I kept it there because it is a little more intuitive in case the .reset/.destroy wanted to do a "skb =bpf_kptr_xchg(&skbn->skb, NULL);" and then explicitly free thebpf_kfree_skb(skb). However, the bpf prog can also directly do thebpf_obj_drop(skbn)and thenbpf_kfree_skb()is not needed, right?My rationale for keeping two skb releasing kfuncs:
bpf_kfree_skb()is the dtor and since dtor can only have one argument, sobpf_qdisc_skb_drop()can not replace it. Sincebpf_kfree_skb()is here to stay, I allow users to call it directly for convenience. Only exposingbpf_qdisc_skb_drop()and callingkfree_skb()inbpf_qdisc_skb_drop()whento_freeis NULL will also do. I don’t have a strong opinion.
Quoted 3 lines
Yes,
bpf_kfree_skb()will not be needed if doingbpf_obj_drop(skbn).bpf_obj_drop()internally will call the dtor of a kptr (i.e., in this case,bpf_kfree_skb()) in an allocated object.
Make sense. Lets keep the bpf_kfree_skb() as-is instead of complicating the bpf_qdisc_skb_drop() with NULL vs non-NULL argument.
PATCH v7 4/10 bpf: net_sched: Add a qdisc watchdog timer
In reply to Amery HungFrom: Amery Hung <[email protected]>
Add a watchdog timer to bpf qdisc. The watchdog can be used to schedule the execution of qdisc through kfunc, bpf_qdisc_schedule(). It can be useful for building traffic shaping scheduling algorithm, where the time the next packet will be dequeued is known.
The implementation relies on struct_ops gen_prologue/epilogue to patch bpf programs provided by users. Operator specific prologue/epilogue kfuncs are introduced instead of watchdog kfuncs so that it is easier to extend prologue/epilogue in the future (writing C vs BPF bytecode).
--- net/sched/bpf_qdisc.c | 106 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-)
net/sched/bpf_qdisc.c +105 -1
@@ -13,6 +13,10 @@static struct bpf_struct_ops bpf_Qdisc_ops; +struct bpf_sched_data { +struct qdisc_watchdog watchdog; +}; +struct bpf_sk_buff_ptr {struct sk_buff *skb; };
@@ -142,6 +146,56 @@ static int bpf_qdisc_btf_struct_access(struct bpf_verifier_log *log,return 0 ; } +BTF_ID_LIST (bpf_qdisc_init_prologue_ids) +BTF_ID (func, bpf_qdisc_init_prologue) + +static int bpf_qdisc_gen_prologue (struct bpf_insn *insn_buf,bool direct_write, +const struct bpf_prog *prog) +{ + struct bpf_insn *insn = insn_buf; + + if (prog->aux->attach_st_ops_member_off != offsetof(struct Qdisc_ops, init)) + return 0; + +/* r6 = r1; // r6 will be "u64 *ctx". r1 is "u64 *ctx". +* r1 = r1[0]; // r1 will be "struct Qdisc *sch" +* r0 = bpf_qdisc_init_prologue(r1); +* r1 = r6; // r1 will be "u64 *ctx". +*/ + *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1); + *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0); + *insn++ = BPF_CALL_KFUNC(0, bpf_qdisc_init_prologue_ids[0]); + *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6); + *insn++ = prog->insnsi[0]; + + return insn - insn_buf; +} + +BTF_ID_LIST(bpf_qdisc_reset_destroy_epilogue_ids) +BTF_ID(func, bpf_qdisc_reset_destroy_epilogue) + +static int bpf_qdisc_gen_epilogue(struct bpf_insn *insn_buf, const struct bpf_prog *prog, + s16 ctx_stack_off) +{ +struct bpf_insn *insn = insn_buf; + +if (prog->aux->attach_st_ops_member_off != offsetof(struct Qdisc_ops , reset) && + prog->aux->attach_st_ops_member_off != offsetof(struct Qdisc_ops , destroy)) +return 0 ; + +/* r1 = stack[ctx_stack_off]; // r1 will be "u64 *ctx" +* r1 = r1[0]; // r1 will be "struct Qdisc *sch" +* r0 = bpf_qdisc_reset_destroy_epilogue(r1); +* BPF_EXIT; +*/ + *insn++ =BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_FP , ctx_stack_off); + *insn++ =BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 ,0 ); + *insn++ =BPF_CALL_KFUNC (0 , bpf_qdisc_reset_destroy_epilogue_ids[0 ]); + *insn++ =BPF_EXIT_INSN (); + +return insn - insn_buf; +} +__bpf_kfunc_start_defs ();/* bpf_skb_get_hash - Get the flow hash of an skb.
@@ -170,6 +224,36 @@ __bpf_kfunc void bpf_qdisc_skb_drop(struct sk_buff *skb,__qdisc_drop (skb, (struct sk_buff **)to_free_list); } +/* bpf_qdisc_watchdog_schedule - Schedule a qdisc to a later time using a timer. +* @sch: The qdisc to be scheduled. +* @expire: The expiry time of the timer. +* @delta_ns: The slack range of the timer. +*/ +__bpf_kfunc void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns) +{ +struct bpf_sched_data *q =qdisc_priv (sch); + +qdisc_watchdog_schedule_range_ns (&q->watchdog, expire, delta_ns); +} + +/* bpf_qdisc_init_prologue - Hidden kfunc called in prologue of .init. */ +__bpf_kfunc void bpf_qdisc_init_prologue(struct Qdisc *sch) +{ +struct bpf_sched_data *q =qdisc_priv (sch); + +qdisc_watchdog_init (&q->watchdog, sch); +} + +/* bpf_qdisc_reset_destroy_epilogue - Hidden kfunc called in epilogue of .reset +* and .destroy +*/ +__bpf_kfunc void bpf_qdisc_reset_destroy_epilogue(struct Qdisc *sch) +{ +struct bpf_sched_data *q =qdisc_priv (sch); + +qdisc_watchdog_cancel (&q->watchdog); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (qdisc_kfunc_ids)
@@ -177,6 +261,9 @@ BTF_ID_FLAGS(func, bpf_skb_get_hash, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_kfree_skb, KF_RELEASE) BTF_ID_FLAGS(func, bpf_qdisc_skb_drop, KF_RELEASE) BTF_ID_FLAGS(func, bpf_dynptr_from_skb, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_qdisc_init_prologue, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_qdisc_reset_destroy_epilogue, KF_TRUSTED_ARGS)BTF_KFUNCS_END (qdisc_kfunc_ids)BTF_SET_START (qdisc_common_kfunc_set)
@@ -187,16 +274,22 @@ BTF_SET_END(qdisc_common_kfunc_set)BTF_SET_START (qdisc_enqueue_kfunc_set)BTF_ID (func ,bpf_qdisc_skb_drop ) +BTF_ID (func, bpf_qdisc_watchdog_schedule)BTF_SET_END (qdisc_enqueue_kfunc_set) +BTF_SET_START (qdisc_dequeue_kfunc_set) +BTF_ID (func, bpf_qdisc_watchdog_schedule) +BTF_SET_END (qdisc_dequeue_kfunc_set) + enum qdisc_ops_kf_flags {QDISC_OPS_KF_COMMON =0 ,QDISC_OPS_KF_ENQUEUE =1 <<0 , + QDISC_OPS_KF_DEQUEUE = 1 << 1, };static const u32 qdisc_ops_context_flags[] = { [QDISC_OP_IDX (enqueue)] =QDISC_OPS_KF_ENQUEUE , - [QDISC_OP_IDX (dequeue)] =QDISC_OPS_KF_COMMON , + [QDISC_OP_IDX (dequeue)] =QDISC_OPS_KF_DEQUEUE , [QDISC_OP_IDX (init)] =QDISC_OPS_KF_COMMON , [QDISC_OP_IDX (reset)] =QDISC_OPS_KF_COMMON , [QDISC_OP_IDX(destroy)] = QDISC_OPS_KF_COMMON,
@@ -219,6 +312,10 @@ static int bpf_qdisc_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id) btf_id_set_contains(&qdisc_enqueue_kfunc_set, kfunc_id))return 0 ; +if ((flags &QDISC_OPS_KF_DEQUEUE ) && +btf_id_set_contains (&qdisc_dequeue_kfunc_set, kfunc_id)) +return 0 ; +if (btf_id_set_contains (&qdisc_common_kfunc_set, kfunc_id))return 0 ;
@@ -235,6 +332,8 @@ static const struct bpf_verifier_ops bpf_qdisc_verifier_ops = { .get_func_proto = bpf_base_func_proto, .is_valid_access = bpf_qdisc_is_valid_access, .btf_struct_access = bpf_qdisc_btf_struct_access, + .gen_prologue = bpf_qdisc_gen_prologue, + .gen_epilogue = bpf_qdisc_gen_epilogue, }; static int bpf_qdisc_init_member(const struct btf_type *t,
@@ -250,6 +349,11 @@ static int bpf_qdisc_init_member(const struct btf_type *t, moff =__btf_member_bit_offset (t, member) /8 ;switch (moff) { +case offsetof(struct Qdisc_ops , priv_size): +if (uqdisc_ops->priv_size) +return -EINVAL ; + qdisc_ops->priv_size =sizeof (struct bpf_sched_data ); +return 1 ;case offsetof(struct Qdisc_ops , peek): qdisc_ops->peek = qdisc_peek_dequeued;return 0 ;
Signature · 1 line
2.47.1
PATCH v7 5/10 bpf: net_sched: Support updating bstats
In reply to Amery HungFrom: Amery Hung <[email protected]>
Add a kfunc to update Qdisc bstats when an skb is dequeued. The kfunc is only available in .dequeue programs.
--- net/sched/bpf_qdisc.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
net/sched/bpf_qdisc.c +11 -0
@@ -254,6 +254,15 @@ __bpf_kfunc void bpf_qdisc_reset_destroy_epilogue(struct Qdisc *sch)qdisc_watchdog_cancel (&q->watchdog); } +/* bpf_qdisc_bstats_update - Update Qdisc basic statistics +* @sch: The qdisc from which an skb is dequeued. +* @skb: The skb to be dequeued. +*/ +__bpf_kfunc void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) +{ +bstats_update (&sch->bstats, skb); +} +__bpf_kfunc_end_defs ();BTF_KFUNCS_START (qdisc_kfunc_ids)
@@ -264,6 +273,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_from_skb, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_watchdog_schedule, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_init_prologue, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_qdisc_reset_destroy_epilogue, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_qdisc_bstats_update, KF_TRUSTED_ARGS)BTF_KFUNCS_END (qdisc_kfunc_ids)BTF_SET_START (qdisc_common_kfunc_set)
@@ -279,6 +289,7 @@ BTF_SET_END(qdisc_enqueue_kfunc_set)BTF_SET_START (qdisc_dequeue_kfunc_set)BTF_ID (func ,bpf_qdisc_watchdog_schedule ) +BTF_ID (func, bpf_qdisc_bstats_update)BTF_SET_END (qdisc_dequeue_kfunc_set) enum qdisc_ops_kf_flags {
Signature · 1 line
2.47.1
PATCH v7 6/10 bpf: net_sched: Disable attaching bpf qdisc to non root
In reply to Amery HungDo not allow users to attach bpf qdiscs to classful qdiscs. This is to prevent accidentally breaking existings classful qdiscs if they rely on some data in the child qdisc. This restriction can potentially be lifted in the future. Note that, we still allow bpf qdisc to be attached to mq.
--- net/sched/bpf_qdisc.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
net/sched/bpf_qdisc.c +23 -2
@@ -158,13 +158,19 @@ static int bpf_qdisc_gen_prologue(struct bpf_insn *insn_buf, bool direct_write,return 0 ;/* r6 = r1; // r6 will be "u64 *ctx". r1 is "u64 *ctx". +* r2 = r1[16]; // r2 will be "struct netlink_ext_ack *extack" * r1 = r1[0]; // r1 will be "struct Qdisc *sch" -* r0 = bpf_qdisc_init_prologue(r1); +* r0 = bpf_qdisc_init_prologue(r1, r2); +* if r0 == 0 goto pc+1; +* BPF_EXIT; * r1 = r6; // r1 will be "u64 *ctx". */ *insn++ =BPF_MOV64_REG (BPF_REG_6 ,BPF_REG_1 ); + *insn++ =BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,16 ); *insn++ =BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 ,0 ); *insn++ =BPF_CALL_KFUNC (0 , bpf_qdisc_init_prologue_ids[0 ]); + *insn++ =BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,1 ); + *insn++ =BPF_EXIT_INSN (); *insn++ =BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ); *insn++ = prog->insnsi[0 ];
@@ -237,11 +243,26 @@ __bpf_kfunc void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 }/* bpf_qdisc_init_prologue - Hidden kfunc called in prologue of .init. */ -__bpf_kfunc void bpf_qdisc_init_prologue(struct Qdisc *sch) +__bpf_kfunc int bpf_qdisc_init_prologue(struct Qdisc *sch, +struct netlink_ext_ack *extack) {struct bpf_sched_data *q =qdisc_priv (sch); +struct net_device *dev =qdisc_dev (sch); +struct Qdisc *p; + +if (sch->parent !=TC_H_ROOT ) { + p =qdisc_lookup (dev,TC_H_MAJ (sch->parent)); +if (!p) +return -ENOENT ; + +if (!(p->flags &TCQ_F_MQROOT )) { +NL_SET_ERR_MSG (extack,"BPF qdisc only supported on root or mq" ); +return -EINVAL ; + } + }qdisc_watchdog_init (&q->watchdog, sch); +return 0 ; }/* bpf_qdisc_reset_destroy_epilogue - Hidden kfunc called in epilogue of .reset
Signature · 1 line
2.47.1
PATCH v7 7/10 libbpf: Support creating and destroying qdisc
In reply to Amery HungFrom: Amery Hung <[email protected]>
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.
--- tools/lib/bpf/libbpf.h | 5 ++++- tools/lib/bpf/netlink.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.h +4 -1
@@ -1283,6 +1283,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) \
@@ -1297,9 +1298,11 @@ struct bpf_tc_hook {int ifindex;enum bpf_tc_attach_point attach_point;__u32 parent; +__u32 handle; +const char *qdisc; size_t :0; }; -#define bpf_tc_hook__last_field parent +#define bpf_tc_hook__last_field qdiscstruct bpf_tc_opts {size_t sz;
tools/lib/bpf/netlink.c +17 -3
@@ -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,const struct bpf_tc_hook *hook); -static int clsact_config (struct libbpf_nla_req *req) +static int clsact_config (struct libbpf_nla_req *req,const 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,const struct bpf_tc_hook *hook) +{ +const 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 :
Signature · 1 line
2.47.1
PATCH v7 8/10 selftests/bpf: Add a basic fifo qdisc test
In reply to Amery HungFrom: Amery Hung <[email protected]>
This selftest includes a bare minimum fifo qdisc, which simply enqueues sk_buffs into the back of a bpf list and dequeues from the front of the list.
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 81 ++++++++++++ .../selftests/bpf/progs/bpf_qdisc_common.h | 31 +++++ .../selftests/bpf/progs/bpf_qdisc_fifo.c | 117 ++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_common.h create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c
tools/testing/selftests/bpf/config +1 -0
@@ -71,6 +71,7 @@ CONFIG_NET_IPGRE=y CONFIG_NET_IPGRE_DEMUX=y CONFIG_NET_IPIP=y CONFIG_NET_MPLS_GSO=y +CONFIG_NET_SCH_BPF=y CONFIG_NET_SCH_FQ=y CONFIG_NET_SCH_INGRESS=y CONFIG_NET_SCHED=y
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c new file +81 -0
81 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _BPF_QDISC_COMMON_H +#define _BPF_QDISC_COMMON_H + +#define NET_XMIT_SUCCESS 0x00 +#define NET_XMIT_DROP 0x01/* skb dropped */ +#define NET_XMIT_CN 0x02/* congestion notification */ + +#define TC_PRIO_CONTROL 7 +#define TC_PRIO_MAX 15 + +#define private (name) SEC(".data." #name) __hidden __attribute__((aligned(8))) + +u32 bpf_skb_get_hash(struct sk_buff *p) __ksym; +void bpf_kfree_skb(struct sk_buff *p) __ksym; +void bpf_qdisc_skb_drop(struct sk_buff *p, struct bpf_sk_buff_ptr *to_free) __ksym; +void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns) __ksym; +void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) __ksym; + +static struct qdisc_skb_cb *qdisc_skb_cb (const struct sk_buff *skb) +{ +return (struct qdisc_skb_cb *)skb->cb; +} + +static inline unsigned int qdisc_pkt_len (const struct sk_buff *skb) +{ +return qdisc_skb_cb (skb)->pkt_len; +} + +#endif
tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c new file +117 -0
117 lines not on this page · raw message
Signature · 1 line
2.47.1
Re: [PATCH bpf-next v7 08/10] selftests/bpf: Add a basic fifo qdisc test
Quoted 1 line · Amery Hung
+void
test_bpf_qdisc(void)
nit. re-name to "test_ns_bpf_qdisc"....
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c line 73
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c new file +4
-0+{ +struct netns_obj *netns; + + netns =netns_new ("bpf_qdisc_ns" , true);
... then this can be saved.
Quoted 23 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c new file +8
-0+if (!ASSERT_OK_PTR (netns,"netns_new" )) +return ; + +if (test__start_subtest ("fifo" )) +test_fifo (); + +netns_free (netns); +}tools/testing/selftests/bpf/progs/bpf_qdisc_common.h new file +9
-0@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _BPF_QDISC_COMMON_H +#define _BPF_QDISC_COMMON_H + +#define NET_XMIT_SUCCESS 0x00 +#define NET_XMIT_DROP 0x01/* skb dropped */ +#define NET_XMIT_CN 0x02/* congestion notification */ +
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h line 19
Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h new file +10
-0+#define TC_PRIO_CONTROL 7 +#define TC_PRIO_MAX 15 + +#define private (name) SEC(".data." #name) __hidden __attribute__((aligned(8))) + +u32 bpf_skb_get_hash(struct sk_buff *p) __ksym; +void bpf_kfree_skb(struct sk_buff *p) __ksym; +void bpf_qdisc_skb_drop(struct sk_buff *p, struct bpf_sk_buff_ptr *to_free) __ksym; +void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns) __ksym; +void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) __ksym;
nit. These kfunc declarations should be no longer needed. vmlinux.h should already have them. Update pahole if vmlinux.h does not have them.
The set has been applied. Please consider following up the nits in selftests. Thanks.
Re: [PATCH bpf-next v7 08/10] selftests/bpf: Add a basic fifo qdisc test
Quoted 51 lines · Martin KaFai Lau
Quoted 1 line · Amery Hung
+void
test_bpf_qdisc(void)nit. re-name to "
test_ns_bpf_qdisc"....tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c line 73
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c new file +4
-0+{ +struct netns_obj *netns; + + netns =netns_new ("bpf_qdisc_ns" , true);... then this can be saved.
Quoted 23 lines
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c new file +8
-0+if (!ASSERT_OK_PTR (netns,"netns_new" )) +return ; + +if (test__start_subtest ("fifo" )) +test_fifo (); + +netns_free (netns); +}tools/testing/selftests/bpf/progs/bpf_qdisc_common.h new file +9
-0@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _BPF_QDISC_COMMON_H +#define _BPF_QDISC_COMMON_H + +#define NET_XMIT_SUCCESS 0x00 +#define NET_XMIT_DROP 0x01/* skb dropped */ +#define NET_XMIT_CN 0x02/* congestion notification */ +tools/testing/selftests/bpf/progs/bpf_qdisc_common.h line 19
Quoted 10 lines
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h new file +10
-0+#define TC_PRIO_CONTROL 7 +#define TC_PRIO_MAX 15 + +#define private (name) SEC(".data." #name) __hidden __attribute__((aligned(8))) + +u32 bpf_skb_get_hash(struct sk_buff *p) __ksym; +void bpf_kfree_skb(struct sk_buff *p) __ksym; +void bpf_qdisc_skb_drop(struct sk_buff *p, struct bpf_sk_buff_ptr *to_free) __ksym; +void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns) __ksym; +void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) __ksym;nit. These kfunc declarations should be no longer needed.
vmlinux.hshould already have them. Update pahole ifvmlinux.hdoes not have them.
Quoted 2 lines
The set has been applied. Please consider following up the nits in selftests. Thanks.
I will send a follow up addressing two things you mentioned. Thanks for the suggestions.
PATCH v7 9/10 selftests/bpf: Add a bpf fq qdisc to selftest
In reply to Amery HungFrom: Amery Hung <[email protected]>
This test implements a more sophisticated qdisc using bpf. The bpf fair- queueing (fq) qdisc gives each flow an equal chance to transmit data. It also respects the timestamp of skb for rate limiting.
--- .../selftests/bpf/prog_tests/bpf_qdisc.c | 24 + .../selftests/bpf/progs/bpf_qdisc_fq.c | 750 ++++++++++++++++++ 2 files changed, 774 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +24 -0
@@ -6,6 +6,7 @@#include "network_helpers.h" #include "bpf_qdisc_fifo.skel.h" +#include "bpf_qdisc_fq.skel.h" #define LO_IFINDEX 1
@@ -66,6 +67,27 @@ static void test_fifo(void)bpf_qdisc_fifo__destroy (fifo_skel); } +static void test_fq (void ) +{ +struct bpf_qdisc_fq *fq_skel; +struct bpf_link *link; + + fq_skel =bpf_qdisc_fq__open_and_load (); +if (!ASSERT_OK_PTR (fq_skel,"bpf_qdisc_fq__open_and_load" )) +return ; + + link =bpf_map__attach_struct_ops (fq_skel->maps.fq); +if (!ASSERT_OK_PTR (link,"bpf_map__attach_struct_ops" )) { +bpf_qdisc_fq__destroy (fq_skel); +return ; + } + +do_test ("bpf_fq" ); + +bpf_link__destroy (link); +bpf_qdisc_fq__destroy (fq_skel); +} +void test_bpf_qdisc (void ) {struct netns_obj *netns;
@@ -76,6 +98,8 @@ void test_bpf_qdisc(void)if (test__start_subtest ("fifo" ))test_fifo (); +if (test__start_subtest ("fq" )) +test_fq ();netns_free (netns); }
tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c new file +750 -0
750 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH v7 10/10 selftests/bpf: Test attaching bpf qdisc to mq and non root
In reply to Amery HungUntil we are certain that existing classful qdiscs work with bpf qdisc, make sure we don't allow attaching a bpf qdisc to non root. Meanwhile, attaching to mq is allowed.
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/bpf_qdisc.c | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+)
tools/testing/selftests/bpf/config +1 -0
@@ -74,6 +74,7 @@ CONFIG_NET_MPLS_GSO=y CONFIG_NET_SCH_BPF=y CONFIG_NET_SCH_FQ=y CONFIG_NET_SCH_INGRESS=y +CONFIG_NET_SCH_HTB=y CONFIG_NET_SCHED=y CONFIG_NETDEVSIM=y CONFIG_NETFILTER=y
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +75 -0
87 lines not on this page · raw message
Signature · 1 line
2.47.1
This series was applied to bpf/bpf-next.git (net) by Martin KaFai Lau <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (net)
by Martin KaFai Lau <[email protected]>:
Quoted 11 lines · On Wed, 9 Apr 2025 14:45:56 -0700 you wrote:
Hi all,
This patchset aims to support implementing qdisc using bpf
struct_ops. This version takes a step back and only implements the minimum support for bpf qdisc. 1) support of adding skb tobpf_listandbpf_rbtreedirectly and 2) classful qdisc are deferred to future patchsets. In addition, we only allow attaching bpf qdisc to root or mq for now. This is to prevent accidentally breaking exisiting classful qdiscs that rely on data in a child qdisc. This limit may be lifted in the future after careful inspection.
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,v7,01/10] bpf: Prepare to reuse get_ctx_arg_idx
https://git.kernel.org/bpf/bpf-next/c/a5c234d9bac6 - [bpf-next,v7,02/10] bpf: net_sched: Support implementation of Qdisc_ops in bpf (no matching commit) - [bpf-next,v7,03/10] bpf: net_sched: Add basic bpf qdisc kfuncs https://git.kernel.org/bpf/bpf-next/c/81bb1c2c3e8e - [bpf-next,v7,04/10] bpf: net_sched: Add a qdisc watchdog timer https://git.kernel.org/bpf/bpf-next/c/f2ccce960e92 - [bpf-next,v7,05/10] bpf: net_sched: Support updating bstats https://git.kernel.org/bpf/bpf-next/c/9658e4249727 - [bpf-next,v7,06/10] bpf: net_sched: Disable attaching bpf qdisc to non root https://git.kernel.org/bpf/bpf-next/c/0ddc97ad5337 - [bpf-next,v7,07/10] libbpf: Support creating and destroying qdisc https://git.kernel.org/bpf/bpf-next/c/8c52471cd771 - [bpf-next,v7,08/10] selftests/bpf: Add a basic fifo qdisc test https://git.kernel.org/bpf/bpf-next/c/cb7e598ef547 - [bpf-next,v7,09/10] selftests/bpf: Add a bpf fq qdisc to selftest https://git.kernel.org/bpf/bpf-next/c/69dfd77a5adc - [bpf-next,v7,10/10] selftests/bpf: Test attaching bpf qdisc to mq and non root https://git.kernel.org/bpf/bpf-next/c/93cb5375fb16
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html