map.c 19 KB

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