BpfLoadTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2018 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. #include <android-base/macros.h>
  17. #include <gtest/gtest.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <iostream>
  21. #include "include/bpf/BpfMap.h"
  22. #include "include/bpf/BpfUtils.h"
  23. #include "include/libbpf_android.h"
  24. using ::testing::Test;
  25. constexpr const char tp_prog_path[] =
  26. "/sys/fs/bpf/prog_bpf_load_tp_prog_tracepoint_sched_sched_switch";
  27. constexpr const char tp_map_path[] = "/sys/fs/bpf/map_bpf_load_tp_prog_cpu_pid";
  28. namespace android {
  29. namespace bpf {
  30. class BpfLoadTest : public testing::Test {
  31. protected:
  32. BpfLoadTest() {}
  33. int mProgFd, mMapFd;
  34. void SetUp() {
  35. SKIP_IF_BPF_NOT_SUPPORTED;
  36. unlink(tp_prog_path);
  37. unlink(tp_map_path);
  38. EXPECT_EQ(android::bpf::loadProg("/system/etc/bpf/bpf_load_tp_prog.o"), 0);
  39. mProgFd = bpf_obj_get(tp_prog_path);
  40. EXPECT_GT(mProgFd, 0);
  41. mMapFd = bpf_obj_get(tp_map_path);
  42. EXPECT_GT(mMapFd, 0);
  43. int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch");
  44. EXPECT_NE(ret, 0);
  45. }
  46. void TearDown() {
  47. SKIP_IF_BPF_NOT_SUPPORTED;
  48. close(mProgFd);
  49. close(mMapFd);
  50. unlink(tp_prog_path);
  51. unlink(tp_map_path);
  52. }
  53. void checkMapNonZero() {
  54. // The test program installs a tracepoint on sched:sched_switch
  55. // and expects the kernel to populate a PID corresponding to CPU
  56. android::bpf::BpfMap<uint32_t, uint32_t> m(mMapFd);
  57. // Wait for program to run a little
  58. sleep(1);
  59. int non_zero = 0;
  60. const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val,
  61. BpfMap<uint32_t, uint32_t>& map) {
  62. if (val && !non_zero) {
  63. non_zero = 1;
  64. }
  65. UNUSED(key);
  66. UNUSED(map);
  67. return android::netdutils::status::ok;
  68. };
  69. EXPECT_OK(m.iterateWithValue(iterFunc));
  70. EXPECT_EQ(non_zero, 1);
  71. }
  72. };
  73. TEST_F(BpfLoadTest, bpfCheckMap) {
  74. SKIP_IF_BPF_NOT_SUPPORTED;
  75. checkMapNonZero();
  76. }
  77. } // namespace bpf
  78. } // namespace android