totemiba.c 41 KB

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