github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/testsuite/integration/ebpf/global_data_test.c (about) 1 #include <stddef.h> 2 #include <linux/bpf.h> 3 4 #define SEC(NAME) __attribute__((section(NAME), used)) 5 6 static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, __u64 flags) = (void *) 2; 7 8 struct bpf_map_def { 9 unsigned int type; 10 unsigned int key_size; 11 unsigned int value_size; 12 unsigned int max_entries; 13 unsigned int map_flags; 14 }; 15 16 struct map_val { 17 __u64 cnt; 18 }; 19 20 struct bpf_map_def SEC("maps") result_number = { 21 .type = BPF_MAP_TYPE_ARRAY, 22 .key_size = sizeof(__u32), 23 .value_size = sizeof(__u64), 24 .max_entries = 11, 25 }; 26 27 struct bpf_map_def SEC("maps") result_string = { 28 .type = BPF_MAP_TYPE_ARRAY, 29 .key_size = sizeof(__u32), 30 .value_size = 32, 31 .max_entries = 5, 32 }; 33 34 struct foo { 35 __u8 a; 36 __u32 b; 37 __u64 c; 38 }; 39 40 struct bpf_map_def SEC("maps") result_struct = { 41 .type = BPF_MAP_TYPE_ARRAY, 42 .key_size = sizeof(__u32), 43 .value_size = sizeof(struct foo), 44 .max_entries = 5, 45 }; 46 47 48 /* Relocation tests for __u64s. */ 49 static __u64 num0; 50 static __u64 num1 = 42; 51 static const __u64 num2 = 24; 52 static __u64 num3 = 0; 53 static __u64 num4 = 0xffeeff; 54 static const __u64 num5 = 0xabab; 55 static const __u64 num6 = 0xab; 56 57 /* Relocation tests for strings. */ 58 static const char str0[32] = "abcdefghijklmnopqrstuvwxyz"; 59 static char str1[32] = "abcdefghijklmnopqrstuvwxyz"; 60 static char str2[32]; 61 62 /* Relocation tests for structs. */ 63 static const struct foo struct0 = { 64 .a = 42, 65 .b = 0xfefeefef, 66 .c = 0x1111111111111111ULL, 67 }; 68 static struct foo struct1; 69 static const struct foo struct2; 70 static struct foo struct3 = { 71 .a = 41, 72 .b = 0xeeeeefef, 73 .c = 0x2111111111111111ULL, 74 }; 75 76 #define test_reloc(map, num, var) \ 77 do { \ 78 __u32 key = num; \ 79 bpf_map_update_elem(&result_##map, &key, var, 0); \ 80 } while (0) 81 82 SEC("xdp") 83 int load_static_data(struct xdp_md *ctx) 84 { 85 static const __u64 bar = ~0; 86 87 test_reloc(number, 0, &num0); 88 test_reloc(number, 1, &num1); 89 test_reloc(number, 2, &num2); 90 test_reloc(number, 3, &num3); 91 test_reloc(number, 4, &num4); 92 test_reloc(number, 5, &num5); 93 num4 = 1234; 94 test_reloc(number, 6, &num4); 95 test_reloc(number, 7, &num0); 96 test_reloc(number, 8, &num6); 97 98 test_reloc(string, 0, str0); 99 test_reloc(string, 1, str1); 100 test_reloc(string, 2, str2); 101 str1[5] = 'x'; 102 test_reloc(string, 3, str1); 103 __builtin_memcpy(&str2[2], "hello", sizeof("hello")); 104 test_reloc(string, 4, str2); 105 106 test_reloc(struct, 0, &struct0); 107 test_reloc(struct, 1, &struct1); 108 test_reloc(struct, 2, &struct2); 109 test_reloc(struct, 3, &struct3); 110 111 test_reloc(number, 9, &struct0.c); 112 test_reloc(number, 10, &bar); 113 114 return XDP_PASS; 115 } 116 117 char _license[] SEC("license") = "GPL";