totemnet.c 15 KB

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