Skip to content

Commit 82cd3bb

Browse files
abhishekvijeevavagin
authored andcommitted
zdtm: update and refactor tests for BPF array and hash maps
This commit achieves the following: a) Refactors ZDTM tests bpf_array.c and bpf_hash.c to make use of the BPF ZDTM library functions. In addition, these tests now verify whether information obtained from both procfs and BPF_OBJ_GET_INFO_BY_FD are the same before and after c/r. b) Updates ZDTM tests bpf_array.c and bpf_hash.c to include a BPF map's name and also to freeze maps Source files modified: * zdtm/static/bpf_array.c * zdtm/static/bpf_hash.c Source files added: * zdtm/static/bpf_array.desc * zdtm/static/bpf_hash.desc Note: ${test_name}.desc files have the 'suid' flag set because BPF_MAP_FREEZE requires the global (root-userns) CAP_SYS_ADMIN or CAP_BPF. Hence, only test flavors 'h' and 'ns' are executed ('uns' is skipped) because BPF_MAP_FREEZE can't be used from non-root user namespaces. Signed-off-by: Abhishek Vijeev <[email protected]>
1 parent 8301c7e commit 82cd3bb

File tree

4 files changed

+73
-39
lines changed

4 files changed

+73
-39
lines changed

test/zdtm/static/bpf_array.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sys/mman.h>
44

55
#include "zdtmtst.h"
6+
#include "bpfmap_zdtm.h"
67

78
const char *test_doc = "Check that data and meta-data for BPF_MAP_TYPE_ARRAY"
89
"is correctly restored";
@@ -58,9 +59,13 @@ int main(int argc, char **argv)
5859
int *keys = NULL, *values = NULL, *visited = NULL;
5960
const uint32_t max_entries = 10;
6061
int ret;
61-
struct bpf_map_info map_info = {};
62-
uint32_t info_len = sizeof(map_info);
62+
struct bpf_map_info old_map_info = {};
63+
struct bpf_map_info new_map_info = {};
64+
struct bpfmap_fdinfo_obj old_fdinfo = {};
65+
struct bpfmap_fdinfo_obj new_fdinfo = {};
66+
uint32_t info_len = sizeof(struct bpf_map_info);
6367
struct bpf_create_map_attr xattr = {
68+
.name = "array_test_map",
6469
.map_type = BPF_MAP_TYPE_ARRAY,
6570
.key_size = sizeof(int),
6671
.value_size = sizeof(int),
@@ -95,34 +100,47 @@ int main(int argc, char **argv)
95100
if (map_batch_update(map_fd, max_entries, keys, values))
96101
goto err;
97102

98-
test_daemon();
99-
100-
test_waitsig();
103+
ret = bpf_map_freeze(map_fd);
104+
if (ret) {
105+
pr_perror("Could not freeze map");
106+
goto err;
107+
}
101108

102-
ret = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
109+
ret = bpf_obj_get_info_by_fd(map_fd, &old_map_info, &info_len);
103110
if (ret) {
104-
pr_perror("Could not get map info");
111+
pr_perror("Could not get old map info");
105112
goto err;
106113
}
107-
108-
if (map_info.type != BPF_MAP_TYPE_ARRAY) {
109-
pr_err("Map type should be BPF_MAP_TYPE_ARRAY\n");
114+
115+
ret = parse_bpfmap_fdinfo(map_fd, &old_fdinfo, 8);
116+
if (ret) {
117+
pr_perror("Could not parse old map fdinfo from procfs");
110118
goto err;
111119
}
112-
if (map_info.key_size != sizeof(*keys)) {
113-
pr_err("Key size should be %zu\n", sizeof(*keys));
120+
121+
test_daemon();
122+
123+
test_waitsig();
124+
125+
ret = bpf_obj_get_info_by_fd(map_fd, &new_map_info, &info_len);
126+
if (ret) {
127+
pr_perror("Could not get new map info");
114128
goto err;
115129
}
116-
if (map_info.value_size != sizeof(*values)) {
117-
pr_err("Value size should be %zu\n", sizeof(*values));
130+
131+
ret = parse_bpfmap_fdinfo(map_fd, &new_fdinfo, 8);
132+
if (ret) {
133+
pr_perror("Could not parse new map fdinfo from procfs");
118134
goto err;
119135
}
120-
if (map_info.max_entries != max_entries) {
121-
pr_err("Max entries should be %d\n", max_entries);
136+
137+
if (cmp_bpf_map_info(&old_map_info, &new_map_info)) {
138+
pr_err("bpf_map_info mismatch\n");
122139
goto err;
123140
}
124-
if (!(map_info.map_flags & BPF_F_NUMA_NODE)) {
125-
pr_err("Map flag BPF_F_NUMA_NODE should be set\n");
141+
142+
if (cmp_bpfmap_fdinfo(&old_fdinfo, &new_fdinfo)) {
143+
pr_err("bpfmap fdinfo mismatch\n");
126144
goto err;
127145
}
128146

test/zdtm/static/bpf_array.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'flavor': 'h ns', 'flags': 'suid'}

test/zdtm/static/bpf_hash.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sys/mman.h>
44

55
#include "zdtmtst.h"
6+
#include "bpfmap_zdtm.h"
67

78
const char *test_doc = "Check that data and meta-data for BPF_MAP_TYPE_HASH"
89
"is correctly restored";
@@ -57,9 +58,13 @@ int main(int argc, char **argv)
5758
int *keys = NULL, *values = NULL, *visited = NULL;
5859
const uint32_t max_entries = 10;
5960
int ret;
60-
struct bpf_map_info map_info = {};
61-
uint32_t info_len = sizeof(map_info);
61+
struct bpf_map_info old_map_info = {};
62+
struct bpf_map_info new_map_info = {};
63+
struct bpfmap_fdinfo_obj old_fdinfo = {};
64+
struct bpfmap_fdinfo_obj new_fdinfo = {};
65+
uint32_t info_len = sizeof(struct bpf_map_info);
6266
struct bpf_create_map_attr xattr = {
67+
.name = "hash_test_map",
6368
.map_type = BPF_MAP_TYPE_HASH,
6469
.key_size = sizeof(int),
6570
.value_size = sizeof(int),
@@ -94,38 +99,47 @@ int main(int argc, char **argv)
9499
if (map_batch_update(map_fd, max_entries, keys, values))
95100
goto err;
96101

97-
test_daemon();
98-
99-
test_waitsig();
100-
101-
ret = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
102+
ret = bpf_map_freeze(map_fd);
102103
if (ret) {
103-
pr_perror("Could not get map info");
104+
pr_perror("Could not freeze map");
104105
goto err;
105106
}
106-
107-
if (map_info.type != BPF_MAP_TYPE_HASH) {
108-
pr_err("Map type should be BPF_MAP_TYPE_HASH\n");
107+
108+
ret = bpf_obj_get_info_by_fd(map_fd, &old_map_info, &info_len);
109+
if (ret) {
110+
pr_perror("Could not get old map info");
109111
goto err;
110112
}
111-
if (map_info.key_size != sizeof(*keys)) {
112-
pr_err("Key size should be %zu\n", sizeof(*keys));
113+
114+
ret = parse_bpfmap_fdinfo(map_fd, &old_fdinfo, 8);
115+
if (ret) {
116+
pr_perror("Could not parse old map fdinfo from procfs");
113117
goto err;
114118
}
115-
if (map_info.value_size != sizeof(*values)) {
116-
pr_err("Value size should be %zu\n", sizeof(*values));
119+
120+
test_daemon();
121+
122+
test_waitsig();
123+
124+
ret = bpf_obj_get_info_by_fd(map_fd, &new_map_info, &info_len);
125+
if (ret) {
126+
pr_perror("Could not get new map info");
117127
goto err;
118128
}
119-
if (map_info.max_entries != max_entries) {
120-
pr_err("Max entries should be %d\n", max_entries);
129+
130+
ret = parse_bpfmap_fdinfo(map_fd, &new_fdinfo, 8);
131+
if (ret) {
132+
pr_perror("Could not parse new map fdinfo from procfs");
121133
goto err;
122134
}
123-
if (!(map_info.map_flags & BPF_F_NO_PREALLOC)) {
124-
pr_err("Map flag BPF_F_NO_PREALLOC should be set\n");
135+
136+
if (cmp_bpf_map_info(&old_map_info, &new_map_info)) {
137+
pr_err("bpf_map_info mismatch\n");
125138
goto err;
126139
}
127-
if (!(map_info.map_flags & BPF_F_NUMA_NODE)) {
128-
pr_err("Map flag BPF_F_NUMA_NODE should be set\n");
140+
141+
if (cmp_bpfmap_fdinfo(&old_fdinfo, &new_fdinfo)) {
142+
pr_err("bpfmap fdinfo mismatch\n");
129143
goto err;
130144
}
131145

test/zdtm/static/bpf_hash.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'flavor': 'h ns', 'flags': 'suid'}

0 commit comments

Comments
 (0)