• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define TEST_LIMIT 8388608
65 
66 namespace android {
67 namespace bpf {
68 
69 struct UidTag {
70     uint32_t uid;
71     uint32_t tag;
72 };
73 
74 struct StatsKey {
75     uint32_t uid;
76     uint32_t tag;
77     uint32_t counterSet;
78     uint32_t ifaceIndex;
79 };
80 
81 struct StatsValue {
82     uint64_t rxPackets;
83     uint64_t rxBytes;
84     uint64_t txPackets;
85     uint64_t txBytes;
86 };
87 
88 struct 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 
97 struct IfaceValue {
98     char name[IFNAMSIZ];
99 };
100 
101 struct 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 
109 int mapRetrieve(const char* pathname, uint32_t flags);
110 
111 struct 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 
BpfMapInfoBpfMapInfo117     BpfMapInfo(uint64_t dummyFd, const char* mapPath)
118         : BpfMapInfo(dummyFd, android::bpf::mapRetrieve(mapPath, 0)) {}
119 
fdBpfMapInfo120     BpfMapInfo(uint64_t dummyFd, int realFd, const char* mapPath = "") : fd(realFd), path(mapPath) {
121         search = BPF_MAP_SEARCH_PATTERN((uint8_t*)&dummyFd);
122         replace = BPF_MAP_REPLACE_PATTERN((uint8_t*)&realFd);
123     }
124 };
125 
126 enum 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 
138 #ifndef DEFAULT_OVERFLOWUID
139 #define DEFAULT_OVERFLOWUID 65534
140 #endif
141 
142 constexpr const int OVERFLOW_COUNTERSET = 2;
143 
144 constexpr const uint64_t NONEXISTENT_COOKIE = 0;
145 
146 constexpr const int MINIMUM_API_REQUIRED = 28;
147 
148 int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
149               uint32_t map_flags);
150 int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags);
151 int findMapEntry(const base::unique_fd& map_fd, void* key, void* value);
152 int deleteMapEntry(const base::unique_fd& map_fd, void* key);
153 int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key);
154 int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey);
155 int bpfProgLoad(bpf_prog_type prog_type, netdutils::Slice bpf_insns, const char* license,
156                 uint32_t kern_version, netdutils::Slice bpf_log);
157 int bpfFdPin(const base::unique_fd& map_fd, const char* pathname);
158 int bpfFdGet(const char* pathname, uint32_t flags);
159 int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd);
160 int detachProgram(bpf_attach_type type, uint32_t cg_fd);
161 uint64_t getSocketCookie(int sockFd);
162 int setrlimitForTest();
163 std::string BpfLevelToString(BpfLevel BpfLevel);
164 BpfLevel getBpfSupportLevel();
165 int synchronizeKernelRCU();
166 
167 #define SKIP_IF_BPF_NOT_SUPPORTED                                                    \
168     do {                                                                             \
169         if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) {    \
170             GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
171             return;                                                                  \
172         }                                                                            \
173     } while (0)
174 
175 #define SKIP_IF_BPF_SUPPORTED                                                           \
176     do {                                                                                \
177         if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \
178     } while (0)
179 
180 bool operator==(const StatsValue& lhs, const StatsValue& rhs);
181 bool operator==(const UidTag& lhs, const UidTag& rhs);
182 bool operator==(const StatsKey& lhs, const StatsKey& rhs);
183 
184 }  // namespace bpf
185 }  // namespace android
186 
187 #endif
188