Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2019 Isovalent, Inc.
0003 
0004 #include <linux/bpf.h>
0005 #include <linux/pkt_cls.h>
0006 #include <string.h>
0007 
0008 #include <bpf/bpf_helpers.h>
0009 
0010 struct {
0011     __uint(type, BPF_MAP_TYPE_ARRAY);
0012     __uint(max_entries, 11);
0013     __type(key, __u32);
0014     __type(value, __u64);
0015 } result_number SEC(".maps");
0016 
0017 struct {
0018     __uint(type, BPF_MAP_TYPE_ARRAY);
0019     __uint(max_entries, 5);
0020     __type(key, __u32);
0021     const char (*value)[32];
0022 } result_string SEC(".maps");
0023 
0024 struct foo {
0025     __u8  a;
0026     __u32 b;
0027     __u64 c;
0028 };
0029 
0030 struct {
0031     __uint(type, BPF_MAP_TYPE_ARRAY);
0032     __uint(max_entries, 5);
0033     __type(key, __u32);
0034     __type(value, struct foo);
0035 } result_struct SEC(".maps");
0036 
0037 /* Relocation tests for __u64s. */
0038 static       __u64 num0;
0039 static       __u64 num1 = 42;
0040 static const __u64 num2 = 24;
0041 static       __u64 num3 = 0;
0042 static       __u64 num4 = 0xffeeff;
0043 static const __u64 num5 = 0xabab;
0044 static const __u64 num6 = 0xab;
0045 
0046 /* Relocation tests for strings. */
0047 static const char str0[32] = "abcdefghijklmnopqrstuvwxyz";
0048 static       char str1[32] = "abcdefghijklmnopqrstuvwxyz";
0049 static       char str2[32];
0050 
0051 /* Relocation tests for structs. */
0052 static const struct foo struct0 = {
0053     .a = 42,
0054     .b = 0xfefeefef,
0055     .c = 0x1111111111111111ULL,
0056 };
0057 static struct foo struct1;
0058 static const struct foo struct2;
0059 static struct foo struct3 = {
0060     .a = 41,
0061     .b = 0xeeeeefef,
0062     .c = 0x2111111111111111ULL,
0063 };
0064 
0065 #define test_reloc(map, num, var)                   \
0066     do {                                \
0067         __u32 key = num;                    \
0068         bpf_map_update_elem(&result_##map, &key, var, 0);   \
0069     } while (0)
0070 
0071 SEC("tc")
0072 int load_static_data(struct __sk_buff *skb)
0073 {
0074     static const __u64 bar = ~0;
0075 
0076     test_reloc(number, 0, &num0);
0077     test_reloc(number, 1, &num1);
0078     test_reloc(number, 2, &num2);
0079     test_reloc(number, 3, &num3);
0080     test_reloc(number, 4, &num4);
0081     test_reloc(number, 5, &num5);
0082     num4 = 1234;
0083     test_reloc(number, 6, &num4);
0084     test_reloc(number, 7, &num0);
0085     test_reloc(number, 8, &num6);
0086 
0087     test_reloc(string, 0, str0);
0088     test_reloc(string, 1, str1);
0089     test_reloc(string, 2, str2);
0090     str1[5] = 'x';
0091     test_reloc(string, 3, str1);
0092     __builtin_memcpy(&str2[2], "hello", sizeof("hello"));
0093     test_reloc(string, 4, str2);
0094 
0095     test_reloc(struct, 0, &struct0);
0096     test_reloc(struct, 1, &struct1);
0097     test_reloc(struct, 2, &struct2);
0098     test_reloc(struct, 3, &struct3);
0099 
0100     test_reloc(number,  9, &struct0.c);
0101     test_reloc(number, 10, &bar);
0102 
0103     return TC_ACT_OK;
0104 }
0105 
0106 char _license[] SEC("license") = "GPL";