ucma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/completion.h>
  33. #include <linux/file.h>
  34. #include <linux/mutex.h>
  35. #include <linux/poll.h>
  36. #include <linux/idr.h>
  37. #include <linux/in.h>
  38. #include <linux/in6.h>
  39. #include <linux/miscdevice.h>
  40. #include <rdma/rdma_user_cm.h>
  41. #include <rdma/ib_marshall.h>
  42. #include <rdma/rdma_cm.h>
  43. MODULE_AUTHOR("Sean Hefty");
  44. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  45. MODULE_LICENSE("Dual BSD/GPL");
  46. enum {
  47. UCMA_MAX_BACKLOG = 128
  48. };
  49. struct ucma_file {
  50. struct mutex mut;
  51. struct file *filp;
  52. struct list_head ctx_list;
  53. struct list_head event_list;
  54. wait_queue_head_t poll_wait;
  55. };
  56. struct ucma_context {
  57. int id;
  58. struct completion comp;
  59. atomic_t ref;
  60. int events_reported;
  61. int backlog;
  62. struct ucma_file *file;
  63. struct rdma_cm_id *cm_id;
  64. u64 uid;
  65. struct list_head list;
  66. struct list_head mc_list;
  67. };
  68. struct ucma_multicast {
  69. struct ucma_context *ctx;
  70. int id;
  71. int events_reported;
  72. u64 uid;
  73. struct list_head list;
  74. struct sockaddr addr;
  75. u8 pad[sizeof(struct sockaddr_in6) -
  76. sizeof(struct sockaddr)];
  77. };
  78. struct ucma_event {
  79. struct ucma_context *ctx;
  80. struct ucma_multicast *mc;
  81. struct list_head list;
  82. struct rdma_cm_id *cm_id;
  83. struct rdma_ucm_event_resp resp;
  84. };
  85. static DEFINE_MUTEX(mut);
  86. static DEFINE_IDR(ctx_idr);
  87. static DEFINE_IDR(multicast_idr);
  88. static inline struct ucma_context *_ucma_find_context(int id,
  89. struct ucma_file *file)
  90. {
  91. struct ucma_context *ctx;
  92. ctx = idr_find(&ctx_idr, id);
  93. if (!ctx)
  94. ctx = ERR_PTR(-ENOENT);
  95. else if (ctx->file != file)
  96. ctx = ERR_PTR(-EINVAL);
  97. return ctx;
  98. }
  99. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  100. {
  101. struct ucma_context *ctx;
  102. mutex_lock(&mut);
  103. ctx = _ucma_find_context(id, file);
  104. if (!IS_ERR(ctx))
  105. atomic_inc(&ctx->ref);
  106. mutex_unlock(&mut);
  107. return ctx;
  108. }
  109. static void ucma_put_ctx(struct ucma_context *ctx)
  110. {
  111. if (atomic_dec_and_test(&ctx->ref))
  112. complete(&ctx->comp);
  113. }
  114. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  115. {
  116. struct ucma_context *ctx;
  117. int ret;
  118. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  119. if (!ctx)
  120. return NULL;
  121. atomic_set(&ctx->ref, 1);
  122. init_completion(&ctx->comp);
  123. INIT_LIST_HEAD(&ctx->mc_list);
  124. ctx->file = file;
  125. do {
  126. ret = idr_pre_get(&ctx_idr, GFP_KERNEL);
  127. if (!ret)
  128. goto error;
  129. mutex_lock(&mut);
  130. ret = idr_get_new(&ctx_idr, ctx, &ctx->id);
  131. mutex_unlock(&mut);
  132. } while (ret == -EAGAIN);
  133. if (ret)
  134. goto error;
  135. list_add_tail(&ctx->list, &file->ctx_list);
  136. return ctx;
  137. error:
  138. kfree(ctx);
  139. return NULL;
  140. }
  141. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  142. {
  143. struct ucma_multicast *mc;
  144. int ret;
  145. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  146. if (!mc)
  147. return NULL;
  148. do {
  149. ret = idr_pre_get(&multicast_idr, GFP_KERNEL);
  150. if (!ret)
  151. goto error;
  152. mutex_lock(&mut);
  153. ret = idr_get_new(&multicast_idr, mc, &mc->id);
  154. mutex_unlock(&mut);
  155. } while (ret == -EAGAIN);
  156. if (ret)
  157. goto error;
  158. mc->ctx = ctx;
  159. list_add_tail(&mc->list, &ctx->mc_list);
  160. return mc;
  161. error:
  162. kfree(mc);
  163. return NULL;
  164. }
  165. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  166. struct rdma_conn_param *src)
  167. {
  168. if (src->private_data_len)
  169. memcpy(dst->private_data, src->private_data,
  170. src->private_data_len);
  171. dst->private_data_len = src->private_data_len;
  172. dst->responder_resources =src->responder_resources;
  173. dst->initiator_depth = src->initiator_depth;
  174. dst->flow_control = src->flow_control;
  175. dst->retry_count = src->retry_count;
  176. dst->rnr_retry_count = src->rnr_retry_count;
  177. dst->srq = src->srq;
  178. dst->qp_num = src->qp_num;
  179. }
  180. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  181. struct rdma_ud_param *src)
  182. {
  183. if (src->private_data_len)
  184. memcpy(dst->private_data, src->private_data,
  185. src->private_data_len);
  186. dst->private_data_len = src->private_data_len;
  187. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  188. dst->qp_num = src->qp_num;
  189. dst->qkey = src->qkey;
  190. }
  191. static void ucma_set_event_context(struct ucma_context *ctx,
  192. struct rdma_cm_event *event,
  193. struct ucma_event *uevent)
  194. {
  195. uevent->ctx = ctx;
  196. switch (event->event) {
  197. case RDMA_CM_EVENT_MULTICAST_JOIN:
  198. case RDMA_CM_EVENT_MULTICAST_ERROR:
  199. uevent->mc = (struct ucma_multicast *)
  200. event->param.ud.private_data;
  201. uevent->resp.uid = uevent->mc->uid;
  202. uevent->resp.id = uevent->mc->id;
  203. break;
  204. default:
  205. uevent->resp.uid = ctx->uid;
  206. uevent->resp.id = ctx->id;
  207. break;
  208. }
  209. }
  210. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  211. struct rdma_cm_event *event)
  212. {
  213. struct ucma_event *uevent;
  214. struct ucma_context *ctx = cm_id->context;
  215. int ret = 0;
  216. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  217. if (!uevent)
  218. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  219. uevent->cm_id = cm_id;
  220. ucma_set_event_context(ctx, event, uevent);
  221. uevent->resp.event = event->event;
  222. uevent->resp.status = event->status;
  223. if (cm_id->ps == RDMA_PS_UDP || cm_id->ps == RDMA_PS_IPOIB)
  224. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  225. else
  226. ucma_copy_conn_event(&uevent->resp.param.conn,
  227. &event->param.conn);
  228. mutex_lock(&ctx->file->mut);
  229. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  230. if (!ctx->backlog) {
  231. ret = -ENOMEM;
  232. kfree(uevent);
  233. goto out;
  234. }
  235. ctx->backlog--;
  236. } else if (!ctx->uid) {
  237. /*
  238. * We ignore events for new connections until userspace has set
  239. * their context. This can only happen if an error occurs on a
  240. * new connection before the user accepts it. This is okay,
  241. * since the accept will just fail later.
  242. */
  243. kfree(uevent);
  244. goto out;
  245. }
  246. list_add_tail(&uevent->list, &ctx->file->event_list);
  247. wake_up_interruptible(&ctx->file->poll_wait);
  248. out:
  249. mutex_unlock(&ctx->file->mut);
  250. return ret;
  251. }
  252. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  253. int in_len, int out_len)
  254. {
  255. struct ucma_context *ctx;
  256. struct rdma_ucm_get_event cmd;
  257. struct ucma_event *uevent;
  258. int ret = 0;
  259. DEFINE_WAIT(wait);
  260. if (out_len < sizeof uevent->resp)
  261. return -ENOSPC;
  262. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  263. return -EFAULT;
  264. mutex_lock(&file->mut);
  265. while (list_empty(&file->event_list)) {
  266. mutex_unlock(&file->mut);
  267. if (file->filp->f_flags & O_NONBLOCK)
  268. return -EAGAIN;
  269. if (wait_event_interruptible(file->poll_wait,
  270. !list_empty(&file->event_list)))
  271. return -ERESTARTSYS;
  272. mutex_lock(&file->mut);
  273. }
  274. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  275. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  276. ctx = ucma_alloc_ctx(file);
  277. if (!ctx) {
  278. ret = -ENOMEM;
  279. goto done;
  280. }
  281. uevent->ctx->backlog++;
  282. ctx->cm_id = uevent->cm_id;
  283. ctx->cm_id->context = ctx;
  284. uevent->resp.id = ctx->id;
  285. }
  286. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  287. &uevent->resp, sizeof uevent->resp)) {
  288. ret = -EFAULT;
  289. goto done;
  290. }
  291. list_del(&uevent->list);
  292. uevent->ctx->events_reported++;
  293. if (uevent->mc)
  294. uevent->mc->events_reported++;
  295. kfree(uevent);
  296. done:
  297. mutex_unlock(&file->mut);
  298. return ret;
  299. }
  300. static ssize_t ucma_create_id(struct ucma_file *file,
  301. const char __user *inbuf,
  302. int in_len, int out_len)
  303. {
  304. struct rdma_ucm_create_id cmd;
  305. struct rdma_ucm_create_id_resp resp;
  306. struct ucma_context *ctx;
  307. int ret;
  308. if (out_len < sizeof(resp))
  309. return -ENOSPC;
  310. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  311. return -EFAULT;
  312. mutex_lock(&file->mut);
  313. ctx = ucma_alloc_ctx(file);
  314. mutex_unlock(&file->mut);
  315. if (!ctx)
  316. return -ENOMEM;
  317. ctx->uid = cmd.uid;
  318. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps);
  319. if (IS_ERR(ctx->cm_id)) {
  320. ret = PTR_ERR(ctx->cm_id);
  321. goto err1;
  322. }
  323. resp.id = ctx->id;
  324. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  325. &resp, sizeof(resp))) {
  326. ret = -EFAULT;
  327. goto err2;
  328. }
  329. return 0;
  330. err2:
  331. rdma_destroy_id(ctx->cm_id);
  332. err1:
  333. mutex_lock(&mut);
  334. idr_remove(&ctx_idr, ctx->id);
  335. mutex_unlock(&mut);
  336. kfree(ctx);
  337. return ret;
  338. }
  339. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  340. {
  341. struct ucma_multicast *mc, *tmp;
  342. mutex_lock(&mut);
  343. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  344. list_del(&mc->list);
  345. idr_remove(&multicast_idr, mc->id);
  346. kfree(mc);
  347. }
  348. mutex_unlock(&mut);
  349. }
  350. static void ucma_cleanup_events(struct ucma_context *ctx)
  351. {
  352. struct ucma_event *uevent, *tmp;
  353. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  354. if (uevent->ctx != ctx)
  355. continue;
  356. list_del(&uevent->list);
  357. /* clear incoming connections. */
  358. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  359. rdma_destroy_id(uevent->cm_id);
  360. kfree(uevent);
  361. }
  362. }
  363. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  364. {
  365. struct ucma_event *uevent, *tmp;
  366. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  367. if (uevent->mc != mc)
  368. continue;
  369. list_del(&uevent->list);
  370. kfree(uevent);
  371. }
  372. }
  373. static int ucma_free_ctx(struct ucma_context *ctx)
  374. {
  375. int events_reported;
  376. /* No new events will be generated after destroying the id. */
  377. rdma_destroy_id(ctx->cm_id);
  378. ucma_cleanup_multicast(ctx);
  379. /* Cleanup events not yet reported to the user. */
  380. mutex_lock(&ctx->file->mut);
  381. ucma_cleanup_events(ctx);
  382. list_del(&ctx->list);
  383. mutex_unlock(&ctx->file->mut);
  384. events_reported = ctx->events_reported;
  385. kfree(ctx);
  386. return events_reported;
  387. }
  388. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  389. int in_len, int out_len)
  390. {
  391. struct rdma_ucm_destroy_id cmd;
  392. struct rdma_ucm_destroy_id_resp resp;
  393. struct ucma_context *ctx;
  394. int ret = 0;
  395. if (out_len < sizeof(resp))
  396. return -ENOSPC;
  397. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  398. return -EFAULT;
  399. mutex_lock(&mut);
  400. ctx = _ucma_find_context(cmd.id, file);
  401. if (!IS_ERR(ctx))
  402. idr_remove(&ctx_idr, ctx->id);
  403. mutex_unlock(&mut);
  404. if (IS_ERR(ctx))
  405. return PTR_ERR(ctx);
  406. ucma_put_ctx(ctx);
  407. wait_for_completion(&ctx->comp);
  408. resp.events_reported = ucma_free_ctx(ctx);
  409. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  410. &resp, sizeof(resp)))
  411. ret = -EFAULT;
  412. return ret;
  413. }
  414. static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
  415. int in_len, int out_len)
  416. {
  417. struct rdma_ucm_bind_addr cmd;
  418. struct ucma_context *ctx;
  419. int ret;
  420. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  421. return -EFAULT;
  422. ctx = ucma_get_ctx(file, cmd.id);
  423. if (IS_ERR(ctx))
  424. return PTR_ERR(ctx);
  425. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  426. ucma_put_ctx(ctx);
  427. return ret;
  428. }
  429. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  430. const char __user *inbuf,
  431. int in_len, int out_len)
  432. {
  433. struct rdma_ucm_resolve_addr cmd;
  434. struct ucma_context *ctx;
  435. int ret;
  436. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  437. return -EFAULT;
  438. ctx = ucma_get_ctx(file, cmd.id);
  439. if (IS_ERR(ctx))
  440. return PTR_ERR(ctx);
  441. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  442. (struct sockaddr *) &cmd.dst_addr,
  443. cmd.timeout_ms);
  444. ucma_put_ctx(ctx);
  445. return ret;
  446. }
  447. static ssize_t ucma_resolve_route(struct ucma_file *file,
  448. const char __user *inbuf,
  449. int in_len, int out_len)
  450. {
  451. struct rdma_ucm_resolve_route cmd;
  452. struct ucma_context *ctx;
  453. int ret;
  454. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  455. return -EFAULT;
  456. ctx = ucma_get_ctx(file, cmd.id);
  457. if (IS_ERR(ctx))
  458. return PTR_ERR(ctx);
  459. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  460. ucma_put_ctx(ctx);
  461. return ret;
  462. }
  463. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  464. struct rdma_route *route)
  465. {
  466. struct rdma_dev_addr *dev_addr;
  467. resp->num_paths = route->num_paths;
  468. switch (route->num_paths) {
  469. case 0:
  470. dev_addr = &route->addr.dev_addr;
  471. ib_addr_get_dgid(dev_addr,
  472. (union ib_gid *) &resp->ib_route[0].dgid);
  473. ib_addr_get_sgid(dev_addr,
  474. (union ib_gid *) &resp->ib_route[0].sgid);
  475. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  476. break;
  477. case 2:
  478. ib_copy_path_rec_to_user(&resp->ib_route[1],
  479. &route->path_rec[1]);
  480. /* fall through */
  481. case 1:
  482. ib_copy_path_rec_to_user(&resp->ib_route[0],
  483. &route->path_rec[0]);
  484. break;
  485. default:
  486. break;
  487. }
  488. }
  489. static ssize_t ucma_query_route(struct ucma_file *file,
  490. const char __user *inbuf,
  491. int in_len, int out_len)
  492. {
  493. struct rdma_ucm_query_route cmd;
  494. struct rdma_ucm_query_route_resp resp;
  495. struct ucma_context *ctx;
  496. struct sockaddr *addr;
  497. int ret = 0;
  498. if (out_len < sizeof(resp))
  499. return -ENOSPC;
  500. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  501. return -EFAULT;
  502. ctx = ucma_get_ctx(file, cmd.id);
  503. if (IS_ERR(ctx))
  504. return PTR_ERR(ctx);
  505. memset(&resp, 0, sizeof resp);
  506. addr = &ctx->cm_id->route.addr.src_addr;
  507. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  508. sizeof(struct sockaddr_in) :
  509. sizeof(struct sockaddr_in6));
  510. addr = &ctx->cm_id->route.addr.dst_addr;
  511. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  512. sizeof(struct sockaddr_in) :
  513. sizeof(struct sockaddr_in6));
  514. if (!ctx->cm_id->device)
  515. goto out;
  516. resp.node_guid = ctx->cm_id->device->node_guid;
  517. resp.port_num = ctx->cm_id->port_num;
  518. switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
  519. case RDMA_TRANSPORT_IB:
  520. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  521. break;
  522. default:
  523. break;
  524. }
  525. out:
  526. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  527. &resp, sizeof(resp)))
  528. ret = -EFAULT;
  529. ucma_put_ctx(ctx);
  530. return ret;
  531. }
  532. static void ucma_copy_conn_param(struct rdma_conn_param *dst,
  533. struct rdma_ucm_conn_param *src)
  534. {
  535. dst->private_data = src->private_data;
  536. dst->private_data_len = src->private_data_len;
  537. dst->responder_resources =src->responder_resources;
  538. dst->initiator_depth = src->initiator_depth;
  539. dst->flow_control = src->flow_control;
  540. dst->retry_count = src->retry_count;
  541. dst->rnr_retry_count = src->rnr_retry_count;
  542. dst->srq = src->srq;
  543. dst->qp_num = src->qp_num;
  544. }
  545. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  546. int in_len, int out_len)
  547. {
  548. struct rdma_ucm_connect cmd;
  549. struct rdma_conn_param conn_param;
  550. struct ucma_context *ctx;
  551. int ret;
  552. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  553. return -EFAULT;
  554. if (!cmd.conn_param.valid)
  555. return -EINVAL;
  556. ctx = ucma_get_ctx(file, cmd.id);
  557. if (IS_ERR(ctx))
  558. return PTR_ERR(ctx);
  559. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  560. ret = rdma_connect(ctx->cm_id, &conn_param);
  561. ucma_put_ctx(ctx);
  562. return ret;
  563. }
  564. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  565. int in_len, int out_len)
  566. {
  567. struct rdma_ucm_listen cmd;
  568. struct ucma_context *ctx;
  569. int ret;
  570. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  571. return -EFAULT;
  572. ctx = ucma_get_ctx(file, cmd.id);
  573. if (IS_ERR(ctx))
  574. return PTR_ERR(ctx);
  575. ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
  576. cmd.backlog : UCMA_MAX_BACKLOG;
  577. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  578. ucma_put_ctx(ctx);
  579. return ret;
  580. }
  581. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  582. int in_len, int out_len)
  583. {
  584. struct rdma_ucm_accept cmd;
  585. struct rdma_conn_param conn_param;
  586. struct ucma_context *ctx;
  587. int ret;
  588. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  589. return -EFAULT;
  590. ctx = ucma_get_ctx(file, cmd.id);
  591. if (IS_ERR(ctx))
  592. return PTR_ERR(ctx);
  593. if (cmd.conn_param.valid) {
  594. ctx->uid = cmd.uid;
  595. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  596. ret = rdma_accept(ctx->cm_id, &conn_param);
  597. } else
  598. ret = rdma_accept(ctx->cm_id, NULL);
  599. ucma_put_ctx(ctx);
  600. return ret;
  601. }
  602. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  603. int in_len, int out_len)
  604. {
  605. struct rdma_ucm_reject cmd;
  606. struct ucma_context *ctx;
  607. int ret;
  608. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  609. return -EFAULT;
  610. ctx = ucma_get_ctx(file, cmd.id);
  611. if (IS_ERR(ctx))
  612. return PTR_ERR(ctx);
  613. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  614. ucma_put_ctx(ctx);
  615. return ret;
  616. }
  617. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  618. int in_len, int out_len)
  619. {
  620. struct rdma_ucm_disconnect cmd;
  621. struct ucma_context *ctx;
  622. int ret;
  623. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  624. return -EFAULT;
  625. ctx = ucma_get_ctx(file, cmd.id);
  626. if (IS_ERR(ctx))
  627. return PTR_ERR(ctx);
  628. ret = rdma_disconnect(ctx->cm_id);
  629. ucma_put_ctx(ctx);
  630. return ret;
  631. }
  632. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  633. const char __user *inbuf,
  634. int in_len, int out_len)
  635. {
  636. struct rdma_ucm_init_qp_attr cmd;
  637. struct ib_uverbs_qp_attr resp;
  638. struct ucma_context *ctx;
  639. struct ib_qp_attr qp_attr;
  640. int ret;
  641. if (out_len < sizeof(resp))
  642. return -ENOSPC;
  643. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  644. return -EFAULT;
  645. ctx = ucma_get_ctx(file, cmd.id);
  646. if (IS_ERR(ctx))
  647. return PTR_ERR(ctx);
  648. resp.qp_attr_mask = 0;
  649. memset(&qp_attr, 0, sizeof qp_attr);
  650. qp_attr.qp_state = cmd.qp_state;
  651. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  652. if (ret)
  653. goto out;
  654. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  655. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  656. &resp, sizeof(resp)))
  657. ret = -EFAULT;
  658. out:
  659. ucma_put_ctx(ctx);
  660. return ret;
  661. }
  662. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  663. void *optval, size_t optlen)
  664. {
  665. int ret = 0;
  666. switch (optname) {
  667. case RDMA_OPTION_ID_TOS:
  668. if (optlen != sizeof(u8)) {
  669. ret = -EINVAL;
  670. break;
  671. }
  672. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  673. break;
  674. default:
  675. ret = -ENOSYS;
  676. }
  677. return ret;
  678. }
  679. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  680. int optname, void *optval, size_t optlen)
  681. {
  682. int ret;
  683. switch (level) {
  684. case RDMA_OPTION_ID:
  685. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  686. break;
  687. default:
  688. ret = -ENOSYS;
  689. }
  690. return ret;
  691. }
  692. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  693. int in_len, int out_len)
  694. {
  695. struct rdma_ucm_set_option cmd;
  696. struct ucma_context *ctx;
  697. void *optval;
  698. int ret;
  699. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  700. return -EFAULT;
  701. ctx = ucma_get_ctx(file, cmd.id);
  702. if (IS_ERR(ctx))
  703. return PTR_ERR(ctx);
  704. optval = kmalloc(cmd.optlen, GFP_KERNEL);
  705. if (!optval) {
  706. ret = -ENOMEM;
  707. goto out1;
  708. }
  709. if (copy_from_user(optval, (void __user *) (unsigned long) cmd.optval,
  710. cmd.optlen)) {
  711. ret = -EFAULT;
  712. goto out2;
  713. }
  714. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  715. cmd.optlen);
  716. out2:
  717. kfree(optval);
  718. out1:
  719. ucma_put_ctx(ctx);
  720. return ret;
  721. }
  722. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  723. int in_len, int out_len)
  724. {
  725. struct rdma_ucm_notify cmd;
  726. struct ucma_context *ctx;
  727. int ret;
  728. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  729. return -EFAULT;
  730. ctx = ucma_get_ctx(file, cmd.id);
  731. if (IS_ERR(ctx))
  732. return PTR_ERR(ctx);
  733. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  734. ucma_put_ctx(ctx);
  735. return ret;
  736. }
  737. static ssize_t ucma_join_multicast(struct ucma_file *file,
  738. const char __user *inbuf,
  739. int in_len, int out_len)
  740. {
  741. struct rdma_ucm_join_mcast cmd;
  742. struct rdma_ucm_create_id_resp resp;
  743. struct ucma_context *ctx;
  744. struct ucma_multicast *mc;
  745. int ret;
  746. if (out_len < sizeof(resp))
  747. return -ENOSPC;
  748. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  749. return -EFAULT;
  750. ctx = ucma_get_ctx(file, cmd.id);
  751. if (IS_ERR(ctx))
  752. return PTR_ERR(ctx);
  753. mutex_lock(&file->mut);
  754. mc = ucma_alloc_multicast(ctx);
  755. if (IS_ERR(mc)) {
  756. ret = PTR_ERR(mc);
  757. goto err1;
  758. }
  759. mc->uid = cmd.uid;
  760. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  761. ret = rdma_join_multicast(ctx->cm_id, &mc->addr, mc);
  762. if (ret)
  763. goto err2;
  764. resp.id = mc->id;
  765. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  766. &resp, sizeof(resp))) {
  767. ret = -EFAULT;
  768. goto err3;
  769. }
  770. mutex_unlock(&file->mut);
  771. ucma_put_ctx(ctx);
  772. return 0;
  773. err3:
  774. rdma_leave_multicast(ctx->cm_id, &mc->addr);
  775. ucma_cleanup_mc_events(mc);
  776. err2:
  777. mutex_lock(&mut);
  778. idr_remove(&multicast_idr, mc->id);
  779. mutex_unlock(&mut);
  780. list_del(&mc->list);
  781. kfree(mc);
  782. err1:
  783. mutex_unlock(&file->mut);
  784. ucma_put_ctx(ctx);
  785. return ret;
  786. }
  787. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  788. const char __user *inbuf,
  789. int in_len, int out_len)
  790. {
  791. struct rdma_ucm_destroy_id cmd;
  792. struct rdma_ucm_destroy_id_resp resp;
  793. struct ucma_multicast *mc;
  794. int ret = 0;
  795. if (out_len < sizeof(resp))
  796. return -ENOSPC;
  797. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  798. return -EFAULT;
  799. mutex_lock(&mut);
  800. mc = idr_find(&multicast_idr, cmd.id);
  801. if (!mc)
  802. mc = ERR_PTR(-ENOENT);
  803. else if (mc->ctx->file != file)
  804. mc = ERR_PTR(-EINVAL);
  805. else {
  806. idr_remove(&multicast_idr, mc->id);
  807. atomic_inc(&mc->ctx->ref);
  808. }
  809. mutex_unlock(&mut);
  810. if (IS_ERR(mc)) {
  811. ret = PTR_ERR(mc);
  812. goto out;
  813. }
  814. rdma_leave_multicast(mc->ctx->cm_id, &mc->addr);
  815. mutex_lock(&mc->ctx->file->mut);
  816. ucma_cleanup_mc_events(mc);
  817. list_del(&mc->list);
  818. mutex_unlock(&mc->ctx->file->mut);
  819. ucma_put_ctx(mc->ctx);
  820. resp.events_reported = mc->events_reported;
  821. kfree(mc);
  822. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  823. &resp, sizeof(resp)))
  824. ret = -EFAULT;
  825. out:
  826. return ret;
  827. }
  828. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  829. {
  830. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  831. if (file1 < file2) {
  832. mutex_lock(&file1->mut);
  833. mutex_lock(&file2->mut);
  834. } else {
  835. mutex_lock(&file2->mut);
  836. mutex_lock(&file1->mut);
  837. }
  838. }
  839. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  840. {
  841. if (file1 < file2) {
  842. mutex_unlock(&file2->mut);
  843. mutex_unlock(&file1->mut);
  844. } else {
  845. mutex_unlock(&file1->mut);
  846. mutex_unlock(&file2->mut);
  847. }
  848. }
  849. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  850. {
  851. struct ucma_event *uevent, *tmp;
  852. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  853. if (uevent->ctx == ctx)
  854. list_move_tail(&uevent->list, &file->event_list);
  855. }
  856. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  857. const char __user *inbuf,
  858. int in_len, int out_len)
  859. {
  860. struct rdma_ucm_migrate_id cmd;
  861. struct rdma_ucm_migrate_resp resp;
  862. struct ucma_context *ctx;
  863. struct file *filp;
  864. struct ucma_file *cur_file;
  865. int ret = 0;
  866. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  867. return -EFAULT;
  868. /* Get current fd to protect against it being closed */
  869. filp = fget(cmd.fd);
  870. if (!filp)
  871. return -ENOENT;
  872. /* Validate current fd and prevent destruction of id. */
  873. ctx = ucma_get_ctx(filp->private_data, cmd.id);
  874. if (IS_ERR(ctx)) {
  875. ret = PTR_ERR(ctx);
  876. goto file_put;
  877. }
  878. cur_file = ctx->file;
  879. if (cur_file == new_file) {
  880. resp.events_reported = ctx->events_reported;
  881. goto response;
  882. }
  883. /*
  884. * Migrate events between fd's, maintaining order, and avoiding new
  885. * events being added before existing events.
  886. */
  887. ucma_lock_files(cur_file, new_file);
  888. mutex_lock(&mut);
  889. list_move_tail(&ctx->list, &new_file->ctx_list);
  890. ucma_move_events(ctx, new_file);
  891. ctx->file = new_file;
  892. resp.events_reported = ctx->events_reported;
  893. mutex_unlock(&mut);
  894. ucma_unlock_files(cur_file, new_file);
  895. response:
  896. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  897. &resp, sizeof(resp)))
  898. ret = -EFAULT;
  899. ucma_put_ctx(ctx);
  900. file_put:
  901. fput(filp);
  902. return ret;
  903. }
  904. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  905. const char __user *inbuf,
  906. int in_len, int out_len) = {
  907. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  908. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  909. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  910. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  911. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  912. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  913. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  914. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  915. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  916. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  917. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  918. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  919. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  920. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  921. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  922. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  923. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  924. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  925. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
  926. };
  927. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  928. size_t len, loff_t *pos)
  929. {
  930. struct ucma_file *file = filp->private_data;
  931. struct rdma_ucm_cmd_hdr hdr;
  932. ssize_t ret;
  933. if (len < sizeof(hdr))
  934. return -EINVAL;
  935. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  936. return -EFAULT;
  937. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  938. return -EINVAL;
  939. if (hdr.in + sizeof(hdr) > len)
  940. return -EINVAL;
  941. if (!ucma_cmd_table[hdr.cmd])
  942. return -ENOSYS;
  943. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  944. if (!ret)
  945. ret = len;
  946. return ret;
  947. }
  948. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  949. {
  950. struct ucma_file *file = filp->private_data;
  951. unsigned int mask = 0;
  952. poll_wait(filp, &file->poll_wait, wait);
  953. if (!list_empty(&file->event_list))
  954. mask = POLLIN | POLLRDNORM;
  955. return mask;
  956. }
  957. static int ucma_open(struct inode *inode, struct file *filp)
  958. {
  959. struct ucma_file *file;
  960. file = kmalloc(sizeof *file, GFP_KERNEL);
  961. if (!file)
  962. return -ENOMEM;
  963. INIT_LIST_HEAD(&file->event_list);
  964. INIT_LIST_HEAD(&file->ctx_list);
  965. init_waitqueue_head(&file->poll_wait);
  966. mutex_init(&file->mut);
  967. filp->private_data = file;
  968. file->filp = filp;
  969. return 0;
  970. }
  971. static int ucma_close(struct inode *inode, struct file *filp)
  972. {
  973. struct ucma_file *file = filp->private_data;
  974. struct ucma_context *ctx, *tmp;
  975. mutex_lock(&file->mut);
  976. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  977. mutex_unlock(&file->mut);
  978. mutex_lock(&mut);
  979. idr_remove(&ctx_idr, ctx->id);
  980. mutex_unlock(&mut);
  981. ucma_free_ctx(ctx);
  982. mutex_lock(&file->mut);
  983. }
  984. mutex_unlock(&file->mut);
  985. kfree(file);
  986. return 0;
  987. }
  988. static const struct file_operations ucma_fops = {
  989. .owner = THIS_MODULE,
  990. .open = ucma_open,
  991. .release = ucma_close,
  992. .write = ucma_write,
  993. .poll = ucma_poll,
  994. };
  995. static struct miscdevice ucma_misc = {
  996. .minor = MISC_DYNAMIC_MINOR,
  997. .name = "rdma_cm",
  998. .fops = &ucma_fops,
  999. };
  1000. static ssize_t show_abi_version(struct device *dev,
  1001. struct device_attribute *attr,
  1002. char *buf)
  1003. {
  1004. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1005. }
  1006. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1007. static int __init ucma_init(void)
  1008. {
  1009. int ret;
  1010. ret = misc_register(&ucma_misc);
  1011. if (ret)
  1012. return ret;
  1013. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1014. if (ret) {
  1015. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1016. goto err;
  1017. }
  1018. return 0;
  1019. err:
  1020. misc_deregister(&ucma_misc);
  1021. return ret;
  1022. }
  1023. static void __exit ucma_cleanup(void)
  1024. {
  1025. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1026. misc_deregister(&ucma_misc);
  1027. idr_destroy(&ctx_idr);
  1028. }
  1029. module_init(ucma_init);
  1030. module_exit(ucma_cleanup);