blob: 895dda60a4629e87e5d1885efc09736c81a9fa4f [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
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
Chenbo Feng75b410b2018-10-10 15:01:19 -070036#include "bpf/BpfMap.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070037#include "bpf/BpfUtils.h"
38
Chenbo Feng75b410b2018-10-10 15:01:19 -070039using ::testing::Test;
40
41namespace android {
42namespace bpf {
43
44using base::unique_fd;
45using netdutils::StatusOr;
46
47constexpr uint32_t TEST_MAP_SIZE = 10;
48constexpr uint32_t TEST_KEY1 = 1;
49constexpr uint32_t TEST_VALUE1 = 10;
50constexpr const char PINNED_MAP_PATH[] = "/sys/fs/bpf/testMap";
51
52class BpfMapTest : public testing::Test {
53 protected:
54 BpfMapTest() {}
55 int mMapFd;
56
57 void SetUp() {
Chenbo Feng249e2f82018-11-20 17:37:00 -080058 SKIP_IF_BPF_NOT_SUPPORTED;
59
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070060 EXPECT_EQ(0, setrlimitForTest());
Chenbo Feng75b410b2018-10-10 15:01:19 -070061 if (!access(PINNED_MAP_PATH, R_OK)) {
62 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
63 }
64 mMapFd = createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint32_t), TEST_MAP_SIZE,
65 BPF_F_NO_PREALLOC);
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070066 EXPECT_LE(0, mMapFd);
Chenbo Feng75b410b2018-10-10 15:01:19 -070067 }
68
69 void TearDown() {
Chenbo Feng249e2f82018-11-20 17:37:00 -080070 SKIP_IF_BPF_NOT_SUPPORTED;
71
Chenbo Feng75b410b2018-10-10 15:01:19 -070072 if (!access(PINNED_MAP_PATH, R_OK)) {
73 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
74 }
75 close(mMapFd);
76 }
77
78 void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
79 EXPECT_FALSE(map.isValid());
80 EXPECT_EQ(-1, map.getMap().get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070081 }
82
83 void checkMapValid(BpfMap<uint32_t, uint32_t>& map) {
84 EXPECT_LE(0, map.getMap().get());
85 EXPECT_TRUE(map.isValid());
86 }
87
88 void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
89 ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
90 uint32_t value_read;
91 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
92 checkValueAndStatus(value, value_read);
93 }
94
95 void checkValueAndStatus(uint32_t refValue, StatusOr<uint32_t> value) {
96 ASSERT_TRUE(isOk(value.status()));
97 ASSERT_EQ(refValue, value.value());
98 }
99
100 void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
101 for (uint32_t key = 0; key < total; key++) {
102 uint32_t value = key * 10;
103 EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
104 }
105 }
106
107 void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
108 auto isEmpty = map.isEmpty();
109 ASSERT_TRUE(isOk(isEmpty));
110 ASSERT_TRUE(isEmpty.value());
111 }
112};
113
114TEST_F(BpfMapTest, constructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800115 SKIP_IF_BPF_NOT_SUPPORTED;
116
Chenbo Feng75b410b2018-10-10 15:01:19 -0700117 BpfMap<uint32_t, uint32_t> testMap1;
118 checkMapInvalid(testMap1);
119
120 BpfMap<uint32_t, uint32_t> testMap2(mMapFd);
121 checkMapValid(testMap2);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700122
123 BpfMap<uint32_t, uint32_t> testMap3(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
124 checkMapValid(testMap3);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700125}
126
127TEST_F(BpfMapTest, basicHelpers) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800128 SKIP_IF_BPF_NOT_SUPPORTED;
129
Chenbo Feng75b410b2018-10-10 15:01:19 -0700130 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
131 uint32_t key = TEST_KEY1;
132 uint32_t value_write = TEST_VALUE1;
133 writeToMapAndCheck(testMap, key, value_write);
134 StatusOr<uint32_t> value_read = testMap.readValue(key);
135 checkValueAndStatus(value_write, value_read);
136 StatusOr<uint32_t> key_read = testMap.getFirstKey();
137 checkValueAndStatus(key, key_read);
138 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
139 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
140 ASSERT_EQ(ENOENT, errno);
141}
142
143TEST_F(BpfMapTest, reset) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800144 SKIP_IF_BPF_NOT_SUPPORTED;
145
Chenbo Feng75b410b2018-10-10 15:01:19 -0700146 BpfMap<uint32_t, uint32_t> testMap;
147 testMap.reset(mMapFd);
148 uint32_t key = TEST_KEY1;
149 uint32_t value_write = TEST_VALUE1;
150 writeToMapAndCheck(testMap, key, value_write);
151 testMap.reset();
152 checkMapInvalid(testMap);
153 unique_fd invalidFd(mMapFd);
154 ASSERT_GT(0, findMapEntry(invalidFd, &key, &value_write));
155 ASSERT_EQ(EBADF, errno);
156}
157
158TEST_F(BpfMapTest, moveConstructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800159 SKIP_IF_BPF_NOT_SUPPORTED;
160
Chenbo Feng75b410b2018-10-10 15:01:19 -0700161 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
162 BpfMap<uint32_t, uint32_t> testMap2;
163 testMap2 = std::move(testMap1);
164 uint32_t key = TEST_KEY1;
165 checkMapInvalid(testMap1);
166 uint32_t value = TEST_VALUE1;
167 writeToMapAndCheck(testMap2, key, value);
168}
169
Chenbo Feng75b410b2018-10-10 15:01:19 -0700170TEST_F(BpfMapTest, SetUpMap) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800171 SKIP_IF_BPF_NOT_SUPPORTED;
172
Maciej Żenczykowski04d88b72019-04-01 10:34:26 -0700173 EXPECT_NE(0, access(PINNED_MAP_PATH, R_OK));
174 BpfMap<uint32_t, uint32_t> testMap1(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
175 ASSERT_EQ(0, bpfFdPin(testMap1.getMap(), PINNED_MAP_PATH));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700176 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
177 checkMapValid(testMap1);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700178 BpfMap<uint32_t, uint32_t> testMap2;
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700179 EXPECT_OK(testMap2.init(PINNED_MAP_PATH));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700180 checkMapValid(testMap2);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700181 uint32_t key = TEST_KEY1;
182 uint32_t value = TEST_VALUE1;
183 writeToMapAndCheck(testMap1, key, value);
184 StatusOr<uint32_t> value_read = testMap2.readValue(key);
185 checkValueAndStatus(value, value_read);
186}
187
188TEST_F(BpfMapTest, iterate) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800189 SKIP_IF_BPF_NOT_SUPPORTED;
190
Chenbo Feng75b410b2018-10-10 15:01:19 -0700191 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
192 populateMap(TEST_MAP_SIZE, testMap);
193 int totalCount = 0;
194 int totalSum = 0;
195 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
196 BpfMap<uint32_t, uint32_t>& map) {
197 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
198 totalCount++;
199 totalSum += key;
200 return map.deleteValue(key);
201 };
202 EXPECT_OK(testMap.iterate(iterateWithDeletion));
203 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
204 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
205 expectMapEmpty(testMap);
206}
207
208TEST_F(BpfMapTest, iterateWithValue) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800209 SKIP_IF_BPF_NOT_SUPPORTED;
210
Chenbo Feng75b410b2018-10-10 15:01:19 -0700211 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
212 populateMap(TEST_MAP_SIZE, testMap);
213 int totalCount = 0;
214 int totalSum = 0;
215 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
216 const uint32_t& value,
217 BpfMap<uint32_t, uint32_t>& map) {
218 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
219 EXPECT_EQ(value, key * 10);
220 totalCount++;
221 totalSum += value;
222 return map.deleteValue(key);
223 };
224 EXPECT_OK(testMap.iterateWithValue(iterateWithDeletion));
225 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
226 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
227 expectMapEmpty(testMap);
228}
229
230TEST_F(BpfMapTest, mapIsEmpty) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800231 SKIP_IF_BPF_NOT_SUPPORTED;
232
Chenbo Feng75b410b2018-10-10 15:01:19 -0700233 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
234 expectMapEmpty(testMap);
235 uint32_t key = TEST_KEY1;
236 uint32_t value_write = TEST_VALUE1;
237 writeToMapAndCheck(testMap, key, value_write);
238 auto isEmpty = testMap.isEmpty();
239 ASSERT_TRUE(isOk(isEmpty));
240 ASSERT_FALSE(isEmpty.value());
241 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
242 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
243 ASSERT_EQ(ENOENT, errno);
244 expectMapEmpty(testMap);
245 int entriesSeen = 0;
246 EXPECT_OK(testMap.iterate(
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800247 [&entriesSeen](const unsigned int&,
248 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
249 entriesSeen++;
250 return netdutils::status::ok;
251 }));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700252 EXPECT_EQ(0, entriesSeen);
253 EXPECT_OK(testMap.iterateWithValue(
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800254 [&entriesSeen](const unsigned int&, const unsigned int&,
255 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
256 entriesSeen++;
257 return netdutils::status::ok;
258 }));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700259 EXPECT_EQ(0, entriesSeen);
260}
261
262TEST_F(BpfMapTest, mapClear) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800263 SKIP_IF_BPF_NOT_SUPPORTED;
264
Chenbo Feng75b410b2018-10-10 15:01:19 -0700265 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
266 populateMap(TEST_MAP_SIZE, testMap);
267 auto isEmpty = testMap.isEmpty();
268 ASSERT_TRUE(isOk(isEmpty));
269 ASSERT_FALSE(isEmpty.value());
270 ASSERT_TRUE(isOk(testMap.clear()));
271 expectMapEmpty(testMap);
272}
273
274} // namespace bpf
275} // namespace android