diff --git a/libbpf_android/BpfUtils.cpp b/libbpf_android/BpfUtils.cpp index 5fed9c2..8689192 100644 --- a/libbpf_android/BpfUtils.cpp +++ b/libbpf_android/BpfUtils.cpp @@ -94,8 +94,6 @@ int setrlimitForTest() { return res; } -#define KVER(a, b, c) ((a)*65536 + (b)*256 + (c)) - unsigned kernelVersion() { struct utsname buf; int ret = uname(&buf); @@ -112,21 +110,5 @@ unsigned kernelVersion() { return KVER(kver_major, kver_minor, kver_sub); } -static BpfLevel getUncachedBpfSupportLevel() { - unsigned kver = kernelVersion(); - - if (kver >= KVER(5, 4, 0)) return BpfLevel::EXTENDED_5_4; - if (kver >= KVER(4, 19, 0)) return BpfLevel::EXTENDED_4_19; - if (kver >= KVER(4, 14, 0)) return BpfLevel::EXTENDED_4_14; - - // Basic BPF support is required on all devices. - return BpfLevel::BASIC_4_9; -} - -BpfLevel getBpfSupportLevel() { - static BpfLevel cache = getUncachedBpfSupportLevel(); - return cache; -} - } // namespace bpf } // namespace android diff --git a/libbpf_android/include/bpf/BpfUtils.h b/libbpf_android/include/bpf/BpfUtils.h index 0843339..08c1844 100644 --- a/libbpf_android/include/bpf/BpfUtils.h +++ b/libbpf_android/include/bpf/BpfUtils.h @@ -30,17 +30,6 @@ namespace android { namespace bpf { -enum class BpfLevel { - // Devices shipped in P with android 4.9 kernel only have the basic eBPF - // functionalities such as xt_bpf and cgroup skb filter. - BASIC_4_9, - // For devices that have 4.14 kernel. It supports advanced features like - // map_in_map and cgroup socket filter. - EXTENDED_4_14, - EXTENDED_4_19, - EXTENDED_5_4, -}; - constexpr const int OVERFLOW_COUNTERSET = 2; constexpr const uint64_t NONEXISTENT_COOKIE = 0; @@ -50,20 +39,26 @@ constexpr const int MINIMUM_API_REQUIRED = 28; uint64_t getSocketCookie(int sockFd); int synchronizeKernelRCU(); int setrlimitForTest(); + +#define KVER(a, b, c) ((a)*65536 + (b)*256 + (c)) + unsigned kernelVersion(); -BpfLevel getBpfSupportLevel(); + +static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, unsigned sub) { + return kernelVersion() >= KVER(major, minor, sub); +} inline bool isBpfSupported() { return true; } -#define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \ - do { \ - if (android::bpf::getBpfSupportLevel() < android::bpf::BpfLevel::EXTENDED_4_14) { \ - GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature" \ - << "not supported\n"; \ - return; \ - } \ +#define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \ + do { \ + if (!android::bpf::isAtLeastKernelVersion(4, 14, 0)) { \ + GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature" \ + << "not supported\n"; \ + return; \ + } \ } while (0) } // namespace bpf