blob: 6c28c05862797ef99df0e85eb36a973912eab612 [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
Steven Morelande7cd2a72020-01-10 17:49:35 -080044using base::Result;
Chenbo Feng75b410b2018-10-10 15:01:19 -070045using base::unique_fd;
Chenbo Feng75b410b2018-10-10 15:01:19 -070046
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() {}
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070055
Chenbo Feng75b410b2018-10-10 15:01:19 -070056 void SetUp() {
Chenbo Feng249e2f82018-11-20 17:37:00 -080057 SKIP_IF_BPF_NOT_SUPPORTED;
58
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070059 EXPECT_EQ(0, setrlimitForTest());
Chenbo Feng75b410b2018-10-10 15:01:19 -070060 if (!access(PINNED_MAP_PATH, R_OK)) {
61 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
62 }
Chenbo Feng75b410b2018-10-10 15:01:19 -070063 }
64
65 void TearDown() {
Chenbo Feng249e2f82018-11-20 17:37:00 -080066 SKIP_IF_BPF_NOT_SUPPORTED;
67
Chenbo Feng75b410b2018-10-10 15:01:19 -070068 if (!access(PINNED_MAP_PATH, R_OK)) {
69 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
70 }
Chenbo Feng75b410b2018-10-10 15:01:19 -070071 }
72
73 void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
74 EXPECT_FALSE(map.isValid());
75 EXPECT_EQ(-1, map.getMap().get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070076 }
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) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080084 ASSERT_TRUE(map.writeValue(key, value, BPF_ANY));
Chenbo Feng75b410b2018-10-10 15:01:19 -070085 uint32_t value_read;
86 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
87 checkValueAndStatus(value, value_read);
88 }
89
Steven Morelande7cd2a72020-01-10 17:49:35 -080090 void checkValueAndStatus(uint32_t refValue, Result<uint32_t> value) {
91 ASSERT_TRUE(value);
Chenbo Feng75b410b2018-10-10 15:01:19 -070092 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;
Steven Morelande7cd2a72020-01-10 17:49:35 -080098 EXPECT_TRUE(map.writeValue(key, value, BPF_ANY));
Chenbo Feng75b410b2018-10-10 15:01:19 -070099 }
100 }
101
102 void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800103 Result<bool> isEmpty = map.isEmpty();
104 ASSERT_TRUE(isEmpty);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700105 ASSERT_TRUE(isEmpty.value());
106 }
107};
108
109TEST_F(BpfMapTest, constructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800110 SKIP_IF_BPF_NOT_SUPPORTED;
111
Chenbo Feng75b410b2018-10-10 15:01:19 -0700112 BpfMap<uint32_t, uint32_t> testMap1;
113 checkMapInvalid(testMap1);
114
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800115 BpfMap<uint32_t, uint32_t> testMap2(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700116 checkMapValid(testMap2);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700117}
118
119TEST_F(BpfMapTest, basicHelpers) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800120 SKIP_IF_BPF_NOT_SUPPORTED;
121
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800122 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700123 uint32_t key = TEST_KEY1;
124 uint32_t value_write = TEST_VALUE1;
125 writeToMapAndCheck(testMap, key, value_write);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800126 Result<uint32_t> value_read = testMap.readValue(key);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700127 checkValueAndStatus(value_write, value_read);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800128 Result<uint32_t> key_read = testMap.getFirstKey();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700129 checkValueAndStatus(key, key_read);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800130 ASSERT_TRUE(testMap.deleteValue(key));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700131 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
132 ASSERT_EQ(ENOENT, errno);
133}
134
135TEST_F(BpfMapTest, reset) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800136 SKIP_IF_BPF_NOT_SUPPORTED;
137
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800138 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700139 uint32_t key = TEST_KEY1;
140 uint32_t value_write = TEST_VALUE1;
141 writeToMapAndCheck(testMap, key, value_write);
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -0700142
Chenbo Feng75b410b2018-10-10 15:01:19 -0700143 testMap.reset();
144 checkMapInvalid(testMap);
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -0700145 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700146 ASSERT_EQ(EBADF, errno);
147}
148
149TEST_F(BpfMapTest, moveConstructor) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800150 SKIP_IF_BPF_NOT_SUPPORTED;
151
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800152 BpfMap<uint32_t, uint32_t> testMap1(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700153 BpfMap<uint32_t, uint32_t> testMap2;
154 testMap2 = std::move(testMap1);
155 uint32_t key = TEST_KEY1;
156 checkMapInvalid(testMap1);
157 uint32_t value = TEST_VALUE1;
158 writeToMapAndCheck(testMap2, key, value);
159}
160
Chenbo Feng75b410b2018-10-10 15:01:19 -0700161TEST_F(BpfMapTest, SetUpMap) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800162 SKIP_IF_BPF_NOT_SUPPORTED;
163
Maciej Żenczykowski04d88b72019-04-01 10:34:26 -0700164 EXPECT_NE(0, access(PINNED_MAP_PATH, R_OK));
165 BpfMap<uint32_t, uint32_t> testMap1(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
166 ASSERT_EQ(0, bpfFdPin(testMap1.getMap(), PINNED_MAP_PATH));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700167 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
168 checkMapValid(testMap1);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700169 BpfMap<uint32_t, uint32_t> testMap2;
Steven Morelande7cd2a72020-01-10 17:49:35 -0800170 EXPECT_TRUE(testMap2.init(PINNED_MAP_PATH));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700171 checkMapValid(testMap2);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700172 uint32_t key = TEST_KEY1;
173 uint32_t value = TEST_VALUE1;
174 writeToMapAndCheck(testMap1, key, value);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800175 Result<uint32_t> value_read = testMap2.readValue(key);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700176 checkValueAndStatus(value, value_read);
177}
178
179TEST_F(BpfMapTest, iterate) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800180 SKIP_IF_BPF_NOT_SUPPORTED;
181
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800182 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700183 populateMap(TEST_MAP_SIZE, testMap);
184 int totalCount = 0;
185 int totalSum = 0;
186 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
187 BpfMap<uint32_t, uint32_t>& map) {
188 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
189 totalCount++;
190 totalSum += key;
191 return map.deleteValue(key);
192 };
Steven Morelande7cd2a72020-01-10 17:49:35 -0800193 EXPECT_TRUE(testMap.iterate(iterateWithDeletion));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700194 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
195 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
196 expectMapEmpty(testMap);
197}
198
199TEST_F(BpfMapTest, iterateWithValue) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800200 SKIP_IF_BPF_NOT_SUPPORTED;
201
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800202 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700203 populateMap(TEST_MAP_SIZE, testMap);
204 int totalCount = 0;
205 int totalSum = 0;
206 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
207 const uint32_t& value,
208 BpfMap<uint32_t, uint32_t>& map) {
209 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
210 EXPECT_EQ(value, key * 10);
211 totalCount++;
212 totalSum += value;
213 return map.deleteValue(key);
214 };
Steven Morelande7cd2a72020-01-10 17:49:35 -0800215 EXPECT_TRUE(testMap.iterateWithValue(iterateWithDeletion));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700216 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
217 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
218 expectMapEmpty(testMap);
219}
220
221TEST_F(BpfMapTest, mapIsEmpty) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800222 SKIP_IF_BPF_NOT_SUPPORTED;
223
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800224 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700225 expectMapEmpty(testMap);
226 uint32_t key = TEST_KEY1;
227 uint32_t value_write = TEST_VALUE1;
228 writeToMapAndCheck(testMap, key, value_write);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800229 Result<bool> isEmpty = testMap.isEmpty();
230 ASSERT_TRUE(isEmpty);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700231 ASSERT_FALSE(isEmpty.value());
Steven Morelande7cd2a72020-01-10 17:49:35 -0800232 ASSERT_TRUE(testMap.deleteValue(key));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700233 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
234 ASSERT_EQ(ENOENT, errno);
235 expectMapEmpty(testMap);
236 int entriesSeen = 0;
Steven Morelande7cd2a72020-01-10 17:49:35 -0800237 EXPECT_TRUE(testMap.iterate(
238 [&entriesSeen](const unsigned int&,
239 const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
240 entriesSeen++;
241 return {};
242 }));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700243 EXPECT_EQ(0, entriesSeen);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800244 EXPECT_TRUE(testMap.iterateWithValue(
245 [&entriesSeen](const unsigned int&, const unsigned int&,
246 const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
247 entriesSeen++;
248 return {};
249 }));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700250 EXPECT_EQ(0, entriesSeen);
251}
252
253TEST_F(BpfMapTest, mapClear) {
Chenbo Feng249e2f82018-11-20 17:37:00 -0800254 SKIP_IF_BPF_NOT_SUPPORTED;
255
Maciej Żenczykowski7c9a9842020-01-20 15:51:31 -0800256 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700257 populateMap(TEST_MAP_SIZE, testMap);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800258 Result<bool> isEmpty = testMap.isEmpty();
259 ASSERT_TRUE(isEmpty);
260 ASSERT_FALSE(*isEmpty);
261 ASSERT_TRUE(testMap.clear());
Chenbo Feng75b410b2018-10-10 15:01:19 -0700262 expectMapEmpty(testMap);
263}
264
265} // namespace bpf
266} // namespace android