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