4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Leon Romanovsky <leonro@mellanox.com>
15 static int res_help(struct rd
*rd
)
17 pr_out("Usage: %s resource\n", rd
->filename
);
18 pr_out(" resource show [DEV]\n");
22 static int res_print_summary(struct rd
*rd
, struct nlattr
**tb
)
24 struct nlattr
*nla_table
= tb
[RDMA_NLDEV_ATTR_RES_SUMMARY
];
25 struct nlattr
*nla_entry
;
30 mnl_attr_for_each_nested(nla_entry
, nla_table
) {
31 struct nlattr
*nla_line
[RDMA_NLDEV_ATTR_MAX
] = {};
34 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
38 if (!nla_line
[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME
] ||
39 !nla_line
[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR
]) {
43 name
= mnl_attr_get_str(nla_line
[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME
]);
44 curr
= mnl_attr_get_u64(nla_line
[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR
]);
45 if (rd
->json_output
) {
46 snprintf(json_name
, 32, "%s", name
);
47 jsonw_lluint_field(rd
->jw
, json_name
, curr
);
49 pr_out("%s %"PRId64
" ", name
, curr
);
55 static int res_no_args_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
57 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
62 mnl_attr_parse(nlh
, 0, rd_attr_cb
, tb
);
63 if (!tb
[RDMA_NLDEV_ATTR_DEV_INDEX
] ||
64 !tb
[RDMA_NLDEV_ATTR_DEV_NAME
] ||
65 !tb
[RDMA_NLDEV_ATTR_RES_SUMMARY
])
68 idx
= mnl_attr_get_u32(tb
[RDMA_NLDEV_ATTR_DEV_INDEX
]);
69 name
= mnl_attr_get_str(tb
[RDMA_NLDEV_ATTR_DEV_NAME
]);
70 if (rd
->json_output
) {
71 jsonw_uint_field(rd
->jw
, "ifindex", idx
);
72 jsonw_string_field(rd
->jw
, "ifname", name
);
74 pr_out("%u: %s: ", idx
, name
);
77 res_print_summary(rd
, tb
);
84 static int _res_send_msg(struct rd
*rd
, uint32_t command
, mnl_cb_t callback
)
86 uint32_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
90 if (command
!= RDMA_NLDEV_CMD_RES_GET
)
93 rd_prepare_msg(rd
, command
, &seq
, flags
);
94 mnl_attr_put_u32(rd
->nlh
, RDMA_NLDEV_ATTR_DEV_INDEX
, rd
->dev_idx
);
96 mnl_attr_put_u32(rd
->nlh
,
97 RDMA_NLDEV_ATTR_PORT_INDEX
, rd
->port_idx
);
99 ret
= rd_send_msg(rd
);
104 jsonw_start_object(rd
->jw
);
105 ret
= rd_recv_msg(rd
, callback
, rd
, seq
);
107 jsonw_end_object(rd
->jw
);
111 #define RES_FUNC(name, command, valid_filters, strict_port) \
112 static int _##name(struct rd *rd)\
114 return _res_send_msg(rd, command, name##_parse_cb); \
116 static int name(struct rd *rd) \
118 int ret = rd_build_filter(rd, valid_filters); \
121 if ((uintptr_t)valid_filters != (uintptr_t)NULL) { \
122 ret = rd_set_arg_to_devname(rd); \
127 return rd_exec_dev(rd, _##name); \
129 return rd_exec_link(rd, _##name, strict_port); \
132 RES_FUNC(res_no_args
, RDMA_NLDEV_CMD_RES_GET
, NULL
, true);
134 static int res_show(struct rd
*rd
)
136 const struct rd_cmd cmds
[] = {
137 { NULL
, res_no_args
},
142 * Special case to support "rdma res show DEV_NAME"
144 if (rd_argc(rd
) == 1 && dev_map_lookup(rd
, false))
145 return rd_exec_dev(rd
, _res_no_args
);
147 return rd_exec_cmd(rd
, cmds
, "parameter");
150 int cmd_res(struct rd
*rd
)
152 const struct rd_cmd cmds
[] = {
154 { "show", res_show
},
155 { "list", res_show
},
156 { "help", res_help
},
160 return rd_exec_cmd(rd
, cmds
, "resource command");