blob: 726e632f19c1e182fe2f06be43c629d12a014ba1 [file] [log] [blame]
Chenbo Feng05393d82018-01-09 15:18:43 -08001/*
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
Chenbo Feng36575d02018-02-05 15:19:15 -080017#include <arpa/inet.h>
18#include <elf.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080019#include <error.h>
Chenbo Feng36575d02018-02-05 15:19:15 -080020#include <fcntl.h>
21#include <inttypes.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080022#include <linux/bpf.h>
23#include <linux/unistd.h>
24#include <net/if.h>
Chenbo Feng36575d02018-02-05 15:19:15 -080025#include <stdint.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Chenbo Feng36575d02018-02-05 15:19:15 -080029#include <unistd.h>
30
31#include <sys/mman.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080035
36#include <android-base/stringprintf.h>
37#include <android-base/unique_fd.h>
38
39#include <netdutils/Misc.h>
Chenbo Feng36575d02018-02-05 15:19:15 -080040#include <netdutils/Slice.h>
Chenbo Feng05393d82018-01-09 15:18:43 -080041#include "bpf/BpfUtils.h"
42
Chenbo Feng36575d02018-02-05 15:19:15 -080043#include "bpf_shared.h"
44
Chenbo Feng05393d82018-01-09 15:18:43 -080045using android::base::unique_fd;
Chenbo Feng36575d02018-02-05 15:19:15 -080046using android::netdutils::Slice;
47
48#define INGRESS_PROG "/system/bin/cgroup_bpf_ingress_prog"
49#define EGRESS_PROG "/system/bin/cgroup_bpf_egress_prog"
50#define MAP_LD_CMD_HEAD 0x18
51
52#define FAIL(str) \
53 do { \
54 perror((str)); \
55 return -1; \
56 } while (0)
57
58// The BPF instruction bytes that we need to replace. x is a placeholder (e.g., COOKIE_TAG_MAP).
59#define MAP_SEARCH_PATTERN(x) \
60 { \
61 0x18, 0x01, 0x00, 0x00, \
62 (x)[0], (x)[1], (x)[2], (x)[3], \
63 0x00, 0x00, 0x00, 0x00, \
64 (x)[4], (x)[5], (x)[6], (x)[7] \
65 }
66
67// The bytes we'll replace them with. x is the actual fd number for the map at runtime.
68// The second byte is changed from 0x01 to 0x11 since 0x11 is the special command used
69// for bpf map fd loading. The original 0x01 is only a normal load command.
70#define MAP_REPLACE_PATTERN(x) \
71 { \
72 0x18, 0x11, 0x00, 0x00, \
73 (x)[0], (x)[1], (x)[2], (x)[3], \
74 0x00, 0x00, 0x00, 0x00, \
75 (x)[4], (x)[5], (x)[6], (x)[7] \
76 }
77
78#define MAP_CMD_SIZE 16
79#define LOG_BUF_SIZE 65536
Chenbo Feng05393d82018-01-09 15:18:43 -080080
81namespace android {
82namespace bpf {
83
Chenbo Feng36575d02018-02-05 15:19:15 -080084void makeFdReplacePattern(uint64_t code, uint64_t mapFd, char* pattern, char* cmd) {
85 char mapCode[sizeof(uint64_t)];
86 char mapCmd[sizeof(uint64_t)];
87 // The byte order is little endian for arm devices.
88 for (uint32_t i = 0; i < sizeof(uint64_t); i++) {
89 mapCode[i] = (code >> (i * 8)) & 0xFF;
90 mapCmd[i] = (mapFd >> (i * 8)) & 0xFF;
91 }
92
93 char tmpPattern[] = MAP_SEARCH_PATTERN(mapCode);
94 memcpy(pattern, tmpPattern, MAP_CMD_SIZE);
95 char tmpCmd[] = MAP_REPLACE_PATTERN(mapCmd);
96 memcpy(cmd, tmpCmd, MAP_CMD_SIZE);
97}
98
99int loadProg(const char* path, int cookieTagMap, int uidStatsMap, int tagStatsMap,
100 int uidCounterSetMap) {
101 int fd = open(path, O_RDONLY);
102 if (fd == -1) {
103 fprintf(stderr, "Failed to open %s program: %s", path, strerror(errno));
104 return -1;
105 }
106
107 struct stat stat;
108 if (fstat(fd, &stat)) FAIL("Fail to get file size");
109
110 off_t fileLen = stat.st_size;
111 char* baseAddr = (char*)mmap(NULL, fileLen, PROT_READ, MAP_PRIVATE, fd, 0);
112 if (baseAddr == MAP_FAILED) FAIL("Failed to map the program into memory");
113
114 if ((uint32_t)fileLen < sizeof(Elf64_Ehdr)) FAIL("file size too small for Elf64_Ehdr");
115
116 Elf64_Ehdr* elf = (Elf64_Ehdr*)baseAddr;
117
118 // Find section names string table. This is the section whose index is e_shstrndx.
119 if (elf->e_shstrndx == SHN_UNDEF ||
120 elf->e_shoff + (elf->e_shstrndx + 1) * sizeof(Elf64_Shdr) > (uint32_t)fileLen) {
121 FAIL("cannot locate namesSection\n");
122 }
123
124 Elf64_Shdr* sections = (Elf64_Shdr*)(baseAddr + elf->e_shoff);
125
126 Elf64_Shdr* namesSection = sections + elf->e_shstrndx;
127
128 if (namesSection->sh_offset + namesSection->sh_size > (uint32_t)fileLen)
129 FAIL("namesSection out of bound\n");
130
131 const char* strTab = baseAddr + namesSection->sh_offset;
132 void* progSection = nullptr;
133 uint64_t progSize = 0;
134 for (int i = 0; i < elf->e_shnum; i++) {
135 Elf64_Shdr* section = sections + i;
136 if (((char*)section - baseAddr) + sizeof(Elf64_Shdr) > (uint32_t)fileLen) {
137 FAIL("next section is out of bound\n");
138 }
139
140 if (!strcmp(strTab + section->sh_name, BPF_PROG_SEC_NAME)) {
141 progSection = baseAddr + section->sh_offset;
142 progSize = (uint64_t)section->sh_size;
143 break;
144 }
145 }
146
147 if (!progSection) FAIL("program section not found");
148 if ((char*)progSection - baseAddr + progSize > (uint32_t)fileLen)
149 FAIL("programSection out of bound\n");
150
151 char* prog = new char[progSize]();
152 memcpy(prog, progSection, progSize);
153
154 char cookieTagMapFdPattern[MAP_CMD_SIZE];
155 char cookieTagMapFdLoadByte[MAP_CMD_SIZE];
156 makeFdReplacePattern(COOKIE_TAG_MAP, cookieTagMap, cookieTagMapFdPattern,
157 cookieTagMapFdLoadByte);
158
159 char uidCounterSetMapFdPattern[MAP_CMD_SIZE];
160 char uidCounterSetMapFdLoadByte[MAP_CMD_SIZE];
161 makeFdReplacePattern(UID_COUNTERSET_MAP, uidCounterSetMap, uidCounterSetMapFdPattern,
162 uidCounterSetMapFdLoadByte);
163
164 char tagStatsMapFdPattern[MAP_CMD_SIZE];
165 char tagStatsMapFdLoadByte[MAP_CMD_SIZE];
166 makeFdReplacePattern(TAG_STATS_MAP, tagStatsMap, tagStatsMapFdPattern, tagStatsMapFdLoadByte);
167
168 char uidStatsMapFdPattern[MAP_CMD_SIZE];
169 char uidStatsMapFdLoadByte[MAP_CMD_SIZE];
170 makeFdReplacePattern(UID_STATS_MAP, uidStatsMap, uidStatsMapFdPattern, uidStatsMapFdLoadByte);
171
172 char* mapHead = prog;
173 while ((uint64_t)(mapHead - prog + MAP_CMD_SIZE) < progSize) {
174 // Scan the program, examining all possible places that might be the start of a map load
175 // operation (i.e., all byes of value MAP_LD_CMD_HEAD).
176 //
177 // In each of these places, check whether it is the start of one of the patterns we want to
178 // replace, and if so, replace it.
179 mapHead = (char*)memchr(mapHead, MAP_LD_CMD_HEAD, progSize);
180 if (!mapHead) break;
181 if ((uint64_t)(mapHead - prog + MAP_CMD_SIZE) < progSize) {
182 if (!memcmp(mapHead, cookieTagMapFdPattern, MAP_CMD_SIZE)) {
183 memcpy(mapHead, cookieTagMapFdLoadByte, MAP_CMD_SIZE);
184 mapHead += MAP_CMD_SIZE;
185 } else if (!memcmp(mapHead, uidCounterSetMapFdPattern, MAP_CMD_SIZE)) {
186 memcpy(mapHead, uidCounterSetMapFdLoadByte, MAP_CMD_SIZE);
187 mapHead += MAP_CMD_SIZE;
188 } else if (!memcmp(mapHead, tagStatsMapFdPattern, MAP_CMD_SIZE)) {
189 memcpy(mapHead, tagStatsMapFdLoadByte, MAP_CMD_SIZE);
190 mapHead += MAP_CMD_SIZE;
191 } else if (!memcmp(mapHead, uidStatsMapFdPattern, MAP_CMD_SIZE)) {
192 memcpy(mapHead, uidStatsMapFdLoadByte, MAP_CMD_SIZE);
193 mapHead += MAP_CMD_SIZE;
194 }
195 }
196 mapHead++;
197 }
198 Slice insns = Slice(prog, progSize);
199 char bpf_log_buf[LOG_BUF_SIZE];
200 Slice bpfLog = Slice(bpf_log_buf, sizeof(bpf_log_buf));
201 return bpfProgLoad(BPF_PROG_TYPE_CGROUP_SKB, insns, "Apache 2.0", 0, bpfLog);
202}
203
Chenbo Feng05393d82018-01-09 15:18:43 -0800204int loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
205 const unique_fd& cookieTagMap, const unique_fd& uidCounterSetMap,
206 const unique_fd& uidStatsMap, const unique_fd& tagStatsMap) {
207 unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
208 if (cg_fd < 0) {
209 perror("Failed to open the cgroup directory");
210 return -1;
211 }
212
213 unique_fd fd;
214 if (type == BPF_CGROUP_INET_EGRESS) {
Chenbo Feng36575d02018-02-05 15:19:15 -0800215 fd.reset(loadProg(INGRESS_PROG, cookieTagMap.get(), uidStatsMap.get(), tagStatsMap.get(),
216 uidCounterSetMap.get()));
Chenbo Feng05393d82018-01-09 15:18:43 -0800217 } else {
Chenbo Feng36575d02018-02-05 15:19:15 -0800218 fd.reset(loadProg(EGRESS_PROG, cookieTagMap.get(), uidStatsMap.get(), tagStatsMap.get(),
219 uidCounterSetMap.get()));
Chenbo Feng05393d82018-01-09 15:18:43 -0800220 }
221
222 if (fd < 0) {
223 fprintf(stderr, "load %s failed: %s", name, strerror(errno));
224 return -1;
225 }
226
227 int ret = attachProgram(type, fd, cg_fd);
228 if (ret) {
229 fprintf(stderr, "%s attach failed: %s", name, strerror(errno));
230 return -1;
231 }
232
233 ret = mapPin(fd, path);
234 if (ret) {
235 fprintf(stderr, "Pin %s as file %s failed: %s", name, path, strerror(errno));
236 return -1;
237 }
238 return 0;
239}
240
241} // namespace bpf
242} // namespace android
243
244using android::bpf::BPF_EGRESS_PROG_PATH;
245using android::bpf::BPF_INGRESS_PROG_PATH;
246using android::bpf::COOKIE_UID_MAP_PATH;
247using android::bpf::TAG_STATS_MAP_PATH;
248using android::bpf::UID_COUNTERSET_MAP_PATH;
249using android::bpf::UID_STATS_MAP_PATH;
250
251static void usage(void) {
252 fprintf(stderr,
253 "Usage: ./bpfloader [-i] [-e]\n"
254 " -i load ingress bpf program\n"
255 " -e load egress bpf program\n");
256}
257
258int main(int argc, char** argv) {
259 int ret = 0;
260 unique_fd cookieTagMap(android::bpf::mapRetrieve(COOKIE_UID_MAP_PATH, 0));
261 if (cookieTagMap < 0) {
262 perror("Failed to get cookieTagMap");
263 exit(-1);
264 }
265
266 unique_fd uidCounterSetMap(android::bpf::mapRetrieve(UID_COUNTERSET_MAP_PATH, 0));
267 if (uidCounterSetMap < 0) {
268 perror("Failed to get uidCounterSetMap");
269 exit(-1);
270 }
271
272 unique_fd uidStatsMap(android::bpf::mapRetrieve(UID_STATS_MAP_PATH, 0));
273 if (uidStatsMap < 0) {
274 perror("Failed to get uidStatsMap");
275 exit(-1);
276 }
277
278 unique_fd tagStatsMap(android::bpf::mapRetrieve(TAG_STATS_MAP_PATH, 0));
279 if (tagStatsMap < 0) {
280 perror("Failed to get tagStatsMap");
281 exit(-1);
282 }
283 int opt;
284 bool doIngress = false, doEgress = false;
285 while ((opt = getopt(argc, argv, "ie")) != -1) {
286 switch (opt) {
287 case 'i':
288 doIngress = true;
289 break;
290 case 'e':
291 doEgress = true;
292 break;
293 default:
294 fprintf(stderr, "unknown argument %c", opt);
295 usage();
296 exit(-1);
297 }
298 }
299 if (doIngress) {
300 ret = android::bpf::loadAndAttachProgram(BPF_CGROUP_INET_INGRESS, BPF_INGRESS_PROG_PATH,
301 "ingress_prog", cookieTagMap, uidCounterSetMap,
302 uidStatsMap, tagStatsMap);
303 if (ret) {
304 fprintf(stderr, "Failed to set up ingress program");
305 return ret;
306 }
307 }
308 if (doEgress) {
309 ret = android::bpf::loadAndAttachProgram(BPF_CGROUP_INET_EGRESS, BPF_EGRESS_PROG_PATH,
310 "egress_prog", cookieTagMap, uidCounterSetMap,
311 uidStatsMap, tagStatsMap);
312 if (ret) {
313 fprintf(stderr, "Failed to set up ingress program");
314 return ret;
315 }
316 }
317 return ret;
318}