ipoib_vlan.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 = ipoib_priv(dev);
  43. return sprintf(buf, "%s\n", priv->parent->name);
  44. }
  45. static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
  46. static bool is_child_unique(struct ipoib_dev_priv *ppriv,
  47. struct ipoib_dev_priv *priv)
  48. {
  49. struct ipoib_dev_priv *tpriv;
  50. ASSERT_RTNL();
  51. /*
  52. * Since the legacy sysfs interface uses pkey for deletion it cannot
  53. * support more than one interface with the same pkey, it creates
  54. * ambiguity. The RTNL interface deletes using the netdev so it does
  55. * not have a problem to support duplicated pkeys.
  56. */
  57. if (priv->child_type != IPOIB_LEGACY_CHILD)
  58. return true;
  59. /*
  60. * First ensure this isn't a duplicate. We check the parent device and
  61. * then all of the legacy child interfaces to make sure the Pkey
  62. * doesn't match.
  63. */
  64. if (ppriv->pkey == priv->pkey)
  65. return false;
  66. list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
  67. if (tpriv->pkey == priv->pkey &&
  68. tpriv->child_type == IPOIB_LEGACY_CHILD)
  69. return false;
  70. }
  71. return true;
  72. }
  73. /*
  74. * NOTE: If this function fails then the priv->dev will remain valid, however
  75. * priv can have been freed and must not be touched by caller in the error
  76. * case.
  77. *
  78. * If (ndev->reg_state == NETREG_UNINITIALIZED) then it is up to the caller to
  79. * free the net_device (just as rtnl_newlink does) otherwise the net_device
  80. * will be freed when the rtnl is unlocked.
  81. */
  82. int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
  83. u16 pkey, int type)
  84. {
  85. struct net_device *ndev = priv->dev;
  86. int result;
  87. ASSERT_RTNL();
  88. /*
  89. * Racing with unregister of the parent must be prevented by the
  90. * caller.
  91. */
  92. WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
  93. if (pkey == 0 || pkey == 0x8000) {
  94. result = -EINVAL;
  95. goto out_early;
  96. }
  97. priv->parent = ppriv->dev;
  98. priv->pkey = pkey;
  99. priv->child_type = type;
  100. if (!is_child_unique(ppriv, priv)) {
  101. result = -ENOTUNIQ;
  102. goto out_early;
  103. }
  104. /* We do not need to touch priv if register_netdevice fails */
  105. ndev->priv_destructor = ipoib_intf_free;
  106. result = register_netdevice(ndev);
  107. if (result) {
  108. ipoib_warn(priv, "failed to initialize; error %i", result);
  109. /*
  110. * register_netdevice sometimes calls priv_destructor,
  111. * sometimes not. Make sure it was done.
  112. */
  113. goto out_early;
  114. }
  115. /* RTNL childs don't need proprietary sysfs entries */
  116. if (type == IPOIB_LEGACY_CHILD) {
  117. if (ipoib_cm_add_mode_attr(ndev))
  118. goto sysfs_failed;
  119. if (ipoib_add_pkey_attr(ndev))
  120. goto sysfs_failed;
  121. if (ipoib_add_umcast_attr(ndev))
  122. goto sysfs_failed;
  123. if (device_create_file(&ndev->dev, &dev_attr_parent))
  124. goto sysfs_failed;
  125. }
  126. return 0;
  127. sysfs_failed:
  128. unregister_netdevice(priv->dev);
  129. return -ENOMEM;
  130. out_early:
  131. if (ndev->priv_destructor)
  132. ndev->priv_destructor(ndev);
  133. return result;
  134. }
  135. int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
  136. {
  137. struct ipoib_dev_priv *ppriv, *priv;
  138. char intf_name[IFNAMSIZ];
  139. struct net_device *ndev;
  140. int result;
  141. if (!capable(CAP_NET_ADMIN))
  142. return -EPERM;
  143. if (!rtnl_trylock())
  144. return restart_syscall();
  145. if (pdev->reg_state != NETREG_REGISTERED) {
  146. rtnl_unlock();
  147. return -EPERM;
  148. }
  149. ppriv = ipoib_priv(pdev);
  150. snprintf(intf_name, sizeof(intf_name), "%s.%04x",
  151. ppriv->dev->name, pkey);
  152. priv = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
  153. if (!priv) {
  154. result = -ENOMEM;
  155. goto out;
  156. }
  157. ndev = priv->dev;
  158. result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
  159. if (result && ndev->reg_state == NETREG_UNINITIALIZED)
  160. free_netdev(ndev);
  161. out:
  162. rtnl_unlock();
  163. return result;
  164. }
  165. struct ipoib_vlan_delete_work {
  166. struct work_struct work;
  167. struct net_device *dev;
  168. };
  169. /*
  170. * sysfs callbacks of a netdevice cannot obtain the rtnl lock as
  171. * unregister_netdev ultimately deletes the sysfs files while holding the rtnl
  172. * lock. This deadlocks the system.
  173. *
  174. * A callback can use rtnl_trylock to avoid the deadlock but it cannot call
  175. * unregister_netdev as that internally takes and releases the rtnl_lock. So
  176. * instead we find the netdev to unregister and then do the actual unregister
  177. * from the global work queue where we can obtain the rtnl_lock safely.
  178. */
  179. static void ipoib_vlan_delete_task(struct work_struct *work)
  180. {
  181. struct ipoib_vlan_delete_work *pwork =
  182. container_of(work, struct ipoib_vlan_delete_work, work);
  183. struct net_device *dev = pwork->dev;
  184. rtnl_lock();
  185. /* Unregistering tasks can race with another task or parent removal */
  186. if (dev->reg_state == NETREG_REGISTERED) {
  187. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  188. struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
  189. ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
  190. unregister_netdevice(dev);
  191. }
  192. rtnl_unlock();
  193. kfree(pwork);
  194. }
  195. int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
  196. {
  197. struct ipoib_dev_priv *ppriv, *priv, *tpriv;
  198. int rc;
  199. if (!capable(CAP_NET_ADMIN))
  200. return -EPERM;
  201. if (!rtnl_trylock())
  202. return restart_syscall();
  203. if (pdev->reg_state != NETREG_REGISTERED) {
  204. rtnl_unlock();
  205. return -EPERM;
  206. }
  207. ppriv = ipoib_priv(pdev);
  208. rc = -ENODEV;
  209. list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
  210. if (priv->pkey == pkey &&
  211. priv->child_type == IPOIB_LEGACY_CHILD) {
  212. struct ipoib_vlan_delete_work *work;
  213. work = kmalloc(sizeof(*work), GFP_KERNEL);
  214. if (!work) {
  215. rc = -ENOMEM;
  216. goto out;
  217. }
  218. down_write(&ppriv->vlan_rwsem);
  219. list_del_init(&priv->list);
  220. up_write(&ppriv->vlan_rwsem);
  221. work->dev = priv->dev;
  222. INIT_WORK(&work->work, ipoib_vlan_delete_task);
  223. queue_work(ipoib_workqueue, &work->work);
  224. rc = 0;
  225. break;
  226. }
  227. }
  228. out:
  229. rtnl_unlock();
  230. return rc;
  231. }