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