totemnet.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2018 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 ENABLE_UDPU
  37. #include <totemudp.h>
  38. #include <totemudpu.h>
  39. #endif
  40. #include <totemknet.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. void *context,
  53. int (*deliver_fn) (
  54. void *context,
  55. const void *msg,
  56. unsigned int msg_len,
  57. const struct sockaddr_storage *system_from),
  58. int (*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 (*nodestatus_get) (
  95. void *transport_context,
  96. unsigned int nodeid,
  97. struct totem_node_status *node_status);
  98. int (*token_target_set) (
  99. void *transport_context,
  100. unsigned int nodeid);
  101. int (*crypto_set) (
  102. void *transport_context,
  103. const char *cipher_type,
  104. const char *hash_type);
  105. int (*recv_mcast_empty) (
  106. void *transport_context);
  107. int (*iface_set) (
  108. void *transport_context,
  109. const struct totem_ip_address *local,
  110. unsigned short ip_port,
  111. unsigned int ring_no);
  112. int (*member_add) (
  113. void *transport_context,
  114. const struct totem_ip_address *local,
  115. const struct totem_ip_address *member,
  116. int ring_no);
  117. int (*member_remove) (
  118. void *transport_context,
  119. const struct totem_ip_address *member,
  120. int ring_no);
  121. int (*member_set_active) (
  122. void *transport_context,
  123. const struct totem_ip_address *member,
  124. int active);
  125. int (*reconfigure) (
  126. void *net_context,
  127. struct totem_config *totem_config);
  128. int (*crypto_reconfigure_phase) (
  129. void *net_context,
  130. struct totem_config *totem_config,
  131. cfg_message_crypto_reconfig_phase_t phase);
  132. void (*stats_clear) (
  133. void *net_context);
  134. };
  135. struct transport transport_entries[] = {
  136. #ifdef ENABLE_UDPU
  137. {
  138. .name = "UDP/IP Multicast",
  139. .initialize = totemudp_initialize,
  140. .buffer_alloc = totemudp_buffer_alloc,
  141. .buffer_release = totemudp_buffer_release,
  142. .processor_count_set = totemudp_processor_count_set,
  143. .token_send = totemudp_token_send,
  144. .mcast_flush_send = totemudp_mcast_flush_send,
  145. .mcast_noflush_send = totemudp_mcast_noflush_send,
  146. .recv_flush = totemudp_recv_flush,
  147. .send_flush = totemudp_send_flush,
  148. .iface_set = totemudp_iface_set,
  149. .iface_check = totemudp_iface_check,
  150. .finalize = totemudp_finalize,
  151. .net_mtu_adjust = totemudp_net_mtu_adjust,
  152. .ifaces_get = totemudp_ifaces_get,
  153. .nodestatus_get = totemudp_nodestatus_get,
  154. .token_target_set = totemudp_token_target_set,
  155. .crypto_set = totemudp_crypto_set,
  156. .recv_mcast_empty = totemudp_recv_mcast_empty,
  157. .member_add = totemudp_member_add,
  158. .member_remove = totemudp_member_remove,
  159. .reconfigure = totemudp_reconfigure,
  160. .crypto_reconfigure_phase = NULL
  161. },
  162. {
  163. .name = "UDP/IP Unicast",
  164. .initialize = totemudpu_initialize,
  165. .buffer_alloc = totemudpu_buffer_alloc,
  166. .buffer_release = totemudpu_buffer_release,
  167. .processor_count_set = totemudpu_processor_count_set,
  168. .token_send = totemudpu_token_send,
  169. .mcast_flush_send = totemudpu_mcast_flush_send,
  170. .mcast_noflush_send = totemudpu_mcast_noflush_send,
  171. .recv_flush = totemudpu_recv_flush,
  172. .send_flush = totemudpu_send_flush,
  173. .iface_set = totemudpu_iface_set,
  174. .iface_check = totemudpu_iface_check,
  175. .finalize = totemudpu_finalize,
  176. .net_mtu_adjust = totemudpu_net_mtu_adjust,
  177. .ifaces_get = totemudpu_ifaces_get,
  178. .nodestatus_get = totemudpu_nodestatus_get,
  179. .token_target_set = totemudpu_token_target_set,
  180. .crypto_set = totemudpu_crypto_set,
  181. .recv_mcast_empty = totemudpu_recv_mcast_empty,
  182. .member_add = totemudpu_member_add,
  183. .member_remove = totemudpu_member_remove,
  184. .reconfigure = totemudpu_reconfigure,
  185. .crypto_reconfigure_phase = NULL
  186. },
  187. #else
  188. {
  189. .name = "UDP/IP Multicast (disabled - expect crash)",
  190. },
  191. {
  192. .name = "UDP/IP Unicast (disabled - expect crash)",
  193. },
  194. #endif
  195. {
  196. .name = "Kronosnet",
  197. .initialize = totemknet_initialize,
  198. .buffer_alloc = totemknet_buffer_alloc,
  199. .buffer_release = totemknet_buffer_release,
  200. .processor_count_set = totemknet_processor_count_set,
  201. .token_send = totemknet_token_send,
  202. .mcast_flush_send = totemknet_mcast_flush_send,
  203. .mcast_noflush_send = totemknet_mcast_noflush_send,
  204. .recv_flush = totemknet_recv_flush,
  205. .send_flush = totemknet_send_flush,
  206. .iface_set = totemknet_iface_set,
  207. .iface_check = totemknet_iface_check,
  208. .finalize = totemknet_finalize,
  209. .net_mtu_adjust = totemknet_net_mtu_adjust,
  210. .ifaces_get = totemknet_ifaces_get,
  211. .nodestatus_get = totemknet_nodestatus_get,
  212. .token_target_set = totemknet_token_target_set,
  213. .crypto_set = totemknet_crypto_set,
  214. .recv_mcast_empty = totemknet_recv_mcast_empty,
  215. .member_add = totemknet_member_add,
  216. .member_remove = totemknet_member_remove,
  217. .reconfigure = totemknet_reconfigure,
  218. .crypto_reconfigure_phase = totemknet_crypto_reconfigure_phase,
  219. .stats_clear = totemknet_stats_clear
  220. }
  221. };
  222. struct totemnet_instance {
  223. void *transport_context;
  224. struct transport *transport;
  225. void (*totemnet_log_printf) (
  226. int level,
  227. int subsys,
  228. const char *function,
  229. const char *file,
  230. int line,
  231. const char *format,
  232. ...)__attribute__((format(printf, 6, 7)));
  233. int totemnet_subsys_id;
  234. };
  235. #define log_printf(level, format, args...) \
  236. do { \
  237. instance->totemnet_log_printf ( \
  238. level, \
  239. instance->totemnet_subsys_id, \
  240. __FUNCTION__, __FILE__, __LINE__, \
  241. (const char *)format, ##args); \
  242. } while (0);
  243. static void totemnet_instance_initialize (
  244. struct totemnet_instance *instance,
  245. struct totem_config *config)
  246. {
  247. int transport;
  248. instance->totemnet_log_printf = config->totem_logging_configuration.log_printf;
  249. instance->totemnet_subsys_id = config->totem_logging_configuration.log_subsys_id;
  250. transport = config->transport_number;
  251. log_printf (LOGSYS_LEVEL_NOTICE,
  252. "Initializing transport (%s).", transport_entries[transport].name);
  253. instance->transport = &transport_entries[transport];
  254. }
  255. int totemnet_crypto_set (
  256. void *net_context,
  257. const char *cipher_type,
  258. const char *hash_type)
  259. {
  260. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  261. int res = 0;
  262. res = instance->transport->crypto_set (instance->transport_context,
  263. cipher_type, hash_type);
  264. return res;
  265. }
  266. int totemnet_finalize (
  267. void *net_context)
  268. {
  269. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  270. int res = 0;
  271. res = instance->transport->finalize (instance->transport_context);
  272. return (res);
  273. }
  274. int totemnet_initialize (
  275. qb_loop_t *loop_pt,
  276. void **net_context,
  277. struct totem_config *totem_config,
  278. totemsrp_stats_t *stats,
  279. void *context,
  280. int (*deliver_fn) (
  281. void *context,
  282. const void *msg,
  283. unsigned int msg_len,
  284. const struct sockaddr_storage *system_from),
  285. int (*iface_change_fn) (
  286. void *context,
  287. const struct totem_ip_address *iface_address,
  288. unsigned int ring_no),
  289. void (*mtu_changed) (
  290. void *context,
  291. int net_mtu),
  292. void (*target_set_completed) (
  293. void *context))
  294. {
  295. struct totemnet_instance *instance;
  296. unsigned int res;
  297. instance = malloc (sizeof (struct totemnet_instance));
  298. if (instance == NULL) {
  299. return (-1);
  300. }
  301. totemnet_instance_initialize (instance, totem_config);
  302. res = instance->transport->initialize (loop_pt,
  303. &instance->transport_context, totem_config, stats,
  304. context, deliver_fn, iface_change_fn, mtu_changed, target_set_completed);
  305. if (res == -1) {
  306. goto error_destroy;
  307. }
  308. *net_context = instance;
  309. return (0);
  310. error_destroy:
  311. free (instance);
  312. return (-1);
  313. }
  314. void *totemnet_buffer_alloc (void *net_context)
  315. {
  316. struct totemnet_instance *instance = net_context;
  317. assert (instance != NULL);
  318. assert (instance->transport != NULL);
  319. return instance->transport->buffer_alloc();
  320. }
  321. void totemnet_buffer_release (void *net_context, void *ptr)
  322. {
  323. struct totemnet_instance *instance = net_context;
  324. assert (instance != NULL);
  325. assert (instance->transport != NULL);
  326. instance->transport->buffer_release (ptr);
  327. }
  328. int totemnet_processor_count_set (
  329. void *net_context,
  330. int processor_count)
  331. {
  332. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  333. int res = 0;
  334. res = instance->transport->processor_count_set (instance->transport_context, processor_count);
  335. return (res);
  336. }
  337. int totemnet_recv_flush (void *net_context)
  338. {
  339. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  340. int res = 0;
  341. res = instance->transport->recv_flush (instance->transport_context);
  342. return (res);
  343. }
  344. int totemnet_send_flush (void *net_context)
  345. {
  346. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  347. int res = 0;
  348. res = instance->transport->send_flush (instance->transport_context);
  349. return (res);
  350. }
  351. int totemnet_token_send (
  352. void *net_context,
  353. const void *msg,
  354. unsigned int msg_len)
  355. {
  356. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  357. int res = 0;
  358. res = instance->transport->token_send (instance->transport_context, msg, msg_len);
  359. return (res);
  360. }
  361. int totemnet_mcast_flush_send (
  362. void *net_context,
  363. const void *msg,
  364. unsigned int msg_len)
  365. {
  366. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  367. int res = 0;
  368. res = instance->transport->mcast_flush_send (instance->transport_context, msg, msg_len);
  369. return (res);
  370. }
  371. int totemnet_mcast_noflush_send (
  372. void *net_context,
  373. const void *msg,
  374. unsigned int msg_len)
  375. {
  376. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  377. int res = 0;
  378. res = instance->transport->mcast_noflush_send (instance->transport_context, msg, msg_len);
  379. return (res);
  380. }
  381. extern int totemnet_iface_check (void *net_context)
  382. {
  383. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  384. int res = 0;
  385. res = instance->transport->iface_check (instance->transport_context);
  386. return (res);
  387. }
  388. extern int totemnet_net_mtu_adjust (void *net_context, struct totem_config *totem_config)
  389. {
  390. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  391. int res = 0;
  392. instance->transport->net_mtu_adjust (instance->transport_context, totem_config);
  393. return (res);
  394. }
  395. int totemnet_iface_set (void *net_context,
  396. const struct totem_ip_address *interface_addr,
  397. unsigned short ip_port,
  398. unsigned int iface_no)
  399. {
  400. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  401. int res;
  402. res = instance->transport->iface_set (instance->transport_context, interface_addr, ip_port, iface_no);
  403. return (res);
  404. }
  405. extern int totemnet_nodestatus_get (
  406. void *net_context,
  407. unsigned int nodeid,
  408. struct totem_node_status *node_status)
  409. {
  410. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  411. unsigned int res;
  412. res = instance->transport->nodestatus_get (instance->transport_context, nodeid, node_status);
  413. return (res);
  414. }
  415. int totemnet_ifaces_get (
  416. void *net_context,
  417. char ***status,
  418. unsigned int *iface_count)
  419. {
  420. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  421. unsigned int res;
  422. res = instance->transport->ifaces_get (instance->transport_context, status, iface_count);
  423. return (res);
  424. }
  425. int totemnet_token_target_set (
  426. void *net_context,
  427. unsigned int nodeid)
  428. {
  429. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  430. unsigned int res;
  431. res = instance->transport->token_target_set (instance->transport_context, nodeid);
  432. return (res);
  433. }
  434. extern int totemnet_recv_mcast_empty (
  435. void *net_context)
  436. {
  437. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  438. unsigned int res;
  439. res = instance->transport->recv_mcast_empty (instance->transport_context);
  440. return (res);
  441. }
  442. extern int totemnet_member_add (
  443. void *net_context,
  444. const struct totem_ip_address *local,
  445. const struct totem_ip_address *member,
  446. int ring_no)
  447. {
  448. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  449. unsigned int res = 0;
  450. if (instance->transport->member_add) {
  451. res = instance->transport->member_add (
  452. instance->transport_context,
  453. local,
  454. member,
  455. ring_no);
  456. }
  457. return (res);
  458. }
  459. extern int totemnet_member_remove (
  460. void *net_context,
  461. const struct totem_ip_address *member,
  462. int ring_no)
  463. {
  464. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  465. unsigned int res = 0;
  466. if (instance->transport->member_remove) {
  467. res = instance->transport->member_remove (
  468. instance->transport_context,
  469. member,
  470. ring_no);
  471. }
  472. return (res);
  473. }
  474. int totemnet_member_set_active (
  475. void *net_context,
  476. const struct totem_ip_address *member,
  477. int active)
  478. {
  479. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  480. unsigned int res = 0;
  481. if (instance->transport->member_set_active) {
  482. res = instance->transport->member_set_active (
  483. instance->transport_context,
  484. member,
  485. active);
  486. }
  487. return (res);
  488. }
  489. int totemnet_reconfigure (
  490. void *net_context,
  491. struct totem_config *totem_config)
  492. {
  493. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  494. unsigned int res = 0;
  495. res = instance->transport->reconfigure (
  496. instance->transport_context,
  497. totem_config);
  498. return (res);
  499. }
  500. int totemnet_crypto_reconfigure_phase (
  501. void *net_context,
  502. struct totem_config *totem_config,
  503. cfg_message_crypto_reconfig_phase_t phase)
  504. {
  505. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  506. unsigned int res = 0;
  507. if (instance->transport->crypto_reconfigure_phase) {
  508. res = instance->transport->crypto_reconfigure_phase (
  509. instance->transport_context,
  510. totem_config, phase);
  511. }
  512. return (res);
  513. }
  514. void totemnet_stats_clear (
  515. void *net_context)
  516. {
  517. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  518. if (instance->transport->stats_clear) {
  519. instance->transport->stats_clear (
  520. instance->transport_context);
  521. }
  522. }