Skip to content

Commit 5a52ae4

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Allow to retrieve cgroup v1 classid from v2 hooks
Today, Kubernetes is still operating on cgroups v1, however, it is possible to retrieve the task's classid based on 'current' out of connect(), sendmsg(), recvmsg() and bind-related hooks for orchestrators which attach to the root cgroup v2 hook in a mixed env like in case of Cilium, for example, in order to then correlate certain pod traffic and use it as part of the key for BPF map lookups. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/555e1c69db7376c0947007b4951c260e1074efc3.1585323121.git.daniel@iogearbox.net
1 parent f318903 commit 5a52ae4

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎include/net/cls_cgroup.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ static inline void sock_update_classid(struct sock_cgroup_data *skcd)
4545
sock_cgroup_set_classid(skcd, classid);
4646
}
4747

48+
static inline u32 __task_get_classid(struct task_struct *task)
49+
{
50+
return task_cls_state(task)->classid;
51+
}
52+
4853
static inline u32 task_get_classid(const struct sk_buff *skb)
4954
{
50-
u32 classid = task_cls_state(current)->classid;
55+
u32 classid = __task_get_classid(current);
5156

5257
/* Due to the nature of the classifier it is required to ignore all
5358
* packets originating from softirq context as accessing `current'

‎net/core/filter.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,19 @@ static const struct bpf_func_proto bpf_msg_pop_data_proto = {
26422642
.arg4_type = ARG_ANYTHING,
26432643
};
26442644

2645+
#ifdef CONFIG_CGROUP_NET_CLASSID
2646+
BPF_CALL_0(bpf_get_cgroup_classid_curr)
2647+
{
2648+
return __task_get_classid(current);
2649+
}
2650+
2651+
static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
2652+
.func = bpf_get_cgroup_classid_curr,
2653+
.gpl_only = false,
2654+
.ret_type = RET_INTEGER,
2655+
};
2656+
#endif
2657+
26452658
BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
26462659
{
26472660
return task_get_classid(skb);
@@ -6005,6 +6018,10 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
60056018
return &bpf_get_netns_cookie_sock_proto;
60066019
case BPF_FUNC_perf_event_output:
60076020
return &bpf_event_output_data_proto;
6021+
#ifdef CONFIG_CGROUP_NET_CLASSID
6022+
case BPF_FUNC_get_cgroup_classid:
6023+
return &bpf_get_cgroup_classid_curr_proto;
6024+
#endif
60086025
default:
60096026
return bpf_base_func_proto(func_id);
60106027
}
@@ -6035,6 +6052,10 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
60356052
return &bpf_get_local_storage_proto;
60366053
case BPF_FUNC_perf_event_output:
60376054
return &bpf_event_output_data_proto;
6055+
#ifdef CONFIG_CGROUP_NET_CLASSID
6056+
case BPF_FUNC_get_cgroup_classid:
6057+
return &bpf_get_cgroup_classid_curr_proto;
6058+
#endif
60386059
#ifdef CONFIG_INET
60396060
case BPF_FUNC_sk_lookup_tcp:
60406061
return &bpf_sock_addr_sk_lookup_tcp_proto;

0 commit comments

Comments
 (0)