ipoib_netlink.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/module.h>
  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 int ipoib_fill_info(struct sk_buff *skb, const struct net_device *dev)
  42. {
  43. struct ipoib_dev_priv *priv = netdev_priv(dev);
  44. u16 val;
  45. if (nla_put_u16(skb, IFLA_IPOIB_PKEY, priv->pkey))
  46. goto nla_put_failure;
  47. val = test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  48. if (nla_put_u16(skb, IFLA_IPOIB_MODE, val))
  49. goto nla_put_failure;
  50. val = test_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  51. if (nla_put_u16(skb, IFLA_IPOIB_UMCAST, val))
  52. goto nla_put_failure;
  53. return 0;
  54. nla_put_failure:
  55. return -EMSGSIZE;
  56. }
  57. static int ipoib_changelink(struct net_device *dev,
  58. struct nlattr *tb[], struct nlattr *data[])
  59. {
  60. u16 mode, umcast;
  61. int ret = 0;
  62. if (data[IFLA_IPOIB_MODE]) {
  63. mode = nla_get_u16(data[IFLA_IPOIB_MODE]);
  64. if (mode == IPOIB_MODE_DATAGRAM)
  65. ret = ipoib_set_mode(dev, "datagram\n");
  66. else if (mode == IPOIB_MODE_CONNECTED)
  67. ret = ipoib_set_mode(dev, "connected\n");
  68. else
  69. ret = -EINVAL;
  70. if (ret < 0)
  71. goto out_err;
  72. }
  73. if (data[IFLA_IPOIB_UMCAST]) {
  74. umcast = nla_get_u16(data[IFLA_IPOIB_UMCAST]);
  75. ipoib_set_umcast(dev, umcast);
  76. }
  77. out_err:
  78. return ret;
  79. }
  80. static int ipoib_new_child_link(struct net *src_net, struct net_device *dev,
  81. struct nlattr *tb[], struct nlattr *data[])
  82. {
  83. struct net_device *pdev;
  84. struct ipoib_dev_priv *ppriv;
  85. u16 child_pkey;
  86. int err;
  87. if (!tb[IFLA_LINK])
  88. return -EINVAL;
  89. pdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  90. if (!pdev)
  91. return -ENODEV;
  92. ppriv = netdev_priv(pdev);
  93. if (test_bit(IPOIB_FLAG_SUBINTERFACE, &ppriv->flags)) {
  94. ipoib_warn(ppriv, "child creation disallowed for child devices\n");
  95. return -EINVAL;
  96. }
  97. if (!data || !data[IFLA_IPOIB_PKEY]) {
  98. ipoib_dbg(ppriv, "no pkey specified, using parent pkey\n");
  99. child_pkey = ppriv->pkey;
  100. } else
  101. child_pkey = nla_get_u16(data[IFLA_IPOIB_PKEY]);
  102. err = __ipoib_vlan_add(ppriv, netdev_priv(dev), child_pkey, IPOIB_RTNL_CHILD);
  103. if (!err && data)
  104. err = ipoib_changelink(dev, tb, data);
  105. return err;
  106. }
  107. static void ipoib_unregister_child_dev(struct net_device *dev, struct list_head *head)
  108. {
  109. struct ipoib_dev_priv *priv, *ppriv;
  110. priv = netdev_priv(dev);
  111. ppriv = netdev_priv(priv->parent);
  112. mutex_lock(&ppriv->vlan_mutex);
  113. unregister_netdevice_queue(dev, head);
  114. list_del(&priv->list);
  115. mutex_unlock(&ppriv->vlan_mutex);
  116. }
  117. static size_t ipoib_get_size(const struct net_device *dev)
  118. {
  119. return nla_total_size(2) + /* IFLA_IPOIB_PKEY */
  120. nla_total_size(2) + /* IFLA_IPOIB_MODE */
  121. nla_total_size(2); /* IFLA_IPOIB_UMCAST */
  122. }
  123. static struct rtnl_link_ops ipoib_link_ops __read_mostly = {
  124. .kind = "ipoib",
  125. .maxtype = IFLA_IPOIB_MAX,
  126. .policy = ipoib_policy,
  127. .priv_size = sizeof(struct ipoib_dev_priv),
  128. .setup = ipoib_setup,
  129. .newlink = ipoib_new_child_link,
  130. .changelink = ipoib_changelink,
  131. .dellink = ipoib_unregister_child_dev,
  132. .get_size = ipoib_get_size,
  133. .fill_info = ipoib_fill_info,
  134. };
  135. int __init ipoib_netlink_init(void)
  136. {
  137. return rtnl_link_register(&ipoib_link_ops);
  138. }
  139. void __exit ipoib_netlink_fini(void)
  140. {
  141. rtnl_link_unregister(&ipoib_link_ops);
  142. }
  143. MODULE_ALIAS_RTNL_LINK("ipoib");