common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * Copyright (C) 2017-2018 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. #include <ctype.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <fts.h>
  37. #include <libgen.h>
  38. #include <mntent.h>
  39. #include <stdbool.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <linux/limits.h>
  45. #include <linux/magic.h>
  46. #include <net/if.h>
  47. #include <sys/mount.h>
  48. #include <sys/stat.h>
  49. #include <sys/types.h>
  50. #include <sys/vfs.h>
  51. #include <bpf.h>
  52. #include "main.h"
  53. #ifndef BPF_FS_MAGIC
  54. #define BPF_FS_MAGIC 0xcafe4a11
  55. #endif
  56. void p_err(const char *fmt, ...)
  57. {
  58. va_list ap;
  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. void p_info(const char *fmt, ...)
  73. {
  74. va_list ap;
  75. if (json_output)
  76. return;
  77. va_start(ap, fmt);
  78. vfprintf(stderr, fmt, ap);
  79. fprintf(stderr, "\n");
  80. va_end(ap);
  81. }
  82. static bool is_bpffs(char *path)
  83. {
  84. struct statfs st_fs;
  85. if (statfs(path, &st_fs) < 0)
  86. return false;
  87. return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
  88. }
  89. static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
  90. {
  91. bool bind_done = false;
  92. while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
  93. if (errno != EINVAL || bind_done) {
  94. snprintf(buff, bufflen,
  95. "mount --make-private %s failed: %s",
  96. target, strerror(errno));
  97. return -1;
  98. }
  99. if (mount(target, target, "none", MS_BIND, NULL)) {
  100. snprintf(buff, bufflen,
  101. "mount --bind %s %s failed: %s",
  102. target, target, strerror(errno));
  103. return -1;
  104. }
  105. bind_done = true;
  106. }
  107. if (mount("bpf", target, "bpf", 0, "mode=0700")) {
  108. snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
  109. target, strerror(errno));
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. int open_obj_pinned(char *path)
  115. {
  116. int fd;
  117. fd = bpf_obj_get(path);
  118. if (fd < 0) {
  119. p_err("bpf obj get (%s): %s", path,
  120. errno == EACCES && !is_bpffs(dirname(path)) ?
  121. "directory not in bpf file system (bpffs)" :
  122. strerror(errno));
  123. return -1;
  124. }
  125. return fd;
  126. }
  127. int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
  128. {
  129. enum bpf_obj_type type;
  130. int fd;
  131. fd = open_obj_pinned(path);
  132. if (fd < 0)
  133. return -1;
  134. type = get_fd_type(fd);
  135. if (type < 0) {
  136. close(fd);
  137. return type;
  138. }
  139. if (type != exp_type) {
  140. p_err("incorrect object type: %s", get_fd_type_name(type));
  141. close(fd);
  142. return -1;
  143. }
  144. return fd;
  145. }
  146. int do_pin_fd(int fd, const char *name)
  147. {
  148. char err_str[ERR_MAX_LEN];
  149. char *file;
  150. char *dir;
  151. int err = 0;
  152. err = bpf_obj_pin(fd, name);
  153. if (!err)
  154. goto out;
  155. file = malloc(strlen(name) + 1);
  156. strcpy(file, name);
  157. dir = dirname(file);
  158. if (errno != EPERM || is_bpffs(dir)) {
  159. p_err("can't pin the object (%s): %s", name, strerror(errno));
  160. goto out_free;
  161. }
  162. /* Attempt to mount bpffs, then retry pinning. */
  163. err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
  164. if (!err) {
  165. err = bpf_obj_pin(fd, name);
  166. if (err)
  167. p_err("can't pin the object (%s): %s", name,
  168. strerror(errno));
  169. } else {
  170. err_str[ERR_MAX_LEN - 1] = '\0';
  171. p_err("can't mount BPF file system to pin the object (%s): %s",
  172. name, err_str);
  173. }
  174. out_free:
  175. free(file);
  176. out:
  177. return err;
  178. }
  179. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
  180. {
  181. unsigned int id;
  182. char *endptr;
  183. int err;
  184. int fd;
  185. if (argc < 3) {
  186. p_err("too few arguments, id ID and FILE path is required");
  187. return -1;
  188. } else if (argc > 3) {
  189. p_err("too many arguments");
  190. return -1;
  191. }
  192. if (!is_prefix(*argv, "id")) {
  193. p_err("expected 'id' got %s", *argv);
  194. return -1;
  195. }
  196. NEXT_ARG();
  197. id = strtoul(*argv, &endptr, 0);
  198. if (*endptr) {
  199. p_err("can't parse %s as ID", *argv);
  200. return -1;
  201. }
  202. NEXT_ARG();
  203. fd = get_fd_by_id(id);
  204. if (fd < 0) {
  205. p_err("can't get prog by id (%u): %s", id, strerror(errno));
  206. return -1;
  207. }
  208. err = do_pin_fd(fd, *argv);
  209. close(fd);
  210. return err;
  211. }
  212. const char *get_fd_type_name(enum bpf_obj_type type)
  213. {
  214. static const char * const names[] = {
  215. [BPF_OBJ_UNKNOWN] = "unknown",
  216. [BPF_OBJ_PROG] = "prog",
  217. [BPF_OBJ_MAP] = "map",
  218. };
  219. if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
  220. return names[BPF_OBJ_UNKNOWN];
  221. return names[type];
  222. }
  223. int get_fd_type(int fd)
  224. {
  225. char path[PATH_MAX];
  226. char buf[512];
  227. ssize_t n;
  228. snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
  229. n = readlink(path, buf, sizeof(buf));
  230. if (n < 0) {
  231. p_err("can't read link type: %s", strerror(errno));
  232. return -1;
  233. }
  234. if (n == sizeof(path)) {
  235. p_err("can't read link type: path too long!");
  236. return -1;
  237. }
  238. if (strstr(buf, "bpf-map"))
  239. return BPF_OBJ_MAP;
  240. else if (strstr(buf, "bpf-prog"))
  241. return BPF_OBJ_PROG;
  242. return BPF_OBJ_UNKNOWN;
  243. }
  244. char *get_fdinfo(int fd, const char *key)
  245. {
  246. char path[PATH_MAX];
  247. char *line = NULL;
  248. size_t line_n = 0;
  249. ssize_t n;
  250. FILE *fdi;
  251. snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
  252. fdi = fopen(path, "r");
  253. if (!fdi) {
  254. p_err("can't open fdinfo: %s", strerror(errno));
  255. return NULL;
  256. }
  257. while ((n = getline(&line, &line_n, fdi))) {
  258. char *value;
  259. int len;
  260. if (!strstr(line, key))
  261. continue;
  262. fclose(fdi);
  263. value = strchr(line, '\t');
  264. if (!value || !value[1]) {
  265. p_err("malformed fdinfo!?");
  266. free(line);
  267. return NULL;
  268. }
  269. value++;
  270. len = strlen(value);
  271. memmove(line, value, len);
  272. line[len - 1] = '\0';
  273. return line;
  274. }
  275. p_err("key '%s' not found in fdinfo", key);
  276. free(line);
  277. fclose(fdi);
  278. return NULL;
  279. }
  280. void print_data_json(uint8_t *data, size_t len)
  281. {
  282. unsigned int i;
  283. jsonw_start_array(json_wtr);
  284. for (i = 0; i < len; i++)
  285. jsonw_printf(json_wtr, "%d", data[i]);
  286. jsonw_end_array(json_wtr);
  287. }
  288. void print_hex_data_json(uint8_t *data, size_t len)
  289. {
  290. unsigned int i;
  291. jsonw_start_array(json_wtr);
  292. for (i = 0; i < len; i++)
  293. jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
  294. jsonw_end_array(json_wtr);
  295. }
  296. int build_pinned_obj_table(struct pinned_obj_table *tab,
  297. enum bpf_obj_type type)
  298. {
  299. struct bpf_prog_info pinned_info = {};
  300. struct pinned_obj *obj_node = NULL;
  301. __u32 len = sizeof(pinned_info);
  302. struct mntent *mntent = NULL;
  303. enum bpf_obj_type objtype;
  304. FILE *mntfile = NULL;
  305. FTSENT *ftse = NULL;
  306. FTS *fts = NULL;
  307. int fd, err;
  308. mntfile = setmntent("/proc/mounts", "r");
  309. if (!mntfile)
  310. return -1;
  311. while ((mntent = getmntent(mntfile))) {
  312. char *path[] = { mntent->mnt_dir, NULL };
  313. if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
  314. continue;
  315. fts = fts_open(path, 0, NULL);
  316. if (!fts)
  317. continue;
  318. while ((ftse = fts_read(fts))) {
  319. if (!(ftse->fts_info & FTS_F))
  320. continue;
  321. fd = open_obj_pinned(ftse->fts_path);
  322. if (fd < 0)
  323. continue;
  324. objtype = get_fd_type(fd);
  325. if (objtype != type) {
  326. close(fd);
  327. continue;
  328. }
  329. memset(&pinned_info, 0, sizeof(pinned_info));
  330. err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
  331. if (err) {
  332. close(fd);
  333. continue;
  334. }
  335. obj_node = malloc(sizeof(*obj_node));
  336. if (!obj_node) {
  337. close(fd);
  338. fts_close(fts);
  339. fclose(mntfile);
  340. return -1;
  341. }
  342. memset(obj_node, 0, sizeof(*obj_node));
  343. obj_node->id = pinned_info.id;
  344. obj_node->path = strdup(ftse->fts_path);
  345. hash_add(tab->table, &obj_node->hash, obj_node->id);
  346. close(fd);
  347. }
  348. fts_close(fts);
  349. }
  350. fclose(mntfile);
  351. return 0;
  352. }
  353. void delete_pinned_obj_table(struct pinned_obj_table *tab)
  354. {
  355. struct pinned_obj *obj;
  356. struct hlist_node *tmp;
  357. unsigned int bkt;
  358. hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
  359. hash_del(&obj->hash);
  360. free(obj->path);
  361. free(obj);
  362. }
  363. }
  364. unsigned int get_page_size(void)
  365. {
  366. static int result;
  367. if (!result)
  368. result = getpagesize();
  369. return result;
  370. }
  371. unsigned int get_possible_cpus(void)
  372. {
  373. static unsigned int result;
  374. char buf[128];
  375. long int n;
  376. char *ptr;
  377. int fd;
  378. if (result)
  379. return result;
  380. fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
  381. if (fd < 0) {
  382. p_err("can't open sysfs possible cpus");
  383. exit(-1);
  384. }
  385. n = read(fd, buf, sizeof(buf));
  386. if (n < 2) {
  387. p_err("can't read sysfs possible cpus");
  388. exit(-1);
  389. }
  390. close(fd);
  391. if (n == sizeof(buf)) {
  392. p_err("read sysfs possible cpus overflow");
  393. exit(-1);
  394. }
  395. ptr = buf;
  396. n = 0;
  397. while (*ptr && *ptr != '\n') {
  398. unsigned int a, b;
  399. if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
  400. n += b - a + 1;
  401. ptr = strchr(ptr, '-') + 1;
  402. } else if (sscanf(ptr, "%u", &a) == 1) {
  403. n++;
  404. } else {
  405. assert(0);
  406. }
  407. while (isdigit(*ptr))
  408. ptr++;
  409. if (*ptr == ',')
  410. ptr++;
  411. }
  412. result = n;
  413. return result;
  414. }
  415. static char *
  416. ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
  417. {
  418. struct stat st;
  419. int err;
  420. err = stat("/proc/self/ns/net", &st);
  421. if (err) {
  422. p_err("Can't stat /proc/self: %s", strerror(errno));
  423. return NULL;
  424. }
  425. if (st.st_dev != ns_dev || st.st_ino != ns_ino)
  426. return NULL;
  427. return if_indextoname(ifindex, buf);
  428. }
  429. static int read_sysfs_hex_int(char *path)
  430. {
  431. char vendor_id_buf[8];
  432. int len;
  433. int fd;
  434. fd = open(path, O_RDONLY);
  435. if (fd < 0) {
  436. p_err("Can't open %s: %s", path, strerror(errno));
  437. return -1;
  438. }
  439. len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
  440. close(fd);
  441. if (len < 0) {
  442. p_err("Can't read %s: %s", path, strerror(errno));
  443. return -1;
  444. }
  445. if (len >= (int)sizeof(vendor_id_buf)) {
  446. p_err("Value in %s too long", path);
  447. return -1;
  448. }
  449. vendor_id_buf[len] = 0;
  450. return strtol(vendor_id_buf, NULL, 0);
  451. }
  452. static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
  453. {
  454. char full_path[64];
  455. snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
  456. devname, entry_name);
  457. return read_sysfs_hex_int(full_path);
  458. }
  459. const char *
  460. ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
  461. const char **opt)
  462. {
  463. char devname[IF_NAMESIZE];
  464. int vendor_id;
  465. int device_id;
  466. if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
  467. p_err("Can't get net device name for ifindex %d: %s", ifindex,
  468. strerror(errno));
  469. return NULL;
  470. }
  471. vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
  472. if (vendor_id < 0) {
  473. p_err("Can't get device vendor id for %s", devname);
  474. return NULL;
  475. }
  476. switch (vendor_id) {
  477. case 0x19ee:
  478. device_id = read_sysfs_netdev_hex_int(devname, "device");
  479. if (device_id != 0x4000 &&
  480. device_id != 0x6000 &&
  481. device_id != 0x6003)
  482. p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
  483. *opt = "ctx4";
  484. return "NFP-6xxx";
  485. default:
  486. p_err("Can't get bfd arch name for device vendor id 0x%04x",
  487. vendor_id);
  488. return NULL;
  489. }
  490. }
  491. void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
  492. {
  493. char name[IF_NAMESIZE];
  494. if (!ifindex)
  495. return;
  496. printf(" dev ");
  497. if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
  498. printf("%s", name);
  499. else
  500. printf("ifindex %u ns_dev %llu ns_ino %llu",
  501. ifindex, ns_dev, ns_inode);
  502. }
  503. void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
  504. {
  505. char name[IF_NAMESIZE];
  506. if (!ifindex)
  507. return;
  508. jsonw_name(json_wtr, "dev");
  509. jsonw_start_object(json_wtr);
  510. jsonw_uint_field(json_wtr, "ifindex", ifindex);
  511. jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
  512. jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
  513. if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
  514. jsonw_string_field(json_wtr, "ifname", name);
  515. jsonw_end_object(json_wtr);
  516. }
  517. int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
  518. {
  519. char *endptr;
  520. NEXT_ARGP();
  521. if (*val) {
  522. p_err("%s already specified", what);
  523. return -1;
  524. }
  525. *val = strtoul(**argv, &endptr, 0);
  526. if (*endptr) {
  527. p_err("can't parse %s as %s", **argv, what);
  528. return -1;
  529. }
  530. NEXT_ARGP();
  531. return 0;
  532. }