4
0

totemnet.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 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. #ifdef HAVE_RDMA
  36. #include <totemiba.h>
  37. #endif
  38. #include <totemudp.h>
  39. #include <totemnet.h>
  40. #define LOGSYS_UTILS_ONLY 1
  41. #include <corosync/engine/logsys.h>
  42. struct transport {
  43. const char *name;
  44. int (*initialize) (
  45. hdb_handle_t poll_handle,
  46. void **transport_instance,
  47. struct totem_config *totem_config,
  48. int interface_no,
  49. void *context,
  50. void (*deliver_fn) (
  51. void *context,
  52. const void *msg,
  53. unsigned int msg_len),
  54. void (*iface_change_fn) (
  55. void *context,
  56. const struct totem_ip_address *iface_address),
  57. void (*target_set_completed) (
  58. void *context));
  59. int (*processor_count_set) (
  60. void *transport_context,
  61. int processor_count);
  62. int (*token_send) (
  63. void *transport_context,
  64. const void *msg,
  65. unsigned int msg_len);
  66. int (*mcast_flush_send) (
  67. void *transport_context,
  68. const void *msg,
  69. unsigned int msg_len);
  70. int (*mcast_noflush_send) (
  71. void *transport_context,
  72. const void *msg,
  73. unsigned int msg_len);
  74. int (*recv_flush) (void *transport_context);
  75. int (*send_flush) (void *transport_context);
  76. int (*iface_check) (void *transport_context);
  77. int (*finalize) (void *transport_context);
  78. void (*net_mtu_adjust) (void *transport_context, struct totem_config *totem_config);
  79. const char *(*iface_print) (void *transport_context);
  80. int (*iface_get) (
  81. void *transport_context,
  82. struct totem_ip_address *addr);
  83. int (*token_target_set) (
  84. void *transport_context,
  85. const struct totem_ip_address *token_target);
  86. int (*crypto_set) (
  87. void *transport_context,
  88. unsigned int type);
  89. int (*recv_mcast_empty) (
  90. void *transport_context);
  91. };
  92. struct transport transport_entries[] = {
  93. {
  94. .name = "UDP/IP",
  95. .initialize = totemudp_initialize,
  96. .processor_count_set = totemudp_processor_count_set,
  97. .token_send = totemudp_token_send,
  98. .mcast_flush_send = totemudp_mcast_flush_send,
  99. .mcast_noflush_send = totemudp_mcast_noflush_send,
  100. .recv_flush = totemudp_recv_flush,
  101. .send_flush = totemudp_send_flush,
  102. .iface_check = totemudp_iface_check,
  103. .finalize = totemudp_finalize,
  104. .net_mtu_adjust = totemudp_net_mtu_adjust,
  105. .iface_print = totemudp_iface_print,
  106. .iface_get = totemudp_iface_get,
  107. .token_target_set = totemudp_token_target_set,
  108. .crypto_set = totemudp_crypto_set,
  109. .recv_mcast_empty = totemudp_recv_mcast_empty
  110. },
  111. #ifdef HAVE_RDMA
  112. {
  113. .name = "Infiniband/IP",
  114. .initialize = totemiba_initialize,
  115. .processor_count_set = totemiba_processor_count_set,
  116. .token_send = totemiba_token_send,
  117. .mcast_flush_send = totemiba_mcast_flush_send,
  118. .mcast_noflush_send = totemiba_mcast_noflush_send,
  119. .recv_flush = totemiba_recv_flush,
  120. .send_flush = totemiba_send_flush,
  121. .iface_check = totemiba_iface_check,
  122. .finalize = totemiba_finalize,
  123. .net_mtu_adjust = totemiba_net_mtu_adjust,
  124. .iface_print = totemiba_iface_print,
  125. .iface_get = totemiba_iface_get,
  126. .token_target_set = totemiba_token_target_set,
  127. .crypto_set = totemiba_crypto_set,
  128. .recv_mcast_empty = totemiba_recv_mcast_empty
  129. }
  130. #endif
  131. };
  132. struct totemnet_instance {
  133. void *transport_context;
  134. struct transport *transport;
  135. void (*totemnet_log_printf) (
  136. unsigned int rec_ident,
  137. const char *function,
  138. const char *file,
  139. int line,
  140. const char *format,
  141. ...)__attribute__((format(printf, 5, 6)));
  142. int totemnet_subsys_id;
  143. };
  144. #define log_printf(level, format, args...) \
  145. do { \
  146. instance->totemnet_log_printf ( \
  147. LOGSYS_ENCODE_RECID(level, \
  148. instance->totemnet_subsys_id, \
  149. LOGSYS_RECID_LOG), \
  150. __FUNCTION__, __FILE__, __LINE__, \
  151. (const char *)format, ##args); \
  152. } while (0);
  153. static void totemnet_instance_initialize (
  154. struct totemnet_instance *instance,
  155. struct totem_config *config)
  156. {
  157. int transport;
  158. instance->totemnet_log_printf = config->totem_logging_configuration.log_printf;
  159. instance->totemnet_subsys_id = config->totem_logging_configuration.log_subsys_id;
  160. transport = 0;
  161. #ifdef HAVE_RDMA
  162. if (config->transport_number == 1) {
  163. transport = 1;
  164. }
  165. #endif
  166. log_printf (LOGSYS_LEVEL_NOTICE,
  167. "Initializing transport (%s).\n", transport_entries[transport].name);
  168. instance->transport = &transport_entries[transport];
  169. }
  170. int totemnet_crypto_set (
  171. void *net_context,
  172. unsigned int type)
  173. {
  174. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  175. int res = 0;
  176. res = instance->transport->crypto_set (instance->transport_context, type);
  177. return res;
  178. }
  179. int totemnet_finalize (
  180. void *net_context)
  181. {
  182. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  183. int res = 0;
  184. res = instance->transport->finalize (instance->transport_context);
  185. return (res);
  186. }
  187. int totemnet_initialize (
  188. hdb_handle_t poll_handle,
  189. void **net_context,
  190. struct totem_config *totem_config,
  191. int interface_no,
  192. void *context,
  193. void (*deliver_fn) (
  194. void *context,
  195. const void *msg,
  196. unsigned int msg_len),
  197. void (*iface_change_fn) (
  198. void *context,
  199. const struct totem_ip_address *iface_address),
  200. void (*target_set_completed) (
  201. void *context))
  202. {
  203. struct totemnet_instance *instance;
  204. unsigned int res;
  205. instance = malloc (sizeof (struct totemnet_instance));
  206. if (instance == NULL) {
  207. return (-1);
  208. }
  209. totemnet_instance_initialize (instance, totem_config);
  210. res = instance->transport->initialize (poll_handle,
  211. &instance->transport_context, totem_config,
  212. interface_no, context, deliver_fn, iface_change_fn, target_set_completed);
  213. if (res == -1) {
  214. goto error_destroy;
  215. }
  216. *net_context = instance;
  217. return (0);
  218. error_destroy:
  219. free (instance);
  220. return (-1);
  221. }
  222. int totemnet_processor_count_set (
  223. void *net_context,
  224. int processor_count)
  225. {
  226. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  227. int res = 0;
  228. res = instance->transport->processor_count_set (instance->transport_context, processor_count);
  229. return (res);
  230. }
  231. int totemnet_recv_flush (void *net_context)
  232. {
  233. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  234. int res = 0;
  235. res = instance->transport->recv_flush (instance->transport_context);
  236. return (res);
  237. }
  238. int totemnet_send_flush (void *net_context)
  239. {
  240. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  241. int res = 0;
  242. res = instance->transport->send_flush (instance->transport_context);
  243. return (res);
  244. }
  245. int totemnet_token_send (
  246. void *net_context,
  247. const void *msg,
  248. unsigned int msg_len)
  249. {
  250. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  251. int res = 0;
  252. res = instance->transport->token_send (instance->transport_context, msg, msg_len);
  253. return (res);
  254. }
  255. int totemnet_mcast_flush_send (
  256. void *net_context,
  257. const void *msg,
  258. unsigned int msg_len)
  259. {
  260. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  261. int res = 0;
  262. res = instance->transport->mcast_flush_send (instance->transport_context, msg, msg_len);
  263. return (res);
  264. }
  265. int totemnet_mcast_noflush_send (
  266. void *net_context,
  267. const void *msg,
  268. unsigned int msg_len)
  269. {
  270. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  271. int res = 0;
  272. res = instance->transport->mcast_noflush_send (instance->transport_context, msg, msg_len);
  273. return (res);
  274. }
  275. extern int totemnet_iface_check (void *net_context)
  276. {
  277. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  278. int res = 0;
  279. res = instance->transport->iface_check (instance->transport_context);
  280. return (res);
  281. }
  282. extern int totemnet_net_mtu_adjust (void *net_context, struct totem_config *totem_config)
  283. {
  284. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  285. int res = 0;
  286. instance->transport->net_mtu_adjust (instance->transport_context, totem_config);
  287. return (res);
  288. }
  289. const char *totemnet_iface_print (void *net_context) {
  290. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  291. const char *ret_char;
  292. ret_char = instance->transport->iface_print (instance->transport_context);
  293. return (ret_char);
  294. }
  295. int totemnet_iface_get (
  296. void *net_context,
  297. struct totem_ip_address *addr)
  298. {
  299. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  300. unsigned int res;
  301. res = instance->transport->iface_get (instance->transport_context, addr);
  302. return (res);
  303. }
  304. int totemnet_token_target_set (
  305. void *net_context,
  306. const struct totem_ip_address *token_target)
  307. {
  308. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  309. unsigned int res;
  310. res = instance->transport->token_target_set (instance->transport_context, token_target);
  311. return (res);
  312. }
  313. extern int totemnet_recv_mcast_empty (
  314. void *net_context)
  315. {
  316. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  317. unsigned int res;
  318. res = instance->transport->recv_mcast_empty (instance->transport_context);
  319. return (res);
  320. }