Merge tag 'batadv-net-for-davem-20170316' of git://git.open-mesh.org/linux-merge
[linux-2.6-microblaze.git] / samples / bpf / map_perf_test_kern.c
1 /* Copyright (c) 2016 Facebook
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
9 #include <linux/version.h>
10 #include <uapi/linux/bpf.h>
11 #include "bpf_helpers.h"
12
13 #define MAX_ENTRIES 1000
14
15 struct bpf_map_def SEC("maps") hash_map = {
16         .type = BPF_MAP_TYPE_HASH,
17         .key_size = sizeof(u32),
18         .value_size = sizeof(long),
19         .max_entries = MAX_ENTRIES,
20 };
21
22 struct bpf_map_def SEC("maps") lru_hash_map = {
23         .type = BPF_MAP_TYPE_LRU_HASH,
24         .key_size = sizeof(u32),
25         .value_size = sizeof(long),
26         .max_entries = 10000,
27 };
28
29 struct bpf_map_def SEC("maps") percpu_lru_hash_map = {
30         .type = BPF_MAP_TYPE_LRU_HASH,
31         .key_size = sizeof(u32),
32         .value_size = sizeof(long),
33         .max_entries = 10000,
34         .map_flags = BPF_F_NO_COMMON_LRU,
35 };
36
37 struct bpf_map_def SEC("maps") percpu_hash_map = {
38         .type = BPF_MAP_TYPE_PERCPU_HASH,
39         .key_size = sizeof(u32),
40         .value_size = sizeof(long),
41         .max_entries = MAX_ENTRIES,
42 };
43
44 struct bpf_map_def SEC("maps") hash_map_alloc = {
45         .type = BPF_MAP_TYPE_HASH,
46         .key_size = sizeof(u32),
47         .value_size = sizeof(long),
48         .max_entries = MAX_ENTRIES,
49         .map_flags = BPF_F_NO_PREALLOC,
50 };
51
52 struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
53         .type = BPF_MAP_TYPE_PERCPU_HASH,
54         .key_size = sizeof(u32),
55         .value_size = sizeof(long),
56         .max_entries = MAX_ENTRIES,
57         .map_flags = BPF_F_NO_PREALLOC,
58 };
59
60 struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
61         .type = BPF_MAP_TYPE_LPM_TRIE,
62         .key_size = 8,
63         .value_size = sizeof(long),
64         .max_entries = 10000,
65         .map_flags = BPF_F_NO_PREALLOC,
66 };
67
68 SEC("kprobe/sys_getuid")
69 int stress_hmap(struct pt_regs *ctx)
70 {
71         u32 key = bpf_get_current_pid_tgid();
72         long init_val = 1;
73         long *value;
74
75         bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
76         value = bpf_map_lookup_elem(&hash_map, &key);
77         if (value)
78                 bpf_map_delete_elem(&hash_map, &key);
79
80         return 0;
81 }
82
83 SEC("kprobe/sys_geteuid")
84 int stress_percpu_hmap(struct pt_regs *ctx)
85 {
86         u32 key = bpf_get_current_pid_tgid();
87         long init_val = 1;
88         long *value;
89
90         bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
91         value = bpf_map_lookup_elem(&percpu_hash_map, &key);
92         if (value)
93                 bpf_map_delete_elem(&percpu_hash_map, &key);
94         return 0;
95 }
96 SEC("kprobe/sys_getgid")
97 int stress_hmap_alloc(struct pt_regs *ctx)
98 {
99         u32 key = bpf_get_current_pid_tgid();
100         long init_val = 1;
101         long *value;
102
103         bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
104         value = bpf_map_lookup_elem(&hash_map_alloc, &key);
105         if (value)
106                 bpf_map_delete_elem(&hash_map_alloc, &key);
107         return 0;
108 }
109
110 SEC("kprobe/sys_getegid")
111 int stress_percpu_hmap_alloc(struct pt_regs *ctx)
112 {
113         u32 key = bpf_get_current_pid_tgid();
114         long init_val = 1;
115         long *value;
116
117         bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
118         value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
119         if (value)
120                 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
121         return 0;
122 }
123
124 SEC("kprobe/sys_getpid")
125 int stress_lru_hmap_alloc(struct pt_regs *ctx)
126 {
127         u32 key = bpf_get_prandom_u32();
128         long val = 1;
129
130         bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
131
132         return 0;
133 }
134
135 SEC("kprobe/sys_getppid")
136 int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
137 {
138         u32 key = bpf_get_prandom_u32();
139         long val = 1;
140
141         bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
142
143         return 0;
144 }
145
146 SEC("kprobe/sys_gettid")
147 int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
148 {
149         union {
150                 u32 b32[2];
151                 u8 b8[8];
152         } key;
153         unsigned int i;
154
155         key.b32[0] = 32;
156         key.b8[4] = 192;
157         key.b8[5] = 168;
158         key.b8[6] = 0;
159         key.b8[7] = 1;
160
161 #pragma clang loop unroll(full)
162         for (i = 0; i < 32; ++i)
163                 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
164
165         return 0;
166 }
167
168 char _license[] SEC("license") = "GPL";
169 u32 _version SEC("version") = LINUX_VERSION_CODE;