Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 1 | /* |
Jakub Kicinski | e64d525 | 2018-05-03 18:37:15 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017-2018 Netronome Systems, Inc. |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | e64d525 | 2018-05-03 18:37:15 -0700 | [diff] [blame] | 34 | #include <ctype.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 35 | #include <errno.h> |
Jiong Wang | e659359 | 2018-01-16 16:05:21 -0800 | [diff] [blame] | 36 | #include <fcntl.h> |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 37 | #include <fts.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 38 | #include <libgen.h> |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 39 | #include <mntent.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 40 | #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> |
Jakub Kicinski | 5226221 | 2017-12-27 18:39:10 -0800 | [diff] [blame] | 47 | #include <net/if.h> |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 48 | #include <sys/mount.h> |
Quentin Monnet | 8302b9b | 2018-11-07 12:29:30 +0000 | [diff] [blame^] | 49 | #include <sys/resource.h> |
Jakub Kicinski | 5226221 | 2017-12-27 18:39:10 -0800 | [diff] [blame] | 50 | #include <sys/stat.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 51 | #include <sys/types.h> |
| 52 | #include <sys/vfs.h> |
| 53 | |
| 54 | #include <bpf.h> |
| 55 | |
| 56 | #include "main.h" |
| 57 | |
Jiri Benc | 20d5de5 | 2018-03-06 14:50:10 +0100 | [diff] [blame] | 58 | #ifndef BPF_FS_MAGIC |
| 59 | #define BPF_FS_MAGIC 0xcafe4a11 |
| 60 | #endif |
| 61 | |
Quentin Monnet | 0b1c27d | 2017-11-03 13:59:07 -0700 | [diff] [blame] | 62 | void p_err(const char *fmt, ...) |
| 63 | { |
| 64 | va_list ap; |
| 65 | |
| 66 | va_start(ap, fmt); |
| 67 | if (json_output) { |
| 68 | jsonw_start_object(json_wtr); |
| 69 | jsonw_name(json_wtr, "error"); |
| 70 | jsonw_vprintf_enquote(json_wtr, fmt, ap); |
| 71 | jsonw_end_object(json_wtr); |
| 72 | } else { |
| 73 | fprintf(stderr, "Error: "); |
| 74 | vfprintf(stderr, fmt, ap); |
| 75 | fprintf(stderr, "\n"); |
| 76 | } |
| 77 | va_end(ap); |
| 78 | } |
| 79 | |
| 80 | void p_info(const char *fmt, ...) |
| 81 | { |
| 82 | va_list ap; |
| 83 | |
| 84 | if (json_output) |
| 85 | return; |
| 86 | |
| 87 | va_start(ap, fmt); |
| 88 | vfprintf(stderr, fmt, ap); |
| 89 | fprintf(stderr, "\n"); |
| 90 | va_end(ap); |
| 91 | } |
| 92 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 93 | static bool is_bpffs(char *path) |
| 94 | { |
| 95 | struct statfs st_fs; |
| 96 | |
| 97 | if (statfs(path, &st_fs) < 0) |
| 98 | return false; |
| 99 | |
| 100 | return (unsigned long)st_fs.f_type == BPF_FS_MAGIC; |
| 101 | } |
| 102 | |
Quentin Monnet | 8302b9b | 2018-11-07 12:29:30 +0000 | [diff] [blame^] | 103 | void set_max_rlimit(void) |
| 104 | { |
| 105 | struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; |
| 106 | |
| 107 | setrlimit(RLIMIT_MEMLOCK, &rinf); |
| 108 | } |
| 109 | |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 110 | static int mnt_bpffs(const char *target, char *buff, size_t bufflen) |
| 111 | { |
| 112 | bool bind_done = false; |
| 113 | |
| 114 | while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) { |
| 115 | if (errno != EINVAL || bind_done) { |
| 116 | snprintf(buff, bufflen, |
| 117 | "mount --make-private %s failed: %s", |
| 118 | target, strerror(errno)); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | if (mount(target, target, "none", MS_BIND, NULL)) { |
| 123 | snprintf(buff, bufflen, |
| 124 | "mount --bind %s %s failed: %s", |
| 125 | target, target, strerror(errno)); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | bind_done = true; |
| 130 | } |
| 131 | |
| 132 | if (mount("bpf", target, "bpf", 0, "mode=0700")) { |
| 133 | snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s", |
| 134 | target, strerror(errno)); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Prashant Bhole | 1852719 | 2017-11-08 13:55:47 +0900 | [diff] [blame] | 141 | int open_obj_pinned(char *path) |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 142 | { |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 143 | int fd; |
| 144 | |
| 145 | fd = bpf_obj_get(path); |
| 146 | if (fd < 0) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 147 | p_err("bpf obj get (%s): %s", path, |
| 148 | errno == EACCES && !is_bpffs(dirname(path)) ? |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 149 | "directory not in bpf file system (bpffs)" : |
| 150 | strerror(errno)); |
| 151 | return -1; |
| 152 | } |
| 153 | |
Prashant Bhole | 1852719 | 2017-11-08 13:55:47 +0900 | [diff] [blame] | 154 | return fd; |
| 155 | } |
| 156 | |
| 157 | int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type) |
| 158 | { |
| 159 | enum bpf_obj_type type; |
| 160 | int fd; |
| 161 | |
| 162 | fd = open_obj_pinned(path); |
| 163 | if (fd < 0) |
| 164 | return -1; |
| 165 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 166 | type = get_fd_type(fd); |
| 167 | if (type < 0) { |
| 168 | close(fd); |
| 169 | return type; |
| 170 | } |
| 171 | if (type != exp_type) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 172 | p_err("incorrect object type: %s", get_fd_type_name(type)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 173 | close(fd); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | return fd; |
| 178 | } |
| 179 | |
Roman Gushchin | 49a086c | 2017-12-13 15:18:53 +0000 | [diff] [blame] | 180 | int do_pin_fd(int fd, const char *name) |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 181 | { |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 182 | char err_str[ERR_MAX_LEN]; |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 183 | char *file; |
| 184 | char *dir; |
Roman Gushchin | 49a086c | 2017-12-13 15:18:53 +0000 | [diff] [blame] | 185 | int err = 0; |
| 186 | |
| 187 | err = bpf_obj_pin(fd, name); |
| 188 | if (!err) |
| 189 | goto out; |
| 190 | |
| 191 | file = malloc(strlen(name) + 1); |
| 192 | strcpy(file, name); |
| 193 | dir = dirname(file); |
| 194 | |
| 195 | if (errno != EPERM || is_bpffs(dir)) { |
| 196 | p_err("can't pin the object (%s): %s", name, strerror(errno)); |
| 197 | goto out_free; |
| 198 | } |
| 199 | |
| 200 | /* Attempt to mount bpffs, then retry pinning. */ |
| 201 | err = mnt_bpffs(dir, err_str, ERR_MAX_LEN); |
| 202 | if (!err) { |
| 203 | err = bpf_obj_pin(fd, name); |
| 204 | if (err) |
| 205 | p_err("can't pin the object (%s): %s", name, |
| 206 | strerror(errno)); |
| 207 | } else { |
| 208 | err_str[ERR_MAX_LEN - 1] = '\0'; |
| 209 | p_err("can't mount BPF file system to pin the object (%s): %s", |
| 210 | name, err_str); |
| 211 | } |
| 212 | |
| 213 | out_free: |
| 214 | free(file); |
| 215 | out: |
| 216 | return err; |
| 217 | } |
| 218 | |
| 219 | int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)) |
| 220 | { |
| 221 | unsigned int id; |
| 222 | char *endptr; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 223 | int err; |
| 224 | int fd; |
| 225 | |
Taeung Song | 759b94a | 2018-07-19 21:10:05 +0900 | [diff] [blame] | 226 | if (argc < 3) { |
| 227 | p_err("too few arguments, id ID and FILE path is required"); |
| 228 | return -1; |
| 229 | } else if (argc > 3) { |
| 230 | p_err("too many arguments"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 234 | if (!is_prefix(*argv, "id")) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 235 | p_err("expected 'id' got %s", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | NEXT_ARG(); |
| 239 | |
| 240 | id = strtoul(*argv, &endptr, 0); |
| 241 | if (*endptr) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 242 | p_err("can't parse %s as ID", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 243 | return -1; |
| 244 | } |
| 245 | NEXT_ARG(); |
| 246 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 247 | fd = get_fd_by_id(id); |
| 248 | if (fd < 0) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 249 | p_err("can't get prog by id (%u): %s", id, strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | |
Roman Gushchin | 49a086c | 2017-12-13 15:18:53 +0000 | [diff] [blame] | 253 | err = do_pin_fd(fd, *argv); |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 254 | |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 255 | close(fd); |
| 256 | return err; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | const char *get_fd_type_name(enum bpf_obj_type type) |
| 260 | { |
| 261 | static const char * const names[] = { |
| 262 | [BPF_OBJ_UNKNOWN] = "unknown", |
| 263 | [BPF_OBJ_PROG] = "prog", |
| 264 | [BPF_OBJ_MAP] = "map", |
| 265 | }; |
| 266 | |
| 267 | if (type < 0 || type >= ARRAY_SIZE(names) || !names[type]) |
| 268 | return names[BPF_OBJ_UNKNOWN]; |
| 269 | |
| 270 | return names[type]; |
| 271 | } |
| 272 | |
| 273 | int get_fd_type(int fd) |
| 274 | { |
| 275 | char path[PATH_MAX]; |
| 276 | char buf[512]; |
| 277 | ssize_t n; |
| 278 | |
| 279 | snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd); |
| 280 | |
| 281 | n = readlink(path, buf, sizeof(buf)); |
| 282 | if (n < 0) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 283 | p_err("can't read link type: %s", strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 284 | return -1; |
| 285 | } |
| 286 | if (n == sizeof(path)) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 287 | p_err("can't read link type: path too long!"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | if (strstr(buf, "bpf-map")) |
| 292 | return BPF_OBJ_MAP; |
| 293 | else if (strstr(buf, "bpf-prog")) |
| 294 | return BPF_OBJ_PROG; |
| 295 | |
| 296 | return BPF_OBJ_UNKNOWN; |
| 297 | } |
| 298 | |
| 299 | char *get_fdinfo(int fd, const char *key) |
| 300 | { |
| 301 | char path[PATH_MAX]; |
| 302 | char *line = NULL; |
| 303 | size_t line_n = 0; |
| 304 | ssize_t n; |
| 305 | FILE *fdi; |
| 306 | |
| 307 | snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd); |
| 308 | |
| 309 | fdi = fopen(path, "r"); |
| 310 | if (!fdi) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 311 | p_err("can't open fdinfo: %s", strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | while ((n = getline(&line, &line_n, fdi))) { |
| 316 | char *value; |
| 317 | int len; |
| 318 | |
| 319 | if (!strstr(line, key)) |
| 320 | continue; |
| 321 | |
| 322 | fclose(fdi); |
| 323 | |
| 324 | value = strchr(line, '\t'); |
| 325 | if (!value || !value[1]) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 326 | p_err("malformed fdinfo!?"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 327 | free(line); |
| 328 | return NULL; |
| 329 | } |
| 330 | value++; |
| 331 | |
| 332 | len = strlen(value); |
| 333 | memmove(line, value, len); |
| 334 | line[len - 1] = '\0'; |
| 335 | |
| 336 | return line; |
| 337 | } |
| 338 | |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 339 | p_err("key '%s' not found in fdinfo", key); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 340 | free(line); |
| 341 | fclose(fdi); |
| 342 | return NULL; |
| 343 | } |
Quentin Monnet | f05e2c3 | 2017-10-23 09:24:10 -0700 | [diff] [blame] | 344 | |
Jakub Kicinski | f412eed | 2018-05-03 18:37:16 -0700 | [diff] [blame] | 345 | void print_data_json(uint8_t *data, size_t len) |
| 346 | { |
| 347 | unsigned int i; |
| 348 | |
| 349 | jsonw_start_array(json_wtr); |
| 350 | for (i = 0; i < len; i++) |
| 351 | jsonw_printf(json_wtr, "%d", data[i]); |
| 352 | jsonw_end_array(json_wtr); |
| 353 | } |
| 354 | |
Quentin Monnet | f05e2c3 | 2017-10-23 09:24:10 -0700 | [diff] [blame] | 355 | void print_hex_data_json(uint8_t *data, size_t len) |
| 356 | { |
| 357 | unsigned int i; |
| 358 | |
| 359 | jsonw_start_array(json_wtr); |
| 360 | for (i = 0; i < len; i++) |
| 361 | jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]); |
| 362 | jsonw_end_array(json_wtr); |
| 363 | } |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 364 | |
| 365 | int build_pinned_obj_table(struct pinned_obj_table *tab, |
| 366 | enum bpf_obj_type type) |
| 367 | { |
| 368 | struct bpf_prog_info pinned_info = {}; |
| 369 | struct pinned_obj *obj_node = NULL; |
| 370 | __u32 len = sizeof(pinned_info); |
| 371 | struct mntent *mntent = NULL; |
| 372 | enum bpf_obj_type objtype; |
| 373 | FILE *mntfile = NULL; |
| 374 | FTSENT *ftse = NULL; |
| 375 | FTS *fts = NULL; |
| 376 | int fd, err; |
| 377 | |
| 378 | mntfile = setmntent("/proc/mounts", "r"); |
| 379 | if (!mntfile) |
| 380 | return -1; |
| 381 | |
| 382 | while ((mntent = getmntent(mntfile))) { |
| 383 | char *path[] = { mntent->mnt_dir, NULL }; |
| 384 | |
| 385 | if (strncmp(mntent->mnt_type, "bpf", 3) != 0) |
| 386 | continue; |
| 387 | |
| 388 | fts = fts_open(path, 0, NULL); |
| 389 | if (!fts) |
| 390 | continue; |
| 391 | |
| 392 | while ((ftse = fts_read(fts))) { |
| 393 | if (!(ftse->fts_info & FTS_F)) |
| 394 | continue; |
| 395 | fd = open_obj_pinned(ftse->fts_path); |
| 396 | if (fd < 0) |
| 397 | continue; |
| 398 | |
| 399 | objtype = get_fd_type(fd); |
| 400 | if (objtype != type) { |
| 401 | close(fd); |
| 402 | continue; |
| 403 | } |
| 404 | memset(&pinned_info, 0, sizeof(pinned_info)); |
| 405 | err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len); |
| 406 | if (err) { |
| 407 | close(fd); |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | obj_node = malloc(sizeof(*obj_node)); |
| 412 | if (!obj_node) { |
| 413 | close(fd); |
| 414 | fts_close(fts); |
| 415 | fclose(mntfile); |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | memset(obj_node, 0, sizeof(*obj_node)); |
| 420 | obj_node->id = pinned_info.id; |
| 421 | obj_node->path = strdup(ftse->fts_path); |
| 422 | hash_add(tab->table, &obj_node->hash, obj_node->id); |
| 423 | |
| 424 | close(fd); |
| 425 | } |
| 426 | fts_close(fts); |
| 427 | } |
| 428 | fclose(mntfile); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | void delete_pinned_obj_table(struct pinned_obj_table *tab) |
| 433 | { |
| 434 | struct pinned_obj *obj; |
| 435 | struct hlist_node *tmp; |
| 436 | unsigned int bkt; |
| 437 | |
| 438 | hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { |
| 439 | hash_del(&obj->hash); |
| 440 | free(obj->path); |
| 441 | free(obj); |
| 442 | } |
| 443 | } |
Jakub Kicinski | 5226221 | 2017-12-27 18:39:10 -0800 | [diff] [blame] | 444 | |
Jakub Kicinski | f412eed | 2018-05-03 18:37:16 -0700 | [diff] [blame] | 445 | unsigned int get_page_size(void) |
| 446 | { |
| 447 | static int result; |
| 448 | |
| 449 | if (!result) |
| 450 | result = getpagesize(); |
| 451 | return result; |
| 452 | } |
| 453 | |
Jakub Kicinski | e64d525 | 2018-05-03 18:37:15 -0700 | [diff] [blame] | 454 | unsigned int get_possible_cpus(void) |
| 455 | { |
| 456 | static unsigned int result; |
| 457 | char buf[128]; |
| 458 | long int n; |
| 459 | char *ptr; |
| 460 | int fd; |
| 461 | |
| 462 | if (result) |
| 463 | return result; |
| 464 | |
| 465 | fd = open("/sys/devices/system/cpu/possible", O_RDONLY); |
| 466 | if (fd < 0) { |
| 467 | p_err("can't open sysfs possible cpus"); |
| 468 | exit(-1); |
| 469 | } |
| 470 | |
| 471 | n = read(fd, buf, sizeof(buf)); |
| 472 | if (n < 2) { |
| 473 | p_err("can't read sysfs possible cpus"); |
| 474 | exit(-1); |
| 475 | } |
| 476 | close(fd); |
| 477 | |
| 478 | if (n == sizeof(buf)) { |
| 479 | p_err("read sysfs possible cpus overflow"); |
| 480 | exit(-1); |
| 481 | } |
| 482 | |
| 483 | ptr = buf; |
| 484 | n = 0; |
| 485 | while (*ptr && *ptr != '\n') { |
| 486 | unsigned int a, b; |
| 487 | |
| 488 | if (sscanf(ptr, "%u-%u", &a, &b) == 2) { |
| 489 | n += b - a + 1; |
| 490 | |
| 491 | ptr = strchr(ptr, '-') + 1; |
| 492 | } else if (sscanf(ptr, "%u", &a) == 1) { |
| 493 | n++; |
| 494 | } else { |
| 495 | assert(0); |
| 496 | } |
| 497 | |
| 498 | while (isdigit(*ptr)) |
| 499 | ptr++; |
| 500 | if (*ptr == ',') |
| 501 | ptr++; |
| 502 | } |
| 503 | |
| 504 | result = n; |
| 505 | |
| 506 | return result; |
| 507 | } |
| 508 | |
Jakub Kicinski | 5226221 | 2017-12-27 18:39:10 -0800 | [diff] [blame] | 509 | static char * |
| 510 | ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf) |
| 511 | { |
| 512 | struct stat st; |
| 513 | int err; |
| 514 | |
| 515 | err = stat("/proc/self/ns/net", &st); |
| 516 | if (err) { |
| 517 | p_err("Can't stat /proc/self: %s", strerror(errno)); |
| 518 | return NULL; |
| 519 | } |
| 520 | |
| 521 | if (st.st_dev != ns_dev || st.st_ino != ns_ino) |
| 522 | return NULL; |
| 523 | |
| 524 | return if_indextoname(ifindex, buf); |
| 525 | } |
| 526 | |
Jiong Wang | e659359 | 2018-01-16 16:05:21 -0800 | [diff] [blame] | 527 | static int read_sysfs_hex_int(char *path) |
| 528 | { |
| 529 | char vendor_id_buf[8]; |
| 530 | int len; |
| 531 | int fd; |
| 532 | |
| 533 | fd = open(path, O_RDONLY); |
| 534 | if (fd < 0) { |
| 535 | p_err("Can't open %s: %s", path, strerror(errno)); |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | len = read(fd, vendor_id_buf, sizeof(vendor_id_buf)); |
| 540 | close(fd); |
| 541 | if (len < 0) { |
| 542 | p_err("Can't read %s: %s", path, strerror(errno)); |
| 543 | return -1; |
| 544 | } |
| 545 | if (len >= (int)sizeof(vendor_id_buf)) { |
| 546 | p_err("Value in %s too long", path); |
| 547 | return -1; |
| 548 | } |
| 549 | |
| 550 | vendor_id_buf[len] = 0; |
| 551 | |
| 552 | return strtol(vendor_id_buf, NULL, 0); |
| 553 | } |
| 554 | |
| 555 | static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name) |
| 556 | { |
| 557 | char full_path[64]; |
| 558 | |
| 559 | snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s", |
| 560 | devname, entry_name); |
| 561 | |
| 562 | return read_sysfs_hex_int(full_path); |
| 563 | } |
| 564 | |
Jakub Kicinski | 3ddeac6 | 2018-10-18 11:34:55 -0700 | [diff] [blame] | 565 | const char * |
| 566 | ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, |
| 567 | const char **opt) |
Jiong Wang | e659359 | 2018-01-16 16:05:21 -0800 | [diff] [blame] | 568 | { |
| 569 | char devname[IF_NAMESIZE]; |
| 570 | int vendor_id; |
| 571 | int device_id; |
| 572 | |
| 573 | if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) { |
| 574 | p_err("Can't get net device name for ifindex %d: %s", ifindex, |
| 575 | strerror(errno)); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | vendor_id = read_sysfs_netdev_hex_int(devname, "vendor"); |
| 580 | if (vendor_id < 0) { |
| 581 | p_err("Can't get device vendor id for %s", devname); |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | switch (vendor_id) { |
| 586 | case 0x19ee: |
| 587 | device_id = read_sysfs_netdev_hex_int(devname, "device"); |
| 588 | if (device_id != 0x4000 && |
| 589 | device_id != 0x6000 && |
| 590 | device_id != 0x6003) |
| 591 | p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch"); |
Jakub Kicinski | 3ddeac6 | 2018-10-18 11:34:55 -0700 | [diff] [blame] | 592 | *opt = "ctx4"; |
Jiong Wang | e659359 | 2018-01-16 16:05:21 -0800 | [diff] [blame] | 593 | return "NFP-6xxx"; |
| 594 | default: |
| 595 | p_err("Can't get bfd arch name for device vendor id 0x%04x", |
| 596 | vendor_id); |
| 597 | return NULL; |
| 598 | } |
| 599 | } |
| 600 | |
Jakub Kicinski | 5226221 | 2017-12-27 18:39:10 -0800 | [diff] [blame] | 601 | void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) |
| 602 | { |
| 603 | char name[IF_NAMESIZE]; |
| 604 | |
| 605 | if (!ifindex) |
| 606 | return; |
| 607 | |
| 608 | printf(" dev "); |
| 609 | if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name)) |
| 610 | printf("%s", name); |
| 611 | else |
| 612 | printf("ifindex %u ns_dev %llu ns_ino %llu", |
| 613 | ifindex, ns_dev, ns_inode); |
| 614 | } |
| 615 | |
| 616 | void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) |
| 617 | { |
| 618 | char name[IF_NAMESIZE]; |
| 619 | |
| 620 | if (!ifindex) |
| 621 | return; |
| 622 | |
| 623 | jsonw_name(json_wtr, "dev"); |
| 624 | jsonw_start_object(json_wtr); |
| 625 | jsonw_uint_field(json_wtr, "ifindex", ifindex); |
| 626 | jsonw_uint_field(json_wtr, "ns_dev", ns_dev); |
| 627 | jsonw_uint_field(json_wtr, "ns_inode", ns_inode); |
| 628 | if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name)) |
| 629 | jsonw_string_field(json_wtr, "ifname", name); |
| 630 | jsonw_end_object(json_wtr); |
| 631 | } |
Jakub Kicinski | 0b592b5a | 2018-10-15 16:30:36 -0700 | [diff] [blame] | 632 | |
| 633 | int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what) |
| 634 | { |
| 635 | char *endptr; |
| 636 | |
| 637 | NEXT_ARGP(); |
| 638 | |
| 639 | if (*val) { |
| 640 | p_err("%s already specified", what); |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | *val = strtoul(**argv, &endptr, 0); |
| 645 | if (*endptr) { |
| 646 | p_err("can't parse %s as %s", **argv, what); |
| 647 | return -1; |
| 648 | } |
| 649 | NEXT_ARGP(); |
| 650 | |
| 651 | return 0; |
| 652 | } |