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> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 22 | #include <linux/unistd.h> |
| 23 | #include <net/if.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/socket.h> |
| 27 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 28 | #include <string> |
| 29 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 30 | #include "android-base/unique_fd.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 31 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 32 | #define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x)) |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 33 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace bpf { |
| 36 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 37 | enum class BpfLevel { |
| 38 | // Devices shipped before P or kernel version is lower than 4.9 do not |
| 39 | // have eBPF enabled. |
| 40 | NONE, |
| 41 | // Devices shipped in P with android 4.9 kernel only have the basic eBPF |
| 42 | // functionalities such as xt_bpf and cgroup skb filter. |
| 43 | BASIC, |
| 44 | // For devices that have 4.14 kernel. It supports advanced features like |
| 45 | // map_in_map and cgroup socket filter. |
| 46 | EXTENDED, |
| 47 | }; |
| 48 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 49 | constexpr const int OVERFLOW_COUNTERSET = 2; |
| 50 | |
| 51 | constexpr const uint64_t NONEXISTENT_COOKIE = 0; |
| 52 | |
| 53 | constexpr const int MINIMUM_API_REQUIRED = 28; |
| 54 | |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 55 | /* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion |
| 56 | * of it that we are using. The kernel's bpf() system call will perform a strict check to ensure |
| 57 | * all unused portions are zero. It will fail with E2BIG if we don't fully zero bpf_attr. |
| 58 | */ |
| 59 | |
| 60 | static inline int bpf(int cmd, const bpf_attr& attr) { |
| 61 | return syscall(__NR_bpf, cmd, &attr, sizeof(attr)); |
| 62 | } |
| 63 | |
| 64 | static inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, |
| 65 | uint32_t max_entries, uint32_t map_flags) { |
| 66 | return bpf(BPF_MAP_CREATE, { |
| 67 | .map_type = map_type, |
| 68 | .key_size = key_size, |
| 69 | .value_size = value_size, |
| 70 | .max_entries = max_entries, |
| 71 | .map_flags = map_flags, |
| 72 | }); |
| 73 | } |
| 74 | |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 75 | static inline int writeToMapEntry(const base::unique_fd& map_fd, const void* key, const void* value, |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 76 | uint64_t flags) { |
| 77 | return bpf(BPF_MAP_UPDATE_ELEM, { |
| 78 | .map_fd = static_cast<__u32>(map_fd.get()), |
| 79 | .key = ptr_to_u64(key), |
| 80 | .value = ptr_to_u64(value), |
| 81 | .flags = flags, |
| 82 | }); |
| 83 | } |
| 84 | |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 85 | static inline int findMapEntry(const base::unique_fd& map_fd, const void* key, void* value) { |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 86 | return bpf(BPF_MAP_LOOKUP_ELEM, { |
| 87 | .map_fd = static_cast<__u32>(map_fd.get()), |
| 88 | .key = ptr_to_u64(key), |
| 89 | .value = ptr_to_u64(value), |
| 90 | }); |
| 91 | } |
| 92 | |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 93 | static inline int deleteMapEntry(const base::unique_fd& map_fd, const void* key) { |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 94 | return bpf(BPF_MAP_DELETE_ELEM, { |
| 95 | .map_fd = static_cast<__u32>(map_fd.get()), |
| 96 | .key = ptr_to_u64(key), |
| 97 | }); |
| 98 | } |
| 99 | |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 100 | static inline int getNextMapKey(const base::unique_fd& map_fd, const void* key, void* next_key) { |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 101 | return bpf(BPF_MAP_GET_NEXT_KEY, { |
| 102 | .map_fd = static_cast<__u32>(map_fd.get()), |
| 103 | .key = ptr_to_u64(key), |
| 104 | .next_key = ptr_to_u64(next_key), |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | static inline int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) { |
| 109 | return getNextMapKey(map_fd, NULL, firstKey); |
| 110 | } |
| 111 | |
| 112 | static inline int bpfFdPin(const base::unique_fd& map_fd, const char* pathname) { |
| 113 | return bpf(BPF_OBJ_PIN, { |
| 114 | .pathname = ptr_to_u64(pathname), |
| 115 | .bpf_fd = static_cast<__u32>(map_fd.get()), |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | static inline int bpfFdGet(const char* pathname, uint32_t flag) { |
| 120 | return bpf(BPF_OBJ_GET, { |
| 121 | .pathname = ptr_to_u64(pathname), |
| 122 | .file_flags = flag, |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | static inline int mapRetrieve(const char* pathname, uint32_t flag) { |
| 127 | return bpfFdGet(pathname, flag); |
| 128 | } |
| 129 | |
Maciej Żenczykowski | 289742f | 2020-01-17 19:18:26 -0800 | [diff] [blame] | 130 | static inline int attachProgram(bpf_attach_type type, const base::unique_fd& prog_fd, |
| 131 | const base::unique_fd& cg_fd) { |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 132 | return bpf(BPF_PROG_ATTACH, { |
Maciej Żenczykowski | 289742f | 2020-01-17 19:18:26 -0800 | [diff] [blame] | 133 | .target_fd = static_cast<__u32>(cg_fd.get()), |
| 134 | .attach_bpf_fd = static_cast<__u32>(prog_fd.get()), |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 135 | .attach_type = type, |
| 136 | }); |
| 137 | } |
| 138 | |
Maciej Żenczykowski | 289742f | 2020-01-17 19:18:26 -0800 | [diff] [blame] | 139 | static inline int detachProgram(bpf_attach_type type, const base::unique_fd& cg_fd) { |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 140 | return bpf(BPF_PROG_DETACH, { |
Maciej Żenczykowski | 289742f | 2020-01-17 19:18:26 -0800 | [diff] [blame] | 141 | .target_fd = static_cast<__u32>(cg_fd.get()), |
Maciej Żenczykowski | b479fd6 | 2020-01-16 02:18:11 -0800 | [diff] [blame] | 142 | .attach_type = type, |
| 143 | }); |
| 144 | } |
| 145 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 146 | uint64_t getSocketCookie(int sockFd); |
Chenbo Feng | 0a1a9a1 | 2019-04-09 12:05:04 -0700 | [diff] [blame] | 147 | int setrlimitForTest(); |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 148 | std::string BpfLevelToString(BpfLevel BpfLevel); |
| 149 | BpfLevel getBpfSupportLevel(); |
Chenbo Feng | 9cd8f14 | 2018-12-04 16:54:56 -0800 | [diff] [blame] | 150 | int synchronizeKernelRCU(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 151 | |
Chenbo Feng | 79b7e61 | 2018-12-11 12:24:23 -0800 | [diff] [blame] | 152 | #define SKIP_IF_BPF_NOT_SUPPORTED \ |
| 153 | do { \ |
| 154 | if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \ |
| 155 | GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \ |
| 156 | return; \ |
| 157 | } \ |
| 158 | } while (0) |
| 159 | |
| 160 | #define SKIP_IF_BPF_SUPPORTED \ |
| 161 | do { \ |
| 162 | if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \ |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 163 | } while (0) |
| 164 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 165 | } // namespace bpf |
| 166 | } // namespace android |
| 167 | |
| 168 | #endif |