-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add a BPF selftest for bpf_skb_change_tail()
As requested by Daniel, we need to add a selftest to cover bpf_skb_change_tail() cases in skb_verdict. Here we test trimming, growing and error cases, and validate its expected return values. Cc: John Fastabend <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Zijian Zhang <[email protected]> Signed-off-by: Cong Wang <[email protected]>
- Loading branch information
Cong Wang
authored and
Kernel Patches Daemon
committed
Nov 26, 2024
1 parent
d840e7e
commit bbfea27
Showing
2 changed files
with
91 additions
and
0 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
40 changes: 40 additions & 0 deletions
40
tools/testing/selftests/bpf/progs/test_sockmap_change_tail.c
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,40 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2024 ByteDance */ | ||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_SOCKMAP); | ||
__uint(max_entries, 1); | ||
__type(key, int); | ||
__type(value, int); | ||
} sock_map_rx SEC(".maps"); | ||
|
||
long change_tail_ret = 1; | ||
|
||
SEC("sk_skb") | ||
int prog_skb_verdict(struct __sk_buff *skb) | ||
{ | ||
char *data, *data_end; | ||
|
||
bpf_skb_pull_data(skb, 1); | ||
data = (char *)(unsigned long)skb->data; | ||
data_end = (char *)(unsigned long)skb->data_end; | ||
|
||
if (data + 1 > data_end) | ||
return SK_PASS; | ||
|
||
if (data[0] == 'T') { /* Trim the packet */ | ||
change_tail_ret = bpf_skb_change_tail(skb, skb->len - 1, 0); | ||
return SK_PASS; | ||
} else if (data[0] == 'G') { /* Grow the packet */ | ||
change_tail_ret = bpf_skb_change_tail(skb, skb->len + 1, 0); | ||
return SK_PASS; | ||
} else if (data[0] == 'E') { /* Error */ | ||
change_tail_ret = bpf_skb_change_tail(skb, 65535, 0); | ||
return SK_PASS; | ||
} | ||
return SK_PASS; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |