blob: 02c7ee43293b558fa494ece04e5a84f2b617a774 [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 Feng75b410b2018-10-10 15:01:19 -070060 if (!access(PINNED_MAP_PATH, R_OK)) {
61 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
62 }
63 mMapFd = createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint32_t), TEST_MAP_SIZE,
64 BPF_F_NO_PREALLOC);
65 }
66
67 void TearDown() {
Chenbo Feng249e2f82018-11-20 17:37:00 -080068 SKIP_IF_BPF_NOT_SUPPORTED;
69
Chenbo Feng75b410b2018-10-10 15:01:19 -070070 if (!access(PINNED_MAP_PATH, R_OK)) {
71 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
72 }
73 close(mMapFd);
74 }
75
76 void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
77 EXPECT_FALSE(map.isValid());
78 EXPECT_EQ(-1, map.getMap().get());
79 EXPECT_TRUE(map.getPinnedPath().empty());
80 }
81
82 void checkMapValid(BpfMap<uint32_t, uint32_t>& map) {
83 EXPECT_LE(0, map.getMap().get());
84 EXPECT_TRUE(map.isValid());
85 }
86
87 void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
88 ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
89 uint32_t value_read;
90 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
91 checkValueAndStatus(value, value_read);
92 }
93
94 void checkValueAndStatus(uint32_t refValue, StatusOr<uint32_t> value) {
95 ASSERT_TRUE(isOk(value.status()));
96 ASSERT_EQ(refValue, value.value());
97 }
98
99 void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
100 for (uint32_t key = 0; key < total; key++) {
101 uint32_t value = key * 10;
102 EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
103 }
104 }
105
106 void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
107 auto isEmpty = map.isEmpty();
108 ASSERT_TRUE(isOk(isEmpty));
109 ASSERT_TRUE(isEmpty.value());
110 }
111};
112
113TEST_F(BpfMapTest, constructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800114 SKIP_IF_BPF_NOT_SUPPORTED;
115
Chenbo Feng75b410b2018-10-10 15:01:19 -0700116 BpfMap<uint32_t, uint32_t> testMap1;
117 checkMapInvalid(testMap1);
118
119 BpfMap<uint32_t, uint32_t> testMap2(mMapFd);
120 checkMapValid(testMap2);
121 EXPECT_TRUE(testMap2.getPinnedPath().empty());
122
123 BpfMap<uint32_t, uint32_t> testMap3(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
124 checkMapValid(testMap3);
125 EXPECT_TRUE(testMap3.getPinnedPath().empty());
126}
127
128TEST_F(BpfMapTest, basicHelpers) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800129 SKIP_IF_BPF_NOT_SUPPORTED;
130
Chenbo Feng75b410b2018-10-10 15:01:19 -0700131 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
132 uint32_t key = TEST_KEY1;
133 uint32_t value_write = TEST_VALUE1;
134 writeToMapAndCheck(testMap, key, value_write);
135 StatusOr<uint32_t> value_read = testMap.readValue(key);
136 checkValueAndStatus(value_write, value_read);
137 StatusOr<uint32_t> key_read = testMap.getFirstKey();
138 checkValueAndStatus(key, key_read);
139 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
140 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
141 ASSERT_EQ(ENOENT, errno);
142}
143
144TEST_F(BpfMapTest, reset) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800145 SKIP_IF_BPF_NOT_SUPPORTED;
146
Chenbo Feng75b410b2018-10-10 15:01:19 -0700147 BpfMap<uint32_t, uint32_t> testMap;
148 testMap.reset(mMapFd);
149 uint32_t key = TEST_KEY1;
150 uint32_t value_write = TEST_VALUE1;
151 writeToMapAndCheck(testMap, key, value_write);
152 testMap.reset();
153 checkMapInvalid(testMap);
154 unique_fd invalidFd(mMapFd);
155 ASSERT_GT(0, findMapEntry(invalidFd, &key, &value_write));
156 ASSERT_EQ(EBADF, errno);
157}
158
159TEST_F(BpfMapTest, moveConstructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800160 SKIP_IF_BPF_NOT_SUPPORTED;
161
Chenbo Feng75b410b2018-10-10 15:01:19 -0700162 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
163 BpfMap<uint32_t, uint32_t> testMap2;
164 testMap2 = std::move(testMap1);
165 uint32_t key = TEST_KEY1;
166 checkMapInvalid(testMap1);
167 uint32_t value = TEST_VALUE1;
168 writeToMapAndCheck(testMap2, key, value);
169}
170
171TEST_F(BpfMapTest, pinnedToPath) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800172 SKIP_IF_BPF_NOT_SUPPORTED;
173
Chenbo Feng75b410b2018-10-10 15:01:19 -0700174 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
175 EXPECT_OK(testMap1.pinToPath(PINNED_MAP_PATH));
176 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
177 EXPECT_EQ(0, testMap1.getPinnedPath().compare(PINNED_MAP_PATH));
178 BpfMap<uint32_t, uint32_t> testMap2(mapRetrieve(PINNED_MAP_PATH, 0));
179 checkMapValid(testMap2);
180 uint32_t key = TEST_KEY1;
181 uint32_t value = TEST_VALUE1;
182 writeToMapAndCheck(testMap1, key, value);
183 StatusOr<uint32_t> value_read = testMap2.readValue(key);
184 checkValueAndStatus(value, value_read);
185}
186
187TEST_F(BpfMapTest, SetUpMap) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800188 SKIP_IF_BPF_NOT_SUPPORTED;
189
Chenbo Feng75b410b2018-10-10 15:01:19 -0700190 BpfMap<uint32_t, uint32_t> testMap1;
191 EXPECT_OK(testMap1.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH));
192 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
193 checkMapValid(testMap1);
194 EXPECT_EQ(0, testMap1.getPinnedPath().compare(PINNED_MAP_PATH));
195 BpfMap<uint32_t, uint32_t> testMap2;
196 EXPECT_OK(testMap2.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH));
197 checkMapValid(testMap2);
198 EXPECT_EQ(0, testMap2.getPinnedPath().compare(PINNED_MAP_PATH));
199 uint32_t key = TEST_KEY1;
200 uint32_t value = TEST_VALUE1;
201 writeToMapAndCheck(testMap1, key, value);
202 StatusOr<uint32_t> value_read = testMap2.readValue(key);
203 checkValueAndStatus(value, value_read);
204}
205
206TEST_F(BpfMapTest, iterate) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800207 SKIP_IF_BPF_NOT_SUPPORTED;
208
Chenbo Feng75b410b2018-10-10 15:01:19 -0700209 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
210 populateMap(TEST_MAP_SIZE, testMap);
211 int totalCount = 0;
212 int totalSum = 0;
213 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
214 BpfMap<uint32_t, uint32_t>& map) {
215 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
216 totalCount++;
217 totalSum += key;
218 return map.deleteValue(key);
219 };
220 EXPECT_OK(testMap.iterate(iterateWithDeletion));
221 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
222 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
223 expectMapEmpty(testMap);
224}
225
226TEST_F(BpfMapTest, iterateWithValue) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800227 SKIP_IF_BPF_NOT_SUPPORTED;
228
Chenbo Feng75b410b2018-10-10 15:01:19 -0700229 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
230 populateMap(TEST_MAP_SIZE, testMap);
231 int totalCount = 0;
232 int totalSum = 0;
233 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
234 const uint32_t& value,
235 BpfMap<uint32_t, uint32_t>& map) {
236 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
237 EXPECT_EQ(value, key * 10);
238 totalCount++;
239 totalSum += value;
240 return map.deleteValue(key);
241 };
242 EXPECT_OK(testMap.iterateWithValue(iterateWithDeletion));
243 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
244 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
245 expectMapEmpty(testMap);
246}
247
248TEST_F(BpfMapTest, mapIsEmpty) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800249 SKIP_IF_BPF_NOT_SUPPORTED;
250
Chenbo Feng75b410b2018-10-10 15:01:19 -0700251 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
252 expectMapEmpty(testMap);
253 uint32_t key = TEST_KEY1;
254 uint32_t value_write = TEST_VALUE1;
255 writeToMapAndCheck(testMap, key, value_write);
256 auto isEmpty = testMap.isEmpty();
257 ASSERT_TRUE(isOk(isEmpty));
258 ASSERT_FALSE(isEmpty.value());
259 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
260 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
261 ASSERT_EQ(ENOENT, errno);
262 expectMapEmpty(testMap);
263 int entriesSeen = 0;
264 EXPECT_OK(testMap.iterate(
265 [&entriesSeen](const unsigned int&,
266 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
267 entriesSeen++;
268 return netdutils::status::ok;
269 }));
270 EXPECT_EQ(0, entriesSeen);
271 EXPECT_OK(testMap.iterateWithValue(
272 [&entriesSeen](const unsigned int&, const unsigned int&,
273 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
274 entriesSeen++;
275 return netdutils::status::ok;
276 }));
277 EXPECT_EQ(0, entriesSeen);
278}
279
280TEST_F(BpfMapTest, mapClear) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800281 SKIP_IF_BPF_NOT_SUPPORTED;
282
Chenbo Feng75b410b2018-10-10 15:01:19 -0700283 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
284 populateMap(TEST_MAP_SIZE, testMap);
285 auto isEmpty = testMap.isEmpty();
286 ASSERT_TRUE(isOk(isEmpty));
287 ASSERT_FALSE(isEmpty.value());
288 ASSERT_TRUE(isOk(testMap.clear()));
289 expectMapEmpty(testMap);
290}
291
292} // namespace bpf
293} // namespace android