map.c 19 KB

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