| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* nametable.c TIPC nametable functionality. |
| 4 |
* |
| 5 |
* Authors: Richard Alpe <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
#include <linux/tipc_netlink.h> |
| 12 |
#include <linux/tipc.h> |
| 13 |
#include <linux/genetlink.h> |
| 14 |
|
| 15 |
#include "cmdl.h" |
| 16 |
#include "msg.h" |
| 17 |
#include "nametable.h" |
| 18 |
#include "misc.h" |
| 19 |
#include "utils.h" |
| 20 |
|
| 21 |
#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */ |
| 22 |
|
| 23 |
static int nametable_show_cb(const struct nlmsghdr *nlh, void *data) |
| 24 |
{ |
| 25 |
int *iteration = data; |
| 26 |
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); |
| 27 |
struct nlattr *info[TIPC_NLA_MAX + 1] = {}; |
| 28 |
struct nlattr *attrs[TIPC_NLA_NAME_TABLE_MAX + 1] = {}; |
| 29 |
struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1] = {}; |
| 30 |
const char *scope[] = { "", "zone", "cluster", "node" }; |
| 31 |
char str[33] = {0,}; |
| 32 |
|
| 33 |
mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info); |
| 34 |
if (!info[TIPC_NLA_NAME_TABLE]) |
| 35 |
return MNL_CB_ERROR; |
| 36 |
|
| 37 |
mnl_attr_parse_nested(info[TIPC_NLA_NAME_TABLE], parse_attrs, attrs); |
| 38 |
if (!attrs[TIPC_NLA_NAME_TABLE_PUBL]) |
| 39 |
return MNL_CB_ERROR; |
| 40 |
|
| 41 |
mnl_attr_parse_nested(attrs[TIPC_NLA_NAME_TABLE_PUBL], parse_attrs, publ); |
| 42 |
if (!publ[TIPC_NLA_NAME_TABLE_PUBL]) |
| 43 |
return MNL_CB_ERROR; |
| 44 |
|
| 45 |
if (!*iteration && !is_json_context()) |
| 46 |
printf("%-10s %-10s %-10s %-8s %-10s %-33s\n", |
| 47 |
"Type", "Lower", "Upper", "Scope", "Port", |
| 48 |
"Node"); |
| 49 |
(*iteration)++; |
| 50 |
|
| 51 |
hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str); |
| 52 |
|
| 53 |
open_json_object(NULL); |
| 54 |
print_uint(PRINT_ANY, "type", "%-10u", |
| 55 |
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE])); |
| 56 |
print_string(PRINT_FP, NULL, " ", ""); |
| 57 |
print_uint(PRINT_ANY, "lower", "%-10u", |
| 58 |
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER])); |
| 59 |
print_string(PRINT_FP, NULL, " ", ""); |
| 60 |
print_uint(PRINT_ANY, "upper", "%-10u", |
| 61 |
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER])); |
| 62 |
print_string(PRINT_FP, NULL, " ", ""); |
| 63 |
print_string(PRINT_ANY, "scope", "%-8s", |
| 64 |
scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]); |
| 65 |
print_string(PRINT_FP, NULL, " ", ""); |
| 66 |
print_uint(PRINT_ANY, "port", "%-10u", |
| 67 |
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF])); |
| 68 |
print_string(PRINT_FP, NULL, " ", ""); |
| 69 |
print_string(PRINT_ANY, "node", "%s", str); |
| 70 |
print_string(PRINT_FP, NULL, "\n", ""); |
| 71 |
close_json_object(); |
| 72 |
|
| 73 |
return MNL_CB_OK; |
| 74 |
} |
| 75 |
|
| 76 |
static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd, |
| 77 |
struct cmdl *cmdl, void *data) |
| 78 |
{ |
| 79 |
int iteration = 0; |
| 80 |
int rc = 0; |
| 81 |
|
| 82 |
if (help_flag) { |
| 83 |
fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]); |
| 84 |
return -EINVAL; |
| 85 |
} |
| 86 |
|
| 87 |
nlh = msg_init(TIPC_NL_NAME_TABLE_GET); |
| 88 |
if (!nlh) { |
| 89 |
fprintf(stderr, "error, message initialisation failed\n"); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
new_json_obj(json); |
| 94 |
rc = msg_dumpit(nlh, nametable_show_cb, &iteration); |
| 95 |
delete_json_obj(); |
| 96 |
|
| 97 |
return rc; |
| 98 |
} |
| 99 |
|
| 100 |
void 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 |
|
| 109 |
int 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 |
} |