1 /*
2 * Copyright (C) 2018 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 */
16
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <linux/inet_diag.h>
25 #include <linux/sock_diag.h>
26 #include <net/if.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include <gtest/gtest.h>
32
33 #include <android-base/stringprintf.h>
34 #include <android-base/strings.h>
35
36 #include <netdutils/MockSyscalls.h>
37 #include "bpf/BpfMap.h"
38 #include "bpf/BpfNetworkStats.h"
39 #include "bpf/BpfUtils.h"
40
41 using ::testing::_;
42 using ::testing::ByMove;
43 using ::testing::Invoke;
44 using ::testing::Return;
45 using ::testing::StrictMock;
46 using ::testing::Test;
47
48 namespace android {
49 namespace bpf {
50
51 using base::unique_fd;
52 using netdutils::StatusOr;
53
54 constexpr uint32_t TEST_MAP_SIZE = 10;
55 constexpr uint32_t TEST_KEY1 = 1;
56 constexpr uint32_t TEST_VALUE1 = 10;
57 constexpr const char PINNED_MAP_PATH[] = "/sys/fs/bpf/testMap";
58
59 class BpfMapTest : public testing::Test {
60 protected:
BpfMapTest()61 BpfMapTest() {}
62 int mMapFd;
63
SetUp()64 void SetUp() {
65 if (!access(PINNED_MAP_PATH, R_OK)) {
66 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
67 }
68 mMapFd = createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint32_t), TEST_MAP_SIZE,
69 BPF_F_NO_PREALLOC);
70 }
71
TearDown()72 void TearDown() {
73 if (!access(PINNED_MAP_PATH, R_OK)) {
74 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
75 }
76 close(mMapFd);
77 }
78
checkMapInvalid(BpfMap<uint32_t,uint32_t> & map)79 void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
80 EXPECT_FALSE(map.isValid());
81 EXPECT_EQ(-1, map.getMap().get());
82 EXPECT_TRUE(map.getPinnedPath().empty());
83 }
84
checkMapValid(BpfMap<uint32_t,uint32_t> & map)85 void checkMapValid(BpfMap<uint32_t, uint32_t>& map) {
86 EXPECT_LE(0, map.getMap().get());
87 EXPECT_TRUE(map.isValid());
88 }
89
writeToMapAndCheck(BpfMap<uint32_t,uint32_t> & map,uint32_t key,uint32_t value)90 void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
91 ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
92 uint32_t value_read;
93 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
94 checkValueAndStatus(value, value_read);
95 }
96
checkValueAndStatus(uint32_t refValue,StatusOr<uint32_t> value)97 void checkValueAndStatus(uint32_t refValue, StatusOr<uint32_t> value) {
98 ASSERT_TRUE(isOk(value.status()));
99 ASSERT_EQ(refValue, value.value());
100 }
101
populateMap(uint32_t total,BpfMap<uint32_t,uint32_t> & map)102 void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
103 for (uint32_t key = 0; key < total; key++) {
104 uint32_t value = key * 10;
105 EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
106 }
107 }
108 };
109
TEST_F(BpfMapTest,constructor)110 TEST_F(BpfMapTest, constructor) {
111 BpfMap<uint32_t, uint32_t> testMap1;
112 checkMapInvalid(testMap1);
113
114 BpfMap<uint32_t, uint32_t> testMap2(mMapFd);
115 checkMapValid(testMap2);
116 EXPECT_TRUE(testMap2.getPinnedPath().empty());
117
118 BpfMap<uint32_t, uint32_t> testMap3(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
119 checkMapValid(testMap3);
120 EXPECT_TRUE(testMap3.getPinnedPath().empty());
121 }
122
TEST_F(BpfMapTest,basicHelpers)123 TEST_F(BpfMapTest, basicHelpers) {
124 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
125 uint32_t key = TEST_KEY1;
126 uint32_t value_write = TEST_VALUE1;
127 writeToMapAndCheck(testMap, key, value_write);
128 StatusOr<uint32_t> value_read = testMap.readValue(key);
129 checkValueAndStatus(value_write, value_read);
130 StatusOr<uint32_t> key_read = testMap.getFirstKey();
131 checkValueAndStatus(key, key_read);
132 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
133 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
134 ASSERT_EQ(ENOENT, errno);
135 }
136
TEST_F(BpfMapTest,reset)137 TEST_F(BpfMapTest, reset) {
138 BpfMap<uint32_t, uint32_t> testMap;
139 testMap.reset(mMapFd);
140 uint32_t key = TEST_KEY1;
141 uint32_t value_write = TEST_VALUE1;
142 writeToMapAndCheck(testMap, key, value_write);
143 testMap.reset();
144 checkMapInvalid(testMap);
145 unique_fd invalidFd(mMapFd);
146 ASSERT_GT(0, findMapEntry(invalidFd, &key, &value_write));
147 ASSERT_EQ(EBADF, errno);
148 }
149
TEST_F(BpfMapTest,moveConstructor)150 TEST_F(BpfMapTest, moveConstructor) {
151 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
152 BpfMap<uint32_t, uint32_t> testMap2;
153 testMap2 = std::move(testMap1);
154 uint32_t key = TEST_KEY1;
155 checkMapInvalid(testMap1);
156 uint32_t value = TEST_VALUE1;
157 writeToMapAndCheck(testMap2, key, value);
158 }
159
TEST_F(BpfMapTest,iterateEmptyMap)160 TEST_F(BpfMapTest, iterateEmptyMap) {
161 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
162 auto itr = testMap.begin();
163 ASSERT_NE(testMap.end(), itr);
164 itr.start();
165 ASSERT_EQ(testMap.end(), itr);
166 ASSERT_FALSE(isOk(itr.next()));
167 ASSERT_EQ(testMap.end(), itr);
168 }
169
TEST_F(BpfMapTest,iterator)170 TEST_F(BpfMapTest, iterator) {
171 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
172 for (uint32_t key = 0; key < TEST_MAP_SIZE; key++) {
173 uint32_t value = key * 10;
174 ASSERT_TRUE(isOk(testMap.writeValue(key, value, BPF_ANY)));
175 }
176 std::vector<uint32_t> valueList;
177 auto itr = testMap.begin();
178 for (itr.start(); itr != testMap.end(); itr.next()) {
179 uint32_t readKey = *itr;
180 StatusOr<uint32_t> readValue = testMap.readValue(readKey);
181 ASSERT_TRUE(isOk(readValue.status()));
182 valueList.push_back(readValue.value());
183 }
184 ASSERT_EQ((size_t)TEST_MAP_SIZE, valueList.size());
185 std::sort(valueList.begin(), valueList.end());
186 for (uint32_t key = 0; key < TEST_MAP_SIZE; key++) {
187 EXPECT_EQ(key * 10, valueList[key]);
188 }
189 }
190
TEST_F(BpfMapTest,twoIterator)191 TEST_F(BpfMapTest, twoIterator) {
192 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
193 for (uint32_t key = 0; key < TEST_MAP_SIZE; key++) {
194 uint32_t value = key * 10;
195 ASSERT_TRUE(isOk(testMap.writeValue(key, value, BPF_ANY)));
196 }
197 auto itr1 = testMap.begin();
198 auto itr2 = testMap.begin();
199 ASSERT_EQ(itr1, itr2);
200 ASSERT_TRUE(isOk(itr1.start()));
201 ASSERT_NE(itr1, itr2);
202 ASSERT_TRUE(isOk(itr2.start()));
203 ASSERT_EQ(itr1, itr2);
204 uint32_t count = 0;
205 while (itr1 != testMap.end()) {
206 ASSERT_TRUE(isOk(itr1.next()));
207 count++;
208 }
209 ASSERT_EQ(testMap.end(), itr1);
210 ASSERT_EQ(TEST_MAP_SIZE, count);
211 while (count != 0) {
212 ASSERT_NE(testMap.end(), itr2);
213 count--;
214 ASSERT_TRUE(isOk(itr2.next()));
215 }
216 ASSERT_EQ(itr1, itr2);
217 ASSERT_EQ(testMap.end(), itr2);
218 }
219
TEST_F(BpfMapTest,pinnedToPath)220 TEST_F(BpfMapTest, pinnedToPath) {
221 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
222 EXPECT_TRUE(isOk(testMap1.pinToPath(PINNED_MAP_PATH)));
223 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
224 EXPECT_EQ(0, testMap1.getPinnedPath().compare(PINNED_MAP_PATH));
225 BpfMap<uint32_t, uint32_t> testMap2(mapRetrieve(PINNED_MAP_PATH, 0));
226 checkMapValid(testMap2);
227 uint32_t key = TEST_KEY1;
228 uint32_t value = TEST_VALUE1;
229 writeToMapAndCheck(testMap1, key, value);
230 StatusOr<uint32_t> value_read = testMap2.readValue(key);
231 checkValueAndStatus(value, value_read);
232 }
233
TEST_F(BpfMapTest,SetUpMap)234 TEST_F(BpfMapTest, SetUpMap) {
235 BpfMap<uint32_t, uint32_t> testMap1;
236 EXPECT_TRUE(isOk(testMap1.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH)));
237 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
238 checkMapValid(testMap1);
239 EXPECT_EQ(0, testMap1.getPinnedPath().compare(PINNED_MAP_PATH));
240 BpfMap<uint32_t, uint32_t> testMap2;
241 testMap2.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH);
242 checkMapValid(testMap2);
243 EXPECT_EQ(0, testMap2.getPinnedPath().compare(PINNED_MAP_PATH));
244 uint32_t key = TEST_KEY1;
245 uint32_t value = TEST_VALUE1;
246 writeToMapAndCheck(testMap1, key, value);
247 StatusOr<uint32_t> value_read = testMap2.readValue(key);
248 checkValueAndStatus(value, value_read);
249 }
250
TEST_F(BpfMapTest,iterate)251 TEST_F(BpfMapTest, iterate) {
252 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
253 populateMap(TEST_MAP_SIZE, testMap);
254 int totalCount = 0;
255 int totalSum = 0;
256 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
257 BpfMap<uint32_t, uint32_t>& map) {
258 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
259 totalCount++;
260 totalSum += key;
261 return map.deleteValue(key);
262 };
263 EXPECT_TRUE(isOk(testMap.iterate(iterateWithDeletion)));
264 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
265 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
266 EXPECT_FALSE(isOk(testMap.getFirstKey()));
267 }
268
TEST_F(BpfMapTest,iterateWithValue)269 TEST_F(BpfMapTest, iterateWithValue) {
270 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
271 populateMap(TEST_MAP_SIZE, testMap);
272 int totalCount = 0;
273 int totalSum = 0;
274 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
275 const uint32_t& value,
276 BpfMap<uint32_t, uint32_t>& map) {
277 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
278 EXPECT_EQ(value, key * 10);
279 totalCount++;
280 totalSum += value;
281 return map.deleteValue(key);
282 };
283 EXPECT_TRUE(isOk(testMap.iterateWithValue(iterateWithDeletion)));
284 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
285 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
286 EXPECT_FALSE(isOk(testMap.getFirstKey()));
287 }
288
289 } // namespace bpf
290 } // namespace android
291