common.c 12 KB

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