amfapp.c 16 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 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. ENTER ("%s",app->name.value);
  239. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  240. su_to_instantiate += amf_sg_start (sg, node);
  241. }
  242. if (su_to_instantiate == 0) {
  243. app->acsm_state = APP_AC_STARTED;
  244. if (is_cluster_start (app->node_to_start)) {
  245. amf_call_function_asynchronous (
  246. timer_function_cluster_application_started, app);
  247. } else {
  248. amf_call_function_asynchronous (
  249. timer_function_node_application_started, app);
  250. }
  251. }
  252. }
  253. static void application_enter_assigning_workload (amf_application_t *app)
  254. {
  255. amf_sg_t *sg = 0;
  256. int posible_to_assign_si = 0;
  257. ENTER ("%s",app->name.value);
  258. app->acsm_state = APP_AC_ASSIGNING_WORKLOAD;
  259. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  260. if (amf_sg_assign_si_req (sg, 0)) {
  261. posible_to_assign_si = 1;
  262. }
  263. }
  264. if (posible_to_assign_si == 0) {
  265. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  266. }
  267. }
  268. static void application_enter_workload_assigned (amf_application_t *app)
  269. {
  270. ENTER ("%s", app->name.value);
  271. if (all_sg_assigned (app)){
  272. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  273. if (app->node_to_start == NULL){
  274. amf_cluster_application_workload_assigned (
  275. app->cluster, app);
  276. } else {
  277. TRACE1("%s",app->node_to_start->name.value);
  278. amf_node_application_workload_assigned(
  279. app->node_to_start, app);
  280. }
  281. amf_call_function_asynchronous (
  282. timer_function_application_recall_deferred_events, app);
  283. }
  284. }
  285. /******************************************************************************
  286. * Event methods
  287. *****************************************************************************/
  288. void amf_application_start (
  289. struct amf_application *app, struct amf_node *node)
  290. {
  291. ENTER ("'%s'", app->name.value);
  292. assert (app != NULL);
  293. switch (app->acsm_state) {
  294. case APP_AC_UNINSTANTIATED:
  295. application_enter_starting_sgs (app, node);
  296. break;
  297. case APP_AC_STARTING_SGS:
  298. if (is_cluster_start (app->node_to_start)) {
  299. start_all_sg_for_cluster (app);
  300. } else { /*is_not_cluster_start*/
  301. application_defer_event (APPLICATION_START_EV, app , node);
  302. }
  303. break;
  304. case APP_AC_STARTED:
  305. if (is_cluster_start (app->node_to_start)) {
  306. app->acsm_state = APP_AC_STARTING_SGS;
  307. start_all_sg_for_cluster (app);
  308. } else { /*is_not_cluster_start*/
  309. application_defer_event (APPLICATION_START_EV, app , node);
  310. }
  311. break;
  312. case APP_AC_ASSIGNING_WORKLOAD:
  313. log_printf (LOG_LEVEL_ERROR, "Request to start application"
  314. " =%s in state APP_AC_ASSIGNING_WORKLOAD(should be deferred)",
  315. app->name.value);
  316. application_defer_event (APPLICATION_START_EV, app , node);
  317. break;
  318. case APP_AC_WORKLOAD_ASSIGNED:
  319. application_enter_starting_sgs (app, node);
  320. break;
  321. default:
  322. assert (0);
  323. break;
  324. }
  325. }
  326. void amf_application_assign_workload (struct amf_application *app,
  327. struct amf_node *node)
  328. {
  329. /*
  330. * TODO: dependency level ignored. Each dependency level should
  331. * be looped and amf_sg_assign_si called several times.
  332. */
  333. assert (app != NULL);
  334. app->node_to_start = node;
  335. ENTER("app->acsm_state = %d",app->acsm_state);
  336. switch (app->acsm_state) {
  337. case APP_AC_STARTING_SGS:
  338. if (is_cluster_start (node)) {
  339. application_enter_assigning_workload (app);
  340. }
  341. break;
  342. case APP_AC_WORKLOAD_ASSIGNED:
  343. application_enter_assigning_workload (app);
  344. break;
  345. case APP_AC_STARTED:
  346. application_enter_assigning_workload (app);
  347. break;
  348. case APP_AC_ASSIGNING_WORKLOAD:
  349. if (app->node_to_start == node) {
  350. /*
  351. * Calling object has violated the contract !
  352. */
  353. assert (0);
  354. } else {
  355. log_printf (LOG_LEVEL_ERROR, "Request to assign workload to"
  356. " application =%s in state APP_AC_ASSIGNING_WORKLOAD "
  357. "(should be deferred)", app->name.value);
  358. application_defer_event (APPLICATION_ASSIGN_WORKLOAD_EV, app,
  359. node);
  360. }
  361. break;
  362. default:
  363. /*
  364. * Calling object has violated the contract !
  365. */
  366. dprintf ("acsm_state = %d",app->acsm_state);
  367. assert (0);
  368. break;
  369. }
  370. }
  371. /******************************************************************************
  372. * Event response methods
  373. *****************************************************************************/
  374. void amf_application_sg_started (struct amf_application *app, struct amf_sg *sg,
  375. struct amf_node *node)
  376. {
  377. ENTER ("'%s %s'", app->name.value, sg->name.value);
  378. assert (app != NULL);
  379. switch (app->acsm_state) {
  380. case APP_AC_STARTING_SGS:
  381. if (no_su_is_instantiating (app)) {
  382. app->acsm_state = APP_AC_STARTED;
  383. if (app->node_to_start == NULL) {
  384. amf_cluster_application_started (app->cluster, app);
  385. } else {
  386. amf_node_application_started (app->node_to_start, app);
  387. }
  388. }
  389. break;
  390. default:
  391. log_printf (LOG_LEVEL_ERROR, "amf_application_sg_started()"
  392. " called in state = %d", app->acsm_state);
  393. openais_exit_error (AIS_DONE_FATAL_ERR);
  394. break;
  395. }
  396. }
  397. void amf_application_sg_assigned (
  398. struct amf_application *app, struct amf_sg *sg)
  399. {
  400. ENTER ("'%s'", app->name.value);
  401. assert (app != NULL);
  402. switch (app->acsm_state) {
  403. case APP_AC_ASSIGNING_WORKLOAD:
  404. application_enter_workload_assigned (app);
  405. break;
  406. default:
  407. log_printf (LOG_LEVEL_ERROR,
  408. "amf_application_sg_assigned()"
  409. " called in state = %d", app->acsm_state);
  410. openais_exit_error (AIS_DONE_FATAL_ERR);
  411. break;
  412. }
  413. }
  414. /******************************************************************************
  415. * General methods
  416. *****************************************************************************/
  417. void amf_application_init (void)
  418. {
  419. log_init ("AMF");
  420. }
  421. struct amf_application *amf_application_new (struct amf_cluster *cluster) {
  422. struct amf_application *app = amf_calloc (1,
  423. sizeof (struct amf_application));
  424. app->cluster = cluster;
  425. app->next = cluster->application_head;
  426. cluster->application_head = app;
  427. app->acsm_state = APP_AC_UNINSTANTIATED;
  428. app->node_to_start = NULL;
  429. return app;
  430. }
  431. void amf_application_delete (struct amf_application *app)
  432. {
  433. struct amf_sg *sg;
  434. struct amf_si *si;
  435. assert (app != NULL);
  436. for (sg = app->sg_head; sg != NULL;) {
  437. struct amf_sg *tmp = sg;
  438. sg = sg->next;
  439. amf_sg_delete (tmp);
  440. }
  441. for (si = app->si_head; si != NULL;) {
  442. struct amf_si *tmp = si;
  443. si = si->next;
  444. amf_si_delete (tmp);
  445. }
  446. free (app);
  447. }
  448. void *amf_application_serialize (
  449. struct amf_application *app, int *len)
  450. {
  451. char *buf = NULL;
  452. int offset = 0, size = 0;
  453. assert (app != NULL);
  454. TRACE8 ("%s", app->name.value);
  455. buf = amf_serialize_SaNameT (buf, &size, &offset, &app->name);
  456. buf = amf_serialize_SaUint32T (
  457. buf, &size, &offset, app->saAmfApplicationAdminState);
  458. buf = amf_serialize_SaUint32T (
  459. buf, &size, &offset, app->saAmfApplicationCurrNumSG);
  460. buf = amf_serialize_SaStringT (
  461. buf, &size, &offset, app->clccli_path);
  462. buf = amf_serialize_SaUint32T (
  463. buf, &size, &offset, app->acsm_state);
  464. *len = offset;
  465. return buf;
  466. }
  467. struct amf_application *amf_application_deserialize (
  468. struct amf_cluster *cluster, char *buf)
  469. {
  470. char *tmp = buf;
  471. struct amf_application *app = amf_application_new (cluster);
  472. tmp = amf_deserialize_SaNameT (tmp, &app->name);
  473. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationAdminState);
  474. tmp = amf_deserialize_SaUint32T (tmp, &app->saAmfApplicationCurrNumSG);
  475. tmp = amf_deserialize_SaStringT (tmp, &app->clccli_path);
  476. tmp = amf_deserialize_SaUint32T (tmp, &app->acsm_state);
  477. return app;
  478. }
  479. struct amf_application *amf_application_find (
  480. struct amf_cluster *cluster, char *name)
  481. {
  482. struct amf_application *app;
  483. assert (cluster != NULL);
  484. for (app = cluster->application_head; app != NULL; app = app->next) {
  485. if (app->name.length == strlen(name) &&
  486. strncmp (name, (char*)app->name.value, app->name.length)
  487. == 0) {
  488. break;
  489. }
  490. }
  491. if (app == NULL) {
  492. dprintf ("App %s not found!", name);
  493. }
  494. return app;
  495. }