blob: 67e9d9fde08540fe1e152b85c450a0165eb37747 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Richter6812baa2017-01-09 16:55:15 +01002/*
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>
Ursula Braun92f3cb02020-07-08 17:05:13 +020015#include <linux/mutex.h>
Thomas Richter6812baa2017-01-09 16:55:15 +010016#include <net/netlink.h>
17#include <net/genetlink.h>
18
19#include <uapi/linux/if.h>
20#include <uapi/linux/smc.h>
21
22#include <rdma/ib_verbs.h>
23
Hans Wippel64e28b52019-02-21 13:01:02 +010024#include <net/netns/generic.h>
25#include "smc_netns.h"
26
Thomas Richter6812baa2017-01-09 16:55:15 +010027#include "smc_pnet.h"
28#include "smc_ib.h"
Hans Wippel1619f772018-06-28 19:05:08 +020029#include "smc_ism.h"
Karsten Graulbc36d2f2019-04-12 12:57:26 +020030#include "smc_core.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010031
Ursula Braune888a2e2020-09-26 12:44:26 +020032static struct net_device *__pnet_find_base_ndev(struct net_device *ndev);
Hans Wippel890a2cb2019-02-21 13:01:00 +010033static struct net_device *pnet_find_base_ndev(struct net_device *ndev);
34
Dmitry Vyukov09d03102020-05-25 17:31:58 +020035static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
Thomas Richter6812baa2017-01-09 16:55:15 +010036 [SMC_PNETID_NAME] = {
37 .type = NLA_NUL_STRING,
Hans Wippelca8dc132019-01-30 18:51:01 +010038 .len = SMC_MAX_PNETID_LEN
Thomas Richter6812baa2017-01-09 16:55:15 +010039 },
40 [SMC_PNETID_ETHNAME] = {
41 .type = NLA_NUL_STRING,
42 .len = IFNAMSIZ - 1
43 },
44 [SMC_PNETID_IBNAME] = {
45 .type = NLA_NUL_STRING,
46 .len = IB_DEVICE_NAME_MAX - 1
47 },
48 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
49};
50
51static struct genl_family smc_pnet_nl_family;
52
Karsten Graulfdff7042020-04-29 17:10:37 +020053enum smc_pnet_nametype {
54 SMC_PNET_ETH = 1,
55 SMC_PNET_IB = 2,
Thomas Richter6812baa2017-01-09 16:55:15 +010056};
57
Hans Wippel890a2cb2019-02-21 13:01:00 +010058/* pnet entry stored in pnet table */
59struct smc_pnetentry {
60 struct list_head list;
61 char pnet_name[SMC_MAX_PNETID_LEN + 1];
Karsten Graulfdff7042020-04-29 17:10:37 +020062 enum smc_pnet_nametype type;
63 union {
64 struct {
65 char eth_name[IFNAMSIZ + 1];
66 struct net_device *ndev;
67 };
68 struct {
69 char ib_name[IB_DEVICE_NAME_MAX + 1];
70 u8 ib_port;
71 };
72 };
Hans Wippel890a2cb2019-02-21 13:01:00 +010073};
Thomas Richter6812baa2017-01-09 16:55:15 +010074
Karsten Graula304e292020-09-26 12:44:19 +020075/* Check if the pnetid is set */
Ursula Braund70bf4f2020-09-26 12:44:27 +020076bool smc_pnet_is_pnetid_set(u8 *pnetid)
Karsten Graula304e292020-09-26 12:44:19 +020077{
78 if (pnetid[0] == 0 || pnetid[0] == _S)
79 return false;
80 return true;
81}
82
Hans Wippel890a2cb2019-02-21 13:01:00 +010083/* Check if two given pnetids match */
84static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
Thomas Richter6812baa2017-01-09 16:55:15 +010085{
Hans Wippel890a2cb2019-02-21 13:01:00 +010086 int i;
Thomas Richter6812baa2017-01-09 16:55:15 +010087
Hans Wippel890a2cb2019-02-21 13:01:00 +010088 for (i = 0; i < SMC_MAX_PNETID_LEN; i++) {
Karsten Graula304e292020-09-26 12:44:19 +020089 if ((pnetid1[i] == 0 || pnetid1[i] == _S) &&
90 (pnetid2[i] == 0 || pnetid2[i] == _S))
Thomas Richter6812baa2017-01-09 16:55:15 +010091 break;
Hans Wippel890a2cb2019-02-21 13:01:00 +010092 if (pnetid1[i] != pnetid2[i])
93 return false;
Thomas Richter6812baa2017-01-09 16:55:15 +010094 }
Hans Wippel890a2cb2019-02-21 13:01:00 +010095 return true;
Thomas Richter6812baa2017-01-09 16:55:15 +010096}
97
98/* Remove a pnetid from the pnet table.
99 */
Hans Wippel64e28b52019-02-21 13:01:02 +0100100static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100101{
102 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel64e28b52019-02-21 13:01:02 +0100103 struct smc_pnettable *pnettable;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100104 struct smc_ib_device *ibdev;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100105 struct smcd_dev *smcd_dev;
Hans Wippel64e28b52019-02-21 13:01:02 +0100106 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100107 int rc = -ENOENT;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100108 int ibport;
Thomas Richter6812baa2017-01-09 16:55:15 +0100109
Hans Wippel64e28b52019-02-21 13:01:02 +0100110 /* get pnettable for namespace */
111 sn = net_generic(net, smc_net_id);
112 pnettable = &sn->pnettable;
113
Karsten Graulfdff7042020-04-29 17:10:37 +0200114 /* remove table entry */
Hans Wippel64e28b52019-02-21 13:01:02 +0100115 write_lock(&pnettable->lock);
116 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist,
Thomas Richter6812baa2017-01-09 16:55:15 +0100117 list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100118 if (!pnet_name ||
119 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100120 list_del(&pnetelem->list);
Karsten Graul0a99be42020-05-05 15:01:20 +0200121 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200122 dev_put(pnetelem->ndev);
Karsten Graul0a99be42020-05-05 15:01:20 +0200123 pr_warn_ratelimited("smc: net device %s "
124 "erased user defined "
125 "pnetid %.16s\n",
126 pnetelem->eth_name,
127 pnetelem->pnet_name);
128 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100129 kfree(pnetelem);
130 rc = 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100131 }
132 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100133 write_unlock(&pnettable->lock);
134
135 /* if this is not the initial namespace, stop here */
136 if (net != &init_net)
137 return rc;
138
Hans Wippel890a2cb2019-02-21 13:01:00 +0100139 /* remove ib devices */
Ursula Braun92f3cb02020-07-08 17:05:13 +0200140 mutex_lock(&smc_ib_devices.mutex);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100141 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
142 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
143 if (ibdev->pnetid_by_user[ibport] &&
144 (!pnet_name ||
145 smc_pnet_match(pnet_name,
146 ibdev->pnetid[ibport]))) {
Karsten Graul0a99be42020-05-05 15:01:20 +0200147 pr_warn_ratelimited("smc: ib device %s ibport "
148 "%d erased user defined "
149 "pnetid %.16s\n",
150 ibdev->ibdev->name,
151 ibport + 1,
152 ibdev->pnetid[ibport]);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100153 memset(ibdev->pnetid[ibport], 0,
154 SMC_MAX_PNETID_LEN);
155 ibdev->pnetid_by_user[ibport] = false;
156 rc = 0;
157 }
158 }
159 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200160 mutex_unlock(&smc_ib_devices.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100161 /* remove smcd devices */
Ursula Braun82087c02020-07-08 17:05:14 +0200162 mutex_lock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100163 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
164 if (smcd_dev->pnetid_by_user &&
165 (!pnet_name ||
166 smc_pnet_match(pnet_name, smcd_dev->pnetid))) {
Karsten Graul0a99be42020-05-05 15:01:20 +0200167 pr_warn_ratelimited("smc: smcd device %s "
168 "erased user defined pnetid "
169 "%.16s\n", dev_name(&smcd_dev->dev),
170 smcd_dev->pnetid);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100171 memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN);
172 smcd_dev->pnetid_by_user = false;
173 rc = 0;
174 }
175 }
Ursula Braun82087c02020-07-08 17:05:14 +0200176 mutex_unlock(&smcd_dev_list.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100177 return rc;
178}
179
Karsten Graulfdff7042020-04-29 17:10:37 +0200180/* Add the reference to a given network device to the pnet table.
181 */
182static int smc_pnet_add_by_ndev(struct net_device *ndev)
183{
184 struct smc_pnetentry *pnetelem, *tmp_pe;
185 struct smc_pnettable *pnettable;
186 struct net *net = dev_net(ndev);
187 struct smc_net *sn;
188 int rc = -ENOENT;
189
190 /* get pnettable for namespace */
191 sn = net_generic(net, smc_net_id);
192 pnettable = &sn->pnettable;
193
194 write_lock(&pnettable->lock);
195 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
196 if (pnetelem->type == SMC_PNET_ETH && !pnetelem->ndev &&
197 !strncmp(pnetelem->eth_name, ndev->name, IFNAMSIZ)) {
198 dev_hold(ndev);
199 pnetelem->ndev = ndev;
200 rc = 0;
Karsten Graul0a99be42020-05-05 15:01:20 +0200201 pr_warn_ratelimited("smc: adding net device %s with "
202 "user defined pnetid %.16s\n",
203 pnetelem->eth_name,
204 pnetelem->pnet_name);
Karsten Graulfdff7042020-04-29 17:10:37 +0200205 break;
206 }
207 }
208 write_unlock(&pnettable->lock);
209 return rc;
210}
211
212/* Remove the reference to a given network device from the pnet table.
Thomas Richter6812baa2017-01-09 16:55:15 +0100213 */
214static int smc_pnet_remove_by_ndev(struct net_device *ndev)
215{
216 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel64e28b52019-02-21 13:01:02 +0100217 struct smc_pnettable *pnettable;
218 struct net *net = dev_net(ndev);
219 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100220 int rc = -ENOENT;
221
Hans Wippel64e28b52019-02-21 13:01:02 +0100222 /* get pnettable for namespace */
223 sn = net_generic(net, smc_net_id);
224 pnettable = &sn->pnettable;
225
226 write_lock(&pnettable->lock);
227 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200228 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100229 dev_put(pnetelem->ndev);
Karsten Graulfdff7042020-04-29 17:10:37 +0200230 pnetelem->ndev = NULL;
Thomas Richter6812baa2017-01-09 16:55:15 +0100231 rc = 0;
Karsten Graul0a99be42020-05-05 15:01:20 +0200232 pr_warn_ratelimited("smc: removing net device %s with "
233 "user defined pnetid %.16s\n",
234 pnetelem->eth_name,
235 pnetelem->pnet_name);
Thomas Richter6812baa2017-01-09 16:55:15 +0100236 break;
237 }
238 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100239 write_unlock(&pnettable->lock);
Thomas Richter6812baa2017-01-09 16:55:15 +0100240 return rc;
241}
242
Karsten Graulfdff7042020-04-29 17:10:37 +0200243/* Apply pnetid to ib device when no pnetid is set.
Thomas Richter6812baa2017-01-09 16:55:15 +0100244 */
Karsten Graulfdff7042020-04-29 17:10:37 +0200245static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port,
246 char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100247{
Karsten Graulfdff7042020-04-29 17:10:37 +0200248 bool applied = false;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100249
Ursula Braun92f3cb02020-07-08 17:05:13 +0200250 mutex_lock(&smc_ib_devices.mutex);
Karsten Graula304e292020-09-26 12:44:19 +0200251 if (!smc_pnet_is_pnetid_set(ib_dev->pnetid[ib_port - 1])) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200252 memcpy(ib_dev->pnetid[ib_port - 1], pnet_name,
253 SMC_MAX_PNETID_LEN);
254 ib_dev->pnetid_by_user[ib_port - 1] = true;
255 applied = true;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100256 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200257 mutex_unlock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200258 return applied;
259}
Hans Wippelf3d74b22019-02-21 13:01:01 +0100260
Karsten Graulfdff7042020-04-29 17:10:37 +0200261/* Apply pnetid to smcd device when no pnetid is set.
262 */
263static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name)
264{
Karsten Graulfdff7042020-04-29 17:10:37 +0200265 bool applied = false;
266
Ursula Braun82087c02020-07-08 17:05:14 +0200267 mutex_lock(&smcd_dev_list.mutex);
Karsten Graula304e292020-09-26 12:44:19 +0200268 if (!smc_pnet_is_pnetid_set(smcd_dev->pnetid)) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200269 memcpy(smcd_dev->pnetid, pnet_name, SMC_MAX_PNETID_LEN);
270 smcd_dev->pnetid_by_user = true;
271 applied = true;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100272 }
Ursula Braun82087c02020-07-08 17:05:14 +0200273 mutex_unlock(&smcd_dev_list.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200274 return applied;
Thomas Richter6812baa2017-01-09 16:55:15 +0100275}
276
277/* The limit for pnetid is 16 characters.
278 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
279 * Lower case letters are converted to upper case.
280 * Interior blanks should not be used.
281 */
282static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
283{
284 char *bf = skip_spaces(pnet_name);
285 size_t len = strlen(bf);
286 char *end = bf + len;
287
288 if (!len)
289 return false;
290 while (--end >= bf && isspace(*end))
291 ;
Ursula Braun0afff912018-06-28 19:05:05 +0200292 if (end - bf >= SMC_MAX_PNETID_LEN)
Thomas Richter6812baa2017-01-09 16:55:15 +0100293 return false;
294 while (bf <= end) {
295 if (!isalnum(*bf))
296 return false;
297 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
298 bf++;
299 }
300 *pnetid = '\0';
301 return true;
302}
303
304/* Find an infiniband device by a given name. The device might not exist. */
Ursula Braun249633a2017-04-10 14:57:57 +0200305static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100306{
307 struct smc_ib_device *ibdev;
308
Ursula Braun92f3cb02020-07-08 17:05:13 +0200309 mutex_lock(&smc_ib_devices.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100310 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
311 if (!strncmp(ibdev->ibdev->name, ib_name,
Hans Wippelaf5f60c2019-02-21 13:01:03 +0100312 sizeof(ibdev->ibdev->name)) ||
313 !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name,
314 IB_DEVICE_NAME_MAX - 1)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100315 goto out;
316 }
317 }
318 ibdev = NULL;
319out:
Ursula Braun92f3cb02020-07-08 17:05:13 +0200320 mutex_unlock(&smc_ib_devices.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100321 return ibdev;
322}
323
Hans Wippelf3d74b22019-02-21 13:01:01 +0100324/* Find an smcd device by a given name. The device might not exist. */
325static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name)
326{
327 struct smcd_dev *smcd_dev;
328
Ursula Braun82087c02020-07-08 17:05:14 +0200329 mutex_lock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100330 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
331 if (!strncmp(dev_name(&smcd_dev->dev), smcd_name,
332 IB_DEVICE_NAME_MAX - 1))
333 goto out;
334 }
335 smcd_dev = NULL;
336out:
Ursula Braun82087c02020-07-08 17:05:14 +0200337 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100338 return smcd_dev;
339}
340
Karsten Graulfdff7042020-04-29 17:10:37 +0200341static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
342 char *eth_name, char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100343{
Karsten Graulfdff7042020-04-29 17:10:37 +0200344 struct smc_pnetentry *tmp_pe, *new_pe;
345 struct net_device *ndev, *base_ndev;
346 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
347 bool new_netdev;
Eric Biggersd49baa72018-05-13 17:01:30 -0700348 int rc;
Thomas Richter6812baa2017-01-09 16:55:15 +0100349
Karsten Graulfdff7042020-04-29 17:10:37 +0200350 /* check if (base) netdev already has a pnetid. If there is one, we do
351 * not want to add a pnet table entry
352 */
353 rc = -EEXIST;
354 ndev = dev_get_by_name(net, eth_name); /* dev_hold() */
355 if (ndev) {
356 base_ndev = pnet_find_base_ndev(ndev);
357 if (!smc_pnetid_by_dev_port(base_ndev->dev.parent,
358 base_ndev->dev_port, ndev_pnetid))
359 goto out_put;
360 }
361
362 /* add a new netdev entry to the pnet table if there isn't one */
363 rc = -ENOMEM;
364 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
365 if (!new_pe)
366 goto out_put;
367 new_pe->type = SMC_PNET_ETH;
368 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
369 strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
370 new_pe->ndev = ndev;
371
372 rc = -EEXIST;
373 new_netdev = true;
374 write_lock(&pnettable->lock);
375 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
376 if (tmp_pe->type == SMC_PNET_ETH &&
377 !strncmp(tmp_pe->eth_name, eth_name, IFNAMSIZ)) {
378 new_netdev = false;
379 break;
380 }
381 }
382 if (new_netdev) {
383 list_add_tail(&new_pe->list, &pnettable->pnetlist);
384 write_unlock(&pnettable->lock);
385 } else {
386 write_unlock(&pnettable->lock);
387 kfree(new_pe);
388 goto out_put;
389 }
Karsten Graul0a99be42020-05-05 15:01:20 +0200390 if (ndev)
391 pr_warn_ratelimited("smc: net device %s "
392 "applied user defined pnetid %.16s\n",
393 new_pe->eth_name, new_pe->pnet_name);
Karsten Graulfdff7042020-04-29 17:10:37 +0200394 return 0;
395
396out_put:
Yajun Deng1160dfa2021-08-05 19:55:27 +0800397 dev_put(ndev);
Karsten Graulfdff7042020-04-29 17:10:37 +0200398 return rc;
399}
400
401static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,
402 u8 ib_port, char *pnet_name)
403{
404 struct smc_pnetentry *tmp_pe, *new_pe;
405 struct smc_ib_device *ib_dev;
406 bool smcddev_applied = true;
407 bool ibdev_applied = true;
408 struct smcd_dev *smcd_dev;
409 bool new_ibdev;
410
411 /* try to apply the pnetid to active devices */
412 ib_dev = smc_pnet_find_ib(ib_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200413 if (ib_dev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200414 ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200415 if (ibdev_applied)
416 pr_warn_ratelimited("smc: ib device %s ibport %d "
417 "applied user defined pnetid "
418 "%.16s\n", ib_dev->ibdev->name,
419 ib_port,
420 ib_dev->pnetid[ib_port - 1]);
421 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200422 smcd_dev = smc_pnet_find_smcd(ib_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200423 if (smcd_dev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200424 smcddev_applied = smc_pnet_apply_smcd(smcd_dev, pnet_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200425 if (smcddev_applied)
426 pr_warn_ratelimited("smc: smcd device %s "
427 "applied user defined pnetid "
428 "%.16s\n", dev_name(&smcd_dev->dev),
429 smcd_dev->pnetid);
430 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200431 /* Apply fails when a device has a hardware-defined pnetid set, do not
432 * add a pnet table entry in that case.
433 */
434 if (!ibdev_applied || !smcddev_applied)
435 return -EEXIST;
436
437 /* add a new ib entry to the pnet table if there isn't one */
438 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
439 if (!new_pe)
440 return -ENOMEM;
441 new_pe->type = SMC_PNET_IB;
442 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
443 strncpy(new_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX);
444 new_pe->ib_port = ib_port;
445
446 new_ibdev = true;
447 write_lock(&pnettable->lock);
448 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
449 if (tmp_pe->type == SMC_PNET_IB &&
450 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
451 new_ibdev = false;
452 break;
453 }
454 }
455 if (new_ibdev) {
456 list_add_tail(&new_pe->list, &pnettable->pnetlist);
457 write_unlock(&pnettable->lock);
458 } else {
459 write_unlock(&pnettable->lock);
460 kfree(new_pe);
461 }
462 return (new_ibdev) ? 0 : -EEXIST;
463}
464
465/* Append a pnetid to the end of the pnet table if not already on this list.
466 */
467static int smc_pnet_enter(struct net *net, struct nlattr *tb[])
468{
469 char pnet_name[SMC_MAX_PNETID_LEN + 1];
470 struct smc_pnettable *pnettable;
471 bool new_netdev = false;
472 bool new_ibdev = false;
473 struct smc_net *sn;
474 u8 ibport = 1;
475 char *string;
476 int rc;
477
478 /* get pnettable for namespace */
479 sn = net_generic(net, smc_net_id);
480 pnettable = &sn->pnettable;
Eric Biggersd49baa72018-05-13 17:01:30 -0700481
482 rc = -EINVAL;
483 if (!tb[SMC_PNETID_NAME])
484 goto error;
485 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
Karsten Graulfdff7042020-04-29 17:10:37 +0200486 if (!smc_pnetid_valid(string, pnet_name))
Eric Biggersd49baa72018-05-13 17:01:30 -0700487 goto error;
488
Hans Wippel890a2cb2019-02-21 13:01:00 +0100489 if (tb[SMC_PNETID_ETHNAME]) {
490 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
Karsten Graulfdff7042020-04-29 17:10:37 +0200491 rc = smc_pnet_add_eth(pnettable, net, string, pnet_name);
492 if (!rc)
493 new_netdev = true;
494 else if (rc != -EEXIST)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100495 goto error;
496 }
Eric Biggersd49baa72018-05-13 17:01:30 -0700497
Hans Wippel64e28b52019-02-21 13:01:02 +0100498 /* if this is not the initial namespace, stop here */
499 if (net != &init_net)
Karsten Graulfdff7042020-04-29 17:10:37 +0200500 return new_netdev ? 0 : -EEXIST;
Hans Wippel64e28b52019-02-21 13:01:02 +0100501
Eric Biggersd49baa72018-05-13 17:01:30 -0700502 rc = -EINVAL;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100503 if (tb[SMC_PNETID_IBNAME]) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200504 string = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
505 string = strim(string);
506 if (tb[SMC_PNETID_IBPORT]) {
507 ibport = nla_get_u8(tb[SMC_PNETID_IBPORT]);
508 if (ibport < 1 || ibport > SMC_MAX_PORTS)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100509 goto error;
510 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200511 rc = smc_pnet_add_ib(pnettable, string, ibport, pnet_name);
512 if (!rc)
513 new_ibdev = true;
514 else if (rc != -EEXIST)
515 goto error;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100516 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200517 return (new_netdev || new_ibdev) ? 0 : -EEXIST;
Thomas Richter6812baa2017-01-09 16:55:15 +0100518
519error:
Thomas Richter6812baa2017-01-09 16:55:15 +0100520 return rc;
521}
522
523/* Convert an smc_pnetentry to a netlink attribute sequence */
Hans Wippel890a2cb2019-02-21 13:01:00 +0100524static int smc_pnet_set_nla(struct sk_buff *msg,
Karsten Graulfdff7042020-04-29 17:10:37 +0200525 struct smc_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100526{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100527 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
Thomas Richter6812baa2017-01-09 16:55:15 +0100528 return -1;
Karsten Graulfdff7042020-04-29 17:10:37 +0200529 if (pnetelem->type == SMC_PNET_ETH) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100530 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
Karsten Graulfdff7042020-04-29 17:10:37 +0200531 pnetelem->eth_name))
Hans Wippel890a2cb2019-02-21 13:01:00 +0100532 return -1;
533 } else {
534 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
535 return -1;
536 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200537 if (pnetelem->type == SMC_PNET_IB) {
538 if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) ||
Hans Wippel890a2cb2019-02-21 13:01:00 +0100539 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
540 return -1;
541 } else {
542 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
543 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
544 return -1;
545 }
546
Thomas Richter6812baa2017-01-09 16:55:15 +0100547 return 0;
548}
549
Thomas Richter6812baa2017-01-09 16:55:15 +0100550static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
551{
552 struct net *net = genl_info_net(info);
Thomas Richter6812baa2017-01-09 16:55:15 +0100553
Karsten Graulfdff7042020-04-29 17:10:37 +0200554 return smc_pnet_enter(net, info->attrs);
Thomas Richter6812baa2017-01-09 16:55:15 +0100555}
556
557static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
558{
Hans Wippel64e28b52019-02-21 13:01:02 +0100559 struct net *net = genl_info_net(info);
560
Eric Biggersd49baa72018-05-13 17:01:30 -0700561 if (!info->attrs[SMC_PNETID_NAME])
562 return -EINVAL;
Hans Wippel64e28b52019-02-21 13:01:02 +0100563 return smc_pnet_remove_by_pnetid(net,
Thomas Richter6812baa2017-01-09 16:55:15 +0100564 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
565}
566
567static int smc_pnet_dump_start(struct netlink_callback *cb)
568{
569 cb->args[0] = 0;
570 return 0;
571}
572
573static int smc_pnet_dumpinfo(struct sk_buff *skb,
574 u32 portid, u32 seq, u32 flags,
Karsten Graulfdff7042020-04-29 17:10:37 +0200575 struct smc_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100576{
577 void *hdr;
578
579 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
580 flags, SMC_PNETID_GET);
581 if (!hdr)
582 return -ENOMEM;
583 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
584 genlmsg_cancel(skb, hdr);
585 return -EMSGSIZE;
586 }
587 genlmsg_end(skb, hdr);
588 return 0;
589}
590
Hans Wippel64e28b52019-02-21 13:01:02 +0100591static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid,
592 u32 seq, u8 *pnetid, int start_idx)
Thomas Richter6812baa2017-01-09 16:55:15 +0100593{
Hans Wippel64e28b52019-02-21 13:01:02 +0100594 struct smc_pnettable *pnettable;
Thomas Richter6812baa2017-01-09 16:55:15 +0100595 struct smc_pnetentry *pnetelem;
Hans Wippel64e28b52019-02-21 13:01:02 +0100596 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100597 int idx = 0;
598
Hans Wippel64e28b52019-02-21 13:01:02 +0100599 /* get pnettable for namespace */
600 sn = net_generic(net, smc_net_id);
601 pnettable = &sn->pnettable;
602
Karsten Graulfdff7042020-04-29 17:10:37 +0200603 /* dump pnettable entries */
Hans Wippel64e28b52019-02-21 13:01:02 +0100604 read_lock(&pnettable->lock);
605 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100606 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
Thomas Richter6812baa2017-01-09 16:55:15 +0100607 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100608 if (idx++ < start_idx)
609 continue;
Karsten Graulfdff7042020-04-29 17:10:37 +0200610 /* if this is not the initial namespace, dump only netdev */
611 if (net != &init_net && pnetelem->type != SMC_PNET_ETH)
612 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100613 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
Karsten Graulfdff7042020-04-29 17:10:37 +0200614 pnetelem)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100615 --idx;
616 break;
617 }
618 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100619 read_unlock(&pnettable->lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100620 return idx;
621}
622
623static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
624{
Hans Wippel64e28b52019-02-21 13:01:02 +0100625 struct net *net = sock_net(skb->sk);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100626 int idx;
627
Hans Wippel64e28b52019-02-21 13:01:02 +0100628 idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100629 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
630
631 cb->args[0] = idx;
Thomas Richter6812baa2017-01-09 16:55:15 +0100632 return skb->len;
633}
634
Hans Wippel890a2cb2019-02-21 13:01:00 +0100635/* Retrieve one PNETID entry */
636static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
637{
Hans Wippel64e28b52019-02-21 13:01:02 +0100638 struct net *net = genl_info_net(info);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100639 struct sk_buff *msg;
640 void *hdr;
641
642 if (!info->attrs[SMC_PNETID_NAME])
643 return -EINVAL;
644
645 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
646 if (!msg)
647 return -ENOMEM;
648
Hans Wippel64e28b52019-02-21 13:01:02 +0100649 _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100650 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
651
652 /* finish multi part message and send it */
653 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
654 NLM_F_MULTI);
655 if (!hdr) {
656 nlmsg_free(msg);
657 return -EMSGSIZE;
658 }
659 return genlmsg_reply(msg, info);
660}
661
Thomas Richter6812baa2017-01-09 16:55:15 +0100662/* Remove and delete all pnetids from pnet table.
663 */
664static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
665{
Hans Wippel64e28b52019-02-21 13:01:02 +0100666 struct net *net = genl_info_net(info);
667
Karsten Graul8ef659f2019-04-11 11:17:33 +0200668 smc_pnet_remove_by_pnetid(net, NULL);
669 return 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100670}
671
672/* SMC_PNETID generic netlink operation definition */
673static const struct genl_ops smc_pnet_ops[] = {
674 {
675 .cmd = SMC_PNETID_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200676 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Hans Wippel29237d22020-01-21 01:04:46 +0100677 /* can be retrieved by unprivileged users */
Thomas Richter6812baa2017-01-09 16:55:15 +0100678 .doit = smc_pnet_get,
679 .dumpit = smc_pnet_dump,
680 .start = smc_pnet_dump_start
681 },
682 {
683 .cmd = SMC_PNETID_ADD,
Johannes Bergef6243a2019-04-26 14:07:31 +0200684 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100685 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100686 .doit = smc_pnet_add
687 },
688 {
689 .cmd = SMC_PNETID_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +0200690 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100691 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100692 .doit = smc_pnet_del
693 },
694 {
695 .cmd = SMC_PNETID_FLUSH,
Johannes Bergef6243a2019-04-26 14:07:31 +0200696 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100697 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100698 .doit = smc_pnet_flush
699 }
700};
701
702/* SMC_PNETID family definition */
Johannes Berg56ce3c52018-09-20 09:27:30 +0200703static struct genl_family smc_pnet_nl_family __ro_after_init = {
Thomas Richter6812baa2017-01-09 16:55:15 +0100704 .hdrsize = 0,
705 .name = SMCR_GENL_FAMILY_NAME,
706 .version = SMCR_GENL_FAMILY_VERSION,
707 .maxattr = SMC_PNETID_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100708 .policy = smc_pnet_policy,
Thomas Richter6812baa2017-01-09 16:55:15 +0100709 .netnsok = true,
710 .module = THIS_MODULE,
711 .ops = smc_pnet_ops,
712 .n_ops = ARRAY_SIZE(smc_pnet_ops)
713};
714
Ursula Braune888a2e2020-09-26 12:44:26 +0200715bool smc_pnet_is_ndev_pnetid(struct net *net, u8 *pnetid)
716{
717 struct smc_net *sn = net_generic(net, smc_net_id);
718 struct smc_pnetids_ndev_entry *pe;
719 bool rc = false;
720
721 read_lock(&sn->pnetids_ndev.lock);
722 list_for_each_entry(pe, &sn->pnetids_ndev.list, list) {
723 if (smc_pnet_match(pnetid, pe->pnetid)) {
724 rc = true;
725 goto unlock;
726 }
727 }
728
729unlock:
730 read_unlock(&sn->pnetids_ndev.lock);
731 return rc;
732}
733
734static int smc_pnet_add_pnetid(struct net *net, u8 *pnetid)
735{
736 struct smc_net *sn = net_generic(net, smc_net_id);
737 struct smc_pnetids_ndev_entry *pe, *pi;
738
739 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
740 if (!pe)
741 return -ENOMEM;
742
743 write_lock(&sn->pnetids_ndev.lock);
744 list_for_each_entry(pi, &sn->pnetids_ndev.list, list) {
745 if (smc_pnet_match(pnetid, pe->pnetid)) {
746 refcount_inc(&pi->refcnt);
747 kfree(pe);
748 goto unlock;
749 }
750 }
751 refcount_set(&pe->refcnt, 1);
752 memcpy(pe->pnetid, pnetid, SMC_MAX_PNETID_LEN);
753 list_add_tail(&pe->list, &sn->pnetids_ndev.list);
754
755unlock:
756 write_unlock(&sn->pnetids_ndev.lock);
757 return 0;
758}
759
760static void smc_pnet_remove_pnetid(struct net *net, u8 *pnetid)
761{
762 struct smc_net *sn = net_generic(net, smc_net_id);
763 struct smc_pnetids_ndev_entry *pe, *pe2;
764
765 write_lock(&sn->pnetids_ndev.lock);
766 list_for_each_entry_safe(pe, pe2, &sn->pnetids_ndev.list, list) {
767 if (smc_pnet_match(pnetid, pe->pnetid)) {
768 if (refcount_dec_and_test(&pe->refcnt)) {
769 list_del(&pe->list);
770 kfree(pe);
771 }
772 break;
773 }
774 }
775 write_unlock(&sn->pnetids_ndev.lock);
776}
777
778static void smc_pnet_add_base_pnetid(struct net *net, struct net_device *dev,
779 u8 *ndev_pnetid)
780{
781 struct net_device *base_dev;
782
783 base_dev = __pnet_find_base_ndev(dev);
784 if (base_dev->flags & IFF_UP &&
785 !smc_pnetid_by_dev_port(base_dev->dev.parent, base_dev->dev_port,
786 ndev_pnetid)) {
787 /* add to PNETIDs list */
788 smc_pnet_add_pnetid(net, ndev_pnetid);
789 }
790}
791
792/* create initial list of netdevice pnetids */
793static void smc_pnet_create_pnetids_list(struct net *net)
794{
795 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
796 struct net_device *dev;
797
798 rtnl_lock();
799 for_each_netdev(net, dev)
800 smc_pnet_add_base_pnetid(net, dev, ndev_pnetid);
801 rtnl_unlock();
802}
803
804/* clean up list of netdevice pnetids */
805static void smc_pnet_destroy_pnetids_list(struct net *net)
806{
807 struct smc_net *sn = net_generic(net, smc_net_id);
808 struct smc_pnetids_ndev_entry *pe, *temp_pe;
809
810 write_lock(&sn->pnetids_ndev.lock);
811 list_for_each_entry_safe(pe, temp_pe, &sn->pnetids_ndev.list, list) {
812 list_del(&pe->list);
813 kfree(pe);
814 }
815 write_unlock(&sn->pnetids_ndev.lock);
816}
817
Thomas Richter6812baa2017-01-09 16:55:15 +0100818static int smc_pnet_netdev_event(struct notifier_block *this,
819 unsigned long event, void *ptr)
820{
821 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Ursula Braune888a2e2020-09-26 12:44:26 +0200822 struct net *net = dev_net(event_dev);
823 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
Thomas Richter6812baa2017-01-09 16:55:15 +0100824
825 switch (event) {
826 case NETDEV_REBOOT:
827 case NETDEV_UNREGISTER:
828 smc_pnet_remove_by_ndev(event_dev);
Guvenc Gulce3d453f52020-12-01 20:20:40 +0100829 smc_ib_ndev_change(event_dev, event);
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200830 return NOTIFY_OK;
Karsten Graulfdff7042020-04-29 17:10:37 +0200831 case NETDEV_REGISTER:
832 smc_pnet_add_by_ndev(event_dev);
Guvenc Gulce3d453f52020-12-01 20:20:40 +0100833 smc_ib_ndev_change(event_dev, event);
Karsten Graulfdff7042020-04-29 17:10:37 +0200834 return NOTIFY_OK;
Ursula Braune888a2e2020-09-26 12:44:26 +0200835 case NETDEV_UP:
836 smc_pnet_add_base_pnetid(net, event_dev, ndev_pnetid);
837 return NOTIFY_OK;
838 case NETDEV_DOWN:
839 event_dev = __pnet_find_base_ndev(event_dev);
840 if (!smc_pnetid_by_dev_port(event_dev->dev.parent,
841 event_dev->dev_port, ndev_pnetid)) {
842 /* remove from PNETIDs list */
843 smc_pnet_remove_pnetid(net, ndev_pnetid);
844 }
845 return NOTIFY_OK;
Thomas Richter6812baa2017-01-09 16:55:15 +0100846 default:
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200847 return NOTIFY_DONE;
Thomas Richter6812baa2017-01-09 16:55:15 +0100848 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100849}
850
851static struct notifier_block smc_netdev_notifier = {
852 .notifier_call = smc_pnet_netdev_event
853};
854
Hans Wippel64e28b52019-02-21 13:01:02 +0100855/* init network namespace */
856int smc_pnet_net_init(struct net *net)
857{
858 struct smc_net *sn = net_generic(net, smc_net_id);
859 struct smc_pnettable *pnettable = &sn->pnettable;
Ursula Braune888a2e2020-09-26 12:44:26 +0200860 struct smc_pnetids_ndev *pnetids_ndev = &sn->pnetids_ndev;
Hans Wippel64e28b52019-02-21 13:01:02 +0100861
862 INIT_LIST_HEAD(&pnettable->pnetlist);
863 rwlock_init(&pnettable->lock);
Ursula Braune888a2e2020-09-26 12:44:26 +0200864 INIT_LIST_HEAD(&pnetids_ndev->list);
865 rwlock_init(&pnetids_ndev->lock);
866
867 smc_pnet_create_pnetids_list(net);
Hans Wippel64e28b52019-02-21 13:01:02 +0100868
869 return 0;
870}
871
Thomas Richter6812baa2017-01-09 16:55:15 +0100872int __init smc_pnet_init(void)
873{
874 int rc;
875
876 rc = genl_register_family(&smc_pnet_nl_family);
877 if (rc)
878 return rc;
879 rc = register_netdevice_notifier(&smc_netdev_notifier);
880 if (rc)
881 genl_unregister_family(&smc_pnet_nl_family);
Ursula Braune888a2e2020-09-26 12:44:26 +0200882
Thomas Richter6812baa2017-01-09 16:55:15 +0100883 return rc;
884}
885
Hans Wippel64e28b52019-02-21 13:01:02 +0100886/* exit network namespace */
887void smc_pnet_net_exit(struct net *net)
888{
889 /* flush pnet table */
890 smc_pnet_remove_by_pnetid(net, NULL);
Ursula Braune888a2e2020-09-26 12:44:26 +0200891 smc_pnet_destroy_pnetids_list(net);
Hans Wippel64e28b52019-02-21 13:01:02 +0100892}
893
Thomas Richter6812baa2017-01-09 16:55:15 +0100894void smc_pnet_exit(void)
895{
Thomas Richter6812baa2017-01-09 16:55:15 +0100896 unregister_netdevice_notifier(&smc_netdev_notifier);
897 genl_unregister_family(&smc_pnet_nl_family);
898}
899
Ursula Braune888a2e2020-09-26 12:44:26 +0200900static struct net_device *__pnet_find_base_ndev(struct net_device *ndev)
Thomas Richter6812baa2017-01-09 16:55:15 +0100901{
Ursula Braun0afff912018-06-28 19:05:05 +0200902 int i, nest_lvl;
903
Ursula Braune888a2e2020-09-26 12:44:26 +0200904 ASSERT_RTNL();
Taehee Yoof3b0a182019-10-21 18:47:58 +0000905 nest_lvl = ndev->lower_level;
Ursula Braun0afff912018-06-28 19:05:05 +0200906 for (i = 0; i < nest_lvl; i++) {
907 struct list_head *lower = &ndev->adj_list.lower;
908
909 if (list_empty(lower))
910 break;
911 lower = lower->next;
912 ndev = netdev_lower_get_next(ndev, &lower);
913 }
Ursula Braune888a2e2020-09-26 12:44:26 +0200914 return ndev;
915}
916
917/* Determine one base device for stacked net devices.
918 * If the lower device level contains more than one devices
919 * (for instance with bonding slaves), just the first device
920 * is used to reach a base device.
921 */
922static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
923{
924 rtnl_lock();
925 ndev = __pnet_find_base_ndev(ndev);
Ursula Braun0afff912018-06-28 19:05:05 +0200926 rtnl_unlock();
927 return ndev;
928}
929
Hans Wippel64e28b52019-02-21 13:01:02 +0100930static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100931 u8 *pnetid)
932{
Hans Wippel64e28b52019-02-21 13:01:02 +0100933 struct smc_pnettable *pnettable;
934 struct net *net = dev_net(ndev);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100935 struct smc_pnetentry *pnetelem;
Hans Wippel64e28b52019-02-21 13:01:02 +0100936 struct smc_net *sn;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100937 int rc = -ENOENT;
938
Hans Wippel64e28b52019-02-21 13:01:02 +0100939 /* get pnettable for namespace */
940 sn = net_generic(net, smc_net_id);
941 pnettable = &sn->pnettable;
942
943 read_lock(&pnettable->lock);
944 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200945 if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100946 /* get pnetid of netdev device */
947 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
948 rc = 0;
949 break;
950 }
951 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100952 read_unlock(&pnettable->lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100953 return rc;
954}
955
Karsten Graul24fb6812021-10-16 11:37:48 +0200956static int smc_pnet_determine_gid(struct smc_ib_device *ibdev, int i,
957 struct smc_init_info *ini)
958{
959 if (!ini->check_smcrv2 &&
960 !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->ib_gid, NULL,
961 NULL)) {
962 ini->ib_dev = ibdev;
963 ini->ib_port = i;
964 return 0;
965 }
966 if (ini->check_smcrv2 &&
967 !smc_ib_determine_gid(ibdev, i, ini->vlan_id, ini->smcrv2.ib_gid_v2,
968 NULL, &ini->smcrv2)) {
969 ini->smcrv2.ib_dev_v2 = ibdev;
970 ini->smcrv2.ib_port_v2 = i;
971 return 0;
972 }
973 return -ENODEV;
974}
975
Karsten Graulfdff7042020-04-29 17:10:37 +0200976/* find a roce device for the given pnetid */
977static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id,
Karsten Graul6c868a32020-05-01 12:48:11 +0200978 struct smc_init_info *ini,
979 struct smc_ib_device *known_dev)
Karsten Graulfdff7042020-04-29 17:10:37 +0200980{
981 struct smc_ib_device *ibdev;
982 int i;
983
Ursula Braun92f3cb02020-07-08 17:05:13 +0200984 mutex_lock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200985 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
Karsten Graul6c868a32020-05-01 12:48:11 +0200986 if (ibdev == known_dev)
987 continue;
Karsten Graulfdff7042020-04-29 17:10:37 +0200988 for (i = 1; i <= SMC_MAX_PORTS; i++) {
989 if (!rdma_is_port_valid(ibdev->ibdev, i))
990 continue;
991 if (smc_pnet_match(ibdev->pnetid[i - 1], pnet_id) &&
992 smc_ib_port_active(ibdev, i) &&
Karsten Graul24fb6812021-10-16 11:37:48 +0200993 !test_bit(i - 1, ibdev->ports_going_away)) {
994 if (!smc_pnet_determine_gid(ibdev, i, ini))
995 goto out;
Karsten Graulfdff7042020-04-29 17:10:37 +0200996 }
997 }
998 }
999out:
Ursula Braun92f3cb02020-07-08 17:05:13 +02001000 mutex_unlock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +02001001}
1002
Karsten Graul6c868a32020-05-01 12:48:11 +02001003/* find alternate roce device with same pnet_id and vlan_id */
1004void smc_pnet_find_alt_roce(struct smc_link_group *lgr,
1005 struct smc_init_info *ini,
1006 struct smc_ib_device *known_dev)
1007{
1008 _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev);
1009}
1010
Ursula Braun54903572019-02-28 15:10:08 +01001011/* if handshake network device belongs to a roce device, return its
1012 * IB device and port
1013 */
1014static void smc_pnet_find_rdma_dev(struct net_device *netdev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001015 struct smc_init_info *ini)
Ursula Braun54903572019-02-28 15:10:08 +01001016{
1017 struct smc_ib_device *ibdev;
1018
Ursula Braun92f3cb02020-07-08 17:05:13 +02001019 mutex_lock(&smc_ib_devices.mutex);
Ursula Braun54903572019-02-28 15:10:08 +01001020 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
1021 struct net_device *ndev;
1022 int i;
1023
1024 for (i = 1; i <= SMC_MAX_PORTS; i++) {
1025 if (!rdma_is_port_valid(ibdev->ibdev, i))
1026 continue;
1027 if (!ibdev->ibdev->ops.get_netdev)
1028 continue;
1029 ndev = ibdev->ibdev->ops.get_netdev(ibdev->ibdev, i);
1030 if (!ndev)
1031 continue;
1032 dev_put(ndev);
1033 if (netdev == ndev &&
1034 smc_ib_port_active(ibdev, i) &&
Karsten Graul24fb6812021-10-16 11:37:48 +02001035 !test_bit(i - 1, ibdev->ports_going_away)) {
1036 if (!smc_pnet_determine_gid(ibdev, i, ini))
1037 break;
Ursula Braun54903572019-02-28 15:10:08 +01001038 }
1039 }
1040 }
Ursula Braun92f3cb02020-07-08 17:05:13 +02001041 mutex_unlock(&smc_ib_devices.mutex);
Ursula Braun54903572019-02-28 15:10:08 +01001042}
1043
Ursula Braun0afff912018-06-28 19:05:05 +02001044/* Determine the corresponding IB device port based on the hardware PNETID.
Ursula Braun7005ada2018-07-25 16:35:31 +02001045 * Searching stops at the first matching active IB device port with vlan_id
1046 * configured.
Ursula Braun54903572019-02-28 15:10:08 +01001047 * If nothing found, check pnetid table.
1048 * If nothing found, try to use handshake device
Ursula Braun0afff912018-06-28 19:05:05 +02001049 */
1050static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001051 struct smc_init_info *ini)
Ursula Braun0afff912018-06-28 19:05:05 +02001052{
1053 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
Ursula Braun0afff912018-06-28 19:05:05 +02001054
1055 ndev = pnet_find_base_ndev(ndev);
1056 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippel890a2cb2019-02-21 13:01:00 +01001057 ndev_pnetid) &&
Ursula Braun54903572019-02-28 15:10:08 +01001058 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001059 smc_pnet_find_rdma_dev(ndev, ini);
Ursula Braun0afff912018-06-28 19:05:05 +02001060 return; /* pnetid could not be determined */
Ursula Braun54903572019-02-28 15:10:08 +01001061 }
Karsten Graul6c868a32020-05-01 12:48:11 +02001062 _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL);
Ursula Braun0afff912018-06-28 19:05:05 +02001063}
1064
Hans Wippel1619f772018-06-28 19:05:08 +02001065static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001066 struct smc_init_info *ini)
Hans Wippel1619f772018-06-28 19:05:08 +02001067{
1068 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
1069 struct smcd_dev *ismdev;
1070
1071 ndev = pnet_find_base_ndev(ndev);
1072 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippelf3d74b22019-02-21 13:01:01 +01001073 ndev_pnetid) &&
1074 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
Hans Wippel1619f772018-06-28 19:05:08 +02001075 return; /* pnetid could not be determined */
1076
Ursula Braun82087c02020-07-08 17:05:14 +02001077 mutex_lock(&smcd_dev_list.mutex);
Hans Wippel1619f772018-06-28 19:05:08 +02001078 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
Ursula Braunc3d94942019-10-09 10:07:46 +02001079 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) &&
Ursula Braun7b2977d2020-09-10 18:48:24 +02001080 !ismdev->going_away &&
Ursula Braun3fc64932020-09-26 12:44:23 +02001081 (!ini->ism_peer_gid[0] ||
1082 !smc_ism_cantalk(ini->ism_peer_gid[0], ini->vlan_id,
Ursula Braun7b2977d2020-09-10 18:48:24 +02001083 ismdev))) {
Ursula Braun3fc64932020-09-26 12:44:23 +02001084 ini->ism_dev[0] = ismdev;
Hans Wippel1619f772018-06-28 19:05:08 +02001085 break;
1086 }
1087 }
Ursula Braun82087c02020-07-08 17:05:14 +02001088 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippel1619f772018-06-28 19:05:08 +02001089}
1090
Ursula Braun0afff912018-06-28 19:05:05 +02001091/* PNET table analysis for a given sock:
1092 * determine ib_device and port belonging to used internal TCP socket
1093 * ethernet interface.
1094 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001095void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini)
Ursula Braun0afff912018-06-28 19:05:05 +02001096{
1097 struct dst_entry *dst = sk_dst_get(sk);
1098
Ursula Braun0afff912018-06-28 19:05:05 +02001099 if (!dst)
1100 goto out;
1101 if (!dst->dev)
1102 goto out_rel;
1103
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001104 smc_pnet_find_roce_by_pnetid(dst->dev, ini);
Ursula Braun0afff912018-06-28 19:05:05 +02001105
Thomas Richter6812baa2017-01-09 16:55:15 +01001106out_rel:
1107 dst_release(dst);
Ursula Braun0afff912018-06-28 19:05:05 +02001108out:
1109 return;
Thomas Richter6812baa2017-01-09 16:55:15 +01001110}
Hans Wippel1619f772018-06-28 19:05:08 +02001111
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001112void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini)
Hans Wippel1619f772018-06-28 19:05:08 +02001113{
1114 struct dst_entry *dst = sk_dst_get(sk);
1115
Ursula Braun3fc64932020-09-26 12:44:23 +02001116 ini->ism_dev[0] = NULL;
Hans Wippel1619f772018-06-28 19:05:08 +02001117 if (!dst)
1118 goto out;
1119 if (!dst->dev)
1120 goto out_rel;
1121
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001122 smc_pnet_find_ism_by_pnetid(dst->dev, ini);
Hans Wippel1619f772018-06-28 19:05:08 +02001123
1124out_rel:
1125 dst_release(dst);
1126out:
1127 return;
1128}
Karsten Graulfdff7042020-04-29 17:10:37 +02001129
1130/* Lookup and apply a pnet table entry to the given ib device.
1131 */
1132int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port)
1133{
1134 char *ib_name = smcibdev->ibdev->name;
1135 struct smc_pnettable *pnettable;
1136 struct smc_pnetentry *tmp_pe;
1137 struct smc_net *sn;
1138 int rc = -ENOENT;
1139
1140 /* get pnettable for init namespace */
1141 sn = net_generic(&init_net, smc_net_id);
1142 pnettable = &sn->pnettable;
1143
1144 read_lock(&pnettable->lock);
1145 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1146 if (tmp_pe->type == SMC_PNET_IB &&
1147 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX) &&
1148 tmp_pe->ib_port == ib_port) {
1149 smc_pnet_apply_ib(smcibdev, ib_port, tmp_pe->pnet_name);
1150 rc = 0;
1151 break;
1152 }
1153 }
1154 read_unlock(&pnettable->lock);
1155
1156 return rc;
1157}
1158
1159/* Lookup and apply a pnet table entry to the given smcd device.
1160 */
1161int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev)
1162{
1163 const char *ib_name = dev_name(&smcddev->dev);
1164 struct smc_pnettable *pnettable;
1165 struct smc_pnetentry *tmp_pe;
1166 struct smc_net *sn;
1167 int rc = -ENOENT;
1168
1169 /* get pnettable for init namespace */
1170 sn = net_generic(&init_net, smc_net_id);
1171 pnettable = &sn->pnettable;
1172
1173 read_lock(&pnettable->lock);
1174 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1175 if (tmp_pe->type == SMC_PNET_IB &&
1176 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
1177 smc_pnet_apply_smcd(smcddev, tmp_pe->pnet_name);
1178 rc = 0;
1179 break;
1180 }
1181 }
1182 read_unlock(&pnettable->lock);
1183
1184 return rc;
1185}