ipoib_netlink.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2012 Mellanox Technologies. - All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  34. #include <net/rtnetlink.h>
  35. #include "ipoib.h"
  36. static const struct nla_policy ipoib_policy[IFLA_IPOIB_MAX + 1] = {
  37. [IFLA_IPOIB_PKEY] = { .type = NLA_U16 },
  38. [IFLA_IPOIB_MODE] = { .type = NLA_U16 },
  39. [IFLA_IPOIB_UMCAST] = { .type = NLA_U16 },
  40. };
  41. static unsigned int ipoib_get_max_num_queues(void)
  42. {
  43. return min_t(unsigned int, num_possible_cpus(), 128);
  44. }
  45. static int ipoib_fill_info(struct sk_buff *skb, const struct net_device *dev)
  46. {
  47. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  48. u16 val;
  49. if (nla_put_u16(skb, IFLA_IPOIB_PKEY, priv->pkey))
  50. goto nla_put_failure;
  51. val = test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  52. if (nla_put_u16(skb, IFLA_IPOIB_MODE, val))
  53. goto nla_put_failure;
  54. val = test_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  55. if (nla_put_u16(skb, IFLA_IPOIB_UMCAST, val))
  56. goto nla_put_failure;
  57. return 0;
  58. nla_put_failure:
  59. return -EMSGSIZE;
  60. }
  61. static int ipoib_changelink(struct net_device *dev, struct nlattr *tb[],
  62. struct nlattr *data[],
  63. struct netlink_ext_ack *extack)
  64. {
  65. u16 mode, umcast;
  66. int ret = 0;
  67. if (data[IFLA_IPOIB_MODE]) {
  68. mode = nla_get_u16(data[IFLA_IPOIB_MODE]);
  69. if (mode == IPOIB_MODE_DATAGRAM)
  70. ret = ipoib_set_mode(dev, "datagram\n");
  71. else if (mode == IPOIB_MODE_CONNECTED)
  72. ret = ipoib_set_mode(dev, "connected\n");
  73. else
  74. ret = -EINVAL;
  75. if (ret < 0)
  76. goto out_err;
  77. }
  78. if (data[IFLA_IPOIB_UMCAST]) {
  79. umcast = nla_get_u16(data[IFLA_IPOIB_UMCAST]);
  80. ipoib_set_umcast(dev, umcast);
  81. }
  82. out_err:
  83. return ret;
  84. }
  85. static int ipoib_new_child_link(struct net_device *dev,
  86. struct rtnl_newlink_params *params,
  87. struct netlink_ext_ack *extack)
  88. {
  89. struct net *link_net = rtnl_newlink_link_net(params);
  90. struct nlattr **data = params->data;
  91. struct nlattr **tb = params->tb;
  92. struct net_device *pdev;
  93. struct ipoib_dev_priv *ppriv;
  94. u16 child_pkey;
  95. int err;
  96. if (!tb[IFLA_LINK])
  97. return -EINVAL;
  98. pdev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK]));
  99. if (!pdev || pdev->type != ARPHRD_INFINIBAND)
  100. return -ENODEV;
  101. ppriv = ipoib_priv(pdev);
  102. if (test_bit(IPOIB_FLAG_SUBINTERFACE, &ppriv->flags)) {
  103. ipoib_warn(ppriv, "child creation disallowed for child devices\n");
  104. return -EINVAL;
  105. }
  106. if (!data || !data[IFLA_IPOIB_PKEY]) {
  107. ipoib_dbg(ppriv, "no pkey specified, using parent pkey\n");
  108. child_pkey = ppriv->pkey;
  109. } else
  110. child_pkey = nla_get_u16(data[IFLA_IPOIB_PKEY]);
  111. err = ipoib_intf_init(ppriv->ca, ppriv->port, dev->name, dev);
  112. if (err) {
  113. ipoib_warn(ppriv, "failed to initialize pkey device\n");
  114. return err;
  115. }
  116. err = __ipoib_vlan_add(ppriv, ipoib_priv(dev),
  117. child_pkey, IPOIB_RTNL_CHILD);
  118. if (err)
  119. return err;
  120. if (data) {
  121. err = ipoib_changelink(dev, tb, data, extack);
  122. if (err) {
  123. unregister_netdevice(dev);
  124. return err;
  125. }
  126. }
  127. return 0;
  128. }
  129. static void ipoib_del_child_link(struct net_device *dev, struct list_head *head)
  130. {
  131. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  132. if (!priv->parent)
  133. return;
  134. unregister_netdevice_queue(dev, head);
  135. }
  136. static size_t ipoib_get_size(const struct net_device *dev)
  137. {
  138. return nla_total_size(2) + /* IFLA_IPOIB_PKEY */
  139. nla_total_size(2) + /* IFLA_IPOIB_MODE */
  140. nla_total_size(2); /* IFLA_IPOIB_UMCAST */
  141. }
  142. static struct rtnl_link_ops ipoib_link_ops __read_mostly = {
  143. .kind = "ipoib",
  144. .netns_refund = true,
  145. .maxtype = IFLA_IPOIB_MAX,
  146. .policy = ipoib_policy,
  147. .priv_size = sizeof(struct ipoib_dev_priv),
  148. .setup = ipoib_setup_common,
  149. .newlink = ipoib_new_child_link,
  150. .dellink = ipoib_del_child_link,
  151. .changelink = ipoib_changelink,
  152. .get_size = ipoib_get_size,
  153. .fill_info = ipoib_fill_info,
  154. .get_num_rx_queues = ipoib_get_max_num_queues,
  155. .get_num_tx_queues = ipoib_get_max_num_queues,
  156. };
  157. struct rtnl_link_ops *ipoib_get_link_ops(void)
  158. {
  159. return &ipoib_link_ops;
  160. }
  161. int __init ipoib_netlink_init(void)
  162. {
  163. return rtnl_link_register(&ipoib_link_ops);
  164. }
  165. void __exit ipoib_netlink_fini(void)
  166. {
  167. rtnl_link_unregister(&ipoib_link_ops);
  168. }
  169. MODULE_ALIAS_RTNL_LINK("ipoib");