amfsu.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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_1:
  287. case SU_RC_ESCALATION_LEVEL_2:
  288. /*
  289. * TODO: send to node
  290. */
  291. case SU_RC_ESCALATION_LEVEL_0:
  292. if (amf_su_presence_state_all_comps_in_su_are_set (
  293. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  294. su_presence_state_set (
  295. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  296. }
  297. break;
  298. case SU_RC_RESTART_COMP_RESTARTING:
  299. su->restart_control_state = SU_RC_RESTART_COMP_SETTING;
  300. reassign_sis (comp->su);
  301. break;
  302. case SU_RC_RESTART_SU_INSTANTIATING:
  303. if (amf_su_presence_state_all_comps_in_su_are_set (
  304. comp->su, SA_AMF_PRESENCE_INSTANTIATED)) {
  305. su->restart_control_state = SU_RC_RESTART_SU_SETTING;
  306. su_presence_state_set (
  307. comp->su, SA_AMF_PRESENCE_INSTANTIATED);
  308. reassign_sis (comp->su);
  309. }
  310. break;
  311. default:
  312. dprintf ("state %d", su->restart_control_state);
  313. assert (0);
  314. break;
  315. }
  316. break;
  317. case SA_AMF_PRESENCE_UNINSTANTIATED:
  318. if (amf_su_presence_state_all_comps_in_su_are_set (
  319. su, SA_AMF_PRESENCE_UNINSTANTIATED)) {
  320. su_presence_state_set (comp->su,SA_AMF_PRESENCE_UNINSTANTIATED);
  321. }
  322. break;
  323. case SA_AMF_PRESENCE_INSTANTIATING:
  324. su_presence_state_set (comp->su,SA_AMF_PRESENCE_INSTANTIATING);
  325. break;
  326. case SA_AMF_PRESENCE_RESTARTING:
  327. break;
  328. case SA_AMF_PRESENCE_TERMINATING:
  329. break;
  330. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  331. su_presence_state_set (
  332. comp->su, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  333. break;
  334. default:
  335. assert (0);
  336. break;
  337. }
  338. }
  339. static void su_comp_op_state_changed (
  340. struct amf_su *su, struct amf_comp *comp, int state)
  341. {
  342. ENTER ("'%s', '%s'", su->name.value, comp->name.value);
  343. switch (state) {
  344. case SA_AMF_OPERATIONAL_ENABLED:
  345. {
  346. struct amf_comp *comp_compare;
  347. int all_set = 1;
  348. for (comp_compare = comp->su->comp_head;
  349. comp_compare != NULL; comp_compare = comp_compare->next) {
  350. if (comp_compare->saAmfCompOperState !=
  351. SA_AMF_OPERATIONAL_ENABLED) {
  352. all_set = 0;
  353. break;
  354. }
  355. }
  356. if (all_set) {
  357. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_ENABLED);
  358. } else {
  359. su_operational_state_set (comp->su, SA_AMF_OPERATIONAL_DISABLED);
  360. }
  361. break;
  362. }
  363. case SA_AMF_OPERATIONAL_DISABLED:
  364. break;
  365. default:
  366. assert (0);
  367. break;
  368. }
  369. }
  370. int amf_su_presence_state_all_comps_in_su_are_set (struct amf_su *su,
  371. SaAmfPresenceStateT state)
  372. {
  373. int all_set = 1;
  374. struct amf_comp *comp;
  375. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  376. if (comp->saAmfCompPresenceState != state) {
  377. all_set = 0;
  378. break;
  379. }
  380. }
  381. return all_set;
  382. }
  383. void amf_su_instantiate (struct amf_su *su)
  384. {
  385. struct amf_comp *comp;
  386. ENTER ("'%s'", su->name.value);
  387. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  388. amf_comp_instantiate (comp);
  389. }
  390. }
  391. amf_si_assignment_t *amf_su_assign_si (struct amf_su *su, struct amf_si *si,
  392. SaAmfHAStateT ha_state)
  393. {
  394. struct amf_si_assignment *si_assignment;
  395. dprintf ("Creating SI '%s' to SU '%s' with hastate %s\n",
  396. getSaNameT (&si->name), getSaNameT (&su->name),
  397. amf_ha_state (ha_state));
  398. si_assignment = amf_malloc (sizeof (struct amf_si_assignment));
  399. amf_su_dn_make (su, &si_assignment->name);
  400. si_assignment->saAmfSISUHAState = 0; /* undefined confirmed HA state */
  401. si_assignment->requested_ha_state = ha_state;
  402. si_assignment->next = si->assigned_sis;
  403. si->assigned_sis = si_assignment;
  404. si_assignment->si = si;
  405. si_assignment->su = su;
  406. {
  407. struct amf_csi *csi;
  408. struct amf_comp *comp;
  409. SaNameT *cs_type;
  410. int i;
  411. /*
  412. ** for each component in SU, find a CSI in the SI with the same type
  413. */
  414. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  415. int no_of_cs_types = 0;
  416. for (i = 0; comp->saAmfCompCsTypes[i]; i++) {
  417. cs_type = comp->saAmfCompCsTypes[i];
  418. no_of_cs_types++;
  419. int no_of_assignments = 0;
  420. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  421. if (!memcmp(csi->saAmfCSTypeName.value, cs_type->value,
  422. cs_type->length)) {
  423. comp_assign_csi (comp, csi, si_assignment, ha_state);
  424. no_of_assignments++;
  425. }
  426. }
  427. if (no_of_assignments == 0) {
  428. log_printf (
  429. LOG_WARNING, "\t No CSIs of type %s configured?!!\n",
  430. getSaNameT (cs_type));
  431. }
  432. }
  433. if (no_of_cs_types == 0) {
  434. log_printf (LOG_LEVEL_ERROR,
  435. "\t No CS types configured for comp %s ?!!\n",
  436. getSaNameT (&comp->name));
  437. }
  438. }
  439. }
  440. return si_assignment;
  441. }
  442. /**
  443. * Used by a component to report a state change event
  444. * @param su
  445. * @param comp
  446. * @param type type of state
  447. * @param state new state
  448. */
  449. void amf_su_comp_state_changed (
  450. struct amf_su *su, struct amf_comp *comp, SaAmfStateT type, int state)
  451. {
  452. switch (type) {
  453. case SA_AMF_PRESENCE_STATE:
  454. su_comp_presence_state_changed (su, comp, state);
  455. break;
  456. case SA_AMF_OP_STATE:
  457. su_comp_op_state_changed (su, comp, state);
  458. break;
  459. default:
  460. assert (0);
  461. break;
  462. }
  463. }
  464. /**
  465. * Determine if the SU is hosted on the local node.
  466. * @param su
  467. *
  468. * @return int
  469. */
  470. int amf_su_is_local (struct amf_su *su)
  471. {
  472. if (name_match (&this_amf_node->name, &su->saAmfSUHostedByNode)) {
  473. return 1;
  474. } else {
  475. return 0;
  476. }
  477. }
  478. /**
  479. * Called by a component to report a suspected error on a component
  480. * @param su
  481. * @param comp
  482. * @param recommended_recovery
  483. */
  484. void amf_su_comp_error_suspected (
  485. struct amf_su *su,
  486. struct amf_comp *comp,
  487. SaAmfRecommendedRecoveryT recommended_recovery)
  488. {
  489. ENTER ("Comp '%s', SU '%s'", comp->name.value, su->name.value);
  490. switch (su->restart_control_state) {
  491. case SU_RC_ESCALATION_LEVEL_0:
  492. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  493. su->restart_control_state = SU_RC_ESCALATION_LEVEL_1;
  494. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  495. } else {
  496. comp_restart (comp);
  497. }
  498. break;
  499. case SU_RC_ESCALATION_LEVEL_1:
  500. if (comp->saAmfCompRestartCount >= su->sg->saAmfSGCompRestartMax) {
  501. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  502. su->restart_control_state = SU_RC_ESCALATION_LEVEL_2;
  503. amf_su_comp_error_suspected (su, comp, recommended_recovery);
  504. } else {
  505. su_restart (comp->su);
  506. }
  507. } else {
  508. comp_restart (comp);
  509. }
  510. break;
  511. case SU_RC_ESCALATION_LEVEL_2:
  512. if (su->saAmfSURestartCount >= su->sg->saAmfSGSuRestartMax) {
  513. /*
  514. * TODO: delegate to node
  515. */
  516. SaNameT dn;
  517. su_operational_state_set (su, SA_AMF_OPERATIONAL_DISABLED);
  518. amf_comp_operational_state_set (
  519. comp, SA_AMF_OPERATIONAL_DISABLED);
  520. amf_comp_dn_make (comp, &dn);
  521. log_printf (LOG_NOTICE, "Error detected for '%s', recovery "
  522. "action:\n\t\tSU failover", dn.value);
  523. amf_sg_failover_su_req (comp->su->sg, comp->su, this_amf_node);
  524. return;
  525. } else {
  526. su_restart (comp->su);
  527. }
  528. break;
  529. default:
  530. dprintf ("TODO Restarting probably, on monday");
  531. dprintf ("restart_control_state = %d",
  532. su->restart_control_state);
  533. break;
  534. }
  535. }
  536. void amf_su_init (void)
  537. {
  538. log_init ("AMF");
  539. }
  540. void amf_su_terminate (struct amf_su *su)
  541. {
  542. struct amf_comp *comp;
  543. ENTER ("'%s'", su->name.value);
  544. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  545. /*
  546. * Terminate all components in SU abruptly
  547. */
  548. comp->error_suspected = 1;
  549. amf_comp_terminate (comp);
  550. }
  551. }
  552. char *amf_su_dn_make (struct amf_su *su, SaNameT *name)
  553. {
  554. int i;
  555. assert (su != NULL);
  556. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  557. "safSu=%s,safSg=%s,safApp=%s",
  558. su->name.value, su->sg->name.value, su->sg->application->name.value);
  559. assert (i <= SA_MAX_NAME_LENGTH);
  560. name->length = i;
  561. return (char *)name->value;
  562. }
  563. struct amf_si_assignment *amf_su_get_next_si_assignment (
  564. struct amf_su *su, const struct amf_si_assignment *si_assignment)
  565. {
  566. struct amf_si *si;
  567. struct amf_si_assignment *tmp_si_assignment;
  568. SaNameT dn;
  569. amf_su_dn_make (su, &dn);
  570. if (si_assignment == NULL) {
  571. assert (su->sg);
  572. assert (su->sg->application);
  573. assert (su->sg->application->si_head);
  574. si = su->sg->application->si_head;
  575. tmp_si_assignment = si->assigned_sis;
  576. } else {
  577. tmp_si_assignment = si_assignment->next;
  578. if (tmp_si_assignment == NULL) {
  579. si = si_assignment->si->next;
  580. if (si == NULL) {
  581. return NULL;
  582. } else {
  583. tmp_si_assignment = si->assigned_sis;
  584. }
  585. } else {
  586. si = tmp_si_assignment->si;
  587. }
  588. }
  589. for (; si != NULL; si = si->next) {
  590. if (tmp_si_assignment == NULL && si != NULL) {
  591. tmp_si_assignment = si->assigned_sis;
  592. }
  593. for (; tmp_si_assignment != NULL;
  594. tmp_si_assignment = tmp_si_assignment->next) {
  595. if (name_match (&tmp_si_assignment->name, &dn)) {
  596. return tmp_si_assignment;
  597. }
  598. }
  599. }
  600. return NULL;
  601. }
  602. void amf_su_foreach_si_assignment (
  603. struct amf_su *su,
  604. void (*foreach_fn)(struct amf_su *su,
  605. struct amf_si_assignment *si_assignment))
  606. {
  607. struct amf_si_assignment *si_assignment;
  608. assert (foreach_fn != NULL);
  609. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  610. while (si_assignment != NULL) {
  611. foreach_fn (su, si_assignment);
  612. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  613. }
  614. }
  615. int amf_su_get_saAmfSUNumCurrActiveSIs(struct amf_su *su)
  616. {
  617. int cnt = 0;
  618. struct amf_si_assignment *si_assignment;
  619. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  620. while (si_assignment != NULL) {
  621. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  622. si_assignment->requested_ha_state == SA_AMF_HA_ACTIVE) {
  623. cnt++;
  624. } else {
  625. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  626. cnt++;
  627. }
  628. }
  629. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  630. }
  631. return cnt;
  632. }
  633. int amf_su_get_saAmfSUNumCurrStandbySIs(struct amf_su *su)
  634. {
  635. int cnt = 0;
  636. struct amf_si_assignment *si_assignment;
  637. si_assignment = amf_su_get_next_si_assignment (su, NULL);
  638. while (si_assignment != NULL) {
  639. if (su->sg->avail_state == SG_AC_AssigningOnRequest &&
  640. si_assignment->requested_ha_state == SA_AMF_HA_STANDBY) {
  641. cnt++;
  642. } else {
  643. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  644. cnt++;
  645. }
  646. }
  647. si_assignment = amf_su_get_next_si_assignment (su, si_assignment);
  648. }
  649. return cnt;
  650. }
  651. SaAmfReadinessStateT amf_su_get_saAmfSUReadinessState (struct amf_su *su)
  652. {
  653. if ((su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) &&
  654. ((su->saAmfSUPresenceState == SA_AMF_PRESENCE_INSTANTIATED) ||
  655. (su->saAmfSUPresenceState == SA_AMF_PRESENCE_RESTARTING))) {
  656. return SA_AMF_READINESS_IN_SERVICE;
  657. } else if (su->saAmfSUOperState == SA_AMF_OPERATIONAL_ENABLED) {
  658. return SA_AMF_READINESS_STOPPING;
  659. } else {
  660. return SA_AMF_READINESS_OUT_OF_SERVICE;
  661. }
  662. }
  663. /**
  664. * Constructor for SU objects. Adds SU last in the ordered
  665. * list owned by the specified SG. Always returns a
  666. * valid SU object, out-of-memory problems are handled here.
  667. * Default values are initialized.
  668. * @param sg
  669. * @param name
  670. *
  671. * @return struct amf_su*
  672. */
  673. struct amf_su *amf_su_new (struct amf_sg *sg, char *name)
  674. {
  675. struct amf_su *tail = sg->su_head;
  676. struct amf_su *su = amf_calloc (1, sizeof (struct amf_su));
  677. while (tail != NULL) {
  678. if (tail->next == NULL) {
  679. break;
  680. }
  681. tail = tail->next;
  682. }
  683. if (tail == NULL) {
  684. sg->su_head = su;
  685. } else {
  686. tail->next = su;
  687. }
  688. su->sg = sg;
  689. /* setup default values from spec. */
  690. su->saAmfSURank = 0;
  691. su->saAmfSUIsExternal = 0;
  692. su->saAmfSUFailover = 1;
  693. su->saAmfSUAdminState = SA_AMF_ADMIN_UNLOCKED;
  694. su->saAmfSUOperState = SA_AMF_OPERATIONAL_DISABLED;
  695. su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  696. su->restart_control_state = SU_RC_ESCALATION_LEVEL_0;
  697. setSaNameT (&su->name, name);
  698. return su;
  699. }
  700. void amf_su_delete (struct amf_su *su)
  701. {
  702. struct amf_comp *comp;
  703. for (comp = su->comp_head; comp != NULL;) {
  704. struct amf_comp *tmp = comp;
  705. comp = comp->next;
  706. amf_comp_delete (tmp);
  707. }
  708. free (su);
  709. }
  710. void *amf_su_serialize (struct amf_su *su, int *len)
  711. {
  712. char *buf = NULL;
  713. int offset = 0, size = 0;
  714. TRACE8 ("%s", su->name.value);
  715. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->name);
  716. buf = amf_serialize_SaUint32T (buf, &size, &offset, su->saAmfSURank);
  717. buf = amf_serialize_SaUint32T (
  718. buf, &size, &offset, su->saAmfSUNumComponents);
  719. buf = amf_serialize_SaUint32T (
  720. buf, &size, &offset, su->saAmfSUIsExternal);
  721. buf = amf_serialize_SaUint32T (
  722. buf, &size, &offset, su->saAmfSUFailover);
  723. buf = amf_serialize_SaUint32T (
  724. buf, &size, &offset, su->saAmfSUPreInstantiable);
  725. buf = amf_serialize_SaUint32T (
  726. buf, &size, &offset, su->saAmfSUOperState);
  727. buf = amf_serialize_SaUint32T (
  728. buf, &size, &offset, su->saAmfSUAdminState);
  729. buf = amf_serialize_SaUint32T (
  730. buf, &size, &offset, su->saAmfSUPresenceState);
  731. buf = amf_serialize_SaNameT (buf, &size, &offset, &su->saAmfSUHostedByNode);
  732. buf = amf_serialize_SaUint32T (
  733. buf, &size, &offset, su->saAmfSURestartCount);
  734. buf = amf_serialize_SaUint32T (
  735. buf, &size, &offset, su->restart_control_state);
  736. buf = amf_serialize_SaUint32T (
  737. buf, &size, &offset, su->escalation_level_history_state);
  738. buf = amf_serialize_SaStringT (
  739. buf, &size, &offset, su->clccli_path);
  740. buf = amf_serialize_SaUint32T (
  741. buf, &size, &offset, su->su_failover_cnt);
  742. *len = offset;
  743. return buf;
  744. }
  745. struct amf_su *amf_su_deserialize (struct amf_sg *sg, char *buf)
  746. {
  747. char *tmp = buf;
  748. struct amf_su *su = amf_su_new (sg, "");
  749. tmp = amf_deserialize_SaNameT (tmp, &su->name);
  750. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURank);
  751. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUNumComponents);
  752. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUIsExternal);
  753. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUFailover);
  754. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPreInstantiable);
  755. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUOperState);
  756. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUAdminState);
  757. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSUPresenceState);
  758. tmp = amf_deserialize_SaNameT (tmp, &su->saAmfSUHostedByNode);
  759. tmp = amf_deserialize_SaUint32T (tmp, &su->saAmfSURestartCount);
  760. tmp = amf_deserialize_SaUint32T (tmp, &su->restart_control_state);
  761. tmp = amf_deserialize_SaUint32T (tmp, &su->escalation_level_history_state);
  762. tmp = amf_deserialize_SaStringT (tmp, &su->clccli_path);
  763. tmp = amf_deserialize_SaUint32T (tmp, &su->su_failover_cnt);
  764. return su;
  765. }
  766. struct amf_su *amf_su_find (struct amf_cluster *cluster, SaNameT *name)
  767. {
  768. struct amf_application *app;
  769. struct amf_sg *sg;
  770. struct amf_su *su = NULL;
  771. char *app_name;
  772. char *sg_name;
  773. char *su_name;
  774. char *ptrptr;
  775. char *buf;
  776. assert (cluster != NULL && name != NULL);
  777. /* malloc new buffer since strtok_r writes to its first argument */
  778. buf = amf_malloc (name->length + 1);
  779. memcpy (buf, name->value, name->length + 1);
  780. su_name = strtok_r(buf, ",", &ptrptr);
  781. sg_name = strtok_r(NULL, ",", &ptrptr);
  782. app_name = strtok_r(NULL, ",", &ptrptr);
  783. if (su_name == NULL || sg_name == NULL || app_name == NULL) {
  784. goto end;
  785. }
  786. su_name += 6;
  787. sg_name += 6;
  788. app_name += 7;
  789. app = amf_application_find (cluster, app_name);
  790. if (app == NULL) {
  791. goto end;
  792. }
  793. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  794. if (strncmp (sg_name, (char*)sg->name.value,
  795. sg->name.length) == 0) {
  796. for (su = sg->su_head; su != NULL; su = su->next) {
  797. if (su->name.length == strlen(su_name) &&
  798. strncmp (su_name, (char*)su->name.value,
  799. su->name.length) == 0) {
  800. goto end;
  801. }
  802. }
  803. }
  804. }
  805. end:
  806. free (buf);
  807. return su;
  808. }