blob: 2613363f3789bdc822c573e8a77054a4f9dfea61 [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 Colitti32bd0712022-01-13 15:44:29 +090023#include <android/api-level.h>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090024#include <android-base/properties.h>
25#include <android-modules-utils/sdk_level.h>
Hungming Chen04569002022-02-16 16:02:49 +080026#include <bpf/BpfUtils.h>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090027
28#include <gtest/gtest.h>
29
30using std::find;
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090031using std::set;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090032using std::string;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090033
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000034using android::bpf::isAtLeastKernelVersion;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090035using android::modules::sdklevel::IsAtLeastR;
36using android::modules::sdklevel::IsAtLeastS;
37using android::modules::sdklevel::IsAtLeastT;
38
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 Żenczykowski6d116d02022-05-16 13:59:12 -070043#define NETD "/sys/fs/bpf/netd_shared/"
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090044
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090045class BpfExistenceTest : public ::testing::Test {
46};
47
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000048// Part of Android R platform, but mainlined in S
49static const set<string> PLATFORM_ONLY_IN_R = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090050 PLATFORM "map_offload_tether_ingress_map",
51 PLATFORM "map_offload_tether_limit_map",
52 PLATFORM "map_offload_tether_stats_map",
53 PLATFORM "prog_offload_schedcls_ingress_tether_ether",
54 PLATFORM "prog_offload_schedcls_ingress_tether_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090055};
56
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000057// Provided by *current* mainline module for S+ devices
58static const set<string> MAINLINE_FOR_S_PLUS = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090059 TETHERING "map_offload_tether_dev_map",
60 TETHERING "map_offload_tether_downstream4_map",
61 TETHERING "map_offload_tether_downstream64_map",
62 TETHERING "map_offload_tether_downstream6_map",
63 TETHERING "map_offload_tether_error_map",
64 TETHERING "map_offload_tether_limit_map",
65 TETHERING "map_offload_tether_stats_map",
66 TETHERING "map_offload_tether_upstream4_map",
67 TETHERING "map_offload_tether_upstream6_map",
Maciej Żenczykowski74b31ca2022-07-19 05:38:26 -070068 TETHERING "map_test_bitmap",
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090069 TETHERING "map_test_tether_downstream6_map",
70 TETHERING "prog_offload_schedcls_tether_downstream4_ether",
71 TETHERING "prog_offload_schedcls_tether_downstream4_rawip",
72 TETHERING "prog_offload_schedcls_tether_downstream6_ether",
73 TETHERING "prog_offload_schedcls_tether_downstream6_rawip",
74 TETHERING "prog_offload_schedcls_tether_upstream4_ether",
75 TETHERING "prog_offload_schedcls_tether_upstream4_rawip",
76 TETHERING "prog_offload_schedcls_tether_upstream6_ether",
77 TETHERING "prog_offload_schedcls_tether_upstream6_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090078};
79
Maciej Żenczykowski74b31ca2022-07-19 05:38:26 -070080// Provided by *current* mainline module for S+ devices with 5.10+ kernels
81static const set<string> MAINLINE_FOR_S_5_10_PLUS = {
82 TETHERING "prog_test_xdp_drop_ipv4_udp_ether",
83};
84
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +000085// Provided by *current* mainline module for T+ devices
86static const set<string> MAINLINE_FOR_T_PLUS = {
Maciej Żenczykowskie9d5ad02022-04-23 09:15:30 -070087 SHARED "map_block_blocked_ports_map",
88 SHARED "map_clatd_clat_egress4_map",
89 SHARED "map_clatd_clat_ingress6_map",
90 SHARED "map_dscp_policy_ipv4_dscp_policies_map",
91 SHARED "map_dscp_policy_ipv4_socket_to_policies_map_A",
92 SHARED "map_dscp_policy_ipv4_socket_to_policies_map_B",
93 SHARED "map_dscp_policy_ipv6_dscp_policies_map",
94 SHARED "map_dscp_policy_ipv6_socket_to_policies_map_A",
95 SHARED "map_dscp_policy_ipv6_socket_to_policies_map_B",
96 SHARED "map_dscp_policy_switch_comp_map",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -070097 NETD "map_netd_app_uid_stats_map",
98 NETD "map_netd_configuration_map",
99 NETD "map_netd_cookie_tag_map",
100 NETD "map_netd_iface_index_name_map",
101 NETD "map_netd_iface_stats_map",
102 NETD "map_netd_stats_map_A",
103 NETD "map_netd_stats_map_B",
104 NETD "map_netd_uid_counterset_map",
105 NETD "map_netd_uid_owner_map",
106 NETD "map_netd_uid_permission_map",
Maciej Żenczykowskie9d5ad02022-04-23 09:15:30 -0700107 SHARED "prog_clatd_schedcls_egress4_clat_ether",
108 SHARED "prog_clatd_schedcls_egress4_clat_rawip",
109 SHARED "prog_clatd_schedcls_ingress6_clat_ether",
110 SHARED "prog_clatd_schedcls_ingress6_clat_rawip",
Maciej Żenczykowski6d116d02022-05-16 13:59:12 -0700111 NETD "prog_netd_cgroupskb_egress_stats",
112 NETD "prog_netd_cgroupskb_ingress_stats",
113 NETD "prog_netd_cgroupsock_inet_create",
114 NETD "prog_netd_schedact_ingress_account",
115 NETD "prog_netd_skfilter_allowlist_xtbpf",
116 NETD "prog_netd_skfilter_denylist_xtbpf",
117 NETD "prog_netd_skfilter_egress_xtbpf",
118 NETD "prog_netd_skfilter_ingress_xtbpf",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900119};
120
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000121// Provided by *current* mainline module for T+ devices with 5.4+ kernels
122static const set<string> MAINLINE_FOR_T_5_4_PLUS = {
Maciej Żenczykowski93b8cc12022-05-18 23:00:36 +0000123 SHARED "prog_block_bind4_block_port",
124 SHARED "prog_block_bind6_block_port",
Maciej Żenczykowskib73da962022-07-14 08:32:14 +0000125};
126
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000127// Provided by *current* mainline module for T+ devices with 5.15+ kernels
128static const set<string> MAINLINE_FOR_T_5_15_PLUS = {
Maciej Żenczykowski93b8cc12022-05-18 23:00:36 +0000129 SHARED "prog_dscp_policy_schedcls_set_dscp_ether",
130 SHARED "prog_dscp_policy_schedcls_set_dscp_raw_ip",
131};
132
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900133void addAll(set<string>* a, const set<string>& b) {
134 a->insert(b.begin(), b.end());
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900135}
136
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000137#define DO_EXPECT(B, V) do { \
138 if (B) addAll(expected, (V)); else addAll(unexpected, (V)); \
139} while (0)
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900140
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900141void getFileLists(set<string>* expected, set<string>* unexpected) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900142 unexpected->clear();
143 expected->clear();
144
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000145 // We do not actually check the platform P/Q (netd) and Q (clatd) things
146 // and only verify the mainline module relevant R+ offload maps & progs.
147 //
148 // The goal of this test is to verify compatibility with the tethering mainline module,
149 // and not to test the platform itself, which may have been modified by vendor or oems,
150 // so we should only test for the removal of stuff that was mainline'd,
151 // and for the presence of mainline stuff.
152 DO_EXPECT(IsAtLeastR() && !IsAtLeastS(), PLATFORM_ONLY_IN_R);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900153
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000154 DO_EXPECT(IsAtLeastS(), MAINLINE_FOR_S_PLUS);
Maciej Żenczykowski74b31ca2022-07-19 05:38:26 -0700155 DO_EXPECT(IsAtLeastS() && isAtLeastKernelVersion(5, 10, 0), MAINLINE_FOR_S_5_10_PLUS);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900156
157 // Nothing added or removed in SCv2.
158
Maciej Żenczykowskie9e77412022-07-16 20:10:16 +0000159 DO_EXPECT(IsAtLeastT(), MAINLINE_FOR_T_PLUS);
160 DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(5, 4, 0), MAINLINE_FOR_T_5_4_PLUS);
161 DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(5, 15, 0), MAINLINE_FOR_T_5_15_PLUS);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900162}
163
164void checkFiles() {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900165 set<string> mustExist;
166 set<string> mustNotExist;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900167
168 getFileLists(&mustExist, &mustNotExist);
169
170 for (const auto& file : mustExist) {
171 EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist";
172 }
173 for (const auto& file : mustNotExist) {
174 int ret = access(file.c_str(), R_OK);
175 int err = errno;
176 EXPECT_EQ(-1, ret) << file << " unexpectedly exists";
177 if (ret == -1) {
178 EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err;
179 }
180 }
181}
182
183TEST_F(BpfExistenceTest, TestPrograms) {
Hungming Chen04569002022-02-16 16:02:49 +0800184 SKIP_IF_BPF_NOT_SUPPORTED;
185
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900186 // Pre-flight check to ensure test has been updated.
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900187 uint64_t buildVersionSdk = android_get_device_api_level();
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900188 ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version";
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900189 if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900190 FAIL() << "Unknown OS version " << buildVersionSdk << ", please update this test";
191 }
192
193 // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf.
194 ASSERT_EQ(0, getuid()) << "This test must run as root.";
195
196 checkFiles();
197}