ucma.c 29 KB

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