ipoib_vlan.c 7.5 KB

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