totemnet.c 12 KB

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