blob: 426dc021f2b594f42f1696d40057e3c58c37fe03 [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
124#ifndef DEFAULT_OVERFLOWUID
125#define DEFAULT_OVERFLOWUID 65534
126#endif
127
128constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf";
129
130constexpr const int OVERFLOW_COUNTERSET = 2;
131
132constexpr const uint64_t NONEXISTENT_COOKIE = 0;
133
134constexpr const int MINIMUM_API_REQUIRED = 28;
135
Chenbo Feng1f20ad32018-11-26 15:18:46 -0800136int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
137 uint32_t map_flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700138int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags);
139int findMapEntry(const base::unique_fd& map_fd, void* key, void* value);
140int deleteMapEntry(const base::unique_fd& map_fd, void* key);
141int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key);
142int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey);
143int bpfProgLoad(bpf_prog_type prog_type, netdutils::Slice bpf_insns, const char* license,
144 uint32_t kern_version, netdutils::Slice bpf_log);
145int bpfFdPin(const base::unique_fd& map_fd, const char* pathname);
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800146int bpfFdGet(const char* pathname, uint32_t flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700147int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd);
148int detachProgram(bpf_attach_type type, uint32_t cg_fd);
149uint64_t getSocketCookie(int sockFd);
150bool hasBpfSupport();
151int parseProgramsFromFile(const char* path, BpfProgInfo* programs, size_t size,
152 const std::vector<BpfMapInfo>& mapPatterns);
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800153int synchronizeKernelRCU();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700154
155#define SKIP_IF_BPF_NOT_SUPPORTED \
156 do { \
157 if (!hasBpfSupport()) return; \
158 } while (0)
159
160constexpr int BPF_CONTINUE = 0;
161constexpr int BPF_DELETED = 1;
162
163bool operator==(const StatsValue& lhs, const StatsValue& rhs);
164bool operator==(const UidTag& lhs, const UidTag& rhs);
165bool operator==(const StatsKey& lhs, const StatsKey& rhs);
166
167} // namespace bpf
168} // namespace android
169
170#endif