totemiba.c 42 KB

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