ucma.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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 <linux/sysctl.h>
  43. #include <linux/module.h>
  44. #include <rdma/rdma_user_cm.h>
  45. #include <rdma/ib_marshall.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <rdma/rdma_cm_ib.h>
  48. #include <rdma/ib_addr.h>
  49. #include <rdma/ib.h>
  50. MODULE_AUTHOR("Sean Hefty");
  51. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. static unsigned int max_backlog = 1024;
  54. static struct ctl_table_header *ucma_ctl_table_hdr;
  55. static struct ctl_table ucma_ctl_table[] = {
  56. {
  57. .procname = "max_backlog",
  58. .data = &max_backlog,
  59. .maxlen = sizeof max_backlog,
  60. .mode = 0644,
  61. .proc_handler = proc_dointvec,
  62. },
  63. { }
  64. };
  65. struct ucma_file {
  66. struct mutex mut;
  67. struct file *filp;
  68. struct list_head ctx_list;
  69. struct list_head event_list;
  70. wait_queue_head_t poll_wait;
  71. };
  72. struct ucma_context {
  73. int id;
  74. struct completion comp;
  75. atomic_t ref;
  76. int events_reported;
  77. int backlog;
  78. struct ucma_file *file;
  79. struct rdma_cm_id *cm_id;
  80. u64 uid;
  81. struct list_head list;
  82. struct list_head mc_list;
  83. };
  84. struct ucma_multicast {
  85. struct ucma_context *ctx;
  86. int id;
  87. int events_reported;
  88. u64 uid;
  89. struct list_head list;
  90. struct sockaddr_storage addr;
  91. };
  92. struct ucma_event {
  93. struct ucma_context *ctx;
  94. struct ucma_multicast *mc;
  95. struct list_head list;
  96. struct rdma_cm_id *cm_id;
  97. struct rdma_ucm_event_resp resp;
  98. };
  99. static DEFINE_MUTEX(mut);
  100. static DEFINE_IDR(ctx_idr);
  101. static DEFINE_IDR(multicast_idr);
  102. static inline struct ucma_context *_ucma_find_context(int id,
  103. struct ucma_file *file)
  104. {
  105. struct ucma_context *ctx;
  106. ctx = idr_find(&ctx_idr, id);
  107. if (!ctx)
  108. ctx = ERR_PTR(-ENOENT);
  109. else if (ctx->file != file)
  110. ctx = ERR_PTR(-EINVAL);
  111. return ctx;
  112. }
  113. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  114. {
  115. struct ucma_context *ctx;
  116. mutex_lock(&mut);
  117. ctx = _ucma_find_context(id, file);
  118. if (!IS_ERR(ctx))
  119. atomic_inc(&ctx->ref);
  120. mutex_unlock(&mut);
  121. return ctx;
  122. }
  123. static void ucma_put_ctx(struct ucma_context *ctx)
  124. {
  125. if (atomic_dec_and_test(&ctx->ref))
  126. complete(&ctx->comp);
  127. }
  128. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  129. {
  130. struct ucma_context *ctx;
  131. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  132. if (!ctx)
  133. return NULL;
  134. atomic_set(&ctx->ref, 1);
  135. init_completion(&ctx->comp);
  136. INIT_LIST_HEAD(&ctx->mc_list);
  137. ctx->file = file;
  138. mutex_lock(&mut);
  139. ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
  140. mutex_unlock(&mut);
  141. if (ctx->id < 0)
  142. goto error;
  143. list_add_tail(&ctx->list, &file->ctx_list);
  144. return ctx;
  145. error:
  146. kfree(ctx);
  147. return NULL;
  148. }
  149. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  150. {
  151. struct ucma_multicast *mc;
  152. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  153. if (!mc)
  154. return NULL;
  155. mutex_lock(&mut);
  156. mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL);
  157. mutex_unlock(&mut);
  158. if (mc->id < 0)
  159. goto error;
  160. mc->ctx = ctx;
  161. list_add_tail(&mc->list, &ctx->mc_list);
  162. return mc;
  163. error:
  164. kfree(mc);
  165. return NULL;
  166. }
  167. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  168. struct rdma_conn_param *src)
  169. {
  170. if (src->private_data_len)
  171. memcpy(dst->private_data, src->private_data,
  172. src->private_data_len);
  173. dst->private_data_len = src->private_data_len;
  174. dst->responder_resources =src->responder_resources;
  175. dst->initiator_depth = src->initiator_depth;
  176. dst->flow_control = src->flow_control;
  177. dst->retry_count = src->retry_count;
  178. dst->rnr_retry_count = src->rnr_retry_count;
  179. dst->srq = src->srq;
  180. dst->qp_num = src->qp_num;
  181. }
  182. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  183. struct rdma_ud_param *src)
  184. {
  185. if (src->private_data_len)
  186. memcpy(dst->private_data, src->private_data,
  187. src->private_data_len);
  188. dst->private_data_len = src->private_data_len;
  189. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  190. dst->qp_num = src->qp_num;
  191. dst->qkey = src->qkey;
  192. }
  193. static void ucma_set_event_context(struct ucma_context *ctx,
  194. struct rdma_cm_event *event,
  195. struct ucma_event *uevent)
  196. {
  197. uevent->ctx = ctx;
  198. switch (event->event) {
  199. case RDMA_CM_EVENT_MULTICAST_JOIN:
  200. case RDMA_CM_EVENT_MULTICAST_ERROR:
  201. uevent->mc = (struct ucma_multicast *)
  202. event->param.ud.private_data;
  203. uevent->resp.uid = uevent->mc->uid;
  204. uevent->resp.id = uevent->mc->id;
  205. break;
  206. default:
  207. uevent->resp.uid = ctx->uid;
  208. uevent->resp.id = ctx->id;
  209. break;
  210. }
  211. }
  212. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  213. struct rdma_cm_event *event)
  214. {
  215. struct ucma_event *uevent;
  216. struct ucma_context *ctx = cm_id->context;
  217. int ret = 0;
  218. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  219. if (!uevent)
  220. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  221. mutex_lock(&ctx->file->mut);
  222. uevent->cm_id = cm_id;
  223. ucma_set_event_context(ctx, event, uevent);
  224. uevent->resp.event = event->event;
  225. uevent->resp.status = event->status;
  226. if (cm_id->qp_type == IB_QPT_UD)
  227. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  228. else
  229. ucma_copy_conn_event(&uevent->resp.param.conn,
  230. &event->param.conn);
  231. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  232. if (!ctx->backlog) {
  233. ret = -ENOMEM;
  234. kfree(uevent);
  235. goto out;
  236. }
  237. ctx->backlog--;
  238. } else if (!ctx->uid || ctx->cm_id != cm_id) {
  239. /*
  240. * We ignore events for new connections until userspace has set
  241. * their context. This can only happen if an error occurs on a
  242. * new connection before the user accepts it. This is okay,
  243. * since the accept will just fail later.
  244. */
  245. kfree(uevent);
  246. goto out;
  247. }
  248. list_add_tail(&uevent->list, &ctx->file->event_list);
  249. wake_up_interruptible(&ctx->file->poll_wait);
  250. out:
  251. mutex_unlock(&ctx->file->mut);
  252. return ret;
  253. }
  254. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  255. int in_len, int out_len)
  256. {
  257. struct ucma_context *ctx;
  258. struct rdma_ucm_get_event cmd;
  259. struct ucma_event *uevent;
  260. int ret = 0;
  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 int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
  302. {
  303. switch (cmd->ps) {
  304. case RDMA_PS_TCP:
  305. *qp_type = IB_QPT_RC;
  306. return 0;
  307. case RDMA_PS_UDP:
  308. case RDMA_PS_IPOIB:
  309. *qp_type = IB_QPT_UD;
  310. return 0;
  311. case RDMA_PS_IB:
  312. *qp_type = cmd->qp_type;
  313. return 0;
  314. default:
  315. return -EINVAL;
  316. }
  317. }
  318. static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
  319. int in_len, int out_len)
  320. {
  321. struct rdma_ucm_create_id cmd;
  322. struct rdma_ucm_create_id_resp resp;
  323. struct ucma_context *ctx;
  324. enum ib_qp_type qp_type;
  325. int ret;
  326. if (out_len < sizeof(resp))
  327. return -ENOSPC;
  328. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  329. return -EFAULT;
  330. ret = ucma_get_qp_type(&cmd, &qp_type);
  331. if (ret)
  332. return ret;
  333. mutex_lock(&file->mut);
  334. ctx = ucma_alloc_ctx(file);
  335. mutex_unlock(&file->mut);
  336. if (!ctx)
  337. return -ENOMEM;
  338. ctx->uid = cmd.uid;
  339. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type);
  340. if (IS_ERR(ctx->cm_id)) {
  341. ret = PTR_ERR(ctx->cm_id);
  342. goto err1;
  343. }
  344. resp.id = ctx->id;
  345. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  346. &resp, sizeof(resp))) {
  347. ret = -EFAULT;
  348. goto err2;
  349. }
  350. return 0;
  351. err2:
  352. rdma_destroy_id(ctx->cm_id);
  353. err1:
  354. mutex_lock(&mut);
  355. idr_remove(&ctx_idr, ctx->id);
  356. mutex_unlock(&mut);
  357. kfree(ctx);
  358. return ret;
  359. }
  360. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  361. {
  362. struct ucma_multicast *mc, *tmp;
  363. mutex_lock(&mut);
  364. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  365. list_del(&mc->list);
  366. idr_remove(&multicast_idr, mc->id);
  367. kfree(mc);
  368. }
  369. mutex_unlock(&mut);
  370. }
  371. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  372. {
  373. struct ucma_event *uevent, *tmp;
  374. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  375. if (uevent->mc != mc)
  376. continue;
  377. list_del(&uevent->list);
  378. kfree(uevent);
  379. }
  380. }
  381. /*
  382. * We cannot hold file->mut when calling rdma_destroy_id() or we can
  383. * deadlock. We also acquire file->mut in ucma_event_handler(), and
  384. * rdma_destroy_id() will wait until all callbacks have completed.
  385. */
  386. static int ucma_free_ctx(struct ucma_context *ctx)
  387. {
  388. int events_reported;
  389. struct ucma_event *uevent, *tmp;
  390. LIST_HEAD(list);
  391. /* No new events will be generated after destroying the id. */
  392. rdma_destroy_id(ctx->cm_id);
  393. ucma_cleanup_multicast(ctx);
  394. /* Cleanup events not yet reported to the user. */
  395. mutex_lock(&ctx->file->mut);
  396. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  397. if (uevent->ctx == ctx)
  398. list_move_tail(&uevent->list, &list);
  399. }
  400. list_del(&ctx->list);
  401. mutex_unlock(&ctx->file->mut);
  402. list_for_each_entry_safe(uevent, tmp, &list, list) {
  403. list_del(&uevent->list);
  404. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  405. rdma_destroy_id(uevent->cm_id);
  406. kfree(uevent);
  407. }
  408. events_reported = ctx->events_reported;
  409. kfree(ctx);
  410. return events_reported;
  411. }
  412. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  413. int in_len, int out_len)
  414. {
  415. struct rdma_ucm_destroy_id cmd;
  416. struct rdma_ucm_destroy_id_resp resp;
  417. struct ucma_context *ctx;
  418. int ret = 0;
  419. if (out_len < sizeof(resp))
  420. return -ENOSPC;
  421. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  422. return -EFAULT;
  423. mutex_lock(&mut);
  424. ctx = _ucma_find_context(cmd.id, file);
  425. if (!IS_ERR(ctx))
  426. idr_remove(&ctx_idr, ctx->id);
  427. mutex_unlock(&mut);
  428. if (IS_ERR(ctx))
  429. return PTR_ERR(ctx);
  430. ucma_put_ctx(ctx);
  431. wait_for_completion(&ctx->comp);
  432. resp.events_reported = ucma_free_ctx(ctx);
  433. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  434. &resp, sizeof(resp)))
  435. ret = -EFAULT;
  436. return ret;
  437. }
  438. static ssize_t ucma_bind_ip(struct ucma_file *file, const char __user *inbuf,
  439. int in_len, int out_len)
  440. {
  441. struct rdma_ucm_bind_ip cmd;
  442. struct ucma_context *ctx;
  443. int ret;
  444. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  445. return -EFAULT;
  446. ctx = ucma_get_ctx(file, cmd.id);
  447. if (IS_ERR(ctx))
  448. return PTR_ERR(ctx);
  449. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  450. ucma_put_ctx(ctx);
  451. return ret;
  452. }
  453. static ssize_t ucma_bind(struct ucma_file *file, const char __user *inbuf,
  454. int in_len, int out_len)
  455. {
  456. struct rdma_ucm_bind cmd;
  457. struct sockaddr *addr;
  458. struct ucma_context *ctx;
  459. int ret;
  460. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  461. return -EFAULT;
  462. addr = (struct sockaddr *) &cmd.addr;
  463. if (cmd.reserved || !cmd.addr_size || (cmd.addr_size != rdma_addr_size(addr)))
  464. return -EINVAL;
  465. ctx = ucma_get_ctx(file, cmd.id);
  466. if (IS_ERR(ctx))
  467. return PTR_ERR(ctx);
  468. ret = rdma_bind_addr(ctx->cm_id, addr);
  469. ucma_put_ctx(ctx);
  470. return ret;
  471. }
  472. static ssize_t ucma_resolve_ip(struct ucma_file *file,
  473. const char __user *inbuf,
  474. int in_len, int out_len)
  475. {
  476. struct rdma_ucm_resolve_ip cmd;
  477. struct ucma_context *ctx;
  478. int ret;
  479. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  480. return -EFAULT;
  481. ctx = ucma_get_ctx(file, cmd.id);
  482. if (IS_ERR(ctx))
  483. return PTR_ERR(ctx);
  484. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  485. (struct sockaddr *) &cmd.dst_addr,
  486. cmd.timeout_ms);
  487. ucma_put_ctx(ctx);
  488. return ret;
  489. }
  490. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  491. const char __user *inbuf,
  492. int in_len, int out_len)
  493. {
  494. struct rdma_ucm_resolve_addr cmd;
  495. struct sockaddr *src, *dst;
  496. struct ucma_context *ctx;
  497. int ret;
  498. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  499. return -EFAULT;
  500. src = (struct sockaddr *) &cmd.src_addr;
  501. dst = (struct sockaddr *) &cmd.dst_addr;
  502. if (cmd.reserved || (cmd.src_size && (cmd.src_size != rdma_addr_size(src))) ||
  503. !cmd.dst_size || (cmd.dst_size != rdma_addr_size(dst)))
  504. return -EINVAL;
  505. ctx = ucma_get_ctx(file, cmd.id);
  506. if (IS_ERR(ctx))
  507. return PTR_ERR(ctx);
  508. ret = rdma_resolve_addr(ctx->cm_id, src, dst, cmd.timeout_ms);
  509. ucma_put_ctx(ctx);
  510. return ret;
  511. }
  512. static ssize_t ucma_resolve_route(struct ucma_file *file,
  513. const char __user *inbuf,
  514. int in_len, int out_len)
  515. {
  516. struct rdma_ucm_resolve_route cmd;
  517. struct ucma_context *ctx;
  518. int ret;
  519. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  520. return -EFAULT;
  521. ctx = ucma_get_ctx(file, cmd.id);
  522. if (IS_ERR(ctx))
  523. return PTR_ERR(ctx);
  524. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  525. ucma_put_ctx(ctx);
  526. return ret;
  527. }
  528. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  529. struct rdma_route *route)
  530. {
  531. struct rdma_dev_addr *dev_addr;
  532. resp->num_paths = route->num_paths;
  533. switch (route->num_paths) {
  534. case 0:
  535. dev_addr = &route->addr.dev_addr;
  536. rdma_addr_get_dgid(dev_addr,
  537. (union ib_gid *) &resp->ib_route[0].dgid);
  538. rdma_addr_get_sgid(dev_addr,
  539. (union ib_gid *) &resp->ib_route[0].sgid);
  540. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  541. break;
  542. case 2:
  543. ib_copy_path_rec_to_user(&resp->ib_route[1],
  544. &route->path_rec[1]);
  545. /* fall through */
  546. case 1:
  547. ib_copy_path_rec_to_user(&resp->ib_route[0],
  548. &route->path_rec[0]);
  549. break;
  550. default:
  551. break;
  552. }
  553. }
  554. static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
  555. struct rdma_route *route)
  556. {
  557. resp->num_paths = route->num_paths;
  558. switch (route->num_paths) {
  559. case 0:
  560. rdma_ip2gid((struct sockaddr *)&route->addr.dst_addr,
  561. (union ib_gid *)&resp->ib_route[0].dgid);
  562. rdma_ip2gid((struct sockaddr *)&route->addr.src_addr,
  563. (union ib_gid *)&resp->ib_route[0].sgid);
  564. resp->ib_route[0].pkey = cpu_to_be16(0xffff);
  565. break;
  566. case 2:
  567. ib_copy_path_rec_to_user(&resp->ib_route[1],
  568. &route->path_rec[1]);
  569. /* fall through */
  570. case 1:
  571. ib_copy_path_rec_to_user(&resp->ib_route[0],
  572. &route->path_rec[0]);
  573. break;
  574. default:
  575. break;
  576. }
  577. }
  578. static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
  579. struct rdma_route *route)
  580. {
  581. struct rdma_dev_addr *dev_addr;
  582. dev_addr = &route->addr.dev_addr;
  583. rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
  584. rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
  585. }
  586. static ssize_t ucma_query_route(struct ucma_file *file,
  587. const char __user *inbuf,
  588. int in_len, int out_len)
  589. {
  590. struct rdma_ucm_query cmd;
  591. struct rdma_ucm_query_route_resp resp;
  592. struct ucma_context *ctx;
  593. struct sockaddr *addr;
  594. int ret = 0;
  595. if (out_len < sizeof(resp))
  596. return -ENOSPC;
  597. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  598. return -EFAULT;
  599. ctx = ucma_get_ctx(file, cmd.id);
  600. if (IS_ERR(ctx))
  601. return PTR_ERR(ctx);
  602. memset(&resp, 0, sizeof resp);
  603. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  604. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  605. sizeof(struct sockaddr_in) :
  606. sizeof(struct sockaddr_in6));
  607. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  608. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  609. sizeof(struct sockaddr_in) :
  610. sizeof(struct sockaddr_in6));
  611. if (!ctx->cm_id->device)
  612. goto out;
  613. resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
  614. resp.port_num = ctx->cm_id->port_num;
  615. if (rdma_cap_ib_sa(ctx->cm_id->device, ctx->cm_id->port_num))
  616. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  617. else if (rdma_protocol_roce(ctx->cm_id->device, ctx->cm_id->port_num))
  618. ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
  619. else if (rdma_protocol_iwarp(ctx->cm_id->device, ctx->cm_id->port_num))
  620. ucma_copy_iw_route(&resp, &ctx->cm_id->route);
  621. out:
  622. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  623. &resp, sizeof(resp)))
  624. ret = -EFAULT;
  625. ucma_put_ctx(ctx);
  626. return ret;
  627. }
  628. static void ucma_query_device_addr(struct rdma_cm_id *cm_id,
  629. struct rdma_ucm_query_addr_resp *resp)
  630. {
  631. if (!cm_id->device)
  632. return;
  633. resp->node_guid = (__force __u64) cm_id->device->node_guid;
  634. resp->port_num = cm_id->port_num;
  635. resp->pkey = (__force __u16) cpu_to_be16(
  636. ib_addr_get_pkey(&cm_id->route.addr.dev_addr));
  637. }
  638. static ssize_t ucma_query_addr(struct ucma_context *ctx,
  639. void __user *response, int out_len)
  640. {
  641. struct rdma_ucm_query_addr_resp resp;
  642. struct sockaddr *addr;
  643. int ret = 0;
  644. if (out_len < sizeof(resp))
  645. return -ENOSPC;
  646. memset(&resp, 0, sizeof resp);
  647. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  648. resp.src_size = rdma_addr_size(addr);
  649. memcpy(&resp.src_addr, addr, resp.src_size);
  650. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  651. resp.dst_size = rdma_addr_size(addr);
  652. memcpy(&resp.dst_addr, addr, resp.dst_size);
  653. ucma_query_device_addr(ctx->cm_id, &resp);
  654. if (copy_to_user(response, &resp, sizeof(resp)))
  655. ret = -EFAULT;
  656. return ret;
  657. }
  658. static ssize_t ucma_query_path(struct ucma_context *ctx,
  659. void __user *response, int out_len)
  660. {
  661. struct rdma_ucm_query_path_resp *resp;
  662. int i, ret = 0;
  663. if (out_len < sizeof(*resp))
  664. return -ENOSPC;
  665. resp = kzalloc(out_len, GFP_KERNEL);
  666. if (!resp)
  667. return -ENOMEM;
  668. resp->num_paths = ctx->cm_id->route.num_paths;
  669. for (i = 0, out_len -= sizeof(*resp);
  670. i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data);
  671. i++, out_len -= sizeof(struct ib_path_rec_data)) {
  672. resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY |
  673. IB_PATH_BIDIRECTIONAL;
  674. ib_sa_pack_path(&ctx->cm_id->route.path_rec[i],
  675. &resp->path_data[i].path_rec);
  676. }
  677. if (copy_to_user(response, resp,
  678. sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
  679. ret = -EFAULT;
  680. kfree(resp);
  681. return ret;
  682. }
  683. static ssize_t ucma_query_gid(struct ucma_context *ctx,
  684. void __user *response, int out_len)
  685. {
  686. struct rdma_ucm_query_addr_resp resp;
  687. struct sockaddr_ib *addr;
  688. int ret = 0;
  689. if (out_len < sizeof(resp))
  690. return -ENOSPC;
  691. memset(&resp, 0, sizeof resp);
  692. ucma_query_device_addr(ctx->cm_id, &resp);
  693. addr = (struct sockaddr_ib *) &resp.src_addr;
  694. resp.src_size = sizeof(*addr);
  695. if (ctx->cm_id->route.addr.src_addr.ss_family == AF_IB) {
  696. memcpy(addr, &ctx->cm_id->route.addr.src_addr, resp.src_size);
  697. } else {
  698. addr->sib_family = AF_IB;
  699. addr->sib_pkey = (__force __be16) resp.pkey;
  700. rdma_addr_get_sgid(&ctx->cm_id->route.addr.dev_addr,
  701. (union ib_gid *) &addr->sib_addr);
  702. addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
  703. &ctx->cm_id->route.addr.src_addr);
  704. }
  705. addr = (struct sockaddr_ib *) &resp.dst_addr;
  706. resp.dst_size = sizeof(*addr);
  707. if (ctx->cm_id->route.addr.dst_addr.ss_family == AF_IB) {
  708. memcpy(addr, &ctx->cm_id->route.addr.dst_addr, resp.dst_size);
  709. } else {
  710. addr->sib_family = AF_IB;
  711. addr->sib_pkey = (__force __be16) resp.pkey;
  712. rdma_addr_get_dgid(&ctx->cm_id->route.addr.dev_addr,
  713. (union ib_gid *) &addr->sib_addr);
  714. addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
  715. &ctx->cm_id->route.addr.dst_addr);
  716. }
  717. if (copy_to_user(response, &resp, sizeof(resp)))
  718. ret = -EFAULT;
  719. return ret;
  720. }
  721. static ssize_t ucma_query(struct ucma_file *file,
  722. const char __user *inbuf,
  723. int in_len, int out_len)
  724. {
  725. struct rdma_ucm_query cmd;
  726. struct ucma_context *ctx;
  727. void __user *response;
  728. int ret;
  729. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  730. return -EFAULT;
  731. response = (void __user *)(unsigned long) cmd.response;
  732. ctx = ucma_get_ctx(file, cmd.id);
  733. if (IS_ERR(ctx))
  734. return PTR_ERR(ctx);
  735. switch (cmd.option) {
  736. case RDMA_USER_CM_QUERY_ADDR:
  737. ret = ucma_query_addr(ctx, response, out_len);
  738. break;
  739. case RDMA_USER_CM_QUERY_PATH:
  740. ret = ucma_query_path(ctx, response, out_len);
  741. break;
  742. case RDMA_USER_CM_QUERY_GID:
  743. ret = ucma_query_gid(ctx, response, out_len);
  744. break;
  745. default:
  746. ret = -ENOSYS;
  747. break;
  748. }
  749. ucma_put_ctx(ctx);
  750. return ret;
  751. }
  752. static void ucma_copy_conn_param(struct rdma_cm_id *id,
  753. struct rdma_conn_param *dst,
  754. struct rdma_ucm_conn_param *src)
  755. {
  756. dst->private_data = src->private_data;
  757. dst->private_data_len = src->private_data_len;
  758. dst->responder_resources =src->responder_resources;
  759. dst->initiator_depth = src->initiator_depth;
  760. dst->flow_control = src->flow_control;
  761. dst->retry_count = src->retry_count;
  762. dst->rnr_retry_count = src->rnr_retry_count;
  763. dst->srq = src->srq;
  764. dst->qp_num = src->qp_num;
  765. dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
  766. }
  767. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  768. int in_len, int out_len)
  769. {
  770. struct rdma_ucm_connect cmd;
  771. struct rdma_conn_param conn_param;
  772. struct ucma_context *ctx;
  773. int ret;
  774. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  775. return -EFAULT;
  776. if (!cmd.conn_param.valid)
  777. return -EINVAL;
  778. ctx = ucma_get_ctx(file, cmd.id);
  779. if (IS_ERR(ctx))
  780. return PTR_ERR(ctx);
  781. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  782. ret = rdma_connect(ctx->cm_id, &conn_param);
  783. ucma_put_ctx(ctx);
  784. return ret;
  785. }
  786. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  787. int in_len, int out_len)
  788. {
  789. struct rdma_ucm_listen cmd;
  790. struct ucma_context *ctx;
  791. int ret;
  792. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  793. return -EFAULT;
  794. ctx = ucma_get_ctx(file, cmd.id);
  795. if (IS_ERR(ctx))
  796. return PTR_ERR(ctx);
  797. ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
  798. cmd.backlog : max_backlog;
  799. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  800. ucma_put_ctx(ctx);
  801. return ret;
  802. }
  803. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  804. int in_len, int out_len)
  805. {
  806. struct rdma_ucm_accept cmd;
  807. struct rdma_conn_param conn_param;
  808. struct ucma_context *ctx;
  809. int ret;
  810. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  811. return -EFAULT;
  812. ctx = ucma_get_ctx(file, cmd.id);
  813. if (IS_ERR(ctx))
  814. return PTR_ERR(ctx);
  815. if (cmd.conn_param.valid) {
  816. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  817. mutex_lock(&file->mut);
  818. ret = rdma_accept(ctx->cm_id, &conn_param);
  819. if (!ret)
  820. ctx->uid = cmd.uid;
  821. mutex_unlock(&file->mut);
  822. } else
  823. ret = rdma_accept(ctx->cm_id, NULL);
  824. ucma_put_ctx(ctx);
  825. return ret;
  826. }
  827. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  828. int in_len, int out_len)
  829. {
  830. struct rdma_ucm_reject cmd;
  831. struct ucma_context *ctx;
  832. int ret;
  833. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  834. return -EFAULT;
  835. ctx = ucma_get_ctx(file, cmd.id);
  836. if (IS_ERR(ctx))
  837. return PTR_ERR(ctx);
  838. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  839. ucma_put_ctx(ctx);
  840. return ret;
  841. }
  842. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  843. int in_len, int out_len)
  844. {
  845. struct rdma_ucm_disconnect cmd;
  846. struct ucma_context *ctx;
  847. int ret;
  848. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  849. return -EFAULT;
  850. ctx = ucma_get_ctx(file, cmd.id);
  851. if (IS_ERR(ctx))
  852. return PTR_ERR(ctx);
  853. ret = rdma_disconnect(ctx->cm_id);
  854. ucma_put_ctx(ctx);
  855. return ret;
  856. }
  857. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  858. const char __user *inbuf,
  859. int in_len, int out_len)
  860. {
  861. struct rdma_ucm_init_qp_attr cmd;
  862. struct ib_uverbs_qp_attr resp;
  863. struct ucma_context *ctx;
  864. struct ib_qp_attr qp_attr;
  865. int ret;
  866. if (out_len < sizeof(resp))
  867. return -ENOSPC;
  868. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  869. return -EFAULT;
  870. ctx = ucma_get_ctx(file, cmd.id);
  871. if (IS_ERR(ctx))
  872. return PTR_ERR(ctx);
  873. resp.qp_attr_mask = 0;
  874. memset(&qp_attr, 0, sizeof qp_attr);
  875. qp_attr.qp_state = cmd.qp_state;
  876. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  877. if (ret)
  878. goto out;
  879. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  880. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  881. &resp, sizeof(resp)))
  882. ret = -EFAULT;
  883. out:
  884. ucma_put_ctx(ctx);
  885. return ret;
  886. }
  887. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  888. void *optval, size_t optlen)
  889. {
  890. int ret = 0;
  891. switch (optname) {
  892. case RDMA_OPTION_ID_TOS:
  893. if (optlen != sizeof(u8)) {
  894. ret = -EINVAL;
  895. break;
  896. }
  897. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  898. break;
  899. case RDMA_OPTION_ID_REUSEADDR:
  900. if (optlen != sizeof(int)) {
  901. ret = -EINVAL;
  902. break;
  903. }
  904. ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
  905. break;
  906. case RDMA_OPTION_ID_AFONLY:
  907. if (optlen != sizeof(int)) {
  908. ret = -EINVAL;
  909. break;
  910. }
  911. ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
  912. break;
  913. default:
  914. ret = -ENOSYS;
  915. }
  916. return ret;
  917. }
  918. static int ucma_set_ib_path(struct ucma_context *ctx,
  919. struct ib_path_rec_data *path_data, size_t optlen)
  920. {
  921. struct ib_sa_path_rec sa_path;
  922. struct rdma_cm_event event;
  923. int ret;
  924. if (optlen % sizeof(*path_data))
  925. return -EINVAL;
  926. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  927. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  928. IB_PATH_BIDIRECTIONAL))
  929. break;
  930. }
  931. if (!optlen)
  932. return -EINVAL;
  933. memset(&sa_path, 0, sizeof(sa_path));
  934. sa_path.vlan_id = 0xffff;
  935. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  936. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  937. if (ret)
  938. return ret;
  939. memset(&event, 0, sizeof event);
  940. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  941. return ucma_event_handler(ctx->cm_id, &event);
  942. }
  943. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  944. void *optval, size_t optlen)
  945. {
  946. int ret;
  947. switch (optname) {
  948. case RDMA_OPTION_IB_PATH:
  949. ret = ucma_set_ib_path(ctx, optval, optlen);
  950. break;
  951. default:
  952. ret = -ENOSYS;
  953. }
  954. return ret;
  955. }
  956. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  957. int optname, void *optval, size_t optlen)
  958. {
  959. int ret;
  960. switch (level) {
  961. case RDMA_OPTION_ID:
  962. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  963. break;
  964. case RDMA_OPTION_IB:
  965. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  966. break;
  967. default:
  968. ret = -ENOSYS;
  969. }
  970. return ret;
  971. }
  972. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  973. int in_len, int out_len)
  974. {
  975. struct rdma_ucm_set_option cmd;
  976. struct ucma_context *ctx;
  977. void *optval;
  978. int ret;
  979. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  980. return -EFAULT;
  981. ctx = ucma_get_ctx(file, cmd.id);
  982. if (IS_ERR(ctx))
  983. return PTR_ERR(ctx);
  984. optval = memdup_user((void __user *) (unsigned long) cmd.optval,
  985. cmd.optlen);
  986. if (IS_ERR(optval)) {
  987. ret = PTR_ERR(optval);
  988. goto out;
  989. }
  990. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  991. cmd.optlen);
  992. kfree(optval);
  993. out:
  994. ucma_put_ctx(ctx);
  995. return ret;
  996. }
  997. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  998. int in_len, int out_len)
  999. {
  1000. struct rdma_ucm_notify cmd;
  1001. struct ucma_context *ctx;
  1002. int ret;
  1003. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1004. return -EFAULT;
  1005. ctx = ucma_get_ctx(file, cmd.id);
  1006. if (IS_ERR(ctx))
  1007. return PTR_ERR(ctx);
  1008. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  1009. ucma_put_ctx(ctx);
  1010. return ret;
  1011. }
  1012. static ssize_t ucma_process_join(struct ucma_file *file,
  1013. struct rdma_ucm_join_mcast *cmd, int out_len)
  1014. {
  1015. struct rdma_ucm_create_id_resp resp;
  1016. struct ucma_context *ctx;
  1017. struct ucma_multicast *mc;
  1018. struct sockaddr *addr;
  1019. int ret;
  1020. if (out_len < sizeof(resp))
  1021. return -ENOSPC;
  1022. addr = (struct sockaddr *) &cmd->addr;
  1023. if (cmd->reserved || !cmd->addr_size || (cmd->addr_size != rdma_addr_size(addr)))
  1024. return -EINVAL;
  1025. ctx = ucma_get_ctx(file, cmd->id);
  1026. if (IS_ERR(ctx))
  1027. return PTR_ERR(ctx);
  1028. mutex_lock(&file->mut);
  1029. mc = ucma_alloc_multicast(ctx);
  1030. if (!mc) {
  1031. ret = -ENOMEM;
  1032. goto err1;
  1033. }
  1034. mc->uid = cmd->uid;
  1035. memcpy(&mc->addr, addr, cmd->addr_size);
  1036. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  1037. if (ret)
  1038. goto err2;
  1039. resp.id = mc->id;
  1040. if (copy_to_user((void __user *)(unsigned long) cmd->response,
  1041. &resp, sizeof(resp))) {
  1042. ret = -EFAULT;
  1043. goto err3;
  1044. }
  1045. mutex_unlock(&file->mut);
  1046. ucma_put_ctx(ctx);
  1047. return 0;
  1048. err3:
  1049. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  1050. ucma_cleanup_mc_events(mc);
  1051. err2:
  1052. mutex_lock(&mut);
  1053. idr_remove(&multicast_idr, mc->id);
  1054. mutex_unlock(&mut);
  1055. list_del(&mc->list);
  1056. kfree(mc);
  1057. err1:
  1058. mutex_unlock(&file->mut);
  1059. ucma_put_ctx(ctx);
  1060. return ret;
  1061. }
  1062. static ssize_t ucma_join_ip_multicast(struct ucma_file *file,
  1063. const char __user *inbuf,
  1064. int in_len, int out_len)
  1065. {
  1066. struct rdma_ucm_join_ip_mcast cmd;
  1067. struct rdma_ucm_join_mcast join_cmd;
  1068. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1069. return -EFAULT;
  1070. join_cmd.response = cmd.response;
  1071. join_cmd.uid = cmd.uid;
  1072. join_cmd.id = cmd.id;
  1073. join_cmd.addr_size = rdma_addr_size((struct sockaddr *) &cmd.addr);
  1074. join_cmd.reserved = 0;
  1075. memcpy(&join_cmd.addr, &cmd.addr, join_cmd.addr_size);
  1076. return ucma_process_join(file, &join_cmd, out_len);
  1077. }
  1078. static ssize_t ucma_join_multicast(struct ucma_file *file,
  1079. const char __user *inbuf,
  1080. int in_len, int out_len)
  1081. {
  1082. struct rdma_ucm_join_mcast cmd;
  1083. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1084. return -EFAULT;
  1085. return ucma_process_join(file, &cmd, out_len);
  1086. }
  1087. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  1088. const char __user *inbuf,
  1089. int in_len, int out_len)
  1090. {
  1091. struct rdma_ucm_destroy_id cmd;
  1092. struct rdma_ucm_destroy_id_resp resp;
  1093. struct ucma_multicast *mc;
  1094. int ret = 0;
  1095. if (out_len < sizeof(resp))
  1096. return -ENOSPC;
  1097. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1098. return -EFAULT;
  1099. mutex_lock(&mut);
  1100. mc = idr_find(&multicast_idr, cmd.id);
  1101. if (!mc)
  1102. mc = ERR_PTR(-ENOENT);
  1103. else if (mc->ctx->file != file)
  1104. mc = ERR_PTR(-EINVAL);
  1105. else {
  1106. idr_remove(&multicast_idr, mc->id);
  1107. atomic_inc(&mc->ctx->ref);
  1108. }
  1109. mutex_unlock(&mut);
  1110. if (IS_ERR(mc)) {
  1111. ret = PTR_ERR(mc);
  1112. goto out;
  1113. }
  1114. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  1115. mutex_lock(&mc->ctx->file->mut);
  1116. ucma_cleanup_mc_events(mc);
  1117. list_del(&mc->list);
  1118. mutex_unlock(&mc->ctx->file->mut);
  1119. ucma_put_ctx(mc->ctx);
  1120. resp.events_reported = mc->events_reported;
  1121. kfree(mc);
  1122. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1123. &resp, sizeof(resp)))
  1124. ret = -EFAULT;
  1125. out:
  1126. return ret;
  1127. }
  1128. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  1129. {
  1130. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  1131. if (file1 < file2) {
  1132. mutex_lock(&file1->mut);
  1133. mutex_lock(&file2->mut);
  1134. } else {
  1135. mutex_lock(&file2->mut);
  1136. mutex_lock(&file1->mut);
  1137. }
  1138. }
  1139. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  1140. {
  1141. if (file1 < file2) {
  1142. mutex_unlock(&file2->mut);
  1143. mutex_unlock(&file1->mut);
  1144. } else {
  1145. mutex_unlock(&file1->mut);
  1146. mutex_unlock(&file2->mut);
  1147. }
  1148. }
  1149. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  1150. {
  1151. struct ucma_event *uevent, *tmp;
  1152. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  1153. if (uevent->ctx == ctx)
  1154. list_move_tail(&uevent->list, &file->event_list);
  1155. }
  1156. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  1157. const char __user *inbuf,
  1158. int in_len, int out_len)
  1159. {
  1160. struct rdma_ucm_migrate_id cmd;
  1161. struct rdma_ucm_migrate_resp resp;
  1162. struct ucma_context *ctx;
  1163. struct fd f;
  1164. struct ucma_file *cur_file;
  1165. int ret = 0;
  1166. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1167. return -EFAULT;
  1168. /* Get current fd to protect against it being closed */
  1169. f = fdget(cmd.fd);
  1170. if (!f.file)
  1171. return -ENOENT;
  1172. /* Validate current fd and prevent destruction of id. */
  1173. ctx = ucma_get_ctx(f.file->private_data, cmd.id);
  1174. if (IS_ERR(ctx)) {
  1175. ret = PTR_ERR(ctx);
  1176. goto file_put;
  1177. }
  1178. cur_file = ctx->file;
  1179. if (cur_file == new_file) {
  1180. resp.events_reported = ctx->events_reported;
  1181. goto response;
  1182. }
  1183. /*
  1184. * Migrate events between fd's, maintaining order, and avoiding new
  1185. * events being added before existing events.
  1186. */
  1187. ucma_lock_files(cur_file, new_file);
  1188. mutex_lock(&mut);
  1189. list_move_tail(&ctx->list, &new_file->ctx_list);
  1190. ucma_move_events(ctx, new_file);
  1191. ctx->file = new_file;
  1192. resp.events_reported = ctx->events_reported;
  1193. mutex_unlock(&mut);
  1194. ucma_unlock_files(cur_file, new_file);
  1195. response:
  1196. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1197. &resp, sizeof(resp)))
  1198. ret = -EFAULT;
  1199. ucma_put_ctx(ctx);
  1200. file_put:
  1201. fdput(f);
  1202. return ret;
  1203. }
  1204. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  1205. const char __user *inbuf,
  1206. int in_len, int out_len) = {
  1207. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  1208. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  1209. [RDMA_USER_CM_CMD_BIND_IP] = ucma_bind_ip,
  1210. [RDMA_USER_CM_CMD_RESOLVE_IP] = ucma_resolve_ip,
  1211. [RDMA_USER_CM_CMD_RESOLVE_ROUTE] = ucma_resolve_route,
  1212. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  1213. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  1214. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  1215. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  1216. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  1217. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  1218. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  1219. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  1220. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  1221. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  1222. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  1223. [RDMA_USER_CM_CMD_JOIN_IP_MCAST] = ucma_join_ip_multicast,
  1224. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  1225. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id,
  1226. [RDMA_USER_CM_CMD_QUERY] = ucma_query,
  1227. [RDMA_USER_CM_CMD_BIND] = ucma_bind,
  1228. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  1229. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast
  1230. };
  1231. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  1232. size_t len, loff_t *pos)
  1233. {
  1234. struct ucma_file *file = filp->private_data;
  1235. struct rdma_ucm_cmd_hdr hdr;
  1236. ssize_t ret;
  1237. if (len < sizeof(hdr))
  1238. return -EINVAL;
  1239. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1240. return -EFAULT;
  1241. if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1242. return -EINVAL;
  1243. if (hdr.in + sizeof(hdr) > len)
  1244. return -EINVAL;
  1245. if (!ucma_cmd_table[hdr.cmd])
  1246. return -ENOSYS;
  1247. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1248. if (!ret)
  1249. ret = len;
  1250. return ret;
  1251. }
  1252. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1253. {
  1254. struct ucma_file *file = filp->private_data;
  1255. unsigned int mask = 0;
  1256. poll_wait(filp, &file->poll_wait, wait);
  1257. if (!list_empty(&file->event_list))
  1258. mask = POLLIN | POLLRDNORM;
  1259. return mask;
  1260. }
  1261. /*
  1262. * ucma_open() does not need the BKL:
  1263. *
  1264. * - no global state is referred to;
  1265. * - there is no ioctl method to race against;
  1266. * - no further module initialization is required for open to work
  1267. * after the device is registered.
  1268. */
  1269. static int ucma_open(struct inode *inode, struct file *filp)
  1270. {
  1271. struct ucma_file *file;
  1272. file = kmalloc(sizeof *file, GFP_KERNEL);
  1273. if (!file)
  1274. return -ENOMEM;
  1275. INIT_LIST_HEAD(&file->event_list);
  1276. INIT_LIST_HEAD(&file->ctx_list);
  1277. init_waitqueue_head(&file->poll_wait);
  1278. mutex_init(&file->mut);
  1279. filp->private_data = file;
  1280. file->filp = filp;
  1281. return nonseekable_open(inode, filp);
  1282. }
  1283. static int ucma_close(struct inode *inode, struct file *filp)
  1284. {
  1285. struct ucma_file *file = filp->private_data;
  1286. struct ucma_context *ctx, *tmp;
  1287. mutex_lock(&file->mut);
  1288. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1289. mutex_unlock(&file->mut);
  1290. mutex_lock(&mut);
  1291. idr_remove(&ctx_idr, ctx->id);
  1292. mutex_unlock(&mut);
  1293. ucma_free_ctx(ctx);
  1294. mutex_lock(&file->mut);
  1295. }
  1296. mutex_unlock(&file->mut);
  1297. kfree(file);
  1298. return 0;
  1299. }
  1300. static const struct file_operations ucma_fops = {
  1301. .owner = THIS_MODULE,
  1302. .open = ucma_open,
  1303. .release = ucma_close,
  1304. .write = ucma_write,
  1305. .poll = ucma_poll,
  1306. .llseek = no_llseek,
  1307. };
  1308. static struct miscdevice ucma_misc = {
  1309. .minor = MISC_DYNAMIC_MINOR,
  1310. .name = "rdma_cm",
  1311. .nodename = "infiniband/rdma_cm",
  1312. .mode = 0666,
  1313. .fops = &ucma_fops,
  1314. };
  1315. static ssize_t show_abi_version(struct device *dev,
  1316. struct device_attribute *attr,
  1317. char *buf)
  1318. {
  1319. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1320. }
  1321. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1322. static int __init ucma_init(void)
  1323. {
  1324. int ret;
  1325. ret = misc_register(&ucma_misc);
  1326. if (ret)
  1327. return ret;
  1328. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1329. if (ret) {
  1330. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1331. goto err1;
  1332. }
  1333. ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
  1334. if (!ucma_ctl_table_hdr) {
  1335. printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n");
  1336. ret = -ENOMEM;
  1337. goto err2;
  1338. }
  1339. return 0;
  1340. err2:
  1341. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1342. err1:
  1343. misc_deregister(&ucma_misc);
  1344. return ret;
  1345. }
  1346. static void __exit ucma_cleanup(void)
  1347. {
  1348. unregister_net_sysctl_table(ucma_ctl_table_hdr);
  1349. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1350. misc_deregister(&ucma_misc);
  1351. idr_destroy(&ctx_idr);
  1352. }
  1353. module_init(ucma_init);
  1354. module_exit(ucma_cleanup);