Add eBPF hooks for cgroups
Subsystems (6)
v3 cover Add eBPF hooks for cgroups
This is v3 of the patch set to allow eBPF programs for network filtering and accounting to be attached to cgroups, so that they apply to all sockets of all tasks placed in that cgroup. The logic also allows to be extendeded for other cgroup based eBPF logic.
I am posting this now with only very few changes from v2 because I'll be travelling for a couple of days and won't have access to my mails.
Changes from v2:
* Fixed the RCU locking details Tejun pointed out.
* Assert bpf_attr.flags == 0 in BPF_PROG_DETACH syscall handler.
Changes from v1:
* Moved all bpf specific cgroup code into its own file, and stub out related functions for !CONFIG_CGROUP_BPF as static inline nops. This way, the call sites are not cluttered with #ifdef guards while the feature remains compile-time configurable.
* Implemented the new scheme proposed by Tejun. Per cgroup, store one set of pointers that are pinned to the cgroup, and one for the programs that are effective. When a program is attached or detached, the change is propagated to all the cgroup's descendants. If a subcgroup has its own pinned program, skip the whole subbranch in order to allow delegation models.
* The hookup for egress packets is now done from __dev_queue_xmit().
* A static key is now used in both the ingress and egress fast paths to keep performance penalties close to zero if the feature is not in use.
* Overall cleanup to make the accessors use the program arrays. This should make it much easier to add new program types, which will then automatically follow the pinned vs. effective logic.
* Fixed locking issues, as pointed out by Eric Dumazet and Alexei Starovoitov. Changes to the program array are now done with xchg() and are protected by cgroup_mutex.
* eBPF programs are now expected to return 1 to let the packet pass, not >= 0. Pointed out by Alexei.
* Operation is now limited to INET sockets, so local AF_UNIX sockets are not affected. The enum members are renamed accordingly. In case other socket families should be supported, this can be extended in the future.
* The sample program learned to support both ingress and egress, and can now optionally make the eBPF program drop packets by making it return 0.
As always, feedback is much appreciated.
Thanks,
Daniel
Daniel Mack (6):
bpf: add new prog type for cgroup socket filtering
cgroup: add support for eBPF programs
bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
net: filter: run cgroup eBPF ingress programs
net: core: run cgroup eBPF egress programs
samples: bpf: add userspace example for attaching eBPF programs to
cgroups
include/linux/bpf-cgroup.h | 70 +++++++++++++++++ include/linux/cgroup-defs.h | 4 + include/uapi/linux/bpf.h | 16 ++++ init/Kconfig | 12 +++ kernel/bpf/Makefile | 1 + kernel/bpf/cgroup.c | 165 ++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 83 ++++++++++++++++++++ kernel/bpf/verifier.c | 1 + kernel/cgroup.c | 18 +++++ net/core/dev.c | 6 ++ net/core/filter.c | 11 +++ samples/bpf/Makefile | 2 + samples/bpf/libbpf.c | 23 ++++++ samples/bpf/libbpf.h | 3 + samples/bpf/test_cgrp2_attach.c | 147 +++++++++++++++++++++++++++++++++++ 15 files changed, 562 insertions(+) create mode 100644 include/linux/bpf-cgroup.h create mode 100644 kernel/bpf/cgroup.c create mode 100644 samples/bpf/test_cgrp2_attach.c
Signature · 1 line
2.5.5
PATCH v3 1/6 bpf: add new prog type for cgroup socket filtering
For now, this program type is equivalent to BPF_PROG_TYPE_SOCKET_FILTER in terms of checks during the verification process. It may access the skb as well.
Programs of this type will be attached to cgroups for network filtering and accounting.
--- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/verifier.c | 1 + net/core/filter.c | 6 ++++++ 3 files changed, 14 insertions(+)
include/uapi/linux/bpf.h +7 -0
@@ -95,6 +95,13 @@ enum bpf_prog_type {BPF_PROG_TYPE_SCHED_ACT ,BPF_PROG_TYPE_TRACEPOINT ,BPF_PROG_TYPE_XDP , + BPF_PROG_TYPE_CGROUP_SOCKET_FILTER, +}; + +enum bpf_attach_type { +BPF_ATTACH_TYPE_CGROUP_INET_INGRESS , +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS , + __MAX_BPF_ATTACH_TYPE };#define BPF_PSEUDO_MAP_FD 1
kernel/bpf/verifier.c +1 -0
@@ -1805,6 +1805,7 @@ static bool may_access_skb(enum bpf_prog_type type)case BPF_PROG_TYPE_SOCKET_FILTER :case BPF_PROG_TYPE_SCHED_CLS :case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_CGROUP_SOCKET_FILTER :return true;default :return false;
net/core/filter.c +6 -0
@@ -2848,12 +2848,18 @@ static struct bpf_prog_type_list xdp_type __read_mostly = { .type = BPF_PROG_TYPE_XDP, }; +static struct bpf_prog_type_list cg_sk_filter_type__read_mostly = { + .ops = &sk_filter_ops, + .type =BPF_PROG_TYPE_CGROUP_SOCKET_FILTER , +}; +static int __init register_sk_filter_ops (void ) {bpf_register_prog_type (&sk_filter_type);bpf_register_prog_type (&sched_cls_type);bpf_register_prog_type (&sched_act_type);bpf_register_prog_type (&xdp_type); +bpf_register_prog_type (&cg_sk_filter_type);return 0 ; }
Signature · 1 line
2.5.5
PATCH v3 2/6 cgroup: add support for eBPF programs
In reply to Daniel MackThis patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
A - B - C
\ D - E
If only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
Attaching and detaching programs will be done through the bpf(2) syscall. For now, ingress and egress inet socket filtering are the only supported use-cases.
--- include/linux/bpf-cgroup.h | 70 +++++++++++++++++++ include/linux/cgroup-defs.h | 4 ++ init/Kconfig | 12 ++++ kernel/bpf/Makefile | 1 + kernel/bpf/cgroup.c | 165 ++++++++++++++++++++++++++++++++++++++++++++ kernel/cgroup.c | 18 +++++ 6 files changed, 270 insertions(+) create mode 100644 include/linux/bpf-cgroup.h create mode 100644 kernel/bpf/cgroup.c
include/linux/bpf-cgroup.h new file +70 -0
@@ -0,0 +1,70 @@ +#ifndef _BPF_CGROUP_H +#define _BPF_CGROUP_H + +#include <linux/bpf.h> +#include <uapi/linux/bpf.h> + +struct sock ; +struct cgroup ; +struct sk_buff ; + +#ifdef CONFIG_CGROUP_BPF + +extern struct static_key_false cgroup_bpf_enabled_key; +#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) + +struct cgroup_bpf { +/* +* Store two sets of bpf_prog pointers, one for programs that are +* pinned directly to this cgroup, and one for those that are effective +* when this cgroup is accessed. +*/ +struct bpf_prog *prog[__MAX_BPF_ATTACH_TYPE]; +struct bpf_prog *effective[__MAX_BPF_ATTACH_TYPE]; +}; + +void cgroup_bpf_put (struct cgroup *cgrp); +void cgroup_bpf_inherit (struct cgroup *cgrp,struct cgroup *parent); + +void __cgroup_bpf_update (struct cgroup *cgrp, +struct cgroup *parent, +struct bpf_prog *prog, +enum bpf_attach_type type); + +/* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */ +void cgroup_bpf_update (struct cgroup *cgrp, +struct bpf_prog *prog, +enum bpf_attach_type type); + +int __cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type); + +/* Wrapper for __cgroup_bpf_run_filter() guarded by cgroup_bpf_enabled */ +static inline int cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type) +{ +if (cgroup_bpf_enabled) +return __cgroup_bpf_run_filter (sk, skb, type); + +return 0 ; +} + +#else + +struct cgroup_bpf {}; +static inline void cgroup_bpf_put (struct cgroup *cgrp) {} +static inline void cgroup_bpf_inherit (struct cgroup *cgrp, +struct cgroup *parent) {} + +static inline int cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type) +{ +return 0 ; +} + +#endif /* CONFIG_CGROUP_BPF */ + +#endif /* _BPF_CGROUP_H */
include/linux/cgroup-defs.h +4 -0
@@ -16,6 +16,7 @@#include <linux/percpu-refcount.h> #include <linux/percpu-rwsem.h> #include <linux/workqueue.h> +#include <linux/bpf-cgroup.h> #ifdef CONFIG_CGROUPS
@@ -300,6 +301,9 @@ struct cgroup {/* used to schedule release agent */ struct work_struct release_agent_work; +/* used to store eBPF programs */ +struct cgroup_bpf bpf; +/* ids of the ancestors at each level including self */ int ancestor_ids[]; };
init/Kconfig +12 -0
@@ -1144,6 +1144,18 @@ config CGROUP_PERF Say N if unsure. +config CGROUP_BPF + bool "Support for eBPF programs attached to cgroups" + depends on BPF_SYSCALL && SOCK_CGROUP_DATA + help + Allow attaching eBPF programs to a cgroup using the bpf(2) + syscall command BPF_PROG_ATTACH. + + In which context these programs are accessed depends on the type + of attachment. For instance, programs that are attached using + BPF_ATTACH_TYPE_CGROUP_INET_INGRESS will be executed on the + ingress path of inet sockets. + config CGROUP_DEBUG bool "Example controller" default n
kernel/bpf/Makefile +1 -0
@@ -5,3 +5,4 @@ obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o ifeq ($(CONFIG_PERF_EVENTS),y) obj-$(CONFIG_BPF_SYSCALL) += stackmap.o endif +obj-$(CONFIG_CGROUP_BPF) += cgroup.o
kernel/bpf/cgroup.c new file +165 -0
165 lines not on this page · raw message
kernel/cgroup.c +18 -0
@@ -5038,6 +5038,8 @@ static void css_release_work_fn(struct work_struct *work)if (cgrp->kn)RCU_INIT_POINTER (*(void __rcu __force **)&cgrp->kn->priv,NULL ); + +cgroup_bpf_put (cgrp); }mutex_unlock (&cgroup_mutex);
@@ -5245,6 +5247,9 @@ static struct cgroup *cgroup_create(struct cgroup *parent)if (!cgroup_on_dfl (cgrp)) cgrp->subtree_control =cgroup_control (cgrp); +if (parent) +cgroup_bpf_inherit (cgrp, parent); +cgroup_propagate_control (cgrp);/* @cgrp doesn't have dir yet so the following will only create csses */
@@ -6417,6 +6422,19 @@ static __init int cgroup_namespaces_init(void) }subsys_initcall (cgroup_namespaces_init); +#ifdef CONFIG_CGROUP_BPF +void cgroup_bpf_update (struct cgroup *cgrp, +struct bpf_prog *prog, +enum bpf_attach_type type) +{ +struct cgroup *parent =cgroup_parent (cgrp); + +mutex_lock (&cgroup_mutex); +__cgroup_bpf_update (cgrp, parent, prog, type); +mutex_unlock (&cgroup_mutex); +} +#endif /* CONFIG_CGROUP_BPF */ +#ifdef CONFIG_CGROUP_DEBUG static struct cgroup_subsys_state *debug_css_alloc (struct cgroup_subsys_state *parent_css)
Signature · 1 line
2.5.5
PATCH v3 3/6 bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackExtend the bpf(2) syscall by two new commands, BPF_PROG_ATTACH and BPF_PROG_DETACH which allow attaching and detaching eBPF programs to a target.
On the API level, the target could be anything that has an fd in userspace, hence the name of the field in union bpf_attr is called 'target_fd'.
When called with BPF_ATTACH_TYPE_CGROUP_INET_{E,IN}GRESS, the target is expected to be a valid file descriptor of a cgroup v2 directory which has the bpf controller enabled. These are the only use-cases implemented by this patch at this point, but more can be added.
If a program of the given type already exists in the given cgroup, the program is swapped automically, so userspace does not have to drop an existing program first before installing a new one, which would otherwise leave a gap in which no program is attached.
For more information on the propagation logic to subcgroups, please refer to the bpf cgroup controller implementation.
The API is guarded by CAP_NET_ADMIN.
syscall
--- include/uapi/linux/bpf.h | 9 ++++++ kernel/bpf/syscall.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+)
include/uapi/linux/bpf.h +9 -0
@@ -73,6 +73,8 @@ enum bpf_cmd {BPF_PROG_LOAD ,BPF_OBJ_PIN ,BPF_OBJ_GET , +BPF_PROG_ATTACH , + BPF_PROG_DETACH, }; enum bpf_map_type {
@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags; + }; } __attribute__((aligned(8)));/* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/syscall.c +83 -0
95 lines not on this page · raw message
Signature · 1 line
2.5.5
PATCH v3 4/6 net: filter: run cgroup eBPF ingress programs
In reply to Daniel MackIf the cgroup associated with the receiving socket has an eBPF programs installed, run them from sk_filter_trim_cap().
eBPF programs used in this context are expected to either return 1 to let the packet pass, or != 1 to drop them. The programs have access to the full skb, including the MAC headers.
Note that cgroup_bpf_run_filter() is stubbed out as static inline nop for !CONFIG_CGROUP_BPF, and is otherwise guarded by a static key if the feature is unused.
--- net/core/filter.c | 5 +++++ 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
@@ -78,6 +78,11 @@ int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)if (skb_pfmemalloc (skb) && !sock_flag (sk,SOCK_MEMALLOC ))return -ENOMEM ; + err =cgroup_bpf_run_filter (sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_INGRESS ); +if (err) +return err; + err =security_sock_rcv_skb (sk, skb);if (err)return err;
Signature · 1 line
2.5.5
PATCH v3 5/6 net: core: run cgroup eBPF egress programs
In reply to Daniel MackIf the cgroup associated with the receiving socket has an eBPF programs installed, run them from __dev_queue_xmit().
eBPF programs used in this context are expected to either return 1 to let the packet pass, or != 1 to drop them. The programs have access to the full skb, including the MAC headers.
Note that cgroup_bpf_run_filter() is stubbed out as static inline nop for !CONFIG_CGROUP_BPF, and is otherwise guarded by a static key if the feature is unused.
--- net/core/dev.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/core/dev.c +6 -0
@@ -141,6 +141,7 @@#include <linux/netfilter_ingress.h> #include <linux/sctp.h> #include <linux/crash_dump.h> +#include <linux/bpf-cgroup.h> #include "net-sysfs.h"
@@ -3329,6 +3330,11 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)if (unlikely (skb_shinfo (skb)->tx_flags &SKBTX_SCHED_TSTAMP ))__skb_tstamp_tx (skb,NULL , skb->sk,SCM_TSTAMP_SCHED ); + rc =cgroup_bpf_run_filter (skb->sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS ); +if (rc) +return rc; +/* Disable soft irqs for various locks below. Also * stops preemption for RCU. */
Signature · 1 line
2.5.5
PATCH v3 6/6 samples: bpf: add userspace example for attaching eBPF programs to cgroups
In reply to Daniel MackAdd a simple userpace program to demonstrate the new API to attach eBPF programs to cgroups. This is what it does:
* Create arraymap in kernel with 4 byte keys and 8 byte values
* Load eBPF program
The eBPF program accesses the map passed in to store two pieces of information. The number of invocations of the program, which maps to the number of packets received, is stored to key 0. Key 1 is incremented on each iteration by the number of bytes stored in the skb.
* Detach any eBPF program previously attached to the cgroup
* Attach the new program to the cgroup using BPF_PROG_ATTACH
* Once a second, read map[0] and map[1] to see how many bytes and packets were seen on any socket of tasks in the given cgroup.
The program takes a cgroup path as 1st argument, and either "ingress" or "egress" as 2nd. Optionally, "drop" can be passed as 3rd argument, which will make the generated eBPF program return 0 instead of 1, so the kernel will drop the packet.
libbpf gained two new wrappers for the new syscall commands.
--- samples/bpf/Makefile | 2 + samples/bpf/libbpf.c | 23 +++++++ samples/bpf/libbpf.h | 3 + samples/bpf/test_cgrp2_attach.c | 147 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 samples/bpf/test_cgrp2_attach.c
samples/bpf/Makefile +2 -0
@@ -22,6 +22,7 @@ hostprogs-y += spintest hostprogs-y += map_perf_test hostprogs-y += test_overhead hostprogs-y += test_cgrp2_array_pin +hostprogs-y += test_cgrp2_attach hostprogs-y += xdp1 hostprogs-y += xdp2 hostprogs-y += test_current_task_under_cgroup
@@ -47,6 +48,7 @@ spintest-objs := bpf_load.o libbpf.o spintest_user.o map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o test_overhead-objs := bpf_load.o libbpf.o test_overhead_user.o test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o +test_cgrp2_attach-objs := libbpf.o test_cgrp2_attach.o xdp1-objs := bpf_load.o libbpf.o xdp1_user.o # reuse xdp1 source intentionally xdp2-objs := bpf_load.o libbpf.o xdp1_user.o
samples/bpf/libbpf.c +23 -0
@@ -104,6 +104,29 @@ int bpf_prog_load(enum bpf_prog_type prog_type,return syscall (__NR_bpf,BPF_PROG_LOAD , &attr,sizeof (attr)); } +int bpf_prog_attach (int prog_fd,int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_bpf_fd = prog_fd, + .attach_type = type, + .attach_flags =0 , + }; + +return syscall (__NR_bpf,BPF_PROG_ATTACH , &attr,sizeof (attr)); +} + +int bpf_prog_detach (int target_fd,enum bpf_attach_type type) +{ +union bpf_attr attr = { + .target_fd = target_fd, + .attach_type = type, + .attach_flags =0 , + }; + +return syscall (__NR_bpf,BPF_PROG_DETACH , &attr,sizeof (attr)); +} +int bpf_obj_pin (int fd,const char *pathname) {union bpf_attr attr = {
samples/bpf/libbpf.h +3 -0
@@ -15,6 +15,9 @@ int bpf_prog_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, int insn_len, const char *license, int kern_version); +int bpf_prog_attach (int prog_fd,int attachable_fd,enum bpf_attach_type type); +int bpf_prog_detach (int attachable_fd,enum bpf_attach_type type); +int bpf_obj_pin (int fd,const char *pathname);int bpf_obj_get (const char *pathname);
samples/bpf/test_cgrp2_attach.c new file +147 -0
147 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Daniel MackQuoted 20 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
Attaching and detaching programs will be done through the bpf(2) syscall. For now, ingress and egress inet socket filtering are the only supported use-cases.
Signed-off-by Daniel Mack <[email protected]>
...
kernel/bpf/cgroup.c line 111
Quoted 5 lines
kernel/bpf/cgroup.c new file +5
-0+css_for_each_descendant_pre (pos, &cgrp->self) { + struct cgroup *desc = container_of(pos, struct cgroup, self); + +/* skip the subtree if the descendant has its own program */ +if (desc->bpf.prog[type] && desc != cgrp)
is desc != cgrp really needed?
I thought css_for_each_descendant_pre() shouldn't walk itself or I'm missing how it works.
Quoted 7 lines
kernel/bpf/cgroup.c new file +7
-0+ pos =css_rightmost_descendant (pos); + else + rcu_assign_pointer(desc->bpf.effective[type], +effective ); + } +} +
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackQuoted 44 lines · Daniel Mack
Extend the bpf(2) syscall by two new commands,
BPF_PROG_ATTACHandBPF_PROG_DETACHwhich allow attaching and detaching eBPF programs to a target.On the API level, the target could be anything that has an fd in userspace, hence the name of the field in union
bpf_attris called 'target_fd'.When called with BPF_ATTACH_TYPE_CGROUP_INET_{E,IN}GRESS, the target is expected to be a valid file descriptor of a cgroup v2 directory which has the bpf controller enabled. These are the only use-cases implemented by this patch at this point, but more can be added.
If a program of the given type already exists in the given cgroup, the program is swapped automically, so userspace does not have to drop an existing program first before installing a new one, which would otherwise leave a gap in which no program is attached.
For more information on the propagation logic to subcgroups, please refer to the bpf cgroup controller implementation.
The API is guarded by
CAP_NET_ADMIN.Signed-off-by Daniel Mack <[email protected]>syscall
--- include/uapi/linux/bpf.h | 9 ++++++ kernel/bpf/syscall.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+)include/uapi/linux/bpf.h +2
-0@@ -73,6 +73,8 @@ enum bpf_cmd {BPF_PROG_LOAD ,BPF_OBJ_PIN ,BPF_OBJ_GET , +BPF_PROG_ATTACH , + BPF_PROG_DETACH, }; enum bpf_map_type {
include/uapi/linux/bpf.h union bpf_attr { · line 158
Quoted 10 lines
include/uapi/linux/bpf.h +7
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags; + };
there is a 4 byte hole in this struct. Can we pack it differently?
Quoted 34 lines
include/uapi/linux/bpf.h +0
-0} __attribute__((aligned(8)));/* integer value in 'imm' field of BPF_CALL instruction selects which helper kernel/bpf/syscall.c +23
-0@@ -822,6 +822,79 @@ static int bpf_obj_get(const union bpf_attr *attr)return bpf_obj_get_user (u64_to_ptr (attr->pathname)); } +#ifdef CONFIG_CGROUP_BPF +static int bpf_prog_attach (const union bpf_attr *attr) +{ +struct bpf_prog *prog; + +if (!capable (CAP_NET_ADMIN )) +return -EPERM ; + +/* Flags are unused for now */ +if (attr->attach_flags !=0 ) +return -EINVAL ; + +switch (attr->attach_type) { +case BPF_ATTACH_TYPE_CGROUP_INET_INGRESS : +case BPF_ATTACH_TYPE_CGROUP_INET_EGRESS : { +struct cgroup *cgrp; + + prog =bpf_prog_get_type (attr->attach_bpf_fd, +BPF_PROG_TYPE_CGROUP_SOCKET_FILTER ); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + cgrp =cgroup_get_from_fd (attr->target_fd);
kernel/bpf/syscall.c static int bpf_obj_get(const union bpf_attr *attr) · line 857
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+if (IS_ERR (cgrp)) { +bpf_prog_put (prog); +return PTR_ERR (cgrp); + } + +cgroup_bpf_update (cgrp, prog, attr->attach_type); +cgroup_put (cgrp); + +break ; + }
this } formatting style is confusing. The above } looks like it matches 'switch () {'.
May be move 'struct cgroup *cgrp' to the top to avoid that?
Hi Daniel,
I don't see the cgroups mailing list address in the cc list. Since this patch is related also to the cgroups subsystem, I would suggest that going forward you will cc also [email protected] to future patches related to cgroups. (I hope this won't cause exceeding the max cc list length for patches).
Regards,
Rami Rosen
Quoted 95 lines · Daniel Mack
This is v3 of the patch set to allow eBPF programs for network filtering and accounting to be attached to cgroups, so that they apply to all sockets of all tasks placed in that cgroup. The logic also allows to be extendeded for other cgroup based eBPF logic.
I am posting this now with only very few changes from v2 because I'll be travelling for a couple of days and won't have access to my mails.
Changes from v2:
* Fixed the RCU locking details Tejun pointed out.
* Assert bpf_attr.flags == 0 in
BPF_PROG_DETACHsyscall handler.Changes from v1:
* Moved all bpf specific cgroup code into its own file, and stub out related functions for !CONFIG_CGROUP_BPF as static inline nops. This way, the call sites are not cluttered with #ifdef guards while the feature remains compile-time configurable.
* Implemented the new scheme proposed by Tejun. Per cgroup, store one set of pointers that are pinned to the cgroup, and one for the programs that are effective. When a program is attached or detached, the change is propagated to all the cgroup's descendants. If a subcgroup has its own pinned program, skip the whole subbranch in order to allow delegation models.
* The hookup for egress packets is now done from
__dev_queue_xmit().* A static key is now used in both the ingress and egress fast paths to keep performance penalties close to zero if the feature is not in use.
* Overall cleanup to make the accessors use the program arrays. This should make it much easier to add new program types, which will then automatically follow the pinned vs. effective logic.
* Fixed locking issues, as pointed out by Eric Dumazet and Alexei Starovoitov. Changes to the program array are now done with
xchg()and are protected bycgroup_mutex.* eBPF programs are now expected to return 1 to let the packet pass, not >= 0. Pointed out by Alexei.
* Operation is now limited to INET sockets, so local
AF_UNIXsockets are not affected. The enum members are renamed accordingly. In case other socket families should be supported, this can be extended in the future.* The sample program learned to support both ingress and egress, and can now optionally make the eBPF program drop packets by making it return 0.
As always, feedback is much appreciated.
Thanks,
DanielDaniel Mack (6):
bpf: add new prog type for cgroup socket filtering
cgroup: add support for eBPF programs
bpf: addBPF_PROG_ATTACHandBPF_PROG_DETACHcommands
net: filter: run cgroup eBPF ingress programs
net: core: run cgroup eBPF egress programs
samples: bpf: add userspace example for attaching eBPF programs to
cgroupsinclude/linux/bpf-cgroup.h | 70 +++++++++++++++++ include/linux/cgroup-defs.h | 4 + include/uapi/linux/bpf.h | 16 ++++ init/Kconfig | 12 +++ kernel/bpf/Makefile | 1 + kernel/bpf/cgroup.c | 165 ++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 83 ++++++++++++++++++++ kernel/bpf/verifier.c | 1 + kernel/cgroup.c | 18 +++++ net/core/dev.c | 6 ++ net/core/filter.c | 11 +++ samples/bpf/Makefile | 2 + samples/bpf/libbpf.c | 23 ++++++ samples/bpf/libbpf.h | 3 + samples/bpf/test_cgrp2_attach.c | 147 +++++++++++++++++++++++++++++++++++ 15 files changed, 562 insertions(+) create mode 100644 include/linux/bpf-cgroup.h create mode 100644 kernel/bpf/cgroup.c create mode 100644 samples/bpf/test_cgrp2_attach.c--
2.5.5
Daniel Borkmann
maintainerRe: [PATCH v3 5/6] net: core: run cgroup eBPF egress programs
In reply to Daniel MackQuoted 29 lines · Daniel Mack
If the cgroup associated with the receiving socket has an eBPF programs installed, run them from
__dev_queue_xmit().eBPF programs used in this context are expected to either return 1 to let the packet pass, or != 1 to drop them. The programs have access to the full skb, including the MAC headers.
Note that
cgroup_bpf_run_filter()is stubbed out as static inline nop for !CONFIG_CGROUP_BPF, and is otherwise guarded by a static key if the feature is unused.Signed-off-by Daniel Mack <[email protected]>--- net/core/dev.c | 6 ++++++ 1 file changed, 6 insertions(+)net/core/dev.c +1
-0@@ -141,6 +141,7 @@#include <linux/netfilter_ingress.h> #include <linux/sctp.h> #include <linux/crash_dump.h> +#include <linux/bpf-cgroup.h> #include "net-sysfs.h"
net/core/dev.c static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv) · line 3336
Quoted 7 lines
net/core/dev.c +4
-0@@ -3329,6 +3330,11 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)if (unlikely (skb_shinfo (skb)->tx_flags &SKBTX_SCHED_TSTAMP ))__skb_tstamp_tx (skb,NULL , skb->sk,SCM_TSTAMP_SCHED ); + rc =cgroup_bpf_run_filter (skb->sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS ); +if (rc) +return rc;
This would leak the whole skb by the way.
Apart from that, could this be modeled w/o affecting the forwarding path (at some local output point where we know to have a valid socket)? Then you could also drop the !sk and sk->sk_family tests, and we wouldn't need to replicate parts of what clsact is doing as well. Hmm, maybe access to src/dst mac could be handled to be just zeroes since not available at that point?
Quoted 4 lines
net/core/dev.c +0
-0/* Disable soft irqs for various locks below. Also * stops preemption for RCU. */
Daniel Borkmann
maintainerRe: [PATCH v3 1/6] bpf: add new prog type for cgroup socket filtering
In reply to Daniel MackQuoted 19 lines · Daniel Mack
For now, this program type is equivalent to
BPF_PROG_TYPE_SOCKET_FILTERin terms of checks during the verification process. It may access the skb as well.Programs of this type will be attached to cgroups for network filtering and accounting.
Signed-off-by Daniel Mack <[email protected]>--- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/verifier.c | 1 + net/core/filter.c | 6 ++++++ 3 files changed, 14 insertions(+)include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h enum bpf_prog_type { · line 99
Quoted 5 lines
include/uapi/linux/bpf.h +2
-0@@ -95,6 +95,13 @@ enum bpf_prog_type {BPF_PROG_TYPE_SCHED_ACT ,BPF_PROG_TYPE_TRACEPOINT ,BPF_PROG_TYPE_XDP , + BPF_PROG_TYPE_CGROUP_SOCKET_FILTER, +};
Nit: can we drop the _FILTER suffix? So just leaving it at BPF_PROG_TYPE_CGROUP_SOCKET. Some of these use cases might not always strictly be related to filtering, so seems cleaner to just leave it out everywhere.
include/uapi/linux/bpf.h enum bpf_prog_type { · line 105
Quoted 6 lines
include/uapi/linux/bpf.h +5
-0+ +enum bpf_attach_type { +BPF_ATTACH_TYPE_CGROUP_INET_INGRESS , +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS , + __MAX_BPF_ATTACH_TYPE };
#define BPF_MAX_ATTACH_TYPE __BPF_MAX_ATTACH_TYPE
And then use that in your follow-up patches for declaring arrays, etc?
Quoted 38 lines
include/uapi/linux/bpf.h +0
-0#define BPF_PSEUDO_MAP_FD 1kernel/bpf/verifier.c +1
-0@@ -1805,6 +1805,7 @@ static bool may_access_skb(enum bpf_prog_type type)case BPF_PROG_TYPE_SOCKET_FILTER :case BPF_PROG_TYPE_SCHED_CLS :case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_CGROUP_SOCKET_FILTER :return true;default :return false;net/core/filter.c +6
-0@@ -2848,12 +2848,18 @@ static struct bpf_prog_type_list xdp_type __read_mostly = { .type = BPF_PROG_TYPE_XDP, }; +static struct bpf_prog_type_list cg_sk_filter_type__read_mostly = { + .ops = &sk_filter_ops, + .type =BPF_PROG_TYPE_CGROUP_SOCKET_FILTER , +}; +static int __init register_sk_filter_ops (void ) {bpf_register_prog_type (&sk_filter_type);bpf_register_prog_type (&sched_cls_type);bpf_register_prog_type (&sched_act_type);bpf_register_prog_type (&xdp_type); +bpf_register_prog_type (&cg_sk_filter_type);return 0 ; }
Re: [PATCH v3 5/6] net: core: run cgroup eBPF egress programs
In reply to Daniel BorkmannQuoted 40 lines · Daniel Borkmann
Quoted 29 lines · Daniel Mack
If the cgroup associated with the receiving socket has an eBPF programs installed, run them from
__dev_queue_xmit().eBPF programs used in this context are expected to either return 1 to let the packet pass, or != 1 to drop them. The programs have access to the full skb, including the MAC headers.
Note that
cgroup_bpf_run_filter()is stubbed out as static inline nop for !CONFIG_CGROUP_BPF, and is otherwise guarded by a static key if the feature is unused.Signed-off-by Daniel Mack <[email protected]>--- net/core/dev.c | 6 ++++++ 1 file changed, 6 insertions(+)net/core/dev.c +1
-0@@ -141,6 +141,7 @@#include <linux/netfilter_ingress.h> #include <linux/sctp.h> #include <linux/crash_dump.h> +#include <linux/bpf-cgroup.h> #include "net-sysfs.h" net/core/dev.c static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv) · line 3336
Quoted 7 lines
net/core/dev.c +4
-0@@ -3329,6 +3330,11 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)if (unlikely (skb_shinfo (skb)->tx_flags &SKBTX_SCHED_TSTAMP ))__skb_tstamp_tx (skb,NULL , skb->sk,SCM_TSTAMP_SCHED ); + rc =cgroup_bpf_run_filter (skb->sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS ); +if (rc) +return rc;This would leak the whole skb by the way.
Quoted 10 lines
Apart from that, could this be modeled w/o affecting the forwarding path (at some local output point where we know to have a valid socket)? Then you could also drop the !sk and
sk->sk_familytests, and we wouldn't need to replicate parts of what clsact is doing as well. Hmm, maybe access to src/dst mac could be handled to be just zeroes since not available at that point?Quoted 4 lines
net/core/dev.c +0
-0/* Disable soft irqs for various locks below. Also * stops preemption for RCU. */
Given this patchset only effects AF_INET, and AF_INET6, why not put the hooks at ip_output, and ip6_output
Daniel Borkmann
maintainerRe: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Daniel MackQuoted 20 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
Attaching and detaching programs will be done through the bpf(2) syscall. For now, ingress and egress inet socket filtering are the only supported use-cases.
Signed-off-by Daniel Mack <[email protected]>
[...]
Quoted 23 lines
kernel/bpf/cgroup.c new file +23
-0+void __cgroup_bpf_update (struct cgroup *cgrp, +struct cgroup *parent, +struct bpf_prog *prog, +enum bpf_attach_type type) +{ +struct bpf_prog *old_prog, *effective; +struct cgroup_subsys_state *pos; + + old_prog =xchg (cgrp->bpf.prog + type, prog); + +if (prog) +static_branch_inc (&cgroup_bpf_enabled_key); + +if (old_prog) { +bpf_prog_put (old_prog); +static_branch_dec (&cgroup_bpf_enabled_key); + } + + effective = (!prog && parent) ? +rcu_dereference_protected (parent->bpf.effective[type], +lockdep_is_held (&cgroup_mutex)) : + prog; +
kernel/bpf/cgroup.c line 116
Quoted 10 lines
kernel/bpf/cgroup.c new file +10
-0+css_for_each_descendant_pre (pos, &cgrp->self) { + struct cgroup *desc = container_of(pos, struct cgroup, self); + +/* skip the subtree if the descendant has its own program */ +if (desc->bpf.prog[type] && desc != cgrp) + pos =css_rightmost_descendant (pos); +else +rcu_assign_pointer (desc->bpf.effective[type], + effective); + }
Shouldn't the old_prog reference only be released right here at the end instead of above (otherwise this could race)?
Quoted 1 line
+}
Daniel Borkmann
maintainerRe: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackQuoted 18 lines · Daniel Mack
Extend the bpf(2) syscall by two new commands,
BPF_PROG_ATTACHandBPF_PROG_DETACHwhich allow attaching and detaching eBPF programs to a target.On the API level, the target could be anything that has an fd in userspace, hence the name of the field in union
bpf_attris called 'target_fd'.When called with BPF_ATTACH_TYPE_CGROUP_INET_{E,IN}GRESS, the target is expected to be a valid file descriptor of a cgroup v2 directory which has the bpf controller enabled. These are the only use-cases implemented by this patch at this point, but more can be added.
If a program of the given type already exists in the given cgroup, the program is swapped automically, so userspace does not have to drop an existing program first before installing a new one, which would otherwise leave a gap in which no program is attached.
Quoted 7 lines
For more information on the propagation logic to subcgroups, please refer to the bpf cgroup controller implementation.
The API is guarded by
CAP_NET_ADMIN.Signed-off-by Daniel Mack <[email protected]>syscall
^^^ slipped in?
Quoted 19 lines
--- include/uapi/linux/bpf.h | 9 ++++++ kernel/bpf/syscall.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+)include/uapi/linux/bpf.h +2
-0@@ -73,6 +73,8 @@ enum bpf_cmd {BPF_PROG_LOAD ,BPF_OBJ_PIN ,BPF_OBJ_GET , +BPF_PROG_ATTACH , + BPF_PROG_DETACH, }; enum bpf_map_type {
include/uapi/linux/bpf.h union bpf_attr { · line 157
Quoted 9 lines
include/uapi/linux/bpf.h +6
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags;
Could we just do ...
__u32 dst_fd;__u32 src_fd;__u32 attach_type;
... and leave flags out, since unused anyway? Also see below.
kernel/bpf/syscall.c static int bpf_obj_get(const union bpf_attr *attr) · line 825
Quoted 13 lines
include/uapi/linux/bpf.h +1
-0+ }; } __attribute__((aligned(8)));/* integer value in 'imm' field of BPF_CALL instruction selects which helper kernel/bpf/syscall.c +1
-0@@ -822,6 +822,79 @@ static int bpf_obj_get(const union bpf_attr *attr)return bpf_obj_get_user (u64_to_ptr (attr->pathname)); } +#ifdef CONFIG_CGROUP_BPF
#define BPF_PROG_ATTACH_LAST_FIELD attach_type #define BPF_PROG_DETACH_LAST_FIELD BPF_PROG_ATTACH_LAST_FIELD
kernel/bpf/syscall.c static int bpf_obj_get(const union bpf_attr *attr) · line 835
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+static int bpf_prog_attach (const union bpf_attr *attr) +{ +struct bpf_prog *prog; + +if (!capable (CAP_NET_ADMIN )) +return -EPERM ; + +/* Flags are unused for now */ +if (attr->attach_flags !=0 ) +return -EINVAL ;
Correct way would be to:
if (CHECK_ATTR(BPF_PROG_ATTACH)) return -EINVAL;
Quoted 28 lines
kernel/bpf/syscall.c +28
-0+ +switch (attr->attach_type) { +case BPF_ATTACH_TYPE_CGROUP_INET_INGRESS : +case BPF_ATTACH_TYPE_CGROUP_INET_EGRESS : { +struct cgroup *cgrp; + + prog =bpf_prog_get_type (attr->attach_bpf_fd, +BPF_PROG_TYPE_CGROUP_SOCKET_FILTER ); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + cgrp =cgroup_get_from_fd (attr->target_fd); +if (IS_ERR (cgrp)) { +bpf_prog_put (prog); +return PTR_ERR (cgrp); + } + +cgroup_bpf_update (cgrp, prog, attr->attach_type); +cgroup_put (cgrp); + +break ; + } + +default : +return -EINVAL ; + } + +return 0 ;
kernel/bpf/syscall.c static int bpf_obj_get(const union bpf_attr *attr) · line 873
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+} + +static int bpf_prog_detach (const union bpf_attr *attr) +{ +if (!capable (CAP_NET_ADMIN )) +return -EPERM ; + +/* Flags are unused for now */ +if (attr->attach_flags !=0 ) +return -EINVAL ;
if (CHECK_ATTR(BPF_PROG_DETACH)) return -EINVAL;
Quoted 44 lines
kernel/bpf/syscall.c +33
-0+switch (attr->attach_type) { +case BPF_ATTACH_TYPE_CGROUP_INET_INGRESS : +case BPF_ATTACH_TYPE_CGROUP_INET_EGRESS : { +struct cgroup *cgrp; + + cgrp =cgroup_get_from_fd (attr->target_fd); +if (IS_ERR (cgrp)) +return PTR_ERR (cgrp); + +cgroup_bpf_update (cgrp,NULL , attr->attach_type); +cgroup_put (cgrp); + +break ; + } + +default : +return -EINVAL ; + } + +return 0 ; +} +#endif /* CONFIG_CGROUP_BPF */ + SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) {union bpf_attr attr = {};@@ -888,6 +961,16 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, sizcase BPF_OBJ_GET : err =bpf_obj_get (&attr);break ; + +#ifdef CONFIG_CGROUP_BPF +case BPF_PROG_ATTACH : + err =bpf_prog_attach (&attr); +break ; +case BPF_PROG_DETACH : + err =bpf_prog_detach (&attr); +break ; +#endif +default : err = -EINVAL ;break ;
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Daniel MackQuoted 7 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
Quoted 8 lines
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
How does this work when running and orchestrator within an orchestrator? The Docker in Docker / Mesos in Mesos use case, where the top level orchestrator is observing the traffic, and there is an orchestrator within that also need to run it.
In this case, I'd like to run E's filter, then if it returns 0, D's, and B's, and so on. Is it possible to allow this, either by flattening out the datastructure (copy a ref to the bpf programs to C and E) or something similar?
Quoted 363 lines
Attaching and detaching programs will be done through the bpf(2) syscall. For now, ingress and egress inet socket filtering are the only supported use-cases.
Signed-off-by Daniel Mack <[email protected]>--- include/linux/bpf-cgroup.h | 70 +++++++++++++++++++ include/linux/cgroup-defs.h | 4 ++ init/Kconfig | 12 ++++ kernel/bpf/Makefile | 1 + kernel/bpf/cgroup.c | 165 ++++++++++++++++++++++++++++++++++++++++++++ kernel/cgroup.c | 18 +++++ 6 files changed, 270 insertions(+) create mode 100644 include/linux/bpf-cgroup.h create mode 100644 kernel/bpf/cgroup.cinclude/linux/bpf-cgroup.h new file +70
-0@@ -0,0 +1,70 @@ +#ifndef _BPF_CGROUP_H +#define _BPF_CGROUP_H + +#include <linux/bpf.h> +#include <uapi/linux/bpf.h> + +struct sock ; +struct cgroup ; +struct sk_buff ; + +#ifdef CONFIG_CGROUP_BPF + +extern struct static_key_false cgroup_bpf_enabled_key; +#define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) + +struct cgroup_bpf { +/* +* Store two sets of bpf_prog pointers, one for programs that are +* pinned directly to this cgroup, and one for those that are effective +* when this cgroup is accessed. +*/ +struct bpf_prog *prog[__MAX_BPF_ATTACH_TYPE]; +struct bpf_prog *effective[__MAX_BPF_ATTACH_TYPE]; +}; + +void cgroup_bpf_put (struct cgroup *cgrp); +void cgroup_bpf_inherit (struct cgroup *cgrp,struct cgroup *parent); + +void __cgroup_bpf_update (struct cgroup *cgrp, +struct cgroup *parent, +struct bpf_prog *prog, +enum bpf_attach_type type); + +/* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */ +void cgroup_bpf_update (struct cgroup *cgrp, +struct bpf_prog *prog, +enum bpf_attach_type type); + +int __cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type); + +/* Wrapper for __cgroup_bpf_run_filter() guarded by cgroup_bpf_enabled */ +static inline int cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type) +{ +if (cgroup_bpf_enabled) +return __cgroup_bpf_run_filter (sk, skb, type); + +return 0 ; +} + +#else + +struct cgroup_bpf {}; +static inline void cgroup_bpf_put (struct cgroup *cgrp) {} +static inline void cgroup_bpf_inherit (struct cgroup *cgrp, +struct cgroup *parent) {} + +static inline int cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type) +{ +return 0 ; +} + +#endif /* CONFIG_CGROUP_BPF */ + +#endif /* _BPF_CGROUP_H */ include/linux/cgroup-defs.h +4
-0@@ -16,6 +16,7 @@#include <linux/percpu-refcount.h> #include <linux/percpu-rwsem.h> #include <linux/workqueue.h> +#include <linux/bpf-cgroup.h> #ifdef CONFIG_CGROUPS @@ -300,6 +301,9 @@ struct cgroup {/* used to schedule release agent */ struct work_struct release_agent_work; +/* used to store eBPF programs */ +struct cgroup_bpf bpf; +/* ids of the ancestors at each level including self */ int ancestor_ids[]; };init/Kconfig +12
-0@@ -1144,6 +1144,18 @@ config CGROUP_PERF Say N if unsure. +config CGROUP_BPF + bool "Support for eBPF programs attached to cgroups" + depends on BPF_SYSCALL && SOCK_CGROUP_DATA + help + Allow attaching eBPF programs to a cgroup using the bpf(2) + syscall command BPF_PROG_ATTACH. + + In which context these programs are accessed depends on the type + of attachment. For instance, programs that are attached using + BPF_ATTACH_TYPE_CGROUP_INET_INGRESS will be executed on the + ingress path of inet sockets. + config CGROUP_DEBUG bool "Example controller" default nkernel/bpf/Makefile +1
-0@@ -5,3 +5,4 @@ obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o ifeq ($(CONFIG_PERF_EVENTS),y) obj-$(CONFIG_BPF_SYSCALL) += stackmap.o endif +obj-$(CONFIG_CGROUP_BPF) += cgroup.okernel/bpf/cgroup.c new file +165
-0@@ -0,0 +1,165 @@ +/* +* Functions to manage eBPF programs attached to cgroups +* +* Copyright (c) 2016 Daniel Mack +* +* This file is subject to the terms and conditions of version 2 of the GNU +* General Public License. See the file COPYING in the main directory of the +* Linux distribution for more details. +*/ + +#include <linux/kernel.h> +#include <linux/atomic.h> +#include <linux/cgroup.h> +#include <linux/slab.h> +#include <linux/bpf.h> +#include <linux/bpf-cgroup.h> +#include <net/sock.h> + +DEFINE_STATIC_KEY_FALSE (cgroup_bpf_enabled_key); +EXPORT_SYMBOL (cgroup_bpf_enabled_key); + +/** +* cgroup_bpf_put() - put references of all bpf programs +* @cgrp: the cgroup to modify +*/ +void cgroup_bpf_put (struct cgroup *cgrp) +{ +unsigned int type; + +for (type =0 ; type <ARRAY_SIZE (cgrp->bpf.prog); type++) { +struct bpf_prog *prog = cgrp->bpf.prog[type]; + +if (prog) { +bpf_prog_put (prog); +static_branch_dec (&cgroup_bpf_enabled_key); + } + } +} + +/** +* cgroup_bpf_inherit() - inherit effective programs from parent +* @cgrp: the cgroup to modify +* @parent: the parent to inherit from +*/ +void cgroup_bpf_inherit (struct cgroup *cgrp,struct cgroup *parent) +{ +unsigned int type; + +for (type =0 ; type <ARRAY_SIZE (cgrp->bpf.effective); type++) { +struct bpf_prog *e; + + e =rcu_dereference_protected (parent->bpf.effective[type], +lockdep_is_held (&cgroup_mutex)); +rcu_assign_pointer (cgrp->bpf.effective[type], e); + } +} + +/** +* __cgroup_bpf_update() - Update the pinned program of a cgroup, and +* propagate the change to descendants +* @cgrp: The cgroup which descendants to traverse +* @prog: A new program to pin +* @type: Type of pinning operation (ingress/egress) +* +* Each cgroup has a set of two pointers for bpf programs; one for eBPF +* programs it owns, and which is effective for execution. +* +* If @prog is %NULL, this function attaches a new program to the cgroup and +* releases the one that is currently attached, if any. @prog is then made +* the effective program of type @type in that cgroup. +* +* If @prog is %NULL, the currently attached program of type @type is released, +* and the effective program of the parent cgroup (if any) is inherited to +* @cgrp. +* +* Then, the descendants of @cgrp are walked and the effective program for +* each of them is set to the effective program of @cgrp unless the +* descendant has its own program attached, in which case the subbranch is +* skipped. This ensures that delegated subcgroups with own programs are left +* untouched. +* +* Must be called with cgroup_mutex held. +*/ +void __cgroup_bpf_update (struct cgroup *cgrp, +struct cgroup *parent, +struct bpf_prog *prog, +enum bpf_attach_type type) +{ +struct bpf_prog *old_prog, *effective; +struct cgroup_subsys_state *pos; + + old_prog =xchg (cgrp->bpf.prog + type, prog); + +if (prog) +static_branch_inc (&cgroup_bpf_enabled_key); + +if (old_prog) { +bpf_prog_put (old_prog); +static_branch_dec (&cgroup_bpf_enabled_key); + } + + effective = (!prog && parent) ? +rcu_dereference_protected (parent->bpf.effective[type], +lockdep_is_held (&cgroup_mutex)) : + prog; + +css_for_each_descendant_pre (pos, &cgrp->self) { + struct cgroup *desc = container_of(pos, struct cgroup, self); + +/* skip the subtree if the descendant has its own program */ +if (desc->bpf.prog[type] && desc != cgrp) + pos =css_rightmost_descendant (pos); +else +rcu_assign_pointer (desc->bpf.effective[type], + effective); + } +} + +/** +* __cgroup_bpf_run_filter() - Run a program for packet filtering +* @sk: The socken sending or receiving traffic +* @skb: The skb that is being sent or received +* @type: The type of program to be exectuted +* +* If no socket is passed, or the socket is not of type INET or INET6, +* this function does nothing and returns 0. +* +* The program type passed in via @type must be suitable for network +* filtering. No further check is performed to assert that. +* +* This function will return %-EPERM if any if an attached program was found +* and if it returned != 1 during execution. In all other cases, 0 is returned. +*/ +int __cgroup_bpf_run_filter (struct sock *sk, +struct sk_buff *skb, +enum bpf_attach_type type) +{ +struct bpf_prog *prog; +struct cgroup *cgrp; +int ret =0 ; + +if (!sk) +return 0 ; + +if (sk->sk_family !=AF_INET && + sk->sk_family !=AF_INET6 ) +return 0 ; + + cgrp =sock_cgroup_ptr (&sk->sk_cgrp_data); + +rcu_read_lock (); + + prog =rcu_dereference (cgrp->bpf.effective[type]); +if (prog) { +unsigned int offset = skb->data -skb_mac_header (skb); + +__skb_push (skb, offset); + ret =bpf_prog_run_clear_cb (prog, skb) ==1 ?0 : -EPERM ; +__skb_pull (skb, offset); + } + +rcu_read_unlock (); + +return ret; +}kernel/cgroup.c +18
-0@@ -5038,6 +5038,8 @@ static void css_release_work_fn(struct work_struct *work)if (cgrp->kn)RCU_INIT_POINTER (*(void __rcu __force **)&cgrp->kn->priv,NULL ); + +cgroup_bpf_put (cgrp); }mutex_unlock (&cgroup_mutex);@@ -5245,6 +5247,9 @@ static struct cgroup *cgroup_create(struct cgroup *parent)if (!cgroup_on_dfl (cgrp)) cgrp->subtree_control =cgroup_control (cgrp); +if (parent) +cgroup_bpf_inherit (cgrp, parent); +cgroup_propagate_control (cgrp);/* @cgrp doesn't have dir yet so the following will only create csses */ @@ -6417,6 +6422,19 @@ static __init int cgroup_namespaces_init(void) }subsys_initcall (cgroup_namespaces_init); +#ifdef CONFIG_CGROUP_BPF +void cgroup_bpf_update (struct cgroup *cgrp, +struct bpf_prog *prog, +enum bpf_attach_type type) +{ +struct cgroup *parent =cgroup_parent (cgrp); + +mutex_lock (&cgroup_mutex); +__cgroup_bpf_update (cgrp, parent, prog, type); +mutex_unlock (&cgroup_mutex); +} +#endif /* CONFIG_CGROUP_BPF */ +#ifdef CONFIG_CGROUP_DEBUG static struct cgroup_subsys_state *debug_css_alloc (struct cgroup_subsys_state *parent_css)Signature · 1 line
2.5.5
Daniel Borkmann
maintainerRe: [PATCH v3 4/6] net: filter: run cgroup eBPF ingress programs
In reply to Daniel MackQuoted 21 lines · Daniel Mack
If the cgroup associated with the receiving socket has an eBPF programs installed, run them from
sk_filter_trim_cap().eBPF programs used in this context are expected to either return 1 to let the packet pass, or != 1 to drop them. The programs have access to the full skb, including the MAC headers.
Note that
cgroup_bpf_run_filter()is stubbed out as static inline nop for !CONFIG_CGROUP_BPF, and is otherwise guarded by a static key if the feature is unused.Signed-off-by Daniel Mack <[email protected]>--- net/core/filter.c | 5 +++++ 1 file changed, 5 insertions(+)net/core/filter.c +0
-0
net/core/filter.c int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap) · line 82
Quoted 5 lines
net/core/filter.c +2
-0@@ -78,6 +78,11 @@ int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)if (skb_pfmemalloc (skb) && !sock_flag (sk,SOCK_MEMALLOC ))return -ENOMEM ; + err =cgroup_bpf_run_filter (sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_INGRESS );
Maybe just BPF_CGROUP_INET_{IN,E}GRESS (seems less cluttered, and we know these were set via bpf(2) as attach_type anyway)?
Quoted 7 lines
net/core/filter.c +3
-0+if (err) +return err; + err =security_sock_rcv_skb (sk, skb);if (err)return err;
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Alexei StarovoitovHi Alexei,
Quoted 22 lines · Alexei Starovoitov
Quoted 20 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
Attaching and detaching programs will be done through the bpf(2) syscall. For now, ingress and egress inet socket filtering are the only supported use-cases.
Signed-off-by Daniel Mack <[email protected]>...
Quoted 9 lines
kernel/bpf/cgroup.c line 111
Quoted 5 lines
kernel/bpf/cgroup.c new file +5
-0+css_for_each_descendant_pre (pos, &cgrp->self) { + struct cgroup *desc = container_of(pos, struct cgroup, self); + +/* skip the subtree if the descendant has its own program */ +if (desc->bpf.prog[type] && desc != cgrp)is desc != cgrp really needed?
I thoughtcss_for_each_descendant_pre()shouldn't walk itself or I'm missing how it works.
Hmm, no - that check is necessary in my tests, and also according to the documentation:
/**
* css_for_each_descendant_pre - pre-order walk of a css's descendants
* @pos: the css * to use as the loop cursor
* @root: css whose descendants to walk
*
* Walk @root's descendants. @root is included in the iteration and the
* first node to be visited. Must be called under rcu_read_lock(). *
Daniel
Re: [PATCH v3 1/6] bpf: add new prog type for cgroup socket filtering
In reply to Daniel BorkmannQuoted 40 lines · Daniel Borkmann
Quoted 19 lines · Daniel Mack
For now, this program type is equivalent to
BPF_PROG_TYPE_SOCKET_FILTERin terms of checks during the verification process. It may access the skb as well.Programs of this type will be attached to cgroups for network filtering and accounting.
Signed-off-by Daniel Mack <[email protected]>--- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/verifier.c | 1 + net/core/filter.c | 6 ++++++ 3 files changed, 14 insertions(+)include/uapi/linux/bpf.h +0
-0include/uapi/linux/bpf.h enum bpf_prog_type { · line 99
Quoted 5 lines
include/uapi/linux/bpf.h +2
-0@@ -95,6 +95,13 @@ enum bpf_prog_type {BPF_PROG_TYPE_SCHED_ACT ,BPF_PROG_TYPE_TRACEPOINT ,BPF_PROG_TYPE_XDP , + BPF_PROG_TYPE_CGROUP_SOCKET_FILTER, +};Nit: can we drop the
_FILTERsuffix? So just leaving it atBPF_PROG_TYPE_CGROUP_SOCKET. Some of these use cases might not always strictly be related to filtering, so seems cleaner to just leave it out everywhere.include/uapi/linux/bpf.h enum bpf_prog_type { · line 105
Quoted 6 lines
include/uapi/linux/bpf.h +5
-0+ +enum bpf_attach_type { +BPF_ATTACH_TYPE_CGROUP_INET_INGRESS , +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS , + __MAX_BPF_ATTACH_TYPE };#define
BPF_MAX_ATTACH_TYPE__BPF_MAX_ATTACH_TYPE
Quoted 2 lines
And then use that in your follow-up patches for declaring arrays, etc?
Agreed, will change.
Thanks,
Daniel
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Daniel BorkmannQuoted 50 lines · Daniel Borkmann
Quoted 20 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
Attaching and detaching programs will be done through the bpf(2) syscall. For now, ingress and egress inet socket filtering are the only supported use-cases.
Signed-off-by Daniel Mack <[email protected]>[...]
Quoted 28 lines
kernel/bpf/cgroup.c new file +28
-0+void __cgroup_bpf_update (struct cgroup *cgrp, +struct cgroup *parent, +struct bpf_prog *prog, +enum bpf_attach_type type) +{ +struct bpf_prog *old_prog, *effective; +struct cgroup_subsys_state *pos; + + old_prog =xchg (cgrp->bpf.prog + type, prog); + +if (prog) +static_branch_inc (&cgroup_bpf_enabled_key); + +if (old_prog) { +bpf_prog_put (old_prog); +static_branch_dec (&cgroup_bpf_enabled_key); + } + + effective = (!prog && parent) ? +rcu_dereference_protected (parent->bpf.effective[type], +lockdep_is_held (&cgroup_mutex)) : + prog; + +css_for_each_descendant_pre (pos, &cgrp->self) { + struct cgroup *desc = container_of(pos, struct cgroup, self); + +/* skip the subtree if the descendant has its own program */ +if (desc->bpf.prog[type] && desc != cgrp)
Quoted 8 lines
kernel/bpf/cgroup.c line 116
Quoted 5 lines
kernel/bpf/cgroup.c new file +5
-0+ pos =css_rightmost_descendant (pos); + else + rcu_assign_pointer(desc->bpf.effective[type], +effective ); + }Shouldn't the
old_progreference only be released right here at the end instead of above (otherwise this could race)?
Yes, that's right. Will change as well. Thanks for spotting!
Daniel
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel BorkmannQuoted 16 lines · Daniel Borkmann
include/uapi/linux/bpf.h union bpf_attr { · line 157
Quoted 11 lines · Daniel Mack
enum bpf_map_type {include/uapi/linux/bpf.h +6
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags;Could we just do ...
Quoted 5 lines
__u32dst_fd;__u32src_fd;__u32attach_type;... and leave flags out, since unused anyway? Also see below.
I'd really like to keep the flags, even if they're unused right now. This only adds 8 bytes during the syscall operation, so it doesn't harm. However, we cannot change the userspace API after the fact, and who knows what this (rather generic) interface will be used for later on.
Quoted 2 lines
#define
BPF_PROG_ATTACH_LAST_FIELDattach_type#defineBPF_PROG_DETACH_LAST_FIELDBPF_PROG_ATTACH_LAST_FIELD
...
Quoted 5 lines
Correct way would be to:
if (CHECK_ATTR(BPF_PROG_ATTACH)) return -EINVAL;
Will add - thanks!
Daniel
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Alexei StarovoitovQuoted 11 lines · Alexei Starovoitov
include/uapi/linux/bpf.h union bpf_attr { · line 158
Quoted 7 lines · Daniel Mack
include/uapi/linux/bpf.h +7
-0+ +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags; + };there is a 4 byte hole in this struct. Can we pack it differently?
Okay - I swapped "type" and "flags" to repair this.
Quoted 17 lines
Quoted 17 lines
kernel/bpf/syscall.c +17
-0+switch (attr->attach_type) { +case BPF_ATTACH_TYPE_CGROUP_INET_INGRESS : +case BPF_ATTACH_TYPE_CGROUP_INET_EGRESS : { +struct cgroup *cgrp; + + prog =bpf_prog_get_type (attr->attach_bpf_fd, +BPF_PROG_TYPE_CGROUP_SOCKET_FILTER ); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + cgrp =cgroup_get_from_fd (attr->target_fd); +if (IS_ERR (cgrp)) { +bpf_prog_put (prog); +return PTR_ERR (cgrp); + } + +cgroup_bpf_update (cgrp, prog, attr->attach_type);
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_obj_get(const union bpf_attr *attr) · line 857
Quoted 4 lines
kernel/bpf/syscall.c +4
-0+cgroup_put (cgrp); + +break ; + }this } formatting style is confusing. The above } looks like it matches 'switch () {'.
May be move 'struct cgroup *cgrp' to the top to avoid that?
I kept it local to its users, but you're right, it's not worth it. Will change.
Thanks,
Daniel
Daniel Borkmann
maintainerRe: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackQuoted 21 lines · Daniel Mack
Quoted 20 lines · Daniel Borkmann
include/uapi/linux/bpf.h union bpf_attr { · line 157
Quoted 11 lines · Daniel Mack
enum bpf_map_type {include/uapi/linux/bpf.h +6
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags;Could we just do ...
__u32dst_fd;__u32src_fd;__u32attach_type;
Quoted 6 lines
Quoted 1 line
... and leave flags out, since unused anyway? Also see below.
I'd really like to keep the flags, even if they're unused right now. This only adds 8 bytes during the syscall operation, so it doesn't harm. However, we cannot change the userspace API after the fact, and who knows what this (rather generic) interface will be used for later on.
With the below suggestion added, then flags doesn't need to be added currently as it can be done safely at a later point in time with respecting old binaries. See also the syscall handling code in kernel/bpf/syscall.c +825 and the CHECK_ATTR() macro. The underlying idea of this was taken from perf_event_open() syscall back then, see [1] for a summary.
[1] https://lkml.org/lkml/2014/8/26/116
Quoted 16 lines
Quoted 2 lines
#define
BPF_PROG_ATTACH_LAST_FIELDattach_type#defineBPF_PROG_DETACH_LAST_FIELDBPF_PROG_ATTACH_LAST_FIELD...
Quoted 5 lines
Correct way would be to:
if (CHECK_ATTR(BPF_PROG_ATTACH)) return -EINVAL;Will add - thanks!
Daniel
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
Quoted 29 lines · Daniel Borkmann
Quoted 21 lines · Daniel Mack
Quoted 20 lines · Daniel Borkmann
include/uapi/linux/bpf.h union bpf_attr { · line 157
Quoted 11 lines · Daniel Mack
enum bpf_map_type {include/uapi/linux/bpf.h +6
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags;Could we just do ...
__u32dst_fd;__u32src_fd;__u32attach_type;Quoted 6 lines
Quoted 1 line
... and leave flags out, since unused anyway? Also see below.
I'd really like to keep the flags, even if they're unused right now. This only adds 8 bytes during the syscall operation, so it doesn't harm. However, we cannot change the userspace API after the fact, and who knows what this (rather generic) interface will be used for later on.
Quoted 8 lines
With the below suggestion added, then flags doesn't need to be added currently as it can be done safely at a later point in time with respecting old binaries. See also the syscall handling code in
kernel/bpf/syscall.c+825 and theCHECK_ATTR()macro. The underlying idea of this was taken fromperf_event_open()syscall back then, see [1] for a summary.[1] https://lkml.org/lkml/2014/8/26/116
Yes, I know that's possible, and I like the idea, but I don't think any new interface should come without flags really, as flags are something that will most certainly be needed at some point anyway. I didn't have them in my first shot, but Alexei pointed out that they should be added, and I agree.
Also, this optimization wouldn't make the transported struct payload any smaller anyway, because the member of that union used by BPF_PROG_LOAD is still by far the biggest.
I really don't think it's worth sparing 8 bytes here and then do the binary compat dance after flags are added, for no real gain.
Thanks,
Daniel
Re: [PATCH v3 5/6] net: core: run cgroup eBPF egress programs
In reply to Daniel BorkmannQuoted 18 lines · Daniel Borkmann
Quoted 16 lines · Daniel Mack
net/core/dev.c +1
-0@@ -141,6 +141,7 @@#include <linux/netfilter_ingress.h> #include <linux/sctp.h> #include <linux/crash_dump.h> +#include <linux/bpf-cgroup.h> #include "net-sysfs.h" @@ -3329,6 +3330,11 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)if (unlikely (skb_shinfo (skb)->tx_flags &SKBTX_SCHED_TSTAMP ))__skb_tstamp_tx (skb,NULL , skb->sk,SCM_TSTAMP_SCHED );
Quoted 6 lines
net/core/dev.c static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv) · line 3336
Quoted 4 lines
net/core/dev.c +4
-0+ rc =cgroup_bpf_run_filter (skb->sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS ); +if (rc) +return rc;This would leak the whole skb by the way.
Ah, right.
Quoted 5 lines
Apart from that, could this be modeled w/o affecting the forwarding path (at some local output point where we know to have a valid socket)? Then you could also drop the !sk and
sk->sk_familytests, and we wouldn't need to replicate parts of what clsact is doing as well. Hmm, maybe access to src/dst mac could be handled to be just zeroes since not available at that point?
Hmm, I wonder where this hook could be put instead then. When placed in ip_output() and ip6_output(), the mac headers cannot be pushed before running the program, resulting in bogus skb data from the eBPF program.
Also, if I read the code correctly, ip[6]_output is not called for multicast packets.
Any other ideas?
Thanks,
Daniel
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Sargun DhillonHi,
Quoted 16 lines · Sargun Dhillon
Quoted 7 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
Quoted 8 lines
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
Quoted 7 lines
How does this work when running and orchestrator within an orchestrator? The Docker in Docker / Mesos in Mesos use case, where the top level orchestrator is observing the traffic, and there is an orchestrator within that also need to run it.
In this case, I'd like to run E's filter, then if it returns 0, D's, and B's, and so on.
Running multiple programs was an idea I had in one of my earlier drafts, but after some discussion, I refrained from it again because potentially walking the cgroup hierarchy on every packet is just too expensive.
Quoted 3 lines
Is it possible to allow this, either by flattening out the datastructure (copy a ref to the bpf programs to C and E) or something similar?
That would mean we carry a list of eBPF program pointers of dynamic size. IOW, the deeper inside the cgroup hierarchy, the bigger the list, so it can store a reference to all programs of all of its ancestor.
While I think that would be possible, even at some later point, I'd really like to avoid it for the sake of simplicity.
Is there any reason why this can't be done in userspace? Compile a program X for A, and overload it with Y, with Y doing the same than X but add some extra checks? Note that all users of the bpf(2) syscall API will need CAP_NET_ADMIN anyway, so there is no delegation to unprivileged sub-orchestators or anything alike really.
Thanks,
Daniel
RE: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackFrom: Daniel Mack
Quoted 11 lines
Quoted 9 lines
include/uapi/linux/bpf.h union bpf_attr { · line 158
Quoted 7 lines
include/uapi/linux/bpf.h +7
-0+ +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags; + };there is a 4 byte hole in this struct. Can we pack it differently?
Okay - I swapped "type" and "flags" to repair this.
That just moves the pad to the end of the structure. Still likely to cause a problem for 32bit apps on a 64bit kernel. If you can't think of any flags, why 64 of them?
David
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
Quoted 11 lines · David Laight
From: Daniel Mack
Quoted 10 lines
Quoted 9 lines
include/uapi/linux/bpf.h union bpf_attr { · line 158
Quoted 7 lines
include/uapi/linux/bpf.h +7
-0+ +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags; + };there is a 4 byte hole in this struct. Can we pack it differently?
Quoted 4 lines
Quoted 1 line
Okay - I swapped "type" and "flags" to repair this.
That just moves the pad to the end of the structure. Still likely to cause a problem for 32bit apps on a 64bit kernel.
What kind of problem do you have in mind? Again, this is embedded in a union of much bigger total size, and the API is not used in any kind of hot-path.
Quoted 1 line
If you can't think of any flags, why 64 of them?
I can't think of them right now, but this is about defining an API that can be used in other context as well. Also, it doesn't matter at all, they don't harm. IMO, it's just better to have them right away than to do a binary compat dance once someone needs them.
Thanks,
Daniel
Daniel Borkmann
maintainerRe: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackQuoted 45 lines · Daniel Mack
Quoted 29 lines · Daniel Borkmann
Quoted 21 lines · Daniel Mack
Quoted 20 lines · Daniel Borkmann
include/uapi/linux/bpf.h union bpf_attr { · line 157
Quoted 11 lines · Daniel Mack
enum bpf_map_type {include/uapi/linux/bpf.h +6
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */ +__u32 target_fd;/* container object to attach to */ +__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags;Could we just do ...
__u32dst_fd;__u32src_fd;__u32attach_type;Quoted 6 lines
Quoted 1 line
... and leave flags out, since unused anyway? Also see below.
I'd really like to keep the flags, even if they're unused right now. This only adds 8 bytes during the syscall operation, so it doesn't harm. However, we cannot change the userspace API after the fact, and who knows what this (rather generic) interface will be used for later on.
Quoted 8 lines
With the below suggestion added, then flags doesn't need to be added currently as it can be done safely at a later point in time with respecting old binaries. See also the syscall handling code in
kernel/bpf/syscall.c+825 and theCHECK_ATTR()macro. The underlying idea of this was taken fromperf_event_open()syscall back then, see [1] for a summary.[1] https://lkml.org/lkml/2014/8/26/116Yes, I know that's possible, and I like the idea, but I don't think any new interface should come without flags really, as flags are something that will most certainly be needed at some point anyway. I didn't have them in my first shot, but Alexei pointed out that they should be added, and I agree.
Quoted 6 lines
Also, this optimization wouldn't make the transported struct payload any smaller anyway, because the member of that union used by
BPF_PROG_LOADis still by far the biggest.I really don't think it's worth sparing 8 bytes here and then do the binary compat dance after flags are added, for no real gain.
Sure, but there's not much of a dance needed, see for example how map_flags were added some time ago. So, iff there's really no foreseeable use-case in sight and since we have this flexibility in place already, then I don't quite follow why it's needed, if there's zero pain to add it later on. I would understand it of course, if it cannot be handled later on anymore.
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel MackQuoted 1 line · Daniel Mack
On 08/27/2016 02:08 AM, Alexei Starovoitov wrote:
[]
Quoted 16 lines
Quoted 16 lines
kernel/bpf/syscall.c +16
-0+switch (attr->attach_type) { +case BPF_ATTACH_TYPE_CGROUP_INET_INGRESS : +case BPF_ATTACH_TYPE_CGROUP_INET_EGRESS : { +struct cgroup *cgrp; + + prog =bpf_prog_get_type (attr->attach_bpf_fd, + BPF_PROG_TYPE_CGROUP_SOCKET_FILTER); +if (IS_ERR (prog)) +return PTR_ERR (prog); + + cgrp =cgroup_get_from_fd (attr->target_fd); +if (IS_ERR (cgrp)) { +bpf_prog_put (prog); +return PTR_ERR (cgrp); + } +
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_obj_get(const union bpf_attr *attr) · line 857
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+cgroup_bpf_update (cgrp, prog, attr->attach_type); +cgroup_put (cgrp); + +break ; + }this } formatting style is confusing. The above } looks like it matches 'switch () {'.
May be move 'struct cgroup *cgrp' to the top to avoid that?
This style of case statements that declare local variables with an open brace after the case statement
switch (bar) {
[cases...]
case foo: {
local declarations;
code...
}
[cases...]
}is used quite frequently in the kernel.
I think it's fine as is.
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
In reply to Daniel BorkmannQuoted 56 lines · Daniel Borkmann
Quoted 48 lines · Daniel Mack
Quoted 32 lines · Daniel Borkmann
Quoted 23 lines · Daniel Mack
Quoted 22 lines · Daniel Borkmann
include/uapi/linux/bpf.h union bpf_attr { · line 157
Quoted 13 lines · Daniel Mack
enum bpf_map_type {include/uapi/linux/bpf.h +2
-0@@ -147,6 +149,13 @@ union bpf_attr {__aligned_u64 pathname;__u32 bpf_fd; }; + +struct {/* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
+__u32target_fd; /* container object to attach
to */include/uapi/linux/bpf.h +3
-0+__u32 attach_bpf_fd;/* eBPF program to attach */ +__u32 attach_type;/* BPF_ATTACH_TYPE_* */ +__u64 attach_flags;Could we just do ...
__u32dst_fd;__u32src_fd;__u32attach_type;Quoted 7 lines
Quoted 1 line
... and leave flags out, since unused anyway? Also see below.
I'd really like to keep the flags, even if they're unused right now. This only adds 8 bytes during the syscall operation, so it doesn't harm.
However, we cannot change the userspace API after the fact, and who knows what this (rather generic) interface will be used for later on.Quoted 8 lines
With the below suggestion added, then flags doesn't need to be added currently as it can be done safely at a later point in time with respecting old binaries. See also the syscall handling code in
kernel/bpf/syscall.c+825 and theCHECK_ATTR()macro. The underlying idea of this was taken fromperf_event_open()syscall back then, see [1] for a summary.[1] https://lkml.org/lkml/2014/8/26/116Yes, I know that's possible, and I like the idea, but I don't think any new interface should come without flags really, as flags are something that will most certainly be needed at some point anyway. I didn't have them in my first shot, but Alexei pointed out that they should be added, and I agree.
Quoted 6 lines
Also, this optimization wouldn't make the transported struct payload any smaller anyway, because the member of that union used by
BPF_PROG_LOADis still by far the biggest.I really don't think it's worth sparing 8 bytes here and then do the binary compat dance after flags are added, for no real gain.
Quoted 6 lines
Sure, but there's not much of a dance needed, see for example how
map_flagswere added some time ago. So, iff there's really no foreseeable use-case in sight and since we have this flexibility in place already, then I don't quite
follow why it's needed, if there's zero pain to add it later on. I would understand it of course, if it cannot be handled later on anymore.
I agree with Daniel B. Since flags are completely unused right now, there is no plan to use it for anything in the coming months and even worse they make annoying hole in the struct, let's not add them. We can safely do that later. CHECK_ATTR() allows us to do it easily. It's not like syscall where flags are must have, since we cannot add it later. Here it's done trivially.
Re: [PATCH v3 3/6] bpf: add BPF_PROG_ATTACH and BPF_PROG_DETACH commands
Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Daniel Borkmann
Quoted 2 lines · Daniel Mack
I really don't think it's worth sparing 8 bytes here and then do the binary compat dance after flags are added, for no real gain.
Sure, but there's not much of a dance needed, see for example how
map_flagswere added some time ago. So, iff there's really no foreseeable use-case in sight and since we have this flexibility in place already, then I don't quite
follow why it's needed, if there's zero pain to add it later on. I would understand it of course, if it cannot be handled later on anymore.
Quoted 6 lines
I agree with Daniel B. Since flags are completely unused right now, there is no plan to use it for anything in the coming months and even worse they make annoying hole in the struct, let's not add them. We can safely do that later.
CHECK_ATTR()allows us to do it easily. It's not like syscall where flags are must have, since we cannot add it later. Here it's done trivially.
Okay then. If you both agree, I won't interfere :)
Daniel
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
In reply to Daniel MackQuoted 24 lines · Daniel Mack
Hi,
Quoted 21 lines · Sargun Dhillon
Quoted 7 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
Quoted 8 lines
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
How does this work when running and orchestrator within an orchestrator? The Docker in Docker / Mesos in Mesos use case, where the top level orchestrator is observing the traffic, and there is an orchestrator within that also need to run it.
Quoted 7 lines
Quoted 2 lines
In this case, I'd like to run E's filter, then if it returns 0, D's, and B's, and so on.
Running multiple programs was an idea I had in one of my earlier drafts, but after some discussion, I refrained from it again because potentially walking the cgroup hierarchy on every packet is just too expensive.
I think you're correct here. Maybe this is something I do with the LSM-attached filters, and not for skb filters. Do you think there might be a way to opt-in to this option?
Quoted 8 lines
Quoted 3 lines
Is it possible to allow this, either by flattening out the datastructure (copy a ref to the bpf programs to C and E) or something similar?
That would mean we carry a list of eBPF program pointers of dynamic size. IOW, the deeper inside the cgroup hierarchy, the bigger the list, so it can store a reference to all programs of all of its ancestor.
Quoted 8 lines
While I think that would be possible, even at some later point, I'd really like to avoid it for the sake of simplicity.
Is there any reason why this can't be done in userspace? Compile a program X for A, and overload it with Y, with Y doing the same than X but add some extra checks? Note that all users of the bpf(2) syscall API will need
CAP_NET_ADMINanyway, so there is no delegation to unprivileged sub-orchestators or anything alike really.
One of the use-cases that's becoming more and more common are containers-in-containers. In this, you have a privileged container that's running something like build orchestration, and you want to do macro-isolation (say limit access to only that tennant's infrastructure). Then, when the build orchestrator runs a build, it may want to monitor, and further isolate the tasks that run in the build job. This is a side-effect of composing different container technologies. Typically you use one system for images, then another for orchestration, and the actual program running inside of it can also leverage containerization.
Example:K8s->Docker->Jenkins Agent->Jenkins Build Job
There's also a differentiation of ownership in each of these systems. I would really not require a middleware system that all my software has to talk to, because sometimes I'm taking off the shelf software (Jenkins), and porting it to containers. I think one of the pieces that's led to the success of cgroups is the straightforward API, and ease of use (and it's getting even easier in v2).
It's perfectly fine to give the lower level tasks CAP_NET_ADMIN, because we use something like seccomp-bpf plus some of the work I've been doing with the LSM to prevent the sub-orchestrators from accidentally blowing away the system. Usually, we trust these orchestrators (internal users), so it's more of a precautionary measure as opposed to a true security measure.
Also, rewriting BPF programs, although pretty straightforward sounds like a pain to do in userspace, even with a helper. If we were to take peoples programs and chain them together via tail call, or similar, I can imagine where rewriting a program might push you over the instruction limit.
Quoted 5 lines
Thanks,
Daniel
Re: [PATCH v3 2/6] cgroup: add support for eBPF programs
Quoted 63 lines · Sargun Dhillon
Quoted 24 lines · Daniel Mack
Hi,
Quoted 21 lines · Sargun Dhillon
Quoted 7 lines · Daniel Mack
This patch adds two sets of eBPF program pointers to struct cgroup. One for such that are directly pinned to a cgroup, and one for such that are effective for it.
To illustrate the logic behind that, assume the following example cgroup hierarchy.
Quoted 8 lines
A - B - C
\ D - EIf only B has a program attached, it will be effective for B, C, D and E. If D then attaches a program itself, that will be effective for both D and E, and the program in B will only affect B and C. Only one program of a given type is effective for a cgroup.
How does this work when running and orchestrator within an orchestrator? The Docker in Docker / Mesos in Mesos use case, where the top level orchestrator is observing the traffic, and there is an orchestrator within that also need to run it.
Quoted 7 lines
Quoted 2 lines
In this case, I'd like to run E's filter, then if it returns 0, D's, and B's, and so on.
Running multiple programs was an idea I had in one of my earlier drafts, but after some discussion, I refrained from it again because potentially walking the cgroup hierarchy on every packet is just too expensive.
I think you're correct here. Maybe this is something I do with the LSM-attached filters, and not for skb filters. Do you think there might be a way to opt-in to this option?
Quoted 8 lines
Quoted 3 lines
Is it possible to allow this, either by flattening out the datastructure (copy a ref to the bpf programs to C and E) or something similar?
That would mean we carry a list of eBPF program pointers of dynamic size. IOW, the deeper inside the cgroup hierarchy, the bigger the list, so it can store a reference to all programs of all of its ancestor.
Quoted 8 lines
While I think that would be possible, even at some later point, I'd really like to avoid it for the sake of simplicity.
Is there any reason why this can't be done in userspace? Compile a program X for A, and overload it with Y, with Y doing the same than X but add some extra checks? Note that all users of the bpf(2) syscall API will need
CAP_NET_ADMINanyway, so there is no delegation to unprivileged sub-orchestators or anything alike really.One of the use-cases that's becoming more and more common are containers-in-containers. In this, you have a privileged container that's running something like build orchestration, and you want to do macro-isolation (say limit access to only that tennant's infrastructure). Then, when the build orchestrator runs a build, it may want to monitor, and further isolate the tasks that run in the build job. This is a side-effect of composing different container technologies. Typically you use one system for images, then another for orchestration, and the actual program running inside of it can also leverage containerization.
Quoted 2 lines
Example:
K8s->Docker->JenkinsAgent->JenkinsBuild Job
frankly I don't buy this argument, since above and other 'examples' of container-in-container look fake to me. There is a ton work to be done for such scheme to be even remotely feasible. The cgroup+bpf stuff would be the last on my list to 'fix' for such deployments. I don't think we should worry about it at present.
Daniel Borkmann
maintainerRe: [PATCH v3 5/6] net: core: run cgroup eBPF egress programs
In reply to Daniel MackQuoted 34 lines · Daniel Mack
Quoted 18 lines · Daniel Borkmann
Quoted 16 lines · Daniel Mack
net/core/dev.c +1
-0@@ -141,6 +141,7 @@#include <linux/netfilter_ingress.h> #include <linux/sctp.h> #include <linux/crash_dump.h> +#include <linux/bpf-cgroup.h> #include "net-sysfs.h" @@ -3329,6 +3330,11 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)if (unlikely (skb_shinfo (skb)->tx_flags &SKBTX_SCHED_TSTAMP ))__skb_tstamp_tx (skb,NULL , skb->sk,SCM_TSTAMP_SCHED );Quoted 6 lines
net/core/dev.c static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv) · line 3336
Quoted 4 lines
net/core/dev.c +4
-0+ rc =cgroup_bpf_run_filter (skb->sk, skb, +BPF_ATTACH_TYPE_CGROUP_INET_EGRESS ); +if (rc) +return rc;This would leak the whole skb by the way.
Ah, right.
Quoted 5 lines
Apart from that, could this be modeled w/o affecting the forwarding path (at some local output point where we know to have a valid socket)? Then you could also drop the !sk and
sk->sk_familytests, and we wouldn't need to replicate parts of what clsact is doing as well. Hmm, maybe access to src/dst mac could be handled to be just zeroes since not available at that point?
Quoted 3 lines
Hmm, I wonder where this hook could be put instead then. When placed in
ip_output()andip6_output(), the mac headers cannot be pushed before running the program, resulting in bogus skb data from the eBPF program.
But as it stands right now, RX will only see a subset of packets in sk_filter() layer (depending on where it's called in the proto handler implementation, so might not even include all control messages, for example) as opposed to the TX hook going that far even 'seeing' everything incl. forwarded packets in the sense that we know a priori that these kind of skbs going through the cgroup_bpf_run_filter() handler when the hook is enabled will just skip this hook eventually anyway. What about letting such progs see /only/ local skbs for RX and TX, with skb->data from L3 onwards (iirc, that would be similar to what current sk_filter() programs see)?