Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 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 | |
Maciej Żenczykowski | 23c436f | 2021-01-21 14:24:15 -0800 | [diff] [blame] | 17 | #pragma once |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 18 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 19 | #include <linux/if_ether.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 20 | #include <net/if.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/socket.h> |
| 24 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Hungming Chen | a46f217 | 2021-01-13 13:56:38 +0800 | [diff] [blame] | 27 | #include "BpfSyscallWrappers.h" |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 28 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace bpf { |
| 31 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 32 | constexpr const int OVERFLOW_COUNTERSET = 2; |
| 33 | |
| 34 | constexpr const uint64_t NONEXISTENT_COOKIE = 0; |
| 35 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 36 | uint64_t getSocketCookie(int sockFd); |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 37 | int synchronizeKernelRCU(); |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 38 | int setrlimitForTest(); |
Maciej Żenczykowski | dcb5228 | 2021-01-17 03:36:34 -0800 | [diff] [blame] | 39 | |
Maciej Żenczykowski | 30af4b5 | 2021-02-11 15:58:12 -0800 | [diff] [blame] | 40 | #define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c)) |
Maciej Żenczykowski | dcb5228 | 2021-01-17 03:36:34 -0800 | [diff] [blame] | 41 | |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 42 | unsigned kernelVersion(); |
Maciej Żenczykowski | dcb5228 | 2021-01-17 03:36:34 -0800 | [diff] [blame] | 43 | |
| 44 | static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, unsigned sub) { |
| 45 | return kernelVersion() >= KVER(major, minor, sub); |
| 46 | } |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 47 | |
Maciej Żenczykowski | 5460527 | 2021-09-17 15:27:42 -0700 | [diff] [blame^] | 48 | #define SKIP_IF_BPF_SUPPORTED \ |
| 49 | do { \ |
| 50 | if (android::bpf::isAtLeastKernelVersion(4, 9, 0)) \ |
| 51 | GTEST_SKIP() << "Skip: bpf is supported."; \ |
Maciej Żenczykowski | 67fa207 | 2021-03-11 19:50:48 -0800 | [diff] [blame] | 52 | } while (0) |
| 53 | |
Maciej Żenczykowski | 5460527 | 2021-09-17 15:27:42 -0700 | [diff] [blame^] | 54 | #define SKIP_IF_BPF_NOT_SUPPORTED \ |
| 55 | do { \ |
| 56 | if (!android::bpf::isAtLeastKernelVersion(4, 9, 0)) \ |
| 57 | GTEST_SKIP() << "Skip: bpf is not supported."; \ |
Maciej Żenczykowski | 67fa207 | 2021-03-11 19:50:48 -0800 | [diff] [blame] | 58 | } while (0) |
| 59 | |
Maciej Żenczykowski | 5460527 | 2021-09-17 15:27:42 -0700 | [diff] [blame^] | 60 | #define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \ |
| 61 | do { \ |
| 62 | if (!android::bpf::isAtLeastKernelVersion(4, 14, 0)) \ |
| 63 | GTEST_SKIP() << "Skip: extended bpf feature not supported."; \ |
Maciej Żenczykowski | 672b0e7 | 2020-02-12 04:15:20 -0800 | [diff] [blame] | 64 | } while (0) |
| 65 | |
Maciej Żenczykowski | 5460527 | 2021-09-17 15:27:42 -0700 | [diff] [blame^] | 66 | #define SKIP_IF_XDP_NOT_SUPPORTED \ |
| 67 | do { \ |
| 68 | if (!android::bpf::isAtLeastKernelVersion(5, 9, 0)) \ |
| 69 | GTEST_SKIP() << "Skip: xdp not supported."; \ |
Maciej Żenczykowski | a36bed3 | 2021-01-20 15:23:40 -0800 | [diff] [blame] | 70 | } while (0) |
| 71 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 72 | } // namespace bpf |
| 73 | } // namespace android |