map_perf_test_kern.c 6.5 KB

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