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