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 | |
| 17 | #ifndef BPF_BPFUTILS_H |
| 18 | #define BPF_BPFUTILS_H |
| 19 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 20 | #include <linux/if_ether.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 21 | #include <net/if.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/socket.h> |
| 25 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 26 | #include <string> |
| 27 | |
Hungming Chen | a46f217 | 2021-01-13 13:56:38 +0800 | [diff] [blame^] | 28 | #include "BpfSyscallWrappers.h" |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 29 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace bpf { |
| 32 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 33 | enum class BpfLevel { |
| 34 | // Devices shipped before P or kernel version is lower than 4.9 do not |
| 35 | // have eBPF enabled. |
| 36 | NONE, |
| 37 | // Devices shipped in P with android 4.9 kernel only have the basic eBPF |
| 38 | // functionalities such as xt_bpf and cgroup skb filter. |
Maciej Żenczykowski | 8a09a5a | 2020-02-17 17:54:51 -0800 | [diff] [blame] | 39 | BASIC_4_9, |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 40 | // For devices that have 4.14 kernel. It supports advanced features like |
| 41 | // map_in_map and cgroup socket filter. |
Maciej Żenczykowski | 8a09a5a | 2020-02-17 17:54:51 -0800 | [diff] [blame] | 42 | EXTENDED_4_14, |
| 43 | EXTENDED_4_19, |
| 44 | EXTENDED_5_4, |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 47 | constexpr const int OVERFLOW_COUNTERSET = 2; |
| 48 | |
| 49 | constexpr const uint64_t NONEXISTENT_COOKIE = 0; |
| 50 | |
| 51 | constexpr const int MINIMUM_API_REQUIRED = 28; |
| 52 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 53 | uint64_t getSocketCookie(int sockFd); |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 54 | int synchronizeKernelRCU(); |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 55 | int setrlimitForTest(); |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 56 | unsigned kernelVersion(); |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 57 | std::string BpfLevelToString(BpfLevel BpfLevel); |
| 58 | BpfLevel getBpfSupportLevel(); |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 59 | |
Maciej Żenczykowski | 06caf87 | 2020-02-11 16:42:21 -0800 | [diff] [blame] | 60 | inline bool isBpfSupported() { |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 61 | return getBpfSupportLevel() != BpfLevel::NONE; |
| 62 | } |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 63 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 64 | #define SKIP_IF_BPF_NOT_SUPPORTED \ |
| 65 | do { \ |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 66 | if (!android::bpf::isBpfSupported()) { \ |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 67 | GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \ |
| 68 | return; \ |
| 69 | } \ |
| 70 | } while (0) |
| 71 | |
Maciej Żenczykowski | c3a640d | 2020-02-11 15:01:21 -0800 | [diff] [blame] | 72 | #define SKIP_IF_BPF_SUPPORTED \ |
| 73 | do { \ |
| 74 | if (android::bpf::isBpfSupported()) return; \ |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 75 | } while (0) |
| 76 | |
Maciej Żenczykowski | 8a09a5a | 2020-02-17 17:54:51 -0800 | [diff] [blame] | 77 | #define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \ |
| 78 | do { \ |
| 79 | if (android::bpf::getBpfSupportLevel() < android::bpf::BpfLevel::EXTENDED_4_14) { \ |
| 80 | GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature" \ |
| 81 | << "not supported\n"; \ |
| 82 | return; \ |
| 83 | } \ |
Maciej Żenczykowski | 672b0e7 | 2020-02-12 04:15:20 -0800 | [diff] [blame] | 84 | } while (0) |
| 85 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 86 | } // namespace bpf |
| 87 | } // namespace android |
| 88 | |
| 89 | #endif |