Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 1 | /* |
| 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 Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 16 | * bpf_existence_test.cpp - checks that the device has expected BPF programs and maps |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <cstdint> |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 20 | #include <set> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 21 | #include <string> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 22 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 23 | #include <android/api-level.h> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 24 | #include <android-base/properties.h> |
| 25 | #include <android-modules-utils/sdk_level.h> |
Hungming Chen | 0456900 | 2022-02-16 16:02:49 +0800 | [diff] [blame] | 26 | #include <bpf/BpfUtils.h> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | using std::find; |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 31 | using std::set; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 32 | using std::string; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 33 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 34 | using android::bpf::isAtLeastKernelVersion; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 35 | using android::modules::sdklevel::IsAtLeastR; |
| 36 | using android::modules::sdklevel::IsAtLeastS; |
| 37 | using android::modules::sdklevel::IsAtLeastT; |
| 38 | |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 39 | #define PLATFORM "/sys/fs/bpf/" |
| 40 | #define TETHERING "/sys/fs/bpf/tethering/" |
Maciej Żenczykowski | 6d116d0 | 2022-05-16 13:59:12 -0700 | [diff] [blame] | 41 | #define PRIVATE "/sys/fs/bpf/net_private/" |
Maciej Żenczykowski | 0736d7b | 2022-04-21 06:42:17 -0700 | [diff] [blame] | 42 | #define SHARED "/sys/fs/bpf/net_shared/" |
Maciej Żenczykowski | 6d116d0 | 2022-05-16 13:59:12 -0700 | [diff] [blame] | 43 | #define NETD "/sys/fs/bpf/netd_shared/" |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 44 | |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 45 | class BpfExistenceTest : public ::testing::Test { |
| 46 | }; |
| 47 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 48 | // Part of Android R platform, but mainlined in S |
| 49 | static const set<string> PLATFORM_ONLY_IN_R = { |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 50 | 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 Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 55 | }; |
| 56 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 57 | // Provided by *current* mainline module for S+ devices |
| 58 | static const set<string> MAINLINE_FOR_S_PLUS = { |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 59 | 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", |
| 68 | TETHERING "map_test_tether_downstream6_map", |
| 69 | TETHERING "prog_offload_schedcls_tether_downstream4_ether", |
| 70 | TETHERING "prog_offload_schedcls_tether_downstream4_rawip", |
| 71 | TETHERING "prog_offload_schedcls_tether_downstream6_ether", |
| 72 | TETHERING "prog_offload_schedcls_tether_downstream6_rawip", |
| 73 | TETHERING "prog_offload_schedcls_tether_upstream4_ether", |
| 74 | TETHERING "prog_offload_schedcls_tether_upstream4_rawip", |
| 75 | TETHERING "prog_offload_schedcls_tether_upstream6_ether", |
| 76 | TETHERING "prog_offload_schedcls_tether_upstream6_rawip", |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 77 | }; |
| 78 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 79 | // Provided by *current* mainline module for T+ devices |
| 80 | static const set<string> MAINLINE_FOR_T_PLUS = { |
Maciej Żenczykowski | e9d5ad0 | 2022-04-23 09:15:30 -0700 | [diff] [blame] | 81 | SHARED "map_block_blocked_ports_map", |
| 82 | SHARED "map_clatd_clat_egress4_map", |
| 83 | SHARED "map_clatd_clat_ingress6_map", |
| 84 | SHARED "map_dscp_policy_ipv4_dscp_policies_map", |
| 85 | SHARED "map_dscp_policy_ipv4_socket_to_policies_map_A", |
| 86 | SHARED "map_dscp_policy_ipv4_socket_to_policies_map_B", |
| 87 | SHARED "map_dscp_policy_ipv6_dscp_policies_map", |
| 88 | SHARED "map_dscp_policy_ipv6_socket_to_policies_map_A", |
| 89 | SHARED "map_dscp_policy_ipv6_socket_to_policies_map_B", |
| 90 | SHARED "map_dscp_policy_switch_comp_map", |
Maciej Żenczykowski | 6d116d0 | 2022-05-16 13:59:12 -0700 | [diff] [blame] | 91 | NETD "map_netd_app_uid_stats_map", |
| 92 | NETD "map_netd_configuration_map", |
| 93 | NETD "map_netd_cookie_tag_map", |
| 94 | NETD "map_netd_iface_index_name_map", |
| 95 | NETD "map_netd_iface_stats_map", |
| 96 | NETD "map_netd_stats_map_A", |
| 97 | NETD "map_netd_stats_map_B", |
| 98 | NETD "map_netd_uid_counterset_map", |
| 99 | NETD "map_netd_uid_owner_map", |
| 100 | NETD "map_netd_uid_permission_map", |
Maciej Żenczykowski | e9d5ad0 | 2022-04-23 09:15:30 -0700 | [diff] [blame] | 101 | SHARED "prog_clatd_schedcls_egress4_clat_ether", |
| 102 | SHARED "prog_clatd_schedcls_egress4_clat_rawip", |
| 103 | SHARED "prog_clatd_schedcls_ingress6_clat_ether", |
| 104 | SHARED "prog_clatd_schedcls_ingress6_clat_rawip", |
Maciej Żenczykowski | 6d116d0 | 2022-05-16 13:59:12 -0700 | [diff] [blame] | 105 | NETD "prog_netd_cgroupskb_egress_stats", |
| 106 | NETD "prog_netd_cgroupskb_ingress_stats", |
| 107 | NETD "prog_netd_cgroupsock_inet_create", |
| 108 | NETD "prog_netd_schedact_ingress_account", |
| 109 | NETD "prog_netd_skfilter_allowlist_xtbpf", |
| 110 | NETD "prog_netd_skfilter_denylist_xtbpf", |
| 111 | NETD "prog_netd_skfilter_egress_xtbpf", |
| 112 | NETD "prog_netd_skfilter_ingress_xtbpf", |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 113 | }; |
| 114 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 115 | // Provided by *current* mainline module for T+ devices with 5.4+ kernels |
| 116 | static const set<string> MAINLINE_FOR_T_5_4_PLUS = { |
Maciej Żenczykowski | 93b8cc1 | 2022-05-18 23:00:36 +0000 | [diff] [blame] | 117 | SHARED "prog_block_bind4_block_port", |
| 118 | SHARED "prog_block_bind6_block_port", |
Maciej Żenczykowski | b73da96 | 2022-07-14 08:32:14 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 121 | // Provided by *current* mainline module for T+ devices with 5.15+ kernels |
| 122 | static const set<string> MAINLINE_FOR_T_5_15_PLUS = { |
Maciej Żenczykowski | 93b8cc1 | 2022-05-18 23:00:36 +0000 | [diff] [blame] | 123 | SHARED "prog_dscp_policy_schedcls_set_dscp_ether", |
| 124 | SHARED "prog_dscp_policy_schedcls_set_dscp_raw_ip", |
| 125 | }; |
| 126 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 127 | void addAll(set<string>* a, const set<string>& b) { |
| 128 | a->insert(b.begin(), b.end()); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 131 | #define DO_EXPECT(B, V) do { \ |
| 132 | if (B) addAll(expected, (V)); else addAll(unexpected, (V)); \ |
| 133 | } while (0) |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 134 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 135 | void getFileLists(set<string>* expected, set<string>* unexpected) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 136 | unexpected->clear(); |
| 137 | expected->clear(); |
| 138 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 139 | // We do not actually check the platform P/Q (netd) and Q (clatd) things |
| 140 | // and only verify the mainline module relevant R+ offload maps & progs. |
| 141 | // |
| 142 | // The goal of this test is to verify compatibility with the tethering mainline module, |
| 143 | // and not to test the platform itself, which may have been modified by vendor or oems, |
| 144 | // so we should only test for the removal of stuff that was mainline'd, |
| 145 | // and for the presence of mainline stuff. |
| 146 | DO_EXPECT(IsAtLeastR() && !IsAtLeastS(), PLATFORM_ONLY_IN_R); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 147 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 148 | DO_EXPECT(IsAtLeastS(), MAINLINE_FOR_S_PLUS); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 149 | |
| 150 | // Nothing added or removed in SCv2. |
| 151 | |
Maciej Żenczykowski | e9e7741 | 2022-07-16 20:10:16 +0000 | [diff] [blame^] | 152 | DO_EXPECT(IsAtLeastT(), MAINLINE_FOR_T_PLUS); |
| 153 | DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(5, 4, 0), MAINLINE_FOR_T_5_4_PLUS); |
| 154 | DO_EXPECT(IsAtLeastT() && isAtLeastKernelVersion(5, 15, 0), MAINLINE_FOR_T_5_15_PLUS); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void checkFiles() { |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 158 | set<string> mustExist; |
| 159 | set<string> mustNotExist; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 160 | |
| 161 | getFileLists(&mustExist, &mustNotExist); |
| 162 | |
| 163 | for (const auto& file : mustExist) { |
| 164 | EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist"; |
| 165 | } |
| 166 | for (const auto& file : mustNotExist) { |
| 167 | int ret = access(file.c_str(), R_OK); |
| 168 | int err = errno; |
| 169 | EXPECT_EQ(-1, ret) << file << " unexpectedly exists"; |
| 170 | if (ret == -1) { |
| 171 | EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | TEST_F(BpfExistenceTest, TestPrograms) { |
Hungming Chen | 0456900 | 2022-02-16 16:02:49 +0800 | [diff] [blame] | 177 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 178 | |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 179 | // Pre-flight check to ensure test has been updated. |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 180 | uint64_t buildVersionSdk = android_get_device_api_level(); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 181 | ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version"; |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 182 | if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 183 | FAIL() << "Unknown OS version " << buildVersionSdk << ", please update this test"; |
| 184 | } |
| 185 | |
| 186 | // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf. |
| 187 | ASSERT_EQ(0, getuid()) << "This test must run as root."; |
| 188 | |
| 189 | checkFiles(); |
| 190 | } |