xref: /linux/tools/testing/selftests/bpf/progs/test_pkt_md_access.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include <stddef.h>
5 #include <string.h>
6 #include <linux/bpf.h>
7 #include <linux/pkt_cls.h>
8 #include <bpf/bpf_helpers.h>
9 
10 #if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
11 #define TEST_FIELD(TYPE, FIELD, MASK)					\
12 	{								\
13 		TYPE tmp = *(volatile TYPE *)&skb->FIELD;		\
14 		if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK))	\
15 			return TC_ACT_SHOT;				\
16 	}
17 #else
18 #define TEST_FIELD_OFFSET(a, b)	((sizeof(a) - sizeof(b)) / sizeof(b))
19 #define TEST_FIELD(TYPE, FIELD, MASK)					\
20 	{								\
21 		TYPE tmp = *((volatile TYPE *)&skb->FIELD +		\
22 			      TEST_FIELD_OFFSET(skb->FIELD, TYPE));	\
23 		if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK))	\
24 			return TC_ACT_SHOT;				\
25 	}
26 #endif
27 
28 SEC("tc")
29 int test_pkt_md_access(struct __sk_buff *skb)
30 {
31 	TEST_FIELD(__u8,  len, 0xFF);
32 	TEST_FIELD(__u16, len, 0xFFFF);
33 	TEST_FIELD(__u32, len, 0xFFFFFFFF);
34 	TEST_FIELD(__u16, protocol, 0xFFFF);
35 	TEST_FIELD(__u32, protocol, 0xFFFFFFFF);
36 	TEST_FIELD(__u8,  hash, 0xFF);
37 	TEST_FIELD(__u16, hash, 0xFFFF);
38 	TEST_FIELD(__u32, hash, 0xFFFFFFFF);
39 
40 	return TC_ACT_OK;
41 }
42