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
13 typedef unsigned long long map_key_t
;
14 typedef unsigned long long map_value_t
;
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
];
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"))
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
;
44 skel
= test_map_init__open();
45 if (!ASSERT_OK_PTR(skel
, "skel_open"))
48 err
= bpf_map__set_type(skel
->maps
.hashmap1
, map_type
);
49 if (!ASSERT_OK(err
, "bpf_map__set_type"))
52 err
= bpf_map__set_max_entries(skel
->maps
.hashmap1
, map_sz
);
53 if (!ASSERT_OK(err
, "bpf_map__set_max_entries"))
56 err
= test_map_init__load(skel
);
57 if (!ASSERT_OK(err
, "skel_load"))
60 *map_fd
= bpf_map__fd(skel
->maps
.hashmap1
);
61 if (CHECK(*map_fd
< 0, "bpf_map__fd", "failed\n"))
64 err
= map_populate(*map_fd
, populate
);
65 if (!ASSERT_OK(err
, "map_populate"))
73 test_map_init__destroy(skel
);
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
,
81 struct test_map_init__bss
*bss
;
87 bss
->inPid
= getpid();
89 if (!ASSERT_OK(test_map_init__attach(skel
), "skel_attach"))
92 /* Let tracepoint trigger */
93 syscall(__NR_getpgid
);
95 test_map_init__detach(skel
);
100 static int check_values_one_cpu(pcpu_map_value_t
*value
, map_value_t expected
)
105 for (i
= 0; i
< nr_cpus
; i
++) {
106 val
= bpf_percpu(value
, i
);
108 if (CHECK(val
!= expected
, "map value",
109 "unexpected for cpu %d: 0x%llx\n", i
, val
))
115 if (CHECK(nzCnt
!= 1, "map value", "set for %d CPUs instead of 1!\n",
122 /* Add key=1 elem with values set for all CPUs
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
;
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"))
141 /* delete element so the entry can be re-used*/
143 err
= bpf_map_delete_elem(map_fd
, &key
);
144 if (!ASSERT_OK(err
, "bpf_map_delete_elem"))
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"))
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"))
157 /* and has expected values */
158 check_values_one_cpu(value
, TEST_VALUE
);
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
;
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"))
183 /* run bpf prog that inserts new key=3 element, re-using LRU slot */
185 err
= prog_run_insert_elem(skel
, key
, TEST_VALUE
);
186 if (!ASSERT_OK(err
, "prog_run_insert_elem"))
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"))
194 /* and has expected values */
195 check_values_one_cpu(value
, TEST_VALUE
);
198 test_map_init__destroy(skel
);
201 void test_map_init(void)
203 nr_cpus
= bpf_num_possible_cpus();
205 printf("%s:SKIP: >1 cpu needed for this test\n", __func__
);
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();