blob: 51a4eca1a2782725ced2cf0e1787ba22f60d5f2b [file] [log] [blame]
Lorenzo Colitti3b38b122022-01-12 16:06:07 +09001/*
2 * Copyright 2022 The Android Open Source Project
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 *
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090016 * bpf_existence_test.cpp - checks that the device has expected BPF programs and maps
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090017 */
18
19#include <cstdint>
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090020#include <set>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090021#include <string>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090022
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090023#include <android-modules-utils/sdk_level.h>
Hungming Chen04569002022-02-16 16:02:49 +080024#include <bpf/BpfUtils.h>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090025
26#include <gtest/gtest.h>
27
28using std::find;
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090029using std::set;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090030using std::string;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090031
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000032using android::bpf::isAtLeastKernelVersion;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090033using android::modules::sdklevel::IsAtLeastR;
34using android::modules::sdklevel::IsAtLeastS;
35using android::modules::sdklevel::IsAtLeastT;
Maciej Żenczykowskic71dfae2023-08-09 21:48:28 +000036using android::modules::sdklevel::IsAtLeastU;
37using android::modules::sdklevel::IsAtLeastV;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090038
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090039#define PLATFORM "/sys/fs/bpf/"
40#define TETHERING "/sys/fs/bpf/tethering/"
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -070041#define PRIVATE "/sys/fs/bpf/net_private/"
Maciej Żenczykowski0736d7b2022-04-21 06:42:17 -070042#define SHARED "/sys/fs/bpf/net_shared/"
Maciej Żenczykowski3cb494f2023-10-04 21:35:41 +000043#define NETD_RO "/sys/fs/bpf/netd_readonly/"
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -070044#define NETD "/sys/fs/bpf/netd_shared/"
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090045
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090046class BpfExistenceTest : public ::testing::Test {
47};
48
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +000049// Part of Android R platform (for 4.9+), but mainlined in S
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000050static const set<string> PLATFORM_ONLY_IN_R = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090051 PLATFORM "map_offload_tether_ingress_map",
52 PLATFORM "map_offload_tether_limit_map",
53 PLATFORM "map_offload_tether_stats_map",
54 PLATFORM "prog_offload_schedcls_ingress_tether_ether",
55 PLATFORM "prog_offload_schedcls_ingress_tether_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090056};
57
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000058// Provided by *current* mainline module for S+ devices
59static const set<string> MAINLINE_FOR_S_PLUS = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090060 TETHERING "map_offload_tether_dev_map",
61 TETHERING "map_offload_tether_downstream4_map",
62 TETHERING "map_offload_tether_downstream64_map",
63 TETHERING "map_offload_tether_downstream6_map",
64 TETHERING "map_offload_tether_error_map",
65 TETHERING "map_offload_tether_limit_map",
66 TETHERING "map_offload_tether_stats_map",
67 TETHERING "map_offload_tether_upstream4_map",
68 TETHERING "map_offload_tether_upstream6_map",
Maciej Żenczykowski74b31ca2022-07-19 05:38:26 -070069 TETHERING "map_test_bitmap",
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090070 TETHERING "map_test_tether_downstream6_map",
71 TETHERING "prog_offload_schedcls_tether_downstream4_ether",
72 TETHERING "prog_offload_schedcls_tether_downstream4_rawip",
73 TETHERING "prog_offload_schedcls_tether_downstream6_ether",
74 TETHERING "prog_offload_schedcls_tether_downstream6_rawip",
75 TETHERING "prog_offload_schedcls_tether_upstream4_ether",
76 TETHERING "prog_offload_schedcls_tether_upstream4_rawip",
77 TETHERING "prog_offload_schedcls_tether_upstream6_ether",
78 TETHERING "prog_offload_schedcls_tether_upstream6_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090079};
80
Maciej Żenczykowski74b31ca2022-07-19 05:38:26 -070081// Provided by *current* mainline module for S+ devices with 5.10+ kernels
82static const set<string> MAINLINE_FOR_S_5_10_PLUS = {
83 TETHERING "prog_test_xdp_drop_ipv4_udp_ether",
84};
85
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000086// Provided by *current* mainline module for T+ devices
87static const set<string> MAINLINE_FOR_T_PLUS = {
Maciej Żenczykowskie9d5ad02022-04-23 09:15:30 -070088 SHARED "map_block_blocked_ports_map",
89 SHARED "map_clatd_clat_egress4_map",
90 SHARED "map_clatd_clat_ingress6_map",
Ken Chen74ff3ee2022-07-14 16:46:39 +080091 SHARED "map_dscpPolicy_ipv4_dscp_policies_map",
Ken Chen74ff3ee2022-07-14 16:46:39 +080092 SHARED "map_dscpPolicy_ipv6_dscp_policies_map",
Maciej Żenczykowskif75b7e12022-07-27 12:56:01 +000093 SHARED "map_dscpPolicy_socket_policy_cache_map",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -070094 NETD "map_netd_app_uid_stats_map",
95 NETD "map_netd_configuration_map",
96 NETD "map_netd_cookie_tag_map",
Ken Chen24330172023-10-20 13:02:14 +080097 NETD "map_netd_data_saver_enabled_map",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -070098 NETD "map_netd_iface_index_name_map",
99 NETD "map_netd_iface_stats_map",
Maciej Żenczykowski6109d942023-08-29 18:39:28 +0000100 NETD "map_netd_ingress_discard_map",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -0700101 NETD "map_netd_stats_map_A",
102 NETD "map_netd_stats_map_B",
103 NETD "map_netd_uid_counterset_map",
104 NETD "map_netd_uid_owner_map",
105 NETD "map_netd_uid_permission_map",
Maciej Żenczykowskie9d5ad02022-04-23 09:15:30 -0700106 SHARED "prog_clatd_schedcls_egress4_clat_rawip",
107 SHARED "prog_clatd_schedcls_ingress6_clat_ether",
108 SHARED "prog_clatd_schedcls_ingress6_clat_rawip",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -0700109 NETD "prog_netd_cgroupskb_egress_stats",
110 NETD "prog_netd_cgroupskb_ingress_stats",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -0700111 NETD "prog_netd_schedact_ingress_account",
112 NETD "prog_netd_skfilter_allowlist_xtbpf",
113 NETD "prog_netd_skfilter_denylist_xtbpf",
114 NETD "prog_netd_skfilter_egress_xtbpf",
115 NETD "prog_netd_skfilter_ingress_xtbpf",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900116};
117
Lorenzo Colitti3505b582022-10-27 19:36:27 +0900118// Provided by *current* mainline module for T+ devices with 4.14+ kernels
119static const set<string> MAINLINE_FOR_T_4_14_PLUS = {
120 NETD "prog_netd_cgroupsock_inet_create",
121};
122
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000123// Provided by *current* mainline module for T+ devices with 5.4+ kernels
Maciej Żenczykowski3cb494f2023-10-04 21:35:41 +0000124static const set<string> MAINLINE_FOR_T_4_19_PLUS = {
125 NETD_RO "prog_block_bind4_block_port",
126 NETD_RO "prog_block_bind6_block_port",
Maciej Żenczykowskib73da962022-07-14 08:32:14 +0000127};
128
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000129// Provided by *current* mainline module for T+ devices with 5.15+ kernels
130static const set<string> MAINLINE_FOR_T_5_15_PLUS = {
Ken Chen74ff3ee2022-07-14 16:46:39 +0800131 SHARED "prog_dscpPolicy_schedcls_set_dscp_ether",
Maciej Żenczykowski93b8cc12022-05-18 23:00:36 +0000132};
133
Ryan Zuklie9fb8f182023-09-28 15:50:59 -0700134// Provided by *current* mainline module for U+ devices
135static const set<string> MAINLINE_FOR_U_PLUS = {
136 NETD "map_netd_packet_trace_enabled_map",
137};
138
139// Provided by *current* mainline module for U+ devices with 5.10+ kernels
140static const set<string> MAINLINE_FOR_U_5_10_PLUS = {
141 NETD "map_netd_packet_trace_ringbuf",
142};
143
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +0000144static void addAll(set<string>& a, const set<string>& b) {
145 a.insert(b.begin(), b.end());
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900146}
147
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +0000148#define DO_EXPECT(B, V) addAll((B) ? mustExist : mustNotExist, (V))
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900149
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +0000150TEST_F(BpfExistenceTest, TestPrograms) {
151 // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf.
152 ASSERT_EQ(0, getuid()) << "This test must run as root.";
153
154 set<string> mustExist;
155 set<string> mustNotExist;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900156
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000157 // We do not actually check the platform P/Q (netd) and Q (clatd) things
158 // and only verify the mainline module relevant R+ offload maps & progs.
159 //
160 // The goal of this test is to verify compatibility with the tethering mainline module,
161 // and not to test the platform itself, which may have been modified by vendor or oems,
162 // so we should only test for the removal of stuff that was mainline'd,
163 // and for the presence of mainline stuff.
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900164
Maciej Żenczykowskic71dfae2023-08-09 21:48:28 +0000165 // Note: Q is no longer supported by mainline
166 ASSERT_TRUE(IsAtLeastR());
167
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +0000168 // R can potentially run on pre-4.9 kernel non-eBPF capable devices.
169 DO_EXPECT(IsAtLeastR() && !IsAtLeastS() && isAtLeastKernelVersion(4, 9, 0), PLATFORM_ONLY_IN_R);
170
171 // S requires Linux Kernel 4.9+ and thus requires eBPF support.
Maciej Żenczykowskic71dfae2023-08-09 21:48:28 +0000172 if (IsAtLeastS()) ASSERT_TRUE(isAtLeastKernelVersion(4, 9, 0));
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000173 DO_EXPECT(IsAtLeastS(), MAINLINE_FOR_S_PLUS);
Maciej Żenczykowski74b31ca2022-07-19 05:38:26 -0700174 DO_EXPECT(IsAtLeastS() && isAtLeastKernelVersion(5, 10, 0), MAINLINE_FOR_S_5_10_PLUS);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900175
176 // Nothing added or removed in SCv2.
177
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +0000178 // T still only requires Linux Kernel 4.9+.
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000179 DO_EXPECT(IsAtLeastT(), MAINLINE_FOR_T_PLUS);
Lorenzo Colitti3505b582022-10-27 19:36:27 +0900180 DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(4, 14, 0), MAINLINE_FOR_T_4_14_PLUS);
Maciej Żenczykowski3cb494f2023-10-04 21:35:41 +0000181 DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(4, 19, 0), MAINLINE_FOR_T_4_19_PLUS);
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000182 DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(5, 15, 0), MAINLINE_FOR_T_5_15_PLUS);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900183
Maciej Żenczykowski2e4c7522022-12-18 11:34:42 +0000184 // U requires Linux Kernel 4.14+, but nothing (as yet) added or removed in U.
Maciej Żenczykowskic71dfae2023-08-09 21:48:28 +0000185 if (IsAtLeastU()) ASSERT_TRUE(isAtLeastKernelVersion(4, 14, 0));
Ryan Zuklie9fb8f182023-09-28 15:50:59 -0700186 DO_EXPECT(IsAtLeastU(), MAINLINE_FOR_U_PLUS);
187 DO_EXPECT(IsAtLeastU() && isAtLeastKernelVersion(5, 10, 0), MAINLINE_FOR_U_5_10_PLUS);
Maciej Żenczykowskic71dfae2023-08-09 21:48:28 +0000188
189 // V requires Linux Kernel 4.19+, but nothing (as yet) added or removed in V.
190 if (IsAtLeastV()) ASSERT_TRUE(isAtLeastKernelVersion(4, 19, 0));
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900191
192 for (const auto& file : mustExist) {
193 EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist";
194 }
195 for (const auto& file : mustNotExist) {
196 int ret = access(file.c_str(), R_OK);
197 int err = errno;
198 EXPECT_EQ(-1, ret) << file << " unexpectedly exists";
199 if (ret == -1) {
200 EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err;
201 }
202 }
203}