xref: /aosp_15_r20/external/bcc/tests/cc/test_pinned_table.cc (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 /*
2  * Copyright (c) 2019 Kinvolk GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <linux/version.h>
18 #include <unistd.h>
19 #include <string>
20 #include <sys/mount.h>
21 
22 #include "BPF.h"
23 #include "catch.hpp"
24 
25 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
26 TEST_CASE("test pinned table", "[pinned_table]") {
27   bool mounted = false;
28   if (system("mount | grep /sys/fs/bpf")) {
29     REQUIRE(system("mkdir -p /sys/fs/bpf") == 0);
30     REQUIRE(system("mount -o nosuid,nodev,noexec,mode=700 -t bpf bpf /sys/fs/bpf") == 0);
31     mounted = true;
32   }
33   // prepare test by pinning table to bpffs
34   {
35     const std::string BPF_PROGRAM = R"(
36       BPF_TABLE("hash", u64, u64, ids, 1024);
37     )";
38 
39     ebpf::BPF bpf;
40     ebpf::StatusTuple res(0);
41     res = bpf.init(BPF_PROGRAM);
42     REQUIRE(res.ok());
43 
44     REQUIRE(bpf_obj_pin(bpf.get_hash_table<int, int>("ids").get_fd(), "/sys/fs/bpf/test_pinned_table") == 0);
45   }
46 
47   // test table access
48   {
49     const std::string BPF_PROGRAM = R"(
50       BPF_TABLE_PINNED("hash", u64, u64, ids, 0, "/sys/fs/bpf/test_pinned_table", BPF_F_NO_PREALLOC);
51     )";
52 
53     ebpf::BPF bpf;
54     ebpf::StatusTuple res(0);
55     res = bpf.init(BPF_PROGRAM);
56     unlink("/sys/fs/bpf/test_pinned_table"); // can delete table here already
57     REQUIRE(res.ok());
58 
59     auto t = bpf.get_hash_table<int, int>("ids");
60     int key, value;
61 
62     // write element
63     key = 0x08;
64     value = 0x43;
65     res = t.update_value(key, value);
66     REQUIRE(res.ok());
67     REQUIRE(t[key] == value);
68   }
69 
70   // test create if not exist
71   {
72     const std::string BPF_PROGRAM = R"(
73       BPF_TABLE_PINNED("hash", u64, u64, ids, 1024, "/sys/fs/bpf/test_pinned_table");
74     )";
75 
76     ebpf::BPF bpf;
77     ebpf::StatusTuple res(0);
78     res = bpf.init(BPF_PROGRAM);
79     REQUIRE(res.ok());
80     unlink("/sys/fs/bpf/test_pinned_table");
81   }
82 
83   if (mounted) {
84     REQUIRE(umount("/sys/fs/bpf") == 0);
85   }
86 }
87 #endif
88 
89 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
90 TEST_CASE("test pinned sk_storage table", "[pinned_sk_storage_table]") {
91   bool mounted = false;
92   if (system("mount | grep /sys/fs/bpf")) {
93     REQUIRE(system("mkdir -p /sys/fs/bpf") == 0);
94     REQUIRE(system("mount -o nosuid,nodev,noexec,mode=700 -t bpf bpf /sys/fs/bpf") == 0);
95     mounted = true;
96   }
97   // prepare test by pinning table to bpffs
98   {
99     const std::string BPF_PROGRAM = R"(
100       BPF_SK_STORAGE(sk_stg, __u64);
101       int test(struct __sk_buff *skb) { return 0; }
102     )";
103 
104     ebpf::BPF bpf;
105     ebpf::StatusTuple res(0);
106     res = bpf.init(BPF_PROGRAM);
107     REQUIRE(res.ok());
108 
109     REQUIRE(bpf_obj_pin(bpf.get_sk_storage_table<unsigned long long>("sk_stg").get_fd(), "/sys/fs/bpf/test_pinned_table") == 0);
110   }
111 
112   // exercise <pinned_map>.sk_storage_get().
113   {
114     const std::string BPF_PROGRAM = R"(
115       BPF_TABLE_PINNED("sk_storage", __u32, __u64, sk_stg, 0, "/sys/fs/bpf/test_pinned_table");
116       int test(struct __sk_buff *skb) {
117         struct bpf_sock *sk;
118         __u64 *val;
119 
120         sk = skb->sk;
121         if (!sk)
122           return 0;
123         sk = bpf_sk_fullsock(sk);
124         if (!sk)
125           return 0;
126 
127         val = sk_stg.sk_storage_get(sk, NULL, BPF_SK_STORAGE_GET_F_CREATE);
128         if (!val)
129           return 0;
130 
131         return 1;
132       }
133     )";
134 
135     ebpf::BPF bpf;
136     ebpf::StatusTuple res(0);
137     res = bpf.init(BPF_PROGRAM);
138     REQUIRE(res.ok());
139     int prog_fd;
140     res = bpf.load_func("test", BPF_PROG_TYPE_CGROUP_SKB, prog_fd);
141     REQUIRE(res.ok());
142   }
143 
144   unlink("/sys/fs/bpf/test_pinned_table");
145   if (mounted) {
146     REQUIRE(umount("/sys/fs/bpf") == 0);
147   }
148 }
149 #endif
150