common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (C) 2017 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 <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. void p_err(const char *fmt, ...)
  54. {
  55. va_list ap;
  56. va_start(ap, fmt);
  57. if (json_output) {
  58. jsonw_start_object(json_wtr);
  59. jsonw_name(json_wtr, "error");
  60. jsonw_vprintf_enquote(json_wtr, fmt, ap);
  61. jsonw_end_object(json_wtr);
  62. } else {
  63. fprintf(stderr, "Error: ");
  64. vfprintf(stderr, fmt, ap);
  65. fprintf(stderr, "\n");
  66. }
  67. va_end(ap);
  68. }
  69. void p_info(const char *fmt, ...)
  70. {
  71. va_list ap;
  72. if (json_output)
  73. return;
  74. va_start(ap, fmt);
  75. vfprintf(stderr, fmt, ap);
  76. fprintf(stderr, "\n");
  77. va_end(ap);
  78. }
  79. static bool is_bpffs(char *path)
  80. {
  81. struct statfs st_fs;
  82. if (statfs(path, &st_fs) < 0)
  83. return false;
  84. return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
  85. }
  86. static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
  87. {
  88. bool bind_done = false;
  89. while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
  90. if (errno != EINVAL || bind_done) {
  91. snprintf(buff, bufflen,
  92. "mount --make-private %s failed: %s",
  93. target, strerror(errno));
  94. return -1;
  95. }
  96. if (mount(target, target, "none", MS_BIND, NULL)) {
  97. snprintf(buff, bufflen,
  98. "mount --bind %s %s failed: %s",
  99. target, target, strerror(errno));
  100. return -1;
  101. }
  102. bind_done = true;
  103. }
  104. if (mount("bpf", target, "bpf", 0, "mode=0700")) {
  105. snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
  106. target, strerror(errno));
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. int open_obj_pinned(char *path)
  112. {
  113. int fd;
  114. fd = bpf_obj_get(path);
  115. if (fd < 0) {
  116. p_err("bpf obj get (%s): %s", path,
  117. errno == EACCES && !is_bpffs(dirname(path)) ?
  118. "directory not in bpf file system (bpffs)" :
  119. strerror(errno));
  120. return -1;
  121. }
  122. return fd;
  123. }
  124. int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
  125. {
  126. enum bpf_obj_type type;
  127. int fd;
  128. fd = open_obj_pinned(path);
  129. if (fd < 0)
  130. return -1;
  131. type = get_fd_type(fd);
  132. if (type < 0) {
  133. close(fd);
  134. return type;
  135. }
  136. if (type != exp_type) {
  137. p_err("incorrect object type: %s", get_fd_type_name(type));
  138. close(fd);
  139. return -1;
  140. }
  141. return fd;
  142. }
  143. int do_pin_fd(int fd, const char *name)
  144. {
  145. char err_str[ERR_MAX_LEN];
  146. char *file;
  147. char *dir;
  148. int err = 0;
  149. err = bpf_obj_pin(fd, name);
  150. if (!err)
  151. goto out;
  152. file = malloc(strlen(name) + 1);
  153. strcpy(file, name);
  154. dir = dirname(file);
  155. if (errno != EPERM || is_bpffs(dir)) {
  156. p_err("can't pin the object (%s): %s", name, strerror(errno));
  157. goto out_free;
  158. }
  159. /* Attempt to mount bpffs, then retry pinning. */
  160. err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
  161. if (!err) {
  162. err = bpf_obj_pin(fd, name);
  163. if (err)
  164. p_err("can't pin the object (%s): %s", name,
  165. strerror(errno));
  166. } else {
  167. err_str[ERR_MAX_LEN - 1] = '\0';
  168. p_err("can't mount BPF file system to pin the object (%s): %s",
  169. name, err_str);
  170. }
  171. out_free:
  172. free(file);
  173. out:
  174. return err;
  175. }
  176. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
  177. {
  178. unsigned int id;
  179. char *endptr;
  180. int err;
  181. int fd;
  182. if (!is_prefix(*argv, "id")) {
  183. p_err("expected 'id' got %s", *argv);
  184. return -1;
  185. }
  186. NEXT_ARG();
  187. id = strtoul(*argv, &endptr, 0);
  188. if (*endptr) {
  189. p_err("can't parse %s as ID", *argv);
  190. return -1;
  191. }
  192. NEXT_ARG();
  193. if (argc != 1)
  194. usage();
  195. fd = get_fd_by_id(id);
  196. if (fd < 0) {
  197. p_err("can't get prog by id (%u): %s", id, strerror(errno));
  198. return -1;
  199. }
  200. err = do_pin_fd(fd, *argv);
  201. close(fd);
  202. return err;
  203. }
  204. const char *get_fd_type_name(enum bpf_obj_type type)
  205. {
  206. static const char * const names[] = {
  207. [BPF_OBJ_UNKNOWN] = "unknown",
  208. [BPF_OBJ_PROG] = "prog",
  209. [BPF_OBJ_MAP] = "map",
  210. };
  211. if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
  212. return names[BPF_OBJ_UNKNOWN];
  213. return names[type];
  214. }
  215. int get_fd_type(int fd)
  216. {
  217. char path[PATH_MAX];
  218. char buf[512];
  219. ssize_t n;
  220. snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
  221. n = readlink(path, buf, sizeof(buf));
  222. if (n < 0) {
  223. p_err("can't read link type: %s", strerror(errno));
  224. return -1;
  225. }
  226. if (n == sizeof(path)) {
  227. p_err("can't read link type: path too long!");
  228. return -1;
  229. }
  230. if (strstr(buf, "bpf-map"))
  231. return BPF_OBJ_MAP;
  232. else if (strstr(buf, "bpf-prog"))
  233. return BPF_OBJ_PROG;
  234. return BPF_OBJ_UNKNOWN;
  235. }
  236. char *get_fdinfo(int fd, const char *key)
  237. {
  238. char path[PATH_MAX];
  239. char *line = NULL;
  240. size_t line_n = 0;
  241. ssize_t n;
  242. FILE *fdi;
  243. snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
  244. fdi = fopen(path, "r");
  245. if (!fdi) {
  246. p_err("can't open fdinfo: %s", strerror(errno));
  247. return NULL;
  248. }
  249. while ((n = getline(&line, &line_n, fdi))) {
  250. char *value;
  251. int len;
  252. if (!strstr(line, key))
  253. continue;
  254. fclose(fdi);
  255. value = strchr(line, '\t');
  256. if (!value || !value[1]) {
  257. p_err("malformed fdinfo!?");
  258. free(line);
  259. return NULL;
  260. }
  261. value++;
  262. len = strlen(value);
  263. memmove(line, value, len);
  264. line[len - 1] = '\0';
  265. return line;
  266. }
  267. p_err("key '%s' not found in fdinfo", key);
  268. free(line);
  269. fclose(fdi);
  270. return NULL;
  271. }
  272. void print_hex_data_json(uint8_t *data, size_t len)
  273. {
  274. unsigned int i;
  275. jsonw_start_array(json_wtr);
  276. for (i = 0; i < len; i++)
  277. jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
  278. jsonw_end_array(json_wtr);
  279. }
  280. int build_pinned_obj_table(struct pinned_obj_table *tab,
  281. enum bpf_obj_type type)
  282. {
  283. struct bpf_prog_info pinned_info = {};
  284. struct pinned_obj *obj_node = NULL;
  285. __u32 len = sizeof(pinned_info);
  286. struct mntent *mntent = NULL;
  287. enum bpf_obj_type objtype;
  288. FILE *mntfile = NULL;
  289. FTSENT *ftse = NULL;
  290. FTS *fts = NULL;
  291. int fd, err;
  292. mntfile = setmntent("/proc/mounts", "r");
  293. if (!mntfile)
  294. return -1;
  295. while ((mntent = getmntent(mntfile))) {
  296. char *path[] = { mntent->mnt_dir, NULL };
  297. if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
  298. continue;
  299. fts = fts_open(path, 0, NULL);
  300. if (!fts)
  301. continue;
  302. while ((ftse = fts_read(fts))) {
  303. if (!(ftse->fts_info & FTS_F))
  304. continue;
  305. fd = open_obj_pinned(ftse->fts_path);
  306. if (fd < 0)
  307. continue;
  308. objtype = get_fd_type(fd);
  309. if (objtype != type) {
  310. close(fd);
  311. continue;
  312. }
  313. memset(&pinned_info, 0, sizeof(pinned_info));
  314. err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
  315. if (err) {
  316. close(fd);
  317. continue;
  318. }
  319. obj_node = malloc(sizeof(*obj_node));
  320. if (!obj_node) {
  321. close(fd);
  322. fts_close(fts);
  323. fclose(mntfile);
  324. return -1;
  325. }
  326. memset(obj_node, 0, sizeof(*obj_node));
  327. obj_node->id = pinned_info.id;
  328. obj_node->path = strdup(ftse->fts_path);
  329. hash_add(tab->table, &obj_node->hash, obj_node->id);
  330. close(fd);
  331. }
  332. fts_close(fts);
  333. }
  334. fclose(mntfile);
  335. return 0;
  336. }
  337. void delete_pinned_obj_table(struct pinned_obj_table *tab)
  338. {
  339. struct pinned_obj *obj;
  340. struct hlist_node *tmp;
  341. unsigned int bkt;
  342. hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
  343. hash_del(&obj->hash);
  344. free(obj->path);
  345. free(obj);
  346. }
  347. }
  348. static char *
  349. ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
  350. {
  351. struct stat st;
  352. int err;
  353. err = stat("/proc/self/ns/net", &st);
  354. if (err) {
  355. p_err("Can't stat /proc/self: %s", strerror(errno));
  356. return NULL;
  357. }
  358. if (st.st_dev != ns_dev || st.st_ino != ns_ino)
  359. return NULL;
  360. return if_indextoname(ifindex, buf);
  361. }
  362. static int read_sysfs_hex_int(char *path)
  363. {
  364. char vendor_id_buf[8];
  365. int len;
  366. int fd;
  367. fd = open(path, O_RDONLY);
  368. if (fd < 0) {
  369. p_err("Can't open %s: %s", path, strerror(errno));
  370. return -1;
  371. }
  372. len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
  373. close(fd);
  374. if (len < 0) {
  375. p_err("Can't read %s: %s", path, strerror(errno));
  376. return -1;
  377. }
  378. if (len >= (int)sizeof(vendor_id_buf)) {
  379. p_err("Value in %s too long", path);
  380. return -1;
  381. }
  382. vendor_id_buf[len] = 0;
  383. return strtol(vendor_id_buf, NULL, 0);
  384. }
  385. static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
  386. {
  387. char full_path[64];
  388. snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
  389. devname, entry_name);
  390. return read_sysfs_hex_int(full_path);
  391. }
  392. const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino)
  393. {
  394. char devname[IF_NAMESIZE];
  395. int vendor_id;
  396. int device_id;
  397. if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
  398. p_err("Can't get net device name for ifindex %d: %s", ifindex,
  399. strerror(errno));
  400. return NULL;
  401. }
  402. vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
  403. if (vendor_id < 0) {
  404. p_err("Can't get device vendor id for %s", devname);
  405. return NULL;
  406. }
  407. switch (vendor_id) {
  408. case 0x19ee:
  409. device_id = read_sysfs_netdev_hex_int(devname, "device");
  410. if (device_id != 0x4000 &&
  411. device_id != 0x6000 &&
  412. device_id != 0x6003)
  413. p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
  414. return "NFP-6xxx";
  415. default:
  416. p_err("Can't get bfd arch name for device vendor id 0x%04x",
  417. vendor_id);
  418. return NULL;
  419. }
  420. }
  421. void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
  422. {
  423. char name[IF_NAMESIZE];
  424. if (!ifindex)
  425. return;
  426. printf(" dev ");
  427. if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
  428. printf("%s", name);
  429. else
  430. printf("ifindex %u ns_dev %llu ns_ino %llu",
  431. ifindex, ns_dev, ns_inode);
  432. }
  433. void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
  434. {
  435. char name[IF_NAMESIZE];
  436. if (!ifindex)
  437. return;
  438. jsonw_name(json_wtr, "dev");
  439. jsonw_start_object(json_wtr);
  440. jsonw_uint_field(json_wtr, "ifindex", ifindex);
  441. jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
  442. jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
  443. if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
  444. jsonw_string_field(json_wtr, "ifname", name);
  445. jsonw_end_object(json_wtr);
  446. }