totemnet.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. #ifdef HAVE_RDMA
  37. #include <totemiba.h>
  38. #endif
  39. #include <totemudp.h>
  40. #include <totemudpu.h>
  41. #include <totemknet.h>
  42. #include <totemnet.h>
  43. #include <qb/qbloop.h>
  44. #define LOGSYS_UTILS_ONLY 1
  45. #include <corosync/logsys.h>
  46. struct transport {
  47. const char *name;
  48. int (*initialize) (
  49. qb_loop_t *loop_pt,
  50. void **transport_instance,
  51. struct totem_config *totem_config,
  52. totemsrp_stats_t *stats,
  53. void *context,
  54. void (*deliver_fn) (
  55. void *context,
  56. const void *msg,
  57. unsigned int msg_len),
  58. void (*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 (*token_target_set) (
  95. void *transport_context,
  96. const struct totem_ip_address *token_target);
  97. int (*crypto_set) (
  98. void *transport_context,
  99. const char *cipher_type,
  100. const char *hash_type);
  101. int (*recv_mcast_empty) (
  102. void *transport_context);
  103. int (*iface_set) (
  104. void *transport_context,
  105. const struct totem_ip_address *local,
  106. unsigned short ip_port,
  107. unsigned int ring_no);
  108. int (*member_add) (
  109. void *transport_context,
  110. const struct totem_ip_address *local,
  111. const struct totem_ip_address *member,
  112. int ring_no);
  113. int (*member_remove) (
  114. void *transport_context,
  115. const struct totem_ip_address *member,
  116. int ring_no);
  117. int (*member_set_active) (
  118. void *transport_context,
  119. const struct totem_ip_address *member,
  120. int active);
  121. int (*reconfigure) (
  122. void *net_context,
  123. struct totem_config *totem_config);
  124. void (*stats_clear) (
  125. void *net_context);
  126. };
  127. struct transport transport_entries[] = {
  128. {
  129. .name = "UDP/IP Multicast",
  130. .initialize = totemudp_initialize,
  131. .buffer_alloc = totemudp_buffer_alloc,
  132. .buffer_release = totemudp_buffer_release,
  133. .processor_count_set = totemudp_processor_count_set,
  134. .token_send = totemudp_token_send,
  135. .mcast_flush_send = totemudp_mcast_flush_send,
  136. .mcast_noflush_send = totemudp_mcast_noflush_send,
  137. .recv_flush = totemudp_recv_flush,
  138. .send_flush = totemudp_send_flush,
  139. .iface_set = totemudp_iface_set,
  140. .iface_check = totemudp_iface_check,
  141. .finalize = totemudp_finalize,
  142. .net_mtu_adjust = totemudp_net_mtu_adjust,
  143. .ifaces_get = totemudp_ifaces_get,
  144. .token_target_set = totemudp_token_target_set,
  145. .crypto_set = totemudp_crypto_set,
  146. .recv_mcast_empty = totemudp_recv_mcast_empty,
  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. void (*iface_change_fn) (
  260. void *context,
  261. const struct totem_ip_address *iface_address,
  262. unsigned int ring_no),
  263. void (*mtu_changed) (
  264. void *context,
  265. int net_mtu),
  266. void (*target_set_completed) (
  267. void *context))
  268. {
  269. struct totemnet_instance *instance;
  270. unsigned int res;
  271. instance = malloc (sizeof (struct totemnet_instance));
  272. if (instance == NULL) {
  273. return (-1);
  274. }
  275. totemnet_instance_initialize (instance, totem_config);
  276. res = instance->transport->initialize (loop_pt,
  277. &instance->transport_context, totem_config, stats,
  278. context, deliver_fn, iface_change_fn, mtu_changed, target_set_completed);
  279. if (res == -1) {
  280. goto error_destroy;
  281. }
  282. *net_context = instance;
  283. return (0);
  284. error_destroy:
  285. free (instance);
  286. return (-1);
  287. }
  288. void *totemnet_buffer_alloc (void *net_context)
  289. {
  290. struct totemnet_instance *instance = net_context;
  291. assert (instance != NULL);
  292. assert (instance->transport != NULL);
  293. return instance->transport->buffer_alloc();
  294. }
  295. void totemnet_buffer_release (void *net_context, void *ptr)
  296. {
  297. struct totemnet_instance *instance = net_context;
  298. assert (instance != NULL);
  299. assert (instance->transport != NULL);
  300. instance->transport->buffer_release (ptr);
  301. }
  302. int totemnet_processor_count_set (
  303. void *net_context,
  304. int processor_count)
  305. {
  306. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  307. int res = 0;
  308. res = instance->transport->processor_count_set (instance->transport_context, processor_count);
  309. return (res);
  310. }
  311. int totemnet_recv_flush (void *net_context)
  312. {
  313. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  314. int res = 0;
  315. res = instance->transport->recv_flush (instance->transport_context);
  316. return (res);
  317. }
  318. int totemnet_send_flush (void *net_context)
  319. {
  320. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  321. int res = 0;
  322. res = instance->transport->send_flush (instance->transport_context);
  323. return (res);
  324. }
  325. int totemnet_token_send (
  326. void *net_context,
  327. const void *msg,
  328. unsigned int msg_len)
  329. {
  330. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  331. int res = 0;
  332. res = instance->transport->token_send (instance->transport_context, msg, msg_len);
  333. return (res);
  334. }
  335. int totemnet_mcast_flush_send (
  336. void *net_context,
  337. const void *msg,
  338. unsigned int msg_len)
  339. {
  340. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  341. int res = 0;
  342. res = instance->transport->mcast_flush_send (instance->transport_context, msg, msg_len);
  343. return (res);
  344. }
  345. int totemnet_mcast_noflush_send (
  346. void *net_context,
  347. const void *msg,
  348. unsigned int msg_len)
  349. {
  350. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  351. int res = 0;
  352. res = instance->transport->mcast_noflush_send (instance->transport_context, msg, msg_len);
  353. return (res);
  354. }
  355. extern int totemnet_iface_check (void *net_context)
  356. {
  357. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  358. int res = 0;
  359. res = instance->transport->iface_check (instance->transport_context);
  360. return (res);
  361. }
  362. extern int totemnet_net_mtu_adjust (void *net_context, struct totem_config *totem_config)
  363. {
  364. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  365. int res = 0;
  366. instance->transport->net_mtu_adjust (instance->transport_context, totem_config);
  367. return (res);
  368. }
  369. int totemnet_iface_set (void *net_context,
  370. const struct totem_ip_address *interface_addr,
  371. unsigned short ip_port,
  372. unsigned int iface_no)
  373. {
  374. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  375. int res;
  376. res = instance->transport->iface_set (instance->transport_context, interface_addr, ip_port, iface_no);
  377. return (res);
  378. }
  379. int totemnet_ifaces_get (
  380. void *net_context,
  381. char ***status,
  382. unsigned int *iface_count)
  383. {
  384. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  385. unsigned int res;
  386. res = instance->transport->ifaces_get (instance->transport_context, status, iface_count);
  387. return (res);
  388. }
  389. int totemnet_token_target_set (
  390. void *net_context,
  391. const struct totem_ip_address *token_target)
  392. {
  393. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  394. unsigned int res;
  395. res = instance->transport->token_target_set (instance->transport_context, token_target);
  396. return (res);
  397. }
  398. extern int totemnet_recv_mcast_empty (
  399. void *net_context)
  400. {
  401. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  402. unsigned int res;
  403. res = instance->transport->recv_mcast_empty (instance->transport_context);
  404. return (res);
  405. }
  406. extern int totemnet_member_add (
  407. void *net_context,
  408. const struct totem_ip_address *local,
  409. const struct totem_ip_address *member,
  410. int ring_no)
  411. {
  412. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  413. unsigned int res = 0;
  414. if (instance->transport->member_add) {
  415. res = instance->transport->member_add (
  416. instance->transport_context,
  417. local,
  418. member,
  419. ring_no);
  420. }
  421. return (res);
  422. }
  423. extern int totemnet_member_remove (
  424. void *net_context,
  425. const struct totem_ip_address *member,
  426. int ring_no)
  427. {
  428. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  429. unsigned int res = 0;
  430. if (instance->transport->member_remove) {
  431. res = instance->transport->member_remove (
  432. instance->transport_context,
  433. member,
  434. ring_no);
  435. }
  436. return (res);
  437. }
  438. int totemnet_member_set_active (
  439. void *net_context,
  440. const struct totem_ip_address *member,
  441. int active)
  442. {
  443. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  444. unsigned int res = 0;
  445. if (instance->transport->member_set_active) {
  446. res = instance->transport->member_set_active (
  447. instance->transport_context,
  448. member,
  449. active);
  450. }
  451. return (res);
  452. }
  453. int totemnet_reconfigure (
  454. void *net_context,
  455. struct totem_config *totem_config)
  456. {
  457. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  458. unsigned int res = 0;
  459. res = instance->transport->reconfigure (
  460. instance->transport_context,
  461. totem_config);
  462. return (res);
  463. }
  464. void totemnet_stats_clear (
  465. void *net_context)
  466. {
  467. struct totemnet_instance *instance = (struct totemnet_instance *)net_context;
  468. if (instance->transport->stats_clear) {
  469. instance->transport->stats_clear (
  470. instance->transport_context);
  471. }
  472. }