0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
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
0061
0062
0063
0064
0065 if (priv->child_type != IPOIB_LEGACY_CHILD)
0066 return true;
0067
0068
0069
0070
0071
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
0087
0088
0089
0090
0091
0092
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
0105
0106
0107 ndev->priv_destructor = ipoib_intf_free;
0108
0109
0110
0111
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
0137
0138
0139 goto out_early;
0140 }
0141
0142
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
0217
0218
0219
0220
0221
0222
0223
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
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 }