4
0

flow.c 14 KB

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