totemiba.c 40 KB

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