map_perf_test_kern.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. };
  39. struct bpf_map_def SEC("maps") array_of_lru_hashs = {
  40. .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
  41. .key_size = sizeof(u32),
  42. .max_entries = MAX_NR_CPUS,
  43. };
  44. struct bpf_map_def SEC("maps") percpu_hash_map = {
  45. .type = BPF_MAP_TYPE_PERCPU_HASH,
  46. .key_size = sizeof(u32),
  47. .value_size = sizeof(long),
  48. .max_entries = MAX_ENTRIES,
  49. };
  50. struct bpf_map_def SEC("maps") hash_map_alloc = {
  51. .type = BPF_MAP_TYPE_HASH,
  52. .key_size = sizeof(u32),
  53. .value_size = sizeof(long),
  54. .max_entries = MAX_ENTRIES,
  55. .map_flags = BPF_F_NO_PREALLOC,
  56. };
  57. struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
  58. .type = BPF_MAP_TYPE_PERCPU_HASH,
  59. .key_size = sizeof(u32),
  60. .value_size = sizeof(long),
  61. .max_entries = MAX_ENTRIES,
  62. .map_flags = BPF_F_NO_PREALLOC,
  63. };
  64. struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
  65. .type = BPF_MAP_TYPE_LPM_TRIE,
  66. .key_size = 8,
  67. .value_size = sizeof(long),
  68. .max_entries = 10000,
  69. .map_flags = BPF_F_NO_PREALLOC,
  70. };
  71. struct bpf_map_def SEC("maps") array_map = {
  72. .type = BPF_MAP_TYPE_ARRAY,
  73. .key_size = sizeof(u32),
  74. .value_size = sizeof(long),
  75. .max_entries = MAX_ENTRIES,
  76. };
  77. SEC("kprobe/sys_getuid")
  78. int stress_hmap(struct pt_regs *ctx)
  79. {
  80. u32 key = bpf_get_current_pid_tgid();
  81. long init_val = 1;
  82. long *value;
  83. bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
  84. value = bpf_map_lookup_elem(&hash_map, &key);
  85. if (value)
  86. bpf_map_delete_elem(&hash_map, &key);
  87. return 0;
  88. }
  89. SEC("kprobe/sys_geteuid")
  90. int stress_percpu_hmap(struct pt_regs *ctx)
  91. {
  92. u32 key = bpf_get_current_pid_tgid();
  93. long init_val = 1;
  94. long *value;
  95. bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
  96. value = bpf_map_lookup_elem(&percpu_hash_map, &key);
  97. if (value)
  98. bpf_map_delete_elem(&percpu_hash_map, &key);
  99. return 0;
  100. }
  101. SEC("kprobe/sys_getgid")
  102. int stress_hmap_alloc(struct pt_regs *ctx)
  103. {
  104. u32 key = bpf_get_current_pid_tgid();
  105. long init_val = 1;
  106. long *value;
  107. bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
  108. value = bpf_map_lookup_elem(&hash_map_alloc, &key);
  109. if (value)
  110. bpf_map_delete_elem(&hash_map_alloc, &key);
  111. return 0;
  112. }
  113. SEC("kprobe/sys_getegid")
  114. int stress_percpu_hmap_alloc(struct pt_regs *ctx)
  115. {
  116. u32 key = bpf_get_current_pid_tgid();
  117. long init_val = 1;
  118. long *value;
  119. bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
  120. value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
  121. if (value)
  122. bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
  123. return 0;
  124. }
  125. SEC("kprobe/sys_connect")
  126. int stress_lru_hmap_alloc(struct pt_regs *ctx)
  127. {
  128. struct sockaddr_in6 *in6;
  129. u16 test_case, dst6[8];
  130. int addrlen, ret;
  131. char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%d\n";
  132. long val = 1;
  133. u32 key = bpf_get_prandom_u32();
  134. in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
  135. addrlen = (int)PT_REGS_PARM3(ctx);
  136. if (addrlen != sizeof(*in6))
  137. return 0;
  138. ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
  139. if (ret)
  140. goto done;
  141. if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
  142. return 0;
  143. test_case = dst6[7];
  144. if (test_case == 0) {
  145. ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
  146. } else if (test_case == 1) {
  147. ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
  148. BPF_ANY);
  149. } else if (test_case == 2) {
  150. void *nolocal_lru_map;
  151. int cpu = bpf_get_smp_processor_id();
  152. nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
  153. &cpu);
  154. if (!nolocal_lru_map) {
  155. ret = -ENOENT;
  156. goto done;
  157. }
  158. ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
  159. BPF_ANY);
  160. } else {
  161. ret = -EINVAL;
  162. }
  163. done:
  164. if (ret)
  165. bpf_trace_printk(fmt, sizeof(fmt), ret);
  166. return 0;
  167. }
  168. SEC("kprobe/sys_gettid")
  169. int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
  170. {
  171. union {
  172. u32 b32[2];
  173. u8 b8[8];
  174. } key;
  175. unsigned int i;
  176. key.b32[0] = 32;
  177. key.b8[4] = 192;
  178. key.b8[5] = 168;
  179. key.b8[6] = 0;
  180. key.b8[7] = 1;
  181. #pragma clang loop unroll(full)
  182. for (i = 0; i < 32; ++i)
  183. bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
  184. return 0;
  185. }
  186. SEC("kprobe/sys_getpgid")
  187. int stress_hash_map_lookup(struct pt_regs *ctx)
  188. {
  189. u32 key = 1, i;
  190. long *value;
  191. #pragma clang loop unroll(full)
  192. for (i = 0; i < 64; ++i)
  193. value = bpf_map_lookup_elem(&hash_map, &key);
  194. return 0;
  195. }
  196. SEC("kprobe/sys_getpgrp")
  197. int stress_array_map_lookup(struct pt_regs *ctx)
  198. {
  199. u32 key = 1, i;
  200. long *value;
  201. #pragma clang loop unroll(full)
  202. for (i = 0; i < 64; ++i)
  203. value = bpf_map_lookup_elem(&array_map, &key);
  204. return 0;
  205. }
  206. char _license[] SEC("license") = "GPL";
  207. u32 _version SEC("version") = LINUX_VERSION_CODE;