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