blob: cfa9d888780e19e6ea574091f1f285ad3c98f25e [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());
Chenbo Feng75b410b2018-10-10 15:01:19 -070079 }
80
81 void checkMapValid(BpfMap<uint32_t, uint32_t>& map) {
82 EXPECT_LE(0, map.getMap().get());
83 EXPECT_TRUE(map.isValid());
84 }
85
86 void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
87 ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
88 uint32_t value_read;
89 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
90 checkValueAndStatus(value, value_read);
91 }
92
93 void checkValueAndStatus(uint32_t refValue, StatusOr<uint32_t> value) {
94 ASSERT_TRUE(isOk(value.status()));
95 ASSERT_EQ(refValue, value.value());
96 }
97
98 void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
99 for (uint32_t key = 0; key < total; key++) {
100 uint32_t value = key * 10;
101 EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
102 }
103 }
104
105 void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
106 auto isEmpty = map.isEmpty();
107 ASSERT_TRUE(isOk(isEmpty));
108 ASSERT_TRUE(isEmpty.value());
109 }
110};
111
112TEST_F(BpfMapTest, constructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800113 SKIP_IF_BPF_NOT_SUPPORTED;
114
Chenbo Feng75b410b2018-10-10 15:01:19 -0700115 BpfMap<uint32_t, uint32_t> testMap1;
116 checkMapInvalid(testMap1);
117
118 BpfMap<uint32_t, uint32_t> testMap2(mMapFd);
119 checkMapValid(testMap2);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700120
121 BpfMap<uint32_t, uint32_t> testMap3(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
122 checkMapValid(testMap3);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700123}
124
125TEST_F(BpfMapTest, basicHelpers) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800126 SKIP_IF_BPF_NOT_SUPPORTED;
127
Chenbo Feng75b410b2018-10-10 15:01:19 -0700128 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
129 uint32_t key = TEST_KEY1;
130 uint32_t value_write = TEST_VALUE1;
131 writeToMapAndCheck(testMap, key, value_write);
132 StatusOr<uint32_t> value_read = testMap.readValue(key);
133 checkValueAndStatus(value_write, value_read);
134 StatusOr<uint32_t> key_read = testMap.getFirstKey();
135 checkValueAndStatus(key, key_read);
136 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
137 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
138 ASSERT_EQ(ENOENT, errno);
139}
140
141TEST_F(BpfMapTest, reset) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800142 SKIP_IF_BPF_NOT_SUPPORTED;
143
Chenbo Feng75b410b2018-10-10 15:01:19 -0700144 BpfMap<uint32_t, uint32_t> testMap;
145 testMap.reset(mMapFd);
146 uint32_t key = TEST_KEY1;
147 uint32_t value_write = TEST_VALUE1;
148 writeToMapAndCheck(testMap, key, value_write);
149 testMap.reset();
150 checkMapInvalid(testMap);
151 unique_fd invalidFd(mMapFd);
152 ASSERT_GT(0, findMapEntry(invalidFd, &key, &value_write));
153 ASSERT_EQ(EBADF, errno);
154}
155
156TEST_F(BpfMapTest, moveConstructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800157 SKIP_IF_BPF_NOT_SUPPORTED;
158
Chenbo Feng75b410b2018-10-10 15:01:19 -0700159 BpfMap<uint32_t, uint32_t> testMap1(mMapFd);
160 BpfMap<uint32_t, uint32_t> testMap2;
161 testMap2 = std::move(testMap1);
162 uint32_t key = TEST_KEY1;
163 checkMapInvalid(testMap1);
164 uint32_t value = TEST_VALUE1;
165 writeToMapAndCheck(testMap2, key, value);
166}
167
Chenbo Feng75b410b2018-10-10 15:01:19 -0700168TEST_F(BpfMapTest, SetUpMap) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800169 SKIP_IF_BPF_NOT_SUPPORTED;
170
Maciej Żenczykowski04d88b72019-04-01 10:34:26 -0700171 EXPECT_NE(0, access(PINNED_MAP_PATH, R_OK));
172 BpfMap<uint32_t, uint32_t> testMap1(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
173 ASSERT_EQ(0, bpfFdPin(testMap1.getMap(), PINNED_MAP_PATH));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700174 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
175 checkMapValid(testMap1);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700176 BpfMap<uint32_t, uint32_t> testMap2;
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700177 EXPECT_OK(testMap2.init(PINNED_MAP_PATH));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700178 checkMapValid(testMap2);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700179 uint32_t key = TEST_KEY1;
180 uint32_t value = TEST_VALUE1;
181 writeToMapAndCheck(testMap1, key, value);
182 StatusOr<uint32_t> value_read = testMap2.readValue(key);
183 checkValueAndStatus(value, value_read);
184}
185
186TEST_F(BpfMapTest, iterate) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800187 SKIP_IF_BPF_NOT_SUPPORTED;
188
Chenbo Feng75b410b2018-10-10 15:01:19 -0700189 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
190 populateMap(TEST_MAP_SIZE, testMap);
191 int totalCount = 0;
192 int totalSum = 0;
193 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
194 BpfMap<uint32_t, uint32_t>& map) {
195 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
196 totalCount++;
197 totalSum += key;
198 return map.deleteValue(key);
199 };
200 EXPECT_OK(testMap.iterate(iterateWithDeletion));
201 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
202 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
203 expectMapEmpty(testMap);
204}
205
206TEST_F(BpfMapTest, iterateWithValue) {
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 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) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800229 SKIP_IF_BPF_NOT_SUPPORTED;
230
Chenbo Feng75b410b2018-10-10 15:01:19 -0700231 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
232 expectMapEmpty(testMap);
233 uint32_t key = TEST_KEY1;
234 uint32_t value_write = TEST_VALUE1;
235 writeToMapAndCheck(testMap, key, value_write);
236 auto isEmpty = testMap.isEmpty();
237 ASSERT_TRUE(isOk(isEmpty));
238 ASSERT_FALSE(isEmpty.value());
239 ASSERT_TRUE(isOk(testMap.deleteValue(key)));
240 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
241 ASSERT_EQ(ENOENT, errno);
242 expectMapEmpty(testMap);
243 int entriesSeen = 0;
244 EXPECT_OK(testMap.iterate(
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800245 [&entriesSeen](const unsigned int&,
246 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
247 entriesSeen++;
248 return netdutils::status::ok;
249 }));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700250 EXPECT_EQ(0, entriesSeen);
251 EXPECT_OK(testMap.iterateWithValue(
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800252 [&entriesSeen](const unsigned int&, const unsigned int&,
253 const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
254 entriesSeen++;
255 return netdutils::status::ok;
256 }));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700257 EXPECT_EQ(0, entriesSeen);
258}
259
260TEST_F(BpfMapTest, mapClear) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800261 SKIP_IF_BPF_NOT_SUPPORTED;
262
Chenbo Feng75b410b2018-10-10 15:01:19 -0700263 BpfMap<uint32_t, uint32_t> testMap(mMapFd);
264 populateMap(TEST_MAP_SIZE, testMap);
265 auto isEmpty = testMap.isEmpty();
266 ASSERT_TRUE(isOk(isEmpty));
267 ASSERT_FALSE(isEmpty.value());
268 ASSERT_TRUE(isOk(testMap.clear()));
269 expectMapEmpty(testMap);
270}
271
272} // namespace bpf
273} // namespace android