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