totemiba.c 40 KB

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