summaryrefslogtreecommitdiff
path: root/common/native/bpf_headers/BpfRingbufTest.cpp
blob: 6c0841c614762e7cf4cc002006cdf54d90e0f560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/file.h>
#include <android-base/macros.h>
#include <android-base/result-gmock.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdlib.h>
#include <unistd.h>

#include "BpfSyscallWrappers.h"
#include "bpf/BpfRingbuf.h"
#include "bpf/BpfUtils.h"
#include "bpf/KernelUtils.h"

#define TEST_RINGBUF_MAGIC_NUM 12345

namespace android {
namespace bpf {
using ::android::base::testing::HasError;
using ::android::base::testing::HasValue;
using ::android::base::testing::WithCode;
using ::testing::AllOf;
using ::testing::Gt;
using ::testing::HasSubstr;
using ::testing::Lt;

class BpfRingbufTest : public ::testing::Test {
 protected:
  BpfRingbufTest()
      : mProgPath("/sys/fs/bpf/prog_bpfRingbufProg_skfilter_ringbuf_test"),
        mRingbufPath("/sys/fs/bpf/map_bpfRingbufProg_test_ringbuf") {}

  void SetUp() {
    if (!android::bpf::isAtLeastKernelVersion(5, 8, 0)) {
      GTEST_SKIP() << "BPF ring buffers not supported below 5.8";
    }

    errno = 0;
    mProgram.reset(retrieveProgram(mProgPath.c_str()));
    EXPECT_EQ(errno, 0);
    ASSERT_GE(mProgram.get(), 0)
        << mProgPath << " was either not found or inaccessible.";
  }

  void RunProgram() {
    char fake_skb[128] = {};
    EXPECT_EQ(runProgram(mProgram, fake_skb, sizeof(fake_skb)), 0);
  }

  void RunTestN(int n) {
    int run_count = 0;
    uint64_t output = 0;
    auto callback = [&](const uint64_t& value) {
      output = value;
      run_count++;
    };

    auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
    ASSERT_RESULT_OK(result);

    for (int i = 0; i < n; i++) {
      RunProgram();
    }

    EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(n));
    EXPECT_EQ(output, TEST_RINGBUF_MAGIC_NUM);
    EXPECT_EQ(run_count, n);
  }

  std::string mProgPath;
  std::string mRingbufPath;
  android::base::unique_fd mProgram;
};

TEST_F(BpfRingbufTest, ConsumeSingle) { RunTestN(1); }
TEST_F(BpfRingbufTest, ConsumeMultiple) { RunTestN(3); }

TEST_F(BpfRingbufTest, FillAndWrap) {
  int run_count = 0;
  auto callback = [&](const uint64_t&) { run_count++; };

  auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
  ASSERT_RESULT_OK(result);

  // 4kb buffer with 16 byte payloads (8 byte data, 8 byte header) should fill
  // after 255 iterations. Exceed that so that some events are dropped.
  constexpr int iterations = 300;
  for (int i = 0; i < iterations; i++) {
    RunProgram();
  }

  // Some events were dropped, but consume all that succeeded.
  EXPECT_THAT(result.value()->ConsumeAll(callback),
              HasValue(AllOf(Gt(250), Lt(260))));
  EXPECT_THAT(run_count, AllOf(Gt(250), Lt(260)));

  // After consuming everything, we should be able to use the ring buffer again.
  run_count = 0;
  RunProgram();
  EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(1));
  EXPECT_EQ(run_count, 1);
}

TEST_F(BpfRingbufTest, WrongTypeSize) {
  // The program under test writes 8-byte uint64_t values so a ringbuffer for
  // 1-byte uint8_t values will fail to read from it. Note that the map_def does
  // not specify the value size, so we fail on read, not creation.
  auto result = BpfRingbuf<uint8_t>::Create(mRingbufPath.c_str());
  ASSERT_RESULT_OK(result);

  RunProgram();

  EXPECT_THAT(result.value()->ConsumeAll([](const uint8_t&) {}),
              HasError(WithCode(EMSGSIZE)));
}

TEST_F(BpfRingbufTest, InvalidPath) {
  EXPECT_THAT(BpfRingbuf<int>::Create("/sys/fs/bpf/bad_path"),
              HasError(WithCode(ENOENT)));
}

}  // namespace bpf
}  // namespace android