common.c 12 KB

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