smc_pnet.c 29 KB

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