amfcomp.c 49 KB

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