nldev.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the names of the copyright holders nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * Alternatively, this software may be distributed under the terms of the
  17. * GNU General Public License ("GPL") version 2 as published by the Free
  18. * Software Foundation.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/pid.h>
  34. #include <linux/pid_namespace.h>
  35. #include <net/netlink.h>
  36. #include <rdma/rdma_cm.h>
  37. #include <rdma/rdma_netlink.h>
  38. #include "core_priv.h"
  39. #include "cma_priv.h"
  40. static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
  41. [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 },
  42. [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING,
  43. .len = IB_DEVICE_NAME_MAX - 1},
  44. [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  45. [RDMA_NLDEV_ATTR_FW_VERSION] = { .type = NLA_NUL_STRING,
  46. .len = IB_FW_VERSION_NAME_MAX - 1},
  47. [RDMA_NLDEV_ATTR_NODE_GUID] = { .type = NLA_U64 },
  48. [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = { .type = NLA_U64 },
  49. [RDMA_NLDEV_ATTR_SUBNET_PREFIX] = { .type = NLA_U64 },
  50. [RDMA_NLDEV_ATTR_LID] = { .type = NLA_U32 },
  51. [RDMA_NLDEV_ATTR_SM_LID] = { .type = NLA_U32 },
  52. [RDMA_NLDEV_ATTR_LMC] = { .type = NLA_U8 },
  53. [RDMA_NLDEV_ATTR_PORT_STATE] = { .type = NLA_U8 },
  54. [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = { .type = NLA_U8 },
  55. [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = { .type = NLA_U8 },
  56. [RDMA_NLDEV_ATTR_RES_SUMMARY] = { .type = NLA_NESTED },
  57. [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = { .type = NLA_NESTED },
  58. [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = { .type = NLA_NUL_STRING,
  59. .len = 16 },
  60. [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = { .type = NLA_U64 },
  61. [RDMA_NLDEV_ATTR_RES_QP] = { .type = NLA_NESTED },
  62. [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = { .type = NLA_NESTED },
  63. [RDMA_NLDEV_ATTR_RES_LQPN] = { .type = NLA_U32 },
  64. [RDMA_NLDEV_ATTR_RES_RQPN] = { .type = NLA_U32 },
  65. [RDMA_NLDEV_ATTR_RES_RQ_PSN] = { .type = NLA_U32 },
  66. [RDMA_NLDEV_ATTR_RES_SQ_PSN] = { .type = NLA_U32 },
  67. [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = { .type = NLA_U8 },
  68. [RDMA_NLDEV_ATTR_RES_TYPE] = { .type = NLA_U8 },
  69. [RDMA_NLDEV_ATTR_RES_STATE] = { .type = NLA_U8 },
  70. [RDMA_NLDEV_ATTR_RES_PID] = { .type = NLA_U32 },
  71. [RDMA_NLDEV_ATTR_RES_KERN_NAME] = { .type = NLA_NUL_STRING,
  72. .len = TASK_COMM_LEN },
  73. [RDMA_NLDEV_ATTR_RES_CM_ID] = { .type = NLA_NESTED },
  74. [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = { .type = NLA_NESTED },
  75. [RDMA_NLDEV_ATTR_RES_PS] = { .type = NLA_U32 },
  76. [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = {
  77. .len = sizeof(struct __kernel_sockaddr_storage) },
  78. [RDMA_NLDEV_ATTR_RES_DST_ADDR] = {
  79. .len = sizeof(struct __kernel_sockaddr_storage) },
  80. [RDMA_NLDEV_ATTR_RES_CQ] = { .type = NLA_NESTED },
  81. [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = { .type = NLA_NESTED },
  82. [RDMA_NLDEV_ATTR_RES_CQE] = { .type = NLA_U32 },
  83. [RDMA_NLDEV_ATTR_RES_USECNT] = { .type = NLA_U64 },
  84. [RDMA_NLDEV_ATTR_RES_POLL_CTX] = { .type = NLA_U8 },
  85. [RDMA_NLDEV_ATTR_RES_MR] = { .type = NLA_NESTED },
  86. [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = { .type = NLA_NESTED },
  87. [RDMA_NLDEV_ATTR_RES_RKEY] = { .type = NLA_U32 },
  88. [RDMA_NLDEV_ATTR_RES_LKEY] = { .type = NLA_U32 },
  89. [RDMA_NLDEV_ATTR_RES_IOVA] = { .type = NLA_U64 },
  90. [RDMA_NLDEV_ATTR_RES_MRLEN] = { .type = NLA_U64 },
  91. [RDMA_NLDEV_ATTR_RES_PD] = { .type = NLA_NESTED },
  92. [RDMA_NLDEV_ATTR_RES_PD_ENTRY] = { .type = NLA_NESTED },
  93. [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY] = { .type = NLA_U32 },
  94. [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY] = { .type = NLA_U32 },
  95. [RDMA_NLDEV_ATTR_NDEV_INDEX] = { .type = NLA_U32 },
  96. [RDMA_NLDEV_ATTR_NDEV_NAME] = { .type = NLA_NUL_STRING,
  97. .len = IFNAMSIZ },
  98. };
  99. static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
  100. {
  101. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
  102. return -EMSGSIZE;
  103. if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
  104. return -EMSGSIZE;
  105. return 0;
  106. }
  107. static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
  108. {
  109. char fw[IB_FW_VERSION_NAME_MAX];
  110. if (fill_nldev_handle(msg, device))
  111. return -EMSGSIZE;
  112. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
  113. return -EMSGSIZE;
  114. BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
  115. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
  116. device->attrs.device_cap_flags, 0))
  117. return -EMSGSIZE;
  118. ib_get_device_fw_str(device, fw);
  119. /* Device without FW has strlen(fw) = 0 */
  120. if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
  121. return -EMSGSIZE;
  122. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
  123. be64_to_cpu(device->node_guid), 0))
  124. return -EMSGSIZE;
  125. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
  126. be64_to_cpu(device->attrs.sys_image_guid), 0))
  127. return -EMSGSIZE;
  128. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
  129. return -EMSGSIZE;
  130. return 0;
  131. }
  132. static int fill_port_info(struct sk_buff *msg,
  133. struct ib_device *device, u32 port,
  134. const struct net *net)
  135. {
  136. struct net_device *netdev = NULL;
  137. struct ib_port_attr attr;
  138. int ret;
  139. if (fill_nldev_handle(msg, device))
  140. return -EMSGSIZE;
  141. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
  142. return -EMSGSIZE;
  143. ret = ib_query_port(device, port, &attr);
  144. if (ret)
  145. return ret;
  146. BUILD_BUG_ON(sizeof(attr.port_cap_flags) > sizeof(u64));
  147. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
  148. (u64)attr.port_cap_flags, 0))
  149. return -EMSGSIZE;
  150. if (rdma_protocol_ib(device, port) &&
  151. nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
  152. attr.subnet_prefix, 0))
  153. return -EMSGSIZE;
  154. if (rdma_protocol_ib(device, port)) {
  155. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
  156. return -EMSGSIZE;
  157. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
  158. return -EMSGSIZE;
  159. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
  160. return -EMSGSIZE;
  161. }
  162. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
  163. return -EMSGSIZE;
  164. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
  165. return -EMSGSIZE;
  166. if (device->get_netdev)
  167. netdev = device->get_netdev(device, port);
  168. if (netdev && net_eq(dev_net(netdev), net)) {
  169. ret = nla_put_u32(msg,
  170. RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
  171. if (ret)
  172. goto out;
  173. ret = nla_put_string(msg,
  174. RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
  175. }
  176. out:
  177. if (netdev)
  178. dev_put(netdev);
  179. return ret;
  180. }
  181. static int fill_res_info_entry(struct sk_buff *msg,
  182. const char *name, u64 curr)
  183. {
  184. struct nlattr *entry_attr;
  185. entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
  186. if (!entry_attr)
  187. return -EMSGSIZE;
  188. if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
  189. goto err;
  190. if (nla_put_u64_64bit(msg,
  191. RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr, 0))
  192. goto err;
  193. nla_nest_end(msg, entry_attr);
  194. return 0;
  195. err:
  196. nla_nest_cancel(msg, entry_attr);
  197. return -EMSGSIZE;
  198. }
  199. static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
  200. {
  201. static const char * const names[RDMA_RESTRACK_MAX] = {
  202. [RDMA_RESTRACK_PD] = "pd",
  203. [RDMA_RESTRACK_CQ] = "cq",
  204. [RDMA_RESTRACK_QP] = "qp",
  205. [RDMA_RESTRACK_CM_ID] = "cm_id",
  206. [RDMA_RESTRACK_MR] = "mr",
  207. };
  208. struct rdma_restrack_root *res = &device->res;
  209. struct nlattr *table_attr;
  210. int ret, i, curr;
  211. if (fill_nldev_handle(msg, device))
  212. return -EMSGSIZE;
  213. table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
  214. if (!table_attr)
  215. return -EMSGSIZE;
  216. for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
  217. if (!names[i])
  218. continue;
  219. curr = rdma_restrack_count(res, i, task_active_pid_ns(current));
  220. ret = fill_res_info_entry(msg, names[i], curr);
  221. if (ret)
  222. goto err;
  223. }
  224. nla_nest_end(msg, table_attr);
  225. return 0;
  226. err:
  227. nla_nest_cancel(msg, table_attr);
  228. return ret;
  229. }
  230. static int fill_res_name_pid(struct sk_buff *msg,
  231. struct rdma_restrack_entry *res)
  232. {
  233. /*
  234. * For user resources, user is should read /proc/PID/comm to get the
  235. * name of the task file.
  236. */
  237. if (rdma_is_kernel_res(res)) {
  238. if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
  239. res->kern_name))
  240. return -EMSGSIZE;
  241. } else {
  242. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
  243. task_pid_vnr(res->task)))
  244. return -EMSGSIZE;
  245. }
  246. return 0;
  247. }
  248. static int fill_res_qp_entry(struct sk_buff *msg, struct netlink_callback *cb,
  249. struct rdma_restrack_entry *res, uint32_t port)
  250. {
  251. struct ib_qp *qp = container_of(res, struct ib_qp, res);
  252. struct ib_qp_init_attr qp_init_attr;
  253. struct nlattr *entry_attr;
  254. struct ib_qp_attr qp_attr;
  255. int ret;
  256. ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
  257. if (ret)
  258. return ret;
  259. if (port && port != qp_attr.port_num)
  260. return 0;
  261. entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_QP_ENTRY);
  262. if (!entry_attr)
  263. goto out;
  264. /* In create_qp() port is not set yet */
  265. if (qp_attr.port_num &&
  266. nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
  267. goto err;
  268. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
  269. goto err;
  270. if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
  271. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
  272. qp_attr.dest_qp_num))
  273. goto err;
  274. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
  275. qp_attr.rq_psn))
  276. goto err;
  277. }
  278. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
  279. goto err;
  280. if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
  281. qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
  282. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
  283. qp_attr.path_mig_state))
  284. goto err;
  285. }
  286. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
  287. goto err;
  288. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
  289. goto err;
  290. if (fill_res_name_pid(msg, res))
  291. goto err;
  292. nla_nest_end(msg, entry_attr);
  293. return 0;
  294. err:
  295. nla_nest_cancel(msg, entry_attr);
  296. out:
  297. return -EMSGSIZE;
  298. }
  299. static int fill_res_cm_id_entry(struct sk_buff *msg,
  300. struct netlink_callback *cb,
  301. struct rdma_restrack_entry *res, uint32_t port)
  302. {
  303. struct rdma_id_private *id_priv =
  304. container_of(res, struct rdma_id_private, res);
  305. struct rdma_cm_id *cm_id = &id_priv->id;
  306. struct nlattr *entry_attr;
  307. if (port && port != cm_id->port_num)
  308. return 0;
  309. entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY);
  310. if (!entry_attr)
  311. goto out;
  312. if (cm_id->port_num &&
  313. nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
  314. goto err;
  315. if (id_priv->qp_num) {
  316. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
  317. goto err;
  318. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
  319. goto err;
  320. }
  321. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
  322. goto err;
  323. if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
  324. goto err;
  325. if (cm_id->route.addr.src_addr.ss_family &&
  326. nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
  327. sizeof(cm_id->route.addr.src_addr),
  328. &cm_id->route.addr.src_addr))
  329. goto err;
  330. if (cm_id->route.addr.dst_addr.ss_family &&
  331. nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
  332. sizeof(cm_id->route.addr.dst_addr),
  333. &cm_id->route.addr.dst_addr))
  334. goto err;
  335. if (fill_res_name_pid(msg, res))
  336. goto err;
  337. nla_nest_end(msg, entry_attr);
  338. return 0;
  339. err:
  340. nla_nest_cancel(msg, entry_attr);
  341. out:
  342. return -EMSGSIZE;
  343. }
  344. static int fill_res_cq_entry(struct sk_buff *msg, struct netlink_callback *cb,
  345. struct rdma_restrack_entry *res, uint32_t port)
  346. {
  347. struct ib_cq *cq = container_of(res, struct ib_cq, res);
  348. struct nlattr *entry_attr;
  349. entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_CQ_ENTRY);
  350. if (!entry_attr)
  351. goto out;
  352. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
  353. goto err;
  354. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
  355. atomic_read(&cq->usecnt), 0))
  356. goto err;
  357. /* Poll context is only valid for kernel CQs */
  358. if (rdma_is_kernel_res(res) &&
  359. nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
  360. goto err;
  361. if (fill_res_name_pid(msg, res))
  362. goto err;
  363. nla_nest_end(msg, entry_attr);
  364. return 0;
  365. err:
  366. nla_nest_cancel(msg, entry_attr);
  367. out:
  368. return -EMSGSIZE;
  369. }
  370. static int fill_res_mr_entry(struct sk_buff *msg, struct netlink_callback *cb,
  371. struct rdma_restrack_entry *res, uint32_t port)
  372. {
  373. struct ib_mr *mr = container_of(res, struct ib_mr, res);
  374. struct nlattr *entry_attr;
  375. entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_MR_ENTRY);
  376. if (!entry_attr)
  377. goto out;
  378. if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
  379. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
  380. goto err;
  381. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
  382. goto err;
  383. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_IOVA,
  384. mr->iova, 0))
  385. goto err;
  386. }
  387. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length, 0))
  388. goto err;
  389. if (fill_res_name_pid(msg, res))
  390. goto err;
  391. nla_nest_end(msg, entry_attr);
  392. return 0;
  393. err:
  394. nla_nest_cancel(msg, entry_attr);
  395. out:
  396. return -EMSGSIZE;
  397. }
  398. static int fill_res_pd_entry(struct sk_buff *msg, struct netlink_callback *cb,
  399. struct rdma_restrack_entry *res, uint32_t port)
  400. {
  401. struct ib_pd *pd = container_of(res, struct ib_pd, res);
  402. struct nlattr *entry_attr;
  403. entry_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_RES_PD_ENTRY);
  404. if (!entry_attr)
  405. goto out;
  406. if (netlink_capable(cb->skb, CAP_NET_ADMIN)) {
  407. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
  408. pd->local_dma_lkey))
  409. goto err;
  410. if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
  411. nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
  412. pd->unsafe_global_rkey))
  413. goto err;
  414. }
  415. if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
  416. atomic_read(&pd->usecnt), 0))
  417. goto err;
  418. if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
  419. nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
  420. pd->unsafe_global_rkey))
  421. goto err;
  422. if (fill_res_name_pid(msg, res))
  423. goto err;
  424. nla_nest_end(msg, entry_attr);
  425. return 0;
  426. err:
  427. nla_nest_cancel(msg, entry_attr);
  428. out:
  429. return -EMSGSIZE;
  430. }
  431. static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
  432. struct netlink_ext_ack *extack)
  433. {
  434. struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
  435. struct ib_device *device;
  436. struct sk_buff *msg;
  437. u32 index;
  438. int err;
  439. err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
  440. nldev_policy, extack);
  441. if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
  442. return -EINVAL;
  443. index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  444. device = ib_device_get_by_index(index);
  445. if (!device)
  446. return -EINVAL;
  447. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  448. if (!msg) {
  449. err = -ENOMEM;
  450. goto err;
  451. }
  452. nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
  453. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
  454. 0, 0);
  455. err = fill_dev_info(msg, device);
  456. if (err)
  457. goto err_free;
  458. nlmsg_end(msg, nlh);
  459. put_device(&device->dev);
  460. return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
  461. err_free:
  462. nlmsg_free(msg);
  463. err:
  464. put_device(&device->dev);
  465. return err;
  466. }
  467. static int _nldev_get_dumpit(struct ib_device *device,
  468. struct sk_buff *skb,
  469. struct netlink_callback *cb,
  470. unsigned int idx)
  471. {
  472. int start = cb->args[0];
  473. struct nlmsghdr *nlh;
  474. if (idx < start)
  475. return 0;
  476. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  477. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
  478. 0, NLM_F_MULTI);
  479. if (fill_dev_info(skb, device)) {
  480. nlmsg_cancel(skb, nlh);
  481. goto out;
  482. }
  483. nlmsg_end(skb, nlh);
  484. idx++;
  485. out: cb->args[0] = idx;
  486. return skb->len;
  487. }
  488. static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  489. {
  490. /*
  491. * There is no need to take lock, because
  492. * we are relying on ib_core's lists_rwsem
  493. */
  494. return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
  495. }
  496. static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
  497. struct netlink_ext_ack *extack)
  498. {
  499. struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
  500. struct ib_device *device;
  501. struct sk_buff *msg;
  502. u32 index;
  503. u32 port;
  504. int err;
  505. err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
  506. nldev_policy, extack);
  507. if (err ||
  508. !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
  509. !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
  510. return -EINVAL;
  511. index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  512. device = ib_device_get_by_index(index);
  513. if (!device)
  514. return -EINVAL;
  515. port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
  516. if (!rdma_is_port_valid(device, port)) {
  517. err = -EINVAL;
  518. goto err;
  519. }
  520. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  521. if (!msg) {
  522. err = -ENOMEM;
  523. goto err;
  524. }
  525. nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
  526. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
  527. 0, 0);
  528. err = fill_port_info(msg, device, port, sock_net(skb->sk));
  529. if (err)
  530. goto err_free;
  531. nlmsg_end(msg, nlh);
  532. put_device(&device->dev);
  533. return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
  534. err_free:
  535. nlmsg_free(msg);
  536. err:
  537. put_device(&device->dev);
  538. return err;
  539. }
  540. static int nldev_port_get_dumpit(struct sk_buff *skb,
  541. struct netlink_callback *cb)
  542. {
  543. struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
  544. struct ib_device *device;
  545. int start = cb->args[0];
  546. struct nlmsghdr *nlh;
  547. u32 idx = 0;
  548. u32 ifindex;
  549. int err;
  550. u32 p;
  551. err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
  552. nldev_policy, NULL);
  553. if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
  554. return -EINVAL;
  555. ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  556. device = ib_device_get_by_index(ifindex);
  557. if (!device)
  558. return -EINVAL;
  559. for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
  560. /*
  561. * The dumpit function returns all information from specific
  562. * index. This specific index is taken from the netlink
  563. * messages request sent by user and it is available
  564. * in cb->args[0].
  565. *
  566. * Usually, the user doesn't fill this field and it causes
  567. * to return everything.
  568. *
  569. */
  570. if (idx < start) {
  571. idx++;
  572. continue;
  573. }
  574. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  575. cb->nlh->nlmsg_seq,
  576. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
  577. RDMA_NLDEV_CMD_PORT_GET),
  578. 0, NLM_F_MULTI);
  579. if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
  580. nlmsg_cancel(skb, nlh);
  581. goto out;
  582. }
  583. idx++;
  584. nlmsg_end(skb, nlh);
  585. }
  586. out:
  587. put_device(&device->dev);
  588. cb->args[0] = idx;
  589. return skb->len;
  590. }
  591. static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
  592. struct netlink_ext_ack *extack)
  593. {
  594. struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
  595. struct ib_device *device;
  596. struct sk_buff *msg;
  597. u32 index;
  598. int ret;
  599. ret = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
  600. nldev_policy, extack);
  601. if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
  602. return -EINVAL;
  603. index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  604. device = ib_device_get_by_index(index);
  605. if (!device)
  606. return -EINVAL;
  607. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  608. if (!msg) {
  609. ret = -ENOMEM;
  610. goto err;
  611. }
  612. nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
  613. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
  614. 0, 0);
  615. ret = fill_res_info(msg, device);
  616. if (ret)
  617. goto err_free;
  618. nlmsg_end(msg, nlh);
  619. put_device(&device->dev);
  620. return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
  621. err_free:
  622. nlmsg_free(msg);
  623. err:
  624. put_device(&device->dev);
  625. return ret;
  626. }
  627. static int _nldev_res_get_dumpit(struct ib_device *device,
  628. struct sk_buff *skb,
  629. struct netlink_callback *cb,
  630. unsigned int idx)
  631. {
  632. int start = cb->args[0];
  633. struct nlmsghdr *nlh;
  634. if (idx < start)
  635. return 0;
  636. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  637. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
  638. 0, NLM_F_MULTI);
  639. if (fill_res_info(skb, device)) {
  640. nlmsg_cancel(skb, nlh);
  641. goto out;
  642. }
  643. nlmsg_end(skb, nlh);
  644. idx++;
  645. out:
  646. cb->args[0] = idx;
  647. return skb->len;
  648. }
  649. static int nldev_res_get_dumpit(struct sk_buff *skb,
  650. struct netlink_callback *cb)
  651. {
  652. return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
  653. }
  654. struct nldev_fill_res_entry {
  655. int (*fill_res_func)(struct sk_buff *msg, struct netlink_callback *cb,
  656. struct rdma_restrack_entry *res, u32 port);
  657. enum rdma_nldev_attr nldev_attr;
  658. enum rdma_nldev_command nldev_cmd;
  659. };
  660. static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
  661. [RDMA_RESTRACK_QP] = {
  662. .fill_res_func = fill_res_qp_entry,
  663. .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
  664. .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
  665. },
  666. [RDMA_RESTRACK_CM_ID] = {
  667. .fill_res_func = fill_res_cm_id_entry,
  668. .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
  669. .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
  670. },
  671. [RDMA_RESTRACK_CQ] = {
  672. .fill_res_func = fill_res_cq_entry,
  673. .nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
  674. .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
  675. },
  676. [RDMA_RESTRACK_MR] = {
  677. .fill_res_func = fill_res_mr_entry,
  678. .nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
  679. .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
  680. },
  681. [RDMA_RESTRACK_PD] = {
  682. .fill_res_func = fill_res_pd_entry,
  683. .nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
  684. .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
  685. },
  686. };
  687. static int res_get_common_dumpit(struct sk_buff *skb,
  688. struct netlink_callback *cb,
  689. enum rdma_restrack_type res_type)
  690. {
  691. const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
  692. struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
  693. struct rdma_restrack_entry *res;
  694. int err, ret = 0, idx = 0;
  695. struct nlattr *table_attr;
  696. struct ib_device *device;
  697. int start = cb->args[0];
  698. struct nlmsghdr *nlh;
  699. u32 index, port = 0;
  700. bool filled = false;
  701. err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
  702. nldev_policy, NULL);
  703. /*
  704. * Right now, we are expecting the device index to get res information,
  705. * but it is possible to extend this code to return all devices in
  706. * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
  707. * if it doesn't exist, we will iterate over all devices.
  708. *
  709. * But it is not needed for now.
  710. */
  711. if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
  712. return -EINVAL;
  713. index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
  714. device = ib_device_get_by_index(index);
  715. if (!device)
  716. return -EINVAL;
  717. /*
  718. * If no PORT_INDEX is supplied, we will return all QPs from that device
  719. */
  720. if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
  721. port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
  722. if (!rdma_is_port_valid(device, port)) {
  723. ret = -EINVAL;
  724. goto err_index;
  725. }
  726. }
  727. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  728. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
  729. 0, NLM_F_MULTI);
  730. if (fill_nldev_handle(skb, device)) {
  731. ret = -EMSGSIZE;
  732. goto err;
  733. }
  734. table_attr = nla_nest_start(skb, fe->nldev_attr);
  735. if (!table_attr) {
  736. ret = -EMSGSIZE;
  737. goto err;
  738. }
  739. down_read(&device->res.rwsem);
  740. hash_for_each_possible(device->res.hash, res, node, res_type) {
  741. if (idx < start)
  742. goto next;
  743. if ((rdma_is_kernel_res(res) &&
  744. task_active_pid_ns(current) != &init_pid_ns) ||
  745. (!rdma_is_kernel_res(res) && task_active_pid_ns(current) !=
  746. task_active_pid_ns(res->task)))
  747. /*
  748. * 1. Kern resources should be visible in init
  749. * namspace only
  750. * 2. Present only resources visible in the current
  751. * namespace
  752. */
  753. goto next;
  754. if (!rdma_restrack_get(res))
  755. /*
  756. * Resource is under release now, but we are not
  757. * relesing lock now, so it will be released in
  758. * our next pass, once we will get ->next pointer.
  759. */
  760. goto next;
  761. filled = true;
  762. up_read(&device->res.rwsem);
  763. ret = fe->fill_res_func(skb, cb, res, port);
  764. down_read(&device->res.rwsem);
  765. /*
  766. * Return resource back, but it won't be released till
  767. * the &device->res.rwsem will be released for write.
  768. */
  769. rdma_restrack_put(res);
  770. if (ret == -EMSGSIZE)
  771. /*
  772. * There is a chance to optimize here.
  773. * It can be done by using list_prepare_entry
  774. * and list_for_each_entry_continue afterwards.
  775. */
  776. break;
  777. if (ret)
  778. goto res_err;
  779. next: idx++;
  780. }
  781. up_read(&device->res.rwsem);
  782. nla_nest_end(skb, table_attr);
  783. nlmsg_end(skb, nlh);
  784. cb->args[0] = idx;
  785. /*
  786. * No more entries to fill, cancel the message and
  787. * return 0 to mark end of dumpit.
  788. */
  789. if (!filled)
  790. goto err;
  791. put_device(&device->dev);
  792. return skb->len;
  793. res_err:
  794. nla_nest_cancel(skb, table_attr);
  795. up_read(&device->res.rwsem);
  796. err:
  797. nlmsg_cancel(skb, nlh);
  798. err_index:
  799. put_device(&device->dev);
  800. return ret;
  801. }
  802. static int nldev_res_get_qp_dumpit(struct sk_buff *skb,
  803. struct netlink_callback *cb)
  804. {
  805. return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_QP);
  806. }
  807. static int nldev_res_get_cm_id_dumpit(struct sk_buff *skb,
  808. struct netlink_callback *cb)
  809. {
  810. return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CM_ID);
  811. }
  812. static int nldev_res_get_cq_dumpit(struct sk_buff *skb,
  813. struct netlink_callback *cb)
  814. {
  815. return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_CQ);
  816. }
  817. static int nldev_res_get_mr_dumpit(struct sk_buff *skb,
  818. struct netlink_callback *cb)
  819. {
  820. return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_MR);
  821. }
  822. static int nldev_res_get_pd_dumpit(struct sk_buff *skb,
  823. struct netlink_callback *cb)
  824. {
  825. return res_get_common_dumpit(skb, cb, RDMA_RESTRACK_PD);
  826. }
  827. static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
  828. [RDMA_NLDEV_CMD_GET] = {
  829. .doit = nldev_get_doit,
  830. .dump = nldev_get_dumpit,
  831. },
  832. [RDMA_NLDEV_CMD_PORT_GET] = {
  833. .doit = nldev_port_get_doit,
  834. .dump = nldev_port_get_dumpit,
  835. },
  836. [RDMA_NLDEV_CMD_RES_GET] = {
  837. .doit = nldev_res_get_doit,
  838. .dump = nldev_res_get_dumpit,
  839. },
  840. [RDMA_NLDEV_CMD_RES_QP_GET] = {
  841. .dump = nldev_res_get_qp_dumpit,
  842. /*
  843. * .doit is not implemented yet for two reasons:
  844. * 1. It is not needed yet.
  845. * 2. There is a need to provide identifier, while it is easy
  846. * for the QPs (device index + port index + LQPN), it is not
  847. * the case for the rest of resources (PD and CQ). Because it
  848. * is better to provide similar interface for all resources,
  849. * let's wait till we will have other resources implemented
  850. * too.
  851. */
  852. },
  853. [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
  854. .dump = nldev_res_get_cm_id_dumpit,
  855. },
  856. [RDMA_NLDEV_CMD_RES_CQ_GET] = {
  857. .dump = nldev_res_get_cq_dumpit,
  858. },
  859. [RDMA_NLDEV_CMD_RES_MR_GET] = {
  860. .dump = nldev_res_get_mr_dumpit,
  861. },
  862. [RDMA_NLDEV_CMD_RES_PD_GET] = {
  863. .dump = nldev_res_get_pd_dumpit,
  864. },
  865. };
  866. void __init nldev_init(void)
  867. {
  868. rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
  869. }
  870. void __exit nldev_exit(void)
  871. {
  872. rdma_nl_unregister(RDMA_NL_NLDEV);
  873. }
  874. MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);