blob: d0bd79e8e6a6a03713497ced2a308ccf9a7e5593 [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() {
58 if (!access(PINNED_MAP_PATH, R_OK)) {
59 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
60 }
61 mMapFd = createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint32_t), TEST_MAP_SIZE,
62 BPF_F_NO_PREALLOC);
63 }
64
65 void TearDown() {
66 if (!access(PINNED_MAP_PATH, R_OK)) {
67 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
68 }
69 close(mMapFd);
70 }
71
72 void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
73 EXPECT_FALSE(map.isValid());
74 EXPECT_EQ(-1, map.getMap().get());
75 EXPECT_TRUE(map.getPinnedPath().empty());
76 }
77
78 void checkMapValid(BpfMap<uint32_t, uint32_t>& map) {
79 EXPECT_LE(0, map.getMap().get());
80 EXPECT_TRUE(map.isValid());
81 }
82
83 void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
84 ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
85 uint32_t value_read;
86 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
87 checkValueAndStatus(value, value_read);
88 }
89
90 void checkValueAndStatus(uint32_t refValue, StatusOr<uint32_t> value) {
91 ASSERT_TRUE(isOk(value.status()));
92 ASSERT_EQ(refValue, value.value());
93 }
94
95 void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
96 for (uint32_t key = 0; key < total; key++) {
97 uint32_t value = key * 10;
98 EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
99 }
100 }
101
102 void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
103 auto isEmpty = map.isEmpty();
104 ASSERT_TRUE(isOk(isEmpty));
105 ASSERT_TRUE(isEmpty.value());
106 }
107};
108
109TEST_F(BpfMapTest, constructor) {
110 BpfMap<uint32_t, uint32_t> testMap1;
111 checkMapInvalid(testMap1);
112
113 BpfMap<uint32_t, uint32_t> testMap2(mMapFd);
114 checkMapValid(testMap2);
115 EXPECT_TRUE(testMap2.getPinnedPath().empty());
116
117 BpfMap<uint32_t, uint32_t> testMap3(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
118 checkMapValid(testMap3);
119 EXPECT_TRUE(testMap3.getPinnedPath().empty());
120}
121
122TEST_F(BpfMapTest, basicHelpers) {
123 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
124 uint32_t key = TEST_KEY1;
125 uint32_t value_write = TEST_VALUE1;
126 writeToMapAndCheck(testMap, key, value_write);
127 StatusOr<uint32_t> value_read = testMap.readValue(key);
128 checkValueAndStatus(value_write, value_read);
129 StatusOr<uint32_t> key_read = testMap.getFirstKey();
130 checkValueAndStatus(key, key_read);
131 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
132 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
133 ASSERT_EQ(ENOENT, errno);
134}
135
136TEST_F(BpfMapTest, reset) {
137 BpfMap<uint32_t, uint32_t> testMap;
138 testMap.reset(mMapFd);
139 uint32_t key = TEST_KEY1;
140 uint32_t value_write = TEST_VALUE1;
141 writeToMapAndCheck(testMap, key, value_write);
142 testMap.reset();
143 checkMapInvalid(testMap);
144 unique_fd invalidFd(mMapFd);
145 ASSERT_GT(0, findMapEntry(invalidFd, &key, &value_write));
146 ASSERT_EQ(EBADF, errno);
147}
148
149TEST_F(BpfMapTest, moveConstructor) {
150 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
151 BpfMap<uint32_t, uint32_t> testMap2;
152 testMap2 = std::move(testMap1);
153 uint32_t key = TEST_KEY1;
154 checkMapInvalid(testMap1);
155 uint32_t value = TEST_VALUE1;
156 writeToMapAndCheck(testMap2, key, value);
157}
158
159TEST_F(BpfMapTest, pinnedToPath) {
160 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
161 EXPECT_OK(testMap1.pinToPath(PINNED_MAP_PATH));
162 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
163 EXPECT_EQ(0, testMap1.getPinnedPath().compare(PINNED_MAP_PATH));
164 BpfMap<uint32_t, uint32_t> testMap2(mapRetrieve(PINNED_MAP_PATH, 0));
165 checkMapValid(testMap2);
166 uint32_t key = TEST_KEY1;
167 uint32_t value = TEST_VALUE1;
168 writeToMapAndCheck(testMap1, key, value);
169 StatusOr<uint32_t> value_read = testMap2.readValue(key);
170 checkValueAndStatus(value, value_read);
171}
172
173TEST_F(BpfMapTest, SetUpMap) {
174 BpfMap<uint32_t, uint32_t> testMap1;
175 EXPECT_OK(testMap1.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH));
176 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
177 checkMapValid(testMap1);
178 EXPECT_EQ(0, testMap1.getPinnedPath().compare(PINNED_MAP_PATH));
179 BpfMap<uint32_t, uint32_t> testMap2;
180 EXPECT_OK(testMap2.getOrCreate(TEST_MAP_SIZE, PINNED_MAP_PATH, BPF_MAP_TYPE_HASH));
181 checkMapValid(testMap2);
182 EXPECT_EQ(0, testMap2.getPinnedPath().compare(PINNED_MAP_PATH));
183 uint32_t key = TEST_KEY1;
184 uint32_t value = TEST_VALUE1;
185 writeToMapAndCheck(testMap1, key, value);
186 StatusOr<uint32_t> value_read = testMap2.readValue(key);
187 checkValueAndStatus(value, value_read);
188}
189
190TEST_F(BpfMapTest, iterate) {
191 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) {
209 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 const uint32_t& value,
215 BpfMap<uint32_t, uint32_t>& map) {
216 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
217 EXPECT_EQ(value, key * 10);
218 totalCount++;
219 totalSum += value;
220 return map.deleteValue(key);
221 };
222 EXPECT_OK(testMap.iterateWithValue(iterateWithDeletion));
223 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
224 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
225 expectMapEmpty(testMap);
226}
227
228TEST_F(BpfMapTest, mapIsEmpty) {
229 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
230 expectMapEmpty(testMap);
231 uint32_t key = TEST_KEY1;
232 uint32_t value_write = TEST_VALUE1;
233 writeToMapAndCheck(testMap, key, value_write);
234 auto isEmpty = testMap.isEmpty();
235 ASSERT_TRUE(isOk(isEmpty));
236 ASSERT_FALSE(isEmpty.value());
237 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
238 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
239 ASSERT_EQ(ENOENT, errno);
240 expectMapEmpty(testMap);
241 int entriesSeen = 0;
242 EXPECT_OK(testMap.iterate(
243 [&entriesSeen](const unsigned int&,
244 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
245 entriesSeen++;
246 return netdutils::status::ok;
247 }));
248 EXPECT_EQ(0, entriesSeen);
249 EXPECT_OK(testMap.iterateWithValue(
250 [&entriesSeen](const unsigned int&, const unsigned int&,
251 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
252 entriesSeen++;
253 return netdutils::status::ok;
254 }));
255 EXPECT_EQ(0, entriesSeen);
256}
257
258TEST_F(BpfMapTest, mapClear) {
259 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
260 populateMap(TEST_MAP_SIZE, testMap);
261 auto isEmpty = testMap.isEmpty();
262 ASSERT_TRUE(isOk(isEmpty));
263 ASSERT_FALSE(isEmpty.value());
264 ASSERT_TRUE(isOk(testMap.clear()));
265 expectMapEmpty(testMap);
266}
267
268} // namespace bpf
269} // namespace android