amfcomp.c 45 KB

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