map.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 <assert.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <linux/err.h>
  37. #include <linux/kernel.h>
  38. #include <stdbool.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <bpf.h>
  46. #include "btf.h"
  47. #include "json_writer.h"
  48. #include "main.h"
  49. static const char * const map_type_name[] = {
  50. [BPF_MAP_TYPE_UNSPEC] = "unspec",
  51. [BPF_MAP_TYPE_HASH] = "hash",
  52. [BPF_MAP_TYPE_ARRAY] = "array",
  53. [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
  54. [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
  55. [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
  56. [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
  57. [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
  58. [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
  59. [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
  60. [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
  61. [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
  62. [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
  63. [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
  64. [BPF_MAP_TYPE_DEVMAP] = "devmap",
  65. [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
  66. [BPF_MAP_TYPE_CPUMAP] = "cpumap",
  67. [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
  68. };
  69. static bool map_is_per_cpu(__u32 type)
  70. {
  71. return type == BPF_MAP_TYPE_PERCPU_HASH ||
  72. type == BPF_MAP_TYPE_PERCPU_ARRAY ||
  73. type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
  74. }
  75. static bool map_is_map_of_maps(__u32 type)
  76. {
  77. return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
  78. type == BPF_MAP_TYPE_HASH_OF_MAPS;
  79. }
  80. static bool map_is_map_of_progs(__u32 type)
  81. {
  82. return type == BPF_MAP_TYPE_PROG_ARRAY;
  83. }
  84. static void *alloc_value(struct bpf_map_info *info)
  85. {
  86. if (map_is_per_cpu(info->type))
  87. return malloc(round_up(info->value_size, 8) *
  88. get_possible_cpus());
  89. else
  90. return malloc(info->value_size);
  91. }
  92. int map_parse_fd(int *argc, char ***argv)
  93. {
  94. int fd;
  95. if (is_prefix(**argv, "id")) {
  96. unsigned int id;
  97. char *endptr;
  98. NEXT_ARGP();
  99. id = strtoul(**argv, &endptr, 0);
  100. if (*endptr) {
  101. p_err("can't parse %s as ID", **argv);
  102. return -1;
  103. }
  104. NEXT_ARGP();
  105. fd = bpf_map_get_fd_by_id(id);
  106. if (fd < 0)
  107. p_err("get map by id (%u): %s", id, strerror(errno));
  108. return fd;
  109. } else if (is_prefix(**argv, "pinned")) {
  110. char *path;
  111. NEXT_ARGP();
  112. path = **argv;
  113. NEXT_ARGP();
  114. return open_obj_pinned_any(path, BPF_OBJ_MAP);
  115. }
  116. p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
  117. return -1;
  118. }
  119. int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
  120. {
  121. int err;
  122. int fd;
  123. fd = map_parse_fd(argc, argv);
  124. if (fd < 0)
  125. return -1;
  126. err = bpf_obj_get_info_by_fd(fd, info, info_len);
  127. if (err) {
  128. p_err("can't get map info: %s", strerror(errno));
  129. close(fd);
  130. return err;
  131. }
  132. return fd;
  133. }
  134. static int do_dump_btf(const struct btf_dumper *d,
  135. struct bpf_map_info *map_info, void *key,
  136. void *value)
  137. {
  138. int ret;
  139. /* start of key-value pair */
  140. jsonw_start_object(d->jw);
  141. jsonw_name(d->jw, "key");
  142. ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
  143. if (ret)
  144. goto err_end_obj;
  145. jsonw_name(d->jw, "value");
  146. ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
  147. err_end_obj:
  148. /* end of key-value pair */
  149. jsonw_end_object(d->jw);
  150. return ret;
  151. }
  152. static int get_btf(struct bpf_map_info *map_info, struct btf **btf)
  153. {
  154. struct bpf_btf_info btf_info = { 0 };
  155. __u32 len = sizeof(btf_info);
  156. __u32 last_size;
  157. int btf_fd;
  158. void *ptr;
  159. int err;
  160. err = 0;
  161. *btf = NULL;
  162. btf_fd = bpf_btf_get_fd_by_id(map_info->btf_id);
  163. if (btf_fd < 0)
  164. return 0;
  165. /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
  166. * let's start with a sane default - 4KiB here - and resize it only if
  167. * bpf_obj_get_info_by_fd() needs a bigger buffer.
  168. */
  169. btf_info.btf_size = 4096;
  170. last_size = btf_info.btf_size;
  171. ptr = malloc(last_size);
  172. if (!ptr) {
  173. err = -ENOMEM;
  174. goto exit_free;
  175. }
  176. bzero(ptr, last_size);
  177. btf_info.btf = ptr_to_u64(ptr);
  178. err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
  179. if (!err && btf_info.btf_size > last_size) {
  180. void *temp_ptr;
  181. last_size = btf_info.btf_size;
  182. temp_ptr = realloc(ptr, last_size);
  183. if (!temp_ptr) {
  184. err = -ENOMEM;
  185. goto exit_free;
  186. }
  187. ptr = temp_ptr;
  188. bzero(ptr, last_size);
  189. btf_info.btf = ptr_to_u64(ptr);
  190. err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
  191. }
  192. if (err || btf_info.btf_size > last_size) {
  193. err = errno;
  194. goto exit_free;
  195. }
  196. *btf = btf__new((__u8 *)btf_info.btf, btf_info.btf_size, NULL);
  197. if (IS_ERR(*btf)) {
  198. err = PTR_ERR(btf);
  199. *btf = NULL;
  200. }
  201. exit_free:
  202. close(btf_fd);
  203. free(ptr);
  204. return err;
  205. }
  206. static json_writer_t *get_btf_writer(void)
  207. {
  208. json_writer_t *jw = jsonw_new(stdout);
  209. if (!jw)
  210. return NULL;
  211. jsonw_pretty(jw, true);
  212. return jw;
  213. }
  214. static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
  215. unsigned char *value, struct btf *btf)
  216. {
  217. jsonw_start_object(json_wtr);
  218. if (!map_is_per_cpu(info->type)) {
  219. jsonw_name(json_wtr, "key");
  220. print_hex_data_json(key, info->key_size);
  221. jsonw_name(json_wtr, "value");
  222. print_hex_data_json(value, info->value_size);
  223. if (btf) {
  224. struct btf_dumper d = {
  225. .btf = btf,
  226. .jw = json_wtr,
  227. .is_plain_text = false,
  228. };
  229. jsonw_name(json_wtr, "formatted");
  230. do_dump_btf(&d, info, key, value);
  231. }
  232. } else {
  233. unsigned int i, n, step;
  234. n = get_possible_cpus();
  235. step = round_up(info->value_size, 8);
  236. jsonw_name(json_wtr, "key");
  237. print_hex_data_json(key, info->key_size);
  238. jsonw_name(json_wtr, "values");
  239. jsonw_start_array(json_wtr);
  240. for (i = 0; i < n; i++) {
  241. jsonw_start_object(json_wtr);
  242. jsonw_int_field(json_wtr, "cpu", i);
  243. jsonw_name(json_wtr, "value");
  244. print_hex_data_json(value + i * step,
  245. info->value_size);
  246. jsonw_end_object(json_wtr);
  247. }
  248. jsonw_end_array(json_wtr);
  249. }
  250. jsonw_end_object(json_wtr);
  251. }
  252. static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
  253. unsigned char *value)
  254. {
  255. if (!map_is_per_cpu(info->type)) {
  256. bool single_line, break_names;
  257. break_names = info->key_size > 16 || info->value_size > 16;
  258. single_line = info->key_size + info->value_size <= 24 &&
  259. !break_names;
  260. printf("key:%c", break_names ? '\n' : ' ');
  261. fprint_hex(stdout, key, info->key_size, " ");
  262. printf(single_line ? " " : "\n");
  263. printf("value:%c", break_names ? '\n' : ' ');
  264. fprint_hex(stdout, value, info->value_size, " ");
  265. printf("\n");
  266. } else {
  267. unsigned int i, n, step;
  268. n = get_possible_cpus();
  269. step = round_up(info->value_size, 8);
  270. printf("key:\n");
  271. fprint_hex(stdout, key, info->key_size, " ");
  272. printf("\n");
  273. for (i = 0; i < n; i++) {
  274. printf("value (CPU %02d):%c",
  275. i, info->value_size > 16 ? '\n' : ' ');
  276. fprint_hex(stdout, value + i * step,
  277. info->value_size, " ");
  278. printf("\n");
  279. }
  280. }
  281. }
  282. static char **parse_bytes(char **argv, const char *name, unsigned char *val,
  283. unsigned int n)
  284. {
  285. unsigned int i = 0, base = 0;
  286. char *endptr;
  287. if (is_prefix(*argv, "hex")) {
  288. base = 16;
  289. argv++;
  290. }
  291. while (i < n && argv[i]) {
  292. val[i] = strtoul(argv[i], &endptr, base);
  293. if (*endptr) {
  294. p_err("error parsing byte: %s", argv[i]);
  295. return NULL;
  296. }
  297. i++;
  298. }
  299. if (i != n) {
  300. p_err("%s expected %d bytes got %d", name, n, i);
  301. return NULL;
  302. }
  303. return argv + i;
  304. }
  305. static int parse_elem(char **argv, struct bpf_map_info *info,
  306. void *key, void *value, __u32 key_size, __u32 value_size,
  307. __u32 *flags, __u32 **value_fd)
  308. {
  309. if (!*argv) {
  310. if (!key && !value)
  311. return 0;
  312. p_err("did not find %s", key ? "key" : "value");
  313. return -1;
  314. }
  315. if (is_prefix(*argv, "key")) {
  316. if (!key) {
  317. if (key_size)
  318. p_err("duplicate key");
  319. else
  320. p_err("unnecessary key");
  321. return -1;
  322. }
  323. argv = parse_bytes(argv + 1, "key", key, key_size);
  324. if (!argv)
  325. return -1;
  326. return parse_elem(argv, info, NULL, value, key_size, value_size,
  327. flags, value_fd);
  328. } else if (is_prefix(*argv, "value")) {
  329. int fd;
  330. if (!value) {
  331. if (value_size)
  332. p_err("duplicate value");
  333. else
  334. p_err("unnecessary value");
  335. return -1;
  336. }
  337. argv++;
  338. if (map_is_map_of_maps(info->type)) {
  339. int argc = 2;
  340. if (value_size != 4) {
  341. p_err("value smaller than 4B for map in map?");
  342. return -1;
  343. }
  344. if (!argv[0] || !argv[1]) {
  345. p_err("not enough value arguments for map in map");
  346. return -1;
  347. }
  348. fd = map_parse_fd(&argc, &argv);
  349. if (fd < 0)
  350. return -1;
  351. *value_fd = value;
  352. **value_fd = fd;
  353. } else if (map_is_map_of_progs(info->type)) {
  354. int argc = 2;
  355. if (value_size != 4) {
  356. p_err("value smaller than 4B for map of progs?");
  357. return -1;
  358. }
  359. if (!argv[0] || !argv[1]) {
  360. p_err("not enough value arguments for map of progs");
  361. return -1;
  362. }
  363. fd = prog_parse_fd(&argc, &argv);
  364. if (fd < 0)
  365. return -1;
  366. *value_fd = value;
  367. **value_fd = fd;
  368. } else {
  369. argv = parse_bytes(argv, "value", value, value_size);
  370. if (!argv)
  371. return -1;
  372. }
  373. return parse_elem(argv, info, key, NULL, key_size, value_size,
  374. flags, NULL);
  375. } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
  376. is_prefix(*argv, "exist")) {
  377. if (!flags) {
  378. p_err("flags specified multiple times: %s", *argv);
  379. return -1;
  380. }
  381. if (is_prefix(*argv, "any"))
  382. *flags = BPF_ANY;
  383. else if (is_prefix(*argv, "noexist"))
  384. *flags = BPF_NOEXIST;
  385. else if (is_prefix(*argv, "exist"))
  386. *flags = BPF_EXIST;
  387. return parse_elem(argv + 1, info, key, value, key_size,
  388. value_size, NULL, value_fd);
  389. }
  390. p_err("expected key or value, got: %s", *argv);
  391. return -1;
  392. }
  393. static int show_map_close_json(int fd, struct bpf_map_info *info)
  394. {
  395. char *memlock;
  396. memlock = get_fdinfo(fd, "memlock");
  397. close(fd);
  398. jsonw_start_object(json_wtr);
  399. jsonw_uint_field(json_wtr, "id", info->id);
  400. if (info->type < ARRAY_SIZE(map_type_name))
  401. jsonw_string_field(json_wtr, "type",
  402. map_type_name[info->type]);
  403. else
  404. jsonw_uint_field(json_wtr, "type", info->type);
  405. if (*info->name)
  406. jsonw_string_field(json_wtr, "name", info->name);
  407. jsonw_name(json_wtr, "flags");
  408. jsonw_printf(json_wtr, "%d", info->map_flags);
  409. print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
  410. jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
  411. jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
  412. jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
  413. if (memlock)
  414. jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
  415. free(memlock);
  416. if (!hash_empty(map_table.table)) {
  417. struct pinned_obj *obj;
  418. jsonw_name(json_wtr, "pinned");
  419. jsonw_start_array(json_wtr);
  420. hash_for_each_possible(map_table.table, obj, hash, info->id) {
  421. if (obj->id == info->id)
  422. jsonw_string(json_wtr, obj->path);
  423. }
  424. jsonw_end_array(json_wtr);
  425. }
  426. jsonw_end_object(json_wtr);
  427. return 0;
  428. }
  429. static int show_map_close_plain(int fd, struct bpf_map_info *info)
  430. {
  431. char *memlock;
  432. memlock = get_fdinfo(fd, "memlock");
  433. close(fd);
  434. printf("%u: ", info->id);
  435. if (info->type < ARRAY_SIZE(map_type_name))
  436. printf("%s ", map_type_name[info->type]);
  437. else
  438. printf("type %u ", info->type);
  439. if (*info->name)
  440. printf("name %s ", info->name);
  441. printf("flags 0x%x", info->map_flags);
  442. print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
  443. printf("\n");
  444. printf("\tkey %uB value %uB max_entries %u",
  445. info->key_size, info->value_size, info->max_entries);
  446. if (memlock)
  447. printf(" memlock %sB", memlock);
  448. free(memlock);
  449. printf("\n");
  450. if (!hash_empty(map_table.table)) {
  451. struct pinned_obj *obj;
  452. hash_for_each_possible(map_table.table, obj, hash, info->id) {
  453. if (obj->id == info->id)
  454. printf("\tpinned %s\n", obj->path);
  455. }
  456. }
  457. return 0;
  458. }
  459. static int do_show(int argc, char **argv)
  460. {
  461. struct bpf_map_info info = {};
  462. __u32 len = sizeof(info);
  463. __u32 id = 0;
  464. int err;
  465. int fd;
  466. if (show_pinned)
  467. build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
  468. if (argc == 2) {
  469. fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
  470. if (fd < 0)
  471. return -1;
  472. if (json_output)
  473. return show_map_close_json(fd, &info);
  474. else
  475. return show_map_close_plain(fd, &info);
  476. }
  477. if (argc)
  478. return BAD_ARG();
  479. if (json_output)
  480. jsonw_start_array(json_wtr);
  481. while (true) {
  482. err = bpf_map_get_next_id(id, &id);
  483. if (err) {
  484. if (errno == ENOENT)
  485. break;
  486. p_err("can't get next map: %s%s", strerror(errno),
  487. errno == EINVAL ? " -- kernel too old?" : "");
  488. break;
  489. }
  490. fd = bpf_map_get_fd_by_id(id);
  491. if (fd < 0) {
  492. if (errno == ENOENT)
  493. continue;
  494. p_err("can't get map by id (%u): %s",
  495. id, strerror(errno));
  496. break;
  497. }
  498. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  499. if (err) {
  500. p_err("can't get map info: %s", strerror(errno));
  501. close(fd);
  502. break;
  503. }
  504. if (json_output)
  505. show_map_close_json(fd, &info);
  506. else
  507. show_map_close_plain(fd, &info);
  508. }
  509. if (json_output)
  510. jsonw_end_array(json_wtr);
  511. return errno == ENOENT ? 0 : -1;
  512. }
  513. static int do_dump(int argc, char **argv)
  514. {
  515. struct bpf_map_info info = {};
  516. void *key, *value, *prev_key;
  517. unsigned int num_elems = 0;
  518. __u32 len = sizeof(info);
  519. json_writer_t *btf_wtr;
  520. struct btf *btf = NULL;
  521. int err;
  522. int fd;
  523. if (argc != 2)
  524. usage();
  525. fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
  526. if (fd < 0)
  527. return -1;
  528. if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
  529. p_err("Dumping maps of maps and program maps not supported");
  530. close(fd);
  531. return -1;
  532. }
  533. key = malloc(info.key_size);
  534. value = alloc_value(&info);
  535. if (!key || !value) {
  536. p_err("mem alloc failed");
  537. err = -1;
  538. goto exit_free;
  539. }
  540. prev_key = NULL;
  541. err = get_btf(&info, &btf);
  542. if (err) {
  543. p_err("failed to get btf");
  544. goto exit_free;
  545. }
  546. if (json_output)
  547. jsonw_start_array(json_wtr);
  548. else
  549. if (btf) {
  550. btf_wtr = get_btf_writer();
  551. if (!btf_wtr) {
  552. p_info("failed to create json writer for btf. falling back to plain output");
  553. btf__free(btf);
  554. btf = NULL;
  555. } else {
  556. jsonw_start_array(btf_wtr);
  557. }
  558. }
  559. while (true) {
  560. err = bpf_map_get_next_key(fd, prev_key, key);
  561. if (err) {
  562. if (errno == ENOENT)
  563. err = 0;
  564. break;
  565. }
  566. if (!bpf_map_lookup_elem(fd, key, value)) {
  567. if (json_output)
  568. print_entry_json(&info, key, value, btf);
  569. else
  570. if (btf) {
  571. struct btf_dumper d = {
  572. .btf = btf,
  573. .jw = btf_wtr,
  574. .is_plain_text = true,
  575. };
  576. do_dump_btf(&d, &info, key, value);
  577. } else {
  578. print_entry_plain(&info, key, value);
  579. }
  580. } else {
  581. if (json_output) {
  582. jsonw_name(json_wtr, "key");
  583. print_hex_data_json(key, info.key_size);
  584. jsonw_name(json_wtr, "value");
  585. jsonw_start_object(json_wtr);
  586. jsonw_string_field(json_wtr, "error",
  587. "can't lookup element");
  588. jsonw_end_object(json_wtr);
  589. } else {
  590. p_info("can't lookup element with key: ");
  591. fprint_hex(stderr, key, info.key_size, " ");
  592. fprintf(stderr, "\n");
  593. }
  594. }
  595. prev_key = key;
  596. num_elems++;
  597. }
  598. if (json_output)
  599. jsonw_end_array(json_wtr);
  600. else if (btf) {
  601. jsonw_end_array(btf_wtr);
  602. jsonw_destroy(&btf_wtr);
  603. } else {
  604. printf("Found %u element%s\n", num_elems,
  605. num_elems != 1 ? "s" : "");
  606. }
  607. exit_free:
  608. free(key);
  609. free(value);
  610. close(fd);
  611. btf__free(btf);
  612. return err;
  613. }
  614. static int do_update(int argc, char **argv)
  615. {
  616. struct bpf_map_info info = {};
  617. __u32 len = sizeof(info);
  618. __u32 *value_fd = NULL;
  619. __u32 flags = BPF_ANY;
  620. void *key, *value;
  621. int fd, err;
  622. if (argc < 2)
  623. usage();
  624. fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
  625. if (fd < 0)
  626. return -1;
  627. key = malloc(info.key_size);
  628. value = alloc_value(&info);
  629. if (!key || !value) {
  630. p_err("mem alloc failed");
  631. err = -1;
  632. goto exit_free;
  633. }
  634. err = parse_elem(argv, &info, key, value, info.key_size,
  635. info.value_size, &flags, &value_fd);
  636. if (err)
  637. goto exit_free;
  638. err = bpf_map_update_elem(fd, key, value, flags);
  639. if (err) {
  640. p_err("update failed: %s", strerror(errno));
  641. goto exit_free;
  642. }
  643. exit_free:
  644. if (value_fd)
  645. close(*value_fd);
  646. free(key);
  647. free(value);
  648. close(fd);
  649. if (!err && json_output)
  650. jsonw_null(json_wtr);
  651. return err;
  652. }
  653. static int do_lookup(int argc, char **argv)
  654. {
  655. struct bpf_map_info info = {};
  656. __u32 len = sizeof(info);
  657. json_writer_t *btf_wtr;
  658. struct btf *btf = NULL;
  659. void *key, *value;
  660. int err;
  661. int fd;
  662. if (argc < 2)
  663. usage();
  664. fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
  665. if (fd < 0)
  666. return -1;
  667. key = malloc(info.key_size);
  668. value = alloc_value(&info);
  669. if (!key || !value) {
  670. p_err("mem alloc failed");
  671. err = -1;
  672. goto exit_free;
  673. }
  674. err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
  675. if (err)
  676. goto exit_free;
  677. err = bpf_map_lookup_elem(fd, key, value);
  678. if (err) {
  679. if (errno == ENOENT) {
  680. if (json_output) {
  681. jsonw_null(json_wtr);
  682. } else {
  683. printf("key:\n");
  684. fprint_hex(stdout, key, info.key_size, " ");
  685. printf("\n\nNot found\n");
  686. }
  687. } else {
  688. p_err("lookup failed: %s", strerror(errno));
  689. }
  690. goto exit_free;
  691. }
  692. /* here means bpf_map_lookup_elem() succeeded */
  693. err = get_btf(&info, &btf);
  694. if (err) {
  695. p_err("failed to get btf");
  696. goto exit_free;
  697. }
  698. if (json_output) {
  699. print_entry_json(&info, key, value, btf);
  700. } else if (btf) {
  701. /* if here json_wtr wouldn't have been initialised,
  702. * so let's create separate writer for btf
  703. */
  704. btf_wtr = get_btf_writer();
  705. if (!btf_wtr) {
  706. p_info("failed to create json writer for btf. falling back to plain output");
  707. btf__free(btf);
  708. btf = NULL;
  709. print_entry_plain(&info, key, value);
  710. } else {
  711. struct btf_dumper d = {
  712. .btf = btf,
  713. .jw = btf_wtr,
  714. .is_plain_text = true,
  715. };
  716. do_dump_btf(&d, &info, key, value);
  717. jsonw_destroy(&btf_wtr);
  718. }
  719. } else {
  720. print_entry_plain(&info, key, value);
  721. }
  722. exit_free:
  723. free(key);
  724. free(value);
  725. close(fd);
  726. btf__free(btf);
  727. return err;
  728. }
  729. static int do_getnext(int argc, char **argv)
  730. {
  731. struct bpf_map_info info = {};
  732. __u32 len = sizeof(info);
  733. void *key, *nextkey;
  734. int err;
  735. int fd;
  736. if (argc < 2)
  737. usage();
  738. fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
  739. if (fd < 0)
  740. return -1;
  741. key = malloc(info.key_size);
  742. nextkey = malloc(info.key_size);
  743. if (!key || !nextkey) {
  744. p_err("mem alloc failed");
  745. err = -1;
  746. goto exit_free;
  747. }
  748. if (argc) {
  749. err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
  750. NULL, NULL);
  751. if (err)
  752. goto exit_free;
  753. } else {
  754. free(key);
  755. key = NULL;
  756. }
  757. err = bpf_map_get_next_key(fd, key, nextkey);
  758. if (err) {
  759. p_err("can't get next key: %s", strerror(errno));
  760. goto exit_free;
  761. }
  762. if (json_output) {
  763. jsonw_start_object(json_wtr);
  764. if (key) {
  765. jsonw_name(json_wtr, "key");
  766. print_hex_data_json(key, info.key_size);
  767. } else {
  768. jsonw_null_field(json_wtr, "key");
  769. }
  770. jsonw_name(json_wtr, "next_key");
  771. print_hex_data_json(nextkey, info.key_size);
  772. jsonw_end_object(json_wtr);
  773. } else {
  774. if (key) {
  775. printf("key:\n");
  776. fprint_hex(stdout, key, info.key_size, " ");
  777. printf("\n");
  778. } else {
  779. printf("key: None\n");
  780. }
  781. printf("next key:\n");
  782. fprint_hex(stdout, nextkey, info.key_size, " ");
  783. printf("\n");
  784. }
  785. exit_free:
  786. free(nextkey);
  787. free(key);
  788. close(fd);
  789. return err;
  790. }
  791. static int do_delete(int argc, char **argv)
  792. {
  793. struct bpf_map_info info = {};
  794. __u32 len = sizeof(info);
  795. void *key;
  796. int err;
  797. int fd;
  798. if (argc < 2)
  799. usage();
  800. fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
  801. if (fd < 0)
  802. return -1;
  803. key = malloc(info.key_size);
  804. if (!key) {
  805. p_err("mem alloc failed");
  806. err = -1;
  807. goto exit_free;
  808. }
  809. err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
  810. if (err)
  811. goto exit_free;
  812. err = bpf_map_delete_elem(fd, key);
  813. if (err)
  814. p_err("delete failed: %s", strerror(errno));
  815. exit_free:
  816. free(key);
  817. close(fd);
  818. if (!err && json_output)
  819. jsonw_null(json_wtr);
  820. return err;
  821. }
  822. static int do_pin(int argc, char **argv)
  823. {
  824. int err;
  825. err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
  826. if (!err && json_output)
  827. jsonw_null(json_wtr);
  828. return err;
  829. }
  830. static int do_help(int argc, char **argv)
  831. {
  832. if (json_output) {
  833. jsonw_null(json_wtr);
  834. return 0;
  835. }
  836. fprintf(stderr,
  837. "Usage: %s %s { show | list } [MAP]\n"
  838. " %s %s dump MAP\n"
  839. " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
  840. " %s %s lookup MAP key DATA\n"
  841. " %s %s getnext MAP [key DATA]\n"
  842. " %s %s delete MAP key DATA\n"
  843. " %s %s pin MAP FILE\n"
  844. " %s %s event_pipe MAP [cpu N index M]\n"
  845. " %s %s help\n"
  846. "\n"
  847. " " HELP_SPEC_MAP "\n"
  848. " DATA := { [hex] BYTES }\n"
  849. " " HELP_SPEC_PROGRAM "\n"
  850. " VALUE := { DATA | MAP | PROG }\n"
  851. " UPDATE_FLAGS := { any | exist | noexist }\n"
  852. " " HELP_SPEC_OPTIONS "\n"
  853. "",
  854. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  855. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  856. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
  857. return 0;
  858. }
  859. static const struct cmd cmds[] = {
  860. { "show", do_show },
  861. { "list", do_show },
  862. { "help", do_help },
  863. { "dump", do_dump },
  864. { "update", do_update },
  865. { "lookup", do_lookup },
  866. { "getnext", do_getnext },
  867. { "delete", do_delete },
  868. { "pin", do_pin },
  869. { "event_pipe", do_event_pipe },
  870. { 0 }
  871. };
  872. int do_map(int argc, char **argv)
  873. {
  874. return cmd_select(cmds, argc, argv, do_help);
  875. }