totemiba.c 42 KB

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