amfapp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /** @file amfapp.c
  2. *
  3. * Copyright (c) 2006 Ericsson AB.
  4. * Author: Hans Feldt, Anders Eriksson, Lars Holm
  5. * - Refactoring of code into several AMF files
  6. * - Constructors/destructors
  7. * - Serializers/deserializers
  8. *
  9. * All rights reserved.
  10. *
  11. *
  12. * This software licensed under BSD license, the text of which follows:
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  36. * THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * AMF Application Class implementation
  39. *
  40. * This file contains functions for handling the AMF applications. It can
  41. * be viewed as the implementation of the AMF Application class
  42. * as described in SAI-Overview-B.02.01. The SA Forum specification
  43. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  44. * and is referred to as 'the spec' below.
  45. *
  46. * The functions in this file are responsible for:
  47. * - on request start the service groups it contains
  48. * - on request order the service groups to assign workload to all
  49. * service units contained in the service group, level by level
  50. * - to handle administrative operation support for the application (FUTURE)
  51. *
  52. * The cluster class contains the following state machines:
  53. * - administrative state machine (ADSM)
  54. * - availability control state machine (ACSM)
  55. *
  56. * The administrative state machine will be implemented in the future.
  57. *
  58. * ACSM handles initial start of an application. In the future it will also
  59. * handle administrative commands on the application as described in paragraph
  60. * 7.4 of the spec. ACSM includes two stable states (UNINSTANTIATED and
  61. * STARTED) and a number of states to control the transition between the
  62. * stable states.
  63. *
  64. * The application is in state UNINSTANTIATED when the application starts.
  65. * (In the future this state will also be assumed after the LOCK_INSTANTIATION
  66. * administrative command.)
  67. *
  68. * State WORKLOAD_ASSIGNED is assumed when the application has been initially
  69. * started and will in the future be re-assumed after the administrative
  70. * command RESTART have been executed.
  71. *
  72. * 1. AMF Synchronization Control State Machine
  73. * =========================================
  74. *
  75. * 1.1 State Transition Table
  76. *
  77. * State: Event: Action: New state:
  78. * ===========================================================================
  79. * UNINSTANTIATED start A6,A1 STARTING_SGS
  80. * STARTING_SGS start [C4] A7
  81. * STARTING_SGS sg_started [C1] A8,A9 STARTED
  82. * STARTED start A6,A1 STARTING_SGS
  83. * STARTED assign_workload A3 ASSIGNING_WORKLOAD
  84. * ASSIGNING_WORKLOAD assign_workload A7
  85. * ASSIGNING_WORKLOAD start A7
  86. * ASSIGNING_WORKLOAD sg_assigned [C2] A10,A9 WORKLOAD_ASSIGNED
  87. * WORKLOAD_ASSIGNED start A6,A1 STARTING_SGS
  88. * WORKLOAD_ASSIGNED assign_workload A3 ASSIGNING_WORKLOAD
  89. *
  90. * 1.2 State Description
  91. * =====================
  92. * UNINSTANTIATED - No SUs within the SGs contained in the application have been
  93. * instantiated.
  94. * STARTING_SGS - Waiting for the contained SGs to start.
  95. * STARTED - No SUs within the SGs contained in the application are in the
  96. * process of beein instantiated. Either the SUs are instantiated or
  97. * instantiation was not possible or instantiation has failed.
  98. * ASSIGNING_WORKLOAD - Waiting for the contained SGs to indicate they have
  99. * assigned workload to its SUs.
  100. * WORKLOAD_ASSIGNED - at least some workload has been assigned to the SUs that
  101. * are in-service.
  102. *
  103. * 1.3 Actions
  104. * ===========
  105. * A1 - [foreach sg in application] sg_start
  106. * A2 -
  107. * A3 - [foreach sg in application] sg_assign
  108. * A4 -
  109. * A5 -
  110. * A6 - save value of received node parameter
  111. * A7 - defer the event
  112. * A8 - [node == NULL] cluster_application_started else node_application_started
  113. * A9 - recall deferred events
  114. * A10 - [node == NULL] cluster_application_assigned else
  115. * node_application_assigned
  116. *
  117. * 1.4 Guards
  118. * ==========
  119. * C1 - No sg has availability control state == INSTANTIATING_SERVICE_UNITS
  120. * C2 - All sgs have availability control state == IDLE
  121. * C3 -
  122. * C4 - saved node value != received node value
  123. */
  124. #include <assert.h>
  125. #include "amf.h"
  126. #include "print.h"
  127. #include "util.h"
  128. /******************************************************************************
  129. * Internal (static) utility functions
  130. *****************************************************************************/
  131. typedef struct application_event {
  132. amf_application_event_type_t event_type;
  133. amf_application_t *app;
  134. amf_node_t *node;
  135. } application_event_t;
  136. static void application_defer_event (
  137. amf_application_event_type_t event_type, amf_application_t *app,
  138. amf_node_t *node)
  139. {
  140. application_event_t app_event = {event_type, app, node};
  141. amf_fifo_put (event_type, &app->deferred_events,
  142. sizeof (application_event_t), &app_event);
  143. }
  144. static void application_recall_deferred_events (amf_application_t *app)
  145. {
  146. application_event_t application_event;
  147. if (amf_fifo_get (&app->deferred_events, &application_event)) {
  148. switch (application_event.event_type) {
  149. case APPLICATION_ASSIGN_WORKLOAD_EV: {
  150. log_printf (LOG_NOTICE,
  151. "Recall APPLICATION_ASSIGN_WORKLOAD_EV");
  152. amf_application_assign_workload (
  153. application_event.app,
  154. application_event.node);
  155. break;
  156. }
  157. case APPLICATION_START_EV: {
  158. log_printf (LOG_NOTICE,
  159. "Recall APPLICATION_START_EV");
  160. amf_application_start (application_event.app,
  161. application_event.node);
  162. break;
  163. }
  164. default:
  165. assert (0);
  166. break;
  167. }
  168. }
  169. }
  170. static void timer_function_application_recall_deferred_events (void *data)
  171. {
  172. amf_application_t *app = (amf_application_t*)data;
  173. ENTER ("");
  174. application_recall_deferred_events (app);
  175. }
  176. static int no_su_is_instantiating (struct amf_application *app)
  177. {
  178. struct amf_sg *sg;
  179. struct amf_su *su;
  180. int all_su_instantiated = 1;
  181. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  182. for (su = sg->su_head; su != NULL; su = su->next) {
  183. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  184. all_su_instantiated = 0;
  185. break;
  186. }
  187. }
  188. }
  189. return all_su_instantiated;
  190. }
  191. static int all_sg_assigned (struct amf_application *app)
  192. {
  193. struct amf_sg *sg;
  194. int all_sg_assigned = 1;
  195. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  196. if (sg->avail_state != SG_AC_Idle) {
  197. all_sg_assigned = 0;
  198. break;
  199. }
  200. }
  201. return all_sg_assigned;
  202. }
  203. static void application_enter_starting_sgs (struct amf_application *app,
  204. struct amf_node *node)
  205. {
  206. amf_sg_t *sg = 0;
  207. app->node_to_start = node;
  208. app->acsm_state = APP_AC_STARTING_SGS;
  209. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  210. amf_sg_start (sg, node);
  211. }
  212. }
  213. static void application_enter_assigning_workload (amf_application_t *app)
  214. {
  215. amf_sg_t *sg = 0;
  216. int posible_to_assign_si = 0;
  217. app->acsm_state = APP_AC_ASSIGNING_WORKLOAD;
  218. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  219. if (amf_sg_assign_si_req (sg, 0)) {
  220. posible_to_assign_si = 1;
  221. }
  222. }
  223. if (posible_to_assign_si == 0) {
  224. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  225. }
  226. }
  227. static void application_enter_workload_assigned (amf_application_t *app)
  228. {
  229. if (all_sg_assigned (app)){
  230. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  231. if (app->node_to_start == NULL){
  232. amf_cluster_application_workload_assigned (
  233. app->cluster, app);
  234. } else {
  235. amf_node_application_workload_assigned(
  236. app->node_to_start, app);
  237. }
  238. amf_call_function_asynchronous (
  239. timer_function_application_recall_deferred_events, app);
  240. }
  241. }
  242. /******************************************************************************
  243. * Event methods
  244. *****************************************************************************/
  245. void amf_application_start (
  246. struct amf_application *app, struct amf_node *node)
  247. {
  248. struct amf_sg *sg;
  249. ENTER ("'%s'", app->name.value);
  250. assert (app != NULL);
  251. switch (app->acsm_state) {
  252. case APP_AC_UNINSTANTIATED:
  253. application_enter_starting_sgs (app, node);
  254. break;
  255. case APP_AC_STARTING_SGS:
  256. if (app->node_to_start == node) {
  257. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  258. amf_sg_start (sg, node);
  259. }
  260. } else {
  261. /* TODO: Save the start request until state == APP_AC_STARTED */
  262. log_printf (LOG_LEVEL_ERROR, "Request to start application"
  263. " =%s in state = %d",app->name.value, app->acsm_state);
  264. openais_exit_error (AIS_DONE_FATAL_ERR);
  265. }
  266. break;
  267. case APP_AC_STARTED:
  268. /* TODO: Recall deferred events */
  269. app->node_to_start = node;
  270. app->acsm_state = APP_AC_STARTING_SGS;
  271. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  272. amf_sg_start (sg, node);
  273. }
  274. break;
  275. case APP_AC_ASSIGNING_WORKLOAD:
  276. log_printf (LOG_LEVEL_ERROR, "Request to start application"
  277. " =%s in state APP_AC_ASSIGNING_WORKLOAD(should be deferred)",
  278. app->name.value);
  279. application_defer_event (APPLICATION_START_EV, app , node);
  280. break;
  281. case APP_AC_WORKLOAD_ASSIGNED:
  282. application_enter_starting_sgs (app, node);
  283. break;
  284. default:
  285. assert (0);
  286. break;
  287. }
  288. }
  289. void amf_application_assign_workload (struct amf_application *app,
  290. struct amf_node *node)
  291. {
  292. /*
  293. * TODO: dependency level ignored. Each dependency level should
  294. * be looped and amf_sg_assign_si called several times.
  295. */
  296. assert (app != NULL);
  297. app->node_to_start = node;
  298. switch (app->acsm_state) {
  299. case APP_AC_WORKLOAD_ASSIGNED:
  300. application_enter_assigning_workload (app);
  301. break;
  302. case APP_AC_STARTED:
  303. application_enter_assigning_workload (app);
  304. break;
  305. case APP_AC_ASSIGNING_WORKLOAD:
  306. if (app->node_to_start == node) {
  307. /*
  308. * Calling object has violated the contract !
  309. */
  310. assert (0);
  311. } else {
  312. log_printf (LOG_LEVEL_ERROR, "Request to assign workload to"
  313. " application =%s in state APP_AC_ASSIGNING_WORKLOAD "
  314. "(should be deferred)", app->name.value);
  315. application_defer_event (APPLICATION_ASSIGN_WORKLOAD_EV, app,
  316. node);
  317. }
  318. break;
  319. default:
  320. /*
  321. * Calling object has violated the contract !
  322. */
  323. assert (0);
  324. break;
  325. }
  326. }
  327. /******************************************************************************
  328. * Event response methods
  329. *****************************************************************************/
  330. void amf_application_sg_started (struct amf_application *app, struct amf_sg *sg,
  331. struct amf_node *node)
  332. {
  333. ENTER ("'%s'", app->name.value);
  334. assert (app != NULL);
  335. switch (app->acsm_state) {
  336. case APP_AC_STARTING_SGS:
  337. if (no_su_is_instantiating (app)) {
  338. app->acsm_state = APP_AC_STARTED;
  339. if (app->node_to_start == NULL) {
  340. amf_cluster_application_started (app->cluster, app);
  341. } else {
  342. amf_node_application_started (app->node_to_start, app);
  343. }
  344. }
  345. break;
  346. default:
  347. log_printf (LOG_LEVEL_ERROR, "amf_application_sg_started()"
  348. " called in state = %d", app->acsm_state);
  349. openais_exit_error (AIS_DONE_FATAL_ERR);
  350. break;
  351. }
  352. }
  353. void amf_application_sg_assigned (
  354. struct amf_application *app, struct amf_sg *sg)
  355. {
  356. ENTER ("'%s'", app->name.value);
  357. assert (app != NULL);
  358. switch (app->acsm_state) {
  359. case APP_AC_ASSIGNING_WORKLOAD:
  360. application_enter_workload_assigned (app);
  361. break;
  362. default:
  363. log_printf (LOG_LEVEL_ERROR,
  364. "amf_application_sg_assigned()"
  365. " called in state = %d", app->acsm_state);
  366. openais_exit_error (AIS_DONE_FATAL_ERR);
  367. break;
  368. }
  369. }
  370. /******************************************************************************
  371. * General methods
  372. *****************************************************************************/
  373. void amf_application_init (void)
  374. {
  375. log_init ("AMF");
  376. }
  377. struct amf_application *amf_application_new (struct amf_cluster *cluster) {
  378. struct amf_application *app = amf_calloc (1,
  379. sizeof (struct amf_application));
  380. app->cluster = cluster;
  381. app->next = cluster->application_head;
  382. cluster->application_head = app;
  383. app->acsm_state = APP_AC_UNINSTANTIATED;
  384. return app;
  385. }
  386. void amf_application_delete (struct amf_application *app)
  387. {
  388. struct amf_sg *sg;
  389. struct amf_si *si;
  390. assert (app != NULL);
  391. for (sg = app->sg_head; sg != NULL;) {
  392. struct amf_sg *tmp = sg;
  393. sg = sg->next;
  394. amf_sg_delete (tmp);
  395. }
  396. for (si = app->si_head; si != NULL;) {
  397. struct amf_si *tmp = si;
  398. si = si->next;
  399. amf_si_delete (tmp);
  400. }
  401. free (app);
  402. }
  403. void *amf_application_serialize (
  404. struct amf_application *app, int *len)
  405. {
  406. char *buf = NULL;
  407. int offset = 0, size = 0;
  408. assert (app != NULL);
  409. TRACE8 ("%s", app->name.value);
  410. buf = amf_serialize_SaNameT (buf, &size, &offset, &app->name);
  411. buf = amf_serialize_SaUint32T (
  412. buf, &size, &offset, app->saAmfApplicationAdminState);
  413. buf = amf_serialize_SaUint32T (
  414. buf, &size, &offset, app->saAmfApplicationCurrNumSG);
  415. buf = amf_serialize_SaStringT (
  416. buf, &size, &offset, app->clccli_path);
  417. buf = amf_serialize_SaUint32T (
  418. buf, &size, &offset, app->acsm_state);
  419. *len = offset;
  420. return buf;
  421. }
  422. struct amf_application *amf_application_deserialize (
  423. struct amf_cluster *cluster, char *buf)
  424. {
  425. char *tmp = buf;
  426. struct amf_application *app = amf_application_new (cluster);
  427. tmp = amf_deserialize_SaNameT (tmp, &app->name);
  428. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationAdminState);
  429. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationCurrNumSG);
  430. tmp = amf_deserialize_SaStringT (tmp, &app->clccli_path);
  431. tmp = amf_deserialize_SaUint32T (tmp, &app->acsm_state);
  432. return app;
  433. }
  434. struct amf_application *amf_application_find (
  435. struct amf_cluster *cluster, char *name)
  436. {
  437. struct amf_application *app;
  438. assert (cluster != NULL);
  439. for (app = cluster->application_head; app != NULL; app = app->next) {
  440. if (app->name.length == strlen(name) &&
  441. strncmp (name, (char*)app->name.value, app->name.length)
  442. == 0) {
  443. break;
  444. }
  445. }
  446. if (app == NULL) {
  447. dprintf ("App %s not found!", name);
  448. }
  449. return app;
  450. }