amfcomp.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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 "service.h"
  138. #include "util.h"
  139. #include "amf.h"
  140. #include "print.h"
  141. enum clc_command_run_operation_type {
  142. CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE = 1,
  143. CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE = 2,
  144. CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP = 3
  145. };
  146. struct clc_command_run_data {
  147. struct amf_comp *comp;
  148. enum clc_command_run_operation_type type;
  149. void (*completion_callback) (void *context);
  150. };
  151. struct clc_interface {
  152. int (*instantiate) (struct amf_comp *comp);
  153. int (*terminate) (struct amf_comp *comp);
  154. int (*cleanup) (struct amf_comp *comp);
  155. };
  156. struct csi_remove_callback_data {
  157. struct amf_csi *csi;
  158. };
  159. struct component_terminate_callback_data {
  160. struct amf_comp *comp;
  161. };
  162. static void comp_presence_state_set (
  163. struct amf_comp *comp,
  164. SaAmfPresenceStateT presence_state);
  165. static void comp_operational_state_set (
  166. struct amf_comp *comp,
  167. SaAmfOperationalStateT operational_state);
  168. static int clc_cli_instantiate (struct amf_comp *comp);
  169. static int clc_instantiate_callback (struct amf_comp *comp);
  170. static int clc_csi_set_callback (struct amf_comp *comp);
  171. static int clc_cli_terminate (struct amf_comp *comp);
  172. static int lib_comp_terminate_request (struct amf_comp *comp);
  173. static int clc_csi_remove_callback (struct amf_comp *comp);
  174. static int clc_cli_cleanup (struct amf_comp *comp);
  175. static int clc_cli_cleanup_local (struct amf_comp *comp);
  176. static void healthcheck_deactivate (struct amf_healthcheck *healthcheck_active);
  177. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck);
  178. static void timer_function_healthcheck_tmo (void *_healthcheck);
  179. /*
  180. * Life cycle functions
  181. */
  182. static struct clc_interface clc_interface_sa_aware = {
  183. clc_cli_instantiate,
  184. lib_comp_terminate_request,
  185. clc_cli_cleanup
  186. };
  187. static struct clc_interface clc_interface_proxied_pre = {
  188. clc_instantiate_callback,
  189. lib_comp_terminate_request,
  190. clc_cli_cleanup
  191. };
  192. static struct clc_interface clc_interface_proxied_non_pre = {
  193. clc_csi_set_callback,
  194. clc_csi_remove_callback,
  195. clc_cli_cleanup_local
  196. };
  197. static struct clc_interface clc_interface_non_proxied_non_saware = {
  198. clc_cli_instantiate,
  199. clc_cli_terminate,
  200. clc_cli_cleanup_local
  201. };
  202. static struct clc_interface *clc_interfaces[4] = {
  203. &clc_interface_sa_aware,
  204. &clc_interface_proxied_pre,
  205. &clc_interface_proxied_non_pre,
  206. &clc_interface_non_proxied_non_saware
  207. };
  208. struct invocation {
  209. void *data;
  210. int interface;
  211. int active;
  212. };
  213. static struct invocation *invocation_entries = 0;
  214. static int invocation_entries_size = 0;
  215. static int invocation_create (
  216. int interface,
  217. void *data)
  218. {
  219. struct invocation *invocation_addr = 0;
  220. struct invocation *invocation_temp;
  221. int i;
  222. int loc = 0;
  223. for (i = 0; i < invocation_entries_size; i++) {
  224. if (invocation_entries[i].active == 0) {
  225. invocation_addr = &invocation_entries[i];
  226. loc = i;
  227. break;
  228. }
  229. }
  230. if (invocation_addr == 0) {
  231. invocation_temp = (struct invocation *)realloc (invocation_entries,
  232. (invocation_entries_size + 1) * sizeof (struct invocation));
  233. if (invocation_temp == 0) {
  234. return (-1);
  235. }
  236. invocation_entries = invocation_temp;
  237. invocation_addr = &invocation_entries[invocation_entries_size];
  238. loc = invocation_entries_size;
  239. invocation_entries_size += 1;
  240. }
  241. invocation_addr->interface = interface;
  242. invocation_addr->data = data;
  243. invocation_addr->active = 1;
  244. return (loc);
  245. }
  246. static int invocation_get_and_destroy (SaUint64T invocation, int *interface,
  247. void **data)
  248. {
  249. if (invocation > invocation_entries_size) {
  250. return (-1);
  251. }
  252. if (invocation_entries[invocation].active == 0) {
  253. return (-1);
  254. }
  255. *interface = invocation_entries[invocation].interface;
  256. *data = invocation_entries[invocation].data;
  257. memset (&invocation_entries[invocation], 0, sizeof (struct invocation));
  258. return (0);
  259. }
  260. static int invocation_get (SaUint64T invocation, int *interface,
  261. void **data)
  262. {
  263. if (invocation > invocation_entries_size) {
  264. return (-1);
  265. }
  266. if (invocation_entries[invocation].active == 0) {
  267. return (-1);
  268. }
  269. *interface = invocation_entries[invocation].interface;
  270. *data = invocation_entries[invocation].data;
  271. return (0);
  272. }
  273. static void invocation_destroy_by_data (void *data)
  274. {
  275. int i;
  276. for (i = 0; i < invocation_entries_size; i++) {
  277. if (invocation_entries[i].data == data) {
  278. memset (&invocation_entries[i], 0,
  279. sizeof (struct invocation));
  280. break;
  281. }
  282. }
  283. }
  284. char *amf_comp_dn_make (struct amf_comp *comp, SaNameT *name)
  285. {
  286. int i = snprintf((char*) name->value, SA_MAX_NAME_LENGTH,
  287. "safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  288. comp->name.value, comp->su->name.value,
  289. comp->su->sg->name.value, comp->su->sg->application->name.value);
  290. assert (i <= SA_MAX_NAME_LENGTH);
  291. name->length = i;
  292. return (char *)name->value;
  293. }
  294. static void *clc_command_run (void *context)
  295. {
  296. struct clc_command_run_data *clc_command_run_data =
  297. (struct clc_command_run_data *)context;
  298. pid_t pid;
  299. int res;
  300. char *argv[10];
  301. char *envp[10];
  302. int status;
  303. char path[PATH_MAX];
  304. char *cmd = 0;
  305. char *comp_argv = 0;
  306. char comp_name[SA_MAX_NAME_LENGTH];
  307. int i;
  308. ENTER_VOID();
  309. pid = fork();
  310. if (pid == -1) {
  311. dprintf ("Couldn't fork process %s\n", strerror (errno));
  312. return (0);
  313. }
  314. if (pid) {
  315. dprintf ("waiting for pid %d to finish\n", pid);
  316. waitpid (pid, &status, 0);
  317. dprintf ("process (%d) finished with %d\n", pid, status);
  318. if (clc_command_run_data->completion_callback) {
  319. clc_command_run_data->completion_callback (context);
  320. }
  321. pthread_exit(0);
  322. }
  323. switch (clc_command_run_data->type) {
  324. case CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE:
  325. cmd = clc_command_run_data->comp->saAmfCompInstantiateCmd;
  326. comp_argv = clc_command_run_data->comp->saAmfCompInstantiateCmdArgv;
  327. break;
  328. case CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE:
  329. cmd = clc_command_run_data->comp->saAmfCompTerminateCmd;
  330. comp_argv = clc_command_run_data->comp->saAmfCompTerminateCmdArgv;
  331. break;
  332. case CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP:
  333. cmd = clc_command_run_data->comp->saAmfCompCleanupCmd;
  334. comp_argv = clc_command_run_data->comp->saAmfCompCleanupCmdArgv;
  335. break;
  336. default:
  337. assert (0 != 1);
  338. break;
  339. }
  340. /* If command is not an absolute path, search for paths in parent objects */
  341. if (cmd[0] != '/') {
  342. if (strlen (clc_command_run_data->comp->clccli_path)) {
  343. sprintf (path, "%s/%s",
  344. clc_command_run_data->comp->clccli_path, cmd);
  345. } else if (strlen (clc_command_run_data->comp->su->clccli_path)) {
  346. sprintf (path, "%s/%s",
  347. clc_command_run_data->comp->su->clccli_path, cmd);
  348. } else if (strlen (clc_command_run_data->comp->su->sg->clccli_path)) {
  349. sprintf (path, "%s/%s",
  350. clc_command_run_data->comp->su->sg->clccli_path, cmd);
  351. } else if (strlen (clc_command_run_data->comp->su->sg->application->clccli_path)) {
  352. sprintf (path, "%s/%s",
  353. clc_command_run_data->comp->su->sg->application->clccli_path, cmd);
  354. }
  355. cmd = path;
  356. }
  357. argv[0] = cmd;
  358. {
  359. /* make a proper argv array */
  360. i = 1;
  361. char *ptrptr;
  362. char *arg = strtok_r(comp_argv, " ", &ptrptr);
  363. while (arg) {
  364. argv[i] = arg;
  365. arg = strtok_r(NULL, " ", & ptrptr);
  366. i++;
  367. }
  368. }
  369. argv[i] = NULL;
  370. assert (i < 10);
  371. envp[0] = comp_name;
  372. i = snprintf(comp_name, SA_MAX_NAME_LENGTH,
  373. "SA_AMF_COMPONENT_NAME=safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  374. clc_command_run_data->comp->name.value,
  375. clc_command_run_data->comp->su->name.value,
  376. clc_command_run_data->comp->su->sg->name.value,
  377. clc_command_run_data->comp->su->sg->application->name.value);
  378. assert (i <= SA_MAX_NAME_LENGTH);
  379. for (i = 1; clc_command_run_data->comp->saAmfCompCmdEnv &&
  380. clc_command_run_data->comp->saAmfCompCmdEnv[i - 1]; i++) {
  381. envp[i] = clc_command_run_data->comp->saAmfCompCmdEnv[i - 1];
  382. }
  383. envp[i] = NULL;
  384. assert (i < 10);
  385. dprintf ("running command '%s' with environment:\n", cmd);
  386. for (i = 0; envp[i] != NULL; i++) {
  387. dprintf (" %s\n", envp[i]);
  388. }
  389. dprintf (" and argv:\n", cmd);
  390. for (i = 0; argv[i] != NULL; i++) {
  391. dprintf (" %s\n", argv[i]);
  392. }
  393. res = execve (cmd, argv, envp);
  394. if (res == -1) {
  395. log_printf (LOG_LEVEL_ERROR, "Couldn't exec program %s (%s)\n",
  396. cmd, strerror (errno));
  397. }
  398. assert (res != -1);
  399. return (0);
  400. }
  401. /*
  402. * Instantiate possible operations
  403. */
  404. static int clc_cli_instantiate (struct amf_comp *comp)
  405. {
  406. int res;
  407. pthread_t thread;
  408. pthread_attr_t thread_attr; /* thread attribute */
  409. struct clc_command_run_data *clc_command_run_data;
  410. ENTER("comp '%s'\n", getSaNameT (&comp->name));
  411. clc_command_run_data = malloc (sizeof (struct clc_command_run_data));
  412. if (clc_command_run_data == NULL) {
  413. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  414. }
  415. clc_command_run_data->comp = comp;
  416. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE;
  417. clc_command_run_data->completion_callback = NULL;
  418. pthread_attr_init (&thread_attr);
  419. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  420. res = pthread_create (&thread, &thread_attr, clc_command_run, (void *)clc_command_run_data);
  421. if (res != 0) {
  422. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  423. }
  424. // TODO error code from pthread_create
  425. return (res);
  426. }
  427. static int clc_instantiate_callback (struct amf_comp *comp)
  428. {
  429. ENTER("comp %s\n", getSaNameT (&comp->name));
  430. return (0);
  431. }
  432. static int clc_csi_set_callback (struct amf_comp *comp)
  433. {
  434. ENTER("comp %s\n", getSaNameT (&comp->name));
  435. return (0);
  436. }
  437. /*
  438. * Terminate possible operations
  439. */
  440. static int clc_cli_terminate (struct amf_comp *comp)
  441. {
  442. ENTER("comp %s\n", getSaNameT (&comp->name));
  443. return (0);
  444. }
  445. static int lib_comp_terminate_request (struct amf_comp *comp)
  446. {
  447. struct res_lib_amf_componentterminatecallback res_lib_amf_componentterminatecallback;
  448. struct component_terminate_callback_data *component_terminate_callback_data;
  449. ENTER("comp %s\n", getSaNameT (&comp->name));
  450. if (comp->saAmfCompPresenceState != SA_AMF_PRESENCE_INSTANTIATED) {
  451. dprintf ("component terminated but not instantiated %s - %d\n",
  452. getSaNameT (&comp->name), comp->saAmfCompPresenceState);
  453. assert (0);
  454. return (0);
  455. }
  456. dprintf ("component name terminating %s\n", getSaNameT (&comp->name));
  457. dprintf ("component presence state %d\n", comp->saAmfCompPresenceState);
  458. res_lib_amf_componentterminatecallback.header.id = MESSAGE_RES_AMF_COMPONENTTERMINATECALLBACK;
  459. res_lib_amf_componentterminatecallback.header.size = sizeof (struct res_lib_amf_componentterminatecallback);
  460. res_lib_amf_componentterminatecallback.header.error = SA_AIS_OK;
  461. memcpy (&res_lib_amf_componentterminatecallback.compName,
  462. &comp->name, sizeof (SaNameT));
  463. component_terminate_callback_data =
  464. malloc (sizeof (struct component_terminate_callback_data));
  465. if (component_terminate_callback_data == NULL) {
  466. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  467. }
  468. component_terminate_callback_data->comp = comp;
  469. res_lib_amf_componentterminatecallback.invocation =
  470. invocation_create (
  471. AMF_RESPONSE_COMPONENTTERMINATECALLBACK,
  472. component_terminate_callback_data);
  473. dprintf ("Creating invocation %llu",
  474. (unsigned long long)res_lib_amf_componentterminatecallback.invocation);
  475. openais_conn_send_response (
  476. openais_conn_partner_get (comp->conn),
  477. &res_lib_amf_componentterminatecallback,
  478. sizeof (struct res_lib_amf_componentterminatecallback));
  479. return (0);
  480. }
  481. static int clc_csi_remove_callback (struct amf_comp *comp)
  482. {
  483. dprintf ("clc_tcsi_remove_callback\n");
  484. return (0);
  485. }
  486. /*
  487. * Clean up completed
  488. */
  489. static void mcast_cleanup_completion_event (void *context)
  490. {
  491. struct clc_command_run_data *clc_command_run_data =
  492. (struct clc_command_run_data *)context;
  493. struct req_exec_amf_clc_cleanup_completed req;
  494. struct iovec iovec;
  495. TRACE2("CLC cleanup done for '%s'",
  496. clc_command_run_data->comp->name.value);
  497. req.header.size = sizeof (struct req_exec_amf_clc_cleanup_completed);
  498. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  499. MESSAGE_REQ_EXEC_AMF_CLC_CLEANUP_COMPLETED);
  500. amf_comp_dn_make (clc_command_run_data->comp, &req.compName);
  501. iovec.iov_base = (char *)&req;
  502. iovec.iov_len = sizeof (req);
  503. assert (totempg_groups_mcast_joined (openais_group_handle,
  504. &iovec, 1, TOTEMPG_AGREED) == 0);
  505. }
  506. /*
  507. * Cleanup possible operations
  508. */
  509. static int clc_cli_cleanup (struct amf_comp *comp)
  510. {
  511. int res;
  512. pthread_t thread;
  513. pthread_attr_t thread_attr; /* thread attribute */
  514. struct clc_command_run_data *clc_command_run_data;
  515. dprintf ("clc_cli_cleanup\n");
  516. clc_command_run_data = malloc (sizeof (struct clc_command_run_data));
  517. if (clc_command_run_data == NULL) {
  518. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  519. }
  520. clc_command_run_data->comp = comp;
  521. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP;
  522. clc_command_run_data->completion_callback = mcast_cleanup_completion_event;
  523. pthread_attr_init (&thread_attr);
  524. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  525. res = pthread_create (&thread, &thread_attr, clc_command_run, (void *)clc_command_run_data);
  526. if (res != 0) {
  527. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  528. }
  529. // TODO error code from pthread_create
  530. return (res);
  531. }
  532. static int clc_cli_cleanup_local (struct amf_comp *comp)
  533. {
  534. dprintf ("clc_cli_cleanup_local\n");
  535. return (0);
  536. }
  537. #if 0
  538. static int clc_terminate (struct amf_comp *comp)
  539. {
  540. int res;
  541. dprintf ("clc terminate for comp %s\n", getSaNameT (&comp->name));
  542. assert (0);
  543. operational_state_comp_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  544. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  545. res = clc_interfaces[comp->comptype]->terminate (comp);
  546. return (0);
  547. }
  548. #endif
  549. struct amf_healthcheck *amf_comp_find_healthcheck (
  550. struct amf_comp *comp, SaAmfHealthcheckKeyT *key)
  551. {
  552. struct amf_healthcheck *healthcheck;
  553. struct amf_healthcheck *ret_healthcheck = 0;
  554. if (key == NULL) {
  555. return NULL;
  556. }
  557. for (healthcheck = comp->healthcheck_head;
  558. healthcheck != NULL;
  559. healthcheck = healthcheck->next) {
  560. if (memcmp (key, &healthcheck->safHealthcheckKey,
  561. sizeof (SaAmfHealthcheckKeyT)) == 0) {
  562. ret_healthcheck = healthcheck;
  563. break;
  564. }
  565. }
  566. return (ret_healthcheck);
  567. }
  568. struct amf_comp *amf_comp_create(struct amf_su *su)
  569. {
  570. struct amf_comp *comp = calloc (1, sizeof (struct amf_comp));
  571. if (comp == NULL) {
  572. openais_exit_error(AIS_DONE_OUT_OF_MEMORY);
  573. }
  574. comp->next = su->comp_head;
  575. su->comp_head = comp;
  576. comp->su = su;
  577. comp->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  578. comp->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  579. comp->saAmfCompNumMaxInstantiateWithoutDelay = 2;
  580. comp->saAmfCompNumMaxAmStartAttempt = 2;
  581. comp->saAmfCompNumMaxAmStopAttempt = 2;
  582. return comp;
  583. }
  584. struct amf_comp *amf_comp_find (struct amf_cluster *cluster, SaNameT *name)
  585. {
  586. struct amf_application *app;
  587. struct amf_sg *sg;
  588. struct amf_su *su;
  589. struct amf_comp *comp = NULL;
  590. char *app_name;
  591. char *sg_name;
  592. char *su_name;
  593. char *comp_name;
  594. char *ptrptr;
  595. char *buf;
  596. /* malloc new buffer since strtok_r writes to its first argument */
  597. buf = malloc (name->length);
  598. memcpy (buf, name->value,name ->length);
  599. comp_name = strtok_r(buf, ",", &ptrptr);
  600. su_name = strtok_r(NULL, ",", &ptrptr);
  601. sg_name = strtok_r(NULL, ",", &ptrptr);
  602. app_name = strtok_r(NULL, ",", &ptrptr);
  603. if (comp_name == NULL || su_name == NULL ||
  604. sg_name == NULL || app_name == NULL) {
  605. goto end;
  606. }
  607. comp_name += 8;
  608. su_name += 6;
  609. sg_name += 6;
  610. app_name += 7;
  611. for (app = cluster->application_head; app != NULL; app = app->next) {
  612. if (strncmp (app_name,
  613. (char*)app->name.value, app->name.length) == 0) {
  614. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  615. if (strncmp (sg_name, (char*)sg->name.value,
  616. sg->name.length) == 0) {
  617. for (su = sg->su_head; su != NULL; su = su->next) {
  618. if (strncmp (su_name, (char*)su->name.value,
  619. su->name.length) == 0) {
  620. for (comp = su->comp_head;
  621. comp != NULL;
  622. comp = comp->next) {
  623. if (strncmp (comp_name,
  624. (char*)comp->name.value,
  625. comp->name.length) == 0) {
  626. goto end;
  627. }
  628. }
  629. }
  630. }
  631. }
  632. }
  633. }
  634. }
  635. end:
  636. free (buf);
  637. return comp;
  638. }
  639. void amf_comp_healthcheck_deactivate (struct amf_comp *comp)
  640. {
  641. struct amf_healthcheck *healthcheck;
  642. if (!amf_su_is_local (comp->su))
  643. return;
  644. ENTER ("'%s'\n", getSaNameT (&comp->name));
  645. for (healthcheck = comp->healthcheck_head;
  646. healthcheck != NULL;
  647. healthcheck = healthcheck->next) {
  648. if (healthcheck->active) {
  649. healthcheck_deactivate (healthcheck);
  650. }
  651. }
  652. }
  653. static void comp_ha_state_set ( struct amf_comp *comp,
  654. struct amf_csi_assignment *csi_assignment,
  655. SaAmfHAStateT ha_state)
  656. {
  657. csi_assignment->saAmfCSICompHAState = ha_state;
  658. TRACE1 ("Setting comp '%s' HA state: %s\n",
  659. comp->name.value, amf_ha_state (csi_assignment->saAmfCSICompHAState));
  660. amf_su_comp_hastate_changed (comp->su, comp, csi_assignment);
  661. }
  662. static void comp_presence_state_set (struct amf_comp *comp,
  663. SaAmfPresenceStateT presence_state)
  664. {
  665. comp->saAmfCompPresenceState = presence_state;
  666. TRACE1 ("Setting comp '%s' presence state: %s\n",
  667. comp->name.value, amf_presence_state (comp->saAmfCompPresenceState));
  668. amf_su_comp_state_changed (
  669. comp->su, comp, SA_AMF_PRESENCE_STATE, presence_state);
  670. }
  671. static void comp_operational_state_set (struct amf_comp *comp,
  672. SaAmfOperationalStateT oper_state)
  673. {
  674. comp->saAmfCompOperState = oper_state;
  675. TRACE1 ("Setting comp '%s' operational state: %s\n",
  676. comp->name.value, amf_op_state (comp->saAmfCompOperState));
  677. amf_su_comp_state_changed (
  678. comp->su, comp, SA_AMF_OP_STATE, oper_state);
  679. }
  680. #if 0
  681. static void lib_csi_remove_request (struct amf_comp *comp,
  682. struct amf_csi *csi)
  683. {
  684. struct res_lib_amf_csiremovecallback res_lib_amf_csiremovecallback;
  685. struct csi_remove_callback_data *csi_remove_callback_data;
  686. dprintf ("\t%s\n", getSaNameT (&comp->name));
  687. res_lib_amf_csiremovecallback.header.id = MESSAGE_RES_AMF_CSIREMOVECALLBACK;
  688. res_lib_amf_csiremovecallback.header.size = sizeof (struct res_lib_amf_csiremovecallback);
  689. res_lib_amf_csiremovecallback.header.error = SA_AIS_OK;
  690. csi_remove_callback_data = malloc (sizeof (struct csi_remove_callback_data));
  691. assert (csi_remove_callback_data); // TODO failure here of malloc
  692. csi_remove_callback_data->csi = csi;
  693. res_lib_amf_csiremovecallback.invocation =
  694. invocation_create (
  695. AMF_RESPONSE_CSIREMOVECALLBACK,
  696. csi_remove_callback_data);
  697. memcpy (&res_lib_amf_csiremovecallback.compName,
  698. &comp->name, sizeof (SaNameT));
  699. memcpy (&res_lib_amf_csiremovecallback.csiName,
  700. &csi->name, sizeof (SaNameT));
  701. res_lib_amf_csiremovecallback.csiFlags = 0;
  702. openais_conn_send_response (
  703. openais_conn_partner_get (comp->conn),
  704. &res_lib_amf_csiremovecallback,
  705. sizeof (struct res_lib_amf_csiremovecallback));
  706. }
  707. #endif
  708. static void comp_reassign_csis (struct amf_comp *comp)
  709. {
  710. struct amf_csi_assignment *csi_assignment = comp->assigned_csis;
  711. ENTER ("'%s'", comp->name.value);
  712. for (; csi_assignment; csi_assignment = csi_assignment->comp_next) {
  713. amf_comp_hastate_set (comp, csi_assignment,
  714. csi_assignment->saAmfCSICompHAState);
  715. }
  716. }
  717. static void healthcheck_deactivate (
  718. struct amf_healthcheck *healthcheck_active)
  719. {
  720. dprintf ("deactivating healthcheck for component %s\n",
  721. getSaNameT (&healthcheck_active->comp->name));
  722. poll_timer_delete (aisexec_poll_handle,
  723. healthcheck_active->timer_handle_period);
  724. poll_timer_delete (aisexec_poll_handle,
  725. healthcheck_active->timer_handle_duration);
  726. invocation_destroy_by_data ((void *)healthcheck_active);
  727. healthcheck_active->active = 0;
  728. }
  729. /**
  730. * This function is called by the timer subsystem when AMF should request
  731. * a new healthcheck from a component.
  732. * @param data
  733. */
  734. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  735. {
  736. struct amf_healthcheck *healthcheck = _healthcheck;
  737. /* send healthcheck request to component */
  738. lib_healthcheck_request (healthcheck);
  739. /* start duration timer for response */
  740. poll_timer_add (aisexec_poll_handle,
  741. healthcheck->saAmfHealthcheckMaxDuration,
  742. (void *)healthcheck,
  743. timer_function_healthcheck_tmo,
  744. &healthcheck->timer_handle_duration);
  745. }
  746. /**
  747. * Multicast a healthcheck timeout event.
  748. * @param healthcheck
  749. */
  750. static void mcast_healthcheck_tmo_event (
  751. struct amf_healthcheck *healthcheck)
  752. {
  753. struct req_exec_amf_healthcheck_tmo req_exec;
  754. struct iovec iovec;
  755. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  756. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  757. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  758. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  759. memcpy (&req_exec.safHealthcheckKey,
  760. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  761. iovec.iov_base = (char *)&req_exec;
  762. iovec.iov_len = sizeof (req_exec);
  763. assert (totempg_groups_mcast_joined (openais_group_handle,
  764. &iovec, 1, TOTEMPG_AGREED) == 0);
  765. }
  766. /**
  767. * This function is called by the timer subsystem when a component has not
  768. * performed a healthcheck on time.
  769. * The event is multicasted to the cluster.
  770. * @param data
  771. */
  772. static void timer_function_healthcheck_tmo (
  773. void *_healthcheck)
  774. {
  775. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  776. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  777. getSaNameT (&healthcheck->comp->name));
  778. mcast_healthcheck_tmo_event (healthcheck);
  779. }
  780. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  781. {
  782. struct res_lib_amf_healthcheckcallback res_lib_amf_healthcheckcallback;
  783. res_lib_amf_healthcheckcallback.header.id =
  784. MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  785. res_lib_amf_healthcheckcallback.header.size =
  786. sizeof (struct res_lib_amf_healthcheckcallback);
  787. res_lib_amf_healthcheckcallback.header.error = SA_AIS_OK;
  788. res_lib_amf_healthcheckcallback.invocation =
  789. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  790. amf_comp_dn_make (healthcheck->comp,
  791. &res_lib_amf_healthcheckcallback.compName);
  792. memcpy (&res_lib_amf_healthcheckcallback.key,
  793. &healthcheck->safHealthcheckKey,
  794. sizeof (SaAmfHealthcheckKeyT));
  795. TRACE8 ("sending healthcheck request to component %s",
  796. res_lib_amf_healthcheckcallback.compName.value);
  797. openais_conn_send_response (
  798. openais_conn_partner_get (healthcheck->comp->conn),
  799. &res_lib_amf_healthcheckcallback,
  800. sizeof (struct res_lib_amf_healthcheckcallback));
  801. }
  802. static void lib_csi_set_request (
  803. struct amf_comp *comp,
  804. struct amf_csi_assignment *csi_assignment,
  805. SaAmfHAStateT requested_ha_state)
  806. {
  807. struct res_lib_amf_csisetcallback* res_lib_amf_csisetcallback;
  808. void* p;
  809. struct amf_csi_attribute *attribute;
  810. size_t char_length_of_csi_attrs=0;
  811. size_t num_of_csi_attrs=0;
  812. int i;
  813. struct amf_csi *csi;
  814. char* csi_attribute_buf;
  815. unsigned int byte_offset;
  816. csi_assignment->requested_ha_state = requested_ha_state;
  817. csi = csi_assignment->csi;
  818. dprintf("\t Assigning CSI '%s' state %s to comp '%s'\n",
  819. getSaNameT (&csi->name), amf_ha_state (requested_ha_state),
  820. comp->name.value);
  821. for (attribute = csi->attributes_head;
  822. attribute != NULL;
  823. attribute = attribute->next) {
  824. for (i = 0; attribute->value[i] != NULL; i++) {
  825. num_of_csi_attrs++;
  826. char_length_of_csi_attrs += strlen(attribute->name);
  827. char_length_of_csi_attrs += strlen(attribute->value[i]);
  828. char_length_of_csi_attrs += 2;
  829. }
  830. }
  831. p = malloc(sizeof(struct res_lib_amf_csisetcallback)+
  832. char_length_of_csi_attrs);
  833. if (p == NULL) {
  834. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  835. }
  836. res_lib_amf_csisetcallback = (struct res_lib_amf_csisetcallback*)p;
  837. /* Address of the buffer containing the Csi name value pair */
  838. csi_attribute_buf = res_lib_amf_csisetcallback->csi_attr_buf;
  839. /* Byteoffset start at the zero byte */
  840. byte_offset = 0;
  841. for (attribute = csi->attributes_head;
  842. attribute != NULL;
  843. attribute = attribute->next) {
  844. for (i = 0; attribute->value[i] != NULL; i++) {
  845. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  846. byte_offset += strlen(attribute->name) + 1;
  847. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  848. byte_offset += strlen(attribute->value[i]) + 1;
  849. }
  850. }
  851. res_lib_amf_csisetcallback->number = num_of_csi_attrs;
  852. res_lib_amf_csisetcallback->csiFlags = SA_AMF_CSI_ADD_ONE;
  853. switch (requested_ha_state) {
  854. case SA_AMF_HA_ACTIVE: {
  855. res_lib_amf_csisetcallback->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  856. res_lib_amf_csisetcallback->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  857. SA_AMF_CSI_NEW_ASSIGN;
  858. break;
  859. }
  860. case SA_AMF_HA_STANDBY: {
  861. res_lib_amf_csisetcallback->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  862. res_lib_amf_csisetcallback->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  863. break;
  864. }
  865. case SA_AMF_HA_QUIESCED: {
  866. /*TODO*/
  867. break;
  868. }
  869. case SA_AMF_HA_QUIESCING: {
  870. /*TODO*/
  871. break;
  872. }
  873. default: {
  874. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  875. break;
  876. }
  877. }
  878. res_lib_amf_csisetcallback->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  879. res_lib_amf_csisetcallback->header.size =
  880. sizeof (struct res_lib_amf_csisetcallback) +
  881. char_length_of_csi_attrs;
  882. res_lib_amf_csisetcallback->header.error = SA_AIS_OK;
  883. amf_comp_dn_make (comp, &res_lib_amf_csisetcallback->compName);
  884. amf_csi_dn_make (csi, &res_lib_amf_csisetcallback->csiName);
  885. res_lib_amf_csisetcallback->haState = requested_ha_state;
  886. res_lib_amf_csisetcallback->invocation =
  887. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  888. openais_conn_send_response (openais_conn_partner_get (comp->conn),
  889. res_lib_amf_csisetcallback,
  890. res_lib_amf_csisetcallback->header.size);
  891. free(p);
  892. }
  893. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  894. {
  895. TRACE2("Exec comp register '%s'", &comp->name.value);
  896. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  897. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  898. if (comp->saAmfCompReadinessState == SA_AMF_READINESS_IN_SERVICE) {
  899. comp_reassign_csis (comp);
  900. }
  901. } else if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  902. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  903. comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  904. }
  905. else {
  906. assert (0);
  907. }
  908. return SA_AIS_OK;
  909. }
  910. void amf_comp_error_report (
  911. struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  912. {
  913. struct res_lib_amf_componenterrorreport res_lib;
  914. TRACE2("Exec comp error report '%s'", &comp->name.value);
  915. if (amf_su_is_local (comp->su)) {
  916. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  917. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  918. res_lib.header.error = SA_AIS_OK;
  919. openais_conn_send_response (comp->conn, &res_lib, sizeof (res_lib));
  920. }
  921. /* report to SU and let it handle the problem */
  922. amf_su_comp_error_suspected (comp->su, comp, recommendedRecovery);
  923. }
  924. /**
  925. * Healthcheck timeout event handler
  926. * @param comp
  927. * @param healthcheck
  928. */
  929. void amf_comp_healthcheck_tmo (
  930. struct amf_comp *comp, struct amf_healthcheck *healthcheck)
  931. {
  932. TRACE2("Exec healthcheck tmo for '%s'", &comp->name.value);
  933. /* report to SU and let it handle the problem */
  934. amf_su_comp_error_suspected (
  935. comp->su, comp, healthcheck->recommendedRecovery);
  936. }
  937. /**
  938. * Event method to be called when a cleanup completed event is received
  939. * @param comp
  940. */
  941. void amf_comp_cleanup_completed (struct amf_comp *comp)
  942. {
  943. TRACE2("Exec CLC cleanup completed for '%s'", &comp->name.value);
  944. amf_comp_instantiate (comp);
  945. }
  946. /**
  947. * Handle the request from a component to start a healthcheck
  948. *
  949. * @param comp
  950. * @param healthcheckKey
  951. * @param invocationType
  952. * @param recommendedRecovery
  953. *
  954. * @return SaAisErrorT - return value to component
  955. */
  956. SaAisErrorT amf_comp_healthcheck_start (
  957. struct amf_comp *comp,
  958. SaAmfHealthcheckKeyT *healthcheckKey,
  959. SaAmfHealthcheckInvocationT invocationType,
  960. SaAmfRecommendedRecoveryT recommendedRecovery)
  961. {
  962. struct amf_healthcheck *healthcheck;
  963. SaAisErrorT error = SA_AIS_OK;
  964. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  965. if (healthcheck == 0) {
  966. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  967. healthcheckKey->key);
  968. error = SA_AIS_ERR_NOT_EXIST;
  969. goto error_exit;
  970. }
  971. dprintf ("Healthcheckstart: '%s', key '%s'",
  972. comp->name.value, healthcheckKey->key);
  973. /*
  974. * Determine if this healthcheck is already active
  975. */
  976. if (healthcheck->active) {
  977. error = SA_AIS_ERR_EXIST;
  978. goto error_exit;
  979. }
  980. /*
  981. * Initialise
  982. */
  983. healthcheck->invocationType = invocationType;
  984. healthcheck->recommendedRecovery = recommendedRecovery;
  985. healthcheck->timer_handle_duration = 0;
  986. healthcheck->timer_handle_period = 0;
  987. healthcheck->active = 1;
  988. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  989. /* start timer to execute first healthcheck request */
  990. poll_timer_add (aisexec_poll_handle,
  991. healthcheck->saAmfHealthcheckPeriod,
  992. (void *)healthcheck,
  993. timer_function_healthcheck_next_fn,
  994. &healthcheck->timer_handle_period);
  995. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  996. /* start supervision timer */
  997. poll_timer_add (aisexec_poll_handle,
  998. healthcheck->saAmfHealthcheckPeriod,
  999. (void *)healthcheck,
  1000. timer_function_healthcheck_tmo,
  1001. &healthcheck->timer_handle_period);
  1002. } else {
  1003. error = SA_AIS_ERR_INVALID_PARAM;
  1004. }
  1005. error_exit:
  1006. return error;
  1007. }
  1008. /**
  1009. * Stop all or a specifed healthcheck
  1010. * @param comp
  1011. * @param healthcheckKey - NULL if all
  1012. *
  1013. * @return SaAisErrorT
  1014. */
  1015. SaAisErrorT amf_comp_healthcheck_stop (
  1016. struct amf_comp *comp,
  1017. SaAmfHealthcheckKeyT *healthcheckKey)
  1018. {
  1019. struct amf_healthcheck *healthcheck;
  1020. SaAisErrorT error = SA_AIS_OK;
  1021. dprintf ("Healthcheckstop: '%s', key '%s'",
  1022. comp->name.value, healthcheckKey->key);
  1023. if (healthcheckKey == NULL) {
  1024. for (healthcheck = comp->healthcheck_head;
  1025. healthcheck != NULL;
  1026. healthcheck = healthcheck->next) {
  1027. healthcheck_deactivate (healthcheck);
  1028. }
  1029. } else {
  1030. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1031. if (healthcheck == NULL) {
  1032. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1033. healthcheckKey->key);
  1034. error = SA_AIS_ERR_NOT_EXIST;
  1035. } else {
  1036. healthcheck_deactivate (healthcheck);
  1037. }
  1038. }
  1039. return error;
  1040. }
  1041. /**
  1042. * Instantiate a component
  1043. * @param comp
  1044. */
  1045. void amf_comp_instantiate (struct amf_comp *comp)
  1046. {
  1047. int res = 0;
  1048. ENTER ("'%s'", getSaNameT (&comp->name));
  1049. if (comp->saAmfCompPresenceState != SA_AMF_PRESENCE_RESTARTING) {
  1050. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATING);
  1051. }
  1052. if (amf_su_is_local (comp->su)) {
  1053. res = clc_interfaces[comp->comptype]->instantiate (comp);
  1054. }
  1055. }
  1056. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1057. SaAmfReadinessStateT state)
  1058. {
  1059. #if 0
  1060. /*
  1061. * Set component readiness state appropriately
  1062. * if unit in service and component is enabled, it is in service
  1063. * otherwise it is out of service page 50 B.02.01
  1064. */
  1065. if (comp->su->saAmfSUReadinessState == SA_AMF_READINESS_IN_SERVICE &&
  1066. comp->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1067. comp->saAmfCompReadinessState = SA_AMF_READINESS_IN_SERVICE;
  1068. } else if (comp->su->saAmfSUReadinessState == SA_AMF_READINESS_STOPPING &&
  1069. comp->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1070. comp->saAmfCompReadinessState = SA_AMF_READINESS_STOPPING;
  1071. } else {
  1072. comp->saAmfCompReadinessState = SA_AMF_READINESS_OUT_OF_SERVICE;
  1073. }
  1074. #endif
  1075. comp->saAmfCompReadinessState = state;
  1076. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1077. comp->name.value, amf_readiness_state (comp->saAmfCompReadinessState));
  1078. }
  1079. /**
  1080. * Handle a component response (received from the lib) of an earlier AMF request.
  1081. * This function should be invoked when the lib request is received.
  1082. * @param invocation [in] associates the response with the request (callback)
  1083. * @param error [in] response from the component of the associated callback
  1084. * @param retval [out] contains return value to component when needed
  1085. *
  1086. * @return ==0 respond to component, do not multicast
  1087. * @return >0 do not respond to component, multicast response
  1088. */
  1089. int amf_comp_response_1 (
  1090. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval)
  1091. {
  1092. int res;
  1093. int interface;
  1094. void *data;
  1095. res = invocation_get (invocation, &interface, &data);
  1096. if (res == -1) {
  1097. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1098. *retval = SA_AIS_ERR_INVALID_PARAM;
  1099. return 0;
  1100. }
  1101. switch (interface) {
  1102. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1103. struct amf_healthcheck *healthcheck = data;
  1104. SaNameT name;
  1105. TRACE3 ("Healthcheck response from '%s': %d",
  1106. amf_comp_dn_make (healthcheck->comp, &name), error);
  1107. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1108. /* the response was on time, delete supervision timer */
  1109. poll_timer_delete (aisexec_poll_handle,
  1110. healthcheck->timer_handle_duration);
  1111. healthcheck->timer_handle_duration = 0;
  1112. /* start timer to execute next healthcheck request */
  1113. poll_timer_add (aisexec_poll_handle,
  1114. healthcheck->saAmfHealthcheckPeriod,
  1115. (void *)healthcheck,
  1116. timer_function_healthcheck_next_fn,
  1117. &healthcheck->timer_handle_period);
  1118. *retval = SA_AIS_OK;
  1119. } else {
  1120. *retval = SA_AIS_ERR_INVALID_PARAM;
  1121. }
  1122. return 0; /* do not multicast event */
  1123. break;
  1124. }
  1125. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1126. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1127. return 1; /* multicast event */
  1128. break;
  1129. #if 0
  1130. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1131. struct component_terminate_callback_data *component_terminate_callback_data;
  1132. component_terminate_callback_data = data;
  1133. dprintf ("Lib component terminate callback response, error: %d", error);
  1134. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1135. escalation_policy_restart (component_terminate_callback_data->comp);
  1136. return 1;
  1137. break;
  1138. }
  1139. #endif
  1140. default:
  1141. assert (0);
  1142. break;
  1143. }
  1144. }
  1145. /**
  1146. * Handle a component response (received from EVS) of an earlier AMF request.
  1147. * This function should be invoked when the multicast request is received.
  1148. * @param invocation [in] associates the response with the request (callback)
  1149. * @param error [in] response from the component of the associated callback
  1150. * @param retval [out] contains return value to component when needed
  1151. *
  1152. * @return component to which the response should be sent
  1153. */
  1154. struct amf_comp *amf_comp_response_2 (
  1155. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval)
  1156. {
  1157. int res;
  1158. int interface;
  1159. void *data;
  1160. struct amf_comp *comp = NULL;
  1161. assert (retval != NULL);
  1162. *retval = SA_AIS_OK;
  1163. res = invocation_get_and_destroy (invocation, &interface, &data);
  1164. if (res == -1) {
  1165. log_printf (LOG_ERR, "Comp response: invocation not found\n");
  1166. *retval = SA_AIS_ERR_INVALID_PARAM;
  1167. return NULL;
  1168. }
  1169. switch (interface) {
  1170. case AMF_RESPONSE_CSISETCALLBACK: {
  1171. struct amf_csi_assignment *csi_assignment = data;
  1172. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1173. csi_assignment->csi->name.value, csi_assignment->comp->name.value, error);
  1174. comp = csi_assignment->comp;
  1175. if (error == SA_AIS_OK) {
  1176. comp_ha_state_set (comp, csi_assignment,
  1177. csi_assignment->requested_ha_state);
  1178. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1179. amf_su_comp_error_suspected (comp->su, comp,
  1180. comp->saAmfCompRecoveryOnError);
  1181. } else {
  1182. *retval = SA_AIS_ERR_INVALID_PARAM;
  1183. }
  1184. break;
  1185. }
  1186. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1187. struct amf_csi_assignment *csi_assignment = data;
  1188. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1189. csi_assignment->csi->name.value, csi_assignment->comp->name.value, error);
  1190. comp = csi_assignment->comp;
  1191. amf_su_comp_hastate_changed (comp->su, comp, csi_assignment);
  1192. break;
  1193. }
  1194. #if 0
  1195. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK:
  1196. break;
  1197. #endif
  1198. default:
  1199. assert (0);
  1200. break;
  1201. }
  1202. return comp;
  1203. }
  1204. /**
  1205. * Request a component to assume a particular HA state
  1206. * @param comp
  1207. * @param csi_assignment
  1208. * @param requested_ha_state
  1209. */
  1210. void amf_comp_hastate_set (
  1211. struct amf_comp *comp,
  1212. struct amf_csi_assignment *csi_assignment,
  1213. SaAmfHAStateT requested_ha_state)
  1214. {
  1215. assert (comp != NULL && csi_assignment != NULL);
  1216. if (!amf_su_is_local (comp->su))
  1217. return;
  1218. lib_csi_set_request(comp, csi_assignment, requested_ha_state);
  1219. }
  1220. /**
  1221. * Request termination of a component
  1222. * @param comp
  1223. */
  1224. void amf_comp_terminate (struct amf_comp *comp)
  1225. {
  1226. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1227. amf_comp_healthcheck_stop (comp, NULL);
  1228. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1229. if (amf_su_is_local (comp->su)) {
  1230. clc_interfaces[comp->comptype]->terminate (comp);
  1231. }
  1232. }
  1233. /**
  1234. * Request restart of a component
  1235. * @param comp
  1236. */
  1237. void amf_comp_restart (struct amf_comp *comp)
  1238. {
  1239. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1240. amf_comp_healthcheck_stop (comp, NULL);
  1241. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1242. if (amf_su_is_local (comp->su)) {
  1243. clc_interfaces[comp->comptype]->cleanup (comp);
  1244. }
  1245. }
  1246. /**
  1247. * Request to return the HA state for a components CSI
  1248. * @param comp
  1249. * @param csi_name
  1250. * @param ha_state
  1251. *
  1252. * @return SaAisErrorT
  1253. */
  1254. SaAisErrorT amf_comp_hastate_get (
  1255. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1256. {
  1257. struct amf_csi_assignment *assignment;
  1258. SaNameT name;
  1259. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1260. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1261. getSaNameT (&comp->name), csi_name->value);
  1262. for (assignment = comp->assigned_csis;
  1263. assignment != NULL; assignment = assignment->comp_next) {
  1264. amf_csi_dn_make (assignment->csi, &name);
  1265. if (name_match (csi_name, &name)) {
  1266. *ha_state = assignment->saAmfCSICompHAState;
  1267. return SA_AIS_OK;
  1268. }
  1269. }
  1270. return SA_AIS_ERR_INVALID_PARAM;
  1271. }
  1272. /**
  1273. * Response from a component informs AMF that it has performed a healthcheck
  1274. * @param comp
  1275. * @param healthcheckKey
  1276. * @param healthcheckResult
  1277. *
  1278. * @return SaAisErrorT
  1279. */
  1280. SaAisErrorT amf_comp_healthcheck_confirm (
  1281. struct amf_comp *comp,
  1282. SaAmfHealthcheckKeyT *healthcheckKey,
  1283. SaAisErrorT healthcheckResult)
  1284. {
  1285. struct amf_healthcheck *healthcheck;
  1286. SaAisErrorT error = SA_AIS_OK;
  1287. dprintf ("Healthcheckconfirm: '%s', key '%s'",
  1288. comp->name.value, healthcheckKey->key);
  1289. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1290. if (healthcheck == NULL) {
  1291. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1292. healthcheckKey->key);
  1293. error = SA_AIS_ERR_NOT_EXIST;
  1294. } else if (healthcheck->active) {
  1295. if (healthcheckResult == SA_AIS_OK) {
  1296. /* the response was on time, restart the supervision timer */
  1297. poll_timer_delete (aisexec_poll_handle,
  1298. healthcheck->timer_handle_period);
  1299. poll_timer_add (aisexec_poll_handle,
  1300. healthcheck->saAmfHealthcheckPeriod,
  1301. (void *)healthcheck,
  1302. timer_function_healthcheck_tmo,
  1303. &healthcheck->timer_handle_period);
  1304. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1305. /* send to cluster */
  1306. mcast_healthcheck_tmo_event (healthcheck);
  1307. } else {
  1308. error = SA_AIS_ERR_INVALID_PARAM;
  1309. }
  1310. } else {
  1311. error = SA_AIS_ERR_INVALID_PARAM;
  1312. }
  1313. return error;
  1314. }
  1315. void amf_comp_init (void)
  1316. {
  1317. log_init ("AMF");
  1318. }