1 // SPDX-License-Identifier: GPL-2.0
5 #include <netinet/in.h>
13 #include <bpf/libbpf.h>
15 #include <test_maps.h>
22 static void map_batch_update(int map_fd, __u32 max_entries,
23 struct test_lpm_key *keys, int *values)
27 char buff[16] = { 0 };
28 DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
33 for (i = 0; i < max_entries; i++) {
35 snprintf(buff, 16, "192.168.1.%d", i + 1);
36 inet_pton(AF_INET, buff, &keys[i].ipv4);
40 err = bpf_map_update_batch(map_fd, keys, values, &max_entries, &opts);
41 CHECK(err, "bpf_map_update_batch()", "error:%s\n", strerror(errno));
44 static void map_batch_verify(int *visited, __u32 max_entries,
45 struct test_lpm_key *keys, int *values)
47 char buff[16] = { 0 };
51 memset(visited, 0, max_entries * sizeof(*visited));
52 for (i = 0; i < max_entries; i++) {
53 inet_ntop(AF_INET, &keys[i].ipv4, buff, 32);
54 CHECK(sscanf(buff, "192.168.1.%d", &lower_byte) == EOF,
55 "sscanf()", "error: i %d\n", i);
56 CHECK(lower_byte != values[i], "key/value checking",
57 "error: i %d key %s value %d\n", i, buff, values[i]);
60 for (i = 0; i < max_entries; i++) {
61 CHECK(visited[i] != 1, "visited checking",
62 "error: keys array at index %d missing\n", i);
66 void test_lpm_trie_map_batch_ops(void)
68 LIBBPF_OPTS(bpf_map_create_opts, create_opts, .map_flags = BPF_F_NO_PREALLOC);
69 struct test_lpm_key *keys, key;
70 int map_fd, *values, *visited;
71 __u32 step, count, total, total_success;
72 const __u32 max_entries = 10;
75 DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
80 map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, "lpm_trie_map",
81 sizeof(struct test_lpm_key), sizeof(int),
82 max_entries, &create_opts);
83 CHECK(map_fd == -1, "bpf_map_create()", "error:%s\n",
86 keys = malloc(max_entries * sizeof(struct test_lpm_key));
87 values = malloc(max_entries * sizeof(int));
88 visited = malloc(max_entries * sizeof(int));
89 CHECK(!keys || !values || !visited, "malloc()", "error:%s\n",
93 for (step = 1; step < max_entries; step++) {
94 map_batch_update(map_fd, max_entries, keys, values);
95 map_batch_verify(visited, max_entries, keys, values);
96 memset(keys, 0, max_entries * sizeof(*keys));
97 memset(values, 0, max_entries * sizeof(*values));
100 /* iteratively lookup/delete elements with 'step'
105 err = bpf_map_lookup_batch(map_fd,
106 total ? &batch : NULL, &batch,
107 keys + total, values + total, &count, &opts);
109 CHECK((err && errno != ENOENT), "lookup with steps",
110 "error: %s\n", strerror(errno));
117 CHECK(total != max_entries, "lookup with steps",
118 "total = %u, max_entries = %u\n", total, max_entries);
120 map_batch_verify(visited, max_entries, keys, values);
124 while (total < max_entries) {
125 if (max_entries - total < step)
126 count = max_entries - total;
127 err = bpf_map_delete_batch(map_fd, keys + total, &count,
129 CHECK((err && errno != ENOENT), "delete batch",
130 "error: %s\n", strerror(errno));
135 CHECK(total != max_entries, "delete with steps",
136 "total = %u, max_entries = %u\n", total, max_entries);
138 /* check map is empty, errno == ENOENT */
139 err = bpf_map_get_next_key(map_fd, NULL, &key);
140 CHECK(!err || errno != ENOENT, "bpf_map_get_next_key()",
141 "error: %s\n", strerror(errno));
146 CHECK(total_success == 0, "check total_success",
147 "unexpected failure\n");
149 printf("%s:PASS\n", __func__);