flow.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (c) 2006 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  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. /*
  35. * New messages are allowed from the library ONLY when the processor has not
  36. * received a OPENAIS_FLOW_CONTROL_STATE_ENABLED from any processor. If a OPENAIS_FLOW_CONTROL_STATE_ENABLED
  37. * message is sent, it must later be cancelled by a OPENAIS_FLOW_CONTROL_STATE_DISABLED
  38. * message.
  39. */
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include <pthread.h>
  44. #include "flow.h"
  45. #include "totem.h"
  46. #include "totempg.h"
  47. #include "print.h"
  48. #include "hdb.h"
  49. #include "../include/list.h"
  50. #define OPENAIS_FLOW_CONTROL_ENABLED_SERVICES_MAX 128
  51. struct flow_control_instance {
  52. struct list_head list_head;
  53. unsigned int service;
  54. };
  55. DECLARE_LIST_INIT (flow_control_service_list_head);
  56. struct flow_control_message {
  57. unsigned int service __attribute__((aligned(8)));
  58. char id[1024] __attribute__((aligned(8)));
  59. unsigned int id_len __attribute__((aligned(8)));
  60. enum openais_flow_control_state flow_control_state __attribute__((aligned(8)));
  61. };
  62. struct flow_control_node_state {
  63. unsigned int nodeid;
  64. enum openais_flow_control_state flow_control_state;
  65. };
  66. struct flow_control_service {
  67. struct flow_control_node_state flow_control_node_state[PROCESSOR_COUNT_MAX];
  68. unsigned int service;
  69. char id[1024];
  70. unsigned int id_len;
  71. void (*flow_control_state_set_fn) (void *context, enum openais_flow_control_state flow_control_state);
  72. void *context;
  73. unsigned int processor_count;
  74. enum openais_flow_control_state flow_control_state;
  75. struct list_head list;
  76. struct list_head list_all;
  77. };
  78. static struct totempg_group flow_control_group = {
  79. .group = "flowcontrol",
  80. .group_len = 12
  81. };
  82. static totempg_groups_handle flow_control_handle;
  83. static struct hdb_handle_database flow_control_hdb = {
  84. .handle_count = 0,
  85. .handles = NULL,
  86. .iterator = 0,
  87. .mutex = PTHREAD_MUTEX_INITIALIZER
  88. };
  89. static unsigned int flow_control_member_list[PROCESSOR_COUNT_MAX];
  90. static unsigned int flow_control_member_list_entries;
  91. static inline int flow_control_xmit (
  92. struct flow_control_service *flow_control_service,
  93. enum openais_flow_control_state flow_control_state)
  94. {
  95. struct flow_control_message flow_control_message;
  96. struct iovec iovec;
  97. unsigned int res;
  98. flow_control_message.service = flow_control_service->service;
  99. flow_control_message.flow_control_state = flow_control_state;
  100. memcpy (&flow_control_message.id, flow_control_service->id,
  101. flow_control_service->id_len);
  102. flow_control_message.id_len = flow_control_service->id_len;
  103. iovec.iov_base = (char *)&flow_control_message;
  104. iovec.iov_len = sizeof (flow_control_message);
  105. res = totempg_groups_mcast_joined (flow_control_handle, &iovec, 1,
  106. TOTEMPG_AGREED);
  107. flow_control_service->flow_control_state_set_fn (
  108. flow_control_service->context,
  109. flow_control_service->flow_control_state);
  110. return (res);
  111. }
  112. static void flow_control_deliver_fn (
  113. unsigned int nodeid,
  114. struct iovec *iovec,
  115. int iov_len,
  116. int endian_conversion_required)
  117. {
  118. struct flow_control_message *flow_control_message = (struct flow_control_message *)iovec[0].iov_base;
  119. struct flow_control_service *flow_control_service;
  120. struct list_head *list;
  121. unsigned int i;
  122. for (list = flow_control_service_list_head.next;
  123. list != &flow_control_service_list_head;
  124. list = list->next) {
  125. flow_control_service = list_entry (list, struct flow_control_service, list_all);
  126. /*
  127. * Find this nodeid in the flow control service and set the message
  128. * enabled or disabled flag
  129. */
  130. for (i = 0; i < flow_control_service->processor_count; i++) {
  131. if (nodeid == flow_control_service->flow_control_node_state[i].nodeid) {
  132. flow_control_service->flow_control_node_state[i].flow_control_state =
  133. flow_control_message->flow_control_state;
  134. break;
  135. }
  136. }
  137. /*
  138. * Determine if any flow control is enabled on any nodes and set
  139. * the internal variable appropriately
  140. */
  141. flow_control_service->flow_control_state = OPENAIS_FLOW_CONTROL_STATE_DISABLED;
  142. flow_control_service->flow_control_state_set_fn (flow_control_service->context, flow_control_service->flow_control_state);
  143. for (i = 0; i < flow_control_service->processor_count; i++) {
  144. if (flow_control_service->flow_control_node_state[i].flow_control_state == OPENAIS_FLOW_CONTROL_STATE_ENABLED) {
  145. flow_control_service->flow_control_state = OPENAIS_FLOW_CONTROL_STATE_ENABLED;
  146. flow_control_service->flow_control_state_set_fn (flow_control_service->context, flow_control_service->flow_control_state);
  147. }
  148. }
  149. } /* for list iteration */
  150. }
  151. static void flow_control_confchg_fn (
  152. enum totem_configuration_type configuration_type,
  153. unsigned int *member_list, int member_list_entries,
  154. unsigned int *left_list, int left_list_entries,
  155. unsigned int *joined_list, int joined_list_entries,
  156. struct memb_ring_id *ring_id)
  157. {
  158. unsigned int i;
  159. struct flow_control_service *flow_control_service;
  160. struct list_head *list;
  161. memcpy (flow_control_member_list, member_list,
  162. sizeof (unsigned int) * member_list_entries);
  163. flow_control_member_list_entries = member_list_entries;
  164. for (list = flow_control_service_list_head.next;
  165. list != &flow_control_service_list_head;
  166. list = list->next) {
  167. flow_control_service = list_entry (list, struct flow_control_service, list_all);
  168. /*
  169. * Set all of the node ids after a configuration change
  170. * Turn on all flow control after a configuration change
  171. */
  172. flow_control_service->processor_count = flow_control_member_list_entries;
  173. flow_control_service->flow_control_state = OPENAIS_FLOW_CONTROL_STATE_ENABLED;
  174. for (i = 0; i < member_list_entries; i++) {
  175. flow_control_service->flow_control_node_state[i].nodeid = member_list[i];
  176. flow_control_service->flow_control_node_state[i].flow_control_state = OPENAIS_FLOW_CONTROL_STATE_ENABLED;
  177. }
  178. }
  179. }
  180. /*
  181. * External API
  182. */
  183. unsigned int openais_flow_control_initialize (void)
  184. {
  185. unsigned int res;
  186. log_init ("FLOW");
  187. res = totempg_groups_initialize (
  188. &flow_control_handle,
  189. flow_control_deliver_fn,
  190. flow_control_confchg_fn);
  191. if (res == -1) {
  192. log_printf (LOG_LEVEL_ERROR,
  193. "Couldn't initialize flow control interface.\n");
  194. return (-1);
  195. }
  196. res = totempg_groups_join (
  197. flow_control_handle,
  198. &flow_control_group,
  199. 1);
  200. if (res == -1) {
  201. log_printf (LOG_LEVEL_ERROR, "Couldn't join flow control group.\n");
  202. return (-1);
  203. }
  204. return (0);
  205. }
  206. unsigned int openais_flow_control_ipc_init (
  207. unsigned int *flow_control_handle,
  208. unsigned int service)
  209. {
  210. struct flow_control_instance *instance;
  211. unsigned int res;
  212. res = hdb_handle_create (&flow_control_hdb,
  213. sizeof (struct flow_control_instance), flow_control_handle);
  214. if (res != 0) {
  215. goto error_exit;
  216. }
  217. res = hdb_handle_get (&flow_control_hdb, *flow_control_handle,
  218. (void *)&instance);
  219. if (res != 0) {
  220. goto error_destroy;
  221. }
  222. instance->service = service;
  223. list_init (&instance->list_head);
  224. return (0);
  225. error_destroy:
  226. hdb_handle_destroy (&flow_control_hdb, *flow_control_handle);
  227. error_exit:
  228. return (-1);
  229. }
  230. unsigned int openais_flow_control_ipc_exit (
  231. unsigned int flow_control_handle)
  232. {
  233. hdb_handle_destroy (&flow_control_hdb, flow_control_handle);
  234. return (0);
  235. }
  236. unsigned int openais_flow_control_create (
  237. unsigned int flow_control_handle,
  238. unsigned int service,
  239. void *id,
  240. unsigned int id_len,
  241. void (*flow_control_state_set_fn) (void *context, enum openais_flow_control_state flow_control_state),
  242. void *context)
  243. {
  244. struct flow_control_service *flow_control_service;
  245. struct flow_control_instance *instance;
  246. unsigned int res;
  247. unsigned int i;
  248. res = hdb_handle_get (&flow_control_hdb, flow_control_handle,
  249. (void *)&instance);
  250. if (res != 0) {
  251. goto error_exit;
  252. }
  253. flow_control_service = malloc (sizeof (struct flow_control_service));
  254. if (flow_control_service == NULL) {
  255. goto error_put;
  256. }
  257. /*
  258. * Add new service to flow control system
  259. */
  260. memset (flow_control_service, 0, sizeof (struct flow_control_service));
  261. flow_control_service->flow_control_state = OPENAIS_FLOW_CONTROL_STATE_DISABLED;
  262. flow_control_service->service = service;
  263. memcpy (flow_control_service->id, id, id_len);
  264. flow_control_service->id_len = id_len;
  265. flow_control_service->flow_control_state_set_fn = flow_control_state_set_fn;
  266. flow_control_service->context = context;
  267. list_init (&flow_control_service->list);
  268. list_add_tail (&instance->list_head,
  269. &flow_control_service->list);
  270. list_init (&flow_control_service->list_all);
  271. list_add_tail (&flow_control_service_list_head,
  272. &flow_control_service->list_all);
  273. for (i = 0; i < flow_control_member_list_entries; i++) {
  274. flow_control_service->flow_control_node_state[i].nodeid = flow_control_member_list[i];
  275. flow_control_service->processor_count = flow_control_member_list_entries;
  276. }
  277. error_put:
  278. hdb_handle_put (&flow_control_hdb, flow_control_handle);
  279. error_exit:
  280. return (res);
  281. }
  282. unsigned int openais_flow_control_destroy (
  283. unsigned int flow_control_identifier,
  284. unsigned int service,
  285. unsigned char *id,
  286. unsigned int id_len)
  287. {
  288. struct flow_control_service *flow_control_service;
  289. struct flow_control_instance *instance;
  290. struct list_head *list;
  291. unsigned int res;
  292. res = hdb_handle_get (&flow_control_hdb, flow_control_handle,
  293. (void *)&instance);
  294. if (res != 0) {
  295. goto error_exit;
  296. }
  297. for (list = flow_control_service_list_head.next;
  298. list != &flow_control_service_list_head;
  299. list = list->next) {
  300. flow_control_service = list_entry (list, struct flow_control_service, list_all);
  301. if ((flow_control_service->id_len == id_len) &&
  302. (memcmp (flow_control_service->id, id, id_len) == 0)) {
  303. list_del (&flow_control_service->list);
  304. list_del (&flow_control_service->list_all);
  305. free (flow_control_service);
  306. break; /* done */
  307. }
  308. }
  309. hdb_handle_put (&flow_control_hdb, flow_control_handle);
  310. error_exit:
  311. return (res);
  312. }
  313. /*
  314. * Disable the ability for new messages to be sent for this service
  315. * with the handle id of length id_len
  316. */
  317. unsigned int openais_flow_control_disable (
  318. unsigned int flow_control_handle)
  319. {
  320. struct flow_control_instance *instance;
  321. struct flow_control_service *flow_control_service;
  322. struct list_head *list;
  323. unsigned int res;
  324. unsigned int i;
  325. res = hdb_handle_get (&flow_control_hdb, flow_control_handle,
  326. (void *)&instance);
  327. if (res != 0) {
  328. goto error_exit;
  329. }
  330. i = 0;
  331. for (list = instance->list_head.next;
  332. list != &instance->list_head;
  333. list = list->next) {
  334. flow_control_service = list_entry (list, struct flow_control_service, list);
  335. flow_control_service->flow_control_state = OPENAIS_FLOW_CONTROL_STATE_DISABLED;
  336. flow_control_xmit (flow_control_service, OPENAIS_FLOW_CONTROL_STATE_DISABLED);
  337. }
  338. hdb_handle_put (&flow_control_hdb, flow_control_handle);
  339. error_exit:
  340. return (res);
  341. }
  342. /*
  343. * Enable the ability for new messagess to be sent for this service
  344. * with the handle id of length id_len
  345. */
  346. unsigned int openais_flow_control_enable (
  347. unsigned int flow_control_handle)
  348. {
  349. struct flow_control_instance *instance;
  350. struct flow_control_service *flow_control_service;
  351. struct list_head *list;
  352. unsigned int res;
  353. res = hdb_handle_get (&flow_control_hdb, flow_control_handle,
  354. (void *)&instance);
  355. if (res != 0) {
  356. goto error_exit;
  357. }
  358. for (list = instance->list_head.next;
  359. list != &instance->list_head;
  360. list = list->next) {
  361. flow_control_service = list_entry (list, struct flow_control_service, list);
  362. flow_control_service->flow_control_state = OPENAIS_FLOW_CONTROL_STATE_ENABLED;
  363. flow_control_xmit (flow_control_service, OPENAIS_FLOW_CONTROL_STATE_ENABLED);
  364. }
  365. hdb_handle_put (&flow_control_hdb, flow_control_handle);
  366. error_exit:
  367. return (res);
  368. }