totemiba.c 43 KB

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