totemnet.c 15 KB

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