]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - rdma/link.c
rdma: add helper rd_sendrecv_msg()
[mirror_iproute2.git] / rdma / link.c
index b0e5bee027d33f49ec8ad29e649f3df37a11ab7f..89e81b84b2ccae8c0401c4ae2e972e57f7022c73 100644 (file)
@@ -1,11 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
 /*
  * link.c      RDMA tool
- *
- *              This program is free software; you can redistribute it and/or
- *              modify it under the terms of the GNU General Public License
- *              as published by the Free Software Foundation; either version
- *              2 of the License, or (at your option) any later version.
- *
  * Authors:     Leon Romanovsky <leonro@mellanox.com>
  */
 
@@ -19,7 +14,8 @@ static int link_help(struct rd *rd)
 
 static const char *caps_to_str(uint32_t idx)
 {
-#define RDMA_PORT_FLAGS(x) \
+#define RDMA_PORT_FLAGS_LOW(x) \
+       x(RESERVED, 0) \
        x(SM, 1) \
        x(NOTICE, 2) \
        x(TRAP, 3) \
@@ -30,9 +26,11 @@ static const char *caps_to_str(uint32_t idx)
        x(PKEY_NVRAM, 8) \
        x(LED_INFO, 9) \
        x(SM_DISABLED, 10) \
-       x(SYS_IMAGE_GUIG, 11) \
+       x(SYS_IMAGE_GUID, 11) \
        x(PKEY_SW_EXT_PORT_TRAP, 12) \
+       x(CABLE_INFO, 13) \
        x(EXTENDED_SPEEDS, 14) \
+       x(CAP_MASK2, 15) \
        x(CM, 16) \
        x(SNMP_TUNNEL, 17) \
        x(REINIT, 18) \
@@ -42,21 +40,50 @@ static const char *caps_to_str(uint32_t idx)
        x(CAP_MASK_NOTICE, 22) \
        x(BOOT_MGMT, 23) \
        x(LINK_LATENCY, 24) \
-       x(CLIENT_REG, 23) \
-       x(IP_BASED_GIDS, 26)
-
-       enum { RDMA_PORT_FLAGS(RDMA_BITMAP_ENUM) };
+       x(CLIENT_REG, 25) \
+       x(OTHER_LOCAL_CHANGES, 26) \
+       x(LINK_SPPED_WIDTH, 27) \
+       x(VENDOR_SPECIFIC_MADS, 28) \
+       x(MULT_PKER_TRAP, 29) \
+       x(MULT_FDB, 30) \
+       x(HIERARCHY_INFO, 31)
+
+#define RDMA_PORT_FLAGS_HIGH(x) \
+       x(SET_NODE_DESC, 0) \
+       x(EXT_INFO, 1) \
+       x(VIRT, 2) \
+       x(SWITCH_POR_STATE_TABLE, 3) \
+       x(LINK_WIDTH_2X, 4) \
+       x(LINK_SPEED_HDR, 5)
+
+       /*
+        * Separation below is needed to allow compilation of rdmatool
+        * on 32bits systems. On such systems, C-enum is limited to be
+        * int and can't hold more than 32 bits.
+        */
+       enum { RDMA_PORT_FLAGS_LOW(RDMA_BITMAP_ENUM) };
+       enum { RDMA_PORT_FLAGS_HIGH(RDMA_BITMAP_ENUM) };
 
        static const char * const
-               rdma_port_names[] = { RDMA_PORT_FLAGS(RDMA_BITMAP_NAMES) };
-       #undef RDMA_PORT_FLAGS
+               rdma_port_names_low[] = { RDMA_PORT_FLAGS_LOW(RDMA_BITMAP_NAMES) };
+       static const char * const
+               rdma_port_names_high[] = { RDMA_PORT_FLAGS_HIGH(RDMA_BITMAP_NAMES) };
+       uint32_t high_idx;
+       #undef RDMA_PORT_FLAGS_LOW
+       #undef RDMA_PORT_FLAGS_HIGH
+
+       if (idx < ARRAY_SIZE(rdma_port_names_low) && rdma_port_names_low[idx])
+               return rdma_port_names_low[idx];
+
+       high_idx = idx - ARRAY_SIZE(rdma_port_names_low);
+       if (high_idx < ARRAY_SIZE(rdma_port_names_high) &&
+           rdma_port_names_high[high_idx])
+               return rdma_port_names_high[high_idx];
 
-       if (idx < ARRAY_SIZE(rdma_port_names) && rdma_port_names[idx])
-               return rdma_port_names[idx];
        return "UNKNOWN";
 }
 
-static void link_print_caps(struct nlattr **tb)
+static void link_print_caps(struct rd *rd, struct nlattr **tb)
 {
        uint64_t caps;
        uint32_t idx;
@@ -66,54 +93,89 @@ static void link_print_caps(struct nlattr **tb)
 
        caps = mnl_attr_get_u64(tb[RDMA_NLDEV_ATTR_CAP_FLAGS]);
 
-       pr_out("\n    caps: <");
+       if (rd->json_output) {
+               jsonw_name(rd->jw, "caps");
+               jsonw_start_array(rd->jw);
+       } else {
+               pr_out("\n    caps: <");
+       }
        for (idx = 0; caps; idx++) {
                if (caps & 0x1) {
-                       pr_out("%s", caps_to_str(idx));
-                       if (caps >> 0x1)
-                               pr_out(", ");
+                       if (rd->json_output) {
+                               jsonw_string(rd->jw, caps_to_str(idx));
+                       } else {
+                               pr_out("%s", caps_to_str(idx));
+                               if (caps >> 0x1)
+                                       pr_out(", ");
+                       }
                }
                caps >>= 0x1;
        }
 
-       pr_out(">");
+       if (rd->json_output)
+               jsonw_end_array(rd->jw);
+       else
+               pr_out(">");
 }
 
-static void link_print_subnet_prefix(struct nlattr **tb)
+static void link_print_subnet_prefix(struct rd *rd, struct nlattr **tb)
 {
        uint64_t subnet_prefix;
+       uint16_t vp[4];
+       char str[32];
 
        if (!tb[RDMA_NLDEV_ATTR_SUBNET_PREFIX])
                return;
 
        subnet_prefix = mnl_attr_get_u64(tb[RDMA_NLDEV_ATTR_SUBNET_PREFIX]);
-       rd_print_u64("subnet_prefix", subnet_prefix);
+       memcpy(vp, &subnet_prefix, sizeof(uint64_t));
+       snprintf(str, 32, "%04x:%04x:%04x:%04x", vp[3], vp[2], vp[1], vp[0]);
+       if (rd->json_output)
+               jsonw_string_field(rd->jw, "subnet_prefix", str);
+       else
+               pr_out("subnet_prefix %s ", str);
 }
 
-static void link_print_lid(struct nlattr **tb)
+static void link_print_lid(struct rd *rd, struct nlattr **tb)
 {
+       uint32_t lid;
+
        if (!tb[RDMA_NLDEV_ATTR_LID])
                return;
 
-       pr_out("lid %u ",
-              mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_LID]));
+       lid = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_LID]);
+       if (rd->json_output)
+               jsonw_uint_field(rd->jw, "lid", lid);
+       else
+               pr_out("lid %u ", lid);
 }
 
-static void link_print_sm_lid(struct nlattr **tb)
+static void link_print_sm_lid(struct rd *rd, struct nlattr **tb)
 {
+       uint32_t sm_lid;
+
        if (!tb[RDMA_NLDEV_ATTR_SM_LID])
                return;
 
-       pr_out("sm_lid %u ",
-              mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_SM_LID]));
+       sm_lid = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_SM_LID]);
+       if (rd->json_output)
+               jsonw_uint_field(rd->jw, "sm_lid", sm_lid);
+       else
+               pr_out("sm_lid %u ", sm_lid);
 }
 
-static void link_print_lmc(struct nlattr **tb)
+static void link_print_lmc(struct rd *rd, struct nlattr **tb)
 {
+       uint8_t lmc;
+
        if (!tb[RDMA_NLDEV_ATTR_LMC])
                return;
 
-       pr_out("lmc %u ", mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_LMC]));
+       lmc = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_LMC]);
+       if (rd->json_output)
+               jsonw_uint_field(rd->jw, "lmc", lmc);
+       else
+               pr_out("lmc %u ", lmc);
 }
 
 static const char *link_state_to_str(uint8_t link_state)
@@ -127,7 +189,7 @@ static const char *link_state_to_str(uint8_t link_state)
        return "UNKNOWN";
 }
 
-static void link_print_state(struct nlattr **tb)
+static void link_print_state(struct rd *rd, struct nlattr **tb)
 {
        uint8_t state;
 
@@ -135,7 +197,10 @@ static void link_print_state(struct nlattr **tb)
                return;
 
        state = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_PORT_STATE]);
-       pr_out("state %s ", link_state_to_str(state));
+       if (rd->json_output)
+               jsonw_string_field(rd->jw, "state", link_state_to_str(state));
+       else
+               pr_out("state %s ", link_state_to_str(state));
 }
 
 static const char *phys_state_to_str(uint8_t phys_state)
@@ -152,7 +217,7 @@ static const char *phys_state_to_str(uint8_t phys_state)
        return "UNKNOWN";
 };
 
-static void link_print_phys_state(struct nlattr **tb)
+static void link_print_phys_state(struct rd *rd, struct nlattr **tb)
 {
        uint8_t phys_state;
 
@@ -160,13 +225,39 @@ static void link_print_phys_state(struct nlattr **tb)
                return;
 
        phys_state = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]);
-       pr_out("physical_state %s ", phys_state_to_str(phys_state));
+       if (rd->json_output)
+               jsonw_string_field(rd->jw, "physical_state",
+                                  phys_state_to_str(phys_state));
+       else
+               pr_out("physical_state %s ", phys_state_to_str(phys_state));
+}
+
+static void link_print_netdev(struct rd *rd, struct nlattr **tb)
+{
+       const char *netdev_name;
+       uint32_t idx;
+
+       if (!tb[RDMA_NLDEV_ATTR_NDEV_NAME] || !tb[RDMA_NLDEV_ATTR_NDEV_INDEX])
+               return;
+
+       netdev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_NDEV_NAME]);
+       idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_NDEV_INDEX]);
+       if (rd->json_output) {
+               jsonw_string_field(rd->jw, "netdev", netdev_name);
+               jsonw_uint_field(rd->jw, "netdev_index", idx);
+       } else {
+               pr_out("netdev %s ", netdev_name);
+               if (rd->show_details)
+                       pr_out("netdev_index %u ", idx);
+       }
 }
 
 static int link_parse_cb(const struct nlmsghdr *nlh, void *data)
 {
        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
        struct rd *rd = data;
+       uint32_t port, idx;
+       char name[32];
 
        mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
        if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
@@ -177,21 +268,32 @@ static int link_parse_cb(const struct nlmsghdr *nlh, void *data)
                return MNL_CB_ERROR;
        }
 
-       pr_out("%u/%u: %s/%u: ",
-              mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]),
-              mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]),
-              mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]),
-              mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]));
-       link_print_subnet_prefix(tb);
-       link_print_lid(tb);
-       link_print_sm_lid(tb);
-       link_print_lmc(tb);
-       link_print_state(tb);
-       link_print_phys_state(tb);
+       idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
+       port = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
+       snprintf(name, 32, "%s/%u",
+                mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]), port);
+
+       if (rd->json_output) {
+               jsonw_uint_field(rd->jw, "ifindex", idx);
+               jsonw_uint_field(rd->jw, "port", port);
+               jsonw_string_field(rd->jw, "ifname", name);
+
+       } else {
+               pr_out("%u/%u: %s: ", idx, port, name);
+       }
+
+       link_print_subnet_prefix(rd, tb);
+       link_print_lid(rd, tb);
+       link_print_sm_lid(rd, tb);
+       link_print_lmc(rd, tb);
+       link_print_state(rd, tb);
+       link_print_phys_state(rd, tb);
+       link_print_netdev(rd, tb);
        if (rd->show_details)
-               link_print_caps(tb);
+               link_print_caps(rd, tb);
 
-       pr_out("\n");
+       if (!rd->json_output)
+               pr_out("\n");
        return MNL_CB_OK;
 }
 
@@ -208,7 +310,12 @@ static int link_no_args(struct rd *rd)
        if (ret)
                return ret;
 
-       return rd_recv_msg(rd, link_parse_cb, rd, seq);
+       if (rd->json_output)
+               jsonw_start_object(rd->jw);
+       ret = rd_recv_msg(rd, link_parse_cb, rd, seq);
+       if (rd->json_output)
+               jsonw_end_object(rd->jw);
+       return ret;
 }
 
 static int link_one_show(struct rd *rd)
@@ -218,49 +325,15 @@ static int link_one_show(struct rd *rd)
                { 0 }
        };
 
+       if (!rd->port_idx)
+               return 0;
+
        return rd_exec_cmd(rd, cmds, "parameter");
 }
 
 static int link_show(struct rd *rd)
 {
-       struct dev_map *dev_map;
-       uint32_t port;
-       int ret;
-
-       if (rd_no_arg(rd)) {
-               list_for_each_entry(dev_map, &rd->dev_map_list, list) {
-                       rd->dev_idx = dev_map->idx;
-                       for (port = 1; port < dev_map->num_ports + 1; port++) {
-                               rd->port_idx = port;
-                               ret = link_one_show(rd);
-                               if (ret)
-                                       return ret;
-                       }
-               }
-
-       } else {
-               dev_map = dev_map_lookup(rd, true);
-               port = get_port_from_argv(rd);
-               if (!dev_map || port > dev_map->num_ports) {
-                       pr_err("Wrong device name\n");
-                       return -ENOENT;
-               }
-               rd_arg_inc(rd);
-               rd->dev_idx = dev_map->idx;
-               rd->port_idx = port ? : 1;
-               for (; rd->port_idx < dev_map->num_ports + 1; rd->port_idx++) {
-                       ret = link_one_show(rd);
-                       if (ret)
-                               return ret;
-                       if (port)
-                               /*
-                                * We got request to show link for devname
-                                * with port index.
-                                */
-                               break;
-               }
-       }
-       return 0;
+       return rd_exec_link(rd, link_one_show, true);
 }
 
 int cmd_link(struct rd *rd)