totemiba.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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. break;
  414. /*
  415. * occurs when the CM joins the multicast group
  416. */
  417. case RDMA_CM_EVENT_MULTICAST_JOIN:
  418. instance->mcast_qpn = event->param.ud.qp_num;
  419. instance->mcast_qkey = event->param.ud.qkey;
  420. instance->mcast_ah = ibv_create_ah (instance->mcast_pd, &event->param.ud.ah_attr);
  421. instance->totemiba_iface_change_fn (instance->rrp_context, &instance->my_id);
  422. break;
  423. case RDMA_CM_EVENT_ADDR_ERROR:
  424. case RDMA_CM_EVENT_ROUTE_ERROR:
  425. case RDMA_CM_EVENT_MULTICAST_ERROR:
  426. log_printf (LOGSYS_LEVEL_ERROR, "multicast error");
  427. break;
  428. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  429. break;
  430. default:
  431. log_printf (LOGSYS_LEVEL_ERROR, "default %d", event->event);
  432. break;
  433. }
  434. rdma_ack_cm_event (event);
  435. return (0);
  436. }
  437. static int recv_token_cq_send_event_fn (
  438. int fd,
  439. int revents,
  440. void *context)
  441. {
  442. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  443. struct ibv_wc wc[32];
  444. struct ibv_cq *ev_cq;
  445. void *ev_ctx;
  446. int res;
  447. int i;
  448. ibv_get_cq_event (instance->recv_token_send_completion_channel, &ev_cq, &ev_ctx);
  449. ibv_ack_cq_events (ev_cq, 1);
  450. res = ibv_req_notify_cq (ev_cq, 0);
  451. res = ibv_poll_cq (instance->recv_token_send_cq, 32, wc);
  452. if (res > 0) {
  453. for (i = 0; i < res; i++) {
  454. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  455. ibv_dereg_mr (wrid2void(wc[i].wr_id));
  456. }
  457. }
  458. return (0);
  459. }
  460. static int recv_token_cq_recv_event_fn (int events, int suck, void *context)
  461. {
  462. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  463. struct ibv_wc wc[32];
  464. struct ibv_cq *ev_cq;
  465. void *ev_ctx;
  466. int res;
  467. int i;
  468. ibv_get_cq_event (instance->recv_token_recv_completion_channel, &ev_cq, &ev_ctx);
  469. ibv_ack_cq_events (ev_cq, 1);
  470. res = ibv_req_notify_cq (ev_cq, 0);
  471. res = ibv_poll_cq (instance->recv_token_recv_cq, 32, wc);
  472. if (res > 0) {
  473. for (i = 0; i < res; i++) {
  474. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  475. recv_token_recv_buf_post (instance, wrid2void(wc[i].wr_id));
  476. }
  477. }
  478. return (0);
  479. }
  480. static int recv_token_accept_destroy (struct totemiba_instance *instance)
  481. {
  482. if (instance->recv_token_accepted == 0) {
  483. return (0);
  484. }
  485. rdma_destroy_qp (instance->recv_token_cma_id);
  486. recv_token_recv_buf_post_destroy (instance);
  487. ibv_destroy_cq (instance->recv_token_send_cq);
  488. ibv_destroy_cq (instance->recv_token_recv_cq);
  489. ibv_destroy_comp_channel (instance->recv_token_send_completion_channel);
  490. ibv_destroy_comp_channel (instance->recv_token_recv_completion_channel);
  491. ibv_dealloc_pd (instance->recv_token_pd);
  492. rdma_destroy_id (instance->recv_token_cma_id);
  493. qb_loop_poll_del (
  494. instance->totemiba_poll_handle,
  495. instance->recv_token_recv_completion_channel->fd);
  496. qb_loop_poll_del (
  497. instance->totemiba_poll_handle,
  498. instance->recv_token_send_completion_channel->fd);
  499. return (0);
  500. }
  501. static int recv_token_accept_setup (struct totemiba_instance *instance)
  502. {
  503. struct ibv_qp_init_attr init_qp_attr;
  504. int res = 0;
  505. /*
  506. * Allocate the protection domain
  507. */
  508. instance->recv_token_pd = ibv_alloc_pd (instance->recv_token_cma_id->verbs);
  509. /*
  510. * Create a completion channel
  511. */
  512. instance->recv_token_recv_completion_channel = ibv_create_comp_channel (instance->recv_token_cma_id->verbs);
  513. if (instance->recv_token_recv_completion_channel == NULL) {
  514. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  515. return (-1);
  516. }
  517. /*
  518. * Create the completion queue
  519. */
  520. instance->recv_token_recv_cq = ibv_create_cq (instance->recv_token_cma_id->verbs,
  521. COMPLETION_QUEUE_ENTRIES, instance,
  522. instance->recv_token_recv_completion_channel, 0);
  523. if (instance->recv_token_recv_cq == NULL) {
  524. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  525. return (-1);
  526. }
  527. res = ibv_req_notify_cq (instance->recv_token_recv_cq, 0);
  528. if (res != 0) {
  529. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  530. return (-1);
  531. }
  532. /*
  533. * Create a completion channel
  534. */
  535. instance->recv_token_send_completion_channel = ibv_create_comp_channel (instance->recv_token_cma_id->verbs);
  536. if (instance->recv_token_send_completion_channel == NULL) {
  537. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  538. return (-1);
  539. }
  540. /*
  541. * Create the completion queue
  542. */
  543. instance->recv_token_send_cq = ibv_create_cq (instance->recv_token_cma_id->verbs,
  544. COMPLETION_QUEUE_ENTRIES, instance,
  545. instance->recv_token_send_completion_channel, 0);
  546. if (instance->recv_token_send_cq == NULL) {
  547. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  548. return (-1);
  549. }
  550. res = ibv_req_notify_cq (instance->recv_token_send_cq, 0);
  551. if (res != 0) {
  552. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  553. return (-1);
  554. }
  555. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  556. init_qp_attr.cap.max_send_wr = 50;
  557. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  558. init_qp_attr.cap.max_send_sge = 1;
  559. init_qp_attr.cap.max_recv_sge = 1;
  560. init_qp_attr.qp_context = instance;
  561. init_qp_attr.sq_sig_all = 0;
  562. init_qp_attr.qp_type = IBV_QPT_UD;
  563. init_qp_attr.send_cq = instance->recv_token_send_cq;
  564. init_qp_attr.recv_cq = instance->recv_token_recv_cq;
  565. res = rdma_create_qp (instance->recv_token_cma_id, instance->recv_token_pd,
  566. &init_qp_attr);
  567. if (res != 0) {
  568. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  569. return (-1);
  570. }
  571. recv_token_recv_buf_post_initial (instance);
  572. qb_loop_poll_add (
  573. instance->totemiba_poll_handle,
  574. QB_LOOP_MED,
  575. instance->recv_token_recv_completion_channel->fd,
  576. POLLIN, instance, recv_token_cq_recv_event_fn);
  577. qb_loop_poll_add (
  578. instance->totemiba_poll_handle,
  579. QB_LOOP_MED,
  580. instance->recv_token_send_completion_channel->fd,
  581. POLLIN, instance, recv_token_cq_send_event_fn);
  582. instance->recv_token_accepted = 1;
  583. return (res);
  584. };
  585. static int recv_token_rdma_event_fn (int events, int suck, void *context)
  586. {
  587. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  588. struct rdma_cm_event *event;
  589. struct rdma_conn_param conn_param;
  590. int res;
  591. res = rdma_get_cm_event (instance->listen_recv_token_channel, &event);
  592. if (res != 0) {
  593. return (0);
  594. }
  595. switch (event->event) {
  596. case RDMA_CM_EVENT_CONNECT_REQUEST:
  597. recv_token_accept_destroy (instance);
  598. instance->recv_token_cma_id = event->id;
  599. recv_token_accept_setup (instance);
  600. memset (&conn_param, 0, sizeof (struct rdma_conn_param));
  601. conn_param.qp_num = instance->recv_token_cma_id->qp->qp_num;
  602. res = rdma_accept (instance->recv_token_cma_id, &conn_param);
  603. break;
  604. default:
  605. log_printf (LOGSYS_LEVEL_ERROR, "default %d", event->event);
  606. break;
  607. }
  608. res = rdma_ack_cm_event (event);
  609. return (0);
  610. }
  611. static int send_token_cq_send_event_fn (int events, int suck, void *context)
  612. {
  613. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  614. struct ibv_wc wc[32];
  615. struct ibv_cq *ev_cq;
  616. void *ev_ctx;
  617. int res;
  618. int i;
  619. ibv_get_cq_event (instance->send_token_send_completion_channel, &ev_cq, &ev_ctx);
  620. ibv_ack_cq_events (ev_cq, 1);
  621. res = ibv_req_notify_cq (ev_cq, 0);
  622. res = ibv_poll_cq (instance->send_token_send_cq, 32, wc);
  623. if (res > 0) {
  624. for (i = 0; i < res; i++) {
  625. token_send_buf_put (instance, wrid2void(wc[i].wr_id));
  626. }
  627. }
  628. return (0);
  629. }
  630. static int send_token_cq_recv_event_fn (int events, int suck, void *context)
  631. {
  632. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  633. struct ibv_wc wc[32];
  634. struct ibv_cq *ev_cq;
  635. void *ev_ctx;
  636. int res;
  637. int i;
  638. ibv_get_cq_event (instance->send_token_recv_completion_channel, &ev_cq, &ev_ctx);
  639. ibv_ack_cq_events (ev_cq, 1);
  640. res = ibv_req_notify_cq (ev_cq, 0);
  641. res = ibv_poll_cq (instance->send_token_recv_cq, 32, wc);
  642. if (res > 0) {
  643. for (i = 0; i < res; i++) {
  644. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  645. }
  646. }
  647. return (0);
  648. }
  649. static int send_token_rdma_event_fn (int events, int suck, void *context)
  650. {
  651. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  652. struct rdma_cm_event *event;
  653. struct rdma_conn_param conn_param;
  654. int res;
  655. res = rdma_get_cm_event (instance->send_token_channel, &event);
  656. if (res != 0) {
  657. return (0);
  658. }
  659. switch (event->event) {
  660. /*
  661. * occurs when we resolve the multicast address
  662. */
  663. case RDMA_CM_EVENT_ADDR_RESOLVED:
  664. res = rdma_resolve_route (instance->send_token_cma_id, 2000);
  665. break;
  666. /*
  667. * occurs when the CM joins the multicast group
  668. */
  669. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  670. memset (&conn_param, 0, sizeof (struct rdma_conn_param));
  671. conn_param.private_data = NULL;
  672. conn_param.private_data_len = 0;
  673. res = rdma_connect (instance->send_token_cma_id, &conn_param);
  674. break;
  675. case RDMA_CM_EVENT_ESTABLISHED:
  676. instance->send_token_qpn = event->param.ud.qp_num;
  677. instance->send_token_qkey = event->param.ud.qkey;
  678. instance->send_token_ah = ibv_create_ah (instance->send_token_pd, &event->param.ud.ah_attr);
  679. instance->totemiba_target_set_completed (instance->rrp_context);
  680. break;
  681. case RDMA_CM_EVENT_ADDR_ERROR:
  682. case RDMA_CM_EVENT_ROUTE_ERROR:
  683. case RDMA_CM_EVENT_MULTICAST_ERROR:
  684. log_printf (LOGSYS_LEVEL_ERROR,
  685. "send_token_rdma_event_fn multicast error");
  686. break;
  687. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  688. break;
  689. case RDMA_CM_EVENT_UNREACHABLE:
  690. log_printf (LOGSYS_LEVEL_ERROR,
  691. "send_token_rdma_event_fn unreachable");
  692. break;
  693. default:
  694. log_printf (LOGSYS_LEVEL_ERROR,
  695. "send_token_rdma_event_fn unknown event %d",
  696. event->event);
  697. break;
  698. }
  699. rdma_ack_cm_event (event);
  700. return (0);
  701. }
  702. static int send_token_bind (struct totemiba_instance *instance)
  703. {
  704. int res;
  705. struct ibv_qp_init_attr init_qp_attr;
  706. instance->send_token_channel = rdma_create_event_channel();
  707. if (instance->send_token_channel == NULL) {
  708. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  709. return (-1);
  710. }
  711. res = rdma_create_id (instance->send_token_channel,
  712. &instance->send_token_cma_id, NULL, RDMA_PS_UDP);
  713. if (res) {
  714. log_printf (LOGSYS_LEVEL_ERROR, "error creating send_token_cma_id");
  715. return (-1);
  716. }
  717. res = rdma_bind_addr (instance->send_token_cma_id,
  718. &instance->send_token_bind_addr);
  719. if (res) {
  720. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for send token");
  721. return (-1);
  722. }
  723. /*
  724. * Resolve the send_token address into a GUID
  725. */
  726. res = rdma_resolve_addr (instance->send_token_cma_id,
  727. &instance->bind_addr, &instance->token_addr, 2000);
  728. if (res) {
  729. log_printf (LOGSYS_LEVEL_ERROR, "error resolving send token address %d %d", res, errno);
  730. return (-1);
  731. }
  732. /*
  733. * Allocate the protection domain
  734. */
  735. instance->send_token_pd = ibv_alloc_pd (instance->send_token_cma_id->verbs);
  736. /*
  737. * Create a completion channel
  738. */
  739. instance->send_token_recv_completion_channel = ibv_create_comp_channel (instance->send_token_cma_id->verbs);
  740. if (instance->send_token_recv_completion_channel == NULL) {
  741. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  742. return (-1);
  743. }
  744. /*
  745. * Create the completion queue
  746. */
  747. instance->send_token_recv_cq = ibv_create_cq (instance->send_token_cma_id->verbs,
  748. COMPLETION_QUEUE_ENTRIES, instance,
  749. instance->send_token_recv_completion_channel, 0);
  750. if (instance->send_token_recv_cq == NULL) {
  751. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  752. return (-1);
  753. }
  754. res = ibv_req_notify_cq (instance->send_token_recv_cq, 0);
  755. if (res != 0) {
  756. log_printf (LOGSYS_LEVEL_ERROR,
  757. "couldn't request notifications of the completion queue");
  758. return (-1);
  759. }
  760. /*
  761. * Create a completion channel
  762. */
  763. instance->send_token_send_completion_channel =
  764. ibv_create_comp_channel (instance->send_token_cma_id->verbs);
  765. if (instance->send_token_send_completion_channel == NULL) {
  766. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  767. return (-1);
  768. }
  769. /*
  770. * Create the completion queue
  771. */
  772. instance->send_token_send_cq = ibv_create_cq (
  773. instance->send_token_cma_id->verbs,
  774. COMPLETION_QUEUE_ENTRIES, instance,
  775. instance->send_token_send_completion_channel, 0);
  776. if (instance->send_token_send_cq == NULL) {
  777. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  778. return (-1);
  779. }
  780. res = ibv_req_notify_cq (instance->send_token_send_cq, 0);
  781. if (res != 0) {
  782. log_printf (LOGSYS_LEVEL_ERROR,
  783. "couldn't request notifications of the completion queue");
  784. return (-1);
  785. }
  786. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  787. init_qp_attr.cap.max_send_wr = 50;
  788. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  789. init_qp_attr.cap.max_send_sge = 1;
  790. init_qp_attr.cap.max_recv_sge = 1;
  791. init_qp_attr.qp_context = instance;
  792. init_qp_attr.sq_sig_all = 0;
  793. init_qp_attr.qp_type = IBV_QPT_UD;
  794. init_qp_attr.send_cq = instance->send_token_send_cq;
  795. init_qp_attr.recv_cq = instance->send_token_recv_cq;
  796. res = rdma_create_qp (instance->send_token_cma_id,
  797. instance->send_token_pd, &init_qp_attr);
  798. if (res != 0) {
  799. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  800. return (-1);
  801. }
  802. qb_loop_poll_add (
  803. instance->totemiba_poll_handle,
  804. QB_LOOP_MED,
  805. instance->send_token_recv_completion_channel->fd,
  806. POLLIN, instance, send_token_cq_recv_event_fn);
  807. qb_loop_poll_add (
  808. instance->totemiba_poll_handle,
  809. QB_LOOP_MED,
  810. instance->send_token_send_completion_channel->fd,
  811. POLLIN, instance, send_token_cq_send_event_fn);
  812. qb_loop_poll_add (
  813. instance->totemiba_poll_handle,
  814. QB_LOOP_MED,
  815. instance->send_token_channel->fd,
  816. POLLIN, instance, send_token_rdma_event_fn);
  817. instance->send_token_bound = 1;
  818. return (0);
  819. }
  820. static int send_token_unbind (struct totemiba_instance *instance)
  821. {
  822. if (instance->send_token_bound == 0) {
  823. return (0);
  824. }
  825. qb_loop_poll_del (
  826. instance->totemiba_poll_handle,
  827. instance->send_token_recv_completion_channel->fd);
  828. qb_loop_poll_del (
  829. instance->totemiba_poll_handle,
  830. instance->send_token_send_completion_channel->fd);
  831. qb_loop_poll_del (
  832. instance->totemiba_poll_handle,
  833. instance->send_token_channel->fd);
  834. rdma_destroy_qp (instance->send_token_cma_id);
  835. ibv_destroy_cq (instance->send_token_send_cq);
  836. ibv_destroy_cq (instance->send_token_recv_cq);
  837. ibv_destroy_comp_channel (instance->send_token_send_completion_channel);
  838. ibv_destroy_comp_channel (instance->send_token_recv_completion_channel);
  839. token_send_buf_destroy (instance);
  840. ibv_dealloc_pd (instance->send_token_pd);
  841. rdma_destroy_id (instance->send_token_cma_id);
  842. rdma_destroy_event_channel (instance->send_token_channel);
  843. return (0);
  844. }
  845. static int recv_token_bind (struct totemiba_instance *instance)
  846. {
  847. int res;
  848. instance->listen_recv_token_channel = rdma_create_event_channel();
  849. if (instance->listen_recv_token_channel == NULL) {
  850. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  851. return (-1);
  852. }
  853. res = rdma_create_id (instance->listen_recv_token_channel,
  854. &instance->listen_recv_token_cma_id, NULL, RDMA_PS_UDP);
  855. if (res) {
  856. log_printf (LOGSYS_LEVEL_ERROR, "error creating recv_token_cma_id");
  857. return (-1);
  858. }
  859. res = rdma_bind_addr (instance->listen_recv_token_cma_id,
  860. &instance->bind_addr);
  861. if (res) {
  862. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for recv token");
  863. return (-1);
  864. }
  865. /*
  866. * Resolve the recv_token address into a GUID
  867. */
  868. res = rdma_listen (instance->listen_recv_token_cma_id, 10);
  869. if (res) {
  870. log_printf (LOGSYS_LEVEL_ERROR, "error listening %d %d", res, errno);
  871. return (-1);
  872. }
  873. qb_loop_poll_add (
  874. instance->totemiba_poll_handle,
  875. QB_LOOP_MED,
  876. instance->listen_recv_token_channel->fd,
  877. POLLIN, instance, recv_token_rdma_event_fn);
  878. return (0);
  879. }
  880. static int mcast_bind (struct totemiba_instance *instance)
  881. {
  882. int res;
  883. struct ibv_qp_init_attr init_qp_attr;
  884. instance->mcast_channel = rdma_create_event_channel();
  885. if (instance->mcast_channel == NULL) {
  886. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  887. return (-1);
  888. }
  889. res = rdma_create_id (instance->mcast_channel, &instance->mcast_cma_id, NULL, RDMA_PS_UDP);
  890. if (res) {
  891. log_printf (LOGSYS_LEVEL_ERROR, "error creating mcast_cma_id");
  892. return (-1);
  893. }
  894. res = rdma_bind_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr);
  895. if (res) {
  896. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for mcast");
  897. return (-1);
  898. }
  899. /*
  900. * Resolve the multicast address into a GUID
  901. */
  902. res = rdma_resolve_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr,
  903. &instance->mcast_addr, 5000);
  904. if (res) {
  905. log_printf (LOGSYS_LEVEL_ERROR, "error resolving multicast address %d %d", res, errno);
  906. return (-1);
  907. }
  908. /*
  909. * Allocate the protection domain
  910. */
  911. instance->mcast_pd = ibv_alloc_pd (instance->mcast_cma_id->verbs);
  912. /*
  913. * Create a completion channel
  914. */
  915. instance->mcast_recv_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  916. if (instance->mcast_recv_completion_channel == NULL) {
  917. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  918. return (-1);
  919. }
  920. /*
  921. * Create the completion queue
  922. */
  923. instance->mcast_recv_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  924. COMPLETION_QUEUE_ENTRIES, instance,
  925. instance->mcast_recv_completion_channel, 0);
  926. if (instance->mcast_recv_cq == NULL) {
  927. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  928. return (-1);
  929. }
  930. res = ibv_req_notify_cq (instance->mcast_recv_cq, 0);
  931. if (res != 0) {
  932. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  933. return (-1);
  934. }
  935. /*
  936. * Create a completion channel
  937. */
  938. instance->mcast_send_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  939. if (instance->mcast_send_completion_channel == NULL) {
  940. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  941. return (-1);
  942. }
  943. /*
  944. * Create the completion queue
  945. */
  946. instance->mcast_send_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  947. COMPLETION_QUEUE_ENTRIES, instance,
  948. instance->mcast_send_completion_channel, 0);
  949. if (instance->mcast_send_cq == NULL) {
  950. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  951. return (-1);
  952. }
  953. res = ibv_req_notify_cq (instance->mcast_send_cq, 0);
  954. if (res != 0) {
  955. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  956. return (-1);
  957. }
  958. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  959. init_qp_attr.cap.max_send_wr = 50;
  960. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  961. init_qp_attr.cap.max_send_sge = 1;
  962. init_qp_attr.cap.max_recv_sge = 1;
  963. init_qp_attr.qp_context = instance;
  964. init_qp_attr.sq_sig_all = 0;
  965. init_qp_attr.qp_type = IBV_QPT_UD;
  966. init_qp_attr.send_cq = instance->mcast_send_cq;
  967. init_qp_attr.recv_cq = instance->mcast_recv_cq;
  968. res = rdma_create_qp (instance->mcast_cma_id, instance->mcast_pd,
  969. &init_qp_attr);
  970. if (res != 0) {
  971. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  972. return (-1);
  973. }
  974. mcast_recv_buf_post_initial (instance);
  975. qb_loop_poll_add (
  976. instance->totemiba_poll_handle,
  977. QB_LOOP_MED,
  978. instance->mcast_recv_completion_channel->fd,
  979. POLLIN, instance, mcast_cq_recv_event_fn);
  980. qb_loop_poll_add (
  981. instance->totemiba_poll_handle,
  982. QB_LOOP_MED,
  983. instance->mcast_send_completion_channel->fd,
  984. POLLIN, instance, mcast_cq_send_event_fn);
  985. qb_loop_poll_add (
  986. instance->totemiba_poll_handle,
  987. QB_LOOP_MED,
  988. instance->mcast_channel->fd,
  989. POLLIN, instance, mcast_rdma_event_fn);
  990. return (0);
  991. }
  992. static void timer_function_netif_check_timeout (
  993. void *data)
  994. {
  995. struct totemiba_instance *instance = (struct totemiba_instance *)data;
  996. int res;
  997. int interface_up;
  998. int interface_num;
  999. int addr_len;
  1000. totemip_iface_check (&instance->totem_interface->bindnet,
  1001. &instance->totem_interface->boundto, &interface_up, &interface_num, instance->totem_config->clear_node_high_bit);
  1002. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1003. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->bind_addr,
  1004. &addr_len);
  1005. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1006. 0, (struct sockaddr_storage *)&instance->send_token_bind_addr,
  1007. &addr_len);
  1008. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1009. 0, (struct sockaddr_storage *)&instance->local_mcast_bind_addr,
  1010. &addr_len);
  1011. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1012. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->my_id,
  1013. &addr_len);
  1014. totemip_sockaddr_to_totemip_convert(
  1015. (const struct sockaddr_storage *)&instance->bind_addr,
  1016. &instance->my_id);
  1017. memcpy (&instance->my_id, &instance->totem_interface->boundto,
  1018. sizeof (struct totem_ip_address));
  1019. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->mcast_addr,
  1020. instance->totem_interface->ip_port,
  1021. (struct sockaddr_storage *)&instance->mcast_addr, &addr_len);
  1022. res = recv_token_bind (instance);
  1023. res = mcast_bind (instance);
  1024. }
  1025. int totemiba_crypto_set (
  1026. void *iba_context,
  1027. const char *cipher_type,
  1028. const char *hash_type)
  1029. {
  1030. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1031. int res = 0;
  1032. instance = NULL;
  1033. return (res);
  1034. }
  1035. int totemiba_finalize (
  1036. void *iba_context)
  1037. {
  1038. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1039. int res = 0;
  1040. instance = NULL;
  1041. return (res);
  1042. }
  1043. /*
  1044. * Create an instance
  1045. */
  1046. int totemiba_initialize (
  1047. qb_loop_t *qb_poll_handle,
  1048. void **iba_context,
  1049. struct totem_config *totem_config,
  1050. int interface_no,
  1051. void *context,
  1052. void (*deliver_fn) (
  1053. void *context,
  1054. const void *msg,
  1055. unsigned int msg_len),
  1056. void (*iface_change_fn) (
  1057. void *context,
  1058. const struct totem_ip_address *iface_address),
  1059. void (*target_set_completed) (
  1060. void *context))
  1061. {
  1062. struct totemiba_instance *instance;
  1063. int res = 0;
  1064. instance = malloc (sizeof (struct totemiba_instance));
  1065. if (instance == NULL) {
  1066. return (-1);
  1067. }
  1068. totemiba_instance_initialize (instance);
  1069. instance->totem_interface = &totem_config->interfaces[interface_no];
  1070. instance->totemiba_poll_handle = qb_poll_handle;
  1071. instance->totem_interface->bindnet.nodeid = totem_config->node_id;
  1072. instance->totemiba_deliver_fn = deliver_fn;
  1073. instance->totemiba_target_set_completed = target_set_completed;
  1074. instance->totemiba_iface_change_fn = iface_change_fn;
  1075. instance->totem_config = totem_config;
  1076. instance->stats = stats;
  1077. instance->rrp_context = context;
  1078. qb_loop_timer_add (instance->totemiba_poll_handle,
  1079. QB_LOOP_MED,
  1080. 100*QB_TIME_NS_IN_MSEC,
  1081. (void *)instance,
  1082. timer_function_netif_check_timeout,
  1083. &instance->timer_netif_check_timeout);
  1084. instance->totemiba_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1085. instance->totemiba_log_printf = totem_config->totem_logging_configuration.log_printf;
  1086. *iba_context = instance;
  1087. return (res);
  1088. }
  1089. void *totemiba_buffer_alloc (void)
  1090. {
  1091. return malloc (MAX_MTU_SIZE);
  1092. }
  1093. void totemiba_buffer_release (void *ptr)
  1094. {
  1095. return free (ptr);
  1096. }
  1097. int totemiba_processor_count_set (
  1098. void *iba_context,
  1099. int processor_count)
  1100. {
  1101. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1102. int res = 0;
  1103. instance = NULL;
  1104. return (res);
  1105. }
  1106. int totemiba_recv_flush (void *iba_context)
  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_send_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_token_send (
  1121. void *iba_context,
  1122. const void *ms,
  1123. unsigned int msg_len)
  1124. {
  1125. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1126. int res = 0;
  1127. struct ibv_send_wr send_wr, *failed_send_wr;
  1128. struct ibv_sge sge;
  1129. void *msg;
  1130. struct send_buf *send_buf;
  1131. send_buf = token_send_buf_get (instance);
  1132. if (send_buf == NULL) {
  1133. return (-1);
  1134. }
  1135. msg = send_buf->buffer;
  1136. memcpy (msg, ms, msg_len);
  1137. send_wr.next = NULL;
  1138. send_wr.sg_list = &sge;
  1139. send_wr.num_sge = 1;
  1140. send_wr.opcode = IBV_WR_SEND;
  1141. send_wr.send_flags = IBV_SEND_SIGNALED;
  1142. send_wr.wr_id = void2wrid(send_buf);
  1143. send_wr.imm_data = 0;
  1144. send_wr.wr.ud.ah = instance->send_token_ah;
  1145. send_wr.wr.ud.remote_qpn = instance->send_token_qpn;
  1146. send_wr.wr.ud.remote_qkey = instance->send_token_qkey;
  1147. sge.length = msg_len;
  1148. sge.lkey = send_buf->mr->lkey;
  1149. sge.addr = (uintptr_t)msg;
  1150. res = ibv_post_send (instance->send_token_cma_id->qp, &send_wr, &failed_send_wr);
  1151. return (res);
  1152. }
  1153. int totemiba_mcast_flush_send (
  1154. void *iba_context,
  1155. const void *ms,
  1156. unsigned int msg_len)
  1157. {
  1158. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1159. int res = 0;
  1160. struct ibv_send_wr send_wr, *failed_send_wr;
  1161. struct ibv_sge sge;
  1162. void *msg;
  1163. struct send_buf *send_buf;
  1164. send_buf = mcast_send_buf_get (instance);
  1165. if (send_buf == NULL) {
  1166. return (-1);
  1167. }
  1168. msg = send_buf->buffer;
  1169. memcpy (msg, ms, msg_len);
  1170. send_wr.next = NULL;
  1171. send_wr.sg_list = &sge;
  1172. send_wr.num_sge = 1;
  1173. send_wr.opcode = IBV_WR_SEND;
  1174. send_wr.send_flags = IBV_SEND_SIGNALED;
  1175. send_wr.wr_id = void2wrid(send_buf);
  1176. send_wr.imm_data = 0;
  1177. send_wr.wr.ud.ah = instance->mcast_ah;
  1178. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1179. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1180. sge.length = msg_len;
  1181. sge.lkey = send_buf->mr->lkey;
  1182. sge.addr = (uintptr_t)msg;
  1183. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1184. return (res);
  1185. }
  1186. int totemiba_mcast_noflush_send (
  1187. void *iba_context,
  1188. const void *ms,
  1189. unsigned int msg_len)
  1190. {
  1191. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1192. int res = 0;
  1193. struct ibv_send_wr send_wr, *failed_send_wr;
  1194. struct ibv_sge sge;
  1195. void *msg;
  1196. struct send_buf *send_buf;
  1197. send_buf = mcast_send_buf_get (instance);
  1198. if (send_buf == NULL) {
  1199. return (-1);
  1200. }
  1201. msg = send_buf->buffer;
  1202. memcpy (msg, ms, msg_len);
  1203. send_wr.next = NULL;
  1204. send_wr.sg_list = &sge;
  1205. send_wr.num_sge = 1;
  1206. send_wr.opcode = IBV_WR_SEND;
  1207. send_wr.send_flags = IBV_SEND_SIGNALED;
  1208. send_wr.wr_id = void2wrid(send_buf);
  1209. send_wr.imm_data = 0;
  1210. send_wr.wr.ud.ah = instance->mcast_ah;
  1211. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1212. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1213. sge.length = msg_len;
  1214. sge.lkey = send_buf->mr->lkey;
  1215. sge.addr = (uintptr_t)msg;
  1216. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1217. return (res);
  1218. }
  1219. extern int totemiba_iface_check (void *iba_context)
  1220. {
  1221. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1222. int res = 0;
  1223. instance = NULL;
  1224. return (res);
  1225. }
  1226. extern void totemiba_net_mtu_adjust (void *iba_context, struct totem_config *totem_config)
  1227. {
  1228. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1229. instance = NULL;
  1230. }
  1231. const char *totemiba_iface_print (void *iba_context) {
  1232. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1233. const char *ret_char;
  1234. ret_char = totemip_print (&instance->my_id);
  1235. return (ret_char);
  1236. }
  1237. int totemiba_iface_get (
  1238. void *iba_context,
  1239. struct totem_ip_address *addr)
  1240. {
  1241. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1242. int res = 0;
  1243. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1244. return (res);
  1245. }
  1246. int totemiba_token_target_set (
  1247. void *iba_context,
  1248. const struct totem_ip_address *token_target)
  1249. {
  1250. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1251. int res = 0;
  1252. int addr_len = 16;
  1253. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)token_target,
  1254. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->token_addr,
  1255. &addr_len);
  1256. res = send_token_unbind (instance);
  1257. res = send_token_bind (instance);
  1258. return (res);
  1259. }
  1260. extern int totemiba_recv_mcast_empty (
  1261. void *iba_context)
  1262. {
  1263. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1264. int res = 0;
  1265. instance = NULL;
  1266. return (res);
  1267. }