Commit 43f35be4 authored by Jiri Pirko's avatar Jiri Pirko Committed by Stephen Hemminger

devlink: introduce helper to print out nice names (ifnames)

By default, ifnames will be printed out. User can turn that off using
"-n" option on the command line.
Signed-off-by: default avatarJiri Pirko <jiri@mellanox.com>
parent 68cab0ba
......@@ -114,6 +114,7 @@ struct dl {
struct list_head ifname_map_list;
int argc;
char **argv;
bool no_nice_names;
};
static int dl_argc(struct dl *dl)
......@@ -290,6 +291,23 @@ static int ifname_map_lookup(struct dl *dl, const char *ifname,
return -ENOENT;
}
static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name,
const char *dev_name, uint32_t port_index,
char **p_ifname)
{
struct ifname_map *ifname_map;
list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
if (strcmp(bus_name, ifname_map->bus_name) == 0 &&
strcmp(dev_name, ifname_map->dev_name) == 0 &&
port_index == ifname_map->port_index) {
*p_ifname = ifname_map->ifname;
return 0;
}
}
return -ENOENT;
}
static unsigned int strslashcount(char *str)
{
unsigned int count = 0;
......@@ -517,16 +535,62 @@ static void cmd_dev_help(void)
pr_out("Usage: devlink dev show [ DEV ]\n");
}
static void __pr_out_handle(const char *bus_name, const char *dev_name)
{
pr_out("%s/%s", bus_name, dev_name);
}
static void pr_out_handle(struct nlattr **tb)
{
pr_out("%s/%s", mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]),
__pr_out_handle(mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]),
mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]));
}
static void __pr_out_port_handle(const char *bus_name, const char *dev_name,
uint32_t port_index)
{
__pr_out_handle(bus_name, dev_name);
pr_out("/%d", port_index);
}
static void pr_out_port_handle(struct nlattr **tb)
{
pr_out_handle(tb);
pr_out("/%d", mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]));
__pr_out_port_handle(mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]),
mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]),
mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]));
}
static void __pr_out_port_handle_nice(struct dl *dl, const char *bus_name,
const char *dev_name, uint32_t port_index)
{
char *ifname;
int err;
if (dl->no_nice_names)
goto no_nice_names;
err = ifname_map_rev_lookup(dl, bus_name, dev_name,
port_index, &ifname);
if (err)
goto no_nice_names;
pr_out("%s", ifname);
return;
no_nice_names:
__pr_out_port_handle(bus_name, dev_name, port_index);
}
static void pr_out_port_handle_nice(struct dl *dl, struct nlattr **tb)
{
const char *bus_name;
const char *dev_name;
uint32_t port_index;
bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
__pr_out_port_handle_nice(dl, bus_name, dev_name, port_index);
}
static void pr_out_dev(struct nlattr **tb)
......@@ -867,7 +931,7 @@ static void help(void)
{
pr_out("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
"where OBJECT := { dev | port | monitor }\n"
" OPTIONS := { -V[ersion] }\n");
" OPTIONS := { -V[ersion] | -n[no-nice-names] }\n");
}
static int dl_cmd(struct dl *dl)
......@@ -939,6 +1003,7 @@ int main(int argc, char **argv)
{
static const struct option long_options[] = {
{ "Version", no_argument, NULL, 'V' },
{ "no-nice-names", no_argument, NULL, 'n' },
{ NULL, 0, NULL, 0 }
};
struct dl *dl;
......@@ -946,13 +1011,22 @@ int main(int argc, char **argv)
int err;
int ret;
while ((opt = getopt_long(argc, argv, "V",
dl = dl_alloc();
if (!dl) {
pr_err("Failed to allocate memory for devlink\n");
return EXIT_FAILURE;
}
while ((opt = getopt_long(argc, argv, "Vn",
long_options, NULL)) >= 0) {
switch (opt) {
case 'V':
printf("devlink utility, iproute2-ss%s\n", SNAPSHOT);
return EXIT_SUCCESS;
case 'n':
dl->no_nice_names = true;
break;
default:
pr_err("Unknown option.\n");
help();
......@@ -963,12 +1037,6 @@ int main(int argc, char **argv)
argc -= optind;
argv += optind;
dl = dl_alloc();
if (!dl) {
pr_err("Failed to allocate memory for devlink\n");
return EXIT_FAILURE;
}
err = dl_init(dl, argc, argv);
if (err) {
ret = EXIT_FAILURE;
......
......@@ -16,6 +16,7 @@ devlink-dev \- devlink device configuration
.ti -8
.IR OPTIONS " := { "
\fB\-V\fR[\fIersion\fR] |
\fB\-n\fR[\fIno-nice-names\fR] }
.ti -8
.B devlink dev show
......
......@@ -16,6 +16,7 @@ devlink-port \- devlink port configuration
.ti -8
.IR OPTIONS " := { "
\fB\-V\fR[\fIersion\fR] |
\fB\-n\fR[\fIno-nice-names\fR] }
.ti -8
.BR "devlink port set "
......
......@@ -19,6 +19,7 @@ devlink \- Devlink tool
.ti -8
.IR OPTIONS " := { "
\fB\-V\fR[\fIersion\fR] |
\fB\-n\fR[\fIno-nice-names\fR] }
.SH OPTIONS
......@@ -28,6 +29,10 @@ Print the version of the
.B devlink
utility and exit.
.TP
.BR "\-n" , " -no-nice-names"
Turn off printing out nice names, for example netdevice ifnames instead of devlink port identification.
.SS
.I OBJECT
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment