Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 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> |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 16 | #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 Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 24 | #include <net/netns/generic.h> |
| 25 | #include "smc_netns.h" |
| 26 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 27 | #include "smc_pnet.h" |
| 28 | #include "smc_ib.h" |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 29 | #include "smc_ism.h" |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 30 | #include "smc_core.h" |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 31 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 32 | static struct net_device *pnet_find_base_ndev(struct net_device *ndev); |
| 33 | |
Dmitry Vyukov | 09d0310 | 2020-05-25 17:31:58 +0200 | [diff] [blame] | 34 | static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 35 | [SMC_PNETID_NAME] = { |
| 36 | .type = NLA_NUL_STRING, |
Hans Wippel | ca8dc13 | 2019-01-30 18:51:01 +0100 | [diff] [blame] | 37 | .len = SMC_MAX_PNETID_LEN |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 38 | }, |
| 39 | [SMC_PNETID_ETHNAME] = { |
| 40 | .type = NLA_NUL_STRING, |
| 41 | .len = IFNAMSIZ - 1 |
| 42 | }, |
| 43 | [SMC_PNETID_IBNAME] = { |
| 44 | .type = NLA_NUL_STRING, |
| 45 | .len = IB_DEVICE_NAME_MAX - 1 |
| 46 | }, |
| 47 | [SMC_PNETID_IBPORT] = { .type = NLA_U8 } |
| 48 | }; |
| 49 | |
| 50 | static struct genl_family smc_pnet_nl_family; |
| 51 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 52 | enum smc_pnet_nametype { |
| 53 | SMC_PNET_ETH = 1, |
| 54 | SMC_PNET_IB = 2, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 57 | /* pnet entry stored in pnet table */ |
| 58 | struct smc_pnetentry { |
| 59 | struct list_head list; |
| 60 | char pnet_name[SMC_MAX_PNETID_LEN + 1]; |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 61 | enum smc_pnet_nametype type; |
| 62 | union { |
| 63 | struct { |
| 64 | char eth_name[IFNAMSIZ + 1]; |
| 65 | struct net_device *ndev; |
| 66 | }; |
| 67 | struct { |
| 68 | char ib_name[IB_DEVICE_NAME_MAX + 1]; |
| 69 | u8 ib_port; |
| 70 | }; |
| 71 | }; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 72 | }; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 73 | |
Karsten Graul | a304e29 | 2020-09-26 12:44:19 +0200 | [diff] [blame] | 74 | /* Check if the pnetid is set */ |
| 75 | static bool smc_pnet_is_pnetid_set(u8 *pnetid) |
| 76 | { |
| 77 | if (pnetid[0] == 0 || pnetid[0] == _S) |
| 78 | return false; |
| 79 | return true; |
| 80 | } |
| 81 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 82 | /* Check if two given pnetids match */ |
| 83 | static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 84 | { |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 85 | int i; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 86 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 87 | for (i = 0; i < SMC_MAX_PNETID_LEN; i++) { |
Karsten Graul | a304e29 | 2020-09-26 12:44:19 +0200 | [diff] [blame] | 88 | if ((pnetid1[i] == 0 || pnetid1[i] == _S) && |
| 89 | (pnetid2[i] == 0 || pnetid2[i] == _S)) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 90 | break; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 91 | if (pnetid1[i] != pnetid2[i]) |
| 92 | return false; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 93 | } |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 94 | return true; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Remove a pnetid from the pnet table. |
| 98 | */ |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 99 | static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 100 | { |
| 101 | struct smc_pnetentry *pnetelem, *tmp_pe; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 102 | struct smc_pnettable *pnettable; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 103 | struct smc_ib_device *ibdev; |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 104 | struct smcd_dev *smcd_dev; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 105 | struct smc_net *sn; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 106 | int rc = -ENOENT; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 107 | int ibport; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 108 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 109 | /* get pnettable for namespace */ |
| 110 | sn = net_generic(net, smc_net_id); |
| 111 | pnettable = &sn->pnettable; |
| 112 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 113 | /* remove table entry */ |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 114 | write_lock(&pnettable->lock); |
| 115 | list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 116 | list) { |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 117 | if (!pnet_name || |
| 118 | smc_pnet_match(pnetelem->pnet_name, pnet_name)) { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 119 | list_del(&pnetelem->list); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 120 | if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 121 | dev_put(pnetelem->ndev); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 122 | pr_warn_ratelimited("smc: net device %s " |
| 123 | "erased user defined " |
| 124 | "pnetid %.16s\n", |
| 125 | pnetelem->eth_name, |
| 126 | pnetelem->pnet_name); |
| 127 | } |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 128 | kfree(pnetelem); |
| 129 | rc = 0; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 132 | write_unlock(&pnettable->lock); |
| 133 | |
| 134 | /* if this is not the initial namespace, stop here */ |
| 135 | if (net != &init_net) |
| 136 | return rc; |
| 137 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 138 | /* remove ib devices */ |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 139 | mutex_lock(&smc_ib_devices.mutex); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 140 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { |
| 141 | for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) { |
| 142 | if (ibdev->pnetid_by_user[ibport] && |
| 143 | (!pnet_name || |
| 144 | smc_pnet_match(pnet_name, |
| 145 | ibdev->pnetid[ibport]))) { |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 146 | pr_warn_ratelimited("smc: ib device %s ibport " |
| 147 | "%d erased user defined " |
| 148 | "pnetid %.16s\n", |
| 149 | ibdev->ibdev->name, |
| 150 | ibport + 1, |
| 151 | ibdev->pnetid[ibport]); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 152 | memset(ibdev->pnetid[ibport], 0, |
| 153 | SMC_MAX_PNETID_LEN); |
| 154 | ibdev->pnetid_by_user[ibport] = false; |
| 155 | rc = 0; |
| 156 | } |
| 157 | } |
| 158 | } |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 159 | mutex_unlock(&smc_ib_devices.mutex); |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 160 | /* remove smcd devices */ |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 161 | mutex_lock(&smcd_dev_list.mutex); |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 162 | list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) { |
| 163 | if (smcd_dev->pnetid_by_user && |
| 164 | (!pnet_name || |
| 165 | smc_pnet_match(pnet_name, smcd_dev->pnetid))) { |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 166 | pr_warn_ratelimited("smc: smcd device %s " |
| 167 | "erased user defined pnetid " |
| 168 | "%.16s\n", dev_name(&smcd_dev->dev), |
| 169 | smcd_dev->pnetid); |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 170 | memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN); |
| 171 | smcd_dev->pnetid_by_user = false; |
| 172 | rc = 0; |
| 173 | } |
| 174 | } |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 175 | mutex_unlock(&smcd_dev_list.mutex); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 176 | return rc; |
| 177 | } |
| 178 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 179 | /* Add the reference to a given network device to the pnet table. |
| 180 | */ |
| 181 | static int smc_pnet_add_by_ndev(struct net_device *ndev) |
| 182 | { |
| 183 | struct smc_pnetentry *pnetelem, *tmp_pe; |
| 184 | struct smc_pnettable *pnettable; |
| 185 | struct net *net = dev_net(ndev); |
| 186 | struct smc_net *sn; |
| 187 | int rc = -ENOENT; |
| 188 | |
| 189 | /* get pnettable for namespace */ |
| 190 | sn = net_generic(net, smc_net_id); |
| 191 | pnettable = &sn->pnettable; |
| 192 | |
| 193 | write_lock(&pnettable->lock); |
| 194 | list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) { |
| 195 | if (pnetelem->type == SMC_PNET_ETH && !pnetelem->ndev && |
| 196 | !strncmp(pnetelem->eth_name, ndev->name, IFNAMSIZ)) { |
| 197 | dev_hold(ndev); |
| 198 | pnetelem->ndev = ndev; |
| 199 | rc = 0; |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 200 | pr_warn_ratelimited("smc: adding net device %s with " |
| 201 | "user defined pnetid %.16s\n", |
| 202 | pnetelem->eth_name, |
| 203 | pnetelem->pnet_name); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | write_unlock(&pnettable->lock); |
| 208 | return rc; |
| 209 | } |
| 210 | |
| 211 | /* Remove the reference to a given network device from the pnet table. |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 212 | */ |
| 213 | static int smc_pnet_remove_by_ndev(struct net_device *ndev) |
| 214 | { |
| 215 | struct smc_pnetentry *pnetelem, *tmp_pe; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 216 | struct smc_pnettable *pnettable; |
| 217 | struct net *net = dev_net(ndev); |
| 218 | struct smc_net *sn; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 219 | int rc = -ENOENT; |
| 220 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 221 | /* get pnettable for namespace */ |
| 222 | sn = net_generic(net, smc_net_id); |
| 223 | pnettable = &sn->pnettable; |
| 224 | |
| 225 | write_lock(&pnettable->lock); |
| 226 | list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 227 | if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 228 | dev_put(pnetelem->ndev); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 229 | pnetelem->ndev = NULL; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 230 | rc = 0; |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 231 | pr_warn_ratelimited("smc: removing net device %s with " |
| 232 | "user defined pnetid %.16s\n", |
| 233 | pnetelem->eth_name, |
| 234 | pnetelem->pnet_name); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | } |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 238 | write_unlock(&pnettable->lock); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 239 | return rc; |
| 240 | } |
| 241 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 242 | /* Apply pnetid to ib device when no pnetid is set. |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 243 | */ |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 244 | static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port, |
| 245 | char *pnet_name) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 246 | { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 247 | bool applied = false; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 248 | |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 249 | mutex_lock(&smc_ib_devices.mutex); |
Karsten Graul | a304e29 | 2020-09-26 12:44:19 +0200 | [diff] [blame] | 250 | if (!smc_pnet_is_pnetid_set(ib_dev->pnetid[ib_port - 1])) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 251 | memcpy(ib_dev->pnetid[ib_port - 1], pnet_name, |
| 252 | SMC_MAX_PNETID_LEN); |
| 253 | ib_dev->pnetid_by_user[ib_port - 1] = true; |
| 254 | applied = true; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 255 | } |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 256 | mutex_unlock(&smc_ib_devices.mutex); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 257 | return applied; |
| 258 | } |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 259 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 260 | /* Apply pnetid to smcd device when no pnetid is set. |
| 261 | */ |
| 262 | static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name) |
| 263 | { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 264 | bool applied = false; |
| 265 | |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 266 | mutex_lock(&smcd_dev_list.mutex); |
Karsten Graul | a304e29 | 2020-09-26 12:44:19 +0200 | [diff] [blame] | 267 | if (!smc_pnet_is_pnetid_set(smcd_dev->pnetid)) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 268 | memcpy(smcd_dev->pnetid, pnet_name, SMC_MAX_PNETID_LEN); |
| 269 | smcd_dev->pnetid_by_user = true; |
| 270 | applied = true; |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 271 | } |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 272 | mutex_unlock(&smcd_dev_list.mutex); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 273 | return applied; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* The limit for pnetid is 16 characters. |
| 277 | * Valid characters should be (single-byte character set) a-z, A-Z, 0-9. |
| 278 | * Lower case letters are converted to upper case. |
| 279 | * Interior blanks should not be used. |
| 280 | */ |
| 281 | static bool smc_pnetid_valid(const char *pnet_name, char *pnetid) |
| 282 | { |
| 283 | char *bf = skip_spaces(pnet_name); |
| 284 | size_t len = strlen(bf); |
| 285 | char *end = bf + len; |
| 286 | |
| 287 | if (!len) |
| 288 | return false; |
| 289 | while (--end >= bf && isspace(*end)) |
| 290 | ; |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 291 | if (end - bf >= SMC_MAX_PNETID_LEN) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 292 | return false; |
| 293 | while (bf <= end) { |
| 294 | if (!isalnum(*bf)) |
| 295 | return false; |
| 296 | *pnetid++ = islower(*bf) ? toupper(*bf) : *bf; |
| 297 | bf++; |
| 298 | } |
| 299 | *pnetid = '\0'; |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | /* Find an infiniband device by a given name. The device might not exist. */ |
Ursula Braun | 249633a | 2017-04-10 14:57:57 +0200 | [diff] [blame] | 304 | static struct smc_ib_device *smc_pnet_find_ib(char *ib_name) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 305 | { |
| 306 | struct smc_ib_device *ibdev; |
| 307 | |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 308 | mutex_lock(&smc_ib_devices.mutex); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 309 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { |
| 310 | if (!strncmp(ibdev->ibdev->name, ib_name, |
Hans Wippel | af5f60c | 2019-02-21 13:01:03 +0100 | [diff] [blame] | 311 | sizeof(ibdev->ibdev->name)) || |
| 312 | !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name, |
| 313 | IB_DEVICE_NAME_MAX - 1)) { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 314 | goto out; |
| 315 | } |
| 316 | } |
| 317 | ibdev = NULL; |
| 318 | out: |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 319 | mutex_unlock(&smc_ib_devices.mutex); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 320 | return ibdev; |
| 321 | } |
| 322 | |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 323 | /* Find an smcd device by a given name. The device might not exist. */ |
| 324 | static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name) |
| 325 | { |
| 326 | struct smcd_dev *smcd_dev; |
| 327 | |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 328 | mutex_lock(&smcd_dev_list.mutex); |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 329 | list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) { |
| 330 | if (!strncmp(dev_name(&smcd_dev->dev), smcd_name, |
| 331 | IB_DEVICE_NAME_MAX - 1)) |
| 332 | goto out; |
| 333 | } |
| 334 | smcd_dev = NULL; |
| 335 | out: |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 336 | mutex_unlock(&smcd_dev_list.mutex); |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 337 | return smcd_dev; |
| 338 | } |
| 339 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 340 | static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net, |
| 341 | char *eth_name, char *pnet_name) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 342 | { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 343 | struct smc_pnetentry *tmp_pe, *new_pe; |
| 344 | struct net_device *ndev, *base_ndev; |
| 345 | u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; |
| 346 | bool new_netdev; |
Eric Biggers | d49baa7 | 2018-05-13 17:01:30 -0700 | [diff] [blame] | 347 | int rc; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 348 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 349 | /* check if (base) netdev already has a pnetid. If there is one, we do |
| 350 | * not want to add a pnet table entry |
| 351 | */ |
| 352 | rc = -EEXIST; |
| 353 | ndev = dev_get_by_name(net, eth_name); /* dev_hold() */ |
| 354 | if (ndev) { |
| 355 | base_ndev = pnet_find_base_ndev(ndev); |
| 356 | if (!smc_pnetid_by_dev_port(base_ndev->dev.parent, |
| 357 | base_ndev->dev_port, ndev_pnetid)) |
| 358 | goto out_put; |
| 359 | } |
| 360 | |
| 361 | /* add a new netdev entry to the pnet table if there isn't one */ |
| 362 | rc = -ENOMEM; |
| 363 | new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL); |
| 364 | if (!new_pe) |
| 365 | goto out_put; |
| 366 | new_pe->type = SMC_PNET_ETH; |
| 367 | memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN); |
| 368 | strncpy(new_pe->eth_name, eth_name, IFNAMSIZ); |
| 369 | new_pe->ndev = ndev; |
| 370 | |
| 371 | rc = -EEXIST; |
| 372 | new_netdev = true; |
| 373 | write_lock(&pnettable->lock); |
| 374 | list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { |
| 375 | if (tmp_pe->type == SMC_PNET_ETH && |
| 376 | !strncmp(tmp_pe->eth_name, eth_name, IFNAMSIZ)) { |
| 377 | new_netdev = false; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | if (new_netdev) { |
| 382 | list_add_tail(&new_pe->list, &pnettable->pnetlist); |
| 383 | write_unlock(&pnettable->lock); |
| 384 | } else { |
| 385 | write_unlock(&pnettable->lock); |
| 386 | kfree(new_pe); |
| 387 | goto out_put; |
| 388 | } |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 389 | if (ndev) |
| 390 | pr_warn_ratelimited("smc: net device %s " |
| 391 | "applied user defined pnetid %.16s\n", |
| 392 | new_pe->eth_name, new_pe->pnet_name); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 393 | return 0; |
| 394 | |
| 395 | out_put: |
| 396 | if (ndev) |
| 397 | dev_put(ndev); |
| 398 | return rc; |
| 399 | } |
| 400 | |
| 401 | static 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 Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 413 | if (ib_dev) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 414 | ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 415 | 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 Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 422 | smcd_dev = smc_pnet_find_smcd(ib_name); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 423 | if (smcd_dev) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 424 | smcddev_applied = smc_pnet_apply_smcd(smcd_dev, pnet_name); |
Karsten Graul | 0a99be4 | 2020-05-05 15:01:20 +0200 | [diff] [blame] | 425 | 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 Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 431 | /* 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 | */ |
| 467 | static 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 Biggers | d49baa7 | 2018-05-13 17:01:30 -0700 | [diff] [blame] | 481 | |
| 482 | rc = -EINVAL; |
| 483 | if (!tb[SMC_PNETID_NAME]) |
| 484 | goto error; |
| 485 | string = (char *)nla_data(tb[SMC_PNETID_NAME]); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 486 | if (!smc_pnetid_valid(string, pnet_name)) |
Eric Biggers | d49baa7 | 2018-05-13 17:01:30 -0700 | [diff] [blame] | 487 | goto error; |
| 488 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 489 | if (tb[SMC_PNETID_ETHNAME]) { |
| 490 | string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 491 | rc = smc_pnet_add_eth(pnettable, net, string, pnet_name); |
| 492 | if (!rc) |
| 493 | new_netdev = true; |
| 494 | else if (rc != -EEXIST) |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 495 | goto error; |
| 496 | } |
Eric Biggers | d49baa7 | 2018-05-13 17:01:30 -0700 | [diff] [blame] | 497 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 498 | /* if this is not the initial namespace, stop here */ |
| 499 | if (net != &init_net) |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 500 | return new_netdev ? 0 : -EEXIST; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 501 | |
Eric Biggers | d49baa7 | 2018-05-13 17:01:30 -0700 | [diff] [blame] | 502 | rc = -EINVAL; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 503 | if (tb[SMC_PNETID_IBNAME]) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 504 | 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 Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 509 | goto error; |
| 510 | } |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 511 | 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 Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 516 | } |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 517 | return (new_netdev || new_ibdev) ? 0 : -EEXIST; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 518 | |
| 519 | error: |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 520 | return rc; |
| 521 | } |
| 522 | |
| 523 | /* Convert an smc_pnetentry to a netlink attribute sequence */ |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 524 | static int smc_pnet_set_nla(struct sk_buff *msg, |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 525 | struct smc_pnetentry *pnetelem) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 526 | { |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 527 | if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name)) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 528 | return -1; |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 529 | if (pnetelem->type == SMC_PNET_ETH) { |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 530 | if (nla_put_string(msg, SMC_PNETID_ETHNAME, |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 531 | pnetelem->eth_name)) |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 532 | return -1; |
| 533 | } else { |
| 534 | if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a")) |
| 535 | return -1; |
| 536 | } |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 537 | if (pnetelem->type == SMC_PNET_IB) { |
| 538 | if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) || |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 539 | 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 Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 550 | static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info) |
| 551 | { |
| 552 | struct net *net = genl_info_net(info); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 553 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 554 | return smc_pnet_enter(net, info->attrs); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info) |
| 558 | { |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 559 | struct net *net = genl_info_net(info); |
| 560 | |
Eric Biggers | d49baa7 | 2018-05-13 17:01:30 -0700 | [diff] [blame] | 561 | if (!info->attrs[SMC_PNETID_NAME]) |
| 562 | return -EINVAL; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 563 | return smc_pnet_remove_by_pnetid(net, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 564 | (char *)nla_data(info->attrs[SMC_PNETID_NAME])); |
| 565 | } |
| 566 | |
| 567 | static int smc_pnet_dump_start(struct netlink_callback *cb) |
| 568 | { |
| 569 | cb->args[0] = 0; |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int smc_pnet_dumpinfo(struct sk_buff *skb, |
| 574 | u32 portid, u32 seq, u32 flags, |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 575 | struct smc_pnetentry *pnetelem) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 576 | { |
| 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 Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 591 | static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid, |
| 592 | u32 seq, u8 *pnetid, int start_idx) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 593 | { |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 594 | struct smc_pnettable *pnettable; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 595 | struct smc_pnetentry *pnetelem; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 596 | struct smc_net *sn; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 597 | int idx = 0; |
| 598 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 599 | /* get pnettable for namespace */ |
| 600 | sn = net_generic(net, smc_net_id); |
| 601 | pnettable = &sn->pnettable; |
| 602 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 603 | /* dump pnettable entries */ |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 604 | read_lock(&pnettable->lock); |
| 605 | list_for_each_entry(pnetelem, &pnettable->pnetlist, list) { |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 606 | if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid)) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 607 | continue; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 608 | if (idx++ < start_idx) |
| 609 | continue; |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 610 | /* if this is not the initial namespace, dump only netdev */ |
| 611 | if (net != &init_net && pnetelem->type != SMC_PNET_ETH) |
| 612 | continue; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 613 | if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI, |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 614 | pnetelem)) { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 615 | --idx; |
| 616 | break; |
| 617 | } |
| 618 | } |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 619 | read_unlock(&pnettable->lock); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 620 | return idx; |
| 621 | } |
| 622 | |
| 623 | static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 624 | { |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 625 | struct net *net = sock_net(skb->sk); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 626 | int idx; |
| 627 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 628 | idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid, |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 629 | cb->nlh->nlmsg_seq, NULL, cb->args[0]); |
| 630 | |
| 631 | cb->args[0] = idx; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 632 | return skb->len; |
| 633 | } |
| 634 | |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 635 | /* Retrieve one PNETID entry */ |
| 636 | static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info) |
| 637 | { |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 638 | struct net *net = genl_info_net(info); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 639 | 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 Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 649 | _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq, |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 650 | 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 Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 662 | /* Remove and delete all pnetids from pnet table. |
| 663 | */ |
| 664 | static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info) |
| 665 | { |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 666 | struct net *net = genl_info_net(info); |
| 667 | |
Karsten Graul | 8ef659f | 2019-04-11 11:17:33 +0200 | [diff] [blame] | 668 | smc_pnet_remove_by_pnetid(net, NULL); |
| 669 | return 0; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* SMC_PNETID generic netlink operation definition */ |
| 673 | static const struct genl_ops smc_pnet_ops[] = { |
| 674 | { |
| 675 | .cmd = SMC_PNETID_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 676 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Hans Wippel | 29237d2 | 2020-01-21 01:04:46 +0100 | [diff] [blame] | 677 | /* can be retrieved by unprivileged users */ |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 678 | .doit = smc_pnet_get, |
| 679 | .dumpit = smc_pnet_dump, |
| 680 | .start = smc_pnet_dump_start |
| 681 | }, |
| 682 | { |
| 683 | .cmd = SMC_PNETID_ADD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 684 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 685 | .flags = GENL_ADMIN_PERM, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 686 | .doit = smc_pnet_add |
| 687 | }, |
| 688 | { |
| 689 | .cmd = SMC_PNETID_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 690 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 691 | .flags = GENL_ADMIN_PERM, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 692 | .doit = smc_pnet_del |
| 693 | }, |
| 694 | { |
| 695 | .cmd = SMC_PNETID_FLUSH, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 696 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 697 | .flags = GENL_ADMIN_PERM, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 698 | .doit = smc_pnet_flush |
| 699 | } |
| 700 | }; |
| 701 | |
| 702 | /* SMC_PNETID family definition */ |
Johannes Berg | 56ce3c5 | 2018-09-20 09:27:30 +0200 | [diff] [blame] | 703 | static struct genl_family smc_pnet_nl_family __ro_after_init = { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 704 | .hdrsize = 0, |
| 705 | .name = SMCR_GENL_FAMILY_NAME, |
| 706 | .version = SMCR_GENL_FAMILY_VERSION, |
| 707 | .maxattr = SMC_PNETID_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 708 | .policy = smc_pnet_policy, |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 709 | .netnsok = true, |
| 710 | .module = THIS_MODULE, |
| 711 | .ops = smc_pnet_ops, |
| 712 | .n_ops = ARRAY_SIZE(smc_pnet_ops) |
| 713 | }; |
| 714 | |
| 715 | static int smc_pnet_netdev_event(struct notifier_block *this, |
| 716 | unsigned long event, void *ptr) |
| 717 | { |
| 718 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); |
| 719 | |
| 720 | switch (event) { |
| 721 | case NETDEV_REBOOT: |
| 722 | case NETDEV_UNREGISTER: |
| 723 | smc_pnet_remove_by_ndev(event_dev); |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 724 | return NOTIFY_OK; |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 725 | case NETDEV_REGISTER: |
| 726 | smc_pnet_add_by_ndev(event_dev); |
| 727 | return NOTIFY_OK; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 728 | default: |
Ursula Braun | be6a3f3 | 2018-06-28 19:05:04 +0200 | [diff] [blame] | 729 | return NOTIFY_DONE; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 730 | } |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static struct notifier_block smc_netdev_notifier = { |
| 734 | .notifier_call = smc_pnet_netdev_event |
| 735 | }; |
| 736 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 737 | /* init network namespace */ |
| 738 | int smc_pnet_net_init(struct net *net) |
| 739 | { |
| 740 | struct smc_net *sn = net_generic(net, smc_net_id); |
| 741 | struct smc_pnettable *pnettable = &sn->pnettable; |
| 742 | |
| 743 | INIT_LIST_HEAD(&pnettable->pnetlist); |
| 744 | rwlock_init(&pnettable->lock); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 749 | int __init smc_pnet_init(void) |
| 750 | { |
| 751 | int rc; |
| 752 | |
| 753 | rc = genl_register_family(&smc_pnet_nl_family); |
| 754 | if (rc) |
| 755 | return rc; |
| 756 | rc = register_netdevice_notifier(&smc_netdev_notifier); |
| 757 | if (rc) |
| 758 | genl_unregister_family(&smc_pnet_nl_family); |
| 759 | return rc; |
| 760 | } |
| 761 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 762 | /* exit network namespace */ |
| 763 | void smc_pnet_net_exit(struct net *net) |
| 764 | { |
| 765 | /* flush pnet table */ |
| 766 | smc_pnet_remove_by_pnetid(net, NULL); |
| 767 | } |
| 768 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 769 | void smc_pnet_exit(void) |
| 770 | { |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 771 | unregister_netdevice_notifier(&smc_netdev_notifier); |
| 772 | genl_unregister_family(&smc_pnet_nl_family); |
| 773 | } |
| 774 | |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 775 | /* Determine one base device for stacked net devices. |
| 776 | * If the lower device level contains more than one devices |
| 777 | * (for instance with bonding slaves), just the first device |
| 778 | * is used to reach a base device. |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 779 | */ |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 780 | static struct net_device *pnet_find_base_ndev(struct net_device *ndev) |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 781 | { |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 782 | int i, nest_lvl; |
| 783 | |
| 784 | rtnl_lock(); |
Taehee Yoo | f3b0a18 | 2019-10-21 18:47:58 +0000 | [diff] [blame] | 785 | nest_lvl = ndev->lower_level; |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 786 | for (i = 0; i < nest_lvl; i++) { |
| 787 | struct list_head *lower = &ndev->adj_list.lower; |
| 788 | |
| 789 | if (list_empty(lower)) |
| 790 | break; |
| 791 | lower = lower->next; |
| 792 | ndev = netdev_lower_get_next(ndev, &lower); |
| 793 | } |
| 794 | rtnl_unlock(); |
| 795 | return ndev; |
| 796 | } |
| 797 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 798 | static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev, |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 799 | u8 *pnetid) |
| 800 | { |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 801 | struct smc_pnettable *pnettable; |
| 802 | struct net *net = dev_net(ndev); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 803 | struct smc_pnetentry *pnetelem; |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 804 | struct smc_net *sn; |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 805 | int rc = -ENOENT; |
| 806 | |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 807 | /* get pnettable for namespace */ |
| 808 | sn = net_generic(net, smc_net_id); |
| 809 | pnettable = &sn->pnettable; |
| 810 | |
| 811 | read_lock(&pnettable->lock); |
| 812 | list_for_each_entry(pnetelem, &pnettable->pnetlist, list) { |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 813 | if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) { |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 814 | /* get pnetid of netdev device */ |
| 815 | memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN); |
| 816 | rc = 0; |
| 817 | break; |
| 818 | } |
| 819 | } |
Hans Wippel | 64e28b5 | 2019-02-21 13:01:02 +0100 | [diff] [blame] | 820 | read_unlock(&pnettable->lock); |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 821 | return rc; |
| 822 | } |
| 823 | |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 824 | /* find a roce device for the given pnetid */ |
| 825 | static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id, |
Karsten Graul | 6c868a3 | 2020-05-01 12:48:11 +0200 | [diff] [blame] | 826 | struct smc_init_info *ini, |
| 827 | struct smc_ib_device *known_dev) |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 828 | { |
| 829 | struct smc_ib_device *ibdev; |
| 830 | int i; |
| 831 | |
| 832 | ini->ib_dev = NULL; |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 833 | mutex_lock(&smc_ib_devices.mutex); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 834 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { |
Karsten Graul | 6c868a3 | 2020-05-01 12:48:11 +0200 | [diff] [blame] | 835 | if (ibdev == known_dev) |
| 836 | continue; |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 837 | for (i = 1; i <= SMC_MAX_PORTS; i++) { |
| 838 | if (!rdma_is_port_valid(ibdev->ibdev, i)) |
| 839 | continue; |
| 840 | if (smc_pnet_match(ibdev->pnetid[i - 1], pnet_id) && |
| 841 | smc_ib_port_active(ibdev, i) && |
| 842 | !test_bit(i - 1, ibdev->ports_going_away) && |
| 843 | !smc_ib_determine_gid(ibdev, i, ini->vlan_id, |
| 844 | ini->ib_gid, NULL)) { |
| 845 | ini->ib_dev = ibdev; |
| 846 | ini->ib_port = i; |
| 847 | goto out; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | out: |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 852 | mutex_unlock(&smc_ib_devices.mutex); |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 853 | } |
| 854 | |
Karsten Graul | 6c868a3 | 2020-05-01 12:48:11 +0200 | [diff] [blame] | 855 | /* find alternate roce device with same pnet_id and vlan_id */ |
| 856 | void smc_pnet_find_alt_roce(struct smc_link_group *lgr, |
| 857 | struct smc_init_info *ini, |
| 858 | struct smc_ib_device *known_dev) |
| 859 | { |
| 860 | _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev); |
| 861 | } |
| 862 | |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 863 | /* if handshake network device belongs to a roce device, return its |
| 864 | * IB device and port |
| 865 | */ |
| 866 | static void smc_pnet_find_rdma_dev(struct net_device *netdev, |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 867 | struct smc_init_info *ini) |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 868 | { |
| 869 | struct smc_ib_device *ibdev; |
| 870 | |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 871 | mutex_lock(&smc_ib_devices.mutex); |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 872 | list_for_each_entry(ibdev, &smc_ib_devices.list, list) { |
| 873 | struct net_device *ndev; |
| 874 | int i; |
| 875 | |
| 876 | for (i = 1; i <= SMC_MAX_PORTS; i++) { |
| 877 | if (!rdma_is_port_valid(ibdev->ibdev, i)) |
| 878 | continue; |
| 879 | if (!ibdev->ibdev->ops.get_netdev) |
| 880 | continue; |
| 881 | ndev = ibdev->ibdev->ops.get_netdev(ibdev->ibdev, i); |
| 882 | if (!ndev) |
| 883 | continue; |
| 884 | dev_put(ndev); |
| 885 | if (netdev == ndev && |
| 886 | smc_ib_port_active(ibdev, i) && |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 887 | !test_bit(i - 1, ibdev->ports_going_away) && |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 888 | !smc_ib_determine_gid(ibdev, i, ini->vlan_id, |
| 889 | ini->ib_gid, NULL)) { |
| 890 | ini->ib_dev = ibdev; |
| 891 | ini->ib_port = i; |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 892 | break; |
| 893 | } |
| 894 | } |
| 895 | } |
Ursula Braun | 92f3cb0 | 2020-07-08 17:05:13 +0200 | [diff] [blame] | 896 | mutex_unlock(&smc_ib_devices.mutex); |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 899 | /* Determine the corresponding IB device port based on the hardware PNETID. |
Ursula Braun | 7005ada | 2018-07-25 16:35:31 +0200 | [diff] [blame] | 900 | * Searching stops at the first matching active IB device port with vlan_id |
| 901 | * configured. |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 902 | * If nothing found, check pnetid table. |
| 903 | * If nothing found, try to use handshake device |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 904 | */ |
| 905 | static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev, |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 906 | struct smc_init_info *ini) |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 907 | { |
| 908 | u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 909 | |
| 910 | ndev = pnet_find_base_ndev(ndev); |
| 911 | if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port, |
Hans Wippel | 890a2cb | 2019-02-21 13:01:00 +0100 | [diff] [blame] | 912 | ndev_pnetid) && |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 913 | smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) { |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 914 | smc_pnet_find_rdma_dev(ndev, ini); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 915 | return; /* pnetid could not be determined */ |
Ursula Braun | 5490357 | 2019-02-28 15:10:08 +0100 | [diff] [blame] | 916 | } |
Karsten Graul | 6c868a3 | 2020-05-01 12:48:11 +0200 | [diff] [blame] | 917 | _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 920 | static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev, |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 921 | struct smc_init_info *ini) |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 922 | { |
| 923 | u8 ndev_pnetid[SMC_MAX_PNETID_LEN]; |
| 924 | struct smcd_dev *ismdev; |
| 925 | |
| 926 | ndev = pnet_find_base_ndev(ndev); |
| 927 | if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port, |
Hans Wippel | f3d74b2 | 2019-02-21 13:01:01 +0100 | [diff] [blame] | 928 | ndev_pnetid) && |
| 929 | smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 930 | return; /* pnetid could not be determined */ |
| 931 | |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 932 | mutex_lock(&smcd_dev_list.mutex); |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 933 | list_for_each_entry(ismdev, &smcd_dev_list.list, list) { |
Ursula Braun | c3d9494 | 2019-10-09 10:07:46 +0200 | [diff] [blame] | 934 | if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) && |
Ursula Braun | 7b2977d | 2020-09-10 18:48:24 +0200 | [diff] [blame] | 935 | !ismdev->going_away && |
Ursula Braun | 3fc6493 | 2020-09-26 12:44:23 +0200 | [diff] [blame^] | 936 | (!ini->ism_peer_gid[0] || |
| 937 | !smc_ism_cantalk(ini->ism_peer_gid[0], ini->vlan_id, |
Ursula Braun | 7b2977d | 2020-09-10 18:48:24 +0200 | [diff] [blame] | 938 | ismdev))) { |
Ursula Braun | 3fc6493 | 2020-09-26 12:44:23 +0200 | [diff] [blame^] | 939 | ini->ism_dev[0] = ismdev; |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | } |
Ursula Braun | 82087c0 | 2020-07-08 17:05:14 +0200 | [diff] [blame] | 943 | mutex_unlock(&smcd_dev_list.mutex); |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 946 | /* PNET table analysis for a given sock: |
| 947 | * determine ib_device and port belonging to used internal TCP socket |
| 948 | * ethernet interface. |
| 949 | */ |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 950 | void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini) |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 951 | { |
| 952 | struct dst_entry *dst = sk_dst_get(sk); |
| 953 | |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 954 | ini->ib_dev = NULL; |
| 955 | ini->ib_port = 0; |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 956 | if (!dst) |
| 957 | goto out; |
| 958 | if (!dst->dev) |
| 959 | goto out_rel; |
| 960 | |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 961 | smc_pnet_find_roce_by_pnetid(dst->dev, ini); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 962 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 963 | out_rel: |
| 964 | dst_release(dst); |
Ursula Braun | 0afff91 | 2018-06-28 19:05:05 +0200 | [diff] [blame] | 965 | out: |
| 966 | return; |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 967 | } |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 968 | |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 969 | void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini) |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 970 | { |
| 971 | struct dst_entry *dst = sk_dst_get(sk); |
| 972 | |
Ursula Braun | 3fc6493 | 2020-09-26 12:44:23 +0200 | [diff] [blame^] | 973 | ini->ism_dev[0] = NULL; |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 974 | if (!dst) |
| 975 | goto out; |
| 976 | if (!dst->dev) |
| 977 | goto out_rel; |
| 978 | |
Karsten Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 979 | smc_pnet_find_ism_by_pnetid(dst->dev, ini); |
Hans Wippel | 1619f77 | 2018-06-28 19:05:08 +0200 | [diff] [blame] | 980 | |
| 981 | out_rel: |
| 982 | dst_release(dst); |
| 983 | out: |
| 984 | return; |
| 985 | } |
Karsten Graul | fdff704 | 2020-04-29 17:10:37 +0200 | [diff] [blame] | 986 | |
| 987 | /* Lookup and apply a pnet table entry to the given ib device. |
| 988 | */ |
| 989 | int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port) |
| 990 | { |
| 991 | char *ib_name = smcibdev->ibdev->name; |
| 992 | struct smc_pnettable *pnettable; |
| 993 | struct smc_pnetentry *tmp_pe; |
| 994 | struct smc_net *sn; |
| 995 | int rc = -ENOENT; |
| 996 | |
| 997 | /* get pnettable for init namespace */ |
| 998 | sn = net_generic(&init_net, smc_net_id); |
| 999 | pnettable = &sn->pnettable; |
| 1000 | |
| 1001 | read_lock(&pnettable->lock); |
| 1002 | list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { |
| 1003 | if (tmp_pe->type == SMC_PNET_IB && |
| 1004 | !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX) && |
| 1005 | tmp_pe->ib_port == ib_port) { |
| 1006 | smc_pnet_apply_ib(smcibdev, ib_port, tmp_pe->pnet_name); |
| 1007 | rc = 0; |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | read_unlock(&pnettable->lock); |
| 1012 | |
| 1013 | return rc; |
| 1014 | } |
| 1015 | |
| 1016 | /* Lookup and apply a pnet table entry to the given smcd device. |
| 1017 | */ |
| 1018 | int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev) |
| 1019 | { |
| 1020 | const char *ib_name = dev_name(&smcddev->dev); |
| 1021 | struct smc_pnettable *pnettable; |
| 1022 | struct smc_pnetentry *tmp_pe; |
| 1023 | struct smc_net *sn; |
| 1024 | int rc = -ENOENT; |
| 1025 | |
| 1026 | /* get pnettable for init namespace */ |
| 1027 | sn = net_generic(&init_net, smc_net_id); |
| 1028 | pnettable = &sn->pnettable; |
| 1029 | |
| 1030 | read_lock(&pnettable->lock); |
| 1031 | list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) { |
| 1032 | if (tmp_pe->type == SMC_PNET_IB && |
| 1033 | !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) { |
| 1034 | smc_pnet_apply_smcd(smcddev, tmp_pe->pnet_name); |
| 1035 | rc = 0; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | read_unlock(&pnettable->lock); |
| 1040 | |
| 1041 | return rc; |
| 1042 | } |