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 const int NUM_CPUS = sysconf(_SC_NPROCESSORS_ONLN);
84 const int NUM_SOCKETS = std::min(NUM_CPUS, MAX_NUM_SOCKETS);
85 
86 class BpfRaceTest : public ::testing::Test {
87  protected:
BpfRaceTest()88   BpfRaceTest() {}
89   BpfMap<uint64_t, stats_value> cookieStatsMap[2];
90   BpfMap<uint32_t, uint32_t> configurationMap;
91   bool stop;
92   std::thread *tds = new std::thread[NUM_SOCKETS];
93 
workerThread(int prog_fd,bool * stop)94   static void workerThread(int prog_fd, bool *stop) {
95     struct sockaddr_in6 remote = {.sin6_family = AF_INET6};
96     struct sockaddr_in6 local;
97     uint64_t j = 0;
98     int recvSock, sendSock, recv_len;
99     char buf[strlen("msg: 18446744073709551615")];
100     int res;
101     socklen_t slen = sizeof(remote);
102 
103     recvSock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
104     EXPECT_NE(-1, recvSock);
105     std::string address = android::base::StringPrintf("::1");
106     EXPECT_NE(0, inet_pton(AF_INET6, address.c_str(), &remote.sin6_addr));
107     EXPECT_NE(-1, bind(recvSock, (struct sockaddr *)&remote, sizeof(remote)));
108     EXPECT_EQ(0, getsockname(recvSock, (struct sockaddr *)&remote, &slen));
109     sendSock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
110     EXPECT_NE(-1, sendSock) << "send socket create failed!\n";
111     EXPECT_NE(-1, setsockopt(recvSock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
112                              sizeof(prog_fd)))
113         << "attach bpf program failed: "
114         << android::base::StringPrintf("%s\n", strerror(errno));
115 
116     // Keep sending and receiving packet until test end.
117     while (!*stop) {
118       std::string id = android::base::StringPrintf("msg: %" PRIu64 "\n", j);
119       res = sendto(sendSock, &id, id.length(), 0, (struct sockaddr *)&remote,
120                    slen);
121       EXPECT_EQ(id.size(), res);
122       recv_len = recvfrom(recvSock, &buf, sizeof(buf), 0,
123                           (struct sockaddr *)&local, &slen);
124       EXPECT_EQ(id.size(), recv_len);
125     }
126   }
127 
SetUp()128   void SetUp() {
129     EXPECT_EQ(0, setrlimitForTest());
130     int ret = access(TEST_PROG_PATH, R_OK);
131     // Always create a new program and remove the pinned program after program
132     // loading is done.
133     if (ret == 0) {
134       remove(TEST_PROG_PATH);
135     }
136     std::string progSrcPath = BPF_SRC_PATH BPF_SRC_NAME;
137     // 0 != 2 means ENOENT - ie. missing bpf program.
138     ASSERT_EQ(0, access(progSrcPath.c_str(), R_OK) ? errno : 0);
139     bool critical = false;
140     ASSERT_EQ(0, android::bpf::loadProg(progSrcPath.c_str(), &critical));
141     ASSERT_EQ(true, critical);
142 
143     errno = 0;
144     int prog_fd = retrieveProgram(TEST_PROG_PATH);
145     EXPECT_EQ(0, errno);
146     ASSERT_LE(3, prog_fd);
147 
148     EXPECT_RESULT_OK(cookieStatsMap[0].init(TEST_STATS_MAP_A_PATH));
149     EXPECT_RESULT_OK(cookieStatsMap[1].init(TEST_STATS_MAP_B_PATH));
150     EXPECT_RESULT_OK(configurationMap.init(TEST_CONFIGURATION_MAP_PATH));
151     EXPECT_TRUE(cookieStatsMap[0].isValid());
152     EXPECT_TRUE(cookieStatsMap[1].isValid());
153     EXPECT_TRUE(configurationMap.isValid());
154     EXPECT_RESULT_OK(configurationMap.writeValue(ACTIVE_MAP_KEY, 0, BPF_ANY));
155 
156     // Start several threads to send and receive packets with an eBPF program
157     // attached to the socket.
158     stop = false;
159 
160     for (int i = 0; i < NUM_SOCKETS; i++) {
161       tds[i] = std::thread(workerThread, prog_fd, &stop);
162     }
163   }
164 
TearDown()165   void TearDown() {
166     // Stop the threads and clean up the program.
167     stop = true;
168     for (int i = 0; i < NUM_SOCKETS; i++) {
169       if (tds[i].joinable()) tds[i].join();
170     }
171     delete [] tds;
172     remove(TEST_PROG_PATH);
173     remove(TEST_STATS_MAP_A_PATH);
174     remove(TEST_STATS_MAP_B_PATH);
175     remove(TEST_CONFIGURATION_MAP_PATH);
176   }
177 
swapAndCleanStatsMap(bool expectSynchronized,int seconds)178   void swapAndCleanStatsMap(bool expectSynchronized, int seconds) {
179     uint64_t i = 0;
180     auto test_start = std::chrono::system_clock::now();
181     while ((std::chrono::duration_cast<std::chrono::milliseconds>(
182                 std::chrono::system_clock::now() - test_start)
183                 .count() /
184             1000) < seconds) {
185       // Check if the vacant map is empty based on the current configuration.
186       auto isEmpty = cookieStatsMap[i].isEmpty();
187       ASSERT_RESULT_OK(isEmpty);
188       if (expectSynchronized) {
189         // The map should always be empty because synchronizeKernelRCU should
190         // ensure that the BPF programs running on all cores have seen the write
191         // to the configuration map that tells them to write to the other map.
192         // If it's not empty, fail.
193         ASSERT_TRUE(isEmpty.value())
194             << "Race problem between stats clean and updates";
195       } else if (!isEmpty.value()) {
196         // We found a race condition, which is expected (eventually) because
197         // we're not calling synchronizeKernelRCU. Pass the test.
198         break;
199       }
200 
201       // Change the configuration and wait for rcu grace period.
202       i ^= 1;
203       ASSERT_RESULT_OK(configurationMap.writeValue(ACTIVE_MAP_KEY, i, BPF_ANY));
204       if (expectSynchronized) {
205         EXPECT_EQ(0, synchronizeKernelRCU());
206       }
207 
208       // Clean up the previous map after map swap.
209       EXPECT_RESULT_OK(cookieStatsMap[i].clear());
210     }
211     if (!expectSynchronized) {
212       auto test_end = std::chrono::system_clock::now();
213       auto diffSec = test_end - test_start;
214       auto msec =
215           std::chrono::duration_cast<std::chrono::milliseconds>(diffSec);
216       EXPECT_GE(seconds, (double)(msec.count() / 1000.0))
217           << "Race problem didn't happen before time out";
218     }
219   }
220 };
221 
222 // Verify the race problem disappear when the kernel call synchronize_rcu
223 // after changing the active map.
TEST_F(BpfRaceTest,testRaceWithBarrier)224 TEST_F(BpfRaceTest, testRaceWithBarrier) {
225   swapAndCleanStatsMap(true, 30);
226 }
227 
228 // Confirm the race problem exists when the kernel doesn't call synchronize_rcu
229 // after changing the active map.
230 // This test is flaky. Race not triggering isn't really a bug per say...
231 // Maybe we should just outright delete this test...
TEST_F(BpfRaceTest,DISABLED_testRaceWithoutBarrier)232 TEST_F(BpfRaceTest, DISABLED_testRaceWithoutBarrier) {
233   swapAndCleanStatsMap(false, 240);
234 }
235 
236 }  // namespace android
237