totemnet.c 13 KB

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