amfapp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 application 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. * WORKLOAD_ASSIGNED) and a number of states to control the transition between
  62. * the 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 Application Availability 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. * STARTING_SGS assign_workload [C4] A3 ASSIGNING_WORKLOAD
  83. * STARTED start A6,A1 STARTING_SGS
  84. * STARTED start [!C4] A7 STARTED
  85. * STARTED assign_workload A3 ASSIGNING_WORKLOAD
  86. * ASSIGNING_WORKLOAD assign_workload A7 ASSIGNING_WORKLOAD
  87. * ASSIGNING_WORKLOAD start A7 ASSIGNING_WORKLOAD
  88. * ASSIGNING_WORKLOAD sg_assigned [C2] A10,A9 WORKLOAD_ASSIGNED
  89. * WORKLOAD_ASSIGNED start A6,A1 STARTING_SGS
  90. * WORKLOAD_ASSIGNED assign_workload A3 ASSIGNING_WORKLOAD
  91. *
  92. * 1.2 State Description
  93. * =====================
  94. * UNINSTANTIATED - No SUs within the SGs contained in the Application have been
  95. * instantiated.
  96. * STARTING_SGS - Waiting for the contained SGs to start.
  97. * STARTED - No SUs within the SGs contained in the Application are in the
  98. * process of beein instantiated. Either the SUs are instantiated or
  99. * instantiation was not possible or instantiation has failed.
  100. * ASSIGNING_WORKLOAD - Waiting for the contained SGs to indicate they have
  101. * assigned workload to its SUs.
  102. * WORKLOAD_ASSIGNED - at least some workload has been assigned to the SUs that
  103. * are in-service.
  104. *
  105. * 1.3 Actions
  106. * ===========
  107. * A1 - [foreach SG in Application] sg_start
  108. * A2 -
  109. * A3 - [foreach SG in Application] sg_assign
  110. * A4 -
  111. * A5 -
  112. * A6 - save value of received node parameter
  113. * A7 - defer the event
  114. * A8 - [node == NULL] cluster_application_started else node_application_started
  115. * A9 - recall deferred events
  116. * A10 - [node == NULL] cluster_application_assigned else
  117. * node_application_assigned
  118. *
  119. * 1.4 Guards
  120. * ==========
  121. * C1 - No SU has presence state == INSTANTIATING
  122. * C2 - All SGs have availability control state == IDLE
  123. * C3 -
  124. * C4 - Sender is Cluster
  125. */
  126. #include <assert.h>
  127. #include <stdlib.h>
  128. #include "amf.h"
  129. #include "print.h"
  130. #include "util.h"
  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. /******************************************************************************
  137. * Internal (static) utility functions
  138. *****************************************************************************/
  139. static int is_cluster_start(amf_node_t *node_to_start)
  140. {
  141. return node_to_start == NULL;
  142. }
  143. static void application_defer_event (
  144. amf_application_event_type_t event_type, amf_application_t *app,
  145. amf_node_t *node)
  146. {
  147. application_event_t app_event = {event_type, app, node};
  148. ENTER("");
  149. amf_fifo_put (event_type, &app->deferred_events,
  150. sizeof (application_event_t), &app_event);
  151. }
  152. static void application_recall_deferred_events (amf_application_t *app)
  153. {
  154. application_event_t application_event;
  155. if (amf_fifo_get (&app->deferred_events, &application_event)) {
  156. switch (application_event.event_type) {
  157. case APPLICATION_ASSIGN_WORKLOAD_EV: {
  158. log_printf (LOG_NOTICE,
  159. "Recall APPLICATION_ASSIGN_WORKLOAD_EV");
  160. amf_application_assign_workload (
  161. application_event.app,
  162. application_event.node);
  163. break;
  164. }
  165. case APPLICATION_START_EV: {
  166. log_printf (LOG_NOTICE,
  167. "Recall APPLICATION_START_EV");
  168. amf_application_start (application_event.app,
  169. application_event.node);
  170. break;
  171. }
  172. default:
  173. assert (0);
  174. break;
  175. }
  176. }
  177. }
  178. static void timer_function_application_recall_deferred_events (void *data)
  179. {
  180. amf_application_t *app = (amf_application_t*)data;
  181. ENTER ("");
  182. application_recall_deferred_events (app);
  183. }
  184. static int no_su_is_instantiating (struct amf_application *app)
  185. {
  186. struct amf_sg *sg;
  187. struct amf_su *su;
  188. int all_su_instantiated = 1;
  189. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  190. for (su = sg->su_head; su != NULL; su = su->next) {
  191. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  192. all_su_instantiated = 0;
  193. break;
  194. }
  195. }
  196. }
  197. return all_su_instantiated;
  198. }
  199. static int all_sg_assigned (struct amf_application *app)
  200. {
  201. struct amf_sg *sg;
  202. int all_sg_assigned = 1;
  203. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  204. if (sg->avail_state != SG_AC_Idle) {
  205. all_sg_assigned = 0;
  206. break;
  207. }
  208. }
  209. return all_sg_assigned;
  210. }
  211. static void start_all_sg_for_cluster (amf_application_t *app)
  212. {
  213. amf_sg_t *sg;
  214. int su_to_instantiate = 0;
  215. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  216. su_to_instantiate += amf_sg_start (sg, NULL);
  217. }
  218. if (su_to_instantiate == 0) {
  219. amf_cluster_application_started (app->cluster, app);
  220. }
  221. }
  222. static void timer_function_cluster_application_started (void* app)
  223. {
  224. ENTER("");
  225. amf_application_t *application = (amf_application_t*)app;
  226. amf_cluster_application_started (application->cluster, application);
  227. }
  228. static void timer_function_node_application_started (void* app)
  229. {
  230. ENTER("");
  231. amf_application_t *application = (amf_application_t*)app;
  232. amf_node_application_started (application->node_to_start, application);
  233. }
  234. static void application_enter_starting_sgs (struct amf_application *app,
  235. struct amf_node *node)
  236. {
  237. amf_sg_t *sg = 0;
  238. int su_to_instantiate = 0;
  239. app->node_to_start = node;
  240. app->acsm_state = APP_AC_STARTING_SGS;
  241. ENTER ("%s",app->name.value);
  242. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  243. su_to_instantiate += amf_sg_start (sg, node);
  244. }
  245. if (su_to_instantiate == 0) {
  246. app->acsm_state = APP_AC_STARTED;
  247. if (is_cluster_start (app->node_to_start)) {
  248. amf_call_function_asynchronous (
  249. timer_function_cluster_application_started, app);
  250. } else {
  251. amf_call_function_asynchronous (
  252. timer_function_node_application_started, app);
  253. }
  254. }
  255. }
  256. static void application_enter_assigning_workload (amf_application_t *app)
  257. {
  258. amf_sg_t *sg = 0;
  259. int posible_to_assign_si = 0;
  260. ENTER ("%s",app->name.value);
  261. app->acsm_state = APP_AC_ASSIGNING_WORKLOAD;
  262. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  263. if (amf_sg_assign_si_req (sg, 0)) {
  264. posible_to_assign_si = 1;
  265. }
  266. }
  267. if (posible_to_assign_si == 0) {
  268. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  269. }
  270. }
  271. static void application_enter_workload_assigned (amf_application_t *app)
  272. {
  273. ENTER ("%s", app->name.value);
  274. if (all_sg_assigned (app)){
  275. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  276. if (app->node_to_start == NULL){
  277. amf_cluster_application_workload_assigned (
  278. app->cluster, app);
  279. } else {
  280. TRACE1("%s",app->node_to_start->name.value);
  281. amf_node_application_workload_assigned(
  282. app->node_to_start, app);
  283. }
  284. amf_call_function_asynchronous (
  285. timer_function_application_recall_deferred_events, app);
  286. }
  287. }
  288. /******************************************************************************
  289. * Event methods
  290. *****************************************************************************/
  291. void amf_application_start (
  292. struct amf_application *app, struct amf_node *node)
  293. {
  294. ENTER ("'%s'", app->name.value);
  295. assert (app != NULL);
  296. switch (app->acsm_state) {
  297. case APP_AC_UNINSTANTIATED:
  298. application_enter_starting_sgs (app, node);
  299. break;
  300. case APP_AC_STARTING_SGS:
  301. if (is_cluster_start (app->node_to_start)) {
  302. start_all_sg_for_cluster (app);
  303. } else { /*is_not_cluster_start*/
  304. application_defer_event (APPLICATION_START_EV, app , node);
  305. }
  306. break;
  307. case APP_AC_STARTED:
  308. if (is_cluster_start (app->node_to_start)) {
  309. app->acsm_state = APP_AC_STARTING_SGS;
  310. start_all_sg_for_cluster (app);
  311. } else { /*is_not_cluster_start*/
  312. application_defer_event (APPLICATION_START_EV, app , node);
  313. }
  314. break;
  315. case APP_AC_ASSIGNING_WORKLOAD:
  316. log_printf (LOG_LEVEL_ERROR, "Request to start application"
  317. " =%s in state APP_AC_ASSIGNING_WORKLOAD(should be deferred)",
  318. app->name.value);
  319. application_defer_event (APPLICATION_START_EV, app , node);
  320. break;
  321. case APP_AC_WORKLOAD_ASSIGNED:
  322. application_enter_starting_sgs (app, node);
  323. break;
  324. default:
  325. assert (0);
  326. break;
  327. }
  328. }
  329. void amf_application_assign_workload (struct amf_application *app,
  330. struct amf_node *node)
  331. {
  332. /*
  333. * TODO: dependency level ignored. Each dependency level should
  334. * be looped and amf_sg_assign_si called several times.
  335. */
  336. assert (app != NULL);
  337. app->node_to_start = node;
  338. ENTER("app->acsm_state = %d",app->acsm_state);
  339. switch (app->acsm_state) {
  340. case APP_AC_STARTING_SGS:
  341. if (is_cluster_start (node)) {
  342. application_enter_assigning_workload (app);
  343. }
  344. break;
  345. case APP_AC_WORKLOAD_ASSIGNED:
  346. application_enter_assigning_workload (app);
  347. break;
  348. case APP_AC_STARTED:
  349. application_enter_assigning_workload (app);
  350. break;
  351. case APP_AC_ASSIGNING_WORKLOAD:
  352. if (app->node_to_start == node) {
  353. /*
  354. * Calling object has violated the contract !
  355. */
  356. assert (0);
  357. } else {
  358. log_printf (LOG_LEVEL_ERROR, "Request to assign workload to"
  359. " application =%s in state APP_AC_ASSIGNING_WORKLOAD "
  360. "(should be deferred)", app->name.value);
  361. application_defer_event (APPLICATION_ASSIGN_WORKLOAD_EV, app,
  362. node);
  363. }
  364. break;
  365. default:
  366. /*
  367. * Calling object has violated the contract !
  368. */
  369. dprintf ("acsm_state = %d",app->acsm_state);
  370. assert (0);
  371. break;
  372. }
  373. }
  374. /******************************************************************************
  375. * Event response methods
  376. *****************************************************************************/
  377. void amf_application_sg_started (struct amf_application *app, struct amf_sg *sg,
  378. struct amf_node *node)
  379. {
  380. ENTER ("'%s %s'", app->name.value, sg->name.value);
  381. assert (app != NULL);
  382. switch (app->acsm_state) {
  383. case APP_AC_STARTING_SGS:
  384. if (no_su_is_instantiating (app)) {
  385. app->acsm_state = APP_AC_STARTED;
  386. if (app->node_to_start == NULL) {
  387. amf_cluster_application_started (app->cluster, app);
  388. } else {
  389. amf_node_application_started (app->node_to_start, app);
  390. }
  391. }
  392. break;
  393. default:
  394. log_printf (LOG_LEVEL_ERROR, "amf_application_sg_started()"
  395. " called in state = %d", app->acsm_state);
  396. openais_exit_error (AIS_DONE_FATAL_ERR);
  397. break;
  398. }
  399. }
  400. void amf_application_sg_assigned (
  401. struct amf_application *app, struct amf_sg *sg)
  402. {
  403. ENTER ("'%s'", app->name.value);
  404. assert (app != NULL);
  405. switch (app->acsm_state) {
  406. case APP_AC_ASSIGNING_WORKLOAD:
  407. application_enter_workload_assigned (app);
  408. break;
  409. default:
  410. log_printf (LOG_LEVEL_ERROR,
  411. "amf_application_sg_assigned()"
  412. " called in state = %d", app->acsm_state);
  413. openais_exit_error (AIS_DONE_FATAL_ERR);
  414. break;
  415. }
  416. }
  417. /******************************************************************************
  418. * General methods
  419. *****************************************************************************/
  420. void amf_application_init (void)
  421. {
  422. log_init ("AMF");
  423. }
  424. struct amf_application *amf_application_new (struct amf_cluster *cluster) {
  425. struct amf_application *app = amf_calloc (1,
  426. sizeof (struct amf_application));
  427. app->cluster = cluster;
  428. app->next = cluster->application_head;
  429. cluster->application_head = app;
  430. app->acsm_state = APP_AC_UNINSTANTIATED;
  431. app->node_to_start = NULL;
  432. return app;
  433. }
  434. void amf_application_delete (struct amf_application *app)
  435. {
  436. struct amf_sg *sg;
  437. struct amf_si *si;
  438. assert (app != NULL);
  439. for (sg = app->sg_head; sg != NULL;) {
  440. struct amf_sg *tmp = sg;
  441. sg = sg->next;
  442. amf_sg_delete (tmp);
  443. }
  444. for (si = app->si_head; si != NULL;) {
  445. struct amf_si *tmp = si;
  446. si = si->next;
  447. amf_si_delete (tmp);
  448. }
  449. free (app);
  450. }
  451. void *amf_application_serialize (
  452. struct amf_application *app, int *len)
  453. {
  454. char *buf = NULL;
  455. int offset = 0, size = 0;
  456. assert (app != NULL);
  457. TRACE8 ("%s", app->name.value);
  458. buf = amf_serialize_SaNameT (buf, &size, &offset, &app->name);
  459. buf = amf_serialize_SaUint32T (
  460. buf, &size, &offset, app->saAmfApplicationAdminState);
  461. buf = amf_serialize_SaUint32T (
  462. buf, &size, &offset, app->saAmfApplicationCurrNumSG);
  463. buf = amf_serialize_SaStringT (
  464. buf, &size, &offset, app->clccli_path);
  465. buf = amf_serialize_SaUint32T (
  466. buf, &size, &offset, app->acsm_state);
  467. *len = offset;
  468. return buf;
  469. }
  470. struct amf_application *amf_application_deserialize (
  471. struct amf_cluster *cluster, char *buf)
  472. {
  473. char *tmp = buf;
  474. struct amf_application *app = amf_application_new (cluster);
  475. tmp = amf_deserialize_SaNameT (tmp, &app->name);
  476. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationAdminState);
  477. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationCurrNumSG);
  478. tmp = amf_deserialize_SaStringT (tmp, &app->clccli_path);
  479. tmp = amf_deserialize_SaUint32T (tmp, &app->acsm_state);
  480. return app;
  481. }
  482. struct amf_application *amf_application_find (
  483. struct amf_cluster *cluster, char *name)
  484. {
  485. struct amf_application *app;
  486. assert (cluster != NULL);
  487. for (app = cluster->application_head; app != NULL; app = app->next) {
  488. if (app->name.length == strlen(name) &&
  489. strncmp (name, (char*)app->name.value, app->name.length)
  490. == 0) {
  491. break;
  492. }
  493. }
  494. if (app == NULL) {
  495. dprintf ("App %s not found!", name);
  496. }
  497. return app;
  498. }