smc_pnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 <net/netlink.h>
  15. #include <net/genetlink.h>
  16. #include <uapi/linux/if.h>
  17. #include <uapi/linux/smc.h>
  18. #include <rdma/ib_verbs.h>
  19. #include "smc_pnet.h"
  20. #include "smc_ib.h"
  21. #define SMC_MAX_PNET_ID_LEN 16 /* Max. length of PNET id */
  22. static struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
  23. [SMC_PNETID_NAME] = {
  24. .type = NLA_NUL_STRING,
  25. .len = SMC_MAX_PNET_ID_LEN - 1
  26. },
  27. [SMC_PNETID_ETHNAME] = {
  28. .type = NLA_NUL_STRING,
  29. .len = IFNAMSIZ - 1
  30. },
  31. [SMC_PNETID_IBNAME] = {
  32. .type = NLA_NUL_STRING,
  33. .len = IB_DEVICE_NAME_MAX - 1
  34. },
  35. [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
  36. };
  37. static struct genl_family smc_pnet_nl_family;
  38. /**
  39. * struct smc_pnettable - SMC PNET table anchor
  40. * @lock: Lock for list action
  41. * @pnetlist: List of PNETIDs
  42. */
  43. static struct smc_pnettable {
  44. rwlock_t lock;
  45. struct list_head pnetlist;
  46. } smc_pnettable = {
  47. .pnetlist = LIST_HEAD_INIT(smc_pnettable.pnetlist),
  48. .lock = __RW_LOCK_UNLOCKED(smc_pnettable.lock)
  49. };
  50. /**
  51. * struct smc_pnetentry - pnet identifier name entry
  52. * @list: List node.
  53. * @pnet_name: Pnet identifier name
  54. * @ndev: pointer to network device.
  55. * @smcibdev: Pointer to IB device.
  56. */
  57. struct smc_pnetentry {
  58. struct list_head list;
  59. char pnet_name[SMC_MAX_PNET_ID_LEN + 1];
  60. struct net_device *ndev;
  61. struct smc_ib_device *smcibdev;
  62. u8 ib_port;
  63. };
  64. /* Check if two RDMA device entries are identical. Use device name and port
  65. * number for comparison.
  66. */
  67. static bool smc_pnet_same_ibname(struct smc_pnetentry *pnetelem, char *ibname,
  68. u8 ibport)
  69. {
  70. return pnetelem->ib_port == ibport &&
  71. !strncmp(pnetelem->smcibdev->ibdev->name, ibname,
  72. sizeof(pnetelem->smcibdev->ibdev->name));
  73. }
  74. /* Find a pnetid in the pnet table.
  75. */
  76. static struct smc_pnetentry *smc_pnet_find_pnetid(char *pnet_name)
  77. {
  78. struct smc_pnetentry *pnetelem, *found_pnetelem = NULL;
  79. read_lock(&smc_pnettable.lock);
  80. list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
  81. if (!strncmp(pnetelem->pnet_name, pnet_name,
  82. sizeof(pnetelem->pnet_name))) {
  83. found_pnetelem = pnetelem;
  84. break;
  85. }
  86. }
  87. read_unlock(&smc_pnettable.lock);
  88. return found_pnetelem;
  89. }
  90. /* Remove a pnetid from the pnet table.
  91. */
  92. static int smc_pnet_remove_by_pnetid(char *pnet_name)
  93. {
  94. struct smc_pnetentry *pnetelem, *tmp_pe;
  95. int rc = -ENOENT;
  96. write_lock(&smc_pnettable.lock);
  97. list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
  98. list) {
  99. if (!strncmp(pnetelem->pnet_name, pnet_name,
  100. sizeof(pnetelem->pnet_name))) {
  101. list_del(&pnetelem->list);
  102. dev_put(pnetelem->ndev);
  103. kfree(pnetelem);
  104. rc = 0;
  105. break;
  106. }
  107. }
  108. write_unlock(&smc_pnettable.lock);
  109. return rc;
  110. }
  111. /* Remove a pnet entry mentioning a given network device from the pnet table.
  112. */
  113. static int smc_pnet_remove_by_ndev(struct net_device *ndev)
  114. {
  115. struct smc_pnetentry *pnetelem, *tmp_pe;
  116. int rc = -ENOENT;
  117. write_lock(&smc_pnettable.lock);
  118. list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
  119. list) {
  120. if (pnetelem->ndev == ndev) {
  121. list_del(&pnetelem->list);
  122. dev_put(pnetelem->ndev);
  123. kfree(pnetelem);
  124. rc = 0;
  125. break;
  126. }
  127. }
  128. write_unlock(&smc_pnettable.lock);
  129. return rc;
  130. }
  131. /* Remove a pnet entry mentioning a given ib device from the pnet table.
  132. */
  133. int smc_pnet_remove_by_ibdev(struct smc_ib_device *ibdev)
  134. {
  135. struct smc_pnetentry *pnetelem, *tmp_pe;
  136. int rc = -ENOENT;
  137. write_lock(&smc_pnettable.lock);
  138. list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
  139. list) {
  140. if (pnetelem->smcibdev == ibdev) {
  141. list_del(&pnetelem->list);
  142. dev_put(pnetelem->ndev);
  143. kfree(pnetelem);
  144. rc = 0;
  145. break;
  146. }
  147. }
  148. write_unlock(&smc_pnettable.lock);
  149. return rc;
  150. }
  151. /* Append a pnetid to the end of the pnet table if not already on this list.
  152. */
  153. static int smc_pnet_enter(struct smc_pnetentry *new_pnetelem)
  154. {
  155. struct smc_pnetentry *pnetelem;
  156. int rc = -EEXIST;
  157. write_lock(&smc_pnettable.lock);
  158. list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
  159. if (!strncmp(pnetelem->pnet_name, new_pnetelem->pnet_name,
  160. sizeof(new_pnetelem->pnet_name)) ||
  161. !strncmp(pnetelem->ndev->name, new_pnetelem->ndev->name,
  162. sizeof(new_pnetelem->ndev->name)) ||
  163. smc_pnet_same_ibname(pnetelem,
  164. new_pnetelem->smcibdev->ibdev->name,
  165. new_pnetelem->ib_port)) {
  166. dev_put(pnetelem->ndev);
  167. goto found;
  168. }
  169. }
  170. list_add_tail(&new_pnetelem->list, &smc_pnettable.pnetlist);
  171. rc = 0;
  172. found:
  173. write_unlock(&smc_pnettable.lock);
  174. return rc;
  175. }
  176. /* The limit for pnetid is 16 characters.
  177. * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
  178. * Lower case letters are converted to upper case.
  179. * Interior blanks should not be used.
  180. */
  181. static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
  182. {
  183. char *bf = skip_spaces(pnet_name);
  184. size_t len = strlen(bf);
  185. char *end = bf + len;
  186. if (!len)
  187. return false;
  188. while (--end >= bf && isspace(*end))
  189. ;
  190. if (end - bf >= SMC_MAX_PNET_ID_LEN)
  191. return false;
  192. while (bf <= end) {
  193. if (!isalnum(*bf))
  194. return false;
  195. *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
  196. bf++;
  197. }
  198. *pnetid = '\0';
  199. return true;
  200. }
  201. /* Find an infiniband device by a given name. The device might not exist. */
  202. static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
  203. {
  204. struct smc_ib_device *ibdev;
  205. spin_lock(&smc_ib_devices.lock);
  206. list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
  207. if (!strncmp(ibdev->ibdev->name, ib_name,
  208. sizeof(ibdev->ibdev->name))) {
  209. goto out;
  210. }
  211. }
  212. ibdev = NULL;
  213. out:
  214. spin_unlock(&smc_ib_devices.lock);
  215. return ibdev;
  216. }
  217. /* Parse the supplied netlink attributes and fill a pnetentry structure.
  218. * For ethernet and infiniband device names verify that the devices exist.
  219. */
  220. static int smc_pnet_fill_entry(struct net *net, struct smc_pnetentry *pnetelem,
  221. struct nlattr *tb[])
  222. {
  223. char *string, *ibname;
  224. int rc;
  225. memset(pnetelem, 0, sizeof(*pnetelem));
  226. INIT_LIST_HEAD(&pnetelem->list);
  227. rc = -EINVAL;
  228. if (!tb[SMC_PNETID_NAME])
  229. goto error;
  230. string = (char *)nla_data(tb[SMC_PNETID_NAME]);
  231. if (!smc_pnetid_valid(string, pnetelem->pnet_name))
  232. goto error;
  233. rc = -EINVAL;
  234. if (!tb[SMC_PNETID_ETHNAME])
  235. goto error;
  236. rc = -ENOENT;
  237. string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
  238. pnetelem->ndev = dev_get_by_name(net, string);
  239. if (!pnetelem->ndev)
  240. goto error;
  241. rc = -EINVAL;
  242. if (!tb[SMC_PNETID_IBNAME])
  243. goto error;
  244. rc = -ENOENT;
  245. ibname = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
  246. ibname = strim(ibname);
  247. pnetelem->smcibdev = smc_pnet_find_ib(ibname);
  248. if (!pnetelem->smcibdev)
  249. goto error;
  250. rc = -EINVAL;
  251. if (!tb[SMC_PNETID_IBPORT])
  252. goto error;
  253. pnetelem->ib_port = nla_get_u8(tb[SMC_PNETID_IBPORT]);
  254. if (pnetelem->ib_port < 1 || pnetelem->ib_port > SMC_MAX_PORTS)
  255. goto error;
  256. return 0;
  257. error:
  258. if (pnetelem->ndev)
  259. dev_put(pnetelem->ndev);
  260. return rc;
  261. }
  262. /* Convert an smc_pnetentry to a netlink attribute sequence */
  263. static int smc_pnet_set_nla(struct sk_buff *msg, struct smc_pnetentry *pnetelem)
  264. {
  265. if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name) ||
  266. nla_put_string(msg, SMC_PNETID_ETHNAME, pnetelem->ndev->name) ||
  267. nla_put_string(msg, SMC_PNETID_IBNAME,
  268. pnetelem->smcibdev->ibdev->name) ||
  269. nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
  270. return -1;
  271. return 0;
  272. }
  273. /* Retrieve one PNETID entry */
  274. static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
  275. {
  276. struct smc_pnetentry *pnetelem;
  277. struct sk_buff *msg;
  278. void *hdr;
  279. int rc;
  280. if (!info->attrs[SMC_PNETID_NAME])
  281. return -EINVAL;
  282. pnetelem = smc_pnet_find_pnetid(
  283. (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
  284. if (!pnetelem)
  285. return -ENOENT;
  286. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  287. if (!msg)
  288. return -ENOMEM;
  289. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  290. &smc_pnet_nl_family, 0, SMC_PNETID_GET);
  291. if (!hdr) {
  292. rc = -EMSGSIZE;
  293. goto err_out;
  294. }
  295. if (smc_pnet_set_nla(msg, pnetelem)) {
  296. rc = -ENOBUFS;
  297. goto err_out;
  298. }
  299. genlmsg_end(msg, hdr);
  300. return genlmsg_reply(msg, info);
  301. err_out:
  302. nlmsg_free(msg);
  303. return rc;
  304. }
  305. static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
  306. {
  307. struct net *net = genl_info_net(info);
  308. struct smc_pnetentry *pnetelem;
  309. int rc;
  310. pnetelem = kzalloc(sizeof(*pnetelem), GFP_KERNEL);
  311. if (!pnetelem)
  312. return -ENOMEM;
  313. rc = smc_pnet_fill_entry(net, pnetelem, info->attrs);
  314. if (!rc)
  315. rc = smc_pnet_enter(pnetelem);
  316. if (rc) {
  317. kfree(pnetelem);
  318. return rc;
  319. }
  320. rc = smc_ib_remember_port_attr(pnetelem->smcibdev, pnetelem->ib_port);
  321. if (rc)
  322. smc_pnet_remove_by_pnetid(pnetelem->pnet_name);
  323. return rc;
  324. }
  325. static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
  326. {
  327. if (!info->attrs[SMC_PNETID_NAME])
  328. return -EINVAL;
  329. return smc_pnet_remove_by_pnetid(
  330. (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
  331. }
  332. static int smc_pnet_dump_start(struct netlink_callback *cb)
  333. {
  334. cb->args[0] = 0;
  335. return 0;
  336. }
  337. static int smc_pnet_dumpinfo(struct sk_buff *skb,
  338. u32 portid, u32 seq, u32 flags,
  339. struct smc_pnetentry *pnetelem)
  340. {
  341. void *hdr;
  342. hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
  343. flags, SMC_PNETID_GET);
  344. if (!hdr)
  345. return -ENOMEM;
  346. if (smc_pnet_set_nla(skb, pnetelem) < 0) {
  347. genlmsg_cancel(skb, hdr);
  348. return -EMSGSIZE;
  349. }
  350. genlmsg_end(skb, hdr);
  351. return 0;
  352. }
  353. static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
  354. {
  355. struct smc_pnetentry *pnetelem;
  356. int idx = 0;
  357. read_lock(&smc_pnettable.lock);
  358. list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
  359. if (idx++ < cb->args[0])
  360. continue;
  361. if (smc_pnet_dumpinfo(skb, NETLINK_CB(cb->skb).portid,
  362. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  363. pnetelem)) {
  364. --idx;
  365. break;
  366. }
  367. }
  368. cb->args[0] = idx;
  369. read_unlock(&smc_pnettable.lock);
  370. return skb->len;
  371. }
  372. /* Remove and delete all pnetids from pnet table.
  373. */
  374. static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
  375. {
  376. struct smc_pnetentry *pnetelem, *tmp_pe;
  377. write_lock(&smc_pnettable.lock);
  378. list_for_each_entry_safe(pnetelem, tmp_pe, &smc_pnettable.pnetlist,
  379. list) {
  380. list_del(&pnetelem->list);
  381. dev_put(pnetelem->ndev);
  382. kfree(pnetelem);
  383. }
  384. write_unlock(&smc_pnettable.lock);
  385. return 0;
  386. }
  387. /* SMC_PNETID generic netlink operation definition */
  388. static const struct genl_ops smc_pnet_ops[] = {
  389. {
  390. .cmd = SMC_PNETID_GET,
  391. .flags = GENL_ADMIN_PERM,
  392. .policy = smc_pnet_policy,
  393. .doit = smc_pnet_get,
  394. .dumpit = smc_pnet_dump,
  395. .start = smc_pnet_dump_start
  396. },
  397. {
  398. .cmd = SMC_PNETID_ADD,
  399. .flags = GENL_ADMIN_PERM,
  400. .policy = smc_pnet_policy,
  401. .doit = smc_pnet_add
  402. },
  403. {
  404. .cmd = SMC_PNETID_DEL,
  405. .flags = GENL_ADMIN_PERM,
  406. .policy = smc_pnet_policy,
  407. .doit = smc_pnet_del
  408. },
  409. {
  410. .cmd = SMC_PNETID_FLUSH,
  411. .flags = GENL_ADMIN_PERM,
  412. .policy = smc_pnet_policy,
  413. .doit = smc_pnet_flush
  414. }
  415. };
  416. /* SMC_PNETID family definition */
  417. static struct genl_family smc_pnet_nl_family = {
  418. .hdrsize = 0,
  419. .name = SMCR_GENL_FAMILY_NAME,
  420. .version = SMCR_GENL_FAMILY_VERSION,
  421. .maxattr = SMC_PNETID_MAX,
  422. .netnsok = true,
  423. .module = THIS_MODULE,
  424. .ops = smc_pnet_ops,
  425. .n_ops = ARRAY_SIZE(smc_pnet_ops)
  426. };
  427. static int smc_pnet_netdev_event(struct notifier_block *this,
  428. unsigned long event, void *ptr)
  429. {
  430. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  431. switch (event) {
  432. case NETDEV_REBOOT:
  433. case NETDEV_UNREGISTER:
  434. smc_pnet_remove_by_ndev(event_dev);
  435. default:
  436. break;
  437. }
  438. return NOTIFY_DONE;
  439. }
  440. static struct notifier_block smc_netdev_notifier = {
  441. .notifier_call = smc_pnet_netdev_event
  442. };
  443. int __init smc_pnet_init(void)
  444. {
  445. int rc;
  446. rc = genl_register_family(&smc_pnet_nl_family);
  447. if (rc)
  448. return rc;
  449. rc = register_netdevice_notifier(&smc_netdev_notifier);
  450. if (rc)
  451. genl_unregister_family(&smc_pnet_nl_family);
  452. return rc;
  453. }
  454. void smc_pnet_exit(void)
  455. {
  456. smc_pnet_flush(NULL, NULL);
  457. unregister_netdevice_notifier(&smc_netdev_notifier);
  458. genl_unregister_family(&smc_pnet_nl_family);
  459. }
  460. /* PNET table analysis for a given sock:
  461. * determine ib_device and port belonging to used internal TCP socket
  462. * ethernet interface.
  463. */
  464. void smc_pnet_find_roce_resource(struct sock *sk,
  465. struct smc_ib_device **smcibdev, u8 *ibport)
  466. {
  467. struct dst_entry *dst = sk_dst_get(sk);
  468. struct smc_pnetentry *pnetelem;
  469. *smcibdev = NULL;
  470. *ibport = 0;
  471. if (!dst)
  472. return;
  473. if (!dst->dev)
  474. goto out_rel;
  475. read_lock(&smc_pnettable.lock);
  476. list_for_each_entry(pnetelem, &smc_pnettable.pnetlist, list) {
  477. if (dst->dev == pnetelem->ndev) {
  478. if (smc_ib_port_active(pnetelem->smcibdev,
  479. pnetelem->ib_port)) {
  480. *smcibdev = pnetelem->smcibdev;
  481. *ibport = pnetelem->ib_port;
  482. }
  483. break;
  484. }
  485. }
  486. read_unlock(&smc_pnettable.lock);
  487. out_rel:
  488. dst_release(dst);
  489. }