| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 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 |  | 
| Maciej Żenczykowski | 6776e3b | 2022-12-10 12:18:23 +0000 | [diff] [blame] | 25 | #include "BpfSyscallWrappers.h" | 
| Ryan Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 26 | #include "bpf/BpfRingbuf.h" | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 27 | #include "bpf/BpfUtils.h" | 
| Patrick Rohr | 311f8b3 | 2023-05-15 19:58:49 -0700 | [diff] [blame] | 28 | #include "bpf/KernelUtils.h" | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 29 |  | 
| Ryan Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 30 | #define TEST_RINGBUF_MAGIC_NUM 12345 | 
|  | 31 |  | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 32 | namespace android { | 
|  | 33 | namespace bpf { | 
|  | 34 | using ::android::base::testing::HasError; | 
|  | 35 | using ::android::base::testing::HasValue; | 
| Ryan Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 36 | using ::android::base::testing::WithCode; | 
|  | 37 | using ::testing::AllOf; | 
|  | 38 | using ::testing::Gt; | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 39 | using ::testing::HasSubstr; | 
| Ryan Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 40 | using ::testing::Lt; | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | class 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 Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 50 | GTEST_SKIP() << "BPF ring buffers not supported below 5.8"; | 
|  | 51 | } | 
|  | 52 |  | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 53 | 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 Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 60 | 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 Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 85 | std::string mProgPath; | 
|  | 86 | std::string mRingbufPath; | 
|  | 87 | android::base::unique_fd mProgram; | 
|  | 88 | }; | 
|  | 89 |  | 
| Ryan Zuklie | 2669e24 | 2022-11-30 11:12:41 -0800 | [diff] [blame] | 90 | TEST_F(BpfRingbufTest, ConsumeSingle) { RunTestN(1); } | 
|  | 91 | TEST_F(BpfRingbufTest, ConsumeMultiple) { RunTestN(3); } | 
|  | 92 |  | 
|  | 93 | TEST_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 |  | 
|  | 119 | TEST_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 |  | 
|  | 132 | TEST_F(BpfRingbufTest, InvalidPath) { | 
|  | 133 | EXPECT_THAT(BpfRingbuf<int>::Create("/sys/fs/bpf/bad_path"), | 
|  | 134 | HasError(WithCode(ENOENT))); | 
|  | 135 | } | 
| Ryan Zuklie | ccd5eb9 | 2022-11-30 10:21:47 -0800 | [diff] [blame] | 136 |  | 
|  | 137 | }  // namespace bpf | 
|  | 138 | }  // namespace android |