blob: b62c94e3997a3e0e443e349a8cf32707bb40bdb0 [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35
36#include <errno.h>
Prashant Bhole4990f1f2017-11-08 13:55:48 +090037#include <fts.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070038#include <libgen.h>
Prashant Bhole4990f1f2017-11-08 13:55:48 +090039#include <mntent.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070040#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <linux/limits.h>
46#include <linux/magic.h>
Quentin Monnet3fc27b72017-10-24 20:11:28 -070047#include <sys/mount.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070048#include <sys/types.h>
49#include <sys/vfs.h>
50
51#include <bpf.h>
52
53#include "main.h"
54
Quentin Monnet0b1c27db2017-11-03 13:59:07 -070055void p_err(const char *fmt, ...)
56{
57 va_list ap;
58
59 va_start(ap, fmt);
60 if (json_output) {
61 jsonw_start_object(json_wtr);
62 jsonw_name(json_wtr, "error");
63 jsonw_vprintf_enquote(json_wtr, fmt, ap);
64 jsonw_end_object(json_wtr);
65 } else {
66 fprintf(stderr, "Error: ");
67 vfprintf(stderr, fmt, ap);
68 fprintf(stderr, "\n");
69 }
70 va_end(ap);
71}
72
73void p_info(const char *fmt, ...)
74{
75 va_list ap;
76
77 if (json_output)
78 return;
79
80 va_start(ap, fmt);
81 vfprintf(stderr, fmt, ap);
82 fprintf(stderr, "\n");
83 va_end(ap);
84}
85
Jakub Kicinski71bb4282017-10-04 20:10:04 -070086static bool is_bpffs(char *path)
87{
88 struct statfs st_fs;
89
90 if (statfs(path, &st_fs) < 0)
91 return false;
92
93 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
94}
95
Quentin Monnet3fc27b72017-10-24 20:11:28 -070096static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
97{
98 bool bind_done = false;
99
100 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
101 if (errno != EINVAL || bind_done) {
102 snprintf(buff, bufflen,
103 "mount --make-private %s failed: %s",
104 target, strerror(errno));
105 return -1;
106 }
107
108 if (mount(target, target, "none", MS_BIND, NULL)) {
109 snprintf(buff, bufflen,
110 "mount --bind %s %s failed: %s",
111 target, target, strerror(errno));
112 return -1;
113 }
114
115 bind_done = true;
116 }
117
118 if (mount("bpf", target, "bpf", 0, "mode=0700")) {
119 snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
120 target, strerror(errno));
121 return -1;
122 }
123
124 return 0;
125}
126
Prashant Bhole18527192017-11-08 13:55:47 +0900127int open_obj_pinned(char *path)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700128{
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700129 int fd;
130
131 fd = bpf_obj_get(path);
132 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700133 p_err("bpf obj get (%s): %s", path,
134 errno == EACCES && !is_bpffs(dirname(path)) ?
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700135 "directory not in bpf file system (bpffs)" :
136 strerror(errno));
137 return -1;
138 }
139
Prashant Bhole18527192017-11-08 13:55:47 +0900140 return fd;
141}
142
143int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
144{
145 enum bpf_obj_type type;
146 int fd;
147
148 fd = open_obj_pinned(path);
149 if (fd < 0)
150 return -1;
151
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700152 type = get_fd_type(fd);
153 if (type < 0) {
154 close(fd);
155 return type;
156 }
157 if (type != exp_type) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700158 p_err("incorrect object type: %s", get_fd_type_name(type));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700159 close(fd);
160 return -1;
161 }
162
163 return fd;
164}
165
Roman Gushchin49a086c2017-12-13 15:18:53 +0000166int do_pin_fd(int fd, const char *name)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700167{
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700168 char err_str[ERR_MAX_LEN];
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700169 char *file;
170 char *dir;
Roman Gushchin49a086c2017-12-13 15:18:53 +0000171 int err = 0;
172
173 err = bpf_obj_pin(fd, name);
174 if (!err)
175 goto out;
176
177 file = malloc(strlen(name) + 1);
178 strcpy(file, name);
179 dir = dirname(file);
180
181 if (errno != EPERM || is_bpffs(dir)) {
182 p_err("can't pin the object (%s): %s", name, strerror(errno));
183 goto out_free;
184 }
185
186 /* Attempt to mount bpffs, then retry pinning. */
187 err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
188 if (!err) {
189 err = bpf_obj_pin(fd, name);
190 if (err)
191 p_err("can't pin the object (%s): %s", name,
192 strerror(errno));
193 } else {
194 err_str[ERR_MAX_LEN - 1] = '\0';
195 p_err("can't mount BPF file system to pin the object (%s): %s",
196 name, err_str);
197 }
198
199out_free:
200 free(file);
201out:
202 return err;
203}
204
205int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
206{
207 unsigned int id;
208 char *endptr;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700209 int err;
210 int fd;
211
212 if (!is_prefix(*argv, "id")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700213 p_err("expected 'id' got %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700214 return -1;
215 }
216 NEXT_ARG();
217
218 id = strtoul(*argv, &endptr, 0);
219 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700220 p_err("can't parse %s as ID", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700221 return -1;
222 }
223 NEXT_ARG();
224
225 if (argc != 1)
226 usage();
227
228 fd = get_fd_by_id(id);
229 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700230 p_err("can't get prog by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700231 return -1;
232 }
233
Roman Gushchin49a086c2017-12-13 15:18:53 +0000234 err = do_pin_fd(fd, *argv);
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700235
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700236 close(fd);
237 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700238}
239
240const char *get_fd_type_name(enum bpf_obj_type type)
241{
242 static const char * const names[] = {
243 [BPF_OBJ_UNKNOWN] = "unknown",
244 [BPF_OBJ_PROG] = "prog",
245 [BPF_OBJ_MAP] = "map",
246 };
247
248 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
249 return names[BPF_OBJ_UNKNOWN];
250
251 return names[type];
252}
253
254int get_fd_type(int fd)
255{
256 char path[PATH_MAX];
257 char buf[512];
258 ssize_t n;
259
260 snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
261
262 n = readlink(path, buf, sizeof(buf));
263 if (n < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700264 p_err("can't read link type: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700265 return -1;
266 }
267 if (n == sizeof(path)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700268 p_err("can't read link type: path too long!");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700269 return -1;
270 }
271
272 if (strstr(buf, "bpf-map"))
273 return BPF_OBJ_MAP;
274 else if (strstr(buf, "bpf-prog"))
275 return BPF_OBJ_PROG;
276
277 return BPF_OBJ_UNKNOWN;
278}
279
280char *get_fdinfo(int fd, const char *key)
281{
282 char path[PATH_MAX];
283 char *line = NULL;
284 size_t line_n = 0;
285 ssize_t n;
286 FILE *fdi;
287
288 snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
289
290 fdi = fopen(path, "r");
291 if (!fdi) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700292 p_err("can't open fdinfo: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700293 return NULL;
294 }
295
296 while ((n = getline(&line, &line_n, fdi))) {
297 char *value;
298 int len;
299
300 if (!strstr(line, key))
301 continue;
302
303 fclose(fdi);
304
305 value = strchr(line, '\t');
306 if (!value || !value[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700307 p_err("malformed fdinfo!?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700308 free(line);
309 return NULL;
310 }
311 value++;
312
313 len = strlen(value);
314 memmove(line, value, len);
315 line[len - 1] = '\0';
316
317 return line;
318 }
319
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700320 p_err("key '%s' not found in fdinfo", key);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700321 free(line);
322 fclose(fdi);
323 return NULL;
324}
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700325
326void print_hex_data_json(uint8_t *data, size_t len)
327{
328 unsigned int i;
329
330 jsonw_start_array(json_wtr);
331 for (i = 0; i < len; i++)
332 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
333 jsonw_end_array(json_wtr);
334}
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900335
336int build_pinned_obj_table(struct pinned_obj_table *tab,
337 enum bpf_obj_type type)
338{
339 struct bpf_prog_info pinned_info = {};
340 struct pinned_obj *obj_node = NULL;
341 __u32 len = sizeof(pinned_info);
342 struct mntent *mntent = NULL;
343 enum bpf_obj_type objtype;
344 FILE *mntfile = NULL;
345 FTSENT *ftse = NULL;
346 FTS *fts = NULL;
347 int fd, err;
348
349 mntfile = setmntent("/proc/mounts", "r");
350 if (!mntfile)
351 return -1;
352
353 while ((mntent = getmntent(mntfile))) {
354 char *path[] = { mntent->mnt_dir, NULL };
355
356 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
357 continue;
358
359 fts = fts_open(path, 0, NULL);
360 if (!fts)
361 continue;
362
363 while ((ftse = fts_read(fts))) {
364 if (!(ftse->fts_info & FTS_F))
365 continue;
366 fd = open_obj_pinned(ftse->fts_path);
367 if (fd < 0)
368 continue;
369
370 objtype = get_fd_type(fd);
371 if (objtype != type) {
372 close(fd);
373 continue;
374 }
375 memset(&pinned_info, 0, sizeof(pinned_info));
376 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
377 if (err) {
378 close(fd);
379 continue;
380 }
381
382 obj_node = malloc(sizeof(*obj_node));
383 if (!obj_node) {
384 close(fd);
385 fts_close(fts);
386 fclose(mntfile);
387 return -1;
388 }
389
390 memset(obj_node, 0, sizeof(*obj_node));
391 obj_node->id = pinned_info.id;
392 obj_node->path = strdup(ftse->fts_path);
393 hash_add(tab->table, &obj_node->hash, obj_node->id);
394
395 close(fd);
396 }
397 fts_close(fts);
398 }
399 fclose(mntfile);
400 return 0;
401}
402
403void delete_pinned_obj_table(struct pinned_obj_table *tab)
404{
405 struct pinned_obj *obj;
406 struct hlist_node *tmp;
407 unsigned int bkt;
408
409 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
410 hash_del(&obj->hash);
411 free(obj->path);
412 free(obj);
413 }
414}