test_maps.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Testsuite for eBPF maps
  3. *
  4. * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. */
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <linux/bpf.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <sys/wait.h>
  17. #include <stdlib.h>
  18. #include "libbpf.h"
  19. /* sanity tests for map API */
  20. static void test_hashmap_sanity(int i, void *data)
  21. {
  22. long long key, next_key, value;
  23. int map_fd;
  24. map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), 2);
  25. if (map_fd < 0) {
  26. printf("failed to create hashmap '%s'\n", strerror(errno));
  27. exit(1);
  28. }
  29. key = 1;
  30. value = 1234;
  31. /* insert key=1 element */
  32. assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
  33. value = 0;
  34. /* BPF_NOEXIST means: add new element if it doesn't exist */
  35. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
  36. /* key=1 already exists */
  37. errno == EEXIST);
  38. assert(bpf_update_elem(map_fd, &key, &value, -1) == -1 && errno == EINVAL);
  39. /* check that key=1 can be found */
  40. assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
  41. key = 2;
  42. /* check that key=2 is not found */
  43. assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
  44. /* BPF_EXIST means: update existing element */
  45. assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == -1 &&
  46. /* key=2 is not there */
  47. errno == ENOENT);
  48. /* insert key=2 element */
  49. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
  50. /* key=1 and key=2 were inserted, check that key=0 cannot be inserted
  51. * due to max_entries limit
  52. */
  53. key = 0;
  54. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
  55. errno == E2BIG);
  56. /* check that key = 0 doesn't exist */
  57. assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
  58. /* iterate over two elements */
  59. assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
  60. (next_key == 1 || next_key == 2));
  61. assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
  62. (next_key == 1 || next_key == 2));
  63. assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
  64. errno == ENOENT);
  65. /* delete both elements */
  66. key = 1;
  67. assert(bpf_delete_elem(map_fd, &key) == 0);
  68. key = 2;
  69. assert(bpf_delete_elem(map_fd, &key) == 0);
  70. assert(bpf_delete_elem(map_fd, &key) == -1 && errno == ENOENT);
  71. key = 0;
  72. /* check that map is empty */
  73. assert(bpf_get_next_key(map_fd, &key, &next_key) == -1 &&
  74. errno == ENOENT);
  75. close(map_fd);
  76. }
  77. static void test_arraymap_sanity(int i, void *data)
  78. {
  79. int key, next_key, map_fd;
  80. long long value;
  81. map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), 2);
  82. if (map_fd < 0) {
  83. printf("failed to create arraymap '%s'\n", strerror(errno));
  84. exit(1);
  85. }
  86. key = 1;
  87. value = 1234;
  88. /* insert key=1 element */
  89. assert(bpf_update_elem(map_fd, &key, &value, BPF_ANY) == 0);
  90. value = 0;
  91. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
  92. errno == EEXIST);
  93. /* check that key=1 can be found */
  94. assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 1234);
  95. key = 0;
  96. /* check that key=0 is also found and zero initialized */
  97. assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
  98. /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
  99. * due to max_entries limit
  100. */
  101. key = 2;
  102. assert(bpf_update_elem(map_fd, &key, &value, BPF_EXIST) == -1 &&
  103. errno == E2BIG);
  104. /* check that key = 2 doesn't exist */
  105. assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
  106. /* iterate over two elements */
  107. assert(bpf_get_next_key(map_fd, &key, &next_key) == 0 &&
  108. next_key == 0);
  109. assert(bpf_get_next_key(map_fd, &next_key, &next_key) == 0 &&
  110. next_key == 1);
  111. assert(bpf_get_next_key(map_fd, &next_key, &next_key) == -1 &&
  112. errno == ENOENT);
  113. /* delete shouldn't succeed */
  114. key = 1;
  115. assert(bpf_delete_elem(map_fd, &key) == -1 && errno == EINVAL);
  116. close(map_fd);
  117. }
  118. #define MAP_SIZE (32 * 1024)
  119. static void test_map_large(void)
  120. {
  121. struct bigkey {
  122. int a;
  123. char b[116];
  124. long long c;
  125. } key;
  126. int map_fd, i, value;
  127. /* allocate 4Mbyte of memory */
  128. map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  129. MAP_SIZE);
  130. if (map_fd < 0) {
  131. printf("failed to create large map '%s'\n", strerror(errno));
  132. exit(1);
  133. }
  134. for (i = 0; i < MAP_SIZE; i++) {
  135. key = (struct bigkey) {.c = i};
  136. value = i;
  137. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
  138. }
  139. key.c = -1;
  140. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
  141. errno == E2BIG);
  142. /* iterate through all elements */
  143. for (i = 0; i < MAP_SIZE; i++)
  144. assert(bpf_get_next_key(map_fd, &key, &key) == 0);
  145. assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
  146. key.c = 0;
  147. assert(bpf_lookup_elem(map_fd, &key, &value) == 0 && value == 0);
  148. key.a = 1;
  149. assert(bpf_lookup_elem(map_fd, &key, &value) == -1 && errno == ENOENT);
  150. close(map_fd);
  151. }
  152. /* fork N children and wait for them to complete */
  153. static void run_parallel(int tasks, void (*fn)(int i, void *data), void *data)
  154. {
  155. pid_t pid[tasks];
  156. int i;
  157. for (i = 0; i < tasks; i++) {
  158. pid[i] = fork();
  159. if (pid[i] == 0) {
  160. fn(i, data);
  161. exit(0);
  162. } else if (pid[i] == -1) {
  163. printf("couldn't spawn #%d process\n", i);
  164. exit(1);
  165. }
  166. }
  167. for (i = 0; i < tasks; i++) {
  168. int status;
  169. assert(waitpid(pid[i], &status, 0) == pid[i]);
  170. assert(status == 0);
  171. }
  172. }
  173. static void test_map_stress(void)
  174. {
  175. run_parallel(100, test_hashmap_sanity, NULL);
  176. run_parallel(100, test_arraymap_sanity, NULL);
  177. }
  178. #define TASKS 1024
  179. #define DO_UPDATE 1
  180. #define DO_DELETE 0
  181. static void do_work(int fn, void *data)
  182. {
  183. int map_fd = ((int *)data)[0];
  184. int do_update = ((int *)data)[1];
  185. int i;
  186. int key, value;
  187. for (i = fn; i < MAP_SIZE; i += TASKS) {
  188. key = value = i;
  189. if (do_update)
  190. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == 0);
  191. else
  192. assert(bpf_delete_elem(map_fd, &key) == 0);
  193. }
  194. }
  195. static void test_map_parallel(void)
  196. {
  197. int i, map_fd, key = 0, value = 0;
  198. int data[2];
  199. map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
  200. MAP_SIZE);
  201. if (map_fd < 0) {
  202. printf("failed to create map for parallel test '%s'\n",
  203. strerror(errno));
  204. exit(1);
  205. }
  206. data[0] = map_fd;
  207. data[1] = DO_UPDATE;
  208. /* use the same map_fd in children to add elements to this map
  209. * child_0 adds key=0, key=1024, key=2048, ...
  210. * child_1 adds key=1, key=1025, key=2049, ...
  211. * child_1023 adds key=1023, ...
  212. */
  213. run_parallel(TASKS, do_work, data);
  214. /* check that key=0 is already there */
  215. assert(bpf_update_elem(map_fd, &key, &value, BPF_NOEXIST) == -1 &&
  216. errno == EEXIST);
  217. /* check that all elements were inserted */
  218. key = -1;
  219. for (i = 0; i < MAP_SIZE; i++)
  220. assert(bpf_get_next_key(map_fd, &key, &key) == 0);
  221. assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
  222. /* another check for all elements */
  223. for (i = 0; i < MAP_SIZE; i++) {
  224. key = MAP_SIZE - i - 1;
  225. assert(bpf_lookup_elem(map_fd, &key, &value) == 0 &&
  226. value == key);
  227. }
  228. /* now let's delete all elemenets in parallel */
  229. data[1] = DO_DELETE;
  230. run_parallel(TASKS, do_work, data);
  231. /* nothing should be left */
  232. key = -1;
  233. assert(bpf_get_next_key(map_fd, &key, &key) == -1 && errno == ENOENT);
  234. }
  235. int main(void)
  236. {
  237. test_hashmap_sanity(0, NULL);
  238. test_arraymap_sanity(0, NULL);
  239. test_map_large();
  240. test_map_parallel();
  241. test_map_stress();
  242. printf("test_maps: OK\n");
  243. return 0;
  244. }