Skip to content

Commit b426ce8

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add classid helper only based on skb->sk
Similarly to 5a52ae4 ("bpf: Allow to retrieve cgroup v1 classid from v2 hooks"), add a helper to retrieve cgroup v1 classid solely based on the skb->sk, so it can be used as key as part of BPF map lookups out of tc from host ns, in particular given the skb->sk is retained these days when crossing net ns thanks to 9c4c325 ("skbuff: preserve sock reference when scrubbing the skb."). This is similar to bpf_skb_cgroup_id() which implements the same for v2. Kubernetes ecosystem is still operating on v1 however, hence net_cls needs to be used there until this can be dropped in with the v2 helper of bpf_skb_cgroup_id(). Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/ed633cf27a1c620e901c5aa99ebdefb028dce600.1601477936.git.daniel@iogearbox.net
1 parent 963ec27 commit b426ce8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,6 +3643,15 @@ union bpf_attr {
36433643
* *flags* are identical to those used for bpf_snprintf_btf.
36443644
* Return
36453645
* 0 on success or a negative error in case of failure.
3646+
*
3647+
* u64 bpf_skb_cgroup_classid(struct sk_buff *skb)
3648+
* Description
3649+
* See **bpf_get_cgroup_classid**\ () for the main description.
3650+
* This helper differs from **bpf_get_cgroup_classid**\ () in that
3651+
* the cgroup v1 net_cls class is retrieved only from the *skb*'s
3652+
* associated socket instead of the current process.
3653+
* Return
3654+
* The id is returned or 0 in case the id could not be retrieved.
36463655
*/
36473656
#define __BPF_FUNC_MAPPER(FN) \
36483657
FN(unspec), \
@@ -3796,6 +3805,7 @@ union bpf_attr {
37963805
FN(copy_from_user), \
37973806
FN(snprintf_btf), \
37983807
FN(seq_printf_btf), \
3808+
FN(skb_cgroup_classid), \
37993809
/* */
38003810

38013811
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

net/core/filter.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,23 @@ static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
27072707
.gpl_only = false,
27082708
.ret_type = RET_INTEGER,
27092709
};
2710+
2711+
BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
2712+
{
2713+
struct sock *sk = skb_to_full_sk(skb);
2714+
2715+
if (!sk || !sk_fullsock(sk))
2716+
return 0;
2717+
2718+
return sock_cgroup_classid(&sk->sk_cgrp_data);
2719+
}
2720+
2721+
static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
2722+
.func = bpf_skb_cgroup_classid,
2723+
.gpl_only = false,
2724+
.ret_type = RET_INTEGER,
2725+
.arg1_type = ARG_PTR_TO_CTX,
2726+
};
27102727
#endif
27112728

27122729
BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
@@ -6772,6 +6789,10 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
67726789
case BPF_FUNC_skb_get_xfrm_state:
67736790
return &bpf_skb_get_xfrm_state_proto;
67746791
#endif
6792+
#ifdef CONFIG_CGROUP_NET_CLASSID
6793+
case BPF_FUNC_skb_cgroup_classid:
6794+
return &bpf_skb_cgroup_classid_proto;
6795+
#endif
67756796
#ifdef CONFIG_SOCK_CGROUP_DATA
67766797
case BPF_FUNC_skb_cgroup_id:
67776798
return &bpf_skb_cgroup_id_proto;

tools/include/uapi/linux/bpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,6 +3643,15 @@ union bpf_attr {
36433643
* *flags* are identical to those used for bpf_snprintf_btf.
36443644
* Return
36453645
* 0 on success or a negative error in case of failure.
3646+
*
3647+
* u64 bpf_skb_cgroup_classid(struct sk_buff *skb)
3648+
* Description
3649+
* See **bpf_get_cgroup_classid**\ () for the main description.
3650+
* This helper differs from **bpf_get_cgroup_classid**\ () in that
3651+
* the cgroup v1 net_cls class is retrieved only from the *skb*'s
3652+
* associated socket instead of the current process.
3653+
* Return
3654+
* The id is returned or 0 in case the id could not be retrieved.
36463655
*/
36473656
#define __BPF_FUNC_MAPPER(FN) \
36483657
FN(unspec), \
@@ -3796,6 +3805,7 @@ union bpf_attr {
37963805
FN(copy_from_user), \
37973806
FN(snprintf_btf), \
37983807
FN(seq_printf_btf), \
3808+
FN(skb_cgroup_classid), \
37993809
/* */
38003810

38013811
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)