totemiba.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /*
  2. * Copyright (c) 2009-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <pthread.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.h>
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <stdlib.h>
  59. #include <sys/types.h>
  60. #include <sys/socket.h>
  61. #include <netdb.h>
  62. #include <rdma/rdma_cma.h>
  63. #include <assert.h>
  64. #include <errno.h>
  65. #include <corosync/sq.h>
  66. #include <corosync/list.h>
  67. #include <corosync/hdb.h>
  68. #include <corosync/swab.h>
  69. #include <qb/qbdefs.h>
  70. #include <qb/qbloop.h>
  71. #define LOGSYS_UTILS_ONLY 1
  72. #include <corosync/logsys.h>
  73. #include "totemiba.h"
  74. #define COMPLETION_QUEUE_ENTRIES 100
  75. #define TOTAL_READ_POSTS 100
  76. #define MAX_MTU_SIZE 4096
  77. struct totemiba_instance {
  78. struct sockaddr bind_addr;
  79. struct sockaddr send_token_bind_addr;
  80. struct sockaddr mcast_addr;
  81. struct sockaddr token_addr;
  82. struct sockaddr local_mcast_bind_addr;
  83. struct totem_interface *totem_interface;
  84. struct totem_config *totem_config;
  85. void (*totemiba_iface_change_fn) (
  86. void *context,
  87. const struct totem_ip_address *iface_address);
  88. void (*totemiba_deliver_fn) (
  89. void *context,
  90. const void *msg,
  91. unsigned int msg_len);
  92. void (*totemiba_target_set_completed) (
  93. void *context);
  94. void *rrp_context;
  95. qb_loop_timer_handle timer_netif_check_timeout;
  96. qb_loop_t *totemiba_poll_handle;
  97. struct totem_ip_address my_id;
  98. struct rdma_event_channel *mcast_channel;
  99. struct rdma_cm_id *mcast_cma_id;
  100. struct ibv_pd *mcast_pd;
  101. struct sockaddr mcast_dest_addr;
  102. uint32_t mcast_qpn;
  103. uint32_t mcast_qkey;
  104. struct ibv_ah *mcast_ah;
  105. struct ibv_comp_channel *mcast_send_completion_channel;
  106. struct ibv_comp_channel *mcast_recv_completion_channel;
  107. struct ibv_cq *mcast_send_cq;
  108. struct ibv_cq *mcast_recv_cq;
  109. int recv_token_accepted;
  110. struct rdma_event_channel *recv_token_channel;
  111. struct rdma_event_channel *listen_recv_token_channel;
  112. struct rdma_cm_id *listen_recv_token_cma_id;
  113. struct rdma_cm_id *recv_token_cma_id;
  114. struct ibv_pd *recv_token_pd;
  115. struct sockaddr recv_token_dest_addr;
  116. struct ibv_comp_channel *recv_token_send_completion_channel;
  117. struct ibv_comp_channel *recv_token_recv_completion_channel;
  118. struct ibv_cq *recv_token_send_cq;
  119. struct ibv_cq *recv_token_recv_cq;
  120. int send_token_bound;
  121. struct rdma_event_channel *send_token_channel;
  122. struct rdma_cm_id *send_token_cma_id;
  123. struct ibv_pd *send_token_pd;
  124. struct sockaddr send_token_dest_addr;
  125. uint32_t send_token_qpn;
  126. uint32_t send_token_qkey;
  127. struct ibv_ah *send_token_ah;
  128. struct ibv_comp_channel *send_token_send_completion_channel;
  129. struct ibv_comp_channel *send_token_recv_completion_channel;
  130. struct ibv_cq *send_token_send_cq;
  131. struct ibv_cq *send_token_recv_cq;
  132. void (*totemiba_log_printf) (
  133. int level,
  134. int subsys,
  135. const char *function,
  136. const char *file,
  137. int line,
  138. const char *format,
  139. ...)__attribute__((format(printf, 6, 7)));
  140. int totemiba_subsys_id;
  141. struct list_head mcast_send_buf_free;
  142. struct list_head token_send_buf_free;
  143. struct list_head mcast_send_buf_head;
  144. struct list_head token_send_buf_head;
  145. struct list_head recv_token_recv_buf_head;
  146. };
  147. union u {
  148. uint64_t wr_id;
  149. void *v;
  150. };
  151. #define log_printf(level, format, args...) \
  152. do { \
  153. instance->totemiba_log_printf ( \
  154. level, \
  155. instance->totemiba_subsys_id, \
  156. __FUNCTION__, __FILE__, __LINE__, \
  157. (const char *)format, ##args); \
  158. } while (0);
  159. struct recv_buf {
  160. struct list_head list_all;
  161. struct ibv_recv_wr recv_wr;
  162. struct ibv_sge sge;
  163. struct ibv_mr *mr;
  164. char buffer[MAX_MTU_SIZE];
  165. };
  166. struct send_buf {
  167. struct list_head list_free;
  168. struct list_head list_all;
  169. struct ibv_mr *mr;
  170. char buffer[MAX_MTU_SIZE];
  171. };
  172. static hdb_handle_t
  173. void2wrid (void *v) { union u u; u.v = v; return u.wr_id; }
  174. static void *
  175. wrid2void (uint64_t wr_id) { union u u; u.wr_id = wr_id; return u.v; }
  176. static void totemiba_instance_initialize (struct totemiba_instance *instance)
  177. {
  178. memset (instance, 0, sizeof (struct totemiba_instance));
  179. list_init (&instance->mcast_send_buf_free);
  180. list_init (&instance->token_send_buf_free);
  181. list_init (&instance->mcast_send_buf_head);
  182. list_init (&instance->token_send_buf_head);
  183. list_init (&instance->recv_token_recv_buf_head);
  184. }
  185. static inline struct send_buf *mcast_send_buf_get (
  186. struct totemiba_instance *instance)
  187. {
  188. struct send_buf *send_buf;
  189. if (list_empty (&instance->mcast_send_buf_free) == 0) {
  190. send_buf = list_entry (instance->mcast_send_buf_free.next, struct send_buf, list_free);
  191. list_del (&send_buf->list_free);
  192. return (send_buf);
  193. }
  194. send_buf = malloc (sizeof (struct send_buf));
  195. if (send_buf == NULL) {
  196. return (NULL);
  197. }
  198. send_buf->mr = ibv_reg_mr (instance->mcast_pd,
  199. send_buf->buffer,
  200. 2048, IBV_ACCESS_LOCAL_WRITE);
  201. if (send_buf->mr == NULL) {
  202. log_printf (LOGSYS_LEVEL_ERROR, "couldn't register memory range");
  203. free (send_buf);
  204. return (NULL);
  205. }
  206. list_init (&send_buf->list_all);
  207. list_add_tail (&send_buf->list_all, &instance->mcast_send_buf_head);
  208. return (send_buf);
  209. }
  210. static inline void mcast_send_buf_put (
  211. struct totemiba_instance *instance,
  212. struct send_buf *send_buf)
  213. {
  214. list_init (&send_buf->list_free);
  215. list_add_tail (&send_buf->list_free, &instance->mcast_send_buf_free);
  216. }
  217. static inline struct send_buf *token_send_buf_get (
  218. struct totemiba_instance *instance)
  219. {
  220. struct send_buf *send_buf;
  221. if (list_empty (&instance->token_send_buf_free) == 0) {
  222. send_buf = list_entry (instance->token_send_buf_free.next, struct send_buf, list_free);
  223. list_del (&send_buf->list_free);
  224. return (send_buf);
  225. }
  226. send_buf = malloc (sizeof (struct send_buf));
  227. if (send_buf == NULL) {
  228. return (NULL);
  229. }
  230. send_buf->mr = ibv_reg_mr (instance->send_token_pd,
  231. send_buf->buffer,
  232. 2048, IBV_ACCESS_LOCAL_WRITE);
  233. if (send_buf->mr == NULL) {
  234. log_printf (LOGSYS_LEVEL_ERROR, "couldn't register memory range");
  235. free (send_buf);
  236. return (NULL);
  237. }
  238. list_init (&send_buf->list_all);
  239. list_add_tail (&send_buf->list_all, &instance->token_send_buf_head);
  240. return (send_buf);
  241. }
  242. static inline void token_send_buf_destroy (struct totemiba_instance *instance)
  243. {
  244. struct list_head *list;
  245. struct send_buf *send_buf;
  246. for (list = instance->token_send_buf_head.next; list != &instance->token_send_buf_head;) {
  247. send_buf = list_entry (list, struct send_buf, list_all);
  248. list = list->next;
  249. ibv_dereg_mr (send_buf->mr);
  250. free (send_buf);
  251. }
  252. list_init (&instance->token_send_buf_free);
  253. list_init (&instance->token_send_buf_head);
  254. }
  255. static inline void token_send_buf_put (
  256. struct totemiba_instance *instance,
  257. struct send_buf *send_buf)
  258. {
  259. list_init (&send_buf->list_free);
  260. list_add_tail (&send_buf->list_free, &instance->token_send_buf_free);
  261. }
  262. static inline struct recv_buf *recv_token_recv_buf_create (
  263. struct totemiba_instance *instance)
  264. {
  265. struct recv_buf *recv_buf;
  266. recv_buf = malloc (sizeof (struct recv_buf));
  267. if (recv_buf == NULL) {
  268. return (NULL);
  269. }
  270. recv_buf->mr = ibv_reg_mr (instance->recv_token_pd, &recv_buf->buffer,
  271. 2048,
  272. IBV_ACCESS_LOCAL_WRITE);
  273. recv_buf->recv_wr.next = NULL;
  274. recv_buf->recv_wr.sg_list = &recv_buf->sge;
  275. recv_buf->recv_wr.num_sge = 1;
  276. recv_buf->recv_wr.wr_id = (uintptr_t)recv_buf;
  277. recv_buf->sge.length = 2048;
  278. recv_buf->sge.lkey = recv_buf->mr->lkey;
  279. recv_buf->sge.addr = (uintptr_t)recv_buf->buffer;
  280. list_init (&recv_buf->list_all);
  281. list_add (&recv_buf->list_all, &instance->recv_token_recv_buf_head);
  282. return (recv_buf);
  283. }
  284. static inline int recv_token_recv_buf_post (struct totemiba_instance *instance, struct recv_buf *recv_buf)
  285. {
  286. struct ibv_recv_wr *fail_recv;
  287. int res;
  288. res = ibv_post_recv (instance->recv_token_cma_id->qp, &recv_buf->recv_wr, &fail_recv);
  289. return (res);
  290. }
  291. static inline void recv_token_recv_buf_post_initial (struct totemiba_instance *instance)
  292. {
  293. struct recv_buf *recv_buf;
  294. unsigned int i;
  295. for (i = 0; i < TOTAL_READ_POSTS; i++) {
  296. recv_buf = recv_token_recv_buf_create (instance);
  297. recv_token_recv_buf_post (instance, recv_buf);
  298. }
  299. }
  300. static inline void recv_token_recv_buf_post_destroy (
  301. struct totemiba_instance *instance)
  302. {
  303. struct recv_buf *recv_buf;
  304. struct list_head *list;
  305. for (list = instance->recv_token_recv_buf_head.next;
  306. list != &instance->recv_token_recv_buf_head;) {
  307. recv_buf = list_entry (list, struct recv_buf, list_all);
  308. list = list->next;
  309. ibv_dereg_mr (recv_buf->mr);
  310. free (recv_buf);
  311. }
  312. list_init (&instance->recv_token_recv_buf_head);
  313. }
  314. static inline struct recv_buf *mcast_recv_buf_create (struct totemiba_instance *instance)
  315. {
  316. struct recv_buf *recv_buf;
  317. struct ibv_mr *mr;
  318. recv_buf = malloc (sizeof (struct recv_buf));
  319. if (recv_buf == NULL) {
  320. return (NULL);
  321. }
  322. mr = ibv_reg_mr (instance->mcast_pd, &recv_buf->buffer,
  323. 2048,
  324. IBV_ACCESS_LOCAL_WRITE);
  325. recv_buf->recv_wr.next = NULL;
  326. recv_buf->recv_wr.sg_list = &recv_buf->sge;
  327. recv_buf->recv_wr.num_sge = 1;
  328. recv_buf->recv_wr.wr_id = (uintptr_t)recv_buf;
  329. recv_buf->sge.length = 2048;
  330. recv_buf->sge.lkey = mr->lkey;
  331. recv_buf->sge.addr = (uintptr_t)recv_buf->buffer;
  332. return (recv_buf);
  333. }
  334. static inline int mcast_recv_buf_post (struct totemiba_instance *instance, struct recv_buf *recv_buf)
  335. {
  336. struct ibv_recv_wr *fail_recv;
  337. int res;
  338. res = ibv_post_recv (instance->mcast_cma_id->qp, &recv_buf->recv_wr, &fail_recv);
  339. return (res);
  340. }
  341. static inline void mcast_recv_buf_post_initial (struct totemiba_instance *instance)
  342. {
  343. struct recv_buf *recv_buf;
  344. unsigned int i;
  345. for (i = 0; i < TOTAL_READ_POSTS; i++) {
  346. recv_buf = mcast_recv_buf_create (instance);
  347. mcast_recv_buf_post (instance, recv_buf);
  348. }
  349. }
  350. static inline void iba_deliver_fn (struct totemiba_instance *instance, uint64_t wr_id, uint32_t bytes)
  351. {
  352. const char *addr;
  353. const struct recv_buf *recv_buf;
  354. recv_buf = wrid2void(wr_id);
  355. addr = &recv_buf->buffer[sizeof (struct ibv_grh)];
  356. instance->totemiba_deliver_fn (instance->rrp_context, addr, bytes);
  357. }
  358. static int mcast_cq_send_event_fn (int events, int suck, void *context)
  359. {
  360. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  361. struct ibv_wc wc[32];
  362. struct ibv_cq *ev_cq;
  363. void *ev_ctx;
  364. int res;
  365. int i;
  366. ibv_get_cq_event (instance->mcast_send_completion_channel, &ev_cq, &ev_ctx);
  367. ibv_ack_cq_events (ev_cq, 1);
  368. res = ibv_req_notify_cq (ev_cq, 0);
  369. res = ibv_poll_cq (instance->mcast_send_cq, 32, wc);
  370. if (res > 0) {
  371. for (i = 0; i < res; i++) {
  372. mcast_send_buf_put (instance, wrid2void(wc[i].wr_id));
  373. }
  374. }
  375. return (0);
  376. }
  377. static int mcast_cq_recv_event_fn (int events, int suck, void *context)
  378. {
  379. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  380. struct ibv_wc wc[64];
  381. struct ibv_cq *ev_cq;
  382. void *ev_ctx;
  383. int res;
  384. int i;
  385. ibv_get_cq_event (instance->mcast_recv_completion_channel, &ev_cq, &ev_ctx);
  386. ibv_ack_cq_events (ev_cq, 1);
  387. res = ibv_req_notify_cq (ev_cq, 0);
  388. res = ibv_poll_cq (instance->mcast_recv_cq, 64, wc);
  389. if (res > 0) {
  390. for (i = 0; i < res; i++) {
  391. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  392. mcast_recv_buf_post (instance, wrid2void(wc[i].wr_id));
  393. }
  394. }
  395. return (0);
  396. }
  397. static int mcast_rdma_event_fn (int events, int suck, void *context)
  398. {
  399. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  400. struct rdma_cm_event *event;
  401. int res;
  402. res = rdma_get_cm_event (instance->mcast_channel, &event);
  403. if (res != 0) {
  404. return (0);
  405. }
  406. switch (event->event) {
  407. /*
  408. * occurs when we resolve the multicast address
  409. */
  410. case RDMA_CM_EVENT_ADDR_RESOLVED:
  411. rdma_join_multicast (instance->mcast_cma_id, &instance->mcast_addr, instance);
  412. usleep(1000);
  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. if(instance->send_token_ah)
  835. {
  836. ibv_destroy_ah(instance->send_token_ah);
  837. instance->send_token_ah = 0;
  838. }
  839. rdma_destroy_qp (instance->send_token_cma_id);
  840. ibv_destroy_cq (instance->send_token_send_cq);
  841. ibv_destroy_cq (instance->send_token_recv_cq);
  842. ibv_destroy_comp_channel (instance->send_token_send_completion_channel);
  843. ibv_destroy_comp_channel (instance->send_token_recv_completion_channel);
  844. token_send_buf_destroy (instance);
  845. ibv_dealloc_pd (instance->send_token_pd);
  846. rdma_destroy_id (instance->send_token_cma_id);
  847. rdma_destroy_event_channel (instance->send_token_channel);
  848. return (0);
  849. }
  850. static int recv_token_bind (struct totemiba_instance *instance)
  851. {
  852. int res;
  853. instance->listen_recv_token_channel = rdma_create_event_channel();
  854. if (instance->listen_recv_token_channel == NULL) {
  855. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  856. return (-1);
  857. }
  858. res = rdma_create_id (instance->listen_recv_token_channel,
  859. &instance->listen_recv_token_cma_id, NULL, RDMA_PS_UDP);
  860. if (res) {
  861. log_printf (LOGSYS_LEVEL_ERROR, "error creating recv_token_cma_id");
  862. return (-1);
  863. }
  864. res = rdma_bind_addr (instance->listen_recv_token_cma_id,
  865. &instance->bind_addr);
  866. if (res) {
  867. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for recv token");
  868. return (-1);
  869. }
  870. /*
  871. * Resolve the recv_token address into a GUID
  872. */
  873. res = rdma_listen (instance->listen_recv_token_cma_id, 10);
  874. if (res) {
  875. log_printf (LOGSYS_LEVEL_ERROR, "error listening %d %d", res, errno);
  876. return (-1);
  877. }
  878. qb_loop_poll_add (
  879. instance->totemiba_poll_handle,
  880. QB_LOOP_MED,
  881. instance->listen_recv_token_channel->fd,
  882. POLLIN, instance, recv_token_rdma_event_fn);
  883. return (0);
  884. }
  885. static int mcast_bind (struct totemiba_instance *instance)
  886. {
  887. int res;
  888. struct ibv_qp_init_attr init_qp_attr;
  889. instance->mcast_channel = rdma_create_event_channel();
  890. if (instance->mcast_channel == NULL) {
  891. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  892. return (-1);
  893. }
  894. res = rdma_create_id (instance->mcast_channel, &instance->mcast_cma_id, NULL, RDMA_PS_UDP);
  895. if (res) {
  896. log_printf (LOGSYS_LEVEL_ERROR, "error creating mcast_cma_id");
  897. return (-1);
  898. }
  899. res = rdma_bind_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr);
  900. if (res) {
  901. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for mcast");
  902. return (-1);
  903. }
  904. /*
  905. * Resolve the multicast address into a GUID
  906. */
  907. res = rdma_resolve_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr,
  908. &instance->mcast_addr, 5000);
  909. if (res) {
  910. log_printf (LOGSYS_LEVEL_ERROR, "error resolving multicast address %d %d", res, errno);
  911. return (-1);
  912. }
  913. /*
  914. * Allocate the protection domain
  915. */
  916. instance->mcast_pd = ibv_alloc_pd (instance->mcast_cma_id->verbs);
  917. /*
  918. * Create a completion channel
  919. */
  920. instance->mcast_recv_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  921. if (instance->mcast_recv_completion_channel == NULL) {
  922. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  923. return (-1);
  924. }
  925. /*
  926. * Create the completion queue
  927. */
  928. instance->mcast_recv_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  929. COMPLETION_QUEUE_ENTRIES, instance,
  930. instance->mcast_recv_completion_channel, 0);
  931. if (instance->mcast_recv_cq == NULL) {
  932. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  933. return (-1);
  934. }
  935. res = ibv_req_notify_cq (instance->mcast_recv_cq, 0);
  936. if (res != 0) {
  937. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  938. return (-1);
  939. }
  940. /*
  941. * Create a completion channel
  942. */
  943. instance->mcast_send_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  944. if (instance->mcast_send_completion_channel == NULL) {
  945. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  946. return (-1);
  947. }
  948. /*
  949. * Create the completion queue
  950. */
  951. instance->mcast_send_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  952. COMPLETION_QUEUE_ENTRIES, instance,
  953. instance->mcast_send_completion_channel, 0);
  954. if (instance->mcast_send_cq == NULL) {
  955. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  956. return (-1);
  957. }
  958. res = ibv_req_notify_cq (instance->mcast_send_cq, 0);
  959. if (res != 0) {
  960. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  961. return (-1);
  962. }
  963. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  964. init_qp_attr.cap.max_send_wr = 50;
  965. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  966. init_qp_attr.cap.max_send_sge = 1;
  967. init_qp_attr.cap.max_recv_sge = 1;
  968. init_qp_attr.qp_context = instance;
  969. init_qp_attr.sq_sig_all = 0;
  970. init_qp_attr.qp_type = IBV_QPT_UD;
  971. init_qp_attr.send_cq = instance->mcast_send_cq;
  972. init_qp_attr.recv_cq = instance->mcast_recv_cq;
  973. res = rdma_create_qp (instance->mcast_cma_id, instance->mcast_pd,
  974. &init_qp_attr);
  975. if (res != 0) {
  976. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  977. return (-1);
  978. }
  979. mcast_recv_buf_post_initial (instance);
  980. qb_loop_poll_add (
  981. instance->totemiba_poll_handle,
  982. QB_LOOP_MED,
  983. instance->mcast_recv_completion_channel->fd,
  984. POLLIN, instance, mcast_cq_recv_event_fn);
  985. qb_loop_poll_add (
  986. instance->totemiba_poll_handle,
  987. QB_LOOP_MED,
  988. instance->mcast_send_completion_channel->fd,
  989. POLLIN, instance, mcast_cq_send_event_fn);
  990. qb_loop_poll_add (
  991. instance->totemiba_poll_handle,
  992. QB_LOOP_MED,
  993. instance->mcast_channel->fd,
  994. POLLIN, instance, mcast_rdma_event_fn);
  995. return (0);
  996. }
  997. static void timer_function_netif_check_timeout (
  998. void *data)
  999. {
  1000. struct totemiba_instance *instance = (struct totemiba_instance *)data;
  1001. int res;
  1002. int interface_up;
  1003. int interface_num;
  1004. int addr_len;
  1005. totemip_iface_check (&instance->totem_interface->bindnet,
  1006. &instance->totem_interface->boundto, &interface_up, &interface_num, instance->totem_config->clear_node_high_bit);
  1007. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1008. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->bind_addr,
  1009. &addr_len);
  1010. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1011. 0, (struct sockaddr_storage *)&instance->send_token_bind_addr,
  1012. &addr_len);
  1013. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1014. 0, (struct sockaddr_storage *)&instance->local_mcast_bind_addr,
  1015. &addr_len);
  1016. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1017. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->my_id,
  1018. &addr_len);
  1019. totemip_sockaddr_to_totemip_convert(
  1020. (const struct sockaddr_storage *)&instance->bind_addr,
  1021. &instance->my_id);
  1022. memcpy (&instance->my_id, &instance->totem_interface->boundto,
  1023. sizeof (struct totem_ip_address));
  1024. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->mcast_addr,
  1025. instance->totem_interface->ip_port,
  1026. (struct sockaddr_storage *)&instance->mcast_addr, &addr_len);
  1027. res = recv_token_bind (instance);
  1028. res = mcast_bind (instance);
  1029. }
  1030. int totemiba_crypto_set (
  1031. void *iba_context,
  1032. const char *cipher_type,
  1033. const char *hash_type)
  1034. {
  1035. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1036. int res = 0;
  1037. instance = NULL;
  1038. return (res);
  1039. }
  1040. int totemiba_finalize (
  1041. void *iba_context)
  1042. {
  1043. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1044. int res = 0;
  1045. instance = NULL;
  1046. return (res);
  1047. }
  1048. /*
  1049. * Create an instance
  1050. */
  1051. int totemiba_initialize (
  1052. qb_loop_t *qb_poll_handle,
  1053. void **iba_context,
  1054. struct totem_config *totem_config,
  1055. int interface_no,
  1056. void *context,
  1057. void (*deliver_fn) (
  1058. void *context,
  1059. const void *msg,
  1060. unsigned int msg_len),
  1061. void (*iface_change_fn) (
  1062. void *context,
  1063. const struct totem_ip_address *iface_address),
  1064. void (*target_set_completed) (
  1065. void *context))
  1066. {
  1067. struct totemiba_instance *instance;
  1068. int res = 0;
  1069. instance = malloc (sizeof (struct totemiba_instance));
  1070. if (instance == NULL) {
  1071. return (-1);
  1072. }
  1073. totemiba_instance_initialize (instance);
  1074. instance->totem_interface = &totem_config->interfaces[interface_no];
  1075. instance->totemiba_poll_handle = qb_poll_handle;
  1076. instance->totem_interface->bindnet.nodeid = totem_config->node_id;
  1077. instance->totemiba_deliver_fn = deliver_fn;
  1078. instance->totemiba_target_set_completed = target_set_completed;
  1079. instance->totemiba_iface_change_fn = iface_change_fn;
  1080. instance->totem_config = totem_config;
  1081. instance->rrp_context = context;
  1082. qb_loop_timer_add (instance->totemiba_poll_handle,
  1083. QB_LOOP_MED,
  1084. 100*QB_TIME_NS_IN_MSEC,
  1085. (void *)instance,
  1086. timer_function_netif_check_timeout,
  1087. &instance->timer_netif_check_timeout);
  1088. instance->totemiba_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1089. instance->totemiba_log_printf = totem_config->totem_logging_configuration.log_printf;
  1090. *iba_context = instance;
  1091. return (res);
  1092. }
  1093. void *totemiba_buffer_alloc (void)
  1094. {
  1095. return malloc (MAX_MTU_SIZE);
  1096. }
  1097. void totemiba_buffer_release (void *ptr)
  1098. {
  1099. return free (ptr);
  1100. }
  1101. int totemiba_processor_count_set (
  1102. void *iba_context,
  1103. int processor_count)
  1104. {
  1105. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1106. int res = 0;
  1107. instance = NULL;
  1108. return (res);
  1109. }
  1110. int totemiba_recv_flush (void *iba_context)
  1111. {
  1112. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1113. int res = 0;
  1114. instance = NULL;
  1115. return (res);
  1116. }
  1117. int totemiba_send_flush (void *iba_context)
  1118. {
  1119. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1120. int res = 0;
  1121. instance = NULL;
  1122. return (res);
  1123. }
  1124. int totemiba_token_send (
  1125. void *iba_context,
  1126. const void *ms,
  1127. unsigned int msg_len)
  1128. {
  1129. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1130. int res = 0;
  1131. struct ibv_send_wr send_wr, *failed_send_wr;
  1132. struct ibv_sge sge;
  1133. void *msg;
  1134. struct send_buf *send_buf;
  1135. send_buf = token_send_buf_get (instance);
  1136. if (send_buf == NULL) {
  1137. return (-1);
  1138. }
  1139. msg = send_buf->buffer;
  1140. memcpy (msg, ms, msg_len);
  1141. send_wr.next = NULL;
  1142. send_wr.sg_list = &sge;
  1143. send_wr.num_sge = 1;
  1144. send_wr.opcode = IBV_WR_SEND;
  1145. send_wr.send_flags = IBV_SEND_SIGNALED;
  1146. send_wr.wr_id = void2wrid(send_buf);
  1147. send_wr.imm_data = 0;
  1148. send_wr.wr.ud.ah = instance->send_token_ah;
  1149. send_wr.wr.ud.remote_qpn = instance->send_token_qpn;
  1150. send_wr.wr.ud.remote_qkey = instance->send_token_qkey;
  1151. sge.length = msg_len;
  1152. sge.lkey = send_buf->mr->lkey;
  1153. sge.addr = (uintptr_t)msg;
  1154. if(instance->send_token_ah != 0 && instance->send_token_bound)
  1155. res = ibv_post_send (instance->send_token_cma_id->qp, &send_wr, &failed_send_wr);
  1156. return (res);
  1157. }
  1158. int totemiba_mcast_flush_send (
  1159. void *iba_context,
  1160. const void *ms,
  1161. unsigned int msg_len)
  1162. {
  1163. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1164. int res = 0;
  1165. struct ibv_send_wr send_wr, *failed_send_wr;
  1166. struct ibv_sge sge;
  1167. void *msg;
  1168. struct send_buf *send_buf;
  1169. send_buf = mcast_send_buf_get (instance);
  1170. if (send_buf == NULL) {
  1171. return (-1);
  1172. }
  1173. msg = send_buf->buffer;
  1174. memcpy (msg, ms, msg_len);
  1175. send_wr.next = NULL;
  1176. send_wr.sg_list = &sge;
  1177. send_wr.num_sge = 1;
  1178. send_wr.opcode = IBV_WR_SEND;
  1179. send_wr.send_flags = IBV_SEND_SIGNALED;
  1180. send_wr.wr_id = void2wrid(send_buf);
  1181. send_wr.imm_data = 0;
  1182. send_wr.wr.ud.ah = instance->mcast_ah;
  1183. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1184. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1185. sge.length = msg_len;
  1186. sge.lkey = send_buf->mr->lkey;
  1187. sge.addr = (uintptr_t)msg;
  1188. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1189. return (res);
  1190. }
  1191. int totemiba_mcast_noflush_send (
  1192. void *iba_context,
  1193. const void *ms,
  1194. unsigned int msg_len)
  1195. {
  1196. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1197. int res = 0;
  1198. struct ibv_send_wr send_wr, *failed_send_wr;
  1199. struct ibv_sge sge;
  1200. void *msg;
  1201. struct send_buf *send_buf;
  1202. send_buf = mcast_send_buf_get (instance);
  1203. if (send_buf == NULL) {
  1204. return (-1);
  1205. }
  1206. msg = send_buf->buffer;
  1207. memcpy (msg, ms, msg_len);
  1208. send_wr.next = NULL;
  1209. send_wr.sg_list = &sge;
  1210. send_wr.num_sge = 1;
  1211. send_wr.opcode = IBV_WR_SEND;
  1212. send_wr.send_flags = IBV_SEND_SIGNALED;
  1213. send_wr.wr_id = void2wrid(send_buf);
  1214. send_wr.imm_data = 0;
  1215. send_wr.wr.ud.ah = instance->mcast_ah;
  1216. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1217. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1218. sge.length = msg_len;
  1219. sge.lkey = send_buf->mr->lkey;
  1220. sge.addr = (uintptr_t)msg;
  1221. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1222. return (res);
  1223. }
  1224. extern int totemiba_iface_check (void *iba_context)
  1225. {
  1226. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1227. int res = 0;
  1228. instance = NULL;
  1229. return (res);
  1230. }
  1231. extern void totemiba_net_mtu_adjust (void *iba_context, struct totem_config *totem_config)
  1232. {
  1233. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1234. instance = NULL;
  1235. }
  1236. const char *totemiba_iface_print (void *iba_context) {
  1237. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1238. const char *ret_char;
  1239. ret_char = totemip_print (&instance->my_id);
  1240. return (ret_char);
  1241. }
  1242. int totemiba_iface_get (
  1243. void *iba_context,
  1244. struct totem_ip_address *addr)
  1245. {
  1246. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1247. int res = 0;
  1248. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1249. return (res);
  1250. }
  1251. int totemiba_token_target_set (
  1252. void *iba_context,
  1253. const struct totem_ip_address *token_target)
  1254. {
  1255. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1256. int res = 0;
  1257. int addr_len = 16;
  1258. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)token_target,
  1259. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->token_addr,
  1260. &addr_len);
  1261. res = send_token_unbind (instance);
  1262. res = send_token_bind (instance);
  1263. return (res);
  1264. }
  1265. extern int totemiba_recv_mcast_empty (
  1266. void *iba_context)
  1267. {
  1268. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1269. int res = 0;
  1270. instance = NULL;
  1271. return (res);
  1272. }