blob: 6b3d25d6a782043ce1b7fca54981e3133d134b9a [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>
37#include <libgen.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43#include <linux/limits.h>
44#include <linux/magic.h>
Quentin Monnet3fc27b72017-10-24 20:11:28 -070045#include <sys/mount.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070046#include <sys/types.h>
47#include <sys/vfs.h>
48
49#include <bpf.h>
50
51#include "main.h"
52
Quentin Monnet0b1c27d2017-11-03 13:59:07 -070053void p_err(const char *fmt, ...)
54{
55 va_list ap;
56
57 va_start(ap, fmt);
58 if (json_output) {
59 jsonw_start_object(json_wtr);
60 jsonw_name(json_wtr, "error");
61 jsonw_vprintf_enquote(json_wtr, fmt, ap);
62 jsonw_end_object(json_wtr);
63 } else {
64 fprintf(stderr, "Error: ");
65 vfprintf(stderr, fmt, ap);
66 fprintf(stderr, "\n");
67 }
68 va_end(ap);
69}
70
71void p_info(const char *fmt, ...)
72{
73 va_list ap;
74
75 if (json_output)
76 return;
77
78 va_start(ap, fmt);
79 vfprintf(stderr, fmt, ap);
80 fprintf(stderr, "\n");
81 va_end(ap);
82}
83
Jakub Kicinski71bb4282017-10-04 20:10:04 -070084static bool is_bpffs(char *path)
85{
86 struct statfs st_fs;
87
88 if (statfs(path, &st_fs) < 0)
89 return false;
90
91 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
92}
93
Quentin Monnet3fc27b72017-10-24 20:11:28 -070094static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
95{
96 bool bind_done = false;
97
98 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
99 if (errno != EINVAL || bind_done) {
100 snprintf(buff, bufflen,
101 "mount --make-private %s failed: %s",
102 target, strerror(errno));
103 return -1;
104 }
105
106 if (mount(target, target, "none", MS_BIND, NULL)) {
107 snprintf(buff, bufflen,
108 "mount --bind %s %s failed: %s",
109 target, target, strerror(errno));
110 return -1;
111 }
112
113 bind_done = true;
114 }
115
116 if (mount("bpf", target, "bpf", 0, "mode=0700")) {
117 snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
118 target, strerror(errno));
119 return -1;
120 }
121
122 return 0;
123}
124
Prashant Bhole18527192017-11-08 13:55:47 +0900125int open_obj_pinned(char *path)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700126{
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700127 int fd;
128
129 fd = bpf_obj_get(path);
130 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700131 p_err("bpf obj get (%s): %s", path,
132 errno == EACCES && !is_bpffs(dirname(path)) ?
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700133 "directory not in bpf file system (bpffs)" :
134 strerror(errno));
135 return -1;
136 }
137
Prashant Bhole18527192017-11-08 13:55:47 +0900138 return fd;
139}
140
141int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
142{
143 enum bpf_obj_type type;
144 int fd;
145
146 fd = open_obj_pinned(path);
147 if (fd < 0)
148 return -1;
149
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700150 type = get_fd_type(fd);
151 if (type < 0) {
152 close(fd);
153 return type;
154 }
155 if (type != exp_type) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700156 p_err("incorrect object type: %s", get_fd_type_name(type));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700157 close(fd);
158 return -1;
159 }
160
161 return fd;
162}
163
164int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
165{
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700166 char err_str[ERR_MAX_LEN];
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700167 unsigned int id;
168 char *endptr;
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700169 char *file;
170 char *dir;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700171 int err;
172 int fd;
173
174 if (!is_prefix(*argv, "id")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700175 p_err("expected 'id' got %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700176 return -1;
177 }
178 NEXT_ARG();
179
180 id = strtoul(*argv, &endptr, 0);
181 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700182 p_err("can't parse %s as ID", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700183 return -1;
184 }
185 NEXT_ARG();
186
187 if (argc != 1)
188 usage();
189
190 fd = get_fd_by_id(id);
191 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700192 p_err("can't get prog by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700193 return -1;
194 }
195
196 err = bpf_obj_pin(fd, *argv);
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700197 if (!err)
198 goto out_close;
199
200 file = malloc(strlen(*argv) + 1);
201 strcpy(file, *argv);
202 dir = dirname(file);
203
204 if (errno != EPERM || is_bpffs(dir)) {
205 p_err("can't pin the object (%s): %s", *argv, strerror(errno));
206 goto out_free;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700207 }
208
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700209 /* Attempt to mount bpffs, then retry pinning. */
210 err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
211 if (!err) {
212 err = bpf_obj_pin(fd, *argv);
213 if (err)
214 p_err("can't pin the object (%s): %s", *argv,
215 strerror(errno));
216 } else {
217 err_str[ERR_MAX_LEN - 1] = '\0';
218 p_err("can't mount BPF file system to pin the object (%s): %s",
219 *argv, err_str);
220 }
221
222out_free:
223 free(file);
224out_close:
225 close(fd);
226 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700227}
228
229const char *get_fd_type_name(enum bpf_obj_type type)
230{
231 static const char * const names[] = {
232 [BPF_OBJ_UNKNOWN] = "unknown",
233 [BPF_OBJ_PROG] = "prog",
234 [BPF_OBJ_MAP] = "map",
235 };
236
237 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
238 return names[BPF_OBJ_UNKNOWN];
239
240 return names[type];
241}
242
243int get_fd_type(int fd)
244{
245 char path[PATH_MAX];
246 char buf[512];
247 ssize_t n;
248
249 snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
250
251 n = readlink(path, buf, sizeof(buf));
252 if (n < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700253 p_err("can't read link type: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700254 return -1;
255 }
256 if (n == sizeof(path)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700257 p_err("can't read link type: path too long!");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700258 return -1;
259 }
260
261 if (strstr(buf, "bpf-map"))
262 return BPF_OBJ_MAP;
263 else if (strstr(buf, "bpf-prog"))
264 return BPF_OBJ_PROG;
265
266 return BPF_OBJ_UNKNOWN;
267}
268
269char *get_fdinfo(int fd, const char *key)
270{
271 char path[PATH_MAX];
272 char *line = NULL;
273 size_t line_n = 0;
274 ssize_t n;
275 FILE *fdi;
276
277 snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
278
279 fdi = fopen(path, "r");
280 if (!fdi) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700281 p_err("can't open fdinfo: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700282 return NULL;
283 }
284
285 while ((n = getline(&line, &line_n, fdi))) {
286 char *value;
287 int len;
288
289 if (!strstr(line, key))
290 continue;
291
292 fclose(fdi);
293
294 value = strchr(line, '\t');
295 if (!value || !value[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700296 p_err("malformed fdinfo!?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700297 free(line);
298 return NULL;
299 }
300 value++;
301
302 len = strlen(value);
303 memmove(line, value, len);
304 line[len - 1] = '\0';
305
306 return line;
307 }
308
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700309 p_err("key '%s' not found in fdinfo", key);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700310 free(line);
311 fclose(fdi);
312 return NULL;
313}
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700314
315void print_hex_data_json(uint8_t *data, size_t len)
316{
317 unsigned int i;
318
319 jsonw_start_array(json_wtr);
320 for (i = 0; i < len; i++)
321 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
322 jsonw_end_array(json_wtr);
323}