Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/bpf.h>
0004 
0005 #include <bpf/bpf_helpers.h>
0006 
0007 struct S {
0008     int v;
0009 };
0010 
0011 struct S global_variable = {};
0012 
0013 struct {
0014     __uint(type, BPF_MAP_TYPE_ARRAY);
0015     __uint(max_entries, 7);
0016     __type(key, __u32);
0017     __type(value, int);
0018 } values SEC(".maps");
0019 
0020 static void save_value(__u32 index, int value)
0021 {
0022     bpf_map_update_elem(&values, &index, &value, 0);
0023 }
0024 
0025 __noinline int foo(__u32 index, struct S *s)
0026 {
0027     if (s) {
0028         save_value(index, s->v);
0029         return ++s->v;
0030     }
0031 
0032     save_value(index, 0);
0033 
0034     return 1;
0035 }
0036 
0037 __noinline int bar(__u32 index, volatile struct S *s)
0038 {
0039     if (s) {
0040         save_value(index, s->v);
0041         return ++s->v;
0042     }
0043 
0044     save_value(index, 0);
0045 
0046     return 1;
0047 }
0048 
0049 __noinline int baz(struct S **s)
0050 {
0051     if (s)
0052         *s = 0;
0053 
0054     return 0;
0055 }
0056 
0057 SEC("cgroup_skb/ingress")
0058 int test_cls(struct __sk_buff *skb)
0059 {
0060     __u32 index = 0;
0061 
0062     {
0063         const int v = foo(index++, 0);
0064 
0065         save_value(index++, v);
0066     }
0067 
0068     {
0069         struct S s = { .v = 100 };
0070 
0071         foo(index++, &s);
0072         save_value(index++, s.v);
0073     }
0074 
0075     {
0076         global_variable.v = 42;
0077         bar(index++, &global_variable);
0078         save_value(index++, global_variable.v);
0079     }
0080 
0081     {
0082         struct S v, *p = &v;
0083 
0084         baz(&p);
0085         save_value(index++, !p);
0086     }
0087 
0088     return 0;
0089 }
0090 
0091 char _license[] SEC("license") = "GPL";