xref: /linux/tools/testing/selftests/bpf/progs/test_sockmap_change_tail.c (revision 22c55fb9eb92395d999b8404d73e58540d11bdd8)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 ByteDance */
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5 
6 #ifndef PAGE_SIZE
7 #define PAGE_SIZE __PAGE_SIZE
8 #endif
9 #define BPF_SKB_MAX_LEN (PAGE_SIZE << 2)
10 
11 struct {
12 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
13 	__uint(max_entries, 1);
14 	__type(key, int);
15 	__type(value, int);
16 } sock_map_rx SEC(".maps");
17 
18 long change_tail_ret = 1;
19 
20 SEC("sk_skb")
21 int prog_skb_verdict(struct __sk_buff *skb)
22 {
23 	char *data, *data_end;
24 
25 	bpf_skb_pull_data(skb, 1);
26 	data = (char *)(unsigned long)skb->data;
27 	data_end = (char *)(unsigned long)skb->data_end;
28 
29 	if (data + 1 > data_end)
30 		return SK_PASS;
31 
32 	if (data[0] == 'T') { /* Trim the packet */
33 		change_tail_ret = bpf_skb_change_tail(skb, skb->len - 1, 0);
34 		return SK_PASS;
35 	} else if (data[0] == 'G') { /* Grow the packet */
36 		change_tail_ret = bpf_skb_change_tail(skb, skb->len + 1, 0);
37 		return SK_PASS;
38 	} else if (data[0] == 'E') { /* Error */
39 		change_tail_ret = bpf_skb_change_tail(skb, BPF_SKB_MAX_LEN, 0);
40 		return SK_PASS;
41 	}
42 	return SK_PASS;
43 }
44 
45 char _license[] SEC("license") = "GPL";
46