totemiba.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <pthread.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.h>
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <stdlib.h>
  59. #include <sys/types.h>
  60. #include <sys/socket.h>
  61. #include <netdb.h>
  62. #include <rdma/rdma_cma.h>
  63. #include <assert.h>
  64. #include <errno.h>
  65. #include <corosync/sq.h>
  66. #include <corosync/list.h>
  67. #include <corosync/hdb.h>
  68. #include <corosync/swab.h>
  69. #include <qb/qbloop.h>
  70. #define LOGSYS_UTILS_ONLY 1
  71. #include <corosync/engine/logsys.h>
  72. #include "totemiba.h"
  73. #include "wthread.h"
  74. #define COMPLETION_QUEUE_ENTRIES 100
  75. #define TOTAL_READ_POSTS 100
  76. #define MAX_MTU_SIZE 4096
  77. struct totemiba_instance {
  78. struct sockaddr bind_addr;
  79. struct sockaddr send_token_bind_addr;
  80. struct sockaddr mcast_addr;
  81. struct sockaddr token_addr;
  82. struct sockaddr local_mcast_bind_addr;
  83. struct totem_interface *totem_interface;
  84. struct totem_config *totem_config;
  85. void (*totemiba_iface_change_fn) (
  86. void *context,
  87. const struct totem_ip_address *iface_address);
  88. void (*totemiba_deliver_fn) (
  89. void *context,
  90. const void *msg,
  91. unsigned int msg_len);
  92. void (*totemiba_target_set_completed) (
  93. void *context);
  94. void *rrp_context;
  95. qb_loop_timer_handle timer_netif_check_timeout;
  96. qb_loop_t *totemiba_poll_handle;
  97. struct totem_ip_address my_id;
  98. struct rdma_event_channel *mcast_channel;
  99. struct rdma_cm_id *mcast_cma_id;
  100. struct ibv_pd *mcast_pd;
  101. struct sockaddr mcast_dest_addr;
  102. uint32_t mcast_qpn;
  103. uint32_t mcast_qkey;
  104. struct ibv_ah *mcast_ah;
  105. struct ibv_comp_channel *mcast_send_completion_channel;
  106. struct ibv_comp_channel *mcast_recv_completion_channel;
  107. struct ibv_cq *mcast_send_cq;
  108. struct ibv_cq *mcast_recv_cq;
  109. int recv_token_accepted;
  110. struct rdma_event_channel *recv_token_channel;
  111. struct rdma_event_channel *listen_recv_token_channel;
  112. struct rdma_cm_id *listen_recv_token_cma_id;
  113. struct rdma_cm_id *recv_token_cma_id;
  114. struct ibv_pd *recv_token_pd;
  115. struct sockaddr recv_token_dest_addr;
  116. struct ibv_comp_channel *recv_token_send_completion_channel;
  117. struct ibv_comp_channel *recv_token_recv_completion_channel;
  118. struct ibv_cq *recv_token_send_cq;
  119. struct ibv_cq *recv_token_recv_cq;
  120. int send_token_bound;
  121. struct rdma_event_channel *send_token_channel;
  122. struct rdma_cm_id *send_token_cma_id;
  123. struct ibv_pd *send_token_pd;
  124. struct sockaddr send_token_dest_addr;
  125. uint32_t send_token_qpn;
  126. uint32_t send_token_qkey;
  127. struct ibv_ah *send_token_ah;
  128. struct ibv_comp_channel *send_token_send_completion_channel;
  129. struct ibv_comp_channel *send_token_recv_completion_channel;
  130. struct ibv_cq *send_token_send_cq;
  131. struct ibv_cq *send_token_recv_cq;
  132. void (*totemiba_log_printf) (
  133. unsigned int rec_ident,
  134. const char *function,
  135. const char *file,
  136. int line,
  137. const char *format,
  138. ...)__attribute__((format(printf, 5, 6)));
  139. int totemiba_subsys_id;
  140. struct list_head mcast_send_buf_free;
  141. struct list_head token_send_buf_free;
  142. struct list_head mcast_send_buf_head;
  143. struct list_head token_send_buf_head;
  144. struct list_head recv_token_recv_buf_head;
  145. };
  146. union u {
  147. uint64_t wr_id;
  148. void *v;
  149. };
  150. #define log_printf(level, format, args...) \
  151. do { \
  152. instance->totemiba_log_printf ( \
  153. level, \
  154. instance->totemiba_subsys_id, \
  155. __FUNCTION__, __FILE__, __LINE__, \
  156. (const char *)format, ##args); \
  157. } while (0);
  158. struct recv_buf {
  159. struct list_head list_all;
  160. struct ibv_recv_wr recv_wr;
  161. struct ibv_sge sge;
  162. struct ibv_mr *mr;
  163. char buffer[MAX_MTU_SIZE];
  164. };
  165. struct send_buf {
  166. struct list_head list_free;
  167. struct list_head list_all;
  168. struct ibv_mr *mr;
  169. char buffer[MAX_MTU_SIZE];
  170. };
  171. static hdb_handle_t
  172. void2wrid (void *v) { union u u; u.v = v; return u.wr_id; }
  173. static void *
  174. wrid2void (uint64_t wr_id) { union u u; u.wr_id = wr_id; return u.v; }
  175. static void totemiba_instance_initialize (struct totemiba_instance *instance)
  176. {
  177. memset (instance, 0, sizeof (struct totemiba_instance));
  178. list_init (&instance->mcast_send_buf_free);
  179. list_init (&instance->token_send_buf_free);
  180. list_init (&instance->mcast_send_buf_head);
  181. list_init (&instance->token_send_buf_head);
  182. list_init (&instance->recv_token_recv_buf_head);
  183. }
  184. static inline struct send_buf *mcast_send_buf_get (
  185. struct totemiba_instance *instance)
  186. {
  187. struct send_buf *send_buf;
  188. if (list_empty (&instance->mcast_send_buf_free) == 0) {
  189. send_buf = list_entry (instance->mcast_send_buf_free.next, struct send_buf, list_free);
  190. list_del (&send_buf->list_free);
  191. return (send_buf);
  192. }
  193. send_buf = malloc (sizeof (struct send_buf));
  194. if (send_buf == NULL) {
  195. return (NULL);
  196. }
  197. send_buf->mr = ibv_reg_mr (instance->mcast_pd,
  198. send_buf->buffer,
  199. 2048, IBV_ACCESS_LOCAL_WRITE);
  200. if (send_buf->mr == NULL) {
  201. log_printf (LOGSYS_LEVEL_ERROR, "couldn't register memory range\n");
  202. free (send_buf);
  203. return (NULL);
  204. }
  205. list_init (&send_buf->list_all);
  206. list_add_tail (&send_buf->list_all, &instance->mcast_send_buf_head);
  207. return (send_buf);
  208. }
  209. static inline void mcast_send_buf_put (
  210. struct totemiba_instance *instance,
  211. struct send_buf *send_buf)
  212. {
  213. list_init (&send_buf->list_free);
  214. list_add_tail (&send_buf->list_free, &instance->mcast_send_buf_free);
  215. }
  216. static inline struct send_buf *token_send_buf_get (
  217. struct totemiba_instance *instance)
  218. {
  219. struct send_buf *send_buf;
  220. if (list_empty (&instance->token_send_buf_free) == 0) {
  221. send_buf = list_entry (instance->token_send_buf_free.next, struct send_buf, list_free);
  222. list_del (&send_buf->list_free);
  223. return (send_buf);
  224. }
  225. send_buf = malloc (sizeof (struct send_buf));
  226. if (send_buf == NULL) {
  227. return (NULL);
  228. }
  229. send_buf->mr = ibv_reg_mr (instance->send_token_pd,
  230. send_buf->buffer,
  231. 2048, IBV_ACCESS_LOCAL_WRITE);
  232. if (send_buf->mr == NULL) {
  233. log_printf (LOGSYS_LEVEL_ERROR, "couldn't register memory range\n");
  234. free (send_buf);
  235. return (NULL);
  236. }
  237. list_init (&send_buf->list_all);
  238. list_add_tail (&send_buf->list_all, &instance->token_send_buf_head);
  239. return (send_buf);
  240. }
  241. static inline void token_send_buf_destroy (struct totemiba_instance *instance)
  242. {
  243. struct list_head *list;
  244. struct send_buf *send_buf;
  245. for (list = instance->token_send_buf_head.next; list != &instance->token_send_buf_head;) {
  246. send_buf = list_entry (list, struct send_buf, list_all);
  247. list = list->next;
  248. ibv_dereg_mr (send_buf->mr);
  249. free (send_buf);
  250. }
  251. list_init (&instance->token_send_buf_free);
  252. list_init (&instance->token_send_buf_head);
  253. }
  254. static inline void token_send_buf_put (
  255. struct totemiba_instance *instance,
  256. struct send_buf *send_buf)
  257. {
  258. list_init (&send_buf->list_free);
  259. list_add_tail (&send_buf->list_free, &instance->token_send_buf_free);
  260. }
  261. static inline struct recv_buf *recv_token_recv_buf_create (
  262. struct totemiba_instance *instance)
  263. {
  264. struct recv_buf *recv_buf;
  265. recv_buf = malloc (sizeof (struct recv_buf));
  266. if (recv_buf == NULL) {
  267. return (NULL);
  268. }
  269. recv_buf->mr = ibv_reg_mr (instance->recv_token_pd, &recv_buf->buffer,
  270. 2048,
  271. IBV_ACCESS_LOCAL_WRITE);
  272. recv_buf->recv_wr.next = NULL;
  273. recv_buf->recv_wr.sg_list = &recv_buf->sge;
  274. recv_buf->recv_wr.num_sge = 1;
  275. recv_buf->recv_wr.wr_id = (uintptr_t)recv_buf;
  276. recv_buf->sge.length = 2048;
  277. recv_buf->sge.lkey = recv_buf->mr->lkey;
  278. recv_buf->sge.addr = (uintptr_t)recv_buf->buffer;
  279. list_init (&recv_buf->list_all);
  280. list_add (&recv_buf->list_all, &instance->recv_token_recv_buf_head);
  281. return (recv_buf);
  282. }
  283. static inline int recv_token_recv_buf_post (struct totemiba_instance *instance, struct recv_buf *recv_buf)
  284. {
  285. struct ibv_recv_wr *fail_recv;
  286. int res;
  287. res = ibv_post_recv (instance->recv_token_cma_id->qp, &recv_buf->recv_wr, &fail_recv);
  288. return (res);
  289. }
  290. static inline void recv_token_recv_buf_post_initial (struct totemiba_instance *instance)
  291. {
  292. struct recv_buf *recv_buf;
  293. unsigned int i;
  294. for (i = 0; i < TOTAL_READ_POSTS; i++) {
  295. recv_buf = recv_token_recv_buf_create (instance);
  296. recv_token_recv_buf_post (instance, recv_buf);
  297. }
  298. }
  299. static inline void recv_token_recv_buf_post_destroy (
  300. struct totemiba_instance *instance)
  301. {
  302. struct recv_buf *recv_buf;
  303. struct list_head *list;
  304. for (list = instance->recv_token_recv_buf_head.next;
  305. list != &instance->recv_token_recv_buf_head;) {
  306. recv_buf = list_entry (list, struct recv_buf, list_all);
  307. list = list->next;
  308. ibv_dereg_mr (recv_buf->mr);
  309. free (recv_buf);
  310. }
  311. list_init (&instance->recv_token_recv_buf_head);
  312. }
  313. static inline struct recv_buf *mcast_recv_buf_create (struct totemiba_instance *instance)
  314. {
  315. struct recv_buf *recv_buf;
  316. struct ibv_mr *mr;
  317. recv_buf = malloc (sizeof (struct recv_buf));
  318. if (recv_buf == NULL) {
  319. return (NULL);
  320. }
  321. mr = ibv_reg_mr (instance->mcast_pd, &recv_buf->buffer,
  322. 2048,
  323. IBV_ACCESS_LOCAL_WRITE);
  324. recv_buf->recv_wr.next = NULL;
  325. recv_buf->recv_wr.sg_list = &recv_buf->sge;
  326. recv_buf->recv_wr.num_sge = 1;
  327. recv_buf->recv_wr.wr_id = (uintptr_t)recv_buf;
  328. recv_buf->sge.length = 2048;
  329. recv_buf->sge.lkey = mr->lkey;
  330. recv_buf->sge.addr = (uintptr_t)recv_buf->buffer;
  331. return (recv_buf);
  332. }
  333. static inline int mcast_recv_buf_post (struct totemiba_instance *instance, struct recv_buf *recv_buf)
  334. {
  335. struct ibv_recv_wr *fail_recv;
  336. int res;
  337. res = ibv_post_recv (instance->mcast_cma_id->qp, &recv_buf->recv_wr, &fail_recv);
  338. return (res);
  339. }
  340. static inline void mcast_recv_buf_post_initial (struct totemiba_instance *instance)
  341. {
  342. struct recv_buf *recv_buf;
  343. unsigned int i;
  344. for (i = 0; i < TOTAL_READ_POSTS; i++) {
  345. recv_buf = mcast_recv_buf_create (instance);
  346. mcast_recv_buf_post (instance, recv_buf);
  347. }
  348. }
  349. static inline void iba_deliver_fn (struct totemiba_instance *instance, uint64_t wr_id, uint32_t bytes)
  350. {
  351. const char *addr;
  352. const struct recv_buf *recv_buf;
  353. recv_buf = wrid2void(wr_id);
  354. addr = &recv_buf->buffer[sizeof (struct ibv_grh)];
  355. instance->totemiba_deliver_fn (instance->rrp_context, addr, bytes);
  356. }
  357. static int mcast_cq_send_event_fn (int events, int suck, void *context)
  358. {
  359. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  360. struct ibv_wc wc[32];
  361. struct ibv_cq *ev_cq;
  362. void *ev_ctx;
  363. int res;
  364. int i;
  365. ibv_get_cq_event (instance->mcast_send_completion_channel, &ev_cq, &ev_ctx);
  366. ibv_ack_cq_events (ev_cq, 1);
  367. res = ibv_req_notify_cq (ev_cq, 0);
  368. res = ibv_poll_cq (instance->mcast_send_cq, 32, wc);
  369. if (res > 0) {
  370. for (i = 0; i < res; i++) {
  371. mcast_send_buf_put (instance, wrid2void(wc[i].wr_id));
  372. }
  373. }
  374. return (0);
  375. }
  376. static int mcast_cq_recv_event_fn (int events, int suck, void *context)
  377. {
  378. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  379. struct ibv_wc wc[64];
  380. struct ibv_cq *ev_cq;
  381. void *ev_ctx;
  382. int res;
  383. int i;
  384. ibv_get_cq_event (instance->mcast_recv_completion_channel, &ev_cq, &ev_ctx);
  385. ibv_ack_cq_events (ev_cq, 1);
  386. res = ibv_req_notify_cq (ev_cq, 0);
  387. res = ibv_poll_cq (instance->mcast_recv_cq, 64, wc);
  388. if (res > 0) {
  389. for (i = 0; i < res; i++) {
  390. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  391. mcast_recv_buf_post (instance, wrid2void(wc[i].wr_id));
  392. }
  393. }
  394. return (0);
  395. }
  396. static int mcast_rdma_event_fn (int events, int suck, void *context)
  397. {
  398. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  399. struct rdma_cm_event *event;
  400. int res;
  401. res = rdma_get_cm_event (instance->mcast_channel, &event);
  402. if (res != 0) {
  403. return (0);
  404. }
  405. switch (event->event) {
  406. /*
  407. * occurs when we resolve the multicast address
  408. */
  409. case RDMA_CM_EVENT_ADDR_RESOLVED:
  410. rdma_join_multicast (instance->mcast_cma_id, &instance->mcast_addr, instance);
  411. break;
  412. /*
  413. * occurs when the CM joins the multicast group
  414. */
  415. case RDMA_CM_EVENT_MULTICAST_JOIN:
  416. instance->mcast_qpn = event->param.ud.qp_num;
  417. instance->mcast_qkey = event->param.ud.qkey;
  418. instance->mcast_ah = ibv_create_ah (instance->mcast_pd, &event->param.ud.ah_attr);
  419. instance->totemiba_iface_change_fn (instance->rrp_context, &instance->my_id);
  420. break;
  421. case RDMA_CM_EVENT_ADDR_ERROR:
  422. case RDMA_CM_EVENT_ROUTE_ERROR:
  423. case RDMA_CM_EVENT_MULTICAST_ERROR:
  424. log_printf (LOGSYS_LEVEL_ERROR, "multicast error\n");
  425. break;
  426. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  427. break;
  428. default:
  429. log_printf (LOGSYS_LEVEL_ERROR, "default %d\n", event->event);
  430. break;
  431. }
  432. rdma_ack_cm_event (event);
  433. return (0);
  434. }
  435. static int recv_token_cq_send_event_fn (hdb_handle_t poll_handle, int events, int suck, void *context)
  436. {
  437. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  438. struct ibv_wc wc[32];
  439. struct ibv_cq *ev_cq;
  440. void *ev_ctx;
  441. int res;
  442. int i;
  443. ibv_get_cq_event (instance->recv_token_send_completion_channel, &ev_cq, &ev_ctx);
  444. ibv_ack_cq_events (ev_cq, 1);
  445. res = ibv_req_notify_cq (ev_cq, 0);
  446. res = ibv_poll_cq (instance->recv_token_send_cq, 32, wc);
  447. if (res > 0) {
  448. for (i = 0; i < res; i++) {
  449. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  450. ibv_dereg_mr (wrid2void(wc[i].wr_id));
  451. }
  452. }
  453. return (0);
  454. }
  455. static int recv_token_cq_recv_event_fn (int events, int suck, void *context)
  456. {
  457. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  458. struct ibv_wc wc[32];
  459. struct ibv_cq *ev_cq;
  460. void *ev_ctx;
  461. int res;
  462. int i;
  463. ibv_get_cq_event (instance->recv_token_recv_completion_channel, &ev_cq, &ev_ctx);
  464. ibv_ack_cq_events (ev_cq, 1);
  465. res = ibv_req_notify_cq (ev_cq, 0);
  466. res = ibv_poll_cq (instance->recv_token_recv_cq, 32, wc);
  467. if (res > 0) {
  468. for (i = 0; i < res; i++) {
  469. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  470. recv_token_recv_buf_post (instance, wrid2void(wc[i].wr_id));
  471. }
  472. }
  473. return (0);
  474. }
  475. static int recv_token_accept_destroy (struct totemiba_instance *instance)
  476. {
  477. if (instance->recv_token_accepted == 0) {
  478. return (0);
  479. }
  480. rdma_destroy_qp (instance->recv_token_cma_id);
  481. recv_token_recv_buf_post_destroy (instance);
  482. ibv_destroy_cq (instance->recv_token_send_cq);
  483. ibv_destroy_cq (instance->recv_token_recv_cq);
  484. ibv_destroy_comp_channel (instance->recv_token_send_completion_channel);
  485. ibv_destroy_comp_channel (instance->recv_token_recv_completion_channel);
  486. ibv_dealloc_pd (instance->recv_token_pd);
  487. rdma_destroy_id (instance->recv_token_cma_id);
  488. qb_loop_poll_del (
  489. instance->totemiba_poll_handle,
  490. instance->recv_token_recv_completion_channel->fd);
  491. qb_loop_poll_del (
  492. instance->totemiba_poll_handle,
  493. instance->recv_token_send_completion_channel->fd);
  494. return (0);
  495. }
  496. static int recv_token_accept_setup (struct totemiba_instance *instance)
  497. {
  498. struct ibv_qp_init_attr init_qp_attr;
  499. int res = 0;
  500. /*
  501. * Allocate the protection domain
  502. */
  503. instance->recv_token_pd = ibv_alloc_pd (instance->recv_token_cma_id->verbs);
  504. /*
  505. * Create a completion channel
  506. */
  507. instance->recv_token_recv_completion_channel = ibv_create_comp_channel (instance->recv_token_cma_id->verbs);
  508. if (instance->recv_token_recv_completion_channel == NULL) {
  509. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel\n");
  510. return (-1);
  511. }
  512. /*
  513. * Create the completion queue
  514. */
  515. instance->recv_token_recv_cq = ibv_create_cq (instance->recv_token_cma_id->verbs,
  516. COMPLETION_QUEUE_ENTRIES, instance,
  517. instance->recv_token_recv_completion_channel, 0);
  518. if (instance->recv_token_recv_cq == NULL) {
  519. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue\n");
  520. return (-1);
  521. }
  522. res = ibv_req_notify_cq (instance->recv_token_recv_cq, 0);
  523. if (res != 0) {
  524. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue\n");
  525. return (-1);
  526. }
  527. /*
  528. * Create a completion channel
  529. */
  530. instance->recv_token_send_completion_channel = ibv_create_comp_channel (instance->recv_token_cma_id->verbs);
  531. if (instance->recv_token_send_completion_channel == NULL) {
  532. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel\n");
  533. return (-1);
  534. }
  535. /*
  536. * Create the completion queue
  537. */
  538. instance->recv_token_send_cq = ibv_create_cq (instance->recv_token_cma_id->verbs,
  539. COMPLETION_QUEUE_ENTRIES, instance,
  540. instance->recv_token_send_completion_channel, 0);
  541. if (instance->recv_token_send_cq == NULL) {
  542. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue\n");
  543. return (-1);
  544. }
  545. res = ibv_req_notify_cq (instance->recv_token_send_cq, 0);
  546. if (res != 0) {
  547. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue\n");
  548. return (-1);
  549. }
  550. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  551. init_qp_attr.cap.max_send_wr = 50;
  552. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  553. init_qp_attr.cap.max_send_sge = 1;
  554. init_qp_attr.cap.max_recv_sge = 1;
  555. init_qp_attr.qp_context = instance;
  556. init_qp_attr.sq_sig_all = 0;
  557. init_qp_attr.qp_type = IBV_QPT_UD;
  558. init_qp_attr.send_cq = instance->recv_token_send_cq;
  559. init_qp_attr.recv_cq = instance->recv_token_recv_cq;
  560. res = rdma_create_qp (instance->recv_token_cma_id, instance->recv_token_pd,
  561. &init_qp_attr);
  562. if (res != 0) {
  563. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair\n");
  564. return (-1);
  565. }
  566. recv_token_recv_buf_post_initial (instance);
  567. qb_loop_poll_add (
  568. instance->totemiba_poll_handle,
  569. QB_LOOP_MED,
  570. instance->recv_token_recv_completion_channel->fd,
  571. POLLIN, instance, recv_token_cq_recv_event_fn);
  572. qb_loop_poll_add (
  573. instance->totemiba_poll_handle,
  574. QB_LOOP_MED,
  575. instance->recv_token_send_completion_channel->fd,
  576. POLLIN, instance, recv_token_cq_send_event_fn);
  577. instance->recv_token_accepted = 1;
  578. return (res);
  579. };
  580. static int recv_token_rdma_event_fn (int events, int suck, void *context)
  581. {
  582. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  583. struct rdma_cm_event *event;
  584. struct rdma_conn_param conn_param;
  585. int res;
  586. res = rdma_get_cm_event (instance->listen_recv_token_channel, &event);
  587. if (res != 0) {
  588. return (0);
  589. }
  590. switch (event->event) {
  591. case RDMA_CM_EVENT_CONNECT_REQUEST:
  592. recv_token_accept_destroy (instance);
  593. instance->recv_token_cma_id = event->id;
  594. recv_token_accept_setup (instance);
  595. memset (&conn_param, 0, sizeof (struct rdma_conn_param));
  596. conn_param.qp_num = instance->recv_token_cma_id->qp->qp_num;
  597. res = rdma_accept (instance->recv_token_cma_id, &conn_param);
  598. break;
  599. default:
  600. log_printf (LOGSYS_LEVEL_ERROR, "default %d\n", event->event);
  601. break;
  602. }
  603. res = rdma_ack_cm_event (event);
  604. return (0);
  605. }
  606. static int send_token_cq_send_event_fn (int events, int suck, void *context)
  607. {
  608. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  609. struct ibv_wc wc[32];
  610. struct ibv_cq *ev_cq;
  611. void *ev_ctx;
  612. int res;
  613. int i;
  614. ibv_get_cq_event (instance->send_token_send_completion_channel, &ev_cq, &ev_ctx);
  615. ibv_ack_cq_events (ev_cq, 1);
  616. res = ibv_req_notify_cq (ev_cq, 0);
  617. res = ibv_poll_cq (instance->send_token_send_cq, 32, wc);
  618. if (res > 0) {
  619. for (i = 0; i < res; i++) {
  620. token_send_buf_put (instance, wrid2void(wc[i].wr_id));
  621. }
  622. }
  623. return (0);
  624. }
  625. static int send_token_cq_recv_event_fn (int events, int suck, void *context)
  626. {
  627. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  628. struct ibv_wc wc[32];
  629. struct ibv_cq *ev_cq;
  630. void *ev_ctx;
  631. int res;
  632. int i;
  633. ibv_get_cq_event (instance->send_token_recv_completion_channel, &ev_cq, &ev_ctx);
  634. ibv_ack_cq_events (ev_cq, 1);
  635. res = ibv_req_notify_cq (ev_cq, 0);
  636. res = ibv_poll_cq (instance->send_token_recv_cq, 32, wc);
  637. if (res > 0) {
  638. for (i = 0; i < res; i++) {
  639. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  640. }
  641. }
  642. return (0);
  643. }
  644. static int send_token_rdma_event_fn (int events, int suck, void *context)
  645. {
  646. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  647. struct rdma_cm_event *event;
  648. struct rdma_conn_param conn_param;
  649. int res;
  650. res = rdma_get_cm_event (instance->send_token_channel, &event);
  651. if (res != 0) {
  652. return (0);
  653. }
  654. switch (event->event) {
  655. /*
  656. * occurs when we resolve the multicast address
  657. */
  658. case RDMA_CM_EVENT_ADDR_RESOLVED:
  659. res = rdma_resolve_route (instance->send_token_cma_id, 2000);
  660. break;
  661. /*
  662. * occurs when the CM joins the multicast group
  663. */
  664. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  665. memset (&conn_param, 0, sizeof (struct rdma_conn_param));
  666. conn_param.private_data = NULL;
  667. conn_param.private_data_len = 0;
  668. res = rdma_connect (instance->send_token_cma_id, &conn_param);
  669. break;
  670. case RDMA_CM_EVENT_ESTABLISHED:
  671. instance->send_token_qpn = event->param.ud.qp_num;
  672. instance->send_token_qkey = event->param.ud.qkey;
  673. instance->send_token_ah = ibv_create_ah (instance->send_token_pd, &event->param.ud.ah_attr);
  674. instance->totemiba_target_set_completed (instance->rrp_context);
  675. break;
  676. case RDMA_CM_EVENT_ADDR_ERROR:
  677. case RDMA_CM_EVENT_ROUTE_ERROR:
  678. case RDMA_CM_EVENT_MULTICAST_ERROR:
  679. log_printf (LOGSYS_LEVEL_ERROR,
  680. "send_token_rdma_event_fn multicast error\n");
  681. break;
  682. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  683. break;
  684. case RDMA_CM_EVENT_UNREACHABLE:
  685. log_printf (LOGSYS_LEVEL_ERROR,
  686. "send_token_rdma_event_fn unreachable\n");
  687. break;
  688. default:
  689. log_printf (LOGSYS_LEVEL_ERROR,
  690. "send_token_rdma_event_fn unknown event %d\n",
  691. event->event);
  692. break;
  693. }
  694. rdma_ack_cm_event (event);
  695. return (0);
  696. }
  697. static int send_token_bind (struct totemiba_instance *instance)
  698. {
  699. int res;
  700. struct ibv_qp_init_attr init_qp_attr;
  701. instance->send_token_channel = rdma_create_event_channel();
  702. if (instance->send_token_channel == NULL) {
  703. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel\n");
  704. return (-1);
  705. }
  706. res = rdma_create_id (instance->send_token_channel,
  707. &instance->send_token_cma_id, NULL, RDMA_PS_UDP);
  708. if (res) {
  709. log_printf (LOGSYS_LEVEL_ERROR, "error creating send_token_cma_id\n");
  710. return (-1);
  711. }
  712. res = rdma_bind_addr (instance->send_token_cma_id,
  713. &instance->send_token_bind_addr);
  714. if (res) {
  715. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for send token\n");
  716. return (-1);
  717. }
  718. /*
  719. * Resolve the send_token address into a GUID
  720. */
  721. res = rdma_resolve_addr (instance->send_token_cma_id,
  722. &instance->bind_addr, &instance->token_addr, 2000);
  723. if (res) {
  724. log_printf (LOGSYS_LEVEL_ERROR, "error resolving send token address %d %d\n", res, errno);
  725. return (-1);
  726. }
  727. /*
  728. * Allocate the protection domain
  729. */
  730. instance->send_token_pd = ibv_alloc_pd (instance->send_token_cma_id->verbs);
  731. /*
  732. * Create a completion channel
  733. */
  734. instance->send_token_recv_completion_channel = ibv_create_comp_channel (instance->send_token_cma_id->verbs);
  735. if (instance->send_token_recv_completion_channel == NULL) {
  736. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel\n");
  737. return (-1);
  738. }
  739. /*
  740. * Create the completion queue
  741. */
  742. instance->send_token_recv_cq = ibv_create_cq (instance->send_token_cma_id->verbs,
  743. COMPLETION_QUEUE_ENTRIES, instance,
  744. instance->send_token_recv_completion_channel, 0);
  745. if (instance->send_token_recv_cq == NULL) {
  746. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue\n");
  747. return (-1);
  748. }
  749. res = ibv_req_notify_cq (instance->send_token_recv_cq, 0);
  750. if (res != 0) {
  751. log_printf (LOGSYS_LEVEL_ERROR,
  752. "couldn't request notifications of the completion queue\n");
  753. return (-1);
  754. }
  755. /*
  756. * Create a completion channel
  757. */
  758. instance->send_token_send_completion_channel =
  759. ibv_create_comp_channel (instance->send_token_cma_id->verbs);
  760. if (instance->send_token_send_completion_channel == NULL) {
  761. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel\n");
  762. return (-1);
  763. }
  764. /*
  765. * Create the completion queue
  766. */
  767. instance->send_token_send_cq = ibv_create_cq (
  768. instance->send_token_cma_id->verbs,
  769. COMPLETION_QUEUE_ENTRIES, instance,
  770. instance->send_token_send_completion_channel, 0);
  771. if (instance->send_token_send_cq == NULL) {
  772. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue\n");
  773. return (-1);
  774. }
  775. res = ibv_req_notify_cq (instance->send_token_send_cq, 0);
  776. if (res != 0) {
  777. log_printf (LOGSYS_LEVEL_ERROR,
  778. "couldn't request notifications of the completion queue\n");
  779. return (-1);
  780. }
  781. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  782. init_qp_attr.cap.max_send_wr = 50;
  783. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  784. init_qp_attr.cap.max_send_sge = 1;
  785. init_qp_attr.cap.max_recv_sge = 1;
  786. init_qp_attr.qp_context = instance;
  787. init_qp_attr.sq_sig_all = 0;
  788. init_qp_attr.qp_type = IBV_QPT_UD;
  789. init_qp_attr.send_cq = instance->send_token_send_cq;
  790. init_qp_attr.recv_cq = instance->send_token_recv_cq;
  791. res = rdma_create_qp (instance->send_token_cma_id,
  792. instance->send_token_pd, &init_qp_attr);
  793. if (res != 0) {
  794. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair\n");
  795. return (-1);
  796. }
  797. qb_loop_poll_add (
  798. instance->totemiba_poll_handle,
  799. QB_LOOP_MED,
  800. instance->send_token_recv_completion_channel->fd,
  801. POLLIN, instance, send_token_cq_recv_event_fn);
  802. qb_loop_poll_add (
  803. instance->totemiba_poll_handle,
  804. QB_LOOP_MED,
  805. instance->send_token_send_completion_channel->fd,
  806. POLLIN, instance, send_token_cq_send_event_fn);
  807. qb_loop_poll_add (
  808. instance->totemiba_poll_handle,
  809. QB_LOOP_MED,
  810. instance->send_token_channel->fd,
  811. POLLIN, instance, send_token_rdma_event_fn);
  812. instance->send_token_bound = 1;
  813. return (0);
  814. }
  815. static int send_token_unbind (struct totemiba_instance *instance)
  816. {
  817. if (instance->send_token_bound == 0) {
  818. return (0);
  819. }
  820. qb_loop_poll_del (
  821. instance->totemiba_poll_handle,
  822. instance->send_token_recv_completion_channel->fd);
  823. qb_loop_poll_del (
  824. instance->totemiba_poll_handle,
  825. instance->send_token_send_completion_channel->fd);
  826. qb_loop_poll_del (
  827. instance->totemiba_poll_handle,
  828. instance->send_token_channel->fd);
  829. rdma_destroy_qp (instance->send_token_cma_id);
  830. ibv_destroy_cq (instance->send_token_send_cq);
  831. ibv_destroy_cq (instance->send_token_recv_cq);
  832. ibv_destroy_comp_channel (instance->send_token_send_completion_channel);
  833. ibv_destroy_comp_channel (instance->send_token_recv_completion_channel);
  834. token_send_buf_destroy (instance);
  835. ibv_dealloc_pd (instance->send_token_pd);
  836. rdma_destroy_id (instance->send_token_cma_id);
  837. rdma_destroy_event_channel (instance->send_token_channel);
  838. return (0);
  839. }
  840. static int recv_token_bind (struct totemiba_instance *instance)
  841. {
  842. int res;
  843. instance->listen_recv_token_channel = rdma_create_event_channel();
  844. if (instance->listen_recv_token_channel == NULL) {
  845. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel\n");
  846. return (-1);
  847. }
  848. res = rdma_create_id (instance->listen_recv_token_channel,
  849. &instance->listen_recv_token_cma_id, NULL, RDMA_PS_UDP);
  850. if (res) {
  851. log_printf (LOGSYS_LEVEL_ERROR, "error creating recv_token_cma_id\n");
  852. return (-1);
  853. }
  854. res = rdma_bind_addr (instance->listen_recv_token_cma_id,
  855. &instance->bind_addr);
  856. if (res) {
  857. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for recv token\n");
  858. return (-1);
  859. }
  860. /*
  861. * Resolve the recv_token address into a GUID
  862. */
  863. res = rdma_listen (instance->listen_recv_token_cma_id, 10);
  864. if (res) {
  865. log_printf (LOGSYS_LEVEL_ERROR, "error listening %d %d\n", res, errno);
  866. return (-1);
  867. }
  868. qb_loop_poll_add (
  869. instance->totemiba_poll_handle,
  870. QB_LOOP_MED,
  871. instance->listen_recv_token_channel->fd,
  872. POLLIN, instance, recv_token_rdma_event_fn);
  873. return (0);
  874. }
  875. static int mcast_bind (struct totemiba_instance *instance)
  876. {
  877. int res;
  878. struct ibv_qp_init_attr init_qp_attr;
  879. instance->mcast_channel = rdma_create_event_channel();
  880. if (instance->mcast_channel == NULL) {
  881. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel\n");
  882. return (-1);
  883. }
  884. res = rdma_create_id (instance->mcast_channel, &instance->mcast_cma_id, NULL, RDMA_PS_UDP);
  885. if (res) {
  886. log_printf (LOGSYS_LEVEL_ERROR, "error creating mcast_cma_id\n");
  887. return (-1);
  888. }
  889. res = rdma_bind_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr);
  890. if (res) {
  891. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for mcast\n");
  892. return (-1);
  893. }
  894. /*
  895. * Resolve the multicast address into a GUID
  896. */
  897. res = rdma_resolve_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr,
  898. &instance->mcast_addr, 5000);
  899. if (res) {
  900. log_printf (LOGSYS_LEVEL_ERROR, "error resolving multicast address %d %d\n", res, errno);
  901. return (-1);
  902. }
  903. /*
  904. * Allocate the protection domain
  905. */
  906. instance->mcast_pd = ibv_alloc_pd (instance->mcast_cma_id->verbs);
  907. /*
  908. * Create a completion channel
  909. */
  910. instance->mcast_recv_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  911. if (instance->mcast_recv_completion_channel == NULL) {
  912. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel\n");
  913. return (-1);
  914. }
  915. /*
  916. * Create the completion queue
  917. */
  918. instance->mcast_recv_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  919. COMPLETION_QUEUE_ENTRIES, instance,
  920. instance->mcast_recv_completion_channel, 0);
  921. if (instance->mcast_recv_cq == NULL) {
  922. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue\n");
  923. return (-1);
  924. }
  925. res = ibv_req_notify_cq (instance->mcast_recv_cq, 0);
  926. if (res != 0) {
  927. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue\n");
  928. return (-1);
  929. }
  930. /*
  931. * Create a completion channel
  932. */
  933. instance->mcast_send_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  934. if (instance->mcast_send_completion_channel == NULL) {
  935. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel\n");
  936. return (-1);
  937. }
  938. /*
  939. * Create the completion queue
  940. */
  941. instance->mcast_send_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  942. COMPLETION_QUEUE_ENTRIES, instance,
  943. instance->mcast_send_completion_channel, 0);
  944. if (instance->mcast_send_cq == NULL) {
  945. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue\n");
  946. return (-1);
  947. }
  948. res = ibv_req_notify_cq (instance->mcast_send_cq, 0);
  949. if (res != 0) {
  950. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue\n");
  951. return (-1);
  952. }
  953. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  954. init_qp_attr.cap.max_send_wr = 50;
  955. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  956. init_qp_attr.cap.max_send_sge = 1;
  957. init_qp_attr.cap.max_recv_sge = 1;
  958. init_qp_attr.qp_context = instance;
  959. init_qp_attr.sq_sig_all = 0;
  960. init_qp_attr.qp_type = IBV_QPT_UD;
  961. init_qp_attr.send_cq = instance->mcast_send_cq;
  962. init_qp_attr.recv_cq = instance->mcast_recv_cq;
  963. res = rdma_create_qp (instance->mcast_cma_id, instance->mcast_pd,
  964. &init_qp_attr);
  965. if (res != 0) {
  966. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair\n");
  967. return (-1);
  968. }
  969. mcast_recv_buf_post_initial (instance);
  970. qb_loop_poll_add (
  971. instance->totemiba_poll_handle,
  972. QB_LOOP_MED,
  973. instance->mcast_recv_completion_channel->fd,
  974. POLLIN, instance, mcast_cq_recv_event_fn);
  975. qb_loop_poll_add (
  976. instance->totemiba_poll_handle,
  977. QB_LOOP_MED,
  978. instance->mcast_send_completion_channel->fd,
  979. POLLIN, instance, mcast_cq_send_event_fn);
  980. qb_loop_poll_add (
  981. instance->totemiba_poll_handle,
  982. QB_LOOP_MED,
  983. instance->mcast_channel->fd,
  984. POLLIN, instance, mcast_rdma_event_fn);
  985. return (0);
  986. }
  987. static void timer_function_netif_check_timeout (
  988. void *data)
  989. {
  990. struct totemiba_instance *instance = (struct totemiba_instance *)data;
  991. int res;
  992. int interface_up;
  993. int interface_num;
  994. int addr_len;
  995. totemip_iface_check (&instance->totem_interface->bindnet,
  996. &instance->totem_interface->boundto, &interface_up, &interface_num, instance->totem_config->clear_node_high_bit);
  997. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  998. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->bind_addr,
  999. &addr_len);
  1000. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1001. 0, (struct sockaddr_storage *)&instance->send_token_bind_addr,
  1002. &addr_len);
  1003. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1004. 0, (struct sockaddr_storage *)&instance->local_mcast_bind_addr,
  1005. &addr_len);
  1006. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1007. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->my_id,
  1008. &addr_len);
  1009. totemip_sockaddr_to_totemip_convert(
  1010. (const struct sockaddr_storage *)&instance->bind_addr,
  1011. &instance->my_id);
  1012. memcpy (&instance->my_id, &instance->totem_interface->boundto,
  1013. sizeof (struct totem_ip_address));
  1014. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->mcast_addr,
  1015. instance->totem_interface->ip_port,
  1016. (struct sockaddr_storage *)&instance->mcast_addr, &addr_len);
  1017. res = recv_token_bind (instance);
  1018. res = mcast_bind (instance);
  1019. }
  1020. int totemiba_crypto_set (
  1021. void *iba_context,
  1022. unsigned int type)
  1023. {
  1024. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1025. int res = 0;
  1026. instance = NULL;
  1027. return (res);
  1028. }
  1029. int totemiba_finalize (
  1030. void *iba_context)
  1031. {
  1032. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1033. int res = 0;
  1034. instance = NULL;
  1035. return (res);
  1036. }
  1037. /*
  1038. * Create an instance
  1039. */
  1040. int totemiba_initialize (
  1041. qb_loop_t *qb_poll_handle,
  1042. void **iba_context,
  1043. struct totem_config *totem_config,
  1044. int interface_no,
  1045. void *context,
  1046. void (*deliver_fn) (
  1047. void *context,
  1048. const void *msg,
  1049. unsigned int msg_len),
  1050. void (*iface_change_fn) (
  1051. void *context,
  1052. const struct totem_ip_address *iface_address),
  1053. void (*target_set_completed) (
  1054. void *context))
  1055. {
  1056. struct totemiba_instance *instance;
  1057. int res = 0;
  1058. instance = malloc (sizeof (struct totemiba_instance));
  1059. if (instance == NULL) {
  1060. return (-1);
  1061. }
  1062. totemiba_instance_initialize (instance);
  1063. instance->totem_interface = &totem_config->interfaces[interface_no];
  1064. instance->totemiba_poll_handle = qb_poll_handle;
  1065. instance->totemiba_deliver_fn = deliver_fn;
  1066. instance->totemiba_target_set_completed = target_set_completed;
  1067. instance->totemiba_iface_change_fn = iface_change_fn;
  1068. instance->totem_config = totem_config;
  1069. instance->rrp_context = context;
  1070. qb_loop_timer_add (instance->totemiba_poll_handle,
  1071. QB_LOOP_MED,
  1072. 100*QB_TIME_NS_IN_NSEC,
  1073. (void *)instance,
  1074. timer_function_netif_check_timeout,
  1075. &instance->timer_netif_check_timeout);
  1076. instance->totemiba_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1077. instance->totemiba_log_printf = totem_config->totem_logging_configuration.log_printf;
  1078. *iba_context = instance;
  1079. return (res);
  1080. }
  1081. void *totemiba_buffer_alloc (void)
  1082. {
  1083. return malloc (MAX_MTU_SIZE);
  1084. }
  1085. void totemiba_buffer_release (void *ptr)
  1086. {
  1087. return free (ptr);
  1088. }
  1089. int totemiba_processor_count_set (
  1090. void *iba_context,
  1091. int processor_count)
  1092. {
  1093. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1094. int res = 0;
  1095. instance = NULL;
  1096. return (res);
  1097. }
  1098. int totemiba_recv_flush (void *iba_context)
  1099. {
  1100. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1101. int res = 0;
  1102. instance = NULL;
  1103. return (res);
  1104. }
  1105. int totemiba_send_flush (void *iba_context)
  1106. {
  1107. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1108. int res = 0;
  1109. instance = NULL;
  1110. return (res);
  1111. }
  1112. int totemiba_token_send (
  1113. void *iba_context,
  1114. const void *ms,
  1115. unsigned int msg_len)
  1116. {
  1117. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1118. int res = 0;
  1119. struct ibv_send_wr send_wr, *failed_send_wr;
  1120. struct ibv_sge sge;
  1121. void *msg;
  1122. struct send_buf *send_buf;
  1123. send_buf = token_send_buf_get (instance);
  1124. if (send_buf == NULL) {
  1125. return (-1);
  1126. }
  1127. msg = send_buf->buffer;
  1128. memcpy (msg, ms, msg_len);
  1129. send_wr.next = NULL;
  1130. send_wr.sg_list = &sge;
  1131. send_wr.num_sge = 1;
  1132. send_wr.opcode = IBV_WR_SEND;
  1133. send_wr.send_flags = IBV_SEND_SIGNALED;
  1134. send_wr.wr_id = void2wrid(send_buf);
  1135. send_wr.imm_data = 0;
  1136. send_wr.wr.ud.ah = instance->send_token_ah;
  1137. send_wr.wr.ud.remote_qpn = instance->send_token_qpn;
  1138. send_wr.wr.ud.remote_qkey = instance->send_token_qkey;
  1139. sge.length = msg_len;
  1140. sge.lkey = send_buf->mr->lkey;
  1141. sge.addr = (uintptr_t)msg;
  1142. res = ibv_post_send (instance->send_token_cma_id->qp, &send_wr, &failed_send_wr);
  1143. return (res);
  1144. }
  1145. int totemiba_mcast_flush_send (
  1146. void *iba_context,
  1147. const void *ms,
  1148. unsigned int msg_len)
  1149. {
  1150. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1151. int res = 0;
  1152. struct ibv_send_wr send_wr, *failed_send_wr;
  1153. struct ibv_sge sge;
  1154. void *msg;
  1155. struct send_buf *send_buf;
  1156. send_buf = mcast_send_buf_get (instance);
  1157. if (send_buf == NULL) {
  1158. return (-1);
  1159. }
  1160. msg = send_buf->buffer;
  1161. memcpy (msg, ms, msg_len);
  1162. send_wr.next = NULL;
  1163. send_wr.sg_list = &sge;
  1164. send_wr.num_sge = 1;
  1165. send_wr.opcode = IBV_WR_SEND;
  1166. send_wr.send_flags = IBV_SEND_SIGNALED;
  1167. send_wr.wr_id = void2wrid(send_buf);
  1168. send_wr.imm_data = 0;
  1169. send_wr.wr.ud.ah = instance->mcast_ah;
  1170. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1171. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1172. sge.length = msg_len;
  1173. sge.lkey = send_buf->mr->lkey;
  1174. sge.addr = (uintptr_t)msg;
  1175. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1176. return (res);
  1177. }
  1178. int totemiba_mcast_noflush_send (
  1179. void *iba_context,
  1180. const void *ms,
  1181. unsigned int msg_len)
  1182. {
  1183. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1184. int res = 0;
  1185. struct ibv_send_wr send_wr, *failed_send_wr;
  1186. struct ibv_sge sge;
  1187. void *msg;
  1188. struct send_buf *send_buf;
  1189. send_buf = mcast_send_buf_get (instance);
  1190. if (send_buf == NULL) {
  1191. return (-1);
  1192. }
  1193. msg = send_buf->buffer;
  1194. memcpy (msg, ms, msg_len);
  1195. send_wr.next = NULL;
  1196. send_wr.sg_list = &sge;
  1197. send_wr.num_sge = 1;
  1198. send_wr.opcode = IBV_WR_SEND;
  1199. send_wr.send_flags = IBV_SEND_SIGNALED;
  1200. send_wr.wr_id = void2wrid(send_buf);
  1201. send_wr.imm_data = 0;
  1202. send_wr.wr.ud.ah = instance->mcast_ah;
  1203. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1204. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1205. sge.length = msg_len;
  1206. sge.lkey = send_buf->mr->lkey;
  1207. sge.addr = (uintptr_t)msg;
  1208. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1209. return (res);
  1210. }
  1211. extern int totemiba_iface_check (void *iba_context)
  1212. {
  1213. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1214. int res = 0;
  1215. instance = NULL;
  1216. return (res);
  1217. }
  1218. extern void totemiba_net_mtu_adjust (void *iba_context, struct totem_config *totem_config)
  1219. {
  1220. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1221. instance = NULL;
  1222. }
  1223. const char *totemiba_iface_print (void *iba_context) {
  1224. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1225. const char *ret_char;
  1226. ret_char = totemip_print (&instance->my_id);
  1227. return (ret_char);
  1228. }
  1229. int totemiba_iface_get (
  1230. void *iba_context,
  1231. struct totem_ip_address *addr)
  1232. {
  1233. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1234. int res = 0;
  1235. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1236. return (res);
  1237. }
  1238. int totemiba_token_target_set (
  1239. void *iba_context,
  1240. const struct totem_ip_address *token_target)
  1241. {
  1242. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1243. int res = 0;
  1244. int addr_len = 16;
  1245. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)token_target,
  1246. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->token_addr,
  1247. &addr_len);
  1248. res = send_token_unbind (instance);
  1249. res = send_token_bind (instance);
  1250. return (res);
  1251. }
  1252. extern int totemiba_recv_mcast_empty (
  1253. void *iba_context)
  1254. {
  1255. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1256. int res = 0;
  1257. instance = NULL;
  1258. return (res);
  1259. }