totemiba.c 40 KB

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