Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
0003  *
0004  * This software is available to you under a choice of one of two
0005  * licenses.  You may choose to be licensed under the terms of the GNU
0006  * General Public License (GPL) Version 2, available from the file
0007  * COPYING in the main directory of this source tree, or the
0008  * OpenIB.org BSD license below:
0009  *
0010  *     Redistribution and use in source and binary forms, with or
0011  *     without modification, are permitted provided that the following
0012  *     conditions are met:
0013  *
0014  *      - Redistributions of source code must retain the above
0015  *        copyright notice, this list of conditions and the following
0016  *        disclaimer.
0017  *
0018  *      - Redistributions in binary form must reproduce the above
0019  *        copyright notice, this list of conditions and the following
0020  *        disclaimer in the documentation and/or other materials
0021  *        provided with the distribution.
0022  *
0023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0024  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0025  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0026  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0027  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0028  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0029  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0030  * SOFTWARE.
0031  */
0032 
0033 #include <linux/sched/signal.h>
0034 
0035 #include <linux/init.h>
0036 #include <linux/seq_file.h>
0037 
0038 #include <linux/uaccess.h>
0039 
0040 #include "ipoib.h"
0041 
0042 static ssize_t parent_show(struct device *d, struct device_attribute *attr,
0043                char *buf)
0044 {
0045     struct net_device *dev = to_net_dev(d);
0046     struct ipoib_dev_priv *priv = ipoib_priv(dev);
0047 
0048     return sysfs_emit(buf, "%s\n", priv->parent->name);
0049 }
0050 static DEVICE_ATTR_RO(parent);
0051 
0052 static bool is_child_unique(struct ipoib_dev_priv *ppriv,
0053                 struct ipoib_dev_priv *priv)
0054 {
0055     struct ipoib_dev_priv *tpriv;
0056 
0057     ASSERT_RTNL();
0058 
0059     /*
0060      * Since the legacy sysfs interface uses pkey for deletion it cannot
0061      * support more than one interface with the same pkey, it creates
0062      * ambiguity.  The RTNL interface deletes using the netdev so it does
0063      * not have a problem to support duplicated pkeys.
0064      */
0065     if (priv->child_type != IPOIB_LEGACY_CHILD)
0066         return true;
0067 
0068     /*
0069      * First ensure this isn't a duplicate. We check the parent device and
0070      * then all of the legacy child interfaces to make sure the Pkey
0071      * doesn't match.
0072      */
0073     if (ppriv->pkey == priv->pkey)
0074         return false;
0075 
0076     list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
0077         if (tpriv->pkey == priv->pkey &&
0078             tpriv->child_type == IPOIB_LEGACY_CHILD)
0079             return false;
0080     }
0081 
0082     return true;
0083 }
0084 
0085 /*
0086  * NOTE: If this function fails then the priv->dev will remain valid, however
0087  * priv will have been freed and must not be touched by caller in the error
0088  * case.
0089  *
0090  * If (ndev->reg_state == NETREG_UNINITIALIZED) then it is up to the caller to
0091  * free the net_device (just as rtnl_newlink does) otherwise the net_device
0092  * will be freed when the rtnl is unlocked.
0093  */
0094 int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
0095              u16 pkey, int type)
0096 {
0097     struct net_device *ndev = priv->dev;
0098     int result;
0099     struct rdma_netdev *rn = netdev_priv(ndev);
0100 
0101     ASSERT_RTNL();
0102 
0103     /*
0104      * We do not need to touch priv if register_netdevice fails, so just
0105      * always use this flow.
0106      */
0107     ndev->priv_destructor = ipoib_intf_free;
0108 
0109     /*
0110      * Racing with unregister of the parent must be prevented by the
0111      * caller.
0112      */
0113     WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
0114 
0115     if (pkey == 0 || pkey == 0x8000) {
0116         result = -EINVAL;
0117         goto out_early;
0118     }
0119 
0120     rn->mtu = priv->mcast_mtu;
0121 
0122     priv->parent = ppriv->dev;
0123     priv->pkey = pkey;
0124     priv->child_type = type;
0125 
0126     if (!is_child_unique(ppriv, priv)) {
0127         result = -ENOTUNIQ;
0128         goto out_early;
0129     }
0130 
0131     result = register_netdevice(ndev);
0132     if (result) {
0133         ipoib_warn(priv, "failed to initialize; error %i", result);
0134 
0135         /*
0136          * register_netdevice sometimes calls priv_destructor,
0137          * sometimes not. Make sure it was done.
0138          */
0139         goto out_early;
0140     }
0141 
0142     /* RTNL childs don't need proprietary sysfs entries */
0143     if (type == IPOIB_LEGACY_CHILD) {
0144         if (ipoib_cm_add_mode_attr(ndev))
0145             goto sysfs_failed;
0146         if (ipoib_add_pkey_attr(ndev))
0147             goto sysfs_failed;
0148         if (ipoib_add_umcast_attr(ndev))
0149             goto sysfs_failed;
0150 
0151         if (device_create_file(&ndev->dev, &dev_attr_parent))
0152             goto sysfs_failed;
0153     }
0154 
0155     return 0;
0156 
0157 sysfs_failed:
0158     unregister_netdevice(priv->dev);
0159     return -ENOMEM;
0160 
0161 out_early:
0162     if (ndev->priv_destructor)
0163         ndev->priv_destructor(ndev);
0164     return result;
0165 }
0166 
0167 int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
0168 {
0169     struct ipoib_dev_priv *ppriv, *priv;
0170     char intf_name[IFNAMSIZ];
0171     struct net_device *ndev;
0172     int result;
0173 
0174     if (!capable(CAP_NET_ADMIN))
0175         return -EPERM;
0176 
0177     if (!rtnl_trylock())
0178         return restart_syscall();
0179 
0180     if (pdev->reg_state != NETREG_REGISTERED) {
0181         rtnl_unlock();
0182         return -EPERM;
0183     }
0184 
0185     ppriv = ipoib_priv(pdev);
0186 
0187     snprintf(intf_name, sizeof(intf_name), "%s.%04x",
0188          ppriv->dev->name, pkey);
0189 
0190     ndev = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
0191     if (IS_ERR(ndev)) {
0192         result = PTR_ERR(ndev);
0193         goto out;
0194     }
0195     priv = ipoib_priv(ndev);
0196 
0197     ndev->rtnl_link_ops = ipoib_get_link_ops();
0198 
0199     result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
0200 
0201     if (result && ndev->reg_state == NETREG_UNINITIALIZED)
0202         free_netdev(ndev);
0203 
0204 out:
0205     rtnl_unlock();
0206 
0207     return result;
0208 }
0209 
0210 struct ipoib_vlan_delete_work {
0211     struct work_struct work;
0212     struct net_device *dev;
0213 };
0214 
0215 /*
0216  * sysfs callbacks of a netdevice cannot obtain the rtnl lock as
0217  * unregister_netdev ultimately deletes the sysfs files while holding the rtnl
0218  * lock. This deadlocks the system.
0219  *
0220  * A callback can use rtnl_trylock to avoid the deadlock but it cannot call
0221  * unregister_netdev as that internally takes and releases the rtnl_lock.  So
0222  * instead we find the netdev to unregister and then do the actual unregister
0223  * from the global work queue where we can obtain the rtnl_lock safely.
0224  */
0225 static void ipoib_vlan_delete_task(struct work_struct *work)
0226 {
0227     struct ipoib_vlan_delete_work *pwork =
0228         container_of(work, struct ipoib_vlan_delete_work, work);
0229     struct net_device *dev = pwork->dev;
0230 
0231     rtnl_lock();
0232 
0233     /* Unregistering tasks can race with another task or parent removal */
0234     if (dev->reg_state == NETREG_REGISTERED) {
0235         struct ipoib_dev_priv *priv = ipoib_priv(dev);
0236         struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
0237 
0238         ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
0239         unregister_netdevice(dev);
0240     }
0241 
0242     rtnl_unlock();
0243 
0244     kfree(pwork);
0245 }
0246 
0247 int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
0248 {
0249     struct ipoib_dev_priv *ppriv, *priv, *tpriv;
0250     int rc;
0251 
0252     if (!capable(CAP_NET_ADMIN))
0253         return -EPERM;
0254 
0255     if (!rtnl_trylock())
0256         return restart_syscall();
0257 
0258     if (pdev->reg_state != NETREG_REGISTERED) {
0259         rtnl_unlock();
0260         return -EPERM;
0261     }
0262 
0263     ppriv = ipoib_priv(pdev);
0264 
0265     rc = -ENODEV;
0266     list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
0267         if (priv->pkey == pkey &&
0268             priv->child_type == IPOIB_LEGACY_CHILD) {
0269             struct ipoib_vlan_delete_work *work;
0270 
0271             work = kmalloc(sizeof(*work), GFP_KERNEL);
0272             if (!work) {
0273                 rc = -ENOMEM;
0274                 goto out;
0275             }
0276 
0277             down_write(&ppriv->vlan_rwsem);
0278             list_del_init(&priv->list);
0279             up_write(&ppriv->vlan_rwsem);
0280             work->dev = priv->dev;
0281             INIT_WORK(&work->work, ipoib_vlan_delete_task);
0282             queue_work(ipoib_workqueue, &work->work);
0283 
0284             rc = 0;
0285             break;
0286         }
0287     }
0288 
0289 out:
0290     rtnl_unlock();
0291 
0292     return rc;
0293 }