4
0

totemnet.c 16 KB

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