totemnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <assert.h>
  36. #ifdef HAVE_RDMA
  37. #include <totemiba.h>
  38. #endif
  39. #include <totemudp.h>
  40. #include <totemudpu.h>
  41. #include <totemnet.h>
  42. #include <qb/qbloop.h>
  43. #define LOGSYS_UTILS_ONLY 1
  44. #include <corosync/logsys.h>
  45. struct transport {
  46. const char *name;
  47. int (*initialize) (
  48. qb_loop_t *loop_pt,
  49. void **transport_instance,
  50. struct totem_config *totem_config,
  51. int interface_no,
  52. void *context,
  53. void (*deliver_fn) (
  54. void *context,
  55. const void *msg,
  56. unsigned int msg_len),
  57. void (*iface_change_fn) (
  58. void *context,
  59. const struct totem_ip_address *iface_address),
  60. void (*target_set_completed) (
  61. void *context));
  62. void *(*buffer_alloc) (void);
  63. void (*buffer_release) (void *ptr);
  64. int (*processor_count_set) (
  65. void *transport_context,
  66. int processor_count);
  67. int (*token_send) (
  68. void *transport_context,
  69. const void *msg,
  70. unsigned int msg_len);
  71. int (*mcast_flush_send) (
  72. void *transport_context,
  73. const void *msg,
  74. unsigned int msg_len);
  75. int (*mcast_noflush_send) (
  76. void *transport_context,
  77. const void *msg,
  78. unsigned int msg_len);
  79. int (*recv_flush) (void *transport_context);
  80. int (*send_flush) (void *transport_context);
  81. int (*iface_check) (void *transport_context);
  82. int (*finalize) (void *transport_context);
  83. void (*net_mtu_adjust) (void *transport_context, struct totem_config *totem_config);
  84. const char *(*iface_print) (void *transport_context);
  85. int (*iface_get) (
  86. void *transport_context,
  87. struct totem_ip_address *addr);
  88. int (*token_target_set) (
  89. void *transport_context,
  90. const struct totem_ip_address *token_target);
  91. int (*crypto_set) (
  92. void *transport_context,
  93. unsigned int type);
  94. int (*recv_mcast_empty) (
  95. void *transport_context);
  96. int (*member_add) (
  97. void *transport_context,
  98. const struct totem_ip_address *member);
  99. int (*member_remove) (
  100. void *transport_context,
  101. const struct totem_ip_address *member);
  102. };
  103. struct transport transport_entries[] = {
  104. {
  105. .name = "UDP/IP Multicast",
  106. .initialize = totemudp_initialize,
  107. .buffer_alloc = totemudp_buffer_alloc,
  108. .buffer_release = totemudp_buffer_release,
  109. .processor_count_set = totemudp_processor_count_set,
  110. .token_send = totemudp_token_send,
  111. .mcast_flush_send = totemudp_mcast_flush_send,
  112. .mcast_noflush_send = totemudp_mcast_noflush_send,
  113. .recv_flush = totemudp_recv_flush,
  114. .send_flush = totemudp_send_flush,
  115. .iface_check = totemudp_iface_check,
  116. .finalize = totemudp_finalize,
  117. .net_mtu_adjust = totemudp_net_mtu_adjust,
  118. .iface_print = totemudp_iface_print,
  119. .iface_get = totemudp_iface_get,
  120. .token_target_set = totemudp_token_target_set,
  121. .crypto_set = totemudp_crypto_set,
  122. .recv_mcast_empty = totemudp_recv_mcast_empty
  123. },
  124. {
  125. .name = "UDP/IP Unicast",
  126. .initialize = totemudpu_initialize,
  127. .buffer_alloc = totemudpu_buffer_alloc,
  128. .buffer_release = totemudpu_buffer_release,
  129. .processor_count_set = totemudpu_processor_count_set,
  130. .token_send = totemudpu_token_send,
  131. .mcast_flush_send = totemudpu_mcast_flush_send,
  132. .mcast_noflush_send = totemudpu_mcast_noflush_send,
  133. .recv_flush = totemudpu_recv_flush,
  134. .send_flush = totemudpu_send_flush,
  135. .iface_check = totemudpu_iface_check,
  136. .finalize = totemudpu_finalize,
  137. .net_mtu_adjust = totemudpu_net_mtu_adjust,
  138. .iface_print = totemudpu_iface_print,
  139. .iface_get = totemudpu_iface_get,
  140. .token_target_set = totemudpu_token_target_set,
  141. .crypto_set = totemudpu_crypto_set,
  142. .recv_mcast_empty = totemudpu_recv_mcast_empty,
  143. .member_add = totemudpu_member_add,
  144. .member_remove = totemudpu_member_remove
  145. },
  146. #ifdef HAVE_RDMA
  147. {
  148. .name = "Infiniband/IP",
  149. .initialize = totemiba_initialize,
  150. .buffer_alloc = totemiba_buffer_alloc,
  151. .buffer_release = totemiba_buffer_release,
  152. .processor_count_set = totemiba_processor_count_set,
  153. .token_send = totemiba_token_send,
  154. .mcast_flush_send = totemiba_mcast_flush_send,
  155. .mcast_noflush_send = totemiba_mcast_noflush_send,
  156. .recv_flush = totemiba_recv_flush,
  157. .send_flush = totemiba_send_flush,
  158. .iface_check = totemiba_iface_check,
  159. .finalize = totemiba_finalize,
  160. .net_mtu_adjust = totemiba_net_mtu_adjust,
  161. .iface_print = totemiba_iface_print,
  162. .iface_get = totemiba_iface_get,
  163. .token_target_set = totemiba_token_target_set,
  164. .crypto_set = totemiba_crypto_set,
  165. .recv_mcast_empty = totemiba_recv_mcast_empty
  166. }
  167. #endif
  168. };
  169. struct totemnet_instance {
  170. void *transport_context;
  171. struct transport *transport;
  172. void (*totemnet_log_printf) (
  173. int level,
  174. int subsys,
  175. const char *function,
  176. const char *file,
  177. int line,
  178. const char *format,
  179. ...)__attribute__((format(printf, 6, 7)));
  180. int totemnet_subsys_id;
  181. };
  182. #define log_printf(level, format, args...) \
  183. do { \
  184. instance->totemnet_log_printf ( \
  185. level, \
  186. instance->totemnet_subsys_id, \
  187. __FUNCTION__, __FILE__, __LINE__, \
  188. (const char *)format, ##args); \
  189. } while (0);
  190. static void totemnet_instance_initialize (
  191. struct totemnet_instance *instance,
  192. struct totem_config *config)
  193. {
  194. int transport;
  195. instance->totemnet_log_printf = config->totem_logging_configuration.log_printf;
  196. instance->totemnet_subsys_id = config->totem_logging_configuration.log_subsys_id;
  197. transport = config->transport_number;
  198. log_printf (LOGSYS_LEVEL_NOTICE,
  199. "Initializing transport (%s).", transport_entries[transport].name);
  200. instance->transport = &transport_entries[transport];
  201. }
  202. int totemnet_crypto_set (
  203. void *net_context,
  204. unsigned int type)
  205. {
  206. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  207. int res = 0;
  208. res = instance->transport->crypto_set (instance->transport_context, type);
  209. return res;
  210. }
  211. int totemnet_finalize (
  212. void *net_context)
  213. {
  214. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  215. int res = 0;
  216. res = instance->transport->finalize (instance->transport_context);
  217. return (res);
  218. }
  219. int totemnet_initialize (
  220. qb_loop_t *loop_pt,
  221. void **net_context,
  222. struct totem_config *totem_config,
  223. int interface_no,
  224. void *context,
  225. void (*deliver_fn) (
  226. void *context,
  227. const void *msg,
  228. unsigned int msg_len),
  229. void (*iface_change_fn) (
  230. void *context,
  231. const struct totem_ip_address *iface_address),
  232. void (*target_set_completed) (
  233. void *context))
  234. {
  235. struct totemnet_instance *instance;
  236. unsigned int res;
  237. instance = malloc (sizeof (struct totemnet_instance));
  238. if (instance == NULL) {
  239. return (-1);
  240. }
  241. totemnet_instance_initialize (instance, totem_config);
  242. res = instance->transport->initialize (loop_pt,
  243. &instance->transport_context, totem_config,
  244. interface_no, context, deliver_fn, iface_change_fn, target_set_completed);
  245. if (res == -1) {
  246. goto error_destroy;
  247. }
  248. *net_context = instance;
  249. return (0);
  250. error_destroy:
  251. free (instance);
  252. return (-1);
  253. }
  254. void *totemnet_buffer_alloc (void *net_context)
  255. {
  256. struct totemnet_instance *instance = net_context;
  257. assert (instance != NULL);
  258. assert (instance->transport != NULL);
  259. return instance->transport->buffer_alloc();
  260. }
  261. void totemnet_buffer_release (void *net_context, void *ptr)
  262. {
  263. struct totemnet_instance *instance = net_context;
  264. assert (instance != NULL);
  265. assert (instance->transport != NULL);
  266. instance->transport->buffer_release (ptr);
  267. }
  268. int totemnet_processor_count_set (
  269. void *net_context,
  270. int processor_count)
  271. {
  272. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  273. int res = 0;
  274. res = instance->transport->processor_count_set (instance->transport_context, processor_count);
  275. return (res);
  276. }
  277. int totemnet_recv_flush (void *net_context)
  278. {
  279. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  280. int res = 0;
  281. res = instance->transport->recv_flush (instance->transport_context);
  282. return (res);
  283. }
  284. int totemnet_send_flush (void *net_context)
  285. {
  286. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  287. int res = 0;
  288. res = instance->transport->send_flush (instance->transport_context);
  289. return (res);
  290. }
  291. int totemnet_token_send (
  292. void *net_context,
  293. const void *msg,
  294. unsigned int msg_len)
  295. {
  296. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  297. int res = 0;
  298. res = instance->transport->token_send (instance->transport_context, msg, msg_len);
  299. return (res);
  300. }
  301. int totemnet_mcast_flush_send (
  302. void *net_context,
  303. const void *msg,
  304. unsigned int msg_len)
  305. {
  306. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  307. int res = 0;
  308. res = instance->transport->mcast_flush_send (instance->transport_context, msg, msg_len);
  309. return (res);
  310. }
  311. int totemnet_mcast_noflush_send (
  312. void *net_context,
  313. const void *msg,
  314. unsigned int msg_len)
  315. {
  316. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  317. int res = 0;
  318. res = instance->transport->mcast_noflush_send (instance->transport_context, msg, msg_len);
  319. return (res);
  320. }
  321. extern int totemnet_iface_check (void *net_context)
  322. {
  323. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  324. int res = 0;
  325. res = instance->transport->iface_check (instance->transport_context);
  326. return (res);
  327. }
  328. extern int totemnet_net_mtu_adjust (void *net_context, struct totem_config *totem_config)
  329. {
  330. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  331. int res = 0;
  332. instance->transport->net_mtu_adjust (instance->transport_context, totem_config);
  333. return (res);
  334. }
  335. const char *totemnet_iface_print (void *net_context) {
  336. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  337. const char *ret_char;
  338. ret_char = instance->transport->iface_print (instance->transport_context);
  339. return (ret_char);
  340. }
  341. int totemnet_iface_get (
  342. void *net_context,
  343. struct totem_ip_address *addr)
  344. {
  345. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  346. unsigned int res;
  347. res = instance->transport->iface_get (instance->transport_context, addr);
  348. return (res);
  349. }
  350. int totemnet_token_target_set (
  351. void *net_context,
  352. const struct totem_ip_address *token_target)
  353. {
  354. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  355. unsigned int res;
  356. res = instance->transport->token_target_set (instance->transport_context, token_target);
  357. return (res);
  358. }
  359. extern int totemnet_recv_mcast_empty (
  360. void *net_context)
  361. {
  362. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  363. unsigned int res;
  364. res = instance->transport->recv_mcast_empty (instance->transport_context);
  365. return (res);
  366. }
  367. extern int totemnet_member_add (
  368. void *net_context,
  369. const struct totem_ip_address *member)
  370. {
  371. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  372. unsigned int res = 0;
  373. if (instance->transport->member_add) {
  374. res = instance->transport->member_add (
  375. instance->transport_context,
  376. member);
  377. }
  378. return (res);
  379. }
  380. extern int totemnet_member_remove (
  381. void *net_context,
  382. const struct totem_ip_address *member)
  383. {
  384. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  385. unsigned int res = 0;
  386. if (instance->transport->member_remove) {
  387. res = instance->transport->member_remove (
  388. instance->transport_context,
  389. member);
  390. }
  391. return (res);
  392. }