amfapp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 "logsys.h"
  130. #include "util.h"
  131. LOGSYS_DECLARE_SUBSYS ("AMF", LOG_INFO);
  132. typedef struct application_event {
  133. amf_application_event_type_t event_type;
  134. amf_application_t *app;
  135. amf_node_t *node;
  136. } application_event_t;
  137. /******************************************************************************
  138. * Internal (static) utility functions
  139. *****************************************************************************/
  140. static int is_cluster_start(amf_node_t *node_to_start)
  141. {
  142. return node_to_start == NULL;
  143. }
  144. static void application_defer_event (
  145. amf_application_event_type_t event_type, amf_application_t *app,
  146. amf_node_t *node)
  147. {
  148. application_event_t app_event = {event_type, app, node};
  149. ENTER("");
  150. amf_fifo_put (event_type, &app->deferred_events,
  151. sizeof (application_event_t), &app_event);
  152. }
  153. static void application_recall_deferred_events (amf_application_t *app)
  154. {
  155. application_event_t application_event;
  156. if (amf_fifo_get (&app->deferred_events, &application_event)) {
  157. switch (application_event.event_type) {
  158. case APPLICATION_ASSIGN_WORKLOAD_EV: {
  159. log_printf (LOG_NOTICE,
  160. "Recall APPLICATION_ASSIGN_WORKLOAD_EV");
  161. amf_application_assign_workload (
  162. application_event.app,
  163. application_event.node);
  164. break;
  165. }
  166. case APPLICATION_START_EV: {
  167. log_printf (LOG_NOTICE,
  168. "Recall APPLICATION_START_EV");
  169. amf_application_start (application_event.app,
  170. application_event.node);
  171. break;
  172. }
  173. default:
  174. assert (0);
  175. break;
  176. }
  177. }
  178. }
  179. static void timer_function_application_recall_deferred_events (void *data)
  180. {
  181. amf_application_t *app = (amf_application_t*)data;
  182. ENTER ("");
  183. application_recall_deferred_events (app);
  184. }
  185. static int no_su_is_instantiating (struct amf_application *app)
  186. {
  187. struct amf_sg *sg;
  188. struct amf_su *su;
  189. int all_su_instantiated = 1;
  190. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  191. for (su = sg->su_head; su != NULL; su = su->next) {
  192. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  193. all_su_instantiated = 0;
  194. break;
  195. }
  196. }
  197. }
  198. return all_su_instantiated;
  199. }
  200. static int all_sg_assigned (struct amf_application *app)
  201. {
  202. struct amf_sg *sg;
  203. int all_sg_assigned = 1;
  204. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  205. if (sg->avail_state != SG_AC_Idle) {
  206. all_sg_assigned = 0;
  207. break;
  208. }
  209. }
  210. return all_sg_assigned;
  211. }
  212. static void start_all_sg_for_cluster (amf_application_t *app)
  213. {
  214. amf_sg_t *sg;
  215. int su_to_instantiate = 0;
  216. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  217. su_to_instantiate += amf_sg_start (sg, NULL);
  218. }
  219. if (su_to_instantiate == 0) {
  220. amf_cluster_application_started (app->cluster, app);
  221. }
  222. }
  223. static void timer_function_cluster_application_started (void* app)
  224. {
  225. ENTER("");
  226. amf_application_t *application = (amf_application_t*)app;
  227. amf_cluster_application_started (application->cluster, application);
  228. }
  229. static void timer_function_node_application_started (void* app)
  230. {
  231. ENTER("");
  232. amf_application_t *application = (amf_application_t*)app;
  233. amf_node_application_started (application->node_to_start, application);
  234. }
  235. static void application_enter_starting_sgs (struct amf_application *app,
  236. struct amf_node *node)
  237. {
  238. amf_sg_t *sg = 0;
  239. int su_to_instantiate = 0;
  240. app->node_to_start = node;
  241. app->acsm_state = APP_AC_STARTING_SGS;
  242. ENTER ("%s",app->name.value);
  243. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  244. su_to_instantiate += amf_sg_start (sg, node);
  245. }
  246. if (su_to_instantiate == 0) {
  247. app->acsm_state = APP_AC_STARTED;
  248. if (is_cluster_start (app->node_to_start)) {
  249. amf_call_function_asynchronous (
  250. timer_function_cluster_application_started, app);
  251. } else {
  252. amf_call_function_asynchronous (
  253. timer_function_node_application_started, app);
  254. }
  255. }
  256. }
  257. static void application_enter_assigning_workload (amf_application_t *app)
  258. {
  259. amf_sg_t *sg = 0;
  260. int posible_to_assign_si = 0;
  261. ENTER ("%s",app->name.value);
  262. app->acsm_state = APP_AC_ASSIGNING_WORKLOAD;
  263. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  264. if (amf_sg_assign_si_req (sg, 0)) {
  265. posible_to_assign_si = 1;
  266. }
  267. }
  268. if (posible_to_assign_si == 0) {
  269. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  270. }
  271. }
  272. static void application_enter_workload_assigned (amf_application_t *app)
  273. {
  274. ENTER ("%s", app->name.value);
  275. if (all_sg_assigned (app)){
  276. app->acsm_state = APP_AC_WORKLOAD_ASSIGNED;
  277. if (app->node_to_start == NULL){
  278. amf_cluster_application_workload_assigned (
  279. app->cluster, app);
  280. } else {
  281. TRACE1("%s",app->node_to_start->name.value);
  282. amf_node_application_workload_assigned(
  283. app->node_to_start, app);
  284. }
  285. amf_call_function_asynchronous (
  286. timer_function_application_recall_deferred_events, app);
  287. }
  288. }
  289. /******************************************************************************
  290. * Event methods
  291. *****************************************************************************/
  292. void amf_application_start (
  293. struct amf_application *app, struct amf_node *node)
  294. {
  295. ENTER ("'%s'", app->name.value);
  296. assert (app != NULL);
  297. switch (app->acsm_state) {
  298. case APP_AC_UNINSTANTIATED:
  299. application_enter_starting_sgs (app, node);
  300. break;
  301. case APP_AC_STARTING_SGS:
  302. if (is_cluster_start (app->node_to_start)) {
  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_STARTED:
  309. if (is_cluster_start (app->node_to_start)) {
  310. app->acsm_state = APP_AC_STARTING_SGS;
  311. start_all_sg_for_cluster (app);
  312. } else { /*is_not_cluster_start*/
  313. application_defer_event (APPLICATION_START_EV, app , node);
  314. }
  315. break;
  316. case APP_AC_ASSIGNING_WORKLOAD:
  317. log_printf (LOG_LEVEL_ERROR, "Request to start application"
  318. " =%s in state APP_AC_ASSIGNING_WORKLOAD(should be deferred)",
  319. app->name.value);
  320. application_defer_event (APPLICATION_START_EV, app , node);
  321. break;
  322. case APP_AC_WORKLOAD_ASSIGNED:
  323. application_enter_starting_sgs (app, node);
  324. break;
  325. default:
  326. assert (0);
  327. break;
  328. }
  329. }
  330. void amf_application_assign_workload (struct amf_application *app,
  331. struct amf_node *node)
  332. {
  333. /*
  334. * TODO: dependency level ignored. Each dependency level should
  335. * be looped and amf_sg_assign_si called several times.
  336. */
  337. assert (app != NULL);
  338. app->node_to_start = node;
  339. ENTER("app->acsm_state = %d",app->acsm_state);
  340. switch (app->acsm_state) {
  341. case APP_AC_STARTING_SGS:
  342. if (is_cluster_start (node)) {
  343. application_enter_assigning_workload (app);
  344. }
  345. break;
  346. case APP_AC_WORKLOAD_ASSIGNED:
  347. application_enter_assigning_workload (app);
  348. break;
  349. case APP_AC_STARTED:
  350. application_enter_assigning_workload (app);
  351. break;
  352. case APP_AC_ASSIGNING_WORKLOAD:
  353. if (app->node_to_start == node) {
  354. /*
  355. * Calling object has violated the contract !
  356. */
  357. assert (0);
  358. } else {
  359. log_printf (LOG_LEVEL_ERROR, "Request to assign workload to"
  360. " application =%s in state APP_AC_ASSIGNING_WORKLOAD "
  361. "(should be deferred)", app->name.value);
  362. application_defer_event (APPLICATION_ASSIGN_WORKLOAD_EV, app,
  363. node);
  364. }
  365. break;
  366. default:
  367. /*
  368. * Calling object has violated the contract !
  369. */
  370. dprintf ("acsm_state = %d",app->acsm_state);
  371. assert (0);
  372. break;
  373. }
  374. }
  375. /******************************************************************************
  376. * Event response methods
  377. *****************************************************************************/
  378. void amf_application_sg_started (struct amf_application *app, struct amf_sg *sg,
  379. struct amf_node *node)
  380. {
  381. ENTER ("'%s %s'", app->name.value, sg->name.value);
  382. assert (app != NULL);
  383. switch (app->acsm_state) {
  384. case APP_AC_STARTING_SGS:
  385. if (no_su_is_instantiating (app)) {
  386. app->acsm_state = APP_AC_STARTED;
  387. if (app->node_to_start == NULL) {
  388. amf_cluster_application_started (app->cluster, app);
  389. } else {
  390. amf_node_application_started (app->node_to_start, app);
  391. }
  392. }
  393. break;
  394. default:
  395. log_printf (LOG_LEVEL_ERROR, "amf_application_sg_started()"
  396. " called in state = %d", app->acsm_state);
  397. openais_exit_error (AIS_DONE_FATAL_ERR);
  398. break;
  399. }
  400. }
  401. void amf_application_sg_assigned (
  402. struct amf_application *app, struct amf_sg *sg)
  403. {
  404. ENTER ("'%s'", app->name.value);
  405. assert (app != NULL);
  406. switch (app->acsm_state) {
  407. case APP_AC_ASSIGNING_WORKLOAD:
  408. application_enter_workload_assigned (app);
  409. break;
  410. default:
  411. log_printf (LOG_LEVEL_ERROR,
  412. "amf_application_sg_assigned()"
  413. " called in state = %d", app->acsm_state);
  414. openais_exit_error (AIS_DONE_FATAL_ERR);
  415. break;
  416. }
  417. }
  418. /******************************************************************************
  419. * General methods
  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. }