xref: /linux/tools/testing/selftests/bpf/progs/test_pkt_md_access.c (revision 56fb34d86e875dbb0d3e6a81c5d3d035db373031)
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_helpers.h"
9 
10 int _version SEC("version") = 1;
11 
12 #if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
13 #define TEST_FIELD(TYPE, FIELD, MASK)					\
14 	{								\
15 		TYPE tmp = *(volatile TYPE *)&skb->FIELD;		\
16 		if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK))	\
17 			return TC_ACT_SHOT;				\
18 	}
19 #else
20 #define TEST_FIELD_OFFSET(a, b)	((sizeof(a) - sizeof(b)) / sizeof(b))
21 #define TEST_FIELD(TYPE, FIELD, MASK)					\
22 	{								\
23 		TYPE tmp = *((volatile TYPE *)&skb->FIELD +		\
24 			      TEST_FIELD_OFFSET(skb->FIELD, TYPE));	\
25 		if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK))	\
26 			return TC_ACT_SHOT;				\
27 	}
28 #endif
29 
30 SEC("test1")
31 int process(struct __sk_buff *skb)
32 {
33 	TEST_FIELD(__u8,  len, 0xFF);
34 	TEST_FIELD(__u16, len, 0xFFFF);
35 	TEST_FIELD(__u32, len, 0xFFFFFFFF);
36 	TEST_FIELD(__u16, protocol, 0xFFFF);
37 	TEST_FIELD(__u32, protocol, 0xFFFFFFFF);
38 	TEST_FIELD(__u8,  hash, 0xFF);
39 	TEST_FIELD(__u16, hash, 0xFFFF);
40 	TEST_FIELD(__u32, hash, 0xFFFFFFFF);
41 
42 	return TC_ACT_OK;
43 }
44