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 | |
| 39 | #define MAP_LD_CMD_HEAD 0x18 |
| 40 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 41 | |
| 42 | // The BPF instruction bytes that we need to replace. x is a placeholder (e.g., COOKIE_TAG_MAP). |
| 43 | #define BPF_MAP_SEARCH_PATTERN(x) \ |
| 44 | { \ |
| 45 | 0x18, 0x01, 0x00, 0x00, \ |
| 46 | (x)[0], (x)[1], (x)[2], (x)[3], \ |
| 47 | 0x00, 0x00, 0x00, 0x00, \ |
| 48 | (x)[4], (x)[5], (x)[6], (x)[7] \ |
| 49 | } |
| 50 | |
| 51 | // The bytes we'll replace them with. x is the actual fd number for the map at runtime. |
| 52 | // The second byte is changed from 0x01 to 0x11 since 0x11 is the special command used |
| 53 | // for bpf map fd loading. The original 0x01 is only a normal load command. |
| 54 | #define BPF_MAP_REPLACE_PATTERN(x) \ |
| 55 | { \ |
| 56 | 0x18, 0x11, 0x00, 0x00, \ |
| 57 | (x)[0], (x)[1], (x)[2], (x)[3], \ |
| 58 | 0x00, 0x00, 0x00, 0x00, \ |
| 59 | (x)[4], (x)[5], (x)[6], (x)[7] \ |
| 60 | } |
| 61 | |
| 62 | #define MAP_CMD_SIZE 16 |
| 63 | |
| 64 | namespace android { |
| 65 | namespace bpf { |
| 66 | |
| 67 | struct UidTag { |
| 68 | uint32_t uid; |
| 69 | uint32_t tag; |
| 70 | }; |
| 71 | |
| 72 | struct StatsKey { |
| 73 | uint32_t uid; |
| 74 | uint32_t tag; |
| 75 | uint32_t counterSet; |
| 76 | uint32_t ifaceIndex; |
| 77 | }; |
| 78 | |
| 79 | struct StatsValue { |
| 80 | uint64_t rxPackets; |
| 81 | uint64_t rxBytes; |
| 82 | uint64_t txPackets; |
| 83 | uint64_t txBytes; |
| 84 | }; |
| 85 | |
| 86 | struct Stats { |
| 87 | uint64_t rxBytes; |
| 88 | uint64_t rxPackets; |
| 89 | uint64_t txBytes; |
| 90 | uint64_t txPackets; |
| 91 | uint64_t tcpRxPackets; |
| 92 | uint64_t tcpTxPackets; |
| 93 | }; |
| 94 | |
| 95 | struct IfaceValue { |
| 96 | char name[IFNAMSIZ]; |
| 97 | }; |
| 98 | |
| 99 | struct BpfProgInfo { |
| 100 | bpf_attach_type attachType; |
| 101 | const char* path; |
| 102 | const char* name; |
| 103 | bpf_prog_type loadType; |
| 104 | base::unique_fd fd; |
| 105 | }; |
| 106 | |
| 107 | int mapRetrieve(const char* pathname, uint32_t flags); |
| 108 | |
| 109 | struct BpfMapInfo { |
| 110 | std::array<uint8_t, MAP_CMD_SIZE> search; |
| 111 | std::array<uint8_t, MAP_CMD_SIZE> replace; |
| 112 | const int fd; |
| 113 | std::string path; |
| 114 | |
| 115 | BpfMapInfo(uint64_t dummyFd, const char* mapPath) |
| 116 | : BpfMapInfo(dummyFd, android::bpf::mapRetrieve(mapPath, 0)) {} |
| 117 | |
| 118 | BpfMapInfo(uint64_t dummyFd, int realFd, const char* mapPath = "") : fd(realFd), path(mapPath) { |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 119 | search = BPF_MAP_SEARCH_PATTERN((uint8_t*)&dummyFd); |
| 120 | replace = BPF_MAP_REPLACE_PATTERN((uint8_t*)&realFd); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 121 | } |
| 122 | }; |
| 123 | |
| 124 | #ifndef DEFAULT_OVERFLOWUID |
| 125 | #define DEFAULT_OVERFLOWUID 65534 |
| 126 | #endif |
| 127 | |
| 128 | constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf"; |
| 129 | |
| 130 | constexpr const int OVERFLOW_COUNTERSET = 2; |
| 131 | |
| 132 | constexpr const uint64_t NONEXISTENT_COOKIE = 0; |
| 133 | |
| 134 | constexpr const int MINIMUM_API_REQUIRED = 28; |
| 135 | |
Chenbo Feng | 1f20ad3 | 2018-11-26 15:18:46 -0800 | [diff] [blame] | 136 | int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries, |
| 137 | uint32_t map_flags); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 138 | int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags); |
| 139 | int findMapEntry(const base::unique_fd& map_fd, void* key, void* value); |
| 140 | int deleteMapEntry(const base::unique_fd& map_fd, void* key); |
| 141 | int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key); |
| 142 | int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey); |
| 143 | int bpfProgLoad(bpf_prog_type prog_type, netdutils::Slice bpf_insns, const char* license, |
| 144 | uint32_t kern_version, netdutils::Slice bpf_log); |
| 145 | int bpfFdPin(const base::unique_fd& map_fd, const char* pathname); |
Chenbo Feng | c1dd764 | 2018-12-22 11:41:20 -0800 | [diff] [blame^] | 146 | int bpfFdGet(const char* pathname, uint32_t flags); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 147 | int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd); |
| 148 | int detachProgram(bpf_attach_type type, uint32_t cg_fd); |
| 149 | uint64_t getSocketCookie(int sockFd); |
| 150 | bool hasBpfSupport(); |
| 151 | int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size, |
| 152 | const std::vector<BpfMapInfo>& mapPatterns); |
| 153 | |
| 154 | #define SKIP_IF_BPF_NOT_SUPPORTED \ |
| 155 | do { \ |
| 156 | if (!hasBpfSupport()) return; \ |
| 157 | } while (0) |
| 158 | |
| 159 | constexpr int BPF_CONTINUE = 0; |
| 160 | constexpr int BPF_DELETED = 1; |
| 161 | |
| 162 | bool operator==(const StatsValue& lhs, const StatsValue& rhs); |
| 163 | bool operator==(const UidTag& lhs, const UidTag& rhs); |
| 164 | bool operator==(const StatsKey& lhs, const StatsKey& rhs); |
| 165 | |
| 166 | } // namespace bpf |
| 167 | } // namespace android |
| 168 | |
| 169 | #endif |