smc_pnet.c 12 KB

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