]> git.proxmox.com Git - mirror_iproute2.git/blame - tipc/nametable.c
tipc: JSON support for showing nametable
[mirror_iproute2.git] / tipc / nametable.c
CommitLineData
f043759d
RA
1/*
2 * nametable.c TIPC nametable functionality.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
10 */
11
12#include <stdio.h>
13#include <errno.h>
14
15#include <linux/tipc_netlink.h>
16#include <linux/tipc.h>
17#include <linux/genetlink.h>
18#include <libmnl/libmnl.h>
19
20#include "cmdl.h"
21#include "msg.h"
22#include "nametable.h"
5947046d 23#include "misc.h"
1304f50a 24#include "utils.h"
f043759d
RA
25
26#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
27
28static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
29{
30 int *iteration = data;
f043759d
RA
31 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
32 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
33 struct nlattr *attrs[TIPC_NLA_NAME_TABLE_MAX + 1] = {};
34 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1] = {};
35 const char *scope[] = { "", "zone", "cluster", "node" };
5947046d 36 char str[33] = {0,};
f043759d
RA
37
38 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
39 if (!info[TIPC_NLA_NAME_TABLE])
40 return MNL_CB_ERROR;
41
42 mnl_attr_parse_nested(info[TIPC_NLA_NAME_TABLE], parse_attrs, attrs);
43 if (!attrs[TIPC_NLA_NAME_TABLE_PUBL])
44 return MNL_CB_ERROR;
45
46 mnl_attr_parse_nested(attrs[TIPC_NLA_NAME_TABLE_PUBL], parse_attrs, publ);
47 if (!publ[TIPC_NLA_NAME_TABLE_PUBL])
48 return MNL_CB_ERROR;
49
1304f50a 50 if (!*iteration && !is_json_context())
5947046d
JM
51 printf("%-10s %-10s %-10s %-8s %-10s %-33s\n",
52 "Type", "Lower", "Upper", "Scope", "Port",
53 "Node");
f043759d
RA
54 (*iteration)++;
55
5947046d
JM
56 hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str);
57
1304f50a
HL
58 open_json_object(NULL);
59 print_uint(PRINT_ANY, "type", "%-10u",
60 mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
61 print_uint(PRINT_ANY, "lower", "%-10u",
62 mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]));
63 print_uint(PRINT_ANY, "upper", "%-10u",
64 mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
65 print_string(PRINT_ANY, "scope", "%-8s",
66 scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
67 print_uint(PRINT_ANY, "port", "%-10u",
68 mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]));
69 print_string(PRINT_ANY, "node", "%s", str);
70 print_string(PRINT_FP, NULL, "\n", "");
71 close_json_object();
f043759d
RA
72
73 return MNL_CB_OK;
74}
75
76static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
77 struct cmdl *cmdl, void *data)
78{
79 int iteration = 0;
80 char buf[MNL_SOCKET_BUFFER_SIZE];
1304f50a 81 int rc = 0;
f043759d
RA
82
83 if (help_flag) {
84 fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
85 return -EINVAL;
86 }
87
88 if (!(nlh = msg_init(buf, TIPC_NL_NAME_TABLE_GET))) {
89 fprintf(stderr, "error, message initialisation failed\n");
90 return -1;
91 }
92
1304f50a
HL
93 new_json_obj(json);
94 rc = msg_dumpit(nlh, nametable_show_cb, &iteration);
95 delete_json_obj();
96
97 return rc;
f043759d
RA
98}
99
100void cmd_nametable_help(struct cmdl *cmdl)
101{
102 fprintf(stderr,
103 "Usage: %s nametable COMMAND\n\n"
104 "COMMANDS\n"
105 " show - Show nametable\n",
106 cmdl->argv[0]);
107}
108
109int cmd_nametable(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
110 void *data)
111{
112 const struct cmd cmds[] = {
113 { "show", cmd_nametable_show, NULL },
114 { NULL }
115 };
116
117 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
118}