1 /*
2 * Copyright (C) 2022 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 <android-base/file.h>
18 #include <android-base/macros.h>
19 #include <android-base/result-gmock.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "BpfSyscallWrappers.h"
26 #include "bpf/BpfRingbuf.h"
27 #include "bpf/BpfUtils.h"
28
29 #define TEST_RINGBUF_MAGIC_NUM 12345
30
31 namespace android {
32 namespace bpf {
33 using ::android::base::testing::HasError;
34 using ::android::base::testing::HasValue;
35 using ::android::base::testing::WithCode;
36 using ::testing::AllOf;
37 using ::testing::Gt;
38 using ::testing::HasSubstr;
39 using ::testing::Lt;
40
41 class BpfRingbufTest : public ::testing::Test {
42 protected:
BpfRingbufTest()43 BpfRingbufTest()
44 : mProgPath("/sys/fs/bpf/prog_bpfRingbufProg_skfilter_ringbuf_test"),
45 mRingbufPath("/sys/fs/bpf/map_bpfRingbufProg_test_ringbuf") {}
46
SetUp()47 void SetUp() {
48 if (!android::bpf::isAtLeastKernelVersion(5, 8, 0)) {
49 GTEST_SKIP() << "BPF ring buffers not supported below 5.8";
50 }
51
52 if (sizeof(unsigned long) != 8) {
53 GTEST_SKIP() << "BPF ring buffers not supported on 32 bit arch";
54 }
55
56 errno = 0;
57 mProgram.reset(retrieveProgram(mProgPath.c_str()));
58 EXPECT_EQ(errno, 0);
59 ASSERT_GE(mProgram.get(), 0)
60 << mProgPath << " was either not found or inaccessible.";
61 }
62
RunProgram()63 void RunProgram() {
64 char fake_skb[128] = {};
65 EXPECT_EQ(runProgram(mProgram, fake_skb, sizeof(fake_skb)), 0);
66 }
67
RunTestN(int n)68 void RunTestN(int n) {
69 int run_count = 0;
70 uint64_t output = 0;
71 auto callback = [&](const uint64_t& value) {
72 output = value;
73 run_count++;
74 };
75
76 auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
77 ASSERT_RESULT_OK(result);
78
79 for (int i = 0; i < n; i++) {
80 RunProgram();
81 }
82
83 EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(n));
84 EXPECT_EQ(output, TEST_RINGBUF_MAGIC_NUM);
85 EXPECT_EQ(run_count, n);
86 }
87
88 std::string mProgPath;
89 std::string mRingbufPath;
90 android::base::unique_fd mProgram;
91 };
92
TEST_F(BpfRingbufTest,ConsumeSingle)93 TEST_F(BpfRingbufTest, ConsumeSingle) { RunTestN(1); }
TEST_F(BpfRingbufTest,ConsumeMultiple)94 TEST_F(BpfRingbufTest, ConsumeMultiple) { RunTestN(3); }
95
TEST_F(BpfRingbufTest,FillAndWrap)96 TEST_F(BpfRingbufTest, FillAndWrap) {
97 int run_count = 0;
98 auto callback = [&](const uint64_t&) { run_count++; };
99
100 auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
101 ASSERT_RESULT_OK(result);
102
103 // 4kb buffer with 16 byte payloads (8 byte data, 8 byte header) should fill
104 // after 255 iterations. Exceed that so that some events are dropped.
105 constexpr int iterations = 300;
106 for (int i = 0; i < iterations; i++) {
107 RunProgram();
108 }
109
110 // Some events were dropped, but consume all that succeeded.
111 EXPECT_THAT(result.value()->ConsumeAll(callback),
112 HasValue(AllOf(Gt(250), Lt(260))));
113 EXPECT_THAT(run_count, AllOf(Gt(250), Lt(260)));
114
115 // After consuming everything, we should be able to use the ring buffer again.
116 run_count = 0;
117 RunProgram();
118 EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(1));
119 EXPECT_EQ(run_count, 1);
120 }
121
TEST_F(BpfRingbufTest,WrongTypeSize)122 TEST_F(BpfRingbufTest, WrongTypeSize) {
123 // The program under test writes 8-byte uint64_t values so a ringbuffer for
124 // 1-byte uint8_t values will fail to read from it. Note that the map_def does
125 // not specify the value size, so we fail on read, not creation.
126 auto result = BpfRingbuf<uint8_t>::Create(mRingbufPath.c_str());
127 ASSERT_RESULT_OK(result);
128
129 RunProgram();
130
131 EXPECT_THAT(result.value()->ConsumeAll([](const uint8_t&) {}),
132 HasError(WithCode(EMSGSIZE)));
133 }
134
TEST_F(BpfRingbufTest,InvalidPath)135 TEST_F(BpfRingbufTest, InvalidPath) {
136 EXPECT_THAT(BpfRingbuf<int>::Create("/sys/fs/bpf/bad_path"),
137 HasError(WithCode(ENOENT)));
138 }
139
140 } // namespace bpf
141 } // namespace android
142