iproute2/rdma/res-srq.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
   2/*
   3 * res-srq.c    RDMA tool
   4 * Authors:     Neta Ostrovsky <netao@nvidia.com>
   5 */
   6
   7#include "res.h"
   8#include <inttypes.h>
   9
  10#define MAX_QP_STR_LEN 256
  11
  12static const char *srq_types_to_str(uint8_t idx)
  13{
  14        static const char *const srq_types_str[] = { "BASIC",
  15                                                     "XRC",
  16                                                     "TM" };
  17
  18        if (idx < ARRAY_SIZE(srq_types_str))
  19                return srq_types_str[idx];
  20        return "UNKNOWN";
  21}
  22
  23static void print_type(uint32_t val)
  24{
  25        print_string(PRINT_ANY, "type", "type %s ", srq_types_to_str(val));
  26}
  27
  28static void print_qps(char *qp_str)
  29{
  30        char *qpn;
  31
  32        if (!strlen(qp_str))
  33                return;
  34
  35        open_json_array(PRINT_ANY, "lqpn");
  36        print_string(PRINT_FP, NULL, " ", NULL);
  37        qpn = strtok(qp_str, ",");
  38        while (qpn) {
  39                print_string(PRINT_ANY, NULL, "%s", qpn);
  40                qpn = strtok(NULL, ",");
  41                if (qpn)
  42                        print_string(PRINT_FP, NULL, ",", NULL);
  43        }
  44        print_string(PRINT_FP, NULL, " ", NULL);
  45        close_json_array(PRINT_JSON, NULL);
  46}
  47
  48static int filter_srq_range_qps(struct rd *rd, struct nlattr **qp_line,
  49                                uint32_t min_range, uint32_t max_range,
  50                                char **delimiter, char *qp_str)
  51{
  52        uint32_t qpn = 0, tmp_min_range = 0, tmp_max_range = 0;
  53        char tmp[16] = {};
  54
  55        for (qpn = min_range; qpn <= max_range; qpn++) {
  56                if (rd_is_filtered_attr(rd, "lqpn", qpn,
  57                                qp_line[RDMA_NLDEV_ATTR_MIN_RANGE])) {
  58                        /* The QPs range contains a LQPN that is filtered */
  59                        if (!tmp_min_range)
  60                                /* There are no QPs previous to
  61                                 * the filtered one
  62                                 */
  63                                continue;
  64                        if (!tmp_max_range)
  65                                snprintf(tmp, sizeof(tmp), "%s%d", *delimiter,
  66                                         tmp_min_range);
  67                        else
  68                                snprintf(tmp, sizeof(tmp), "%s%d-%d",
  69                                         *delimiter, tmp_min_range,
  70                                         tmp_max_range);
  71
  72                        strncat(qp_str, tmp,
  73                                MAX_QP_STR_LEN - strlen(qp_str) - 1);
  74
  75                        memset(tmp, 0, strlen(tmp));
  76                        *delimiter = ",";
  77                        tmp_min_range = 0;
  78                        tmp_max_range = 0;
  79                        continue;
  80                }
  81                if (!tmp_min_range)
  82                        tmp_min_range = qpn;
  83                else
  84                        tmp_max_range = qpn;
  85        }
  86
  87        if (!tmp_min_range)
  88                return 0;
  89        if (!tmp_max_range)
  90                snprintf(tmp, sizeof(tmp), "%s%d", *delimiter, tmp_min_range);
  91        else
  92                snprintf(tmp, sizeof(tmp), "%s%d-%d", *delimiter,
  93                         tmp_min_range, tmp_max_range);
  94
  95        strncat(qp_str, tmp, MAX_QP_STR_LEN - strlen(qp_str) - 1);
  96        *delimiter = ",";
  97        return 0;
  98}
  99
 100static int get_srq_qps(struct rd *rd, struct nlattr *qp_table,  char *qp_str)
 101{
 102        uint32_t qpn = 0, min_range = 0, max_range = 0;
 103        struct nlattr *nla_entry;
 104        struct filter_entry *fe;
 105        char *delimiter = "";
 106        char tmp[16] = {};
 107
 108        if (!qp_table)
 109                return MNL_CB_ERROR;
 110
 111        /* If there are no QPs associated with the SRQ, return */
 112        if (!(mnl_attr_get_payload_len(qp_table))) {
 113                list_for_each_entry(fe, &rd->filter_list, list) {
 114                        if (!strcmpx(fe->key, "lqpn"))
 115                                /* We found the key -
 116                                 * user requested to filter by LQPN
 117                                 */
 118                                return -EINVAL;
 119                }
 120                return MNL_CB_OK;
 121        }
 122
 123        mnl_attr_for_each_nested(nla_entry, qp_table) {
 124                struct nlattr *qp_line[RDMA_NLDEV_ATTR_MAX] = {};
 125
 126                if (mnl_attr_parse_nested(nla_entry, rd_attr_cb, qp_line) !=
 127                    MNL_CB_OK)
 128                        goto out;
 129
 130                if (qp_line[RDMA_NLDEV_ATTR_RES_LQPN]) {
 131                        qpn = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_RES_LQPN]);
 132                        if (rd_is_filtered_attr(rd, "lqpn", qpn,
 133                                        qp_line[RDMA_NLDEV_ATTR_RES_LQPN]))
 134                                continue;
 135                        snprintf(tmp, sizeof(tmp), "%s%d", delimiter, qpn);
 136                        strncat(qp_str, tmp,
 137                                MAX_QP_STR_LEN - strlen(qp_str) - 1);
 138                        delimiter = ",";
 139                } else if (qp_line[RDMA_NLDEV_ATTR_MIN_RANGE] &&
 140                           qp_line[RDMA_NLDEV_ATTR_MAX_RANGE]) {
 141                        min_range = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_MIN_RANGE]);
 142                        max_range = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_MAX_RANGE]);
 143
 144                        if (filter_srq_range_qps(rd, qp_line, min_range,
 145                                                 max_range, &delimiter,
 146                                                 qp_str))
 147                                goto out;
 148                } else {
 149                        goto out;
 150                }
 151        }
 152
 153        if (!strlen(qp_str))
 154                /* Check if there are no QPs to display after filter */
 155                goto out;
 156
 157        return MNL_CB_OK;
 158
 159out:
 160        memset(qp_str, 0, strlen(qp_str));
 161        return -EINVAL;
 162}
 163
 164static int res_srq_line_raw(struct rd *rd, const char *name, int idx,
 165                            struct nlattr **nla_line)
 166{
 167        if (!nla_line[RDMA_NLDEV_ATTR_RES_RAW])
 168                return MNL_CB_ERROR;
 169
 170        open_json_object(NULL);
 171        print_dev(idx, name);
 172        print_raw_data(rd, nla_line);
 173        close_json_object();
 174        newline();
 175
 176        return MNL_CB_OK;
 177}
 178
 179static int res_srq_line(struct rd *rd, const char *name, int idx,
 180                        struct nlattr **nla_line)
 181{
 182        uint32_t srqn = 0, pid = 0, pdn = 0, cqn = 0;
 183        char qp_str[MAX_QP_STR_LEN] = {};
 184        char *comm = NULL;
 185        uint8_t type = 0;
 186        SPRINT_BUF(b);
 187
 188        if (!nla_line[RDMA_NLDEV_ATTR_RES_SRQN])
 189                return MNL_CB_ERROR;
 190
 191        if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
 192                pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
 193                if (!get_task_name(pid, b, sizeof(b)))
 194                        comm = b;
 195        } else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
 196                /* discard const from mnl_attr_get_str */
 197                comm = (char *)mnl_attr_get_str(
 198                        nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
 199        }
 200
 201        if (rd_is_filtered_attr(rd, "pid", pid,
 202                                nla_line[RDMA_NLDEV_ATTR_RES_PID]))
 203                goto out;
 204
 205        if (nla_line[RDMA_NLDEV_ATTR_RES_SRQN])
 206                srqn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SRQN]);
 207        if (rd_is_filtered_attr(rd, "srqn", srqn,
 208                                nla_line[RDMA_NLDEV_ATTR_RES_SRQN]))
 209                goto out;
 210
 211        if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE])
 212                type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
 213        if (rd_is_string_filtered_attr(rd, "type", srq_types_to_str(type),
 214                                       nla_line[RDMA_NLDEV_ATTR_RES_TYPE]))
 215                goto out;
 216
 217        if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
 218                pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
 219        if (rd_is_filtered_attr(rd, "pdn", pdn,
 220                                nla_line[RDMA_NLDEV_ATTR_RES_PDN]))
 221                goto out;
 222
 223        if (nla_line[RDMA_NLDEV_ATTR_RES_CQN])
 224                cqn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CQN]);
 225        if (rd_is_filtered_attr(rd, "cqn", cqn,
 226                                nla_line[RDMA_NLDEV_ATTR_RES_CQN]))
 227                goto out;
 228
 229        if (get_srq_qps(rd, nla_line[RDMA_NLDEV_ATTR_RES_QP], qp_str) !=
 230                        MNL_CB_OK)
 231                goto out;
 232
 233        open_json_object(NULL);
 234        print_dev(idx, name);
 235        res_print_u32("srqn", srqn, nla_line[RDMA_NLDEV_ATTR_RES_SRQN]);
 236        print_type(type);
 237        print_qps(qp_str);
 238        res_print_u32("pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
 239        res_print_u32("cqn", cqn, nla_line[RDMA_NLDEV_ATTR_RES_CQN]);
 240        res_print_u32("pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
 241        print_comm(comm, nla_line);
 242
 243        print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
 244        close_json_object();
 245        newline();
 246
 247out:
 248        return MNL_CB_OK;
 249}
 250
 251int res_srq_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
 252{
 253        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
 254        struct rd *rd = data;
 255        const char *name;
 256        uint32_t idx;
 257
 258        mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
 259        if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
 260                return MNL_CB_ERROR;
 261
 262        name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
 263        idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
 264
 265        return (rd->show_raw) ? res_srq_line_raw(rd, name, idx, tb) :
 266                res_srq_line(rd, name, idx, tb);
 267}
 268
 269int res_srq_parse_cb(const struct nlmsghdr *nlh, void *data)
 270{
 271        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
 272        struct nlattr *nla_table, *nla_entry;
 273        struct rd *rd = data;
 274        int ret = MNL_CB_OK;
 275        const char *name;
 276        uint32_t idx;
 277
 278        mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
 279        if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
 280            !tb[RDMA_NLDEV_ATTR_RES_SRQ])
 281                return MNL_CB_ERROR;
 282
 283        name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
 284        idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
 285        nla_table = tb[RDMA_NLDEV_ATTR_RES_SRQ];
 286
 287        mnl_attr_for_each_nested(nla_entry, nla_table) {
 288                struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
 289
 290                ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
 291                if (ret != MNL_CB_OK)
 292                        break;
 293
 294                ret = (rd->show_raw) ? res_srq_line_raw(rd, name, idx, nla_line) :
 295                       res_srq_line(rd, name, idx, nla_line);
 296                if (ret != MNL_CB_OK)
 297                        break;
 298        }
 299        return ret;
 300}
 301