amfcomp.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. /** @file amfcomp.c
  2. *
  3. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. * Copyright (c) 2006 Ericsson AB.
  6. *
  7. * All rights reserved.
  8. *
  9. * Author: Steven Dake (sdake@mvista.com)
  10. *
  11. * Author: Hans Feldt, Anders Eriksson, Lars Holm
  12. * - Introduced AMF B.02 information model
  13. * - Use DN in API and multicast messages
  14. * - (Re-)Introduction of event based multicast messages
  15. * - Refactoring of code into several AMF files
  16. * - Component/SU restart, SU failover
  17. * - Constructors/destructors
  18. * - Serializers/deserializers
  19. *
  20. * This software licensed under BSD license, the text of which follows:
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions are met:
  24. *
  25. * - Redistributions of source code must retain the above copyright notice,
  26. * this list of conditions and the following disclaimer.
  27. * - Redistributions in binary form must reproduce the above copyright notice,
  28. * this list of conditions and the following disclaimer in the documentation
  29. * and/or other materials provided with the distribution.
  30. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  31. * contributors may be used to endorse or promote products derived from this
  32. * software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  38. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  39. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  40. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  41. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  42. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  44. * THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. * AMF Component Class Implementation
  47. *
  48. * This file contains functions for handling AMF-components. It can be
  49. * viewed as the implementation of the AMF Component class (called comp)
  50. * as described in SAI-Overview-B.02.01. The SA Forum specification
  51. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  52. * and is referred to as 'the spec' below.
  53. *
  54. * The functions in this file are responsible for handling the following
  55. * types of components:
  56. * - sa-aware components
  57. * (proxy or non-proxy)
  58. * - non-sa-aware components
  59. * (non-proxied non-pre-instantiable and
  60. * proxied pre-instantiable or not pre-instantiable)
  61. *
  62. * The functions of this file are also responsible for:
  63. * - handling all communication with the AMF API library supported by the
  64. * AMF main function, see below
  65. * - instantiating and terminating components upon request
  66. * - updating the ha-state of the CSI-assignment related to the component
  67. * - initiating an error report to the parent SU
  68. * - handling all run time attributes of the AMF Component; all cached
  69. * attributes are stored as variables and sent to the IMM service
  70. * upon the changes described in the specification.
  71. *
  72. * Incoming events from the AMF library is primarily handled by the AMF
  73. * main function which:
  74. * <1> transforms the incoming event to an event that is multicast
  75. * to all AMF service instances in the cluster
  76. * <2> the event received from multicast is tranformed to a function
  77. * call of the external interface of comp
  78. *
  79. * Outgoing events to the AMF library is handled by static functions called
  80. * lib_<api callback function name>_request which creates an invocation handle
  81. * unique to this call and stores any variables comp want to associate to the
  82. * call back so it is possible to pick them up when the component responses
  83. * through the API. Finally, a timer is started to supervise that a response
  84. * really is received.
  85. *
  86. * Comp initiates error reports to its parent SU in the cases described in
  87. * paragraph 3.3.2.2 in the spec. Comp delegates all actions to SU except
  88. * - it stores the received or pre-configured recommended recovery
  89. * action
  90. * - sets the operational state to DISABLED unless the
  91. * recommended recovery action was SA_AMF_COMP_RESTART. (In this case
  92. * SU or node may set operational state of the component later on
  93. * when it has been fully investigated that no escallation to a
  94. * more powerful recovery action shall be made.)
  95. *
  96. * Comp contains the following state machines:
  97. * - presence state machine (PRSM)
  98. * - operational state machine (OPSM)
  99. * - readiness state machine (RESM)
  100. * - ha state per component service instance (CSI)
  101. *
  102. * The behaviour of comp is mainly controlled by the presence state machine,
  103. * while the operational and readiness state machines are used only to report
  104. * information to its parent (service unit SU) and management (IMM). Comp does
  105. * not control the logic to assign a CSI to itself and neither to decide the
  106. * value of the ha-state but only to faciltate the communication of the CSI
  107. * set (or remove) order and to evaluate the response from the library.
  108. *
  109. * The presence state machine implements all the states described in the
  110. * specification.
  111. * The '-ING' states of PRSM are designed as composite states (UML terminology).
  112. * Being a composite state means that the state contains substates.
  113. * PRSM composite states are:
  114. * - TERMINATING (TERMINATE and CLEANUP)
  115. * - INSTANTIATING (INSTANTIATE, INSTANTIATEDELAY and CLEANUP)
  116. * - RESTARTING (TERMINATE, INSTANTIATE, INSTANTIATEDELAY and CLEANUP)
  117. *
  118. * The reason for introducing these composite states is to make it easier to
  119. * understand the implementation of the behaviour described in paragraphs
  120. * 4.1 - 4.6 in the spec. The comp PRSM implements all the logic described
  121. * except for node reboot, which is handled by the AMF Node class.
  122. * Also PRSM reports all changes of state to its parent SU.
  123. *
  124. */
  125. #include <sys/types.h>
  126. #include <unistd.h>
  127. #include <sys/wait.h>
  128. #include <stdio.h>
  129. #include <string.h>
  130. #include <stdlib.h>
  131. #include <errno.h>
  132. #include <assert.h>
  133. #include "../include/saAis.h"
  134. #include "../include/saAmf.h"
  135. #include "../include/ipc_gen.h"
  136. #include "../include/ipc_amf.h"
  137. #include "totempg.h"
  138. #include "timer.h"
  139. #include "ipc.h"
  140. #include "service.h"
  141. #include "util.h"
  142. #include "amf.h"
  143. #include "print.h"
  144. #include "main.h"
  145. enum clc_command_run_operation_type {
  146. CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE = 1,
  147. CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE = 2,
  148. CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP = 3
  149. };
  150. struct clc_command_run_data {
  151. struct amf_comp *comp;
  152. enum clc_command_run_operation_type type;
  153. void (*completion_callback) (void *context);
  154. };
  155. struct clc_interface {
  156. int (*instantiate) (struct amf_comp *comp);
  157. int (*terminate) (struct amf_comp *comp);
  158. int (*cleanup) (struct amf_comp *comp);
  159. };
  160. struct csi_remove_callback_data {
  161. struct amf_csi *csi;
  162. };
  163. struct component_terminate_callback_data {
  164. struct amf_comp *comp;
  165. };
  166. static void comp_presence_state_set (
  167. struct amf_comp *comp,
  168. SaAmfPresenceStateT presence_state);
  169. static int clc_cli_instantiate (struct amf_comp *comp);
  170. static int clc_instantiate_callback (struct amf_comp *comp);
  171. static int clc_csi_set_callback (struct amf_comp *comp);
  172. static int clc_cli_terminate (struct amf_comp *comp);
  173. static int lib_comp_terminate_request (struct amf_comp *comp);
  174. static int clc_csi_remove_callback (struct amf_comp *comp);
  175. static int clc_cli_cleanup (struct amf_comp *comp);
  176. static int clc_cli_cleanup_local (struct amf_comp *comp);
  177. static void healthcheck_deactivate (struct amf_healthcheck *healthcheck_active);
  178. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck);
  179. static void timer_function_healthcheck_tmo (void *_healthcheck);
  180. static void lib_csi_set_request (
  181. struct amf_comp *comp,
  182. struct amf_csi_assignment *csi_assignment);
  183. /*
  184. * Life cycle functions
  185. */
  186. static struct clc_interface clc_interface_sa_aware = {
  187. clc_cli_instantiate,
  188. lib_comp_terminate_request,
  189. clc_cli_cleanup
  190. };
  191. static struct clc_interface clc_interface_proxied_pre = {
  192. clc_instantiate_callback,
  193. lib_comp_terminate_request,
  194. clc_cli_cleanup
  195. };
  196. static struct clc_interface clc_interface_proxied_non_pre = {
  197. clc_csi_set_callback,
  198. clc_csi_remove_callback,
  199. clc_cli_cleanup_local
  200. };
  201. static struct clc_interface clc_interface_non_proxied_non_saware = {
  202. clc_cli_instantiate,
  203. clc_cli_terminate,
  204. clc_cli_cleanup_local
  205. };
  206. static struct clc_interface *clc_interfaces[4] = {
  207. &clc_interface_sa_aware,
  208. &clc_interface_proxied_pre,
  209. &clc_interface_proxied_non_pre,
  210. &clc_interface_non_proxied_non_saware
  211. };
  212. struct invocation {
  213. void *data;
  214. int interface;
  215. int active;
  216. };
  217. static struct invocation *invocation_entries = 0;
  218. static int invocation_entries_size = 0;
  219. static int invocation_create (
  220. int interface,
  221. void *data)
  222. {
  223. struct invocation *invocation_addr = 0;
  224. struct invocation *invocation_temp;
  225. int i;
  226. int loc = 0;
  227. for (i = 0; i < invocation_entries_size; i++) {
  228. if (invocation_entries[i].active == 0) {
  229. invocation_addr = &invocation_entries[i];
  230. loc = i;
  231. break;
  232. }
  233. }
  234. if (invocation_addr == 0) {
  235. invocation_temp = (struct invocation *)realloc (invocation_entries,
  236. (invocation_entries_size + 1) * sizeof (struct invocation));
  237. if (invocation_temp == NULL) {
  238. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  239. }
  240. invocation_entries = invocation_temp;
  241. invocation_addr = &invocation_entries[invocation_entries_size];
  242. loc = invocation_entries_size;
  243. invocation_entries_size += 1;
  244. }
  245. invocation_addr->interface = interface;
  246. invocation_addr->data = data;
  247. invocation_addr->active = 1;
  248. return (loc);
  249. }
  250. static int invocation_get_and_destroy (
  251. SaUint64T invocation, unsigned int *interface, void **data)
  252. {
  253. if (invocation > invocation_entries_size) {
  254. return (-1);
  255. }
  256. if (invocation_entries[invocation].active == 0) {
  257. return (-1);
  258. }
  259. *interface = invocation_entries[invocation].interface;
  260. *data = invocation_entries[invocation].data;
  261. memset (&invocation_entries[invocation], 0, sizeof (struct invocation));
  262. return (0);
  263. }
  264. static void invocation_destroy_by_data (void *data)
  265. {
  266. int i;
  267. for (i = 0; i < invocation_entries_size; i++) {
  268. if (invocation_entries[i].data == data) {
  269. memset (&invocation_entries[i], 0,
  270. sizeof (struct invocation));
  271. break;
  272. }
  273. }
  274. }
  275. /**
  276. * Set suspected error flag and report to SU.
  277. *
  278. * @param comp
  279. * @param recommended_recovery
  280. */
  281. static void report_error_suspected (
  282. struct amf_comp *comp,
  283. SaAmfRecommendedRecoveryT recommended_recovery)
  284. {
  285. comp->error_suspected = 1;
  286. amf_su_comp_error_suspected (
  287. comp->su, comp, recommended_recovery);
  288. }
  289. #ifndef xprintf
  290. #define xprintf(...)
  291. #endif
  292. static void *clc_command_run (void *context)
  293. {
  294. struct clc_command_run_data *clc_command_run_data =
  295. (struct clc_command_run_data *)context;
  296. pid_t pid;
  297. int res;
  298. char **argv = NULL;
  299. char **envp = NULL;
  300. int status;
  301. char path[PATH_MAX];
  302. char *cmd = 0;
  303. char *comp_argv = 0;
  304. char comp_name[SA_MAX_NAME_LENGTH + 24];
  305. int i;
  306. int argv_size;
  307. int envp_size;
  308. ENTER_VOID();
  309. pid = fork();
  310. if (pid == -1) {
  311. fprintf (stderr, "Couldn't fork process %s\n", strerror (errno));
  312. return (0);
  313. }
  314. if (pid) {
  315. xprintf ("waiting for pid %d to finish\n", pid);
  316. waitpid (pid, &status, 0);
  317. if (WIFEXITED (status) != 0 && WEXITSTATUS(status) != 0) {
  318. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  319. " %d - %s\n", pid, WEXITSTATUS(status),
  320. strerror (WEXITSTATUS(status)));
  321. /*
  322. * TODO: remove this and handle properly later...
  323. */
  324. openais_exit_error (AIS_DONE_FATAL_ERR);
  325. }
  326. if (WIFSIGNALED (status) != 0) {
  327. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  328. " %d\n", pid, WTERMSIG(status));
  329. /*
  330. * TODO: remove this and handle properly later...
  331. */
  332. openais_exit_error (AIS_DONE_FATAL_ERR);
  333. }
  334. xprintf ("process (%d) finished with %x\n", pid, status);
  335. if (clc_command_run_data->completion_callback) {
  336. clc_command_run_data->completion_callback (context);
  337. }
  338. pthread_exit(0);
  339. }
  340. switch (clc_command_run_data->type) {
  341. case CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE:
  342. cmd = clc_command_run_data->comp->saAmfCompInstantiateCmd;
  343. comp_argv = clc_command_run_data->comp->saAmfCompInstantiateCmdArgv;
  344. break;
  345. case CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE:
  346. cmd = clc_command_run_data->comp->saAmfCompTerminateCmd;
  347. comp_argv = clc_command_run_data->comp->saAmfCompTerminateCmdArgv;
  348. break;
  349. case CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP:
  350. cmd = clc_command_run_data->comp->saAmfCompCleanupCmd;
  351. comp_argv = clc_command_run_data->comp->saAmfCompCleanupCmdArgv;
  352. break;
  353. default:
  354. assert (0 != 1);
  355. break;
  356. }
  357. /* If command is not an absolute path, search for paths in parent objects */
  358. if (cmd[0] != '/') {
  359. if (clc_command_run_data->comp->clccli_path != NULL) {
  360. sprintf (path, "%s/%s",
  361. clc_command_run_data->comp->clccli_path, cmd);
  362. } else if (clc_command_run_data->comp->su->clccli_path != NULL) {
  363. sprintf (path, "%s/%s",
  364. clc_command_run_data->comp->su->clccli_path, cmd);
  365. } else if (clc_command_run_data->comp->su->sg->clccli_path != NULL) {
  366. sprintf (path, "%s/%s",
  367. clc_command_run_data->comp->su->sg->clccli_path, cmd);
  368. } else if (clc_command_run_data->comp->su->sg->application->clccli_path != NULL) {
  369. sprintf (path, "%s/%s",
  370. clc_command_run_data->comp->su->sg->application->clccli_path, cmd);
  371. }
  372. cmd = path;
  373. }
  374. argv_size = 2;
  375. argv = amf_malloc (sizeof (char*) * argv_size);
  376. argv[0] = cmd;
  377. {
  378. /* make a proper argv array */
  379. i = 1;
  380. char *ptrptr;
  381. char *arg = strtok_r(comp_argv, " ", &ptrptr);
  382. while (arg) {
  383. argv_size++;
  384. argv = realloc (argv, sizeof (char*) * argv_size);
  385. if (argv == NULL) {
  386. fprintf (stderr, "out-of-memory");
  387. exit (-1);
  388. }
  389. argv[i] = arg;
  390. arg = strtok_r(NULL, " ", &ptrptr);
  391. i++;
  392. }
  393. }
  394. argv[i] = NULL;
  395. i = snprintf (comp_name, SA_MAX_NAME_LENGTH,
  396. "SA_AMF_COMPONENT_NAME=safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  397. clc_command_run_data->comp->name.value,
  398. clc_command_run_data->comp->su->name.value,
  399. clc_command_run_data->comp->su->sg->name.value,
  400. clc_command_run_data->comp->su->sg->application->name.value);
  401. assert (i <= sizeof (comp_name));
  402. /* two is for component name and NULL termination */
  403. envp_size = 2;
  404. envp = amf_malloc (sizeof (char*) * envp_size);
  405. envp[0] = comp_name;
  406. for (i = 1; clc_command_run_data->comp->saAmfCompCmdEnv &&
  407. clc_command_run_data->comp->saAmfCompCmdEnv[i - 1]; i++) {
  408. envp_size++;
  409. envp = realloc (envp, sizeof (char*) * envp_size);
  410. if (envp == NULL) {
  411. fprintf (stderr, "out-of-memory");
  412. exit (-1);
  413. }
  414. envp[i] = clc_command_run_data->comp->saAmfCompCmdEnv[i - 1];
  415. }
  416. envp[i] = NULL;
  417. xprintf ("running command '%s' with environment (%d):\n", cmd, envp_size);
  418. for (i = 0; envp[i] != NULL; i++) {
  419. xprintf (" %s\n", envp[i]);
  420. }
  421. xprintf (" and argv (%d):\n", argv_size);
  422. for (i = 0; argv[i] != NULL; i++) {
  423. xprintf (" %s\n", argv[i]);
  424. }
  425. res = execve (cmd, argv, envp);
  426. if (res == -1) {
  427. fprintf (stderr, "Couldn't exec program %s (%s)\n",
  428. cmd, strerror (errno));
  429. }
  430. exit (res); /* abnormal exit of forked process */
  431. return (0);
  432. }
  433. static void amf_comp_instantiate_tmo (void *component)
  434. {
  435. SaNameT compName;
  436. amf_comp_dn_make (component, &compName);
  437. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE_TMO,
  438. &compName, sizeof (SaNameT));
  439. }
  440. static void start_component_instantiate_timer (struct amf_comp *component)
  441. {
  442. poll_timer_add (aisexec_poll_handle,
  443. component->saAmfCompInstantiateTimeout,
  444. component,
  445. amf_comp_instantiate_tmo,
  446. &component->instantiate_timeout_handle);
  447. }
  448. /*
  449. * Instantiate possible operations
  450. */
  451. static int clc_cli_instantiate (struct amf_comp *comp)
  452. {
  453. int res;
  454. pthread_t thread;
  455. pthread_attr_t thread_attr; /* thread attribute */
  456. struct clc_command_run_data *clc_command_run_data;
  457. ENTER("comp '%s'\n", getSaNameT (&comp->name));
  458. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  459. clc_command_run_data->comp = comp;
  460. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE;
  461. clc_command_run_data->completion_callback = NULL;
  462. pthread_attr_init (&thread_attr);
  463. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  464. res = pthread_create (&thread, &thread_attr, clc_command_run,
  465. (void *)clc_command_run_data);
  466. if (res != 0) {
  467. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  468. }
  469. start_component_instantiate_timer (comp);
  470. // clc_command_run_data->completion_callback (clc_command_run_data);
  471. // TODO error code from pthread_create
  472. return (res);
  473. }
  474. static int clc_instantiate_callback (struct amf_comp *comp)
  475. {
  476. ENTER("comp %s\n", getSaNameT (&comp->name));
  477. return (0);
  478. }
  479. static int clc_csi_set_callback (struct amf_comp *comp)
  480. {
  481. ENTER("comp %s\n", getSaNameT (&comp->name));
  482. return (0);
  483. }
  484. /*
  485. * Terminate possible operations
  486. */
  487. static int clc_cli_terminate (struct amf_comp *comp)
  488. {
  489. ENTER("comp %s\n", getSaNameT (&comp->name));
  490. return (0);
  491. }
  492. /**
  493. * Request component to terminate itself
  494. * @param comp
  495. *
  496. * @return int
  497. */
  498. static int lib_comp_terminate_request (struct amf_comp *comp)
  499. {
  500. struct res_lib_amf_componentterminatecallback res_lib;
  501. struct component_terminate_callback_data *component_terminate_callback_data;
  502. ENTER("comp %s\n", getSaNameT (&comp->name));
  503. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTTERMINATECALLBACK;
  504. res_lib.header.size = sizeof (struct res_lib_amf_componentterminatecallback);
  505. res_lib.header.error = SA_AIS_OK;
  506. memcpy (&res_lib.compName, &comp->name, sizeof (SaNameT));
  507. component_terminate_callback_data =
  508. amf_malloc (sizeof (struct component_terminate_callback_data));
  509. component_terminate_callback_data->comp = comp;
  510. res_lib.invocation =
  511. invocation_create (
  512. AMF_RESPONSE_COMPONENTTERMINATECALLBACK,
  513. component_terminate_callback_data);
  514. openais_conn_send_response (
  515. openais_conn_partner_get (comp->conn),
  516. &res_lib,
  517. sizeof (struct res_lib_amf_componentterminatecallback));
  518. return (0);
  519. }
  520. static int clc_csi_remove_callback (struct amf_comp *comp)
  521. {
  522. dprintf ("clc_tcsi_remove_callback\n");
  523. return (0);
  524. }
  525. /*
  526. * Clean up completed
  527. */
  528. static void mcast_cleanup_completion_event (void *context)
  529. {
  530. struct clc_command_run_data *clc_command_run_data =
  531. (struct clc_command_run_data *)context;
  532. struct req_exec_amf_clc_cleanup_completed req;
  533. struct iovec iovec;
  534. req.header.size = sizeof (struct req_exec_amf_clc_cleanup_completed);
  535. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  536. MESSAGE_REQ_EXEC_AMF_CLC_CLEANUP_COMPLETED);
  537. amf_comp_dn_make (clc_command_run_data->comp, &req.compName);
  538. iovec.iov_base = (char *)&req;
  539. iovec.iov_len = sizeof (req);
  540. assert (totempg_groups_mcast_joined (openais_group_handle,
  541. &iovec, 1, TOTEMPG_AGREED) == 0);
  542. }
  543. /*
  544. * Cleanup possible operations
  545. */
  546. static int clc_cli_cleanup (struct amf_comp *comp)
  547. {
  548. int res;
  549. pthread_t thread;
  550. pthread_attr_t thread_attr; /* thread attribute */
  551. struct clc_command_run_data *clc_command_run_data;
  552. dprintf ("clc_cli_cleanup\n");
  553. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  554. clc_command_run_data->comp = comp;
  555. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP;
  556. clc_command_run_data->completion_callback = mcast_cleanup_completion_event;
  557. pthread_attr_init (&thread_attr);
  558. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  559. res = pthread_create (&thread, &thread_attr, clc_command_run,
  560. (void *)clc_command_run_data);
  561. if (res != 0) {
  562. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  563. }
  564. // TODO error code from pthread_create
  565. return (res);
  566. }
  567. static int clc_cli_cleanup_local (struct amf_comp *comp)
  568. {
  569. dprintf ("clc_cli_cleanup_local\n");
  570. return (0);
  571. }
  572. #if 0
  573. static int clc_terminate (struct amf_comp *comp)
  574. {
  575. int res;
  576. dprintf ("clc terminate for comp %s\n", getSaNameT (&comp->name));
  577. assert (0);
  578. operational_state_comp_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  579. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  580. res = clc_interfaces[comp->comptype]->terminate (comp);
  581. return (0);
  582. }
  583. #endif
  584. char *amf_comp_dn_make (struct amf_comp *comp, SaNameT *name)
  585. {
  586. int i = snprintf ((char*) name->value, SA_MAX_NAME_LENGTH,
  587. "safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  588. comp->name.value, comp->su->name.value,
  589. comp->su->sg->name.value, comp->su->sg->application->name.value);
  590. assert (i <= SA_MAX_NAME_LENGTH);
  591. name->length = i;
  592. return (char *)name->value;
  593. }
  594. struct amf_healthcheck *amf_comp_find_healthcheck (
  595. struct amf_comp *comp, SaAmfHealthcheckKeyT *key)
  596. {
  597. struct amf_healthcheck *healthcheck;
  598. struct amf_healthcheck *ret_healthcheck = 0;
  599. if (key == NULL) {
  600. return NULL;
  601. }
  602. for (healthcheck = comp->healthcheck_head;
  603. healthcheck != NULL;
  604. healthcheck = healthcheck->next) {
  605. if (key->keyLen == (healthcheck->safHealthcheckKey).keyLen) {
  606. if (memcmp (key->key,
  607. (healthcheck->safHealthcheckKey).key,
  608. key->keyLen) == 0) {
  609. ret_healthcheck = healthcheck;
  610. break;
  611. }
  612. }
  613. }
  614. return (ret_healthcheck);
  615. }
  616. /**
  617. * Constructor for component objects. Adds component last in
  618. * the list owned by the specified SU. Always returns a
  619. * valid comp object, out-of-memory problems are handled
  620. * here. Default values are initialized.
  621. * @param su
  622. * @param name
  623. *
  624. * @return struct amf_comp*
  625. */
  626. struct amf_comp *amf_comp_new(struct amf_su *su, char *name)
  627. {
  628. struct amf_comp *tail = su->comp_head;
  629. struct amf_comp *comp = amf_calloc (1, sizeof (struct amf_comp));
  630. while (tail != NULL) {
  631. if (tail->next == NULL) {
  632. break;
  633. }
  634. tail = tail->next;
  635. }
  636. if (tail == NULL) {
  637. su->comp_head = comp;
  638. } else {
  639. tail->next = comp;
  640. }
  641. comp->su = su;
  642. /* setup default values from spec. */
  643. comp->saAmfCompNumMaxInstantiateWithoutDelay = 2;
  644. comp->saAmfCompNumMaxAmStartAttempt = 2;
  645. comp->saAmfCompNumMaxAmStopAttempt = 2;
  646. comp->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  647. comp->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  648. setSaNameT (&comp->name, name);
  649. return comp;
  650. }
  651. void amf_comp_delete (struct amf_comp *comp)
  652. {
  653. int i;
  654. struct amf_healthcheck *healthcheck;
  655. for (healthcheck = comp->healthcheck_head; healthcheck != NULL;) {
  656. struct amf_healthcheck *tmp = healthcheck;
  657. healthcheck = healthcheck->next;
  658. free (tmp);
  659. }
  660. for (i = 0; comp->saAmfCompCsTypes[i] != NULL; i++) {
  661. free (comp->saAmfCompCsTypes[i]);
  662. }
  663. for (i = 0; comp->saAmfCompCmdEnv[i] != NULL; i++) {
  664. free (comp->saAmfCompCmdEnv[i]);
  665. }
  666. free (comp->saAmfCompInstantiateCmd);
  667. free (comp->saAmfCompInstantiateCmdArgv);
  668. free (comp->saAmfCompTerminateCmd);
  669. free (comp->saAmfCompTerminateCmdArgv);
  670. free (comp->saAmfCompCleanupCmd);
  671. free (comp->saAmfCompCleanupCmdArgv);
  672. free (comp->saAmfCompAmStartCmd);
  673. free (comp->saAmfCompAmStartCmdArgv);
  674. free (comp->saAmfCompAmStopCmd);
  675. free (comp->saAmfCompAmStopCmdArgv);
  676. free (comp->clccli_path);
  677. free (comp);
  678. }
  679. struct amf_comp *amf_comp_find (struct amf_cluster *cluster, SaNameT *name)
  680. {
  681. struct amf_application *app;
  682. struct amf_sg *sg;
  683. struct amf_su *su;
  684. struct amf_comp *comp = NULL;
  685. char *app_name;
  686. char *sg_name;
  687. char *su_name;
  688. char *comp_name;
  689. char *ptrptr;
  690. char *buf;
  691. assert (cluster != NULL && name != NULL);
  692. /* malloc new buffer since strtok_r writes to its first argument */
  693. buf = amf_malloc (name->length + 1);
  694. memcpy (buf, name->value,name ->length + 1);
  695. comp_name = strtok_r(buf, ",", &ptrptr);
  696. su_name = strtok_r(NULL, ",", &ptrptr);
  697. sg_name = strtok_r(NULL, ",", &ptrptr);
  698. app_name = strtok_r(NULL, ",", &ptrptr);
  699. if (comp_name == NULL || su_name == NULL ||
  700. sg_name == NULL || app_name == NULL) {
  701. goto end;
  702. }
  703. comp_name += 8;
  704. su_name += 6;
  705. sg_name += 6;
  706. app_name += 7;
  707. app = amf_application_find (cluster, app_name);
  708. if (app == NULL) {
  709. goto end;
  710. }
  711. sg = amf_sg_find (app, sg_name);
  712. if (sg == NULL) {
  713. goto end;
  714. }
  715. for (su = sg->su_head; su != NULL; su = su->next) {
  716. if (strncmp (su_name, (char*)su->name.value, su->name.length) == 0) {
  717. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  718. if (comp->name.length == strlen(comp_name) &&
  719. strncmp (comp_name, (char*)comp->name.value,
  720. comp->name.length) == 0) {
  721. goto end;
  722. }
  723. }
  724. }
  725. }
  726. end:
  727. free (buf);
  728. return comp;
  729. }
  730. void amf_comp_healthcheck_deactivate (struct amf_comp *comp)
  731. {
  732. struct amf_healthcheck *healthcheck;
  733. if (!amf_su_is_local (comp->su))
  734. return;
  735. ENTER ("'%s'\n", getSaNameT (&comp->name));
  736. for (healthcheck = comp->healthcheck_head;
  737. healthcheck != NULL;
  738. healthcheck = healthcheck->next) {
  739. if (healthcheck->active) {
  740. healthcheck_deactivate (healthcheck);
  741. }
  742. }
  743. }
  744. static void comp_ha_state_set ( struct amf_comp *comp,
  745. struct amf_csi_assignment *csi_assignment,
  746. SaAmfHAStateT ha_state)
  747. {
  748. /* set confirmed HA state */
  749. csi_assignment->saAmfCSICompHAState = ha_state;
  750. TRACE1 ("Setting comp '%s', SU '%s' CSI '%s', HA state: %s\n",
  751. comp->name.value, comp->su->name.value,
  752. csi_assignment->csi->name.value,
  753. amf_ha_state (csi_assignment->saAmfCSICompHAState));
  754. amf_si_comp_set_ha_state_done (csi_assignment->csi->si, csi_assignment);
  755. }
  756. static void comp_presence_state_set (struct amf_comp *comp,
  757. SaAmfPresenceStateT presence_state)
  758. {
  759. comp->saAmfCompPresenceState = presence_state;
  760. TRACE1 ("Setting comp '%s', SU '%s' presence state: %s\n",
  761. comp->name.value, comp->su->name.value,
  762. amf_presence_state (comp->saAmfCompPresenceState));
  763. amf_su_comp_state_changed (
  764. comp->su, comp, SA_AMF_PRESENCE_STATE, presence_state);
  765. }
  766. #if 0
  767. static void lib_csi_remove_request (struct amf_comp *comp,
  768. struct amf_csi *csi)
  769. {
  770. struct res_lib_amf_csiremovecallback res_lib_amf_csiremovecallback;
  771. struct csi_remove_callback_data *csi_remove_callback_data;
  772. dprintf ("\t%s\n", getSaNameT (&comp->name));
  773. res_lib_amf_csiremovecallback.header.id = MESSAGE_RES_AMF_CSIREMOVECALLBACK;
  774. res_lib_amf_csiremovecallback.header.size = sizeof (struct res_lib_amf_csiremovecallback);
  775. res_lib_amf_csiremovecallback.header.error = SA_AIS_OK;
  776. csi_remove_callback_data = malloc (sizeof (struct csi_remove_callback_data));
  777. assert (csi_remove_callback_data); // TODO failure here of malloc
  778. csi_remove_callback_data->csi = csi;
  779. res_lib_amf_csiremovecallback.invocation =
  780. invocation_create (
  781. AMF_RESPONSE_CSIREMOVECALLBACK,
  782. csi_remove_callback_data);
  783. memcpy (&res_lib_amf_csiremovecallback.compName,
  784. &comp->name, sizeof (SaNameT));
  785. memcpy (&res_lib_amf_csiremovecallback.csiName,
  786. &csi->name, sizeof (SaNameT));
  787. res_lib_amf_csiremovecallback.csiFlags = 0;
  788. openais_conn_send_response (
  789. openais_conn_partner_get (comp->conn),
  790. &res_lib_amf_csiremovecallback,
  791. sizeof (struct res_lib_amf_csiremovecallback));
  792. }
  793. #endif
  794. struct amf_csi_assignment *amf_comp_get_next_csi_assignment (
  795. struct amf_comp *component,
  796. const struct amf_csi_assignment *csi_assignment)
  797. {
  798. struct amf_si *si;
  799. struct amf_csi *csi;
  800. struct amf_csi_assignment *tmp_csi_assignment;
  801. SaNameT dn;
  802. amf_comp_dn_make (component, &dn);
  803. if (csi_assignment == NULL) {
  804. si = component->su->sg->application->si_head;
  805. csi = si->csi_head;
  806. tmp_csi_assignment = csi->assigned_csis;
  807. } else {
  808. tmp_csi_assignment = csi_assignment->next;
  809. if (tmp_csi_assignment == NULL) {
  810. csi = csi_assignment->csi->next;
  811. if (csi == NULL) {
  812. si = csi_assignment->csi->si->next;
  813. if (si == NULL) {
  814. return NULL;
  815. } else {
  816. csi = si->csi_head;
  817. tmp_csi_assignment = csi->assigned_csis;
  818. }
  819. } else {
  820. si = csi->si;
  821. tmp_csi_assignment = csi->assigned_csis;
  822. }
  823. } else {
  824. csi = tmp_csi_assignment->csi;
  825. si = csi->si;
  826. }
  827. }
  828. for (; si != NULL; si = si->next) {
  829. if (tmp_csi_assignment == NULL && csi == NULL && si != NULL) {
  830. csi = si->csi_head;
  831. tmp_csi_assignment = csi->assigned_csis;
  832. }
  833. for (; csi != NULL; csi = csi->next) {
  834. if (tmp_csi_assignment == NULL && csi != NULL) {
  835. tmp_csi_assignment = csi->assigned_csis;
  836. }
  837. for (; tmp_csi_assignment != NULL;
  838. tmp_csi_assignment = tmp_csi_assignment->next) {
  839. if (name_match (&tmp_csi_assignment->name, &dn)) {
  840. return tmp_csi_assignment;
  841. }
  842. }
  843. }
  844. }
  845. return NULL;
  846. }
  847. void amf_comp_foreach_csi_assignment (
  848. struct amf_comp *component,
  849. void (*foreach_fn) (struct amf_comp *component,
  850. struct amf_csi_assignment *csi_assignment))
  851. {
  852. struct amf_csi_assignment *csi_assignment;
  853. assert (foreach_fn != NULL);
  854. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  855. while (csi_assignment != NULL) {
  856. foreach_fn (component, csi_assignment);
  857. csi_assignment = amf_comp_get_next_csi_assignment (
  858. component, csi_assignment);
  859. }
  860. }
  861. static struct amf_csi_assignment *csi_assignment_find_in (
  862. struct amf_comp *component, SaNameT *csi_name)
  863. {
  864. struct amf_csi_assignment *csi_assignment;
  865. SaNameT dn;
  866. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  867. while (csi_assignment != NULL) {
  868. amf_csi_dn_make (csi_assignment->csi, &dn);
  869. if (name_match (csi_name, &dn)) {
  870. return csi_assignment;
  871. }
  872. csi_assignment = amf_comp_get_next_csi_assignment (
  873. component, csi_assignment);
  874. }
  875. return NULL;
  876. }
  877. static void healthcheck_deactivate (
  878. struct amf_healthcheck *healthcheck_active)
  879. {
  880. dprintf ("deactivating healthcheck for component %s\n",
  881. getSaNameT (&healthcheck_active->comp->name));
  882. poll_timer_delete (aisexec_poll_handle,
  883. healthcheck_active->timer_handle_period);
  884. poll_timer_delete (aisexec_poll_handle,
  885. healthcheck_active->timer_handle_duration);
  886. invocation_destroy_by_data ((void *)healthcheck_active);
  887. healthcheck_active->active = 0;
  888. }
  889. /**
  890. * This function is called by the timer subsystem when AMF should request
  891. * a new healthcheck from a component.
  892. * @param data
  893. */
  894. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  895. {
  896. struct amf_healthcheck *healthcheck = _healthcheck;
  897. /* send healthcheck request to component */
  898. lib_healthcheck_request (healthcheck);
  899. /* start duration timer for response */
  900. poll_timer_add (aisexec_poll_handle,
  901. healthcheck->saAmfHealthcheckMaxDuration,
  902. (void *)healthcheck,
  903. timer_function_healthcheck_tmo,
  904. &healthcheck->timer_handle_duration);
  905. }
  906. /**
  907. * Multicast a healthcheck timeout event.
  908. * @param healthcheck
  909. */
  910. static void mcast_healthcheck_tmo_event (
  911. struct amf_healthcheck *healthcheck)
  912. {
  913. struct req_exec_amf_healthcheck_tmo req_exec;
  914. struct iovec iovec;
  915. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  916. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  917. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  918. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  919. memcpy (&req_exec.safHealthcheckKey,
  920. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  921. iovec.iov_base = (char *)&req_exec;
  922. iovec.iov_len = sizeof (req_exec);
  923. assert (totempg_groups_mcast_joined (openais_group_handle,
  924. &iovec, 1, TOTEMPG_AGREED) == 0);
  925. }
  926. /**
  927. * This function is called by the timer subsystem when a component has not
  928. * performed a healthcheck on time.
  929. * The event is multicasted to the cluster.
  930. * @param data
  931. */
  932. static void timer_function_healthcheck_tmo (
  933. void *_healthcheck)
  934. {
  935. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  936. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  937. getSaNameT (&healthcheck->comp->name));
  938. mcast_healthcheck_tmo_event (healthcheck);
  939. }
  940. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  941. {
  942. struct res_lib_amf_healthcheckcallback res_lib;
  943. res_lib.header.id = MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  944. res_lib.header.size = sizeof (struct res_lib_amf_healthcheckcallback);
  945. res_lib.header.error = SA_AIS_OK;
  946. res_lib.invocation =
  947. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  948. amf_comp_dn_make (healthcheck->comp, &res_lib.compName);
  949. memcpy (&res_lib.key, &healthcheck->safHealthcheckKey,
  950. sizeof (SaAmfHealthcheckKeyT));
  951. TRACE7 ("sending healthcheck request to component %s",
  952. res_lib.compName.value);
  953. openais_conn_send_response (
  954. openais_conn_partner_get (healthcheck->comp->conn),
  955. &res_lib, sizeof (struct res_lib_amf_healthcheckcallback));
  956. }
  957. static void lib_csi_set_request (
  958. struct amf_comp *comp,
  959. struct amf_csi_assignment *csi_assignment)
  960. {
  961. struct res_lib_amf_csisetcallback* res_lib;
  962. void* p;
  963. struct amf_csi_attribute *attribute;
  964. size_t char_length_of_csi_attrs=0;
  965. size_t num_of_csi_attrs=0;
  966. int i;
  967. struct amf_csi *csi;
  968. char* csi_attribute_buf;
  969. unsigned int byte_offset;
  970. if (!amf_su_is_local (comp->su))
  971. return;
  972. csi = csi_assignment->csi;
  973. ENTER ("Assigning CSI '%s' state %s to comp '%s'\n",
  974. getSaNameT (&csi->name),
  975. amf_ha_state (csi_assignment->requested_ha_state),
  976. comp->name.value);
  977. for (attribute = csi->attributes_head;
  978. attribute != NULL;
  979. attribute = attribute->next) {
  980. for (i = 0; attribute->value[i] != NULL; i++) {
  981. num_of_csi_attrs++;
  982. char_length_of_csi_attrs += strlen(attribute->name);
  983. char_length_of_csi_attrs += strlen(attribute->value[i]);
  984. char_length_of_csi_attrs += 2;
  985. }
  986. }
  987. p = amf_malloc(sizeof(struct res_lib_amf_csisetcallback) +
  988. char_length_of_csi_attrs);
  989. res_lib = (struct res_lib_amf_csisetcallback*)p;
  990. /* Address of the buffer containing the Csi name value pair */
  991. csi_attribute_buf = res_lib->csi_attr_buf;
  992. /* Byteoffset start at the zero byte */
  993. byte_offset = 0;
  994. for (attribute = csi->attributes_head;
  995. attribute != NULL;
  996. attribute = attribute->next) {
  997. for (i = 0; attribute->value[i] != NULL; i++) {
  998. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  999. byte_offset += strlen(attribute->name) + 1;
  1000. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  1001. byte_offset += strlen(attribute->value[i]) + 1;
  1002. }
  1003. }
  1004. res_lib->number = num_of_csi_attrs;
  1005. res_lib->csiFlags = SA_AMF_CSI_ADD_ONE;
  1006. switch (csi_assignment->requested_ha_state) {
  1007. case SA_AMF_HA_ACTIVE: {
  1008. res_lib->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  1009. res_lib->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  1010. SA_AMF_CSI_NEW_ASSIGN;
  1011. break;
  1012. }
  1013. case SA_AMF_HA_STANDBY: {
  1014. res_lib->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  1015. res_lib->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  1016. break;
  1017. }
  1018. case SA_AMF_HA_QUIESCED: {
  1019. /*TODO*/
  1020. break;
  1021. }
  1022. case SA_AMF_HA_QUIESCING: {
  1023. /*TODO*/
  1024. break;
  1025. }
  1026. default: {
  1027. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  1028. break;
  1029. }
  1030. }
  1031. res_lib->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  1032. res_lib->header.size =
  1033. sizeof (struct res_lib_amf_csisetcallback) +
  1034. char_length_of_csi_attrs;
  1035. res_lib->header.error = SA_AIS_OK;
  1036. amf_comp_dn_make (comp, &res_lib->compName);
  1037. amf_csi_dn_make (csi, &res_lib->csiName);
  1038. res_lib->haState = csi_assignment->requested_ha_state;
  1039. res_lib->invocation =
  1040. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  1041. openais_conn_send_response (
  1042. openais_conn_partner_get (comp->conn), res_lib, res_lib->header.size);
  1043. free(p);
  1044. }
  1045. static void stop_component_instantiate_timer (struct amf_comp *component)
  1046. {
  1047. if (component->instantiate_timeout_handle) {
  1048. dprintf ("Stop component instantiate timer");
  1049. poll_timer_delete (aisexec_poll_handle,
  1050. component->instantiate_timeout_handle);
  1051. component->instantiate_timeout_handle = 0;
  1052. }
  1053. }
  1054. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  1055. {
  1056. TRACE2("Exec comp register '%s'", comp->name.value);
  1057. stop_component_instantiate_timer (comp);
  1058. switch (comp->saAmfCompPresenceState) {
  1059. case SA_AMF_PRESENCE_RESTARTING:
  1060. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1061. break;
  1062. case SA_AMF_PRESENCE_INSTANTIATING:
  1063. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  1064. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1065. break;
  1066. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1067. /* ignore due to instantitate timeout a while ago */
  1068. break;
  1069. default:
  1070. assert (0);
  1071. break;
  1072. }
  1073. return SA_AIS_OK;
  1074. }
  1075. void amf_comp_error_report (struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  1076. {
  1077. struct res_lib_amf_componenterrorreport res_lib;
  1078. TRACE2("Exec comp error report '%s'", comp->name.value);
  1079. if (amf_su_is_local (comp->su)) {
  1080. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  1081. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  1082. res_lib.header.error = SA_AIS_OK;
  1083. openais_conn_send_response (comp->conn, &res_lib, sizeof (res_lib));
  1084. }
  1085. /* report to SU and let it handle the problem */
  1086. report_error_suspected (comp, recommendedRecovery);
  1087. }
  1088. /**
  1089. * Healthcheck timeout event handler
  1090. * @param comp
  1091. * @param healthcheck
  1092. */
  1093. void amf_comp_healthcheck_tmo (
  1094. struct amf_comp *comp, struct amf_healthcheck *healthcheck)
  1095. {
  1096. TRACE2("Exec healthcheck tmo for '%s'", comp->name.value);
  1097. /* report to SU and let it handle the problem */
  1098. report_error_suspected (comp, healthcheck->recommendedRecovery);
  1099. }
  1100. static void clear_ha_state (
  1101. struct amf_comp *comp, struct amf_csi_assignment *csi_assignment)
  1102. {
  1103. ENTER ("");
  1104. csi_assignment->saAmfCSICompHAState = 0;
  1105. }
  1106. /**
  1107. * Event method to be called when a cleanup completed event is received
  1108. * @param comp
  1109. */
  1110. void amf_comp_cleanup_completed (struct amf_comp *comp)
  1111. {
  1112. TRACE2("Exec CLC cleanup completed for '%s'", comp->name.value);
  1113. /* Set all CSI's confirmed HA state to unknown */
  1114. amf_comp_foreach_csi_assignment (comp, clear_ha_state);
  1115. /* clear error suspected flag, component is terminated now */
  1116. comp->error_suspected = 0;
  1117. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  1118. amf_comp_instantiate (comp);
  1119. } else {
  1120. comp_presence_state_set (comp, SA_AMF_PRESENCE_UNINSTANTIATED);
  1121. }
  1122. }
  1123. /**
  1124. * Handle the request from a component to start a healthcheck
  1125. *
  1126. * @param comp
  1127. * @param healthcheckKey
  1128. * @param invocationType
  1129. * @param recommendedRecovery
  1130. *
  1131. * @return SaAisErrorT - return value to component
  1132. */
  1133. SaAisErrorT amf_comp_healthcheck_start (
  1134. struct amf_comp *comp,
  1135. SaAmfHealthcheckKeyT *healthcheckKey,
  1136. SaAmfHealthcheckInvocationT invocationType,
  1137. SaAmfRecommendedRecoveryT recommendedRecovery)
  1138. {
  1139. struct amf_healthcheck *healthcheck;
  1140. SaAisErrorT error = SA_AIS_OK;
  1141. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1142. if (healthcheck == 0) {
  1143. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  1144. healthcheckKey->key);
  1145. error = SA_AIS_ERR_NOT_EXIST;
  1146. goto error_exit;
  1147. }
  1148. dprintf ("Healthcheckstart: '%s', key '%s'",
  1149. comp->name.value, healthcheckKey->key);
  1150. /*
  1151. * Determine if this healthcheck is already active
  1152. */
  1153. if (healthcheck->active) {
  1154. error = SA_AIS_ERR_EXIST;
  1155. goto error_exit;
  1156. }
  1157. /*
  1158. * Initialise
  1159. */
  1160. healthcheck->invocationType = invocationType;
  1161. healthcheck->recommendedRecovery = recommendedRecovery;
  1162. healthcheck->timer_handle_duration = 0;
  1163. healthcheck->timer_handle_period = 0;
  1164. healthcheck->active = 1;
  1165. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1166. /* start timer to execute first healthcheck request */
  1167. poll_timer_add (aisexec_poll_handle,
  1168. healthcheck->saAmfHealthcheckPeriod,
  1169. (void *)healthcheck,
  1170. timer_function_healthcheck_next_fn,
  1171. &healthcheck->timer_handle_period);
  1172. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  1173. /* start supervision timer */
  1174. poll_timer_add (aisexec_poll_handle,
  1175. healthcheck->saAmfHealthcheckPeriod,
  1176. (void *)healthcheck,
  1177. timer_function_healthcheck_tmo,
  1178. &healthcheck->timer_handle_period);
  1179. } else {
  1180. error = SA_AIS_ERR_INVALID_PARAM;
  1181. }
  1182. error_exit:
  1183. return error;
  1184. }
  1185. /**
  1186. * Stop all or a specifed healthcheck
  1187. * @param comp
  1188. * @param healthcheckKey - NULL if all
  1189. *
  1190. * @return SaAisErrorT
  1191. */
  1192. SaAisErrorT amf_comp_healthcheck_stop (
  1193. struct amf_comp *comp,
  1194. SaAmfHealthcheckKeyT *healthcheckKey)
  1195. {
  1196. struct amf_healthcheck *healthcheck;
  1197. SaAisErrorT error = SA_AIS_OK;
  1198. dprintf ("Healthcheckstop: '%s'", comp->name.value);
  1199. if (!amf_su_is_local (comp->su)) {
  1200. return SA_AIS_OK;
  1201. }
  1202. if (healthcheckKey == NULL) {
  1203. for (healthcheck = comp->healthcheck_head;
  1204. healthcheck != NULL;
  1205. healthcheck = healthcheck->next) {
  1206. healthcheck_deactivate (healthcheck);
  1207. }
  1208. } else {
  1209. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1210. if (healthcheck == NULL) {
  1211. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1212. healthcheckKey->key);
  1213. error = SA_AIS_ERR_NOT_EXIST;
  1214. } else {
  1215. healthcheck_deactivate (healthcheck);
  1216. }
  1217. }
  1218. return error;
  1219. }
  1220. /**
  1221. * Instantiate a component
  1222. * @param comp
  1223. */
  1224. void amf_comp_instantiate (struct amf_comp *comp)
  1225. {
  1226. ENTER ("'%s' SU '%s'", getSaNameT (&comp->name),
  1227. getSaNameT (&comp->su->name));
  1228. switch (comp->saAmfCompPresenceState) {
  1229. case SA_AMF_PRESENCE_RESTARTING:
  1230. /* fall through */
  1231. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1232. if (amf_su_is_local (comp->su)) {
  1233. TRACE1("Send instantiate event for comp '%s' from host %s",
  1234. comp->name.value, comp->su->saAmfSUHostedByNode.value);
  1235. SaNameT compName;
  1236. amf_comp_dn_make (comp, &compName);
  1237. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE,
  1238. &compName, sizeof (SaNameT));
  1239. }
  1240. break;
  1241. default:
  1242. dprintf("Instantiate ignored in Component presence state %d",
  1243. comp->saAmfCompPresenceState);
  1244. break;
  1245. }
  1246. }
  1247. void amf_comp_instantiate_tmo_event (struct amf_comp *comp)
  1248. {
  1249. ENTER ("Comp instantiate timeout after %d ms '%s' '%s'",
  1250. comp->saAmfCompInstantiateTimeout, comp->su->name.value,
  1251. comp->name.value);
  1252. switch (comp->saAmfCompPresenceState) {
  1253. case SA_AMF_PRESENCE_RESTARTING:
  1254. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1255. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1256. break;
  1257. case SA_AMF_PRESENCE_INSTANTIATING:
  1258. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1259. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1260. break;
  1261. case SA_AMF_PRESENCE_INSTANTIATED:
  1262. assert (comp->instantiate_timeout_handle == 0);
  1263. break;
  1264. default:
  1265. dprintf("Presence state = %d", comp->saAmfCompPresenceState);
  1266. assert (0);
  1267. break;
  1268. }
  1269. }
  1270. void amf_comp_instantiate_event (struct amf_comp *component)
  1271. {
  1272. int res;
  1273. ENTER ("");
  1274. switch (component->saAmfCompPresenceState) {
  1275. case SA_AMF_PRESENCE_INSTANTIATING:
  1276. case SA_AMF_PRESENCE_INSTANTIATED:
  1277. case SA_AMF_PRESENCE_TERMINATING:
  1278. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1279. case SA_AMF_PRESENCE_TERMINATION_FAILED:
  1280. dprintf("Instantiate ignored in Component presence state %d",
  1281. component->saAmfCompPresenceState);
  1282. break;
  1283. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1284. comp_presence_state_set (component, SA_AMF_PRESENCE_INSTANTIATING);
  1285. amf_su_comp_state_changed(component->su,
  1286. component,SA_AMF_PRESENCE_STATE,SA_AMF_PRESENCE_INSTANTIATING);
  1287. if (amf_su_is_local (component->su)) {
  1288. res = clc_interfaces[component->comptype]->instantiate (
  1289. component);
  1290. }
  1291. break;
  1292. case SA_AMF_PRESENCE_RESTARTING:
  1293. if (amf_su_is_local (component->su)) {
  1294. res = clc_interfaces[component->comptype]->instantiate (
  1295. component);
  1296. }
  1297. break;
  1298. default:
  1299. dprintf("Component presence state %d",
  1300. component->saAmfCompPresenceState);
  1301. assert (0);
  1302. break;
  1303. }
  1304. }
  1305. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1306. SaAmfReadinessStateT state)
  1307. {
  1308. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1309. comp->name.value, amf_readiness_state (state));
  1310. }
  1311. /**
  1312. * Handle a component response (received from the lib) of an earlier AMF request.
  1313. * This function should be invoked when the lib request is received.
  1314. * @param invocation [in] associates the response with the request (callback)
  1315. * @param error [in] response from the component of the associated callback
  1316. * @param retval [out] contains return value to component when needed
  1317. *
  1318. * @return ==0 respond to component, do not multicast
  1319. * @return >0 do not respond to component, multicast response
  1320. */
  1321. int amf_comp_response_1 (
  1322. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval,
  1323. SaUint32T *interface, SaNameT *dn)
  1324. {
  1325. int res;
  1326. void *data;
  1327. res = invocation_get_and_destroy (invocation, interface, &data);
  1328. if (res == -1) {
  1329. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1330. *retval = SA_AIS_ERR_INVALID_PARAM;
  1331. return 0;
  1332. }
  1333. switch (*interface) {
  1334. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1335. struct amf_healthcheck *healthcheck = data;
  1336. SaNameT name;
  1337. TRACE7 ("Healthcheck response from '%s': %d",
  1338. amf_comp_dn_make (healthcheck->comp, &name), error);
  1339. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1340. /* the response was on time, delete supervision timer */
  1341. poll_timer_delete (aisexec_poll_handle,
  1342. healthcheck->timer_handle_duration);
  1343. healthcheck->timer_handle_duration = 0;
  1344. /* start timer to execute next healthcheck request */
  1345. poll_timer_add (aisexec_poll_handle,
  1346. healthcheck->saAmfHealthcheckPeriod,
  1347. (void *)healthcheck,
  1348. timer_function_healthcheck_next_fn,
  1349. &healthcheck->timer_handle_period);
  1350. *retval = SA_AIS_OK;
  1351. } else {
  1352. *retval = SA_AIS_ERR_INVALID_PARAM;
  1353. }
  1354. return 0; /* do not multicast event */
  1355. break;
  1356. }
  1357. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1358. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1359. amf_csi_assignment_dn_make (data, dn);
  1360. return 1; /* multicast event */
  1361. break;
  1362. #if 0
  1363. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1364. struct component_terminate_callback_data *component_terminate_callback_data;
  1365. component_terminate_callback_data = data;
  1366. dprintf ("Lib component terminate callback response, error: %d", error);
  1367. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1368. escalation_policy_restart (component_terminate_callback_data->comp);
  1369. return 1;
  1370. break;
  1371. }
  1372. #endif
  1373. default:
  1374. assert (0);
  1375. break;
  1376. }
  1377. /* XXX we fall here in case NDEBUG is set */
  1378. *retval = -1;
  1379. return 0;
  1380. }
  1381. /**
  1382. * Handle a component response (received from EVS) of an earlier AMF request.
  1383. * This function should be invoked when the multicast request is received.
  1384. * @param invocation [in] associates the response with the request (callback)
  1385. * @param error [in] response from the component of the associated callback
  1386. * @param retval [out] contains return value to component when needed
  1387. *
  1388. * @return component to which the response should be sent
  1389. */
  1390. struct amf_comp *amf_comp_response_2 (
  1391. SaUint32T interface, SaNameT *dn, SaAisErrorT error, SaAisErrorT *retval)
  1392. {
  1393. struct amf_csi_assignment *csi_assignment;
  1394. struct amf_comp *comp = NULL;
  1395. assert (retval != NULL);
  1396. *retval = SA_AIS_OK;
  1397. switch (interface) {
  1398. case AMF_RESPONSE_CSISETCALLBACK: {
  1399. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1400. assert (csi_assignment != NULL);
  1401. comp = csi_assignment->comp;
  1402. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1403. csi_assignment->csi->name.value,
  1404. csi_assignment->comp->name.value, error);
  1405. comp = csi_assignment->comp;
  1406. if (error == SA_AIS_OK) {
  1407. comp_ha_state_set (
  1408. comp, csi_assignment, csi_assignment->requested_ha_state);
  1409. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1410. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1411. csi_assignment);
  1412. } else {
  1413. *retval = SA_AIS_ERR_INVALID_PARAM;
  1414. }
  1415. break;
  1416. }
  1417. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1418. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1419. assert (csi_assignment != NULL);
  1420. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1421. csi_assignment->csi->name.value,
  1422. csi_assignment->comp->name.value, error);
  1423. comp = csi_assignment->comp;
  1424. if (error == SA_AIS_OK) {
  1425. comp_ha_state_set (comp, csi_assignment,
  1426. csi_assignment->requested_ha_state);
  1427. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1428. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1429. csi_assignment);
  1430. } else {
  1431. *retval = SA_AIS_ERR_INVALID_PARAM;
  1432. }
  1433. break;
  1434. }
  1435. #if 0
  1436. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1437. struct component_terminate_callback_data *callback_data = data;
  1438. dprintf ("Lib comp '%s' terminate callback response, error: %d",
  1439. callback_data->comp->name.value, error);
  1440. comp_presence_state_set (callback_data->comp,
  1441. SA_AMF_PRESENCE_UNINSTANTIATED);
  1442. break;
  1443. }
  1444. #endif
  1445. default:
  1446. assert (0);
  1447. break;
  1448. }
  1449. return comp;
  1450. }
  1451. /**
  1452. * Request a component to assume a particular HA state
  1453. * @param comp
  1454. * @param csi_assignment
  1455. * @param requested_ha_state
  1456. */
  1457. void amf_comp_hastate_set (
  1458. struct amf_comp *component,
  1459. struct amf_csi_assignment *csi_assignment)
  1460. {
  1461. ENTER ("'%s'", csi_assignment->csi->name.value);
  1462. assert (component != NULL && csi_assignment != NULL);
  1463. if (!component->error_suspected) {
  1464. lib_csi_set_request(component, csi_assignment);
  1465. } else {
  1466. if (csi_assignment->requested_ha_state == SA_AMF_HA_QUIESCED) {
  1467. csi_assignment->saAmfCSICompHAState = csi_assignment->requested_ha_state;
  1468. } else {
  1469. assert (0);
  1470. }
  1471. }
  1472. LEAVE("");
  1473. }
  1474. /**
  1475. * Request termination of a component
  1476. * @param comp
  1477. */
  1478. void amf_comp_terminate (struct amf_comp *comp)
  1479. {
  1480. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1481. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1482. if (amf_su_is_local (comp->su)) {
  1483. amf_comp_healthcheck_stop (comp, NULL);
  1484. if (comp->error_suspected) {
  1485. clc_interfaces[comp->comptype]->cleanup (comp);
  1486. } else {
  1487. clc_interfaces[comp->comptype]->terminate (comp);
  1488. }
  1489. }
  1490. }
  1491. /**
  1492. * Request restart of a component
  1493. * @param comp
  1494. */
  1495. void amf_comp_restart (struct amf_comp *comp)
  1496. {
  1497. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1498. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1499. comp->saAmfCompRestartCount += 1;
  1500. if (amf_su_is_local (comp->su)) {
  1501. amf_comp_healthcheck_stop (comp, NULL);
  1502. clc_interfaces[comp->comptype]->cleanup (comp);
  1503. }
  1504. }
  1505. /**
  1506. * Request to return the HA state for a components CSI
  1507. * @param comp
  1508. * @param csi_name
  1509. * @param ha_state
  1510. *
  1511. * @return SaAisErrorT
  1512. */
  1513. SaAisErrorT amf_comp_hastate_get (
  1514. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1515. {
  1516. struct amf_csi_assignment *assignment;
  1517. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1518. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1519. getSaNameT (&comp->name), csi_name->value);
  1520. assignment = csi_assignment_find_in (comp, csi_name);
  1521. if (assignment != NULL) {
  1522. *ha_state = assignment->saAmfCSICompHAState;
  1523. return SA_AIS_OK;
  1524. }
  1525. return SA_AIS_ERR_INVALID_PARAM;
  1526. }
  1527. /**
  1528. * Response from a component informs AMF that it has performed a healthcheck
  1529. * @param comp
  1530. * @param healthcheckKey
  1531. * @param healthcheckResult
  1532. *
  1533. * @return SaAisErrorT
  1534. */
  1535. SaAisErrorT amf_comp_healthcheck_confirm (
  1536. struct amf_comp *comp,
  1537. SaAmfHealthcheckKeyT *healthcheckKey,
  1538. SaAisErrorT healthcheckResult)
  1539. {
  1540. struct amf_healthcheck *healthcheck;
  1541. SaAisErrorT error = SA_AIS_OK;
  1542. dprintf ("Healthcheckconfirm: '%s', key '%s'",
  1543. comp->name.value, healthcheckKey->key);
  1544. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1545. if (healthcheck == NULL) {
  1546. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1547. healthcheckKey->key);
  1548. error = SA_AIS_ERR_NOT_EXIST;
  1549. } else if (healthcheck->active) {
  1550. if (healthcheckResult == SA_AIS_OK) {
  1551. /* the response was on time, restart the supervision timer */
  1552. poll_timer_delete (aisexec_poll_handle,
  1553. healthcheck->timer_handle_period);
  1554. poll_timer_add (aisexec_poll_handle,
  1555. healthcheck->saAmfHealthcheckPeriod,
  1556. (void *)healthcheck,
  1557. timer_function_healthcheck_tmo,
  1558. &healthcheck->timer_handle_period);
  1559. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1560. /* send to cluster */
  1561. mcast_healthcheck_tmo_event (healthcheck);
  1562. } else {
  1563. error = SA_AIS_ERR_INVALID_PARAM;
  1564. }
  1565. } else {
  1566. error = SA_AIS_ERR_INVALID_PARAM;
  1567. }
  1568. return error;
  1569. }
  1570. void amf_comp_init (void)
  1571. {
  1572. log_init ("AMF");
  1573. }
  1574. void amf_comp_operational_state_set (struct amf_comp *comp,
  1575. SaAmfOperationalStateT oper_state)
  1576. {
  1577. comp->saAmfCompOperState = oper_state;
  1578. TRACE1 ("Setting comp '%s', SU '%s' operational state: %s\n",
  1579. comp->name.value, comp->su->name.value,
  1580. amf_op_state (comp->saAmfCompOperState));
  1581. amf_su_comp_state_changed (
  1582. comp->su, comp, SA_AMF_OP_STATE, oper_state);
  1583. }
  1584. int amf_comp_get_saAmfCompNumCurrActiveCsi(struct amf_comp *component)
  1585. {
  1586. int cnt = 0;
  1587. struct amf_csi_assignment *csi_assignment;
  1588. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1589. while (csi_assignment != NULL) {
  1590. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_ACTIVE) {
  1591. cnt++;
  1592. }
  1593. csi_assignment = amf_comp_get_next_csi_assignment (
  1594. component, csi_assignment);
  1595. }
  1596. return cnt;
  1597. }
  1598. int amf_comp_get_saAmfCompNumCurrStandbyCsi(struct amf_comp *component)
  1599. {
  1600. int cnt = 0;
  1601. struct amf_csi_assignment *csi_assignment;
  1602. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1603. while (csi_assignment != NULL) {
  1604. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_STANDBY) {
  1605. cnt++;
  1606. }
  1607. csi_assignment = amf_comp_get_next_csi_assignment (
  1608. component, csi_assignment);
  1609. }
  1610. return cnt;
  1611. }
  1612. SaAmfReadinessStateT amf_comp_get_saAmfCompReadinessState (
  1613. struct amf_comp *component)
  1614. {
  1615. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1616. return amf_su_get_saAmfSUReadinessState (component->su);
  1617. } else if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_DISABLED) {
  1618. return SA_AMF_READINESS_OUT_OF_SERVICE;
  1619. }
  1620. assert (0);
  1621. /* XXX we fall here in case NDEBUG is set */
  1622. return -1;
  1623. }
  1624. /**
  1625. * Component is informed that the node where the 'real'
  1626. * component process is executing has unexpectadly left the
  1627. * node. If there is a pending interaction between AMF
  1628. * (component) and the 'real' component process, then component
  1629. * will indicate to its subordinate objects the interaction
  1630. * failed. Pending presence state changes is indicated by
  1631. * reporting the new state is uninstantiated while pending csi
  1632. * operations are indicated by 'operation failed'.
  1633. * @param comp
  1634. *
  1635. * @return void
  1636. */
  1637. void amf_comp_node_left (struct amf_comp *component)
  1638. {
  1639. int change_pending = 0;
  1640. struct amf_csi_assignment *csi_assignment;
  1641. ENTER("");
  1642. if (component->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING ||
  1643. component->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING ||
  1644. component->saAmfCompPresenceState == SA_AMF_PRESENCE_TERMINATING) {
  1645. change_pending = 1;
  1646. }
  1647. component->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  1648. if (amf_su_presence_state_all_comps_in_su_are_set (component->su,
  1649. SA_AMF_PRESENCE_UNINSTANTIATED) != 0) {
  1650. component->su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  1651. }
  1652. if (change_pending) {
  1653. change_pending =0;
  1654. amf_su_comp_state_changed ( component->su,
  1655. component,
  1656. SA_AMF_PRESENCE_STATE,
  1657. SA_AMF_PRESENCE_UNINSTANTIATED);
  1658. }
  1659. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1660. change_pending = 1;
  1661. }
  1662. component->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  1663. if (change_pending) {
  1664. change_pending =0;
  1665. amf_su_comp_state_changed ( component->su,
  1666. component,
  1667. SA_AMF_OP_STATE,
  1668. SA_AMF_OPERATIONAL_DISABLED);
  1669. }
  1670. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1671. while (csi_assignment != NULL) {
  1672. if (csi_assignment->requested_ha_state !=
  1673. csi_assignment->saAmfCSICompHAState) {
  1674. amf_si_comp_set_ha_state_failed (
  1675. csi_assignment->csi->si,csi_assignment);
  1676. }
  1677. csi_assignment = amf_comp_get_next_csi_assignment (
  1678. component, csi_assignment);
  1679. }
  1680. }
  1681. /**
  1682. * Serialize a component including variable length arrays and
  1683. * strings to a buffer returned. Buffer is to be freed by
  1684. * caller.
  1685. * @param component
  1686. * @param len
  1687. *
  1688. * @return void*
  1689. */
  1690. void *amf_comp_serialize (struct amf_comp *component, int *len)
  1691. {
  1692. char *buf = NULL;
  1693. int i, offset = 0, size = 0;
  1694. TRACE8 ("%s", component->name.value);
  1695. buf = amf_serialize_SaNameT (buf, &size, &offset, &component->name);
  1696. /* count cstypes and write to buf */
  1697. for (i = 0; component->saAmfCompCsTypes &&
  1698. component->saAmfCompCsTypes[i] != NULL; i++);
  1699. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  1700. for (i = 0; component->saAmfCompCsTypes &&
  1701. component->saAmfCompCsTypes[i] != NULL; i++) {
  1702. buf = amf_serialize_SaNameT (
  1703. buf, &size, &offset, component->saAmfCompCsTypes[i]);
  1704. }
  1705. buf = amf_serialize_SaUint32T (
  1706. buf, &size, &offset, component->saAmfCompCategory);
  1707. buf = amf_serialize_SaUint32T (
  1708. buf, &size, &offset, component->saAmfCompCapability);
  1709. buf = amf_serialize_SaUint32T (
  1710. buf, &size, &offset, component->saAmfCompNumMaxActiveCsi);
  1711. buf = amf_serialize_SaUint32T (
  1712. buf, &size, &offset, component->saAmfCompNumMaxStandbyCsi);
  1713. /* count environment vars and write to buf */
  1714. for (i = 0; component->saAmfCompCmdEnv &&
  1715. component->saAmfCompCmdEnv[i] != NULL; i++);
  1716. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  1717. for (i = 0; component->saAmfCompCmdEnv &&
  1718. component->saAmfCompCmdEnv[i] != NULL; i++) {
  1719. buf = amf_serialize_SaStringT (
  1720. buf, &size, &offset, component->saAmfCompCmdEnv[i]);
  1721. }
  1722. buf = amf_serialize_SaUint32T (
  1723. buf, &size, &offset, component->saAmfCompDefaultClcCliTimeout);
  1724. buf = amf_serialize_SaUint32T (
  1725. buf, &size, &offset, component->saAmfCompDefaultCallbackTimeOut);
  1726. buf = amf_serialize_SaStringT (
  1727. buf, &size, &offset, component->saAmfCompInstantiateCmd);
  1728. buf = amf_serialize_SaStringT (
  1729. buf, &size, &offset, component->saAmfCompInstantiateCmdArgv);
  1730. buf = amf_serialize_SaUint32T (
  1731. buf, &size, &offset, component->saAmfCompInstantiateTimeout);
  1732. buf = amf_serialize_SaUint32T (
  1733. buf, &size, &offset, component->saAmfCompInstantiationLevel);
  1734. buf = amf_serialize_SaUint32T (
  1735. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithoutDelay);
  1736. buf = amf_serialize_SaUint32T (
  1737. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithDelay);
  1738. buf = amf_serialize_SaUint32T (
  1739. buf, &size, &offset, component->saAmfCompDelayBetweenInstantiateAttempts);
  1740. buf = amf_serialize_SaStringT (
  1741. buf, &size, &offset, component->saAmfCompTerminateCmd);
  1742. buf = amf_serialize_SaUint32T (
  1743. buf, &size, &offset, component->saAmfCompTerminateTimeout);
  1744. buf = amf_serialize_SaStringT (
  1745. buf, &size, &offset, component->saAmfCompTerminateCmdArgv);
  1746. buf = amf_serialize_SaStringT (
  1747. buf, &size, &offset, component->saAmfCompCleanupCmd);
  1748. buf = amf_serialize_SaUint32T (
  1749. buf, &size, &offset, component->saAmfCompCleanupTimeout);
  1750. buf = amf_serialize_SaStringT (
  1751. buf, &size, &offset, component->saAmfCompCleanupCmdArgv);
  1752. buf = amf_serialize_SaStringT (
  1753. buf, &size, &offset, component->saAmfCompAmStartCmd);
  1754. buf = amf_serialize_SaUint32T (
  1755. buf, &size, &offset, component->saAmfCompAmStartTimeout);
  1756. buf = amf_serialize_SaStringT (
  1757. buf, &size, &offset, component->saAmfCompAmStartCmdArgv);
  1758. buf = amf_serialize_SaUint32T (
  1759. buf, &size, &offset, component->saAmfCompNumMaxAmStartAttempt);
  1760. buf = amf_serialize_SaStringT (
  1761. buf, &size, &offset, component->saAmfCompAmStopCmd);
  1762. buf = amf_serialize_SaUint32T (
  1763. buf, &size, &offset, component->saAmfCompAmStopTimeout);
  1764. buf = amf_serialize_SaStringT (
  1765. buf, &size, &offset, component->saAmfCompAmStopCmdArgv);
  1766. buf = amf_serialize_SaUint32T (
  1767. buf, &size, &offset, component->saAmfCompNumMaxAmStopAttempt);
  1768. buf = amf_serialize_SaUint32T (
  1769. buf, &size, &offset, component->saAmfCompTerminateCallbackTimeout);
  1770. buf = amf_serialize_SaUint32T (
  1771. buf, &size, &offset, component->saAmfCompCSISetCallbackTimeout);
  1772. buf = amf_serialize_SaUint32T (
  1773. buf, &size, &offset, component->saAmfCompQuiescingCompleteTimeout);
  1774. buf = amf_serialize_SaUint32T (
  1775. buf, &size, &offset, component->saAmfCompCSIRmvCallbackTimeout);
  1776. buf = amf_serialize_SaUint32T (
  1777. buf, &size, &offset, component->saAmfCompRecoveryOnError);
  1778. buf = amf_serialize_SaUint32T (
  1779. buf, &size, &offset, component->saAmfCompDisableRestart);
  1780. buf = amf_serialize_SaNameT (
  1781. buf, &size, &offset, &component->saAmfCompProxyCsi);
  1782. buf = amf_serialize_SaUint32T (
  1783. buf, &size, &offset, component->saAmfCompOperState);
  1784. buf = amf_serialize_SaUint32T (
  1785. buf, &size, &offset, component->saAmfCompPresenceState);
  1786. buf = amf_serialize_SaUint32T (
  1787. buf, &size, &offset, component->saAmfCompRestartCount);
  1788. buf = amf_serialize_SaNameT (
  1789. buf, &size, &offset, &component->saAmfCompCurrProxyName);
  1790. buf = amf_serialize_SaStringT (
  1791. buf, &size, &offset, component->clccli_path);
  1792. buf = amf_serialize_SaUint32T (
  1793. buf, &size, &offset, component->comptype);
  1794. buf = amf_serialize_SaUint32T (
  1795. buf, &size, &offset, component->error_suspected);
  1796. *len = offset;
  1797. return buf;
  1798. }
  1799. /**
  1800. * Deserialize a buffer into a AMF component object.
  1801. * @param su
  1802. * @param buf
  1803. * @param size
  1804. *
  1805. * @return struct amf_comp*
  1806. */
  1807. struct amf_comp *amf_comp_deserialize (struct amf_su *su, char *buf)
  1808. {
  1809. char *tmp = buf;
  1810. int i;
  1811. SaUint32T cnt;
  1812. struct amf_comp *component = amf_comp_new (su, "");
  1813. tmp = amf_deserialize_SaNameT (tmp, &component->name);
  1814. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  1815. component->saAmfCompCsTypes = amf_malloc ((cnt + 1) * sizeof (SaNameT*));
  1816. for (i = 0; i < cnt; i++) {
  1817. component->saAmfCompCsTypes[i] = amf_malloc (sizeof (SaNameT));
  1818. tmp = amf_deserialize_SaNameT (tmp, component->saAmfCompCsTypes[i]);
  1819. }
  1820. component->saAmfCompCsTypes[i] = NULL;
  1821. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCategory);
  1822. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCapability);
  1823. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxActiveCsi);
  1824. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxStandbyCsi);
  1825. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  1826. component->saAmfCompCmdEnv = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  1827. for (i = 0; i < cnt; i++) {
  1828. tmp = amf_deserialize_SaStringT (tmp, &component->saAmfCompCmdEnv[i]);
  1829. }
  1830. component->saAmfCompCmdEnv[i] = NULL;
  1831. tmp = amf_deserialize_SaUint32T (
  1832. tmp, &component->saAmfCompDefaultClcCliTimeout);
  1833. tmp = amf_deserialize_SaUint32T (
  1834. tmp, &component->saAmfCompDefaultCallbackTimeOut);
  1835. tmp = amf_deserialize_SaStringT (
  1836. tmp, &component->saAmfCompInstantiateCmd);
  1837. tmp = amf_deserialize_SaStringT (
  1838. tmp, &component->saAmfCompInstantiateCmdArgv);
  1839. tmp = amf_deserialize_SaUint32T (
  1840. tmp, &component->saAmfCompInstantiateTimeout);
  1841. tmp = amf_deserialize_SaUint32T (
  1842. tmp, &component->saAmfCompInstantiationLevel);
  1843. tmp = amf_deserialize_SaUint32T (
  1844. tmp, &component->saAmfCompNumMaxInstantiateWithoutDelay);
  1845. tmp = amf_deserialize_SaUint32T (
  1846. tmp, &component->saAmfCompNumMaxInstantiateWithDelay);
  1847. tmp = amf_deserialize_SaUint32T (
  1848. tmp, &component->saAmfCompDelayBetweenInstantiateAttempts);
  1849. tmp = amf_deserialize_SaStringT (
  1850. tmp, &component->saAmfCompTerminateCmd);
  1851. tmp = amf_deserialize_SaUint32T (
  1852. tmp, &component->saAmfCompTerminateTimeout);
  1853. tmp = amf_deserialize_SaStringT (
  1854. tmp, &component->saAmfCompTerminateCmdArgv);
  1855. tmp = amf_deserialize_SaStringT (
  1856. tmp, &component->saAmfCompCleanupCmd);
  1857. tmp = amf_deserialize_SaUint32T (
  1858. tmp, &component->saAmfCompCleanupTimeout);
  1859. tmp = amf_deserialize_SaStringT (
  1860. tmp, &component->saAmfCompCleanupCmdArgv);
  1861. tmp = amf_deserialize_SaStringT (
  1862. tmp, &component->saAmfCompAmStartCmd);
  1863. tmp = amf_deserialize_SaUint32T (
  1864. tmp, &component->saAmfCompAmStartTimeout);
  1865. tmp = amf_deserialize_SaStringT (
  1866. tmp, &component->saAmfCompAmStartCmdArgv);
  1867. tmp = amf_deserialize_SaUint32T (
  1868. tmp, &component->saAmfCompNumMaxAmStartAttempt);
  1869. tmp = amf_deserialize_SaStringT (
  1870. tmp, &component->saAmfCompAmStopCmd);
  1871. tmp = amf_deserialize_SaUint32T (
  1872. tmp, &component->saAmfCompAmStopTimeout);
  1873. tmp = amf_deserialize_SaStringT (
  1874. tmp, &component->saAmfCompAmStopCmdArgv);
  1875. tmp = amf_deserialize_SaUint32T (
  1876. tmp, &component->saAmfCompNumMaxAmStopAttempt);
  1877. tmp = amf_deserialize_SaUint32T (
  1878. tmp, &component->saAmfCompTerminateCallbackTimeout);
  1879. tmp = amf_deserialize_SaUint32T (
  1880. tmp, &component->saAmfCompCSISetCallbackTimeout);
  1881. tmp = amf_deserialize_SaUint32T (
  1882. tmp, &component->saAmfCompQuiescingCompleteTimeout);
  1883. tmp = amf_deserialize_SaUint32T (
  1884. tmp, &component->saAmfCompCSIRmvCallbackTimeout);
  1885. tmp = amf_deserialize_SaUint32T (
  1886. tmp, &component->saAmfCompRecoveryOnError);
  1887. tmp = amf_deserialize_SaUint32T (
  1888. tmp, &component->saAmfCompDisableRestart);
  1889. tmp = amf_deserialize_SaNameT (
  1890. tmp, &component->saAmfCompProxyCsi);
  1891. tmp = amf_deserialize_SaUint32T (
  1892. tmp, &component->saAmfCompOperState);
  1893. tmp = amf_deserialize_SaUint32T (
  1894. tmp, &component->saAmfCompPresenceState);
  1895. tmp = amf_deserialize_SaUint32T (
  1896. tmp, &component->saAmfCompRestartCount);
  1897. tmp = amf_deserialize_SaNameT (
  1898. tmp, &component->saAmfCompCurrProxyName);
  1899. tmp = amf_deserialize_SaStringT (
  1900. tmp, &component->clccli_path);
  1901. tmp = amf_deserialize_SaUint32T (
  1902. tmp, &component->comptype);
  1903. tmp = amf_deserialize_SaUint32T (
  1904. tmp, &component->error_suspected);
  1905. return component;
  1906. }
  1907. void *amf_healthcheck_serialize (struct amf_healthcheck *healthcheck, int *len)
  1908. {
  1909. char *buf = NULL;
  1910. int offset = 0, size = 0;
  1911. TRACE8 ("%s", healthcheck->safHealthcheckKey.key);
  1912. buf = amf_serialize_opaque (buf, &size, &offset,
  1913. &healthcheck->safHealthcheckKey.key, SA_AMF_HEALTHCHECK_KEY_MAX);
  1914. buf = amf_serialize_SaUint16T (buf, &size, &offset,
  1915. healthcheck->safHealthcheckKey.keyLen);
  1916. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  1917. healthcheck->saAmfHealthcheckMaxDuration);
  1918. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  1919. healthcheck->saAmfHealthcheckPeriod);
  1920. *len = offset;
  1921. return buf;
  1922. }
  1923. struct amf_healthcheck *amf_healthcheck_deserialize (
  1924. struct amf_comp *comp, char *buf)
  1925. {
  1926. char *tmp = buf;
  1927. int cnt;
  1928. amf_healthcheck_t *healthcheck = amf_healthcheck_new (comp);
  1929. tmp = amf_deserialize_opaque (tmp, &healthcheck->safHealthcheckKey.key, &cnt);
  1930. tmp = amf_deserialize_SaUint16T (tmp,
  1931. &healthcheck->safHealthcheckKey.keyLen);
  1932. tmp = amf_deserialize_SaUint32T (tmp,
  1933. &healthcheck->saAmfHealthcheckMaxDuration);
  1934. tmp = amf_deserialize_SaUint32T (tmp,
  1935. &healthcheck->saAmfHealthcheckPeriod);
  1936. return healthcheck;
  1937. }
  1938. amf_healthcheck_t *amf_healthcheck_new (struct amf_comp *comp)
  1939. {
  1940. amf_healthcheck_t *healthcheck = amf_calloc (1, sizeof (amf_healthcheck_t));
  1941. healthcheck->comp = comp;
  1942. healthcheck->next = comp->healthcheck_head;
  1943. comp->healthcheck_head = healthcheck;
  1944. return healthcheck;
  1945. }