smc_pnet.c 31 KB

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