flow.c 14 KB

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