1b915ebe6SJoe Stringer // SPDX-License-Identifier: GPL-2.0
2b915ebe6SJoe Stringer // Copyright (c) 2019 Isovalent, Inc.
3b915ebe6SJoe Stringer
4b915ebe6SJoe Stringer #include <linux/bpf.h>
5b915ebe6SJoe Stringer #include <linux/pkt_cls.h>
6b915ebe6SJoe Stringer #include <string.h>
7b915ebe6SJoe Stringer
83e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
9b915ebe6SJoe Stringer
10df0b7792SAndrii Nakryiko struct {
11bc7430ccSAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
12bc7430ccSAndrii Nakryiko __uint(max_entries, 11);
13bc7430ccSAndrii Nakryiko __type(key, __u32);
14bc7430ccSAndrii Nakryiko __type(value, __u64);
15bc7430ccSAndrii Nakryiko } result_number SEC(".maps");
16b915ebe6SJoe Stringer
17df0b7792SAndrii Nakryiko struct {
18bc7430ccSAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
19bc7430ccSAndrii Nakryiko __uint(max_entries, 5);
20bc7430ccSAndrii Nakryiko __type(key, __u32);
21df0b7792SAndrii Nakryiko const char (*value)[32];
22bc7430ccSAndrii Nakryiko } result_string SEC(".maps");
23b915ebe6SJoe Stringer
24b915ebe6SJoe Stringer struct foo {
25b915ebe6SJoe Stringer __u8 a;
26b915ebe6SJoe Stringer __u32 b;
27b915ebe6SJoe Stringer __u64 c;
28b915ebe6SJoe Stringer };
29b915ebe6SJoe Stringer
30df0b7792SAndrii Nakryiko struct {
31bc7430ccSAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
32bc7430ccSAndrii Nakryiko __uint(max_entries, 5);
33bc7430ccSAndrii Nakryiko __type(key, __u32);
34bc7430ccSAndrii Nakryiko __type(value, struct foo);
35bc7430ccSAndrii Nakryiko } result_struct SEC(".maps");
36b915ebe6SJoe Stringer
37b915ebe6SJoe Stringer /* Relocation tests for __u64s. */
38b915ebe6SJoe Stringer static __u64 num0;
39b915ebe6SJoe Stringer static __u64 num1 = 42;
40b915ebe6SJoe Stringer static const __u64 num2 = 24;
41b915ebe6SJoe Stringer static __u64 num3 = 0;
42b915ebe6SJoe Stringer static __u64 num4 = 0xffeeff;
43b915ebe6SJoe Stringer static const __u64 num5 = 0xabab;
44b915ebe6SJoe Stringer static const __u64 num6 = 0xab;
45b915ebe6SJoe Stringer
46b915ebe6SJoe Stringer /* Relocation tests for strings. */
47b915ebe6SJoe Stringer static const char str0[32] = "abcdefghijklmnopqrstuvwxyz";
48b915ebe6SJoe Stringer static char str1[32] = "abcdefghijklmnopqrstuvwxyz";
49b915ebe6SJoe Stringer static char str2[32];
50b915ebe6SJoe Stringer
51b915ebe6SJoe Stringer /* Relocation tests for structs. */
52b915ebe6SJoe Stringer static const struct foo struct0 = {
53b915ebe6SJoe Stringer .a = 42,
54b915ebe6SJoe Stringer .b = 0xfefeefef,
55b915ebe6SJoe Stringer .c = 0x1111111111111111ULL,
56b915ebe6SJoe Stringer };
57b915ebe6SJoe Stringer static struct foo struct1;
58b915ebe6SJoe Stringer static const struct foo struct2;
59b915ebe6SJoe Stringer static struct foo struct3 = {
60b915ebe6SJoe Stringer .a = 41,
61b915ebe6SJoe Stringer .b = 0xeeeeefef,
62b915ebe6SJoe Stringer .c = 0x2111111111111111ULL,
63b915ebe6SJoe Stringer };
64b915ebe6SJoe Stringer
65b915ebe6SJoe Stringer #define test_reloc(map, num, var) \
66b915ebe6SJoe Stringer do { \
67b915ebe6SJoe Stringer __u32 key = num; \
68b915ebe6SJoe Stringer bpf_map_update_elem(&result_##map, &key, var, 0); \
69b915ebe6SJoe Stringer } while (0)
70b915ebe6SJoe Stringer
71*c22bdd28SAndrii Nakryiko SEC("tc")
load_static_data(struct __sk_buff * skb)72b915ebe6SJoe Stringer int load_static_data(struct __sk_buff *skb)
73b915ebe6SJoe Stringer {
74b915ebe6SJoe Stringer static const __u64 bar = ~0;
75b915ebe6SJoe Stringer
76b915ebe6SJoe Stringer test_reloc(number, 0, &num0);
77b915ebe6SJoe Stringer test_reloc(number, 1, &num1);
78b915ebe6SJoe Stringer test_reloc(number, 2, &num2);
79b915ebe6SJoe Stringer test_reloc(number, 3, &num3);
80b915ebe6SJoe Stringer test_reloc(number, 4, &num4);
81b915ebe6SJoe Stringer test_reloc(number, 5, &num5);
82b915ebe6SJoe Stringer num4 = 1234;
83b915ebe6SJoe Stringer test_reloc(number, 6, &num4);
84b915ebe6SJoe Stringer test_reloc(number, 7, &num0);
85b915ebe6SJoe Stringer test_reloc(number, 8, &num6);
86b915ebe6SJoe Stringer
87b915ebe6SJoe Stringer test_reloc(string, 0, str0);
88b915ebe6SJoe Stringer test_reloc(string, 1, str1);
89b915ebe6SJoe Stringer test_reloc(string, 2, str2);
90b915ebe6SJoe Stringer str1[5] = 'x';
91b915ebe6SJoe Stringer test_reloc(string, 3, str1);
92b915ebe6SJoe Stringer __builtin_memcpy(&str2[2], "hello", sizeof("hello"));
93b915ebe6SJoe Stringer test_reloc(string, 4, str2);
94b915ebe6SJoe Stringer
95b915ebe6SJoe Stringer test_reloc(struct, 0, &struct0);
96b915ebe6SJoe Stringer test_reloc(struct, 1, &struct1);
97b915ebe6SJoe Stringer test_reloc(struct, 2, &struct2);
98b915ebe6SJoe Stringer test_reloc(struct, 3, &struct3);
99b915ebe6SJoe Stringer
100b915ebe6SJoe Stringer test_reloc(number, 9, &struct0.c);
101b915ebe6SJoe Stringer test_reloc(number, 10, &bar);
102b915ebe6SJoe Stringer
103b915ebe6SJoe Stringer return TC_ACT_OK;
104b915ebe6SJoe Stringer }
105b915ebe6SJoe Stringer
106b915ebe6SJoe Stringer char _license[] SEC("license") = "GPL";
107