1// SPDX-License-Identifier: GPL-2.0 2 3/* 4 * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH 5 * 6 * Author: Roberto Sassu <roberto.sassu@huawei.com> 7 */ 8 9#include <test_progs.h> 10 11#include "test_libbpf_get_fd_by_id_opts.skel.h" 12 13void test_libbpf_get_fd_by_id_opts(void) 14{ 15 struct test_libbpf_get_fd_by_id_opts *skel; 16 struct bpf_map_info info_m = {}; 17 __u32 len = sizeof(info_m), value; 18 int ret, zero = 0, fd = -1; 19 LIBBPF_OPTS(bpf_get_fd_by_id_opts, fd_opts_rdonly, 20 .open_flags = BPF_F_RDONLY, 21 ); 22 23 skel = test_libbpf_get_fd_by_id_opts__open_and_load(); 24 if (!ASSERT_OK_PTR(skel, 25 "test_libbpf_get_fd_by_id_opts__open_and_load")) 26 return; 27 28 ret = test_libbpf_get_fd_by_id_opts__attach(skel); 29 if (!ASSERT_OK(ret, "test_libbpf_get_fd_by_id_opts__attach")) 30 goto close_prog; 31 32 ret = bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.data_input), 33 &info_m, &len); 34 if (!ASSERT_OK(ret, "bpf_map_get_info_by_fd")) 35 goto close_prog; 36 37 fd = bpf_map_get_fd_by_id(info_m.id); 38 if (!ASSERT_LT(fd, 0, "bpf_map_get_fd_by_id")) 39 goto close_prog; 40 41 fd = bpf_map_get_fd_by_id_opts(info_m.id, NULL); 42 if (!ASSERT_LT(fd, 0, "bpf_map_get_fd_by_id_opts")) 43 goto close_prog; 44 45 fd = bpf_map_get_fd_by_id_opts(info_m.id, &fd_opts_rdonly); 46 if (!ASSERT_GE(fd, 0, "bpf_map_get_fd_by_id_opts")) 47 goto close_prog; 48 49 /* Map lookup should work with read-only fd. */ 50 ret = bpf_map_lookup_elem(fd, &zero, &value); 51 if (!ASSERT_OK(ret, "bpf_map_lookup_elem")) 52 goto close_prog; 53 54 if (!ASSERT_EQ(value, 0, "map value mismatch")) 55 goto close_prog; 56 57 /* Map update should not work with read-only fd. */ 58 ret = bpf_map_update_elem(fd, &zero, &len, BPF_ANY); 59 if (!ASSERT_LT(ret, 0, "bpf_map_update_elem")) 60 goto close_prog; 61 62 /* Map update should work with read-write fd. */ 63 ret = bpf_map_update_elem(bpf_map__fd(skel->maps.data_input), &zero, 64 &len, BPF_ANY); 65 if (!ASSERT_OK(ret, "bpf_map_update_elem")) 66 goto close_prog; 67 68 /* Prog get fd with opts set should not work (no kernel support). */ 69 ret = bpf_prog_get_fd_by_id_opts(0, &fd_opts_rdonly); 70 if (!ASSERT_EQ(ret, -EINVAL, "bpf_prog_get_fd_by_id_opts")) 71 goto close_prog; 72 73 /* Link get fd with opts set should not work (no kernel support). */ 74 ret = bpf_link_get_fd_by_id_opts(0, &fd_opts_rdonly); 75 if (!ASSERT_EQ(ret, -EINVAL, "bpf_link_get_fd_by_id_opts")) 76 goto close_prog; 77 78 /* BTF get fd with opts set should not work (no kernel support). */ 79 ret = bpf_btf_get_fd_by_id_opts(0, &fd_opts_rdonly); 80 ASSERT_EQ(ret, -EINVAL, "bpf_btf_get_fd_by_id_opts"); 81 82close_prog: 83 if (fd >= 0) 84 close(fd); 85 86 test_libbpf_get_fd_by_id_opts__destroy(skel); 87} 88