1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <arpa/inet.h>
4 #include <linux/bpf.h>
5 #include <netinet/in.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 
12 #include <bpf/bpf.h>
13 #include <bpf/libbpf.h>
14 
15 #include <test_maps.h>
16 
17 struct test_lpm_key {
18 	__u32 prefix;
19 	struct in_addr ipv4;
20 };
21 
map_batch_update(int map_fd,__u32 max_entries,struct test_lpm_key * keys,int * values)22 static void map_batch_update(int map_fd, __u32 max_entries,
23 			     struct test_lpm_key *keys, int *values)
24 {
25 	__u32 i;
26 	int err;
27 	char buff[16] = { 0 };
28 	DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
29 		.elem_flags = 0,
30 		.flags = 0,
31 	);
32 
33 	for (i = 0; i < max_entries; i++) {
34 		keys[i].prefix = 32;
35 		snprintf(buff, 16, "192.168.1.%d", i + 1);
36 		inet_pton(AF_INET, buff, &keys[i].ipv4);
37 		values[i] = i + 1;
38 	}
39 
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));
42 }
43 
map_batch_verify(int * visited,__u32 max_entries,struct test_lpm_key * keys,int * values)44 static void map_batch_verify(int *visited, __u32 max_entries,
45 			     struct test_lpm_key *keys, int *values)
46 {
47 	char buff[16] = { 0 };
48 	int lower_byte = 0;
49 	__u32 i;
50 
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]);
58 		visited[i] = 1;
59 	}
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);
63 	}
64 }
65 
test_lpm_trie_map_batch_ops(void)66 void test_lpm_trie_map_batch_ops(void)
67 {
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;
73 	__u64 batch = 0;
74 	int err;
75 	DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
76 		.elem_flags = 0,
77 		.flags = 0,
78 	);
79 
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",
84 	      strerror(errno));
85 
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",
90 	      strerror(errno));
91 
92 	total_success = 0;
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));
98 		batch = 0;
99 		total = 0;
100 		/* iteratively lookup/delete elements with 'step'
101 		 * elements each.
102 		 */
103 		count = step;
104 		while (true) {
105 			err = bpf_map_lookup_batch(map_fd,
106 				total ? &batch : NULL, &batch,
107 				keys + total, values + total, &count, &opts);
108 
109 			CHECK((err && errno != ENOENT), "lookup with steps",
110 			      "error: %s\n", strerror(errno));
111 
112 			total += count;
113 			if (err)
114 				break;
115 		}
116 
117 		CHECK(total != max_entries, "lookup with steps",
118 		      "total = %u, max_entries = %u\n", total, max_entries);
119 
120 		map_batch_verify(visited, max_entries, keys, values);
121 
122 		total = 0;
123 		count = step;
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,
128 						   &opts);
129 			CHECK((err && errno != ENOENT), "delete batch",
130 			      "error: %s\n", strerror(errno));
131 			total += count;
132 			if (err)
133 				break;
134 		}
135 		CHECK(total != max_entries, "delete with steps",
136 		      "total = %u, max_entries = %u\n", total, max_entries);
137 
138 		/* check map is empty, errono == 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));
142 
143 		total_success++;
144 	}
145 
146 	CHECK(total_success == 0, "check total_success",
147 	      "unexpected failure\n");
148 
149 	printf("%s:PASS\n", __func__);
150 
151 	free(keys);
152 	free(values);
153 	free(visited);
154 	close(map_fd);
155 }
156