blob: 93a13a42c9935f639aa987511378cd92349a0953 [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
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070064#define TEST_LIMIT 8388608
65
Chenbo Feng75b410b2018-10-10 15:01:19 -070066namespace android {
67namespace bpf {
68
69struct UidTag {
70 uint32_t uid;
71 uint32_t tag;
72};
73
74struct StatsKey {
75 uint32_t uid;
76 uint32_t tag;
77 uint32_t counterSet;
78 uint32_t ifaceIndex;
79};
80
81struct StatsValue {
82 uint64_t rxPackets;
83 uint64_t rxBytes;
84 uint64_t txPackets;
85 uint64_t txBytes;
86};
87
88struct Stats {
89 uint64_t rxBytes;
90 uint64_t rxPackets;
91 uint64_t txBytes;
92 uint64_t txPackets;
93 uint64_t tcpRxPackets;
94 uint64_t tcpTxPackets;
95};
96
97struct IfaceValue {
98 char name[IFNAMSIZ];
99};
100
101struct BpfProgInfo {
102 bpf_attach_type attachType;
103 const char* path;
104 const char* name;
105 bpf_prog_type loadType;
106 base::unique_fd fd;
107};
108
109int mapRetrieve(const char* pathname, uint32_t flags);
110
111struct BpfMapInfo {
112 std::array<uint8_t, MAP_CMD_SIZE> search;
113 std::array<uint8_t, MAP_CMD_SIZE> replace;
114 const int fd;
115 std::string path;
116
117 BpfMapInfo(uint64_t dummyFd, const char* mapPath)
118 : BpfMapInfo(dummyFd, android::bpf::mapRetrieve(mapPath, 0)) {}
119
120 BpfMapInfo(uint64_t dummyFd, int realFd, const char* mapPath = "") : fd(realFd), path(mapPath) {
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800121 search = BPF_MAP_SEARCH_PATTERN((uint8_t*)&dummyFd);
122 replace = BPF_MAP_REPLACE_PATTERN((uint8_t*)&realFd);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700123 }
124};
125
Chenbo Feng79b7e612018-12-11 12:24:23 -0800126enum class BpfLevel {
127 // Devices shipped before P or kernel version is lower than 4.9 do not
128 // have eBPF enabled.
129 NONE,
130 // Devices shipped in P with android 4.9 kernel only have the basic eBPF
131 // functionalities such as xt_bpf and cgroup skb filter.
132 BASIC,
133 // For devices that have 4.14 kernel. It supports advanced features like
134 // map_in_map and cgroup socket filter.
135 EXTENDED,
136};
137
Chenbo Feng75b410b2018-10-10 15:01:19 -0700138#ifndef DEFAULT_OVERFLOWUID
139#define DEFAULT_OVERFLOWUID 65534
140#endif
141
Chenbo Feng75b410b2018-10-10 15:01:19 -0700142constexpr const int OVERFLOW_COUNTERSET = 2;
143
144constexpr const uint64_t NONEXISTENT_COOKIE = 0;
145
146constexpr const int MINIMUM_API_REQUIRED = 28;
147
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800148int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
149 uint32_t map_flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700150int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags);
151int findMapEntry(const base::unique_fd& map_fd, void* key, void* value);
152int deleteMapEntry(const base::unique_fd& map_fd, void* key);
153int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key);
154int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey);
155int bpfProgLoad(bpf_prog_type prog_type, netdutils::Slice bpf_insns, const char* license,
156 uint32_t kern_version, netdutils::Slice bpf_log);
157int bpfFdPin(const base::unique_fd& map_fd, const char* pathname);
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800158int bpfFdGet(const char* pathname, uint32_t flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700159int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd);
160int detachProgram(bpf_attach_type type, uint32_t cg_fd);
161uint64_t getSocketCookie(int sockFd);
Chenbo Feng0a1a9a12019-04-09 12:05:04 -0700162int setrlimitForTest();
Chenbo Feng79b7e612018-12-11 12:24:23 -0800163std::string BpfLevelToString(BpfLevel BpfLevel);
164BpfLevel getBpfSupportLevel();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700165int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
166 const std::vector<BpfMapInfo>& mapPatterns);
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800167int synchronizeKernelRCU();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700168
Chenbo Feng79b7e612018-12-11 12:24:23 -0800169#define SKIP_IF_BPF_NOT_SUPPORTED \
170 do { \
171 if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \
172 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
173 return; \
174 } \
175 } while (0)
176
177#define SKIP_IF_BPF_SUPPORTED \
178 do { \
179 if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -0700180 } while (0)
181
182constexpr int BPF_CONTINUE = 0;
183constexpr int BPF_DELETED = 1;
184
185bool operator==(const StatsValue& lhs, const StatsValue& rhs);
186bool operator==(const UidTag& lhs, const UidTag& rhs);
187bool operator==(const StatsKey& lhs, const StatsKey& rhs);
188
189} // namespace bpf
190} // namespace android
191
192#endif