PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/testing/selftests/bpf/map_tests/htab_map_batch_ops.c

http://github.com/torvalds/linux
C | 283 lines | 220 code | 38 blank | 25 comment | 53 complexity | 1d3274e308f02b4ac99ceb05dc311e5f MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Facebook */
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <bpf/bpf.h>
  7. #include <bpf/libbpf.h>
  8. #include <bpf_util.h>
  9. #include <test_maps.h>
  10. static void map_batch_update(int map_fd, __u32 max_entries, int *keys,
  11. void *values, bool is_pcpu)
  12. {
  13. typedef BPF_DECLARE_PERCPU(int, value);
  14. value *v = NULL;
  15. int i, j, err;
  16. DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
  17. .elem_flags = 0,
  18. .flags = 0,
  19. );
  20. if (is_pcpu)
  21. v = (value *)values;
  22. for (i = 0; i < max_entries; i++) {
  23. keys[i] = i + 1;
  24. if (is_pcpu)
  25. for (j = 0; j < bpf_num_possible_cpus(); j++)
  26. bpf_percpu(v[i], j) = i + 2 + j;
  27. else
  28. ((int *)values)[i] = i + 2;
  29. }
  30. err = bpf_map_update_batch(map_fd, keys, values, &max_entries, &opts);
  31. CHECK(err, "bpf_map_update_batch()", "error:%s\n", strerror(errno));
  32. }
  33. static void map_batch_verify(int *visited, __u32 max_entries,
  34. int *keys, void *values, bool is_pcpu)
  35. {
  36. typedef BPF_DECLARE_PERCPU(int, value);
  37. value *v = NULL;
  38. int i, j;
  39. if (is_pcpu)
  40. v = (value *)values;
  41. memset(visited, 0, max_entries * sizeof(*visited));
  42. for (i = 0; i < max_entries; i++) {
  43. if (is_pcpu) {
  44. for (j = 0; j < bpf_num_possible_cpus(); j++) {
  45. CHECK(keys[i] + 1 + j != bpf_percpu(v[i], j),
  46. "key/value checking",
  47. "error: i %d j %d key %d value %d\n",
  48. i, j, keys[i], bpf_percpu(v[i], j));
  49. }
  50. } else {
  51. CHECK(keys[i] + 1 != ((int *)values)[i],
  52. "key/value checking",
  53. "error: i %d key %d value %d\n", i, keys[i],
  54. ((int *)values)[i]);
  55. }
  56. visited[i] = 1;
  57. }
  58. for (i = 0; i < max_entries; i++) {
  59. CHECK(visited[i] != 1, "visited checking",
  60. "error: keys array at index %d missing\n", i);
  61. }
  62. }
  63. void __test_map_lookup_and_delete_batch(bool is_pcpu)
  64. {
  65. __u32 batch, count, total, total_success;
  66. typedef BPF_DECLARE_PERCPU(int, value);
  67. int map_fd, *keys, *visited, key;
  68. const __u32 max_entries = 10;
  69. value pcpu_values[max_entries];
  70. int err, step, value_size;
  71. bool nospace_err;
  72. void *values;
  73. struct bpf_create_map_attr xattr = {
  74. .name = "hash_map",
  75. .map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH :
  76. BPF_MAP_TYPE_HASH,
  77. .key_size = sizeof(int),
  78. .value_size = sizeof(int),
  79. };
  80. DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
  81. .elem_flags = 0,
  82. .flags = 0,
  83. );
  84. xattr.max_entries = max_entries;
  85. map_fd = bpf_create_map_xattr(&xattr);
  86. CHECK(map_fd == -1,
  87. "bpf_create_map_xattr()", "error:%s\n", strerror(errno));
  88. value_size = is_pcpu ? sizeof(value) : sizeof(int);
  89. keys = malloc(max_entries * sizeof(int));
  90. if (is_pcpu)
  91. values = pcpu_values;
  92. else
  93. values = malloc(max_entries * sizeof(int));
  94. visited = malloc(max_entries * sizeof(int));
  95. CHECK(!keys || !values || !visited, "malloc()",
  96. "error:%s\n", strerror(errno));
  97. /* test 1: lookup/delete an empty hash table, -ENOENT */
  98. count = max_entries;
  99. err = bpf_map_lookup_and_delete_batch(map_fd, NULL, &batch, keys,
  100. values, &count, &opts);
  101. CHECK((err && errno != ENOENT), "empty map",
  102. "error: %s\n", strerror(errno));
  103. /* populate elements to the map */
  104. map_batch_update(map_fd, max_entries, keys, values, is_pcpu);
  105. /* test 2: lookup/delete with count = 0, success */
  106. count = 0;
  107. err = bpf_map_lookup_and_delete_batch(map_fd, NULL, &batch, keys,
  108. values, &count, &opts);
  109. CHECK(err, "count = 0", "error: %s\n", strerror(errno));
  110. /* test 3: lookup/delete with count = max_entries, success */
  111. memset(keys, 0, max_entries * sizeof(*keys));
  112. memset(values, 0, max_entries * value_size);
  113. count = max_entries;
  114. err = bpf_map_lookup_and_delete_batch(map_fd, NULL, &batch, keys,
  115. values, &count, &opts);
  116. CHECK((err && errno != ENOENT), "count = max_entries",
  117. "error: %s\n", strerror(errno));
  118. CHECK(count != max_entries, "count = max_entries",
  119. "count = %u, max_entries = %u\n", count, max_entries);
  120. map_batch_verify(visited, max_entries, keys, values, is_pcpu);
  121. /* bpf_map_get_next_key() should return -ENOENT for an empty map. */
  122. err = bpf_map_get_next_key(map_fd, NULL, &key);
  123. CHECK(!err, "bpf_map_get_next_key()", "error: %s\n", strerror(errno));
  124. /* test 4: lookup/delete in a loop with various steps. */
  125. total_success = 0;
  126. for (step = 1; step < max_entries; step++) {
  127. map_batch_update(map_fd, max_entries, keys, values, is_pcpu);
  128. memset(keys, 0, max_entries * sizeof(*keys));
  129. memset(values, 0, max_entries * value_size);
  130. total = 0;
  131. /* iteratively lookup/delete elements with 'step'
  132. * elements each
  133. */
  134. count = step;
  135. nospace_err = false;
  136. while (true) {
  137. err = bpf_map_lookup_batch(map_fd,
  138. total ? &batch : NULL,
  139. &batch, keys + total,
  140. values +
  141. total * value_size,
  142. &count, &opts);
  143. /* It is possible that we are failing due to buffer size
  144. * not big enough. In such cases, let us just exit and
  145. * go with large steps. Not that a buffer size with
  146. * max_entries should always work.
  147. */
  148. if (err && errno == ENOSPC) {
  149. nospace_err = true;
  150. break;
  151. }
  152. CHECK((err && errno != ENOENT), "lookup with steps",
  153. "error: %s\n", strerror(errno));
  154. total += count;
  155. if (err)
  156. break;
  157. }
  158. if (nospace_err == true)
  159. continue;
  160. CHECK(total != max_entries, "lookup with steps",
  161. "total = %u, max_entries = %u\n", total, max_entries);
  162. map_batch_verify(visited, max_entries, keys, values, is_pcpu);
  163. total = 0;
  164. count = step;
  165. while (total < max_entries) {
  166. if (max_entries - total < step)
  167. count = max_entries - total;
  168. err = bpf_map_delete_batch(map_fd,
  169. keys + total,
  170. &count, &opts);
  171. CHECK((err && errno != ENOENT), "delete batch",
  172. "error: %s\n", strerror(errno));
  173. total += count;
  174. if (err)
  175. break;
  176. }
  177. CHECK(total != max_entries, "delete with steps",
  178. "total = %u, max_entries = %u\n", total, max_entries);
  179. /* check map is empty, errono == ENOENT */
  180. err = bpf_map_get_next_key(map_fd, NULL, &key);
  181. CHECK(!err || errno != ENOENT, "bpf_map_get_next_key()",
  182. "error: %s\n", strerror(errno));
  183. /* iteratively lookup/delete elements with 'step'
  184. * elements each
  185. */
  186. map_batch_update(map_fd, max_entries, keys, values, is_pcpu);
  187. memset(keys, 0, max_entries * sizeof(*keys));
  188. memset(values, 0, max_entries * value_size);
  189. total = 0;
  190. count = step;
  191. nospace_err = false;
  192. while (true) {
  193. err = bpf_map_lookup_and_delete_batch(map_fd,
  194. total ? &batch : NULL,
  195. &batch, keys + total,
  196. values +
  197. total * value_size,
  198. &count, &opts);
  199. /* It is possible that we are failing due to buffer size
  200. * not big enough. In such cases, let us just exit and
  201. * go with large steps. Not that a buffer size with
  202. * max_entries should always work.
  203. */
  204. if (err && errno == ENOSPC) {
  205. nospace_err = true;
  206. break;
  207. }
  208. CHECK((err && errno != ENOENT), "lookup with steps",
  209. "error: %s\n", strerror(errno));
  210. total += count;
  211. if (err)
  212. break;
  213. }
  214. if (nospace_err == true)
  215. continue;
  216. CHECK(total != max_entries, "lookup/delete with steps",
  217. "total = %u, max_entries = %u\n", total, max_entries);
  218. map_batch_verify(visited, max_entries, keys, values, is_pcpu);
  219. err = bpf_map_get_next_key(map_fd, NULL, &key);
  220. CHECK(!err, "bpf_map_get_next_key()", "error: %s\n",
  221. strerror(errno));
  222. total_success++;
  223. }
  224. CHECK(total_success == 0, "check total_success",
  225. "unexpected failure\n");
  226. free(keys);
  227. free(visited);
  228. if (!is_pcpu)
  229. free(values);
  230. }
  231. void htab_map_batch_ops(void)
  232. {
  233. __test_map_lookup_and_delete_batch(false);
  234. printf("test_%s:PASS\n", __func__);
  235. }
  236. void htab_percpu_map_batch_ops(void)
  237. {
  238. __test_map_lookup_and_delete_batch(true);
  239. printf("test_%s:PASS\n", __func__);
  240. }
  241. void test_htab_map_batch_ops(void)
  242. {
  243. htab_map_batch_ops();
  244. htab_percpu_map_batch_ops();
  245. }