totemnet.c 16 KB

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