ucma.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 <rdma/rdma_user_cm.h>
  42. #include <rdma/ib_marshall.h>
  43. #include <rdma/rdma_cm.h>
  44. #include <rdma/rdma_cm_ib.h>
  45. MODULE_AUTHOR("Sean Hefty");
  46. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. enum {
  49. UCMA_MAX_BACKLOG = 128
  50. };
  51. struct ucma_file {
  52. struct mutex mut;
  53. struct file *filp;
  54. struct list_head ctx_list;
  55. struct list_head event_list;
  56. wait_queue_head_t poll_wait;
  57. };
  58. struct ucma_context {
  59. int id;
  60. struct completion comp;
  61. atomic_t ref;
  62. int events_reported;
  63. int backlog;
  64. struct ucma_file *file;
  65. struct rdma_cm_id *cm_id;
  66. u64 uid;
  67. struct list_head list;
  68. struct list_head mc_list;
  69. };
  70. struct ucma_multicast {
  71. struct ucma_context *ctx;
  72. int id;
  73. int events_reported;
  74. u64 uid;
  75. struct list_head list;
  76. struct sockaddr_storage addr;
  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. rdma_addr_get_dgid(dev_addr,
  472. (union ib_gid *) &resp->ib_route[0].dgid);
  473. rdma_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 = (struct sockaddr *) &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 = (struct sockaddr *) &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 = (__force __u64) 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_ib_path(struct ucma_context *ctx,
  680. struct ib_path_rec_data *path_data, size_t optlen)
  681. {
  682. struct ib_sa_path_rec sa_path;
  683. struct rdma_cm_event event;
  684. int ret;
  685. if (optlen % sizeof(*path_data))
  686. return -EINVAL;
  687. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  688. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  689. IB_PATH_BIDIRECTIONAL))
  690. break;
  691. }
  692. if (!optlen)
  693. return -EINVAL;
  694. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  695. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  696. if (ret)
  697. return ret;
  698. memset(&event, 0, sizeof event);
  699. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  700. return ucma_event_handler(ctx->cm_id, &event);
  701. }
  702. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  703. void *optval, size_t optlen)
  704. {
  705. int ret;
  706. switch (optname) {
  707. case RDMA_OPTION_IB_PATH:
  708. ret = ucma_set_ib_path(ctx, optval, optlen);
  709. break;
  710. default:
  711. ret = -ENOSYS;
  712. }
  713. return ret;
  714. }
  715. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  716. int optname, void *optval, size_t optlen)
  717. {
  718. int ret;
  719. switch (level) {
  720. case RDMA_OPTION_ID:
  721. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  722. break;
  723. case RDMA_OPTION_IB:
  724. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  725. break;
  726. default:
  727. ret = -ENOSYS;
  728. }
  729. return ret;
  730. }
  731. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  732. int in_len, int out_len)
  733. {
  734. struct rdma_ucm_set_option cmd;
  735. struct ucma_context *ctx;
  736. void *optval;
  737. int ret;
  738. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  739. return -EFAULT;
  740. ctx = ucma_get_ctx(file, cmd.id);
  741. if (IS_ERR(ctx))
  742. return PTR_ERR(ctx);
  743. optval = kmalloc(cmd.optlen, GFP_KERNEL);
  744. if (!optval) {
  745. ret = -ENOMEM;
  746. goto out1;
  747. }
  748. if (copy_from_user(optval, (void __user *) (unsigned long) cmd.optval,
  749. cmd.optlen)) {
  750. ret = -EFAULT;
  751. goto out2;
  752. }
  753. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  754. cmd.optlen);
  755. out2:
  756. kfree(optval);
  757. out1:
  758. ucma_put_ctx(ctx);
  759. return ret;
  760. }
  761. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  762. int in_len, int out_len)
  763. {
  764. struct rdma_ucm_notify cmd;
  765. struct ucma_context *ctx;
  766. int ret;
  767. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  768. return -EFAULT;
  769. ctx = ucma_get_ctx(file, cmd.id);
  770. if (IS_ERR(ctx))
  771. return PTR_ERR(ctx);
  772. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  773. ucma_put_ctx(ctx);
  774. return ret;
  775. }
  776. static ssize_t ucma_join_multicast(struct ucma_file *file,
  777. const char __user *inbuf,
  778. int in_len, int out_len)
  779. {
  780. struct rdma_ucm_join_mcast cmd;
  781. struct rdma_ucm_create_id_resp resp;
  782. struct ucma_context *ctx;
  783. struct ucma_multicast *mc;
  784. int ret;
  785. if (out_len < sizeof(resp))
  786. return -ENOSPC;
  787. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  788. return -EFAULT;
  789. ctx = ucma_get_ctx(file, cmd.id);
  790. if (IS_ERR(ctx))
  791. return PTR_ERR(ctx);
  792. mutex_lock(&file->mut);
  793. mc = ucma_alloc_multicast(ctx);
  794. if (!mc) {
  795. ret = -ENOMEM;
  796. goto err1;
  797. }
  798. mc->uid = cmd.uid;
  799. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  800. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  801. if (ret)
  802. goto err2;
  803. resp.id = mc->id;
  804. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  805. &resp, sizeof(resp))) {
  806. ret = -EFAULT;
  807. goto err3;
  808. }
  809. mutex_unlock(&file->mut);
  810. ucma_put_ctx(ctx);
  811. return 0;
  812. err3:
  813. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  814. ucma_cleanup_mc_events(mc);
  815. err2:
  816. mutex_lock(&mut);
  817. idr_remove(&multicast_idr, mc->id);
  818. mutex_unlock(&mut);
  819. list_del(&mc->list);
  820. kfree(mc);
  821. err1:
  822. mutex_unlock(&file->mut);
  823. ucma_put_ctx(ctx);
  824. return ret;
  825. }
  826. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  827. const char __user *inbuf,
  828. int in_len, int out_len)
  829. {
  830. struct rdma_ucm_destroy_id cmd;
  831. struct rdma_ucm_destroy_id_resp resp;
  832. struct ucma_multicast *mc;
  833. int ret = 0;
  834. if (out_len < sizeof(resp))
  835. return -ENOSPC;
  836. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  837. return -EFAULT;
  838. mutex_lock(&mut);
  839. mc = idr_find(&multicast_idr, cmd.id);
  840. if (!mc)
  841. mc = ERR_PTR(-ENOENT);
  842. else if (mc->ctx->file != file)
  843. mc = ERR_PTR(-EINVAL);
  844. else {
  845. idr_remove(&multicast_idr, mc->id);
  846. atomic_inc(&mc->ctx->ref);
  847. }
  848. mutex_unlock(&mut);
  849. if (IS_ERR(mc)) {
  850. ret = PTR_ERR(mc);
  851. goto out;
  852. }
  853. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  854. mutex_lock(&mc->ctx->file->mut);
  855. ucma_cleanup_mc_events(mc);
  856. list_del(&mc->list);
  857. mutex_unlock(&mc->ctx->file->mut);
  858. ucma_put_ctx(mc->ctx);
  859. resp.events_reported = mc->events_reported;
  860. kfree(mc);
  861. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  862. &resp, sizeof(resp)))
  863. ret = -EFAULT;
  864. out:
  865. return ret;
  866. }
  867. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  868. {
  869. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  870. if (file1 < file2) {
  871. mutex_lock(&file1->mut);
  872. mutex_lock(&file2->mut);
  873. } else {
  874. mutex_lock(&file2->mut);
  875. mutex_lock(&file1->mut);
  876. }
  877. }
  878. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  879. {
  880. if (file1 < file2) {
  881. mutex_unlock(&file2->mut);
  882. mutex_unlock(&file1->mut);
  883. } else {
  884. mutex_unlock(&file1->mut);
  885. mutex_unlock(&file2->mut);
  886. }
  887. }
  888. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  889. {
  890. struct ucma_event *uevent, *tmp;
  891. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  892. if (uevent->ctx == ctx)
  893. list_move_tail(&uevent->list, &file->event_list);
  894. }
  895. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  896. const char __user *inbuf,
  897. int in_len, int out_len)
  898. {
  899. struct rdma_ucm_migrate_id cmd;
  900. struct rdma_ucm_migrate_resp resp;
  901. struct ucma_context *ctx;
  902. struct file *filp;
  903. struct ucma_file *cur_file;
  904. int ret = 0;
  905. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  906. return -EFAULT;
  907. /* Get current fd to protect against it being closed */
  908. filp = fget(cmd.fd);
  909. if (!filp)
  910. return -ENOENT;
  911. /* Validate current fd and prevent destruction of id. */
  912. ctx = ucma_get_ctx(filp->private_data, cmd.id);
  913. if (IS_ERR(ctx)) {
  914. ret = PTR_ERR(ctx);
  915. goto file_put;
  916. }
  917. cur_file = ctx->file;
  918. if (cur_file == new_file) {
  919. resp.events_reported = ctx->events_reported;
  920. goto response;
  921. }
  922. /*
  923. * Migrate events between fd's, maintaining order, and avoiding new
  924. * events being added before existing events.
  925. */
  926. ucma_lock_files(cur_file, new_file);
  927. mutex_lock(&mut);
  928. list_move_tail(&ctx->list, &new_file->ctx_list);
  929. ucma_move_events(ctx, new_file);
  930. ctx->file = new_file;
  931. resp.events_reported = ctx->events_reported;
  932. mutex_unlock(&mut);
  933. ucma_unlock_files(cur_file, new_file);
  934. response:
  935. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  936. &resp, sizeof(resp)))
  937. ret = -EFAULT;
  938. ucma_put_ctx(ctx);
  939. file_put:
  940. fput(filp);
  941. return ret;
  942. }
  943. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  944. const char __user *inbuf,
  945. int in_len, int out_len) = {
  946. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  947. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  948. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  949. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  950. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  951. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  952. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  953. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  954. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  955. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  956. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  957. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  958. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  959. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  960. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  961. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  962. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  963. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  964. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
  965. };
  966. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  967. size_t len, loff_t *pos)
  968. {
  969. struct ucma_file *file = filp->private_data;
  970. struct rdma_ucm_cmd_hdr hdr;
  971. ssize_t ret;
  972. if (len < sizeof(hdr))
  973. return -EINVAL;
  974. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  975. return -EFAULT;
  976. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  977. return -EINVAL;
  978. if (hdr.in + sizeof(hdr) > len)
  979. return -EINVAL;
  980. if (!ucma_cmd_table[hdr.cmd])
  981. return -ENOSYS;
  982. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  983. if (!ret)
  984. ret = len;
  985. return ret;
  986. }
  987. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  988. {
  989. struct ucma_file *file = filp->private_data;
  990. unsigned int mask = 0;
  991. poll_wait(filp, &file->poll_wait, wait);
  992. if (!list_empty(&file->event_list))
  993. mask = POLLIN | POLLRDNORM;
  994. return mask;
  995. }
  996. /*
  997. * ucma_open() does not need the BKL:
  998. *
  999. * - no global state is referred to;
  1000. * - there is no ioctl method to race against;
  1001. * - no further module initialization is required for open to work
  1002. * after the device is registered.
  1003. */
  1004. static int ucma_open(struct inode *inode, struct file *filp)
  1005. {
  1006. struct ucma_file *file;
  1007. file = kmalloc(sizeof *file, GFP_KERNEL);
  1008. if (!file)
  1009. return -ENOMEM;
  1010. INIT_LIST_HEAD(&file->event_list);
  1011. INIT_LIST_HEAD(&file->ctx_list);
  1012. init_waitqueue_head(&file->poll_wait);
  1013. mutex_init(&file->mut);
  1014. filp->private_data = file;
  1015. file->filp = filp;
  1016. return 0;
  1017. }
  1018. static int ucma_close(struct inode *inode, struct file *filp)
  1019. {
  1020. struct ucma_file *file = filp->private_data;
  1021. struct ucma_context *ctx, *tmp;
  1022. mutex_lock(&file->mut);
  1023. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1024. mutex_unlock(&file->mut);
  1025. mutex_lock(&mut);
  1026. idr_remove(&ctx_idr, ctx->id);
  1027. mutex_unlock(&mut);
  1028. ucma_free_ctx(ctx);
  1029. mutex_lock(&file->mut);
  1030. }
  1031. mutex_unlock(&file->mut);
  1032. kfree(file);
  1033. return 0;
  1034. }
  1035. static const struct file_operations ucma_fops = {
  1036. .owner = THIS_MODULE,
  1037. .open = ucma_open,
  1038. .release = ucma_close,
  1039. .write = ucma_write,
  1040. .poll = ucma_poll,
  1041. };
  1042. static struct miscdevice ucma_misc = {
  1043. .minor = MISC_DYNAMIC_MINOR,
  1044. .name = "rdma_cm",
  1045. .fops = &ucma_fops,
  1046. };
  1047. static ssize_t show_abi_version(struct device *dev,
  1048. struct device_attribute *attr,
  1049. char *buf)
  1050. {
  1051. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1052. }
  1053. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1054. static int __init ucma_init(void)
  1055. {
  1056. int ret;
  1057. ret = misc_register(&ucma_misc);
  1058. if (ret)
  1059. return ret;
  1060. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1061. if (ret) {
  1062. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1063. goto err;
  1064. }
  1065. return 0;
  1066. err:
  1067. misc_deregister(&ucma_misc);
  1068. return ret;
  1069. }
  1070. static void __exit ucma_cleanup(void)
  1071. {
  1072. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1073. misc_deregister(&ucma_misc);
  1074. idr_destroy(&ctx_idr);
  1075. }
  1076. module_init(ucma_init);
  1077. module_exit(ucma_cleanup);