amfapp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 "amf.h"
  128. #include "print.h"
  129. #include "util.h"
  130. typedef struct application_event {
  131. amf_application_event_type_t event_type;
  132. amf_application_t *app;
  133. amf_node_t *node;
  134. } application_event_t;
  135. /******************************************************************************
  136. * Internal (static) utility functions
  137. *****************************************************************************/
  138. static int is_cluster_start(amf_node_t *node_to_start)
  139. {
  140. return node_to_start == NULL;
  141. }
  142. static void application_defer_event (
  143. amf_application_event_type_t event_type, amf_application_t *app,
  144. amf_node_t *node)
  145. {
  146. application_event_t app_event = {event_type, app, node};
  147. ENTER("");
  148. amf_fifo_put (event_type, &app->deferred_events,
  149. sizeof (application_event_t), &app_event);
  150. }
  151. static void application_recall_deferred_events (amf_application_t *app)
  152. {
  153. application_event_t application_event;
  154. if (amf_fifo_get (&app->deferred_events, &application_event)) {
  155. switch (application_event.event_type) {
  156. case APPLICATION_ASSIGN_WORKLOAD_EV: {
  157. log_printf (LOG_NOTICE,
  158. "Recall APPLICATION_ASSIGN_WORKLOAD_EV");
  159. amf_application_assign_workload (
  160. application_event.app,
  161. application_event.node);
  162. break;
  163. }
  164. case APPLICATION_START_EV: {
  165. log_printf (LOG_NOTICE,
  166. "Recall APPLICATION_START_EV");
  167. amf_application_start (application_event.app,
  168. application_event.node);
  169. break;
  170. }
  171. default:
  172. assert (0);
  173. break;
  174. }
  175. }
  176. }
  177. static void timer_function_application_recall_deferred_events (void *data)
  178. {
  179. amf_application_t *app = (amf_application_t*)data;
  180. ENTER ("");
  181. application_recall_deferred_events (app);
  182. }
  183. static int no_su_is_instantiating (struct amf_application *app)
  184. {
  185. struct amf_sg *sg;
  186. struct amf_su *su;
  187. int all_su_instantiated = 1;
  188. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  189. for (su = sg->su_head; su != NULL; su = su->next) {
  190. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  191. all_su_instantiated = 0;
  192. break;
  193. }
  194. }
  195. }
  196. return all_su_instantiated;
  197. }
  198. static int all_sg_assigned (struct amf_application *app)
  199. {
  200. struct amf_sg *sg;
  201. int all_sg_assigned = 1;
  202. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  203. if (sg->avail_state != SG_AC_Idle) {
  204. all_sg_assigned = 0;
  205. break;
  206. }
  207. }
  208. return all_sg_assigned;
  209. }
  210. static void start_all_sg_for_cluster (amf_application_t *app)
  211. {
  212. amf_sg_t *sg;
  213. int su_to_instantiate = 0;
  214. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  215. su_to_instantiate += amf_sg_start (sg, NULL);
  216. }
  217. if (su_to_instantiate == 0) {
  218. amf_cluster_application_started (app->cluster, app);
  219. }
  220. }
  221. static void timer_function_cluster_application_started (void* app)
  222. {
  223. ENTER("");
  224. amf_application_t *application = (amf_application_t*)app;
  225. amf_cluster_application_started (application->cluster, application);
  226. }
  227. static void timer_function_node_application_started (void* app)
  228. {
  229. ENTER("");
  230. amf_application_t *application = (amf_application_t*)app;
  231. amf_node_application_started (application->node_to_start, application);
  232. }
  233. static void application_enter_starting_sgs (struct amf_application *app,
  234. struct amf_node *node)
  235. {
  236. amf_sg_t *sg = 0;
  237. int su_to_instantiate = 0;
  238. app->node_to_start = node;
  239. app->acsm_state = APP_AC_STARTING_SGS;
  240. ENTER ("%s",app->name.value);
  241. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  242. su_to_instantiate += amf_sg_start (sg, node);
  243. }
  244. if (su_to_instantiate == 0) {
  245. app->acsm_state = APP_AC_STARTED;
  246. if (is_cluster_start (app->node_to_start)) {
  247. amf_call_function_asynchronous (
  248. timer_function_cluster_application_started, app);
  249. } else {
  250. amf_call_function_asynchronous (
  251. timer_function_node_application_started, app);
  252. }
  253. }
  254. }
  255. static void application_enter_assigning_workload (amf_application_t *app)
  256. {
  257. amf_sg_t *sg = 0;
  258. int posible_to_assign_si = 0;
  259. ENTER ("%s",app->name.value);
  260. app->acsm_state = APP_AC_ASSIGNING_WORKLOAD;
  261. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  262. if (amf_sg_assign_si_req (sg, 0)) {
  263. posible_to_assign_si = 1;
  264. }
  265. }
  266. if (posible_to_assign_si == 0) {
  267. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  268. }
  269. }
  270. static void application_enter_workload_assigned (amf_application_t *app)
  271. {
  272. ENTER ("%s", app->name.value);
  273. if (all_sg_assigned (app)){
  274. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  275. if (app->node_to_start == NULL){
  276. amf_cluster_application_workload_assigned (
  277. app->cluster, app);
  278. } else {
  279. TRACE1("%s",app->node_to_start->name.value);
  280. amf_node_application_workload_assigned(
  281. app->node_to_start, app);
  282. }
  283. amf_call_function_asynchronous (
  284. timer_function_application_recall_deferred_events, app);
  285. }
  286. }
  287. /******************************************************************************
  288. * Event methods
  289. *****************************************************************************/
  290. void amf_application_start (
  291. struct amf_application *app, struct amf_node *node)
  292. {
  293. ENTER ("'%s'", app->name.value);
  294. assert (app != NULL);
  295. switch (app->acsm_state) {
  296. case APP_AC_UNINSTANTIATED:
  297. application_enter_starting_sgs (app, node);
  298. break;
  299. case APP_AC_STARTING_SGS:
  300. if (is_cluster_start (app->node_to_start)) {
  301. start_all_sg_for_cluster (app);
  302. } else { /*is_not_cluster_start*/
  303. application_defer_event (APPLICATION_START_EV, app , node);
  304. }
  305. break;
  306. case APP_AC_STARTED:
  307. if (is_cluster_start (app->node_to_start)) {
  308. app->acsm_state = APP_AC_STARTING_SGS;
  309. start_all_sg_for_cluster (app);
  310. } else { /*is_not_cluster_start*/
  311. application_defer_event (APPLICATION_START_EV, app , node);
  312. }
  313. break;
  314. case APP_AC_ASSIGNING_WORKLOAD:
  315. log_printf (LOG_LEVEL_ERROR, "Request to start application"
  316. " =%s in state APP_AC_ASSIGNING_WORKLOAD(should be deferred)",
  317. app->name.value);
  318. application_defer_event (APPLICATION_START_EV, app , node);
  319. break;
  320. case APP_AC_WORKLOAD_ASSIGNED:
  321. application_enter_starting_sgs (app, node);
  322. break;
  323. default:
  324. assert (0);
  325. break;
  326. }
  327. }
  328. void amf_application_assign_workload (struct amf_application *app,
  329. struct amf_node *node)
  330. {
  331. /*
  332. * TODO: dependency level ignored. Each dependency level should
  333. * be looped and amf_sg_assign_si called several times.
  334. */
  335. assert (app != NULL);
  336. app->node_to_start = node;
  337. ENTER("app->acsm_state = %d",app->acsm_state);
  338. switch (app->acsm_state) {
  339. case APP_AC_STARTING_SGS:
  340. if (is_cluster_start (node)) {
  341. application_enter_assigning_workload (app);
  342. }
  343. break;
  344. case APP_AC_WORKLOAD_ASSIGNED:
  345. application_enter_assigning_workload (app);
  346. break;
  347. case APP_AC_STARTED:
  348. application_enter_assigning_workload (app);
  349. break;
  350. case APP_AC_ASSIGNING_WORKLOAD:
  351. if (app->node_to_start == node) {
  352. /*
  353. * Calling object has violated the contract !
  354. */
  355. assert (0);
  356. } else {
  357. log_printf (LOG_LEVEL_ERROR, "Request to assign workload to"
  358. " application =%s in state APP_AC_ASSIGNING_WORKLOAD "
  359. "(should be deferred)", app->name.value);
  360. application_defer_event (APPLICATION_ASSIGN_WORKLOAD_EV, app,
  361. node);
  362. }
  363. break;
  364. default:
  365. /*
  366. * Calling object has violated the contract !
  367. */
  368. dprintf ("acsm_state = %d",app->acsm_state);
  369. assert (0);
  370. break;
  371. }
  372. }
  373. /******************************************************************************
  374. * Event response methods
  375. *****************************************************************************/
  376. void amf_application_sg_started (struct amf_application *app, struct amf_sg *sg,
  377. struct amf_node *node)
  378. {
  379. ENTER ("'%s %s'", app->name.value, sg->name.value);
  380. assert (app != NULL);
  381. switch (app->acsm_state) {
  382. case APP_AC_STARTING_SGS:
  383. if (no_su_is_instantiating (app)) {
  384. app->acsm_state = APP_AC_STARTED;
  385. if (app->node_to_start == NULL) {
  386. amf_cluster_application_started (app->cluster, app);
  387. } else {
  388. amf_node_application_started (app->node_to_start, app);
  389. }
  390. }
  391. break;
  392. default:
  393. log_printf (LOG_LEVEL_ERROR, "amf_application_sg_started()"
  394. " called in state = %d", app->acsm_state);
  395. openais_exit_error (AIS_DONE_FATAL_ERR);
  396. break;
  397. }
  398. }
  399. void amf_application_sg_assigned (
  400. struct amf_application *app, struct amf_sg *sg)
  401. {
  402. ENTER ("'%s'", app->name.value);
  403. assert (app != NULL);
  404. switch (app->acsm_state) {
  405. case APP_AC_ASSIGNING_WORKLOAD:
  406. application_enter_workload_assigned (app);
  407. break;
  408. default:
  409. log_printf (LOG_LEVEL_ERROR,
  410. "amf_application_sg_assigned()"
  411. " called in state = %d", app->acsm_state);
  412. openais_exit_error (AIS_DONE_FATAL_ERR);
  413. break;
  414. }
  415. }
  416. /******************************************************************************
  417. * General methods
  418. *****************************************************************************/
  419. void amf_application_init (void)
  420. {
  421. log_init ("AMF");
  422. }
  423. struct amf_application *amf_application_new (struct amf_cluster *cluster) {
  424. struct amf_application *app = amf_calloc (1,
  425. sizeof (struct amf_application));
  426. app->cluster = cluster;
  427. app->next = cluster->application_head;
  428. cluster->application_head = app;
  429. app->acsm_state = APP_AC_UNINSTANTIATED;
  430. app->node_to_start = NULL;
  431. return app;
  432. }
  433. void amf_application_delete (struct amf_application *app)
  434. {
  435. struct amf_sg *sg;
  436. struct amf_si *si;
  437. assert (app != NULL);
  438. for (sg = app->sg_head; sg != NULL;) {
  439. struct amf_sg *tmp = sg;
  440. sg = sg->next;
  441. amf_sg_delete (tmp);
  442. }
  443. for (si = app->si_head; si != NULL;) {
  444. struct amf_si *tmp = si;
  445. si = si->next;
  446. amf_si_delete (tmp);
  447. }
  448. free (app);
  449. }
  450. void *amf_application_serialize (
  451. struct amf_application *app, int *len)
  452. {
  453. char *buf = NULL;
  454. int offset = 0, size = 0;
  455. assert (app != NULL);
  456. TRACE8 ("%s", app->name.value);
  457. buf = amf_serialize_SaNameT (buf, &size, &offset, &app->name);
  458. buf = amf_serialize_SaUint32T (
  459. buf, &size, &offset, app->saAmfApplicationAdminState);
  460. buf = amf_serialize_SaUint32T (
  461. buf, &size, &offset, app->saAmfApplicationCurrNumSG);
  462. buf = amf_serialize_SaStringT (
  463. buf, &size, &offset, app->clccli_path);
  464. buf = amf_serialize_SaUint32T (
  465. buf, &size, &offset, app->acsm_state);
  466. *len = offset;
  467. return buf;
  468. }
  469. struct amf_application *amf_application_deserialize (
  470. struct amf_cluster *cluster, char *buf)
  471. {
  472. char *tmp = buf;
  473. struct amf_application *app = amf_application_new (cluster);
  474. tmp = amf_deserialize_SaNameT (tmp, &app->name);
  475. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationAdminState);
  476. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationCurrNumSG);
  477. tmp = amf_deserialize_SaStringT (tmp, &app->clccli_path);
  478. tmp = amf_deserialize_SaUint32T (tmp, &app->acsm_state);
  479. return app;
  480. }
  481. struct amf_application *amf_application_find (
  482. struct amf_cluster *cluster, char *name)
  483. {
  484. struct amf_application *app;
  485. assert (cluster != NULL);
  486. for (app = cluster->application_head; app != NULL; app = app->next) {
  487. if (app->name.length == strlen(name) &&
  488. strncmp (name, (char*)app->name.value, app->name.length)
  489. == 0) {
  490. break;
  491. }
  492. }
  493. if (app == NULL) {
  494. dprintf ("App %s not found!", name);
  495. }
  496. return app;
  497. }