amfapp.c 16 KB

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