totemiba.c 40 KB

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