1 /*
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 requied 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 
18 #define LOG_TAG "BpfTest"
19 
20 #include <arpa/inet.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <linux/pfkeyv2.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 
30 #include <thread>
31 
32 #include <android-base/file.h>
33 #include <android-base/stringprintf.h>
34 #include <android-base/unique_fd.h>
35 #include <gtest/gtest.h>
36 #include <utils/Log.h>
37 
38 #include "bpf/BpfMap.h"
39 #include "bpf/BpfUtils.h"
40 #include "kern.h"
41 #include "libbpf_android.h"
42 
43 using android::base::unique_fd;
44 using namespace android::bpf;
45 
46 namespace android {
47 
TEST(BpfTest,bpfMapPinTest)48 TEST(BpfTest, bpfMapPinTest) {
49   EXPECT_EQ(0, setrlimitForTest());
50   const char* bpfMapPath = "/sys/fs/bpf/testMap";
51   int ret = access(bpfMapPath, F_OK);
52   if (!ret) {
53     ASSERT_EQ(0, remove(bpfMapPath));
54   } else {
55     ASSERT_EQ(errno, ENOENT);
56   }
57 
58   android::base::unique_fd mapfd(createMap(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
59                                            sizeof(uint32_t), 10,
60                                            BPF_F_NO_PREALLOC));
61   ASSERT_LT(0, mapfd) << "create map failed with error: " << strerror(errno);
62   ASSERT_EQ(0, bpfFdPin(mapfd, bpfMapPath))
63       << "pin map failed with error: " << strerror(errno);
64   ASSERT_EQ(0, access(bpfMapPath, F_OK));
65   ASSERT_EQ(0, remove(bpfMapPath));
66 }
67 
68 #define BPF_SRC_PATH "/data/local/tmp"
69 
70 #if defined(__aarch64__) || defined(__x86_64__)
71 #define BPF_SRC_NAME "/64/kern.o"
72 #else
73 #define BPF_SRC_NAME "/32/kern.o"
74 #endif
75 
76 #define BPF_PATH "/sys/fs/bpf"
77 #define TEST_PROG_PATH BPF_PATH "/prog_kern_skfilter_test"
78 #define TEST_STATS_MAP_A_PATH BPF_PATH "/map_kern_test_stats_map_A"
79 #define TEST_STATS_MAP_B_PATH BPF_PATH "/map_kern_test_stats_map_B"
80 #define TEST_CONFIGURATION_MAP_PATH BPF_PATH "/map_kern_test_configuration_map"
81 
82 constexpr int ACTIVE_MAP_KEY = 1;
83 
84 class BpfRaceTest : public ::testing::Test {
85  protected:
BpfRaceTest()86   BpfRaceTest() {}
87   BpfMap<uint64_t, stats_value> cookieStatsMap[2];
88   BpfMap<uint32_t, uint32_t> configurationMap;
89   bool stop;
90   std::thread tds[NUM_SOCKETS];
91 
workerThread(int prog_fd,bool * stop)92   static void workerThread(int prog_fd, bool *stop) {
93     struct sockaddr_in6 remote = {.sin6_family = AF_INET6};
94     struct sockaddr_in6 local;
95     uint64_t j = 0;
96     int recvSock, sendSock, recv_len;
97     char buf[strlen("msg: 18446744073709551615")];
98     int res;
99     socklen_t slen = sizeof(remote);
100 
101     recvSock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
102     EXPECT_NE(-1, recvSock);
103     std::string address = android::base::StringPrintf("::1");
104     EXPECT_NE(0, inet_pton(AF_INET6, address.c_str(), &remote.sin6_addr));
105     EXPECT_NE(-1, bind(recvSock, (struct sockaddr *)&remote, sizeof(remote)));
106     EXPECT_EQ(0, getsockname(recvSock, (struct sockaddr *)&remote, &slen));
107     sendSock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
108     EXPECT_NE(-1, sendSock) << "send socket create failed!\n";
109     EXPECT_NE(-1, setsockopt(recvSock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
110                              sizeof(prog_fd)))
111         << "attach bpf program failed: "
112         << android::base::StringPrintf("%s\n", strerror(errno));
113 
114     // Keep sending and receiving packet until test end.
115     while (!*stop) {
116       std::string id = android::base::StringPrintf("msg: %" PRIu64 "\n", j);
117       res = sendto(sendSock, &id, id.length(), 0, (struct sockaddr *)&remote,
118                    slen);
119       EXPECT_EQ(id.size(), res);
120       recv_len = recvfrom(recvSock, &buf, sizeof(buf), 0,
121                           (struct sockaddr *)&local, &slen);
122       EXPECT_EQ(id.size(), recv_len);
123     }
124   }
125 
SetUp()126   void SetUp() {
127     EXPECT_EQ(0, setrlimitForTest());
128     int ret = access(TEST_PROG_PATH, R_OK);
129     // Always create a new program and remove the pinned program after program
130     // loading is done.
131     if (ret == 0) {
132       remove(TEST_PROG_PATH);
133     }
134     std::string progSrcPath = BPF_SRC_PATH BPF_SRC_NAME;
135     // 0 != 2 means ENOENT - ie. missing bpf program.
136     ASSERT_EQ(0, access(progSrcPath.c_str(), R_OK) ? errno : 0);
137     bool critical = false;
138     ASSERT_EQ(0, android::bpf::loadProg(progSrcPath.c_str(), &critical));
139     ASSERT_EQ(true, critical);
140 
141     errno = 0;
142     int prog_fd = retrieveProgram(TEST_PROG_PATH);
143     EXPECT_EQ(0, errno);
144     ASSERT_LE(3, prog_fd);
145 
146     EXPECT_RESULT_OK(cookieStatsMap[0].init(TEST_STATS_MAP_A_PATH));
147     EXPECT_RESULT_OK(cookieStatsMap[1].init(TEST_STATS_MAP_B_PATH));
148     EXPECT_RESULT_OK(configurationMap.init(TEST_CONFIGURATION_MAP_PATH));
149     EXPECT_TRUE(cookieStatsMap[0].isValid());
150     EXPECT_TRUE(cookieStatsMap[1].isValid());
151     EXPECT_TRUE(configurationMap.isValid());
152     EXPECT_RESULT_OK(configurationMap.writeValue(ACTIVE_MAP_KEY, 0, BPF_ANY));
153 
154     // Start several threads to send and receive packets with an eBPF program
155     // attached to the socket.
156     stop = false;
157 
158     for (int i = 0; i < NUM_SOCKETS; i++) {
159       tds[i] = std::thread(workerThread, prog_fd, &stop);
160     }
161   }
162 
TearDown()163   void TearDown() {
164     // Stop the threads and clean up the program.
165     stop = true;
166     for (int i = 0; i < NUM_SOCKETS; i++) {
167       if (tds[i].joinable()) tds[i].join();
168     }
169     remove(TEST_PROG_PATH);
170     remove(TEST_STATS_MAP_A_PATH);
171     remove(TEST_STATS_MAP_B_PATH);
172     remove(TEST_CONFIGURATION_MAP_PATH);
173   }
174 
swapAndCleanStatsMap(bool expectSynchronized,int seconds)175   void swapAndCleanStatsMap(bool expectSynchronized, int seconds) {
176     uint64_t i = 0;
177     auto test_start = std::chrono::system_clock::now();
178     while ((std::chrono::duration_cast<std::chrono::milliseconds>(
179                 std::chrono::system_clock::now() - test_start)
180                 .count() /
181             1000) < seconds) {
182       // Check if the vacant map is empty based on the current configuration.
183       auto isEmpty = cookieStatsMap[i].isEmpty();
184       ASSERT_RESULT_OK(isEmpty);
185       if (expectSynchronized) {
186         // The map should always be empty because synchronizeKernelRCU should
187         // ensure that the BPF programs running on all cores have seen the write
188         // to the configuration map that tells them to write to the other map.
189         // If it's not empty, fail.
190         ASSERT_TRUE(isEmpty.value())
191             << "Race problem between stats clean and updates";
192       } else if (!isEmpty.value()) {
193         // We found a race condition, which is expected (eventually) because
194         // we're not calling synchronizeKernelRCU. Pass the test.
195         break;
196       }
197 
198       // Change the configuration and wait for rcu grace period.
199       i ^= 1;
200       ASSERT_RESULT_OK(configurationMap.writeValue(ACTIVE_MAP_KEY, i, BPF_ANY));
201       if (expectSynchronized) {
202         EXPECT_EQ(0, synchronizeKernelRCU());
203       }
204 
205       // Clean up the previous map after map swap.
206       EXPECT_RESULT_OK(cookieStatsMap[i].clear());
207     }
208     if (!expectSynchronized) {
209       auto test_end = std::chrono::system_clock::now();
210       auto diffSec = test_end - test_start;
211       auto msec =
212           std::chrono::duration_cast<std::chrono::milliseconds>(diffSec);
213       EXPECT_GE(seconds, (double)(msec.count() / 1000.0))
214           << "Race problem didn't happen before time out";
215     }
216   }
217 };
218 
219 // Verify the race problem disappear when the kernel call synchronize_rcu
220 // after changing the active map.
TEST_F(BpfRaceTest,testRaceWithBarrier)221 TEST_F(BpfRaceTest, testRaceWithBarrier) {
222   swapAndCleanStatsMap(true, 30);
223 }
224 
225 // Confirm the race problem exists when the kernel doesn't call synchronize_rcu
226 // after changing the active map.
227 // This test is flaky. Race not triggering isn't really a bug per say...
228 // Maybe we should just outright delete this test...
TEST_F(BpfRaceTest,testRaceWithoutBarrier)229 TEST_F(BpfRaceTest, testRaceWithoutBarrier) {
230   swapAndCleanStatsMap(false, 240);
231 }
232 
233 }  // namespace android
234