nldev.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <net/netlink.h>
  33. #include <rdma/rdma_netlink.h>
  34. #include "core_priv.h"
  35. static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
  36. [RDMA_NLDEV_ATTR_DEV_INDEX] = { .type = NLA_U32 },
  37. [RDMA_NLDEV_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING,
  38. .len = IB_DEVICE_NAME_MAX - 1},
  39. [RDMA_NLDEV_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  40. };
  41. static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
  42. {
  43. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
  44. return -EMSGSIZE;
  45. if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME, device->name))
  46. return -EMSGSIZE;
  47. if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
  48. return -EMSGSIZE;
  49. return 0;
  50. }
  51. static int _nldev_get_dumpit(struct ib_device *device,
  52. struct sk_buff *skb,
  53. struct netlink_callback *cb,
  54. unsigned int idx)
  55. {
  56. int start = cb->args[0];
  57. struct nlmsghdr *nlh;
  58. if (idx < start)
  59. return 0;
  60. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  61. RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
  62. 0, NLM_F_MULTI);
  63. if (fill_dev_info(skb, device)) {
  64. nlmsg_cancel(skb, nlh);
  65. goto out;
  66. }
  67. nlmsg_end(skb, nlh);
  68. idx++;
  69. out: cb->args[0] = idx;
  70. return skb->len;
  71. }
  72. static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
  73. {
  74. /*
  75. * There is no need to take lock, because
  76. * we are relying on ib_core's lists_rwsem
  77. */
  78. return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
  79. }
  80. static const struct rdma_nl_cbs nldev_cb_table[] = {
  81. [RDMA_NLDEV_CMD_GET] = {
  82. .dump = nldev_get_dumpit,
  83. },
  84. };
  85. void __init nldev_init(void)
  86. {
  87. rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
  88. }
  89. void __exit nldev_exit(void)
  90. {
  91. rdma_nl_unregister(RDMA_NL_NLDEV);
  92. }