amfcomp.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. /** @file amfcomp.c
  2. *
  3. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  4. * Author: Steven Dake (sdake@mvista.com)
  5. *
  6. * Copyright (c) 2006 Ericsson AB.
  7. * Author: Hans Feldt
  8. * - Introduced AMF B.02 information model
  9. * - Use DN in API and multicast messages
  10. * - (Re-)Introduction of event based multicast messages
  11. * - Refactoring of code into several AMF files
  12. * Author: Anders Eriksson, Lars Holm
  13. * - Component/SU restart, SU failover
  14. *
  15. * All rights reserved.
  16. *
  17. *
  18. * This software licensed under BSD license, the text of which follows:
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimer.
  25. * - Redistributions in binary form must reproduce the above copyright notice,
  26. * this list of conditions and the following disclaimer in the documentation
  27. * and/or other materials provided with the distribution.
  28. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  29. * contributors may be used to endorse or promote products derived from this
  30. * software without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  36. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  42. * THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. * AMF Component Class Implementation
  45. *
  46. * This file contains functions for handling AMF-components. It can be
  47. * viewed as the implementation of the AMF Component class (called comp)
  48. * as described in SAI-Overview-B.02.01. The SA Forum specification
  49. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  50. * and is referred to as 'the spec' below.
  51. *
  52. * The functions in this file are responsible for handling the following
  53. * types of components:
  54. * - sa-aware components
  55. * (proxy or non-proxy)
  56. * - non-sa-aware components
  57. * (non-proxied non-pre-instantiable and
  58. * proxied pre-instantiable or not pre-instantiable)
  59. *
  60. * The functions of this file are also responsible for:
  61. * - handling all communication with the AMF API library supported by the
  62. * AMF main function, see below
  63. * - instantiating and terminating components upon request
  64. * - updating the ha-state of the CSI-assignment related to the component
  65. * - initiating an error report to the parent SU
  66. * - handling all run time attributes of the AMF Component; all cached
  67. * attributes are stored as variables and sent to the IMM service
  68. * upon the changes described in the specification.
  69. *
  70. * Incoming events from the AMF library is primarily handled by the AMF
  71. * main function which:
  72. * <1> transforms the incoming event to an event that is multicast
  73. * to all AMF service instances in the cluster
  74. * <2> the event received from multicast is tranformed to a function
  75. * call of the external interface of comp
  76. *
  77. * Outgoing events to the AMF library is handled by static functions called
  78. * lib_<api callback function name>_request which creates an invocation handle
  79. * unique to this call and stores any variables comp want to associate to the
  80. * call back so it is possible to pick them up when the component responses
  81. * through the API. Finally, a timer is started to supervise that a response
  82. * really is received.
  83. *
  84. * Comp initiates error reports to its parent SU in the cases described in
  85. * paragraph 3.3.2.2 in the spec. Comp delegates all actions to SU except
  86. * - it stores the received or pre-configured recommended recovery
  87. * action
  88. * - sets the operational state to DISABLED unless the
  89. * recommended recovery action was SA_AMF_COMP_RESTART. (In this case
  90. * SU or node may set operational state of the component later on
  91. * when it has been fully investigated that no escallation to a
  92. * more powerful recovery action shall be made.)
  93. *
  94. * Comp contains the following state machines:
  95. * - presence state machine (PRSM)
  96. * - operational state machine (OPSM)
  97. * - readiness state machine (RESM)
  98. * - ha state per component service instance (CSI)
  99. *
  100. * The behaviour of comp is mainly controlled by the presence state machine,
  101. * while the operational and readiness state machines are used only to report
  102. * information to its parent (service unit SU) and management (IMM). Comp does
  103. * not control the logic to assign a CSI to itself and neither to decide the
  104. * value of the ha-state but only to faciltate the communication of the CSI
  105. * set (or remove) order and to evaluate the response from the library.
  106. *
  107. * The presence state machine implements all the states described in the
  108. * specification.
  109. * The '-ING' states of PRSM are designed as composite states (UML terminology).
  110. * Being a composite state means that the state contains substates.
  111. * PRSM composite states are:
  112. * - TERMINATING (TERMINATE and CLEANUP)
  113. * - INSTANTIATING (INSTANTIATE, INSTANTIATEDELAY and CLEANUP)
  114. * - RESTARTING (TERMINATE, INSTANTIATE, INSTANTIATEDELAY and CLEANUP)
  115. *
  116. * The reason for introducing these composite states is to make it easier to
  117. * understand the implementation of the behaviour described in paragraphs
  118. * 4.1 - 4.6 in the spec. The comp PRSM implements all the logic described
  119. * except for node reboot, which is handled by the AMF Node class.
  120. * Also PRSM reports all changes of state to its parent SU.
  121. *
  122. */
  123. #include <sys/types.h>
  124. #include <unistd.h>
  125. #include <sys/wait.h>
  126. #include <stdio.h>
  127. #include <string.h>
  128. #include <stdlib.h>
  129. #include <errno.h>
  130. #include <assert.h>
  131. #include "../include/saAis.h"
  132. #include "../include/saAmf.h"
  133. #include "../include/ipc_gen.h"
  134. #include "../include/ipc_amf.h"
  135. #include "totempg.h"
  136. #include "main.h"
  137. #include "ipc.h"
  138. #include "service.h"
  139. #include "util.h"
  140. #include "amf.h"
  141. #include "print.h"
  142. enum clc_command_run_operation_type {
  143. CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE = 1,
  144. CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE = 2,
  145. CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP = 3
  146. };
  147. struct clc_command_run_data {
  148. struct amf_comp *comp;
  149. enum clc_command_run_operation_type type;
  150. void (*completion_callback) (void *context);
  151. };
  152. struct clc_interface {
  153. int (*instantiate) (struct amf_comp *comp);
  154. int (*terminate) (struct amf_comp *comp);
  155. int (*cleanup) (struct amf_comp *comp);
  156. };
  157. struct csi_remove_callback_data {
  158. struct amf_csi *csi;
  159. };
  160. struct component_terminate_callback_data {
  161. struct amf_comp *comp;
  162. };
  163. static void comp_presence_state_set (
  164. struct amf_comp *comp,
  165. SaAmfPresenceStateT presence_state);
  166. static int clc_cli_instantiate (struct amf_comp *comp);
  167. static int clc_instantiate_callback (struct amf_comp *comp);
  168. static int clc_csi_set_callback (struct amf_comp *comp);
  169. static int clc_cli_terminate (struct amf_comp *comp);
  170. static int lib_comp_terminate_request (struct amf_comp *comp);
  171. static int clc_csi_remove_callback (struct amf_comp *comp);
  172. static int clc_cli_cleanup (struct amf_comp *comp);
  173. static int clc_cli_cleanup_local (struct amf_comp *comp);
  174. static void healthcheck_deactivate (struct amf_healthcheck *healthcheck_active);
  175. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck);
  176. static void timer_function_healthcheck_tmo (void *_healthcheck);
  177. static void lib_csi_set_request (
  178. struct amf_comp *comp,
  179. struct amf_csi_assignment *csi_assignment);
  180. /*
  181. * Life cycle functions
  182. */
  183. static struct clc_interface clc_interface_sa_aware = {
  184. clc_cli_instantiate,
  185. lib_comp_terminate_request,
  186. clc_cli_cleanup
  187. };
  188. static struct clc_interface clc_interface_proxied_pre = {
  189. clc_instantiate_callback,
  190. lib_comp_terminate_request,
  191. clc_cli_cleanup
  192. };
  193. static struct clc_interface clc_interface_proxied_non_pre = {
  194. clc_csi_set_callback,
  195. clc_csi_remove_callback,
  196. clc_cli_cleanup_local
  197. };
  198. static struct clc_interface clc_interface_non_proxied_non_saware = {
  199. clc_cli_instantiate,
  200. clc_cli_terminate,
  201. clc_cli_cleanup_local
  202. };
  203. static struct clc_interface *clc_interfaces[4] = {
  204. &clc_interface_sa_aware,
  205. &clc_interface_proxied_pre,
  206. &clc_interface_proxied_non_pre,
  207. &clc_interface_non_proxied_non_saware
  208. };
  209. struct invocation {
  210. void *data;
  211. int interface;
  212. int active;
  213. };
  214. static struct invocation *invocation_entries = 0;
  215. static int invocation_entries_size = 0;
  216. static int invocation_create (
  217. int interface,
  218. void *data)
  219. {
  220. struct invocation *invocation_addr = 0;
  221. struct invocation *invocation_temp;
  222. int i;
  223. int loc = 0;
  224. for (i = 0; i < invocation_entries_size; i++) {
  225. if (invocation_entries[i].active == 0) {
  226. invocation_addr = &invocation_entries[i];
  227. loc = i;
  228. break;
  229. }
  230. }
  231. if (invocation_addr == 0) {
  232. invocation_temp = (struct invocation *)realloc (invocation_entries,
  233. (invocation_entries_size + 1) * sizeof (struct invocation));
  234. if (invocation_temp == 0) {
  235. return (-1);
  236. }
  237. invocation_entries = invocation_temp;
  238. invocation_addr = &invocation_entries[invocation_entries_size];
  239. loc = invocation_entries_size;
  240. invocation_entries_size += 1;
  241. }
  242. invocation_addr->interface = interface;
  243. invocation_addr->data = data;
  244. invocation_addr->active = 1;
  245. return (loc);
  246. }
  247. static int invocation_get_and_destroy (SaUint64T invocation, int *interface,
  248. void **data)
  249. {
  250. if (invocation > invocation_entries_size) {
  251. return (-1);
  252. }
  253. if (invocation_entries[invocation].active == 0) {
  254. return (-1);
  255. }
  256. *interface = invocation_entries[invocation].interface;
  257. *data = invocation_entries[invocation].data;
  258. memset (&invocation_entries[invocation], 0, sizeof (struct invocation));
  259. return (0);
  260. }
  261. static int invocation_get (SaUint64T invocation, int *interface,
  262. void **data)
  263. {
  264. if (invocation > invocation_entries_size) {
  265. return (-1);
  266. }
  267. if (invocation_entries[invocation].active == 0) {
  268. return (-1);
  269. }
  270. *interface = invocation_entries[invocation].interface;
  271. *data = invocation_entries[invocation].data;
  272. return (0);
  273. }
  274. static void invocation_destroy_by_data (void *data)
  275. {
  276. int i;
  277. for (i = 0; i < invocation_entries_size; i++) {
  278. if (invocation_entries[i].data == data) {
  279. memset (&invocation_entries[i], 0,
  280. sizeof (struct invocation));
  281. break;
  282. }
  283. }
  284. }
  285. /**
  286. * Set suspected error flag and report to SU.
  287. *
  288. * @param comp
  289. * @param recommended_recovery
  290. */
  291. static void report_error_suspected (
  292. struct amf_comp *comp,
  293. SaAmfRecommendedRecoveryT recommended_recovery)
  294. {
  295. comp->error_suspected = 1;
  296. amf_su_comp_error_suspected (
  297. comp->su, comp, recommended_recovery);
  298. }
  299. char *amf_comp_dn_make (struct amf_comp *comp, SaNameT *name)
  300. {
  301. int i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  302. "safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  303. comp->name.value, comp->su->name.value,
  304. comp->su->sg->name.value, comp->su->sg->application->name.value);
  305. assert (i <= SA_MAX_NAME_LENGTH);
  306. name->length = i;
  307. return (char *)name->value;
  308. }
  309. #ifndef xprintf
  310. #define xprintf(...)
  311. #endif
  312. static void *clc_command_run (void *context)
  313. {
  314. struct clc_command_run_data *clc_command_run_data =
  315. (struct clc_command_run_data *)context;
  316. pid_t pid;
  317. int res;
  318. char *argv[10];
  319. char *envp[10];
  320. int status;
  321. char path[PATH_MAX];
  322. char *cmd = 0;
  323. char *comp_argv = 0;
  324. char comp_name[SA_MAX_NAME_LENGTH];
  325. int i;
  326. ENTER_VOID();
  327. pid = fork();
  328. if (pid == -1) {
  329. fprintf (stderr, "Couldn't fork process %s\n", strerror (errno));
  330. return (0);
  331. }
  332. if (pid) {
  333. xprintf ("waiting for pid %d to finish\n", pid);
  334. waitpid (pid, &status, 0);
  335. if (WEXITSTATUS(status) != 0) {
  336. fprintf (stderr, "Error: CLC_CLI failed with exit status:"
  337. " %d - %s\n", WEXITSTATUS(status),
  338. strerror (WEXITSTATUS(status)));
  339. /*
  340. * TODO: remove this and handle properly later...
  341. */
  342. openais_exit_error (AIS_DONE_FATAL_ERR);
  343. }
  344. xprintf ("process (%d) finished with %d\n", pid, status);
  345. if (clc_command_run_data->completion_callback) {
  346. clc_command_run_data->completion_callback (context);
  347. }
  348. pthread_exit(0);
  349. }
  350. switch (clc_command_run_data->type) {
  351. case CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE:
  352. cmd = clc_command_run_data->comp->saAmfCompInstantiateCmd;
  353. comp_argv = clc_command_run_data->comp->saAmfCompInstantiateCmdArgv;
  354. break;
  355. case CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE:
  356. cmd = clc_command_run_data->comp->saAmfCompTerminateCmd;
  357. comp_argv = clc_command_run_data->comp->saAmfCompTerminateCmdArgv;
  358. break;
  359. case CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP:
  360. cmd = clc_command_run_data->comp->saAmfCompCleanupCmd;
  361. comp_argv = clc_command_run_data->comp->saAmfCompCleanupCmdArgv;
  362. break;
  363. default:
  364. assert (0 != 1);
  365. break;
  366. }
  367. /* If command is not an absolute path, search for paths in parent objects */
  368. if (cmd[0] != '/') {
  369. if (strlen (clc_command_run_data->comp->clccli_path)) {
  370. sprintf (path, "%s/%s",
  371. clc_command_run_data->comp->clccli_path, cmd);
  372. } else if (strlen (clc_command_run_data->comp->su->clccli_path)) {
  373. sprintf (path, "%s/%s",
  374. clc_command_run_data->comp->su->clccli_path, cmd);
  375. } else if (strlen (clc_command_run_data->comp->su->sg->clccli_path)) {
  376. sprintf (path, "%s/%s",
  377. clc_command_run_data->comp->su->sg->clccli_path, cmd);
  378. } else if (strlen (clc_command_run_data->comp->su->sg->application->clccli_path)) {
  379. sprintf (path, "%s/%s",
  380. clc_command_run_data->comp->su->sg->application->clccli_path, cmd);
  381. }
  382. cmd = path;
  383. }
  384. argv[0] = cmd;
  385. {
  386. /* make a proper argv array */
  387. i = 1;
  388. char *ptrptr;
  389. char *arg = strtok_r(comp_argv, " ", &ptrptr);
  390. while (arg) {
  391. argv[i] = arg;
  392. arg = strtok_r(NULL, " ", & ptrptr);
  393. i++;
  394. }
  395. }
  396. argv[i] = NULL;
  397. assert (i < 10);
  398. envp[0] = comp_name;
  399. i = snprintf(comp_name, SA_MAX_NAME_LENGTH,
  400. "SA_AMF_COMPONENT_NAME=safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  401. clc_command_run_data->comp->name.value,
  402. clc_command_run_data->comp->su->name.value,
  403. clc_command_run_data->comp->su->sg->name.value,
  404. clc_command_run_data->comp->su->sg->application->name.value);
  405. assert (i <= SA_MAX_NAME_LENGTH);
  406. for (i = 1; clc_command_run_data->comp->saAmfCompCmdEnv &&
  407. clc_command_run_data->comp->saAmfCompCmdEnv[i - 1]; i++) {
  408. envp[i] = clc_command_run_data->comp->saAmfCompCmdEnv[i - 1];
  409. }
  410. envp[i] = NULL;
  411. assert (i < 10);
  412. xprintf ("running command '%s' with environment:\n", cmd);
  413. for (i = 0; envp[i] != NULL; i++) {
  414. xprintf (" %s\n", envp[i]);
  415. }
  416. xprintf (" and argv:\n");
  417. for (i = 0; argv[i] != NULL; i++) {
  418. xprintf (" %s\n", argv[i]);
  419. }
  420. res = execve (cmd, argv, envp);
  421. if (res == -1) {
  422. fprintf (stderr, "Couldn't exec program %s (%s)\n",
  423. cmd, strerror (errno));
  424. }
  425. assert (res != -1);
  426. return (0);
  427. }
  428. /*
  429. * Instantiate possible operations
  430. */
  431. static int clc_cli_instantiate (struct amf_comp *comp)
  432. {
  433. int res;
  434. pthread_t thread;
  435. pthread_attr_t thread_attr; /* thread attribute */
  436. struct clc_command_run_data *clc_command_run_data;
  437. ENTER("comp '%s'\n", getSaNameT (&comp->name));
  438. clc_command_run_data = malloc (sizeof (struct clc_command_run_data));
  439. if (clc_command_run_data == NULL) {
  440. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  441. }
  442. clc_command_run_data->comp = comp;
  443. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE;
  444. clc_command_run_data->completion_callback = NULL;
  445. pthread_attr_init (&thread_attr);
  446. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  447. res = pthread_create (&thread, &thread_attr, clc_command_run,
  448. (void *)clc_command_run_data);
  449. if (res != 0) {
  450. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  451. }
  452. // TODO error code from pthread_create
  453. return (res);
  454. }
  455. static int clc_instantiate_callback (struct amf_comp *comp)
  456. {
  457. ENTER("comp %s\n", getSaNameT (&comp->name));
  458. return (0);
  459. }
  460. static int clc_csi_set_callback (struct amf_comp *comp)
  461. {
  462. ENTER("comp %s\n", getSaNameT (&comp->name));
  463. return (0);
  464. }
  465. /*
  466. * Terminate possible operations
  467. */
  468. static int clc_cli_terminate (struct amf_comp *comp)
  469. {
  470. ENTER("comp %s\n", getSaNameT (&comp->name));
  471. return (0);
  472. }
  473. /**
  474. * Request component to terminate itself
  475. * @param comp
  476. *
  477. * @return int
  478. */
  479. static int lib_comp_terminate_request (struct amf_comp *comp)
  480. {
  481. struct res_lib_amf_componentterminatecallback res_lib;
  482. struct component_terminate_callback_data *component_terminate_callback_data;
  483. ENTER("comp %s\n", getSaNameT (&comp->name));
  484. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTTERMINATECALLBACK;
  485. res_lib.header.size = sizeof (struct res_lib_amf_componentterminatecallback);
  486. res_lib.header.error = SA_AIS_OK;
  487. memcpy (&res_lib.compName, &comp->name, sizeof (SaNameT));
  488. component_terminate_callback_data =
  489. malloc (sizeof (struct component_terminate_callback_data));
  490. if (component_terminate_callback_data == NULL) {
  491. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  492. }
  493. component_terminate_callback_data->comp = comp;
  494. res_lib.invocation =
  495. invocation_create (
  496. AMF_RESPONSE_COMPONENTTERMINATECALLBACK,
  497. component_terminate_callback_data);
  498. openais_conn_send_response (
  499. openais_conn_partner_get (comp->conn),
  500. &res_lib,
  501. sizeof (struct res_lib_amf_componentterminatecallback));
  502. return (0);
  503. }
  504. static int clc_csi_remove_callback (struct amf_comp *comp)
  505. {
  506. dprintf ("clc_tcsi_remove_callback\n");
  507. return (0);
  508. }
  509. /*
  510. * Clean up completed
  511. */
  512. static void mcast_cleanup_completion_event (void *context)
  513. {
  514. struct clc_command_run_data *clc_command_run_data =
  515. (struct clc_command_run_data *)context;
  516. struct req_exec_amf_clc_cleanup_completed req;
  517. struct iovec iovec;
  518. TRACE2("CLC cleanup done for '%s'",
  519. clc_command_run_data->comp->name.value);
  520. req.header.size = sizeof (struct req_exec_amf_clc_cleanup_completed);
  521. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  522. MESSAGE_REQ_EXEC_AMF_CLC_CLEANUP_COMPLETED);
  523. amf_comp_dn_make (clc_command_run_data->comp, &req.compName);
  524. iovec.iov_base = (char *)&req;
  525. iovec.iov_len = sizeof (req);
  526. assert (totempg_groups_mcast_joined (openais_group_handle,
  527. &iovec, 1, TOTEMPG_AGREED) == 0);
  528. }
  529. /*
  530. * Cleanup possible operations
  531. */
  532. static int clc_cli_cleanup (struct amf_comp *comp)
  533. {
  534. int res;
  535. pthread_t thread;
  536. pthread_attr_t thread_attr; /* thread attribute */
  537. struct clc_command_run_data *clc_command_run_data;
  538. dprintf ("clc_cli_cleanup\n");
  539. clc_command_run_data = malloc (sizeof (struct clc_command_run_data));
  540. if (clc_command_run_data == NULL) {
  541. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  542. }
  543. clc_command_run_data->comp = comp;
  544. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP;
  545. clc_command_run_data->completion_callback = mcast_cleanup_completion_event;
  546. pthread_attr_init (&thread_attr);
  547. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  548. res = pthread_create (&thread, &thread_attr, clc_command_run,
  549. (void *)clc_command_run_data);
  550. if (res != 0) {
  551. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  552. }
  553. // TODO error code from pthread_create
  554. return (res);
  555. }
  556. static int clc_cli_cleanup_local (struct amf_comp *comp)
  557. {
  558. dprintf ("clc_cli_cleanup_local\n");
  559. return (0);
  560. }
  561. #if 0
  562. static int clc_terminate (struct amf_comp *comp)
  563. {
  564. int res;
  565. dprintf ("clc terminate for comp %s\n", getSaNameT (&comp->name));
  566. assert (0);
  567. operational_state_comp_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  568. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  569. res = clc_interfaces[comp->comptype]->terminate (comp);
  570. return (0);
  571. }
  572. #endif
  573. struct amf_healthcheck *amf_comp_find_healthcheck (
  574. struct amf_comp *comp, SaAmfHealthcheckKeyT *key)
  575. {
  576. struct amf_healthcheck *healthcheck;
  577. struct amf_healthcheck *ret_healthcheck = 0;
  578. if (key == NULL) {
  579. return NULL;
  580. }
  581. for (healthcheck = comp->healthcheck_head;
  582. healthcheck != NULL;
  583. healthcheck = healthcheck->next) {
  584. if (memcmp (key, &healthcheck->safHealthcheckKey,
  585. sizeof (SaAmfHealthcheckKeyT)) == 0) {
  586. ret_healthcheck = healthcheck;
  587. break;
  588. }
  589. }
  590. return (ret_healthcheck);
  591. }
  592. struct amf_comp *amf_comp_create(struct amf_su *su)
  593. {
  594. struct amf_comp *comp = calloc (1, sizeof (struct amf_comp));
  595. if (comp == NULL) {
  596. openais_exit_error(AIS_DONE_OUT_OF_MEMORY);
  597. }
  598. comp->next = su->comp_head;
  599. su->comp_head = comp;
  600. comp->su = su;
  601. comp->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  602. comp->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  603. comp->saAmfCompNumMaxInstantiateWithoutDelay = 2;
  604. comp->saAmfCompNumMaxAmStartAttempt = 2;
  605. comp->saAmfCompNumMaxAmStopAttempt = 2;
  606. return comp;
  607. }
  608. struct amf_comp *amf_comp_find (struct amf_cluster *cluster, SaNameT *name)
  609. {
  610. struct amf_application *app;
  611. struct amf_sg *sg;
  612. struct amf_su *su;
  613. struct amf_comp *comp = NULL;
  614. char *app_name;
  615. char *sg_name;
  616. char *su_name;
  617. char *comp_name;
  618. char *ptrptr;
  619. char *buf;
  620. /* malloc new buffer since strtok_r writes to its first argument */
  621. buf = malloc (name->length + 1);
  622. memcpy (buf, name->value,name ->length);
  623. comp_name = strtok_r(buf, ",", &ptrptr);
  624. su_name = strtok_r(NULL, ",", &ptrptr);
  625. sg_name = strtok_r(NULL, ",", &ptrptr);
  626. app_name = strtok_r(NULL, ",", &ptrptr);
  627. if (comp_name == NULL || su_name == NULL ||
  628. sg_name == NULL || app_name == NULL) {
  629. goto end;
  630. }
  631. comp_name += 8;
  632. su_name += 6;
  633. sg_name += 6;
  634. app_name += 7;
  635. for (app = cluster->application_head; app != NULL; app = app->next) {
  636. if (strncmp (app_name,
  637. (char*)app->name.value, app->name.length) == 0) {
  638. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  639. if (strncmp (sg_name, (char*)sg->name.value,
  640. sg->name.length) == 0) {
  641. for (su = sg->su_head; su != NULL; su = su->next) {
  642. if (strncmp (su_name, (char*)su->name.value,
  643. su->name.length) == 0) {
  644. for (comp = su->comp_head;
  645. comp != NULL;
  646. comp = comp->next) {
  647. if (strncmp (comp_name,
  648. (char*)comp->name.value,
  649. comp->name.length) == 0) {
  650. goto end;
  651. }
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. end:
  660. free (buf);
  661. return comp;
  662. }
  663. void amf_comp_healthcheck_deactivate (struct amf_comp *comp)
  664. {
  665. struct amf_healthcheck *healthcheck;
  666. if (!amf_su_is_local (comp->su))
  667. return;
  668. ENTER ("'%s'\n", getSaNameT (&comp->name));
  669. for (healthcheck = comp->healthcheck_head;
  670. healthcheck != NULL;
  671. healthcheck = healthcheck->next) {
  672. if (healthcheck->active) {
  673. healthcheck_deactivate (healthcheck);
  674. }
  675. }
  676. }
  677. static void comp_ha_state_set ( struct amf_comp *comp,
  678. struct amf_csi_assignment *csi_assignment,
  679. SaAmfHAStateT ha_state)
  680. {
  681. /* set confirmed HA state */
  682. csi_assignment->saAmfCSICompHAState = ha_state;
  683. TRACE1 ("Setting comp '%s', SU '%s' CSI '%s', HA state: %s\n",
  684. comp->name.value, comp->su->name.value,
  685. csi_assignment->csi->name.value,
  686. amf_ha_state (csi_assignment->saAmfCSICompHAState));
  687. amf_si_comp_set_ha_state_done (csi_assignment->csi->si, csi_assignment);
  688. }
  689. static void comp_presence_state_set (struct amf_comp *comp,
  690. SaAmfPresenceStateT presence_state)
  691. {
  692. comp->saAmfCompPresenceState = presence_state;
  693. TRACE1 ("Setting comp '%s', SU '%s' presence state: %s\n",
  694. comp->name.value, comp->su->name.value,
  695. amf_presence_state (comp->saAmfCompPresenceState));
  696. amf_su_comp_state_changed (
  697. comp->su, comp, SA_AMF_PRESENCE_STATE, presence_state);
  698. }
  699. #if 0
  700. static void lib_csi_remove_request (struct amf_comp *comp,
  701. struct amf_csi *csi)
  702. {
  703. struct res_lib_amf_csiremovecallback res_lib_amf_csiremovecallback;
  704. struct csi_remove_callback_data *csi_remove_callback_data;
  705. dprintf ("\t%s\n", getSaNameT (&comp->name));
  706. res_lib_amf_csiremovecallback.header.id = MESSAGE_RES_AMF_CSIREMOVECALLBACK;
  707. res_lib_amf_csiremovecallback.header.size = sizeof (struct res_lib_amf_csiremovecallback);
  708. res_lib_amf_csiremovecallback.header.error = SA_AIS_OK;
  709. csi_remove_callback_data = malloc (sizeof (struct csi_remove_callback_data));
  710. assert (csi_remove_callback_data); // TODO failure here of malloc
  711. csi_remove_callback_data->csi = csi;
  712. res_lib_amf_csiremovecallback.invocation =
  713. invocation_create (
  714. AMF_RESPONSE_CSIREMOVECALLBACK,
  715. csi_remove_callback_data);
  716. memcpy (&res_lib_amf_csiremovecallback.compName,
  717. &comp->name, sizeof (SaNameT));
  718. memcpy (&res_lib_amf_csiremovecallback.csiName,
  719. &csi->name, sizeof (SaNameT));
  720. res_lib_amf_csiremovecallback.csiFlags = 0;
  721. openais_conn_send_response (
  722. openais_conn_partner_get (comp->conn),
  723. &res_lib_amf_csiremovecallback,
  724. sizeof (struct res_lib_amf_csiremovecallback));
  725. }
  726. #endif
  727. struct amf_csi_assignment *amf_comp_get_next_csi_assignment (
  728. struct amf_comp *component, const struct amf_csi_assignment *csi_assignment)
  729. {
  730. struct amf_si *si;
  731. struct amf_csi *csi;
  732. struct amf_csi_assignment *tmp_csi_assignment;
  733. SaNameT dn;
  734. amf_comp_dn_make (component, &dn);
  735. if (csi_assignment == NULL) {
  736. si = component->su->sg->application->si_head;
  737. csi = si->csi_head;
  738. tmp_csi_assignment = csi->assigned_csis;
  739. } else {
  740. tmp_csi_assignment = csi_assignment->next;
  741. if (tmp_csi_assignment == NULL) {
  742. csi = csi_assignment->csi->next;
  743. if (csi == NULL) {
  744. si = csi_assignment->csi->si->next;
  745. if (si == NULL) {
  746. return NULL;
  747. } else {
  748. csi = si->csi_head;
  749. tmp_csi_assignment = csi->assigned_csis;
  750. }
  751. } else {
  752. si = csi->si;
  753. tmp_csi_assignment = csi->assigned_csis;
  754. }
  755. } else {
  756. csi = tmp_csi_assignment->csi;
  757. si = csi->si;
  758. }
  759. }
  760. for (; si != NULL; si = si->next) {
  761. if (tmp_csi_assignment == NULL && csi == NULL && si != NULL) {
  762. csi = si->csi_head;
  763. tmp_csi_assignment = csi->assigned_csis;
  764. }
  765. for (; csi != NULL; csi = csi->next) {
  766. if (tmp_csi_assignment == NULL && csi != NULL) {
  767. tmp_csi_assignment = csi->assigned_csis;
  768. }
  769. for (; tmp_csi_assignment != NULL;
  770. tmp_csi_assignment = tmp_csi_assignment->next) {
  771. if (name_match (&tmp_csi_assignment->name, &dn)) {
  772. return tmp_csi_assignment;
  773. }
  774. }
  775. }
  776. }
  777. return NULL;
  778. }
  779. void amf_comp_foreach_csi_assignment (
  780. struct amf_comp *component,
  781. void (*foreach_fn)(struct amf_comp *component,
  782. struct amf_csi_assignment *csi_assignment))
  783. {
  784. struct amf_csi_assignment *csi_assignment;
  785. assert (foreach_fn != NULL);
  786. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  787. while (csi_assignment != NULL) {
  788. foreach_fn (component, csi_assignment);
  789. csi_assignment = amf_comp_get_next_csi_assignment (
  790. component, csi_assignment);
  791. }
  792. }
  793. static struct amf_csi_assignment *csi_assignment_find_in (
  794. struct amf_comp *component, SaNameT *csi_name)
  795. {
  796. struct amf_csi_assignment *csi_assignment;
  797. SaNameT dn;
  798. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  799. while (csi_assignment != NULL) {
  800. amf_csi_dn_make (csi_assignment->csi, &dn);
  801. if (name_match (csi_name, &dn)) {
  802. return csi_assignment;
  803. }
  804. csi_assignment = amf_comp_get_next_csi_assignment (
  805. component, csi_assignment);
  806. }
  807. return NULL;
  808. }
  809. static void healthcheck_deactivate (
  810. struct amf_healthcheck *healthcheck_active)
  811. {
  812. dprintf ("deactivating healthcheck for component %s\n",
  813. getSaNameT (&healthcheck_active->comp->name));
  814. poll_timer_delete (aisexec_poll_handle,
  815. healthcheck_active->timer_handle_period);
  816. poll_timer_delete (aisexec_poll_handle,
  817. healthcheck_active->timer_handle_duration);
  818. invocation_destroy_by_data ((void *)healthcheck_active);
  819. healthcheck_active->active = 0;
  820. }
  821. /**
  822. * This function is called by the timer subsystem when AMF should request
  823. * a new healthcheck from a component.
  824. * @param data
  825. */
  826. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  827. {
  828. struct amf_healthcheck *healthcheck = _healthcheck;
  829. /* send healthcheck request to component */
  830. lib_healthcheck_request (healthcheck);
  831. /* start duration timer for response */
  832. poll_timer_add (aisexec_poll_handle,
  833. healthcheck->saAmfHealthcheckMaxDuration,
  834. (void *)healthcheck,
  835. timer_function_healthcheck_tmo,
  836. &healthcheck->timer_handle_duration);
  837. }
  838. /**
  839. * Multicast a healthcheck timeout event.
  840. * @param healthcheck
  841. */
  842. static void mcast_healthcheck_tmo_event (
  843. struct amf_healthcheck *healthcheck)
  844. {
  845. struct req_exec_amf_healthcheck_tmo req_exec;
  846. struct iovec iovec;
  847. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  848. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  849. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  850. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  851. memcpy (&req_exec.safHealthcheckKey,
  852. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  853. iovec.iov_base = (char *)&req_exec;
  854. iovec.iov_len = sizeof (req_exec);
  855. assert (totempg_groups_mcast_joined (openais_group_handle,
  856. &iovec, 1, TOTEMPG_AGREED) == 0);
  857. }
  858. /**
  859. * This function is called by the timer subsystem when a component has not
  860. * performed a healthcheck on time.
  861. * The event is multicasted to the cluster.
  862. * @param data
  863. */
  864. static void timer_function_healthcheck_tmo (
  865. void *_healthcheck)
  866. {
  867. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  868. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  869. getSaNameT (&healthcheck->comp->name));
  870. mcast_healthcheck_tmo_event (healthcheck);
  871. }
  872. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  873. {
  874. struct res_lib_amf_healthcheckcallback res_lib;
  875. res_lib.header.id = MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  876. res_lib.header.size = sizeof (struct res_lib_amf_healthcheckcallback);
  877. res_lib.header.error = SA_AIS_OK;
  878. res_lib.invocation =
  879. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  880. amf_comp_dn_make (healthcheck->comp, &res_lib.compName);
  881. memcpy (&res_lib.key, &healthcheck->safHealthcheckKey,
  882. sizeof (SaAmfHealthcheckKeyT));
  883. TRACE8 ("sending healthcheck request to component %s",
  884. res_lib.compName.value);
  885. openais_conn_send_response (
  886. openais_conn_partner_get (healthcheck->comp->conn),
  887. &res_lib, sizeof (struct res_lib_amf_healthcheckcallback));
  888. }
  889. static void lib_csi_set_request (
  890. struct amf_comp *comp,
  891. struct amf_csi_assignment *csi_assignment)
  892. {
  893. struct res_lib_amf_csisetcallback* res_lib;
  894. void* p;
  895. struct amf_csi_attribute *attribute;
  896. size_t char_length_of_csi_attrs=0;
  897. size_t num_of_csi_attrs=0;
  898. int i;
  899. struct amf_csi *csi;
  900. char* csi_attribute_buf;
  901. unsigned int byte_offset;
  902. if (!amf_su_is_local (comp->su))
  903. return;
  904. csi = csi_assignment->csi;
  905. ENTER ("Assigning CSI '%s' state %s to comp '%s'\n",
  906. getSaNameT (&csi->name), amf_ha_state (csi_assignment->requested_ha_state),
  907. comp->name.value);
  908. for (attribute = csi->attributes_head;
  909. attribute != NULL;
  910. attribute = attribute->next) {
  911. for (i = 0; attribute->value[i] != NULL; i++) {
  912. num_of_csi_attrs++;
  913. char_length_of_csi_attrs += strlen(attribute->name);
  914. char_length_of_csi_attrs += strlen(attribute->value[i]);
  915. char_length_of_csi_attrs += 2;
  916. }
  917. }
  918. p = malloc(sizeof(struct res_lib_amf_csisetcallback) +
  919. char_length_of_csi_attrs);
  920. if (p == NULL) {
  921. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  922. }
  923. res_lib = (struct res_lib_amf_csisetcallback*)p;
  924. /* Address of the buffer containing the Csi name value pair */
  925. csi_attribute_buf = res_lib->csi_attr_buf;
  926. /* Byteoffset start at the zero byte */
  927. byte_offset = 0;
  928. for (attribute = csi->attributes_head;
  929. attribute != NULL;
  930. attribute = attribute->next) {
  931. for (i = 0; attribute->value[i] != NULL; i++) {
  932. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  933. byte_offset += strlen(attribute->name) + 1;
  934. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  935. byte_offset += strlen(attribute->value[i]) + 1;
  936. }
  937. }
  938. res_lib->number = num_of_csi_attrs;
  939. res_lib->csiFlags = SA_AMF_CSI_ADD_ONE;
  940. switch (csi_assignment->requested_ha_state) {
  941. case SA_AMF_HA_ACTIVE: {
  942. res_lib->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  943. res_lib->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  944. SA_AMF_CSI_NEW_ASSIGN;
  945. break;
  946. }
  947. case SA_AMF_HA_STANDBY: {
  948. res_lib->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  949. res_lib->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  950. break;
  951. }
  952. case SA_AMF_HA_QUIESCED: {
  953. /*TODO*/
  954. break;
  955. }
  956. case SA_AMF_HA_QUIESCING: {
  957. /*TODO*/
  958. break;
  959. }
  960. default: {
  961. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  962. break;
  963. }
  964. }
  965. res_lib->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  966. res_lib->header.size =
  967. sizeof (struct res_lib_amf_csisetcallback) +
  968. char_length_of_csi_attrs;
  969. res_lib->header.error = SA_AIS_OK;
  970. amf_comp_dn_make (comp, &res_lib->compName);
  971. amf_csi_dn_make (csi, &res_lib->csiName);
  972. res_lib->haState = csi_assignment->requested_ha_state;
  973. res_lib->invocation =
  974. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  975. openais_conn_send_response (
  976. openais_conn_partner_get (comp->conn), res_lib, res_lib->header.size);
  977. free(p);
  978. }
  979. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  980. {
  981. TRACE2("Exec comp register '%s'", &comp->name.value);
  982. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  983. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  984. } else if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  985. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  986. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  987. }
  988. else {
  989. assert (0);
  990. }
  991. return SA_AIS_OK;
  992. }
  993. void amf_comp_error_report (
  994. struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  995. {
  996. struct res_lib_amf_componenterrorreport res_lib;
  997. TRACE2("Exec comp error report '%s'", &comp->name.value);
  998. if (amf_su_is_local (comp->su)) {
  999. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  1000. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  1001. res_lib.header.error = SA_AIS_OK;
  1002. openais_conn_send_response (comp->conn, &res_lib, sizeof (res_lib));
  1003. }
  1004. /* report to SU and let it handle the problem */
  1005. report_error_suspected (comp, recommendedRecovery);
  1006. }
  1007. /**
  1008. * Healthcheck timeout event handler
  1009. * @param comp
  1010. * @param healthcheck
  1011. */
  1012. void amf_comp_healthcheck_tmo (
  1013. struct amf_comp *comp, struct amf_healthcheck *healthcheck)
  1014. {
  1015. TRACE2("Exec healthcheck tmo for '%s'", &comp->name.value);
  1016. /* report to SU and let it handle the problem */
  1017. report_error_suspected (comp, healthcheck->recommendedRecovery);
  1018. }
  1019. static void clear_ha_state (
  1020. struct amf_comp *comp, struct amf_csi_assignment *csi_assignment)
  1021. {
  1022. ENTER ("");
  1023. csi_assignment->saAmfCSICompHAState = 0;
  1024. }
  1025. /**
  1026. * Event method to be called when a cleanup completed event is received
  1027. * @param comp
  1028. */
  1029. void amf_comp_cleanup_completed (struct amf_comp *comp)
  1030. {
  1031. TRACE2("Exec CLC cleanup completed for '%s'", &comp->name.value);
  1032. /* Set all CSI's confirmed HA state to unknown */
  1033. amf_comp_foreach_csi_assignment (comp, clear_ha_state);
  1034. /* clear error suspected flag, component is terminated now */
  1035. comp->error_suspected = 0;
  1036. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  1037. amf_comp_instantiate (comp);
  1038. } else {
  1039. comp_presence_state_set (comp, SA_AMF_PRESENCE_UNINSTANTIATED);
  1040. }
  1041. }
  1042. /**
  1043. * Handle the request from a component to start a healthcheck
  1044. *
  1045. * @param comp
  1046. * @param healthcheckKey
  1047. * @param invocationType
  1048. * @param recommendedRecovery
  1049. *
  1050. * @return SaAisErrorT - return value to component
  1051. */
  1052. SaAisErrorT amf_comp_healthcheck_start (
  1053. struct amf_comp *comp,
  1054. SaAmfHealthcheckKeyT *healthcheckKey,
  1055. SaAmfHealthcheckInvocationT invocationType,
  1056. SaAmfRecommendedRecoveryT recommendedRecovery)
  1057. {
  1058. struct amf_healthcheck *healthcheck;
  1059. SaAisErrorT error = SA_AIS_OK;
  1060. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1061. if (healthcheck == 0) {
  1062. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  1063. healthcheckKey->key);
  1064. error = SA_AIS_ERR_NOT_EXIST;
  1065. goto error_exit;
  1066. }
  1067. dprintf ("Healthcheckstart: '%s', key '%s'",
  1068. comp->name.value, healthcheckKey->key);
  1069. /*
  1070. * Determine if this healthcheck is already active
  1071. */
  1072. if (healthcheck->active) {
  1073. error = SA_AIS_ERR_EXIST;
  1074. goto error_exit;
  1075. }
  1076. /*
  1077. * Initialise
  1078. */
  1079. healthcheck->invocationType = invocationType;
  1080. healthcheck->recommendedRecovery = recommendedRecovery;
  1081. healthcheck->timer_handle_duration = 0;
  1082. healthcheck->timer_handle_period = 0;
  1083. healthcheck->active = 1;
  1084. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1085. /* start timer to execute first healthcheck request */
  1086. poll_timer_add (aisexec_poll_handle,
  1087. healthcheck->saAmfHealthcheckPeriod,
  1088. (void *)healthcheck,
  1089. timer_function_healthcheck_next_fn,
  1090. &healthcheck->timer_handle_period);
  1091. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  1092. /* start supervision timer */
  1093. poll_timer_add (aisexec_poll_handle,
  1094. healthcheck->saAmfHealthcheckPeriod,
  1095. (void *)healthcheck,
  1096. timer_function_healthcheck_tmo,
  1097. &healthcheck->timer_handle_period);
  1098. } else {
  1099. error = SA_AIS_ERR_INVALID_PARAM;
  1100. }
  1101. error_exit:
  1102. return error;
  1103. }
  1104. /**
  1105. * Stop all or a specifed healthcheck
  1106. * @param comp
  1107. * @param healthcheckKey - NULL if all
  1108. *
  1109. * @return SaAisErrorT
  1110. */
  1111. SaAisErrorT amf_comp_healthcheck_stop (
  1112. struct amf_comp *comp,
  1113. SaAmfHealthcheckKeyT *healthcheckKey)
  1114. {
  1115. struct amf_healthcheck *healthcheck;
  1116. SaAisErrorT error = SA_AIS_OK;
  1117. dprintf ("Healthcheckstop: '%s', key '%s'",
  1118. comp->name.value, healthcheckKey->key);
  1119. if (healthcheckKey == NULL) {
  1120. for (healthcheck = comp->healthcheck_head;
  1121. healthcheck != NULL;
  1122. healthcheck = healthcheck->next) {
  1123. healthcheck_deactivate (healthcheck);
  1124. }
  1125. } else {
  1126. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1127. if (healthcheck == NULL) {
  1128. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1129. healthcheckKey->key);
  1130. error = SA_AIS_ERR_NOT_EXIST;
  1131. } else {
  1132. healthcheck_deactivate (healthcheck);
  1133. }
  1134. }
  1135. return error;
  1136. }
  1137. /**
  1138. * Instantiate a component
  1139. * @param comp
  1140. */
  1141. void amf_comp_instantiate (struct amf_comp *comp)
  1142. {
  1143. int res = 0;
  1144. ENTER ("'%s' SU '%s'", getSaNameT (&comp->name),
  1145. getSaNameT (&comp->su->name));
  1146. if (comp->saAmfCompPresenceState != SA_AMF_PRESENCE_RESTARTING) {
  1147. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATING);
  1148. }
  1149. if (amf_su_is_local (comp->su)) {
  1150. res = clc_interfaces[comp->comptype]->instantiate (comp);
  1151. }
  1152. }
  1153. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1154. SaAmfReadinessStateT state)
  1155. {
  1156. // comp->saAmfCompReadinessState = state;
  1157. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1158. comp->name.value, amf_readiness_state (state));
  1159. }
  1160. /**
  1161. * Handle a component response (received from the lib) of an earlier AMF request.
  1162. * This function should be invoked when the lib request is received.
  1163. * @param invocation [in] associates the response with the request (callback)
  1164. * @param error [in] response from the component of the associated callback
  1165. * @param retval [out] contains return value to component when needed
  1166. *
  1167. * @return ==0 respond to component, do not multicast
  1168. * @return >0 do not respond to component, multicast response
  1169. */
  1170. int amf_comp_response_1 (
  1171. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval)
  1172. {
  1173. int res;
  1174. int interface;
  1175. void *data;
  1176. res = invocation_get (invocation, &interface, &data);
  1177. if (res == -1) {
  1178. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1179. *retval = SA_AIS_ERR_INVALID_PARAM;
  1180. return 0;
  1181. }
  1182. switch (interface) {
  1183. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1184. struct amf_healthcheck *healthcheck = data;
  1185. SaNameT name;
  1186. TRACE8 ("Healthcheck response from '%s': %d",
  1187. amf_comp_dn_make (healthcheck->comp, &name), error);
  1188. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1189. /* the response was on time, delete supervision timer */
  1190. poll_timer_delete (aisexec_poll_handle,
  1191. healthcheck->timer_handle_duration);
  1192. healthcheck->timer_handle_duration = 0;
  1193. /* start timer to execute next healthcheck request */
  1194. poll_timer_add (aisexec_poll_handle,
  1195. healthcheck->saAmfHealthcheckPeriod,
  1196. (void *)healthcheck,
  1197. timer_function_healthcheck_next_fn,
  1198. &healthcheck->timer_handle_period);
  1199. *retval = SA_AIS_OK;
  1200. } else {
  1201. *retval = SA_AIS_ERR_INVALID_PARAM;
  1202. }
  1203. return 0; /* do not multicast event */
  1204. break;
  1205. }
  1206. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1207. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1208. return 1; /* multicast event */
  1209. break;
  1210. #if 0
  1211. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1212. struct component_terminate_callback_data *component_terminate_callback_data;
  1213. component_terminate_callback_data = data;
  1214. dprintf ("Lib component terminate callback response, error: %d", error);
  1215. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1216. escalation_policy_restart (component_terminate_callback_data->comp);
  1217. return 1;
  1218. break;
  1219. }
  1220. #endif
  1221. default:
  1222. assert (0);
  1223. break;
  1224. }
  1225. }
  1226. /**
  1227. * Handle a component response (received from EVS) of an earlier AMF request.
  1228. * This function should be invoked when the multicast request is received.
  1229. * @param invocation [in] associates the response with the request (callback)
  1230. * @param error [in] response from the component of the associated callback
  1231. * @param retval [out] contains return value to component when needed
  1232. *
  1233. * @return component to which the response should be sent
  1234. */
  1235. struct amf_comp *amf_comp_response_2 (
  1236. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval)
  1237. {
  1238. int res;
  1239. int interface;
  1240. void *data;
  1241. struct amf_comp *comp = NULL;
  1242. assert (retval != NULL);
  1243. *retval = SA_AIS_OK;
  1244. res = invocation_get_and_destroy (invocation, &interface, &data);
  1245. if (res == -1) {
  1246. log_printf (LOG_ERR, "Comp response: invocation not found\n");
  1247. *retval = SA_AIS_ERR_INVALID_PARAM;
  1248. return NULL;
  1249. }
  1250. switch (interface) {
  1251. case AMF_RESPONSE_CSISETCALLBACK: {
  1252. struct amf_csi_assignment *csi_assignment = data;
  1253. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1254. csi_assignment->csi->name.value,
  1255. csi_assignment->comp->name.value, error);
  1256. comp = csi_assignment->comp;
  1257. if (error == SA_AIS_OK) {
  1258. comp_ha_state_set (
  1259. comp, csi_assignment, csi_assignment->requested_ha_state);
  1260. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1261. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1262. csi_assignment);
  1263. } else {
  1264. *retval = SA_AIS_ERR_INVALID_PARAM;
  1265. }
  1266. break;
  1267. }
  1268. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1269. struct amf_csi_assignment *csi_assignment = data;
  1270. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1271. csi_assignment->csi->name.value,
  1272. csi_assignment->comp->name.value, error);
  1273. comp = csi_assignment->comp;
  1274. if (error == SA_AIS_OK) {
  1275. comp_ha_state_set (comp, csi_assignment,
  1276. csi_assignment->requested_ha_state);
  1277. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1278. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1279. csi_assignment);
  1280. } else {
  1281. *retval = SA_AIS_ERR_INVALID_PARAM;
  1282. }
  1283. break;
  1284. }
  1285. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1286. struct component_terminate_callback_data *callback_data = data;
  1287. dprintf ("Lib comp '%s' terminate callback response, error: %d",
  1288. callback_data->comp->name.value, error);
  1289. comp_presence_state_set (callback_data->comp,
  1290. SA_AMF_PRESENCE_UNINSTANTIATED);
  1291. break;
  1292. }
  1293. default:
  1294. assert (0);
  1295. break;
  1296. }
  1297. return comp;
  1298. }
  1299. /**
  1300. * Request a component to assume a particular HA state
  1301. * @param comp
  1302. * @param csi_assignment
  1303. * @param requested_ha_state
  1304. */
  1305. void amf_comp_hastate_set (
  1306. struct amf_comp *component,
  1307. struct amf_csi_assignment *csi_assignment)
  1308. {
  1309. assert (component != NULL && csi_assignment != NULL);
  1310. ENTER ("'%s'", csi_assignment->csi->name.value);
  1311. if (!component->error_suspected) {
  1312. lib_csi_set_request(component, csi_assignment);
  1313. } else {
  1314. if (csi_assignment->requested_ha_state == SA_AMF_HA_QUIESCED) {
  1315. csi_assignment->saAmfCSICompHAState = csi_assignment->requested_ha_state;
  1316. } else {
  1317. assert (0);
  1318. }
  1319. }
  1320. }
  1321. /**
  1322. * Request termination of a component
  1323. * @param comp
  1324. */
  1325. void amf_comp_terminate (struct amf_comp *comp)
  1326. {
  1327. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1328. amf_comp_healthcheck_stop (comp, NULL);
  1329. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1330. if (amf_su_is_local (comp->su)) {
  1331. if (comp->error_suspected) {
  1332. clc_interfaces[comp->comptype]->cleanup (comp);
  1333. } else {
  1334. clc_interfaces[comp->comptype]->terminate (comp);
  1335. }
  1336. }
  1337. }
  1338. /**
  1339. * Request restart of a component
  1340. * @param comp
  1341. */
  1342. void amf_comp_restart (struct amf_comp *comp)
  1343. {
  1344. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1345. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1346. comp->saAmfCompRestartCount += 1;
  1347. amf_comp_healthcheck_stop (comp, NULL);
  1348. if (amf_su_is_local (comp->su)) {
  1349. clc_interfaces[comp->comptype]->cleanup (comp);
  1350. }
  1351. }
  1352. /**
  1353. * Request to return the HA state for a components CSI
  1354. * @param comp
  1355. * @param csi_name
  1356. * @param ha_state
  1357. *
  1358. * @return SaAisErrorT
  1359. */
  1360. SaAisErrorT amf_comp_hastate_get (
  1361. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1362. {
  1363. struct amf_csi_assignment *assignment;
  1364. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1365. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1366. getSaNameT (&comp->name), csi_name->value);
  1367. assignment = csi_assignment_find_in (comp, csi_name);
  1368. if (assignment != NULL) {
  1369. *ha_state = assignment->saAmfCSICompHAState;
  1370. return SA_AIS_OK;
  1371. }
  1372. return SA_AIS_ERR_INVALID_PARAM;
  1373. }
  1374. /**
  1375. * Response from a component informs AMF that it has performed a healthcheck
  1376. * @param comp
  1377. * @param healthcheckKey
  1378. * @param healthcheckResult
  1379. *
  1380. * @return SaAisErrorT
  1381. */
  1382. SaAisErrorT amf_comp_healthcheck_confirm (
  1383. struct amf_comp *comp,
  1384. SaAmfHealthcheckKeyT *healthcheckKey,
  1385. SaAisErrorT healthcheckResult)
  1386. {
  1387. struct amf_healthcheck *healthcheck;
  1388. SaAisErrorT error = SA_AIS_OK;
  1389. dprintf ("Healthcheckconfirm: '%s', key '%s'",
  1390. comp->name.value, healthcheckKey->key);
  1391. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1392. if (healthcheck == NULL) {
  1393. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1394. healthcheckKey->key);
  1395. error = SA_AIS_ERR_NOT_EXIST;
  1396. } else if (healthcheck->active) {
  1397. if (healthcheckResult == SA_AIS_OK) {
  1398. /* the response was on time, restart the supervision timer */
  1399. poll_timer_delete (aisexec_poll_handle,
  1400. healthcheck->timer_handle_period);
  1401. poll_timer_add (aisexec_poll_handle,
  1402. healthcheck->saAmfHealthcheckPeriod,
  1403. (void *)healthcheck,
  1404. timer_function_healthcheck_tmo,
  1405. &healthcheck->timer_handle_period);
  1406. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1407. /* send to cluster */
  1408. mcast_healthcheck_tmo_event (healthcheck);
  1409. } else {
  1410. error = SA_AIS_ERR_INVALID_PARAM;
  1411. }
  1412. } else {
  1413. error = SA_AIS_ERR_INVALID_PARAM;
  1414. }
  1415. return error;
  1416. }
  1417. void amf_comp_init (void)
  1418. {
  1419. log_init ("AMF");
  1420. }
  1421. void amf_comp_operational_state_set (struct amf_comp *comp,
  1422. SaAmfOperationalStateT oper_state)
  1423. {
  1424. comp->saAmfCompOperState = oper_state;
  1425. TRACE1 ("Setting comp '%s', SU '%s' operational state: %s\n",
  1426. comp->name.value, comp->su->name.value,
  1427. amf_op_state (comp->saAmfCompOperState));
  1428. amf_su_comp_state_changed (
  1429. comp->su, comp, SA_AMF_OP_STATE, oper_state);
  1430. }
  1431. int amf_comp_get_saAmfCompNumCurrActiveCsi(struct amf_comp *component)
  1432. {
  1433. int cnt = 0;
  1434. struct amf_csi_assignment *csi_assignment;
  1435. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1436. while (csi_assignment != NULL) {
  1437. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_ACTIVE) {
  1438. cnt++;
  1439. }
  1440. csi_assignment = amf_comp_get_next_csi_assignment (
  1441. component, csi_assignment);
  1442. }
  1443. return cnt;
  1444. }
  1445. int amf_comp_get_saAmfCompNumCurrStandbyCsi(struct amf_comp *component)
  1446. {
  1447. int cnt = 0;
  1448. struct amf_csi_assignment *csi_assignment;
  1449. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1450. while (csi_assignment != NULL) {
  1451. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_STANDBY) {
  1452. cnt++;
  1453. }
  1454. csi_assignment = amf_comp_get_next_csi_assignment (
  1455. component, csi_assignment);
  1456. }
  1457. return cnt;
  1458. }
  1459. SaAmfReadinessStateT amf_comp_get_saAmfCompReadinessState (
  1460. struct amf_comp *component)
  1461. {
  1462. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1463. return amf_su_get_saAmfSUReadinessState (component->su);
  1464. } else if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_DISABLED) {
  1465. return SA_AMF_READINESS_OUT_OF_SERVICE;
  1466. }
  1467. assert (0);
  1468. }