totemiba.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <pthread.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.h>
  56. #include <corosync/sq.h>
  57. #include <corosync/list.h>
  58. #include <corosync/hdb.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/totem/coropoll.h>
  61. #define LOGSYS_UTILS_ONLY 1
  62. #include <corosync/engine/logsys.h>
  63. #include "totemiba.h"
  64. #include "wthread.h"
  65. #include "crypto.h"
  66. struct totemiba_instance {
  67. int dummy;
  68. };
  69. DECLARE_HDB_DATABASE (totemiba_instance_database,NULL);
  70. static void totemiba_instance_initialize (struct totemiba_instance *instance)
  71. {
  72. memset (instance, 0, sizeof (struct totemiba_instance));
  73. }
  74. #define log_printf(level, format, args...) \
  75. do { \
  76. instance->totemiba_log_printf ( \
  77. LOGSYS_ENCODE_RECID(level, \
  78. instance->totemiba_subsys_id, \
  79. LOGSYS_RECID_LOG), \
  80. __FUNCTION__, __FILE__, __LINE__, \
  81. (const char *)format, ##args); \
  82. } while (0);
  83. int totemiba_crypto_set (hdb_handle_t handle,
  84. unsigned int type)
  85. {
  86. struct totemiba_instance *instance;
  87. int res = 0;
  88. res = hdb_handle_get (&totemiba_instance_database, handle,
  89. (void *)&instance);
  90. if (res != 0) {
  91. res = ENOENT;
  92. goto error_exit;
  93. }
  94. hdb_handle_put (&totemiba_instance_database, handle);
  95. error_exit:
  96. return res;
  97. }
  98. int totemiba_finalize (
  99. hdb_handle_t handle)
  100. {
  101. struct totemiba_instance *instance;
  102. int res = 0;
  103. res = hdb_handle_get (&totemiba_instance_database, handle,
  104. (void *)&instance);
  105. if (res != 0) {
  106. res = ENOENT;
  107. goto error_exit;
  108. }
  109. hdb_handle_put (&totemiba_instance_database, handle);
  110. error_exit:
  111. return (res);
  112. }
  113. /*
  114. * Create an instance
  115. */
  116. int totemiba_initialize (
  117. hdb_handle_t poll_handle,
  118. hdb_handle_t *handle,
  119. struct totem_config *totem_config,
  120. int interface_no,
  121. void *context,
  122. void (*deliver_fn) (
  123. void *context,
  124. const void *msg,
  125. unsigned int msg_len),
  126. void (*iface_change_fn) (
  127. void *context,
  128. const struct totem_ip_address *iface_address))
  129. {
  130. struct totemiba_instance *instance;
  131. unsigned int res;
  132. res = hdb_handle_create (&totemiba_instance_database,
  133. sizeof (struct totemiba_instance), handle);
  134. if (res != 0) {
  135. goto error_exit;
  136. }
  137. res = hdb_handle_get (&totemiba_instance_database, *handle,
  138. (void *)&instance);
  139. if (res != 0) {
  140. goto error_destroy;
  141. }
  142. totemiba_instance_initialize (instance);
  143. error_exit:
  144. hdb_handle_put (&totemiba_instance_database, *handle);
  145. return (0);
  146. error_destroy:
  147. hdb_handle_destroy (&totemiba_instance_database, *handle);
  148. return (-1);
  149. }
  150. int totemiba_processor_count_set (
  151. hdb_handle_t handle,
  152. int processor_count)
  153. {
  154. struct totemiba_instance *instance;
  155. int res = 0;
  156. res = hdb_handle_get (&totemiba_instance_database, handle,
  157. (void *)&instance);
  158. if (res != 0) {
  159. res = ENOENT;
  160. goto error_exit;
  161. }
  162. hdb_handle_put (&totemiba_instance_database, handle);
  163. error_exit:
  164. return (res);
  165. }
  166. int totemiba_recv_flush (hdb_handle_t handle)
  167. {
  168. struct totemiba_instance *instance;
  169. int res = 0;
  170. res = hdb_handle_get (&totemiba_instance_database, handle,
  171. (void *)&instance);
  172. if (res != 0) {
  173. res = ENOENT;
  174. goto error_exit;
  175. }
  176. hdb_handle_put (&totemiba_instance_database, handle);
  177. error_exit:
  178. return (res);
  179. }
  180. int totemiba_send_flush (hdb_handle_t handle)
  181. {
  182. struct totemiba_instance *instance;
  183. int res = 0;
  184. res = hdb_handle_get (&totemiba_instance_database, handle,
  185. (void *)&instance);
  186. if (res != 0) {
  187. res = ENOENT;
  188. goto error_exit;
  189. }
  190. hdb_handle_put (&totemiba_instance_database, handle);
  191. error_exit:
  192. return (res);
  193. }
  194. int totemiba_token_send (
  195. hdb_handle_t handle,
  196. const void *msg,
  197. unsigned int msg_len)
  198. {
  199. struct totemiba_instance *instance;
  200. int res = 0;
  201. res = hdb_handle_get (&totemiba_instance_database, handle,
  202. (void *)&instance);
  203. if (res != 0) {
  204. res = ENOENT;
  205. goto error_exit;
  206. }
  207. hdb_handle_put (&totemiba_instance_database, handle);
  208. error_exit:
  209. return (res);
  210. }
  211. int totemiba_mcast_flush_send (
  212. hdb_handle_t handle,
  213. const void *msg,
  214. unsigned int msg_len)
  215. {
  216. struct totemiba_instance *instance;
  217. int res = 0;
  218. res = hdb_handle_get (&totemiba_instance_database, handle,
  219. (void *)&instance);
  220. if (res != 0) {
  221. res = ENOENT;
  222. goto error_exit;
  223. }
  224. hdb_handle_put (&totemiba_instance_database, handle);
  225. error_exit:
  226. return (res);
  227. }
  228. int totemiba_mcast_noflush_send (
  229. hdb_handle_t handle,
  230. const void *msg,
  231. unsigned int msg_len)
  232. {
  233. struct totemiba_instance *instance;
  234. int res = 0;
  235. res = hdb_handle_get (&totemiba_instance_database, handle,
  236. (void *)&instance);
  237. if (res != 0) {
  238. res = ENOENT;
  239. goto error_exit;
  240. }
  241. hdb_handle_put (&totemiba_instance_database, handle);
  242. error_exit:
  243. return (res);
  244. }
  245. extern int totemiba_iface_check (hdb_handle_t handle)
  246. {
  247. struct totemiba_instance *instance;
  248. int res = 0;
  249. res = hdb_handle_get (&totemiba_instance_database, handle,
  250. (void *)&instance);
  251. if (res != 0) {
  252. res = ENOENT;
  253. goto error_exit;
  254. }
  255. hdb_handle_put (&totemiba_instance_database, handle);
  256. error_exit:
  257. return (res);
  258. }
  259. extern void totemiba_net_mtu_adjust (hdb_handle_t handle, struct totem_config *totem_config)
  260. {
  261. }
  262. const char *totemiba_iface_print (hdb_handle_t handle) {
  263. struct totemiba_instance *instance;
  264. int res = 0;
  265. const char *ret_char;
  266. res = hdb_handle_get (&totemiba_instance_database, handle,
  267. (void *)&instance);
  268. if (res != 0) {
  269. ret_char = "Invalid totemiba handle";
  270. goto error_exit;
  271. }
  272. hdb_handle_put (&totemiba_instance_database, handle);
  273. error_exit:
  274. return (ret_char);
  275. }
  276. int totemiba_iface_get (
  277. hdb_handle_t handle,
  278. struct totem_ip_address *addr)
  279. {
  280. struct totemiba_instance *instance;
  281. unsigned int res;
  282. res = hdb_handle_get (&totemiba_instance_database, handle,
  283. (void *)&instance);
  284. if (res != 0) {
  285. goto error_exit;
  286. }
  287. hdb_handle_put (&totemiba_instance_database, handle);
  288. error_exit:
  289. return (res);
  290. }
  291. int totemiba_token_target_set (
  292. hdb_handle_t handle,
  293. const struct totem_ip_address *token_target)
  294. {
  295. struct totemiba_instance *instance;
  296. unsigned int res;
  297. res = hdb_handle_get (&totemiba_instance_database, handle,
  298. (void *)&instance);
  299. if (res != 0) {
  300. goto error_exit;
  301. }
  302. hdb_handle_put (&totemiba_instance_database, handle);
  303. error_exit:
  304. return (res);
  305. }
  306. extern int totemiba_recv_mcast_empty (
  307. hdb_handle_t handle)
  308. {
  309. struct totemiba_instance *instance;
  310. unsigned int res;
  311. res = hdb_handle_get (&totemiba_instance_database, handle,
  312. (void *)&instance);
  313. if (res != 0) {
  314. goto error_exit;
  315. }
  316. hdb_handle_put (&totemiba_instance_database, handle);
  317. return (0);
  318. error_exit:
  319. return (res);
  320. }