totemnet.c 12 KB

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