-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bpf-ancestor-cgroup-id'
Andrey Ignatov says: ==================== This patch set adds new BPF helper bpf_skb_ancestor_cgroup_id that returns id of cgroup v2 that is ancestor of cgroup associated with the skb at the ancestor_level. The helper is useful to implement policies in TC based on cgroups that are upper in hierarchy than immediate cgroup associated with skb. v1->v2: - more reliable check for testing IPv6 to become ready in selftest. ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
- Loading branch information
Showing
9 changed files
with
404 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/bin/sh | ||
# SPDX-License-Identifier: GPL-2.0 | ||
# Copyright (c) 2018 Facebook | ||
|
||
set -eu | ||
|
||
wait_for_ip() | ||
{ | ||
local _i | ||
echo -n "Wait for testing link-local IP to become available " | ||
for _i in $(seq ${MAX_PING_TRIES}); do | ||
echo -n "." | ||
if ping -6 -q -c 1 -W 1 ff02::1%${TEST_IF} >/dev/null 2>&1; then | ||
echo " OK" | ||
return | ||
fi | ||
sleep 1 | ||
done | ||
echo 1>&2 "ERROR: Timeout waiting for test IP to become available." | ||
exit 1 | ||
} | ||
|
||
setup() | ||
{ | ||
# Create testing interfaces not to interfere with current environment. | ||
ip link add dev ${TEST_IF} type veth peer name ${TEST_IF_PEER} | ||
ip link set ${TEST_IF} up | ||
ip link set ${TEST_IF_PEER} up | ||
|
||
wait_for_ip | ||
|
||
tc qdisc add dev ${TEST_IF} clsact | ||
tc filter add dev ${TEST_IF} egress bpf obj ${BPF_PROG_OBJ} \ | ||
sec ${BPF_PROG_SECTION} da | ||
|
||
BPF_PROG_ID=$(tc filter show dev ${TEST_IF} egress | \ | ||
awk '/ id / {sub(/.* id /, "", $0); print($1)}') | ||
} | ||
|
||
cleanup() | ||
{ | ||
ip link del ${TEST_IF} 2>/dev/null || : | ||
ip link del ${TEST_IF_PEER} 2>/dev/null || : | ||
} | ||
|
||
main() | ||
{ | ||
trap cleanup EXIT 2 3 6 15 | ||
setup | ||
${PROG} ${TEST_IF} ${BPF_PROG_ID} | ||
} | ||
|
||
DIR=$(dirname $0) | ||
TEST_IF="test_cgid_1" | ||
TEST_IF_PEER="test_cgid_2" | ||
MAX_PING_TRIES=5 | ||
BPF_PROG_OBJ="${DIR}/test_skb_cgroup_id_kern.o" | ||
BPF_PROG_SECTION="cgroup_id_logger" | ||
BPF_PROG_ID=0 | ||
PROG="${DIR}/test_skb_cgroup_id_user" | ||
|
||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2018 Facebook | ||
|
||
#include <linux/bpf.h> | ||
#include <linux/pkt_cls.h> | ||
|
||
#include <string.h> | ||
|
||
#include "bpf_helpers.h" | ||
|
||
#define NUM_CGROUP_LEVELS 4 | ||
|
||
struct bpf_map_def SEC("maps") cgroup_ids = { | ||
.type = BPF_MAP_TYPE_ARRAY, | ||
.key_size = sizeof(__u32), | ||
.value_size = sizeof(__u64), | ||
.max_entries = NUM_CGROUP_LEVELS, | ||
}; | ||
|
||
static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level) | ||
{ | ||
__u64 id; | ||
|
||
/* [1] &level passed to external function that may change it, it's | ||
* incompatible with loop unroll. | ||
*/ | ||
id = bpf_skb_ancestor_cgroup_id(skb, level); | ||
bpf_map_update_elem(&cgroup_ids, &level, &id, 0); | ||
} | ||
|
||
SEC("cgroup_id_logger") | ||
int log_cgroup_id(struct __sk_buff *skb) | ||
{ | ||
/* Loop unroll can't be used here due to [1]. Unrolling manually. | ||
* Number of calls should be in sync with NUM_CGROUP_LEVELS. | ||
*/ | ||
log_nth_level(skb, 0); | ||
log_nth_level(skb, 1); | ||
log_nth_level(skb, 2); | ||
log_nth_level(skb, 3); | ||
|
||
return TC_ACT_OK; | ||
} | ||
|
||
int _version SEC("version") = 1; | ||
|
||
char _license[] SEC("license") = "GPL"; |
Oops, something went wrong.