totemiba.c 40 KB

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