iproute2/rdma/res-ctx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
   2/*
   3 * res-ctx.c    RDMA tool
   4 * Authors:     Neta Ostrovsky <netao@nvidia.com>
   5 */
   6
   7#include "res.h"
   8#include <inttypes.h>
   9
  10static int res_ctx_line(struct rd *rd, const char *name, int idx,
  11                        struct nlattr **nla_line)
  12{
  13        char *comm = NULL;
  14        uint32_t ctxn = 0;
  15        uint32_t pid = 0;
  16
  17        if (!nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
  18                return MNL_CB_ERROR;
  19
  20        if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
  21                pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
  22                comm = get_task_name(pid);
  23        }
  24
  25        if (rd_is_filtered_attr(rd, "pid", pid,
  26                                nla_line[RDMA_NLDEV_ATTR_RES_PID]))
  27                goto out;
  28
  29        if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
  30                ctxn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
  31
  32        if (rd_is_filtered_attr(rd, "ctxn", ctxn,
  33                                nla_line[RDMA_NLDEV_ATTR_RES_CTXN]))
  34                goto out;
  35
  36        if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
  37                /* discard const from mnl_attr_get_str */
  38                comm = (char *)mnl_attr_get_str(
  39                        nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
  40
  41        open_json_object(NULL);
  42        print_dev(rd, idx, name);
  43        res_print_uint(rd, "ctxn", ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
  44        res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
  45        print_comm(rd, comm, nla_line);
  46
  47        print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
  48        newline(rd);
  49
  50out:
  51        if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
  52                free(comm);
  53        return MNL_CB_OK;
  54}
  55
  56int res_ctx_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
  57{
  58        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
  59        struct rd *rd = data;
  60        const char *name;
  61        uint32_t idx;
  62
  63        mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
  64        if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
  65                return MNL_CB_ERROR;
  66
  67        name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
  68        idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  69
  70        return res_ctx_line(rd, name, idx, tb);
  71}
  72
  73int res_ctx_parse_cb(const struct nlmsghdr *nlh, void *data)
  74{
  75        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
  76        struct nlattr *nla_table, *nla_entry;
  77        struct rd *rd = data;
  78        int ret = MNL_CB_OK;
  79        const char *name;
  80        uint32_t idx;
  81
  82        mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
  83        if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
  84            !tb[RDMA_NLDEV_ATTR_RES_CTX])
  85                return MNL_CB_ERROR;
  86
  87        name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
  88        idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  89        nla_table = tb[RDMA_NLDEV_ATTR_RES_CTX];
  90
  91        mnl_attr_for_each_nested(nla_entry, nla_table) {
  92                struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
  93
  94                ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
  95                if (ret != MNL_CB_OK)
  96                        break;
  97
  98                ret = res_ctx_line(rd, name, idx, nla_line);
  99                if (ret != MNL_CB_OK)
 100                        break;
 101        }
 102        return ret;
 103}
 104