amfsi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /** @file amfsi.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. * - Component/SU restart, SU failover
  7. * - Constructors/destructors
  8. * - Serializers/deserializers
  9. *
  10. * All rights reserved.
  11. *
  12. *
  13. * This software licensed under BSD license, the text of which follows:
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  37. * THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * AMF Workload related classes Implementation
  40. *
  41. * This file contains functions for handling :
  42. * - AMF service instances(SI)
  43. * - AMF SI Dependency
  44. * - AMF SI Ranked SU
  45. * - AMF SI Assignment
  46. * - AMF component service instances (CSI)
  47. * - AMF CSI Assignment
  48. * - AMF CSI Type
  49. * - AMF CSI Attribute
  50. * The file can be viewed as the implementation of the classes listed above
  51. * as described in SAI-Overview-B.02.01. The SA Forum specification
  52. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  53. * and is referred to as 'the spec' below.
  54. *
  55. * The functions in this file are responsible for:
  56. * - calculating and storing an SI_dependency_level integer per SI
  57. * - calculating and storing a csi_dependency_level integer per CSI
  58. * - on request change HA state of an SI or CSI in such a way that the
  59. * requirements regarding SI -> SI dependencies (paragraphs 3.9.1.1 and
  60. * 3.9.1.2) and CSI -> CSI dependencies (paragraph 3.9.1.3) are fully
  61. * respected
  62. *
  63. * The si_dependency_level is an attribute calculated at init (in the future
  64. * also at reconfiguration) which indicates dependencies between SIs as
  65. * an integer. The si_dependency level indicates to which extent an SI depends
  66. * on other SIs such that an SI that depends on no other SI is on
  67. * si_dependecy_level == 1, an SI that depends only on an SI on
  68. * si_dependency_level == 1 is on si_dependency-level == 2.
  69. * An SI that depends on several SIs gets a si_dependency_level that is one
  70. * unit higher than the SI with the highest si_dependency_level it depends on.
  71. *
  72. * The csi_dependency_level attribute works the same way.
  73. *
  74. * According to paragraph 3.9.1 of the spec, a change to or from the ACTIVE
  75. * HA state is not always allowed without first deactivate dependent SI and CSI
  76. * assignments. Dependencies between CSIs are absolute while an SI that depends
  77. * on another SI may tolerate that the SI on which it depends is inactive for a
  78. * configurable time (the tolerance time). The consequence of this is that a
  79. * request to change the SI state may require a sequence of requests to
  80. * components to assume a new HA state for a CSI-assignment and to guarantee
  81. * the dependency rules, the active response from the component has to be
  82. * awaited before next HA state can be set.
  83. *
  84. * This file implements an SI state machine that fully implements these rules.
  85. * This state machine is called SI Dependency Control State Machine (dcsm)
  86. * and has the following states:
  87. * - DEACTIVATED (there is no SI-assignment with active HA state)
  88. * - ACTIVATING (a request to set the ACTIVE HA state has been received and
  89. * setting ACTIVE HA states to the appropriate components are
  90. * in progress)
  91. * - ACTIVATED (there is at least one SI-assignment with the ACTIVE HA-state)
  92. * - DEACTIVATING (a request to de-activate an SI or only a specific CSI
  93. * within an SI has been received and setting the QUISCED
  94. * HA states to the appropriate components are in progress)
  95. * - DEPENDENCY_DEACTIVATING (the SI-SI dependency tolerance timer has expired
  96. * and setting the QUISCED HA states to the
  97. * appropriate components are in progress)
  98. * - DEPENDENCY_DEACTIVATED (as state DEACTIVATED but will automatically
  99. * transition to state ACTIVATING when the
  100. * dependency problem is solved, i.e. the SI on
  101. * which it depends has re-assumed the ACTIVE HA
  102. * state)
  103. * - SETTING (a request to change the HA state when neither the existing
  104. * nor the requested state is ACTIVE)
  105. *
  106. * This file also implements:
  107. * - SI: Assignment state (for report purposes)
  108. * - SI Assignment: HA state
  109. * - CSI Assignment: HA state
  110. *
  111. */
  112. #include <assert.h>
  113. #include <stdio.h>
  114. #include "amf.h"
  115. #include "print.h"
  116. #include "util.h"
  117. #include "aispoll.h"
  118. #include "main.h"
  119. /**
  120. * Check that all CSI assignments belonging to an SI assignment
  121. * has been removed.
  122. * @param si_assignment
  123. *
  124. * @return int
  125. */
  126. static int all_csi_assignments_removed (amf_si_assignment_t *si_assignment)
  127. {
  128. amf_csi_assignment_t *csi_assignment;
  129. amf_csi_t *csi;
  130. int all_removed = 1;
  131. for (csi = si_assignment->si->csi_head; csi != NULL; csi = csi->next) {
  132. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  133. csi_assignment = csi_assignment->next) {
  134. /*
  135. * If the CSI assignment and the SI assignment belongs to the
  136. * same SU, we have a match and can request the component to
  137. * remove the CSI.
  138. */
  139. if (name_match (&csi_assignment->comp->su->name,
  140. &si_assignment->su->name)) {
  141. if (csi_assignment->requested_ha_state !=
  142. csi_assignment->saAmfCSICompHAState) {
  143. all_removed = 0;
  144. }
  145. }
  146. }
  147. }
  148. return all_removed;
  149. }
  150. /**
  151. * Check if any CSI assignment belonging to SU has the requested
  152. * state.
  153. * @param su
  154. * @param hastate
  155. *
  156. * @return int
  157. */
  158. static int any_csi_has_hastate_in_su (struct amf_su *su, SaAmfHAStateT hastate)
  159. {
  160. struct amf_comp *component;
  161. struct amf_csi_assignment *csi_assignment;
  162. int exist = 0;
  163. for (component = su->comp_head; component != NULL;
  164. component = component->next) {
  165. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  166. while (csi_assignment != NULL) {
  167. if (csi_assignment->saAmfCSICompHAState == hastate) {
  168. exist = 1;
  169. goto done;
  170. }
  171. csi_assignment =
  172. amf_comp_get_next_csi_assignment (component, csi_assignment);
  173. }
  174. }
  175. done:
  176. return exist;
  177. }
  178. /**
  179. * Check if all CSI assignments belonging to a
  180. * an SI assignemnt has the requested state.
  181. * @param su
  182. * @param hastate
  183. *
  184. * @return int
  185. */
  186. static int all_csi_has_hastate_for_si (
  187. struct amf_si_assignment *si_assignment, SaAmfHAStateT hastate)
  188. {
  189. struct amf_comp *component;
  190. struct amf_csi_assignment *tmp_csi_assignment;
  191. int all = 1;
  192. for (component = si_assignment->su->comp_head; component != NULL;
  193. component = component->next) {
  194. tmp_csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  195. while (tmp_csi_assignment != NULL) {
  196. if ((tmp_csi_assignment->si_assignment == si_assignment) &&
  197. (tmp_csi_assignment->saAmfCSICompHAState != hastate)) {
  198. all = 0;
  199. goto done;
  200. }
  201. tmp_csi_assignment =
  202. amf_comp_get_next_csi_assignment (component, tmp_csi_assignment);
  203. }
  204. }
  205. done:
  206. return all;
  207. }
  208. /**
  209. * Implements table 6 in 3.3.2.4
  210. * TODO: active & standby is not correct calculated acc. to
  211. * table. This knowledge is e.g. used in assign_si_assumed_cbfn
  212. * (sg.c)
  213. * @param csi_assignment
  214. */
  215. static void set_si_ha_state (struct amf_csi_assignment *csi_assignment)
  216. {
  217. SaAmfHAStateT old_ha_state =
  218. csi_assignment->si_assignment->saAmfSISUHAState;
  219. SaAmfAssignmentStateT old_assigment_state =
  220. amf_si_get_saAmfSIAssignmentState (csi_assignment->csi->si);
  221. if (all_csi_has_hastate_for_si (
  222. csi_assignment->si_assignment, SA_AMF_HA_ACTIVE)) {
  223. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_ACTIVE;
  224. }
  225. if (all_csi_has_hastate_for_si (
  226. csi_assignment->si_assignment, SA_AMF_HA_STANDBY)) {
  227. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_STANDBY;
  228. }
  229. if (any_csi_has_hastate_in_su (
  230. csi_assignment->comp->su, SA_AMF_HA_QUIESCING)) {
  231. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_QUIESCING;
  232. }
  233. if (any_csi_has_hastate_in_su (
  234. csi_assignment->comp->su, SA_AMF_HA_QUIESCED)) {
  235. csi_assignment->si_assignment->saAmfSISUHAState = SA_AMF_HA_QUIESCED;
  236. }
  237. /* log changes to HA state */
  238. if (old_ha_state != csi_assignment->si_assignment->saAmfSISUHAState) {
  239. log_printf (LOG_NOTICE, "SU HA state changed to '%s' for:\n"
  240. "\t\tSI '%s', SU '%s'",
  241. amf_ha_state (csi_assignment->si_assignment->saAmfSISUHAState),
  242. csi_assignment->si_assignment->si->name.value,
  243. csi_assignment->si_assignment->name.value);
  244. }
  245. /* log changes to assignment state */
  246. if (old_assigment_state !=
  247. amf_si_get_saAmfSIAssignmentState (csi_assignment->csi->si)) {
  248. log_printf (LOG_NOTICE, "SI Assignment state changed to '%s' for:\n"
  249. "\t\tSI '%s', SU '%s'",
  250. amf_assignment_state (
  251. amf_si_get_saAmfSIAssignmentState (csi_assignment->csi->si)),
  252. csi_assignment->si_assignment->si->name.value,
  253. csi_assignment->si_assignment->name.value);
  254. }
  255. }
  256. char *amf_csi_dn_make (struct amf_csi *csi, SaNameT *name)
  257. {
  258. int i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  259. "safCsi=%s,safSi=%s,safApp=%s",
  260. csi->name.value, csi->si->name.value,
  261. csi->si->application->name.value);
  262. assert (i <= SA_MAX_NAME_LENGTH);
  263. name->length = i;
  264. return(char *)name->value;
  265. }
  266. void amf_si_init (void)
  267. {
  268. log_init ("AMF");
  269. }
  270. void amf_si_comp_set_ha_state_done (
  271. struct amf_si *si, struct amf_csi_assignment *csi_assignment)
  272. {
  273. ENTER ("'%s', '%s'", si->name.value, csi_assignment->csi->name.value);
  274. set_si_ha_state (csi_assignment);
  275. assert (csi_assignment->si_assignment->assumed_callback_fn != NULL);
  276. /*
  277. * Report to caller when the requested SI assignment state is
  278. * confirmed.
  279. */
  280. if (csi_assignment->si_assignment->requested_ha_state ==
  281. csi_assignment->si_assignment->saAmfSISUHAState) {
  282. csi_assignment->si_assignment->assumed_callback_fn (
  283. csi_assignment->si_assignment, 0);
  284. csi_assignment->si_assignment->assumed_callback_fn = NULL;
  285. }
  286. }
  287. void amf_si_activate (
  288. struct amf_si *si,
  289. void (*activated_callback_fn)(struct amf_si *si, int result))
  290. {
  291. struct amf_csi *csi;
  292. ENTER ("'%s'", si->name.value);
  293. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  294. struct amf_csi_assignment *csi_assignment;
  295. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  296. csi_assignment = csi_assignment->next) {
  297. csi_assignment->si_assignment->requested_ha_state =
  298. SA_AMF_HA_ACTIVE;
  299. /*
  300. * TODO: only active assignments should be set when dependency
  301. * levels are used.
  302. */
  303. csi_assignment->requested_ha_state = SA_AMF_HA_ACTIVE;
  304. amf_comp_hastate_set (csi_assignment->comp, csi_assignment);
  305. }
  306. }
  307. }
  308. void amf_si_comp_set_ha_state_failed (
  309. struct amf_si *si, struct amf_csi_assignment *csi_assignment)
  310. {
  311. ENTER ("");
  312. assert (0);
  313. }
  314. static void timer_function_ha_state_assumed (void *_si_assignment)
  315. {
  316. struct amf_si_assignment *si_assignment = _si_assignment;
  317. ENTER ("");
  318. si_assignment->saAmfSISUHAState = si_assignment->requested_ha_state;
  319. si_assignment->assumed_callback_fn (si_assignment, 0);
  320. }
  321. void amf_si_ha_state_assume (
  322. struct amf_si_assignment *si_assignment,
  323. void (*assumed_ha_state_callback_fn)(struct amf_si_assignment *si_assignment,
  324. int result))
  325. {
  326. struct amf_csi_assignment *csi_assignment;
  327. struct amf_csi *csi;
  328. int csi_assignment_cnt = 0;
  329. int hastate_set_done_cnt = 0;
  330. ENTER ("SI '%s' SU '%s' state %s", si_assignment->si->name.value,
  331. si_assignment->su->name.value,
  332. amf_ha_state (si_assignment->requested_ha_state));
  333. si_assignment->assumed_callback_fn = assumed_ha_state_callback_fn;
  334. for (csi = si_assignment->si->csi_head; csi != NULL; csi = csi->next) {
  335. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  336. csi_assignment = csi_assignment->next) {
  337. /*
  338. * If the CSI assignment and the SI assignment belongs to the
  339. * same SU, we have a match and can request the component to
  340. * change HA state.
  341. */
  342. if (name_match (&csi_assignment->comp->su->name,
  343. &si_assignment->su->name) &&
  344. (csi_assignment->saAmfCSICompHAState !=
  345. si_assignment->requested_ha_state)) {
  346. csi_assignment_cnt++;
  347. csi_assignment->requested_ha_state =
  348. si_assignment->requested_ha_state;
  349. amf_comp_hastate_set (csi_assignment->comp, csi_assignment);
  350. if (csi_assignment->saAmfCSICompHAState ==
  351. csi_assignment->requested_ha_state) {
  352. hastate_set_done_cnt++;
  353. }
  354. }
  355. }
  356. }
  357. /*
  358. * If the SU has only one component which is the faulty one, we
  359. * will not get an asynchronous response from the component.
  360. * This response (amf_si_comp_set_ha_state_done) is used to do
  361. * the next state transition. The asynchronous response is
  362. * simulated using a timeout instead.
  363. */
  364. if (csi_assignment_cnt == hastate_set_done_cnt) {
  365. poll_timer_handle handle;
  366. poll_timer_add (aisexec_poll_handle, 0, si_assignment,
  367. timer_function_ha_state_assumed, &handle);
  368. }
  369. }
  370. /**
  371. * Get number of active assignments for the specified SI
  372. * @param si
  373. *
  374. * @return int
  375. */
  376. int amf_si_get_saAmfSINumCurrActiveAssignments (struct amf_si *si)
  377. {
  378. int cnt = 0;
  379. struct amf_si_assignment *si_assignment;
  380. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  381. si_assignment = si_assignment->next) {
  382. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  383. cnt++;
  384. }
  385. }
  386. return cnt;
  387. }
  388. int amf_si_su_get_saAmfSINumCurrActiveAssignments (struct amf_si *si,
  389. struct amf_su *su)
  390. {
  391. int cnt = 0;
  392. struct amf_si_assignment *si_assignment;
  393. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  394. si_assignment = si_assignment->next) {
  395. if (si_assignment->su == su &&
  396. si_assignment->saAmfSISUHAState == SA_AMF_HA_ACTIVE) {
  397. cnt++;
  398. }
  399. }
  400. return cnt;
  401. }
  402. int amf_si_get_saAmfSINumCurrStandbyAssignments (struct amf_si *si)
  403. {
  404. int cnt = 0;
  405. struct amf_si_assignment *si_assignment;
  406. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  407. si_assignment = si_assignment->next) {
  408. if (si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  409. cnt++;
  410. }
  411. }
  412. return cnt;
  413. }
  414. int amf_si_su_get_saAmfSINumCurrStandbyAssignments (struct amf_si *si,
  415. struct amf_su *su)
  416. {
  417. int cnt = 0;
  418. struct amf_si_assignment *si_assignment;
  419. for (si_assignment = si->assigned_sis; si_assignment != NULL;
  420. si_assignment = si_assignment->next) {
  421. if (si_assignment->su == su &&
  422. si_assignment->saAmfSISUHAState == SA_AMF_HA_STANDBY) {
  423. cnt++;
  424. }
  425. }
  426. return cnt;
  427. }
  428. SaAmfAssignmentStateT amf_si_get_saAmfSIAssignmentState (struct amf_si *si)
  429. {
  430. if ((amf_si_get_saAmfSINumCurrActiveAssignments (si) ==
  431. si->saAmfSIPrefActiveAssignments) &&
  432. (amf_si_get_saAmfSINumCurrStandbyAssignments (si) ==
  433. si->saAmfSIPrefStandbyAssignments)) {
  434. return SA_AMF_ASSIGNMENT_FULLY_ASSIGNED;
  435. } else if (amf_si_get_saAmfSINumCurrActiveAssignments (si) == 0) {
  436. return SA_AMF_ASSIGNMENT_UNASSIGNED;
  437. } else {
  438. return SA_AMF_ASSIGNMENT_PARTIALLY_ASSIGNED;
  439. }
  440. }
  441. void amf_csi_delete_assignments (struct amf_csi *csi, struct amf_su *su)
  442. {
  443. struct amf_csi_assignment *csi_assignment;
  444. ENTER ("'%s'", su->name.value);
  445. struct amf_csi_assignment **prev = &csi->assigned_csis;
  446. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  447. csi_assignment = csi_assignment->next) {
  448. if (csi_assignment->comp->su == su) {
  449. struct amf_csi_assignment *tmp = csi_assignment;
  450. *prev = csi_assignment->next;
  451. dprintf ("CSI assignment %s unlinked", tmp->name.value);
  452. free (tmp);
  453. } else {
  454. prev = &csi_assignment->next;
  455. }
  456. }
  457. }
  458. /**
  459. * Constructor for SI objects. Adds SI last in the ordered
  460. * list owned by the specified application. Always returns a
  461. * valid SI object, out-of-memory problems are handled here.
  462. * Default values are initialized.
  463. * @param app
  464. *
  465. * @return struct amf_si*
  466. */
  467. struct amf_si *amf_si_new (struct amf_application *app, char *name)
  468. {
  469. struct amf_si *tail = app->si_head;
  470. struct amf_si *si = amf_calloc (1, sizeof (struct amf_si));
  471. while (tail != NULL) {
  472. if (tail->next == NULL) {
  473. break;
  474. }
  475. tail = tail->next;
  476. }
  477. if (tail == NULL) {
  478. app->si_head = si;
  479. } else {
  480. tail->next = si;
  481. }
  482. si->application = app;
  483. /* setup default values from spec. */
  484. si->saAmfSIAdminState = SA_AMF_ADMIN_UNLOCKED;
  485. si->saAmfSIRank = 0;
  486. si->saAmfSIPrefActiveAssignments = 1;
  487. si->saAmfSIPrefStandbyAssignments = 1;
  488. si->assigned_sis = NULL;
  489. si->csi_head = NULL;
  490. setSaNameT (&si->name, name);
  491. return si;
  492. }
  493. void amf_si_delete (struct amf_si *si)
  494. {
  495. struct amf_si_assignment *si_assignment;
  496. struct amf_csi *csi;
  497. for (csi = si->csi_head; csi != NULL;) {
  498. struct amf_csi *tmp = csi;
  499. csi = csi->next;
  500. amf_csi_delete (tmp);
  501. }
  502. for (si_assignment = si->assigned_sis; si_assignment != NULL;) {
  503. struct amf_si_assignment *tmp = si_assignment;
  504. si_assignment = si_assignment->next;
  505. free (tmp);
  506. }
  507. free (si);
  508. }
  509. void *amf_si_serialize (struct amf_si *si, int *len)
  510. {
  511. char *buf = NULL;
  512. int offset = 0, size = 0;
  513. TRACE8 ("%s", si->name.value);
  514. buf = amf_serialize_SaNameT (buf, &size, &offset, &si->name);
  515. buf = amf_serialize_SaNameT (buf, &size, &offset,
  516. &si->saAmfSIProtectedbySG);
  517. buf = amf_serialize_SaUint32T (buf, &size, &offset, si->saAmfSIRank);
  518. buf = amf_serialize_SaUint32T (buf, &size, &offset, si->saAmfSINumCSIs);
  519. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  520. si->saAmfSIPrefActiveAssignments);
  521. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  522. si->saAmfSIPrefStandbyAssignments);
  523. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  524. si->saAmfSIAdminState);
  525. *len = offset;
  526. return buf;
  527. }
  528. struct amf_si *amf_si_deserialize (struct amf_application *app, char *buf)
  529. {
  530. char *tmp = buf;
  531. struct amf_si *si = amf_si_new (app, "");
  532. tmp = amf_deserialize_SaNameT (tmp, &si->name);
  533. tmp = amf_deserialize_SaNameT (tmp, &si->saAmfSIProtectedbySG);
  534. tmp = amf_deserialize_SaUint32T (tmp, &si->saAmfSIRank);
  535. tmp = amf_deserialize_SaUint32T (tmp, &si->saAmfSINumCSIs);
  536. tmp = amf_deserialize_SaUint32T (tmp, &si->saAmfSIPrefActiveAssignments);
  537. tmp = amf_deserialize_SaUint32T (tmp, &si->saAmfSIPrefStandbyAssignments);
  538. tmp = amf_deserialize_SaUint32T (tmp, &si->saAmfSIAdminState);
  539. return si;
  540. }
  541. /*****************************************************************************
  542. * SI Assignment class implementation *
  543. ****************************************************************************/
  544. struct amf_si_assignment *amf_si_assignment_new (struct amf_si *si)
  545. {
  546. struct amf_si_assignment *si_assignment =
  547. amf_calloc (1, sizeof (struct amf_si_assignment));
  548. si_assignment->si = si;
  549. si_assignment->next = si->assigned_sis;
  550. si->assigned_sis = si_assignment;
  551. return si_assignment;
  552. }
  553. void *amf_si_assignment_serialize (
  554. amf_si_assignment_t *si_assignment, int *len)
  555. {
  556. char *buf = NULL;
  557. int offset = 0, size = 0;
  558. TRACE8 ("%s", si_assignment->name.value);
  559. buf = amf_serialize_SaNameT (buf, &size, &offset, &si_assignment->name);
  560. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  561. si_assignment->saAmfSISUHAState);
  562. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  563. si_assignment->requested_ha_state);
  564. *len = offset;
  565. return buf;
  566. }
  567. struct amf_si_assignment *amf_si_assignment_deserialize (
  568. struct amf_si *si, char *buf)
  569. {
  570. char *tmp = buf;
  571. struct amf_si_assignment *si_assignment = amf_si_assignment_new (si);
  572. tmp = amf_deserialize_SaNameT (tmp, &si_assignment->name);
  573. tmp = amf_deserialize_SaUint32T (tmp, &si_assignment->saAmfSISUHAState);
  574. tmp = amf_deserialize_SaUint32T (tmp, &si_assignment->requested_ha_state);
  575. si_assignment->su = amf_su_find (si->application->cluster,
  576. &si_assignment->name);
  577. return si_assignment;
  578. }
  579. struct amf_si *amf_si_find (struct amf_application *app, char *name)
  580. {
  581. struct amf_si *si;
  582. for (si = app->si_head; si != NULL; si = si->next) {
  583. if (si->name.length == strlen(name) &&
  584. strncmp (name, (char*)si->name.value, si->name.length) == 0) {
  585. break;
  586. }
  587. }
  588. if (si == NULL) {
  589. dprintf ("SI %s not found!", name);
  590. }
  591. return si;
  592. }
  593. /*****************************************************************************
  594. * CSI class implementation *
  595. ****************************************************************************/
  596. struct amf_csi *amf_csi_new (struct amf_si *si)
  597. {
  598. struct amf_csi *csi = amf_calloc (1, sizeof (struct amf_csi));
  599. csi->si = si;
  600. csi->next = si->csi_head;
  601. si->csi_head = csi;
  602. return csi;
  603. }
  604. void amf_csi_delete (struct amf_csi *csi)
  605. {
  606. struct amf_csi_assignment *csi_assignment;
  607. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;) {
  608. struct amf_csi_assignment *tmp = csi_assignment;
  609. csi_assignment = csi_assignment->next;
  610. free (tmp);
  611. }
  612. free (csi);
  613. }
  614. void *amf_csi_serialize (struct amf_csi *csi, int *len)
  615. {
  616. char *buf = NULL;
  617. int offset = 0, size = 0;
  618. TRACE8 ("%s", csi->name.value);
  619. buf = amf_serialize_SaNameT (buf, &size, &offset, &csi->name);
  620. buf = amf_serialize_SaNameT (buf, &size, &offset, &csi->saAmfCSTypeName);
  621. *len = offset;
  622. return buf;
  623. }
  624. struct amf_csi *amf_csi_deserialize (struct amf_si *si, char *buf)
  625. {
  626. char *tmp = buf;
  627. struct amf_csi *csi = amf_csi_new (si);
  628. tmp = amf_deserialize_SaNameT (tmp, &csi->name);
  629. tmp = amf_deserialize_SaNameT (tmp, &csi->saAmfCSTypeName);
  630. return csi;
  631. }
  632. struct amf_csi *amf_csi_find (struct amf_si *si, char *name)
  633. {
  634. struct amf_csi *csi;
  635. for (csi = si->csi_head; csi != NULL; csi = csi->next) {
  636. if (csi->name.length == strlen(name) &&
  637. strncmp (name, (char*)csi->name.value, csi->name.length) == 0) {
  638. break;
  639. }
  640. }
  641. if (csi == NULL) {
  642. dprintf ("CSI %s not found!", name);
  643. }
  644. return csi;
  645. }
  646. /*****************************************************************************
  647. * CSI Assignment class implementation *
  648. ****************************************************************************/
  649. struct amf_csi_assignment *amf_csi_assignment_new (struct amf_csi *csi)
  650. {
  651. struct amf_csi_assignment *csi_assignment =
  652. amf_calloc (1, sizeof (struct amf_csi_assignment));
  653. csi_assignment->csi = csi;
  654. csi_assignment->next = csi->assigned_csis;
  655. csi->assigned_csis = csi_assignment;
  656. return csi_assignment;
  657. }
  658. void *amf_csi_assignment_serialize (
  659. struct amf_csi_assignment *csi_assignment, int *len)
  660. {
  661. char *buf = NULL;
  662. int offset = 0, size = 0;
  663. TRACE8 ("%s", csi_assignment->name.value);
  664. buf = amf_serialize_SaNameT (buf, &size, &offset, &csi_assignment->name);
  665. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  666. csi_assignment->saAmfCSICompHAState);
  667. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  668. csi_assignment->requested_ha_state);
  669. *len = offset;
  670. return buf;
  671. }
  672. struct amf_si_assignment *si_assignment_find (
  673. struct amf_csi_assignment *csi_assignment)
  674. {
  675. struct amf_comp *component;
  676. struct amf_si_assignment *si_assignment = NULL;
  677. component = amf_comp_find(csi_assignment->csi->si->application->cluster,
  678. &csi_assignment->name);
  679. for (si_assignment = csi_assignment->csi->si->assigned_sis;
  680. si_assignment != NULL; si_assignment = si_assignment->next) {
  681. SaNameT su_name;
  682. amf_su_dn_make (component->su,&su_name);
  683. if (name_match(&su_name, &si_assignment->name)) {
  684. break;
  685. }
  686. }
  687. return si_assignment;
  688. }
  689. struct amf_csi_assignment *amf_csi_assignment_deserialize (
  690. struct amf_csi *csi, char *buf)
  691. {
  692. char *tmp = buf;
  693. struct amf_csi_assignment *csi_assignment = amf_csi_assignment_new (csi);
  694. tmp = amf_deserialize_SaNameT (tmp, &csi_assignment->name);
  695. tmp = amf_deserialize_SaUint32T (tmp,
  696. &csi_assignment->saAmfCSICompHAState);
  697. tmp = amf_deserialize_SaUint32T (tmp, &csi_assignment->requested_ha_state);
  698. csi_assignment->comp = amf_comp_find (csi->si->application->cluster,
  699. &csi_assignment->name);
  700. assert (csi_assignment->comp != NULL);
  701. csi_assignment->si_assignment = si_assignment_find(csi_assignment);
  702. return csi_assignment;
  703. }
  704. char *amf_csi_assignment_dn_make (
  705. struct amf_csi_assignment *csi_assignment, SaNameT *name)
  706. {
  707. SaNameT comp_name;
  708. struct amf_csi *csi = csi_assignment->csi;
  709. int i;
  710. amf_comp_dn_make (csi_assignment->comp, &comp_name);
  711. i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  712. "safCSIComp=%s,safCsi=%s,safSi=%s,safApp=%s",
  713. comp_name.value,
  714. csi->name.value, csi->si->name.value,
  715. csi->si->application->name.value);
  716. assert (i <= SA_MAX_NAME_LENGTH);
  717. name->length = i;
  718. return(char *)name->value;
  719. }
  720. struct amf_csi_assignment *amf_csi_assignment_find (
  721. struct amf_cluster *cluster, SaNameT *name)
  722. {
  723. struct amf_application *app;
  724. struct amf_si *si;
  725. struct amf_csi *csi;
  726. struct amf_csi_assignment *csi_assignment = NULL;
  727. char *app_name;
  728. char *si_name;
  729. char *csi_name;
  730. char *csi_assignment_name;
  731. char *buf;
  732. /* malloc new buffer since we need to write to the buffer */
  733. buf = amf_malloc (name->length + 1);
  734. memcpy (buf, name->value, name->length + 1);
  735. csi_assignment_name = strstr (buf, "safCSIComp=");
  736. csi_name = strstr (buf, "safCsi=");
  737. si_name = strstr (buf, "safSi=");
  738. app_name = strstr (buf, "safApp=");
  739. app_name++;
  740. app_name = strstr (app_name, "safApp=");
  741. if (csi_assignment_name == NULL || csi_name == NULL || si_name == NULL ||
  742. app_name == NULL) {
  743. goto end;
  744. }
  745. *(csi_name - 1) = '\0';
  746. *(si_name - 1) = '\0';
  747. *(app_name - 1) = '\0';
  748. /* jump to value */
  749. csi_assignment_name += 11;
  750. csi_name += 7;
  751. si_name += 6;
  752. app_name += 7;
  753. app = amf_application_find (cluster, app_name);
  754. if (app == NULL) {
  755. goto end;
  756. }
  757. si = amf_si_find (app, si_name);
  758. if (si == NULL) {
  759. goto end;
  760. }
  761. csi = amf_csi_find (si, csi_name);
  762. if (csi == NULL) {
  763. goto end;
  764. }
  765. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  766. csi_assignment = csi_assignment->next) {
  767. if (csi_assignment->name.length == strlen(csi_assignment_name) &&
  768. strncmp (csi_assignment_name,
  769. (char*)csi_assignment->name.value,
  770. csi_assignment->name.length) == 0) {
  771. goto end;
  772. }
  773. }
  774. end:
  775. assert(csi_assignment);
  776. free (buf);
  777. return csi_assignment;
  778. }
  779. struct amf_csi_attribute *amf_csi_attribute_new (struct amf_csi *csi)
  780. {
  781. struct amf_csi_attribute *csi_attribute =
  782. amf_calloc (1, sizeof (struct amf_csi_assignment));
  783. csi_attribute->next = csi->attributes_head;
  784. csi->attributes_head = csi_attribute;
  785. return csi_attribute;
  786. }
  787. void *amf_csi_attribute_serialize (
  788. struct amf_csi_attribute *csi_attribute, int *len)
  789. {
  790. char *buf = NULL;
  791. int i, offset = 0, size = 0;
  792. TRACE8 ("%s", csi_attribute->name);
  793. buf = amf_serialize_SaStringT (buf, &size, &offset, csi_attribute->name);
  794. /* count value and write to buf */
  795. for (i = 0; csi_attribute->value &&
  796. csi_attribute->value[i] != NULL; i++);
  797. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  798. for (i = 0; csi_attribute->value &&
  799. csi_attribute->value[i] != NULL; i++) {
  800. buf = amf_serialize_SaStringT (
  801. buf, &size, &offset, csi_attribute->value[i]);
  802. }
  803. *len = offset;
  804. return buf;
  805. }
  806. struct amf_csi_attribute *amf_csi_attribute_deserialize (
  807. struct amf_csi *csi, char *buf)
  808. {
  809. char *tmp = buf;
  810. struct amf_csi_attribute *csi_attribute;
  811. int i;
  812. SaUint32T cnt;
  813. csi_attribute = amf_csi_attribute_new (csi);
  814. tmp = amf_deserialize_SaStringT (tmp, &csi_attribute->name);
  815. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  816. csi_attribute->value = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  817. for (i = 0; i < cnt; i++) {
  818. tmp = amf_deserialize_SaStringT (tmp, &csi_attribute->value[i]);
  819. }
  820. csi_attribute->value[i] = NULL;
  821. return csi_attribute;
  822. }
  823. void amf_si_assignment_remove (amf_si_assignment_t *si_assignment,
  824. async_func_t async_func)
  825. {
  826. struct amf_csi_assignment *csi_assignment;
  827. struct amf_csi *csi;
  828. int csi_assignment_cnt = 0;
  829. ENTER ("SI '%s' SU '%s' state %s", si_assignment->si->name.value,
  830. si_assignment->su->name.value,
  831. amf_ha_state (si_assignment->requested_ha_state));
  832. si_assignment->requested_ha_state = USR_AMF_HA_STATE_REMOVED;
  833. si_assignment->removed_callback_fn = async_func;
  834. for (csi = si_assignment->si->csi_head; csi != NULL; csi = csi->next) {
  835. for (csi_assignment = csi->assigned_csis; csi_assignment != NULL;
  836. csi_assignment = csi_assignment->next) {
  837. /*
  838. * If the CSI assignment and the SI assignment belongs to the
  839. * same SU, we have a match and can request the component to
  840. * remove the CSI.
  841. */
  842. if (name_match (&csi_assignment->comp->su->name,
  843. &si_assignment->su->name)) {
  844. csi_assignment_cnt++;
  845. csi_assignment->requested_ha_state = USR_AMF_HA_STATE_REMOVED;
  846. amf_comp_csi_remove (csi_assignment->comp, csi_assignment);
  847. }
  848. }
  849. }
  850. /*
  851. * If the SU has only one component which is the faulty one, we
  852. * will not get an asynchronous response from the component.
  853. * This response (amf_si_comp_set_ha_state_done) is used to do
  854. * the next state transition. The asynchronous response is
  855. * simulated using a timeout instead.
  856. */
  857. if (csi_assignment_cnt == 0) {
  858. amf_call_function_asynchronous (async_func, si_assignment);
  859. }
  860. }
  861. void amf_si_comp_csi_removed (
  862. struct amf_si *si, struct amf_csi_assignment *csi_assignment,
  863. SaAisErrorT error)
  864. {
  865. ENTER ("'%s', '%s'", si->name.value, csi_assignment->csi->name.value);
  866. assert (csi_assignment->si_assignment->removed_callback_fn != NULL);
  867. csi_assignment->saAmfCSICompHAState = USR_AMF_HA_STATE_REMOVED;
  868. /*
  869. * Report to caller when all requested CSI assignments has
  870. * been removed.
  871. */
  872. if (all_csi_assignments_removed(csi_assignment->si_assignment)) {
  873. csi_assignment->si_assignment->removed_callback_fn (
  874. csi_assignment->si_assignment);
  875. }
  876. }