ipoib_vlan.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. 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/module.h>
  33. #include <linux/sched/signal.h>
  34. #include <linux/init.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/uaccess.h>
  37. #include "ipoib.h"
  38. static ssize_t show_parent(struct device *d, struct device_attribute *attr,
  39. char *buf)
  40. {
  41. struct net_device *dev = to_net_dev(d);
  42. struct ipoib_dev_priv *priv = netdev_priv(dev);
  43. return sprintf(buf, "%s\n", priv->parent->name);
  44. }
  45. static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
  46. int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
  47. u16 pkey, int type)
  48. {
  49. int result;
  50. priv->max_ib_mtu = ppriv->max_ib_mtu;
  51. /* MTU will be reset when mcast join happens */
  52. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  53. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  54. priv->parent = ppriv->dev;
  55. set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
  56. ipoib_set_dev_features(priv, ppriv->ca);
  57. priv->pkey = pkey;
  58. memcpy(priv->dev->dev_addr, ppriv->dev->dev_addr, INFINIBAND_ALEN);
  59. memcpy(&priv->local_gid, &ppriv->local_gid, sizeof(priv->local_gid));
  60. set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
  61. priv->dev->broadcast[8] = pkey >> 8;
  62. priv->dev->broadcast[9] = pkey & 0xff;
  63. result = ipoib_dev_init(priv->dev, ppriv->ca, ppriv->port);
  64. if (result < 0) {
  65. ipoib_warn(ppriv, "failed to initialize subinterface: "
  66. "device %s, port %d",
  67. ppriv->ca->name, ppriv->port);
  68. goto err;
  69. }
  70. result = register_netdevice(priv->dev);
  71. if (result) {
  72. ipoib_warn(priv, "failed to initialize; error %i", result);
  73. goto register_failed;
  74. }
  75. ipoib_create_debug_files(priv->dev);
  76. /* RTNL childs don't need proprietary sysfs entries */
  77. if (type == IPOIB_LEGACY_CHILD) {
  78. if (ipoib_cm_add_mode_attr(priv->dev))
  79. goto sysfs_failed;
  80. if (ipoib_add_pkey_attr(priv->dev))
  81. goto sysfs_failed;
  82. if (ipoib_add_umcast_attr(priv->dev))
  83. goto sysfs_failed;
  84. if (device_create_file(&priv->dev->dev, &dev_attr_parent))
  85. goto sysfs_failed;
  86. }
  87. priv->child_type = type;
  88. list_add_tail(&priv->list, &ppriv->child_intfs);
  89. return 0;
  90. sysfs_failed:
  91. result = -ENOMEM;
  92. ipoib_delete_debug_files(priv->dev);
  93. unregister_netdevice(priv->dev);
  94. register_failed:
  95. ipoib_dev_cleanup(priv->dev);
  96. err:
  97. return result;
  98. }
  99. int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
  100. {
  101. struct ipoib_dev_priv *ppriv, *priv;
  102. char intf_name[IFNAMSIZ];
  103. struct ipoib_dev_priv *tpriv;
  104. int result;
  105. if (!capable(CAP_NET_ADMIN))
  106. return -EPERM;
  107. ppriv = netdev_priv(pdev);
  108. if (test_bit(IPOIB_FLAG_GOING_DOWN, &ppriv->flags))
  109. return -EPERM;
  110. snprintf(intf_name, sizeof intf_name, "%s.%04x",
  111. ppriv->dev->name, pkey);
  112. priv = ipoib_intf_alloc(intf_name);
  113. if (!priv)
  114. return -ENOMEM;
  115. if (!rtnl_trylock())
  116. return restart_syscall();
  117. down_write(&ppriv->vlan_rwsem);
  118. /*
  119. * First ensure this isn't a duplicate. We check the parent device and
  120. * then all of the legacy child interfaces to make sure the Pkey
  121. * doesn't match.
  122. */
  123. if (ppriv->pkey == pkey) {
  124. result = -ENOTUNIQ;
  125. goto out;
  126. }
  127. list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
  128. if (tpriv->pkey == pkey &&
  129. tpriv->child_type == IPOIB_LEGACY_CHILD) {
  130. result = -ENOTUNIQ;
  131. goto out;
  132. }
  133. }
  134. result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
  135. out:
  136. up_write(&ppriv->vlan_rwsem);
  137. rtnl_unlock();
  138. if (result)
  139. free_netdev(priv->dev);
  140. return result;
  141. }
  142. int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
  143. {
  144. struct ipoib_dev_priv *ppriv, *priv, *tpriv;
  145. struct net_device *dev = NULL;
  146. if (!capable(CAP_NET_ADMIN))
  147. return -EPERM;
  148. ppriv = netdev_priv(pdev);
  149. if (test_bit(IPOIB_FLAG_GOING_DOWN, &ppriv->flags))
  150. return -EPERM;
  151. if (!rtnl_trylock())
  152. return restart_syscall();
  153. down_write(&ppriv->vlan_rwsem);
  154. list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
  155. if (priv->pkey == pkey &&
  156. priv->child_type == IPOIB_LEGACY_CHILD) {
  157. list_del(&priv->list);
  158. dev = priv->dev;
  159. break;
  160. }
  161. }
  162. up_write(&ppriv->vlan_rwsem);
  163. if (dev) {
  164. ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
  165. unregister_netdevice(dev);
  166. }
  167. rtnl_unlock();
  168. if (dev) {
  169. free_netdev(dev);
  170. return 0;
  171. }
  172. return -ENODEV;
  173. }