WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / map_init.c
blob14a31109dd0e0df1085ceefb8cb5d0e7771a2989
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Tessares SA <http://www.tessares.net> */
4 #include <test_progs.h>
5 #include "test_map_init.skel.h"
7 #define TEST_VALUE 0x1234
8 #define FILL_VALUE 0xdeadbeef
10 static int nr_cpus;
11 static int duration;
13 typedef unsigned long long map_key_t;
14 typedef unsigned long long map_value_t;
15 typedef struct {
16 map_value_t v; /* padding */
17 } __bpf_percpu_val_align pcpu_map_value_t;
20 static int map_populate(int map_fd, int num)
22 pcpu_map_value_t value[nr_cpus];
23 int i, err;
24 map_key_t key;
26 for (i = 0; i < nr_cpus; i++)
27 bpf_percpu(value, i) = FILL_VALUE;
29 for (key = 1; key <= num; key++) {
30 err = bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST);
31 if (!ASSERT_OK(err, "bpf_map_update_elem"))
32 return -1;
35 return 0;
38 static struct test_map_init *setup(enum bpf_map_type map_type, int map_sz,
39 int *map_fd, int populate)
41 struct test_map_init *skel;
42 int err;
44 skel = test_map_init__open();
45 if (!ASSERT_OK_PTR(skel, "skel_open"))
46 return NULL;
48 err = bpf_map__set_type(skel->maps.hashmap1, map_type);
49 if (!ASSERT_OK(err, "bpf_map__set_type"))
50 goto error;
52 err = bpf_map__set_max_entries(skel->maps.hashmap1, map_sz);
53 if (!ASSERT_OK(err, "bpf_map__set_max_entries"))
54 goto error;
56 err = test_map_init__load(skel);
57 if (!ASSERT_OK(err, "skel_load"))
58 goto error;
60 *map_fd = bpf_map__fd(skel->maps.hashmap1);
61 if (CHECK(*map_fd < 0, "bpf_map__fd", "failed\n"))
62 goto error;
64 err = map_populate(*map_fd, populate);
65 if (!ASSERT_OK(err, "map_populate"))
66 goto error_map;
68 return skel;
70 error_map:
71 close(*map_fd);
72 error:
73 test_map_init__destroy(skel);
74 return NULL;
77 /* executes bpf program that updates map with key, value */
78 static int prog_run_insert_elem(struct test_map_init *skel, map_key_t key,
79 map_value_t value)
81 struct test_map_init__bss *bss;
83 bss = skel->bss;
85 bss->inKey = key;
86 bss->inValue = value;
87 bss->inPid = getpid();
89 if (!ASSERT_OK(test_map_init__attach(skel), "skel_attach"))
90 return -1;
92 /* Let tracepoint trigger */
93 syscall(__NR_getpgid);
95 test_map_init__detach(skel);
97 return 0;
100 static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)
102 int i, nzCnt = 0;
103 map_value_t val;
105 for (i = 0; i < nr_cpus; i++) {
106 val = bpf_percpu(value, i);
107 if (val) {
108 if (CHECK(val != expected, "map value",
109 "unexpected for cpu %d: 0x%llx\n", i, val))
110 return -1;
111 nzCnt++;
115 if (CHECK(nzCnt != 1, "map value", "set for %d CPUs instead of 1!\n",
116 nzCnt))
117 return -1;
119 return 0;
122 /* Add key=1 elem with values set for all CPUs
123 * Delete elem key=1
124 * Run bpf prog that inserts new key=1 elem with value=0x1234
125 * (bpf prog can only set value for current CPU)
126 * Lookup Key=1 and check value is as expected for all CPUs:
127 * value set by bpf prog for one CPU, 0 for all others
129 static void test_pcpu_map_init(void)
131 pcpu_map_value_t value[nr_cpus];
132 struct test_map_init *skel;
133 int map_fd, err;
134 map_key_t key;
136 /* max 1 elem in map so insertion is forced to reuse freed entry */
137 skel = setup(BPF_MAP_TYPE_PERCPU_HASH, 1, &map_fd, 1);
138 if (!ASSERT_OK_PTR(skel, "prog_setup"))
139 return;
141 /* delete element so the entry can be re-used*/
142 key = 1;
143 err = bpf_map_delete_elem(map_fd, &key);
144 if (!ASSERT_OK(err, "bpf_map_delete_elem"))
145 goto cleanup;
147 /* run bpf prog that inserts new elem, re-using the slot just freed */
148 err = prog_run_insert_elem(skel, key, TEST_VALUE);
149 if (!ASSERT_OK(err, "prog_run_insert_elem"))
150 goto cleanup;
152 /* check that key=1 was re-created by bpf prog */
153 err = bpf_map_lookup_elem(map_fd, &key, value);
154 if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
155 goto cleanup;
157 /* and has expected values */
158 check_values_one_cpu(value, TEST_VALUE);
160 cleanup:
161 test_map_init__destroy(skel);
164 /* Add key=1 and key=2 elems with values set for all CPUs
165 * Run bpf prog that inserts new key=3 elem
166 * (only for current cpu; other cpus should have initial value = 0)
167 * Lookup Key=1 and check value is as expected for all CPUs
169 static void test_pcpu_lru_map_init(void)
171 pcpu_map_value_t value[nr_cpus];
172 struct test_map_init *skel;
173 int map_fd, err;
174 map_key_t key;
176 /* Set up LRU map with 2 elements, values filled for all CPUs.
177 * With these 2 elements, the LRU map is full
179 skel = setup(BPF_MAP_TYPE_LRU_PERCPU_HASH, 2, &map_fd, 2);
180 if (!ASSERT_OK_PTR(skel, "prog_setup"))
181 return;
183 /* run bpf prog that inserts new key=3 element, re-using LRU slot */
184 key = 3;
185 err = prog_run_insert_elem(skel, key, TEST_VALUE);
186 if (!ASSERT_OK(err, "prog_run_insert_elem"))
187 goto cleanup;
189 /* check that key=3 replaced one of earlier elements */
190 err = bpf_map_lookup_elem(map_fd, &key, value);
191 if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
192 goto cleanup;
194 /* and has expected values */
195 check_values_one_cpu(value, TEST_VALUE);
197 cleanup:
198 test_map_init__destroy(skel);
201 void test_map_init(void)
203 nr_cpus = bpf_num_possible_cpus();
204 if (nr_cpus <= 1) {
205 printf("%s:SKIP: >1 cpu needed for this test\n", __func__);
206 test__skip();
207 return;
210 if (test__start_subtest("pcpu_map_init"))
211 test_pcpu_map_init();
212 if (test__start_subtest("pcpu_lru_map_init"))
213 test_pcpu_lru_map_init();