totemnet.c 14 KB

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