| 1 |
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| 2 |
/* |
| 3 |
* res-qp.c RDMA tool |
| 4 |
* Authors: Leon Romanovsky <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "res.h" |
| 8 |
#include <inttypes.h> |
| 9 |
|
| 10 |
static const char *path_mig_to_str(uint8_t idx) |
| 11 |
{ |
| 12 |
static const char *const path_mig_str[] = { "MIGRATED", "REARM", |
| 13 |
"ARMED" }; |
| 14 |
|
| 15 |
if (idx < ARRAY_SIZE(path_mig_str)) |
| 16 |
return path_mig_str[idx]; |
| 17 |
return "UNKNOWN"; |
| 18 |
} |
| 19 |
|
| 20 |
static const char *qp_states_to_str(uint8_t idx) |
| 21 |
{ |
| 22 |
static const char *const qp_states_str[] = { "RESET", "INIT", "RTR", |
| 23 |
"RTS", "SQD", "SQE", |
| 24 |
"ERR" }; |
| 25 |
|
| 26 |
if (idx < ARRAY_SIZE(qp_states_str)) |
| 27 |
return qp_states_str[idx]; |
| 28 |
return "UNKNOWN"; |
| 29 |
} |
| 30 |
|
| 31 |
static void print_rqpn(uint32_t val, struct nlattr **nla_line) |
| 32 |
{ |
| 33 |
if (!nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) |
| 34 |
return; |
| 35 |
print_uint(PRINT_ANY, "rqpn", "rqpn %d ", val); |
| 36 |
} |
| 37 |
|
| 38 |
static void print_type(uint32_t val) |
| 39 |
{ |
| 40 |
print_string(PRINT_ANY, "type", "type %s ", qp_types_to_str(val)); |
| 41 |
} |
| 42 |
|
| 43 |
/* |
| 44 |
* print the subtype only if the QPT is IB_QPT_DRIVER |
| 45 |
*/ |
| 46 |
static void print_subtype(uint8_t type, const char *sub_type) |
| 47 |
{ |
| 48 |
if (type == 0xFF && sub_type) |
| 49 |
print_string(PRINT_ANY, "subtype", "subtype %s ", sub_type); |
| 50 |
} |
| 51 |
|
| 52 |
static void print_state(uint32_t val) |
| 53 |
{ |
| 54 |
print_string(PRINT_ANY, "state", "state %s ", qp_states_to_str(val)); |
| 55 |
} |
| 56 |
|
| 57 |
static void print_rqpsn(uint32_t val, struct nlattr **nla_line) |
| 58 |
{ |
| 59 |
if (!nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) |
| 60 |
return; |
| 61 |
|
| 62 |
print_uint(PRINT_ANY, "rq-psn", "rq-psn %d ", val); |
| 63 |
} |
| 64 |
|
| 65 |
static void print_pathmig(uint32_t val, struct nlattr **nla_line) |
| 66 |
{ |
| 67 |
if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) |
| 68 |
return; |
| 69 |
|
| 70 |
print_string(PRINT_ANY, "path-mig-state", "path-mig-state %s ", path_mig_to_str(val)); |
| 71 |
} |
| 72 |
|
| 73 |
static int res_qp_line_raw(struct rd *rd, const char *name, int idx, |
| 74 |
struct nlattr **nla_line) |
| 75 |
{ |
| 76 |
if (!nla_line[RDMA_NLDEV_ATTR_RES_RAW]) |
| 77 |
return MNL_CB_ERROR; |
| 78 |
|
| 79 |
open_json_object(NULL); |
| 80 |
print_link(idx, name, rd->port_idx, nla_line); |
| 81 |
print_raw_data(rd, nla_line); |
| 82 |
close_json_object(); |
| 83 |
newline(); |
| 84 |
|
| 85 |
return MNL_CB_OK; |
| 86 |
} |
| 87 |
|
| 88 |
static int res_qp_line(struct rd *rd, const char *name, int idx, |
| 89 |
struct nlattr **nla_line) |
| 90 |
{ |
| 91 |
uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn; |
| 92 |
uint8_t type, state, path_mig_state = 0; |
| 93 |
const char *sub_type = NULL; |
| 94 |
uint32_t port = 0, pid = 0; |
| 95 |
uint32_t pdn = 0; |
| 96 |
char *comm = NULL; |
| 97 |
SPRINT_BUF(b); |
| 98 |
|
| 99 |
if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] || |
| 100 |
!nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] || |
| 101 |
!nla_line[RDMA_NLDEV_ATTR_RES_TYPE] || |
| 102 |
!nla_line[RDMA_NLDEV_ATTR_RES_STATE]) |
| 103 |
return MNL_CB_ERROR; |
| 104 |
|
| 105 |
if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) |
| 106 |
port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]); |
| 107 |
|
| 108 |
if (port != rd->port_idx) |
| 109 |
goto out; |
| 110 |
|
| 111 |
lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]); |
| 112 |
if (rd_is_filtered_attr(rd, "lqpn", lqpn, |
| 113 |
nla_line[RDMA_NLDEV_ATTR_RES_LQPN])) |
| 114 |
goto out; |
| 115 |
|
| 116 |
if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
| 117 |
pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]); |
| 118 |
if (rd_is_filtered_attr(rd, "pdn", pdn, |
| 119 |
nla_line[RDMA_NLDEV_ATTR_RES_PDN])) |
| 120 |
goto out; |
| 121 |
|
| 122 |
if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) |
| 123 |
rqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQPN]); |
| 124 |
if (rd_is_filtered_attr(rd, "rqpn", rqpn, |
| 125 |
nla_line[RDMA_NLDEV_ATTR_RES_RQPN])) |
| 126 |
goto out; |
| 127 |
|
| 128 |
if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) |
| 129 |
rq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]); |
| 130 |
if (rd_is_filtered_attr(rd, "rq-psn", rq_psn, |
| 131 |
nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN])) |
| 132 |
goto out; |
| 133 |
|
| 134 |
sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]); |
| 135 |
if (rd_is_filtered_attr(rd, "sq-psn", sq_psn, |
| 136 |
nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN])) |
| 137 |
goto out; |
| 138 |
|
| 139 |
if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) |
| 140 |
path_mig_state = mnl_attr_get_u8( |
| 141 |
nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]); |
| 142 |
if (rd_is_string_filtered_attr( |
| 143 |
rd, "path-mig-state", path_mig_to_str(path_mig_state), |
| 144 |
nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE])) |
| 145 |
goto out; |
| 146 |
|
| 147 |
if (nla_line[RDMA_NLDEV_ATTR_RES_SUBTYPE]) |
| 148 |
sub_type = |
| 149 |
mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUBTYPE]); |
| 150 |
|
| 151 |
type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]); |
| 152 |
if (rd_is_string_filtered_attr(rd, "type", qp_types_to_str(type), |
| 153 |
nla_line[RDMA_NLDEV_ATTR_RES_TYPE])) |
| 154 |
goto out; |
| 155 |
|
| 156 |
state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]); |
| 157 |
if (rd_is_string_filtered_attr(rd, "state", qp_states_to_str(state), |
| 158 |
nla_line[RDMA_NLDEV_ATTR_RES_STATE])) |
| 159 |
goto out; |
| 160 |
|
| 161 |
if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { |
| 162 |
pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]); |
| 163 |
if (!get_task_name(pid, b, sizeof(b))) |
| 164 |
comm = b; |
| 165 |
} else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) { |
| 166 |
/* discard const from mnl_attr_get_str */ |
| 167 |
comm = (char *)mnl_attr_get_str( |
| 168 |
nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); |
| 169 |
} |
| 170 |
|
| 171 |
if (rd_is_filtered_attr(rd, "pid", pid, |
| 172 |
nla_line[RDMA_NLDEV_ATTR_RES_PID])) |
| 173 |
goto out; |
| 174 |
|
| 175 |
open_json_object(NULL); |
| 176 |
print_link(idx, name, port, nla_line); |
| 177 |
res_print_u32("lqpn", lqpn, nla_line[RDMA_NLDEV_ATTR_RES_LQPN]); |
| 178 |
print_rqpn(rqpn, nla_line); |
| 179 |
|
| 180 |
print_type(type); |
| 181 |
print_subtype(type, sub_type); |
| 182 |
print_state(state); |
| 183 |
|
| 184 |
print_rqpsn(rq_psn, nla_line); |
| 185 |
res_print_u32("sq-psn", sq_psn, nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]); |
| 186 |
|
| 187 |
print_pathmig(path_mig_state, nla_line); |
| 188 |
res_print_u32("pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]); |
| 189 |
res_print_u32("pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]); |
| 190 |
print_comm(comm, nla_line); |
| 191 |
|
| 192 |
print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
| 193 |
close_json_object(); |
| 194 |
newline(); |
| 195 |
out: |
| 196 |
return MNL_CB_OK; |
| 197 |
} |
| 198 |
|
| 199 |
int res_qp_idx_parse_cb(const struct nlmsghdr *nlh, void *data) |
| 200 |
{ |
| 201 |
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; |
| 202 |
struct rd *rd = data; |
| 203 |
const char *name; |
| 204 |
uint32_t idx; |
| 205 |
|
| 206 |
mnl_attr_parse(nlh, 0, rd_attr_cb, tb); |
| 207 |
if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME]) |
| 208 |
return MNL_CB_ERROR; |
| 209 |
|
| 210 |
name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); |
| 211 |
idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); |
| 212 |
|
| 213 |
return (rd->show_raw) ? res_qp_line_raw(rd, name, idx, tb) : |
| 214 |
res_qp_line(rd, name, idx, tb); |
| 215 |
} |
| 216 |
|
| 217 |
int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data) |
| 218 |
{ |
| 219 |
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; |
| 220 |
struct nlattr *nla_table, *nla_entry; |
| 221 |
struct rd *rd = data; |
| 222 |
int ret = MNL_CB_OK; |
| 223 |
const char *name; |
| 224 |
uint32_t idx; |
| 225 |
|
| 226 |
mnl_attr_parse(nlh, 0, rd_attr_cb, tb); |
| 227 |
if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || |
| 228 |
!tb[RDMA_NLDEV_ATTR_RES_QP]) |
| 229 |
return MNL_CB_ERROR; |
| 230 |
|
| 231 |
name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); |
| 232 |
idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); |
| 233 |
nla_table = tb[RDMA_NLDEV_ATTR_RES_QP]; |
| 234 |
|
| 235 |
mnl_attr_for_each_nested(nla_entry, nla_table) { |
| 236 |
struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; |
| 237 |
|
| 238 |
ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); |
| 239 |
if (ret != MNL_CB_OK) |
| 240 |
break; |
| 241 |
|
| 242 |
ret = (rd->show_raw) ? res_qp_line_raw(rd, name, idx, nla_line) : |
| 243 |
res_qp_line(rd, name, idx, nla_line); |
| 244 |
if (ret != MNL_CB_OK) |
| 245 |
break; |
| 246 |
} |
| 247 |
return ret; |
| 248 |
} |