blob: 7e7cabde0e84bf24f3115642f68750d9ef2cf221 [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/"
Maciej Żenczykowski0736d7b2022-04-21 06:42:17 -070045#define SHARED "/sys/fs/bpf/net_shared/"
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090046
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090047class BpfExistenceTest : public ::testing::Test {
48};
49
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090050static const set<string> INTRODUCED_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
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090058static const set<string> INTRODUCED_S = {
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",
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 Colitti3b38b122022-01-12 16:06:07 +090077};
78
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090079static const set<string> REMOVED_S = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090080 PLATFORM "map_offload_tether_ingress_map",
81 PLATFORM "map_offload_tether_limit_map",
82 PLATFORM "map_offload_tether_stats_map",
83 PLATFORM "prog_offload_schedcls_ingress_tether_ether",
84 PLATFORM "prog_offload_schedcls_ingress_tether_rawip",
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090085};
86
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090087static const set<string> INTRODUCED_T = {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090088};
89
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090090static const set<string> REMOVED_T = {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090091};
92
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090093void addAll(set<string>* a, const set<string>& b) {
94 a->insert(b.begin(), b.end());
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090095}
96
Lorenzo Colitti32999372022-01-14 09:37:25 +090097void removeAll(set<string>* a, const set<string>& b) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090098 for (const auto& toRemove : b) {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090099 a->erase(toRemove);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900100 }
101}
102
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900103void getFileLists(set<string>* expected, set<string>* unexpected) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900104 unexpected->clear();
105 expected->clear();
106
107 addAll(unexpected, INTRODUCED_R);
108 addAll(unexpected, INTRODUCED_S);
109 addAll(unexpected, INTRODUCED_T);
110
111 if (IsAtLeastR()) {
112 addAll(expected, INTRODUCED_R);
113 removeAll(unexpected, INTRODUCED_R);
114 // Nothing removed in R.
115 }
116
117 if (IsAtLeastS()) {
118 addAll(expected, INTRODUCED_S);
119 removeAll(expected, REMOVED_S);
120
121 addAll(unexpected, REMOVED_S);
122 removeAll(unexpected, INTRODUCED_S);
123 }
124
125 // Nothing added or removed in SCv2.
126
127 if (IsAtLeastT()) {
128 addAll(expected, INTRODUCED_T);
129 removeAll(expected, REMOVED_T);
130
131 addAll(unexpected, REMOVED_T);
132 removeAll(unexpected, INTRODUCED_T);
133 }
134}
135
136void checkFiles() {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900137 set<string> mustExist;
138 set<string> mustNotExist;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900139
140 getFileLists(&mustExist, &mustNotExist);
141
142 for (const auto& file : mustExist) {
143 EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist";
144 }
145 for (const auto& file : mustNotExist) {
146 int ret = access(file.c_str(), R_OK);
147 int err = errno;
148 EXPECT_EQ(-1, ret) << file << " unexpectedly exists";
149 if (ret == -1) {
150 EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err;
151 }
152 }
153}
154
155TEST_F(BpfExistenceTest, TestPrograms) {
Hungming Chen04569002022-02-16 16:02:49 +0800156 SKIP_IF_BPF_NOT_SUPPORTED;
157
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900158 // Pre-flight check to ensure test has been updated.
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900159 uint64_t buildVersionSdk = android_get_device_api_level();
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900160 ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version";
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900161 if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900162 FAIL() << "Unknown OS version " << buildVersionSdk << ", please update this test";
163 }
164
165 // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf.
166 ASSERT_EQ(0, getuid()) << "This test must run as root.";
167
168 checkFiles();
169}