1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/module.h>
34#include <linux/sched/signal.h>
35
36#include <linux/init.h>
37#include <linux/seq_file.h>
38
39#include <linux/uaccess.h>
40
41#include "ipoib.h"
42
43static ssize_t show_parent(struct device *d, struct device_attribute *attr,
44 char *buf)
45{
46 struct net_device *dev = to_net_dev(d);
47 struct ipoib_dev_priv *priv = ipoib_priv(dev);
48
49 return sprintf(buf, "%s\n", priv->parent->name);
50}
51static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
52
53static bool is_child_unique(struct ipoib_dev_priv *ppriv,
54 struct ipoib_dev_priv *priv)
55{
56 struct ipoib_dev_priv *tpriv;
57
58 ASSERT_RTNL();
59
60
61
62
63
64
65
66 if (priv->child_type != IPOIB_LEGACY_CHILD)
67 return true;
68
69
70
71
72
73
74 if (ppriv->pkey == priv->pkey)
75 return false;
76
77 list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
78 if (tpriv->pkey == priv->pkey &&
79 tpriv->child_type == IPOIB_LEGACY_CHILD)
80 return false;
81 }
82
83 return true;
84}
85
86
87
88
89
90
91
92
93
94
95int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
96 u16 pkey, int type)
97{
98 struct net_device *ndev = priv->dev;
99 int result;
100 struct rdma_netdev *rn = netdev_priv(ndev);
101
102 ASSERT_RTNL();
103
104
105
106
107
108 ndev->priv_destructor = ipoib_intf_free;
109
110
111
112
113
114 WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
115
116 if (pkey == 0 || pkey == 0x8000) {
117 result = -EINVAL;
118 goto out_early;
119 }
120
121 rn->mtu = priv->mcast_mtu;
122
123 priv->parent = ppriv->dev;
124 priv->pkey = pkey;
125 priv->child_type = type;
126
127 if (!is_child_unique(ppriv, priv)) {
128 result = -ENOTUNIQ;
129 goto out_early;
130 }
131
132 result = register_netdevice(ndev);
133 if (result) {
134 ipoib_warn(priv, "failed to initialize; error %i", result);
135
136
137
138
139
140 goto out_early;
141 }
142
143
144 if (type == IPOIB_LEGACY_CHILD) {
145 if (ipoib_cm_add_mode_attr(ndev))
146 goto sysfs_failed;
147 if (ipoib_add_pkey_attr(ndev))
148 goto sysfs_failed;
149 if (ipoib_add_umcast_attr(ndev))
150 goto sysfs_failed;
151
152 if (device_create_file(&ndev->dev, &dev_attr_parent))
153 goto sysfs_failed;
154 }
155
156 return 0;
157
158sysfs_failed:
159 unregister_netdevice(priv->dev);
160 return -ENOMEM;
161
162out_early:
163 if (ndev->priv_destructor)
164 ndev->priv_destructor(ndev);
165 return result;
166}
167
168int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
169{
170 struct ipoib_dev_priv *ppriv, *priv;
171 char intf_name[IFNAMSIZ];
172 struct net_device *ndev;
173 int result;
174
175 if (!capable(CAP_NET_ADMIN))
176 return -EPERM;
177
178 if (!rtnl_trylock())
179 return restart_syscall();
180
181 if (pdev->reg_state != NETREG_REGISTERED) {
182 rtnl_unlock();
183 return -EPERM;
184 }
185
186 ppriv = ipoib_priv(pdev);
187
188 snprintf(intf_name, sizeof(intf_name), "%s.%04x",
189 ppriv->dev->name, pkey);
190
191 ndev = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
192 if (IS_ERR(ndev)) {
193 result = PTR_ERR(ndev);
194 goto out;
195 }
196 priv = ipoib_priv(ndev);
197
198 ndev->rtnl_link_ops = ipoib_get_link_ops();
199
200 result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
201
202 if (result && ndev->reg_state == NETREG_UNINITIALIZED)
203 free_netdev(ndev);
204
205out:
206 rtnl_unlock();
207
208 return result;
209}
210
211struct ipoib_vlan_delete_work {
212 struct work_struct work;
213 struct net_device *dev;
214};
215
216
217
218
219
220
221
222
223
224
225
226static void ipoib_vlan_delete_task(struct work_struct *work)
227{
228 struct ipoib_vlan_delete_work *pwork =
229 container_of(work, struct ipoib_vlan_delete_work, work);
230 struct net_device *dev = pwork->dev;
231
232 rtnl_lock();
233
234
235 if (dev->reg_state == NETREG_REGISTERED) {
236 struct ipoib_dev_priv *priv = ipoib_priv(dev);
237 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
238
239 ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
240 unregister_netdevice(dev);
241 }
242
243 rtnl_unlock();
244
245 kfree(pwork);
246}
247
248int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
249{
250 struct ipoib_dev_priv *ppriv, *priv, *tpriv;
251 int rc;
252
253 if (!capable(CAP_NET_ADMIN))
254 return -EPERM;
255
256 if (!rtnl_trylock())
257 return restart_syscall();
258
259 if (pdev->reg_state != NETREG_REGISTERED) {
260 rtnl_unlock();
261 return -EPERM;
262 }
263
264 ppriv = ipoib_priv(pdev);
265
266 rc = -ENODEV;
267 list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
268 if (priv->pkey == pkey &&
269 priv->child_type == IPOIB_LEGACY_CHILD) {
270 struct ipoib_vlan_delete_work *work;
271
272 work = kmalloc(sizeof(*work), GFP_KERNEL);
273 if (!work) {
274 rc = -ENOMEM;
275 goto out;
276 }
277
278 down_write(&ppriv->vlan_rwsem);
279 list_del_init(&priv->list);
280 up_write(&ppriv->vlan_rwsem);
281 work->dev = priv->dev;
282 INIT_WORK(&work->work, ipoib_vlan_delete_task);
283 queue_work(ipoib_workqueue, &work->work);
284
285 rc = 0;
286 break;
287 }
288 }
289
290out:
291 rtnl_unlock();
292
293 return rc;
294}
295