blob: f348333241641722833e7a7950ecffb563e770c6 [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
Patrick Rohr311f8b32023-05-15 19:58:49 -070053 if (!isKernel64Bit()) {
54 GTEST_SKIP() << "BPF ring buffers not supported on 32 bit kernel";
Ryan Zuklieccd5eb92022-11-30 10:21:47 -080055 }
56
57 errno = 0;
58 mProgram.reset(retrieveProgram(mProgPath.c_str()));
59 EXPECT_EQ(errno, 0);
60 ASSERT_GE(mProgram.get(), 0)
61 << mProgPath << " was either not found or inaccessible.";
62 }
63
Ryan Zuklie2669e242022-11-30 11:12:41 -080064 void RunProgram() {
65 char fake_skb[128] = {};
66 EXPECT_EQ(runProgram(mProgram, fake_skb, sizeof(fake_skb)), 0);
67 }
68
69 void RunTestN(int n) {
70 int run_count = 0;
71 uint64_t output = 0;
72 auto callback = [&](const uint64_t& value) {
73 output = value;
74 run_count++;
75 };
76
77 auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
78 ASSERT_RESULT_OK(result);
79
80 for (int i = 0; i < n; i++) {
81 RunProgram();
82 }
83
84 EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(n));
85 EXPECT_EQ(output, TEST_RINGBUF_MAGIC_NUM);
86 EXPECT_EQ(run_count, n);
87 }
88
Ryan Zuklieccd5eb92022-11-30 10:21:47 -080089 std::string mProgPath;
90 std::string mRingbufPath;
91 android::base::unique_fd mProgram;
92};
93
Ryan Zuklie2669e242022-11-30 11:12:41 -080094TEST_F(BpfRingbufTest, ConsumeSingle) { RunTestN(1); }
95TEST_F(BpfRingbufTest, ConsumeMultiple) { RunTestN(3); }
96
97TEST_F(BpfRingbufTest, FillAndWrap) {
98 int run_count = 0;
99 auto callback = [&](const uint64_t&) { run_count++; };
100
101 auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
102 ASSERT_RESULT_OK(result);
103
104 // 4kb buffer with 16 byte payloads (8 byte data, 8 byte header) should fill
105 // after 255 iterations. Exceed that so that some events are dropped.
106 constexpr int iterations = 300;
107 for (int i = 0; i < iterations; i++) {
108 RunProgram();
109 }
110
111 // Some events were dropped, but consume all that succeeded.
112 EXPECT_THAT(result.value()->ConsumeAll(callback),
113 HasValue(AllOf(Gt(250), Lt(260))));
114 EXPECT_THAT(run_count, AllOf(Gt(250), Lt(260)));
115
116 // After consuming everything, we should be able to use the ring buffer again.
117 run_count = 0;
118 RunProgram();
119 EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(1));
120 EXPECT_EQ(run_count, 1);
121}
122
123TEST_F(BpfRingbufTest, WrongTypeSize) {
124 // The program under test writes 8-byte uint64_t values so a ringbuffer for
125 // 1-byte uint8_t values will fail to read from it. Note that the map_def does
126 // not specify the value size, so we fail on read, not creation.
127 auto result = BpfRingbuf<uint8_t>::Create(mRingbufPath.c_str());
128 ASSERT_RESULT_OK(result);
129
130 RunProgram();
131
132 EXPECT_THAT(result.value()->ConsumeAll([](const uint8_t&) {}),
133 HasError(WithCode(EMSGSIZE)));
134}
135
136TEST_F(BpfRingbufTest, InvalidPath) {
137 EXPECT_THAT(BpfRingbuf<int>::Create("/sys/fs/bpf/bad_path"),
138 HasError(WithCode(ENOENT)));
139}
Ryan Zuklieccd5eb92022-11-30 10:21:47 -0800140
141} // namespace bpf
142} // namespace android