1 /*
2 * Copyright (C) 2017 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 #pragma once
18
19 #include <linux/if_ether.h>
20 #include <net/if.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/socket.h>
24
25 #include <string>
26
27 #include "BpfSyscallWrappers.h"
28
29 namespace android {
30 namespace bpf {
31
32 constexpr const int OVERFLOW_COUNTERSET = 2;
33
34 constexpr const uint64_t NONEXISTENT_COOKIE = 0;
35
36 uint64_t getSocketCookie(int sockFd);
37 int synchronizeKernelRCU();
38 int setrlimitForTest();
39
40 #define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c))
41
42 unsigned kernelVersion();
43
isAtLeastKernelVersion(unsigned major,unsigned minor,unsigned sub)44 static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, unsigned sub) {
45 return kernelVersion() >= KVER(major, minor, sub);
46 }
47
48 #define SKIP_IF_BPF_SUPPORTED \
49 do { \
50 if (android::bpf::isAtLeastKernelVersion(4, 9, 0)) { \
51 GTEST_LOG_(INFO) << "This test is skipped since bpf is supported\n"; \
52 return; \
53 } \
54 } while (0)
55
56 #define SKIP_IF_BPF_NOT_SUPPORTED \
57 do { \
58 if (!android::bpf::isAtLeastKernelVersion(4, 9, 0)) { \
59 GTEST_LOG_(INFO) << "This test is skipped since bpf is not supported\n"; \
60 return; \
61 } \
62 } while (0)
63
64 #define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \
65 do { \
66 if (!android::bpf::isAtLeastKernelVersion(4, 14, 0)) { \
67 GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature" \
68 << "not supported\n"; \
69 return; \
70 } \
71 } while (0)
72
73 #define SKIP_IF_XDP_NOT_SUPPORTED \
74 do { \
75 if (!android::bpf::isAtLeastKernelVersion(5, 9, 0)) { \
76 GTEST_LOG_(INFO) << "This test is skipped since xdp" \
77 << "not supported\n"; \
78 return; \
79 } \
80 } while (0)
81
82 } // namespace bpf
83 } // namespace android
84