common.c 12 KB

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