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 | |
| 20 | #include <linux/bpf.h> |
| 21 | #include <linux/if_ether.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/unistd.h> |
| 24 | #include <net/if.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/socket.h> |
| 28 | |
| 29 | #include "android-base/unique_fd.h" |
| 30 | #include "netdutils/Slice.h" |
| 31 | #include "netdutils/StatusOr.h" |
| 32 | |
| 33 | #define BPF_PASS 1 |
| 34 | #define BPF_DROP 0 |
| 35 | |
| 36 | #define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x)) |
| 37 | #define DEFAULT_LOG_LEVEL 1 |
| 38 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 39 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 40 | |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 41 | #define TEST_LIMIT 8388608 |
| 42 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace bpf { |
| 45 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 46 | int mapRetrieve(const char* pathname, uint32_t flags); |
| 47 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 48 | enum class BpfLevel { |
| 49 | // Devices shipped before P or kernel version is lower than 4.9 do not |
| 50 | // have eBPF enabled. |
| 51 | NONE, |
| 52 | // Devices shipped in P with android 4.9 kernel only have the basic eBPF |
| 53 | // functionalities such as xt_bpf and cgroup skb filter. |
| 54 | BASIC, |
| 55 | // For devices that have 4.14 kernel. It supports advanced features like |
| 56 | // map_in_map and cgroup socket filter. |
| 57 | EXTENDED, |
| 58 | }; |
| 59 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 60 | #ifndef DEFAULT_OVERFLOWUID |
| 61 | #define DEFAULT_OVERFLOWUID 65534 |
| 62 | #endif |
| 63 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 64 | constexpr const int OVERFLOW_COUNTERSET = 2; |
| 65 | |
| 66 | constexpr const uint64_t NONEXISTENT_COOKIE = 0; |
| 67 | |
| 68 | constexpr const int MINIMUM_API_REQUIRED = 28; |
| 69 | |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 70 | int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries, |
| 71 | uint32_t map_flags); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 72 | int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags); |
| 73 | int findMapEntry(const base::unique_fd& map_fd, void* key, void* value); |
| 74 | int deleteMapEntry(const base::unique_fd& map_fd, void* key); |
| 75 | int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key); |
| 76 | int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 77 | int bpfFdPin(const base::unique_fd& map_fd, const char* pathname); |
Chenbo Feng | c1dd764 | 2018-12-22 11:41:20 -0800 | [diff] [blame] | 78 | int bpfFdGet(const char* pathname, uint32_t flags); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 79 | int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd); |
| 80 | int detachProgram(bpf_attach_type type, uint32_t cg_fd); |
| 81 | uint64_t getSocketCookie(int sockFd); |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 82 | int setrlimitForTest(); |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 83 | std::string BpfLevelToString(BpfLevel BpfLevel); |
| 84 | BpfLevel getBpfSupportLevel(); |
Chenbo Feng | 9cd8f14 | 2018-12-04 16:54:56 -0800 | [diff] [blame] | 85 | int synchronizeKernelRCU(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 86 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 87 | #define SKIP_IF_BPF_NOT_SUPPORTED \ |
| 88 | do { \ |
| 89 | if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \ |
| 90 | GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \ |
| 91 | return; \ |
| 92 | } \ |
| 93 | } while (0) |
| 94 | |
| 95 | #define SKIP_IF_BPF_SUPPORTED \ |
| 96 | do { \ |
| 97 | if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \ |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 98 | } while (0) |
| 99 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 100 | } // namespace bpf |
| 101 | } // namespace android |
| 102 | |
| 103 | #endif |