totemiba.c 41 KB

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