map_perf_test.bpf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "vmlinux.h"
  8. #include <errno.h>
  9. #include <linux/version.h>
  10. #include <bpf/bpf_helpers.h>
  11. #include <bpf/bpf_tracing.h>
  12. #include <bpf/bpf_core_read.h>
  13. #define MAX_ENTRIES 1000
  14. #define MAX_NR_CPUS 1024
  15. struct {
  16. __uint(type, BPF_MAP_TYPE_HASH);
  17. __type(key, u32);
  18. __type(value, long);
  19. __uint(max_entries, MAX_ENTRIES);
  20. } hash_map SEC(".maps");
  21. struct {
  22. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  23. __type(key, u32);
  24. __type(value, long);
  25. __uint(max_entries, 10000);
  26. } lru_hash_map SEC(".maps");
  27. struct {
  28. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  29. __type(key, u32);
  30. __type(value, long);
  31. __uint(max_entries, 10000);
  32. __uint(map_flags, BPF_F_NO_COMMON_LRU);
  33. } nocommon_lru_hash_map SEC(".maps");
  34. struct inner_lru {
  35. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  36. __type(key, u32);
  37. __type(value, long);
  38. __uint(max_entries, MAX_ENTRIES);
  39. __uint(map_flags, BPF_F_NUMA_NODE);
  40. __uint(numa_node, 0);
  41. } inner_lru_hash_map SEC(".maps");
  42. struct {
  43. __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
  44. __uint(max_entries, MAX_NR_CPUS);
  45. __uint(key_size, sizeof(u32));
  46. __array(values, struct inner_lru); /* use inner_lru as inner map */
  47. } array_of_lru_hashs SEC(".maps") = {
  48. /* statically initialize the first element */
  49. .values = { &inner_lru_hash_map },
  50. };
  51. struct {
  52. __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
  53. __uint(key_size, sizeof(u32));
  54. __uint(value_size, sizeof(long));
  55. __uint(max_entries, MAX_ENTRIES);
  56. } percpu_hash_map SEC(".maps");
  57. struct {
  58. __uint(type, BPF_MAP_TYPE_HASH);
  59. __type(key, u32);
  60. __type(value, long);
  61. __uint(max_entries, MAX_ENTRIES);
  62. __uint(map_flags, BPF_F_NO_PREALLOC);
  63. } hash_map_alloc SEC(".maps");
  64. struct {
  65. __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
  66. __uint(key_size, sizeof(u32));
  67. __uint(value_size, sizeof(long));
  68. __uint(max_entries, MAX_ENTRIES);
  69. __uint(map_flags, BPF_F_NO_PREALLOC);
  70. } percpu_hash_map_alloc SEC(".maps");
  71. struct {
  72. __uint(type, BPF_MAP_TYPE_LPM_TRIE);
  73. __uint(key_size, 8);
  74. __uint(value_size, sizeof(long));
  75. __uint(max_entries, 10000);
  76. __uint(map_flags, BPF_F_NO_PREALLOC);
  77. } lpm_trie_map_alloc SEC(".maps");
  78. struct {
  79. __uint(type, BPF_MAP_TYPE_ARRAY);
  80. __type(key, u32);
  81. __type(value, long);
  82. __uint(max_entries, MAX_ENTRIES);
  83. } array_map SEC(".maps");
  84. struct {
  85. __uint(type, BPF_MAP_TYPE_LRU_HASH);
  86. __type(key, u32);
  87. __type(value, long);
  88. __uint(max_entries, MAX_ENTRIES);
  89. } lru_hash_lookup_map SEC(".maps");
  90. SEC("ksyscall/getuid")
  91. int BPF_KSYSCALL(stress_hmap)
  92. {
  93. u32 key = bpf_get_current_pid_tgid();
  94. long init_val = 1;
  95. long *value;
  96. int i;
  97. for (i = 0; i < 10; i++) {
  98. bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
  99. value = bpf_map_lookup_elem(&hash_map, &key);
  100. if (value)
  101. bpf_map_delete_elem(&hash_map, &key);
  102. }
  103. return 0;
  104. }
  105. SEC("ksyscall/geteuid")
  106. int BPF_KSYSCALL(stress_percpu_hmap)
  107. {
  108. u32 key = bpf_get_current_pid_tgid();
  109. long init_val = 1;
  110. long *value;
  111. int i;
  112. for (i = 0; i < 10; i++) {
  113. bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
  114. value = bpf_map_lookup_elem(&percpu_hash_map, &key);
  115. if (value)
  116. bpf_map_delete_elem(&percpu_hash_map, &key);
  117. }
  118. return 0;
  119. }
  120. SEC("ksyscall/getgid")
  121. int BPF_KSYSCALL(stress_hmap_alloc)
  122. {
  123. u32 key = bpf_get_current_pid_tgid();
  124. long init_val = 1;
  125. long *value;
  126. int i;
  127. for (i = 0; i < 10; i++) {
  128. bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
  129. value = bpf_map_lookup_elem(&hash_map_alloc, &key);
  130. if (value)
  131. bpf_map_delete_elem(&hash_map_alloc, &key);
  132. }
  133. return 0;
  134. }
  135. SEC("ksyscall/getegid")
  136. int BPF_KSYSCALL(stress_percpu_hmap_alloc)
  137. {
  138. u32 key = bpf_get_current_pid_tgid();
  139. long init_val = 1;
  140. long *value;
  141. int i;
  142. for (i = 0; i < 10; i++) {
  143. bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
  144. value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
  145. if (value)
  146. bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
  147. }
  148. return 0;
  149. }
  150. SEC("ksyscall/connect")
  151. int BPF_KSYSCALL(stress_lru_hmap_alloc, int fd, struct sockaddr_in *uservaddr,
  152. int addrlen)
  153. {
  154. char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
  155. union {
  156. u16 dst6[8];
  157. struct {
  158. u16 magic0;
  159. u16 magic1;
  160. u16 tcase;
  161. u16 unused16;
  162. u32 unused32;
  163. u32 key;
  164. };
  165. } test_params;
  166. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)uservaddr;
  167. u16 test_case;
  168. long val = 1;
  169. u32 key = 0;
  170. int ret;
  171. if (addrlen != sizeof(*in6))
  172. return 0;
  173. ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6),
  174. &in6->sin6_addr);
  175. if (ret)
  176. goto done;
  177. if (test_params.magic0 != 0xdead ||
  178. test_params.magic1 != 0xbeef)
  179. return 0;
  180. test_case = test_params.tcase;
  181. if (test_case != 3)
  182. key = bpf_get_prandom_u32();
  183. if (test_case == 0) {
  184. ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
  185. } else if (test_case == 1) {
  186. ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
  187. BPF_ANY);
  188. } else if (test_case == 2) {
  189. void *nolocal_lru_map;
  190. int cpu = bpf_get_smp_processor_id();
  191. nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
  192. &cpu);
  193. if (!nolocal_lru_map) {
  194. ret = -ENOENT;
  195. goto done;
  196. }
  197. ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
  198. BPF_ANY);
  199. } else if (test_case == 3) {
  200. u32 i;
  201. key = test_params.key;
  202. #pragma clang loop unroll(full)
  203. for (i = 0; i < 32; i++) {
  204. bpf_map_lookup_elem(&lru_hash_lookup_map, &key);
  205. key++;
  206. }
  207. } else {
  208. ret = -EINVAL;
  209. }
  210. done:
  211. if (ret)
  212. bpf_trace_printk(fmt, sizeof(fmt), ret);
  213. return 0;
  214. }
  215. SEC("ksyscall/gettid")
  216. int BPF_KSYSCALL(stress_lpm_trie_map_alloc)
  217. {
  218. union {
  219. u32 b32[2];
  220. u8 b8[8];
  221. } key;
  222. unsigned int i;
  223. key.b32[0] = 32;
  224. key.b8[4] = 192;
  225. key.b8[5] = 168;
  226. key.b8[6] = 0;
  227. key.b8[7] = 1;
  228. #pragma clang loop unroll(full)
  229. for (i = 0; i < 32; ++i)
  230. bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
  231. return 0;
  232. }
  233. SEC("ksyscall/getpgid")
  234. int BPF_KSYSCALL(stress_hash_map_lookup)
  235. {
  236. u32 key = 1, i;
  237. long *value;
  238. #pragma clang loop unroll(full)
  239. for (i = 0; i < 64; ++i)
  240. value = bpf_map_lookup_elem(&hash_map, &key);
  241. return 0;
  242. }
  243. SEC("ksyscall/getppid")
  244. int BPF_KSYSCALL(stress_array_map_lookup)
  245. {
  246. u32 key = 1, i;
  247. long *value;
  248. #pragma clang loop unroll(full)
  249. for (i = 0; i < 64; ++i)
  250. value = bpf_map_lookup_elem(&array_map, &key);
  251. return 0;
  252. }
  253. char _license[] SEC("license") = "GPL";
  254. u32 _version SEC("version") = LINUX_VERSION_CODE;