]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6812baab TR |
2 | /* |
3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE | |
4 | * | |
5 | * Generic netlink support functions to configure an SMC-R PNET table | |
6 | * | |
7 | * Copyright IBM Corp. 2016 | |
8 | * | |
9 | * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com> | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/list.h> | |
14 | #include <linux/ctype.h> | |
15 | #include <net/netlink.h> | |
16 | #include <net/genetlink.h> | |
17 | ||
18 | #include <uapi/linux/if.h> | |
19 | #include <uapi/linux/smc.h> | |
20 | ||
21 | #include <rdma/ib_verbs.h> | |
22 | ||
23 | #include "smc_pnet.h" | |
24 | #include "smc_ib.h" | |
1619f770 | 25 | #include "smc_ism.h" |
6812baab | 26 | |
890a2cb4 HW |
27 | #define SMC_ASCII_BLANK 32 |
28 | ||
29 | static struct net_device *pnet_find_base_ndev(struct net_device *ndev); | |
30 | ||
6812baab TR |
31 | static struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = { |
32 | [SMC_PNETID_NAME] = { | |
33 | .type = NLA_NUL_STRING, | |
ca8dc133 | 34 | .len = SMC_MAX_PNETID_LEN |
6812baab TR |
35 | }, |
36 | [SMC_PNETID_ETHNAME] = { | |
37 | .type = NLA_NUL_STRING, | |
38 | .len = IFNAMSIZ - 1 | |
39 | }, | |
40 | [SMC_PNETID_IBNAME] = { | |
41 | .type = NLA_NUL_STRING, | |
42 | .len = IB_DEVICE_NAME_MAX - 1 | |
43 | }, | |
44 | [SMC_PNETID_IBPORT] = { .type = NLA_U8 } | |
45 | }; | |
46 | ||
47 | static struct genl_family smc_pnet_nl_family; | |
48 | ||
49 | /** | |
50 | * struct smc_pnettable - SMC PNET table anchor | |
51 | * @lock: Lock for list action | |
52 | * @pnetlist: List of PNETIDs | |
53 | */ | |
54 | static struct smc_pnettable { | |
55 | rwlock_t lock; | |
56 | struct list_head pnetlist; | |
57 | } smc_pnettable = { | |
58 | .pnetlist = LIST_HEAD_INIT(smc_pnettable.pnetlist), | |
59 | .lock = __RW_LOCK_UNLOCKED(smc_pnettable.lock) | |
60 | }; | |
61 | ||
62 | /** | |
890a2cb4 | 63 | * struct smc_user_pnetentry - pnet identifier name entry for/from user |
6812baab TR |
64 | * @list: List node. |
65 | * @pnet_name: Pnet identifier name | |
66 | * @ndev: pointer to network device. | |
67 | * @smcibdev: Pointer to IB device. | |
890a2cb4 | 68 | * @ib_port: Port of IB device. |
f3d74b22 | 69 | * @smcd_dev: Pointer to smcd device. |
6812baab | 70 | */ |
890a2cb4 | 71 | struct smc_user_pnetentry { |
6812baab | 72 | struct list_head list; |
0afff91c | 73 | char pnet_name[SMC_MAX_PNETID_LEN + 1]; |
6812baab TR |
74 | struct net_device *ndev; |
75 | struct smc_ib_device *smcibdev; | |
76 | u8 ib_port; | |
f3d74b22 | 77 | struct smcd_dev *smcd_dev; |
6812baab TR |
78 | }; |
79 | ||
890a2cb4 HW |
80 | /* pnet entry stored in pnet table */ |
81 | struct smc_pnetentry { | |
82 | struct list_head list; | |
83 | char pnet_name[SMC_MAX_PNETID_LEN + 1]; | |
84 | struct net_device *ndev; | |
85 | }; | |
6812baab | 86 | |
890a2cb4 HW |
87 | /* Check if two given pnetids match */ |
88 | static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2) | |
6812baab | 89 | { |
890a2cb4 | 90 | int i; |
6812baab | 91 | |
890a2cb4 HW |
92 | for (i = 0; i < SMC_MAX_PNETID_LEN; i++) { |
93 | if ((pnetid1[i] == 0 || pnetid1[i] == SMC_ASCII_BLANK) && | |
94 | (pnetid2[i] == 0 || pnetid2[i] == SMC_ASCII_BLANK)) | |
6812baab | 95 | break; |
890a2cb4 HW |
96 | if (pnetid1[i] != pnetid2[i]) |
97 | return false; | |
6812baab | 98 | } |
890a2cb4 | 99 | return true; |
6812baab TR |
100 | } |
101 | ||
102 | /* Remove a pnetid from the pnet table. | |
103 | */ | |
104 | static int smc_pnet_remove_by_pnetid(char *pnet_name) | |
105 | { | |
106 | struct smc_pnetentry *pnetelem, *tmp_pe; | |
890a2cb4 | 107 | struct smc_ib_device *ibdev; |
f3d74b22 | 108 | struct smcd_dev *smcd_dev; |
6812baab | 109 | int rc = -ENOENT; |
890a2cb4 | 110 | int ibport; |
6812baab | 111 | |
890a2cb4 | 112 | /* remove netdevices */ |
6812baab TR |
113 | write_lock(&smc_pnettable.lock); |
114 | list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist, | |
115 | list) { | |
890a2cb4 HW |
116 | if (!pnet_name || |
117 | smc_pnet_match(pnetelem->pnet_name, pnet_name)) { | |
6812baab TR |
118 | list_del(&pnetelem->list); |
119 | dev_put(pnetelem->ndev); | |
120 | kfree(pnetelem); | |
121 | rc = 0; | |
6812baab TR |
122 | } |
123 | } | |
124 | write_unlock(&smc_pnettable.lock); | |
890a2cb4 HW |
125 | /* remove ib devices */ |
126 | spin_lock(&smc_ib_devices.lock); | |
127 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { | |
128 | for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) { | |
129 | if (ibdev->pnetid_by_user[ibport] && | |
130 | (!pnet_name || | |
131 | smc_pnet_match(pnet_name, | |
132 | ibdev->pnetid[ibport]))) { | |
133 | memset(ibdev->pnetid[ibport], 0, | |
134 | SMC_MAX_PNETID_LEN); | |
135 | ibdev->pnetid_by_user[ibport] = false; | |
136 | rc = 0; | |
137 | } | |
138 | } | |
139 | } | |
140 | spin_unlock(&smc_ib_devices.lock); | |
f3d74b22 HW |
141 | /* remove smcd devices */ |
142 | spin_lock(&smcd_dev_list.lock); | |
143 | list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) { | |
144 | if (smcd_dev->pnetid_by_user && | |
145 | (!pnet_name || | |
146 | smc_pnet_match(pnet_name, smcd_dev->pnetid))) { | |
147 | memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN); | |
148 | smcd_dev->pnetid_by_user = false; | |
149 | rc = 0; | |
150 | } | |
151 | } | |
152 | spin_unlock(&smcd_dev_list.lock); | |
6812baab TR |
153 | return rc; |
154 | } | |
155 | ||
156 | /* Remove a pnet entry mentioning a given network device from the pnet table. | |
157 | */ | |
158 | static int smc_pnet_remove_by_ndev(struct net_device *ndev) | |
159 | { | |
160 | struct smc_pnetentry *pnetelem, *tmp_pe; | |
161 | int rc = -ENOENT; | |
162 | ||
163 | write_lock(&smc_pnettable.lock); | |
164 | list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist, | |
165 | list) { | |
166 | if (pnetelem->ndev == ndev) { | |
167 | list_del(&pnetelem->list); | |
168 | dev_put(pnetelem->ndev); | |
169 | kfree(pnetelem); | |
170 | rc = 0; | |
171 | break; | |
172 | } | |
173 | } | |
174 | write_unlock(&smc_pnettable.lock); | |
175 | return rc; | |
176 | } | |
177 | ||
890a2cb4 | 178 | /* Append a pnetid to the end of the pnet table if not already on this list. |
6812baab | 179 | */ |
890a2cb4 | 180 | static int smc_pnet_enter(struct smc_user_pnetentry *new_pnetelem) |
6812baab | 181 | { |
890a2cb4 HW |
182 | u8 pnet_null[SMC_MAX_PNETID_LEN] = {0}; |
183 | u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; | |
184 | struct smc_pnetentry *tmp_pnetelem; | |
185 | struct smc_pnetentry *pnetelem; | |
f3d74b22 | 186 | bool new_smcddev = false; |
890a2cb4 HW |
187 | struct net_device *ndev; |
188 | bool new_netdev = true; | |
189 | bool new_ibdev = false; | |
190 | ||
191 | if (new_pnetelem->smcibdev) { | |
192 | struct smc_ib_device *ib_dev = new_pnetelem->smcibdev; | |
193 | int ib_port = new_pnetelem->ib_port; | |
194 | ||
195 | spin_lock(&smc_ib_devices.lock); | |
196 | if (smc_pnet_match(ib_dev->pnetid[ib_port - 1], pnet_null)) { | |
197 | memcpy(ib_dev->pnetid[ib_port - 1], | |
198 | new_pnetelem->pnet_name, SMC_MAX_PNETID_LEN); | |
199 | ib_dev->pnetid_by_user[ib_port - 1] = true; | |
200 | new_ibdev = true; | |
6812baab | 201 | } |
890a2cb4 | 202 | spin_unlock(&smc_ib_devices.lock); |
6812baab | 203 | } |
f3d74b22 HW |
204 | if (new_pnetelem->smcd_dev) { |
205 | struct smcd_dev *smcd_dev = new_pnetelem->smcd_dev; | |
206 | ||
207 | spin_lock(&smcd_dev_list.lock); | |
208 | if (smc_pnet_match(smcd_dev->pnetid, pnet_null)) { | |
209 | memcpy(smcd_dev->pnetid, new_pnetelem->pnet_name, | |
210 | SMC_MAX_PNETID_LEN); | |
211 | smcd_dev->pnetid_by_user = true; | |
212 | new_smcddev = true; | |
213 | } | |
214 | spin_unlock(&smcd_dev_list.lock); | |
215 | } | |
6812baab | 216 | |
890a2cb4 | 217 | if (!new_pnetelem->ndev) |
f3d74b22 | 218 | return (new_ibdev || new_smcddev) ? 0 : -EEXIST; |
890a2cb4 HW |
219 | |
220 | /* check if (base) netdev already has a pnetid. If there is one, we do | |
221 | * not want to add a pnet table entry | |
222 | */ | |
223 | ndev = pnet_find_base_ndev(new_pnetelem->ndev); | |
224 | if (!smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port, | |
225 | ndev_pnetid)) | |
f3d74b22 | 226 | return (new_ibdev || new_smcddev) ? 0 : -EEXIST; |
890a2cb4 HW |
227 | |
228 | /* add a new netdev entry to the pnet table if there isn't one */ | |
229 | tmp_pnetelem = kzalloc(sizeof(*pnetelem), GFP_KERNEL); | |
230 | if (!tmp_pnetelem) | |
231 | return -ENOMEM; | |
232 | memcpy(tmp_pnetelem->pnet_name, new_pnetelem->pnet_name, | |
233 | SMC_MAX_PNETID_LEN); | |
234 | tmp_pnetelem->ndev = new_pnetelem->ndev; | |
6812baab TR |
235 | |
236 | write_lock(&smc_pnettable.lock); | |
237 | list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) { | |
890a2cb4 HW |
238 | if (pnetelem->ndev == new_pnetelem->ndev) |
239 | new_netdev = false; | |
6812baab | 240 | } |
890a2cb4 HW |
241 | if (new_netdev) { |
242 | dev_hold(tmp_pnetelem->ndev); | |
243 | list_add_tail(&tmp_pnetelem->list, &smc_pnettable.pnetlist); | |
244 | write_unlock(&smc_pnettable.lock); | |
245 | } else { | |
246 | write_unlock(&smc_pnettable.lock); | |
247 | kfree(tmp_pnetelem); | |
248 | } | |
249 | ||
f3d74b22 | 250 | return (new_netdev || new_ibdev || new_smcddev) ? 0 : -EEXIST; |
6812baab TR |
251 | } |
252 | ||
253 | /* The limit for pnetid is 16 characters. | |
254 | * Valid characters should be (single-byte character set) a-z, A-Z, 0-9. | |
255 | * Lower case letters are converted to upper case. | |
256 | * Interior blanks should not be used. | |
257 | */ | |
258 | static bool smc_pnetid_valid(const char *pnet_name, char *pnetid) | |
259 | { | |
260 | char *bf = skip_spaces(pnet_name); | |
261 | size_t len = strlen(bf); | |
262 | char *end = bf + len; | |
263 | ||
264 | if (!len) | |
265 | return false; | |
266 | while (--end >= bf && isspace(*end)) | |
267 | ; | |
0afff91c | 268 | if (end - bf >= SMC_MAX_PNETID_LEN) |
6812baab TR |
269 | return false; |
270 | while (bf <= end) { | |
271 | if (!isalnum(*bf)) | |
272 | return false; | |
273 | *pnetid++ = islower(*bf) ? toupper(*bf) : *bf; | |
274 | bf++; | |
275 | } | |
276 | *pnetid = '\0'; | |
277 | return true; | |
278 | } | |
279 | ||
280 | /* Find an infiniband device by a given name. The device might not exist. */ | |
249633a4 | 281 | static struct smc_ib_device *smc_pnet_find_ib(char *ib_name) |
6812baab TR |
282 | { |
283 | struct smc_ib_device *ibdev; | |
284 | ||
285 | spin_lock(&smc_ib_devices.lock); | |
286 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { | |
287 | if (!strncmp(ibdev->ibdev->name, ib_name, | |
288 | sizeof(ibdev->ibdev->name))) { | |
289 | goto out; | |
290 | } | |
291 | } | |
292 | ibdev = NULL; | |
293 | out: | |
294 | spin_unlock(&smc_ib_devices.lock); | |
295 | return ibdev; | |
296 | } | |
297 | ||
f3d74b22 HW |
298 | /* Find an smcd device by a given name. The device might not exist. */ |
299 | static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name) | |
300 | { | |
301 | struct smcd_dev *smcd_dev; | |
302 | ||
303 | spin_lock(&smcd_dev_list.lock); | |
304 | list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) { | |
305 | if (!strncmp(dev_name(&smcd_dev->dev), smcd_name, | |
306 | IB_DEVICE_NAME_MAX - 1)) | |
307 | goto out; | |
308 | } | |
309 | smcd_dev = NULL; | |
310 | out: | |
311 | spin_unlock(&smcd_dev_list.lock); | |
312 | return smcd_dev; | |
313 | } | |
314 | ||
6812baab TR |
315 | /* Parse the supplied netlink attributes and fill a pnetentry structure. |
316 | * For ethernet and infiniband device names verify that the devices exist. | |
317 | */ | |
890a2cb4 HW |
318 | static int smc_pnet_fill_entry(struct net *net, |
319 | struct smc_user_pnetentry *pnetelem, | |
6812baab TR |
320 | struct nlattr *tb[]) |
321 | { | |
d49baa7e EB |
322 | char *string, *ibname; |
323 | int rc; | |
6812baab TR |
324 | |
325 | memset(pnetelem, 0, sizeof(*pnetelem)); | |
326 | INIT_LIST_HEAD(&pnetelem->list); | |
d49baa7e EB |
327 | |
328 | rc = -EINVAL; | |
329 | if (!tb[SMC_PNETID_NAME]) | |
330 | goto error; | |
331 | string = (char *)nla_data(tb[SMC_PNETID_NAME]); | |
332 | if (!smc_pnetid_valid(string, pnetelem->pnet_name)) | |
333 | goto error; | |
334 | ||
335 | rc = -EINVAL; | |
890a2cb4 HW |
336 | if (tb[SMC_PNETID_ETHNAME]) { |
337 | string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]); | |
338 | pnetelem->ndev = dev_get_by_name(net, string); | |
339 | if (!pnetelem->ndev) | |
340 | goto error; | |
341 | } | |
d49baa7e EB |
342 | |
343 | rc = -EINVAL; | |
890a2cb4 HW |
344 | if (tb[SMC_PNETID_IBNAME]) { |
345 | ibname = (char *)nla_data(tb[SMC_PNETID_IBNAME]); | |
346 | ibname = strim(ibname); | |
347 | pnetelem->smcibdev = smc_pnet_find_ib(ibname); | |
f3d74b22 HW |
348 | pnetelem->smcd_dev = smc_pnet_find_smcd(ibname); |
349 | if (!pnetelem->smcibdev && !pnetelem->smcd_dev) | |
890a2cb4 HW |
350 | goto error; |
351 | if (pnetelem->smcibdev) { | |
352 | if (!tb[SMC_PNETID_IBPORT]) | |
353 | goto error; | |
354 | pnetelem->ib_port = nla_get_u8(tb[SMC_PNETID_IBPORT]); | |
355 | if (pnetelem->ib_port < 1 || | |
356 | pnetelem->ib_port > SMC_MAX_PORTS) | |
357 | goto error; | |
358 | } | |
359 | } | |
d49baa7e | 360 | |
6812baab TR |
361 | return 0; |
362 | ||
363 | error: | |
364 | if (pnetelem->ndev) | |
365 | dev_put(pnetelem->ndev); | |
366 | return rc; | |
367 | } | |
368 | ||
369 | /* Convert an smc_pnetentry to a netlink attribute sequence */ | |
890a2cb4 HW |
370 | static int smc_pnet_set_nla(struct sk_buff *msg, |
371 | struct smc_user_pnetentry *pnetelem) | |
6812baab | 372 | { |
890a2cb4 | 373 | if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name)) |
6812baab | 374 | return -1; |
890a2cb4 HW |
375 | if (pnetelem->ndev) { |
376 | if (nla_put_string(msg, SMC_PNETID_ETHNAME, | |
377 | pnetelem->ndev->name)) | |
378 | return -1; | |
379 | } else { | |
380 | if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a")) | |
381 | return -1; | |
6812baab | 382 | } |
890a2cb4 HW |
383 | if (pnetelem->smcibdev) { |
384 | if (nla_put_string(msg, SMC_PNETID_IBNAME, | |
385 | pnetelem->smcibdev->ibdev->name) || | |
386 | nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port)) | |
387 | return -1; | |
f3d74b22 HW |
388 | } else if (pnetelem->smcd_dev) { |
389 | if (nla_put_string(msg, SMC_PNETID_IBNAME, | |
390 | dev_name(&pnetelem->smcd_dev->dev)) || | |
391 | nla_put_u8(msg, SMC_PNETID_IBPORT, 1)) | |
392 | return -1; | |
890a2cb4 HW |
393 | } else { |
394 | if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") || | |
395 | nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff)) | |
396 | return -1; | |
6812baab TR |
397 | } |
398 | ||
890a2cb4 | 399 | return 0; |
6812baab TR |
400 | } |
401 | ||
402 | static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info) | |
403 | { | |
404 | struct net *net = genl_info_net(info); | |
890a2cb4 | 405 | struct smc_user_pnetentry pnetelem; |
6812baab TR |
406 | int rc; |
407 | ||
890a2cb4 | 408 | rc = smc_pnet_fill_entry(net, &pnetelem, info->attrs); |
6812baab | 409 | if (!rc) |
890a2cb4 HW |
410 | rc = smc_pnet_enter(&pnetelem); |
411 | if (pnetelem.ndev) | |
412 | dev_put(pnetelem.ndev); | |
6812baab TR |
413 | return rc; |
414 | } | |
415 | ||
416 | static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info) | |
417 | { | |
d49baa7e EB |
418 | if (!info->attrs[SMC_PNETID_NAME]) |
419 | return -EINVAL; | |
6812baab TR |
420 | return smc_pnet_remove_by_pnetid( |
421 | (char *)nla_data(info->attrs[SMC_PNETID_NAME])); | |
422 | } | |
423 | ||
424 | static int smc_pnet_dump_start(struct netlink_callback *cb) | |
425 | { | |
426 | cb->args[0] = 0; | |
427 | return 0; | |
428 | } | |
429 | ||
430 | static int smc_pnet_dumpinfo(struct sk_buff *skb, | |
431 | u32 portid, u32 seq, u32 flags, | |
890a2cb4 | 432 | struct smc_user_pnetentry *pnetelem) |
6812baab TR |
433 | { |
434 | void *hdr; | |
435 | ||
436 | hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family, | |
437 | flags, SMC_PNETID_GET); | |
438 | if (!hdr) | |
439 | return -ENOMEM; | |
440 | if (smc_pnet_set_nla(skb, pnetelem) < 0) { | |
441 | genlmsg_cancel(skb, hdr); | |
442 | return -EMSGSIZE; | |
443 | } | |
444 | genlmsg_end(skb, hdr); | |
445 | return 0; | |
446 | } | |
447 | ||
890a2cb4 HW |
448 | static int _smc_pnet_dump(struct sk_buff *skb, u32 portid, u32 seq, u8 *pnetid, |
449 | int start_idx) | |
6812baab | 450 | { |
890a2cb4 | 451 | struct smc_user_pnetentry tmp_entry; |
6812baab | 452 | struct smc_pnetentry *pnetelem; |
890a2cb4 | 453 | struct smc_ib_device *ibdev; |
f3d74b22 | 454 | struct smcd_dev *smcd_dev; |
6812baab | 455 | int idx = 0; |
890a2cb4 | 456 | int ibport; |
6812baab | 457 | |
890a2cb4 | 458 | /* dump netdevices */ |
6812baab TR |
459 | read_lock(&smc_pnettable.lock); |
460 | list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) { | |
890a2cb4 HW |
461 | if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid)) |
462 | continue; | |
463 | if (idx++ < start_idx) | |
6812baab | 464 | continue; |
890a2cb4 HW |
465 | memset(&tmp_entry, 0, sizeof(tmp_entry)); |
466 | memcpy(&tmp_entry.pnet_name, pnetelem->pnet_name, | |
467 | SMC_MAX_PNETID_LEN); | |
468 | tmp_entry.ndev = pnetelem->ndev; | |
469 | if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI, | |
470 | &tmp_entry)) { | |
6812baab TR |
471 | --idx; |
472 | break; | |
473 | } | |
474 | } | |
6812baab | 475 | read_unlock(&smc_pnettable.lock); |
890a2cb4 HW |
476 | |
477 | /* dump ib devices */ | |
478 | spin_lock(&smc_ib_devices.lock); | |
479 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { | |
480 | for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) { | |
481 | if (ibdev->pnetid_by_user[ibport]) { | |
482 | if (pnetid && | |
483 | !smc_pnet_match(ibdev->pnetid[ibport], | |
484 | pnetid)) | |
485 | continue; | |
486 | if (idx++ < start_idx) | |
487 | continue; | |
488 | memset(&tmp_entry, 0, sizeof(tmp_entry)); | |
489 | memcpy(&tmp_entry.pnet_name, | |
490 | ibdev->pnetid[ibport], | |
491 | SMC_MAX_PNETID_LEN); | |
492 | tmp_entry.smcibdev = ibdev; | |
493 | tmp_entry.ib_port = ibport + 1; | |
494 | if (smc_pnet_dumpinfo(skb, portid, seq, | |
495 | NLM_F_MULTI, | |
496 | &tmp_entry)) { | |
497 | --idx; | |
498 | break; | |
499 | } | |
500 | } | |
501 | } | |
502 | } | |
503 | spin_unlock(&smc_ib_devices.lock); | |
504 | ||
f3d74b22 HW |
505 | /* dump smcd devices */ |
506 | spin_lock(&smcd_dev_list.lock); | |
507 | list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) { | |
508 | if (smcd_dev->pnetid_by_user) { | |
509 | if (pnetid && !smc_pnet_match(smcd_dev->pnetid, pnetid)) | |
510 | continue; | |
511 | if (idx++ < start_idx) | |
512 | continue; | |
513 | memset(&tmp_entry, 0, sizeof(tmp_entry)); | |
514 | memcpy(&tmp_entry.pnet_name, smcd_dev->pnetid, | |
515 | SMC_MAX_PNETID_LEN); | |
516 | tmp_entry.smcd_dev = smcd_dev; | |
517 | if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI, | |
518 | &tmp_entry)) { | |
519 | --idx; | |
520 | break; | |
521 | } | |
522 | } | |
523 | } | |
524 | spin_unlock(&smcd_dev_list.lock); | |
525 | ||
890a2cb4 HW |
526 | return idx; |
527 | } | |
528 | ||
529 | static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
530 | { | |
531 | int idx; | |
532 | ||
533 | idx = _smc_pnet_dump(skb, NETLINK_CB(cb->skb).portid, | |
534 | cb->nlh->nlmsg_seq, NULL, cb->args[0]); | |
535 | ||
536 | cb->args[0] = idx; | |
6812baab TR |
537 | return skb->len; |
538 | } | |
539 | ||
890a2cb4 HW |
540 | /* Retrieve one PNETID entry */ |
541 | static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info) | |
542 | { | |
543 | struct sk_buff *msg; | |
544 | void *hdr; | |
545 | ||
546 | if (!info->attrs[SMC_PNETID_NAME]) | |
547 | return -EINVAL; | |
548 | ||
549 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
550 | if (!msg) | |
551 | return -ENOMEM; | |
552 | ||
553 | _smc_pnet_dump(msg, info->snd_portid, info->snd_seq, | |
554 | nla_data(info->attrs[SMC_PNETID_NAME]), 0); | |
555 | ||
556 | /* finish multi part message and send it */ | |
557 | hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0, | |
558 | NLM_F_MULTI); | |
559 | if (!hdr) { | |
560 | nlmsg_free(msg); | |
561 | return -EMSGSIZE; | |
562 | } | |
563 | return genlmsg_reply(msg, info); | |
564 | } | |
565 | ||
6812baab TR |
566 | /* Remove and delete all pnetids from pnet table. |
567 | */ | |
568 | static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info) | |
569 | { | |
890a2cb4 | 570 | return smc_pnet_remove_by_pnetid(NULL); |
6812baab TR |
571 | } |
572 | ||
573 | /* SMC_PNETID generic netlink operation definition */ | |
574 | static const struct genl_ops smc_pnet_ops[] = { | |
575 | { | |
576 | .cmd = SMC_PNETID_GET, | |
577 | .flags = GENL_ADMIN_PERM, | |
578 | .policy = smc_pnet_policy, | |
579 | .doit = smc_pnet_get, | |
580 | .dumpit = smc_pnet_dump, | |
581 | .start = smc_pnet_dump_start | |
582 | }, | |
583 | { | |
584 | .cmd = SMC_PNETID_ADD, | |
585 | .flags = GENL_ADMIN_PERM, | |
586 | .policy = smc_pnet_policy, | |
587 | .doit = smc_pnet_add | |
588 | }, | |
589 | { | |
590 | .cmd = SMC_PNETID_DEL, | |
591 | .flags = GENL_ADMIN_PERM, | |
592 | .policy = smc_pnet_policy, | |
593 | .doit = smc_pnet_del | |
594 | }, | |
595 | { | |
596 | .cmd = SMC_PNETID_FLUSH, | |
597 | .flags = GENL_ADMIN_PERM, | |
598 | .policy = smc_pnet_policy, | |
599 | .doit = smc_pnet_flush | |
600 | } | |
601 | }; | |
602 | ||
603 | /* SMC_PNETID family definition */ | |
56ce3c5a | 604 | static struct genl_family smc_pnet_nl_family __ro_after_init = { |
6812baab TR |
605 | .hdrsize = 0, |
606 | .name = SMCR_GENL_FAMILY_NAME, | |
607 | .version = SMCR_GENL_FAMILY_VERSION, | |
608 | .maxattr = SMC_PNETID_MAX, | |
609 | .netnsok = true, | |
610 | .module = THIS_MODULE, | |
611 | .ops = smc_pnet_ops, | |
612 | .n_ops = ARRAY_SIZE(smc_pnet_ops) | |
613 | }; | |
614 | ||
615 | static int smc_pnet_netdev_event(struct notifier_block *this, | |
616 | unsigned long event, void *ptr) | |
617 | { | |
618 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); | |
619 | ||
620 | switch (event) { | |
621 | case NETDEV_REBOOT: | |
622 | case NETDEV_UNREGISTER: | |
623 | smc_pnet_remove_by_ndev(event_dev); | |
be6a3f38 | 624 | return NOTIFY_OK; |
6812baab | 625 | default: |
be6a3f38 | 626 | return NOTIFY_DONE; |
6812baab | 627 | } |
6812baab TR |
628 | } |
629 | ||
630 | static struct notifier_block smc_netdev_notifier = { | |
631 | .notifier_call = smc_pnet_netdev_event | |
632 | }; | |
633 | ||
634 | int __init smc_pnet_init(void) | |
635 | { | |
636 | int rc; | |
637 | ||
638 | rc = genl_register_family(&smc_pnet_nl_family); | |
639 | if (rc) | |
640 | return rc; | |
641 | rc = register_netdevice_notifier(&smc_netdev_notifier); | |
642 | if (rc) | |
643 | genl_unregister_family(&smc_pnet_nl_family); | |
644 | return rc; | |
645 | } | |
646 | ||
647 | void smc_pnet_exit(void) | |
648 | { | |
649 | smc_pnet_flush(NULL, NULL); | |
650 | unregister_netdevice_notifier(&smc_netdev_notifier); | |
651 | genl_unregister_family(&smc_pnet_nl_family); | |
652 | } | |
653 | ||
0afff91c UB |
654 | /* Determine one base device for stacked net devices. |
655 | * If the lower device level contains more than one devices | |
656 | * (for instance with bonding slaves), just the first device | |
657 | * is used to reach a base device. | |
6812baab | 658 | */ |
0afff91c | 659 | static struct net_device *pnet_find_base_ndev(struct net_device *ndev) |
6812baab | 660 | { |
0afff91c | 661 | int i, nest_lvl; |
6812baab | 662 | |
0afff91c UB |
663 | rtnl_lock(); |
664 | nest_lvl = dev_get_nest_level(ndev); | |
665 | for (i = 0; i < nest_lvl; i++) { | |
666 | struct list_head *lower = &ndev->adj_list.lower; | |
667 | ||
668 | if (list_empty(lower)) | |
669 | break; | |
670 | lower = lower->next; | |
671 | ndev = netdev_lower_get_next(ndev, &lower); | |
672 | } | |
673 | rtnl_unlock(); | |
674 | return ndev; | |
675 | } | |
676 | ||
890a2cb4 HW |
677 | static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *netdev, |
678 | u8 *pnetid) | |
679 | { | |
680 | struct smc_pnetentry *pnetelem; | |
681 | int rc = -ENOENT; | |
682 | ||
683 | read_lock(&smc_pnettable.lock); | |
684 | list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) { | |
685 | if (netdev == pnetelem->ndev) { | |
686 | /* get pnetid of netdev device */ | |
687 | memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN); | |
688 | rc = 0; | |
689 | break; | |
690 | } | |
691 | } | |
692 | read_unlock(&smc_pnettable.lock); | |
693 | return rc; | |
694 | } | |
695 | ||
0afff91c | 696 | /* Determine the corresponding IB device port based on the hardware PNETID. |
7005ada6 UB |
697 | * Searching stops at the first matching active IB device port with vlan_id |
698 | * configured. | |
0afff91c UB |
699 | */ |
700 | static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev, | |
701 | struct smc_ib_device **smcibdev, | |
7005ada6 UB |
702 | u8 *ibport, unsigned short vlan_id, |
703 | u8 gid[]) | |
0afff91c UB |
704 | { |
705 | u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; | |
706 | struct smc_ib_device *ibdev; | |
707 | int i; | |
708 | ||
709 | ndev = pnet_find_base_ndev(ndev); | |
710 | if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port, | |
890a2cb4 HW |
711 | ndev_pnetid) && |
712 | smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) | |
0afff91c UB |
713 | return; /* pnetid could not be determined */ |
714 | ||
715 | spin_lock(&smc_ib_devices.lock); | |
716 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { | |
717 | for (i = 1; i <= SMC_MAX_PORTS; i++) { | |
7005ada6 UB |
718 | if (!rdma_is_port_valid(ibdev->ibdev, i)) |
719 | continue; | |
890a2cb4 | 720 | if (smc_pnet_match(ibdev->pnetid[i - 1], ndev_pnetid) && |
7005ada6 UB |
721 | smc_ib_port_active(ibdev, i) && |
722 | !smc_ib_determine_gid(ibdev, i, vlan_id, gid, | |
723 | NULL)) { | |
0afff91c UB |
724 | *smcibdev = ibdev; |
725 | *ibport = i; | |
7005ada6 | 726 | goto out; |
0afff91c UB |
727 | } |
728 | } | |
729 | } | |
7005ada6 | 730 | out: |
0afff91c UB |
731 | spin_unlock(&smc_ib_devices.lock); |
732 | } | |
733 | ||
1619f770 HW |
734 | static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev, |
735 | struct smcd_dev **smcismdev) | |
736 | { | |
737 | u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; | |
738 | struct smcd_dev *ismdev; | |
739 | ||
740 | ndev = pnet_find_base_ndev(ndev); | |
741 | if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port, | |
f3d74b22 HW |
742 | ndev_pnetid) && |
743 | smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) | |
1619f770 HW |
744 | return; /* pnetid could not be determined */ |
745 | ||
746 | spin_lock(&smcd_dev_list.lock); | |
747 | list_for_each_entry(ismdev, &smcd_dev_list.list, list) { | |
f3d74b22 | 748 | if (smc_pnet_match(ismdev->pnetid, ndev_pnetid)) { |
1619f770 HW |
749 | *smcismdev = ismdev; |
750 | break; | |
751 | } | |
752 | } | |
753 | spin_unlock(&smcd_dev_list.lock); | |
754 | } | |
755 | ||
0afff91c UB |
756 | /* PNET table analysis for a given sock: |
757 | * determine ib_device and port belonging to used internal TCP socket | |
758 | * ethernet interface. | |
759 | */ | |
760 | void smc_pnet_find_roce_resource(struct sock *sk, | |
7005ada6 UB |
761 | struct smc_ib_device **smcibdev, u8 *ibport, |
762 | unsigned short vlan_id, u8 gid[]) | |
0afff91c UB |
763 | { |
764 | struct dst_entry *dst = sk_dst_get(sk); | |
765 | ||
766 | *smcibdev = NULL; | |
767 | *ibport = 0; | |
768 | ||
769 | if (!dst) | |
770 | goto out; | |
771 | if (!dst->dev) | |
772 | goto out_rel; | |
773 | ||
7005ada6 | 774 | smc_pnet_find_roce_by_pnetid(dst->dev, smcibdev, ibport, vlan_id, gid); |
0afff91c | 775 | |
6812baab TR |
776 | out_rel: |
777 | dst_release(dst); | |
0afff91c UB |
778 | out: |
779 | return; | |
6812baab | 780 | } |
1619f770 HW |
781 | |
782 | void smc_pnet_find_ism_resource(struct sock *sk, struct smcd_dev **smcismdev) | |
783 | { | |
784 | struct dst_entry *dst = sk_dst_get(sk); | |
785 | ||
786 | *smcismdev = NULL; | |
787 | if (!dst) | |
788 | goto out; | |
789 | if (!dst->dev) | |
790 | goto out_rel; | |
791 | ||
1619f770 HW |
792 | smc_pnet_find_ism_by_pnetid(dst->dev, smcismdev); |
793 | ||
794 | out_rel: | |
795 | dst_release(dst); | |
796 | out: | |
797 | return; | |
798 | } |