WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / map_tests / array_map_batch_ops.c
blobf0a64d8ac59a5cf26cd3082a8994f3fb75a3fb8b
1 // SPDX-License-Identifier: GPL-2.0
3 #include <stdio.h>
4 #include <errno.h>
5 #include <string.h>
7 #include <bpf/bpf.h>
8 #include <bpf/libbpf.h>
10 #include <test_maps.h>
12 static void map_batch_update(int map_fd, __u32 max_entries, int *keys,
13 int *values)
15 int i, err;
16 DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
17 .elem_flags = 0,
18 .flags = 0,
21 for (i = 0; i < max_entries; i++) {
22 keys[i] = i;
23 values[i] = i + 1;
26 err = bpf_map_update_batch(map_fd, keys, values, &max_entries, &opts);
27 CHECK(err, "bpf_map_update_batch()", "error:%s\n", strerror(errno));
30 static void map_batch_verify(int *visited, __u32 max_entries,
31 int *keys, int *values)
33 int i;
35 memset(visited, 0, max_entries * sizeof(*visited));
36 for (i = 0; i < max_entries; i++) {
37 CHECK(keys[i] + 1 != values[i], "key/value checking",
38 "error: i %d key %d value %d\n", i, keys[i], values[i]);
39 visited[i] = 1;
41 for (i = 0; i < max_entries; i++) {
42 CHECK(visited[i] != 1, "visited checking",
43 "error: keys array at index %d missing\n", i);
47 void test_array_map_batch_ops(void)
49 struct bpf_create_map_attr xattr = {
50 .name = "array_map",
51 .map_type = BPF_MAP_TYPE_ARRAY,
52 .key_size = sizeof(int),
53 .value_size = sizeof(int),
55 int map_fd, *keys, *values, *visited;
56 __u32 count, total, total_success;
57 const __u32 max_entries = 10;
58 bool nospace_err;
59 __u64 batch = 0;
60 int err, step;
61 DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
62 .elem_flags = 0,
63 .flags = 0,
66 xattr.max_entries = max_entries;
67 map_fd = bpf_create_map_xattr(&xattr);
68 CHECK(map_fd == -1,
69 "bpf_create_map_xattr()", "error:%s\n", strerror(errno));
71 keys = malloc(max_entries * sizeof(int));
72 values = malloc(max_entries * sizeof(int));
73 visited = malloc(max_entries * sizeof(int));
74 CHECK(!keys || !values || !visited, "malloc()", "error:%s\n",
75 strerror(errno));
77 /* populate elements to the map */
78 map_batch_update(map_fd, max_entries, keys, values);
80 /* test 1: lookup in a loop with various steps. */
81 total_success = 0;
82 for (step = 1; step < max_entries; step++) {
83 map_batch_update(map_fd, max_entries, keys, values);
84 map_batch_verify(visited, max_entries, keys, values);
85 memset(keys, 0, max_entries * sizeof(*keys));
86 memset(values, 0, max_entries * sizeof(*values));
87 batch = 0;
88 total = 0;
89 /* iteratively lookup/delete elements with 'step'
90 * elements each.
92 count = step;
93 nospace_err = false;
94 while (true) {
95 err = bpf_map_lookup_batch(map_fd,
96 total ? &batch : NULL, &batch,
97 keys + total,
98 values + total,
99 &count, &opts);
101 CHECK((err && errno != ENOENT), "lookup with steps",
102 "error: %s\n", strerror(errno));
104 total += count;
105 if (err)
106 break;
110 if (nospace_err == true)
111 continue;
113 CHECK(total != max_entries, "lookup with steps",
114 "total = %u, max_entries = %u\n", total, max_entries);
116 map_batch_verify(visited, max_entries, keys, values);
118 total_success++;
121 CHECK(total_success == 0, "check total_success",
122 "unexpected failure\n");
124 printf("%s:PASS\n", __func__);
126 free(keys);
127 free(values);
128 free(visited);