This source file includes following definitions.
- show_parent
- is_child_unique
- __ipoib_vlan_add
- ipoib_vlan_add
- ipoib_vlan_delete_task
- ipoib_vlan_delete
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
43 static 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 }
51 static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
52
53 static 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
95 int __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
101 ASSERT_RTNL();
102
103
104
105
106
107 ndev->priv_destructor = ipoib_intf_free;
108
109
110
111
112
113 WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
114
115 if (pkey == 0 || pkey == 0x8000) {
116 result = -EINVAL;
117 goto out_early;
118 }
119
120 priv->parent = ppriv->dev;
121 priv->pkey = pkey;
122 priv->child_type = type;
123
124 if (!is_child_unique(ppriv, priv)) {
125 result = -ENOTUNIQ;
126 goto out_early;
127 }
128
129 result = register_netdevice(ndev);
130 if (result) {
131 ipoib_warn(priv, "failed to initialize; error %i", result);
132
133
134
135
136
137 goto out_early;
138 }
139
140
141 if (type == IPOIB_LEGACY_CHILD) {
142 if (ipoib_cm_add_mode_attr(ndev))
143 goto sysfs_failed;
144 if (ipoib_add_pkey_attr(ndev))
145 goto sysfs_failed;
146 if (ipoib_add_umcast_attr(ndev))
147 goto sysfs_failed;
148
149 if (device_create_file(&ndev->dev, &dev_attr_parent))
150 goto sysfs_failed;
151 }
152
153 return 0;
154
155 sysfs_failed:
156 unregister_netdevice(priv->dev);
157 return -ENOMEM;
158
159 out_early:
160 if (ndev->priv_destructor)
161 ndev->priv_destructor(ndev);
162 return result;
163 }
164
165 int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
166 {
167 struct ipoib_dev_priv *ppriv, *priv;
168 char intf_name[IFNAMSIZ];
169 struct net_device *ndev;
170 int result;
171
172 if (!capable(CAP_NET_ADMIN))
173 return -EPERM;
174
175 if (!rtnl_trylock())
176 return restart_syscall();
177
178 if (pdev->reg_state != NETREG_REGISTERED) {
179 rtnl_unlock();
180 return -EPERM;
181 }
182
183 ppriv = ipoib_priv(pdev);
184
185 snprintf(intf_name, sizeof(intf_name), "%s.%04x",
186 ppriv->dev->name, pkey);
187
188 ndev = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
189 if (IS_ERR(ndev)) {
190 result = PTR_ERR(ndev);
191 goto out;
192 }
193 priv = ipoib_priv(ndev);
194
195 result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
196
197 if (result && ndev->reg_state == NETREG_UNINITIALIZED)
198 free_netdev(ndev);
199
200 out:
201 rtnl_unlock();
202
203 return result;
204 }
205
206 struct ipoib_vlan_delete_work {
207 struct work_struct work;
208 struct net_device *dev;
209 };
210
211
212
213
214
215
216
217
218
219
220
221 static void ipoib_vlan_delete_task(struct work_struct *work)
222 {
223 struct ipoib_vlan_delete_work *pwork =
224 container_of(work, struct ipoib_vlan_delete_work, work);
225 struct net_device *dev = pwork->dev;
226
227 rtnl_lock();
228
229
230 if (dev->reg_state == NETREG_REGISTERED) {
231 struct ipoib_dev_priv *priv = ipoib_priv(dev);
232 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
233
234 ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
235 unregister_netdevice(dev);
236 }
237
238 rtnl_unlock();
239
240 kfree(pwork);
241 }
242
243 int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
244 {
245 struct ipoib_dev_priv *ppriv, *priv, *tpriv;
246 int rc;
247
248 if (!capable(CAP_NET_ADMIN))
249 return -EPERM;
250
251 if (!rtnl_trylock())
252 return restart_syscall();
253
254 if (pdev->reg_state != NETREG_REGISTERED) {
255 rtnl_unlock();
256 return -EPERM;
257 }
258
259 ppriv = ipoib_priv(pdev);
260
261 rc = -ENODEV;
262 list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
263 if (priv->pkey == pkey &&
264 priv->child_type == IPOIB_LEGACY_CHILD) {
265 struct ipoib_vlan_delete_work *work;
266
267 work = kmalloc(sizeof(*work), GFP_KERNEL);
268 if (!work) {
269 rc = -ENOMEM;
270 goto out;
271 }
272
273 down_write(&ppriv->vlan_rwsem);
274 list_del_init(&priv->list);
275 up_write(&ppriv->vlan_rwsem);
276 work->dev = priv->dev;
277 INIT_WORK(&work->work, ipoib_vlan_delete_task);
278 queue_work(ipoib_workqueue, &work->work);
279
280 rc = 0;
281 break;
282 }
283 }
284
285 out:
286 rtnl_unlock();
287
288 return rc;
289 }