blob: 2bba282d6defbfcf027cd7fd259e837f46008b10 [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
34using android::modules::sdklevel::IsAtLeastR;
35using android::modules::sdklevel::IsAtLeastS;
36using android::modules::sdklevel::IsAtLeastT;
37
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090038// Mainline development branches lack the constant for the current development OS.
39#ifndef __ANDROID_API_T__
40#define __ANDROID_API_T__ 33
41#endif
42
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090043#define PLATFORM "/sys/fs/bpf/"
44#define TETHERING "/sys/fs/bpf/tethering/"
45
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090046class BpfExistenceTest : public ::testing::Test {
47};
48
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090049static const set<string> INTRODUCED_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
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090057static const set<string> INTRODUCED_S = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090058 TETHERING "map_offload_tether_dev_map",
59 TETHERING "map_offload_tether_downstream4_map",
60 TETHERING "map_offload_tether_downstream64_map",
61 TETHERING "map_offload_tether_downstream6_map",
62 TETHERING "map_offload_tether_error_map",
63 TETHERING "map_offload_tether_limit_map",
64 TETHERING "map_offload_tether_stats_map",
65 TETHERING "map_offload_tether_upstream4_map",
66 TETHERING "map_offload_tether_upstream6_map",
67 TETHERING "map_test_tether_downstream6_map",
68 TETHERING "prog_offload_schedcls_tether_downstream4_ether",
69 TETHERING "prog_offload_schedcls_tether_downstream4_rawip",
70 TETHERING "prog_offload_schedcls_tether_downstream6_ether",
71 TETHERING "prog_offload_schedcls_tether_downstream6_rawip",
72 TETHERING "prog_offload_schedcls_tether_upstream4_ether",
73 TETHERING "prog_offload_schedcls_tether_upstream4_rawip",
74 TETHERING "prog_offload_schedcls_tether_upstream6_ether",
75 TETHERING "prog_offload_schedcls_tether_upstream6_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090076};
77
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090078static const set<string> REMOVED_S = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090079 PLATFORM "map_offload_tether_ingress_map",
80 PLATFORM "map_offload_tether_limit_map",
81 PLATFORM "map_offload_tether_stats_map",
82 PLATFORM "prog_offload_schedcls_ingress_tether_ether",
83 PLATFORM "prog_offload_schedcls_ingress_tether_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090084};
85
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090086static const set<string> INTRODUCED_T = {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090087};
88
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090089static const set<string> REMOVED_T = {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090090};
91
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090092void addAll(set<string>* a, const set<string>& b) {
93 a->insert(b.begin(), b.end());
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090094}
95
Lorenzo Colitti32999372022-01-14 09:37:25 +090096void removeAll(set<string>* a, const set<string>& b) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090097 for (const auto& toRemove : b) {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090098 a->erase(toRemove);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090099 }
100}
101
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900102void getFileLists(set<string>* expected, set<string>* unexpected) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900103 unexpected->clear();
104 expected->clear();
105
106 addAll(unexpected, INTRODUCED_R);
107 addAll(unexpected, INTRODUCED_S);
108 addAll(unexpected, INTRODUCED_T);
109
110 if (IsAtLeastR()) {
111 addAll(expected, INTRODUCED_R);
112 removeAll(unexpected, INTRODUCED_R);
113 // Nothing removed in R.
114 }
115
116 if (IsAtLeastS()) {
117 addAll(expected, INTRODUCED_S);
118 removeAll(expected, REMOVED_S);
119
120 addAll(unexpected, REMOVED_S);
121 removeAll(unexpected, INTRODUCED_S);
122 }
123
124 // Nothing added or removed in SCv2.
125
126 if (IsAtLeastT()) {
127 addAll(expected, INTRODUCED_T);
128 removeAll(expected, REMOVED_T);
129
130 addAll(unexpected, REMOVED_T);
131 removeAll(unexpected, INTRODUCED_T);
132 }
133}
134
135void checkFiles() {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900136 set<string> mustExist;
137 set<string> mustNotExist;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900138
139 getFileLists(&mustExist, &mustNotExist);
140
141 for (const auto& file : mustExist) {
142 EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist";
143 }
144 for (const auto& file : mustNotExist) {
145 int ret = access(file.c_str(), R_OK);
146 int err = errno;
147 EXPECT_EQ(-1, ret) << file << " unexpectedly exists";
148 if (ret == -1) {
149 EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err;
150 }
151 }
152}
153
154TEST_F(BpfExistenceTest, TestPrograms) {
Hungming Chen04569002022-02-16 16:02:49 +0800155 SKIP_IF_BPF_NOT_SUPPORTED;
156
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900157 // Pre-flight check to ensure test has been updated.
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900158 uint64_t buildVersionSdk = android_get_device_api_level();
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900159 ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version";
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900160 if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900161 FAIL() << "Unknown OS version " << buildVersionSdk << ", please update this test";
162 }
163
164 // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf.
165 ASSERT_EQ(0, getuid()) << "This test must run as root.";
166
167 checkFiles();
168}