map_perf_test_kern.c 6.1 KB

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