amfsu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /** @file exec/amfsu.c
  2. *
  3. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  4. * Author: Steven Dake (sdake@mvista.com)
  5. *
  6. * Copyright (c) 2006 Ericsson AB.
  7. * Author: Hans Feldt, Anders Eriksson, Lars Holm
  8. * - Introduced AMF B.02 information model
  9. * - Use DN in API and multicast messages
  10. * - (Re-)Introduction of event based multicast messages
  11. * - Refactoring of code into several AMF files
  12. * - Component/SU restart, SU failover
  13. * - Constructors/destructors
  14. * - Serializers/deserializers
  15. *
  16. * All rights reserved.
  17. *
  18. *
  19. * This software licensed under BSD license, the text of which follows:
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright notice,
  25. * this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  30. * contributors may be used to endorse or promote products derived from this
  31. * software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  35. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  37. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  38. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  39. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  43. * THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * AMF Service Unit Class Implementation
  46. *
  47. * This file contains functions for handling AMF-service units(SUs). It can be
  48. * viewed as the implementation of the AMF Service Unit class (called SU)
  49. * as described in SAI-Overview-B.02.01. The SA Forum specification
  50. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  51. * and is referred to as 'the spec' below.
  52. *
  53. * The functions in this file are responsible for:
  54. * - instantiating and terminating service units on request
  55. * (considering the dependencies between components described in paragraph
  56. * 3.9.2)
  57. * - creating and deleting CSI-assignment objects between its components and
  58. * CSI-objects upon request
  59. * - receiving error reports from its components and forwarding them to
  60. * appropriate handler (SU or SG or node or cluster)
  61. * - implementing restart of itself and its components (paragraph 3.12.1.2)
  62. * - implementing error escallation level 1 (paragraph 3.12.2.2 in the spec)
  63. * - handling all run time attributes of the AMF SU; all cached
  64. * attributes are stored as variables and sent to the IMM service
  65. * upon the changes described in the specification.
  66. *
  67. * SU contains the following state machines:
  68. * - presence state machine (PRSM)
  69. * - administrative state machine (ADSM) (NOT IN THIS RELEASE)
  70. * - operational state machine (OPSM)
  71. * - readiness state machine (RESM)
  72. * - ha state per service instance (SI)
  73. * - restart control state machine (RCSM)
  74. *
  75. * The presence state machine orders intantiation of its components on request.
  76. * It fully respects the dependency rules between components at instantiation
  77. * such that it orders instantiation simultaneously only of components on the
  78. * same instantiation level. The presence state machine is implemented with
  79. * the states described in the spec and the state transitions are trigged by
  80. * reported state transitions from its contained components according to
  81. * paragraph 3.3.1.1.
  82. *
  83. * The operational state machine is not responsible for any control function.
  84. * It assumes the DISABLED state if an incoming operational state change report
  85. * from a component indicates the component has assumed the DISABLED state.
  86. * Operational state changes are reported to IMM.
  87. *
  88. * The readiness state machine is not used for any control but is updated and
  89. * reported to IMM when it is changed.
  90. *
  91. * The restart control state machine (RCSM) is used to implement level 1 of
  92. * the error escallation polycy described in chapter 3.12.2 of the spec. It
  93. * also implements component restart and service unit restart as described in
  94. * paragraph 3.12.1.2 and 3.12.1.3.
  95. * RCSM contains three composite states.
  96. * Being a composite state means that the state contains substates.
  97. * RCSM composite states are:
  98. * - ESCALLATION_LEVEL (LEVEL_0, LEVEL_1 and LEVEL_2)
  99. * - RESTARTING_COMPONENT (DEACTIVATING, RESTARTING, SETTING and ACTIVATING)
  100. * - RESTARTING_SERVICE_UNIT (DEACTIVATING, TERMINATING, INSTANTIATING,
  101. * and ACTIVATING)
  102. *
  103. * ESCALLATION_LEVEL is a kind of idle state where no actions are performed
  104. * and used only to remember the escallation level. Substate LEVEL_0 indicates
  105. * no escallation. LEVEL_1 indicates that a component restart has been
  106. * executed recently and the escallation timer is still running. At this level
  107. * component restart requests will transition to RESTARTING_COMPONENT but
  108. * if there are too many restart requests before the probation timer expires
  109. * then a transition will be made to LEVEL_2 and the restart request will
  110. * be forwarded to the node instance hosting this component.
  111. * State RESTARTING_SERVICE_UNIT will only be assumed if the node explicitly
  112. * requests the SU to execute a restart of itself (after having evaluated its
  113. * part of the error escallation policy).
  114. *
  115. */
  116. /*
  117. *
  118. */
  119. #include <stdlib.h>
  120. #include <assert.h>
  121. #include <string.h>
  122. #include <errno.h>
  123. #include "amf.h"
  124. #include "util.h"
  125. #include "print.h"
  126. #include "main.h"
  127. /**
  128. * This function only logs since the readiness state is runtime
  129. * calculated.
  130. * @param su
  131. * @param amf_readiness_state
  132. */
  133. static void su_readiness_state_set (struct amf_su *su,
  134. SaAmfReadinessStateT readiness_state)
  135. {
  136. log_printf (LOG_NOTICE, "Setting SU '%s' readiness state: %s\n",
  137. su->name.value, amf_readiness_state (readiness_state));
  138. }
  139. static void clear_ha_state (
  140. struct amf_su *su, struct amf_si_assignment *si_assignment)
  141. {
  142. ENTER ("");
  143. si_assignment->saAmfSISUHAState = 0;
  144. }
  145. static void su_presence_state_set (struct amf_su *su,
  146. SaAmfPresenceStateT presence_state)
  147. {
  148. /*
  149. * Set all SI's confirmed HA state to unknown if uninstantiated
  150. */
  151. if (su->saAmfSUPresenceState == SA_AMF_PRESENCE_UNINSTANTIATED) {
  152. amf_su_foreach_si_assignment (su, clear_ha_state);
  153. }
  154. su->saAmfSUPresenceState = presence_state;
  155. log_printf (LOG_NOTICE, "Setting SU '%s' presence state: %s\n",
  156. su->name.value, amf_presence_state (presence_state));
  157. if (su->restart_control_state != SU_RC_RESTART_SU_SETTING) {
  158. amf_sg_su_state_changed (
  159. su->sg, su, SA_AMF_PRESENCE_STATE, presence_state);
  160. }
  161. }
  162. static void su_operational_state_set (struct amf_su *su,
  163. SaAmfOperationalStateT oper_state)
  164. {
  165. struct amf_comp* comp;
  166. su->saAmfSUOperState = oper_state;
  167. log_printf (LOG_NOTICE, "Setting SU '%s' operational state: %s\n",
  168. su->name.value, amf_op_state (oper_state));
  169. if (oper_state == SA_AMF_OPERATIONAL_ENABLED) {
  170. su_readiness_state_set (su, SA_AMF_READINESS_IN_SERVICE);
  171. for (comp = su->comp_head; comp; comp = comp->next) {
  172. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_IN_SERVICE);
  173. }
  174. } else if (oper_state == SA_AMF_OPERATIONAL_DISABLED) {
  175. su_readiness_state_set (su, SA_AMF_READINESS_OUT_OF_SERVICE);
  176. for (comp = su->comp_head; comp; comp = comp->next) {
  177. amf_comp_readiness_state_set (comp, SA_AMF_READINESS_OUT_OF_SERVICE);
  178. }
  179. }
  180. }
  181. static void comp_assign_csi (struct amf_comp *comp, struct amf_csi *csi,
  182. struct amf_si_assignment *si_assignment, SaAmfHAStateT ha_state)
  183. {
  184. struct amf_csi_assignment *csi_assignment;
  185. dprintf (" Creating CSI '%s' to comp '%s' with hastate %s\n",
  186. getSaNameT (&csi->name), getSaNameT (&comp->name),
  187. amf_ha_state (ha_state));
  188. csi_assignment = amf_malloc (sizeof (struct amf_csi_assignment));
  189. csi_assignment->next = csi->assigned_csis;
  190. csi->assigned_csis = csi_assignment;
  191. amf_comp_dn_make (comp, &csi_assignment->name);
  192. csi_assignment->comp = comp;
  193. csi_assignment->csi = csi;
  194. csi_assignment->saAmfCSICompHAState = 0; /* undefined confirmed HA state */
  195. csi_assignment->requested_ha_state = ha_state;
  196. csi_assignment->si_assignment = si_assignment;
  197. }
  198. static void su_restart (struct amf_su *su)
  199. {
  200. struct amf_comp *comp;
  201. SaNameT dn;
  202. ENTER ("'%s'", su->name.value);
  203. amf_su_dn_make (su, &dn);
  204. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  205. "action: SU restart", dn.value);
  206. su->restart_control_state = SU_RC_RESTART_SU_DEACTIVATING;
  207. su->restart_control_state = SU_RC_RESTART_SU_INSTANTIATING;
  208. su->escalation_level_history_state =
  209. SU_RC_ESCALATION_LEVEL_2;
  210. su->saAmfSURestartCount += 1;
  211. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  212. amf_comp_restart (comp);
  213. }
  214. }
  215. static void comp_restart (struct amf_comp *comp)
  216. {
  217. SaNameT dn;
  218. ENTER ("'%s'", comp->name.value);
  219. amf_comp_dn_make (comp, &dn);
  220. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  221. "action: Component restart", dn.value);
  222. comp->su->restart_control_state = SU_RC_RESTART_COMP_DEACTIVATING;
  223. comp->su->restart_control_state = SU_RC_RESTART_COMP_RESTARTING;
  224. comp->su->escalation_level_history_state = SU_RC_ESCALATION_LEVEL_1;
  225. amf_comp_restart (comp);
  226. }
  227. static void si_ha_state_assumed_cbfn (
  228. struct amf_si_assignment *si_assignment, int result)
  229. {
  230. struct amf_si_assignment *tmp_si_assignment;
  231. struct amf_comp *comp;
  232. struct amf_csi_assignment *csi_assignment;
  233. int all_confirmed = 1;
  234. ENTER ("");
  235. tmp_si_assignment = amf_su_get_next_si_assignment(si_assignment->su, NULL);
  236. while (tmp_si_assignment != NULL) {
  237. for (comp = tmp_si_assignment->su->comp_head; comp != NULL;
  238. comp = comp->next) {
  239. csi_assignment = amf_comp_get_next_csi_assignment(comp, NULL);
  240. while (csi_assignment != NULL) {
  241. if (csi_assignment->requested_ha_state !=
  242. csi_assignment->saAmfCSICompHAState) {
  243. all_confirmed = 0;
  244. }
  245. csi_assignment = amf_comp_get_next_csi_assignment(
  246. comp, csi_assignment);
  247. }
  248. }
  249. tmp_si_assignment = amf_su_get_next_si_assignment(
  250. si_assignment->su, tmp_si_assignment);
  251. }
  252. if (all_confirmed) {
  253. switch (si_assignment->su->restart_control_state) {
  254. case SU_RC_RESTART_COMP_SETTING:
  255. log_printf (LOG_NOTICE, "Component restart recovery finished");
  256. break;
  257. case SU_RC_RESTART_SU_SETTING:
  258. log_printf (LOG_NOTICE, "SU restart recovery finished");
  259. break;
  260. default:
  261. assert (0);
  262. break;
  263. }
  264. si_assignment->su->restart_control_state =
  265. si_assignment->su->escalation_level_history_state;
  266. }
  267. }
  268. static void reassign_sis(struct amf_su *su)
  269. {
  270. struct amf_si_assignment *si_assignment;
  271. ENTER ("");
  272. si_assignment = amf_su_get_next_si_assignment(su, NULL);
  273. while (si_assignment != NULL) {
  274. si_assignment->saAmfSISUHAState = 0; /* unknown */
  275. amf_si_ha_state_assume (si_assignment, si_ha_state_assumed_cbfn);
  276. si_assignment = amf_su_get_next_si_assignment(su, si_assignment);
  277. }
  278. }
  279. static void su_comp_presence_state_changed (
  280. struct amf_su *su, struct amf_comp *comp, int state)
  281. {
  282. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  283. switch (state) {
  284. case SA_AMF_PRESENCE_INSTANTIATED:
  285. switch (su->restart_control_state) {
  286. case SU_RC_ESCALATION_LEVEL_2:
  287. /*
  288. * TODO: send to node
  289. */
  290. case SU_RC_ESCALATION_LEVEL_0:
  291. if (amf_su_presence_state_all_comps_in_su_are_set (
  292. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  293. su_presence_state_set (
  294. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  295. }
  296. break;
  297. case SU_RC_RESTART_COMP_RESTARTING:
  298. su->restart_control_state = SU_RC_RESTART_COMP_SETTING;
  299. reassign_sis (comp->su);
  300. break;
  301. case SU_RC_RESTART_SU_INSTANTIATING:
  302. if (amf_su_presence_state_all_comps_in_su_are_set (
  303. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  304. su->restart_control_state = SU_RC_RESTART_SU_SETTING;
  305. su_presence_state_set (
  306. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  307. reassign_sis (comp->su);
  308. }
  309. break;
  310. default:
  311. dprintf ("state %d", su->restart_control_state);
  312. assert (0);
  313. break;
  314. }
  315. break;
  316. case SA_AMF_PRESENCE_UNINSTANTIATED:
  317. if (amf_su_presence_state_all_comps_in_su_are_set (
  318. su, SA_AMF_PRESENCE_UNINSTANTIATED)) {
  319. su_presence_state_set (comp->su,SA_AMF_PRESENCE_UNINSTANTIATED);
  320. }
  321. break;
  322. case SA_AMF_PRESENCE_INSTANTIATING:
  323. su_presence_state_set (comp->su,SA_AMF_PRESENCE_INSTANTIATING);
  324. break;
  325. case SA_AMF_PRESENCE_RESTARTING:
  326. break;
  327. case SA_AMF_PRESENCE_TERMINATING:
  328. break;
  329. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  330. su_presence_state_set (
  331. comp->su, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  332. break;
  333. default:
  334. assert (0);
  335. break;
  336. }
  337. }
  338. static void su_comp_op_state_changed (
  339. struct amf_su *su, struct amf_comp *comp, int state)
  340. {
  341. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  342. switch (state) {
  343. case SA_AMF_OPERATIONAL_ENABLED:
  344. {
  345. struct amf_comp *comp_compare;
  346. int all_set = 1;
  347. for (comp_compare = comp->su->comp_head;
  348. comp_compare != NULL; comp_compare = comp_compare->next) {
  349. if (comp_compare->saAmfCompOperState !=
  350. SA_AMF_OPERATIONAL_ENABLED) {
  351. all_set = 0;
  352. break;
  353. }
  354. }
  355. if (all_set) {
  356. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_ENABLED);
  357. } else {
  358. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_DISABLED);
  359. }
  360. break;
  361. }
  362. case SA_AMF_OPERATIONAL_DISABLED:
  363. break;
  364. default:
  365. assert (0);
  366. break;
  367. }
  368. }
  369. int amf_su_presence_state_all_comps_in_su_are_set (struct amf_su *su,
  370. SaAmfPresenceStateT state)
  371. {
  372. int all_set = 1;
  373. struct amf_comp *comp;
  374. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  375. if (comp->saAmfCompPresenceState != state) {
  376. all_set = 0;
  377. }
  378. }
  379. return all_set;
  380. }
  381. void amf_su_instantiate (struct amf_su *su)
  382. {
  383. struct amf_comp *comp;
  384. ENTER ("'%s'", su->name.value);
  385. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  386. amf_comp_instantiate (comp);
  387. }
  388. }
  389. void amf_su_assign_si (struct amf_su *su, struct amf_si *si,
  390. SaAmfHAStateT ha_state)
  391. {
  392. struct amf_si_assignment *si_assignment;
  393. dprintf ("Creating SI '%s' to SU '%s' with hastate %s\n",
  394. getSaNameT (&si->name), getSaNameT (&su->name),
  395. amf_ha_state (ha_state));
  396. si_assignment = amf_malloc (sizeof (struct amf_si_assignment));
  397. amf_su_dn_make (su, &si_assignment->name);
  398. si_assignment->saAmfSISUHAState = 0; /* undefined confirmed HA state */
  399. si_assignment->requested_ha_state = ha_state;
  400. si_assignment->next = si->assigned_sis;
  401. si->assigned_sis = si_assignment;
  402. si_assignment->si = si;
  403. si_assignment->su = su;
  404. {
  405. struct amf_csi *csi;
  406. struct amf_comp *comp;
  407. SaNameT *cs_type;
  408. int i;
  409. /*
  410. ** for each component in SU, find a CSI in the SI with the same type
  411. */
  412. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  413. int no_of_cs_types = 0;
  414. for (i = 0; comp->saAmfCompCsTypes[i]; i++) {
  415. cs_type = comp->saAmfCompCsTypes[i];
  416. no_of_cs_types++;
  417. int no_of_assignments = 0;
  418. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  419. if (!memcmp(csi->saAmfCSTypeName.value, cs_type->value,
  420. cs_type->length)) {
  421. comp_assign_csi (comp, csi, si_assignment, ha_state);
  422. no_of_assignments++;
  423. }
  424. }
  425. if (no_of_assignments == 0) {
  426. log_printf (
  427. LOG_WARNING, "\t No CSIs of type %s configured?!!\n",
  428. getSaNameT (cs_type));
  429. }
  430. }
  431. if (no_of_cs_types == 0) {
  432. log_printf (LOG_LEVEL_ERROR,
  433. "\t No CS types configured for comp %s ?!!\n",
  434. getSaNameT (&comp->name));
  435. }
  436. }
  437. }
  438. }
  439. /**
  440. * Used by a component to report a state change event
  441. * @param su
  442. * @param comp
  443. * @param type type of state
  444. * @param state new state
  445. */
  446. void amf_su_comp_state_changed (
  447. struct amf_su *su, struct amf_comp *comp, SaAmfStateT type, int state)
  448. {
  449. switch (type) {
  450. case SA_AMF_PRESENCE_STATE:
  451. su_comp_presence_state_changed (su, comp, state);
  452. break;
  453. case SA_AMF_OP_STATE:
  454. su_comp_op_state_changed (su, comp, state);
  455. break;
  456. default:
  457. assert (0);
  458. break;
  459. }
  460. }
  461. /**
  462. * Determine if the SU is hosted on the local node.
  463. * @param su
  464. *
  465. * @return int
  466. */
  467. int amf_su_is_local (struct amf_su *su)
  468. {
  469. if (name_match (&this_amf_node->name, &su->saAmfSUHostedByNode)) {
  470. return 1;
  471. } else {
  472. return 0;
  473. }
  474. }
  475. /**
  476. * Called by a component to report a suspected error on a component
  477. * @param su
  478. * @param comp
  479. * @param recommended_recovery
  480. */
  481. void amf_su_comp_error_suspected (
  482. struct amf_su *su,
  483. struct amf_comp *comp,
  484. SaAmfRecommendedRecoveryT recommended_recovery)
  485. {
  486. ENTER ("Comp '%s', SU '%s'", comp->name.value, su->name.value);
  487. switch (su->restart_control_state) {
  488. case SU_RC_ESCALATION_LEVEL_0:
  489. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  490. su->restart_control_state = SU_RC_ESCALATION_LEVEL_1;
  491. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  492. } else {
  493. comp_restart (comp);
  494. }
  495. break;
  496. case SU_RC_ESCALATION_LEVEL_1:
  497. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  498. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  499. su->restart_control_state = SU_RC_ESCALATION_LEVEL_2;
  500. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  501. } else {
  502. su_restart (comp->su);
  503. }
  504. } else {
  505. comp_restart (comp);
  506. }
  507. break;
  508. case SU_RC_ESCALATION_LEVEL_2:
  509. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  510. /*
  511. * TODO: delegate to node
  512. */
  513. SaNameT dn;
  514. su_operational_state_set (su, SA_AMF_OPERATIONAL_DISABLED);
  515. amf_comp_operational_state_set (
  516. comp, SA_AMF_OPERATIONAL_DISABLED);
  517. amf_comp_dn_make (comp, &dn);
  518. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  519. "action:\n\t\tSU failover", dn.value);
  520. amf_sg_failover_su_req (comp->su->sg, comp->su, this_amf_node);
  521. return;
  522. } else {
  523. su_restart (comp->su);
  524. }
  525. break;
  526. default:
  527. break;
  528. }
  529. }
  530. void amf_su_init (void)
  531. {
  532. log_init ("AMF");
  533. }
  534. void amf_su_terminate (struct amf_su *su)
  535. {
  536. struct amf_comp *comp;
  537. ENTER ("'%s'", su->name.value);
  538. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  539. /*
  540. * Terminate all components in SU abruptly
  541. */
  542. comp->error_suspected = 1;
  543. amf_comp_terminate (comp);
  544. }
  545. }
  546. char *amf_su_dn_make (struct amf_su *su, SaNameT *name)
  547. {
  548. int i;
  549. assert (su != NULL);
  550. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  551. "safSu=%s,safSg=%s,safApp=%s",
  552. su->name.value, su->sg->name.value, su->sg->application->name.value);
  553. assert (i <= SA_MAX_NAME_LENGTH);
  554. name->length = i;
  555. return (char *)name->value;
  556. }
  557. struct amf_si_assignment *amf_su_get_next_si_assignment (
  558. struct amf_su *su, const struct amf_si_assignment *si_assignment)
  559. {
  560. struct amf_si *si;
  561. struct amf_si_assignment *tmp_si_assignment;
  562. SaNameT dn;
  563. amf_su_dn_make (su, &dn);
  564. if (si_assignment == NULL) {
  565. assert (su->sg);
  566. assert (su->sg->application);
  567. assert (su->sg->application->si_head);
  568. si = su->sg->application->si_head;
  569. tmp_si_assignment = si->assigned_sis;
  570. } else {
  571. tmp_si_assignment = si_assignment->next;
  572. if (tmp_si_assignment == NULL) {
  573. si = si_assignment->si->next;
  574. if (si == NULL) {
  575. return NULL;
  576. } else {
  577. tmp_si_assignment = si->assigned_sis;
  578. }
  579. } else {
  580. si = tmp_si_assignment->si;
  581. }
  582. }
  583. for (; si != NULL; si = si->next) {
  584. if (tmp_si_assignment == NULL && si != NULL) {
  585. tmp_si_assignment = si->assigned_sis;
  586. }
  587. for (; tmp_si_assignment != NULL;
  588. tmp_si_assignment = tmp_si_assignment->next) {
  589. if (name_match (&tmp_si_assignment->name, &dn)) {
  590. return tmp_si_assignment;
  591. }
  592. }
  593. }
  594. return NULL;
  595. }
  596. void amf_su_foreach_si_assignment (
  597. struct amf_su *su,
  598. void (*foreach_fn)(struct amf_su *su,
  599. struct amf_si_assignment *si_assignment))
  600. {
  601. struct amf_si_assignment *si_assignment;
  602. assert (foreach_fn != NULL);
  603. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  604. while (si_assignment != NULL) {
  605. foreach_fn (su, si_assignment);
  606. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  607. }
  608. }
  609. int amf_su_get_saAmfSUNumCurrActiveSIs(struct amf_su *su)
  610. {
  611. int cnt = 0;
  612. struct amf_si_assignment *si_assignment;
  613. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  614. while (si_assignment != NULL) {
  615. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  616. si_assignment->requested_ha_state == SA_AMF_HA_ACTIVE) {
  617. cnt++;
  618. } else {
  619. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  620. cnt++;
  621. }
  622. }
  623. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  624. }
  625. return cnt;
  626. }
  627. int amf_su_get_saAmfSUNumCurrStandbySIs(struct amf_su *su)
  628. {
  629. int cnt = 0;
  630. struct amf_si_assignment *si_assignment;
  631. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  632. while (si_assignment != NULL) {
  633. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  634. si_assignment->requested_ha_state == SA_AMF_HA_STANDBY) {
  635. cnt++;
  636. } else {
  637. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  638. cnt++;
  639. }
  640. }
  641. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  642. }
  643. return cnt;
  644. }
  645. SaAmfReadinessStateT amf_su_get_saAmfSUReadinessState (struct amf_su *su)
  646. {
  647. if ((su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) &&
  648. ((su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATED) ||
  649. (su->saAmfSUPresenceState == SA_AMF_PRESENCE_RESTARTING))) {
  650. return SA_AMF_READINESS_IN_SERVICE;
  651. } else if (su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) {
  652. return SA_AMF_READINESS_STOPPING;
  653. } else {
  654. return SA_AMF_READINESS_OUT_OF_SERVICE;
  655. }
  656. }
  657. /**
  658. * Constructor for SU objects. Adds SU last in the ordered
  659. * list owned by the specified SG. Always returns a
  660. * valid SU object, out-of-memory problems are handled here.
  661. * Default values are initialized.
  662. * @param sg
  663. * @param name
  664. *
  665. * @return struct amf_su*
  666. */
  667. struct amf_su *amf_su_new (struct amf_sg *sg, char *name)
  668. {
  669. struct amf_su *tail = sg->su_head;
  670. struct amf_su *su = amf_calloc (1, sizeof (struct amf_su));
  671. while (tail != NULL) {
  672. if (tail->next == NULL) {
  673. break;
  674. }
  675. tail = tail->next;
  676. }
  677. if (tail == NULL) {
  678. sg->su_head = su;
  679. } else {
  680. tail->next = su;
  681. }
  682. su->sg = sg;
  683. /* setup default values from spec. */
  684. su->saAmfSURank = 0;
  685. su->saAmfSUIsExternal = 0;
  686. su->saAmfSUFailover = 1;
  687. su->saAmfSUAdminState = SA_AMF_ADMIN_UNLOCKED;
  688. su->saAmfSUOperState = SA_AMF_OPERATIONAL_DISABLED;
  689. su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  690. su->restart_control_state = SU_RC_ESCALATION_LEVEL_0;
  691. setSaNameT (&su->name, name);
  692. return su;
  693. }
  694. void amf_su_delete (struct amf_su *su)
  695. {
  696. struct amf_comp *comp;
  697. for (comp = su->comp_head; comp != NULL;) {
  698. struct amf_comp *tmp = comp;
  699. comp = comp->next;
  700. amf_comp_delete (tmp);
  701. }
  702. free (su);
  703. }
  704. void *amf_su_serialize (struct amf_su *su, int *len)
  705. {
  706. char *buf = NULL;
  707. int offset = 0, size = 0;
  708. TRACE8 ("%s", su->name.value);
  709. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->name);
  710. buf = amf_serialize_SaUint32T (buf, &size, &offset, su->saAmfSURank);
  711. buf = amf_serialize_SaUint32T (
  712. buf, &size, &offset, su->saAmfSUNumComponents);
  713. buf = amf_serialize_SaUint32T (
  714. buf, &size, &offset, su->saAmfSUIsExternal);
  715. buf = amf_serialize_SaUint32T (
  716. buf, &size, &offset, su->saAmfSUFailover);
  717. buf = amf_serialize_SaUint32T (
  718. buf, &size, &offset, su->saAmfSUPreInstantiable);
  719. buf = amf_serialize_SaUint32T (
  720. buf, &size, &offset, su->saAmfSUOperState);
  721. buf = amf_serialize_SaUint32T (
  722. buf, &size, &offset, su->saAmfSUAdminState);
  723. buf = amf_serialize_SaUint32T (
  724. buf, &size, &offset, su->saAmfSUPresenceState);
  725. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->saAmfSUHostedByNode);
  726. buf = amf_serialize_SaUint32T (
  727. buf, &size, &offset, su->saAmfSURestartCount);
  728. buf = amf_serialize_SaUint32T (
  729. buf, &size, &offset, su->restart_control_state);
  730. buf = amf_serialize_SaUint32T (
  731. buf, &size, &offset, su->escalation_level_history_state);
  732. buf = amf_serialize_SaStringT (
  733. buf, &size, &offset, su->clccli_path);
  734. buf = amf_serialize_SaUint32T (
  735. buf, &size, &offset, su->su_failover_cnt);
  736. *len = offset;
  737. return buf;
  738. }
  739. struct amf_su *amf_su_deserialize (struct amf_sg *sg, char *buf)
  740. {
  741. char *tmp = buf;
  742. struct amf_su *su = amf_su_new (sg, "");
  743. tmp = amf_deserialize_SaNameT (tmp, &su->name);
  744. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURank);
  745. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUNumComponents);
  746. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUIsExternal);
  747. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUFailover);
  748. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPreInstantiable);
  749. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUOperState);
  750. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUAdminState);
  751. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPresenceState);
  752. tmp = amf_deserialize_SaNameT (tmp, &su->saAmfSUHostedByNode);
  753. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURestartCount);
  754. tmp = amf_deserialize_SaUint32T (tmp, &su->restart_control_state);
  755. tmp = amf_deserialize_SaUint32T (tmp, &su->escalation_level_history_state);
  756. tmp = amf_deserialize_SaStringT (tmp, &su->clccli_path);
  757. tmp = amf_deserialize_SaUint32T (tmp, &su->su_failover_cnt);
  758. return su;
  759. }
  760. struct amf_su *amf_su_find (struct amf_cluster *cluster, SaNameT *name)
  761. {
  762. struct amf_application *app;
  763. struct amf_sg *sg;
  764. struct amf_su *su = NULL;
  765. char *app_name;
  766. char *sg_name;
  767. char *su_name;
  768. char *ptrptr;
  769. char *buf;
  770. assert (cluster != NULL && name != NULL);
  771. /* malloc new buffer since strtok_r writes to its first argument */
  772. buf = amf_malloc (name->length + 1);
  773. memcpy (buf, name->value, name->length + 1);
  774. su_name = strtok_r(buf, ",", &ptrptr);
  775. sg_name = strtok_r(NULL, ",", &ptrptr);
  776. app_name = strtok_r(NULL, ",", &ptrptr);
  777. if (su_name == NULL || sg_name == NULL || app_name == NULL) {
  778. goto end;
  779. }
  780. su_name += 6;
  781. sg_name += 6;
  782. app_name += 7;
  783. app = amf_application_find (cluster, app_name);
  784. if (app == NULL) {
  785. goto end;
  786. }
  787. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  788. if (strncmp (sg_name, (char*)sg->name.value,
  789. sg->name.length) == 0) {
  790. for (su = sg->su_head; su != NULL; su = su->next) {
  791. if (su->name.length == strlen(su_name) &&
  792. strncmp (su_name, (char*)su->name.value,
  793. su->name.length) == 0) {
  794. goto end;
  795. }
  796. }
  797. }
  798. }
  799. end:
  800. free (buf);
  801. return su;
  802. }