1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <test_progs.h>
#include <bpf/btf.h>
#include "access_map_in_map.skel.h"
#include "update_map_in_htab.skel.h"
struct thread_ctx {
pthread_barrier_t barrier;
int outer_map_fd;
int start, abort;
int loop, err;
};
static int wait_for_start_or_abort(struct thread_ctx *ctx)
{
while (!ctx->start && !ctx->abort)
usleep(1);
return ctx->abort ? -1 : 0;
}
static void *update_map_fn(void *data)
{
struct thread_ctx *ctx = data;
int loop = ctx->loop, err = 0;
if (wait_for_start_or_abort(ctx) < 0)
return NULL;
pthread_barrier_wait(&ctx->barrier);
while (loop-- > 0) {
int fd, zero = 0;
fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 4, 1, NULL);
if (fd < 0) {
err |= 1;
pthread_barrier_wait(&ctx->barrier);
continue;
}
/* Remove the old inner map */
if (bpf_map_update_elem(ctx->outer_map_fd, &zero, &fd, 0) < 0)
err |= 2;
close(fd);
pthread_barrier_wait(&ctx->barrier);
}
ctx->err = err;
return NULL;
}
static void *access_map_fn(void *data)
{
struct thread_ctx *ctx = data;
int loop = ctx->loop;
if (wait_for_start_or_abort(ctx) < 0)
return NULL;
pthread_barrier_wait(&ctx->barrier);
while (loop-- > 0) {
/* Access the old inner map */
syscall(SYS_getpgid);
pthread_barrier_wait(&ctx->barrier);
}
return NULL;
}
static void test_map_in_map_access(const char *prog_name, const char *map_name)
{
struct access_map_in_map *skel;
struct bpf_map *outer_map;
struct bpf_program *prog;
struct thread_ctx ctx;
pthread_t tid[2];
int err;
skel = access_map_in_map__open();
if (!ASSERT_OK_PTR(skel, "access_map_in_map open"))
return;
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
if (!ASSERT_OK_PTR(prog, "find program"))
goto out;
bpf_program__set_autoload(prog, true);
outer_map = bpf_object__find_map_by_name(skel->obj, map_name);
if (!ASSERT_OK_PTR(outer_map, "find map"))
goto out;
err = access_map_in_map__load(skel);
if (!ASSERT_OK(err, "access_map_in_map load"))
goto out;
err = access_map_in_map__attach(skel);
if (!ASSERT_OK(err, "access_map_in_map attach"))
goto out;
skel->bss->tgid = getpid();
memset(&ctx, 0, sizeof(ctx));
pthread_barrier_init(&ctx.barrier, NULL, 2);
ctx.outer_map_fd = bpf_map__fd(outer_map);
ctx.loop = 4;
err = pthread_create(&tid[0], NULL, update_map_fn, &ctx);
if (!ASSERT_OK(err, "close_thread"))
goto out;
err = pthread_create(&tid[1], NULL, access_map_fn, &ctx);
if (!ASSERT_OK(err, "read_thread")) {
ctx.abort = 1;
pthread_join(tid[0], NULL);
goto out;
}
ctx.start = 1;
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
ASSERT_OK(ctx.err, "err");
out:
access_map_in_map__destroy(skel);
}
static void add_del_fd_htab(int outer_fd)
{
int inner_fd, err;
int key = 1;
inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr1", 4, 4, 1, NULL);
if (!ASSERT_OK_FD(inner_fd, "inner1"))
return;
err = bpf_map_update_elem(outer_fd, &key, &inner_fd, BPF_NOEXIST);
close(inner_fd);
if (!ASSERT_OK(err, "add"))
return;
/* Delete */
err = bpf_map_delete_elem(outer_fd, &key);
ASSERT_OK(err, "del");
}
static void overwrite_fd_htab(int outer_fd)
{
int inner_fd, err;
int key = 1;
inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr1", 4, 4, 1, NULL);
if (!ASSERT_OK_FD(inner_fd, "inner1"))
return;
err = bpf_map_update_elem(outer_fd, &key, &inner_fd, BPF_NOEXIST);
close(inner_fd);
if (!ASSERT_OK(err, "add"))
return;
/* Overwrite */
inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr2", 4, 4, 1, NULL);
if (!ASSERT_OK_FD(inner_fd, "inner2"))
goto out;
err = bpf_map_update_elem(outer_fd, &key, &inner_fd, BPF_EXIST);
close(inner_fd);
if (!ASSERT_OK(err, "overwrite"))
goto out;
err = bpf_map_delete_elem(outer_fd, &key);
ASSERT_OK(err, "del");
return;
out:
bpf_map_delete_elem(outer_fd, &key);
}
static void lookup_delete_fd_htab(int outer_fd)
{
int key = 1, value;
int inner_fd, err;
inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr1", 4, 4, 1, NULL);
if (!ASSERT_OK_FD(inner_fd, "inner1"))
return;
err = bpf_map_update_elem(outer_fd, &key, &inner_fd, BPF_NOEXIST);
close(inner_fd);
if (!ASSERT_OK(err, "add"))
return;
/* lookup_and_delete is not supported for htab of maps */
err = bpf_map_lookup_and_delete_elem(outer_fd, &key, &value);
ASSERT_EQ(err, -ENOTSUPP, "lookup_del");
err = bpf_map_delete_elem(outer_fd, &key);
ASSERT_OK(err, "del");
}
static void batched_lookup_delete_fd_htab(int outer_fd)
{
int keys[2] = {1, 2}, values[2];
unsigned int cnt, batch;
int inner_fd, err;
inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr1", 4, 4, 1, NULL);
if (!ASSERT_OK_FD(inner_fd, "inner1"))
return;
err = bpf_map_update_elem(outer_fd, &keys[0], &inner_fd, BPF_NOEXIST);
close(inner_fd);
if (!ASSERT_OK(err, "add1"))
return;
inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "arr2", 4, 4, 1, NULL);
if (!ASSERT_OK_FD(inner_fd, "inner2"))
goto out;
err = bpf_map_update_elem(outer_fd, &keys[1], &inner_fd, BPF_NOEXIST);
close(inner_fd);
if (!ASSERT_OK(err, "add2"))
goto out;
/* batched lookup_and_delete */
cnt = ARRAY_SIZE(keys);
err = bpf_map_lookup_and_delete_batch(outer_fd, NULL, &batch, keys, values, &cnt, NULL);
ASSERT_TRUE((!err || err == -ENOENT), "delete_batch ret");
ASSERT_EQ(cnt, ARRAY_SIZE(keys), "delete_batch cnt");
out:
bpf_map_delete_elem(outer_fd, &keys[0]);
}
static void test_update_map_in_htab(bool preallocate)
{
struct update_map_in_htab *skel;
int err, fd;
skel = update_map_in_htab__open();
if (!ASSERT_OK_PTR(skel, "open"))
return;
err = update_map_in_htab__load(skel);
if (!ASSERT_OK(err, "load"))
goto out;
fd = preallocate ? bpf_map__fd(skel->maps.outer_htab_map) :
bpf_map__fd(skel->maps.outer_alloc_htab_map);
add_del_fd_htab(fd);
overwrite_fd_htab(fd);
lookup_delete_fd_htab(fd);
batched_lookup_delete_fd_htab(fd);
out:
update_map_in_htab__destroy(skel);
}
void test_map_in_map(void)
{
if (test__start_subtest("acc_map_in_array"))
test_map_in_map_access("access_map_in_array", "outer_array_map");
if (test__start_subtest("sleepable_acc_map_in_array"))
test_map_in_map_access("sleepable_access_map_in_array", "outer_array_map");
if (test__start_subtest("acc_map_in_htab"))
test_map_in_map_access("access_map_in_htab", "outer_htab_map");
if (test__start_subtest("sleepable_acc_map_in_htab"))
test_map_in_map_access("sleepable_access_map_in_htab", "outer_htab_map");
if (test__start_subtest("update_map_in_htab"))
test_update_map_in_htab(true);
if (test__start_subtest("update_map_in_alloc_htab"))
test_update_map_in_htab(false);
}
|