4
0

totemiba.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * Copyright (c) 2009-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <pthread.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.h>
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <stdlib.h>
  59. #include <sys/types.h>
  60. #include <sys/socket.h>
  61. #include <netdb.h>
  62. #include <rdma/rdma_cma.h>
  63. #include <assert.h>
  64. #include <errno.h>
  65. #include <corosync/sq.h>
  66. #include <corosync/list.h>
  67. #include <corosync/hdb.h>
  68. #include <corosync/swab.h>
  69. #include <qb/qbdefs.h>
  70. #include <qb/qbloop.h>
  71. #define LOGSYS_UTILS_ONLY 1
  72. #include <corosync/logsys.h>
  73. #include "totemiba.h"
  74. #define COMPLETION_QUEUE_ENTRIES 100
  75. #define TOTAL_READ_POSTS 100
  76. #define MAX_MTU_SIZE 4096
  77. #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. qb_loop_timer_handle timer_netif_check_timeout;
  98. qb_loop_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. int level,
  136. int subsys,
  137. const char *function,
  138. const char *file,
  139. int line,
  140. const char *format,
  141. ...)__attribute__((format(printf, 6, 7)));
  142. int totemiba_subsys_id;
  143. struct list_head mcast_send_buf_free;
  144. struct list_head token_send_buf_free;
  145. struct list_head mcast_send_buf_head;
  146. struct list_head token_send_buf_head;
  147. struct list_head recv_token_recv_buf_head;
  148. int mcast_seen_joined;
  149. qb_loop_timer_handle mcast_rejoin;
  150. };
  151. union u {
  152. uint64_t wr_id;
  153. void *v;
  154. };
  155. #define log_printf(level, format, args...) \
  156. do { \
  157. instance->totemiba_log_printf ( \
  158. level, \
  159. instance->totemiba_subsys_id, \
  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");
  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");
  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 (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 (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",
  415. errno,
  416. MCAST_REJOIN_MSEC);
  417. qb_loop_timer_add (instance->totemiba_poll_handle,
  418. QB_LOOP_MED,
  419. MCAST_REJOIN_MSEC * QB_TIME_NS_IN_MSEC,
  420. (void *)instance,
  421. mcast_rejoin,
  422. &instance->mcast_rejoin);
  423. }
  424. }
  425. static int mcast_rdma_event_fn (int fd, int events, void *context)
  426. {
  427. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  428. struct rdma_cm_event *event;
  429. int res;
  430. res = rdma_get_cm_event (instance->mcast_channel, &event);
  431. if (res != 0) {
  432. return (0);
  433. }
  434. switch (event->event) {
  435. /*
  436. * occurs when we resolve the multicast address
  437. */
  438. case RDMA_CM_EVENT_ADDR_RESOLVED:
  439. res = rdma_join_multicast (instance->mcast_cma_id, &instance->mcast_addr, instance);
  440. usleep(1000);
  441. if (res == 0) break;
  442. case RDMA_CM_EVENT_MULTICAST_ERROR:
  443. log_printf (LOGSYS_LEVEL_ERROR, "multicast error, trying to rejoin in %u ms", MCAST_REJOIN_MSEC);
  444. qb_loop_timer_add (instance->totemiba_poll_handle,
  445. QB_LOOP_MED,
  446. MCAST_REJOIN_MSEC * QB_TIME_NS_IN_MSEC,
  447. (void *)instance,
  448. mcast_rejoin,
  449. &instance->mcast_rejoin);
  450. break;
  451. /*
  452. * occurs when the CM joins the multicast group
  453. */
  454. case RDMA_CM_EVENT_MULTICAST_JOIN:
  455. instance->mcast_qpn = event->param.ud.qp_num;
  456. instance->mcast_qkey = event->param.ud.qkey;
  457. instance->mcast_ah = ibv_create_ah (instance->mcast_pd, &event->param.ud.ah_attr);
  458. if (instance->mcast_seen_joined == 0) {
  459. log_printf (LOGSYS_LEVEL_DEBUG, "joining mcast 1st time, running callbacks");
  460. instance->totemiba_iface_change_fn (instance->rrp_context, &instance->my_id);
  461. instance->mcast_seen_joined=1;
  462. }
  463. log_printf (LOGSYS_LEVEL_NOTICE, "Joined multicast!");
  464. break;
  465. case RDMA_CM_EVENT_ADDR_ERROR:
  466. case RDMA_CM_EVENT_ROUTE_ERROR:
  467. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  468. break;
  469. default:
  470. log_printf (LOGSYS_LEVEL_ERROR, "default %d", event->event);
  471. break;
  472. }
  473. rdma_ack_cm_event (event);
  474. return (0);
  475. }
  476. static int recv_token_cq_send_event_fn (
  477. int fd,
  478. int revents,
  479. void *context)
  480. {
  481. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  482. struct ibv_wc wc[32];
  483. struct ibv_cq *ev_cq;
  484. void *ev_ctx;
  485. int res;
  486. int i;
  487. ibv_get_cq_event (instance->recv_token_send_completion_channel, &ev_cq, &ev_ctx);
  488. ibv_ack_cq_events (ev_cq, 1);
  489. res = ibv_req_notify_cq (ev_cq, 0);
  490. res = ibv_poll_cq (instance->recv_token_send_cq, 32, wc);
  491. if (res > 0) {
  492. for (i = 0; i < res; i++) {
  493. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  494. ibv_dereg_mr (wrid2void(wc[i].wr_id));
  495. }
  496. }
  497. return (0);
  498. }
  499. static int recv_token_cq_recv_event_fn (int fd, int events, void *context)
  500. {
  501. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  502. struct ibv_wc wc[32];
  503. struct ibv_cq *ev_cq;
  504. void *ev_ctx;
  505. int res;
  506. int i;
  507. ibv_get_cq_event (instance->recv_token_recv_completion_channel, &ev_cq, &ev_ctx);
  508. ibv_ack_cq_events (ev_cq, 1);
  509. res = ibv_req_notify_cq (ev_cq, 0);
  510. res = ibv_poll_cq (instance->recv_token_recv_cq, 32, wc);
  511. if (res > 0) {
  512. for (i = 0; i < res; i++) {
  513. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  514. recv_token_recv_buf_post (instance, wrid2void(wc[i].wr_id));
  515. }
  516. }
  517. return (0);
  518. }
  519. static int recv_token_accept_destroy (struct totemiba_instance *instance)
  520. {
  521. if (instance->recv_token_accepted == 0) {
  522. return (0);
  523. }
  524. qb_loop_poll_del (
  525. instance->totemiba_poll_handle,
  526. instance->recv_token_recv_completion_channel->fd);
  527. qb_loop_poll_del (
  528. instance->totemiba_poll_handle,
  529. instance->recv_token_send_completion_channel->fd);
  530. rdma_destroy_qp (instance->recv_token_cma_id);
  531. recv_token_recv_buf_post_destroy (instance);
  532. ibv_destroy_cq (instance->recv_token_send_cq);
  533. ibv_destroy_cq (instance->recv_token_recv_cq);
  534. ibv_destroy_comp_channel (instance->recv_token_send_completion_channel);
  535. ibv_destroy_comp_channel (instance->recv_token_recv_completion_channel);
  536. ibv_dealloc_pd (instance->recv_token_pd);
  537. rdma_destroy_id (instance->recv_token_cma_id);
  538. return (0);
  539. }
  540. static int recv_token_accept_setup (struct totemiba_instance *instance)
  541. {
  542. struct ibv_qp_init_attr init_qp_attr;
  543. int res = 0;
  544. /*
  545. * Allocate the protection domain
  546. */
  547. instance->recv_token_pd = ibv_alloc_pd (instance->recv_token_cma_id->verbs);
  548. /*
  549. * Create a completion channel
  550. */
  551. instance->recv_token_recv_completion_channel = ibv_create_comp_channel (instance->recv_token_cma_id->verbs);
  552. if (instance->recv_token_recv_completion_channel == NULL) {
  553. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  554. return (-1);
  555. }
  556. /*
  557. * Create the completion queue
  558. */
  559. instance->recv_token_recv_cq = ibv_create_cq (instance->recv_token_cma_id->verbs,
  560. COMPLETION_QUEUE_ENTRIES, instance,
  561. instance->recv_token_recv_completion_channel, 0);
  562. if (instance->recv_token_recv_cq == NULL) {
  563. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  564. return (-1);
  565. }
  566. res = ibv_req_notify_cq (instance->recv_token_recv_cq, 0);
  567. if (res != 0) {
  568. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  569. return (-1);
  570. }
  571. /*
  572. * Create a completion channel
  573. */
  574. instance->recv_token_send_completion_channel = ibv_create_comp_channel (instance->recv_token_cma_id->verbs);
  575. if (instance->recv_token_send_completion_channel == NULL) {
  576. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  577. return (-1);
  578. }
  579. /*
  580. * Create the completion queue
  581. */
  582. instance->recv_token_send_cq = ibv_create_cq (instance->recv_token_cma_id->verbs,
  583. COMPLETION_QUEUE_ENTRIES, instance,
  584. instance->recv_token_send_completion_channel, 0);
  585. if (instance->recv_token_send_cq == NULL) {
  586. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  587. return (-1);
  588. }
  589. res = ibv_req_notify_cq (instance->recv_token_send_cq, 0);
  590. if (res != 0) {
  591. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  592. return (-1);
  593. }
  594. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  595. init_qp_attr.cap.max_send_wr = 50;
  596. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  597. init_qp_attr.cap.max_send_sge = 1;
  598. init_qp_attr.cap.max_recv_sge = 1;
  599. init_qp_attr.qp_context = instance;
  600. init_qp_attr.sq_sig_all = 0;
  601. init_qp_attr.qp_type = IBV_QPT_UD;
  602. init_qp_attr.send_cq = instance->recv_token_send_cq;
  603. init_qp_attr.recv_cq = instance->recv_token_recv_cq;
  604. res = rdma_create_qp (instance->recv_token_cma_id, instance->recv_token_pd,
  605. &init_qp_attr);
  606. if (res != 0) {
  607. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  608. return (-1);
  609. }
  610. recv_token_recv_buf_post_initial (instance);
  611. qb_loop_poll_add (
  612. instance->totemiba_poll_handle,
  613. QB_LOOP_MED,
  614. instance->recv_token_recv_completion_channel->fd,
  615. POLLIN, instance, recv_token_cq_recv_event_fn);
  616. qb_loop_poll_add (
  617. instance->totemiba_poll_handle,
  618. QB_LOOP_MED,
  619. instance->recv_token_send_completion_channel->fd,
  620. POLLIN, instance, recv_token_cq_send_event_fn);
  621. instance->recv_token_accepted = 1;
  622. return (res);
  623. };
  624. static int recv_token_rdma_event_fn (int fd, int events, void *context)
  625. {
  626. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  627. struct rdma_cm_event *event;
  628. struct rdma_conn_param conn_param;
  629. int res;
  630. res = rdma_get_cm_event (instance->listen_recv_token_channel, &event);
  631. if (res != 0) {
  632. return (0);
  633. }
  634. switch (event->event) {
  635. case RDMA_CM_EVENT_CONNECT_REQUEST:
  636. recv_token_accept_destroy (instance);
  637. instance->recv_token_cma_id = event->id;
  638. recv_token_accept_setup (instance);
  639. memset (&conn_param, 0, sizeof (struct rdma_conn_param));
  640. conn_param.qp_num = instance->recv_token_cma_id->qp->qp_num;
  641. res = rdma_accept (instance->recv_token_cma_id, &conn_param);
  642. break;
  643. default:
  644. log_printf (LOGSYS_LEVEL_ERROR, "default %d", event->event);
  645. break;
  646. }
  647. res = rdma_ack_cm_event (event);
  648. return (0);
  649. }
  650. static int send_token_cq_send_event_fn (int fd, int events, void *context)
  651. {
  652. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  653. struct ibv_wc wc[32];
  654. struct ibv_cq *ev_cq;
  655. void *ev_ctx;
  656. int res;
  657. int i;
  658. ibv_get_cq_event (instance->send_token_send_completion_channel, &ev_cq, &ev_ctx);
  659. ibv_ack_cq_events (ev_cq, 1);
  660. res = ibv_req_notify_cq (ev_cq, 0);
  661. res = ibv_poll_cq (instance->send_token_send_cq, 32, wc);
  662. if (res > 0) {
  663. for (i = 0; i < res; i++) {
  664. token_send_buf_put (instance, wrid2void(wc[i].wr_id));
  665. }
  666. }
  667. return (0);
  668. }
  669. static int send_token_cq_recv_event_fn (int fd, int events, void *context)
  670. {
  671. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  672. struct ibv_wc wc[32];
  673. struct ibv_cq *ev_cq;
  674. void *ev_ctx;
  675. int res;
  676. int i;
  677. ibv_get_cq_event (instance->send_token_recv_completion_channel, &ev_cq, &ev_ctx);
  678. ibv_ack_cq_events (ev_cq, 1);
  679. res = ibv_req_notify_cq (ev_cq, 0);
  680. res = ibv_poll_cq (instance->send_token_recv_cq, 32, wc);
  681. if (res > 0) {
  682. for (i = 0; i < res; i++) {
  683. iba_deliver_fn (instance, wc[i].wr_id, wc[i].byte_len);
  684. }
  685. }
  686. return (0);
  687. }
  688. static int send_token_rdma_event_fn (int fd, int events, void *context)
  689. {
  690. struct totemiba_instance *instance = (struct totemiba_instance *)context;
  691. struct rdma_cm_event *event;
  692. struct rdma_conn_param conn_param;
  693. int res;
  694. res = rdma_get_cm_event (instance->send_token_channel, &event);
  695. if (res != 0) {
  696. return (0);
  697. }
  698. switch (event->event) {
  699. /*
  700. * occurs when we resolve the multicast address
  701. */
  702. case RDMA_CM_EVENT_ADDR_RESOLVED:
  703. res = rdma_resolve_route (instance->send_token_cma_id, 2000);
  704. break;
  705. /*
  706. * occurs when the CM joins the multicast group
  707. */
  708. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  709. memset (&conn_param, 0, sizeof (struct rdma_conn_param));
  710. conn_param.private_data = NULL;
  711. conn_param.private_data_len = 0;
  712. res = rdma_connect (instance->send_token_cma_id, &conn_param);
  713. break;
  714. case RDMA_CM_EVENT_ESTABLISHED:
  715. instance->send_token_qpn = event->param.ud.qp_num;
  716. instance->send_token_qkey = event->param.ud.qkey;
  717. instance->send_token_ah = ibv_create_ah (instance->send_token_pd, &event->param.ud.ah_attr);
  718. instance->totemiba_target_set_completed (instance->rrp_context);
  719. break;
  720. case RDMA_CM_EVENT_ADDR_ERROR:
  721. case RDMA_CM_EVENT_ROUTE_ERROR:
  722. case RDMA_CM_EVENT_MULTICAST_ERROR:
  723. log_printf (LOGSYS_LEVEL_ERROR,
  724. "send_token_rdma_event_fn multicast error");
  725. break;
  726. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  727. break;
  728. case RDMA_CM_EVENT_UNREACHABLE:
  729. log_printf (LOGSYS_LEVEL_ERROR,
  730. "send_token_rdma_event_fn unreachable");
  731. break;
  732. default:
  733. log_printf (LOGSYS_LEVEL_ERROR,
  734. "send_token_rdma_event_fn unknown event %d",
  735. event->event);
  736. break;
  737. }
  738. rdma_ack_cm_event (event);
  739. return (0);
  740. }
  741. static int send_token_bind (struct totemiba_instance *instance)
  742. {
  743. int res;
  744. struct ibv_qp_init_attr init_qp_attr;
  745. instance->send_token_channel = rdma_create_event_channel();
  746. if (instance->send_token_channel == NULL) {
  747. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  748. return (-1);
  749. }
  750. res = rdma_create_id (instance->send_token_channel,
  751. &instance->send_token_cma_id, NULL, RDMA_PS_UDP);
  752. if (res) {
  753. log_printf (LOGSYS_LEVEL_ERROR, "error creating send_token_cma_id");
  754. return (-1);
  755. }
  756. res = rdma_bind_addr (instance->send_token_cma_id,
  757. &instance->send_token_bind_addr);
  758. if (res) {
  759. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for send token");
  760. return (-1);
  761. }
  762. /*
  763. * Resolve the send_token address into a GUID
  764. */
  765. res = rdma_resolve_addr (instance->send_token_cma_id,
  766. &instance->bind_addr, &instance->token_addr, 2000);
  767. if (res) {
  768. log_printf (LOGSYS_LEVEL_ERROR, "error resolving send token address %d %d", res, errno);
  769. return (-1);
  770. }
  771. /*
  772. * Allocate the protection domain
  773. */
  774. instance->send_token_pd = ibv_alloc_pd (instance->send_token_cma_id->verbs);
  775. /*
  776. * Create a completion channel
  777. */
  778. instance->send_token_recv_completion_channel = ibv_create_comp_channel (instance->send_token_cma_id->verbs);
  779. if (instance->send_token_recv_completion_channel == NULL) {
  780. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  781. return (-1);
  782. }
  783. /*
  784. * Create the completion queue
  785. */
  786. instance->send_token_recv_cq = ibv_create_cq (instance->send_token_cma_id->verbs,
  787. COMPLETION_QUEUE_ENTRIES, instance,
  788. instance->send_token_recv_completion_channel, 0);
  789. if (instance->send_token_recv_cq == NULL) {
  790. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  791. return (-1);
  792. }
  793. res = ibv_req_notify_cq (instance->send_token_recv_cq, 0);
  794. if (res != 0) {
  795. log_printf (LOGSYS_LEVEL_ERROR,
  796. "couldn't request notifications of the completion queue");
  797. return (-1);
  798. }
  799. /*
  800. * Create a completion channel
  801. */
  802. instance->send_token_send_completion_channel =
  803. ibv_create_comp_channel (instance->send_token_cma_id->verbs);
  804. if (instance->send_token_send_completion_channel == NULL) {
  805. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  806. return (-1);
  807. }
  808. /*
  809. * Create the completion queue
  810. */
  811. instance->send_token_send_cq = ibv_create_cq (
  812. instance->send_token_cma_id->verbs,
  813. COMPLETION_QUEUE_ENTRIES, instance,
  814. instance->send_token_send_completion_channel, 0);
  815. if (instance->send_token_send_cq == NULL) {
  816. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  817. return (-1);
  818. }
  819. res = ibv_req_notify_cq (instance->send_token_send_cq, 0);
  820. if (res != 0) {
  821. log_printf (LOGSYS_LEVEL_ERROR,
  822. "couldn't request notifications of the completion queue");
  823. return (-1);
  824. }
  825. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  826. init_qp_attr.cap.max_send_wr = 50;
  827. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  828. init_qp_attr.cap.max_send_sge = 1;
  829. init_qp_attr.cap.max_recv_sge = 1;
  830. init_qp_attr.qp_context = instance;
  831. init_qp_attr.sq_sig_all = 0;
  832. init_qp_attr.qp_type = IBV_QPT_UD;
  833. init_qp_attr.send_cq = instance->send_token_send_cq;
  834. init_qp_attr.recv_cq = instance->send_token_recv_cq;
  835. res = rdma_create_qp (instance->send_token_cma_id,
  836. instance->send_token_pd, &init_qp_attr);
  837. if (res != 0) {
  838. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  839. return (-1);
  840. }
  841. qb_loop_poll_add (
  842. instance->totemiba_poll_handle,
  843. QB_LOOP_MED,
  844. instance->send_token_recv_completion_channel->fd,
  845. POLLIN, instance, send_token_cq_recv_event_fn);
  846. qb_loop_poll_add (
  847. instance->totemiba_poll_handle,
  848. QB_LOOP_MED,
  849. instance->send_token_send_completion_channel->fd,
  850. POLLIN, instance, send_token_cq_send_event_fn);
  851. qb_loop_poll_add (
  852. instance->totemiba_poll_handle,
  853. QB_LOOP_MED,
  854. instance->send_token_channel->fd,
  855. POLLIN, instance, send_token_rdma_event_fn);
  856. instance->send_token_bound = 1;
  857. return (0);
  858. }
  859. static int send_token_unbind (struct totemiba_instance *instance)
  860. {
  861. if (instance->send_token_bound == 0) {
  862. return (0);
  863. }
  864. qb_loop_poll_del (
  865. instance->totemiba_poll_handle,
  866. instance->send_token_recv_completion_channel->fd);
  867. qb_loop_poll_del (
  868. instance->totemiba_poll_handle,
  869. instance->send_token_send_completion_channel->fd);
  870. qb_loop_poll_del (
  871. instance->totemiba_poll_handle,
  872. instance->send_token_channel->fd);
  873. if(instance->send_token_ah)
  874. {
  875. ibv_destroy_ah(instance->send_token_ah);
  876. instance->send_token_ah = 0;
  877. }
  878. rdma_destroy_qp (instance->send_token_cma_id);
  879. ibv_destroy_cq (instance->send_token_send_cq);
  880. ibv_destroy_cq (instance->send_token_recv_cq);
  881. ibv_destroy_comp_channel (instance->send_token_send_completion_channel);
  882. ibv_destroy_comp_channel (instance->send_token_recv_completion_channel);
  883. token_send_buf_destroy (instance);
  884. ibv_dealloc_pd (instance->send_token_pd);
  885. rdma_destroy_id (instance->send_token_cma_id);
  886. rdma_destroy_event_channel (instance->send_token_channel);
  887. return (0);
  888. }
  889. static int recv_token_bind (struct totemiba_instance *instance)
  890. {
  891. int res;
  892. struct ibv_port_attr port_attr;
  893. instance->listen_recv_token_channel = rdma_create_event_channel();
  894. if (instance->listen_recv_token_channel == NULL) {
  895. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  896. return (-1);
  897. }
  898. res = rdma_create_id (instance->listen_recv_token_channel,
  899. &instance->listen_recv_token_cma_id, NULL, RDMA_PS_UDP);
  900. if (res) {
  901. log_printf (LOGSYS_LEVEL_ERROR, "error creating recv_token_cma_id");
  902. return (-1);
  903. }
  904. res = rdma_bind_addr (instance->listen_recv_token_cma_id,
  905. &instance->bind_addr);
  906. if (res) {
  907. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for recv token");
  908. return (-1);
  909. }
  910. /*
  911. * Determine active_mtu of port and compare it with the configured one (160 is aproximation of all totem
  912. * structures.
  913. *
  914. * TODO: Implement MTU discovery also for IP and handle MTU correctly for all structures inside totemsrp,
  915. * crypto, ...
  916. */
  917. res = ibv_query_port (instance->listen_recv_token_cma_id->verbs, instance->listen_recv_token_cma_id->port_num, &port_attr);
  918. if ( (1 << (port_attr.active_mtu + 7)) < instance->totem_config->net_mtu + 160) {
  919. log_printf (LOGSYS_LEVEL_ERROR, "requested net_mtu is %d and is larger than the active port mtu %d\n",\
  920. instance->totem_config->net_mtu + 160, (1 << (port_attr.active_mtu + 7)));
  921. return (-1);
  922. }
  923. /*
  924. * Resolve the recv_token address into a GUID
  925. */
  926. res = rdma_listen (instance->listen_recv_token_cma_id, 10);
  927. if (res) {
  928. log_printf (LOGSYS_LEVEL_ERROR, "error listening %d %d", res, errno);
  929. return (-1);
  930. }
  931. qb_loop_poll_add (
  932. instance->totemiba_poll_handle,
  933. QB_LOOP_MED,
  934. instance->listen_recv_token_channel->fd,
  935. POLLIN, instance, recv_token_rdma_event_fn);
  936. return (0);
  937. }
  938. static int mcast_bind (struct totemiba_instance *instance)
  939. {
  940. int res;
  941. struct ibv_qp_init_attr init_qp_attr;
  942. instance->mcast_channel = rdma_create_event_channel();
  943. if (instance->mcast_channel == NULL) {
  944. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create rdma channel");
  945. return (-1);
  946. }
  947. res = rdma_create_id (instance->mcast_channel, &instance->mcast_cma_id, NULL, RDMA_PS_UDP);
  948. if (res) {
  949. log_printf (LOGSYS_LEVEL_ERROR, "error creating mcast_cma_id");
  950. return (-1);
  951. }
  952. res = rdma_bind_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr);
  953. if (res) {
  954. log_printf (LOGSYS_LEVEL_ERROR, "error doing rdma_bind_addr for mcast");
  955. return (-1);
  956. }
  957. /*
  958. * Resolve the multicast address into a GUID
  959. */
  960. res = rdma_resolve_addr (instance->mcast_cma_id, &instance->local_mcast_bind_addr,
  961. &instance->mcast_addr, 5000);
  962. if (res) {
  963. log_printf (LOGSYS_LEVEL_ERROR, "error resolving multicast address %d %d", res, errno);
  964. return (-1);
  965. }
  966. /*
  967. * Allocate the protection domain
  968. */
  969. instance->mcast_pd = ibv_alloc_pd (instance->mcast_cma_id->verbs);
  970. /*
  971. * Create a completion channel
  972. */
  973. instance->mcast_recv_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  974. if (instance->mcast_recv_completion_channel == NULL) {
  975. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  976. return (-1);
  977. }
  978. /*
  979. * Create the completion queue
  980. */
  981. instance->mcast_recv_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  982. COMPLETION_QUEUE_ENTRIES, instance,
  983. instance->mcast_recv_completion_channel, 0);
  984. if (instance->mcast_recv_cq == NULL) {
  985. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  986. return (-1);
  987. }
  988. res = ibv_req_notify_cq (instance->mcast_recv_cq, 0);
  989. if (res != 0) {
  990. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  991. return (-1);
  992. }
  993. /*
  994. * Create a completion channel
  995. */
  996. instance->mcast_send_completion_channel = ibv_create_comp_channel (instance->mcast_cma_id->verbs);
  997. if (instance->mcast_send_completion_channel == NULL) {
  998. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion channel");
  999. return (-1);
  1000. }
  1001. /*
  1002. * Create the completion queue
  1003. */
  1004. instance->mcast_send_cq = ibv_create_cq (instance->mcast_cma_id->verbs,
  1005. COMPLETION_QUEUE_ENTRIES, instance,
  1006. instance->mcast_send_completion_channel, 0);
  1007. if (instance->mcast_send_cq == NULL) {
  1008. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create completion queue");
  1009. return (-1);
  1010. }
  1011. res = ibv_req_notify_cq (instance->mcast_send_cq, 0);
  1012. if (res != 0) {
  1013. log_printf (LOGSYS_LEVEL_ERROR, "couldn't request notifications of the completion queue");
  1014. return (-1);
  1015. }
  1016. memset (&init_qp_attr, 0, sizeof (struct ibv_qp_init_attr));
  1017. init_qp_attr.cap.max_send_wr = 50;
  1018. init_qp_attr.cap.max_recv_wr = TOTAL_READ_POSTS;
  1019. init_qp_attr.cap.max_send_sge = 1;
  1020. init_qp_attr.cap.max_recv_sge = 1;
  1021. init_qp_attr.qp_context = instance;
  1022. init_qp_attr.sq_sig_all = 0;
  1023. init_qp_attr.qp_type = IBV_QPT_UD;
  1024. init_qp_attr.send_cq = instance->mcast_send_cq;
  1025. init_qp_attr.recv_cq = instance->mcast_recv_cq;
  1026. res = rdma_create_qp (instance->mcast_cma_id, instance->mcast_pd,
  1027. &init_qp_attr);
  1028. if (res != 0) {
  1029. log_printf (LOGSYS_LEVEL_ERROR, "couldn't create queue pair");
  1030. return (-1);
  1031. }
  1032. mcast_recv_buf_post_initial (instance);
  1033. qb_loop_poll_add (
  1034. instance->totemiba_poll_handle,
  1035. QB_LOOP_MED,
  1036. instance->mcast_recv_completion_channel->fd,
  1037. POLLIN, instance, mcast_cq_recv_event_fn);
  1038. qb_loop_poll_add (
  1039. instance->totemiba_poll_handle,
  1040. QB_LOOP_MED,
  1041. instance->mcast_send_completion_channel->fd,
  1042. POLLIN, instance, mcast_cq_send_event_fn);
  1043. qb_loop_poll_add (
  1044. instance->totemiba_poll_handle,
  1045. QB_LOOP_MED,
  1046. instance->mcast_channel->fd,
  1047. POLLIN, instance, mcast_rdma_event_fn);
  1048. return (0);
  1049. }
  1050. static void timer_function_netif_check_timeout (
  1051. void *data)
  1052. {
  1053. struct totemiba_instance *instance = (struct totemiba_instance *)data;
  1054. int res;
  1055. int interface_up;
  1056. int interface_num;
  1057. int addr_len;
  1058. totemip_iface_check (&instance->totem_interface->bindnet,
  1059. &instance->totem_interface->boundto, &interface_up, &interface_num, instance->totem_config->clear_node_high_bit);
  1060. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1061. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->bind_addr,
  1062. &addr_len);
  1063. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1064. 0, (struct sockaddr_storage *)&instance->send_token_bind_addr,
  1065. &addr_len);
  1066. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1067. 0, (struct sockaddr_storage *)&instance->local_mcast_bind_addr,
  1068. &addr_len);
  1069. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->boundto,
  1070. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->my_id,
  1071. &addr_len);
  1072. totemip_sockaddr_to_totemip_convert(
  1073. (const struct sockaddr_storage *)&instance->bind_addr,
  1074. &instance->my_id);
  1075. memcpy (&instance->my_id, &instance->totem_interface->boundto,
  1076. sizeof (struct totem_ip_address));
  1077. totemip_totemip_to_sockaddr_convert(&instance->totem_interface->mcast_addr,
  1078. instance->totem_interface->ip_port,
  1079. (struct sockaddr_storage *)&instance->mcast_addr, &addr_len);
  1080. res = recv_token_bind (instance);
  1081. res = mcast_bind (instance);
  1082. }
  1083. int totemiba_crypto_set (
  1084. void *iba_context,
  1085. const char *cipher_type,
  1086. const char *hash_type)
  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_finalize (
  1094. void *iba_context)
  1095. {
  1096. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1097. int res = 0;
  1098. instance = NULL;
  1099. return (res);
  1100. }
  1101. /*
  1102. * Create an instance
  1103. */
  1104. int totemiba_initialize (
  1105. qb_loop_t *qb_poll_handle,
  1106. void **iba_context,
  1107. struct totem_config *totem_config,
  1108. totemsrp_stats_t *stats,
  1109. int interface_no,
  1110. void *context,
  1111. void (*deliver_fn) (
  1112. void *context,
  1113. const void *msg,
  1114. unsigned int msg_len),
  1115. void (*iface_change_fn) (
  1116. void *context,
  1117. const struct totem_ip_address *iface_address),
  1118. void (*target_set_completed) (
  1119. void *context))
  1120. {
  1121. struct totemiba_instance *instance;
  1122. int res = 0;
  1123. instance = malloc (sizeof (struct totemiba_instance));
  1124. if (instance == NULL) {
  1125. return (-1);
  1126. }
  1127. totemiba_instance_initialize (instance);
  1128. instance->totem_interface = &totem_config->interfaces[interface_no];
  1129. instance->totemiba_poll_handle = qb_poll_handle;
  1130. instance->totem_interface->bindnet.nodeid = totem_config->node_id;
  1131. instance->totemiba_deliver_fn = deliver_fn;
  1132. instance->totemiba_target_set_completed = target_set_completed;
  1133. instance->totemiba_iface_change_fn = iface_change_fn;
  1134. instance->totem_config = totem_config;
  1135. instance->stats = stats;
  1136. instance->rrp_context = context;
  1137. qb_loop_timer_add (instance->totemiba_poll_handle,
  1138. QB_LOOP_MED,
  1139. 100*QB_TIME_NS_IN_MSEC,
  1140. (void *)instance,
  1141. timer_function_netif_check_timeout,
  1142. &instance->timer_netif_check_timeout);
  1143. instance->totemiba_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1144. instance->totemiba_log_printf = totem_config->totem_logging_configuration.log_printf;
  1145. *iba_context = instance;
  1146. return (res);
  1147. }
  1148. void *totemiba_buffer_alloc (void)
  1149. {
  1150. return malloc (MAX_MTU_SIZE);
  1151. }
  1152. void totemiba_buffer_release (void *ptr)
  1153. {
  1154. return free (ptr);
  1155. }
  1156. int totemiba_processor_count_set (
  1157. void *iba_context,
  1158. int processor_count)
  1159. {
  1160. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1161. int res = 0;
  1162. instance = NULL;
  1163. return (res);
  1164. }
  1165. int totemiba_recv_flush (void *iba_context)
  1166. {
  1167. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1168. int res = 0;
  1169. instance = NULL;
  1170. return (res);
  1171. }
  1172. int totemiba_send_flush (void *iba_context)
  1173. {
  1174. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1175. int res = 0;
  1176. instance = NULL;
  1177. return (res);
  1178. }
  1179. int totemiba_token_send (
  1180. void *iba_context,
  1181. const void *ms,
  1182. unsigned int msg_len)
  1183. {
  1184. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1185. int res = 0;
  1186. struct ibv_send_wr send_wr, *failed_send_wr;
  1187. struct ibv_sge sge;
  1188. void *msg;
  1189. struct send_buf *send_buf;
  1190. send_buf = token_send_buf_get (instance);
  1191. if (send_buf == NULL) {
  1192. return (-1);
  1193. }
  1194. msg = send_buf->buffer;
  1195. memcpy (msg, ms, msg_len);
  1196. send_wr.next = NULL;
  1197. send_wr.sg_list = &sge;
  1198. send_wr.num_sge = 1;
  1199. send_wr.opcode = IBV_WR_SEND;
  1200. send_wr.send_flags = IBV_SEND_SIGNALED;
  1201. send_wr.wr_id = void2wrid(send_buf);
  1202. send_wr.imm_data = 0;
  1203. send_wr.wr.ud.ah = instance->send_token_ah;
  1204. send_wr.wr.ud.remote_qpn = instance->send_token_qpn;
  1205. send_wr.wr.ud.remote_qkey = instance->send_token_qkey;
  1206. sge.length = msg_len;
  1207. sge.lkey = send_buf->mr->lkey;
  1208. sge.addr = (uintptr_t)msg;
  1209. if(instance->send_token_ah != 0 && instance->send_token_bound)
  1210. res = ibv_post_send (instance->send_token_cma_id->qp, &send_wr, &failed_send_wr);
  1211. return (res);
  1212. }
  1213. int totemiba_mcast_flush_send (
  1214. void *iba_context,
  1215. const void *ms,
  1216. unsigned int msg_len)
  1217. {
  1218. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1219. int res = 0;
  1220. struct ibv_send_wr send_wr, *failed_send_wr;
  1221. struct ibv_sge sge;
  1222. void *msg;
  1223. struct send_buf *send_buf;
  1224. send_buf = mcast_send_buf_get (instance);
  1225. if (send_buf == NULL) {
  1226. return (-1);
  1227. }
  1228. msg = send_buf->buffer;
  1229. memcpy (msg, ms, msg_len);
  1230. send_wr.next = NULL;
  1231. send_wr.sg_list = &sge;
  1232. send_wr.num_sge = 1;
  1233. send_wr.opcode = IBV_WR_SEND;
  1234. send_wr.send_flags = IBV_SEND_SIGNALED;
  1235. send_wr.wr_id = void2wrid(send_buf);
  1236. send_wr.imm_data = 0;
  1237. send_wr.wr.ud.ah = instance->mcast_ah;
  1238. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1239. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1240. sge.length = msg_len;
  1241. sge.lkey = send_buf->mr->lkey;
  1242. sge.addr = (uintptr_t)msg;
  1243. if (instance->mcast_ah != 0) {
  1244. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1245. }
  1246. return (res);
  1247. }
  1248. int totemiba_mcast_noflush_send (
  1249. void *iba_context,
  1250. const void *ms,
  1251. unsigned int msg_len)
  1252. {
  1253. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1254. int res = 0;
  1255. struct ibv_send_wr send_wr, *failed_send_wr;
  1256. struct ibv_sge sge;
  1257. void *msg;
  1258. struct send_buf *send_buf;
  1259. send_buf = mcast_send_buf_get (instance);
  1260. if (send_buf == NULL) {
  1261. return (-1);
  1262. }
  1263. msg = send_buf->buffer;
  1264. memcpy (msg, ms, msg_len);
  1265. send_wr.next = NULL;
  1266. send_wr.sg_list = &sge;
  1267. send_wr.num_sge = 1;
  1268. send_wr.opcode = IBV_WR_SEND;
  1269. send_wr.send_flags = IBV_SEND_SIGNALED;
  1270. send_wr.wr_id = void2wrid(send_buf);
  1271. send_wr.imm_data = 0;
  1272. send_wr.wr.ud.ah = instance->mcast_ah;
  1273. send_wr.wr.ud.remote_qpn = instance->mcast_qpn;
  1274. send_wr.wr.ud.remote_qkey = instance->mcast_qkey;
  1275. sge.length = msg_len;
  1276. sge.lkey = send_buf->mr->lkey;
  1277. sge.addr = (uintptr_t)msg;
  1278. if (instance->mcast_ah != 0) {
  1279. res = ibv_post_send (instance->mcast_cma_id->qp, &send_wr, &failed_send_wr);
  1280. }
  1281. return (res);
  1282. }
  1283. extern int totemiba_iface_check (void *iba_context)
  1284. {
  1285. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1286. int res = 0;
  1287. instance = NULL;
  1288. return (res);
  1289. }
  1290. extern void totemiba_net_mtu_adjust (void *iba_context, struct totem_config *totem_config)
  1291. {
  1292. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1293. instance = NULL;
  1294. }
  1295. const char *totemiba_iface_print (void *iba_context) {
  1296. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1297. const char *ret_char;
  1298. ret_char = totemip_print (&instance->my_id);
  1299. return (ret_char);
  1300. }
  1301. int totemiba_iface_get (
  1302. void *iba_context,
  1303. struct totem_ip_address *addr)
  1304. {
  1305. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1306. int res = 0;
  1307. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1308. return (res);
  1309. }
  1310. int totemiba_token_target_set (
  1311. void *iba_context,
  1312. const struct totem_ip_address *token_target)
  1313. {
  1314. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1315. int res = 0;
  1316. int addr_len = 16;
  1317. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)token_target,
  1318. instance->totem_interface->ip_port, (struct sockaddr_storage *)&instance->token_addr,
  1319. &addr_len);
  1320. res = send_token_unbind (instance);
  1321. res = send_token_bind (instance);
  1322. return (res);
  1323. }
  1324. extern int totemiba_recv_mcast_empty (
  1325. void *iba_context)
  1326. {
  1327. struct totemiba_instance *instance = (struct totemiba_instance *)iba_context;
  1328. int res = 0;
  1329. instance = NULL;
  1330. return (res);
  1331. }