blob: 77a18fa892254360f1a3bb502731eb2205c10f8b [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
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
64namespace android {
65namespace bpf {
66
67struct UidTag {
68 uint32_t uid;
69 uint32_t tag;
70};
71
72struct StatsKey {
73 uint32_t uid;
74 uint32_t tag;
75 uint32_t counterSet;
76 uint32_t ifaceIndex;
77};
78
79struct StatsValue {
80 uint64_t rxPackets;
81 uint64_t rxBytes;
82 uint64_t txPackets;
83 uint64_t txBytes;
84};
85
86struct 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
95struct IfaceValue {
96 char name[IFNAMSIZ];
97};
98
99struct 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
107int mapRetrieve(const char* pathname, uint32_t flags);
108
109struct 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 Feng1f20ad32018-11-26 15:18:46 -0800119 search = BPF_MAP_SEARCH_PATTERN((uint8_t*)&dummyFd);
120 replace = BPF_MAP_REPLACE_PATTERN((uint8_t*)&realFd);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700121 }
122};
123
Chenbo Feng79b7e612018-12-11 12:24:23 -0800124enum class BpfLevel {
125 // Devices shipped before P or kernel version is lower than 4.9 do not
126 // have eBPF enabled.
127 NONE,
128 // Devices shipped in P with android 4.9 kernel only have the basic eBPF
129 // functionalities such as xt_bpf and cgroup skb filter.
130 BASIC,
131 // For devices that have 4.14 kernel. It supports advanced features like
132 // map_in_map and cgroup socket filter.
133 EXTENDED,
134};
135
Chenbo Feng75b410b2018-10-10 15:01:19 -0700136#ifndef DEFAULT_OVERFLOWUID
137#define DEFAULT_OVERFLOWUID 65534
138#endif
139
Chenbo Feng75b410b2018-10-10 15:01:19 -0700140constexpr const int OVERFLOW_COUNTERSET = 2;
141
142constexpr const uint64_t NONEXISTENT_COOKIE = 0;
143
144constexpr const int MINIMUM_API_REQUIRED = 28;
145
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800146int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
147 uint32_t map_flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700148int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags);
149int findMapEntry(const base::unique_fd& map_fd, void* key, void* value);
150int deleteMapEntry(const base::unique_fd& map_fd, void* key);
151int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key);
152int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey);
153int bpfProgLoad(bpf_prog_type prog_type, netdutils::Slice bpf_insns, const char* license,
154 uint32_t kern_version, netdutils::Slice bpf_log);
155int bpfFdPin(const base::unique_fd& map_fd, const char* pathname);
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800156int bpfFdGet(const char* pathname, uint32_t flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700157int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd);
158int detachProgram(bpf_attach_type type, uint32_t cg_fd);
159uint64_t getSocketCookie(int sockFd);
Chenbo Feng79b7e612018-12-11 12:24:23 -0800160std::string BpfLevelToString(BpfLevel BpfLevel);
161BpfLevel getBpfSupportLevel();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700162int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
163 const std::vector<BpfMapInfo>& mapPatterns);
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800164int synchronizeKernelRCU();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700165
Chenbo Feng79b7e612018-12-11 12:24:23 -0800166#define SKIP_IF_BPF_NOT_SUPPORTED \
167 do { \
168 if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \
169 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
170 return; \
171 } \
172 } while (0)
173
174#define SKIP_IF_BPF_SUPPORTED \
175 do { \
176 if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -0700177 } while (0)
178
179constexpr int BPF_CONTINUE = 0;
180constexpr int BPF_DELETED = 1;
181
182bool operator==(const StatsValue& lhs, const StatsValue& rhs);
183bool operator==(const UidTag& lhs, const UidTag& rhs);
184bool operator==(const StatsKey& lhs, const StatsKey& rhs);
185
186} // namespace bpf
187} // namespace android
188
189#endif