]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - rdma/res.c
rdma/sys.c: fix possible out-of-bound array access
[mirror_iproute2.git] / rdma / res.c
index bd97c58e177f347d1e6ba4a40e77846091f311af..6003006e98aa43ca32a481240d541f84bbf9c809 100644 (file)
@@ -1,21 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
 /*
  * res.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>
  */
 
-#include "rdma.h"
+#include "res.h"
 #include <inttypes.h>
 
 static int res_help(struct rd *rd)
 {
        pr_out("Usage: %s resource\n", rd->filename);
        pr_out("          resource show [DEV]\n");
+       pr_out("          resource show [qp|cm_id|pd|mr|cq]\n");
+       pr_out("          resource show qp link [DEV/PORT]\n");
+       pr_out("          resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
+       pr_out("          resource show cm_id link [DEV/PORT]\n");
+       pr_out("          resource show cm_id link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
+       pr_out("          resource show cq link [DEV/PORT]\n");
+       pr_out("          resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
+       pr_out("          resource show pd dev [DEV]\n");
+       pr_out("          resource show pd dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
+       pr_out("          resource show mr dev [DEV]\n");
+       pr_out("          resource show mr dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
        return 0;
 }
 
@@ -29,7 +35,6 @@ static int res_print_summary(struct rd *rd, struct nlattr **tb)
 
        mnl_attr_for_each_nested(nla_entry, nla_table) {
                struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
-               char json_name[32];
 
                err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
                if (err != MNL_CB_OK)
@@ -42,16 +47,18 @@ static int res_print_summary(struct rd *rd, struct nlattr **tb)
 
                name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
                curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
-               if (rd->json_output) {
-                       snprintf(json_name, 32, "%s", name);
-                       jsonw_lluint_field(rd->jw, json_name, curr);
-               } else {
-                       pr_out("%s %"PRId64 " ", name, curr);
-               }
+               res_print_uint(
+                       rd, name, curr,
+                       nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
        }
        return 0;
 }
 
+static int res_no_args_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
+{
+       return MNL_CB_OK;
+}
+
 static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
 {
        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
@@ -81,7 +88,34 @@ static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
        return MNL_CB_OK;
 }
 
-static int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
+int _res_send_idx_msg(struct rd *rd, uint32_t command, mnl_cb_t callback,
+                     uint32_t idx, uint32_t id)
+{
+       uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
+       uint32_t seq;
+       int ret;
+
+       rd_prepare_msg(rd, command, &seq, flags);
+       mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
+       if (rd->port_idx)
+               mnl_attr_put_u32(rd->nlh,
+                                RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
+
+       mnl_attr_put_u32(rd->nlh, id, idx);
+
+       ret = rd_send_msg(rd);
+       if (ret)
+               return ret;
+
+       if (rd->json_output)
+               jsonw_start_object(rd->jw);
+       ret = rd_recv_msg(rd, callback, rd, seq);
+       if (rd->json_output)
+               jsonw_end_object(rd->jw);
+       return ret;
+}
+
+int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
 {
        uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
        uint32_t seq;
@@ -108,33 +142,126 @@ static int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
        return ret;
 }
 
-#define RES_FUNC(name, command, valid_filters, strict_port) \
-       static int _##name(struct rd *rd)\
-       { \
-               return _res_send_msg(rd, command, name##_parse_cb); \
-       } \
-       static int name(struct rd *rd) \
-       {\
-               int ret = rd_build_filter(rd, valid_filters); \
-               if (ret) \
-                       return ret; \
-               if ((uintptr_t)valid_filters != (uintptr_t)NULL) { \
-                       ret = rd_set_arg_to_devname(rd); \
-                       if (ret) \
-                               return ret;\
-               } \
-               if (strict_port) \
-                       return rd_exec_dev(rd, _##name); \
-               else \
-                       return rd_exec_link(rd, _##name, strict_port); \
+const char *qp_types_to_str(uint8_t idx)
+{
+       static const char * const qp_types_str[] = { "SMI", "GSI", "RC",
+                                                    "UC", "UD", "RAW_IPV6",
+                                                    "RAW_ETHERTYPE",
+                                                    "UNKNOWN", "RAW_PACKET",
+                                                    "XRC_INI", "XRC_TGT",
+                                                    [0xFF] = "DRIVER",
+       };
+
+       if (idx < ARRAY_SIZE(qp_types_str) && qp_types_str[idx])
+               return qp_types_str[idx];
+       return "UNKNOWN";
+}
+
+void print_comm(struct rd *rd, const char *str, struct nlattr **nla_line)
+{
+       char tmp[18];
+
+       if (!str)
+               return;
+
+       if (rd->json_output) {
+               /* Don't beatify output in JSON format */
+               jsonw_string_field(rd->jw, "comm", str);
+               return;
        }
 
-RES_FUNC(res_no_args,  RDMA_NLDEV_CMD_RES_GET, NULL, true);
+       if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
+               snprintf(tmp, sizeof(tmp), "%s", str);
+       else
+               snprintf(tmp, sizeof(tmp), "[%s]", str);
+
+       pr_out("comm %s ", tmp);
+}
+
+void print_dev(struct rd *rd, uint32_t idx, const char *name)
+{
+       if (rd->json_output) {
+               jsonw_uint_field(rd->jw, "ifindex", idx);
+               jsonw_string_field(rd->jw, "ifname", name);
+       } else {
+               pr_out("dev %s ", name);
+       }
+}
+
+void print_link(struct rd *rd, uint32_t idx, const char *name, uint32_t port,
+               struct nlattr **nla_line)
+{
+       if (rd->json_output) {
+               jsonw_uint_field(rd->jw, "ifindex", idx);
+
+               if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
+                       jsonw_uint_field(rd->jw, "port", port);
+
+               jsonw_string_field(rd->jw, "ifname", name);
+       } else {
+               if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
+                       pr_out("link %s/%u ", name, port);
+               else
+                       pr_out("link %s/- ", name);
+       }
+}
+
+char *get_task_name(uint32_t pid)
+{
+       char *comm;
+       FILE *f;
+
+       if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
+               return NULL;
+
+       f = fopen(comm, "r");
+       free(comm);
+       if (!f)
+               return NULL;
+
+       if (fscanf(f, "%ms\n", &comm) != 1)
+               comm = NULL;
+
+       fclose(f);
+
+       return comm;
+}
+
+void print_key(struct rd *rd, const char *name, uint64_t val,
+              struct nlattr *nlattr)
+{
+       if (!nlattr)
+               return;
+
+       if (rd->json_output)
+               jsonw_xint_field(rd->jw, name, val);
+       else
+               pr_out("%s 0x%" PRIx64 " ", name, val);
+}
+
+void res_print_uint(struct rd *rd, const char *name, uint64_t val,
+                   struct nlattr *nlattr)
+{
+       if (!nlattr)
+               return;
+
+       if (rd->json_output)
+               jsonw_u64_field(rd->jw, name, val);
+       else
+               pr_out("%s %" PRIu64 " ", name, val);
+}
+
+RES_FUNC(res_no_args,  RDMA_NLDEV_CMD_RES_GET, NULL, true, 0);
 
 static int res_show(struct rd *rd)
 {
        const struct rd_cmd cmds[] = {
                { NULL,         res_no_args     },
+               { "qp",         res_qp          },
+               { "cm_id",      res_cm_id       },
+               { "cq",         res_cq          },
+               { "mr",         res_mr          },
+               { "pd",         res_pd          },
                { 0 }
        };