amfcomp.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 "main.h"
  136. #include "ipc.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. openais_timer_delete (healthcheck_active->timer_handle_period);
  723. openais_timer_delete (healthcheck_active->timer_handle_duration);
  724. invocation_destroy_by_data ((void *)healthcheck_active);
  725. healthcheck_active->active = 0;
  726. }
  727. /**
  728. * This function is called by the timer subsystem when AMF should request
  729. * a new healthcheck from a component.
  730. * @param data
  731. */
  732. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  733. {
  734. struct amf_healthcheck *healthcheck = _healthcheck;
  735. /* send healthcheck request to component */
  736. lib_healthcheck_request (healthcheck);
  737. /* start duration timer for response */
  738. openais_timer_add (
  739. healthcheck->saAmfHealthcheckMaxDuration,
  740. (void *)healthcheck,
  741. timer_function_healthcheck_tmo,
  742. &healthcheck->timer_handle_duration);
  743. }
  744. /**
  745. * Multicast a healthcheck timeout event.
  746. * @param healthcheck
  747. */
  748. static void mcast_healthcheck_tmo_event (
  749. struct amf_healthcheck *healthcheck)
  750. {
  751. struct req_exec_amf_healthcheck_tmo req_exec;
  752. struct iovec iovec;
  753. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  754. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  755. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  756. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  757. memcpy (&req_exec.safHealthcheckKey,
  758. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  759. iovec.iov_base = (char *)&req_exec;
  760. iovec.iov_len = sizeof (req_exec);
  761. assert (totempg_groups_mcast_joined (openais_group_handle,
  762. &iovec, 1, TOTEMPG_AGREED) == 0);
  763. }
  764. /**
  765. * This function is called by the timer subsystem when a component has not
  766. * performed a healthcheck on time.
  767. * The event is multicasted to the cluster.
  768. * @param data
  769. */
  770. static void timer_function_healthcheck_tmo (
  771. void *_healthcheck)
  772. {
  773. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  774. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  775. getSaNameT (&healthcheck->comp->name));
  776. mcast_healthcheck_tmo_event (healthcheck);
  777. }
  778. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  779. {
  780. struct res_lib_amf_healthcheckcallback res_lib_amf_healthcheckcallback;
  781. res_lib_amf_healthcheckcallback.header.id =
  782. MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  783. res_lib_amf_healthcheckcallback.header.size =
  784. sizeof (struct res_lib_amf_healthcheckcallback);
  785. res_lib_amf_healthcheckcallback.header.error = SA_AIS_OK;
  786. res_lib_amf_healthcheckcallback.invocation =
  787. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  788. amf_comp_dn_make (healthcheck->comp,
  789. &res_lib_amf_healthcheckcallback.compName);
  790. memcpy (&res_lib_amf_healthcheckcallback.key,
  791. &healthcheck->safHealthcheckKey,
  792. sizeof (SaAmfHealthcheckKeyT));
  793. TRACE8 ("sending healthcheck request to component %s",
  794. res_lib_amf_healthcheckcallback.compName.value);
  795. openais_conn_send_response (
  796. openais_conn_partner_get (healthcheck->comp->conn),
  797. &res_lib_amf_healthcheckcallback,
  798. sizeof (struct res_lib_amf_healthcheckcallback));
  799. }
  800. static void lib_csi_set_request (
  801. struct amf_comp *comp,
  802. struct amf_csi_assignment *csi_assignment,
  803. SaAmfHAStateT requested_ha_state)
  804. {
  805. struct res_lib_amf_csisetcallback* res_lib_amf_csisetcallback;
  806. void* p;
  807. struct amf_csi_attribute *attribute;
  808. size_t char_length_of_csi_attrs=0;
  809. size_t num_of_csi_attrs=0;
  810. int i;
  811. struct amf_csi *csi;
  812. char* csi_attribute_buf;
  813. unsigned int byte_offset;
  814. csi_assignment->requested_ha_state = requested_ha_state;
  815. csi = csi_assignment->csi;
  816. dprintf("\t Assigning CSI '%s' state %s to comp '%s'\n",
  817. getSaNameT (&csi->name), amf_ha_state (requested_ha_state),
  818. comp->name.value);
  819. for (attribute = csi->attributes_head;
  820. attribute != NULL;
  821. attribute = attribute->next) {
  822. for (i = 0; attribute->value[i] != NULL; i++) {
  823. num_of_csi_attrs++;
  824. char_length_of_csi_attrs += strlen(attribute->name);
  825. char_length_of_csi_attrs += strlen(attribute->value[i]);
  826. char_length_of_csi_attrs += 2;
  827. }
  828. }
  829. p = malloc(sizeof(struct res_lib_amf_csisetcallback)+
  830. char_length_of_csi_attrs);
  831. if (p == NULL) {
  832. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  833. }
  834. res_lib_amf_csisetcallback = (struct res_lib_amf_csisetcallback*)p;
  835. /* Address of the buffer containing the Csi name value pair */
  836. csi_attribute_buf = res_lib_amf_csisetcallback->csi_attr_buf;
  837. /* Byteoffset start at the zero byte */
  838. byte_offset = 0;
  839. for (attribute = csi->attributes_head;
  840. attribute != NULL;
  841. attribute = attribute->next) {
  842. for (i = 0; attribute->value[i] != NULL; i++) {
  843. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  844. byte_offset += strlen(attribute->name) + 1;
  845. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  846. byte_offset += strlen(attribute->value[i]) + 1;
  847. }
  848. }
  849. res_lib_amf_csisetcallback->number = num_of_csi_attrs;
  850. res_lib_amf_csisetcallback->csiFlags = SA_AMF_CSI_ADD_ONE;
  851. switch (requested_ha_state) {
  852. case SA_AMF_HA_ACTIVE: {
  853. res_lib_amf_csisetcallback->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  854. res_lib_amf_csisetcallback->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  855. SA_AMF_CSI_NEW_ASSIGN;
  856. break;
  857. }
  858. case SA_AMF_HA_STANDBY: {
  859. res_lib_amf_csisetcallback->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  860. res_lib_amf_csisetcallback->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  861. break;
  862. }
  863. case SA_AMF_HA_QUIESCED: {
  864. /*TODO*/
  865. break;
  866. }
  867. case SA_AMF_HA_QUIESCING: {
  868. /*TODO*/
  869. break;
  870. }
  871. default: {
  872. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  873. break;
  874. }
  875. }
  876. res_lib_amf_csisetcallback->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  877. res_lib_amf_csisetcallback->header.size =
  878. sizeof (struct res_lib_amf_csisetcallback) +
  879. char_length_of_csi_attrs;
  880. res_lib_amf_csisetcallback->header.error = SA_AIS_OK;
  881. amf_comp_dn_make (comp, &res_lib_amf_csisetcallback->compName);
  882. amf_csi_dn_make (csi, &res_lib_amf_csisetcallback->csiName);
  883. res_lib_amf_csisetcallback->haState = requested_ha_state;
  884. res_lib_amf_csisetcallback->invocation =
  885. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  886. openais_conn_send_response (openais_conn_partner_get (comp->conn),
  887. res_lib_amf_csisetcallback,
  888. res_lib_amf_csisetcallback->header.size);
  889. free(p);
  890. }
  891. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  892. {
  893. TRACE2("Exec comp register '%s'", &comp->name.value);
  894. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  895. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  896. if (comp->saAmfCompReadinessState == SA_AMF_READINESS_IN_SERVICE) {
  897. comp_reassign_csis (comp);
  898. }
  899. } else if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING) {
  900. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  901. comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  902. }
  903. else {
  904. assert (0);
  905. }
  906. return SA_AIS_OK;
  907. }
  908. void amf_comp_error_report (
  909. struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  910. {
  911. struct res_lib_amf_componenterrorreport res_lib;
  912. TRACE2("Exec comp error report '%s'", &comp->name.value);
  913. if (amf_su_is_local (comp->su)) {
  914. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  915. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  916. res_lib.header.error = SA_AIS_OK;
  917. openais_conn_send_response (comp->conn, &res_lib, sizeof (res_lib));
  918. }
  919. /* report to SU and let it handle the problem */
  920. amf_su_comp_error_suspected (comp->su, comp, recommendedRecovery);
  921. }
  922. /**
  923. * Healthcheck timeout event handler
  924. * @param comp
  925. * @param healthcheck
  926. */
  927. void amf_comp_healthcheck_tmo (
  928. struct amf_comp *comp, struct amf_healthcheck *healthcheck)
  929. {
  930. TRACE2("Exec healthcheck tmo for '%s'", &comp->name.value);
  931. /* report to SU and let it handle the problem */
  932. amf_su_comp_error_suspected (
  933. comp->su, comp, healthcheck->recommendedRecovery);
  934. }
  935. /**
  936. * Event method to be called when a cleanup completed event is received
  937. * @param comp
  938. */
  939. void amf_comp_cleanup_completed (struct amf_comp *comp)
  940. {
  941. TRACE2("Exec CLC cleanup completed for '%s'", &comp->name.value);
  942. amf_comp_instantiate (comp);
  943. }
  944. /**
  945. * Handle the request from a component to start a healthcheck
  946. *
  947. * @param comp
  948. * @param healthcheckKey
  949. * @param invocationType
  950. * @param recommendedRecovery
  951. *
  952. * @return SaAisErrorT - return value to component
  953. */
  954. SaAisErrorT amf_comp_healthcheck_start (
  955. struct amf_comp *comp,
  956. SaAmfHealthcheckKeyT *healthcheckKey,
  957. SaAmfHealthcheckInvocationT invocationType,
  958. SaAmfRecommendedRecoveryT recommendedRecovery)
  959. {
  960. struct amf_healthcheck *healthcheck;
  961. SaAisErrorT error = SA_AIS_OK;
  962. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  963. if (healthcheck == 0) {
  964. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  965. healthcheckKey->key);
  966. error = SA_AIS_ERR_NOT_EXIST;
  967. goto error_exit;
  968. }
  969. dprintf ("Healthcheckstart: '%s', key '%s'",
  970. comp->name.value, healthcheckKey->key);
  971. /*
  972. * Determine if this healthcheck is already active
  973. */
  974. if (healthcheck->active) {
  975. error = SA_AIS_ERR_EXIST;
  976. goto error_exit;
  977. }
  978. /*
  979. * Initialise
  980. */
  981. healthcheck->invocationType = invocationType;
  982. healthcheck->recommendedRecovery = recommendedRecovery;
  983. healthcheck->timer_handle_duration = 0;
  984. healthcheck->timer_handle_period = 0;
  985. healthcheck->active = 1;
  986. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  987. /* start timer to execute first healthcheck request */
  988. openais_timer_add (
  989. healthcheck->saAmfHealthcheckPeriod,
  990. (void *)healthcheck,
  991. timer_function_healthcheck_next_fn,
  992. &healthcheck->timer_handle_period);
  993. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  994. /* start supervision timer */
  995. openais_timer_add (
  996. healthcheck->saAmfHealthcheckPeriod,
  997. (void *)healthcheck,
  998. timer_function_healthcheck_tmo,
  999. &healthcheck->timer_handle_period);
  1000. } else {
  1001. error = SA_AIS_ERR_INVALID_PARAM;
  1002. }
  1003. error_exit:
  1004. return error;
  1005. }
  1006. /**
  1007. * Stop all or a specifed healthcheck
  1008. * @param comp
  1009. * @param healthcheckKey - NULL if all
  1010. *
  1011. * @return SaAisErrorT
  1012. */
  1013. SaAisErrorT amf_comp_healthcheck_stop (
  1014. struct amf_comp *comp,
  1015. SaAmfHealthcheckKeyT *healthcheckKey)
  1016. {
  1017. struct amf_healthcheck *healthcheck;
  1018. SaAisErrorT error = SA_AIS_OK;
  1019. dprintf ("Healthcheckstop: '%s', key '%s'",
  1020. comp->name.value, healthcheckKey->key);
  1021. if (healthcheckKey == NULL) {
  1022. for (healthcheck = comp->healthcheck_head;
  1023. healthcheck != NULL;
  1024. healthcheck = healthcheck->next) {
  1025. healthcheck_deactivate (healthcheck);
  1026. }
  1027. } else {
  1028. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1029. if (healthcheck == NULL) {
  1030. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1031. healthcheckKey->key);
  1032. error = SA_AIS_ERR_NOT_EXIST;
  1033. } else {
  1034. healthcheck_deactivate (healthcheck);
  1035. }
  1036. }
  1037. return error;
  1038. }
  1039. /**
  1040. * Instantiate a component
  1041. * @param comp
  1042. */
  1043. void amf_comp_instantiate (struct amf_comp *comp)
  1044. {
  1045. int res = 0;
  1046. ENTER ("'%s'", getSaNameT (&comp->name));
  1047. if (comp->saAmfCompPresenceState != SA_AMF_PRESENCE_RESTARTING) {
  1048. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATING);
  1049. }
  1050. if (amf_su_is_local (comp->su)) {
  1051. res = clc_interfaces[comp->comptype]->instantiate (comp);
  1052. }
  1053. }
  1054. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1055. SaAmfReadinessStateT state)
  1056. {
  1057. #if 0
  1058. /*
  1059. * Set component readiness state appropriately
  1060. * if unit in service and component is enabled, it is in service
  1061. * otherwise it is out of service page 50 B.02.01
  1062. */
  1063. if (comp->su->saAmfSUReadinessState == SA_AMF_READINESS_IN_SERVICE &&
  1064. comp->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1065. comp->saAmfCompReadinessState = SA_AMF_READINESS_IN_SERVICE;
  1066. } else if (comp->su->saAmfSUReadinessState == SA_AMF_READINESS_STOPPING &&
  1067. comp->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1068. comp->saAmfCompReadinessState = SA_AMF_READINESS_STOPPING;
  1069. } else {
  1070. comp->saAmfCompReadinessState = SA_AMF_READINESS_OUT_OF_SERVICE;
  1071. }
  1072. #endif
  1073. comp->saAmfCompReadinessState = state;
  1074. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1075. comp->name.value, amf_readiness_state (comp->saAmfCompReadinessState));
  1076. }
  1077. /**
  1078. * Handle a component response (received from the lib) of an earlier AMF request.
  1079. * This function should be invoked when the lib request is received.
  1080. * @param invocation [in] associates the response with the request (callback)
  1081. * @param error [in] response from the component of the associated callback
  1082. * @param retval [out] contains return value to component when needed
  1083. *
  1084. * @return ==0 respond to component, do not multicast
  1085. * @return >0 do not respond to component, multicast response
  1086. */
  1087. int amf_comp_response_1 (
  1088. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval)
  1089. {
  1090. int res;
  1091. int interface;
  1092. void *data;
  1093. res = invocation_get (invocation, &interface, &data);
  1094. if (res == -1) {
  1095. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1096. *retval = SA_AIS_ERR_INVALID_PARAM;
  1097. return 0;
  1098. }
  1099. switch (interface) {
  1100. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1101. struct amf_healthcheck *healthcheck = data;
  1102. SaNameT name;
  1103. TRACE3 ("Healthcheck response from '%s': %d",
  1104. amf_comp_dn_make (healthcheck->comp, &name), error);
  1105. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1106. /* the response was on time, delete supervision timer */
  1107. openais_timer_delete (healthcheck->timer_handle_duration);
  1108. healthcheck->timer_handle_duration = 0;
  1109. /* start timer to execute next healthcheck request */
  1110. openais_timer_add (
  1111. healthcheck->saAmfHealthcheckPeriod,
  1112. (void *)healthcheck,
  1113. timer_function_healthcheck_next_fn,
  1114. &healthcheck->timer_handle_period);
  1115. *retval = SA_AIS_OK;
  1116. } else {
  1117. *retval = SA_AIS_ERR_INVALID_PARAM;
  1118. }
  1119. return 0; /* do not multicast event */
  1120. break;
  1121. }
  1122. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1123. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1124. return 1; /* multicast event */
  1125. break;
  1126. #if 0
  1127. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1128. struct component_terminate_callback_data *component_terminate_callback_data;
  1129. component_terminate_callback_data = data;
  1130. dprintf ("Lib component terminate callback response, error: %d", error);
  1131. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1132. escalation_policy_restart (component_terminate_callback_data->comp);
  1133. return 1;
  1134. break;
  1135. }
  1136. #endif
  1137. default:
  1138. assert (0);
  1139. break;
  1140. }
  1141. }
  1142. /**
  1143. * Handle a component response (received from EVS) of an earlier AMF request.
  1144. * This function should be invoked when the multicast request is received.
  1145. * @param invocation [in] associates the response with the request (callback)
  1146. * @param error [in] response from the component of the associated callback
  1147. * @param retval [out] contains return value to component when needed
  1148. *
  1149. * @return component to which the response should be sent
  1150. */
  1151. struct amf_comp *amf_comp_response_2 (
  1152. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval)
  1153. {
  1154. int res;
  1155. int interface;
  1156. void *data;
  1157. struct amf_comp *comp = NULL;
  1158. assert (retval != NULL);
  1159. *retval = SA_AIS_OK;
  1160. res = invocation_get_and_destroy (invocation, &interface, &data);
  1161. if (res == -1) {
  1162. log_printf (LOG_ERR, "Comp response: invocation not found\n");
  1163. *retval = SA_AIS_ERR_INVALID_PARAM;
  1164. return NULL;
  1165. }
  1166. switch (interface) {
  1167. case AMF_RESPONSE_CSISETCALLBACK: {
  1168. struct amf_csi_assignment *csi_assignment = data;
  1169. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1170. csi_assignment->csi->name.value, csi_assignment->comp->name.value, error);
  1171. comp = csi_assignment->comp;
  1172. if (error == SA_AIS_OK) {
  1173. comp_ha_state_set (comp, csi_assignment,
  1174. csi_assignment->requested_ha_state);
  1175. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1176. amf_su_comp_error_suspected (comp->su, comp,
  1177. comp->saAmfCompRecoveryOnError);
  1178. } else {
  1179. *retval = SA_AIS_ERR_INVALID_PARAM;
  1180. }
  1181. break;
  1182. }
  1183. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1184. struct amf_csi_assignment *csi_assignment = data;
  1185. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1186. csi_assignment->csi->name.value, csi_assignment->comp->name.value, error);
  1187. comp = csi_assignment->comp;
  1188. amf_su_comp_hastate_changed (comp->su, comp, csi_assignment);
  1189. break;
  1190. }
  1191. #if 0
  1192. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK:
  1193. break;
  1194. #endif
  1195. default:
  1196. assert (0);
  1197. break;
  1198. }
  1199. return comp;
  1200. }
  1201. /**
  1202. * Request a component to assume a particular HA state
  1203. * @param comp
  1204. * @param csi_assignment
  1205. * @param requested_ha_state
  1206. */
  1207. void amf_comp_hastate_set (
  1208. struct amf_comp *comp,
  1209. struct amf_csi_assignment *csi_assignment,
  1210. SaAmfHAStateT requested_ha_state)
  1211. {
  1212. assert (comp != NULL && csi_assignment != NULL);
  1213. if (!amf_su_is_local (comp->su))
  1214. return;
  1215. lib_csi_set_request(comp, csi_assignment, requested_ha_state);
  1216. }
  1217. /**
  1218. * Request termination of a component
  1219. * @param comp
  1220. */
  1221. void amf_comp_terminate (struct amf_comp *comp)
  1222. {
  1223. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1224. amf_comp_healthcheck_stop (comp, NULL);
  1225. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1226. if (amf_su_is_local (comp->su)) {
  1227. clc_interfaces[comp->comptype]->terminate (comp);
  1228. }
  1229. }
  1230. /**
  1231. * Request restart of a component
  1232. * @param comp
  1233. */
  1234. void amf_comp_restart (struct amf_comp *comp)
  1235. {
  1236. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1237. amf_comp_healthcheck_stop (comp, NULL);
  1238. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1239. if (amf_su_is_local (comp->su)) {
  1240. clc_interfaces[comp->comptype]->cleanup (comp);
  1241. }
  1242. }
  1243. /**
  1244. * Request to return the HA state for a components CSI
  1245. * @param comp
  1246. * @param csi_name
  1247. * @param ha_state
  1248. *
  1249. * @return SaAisErrorT
  1250. */
  1251. SaAisErrorT amf_comp_hastate_get (
  1252. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1253. {
  1254. struct amf_csi_assignment *assignment;
  1255. SaNameT name;
  1256. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1257. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1258. getSaNameT (&comp->name), csi_name->value);
  1259. for (assignment = comp->assigned_csis;
  1260. assignment != NULL; assignment = assignment->comp_next) {
  1261. amf_csi_dn_make (assignment->csi, &name);
  1262. if (name_match (csi_name, &name)) {
  1263. *ha_state = assignment->saAmfCSICompHAState;
  1264. return SA_AIS_OK;
  1265. }
  1266. }
  1267. return SA_AIS_ERR_INVALID_PARAM;
  1268. }
  1269. /**
  1270. * Response from a component informs AMF that it has performed a healthcheck
  1271. * @param comp
  1272. * @param healthcheckKey
  1273. * @param healthcheckResult
  1274. *
  1275. * @return SaAisErrorT
  1276. */
  1277. SaAisErrorT amf_comp_healthcheck_confirm (
  1278. struct amf_comp *comp,
  1279. SaAmfHealthcheckKeyT *healthcheckKey,
  1280. SaAisErrorT healthcheckResult)
  1281. {
  1282. struct amf_healthcheck *healthcheck;
  1283. SaAisErrorT error = SA_AIS_OK;
  1284. dprintf ("Healthcheckconfirm: '%s', key '%s'",
  1285. comp->name.value, healthcheckKey->key);
  1286. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1287. if (healthcheck == NULL) {
  1288. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1289. healthcheckKey->key);
  1290. error = SA_AIS_ERR_NOT_EXIST;
  1291. } else if (healthcheck->active) {
  1292. if (healthcheckResult == SA_AIS_OK) {
  1293. /* the response was on time, restart the supervision timer */
  1294. openais_timer_delete (healthcheck->timer_handle_period);
  1295. openais_timer_add (
  1296. healthcheck->saAmfHealthcheckPeriod,
  1297. (void *)healthcheck,
  1298. timer_function_healthcheck_tmo,
  1299. &healthcheck->timer_handle_period);
  1300. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1301. /* send to cluster */
  1302. mcast_healthcheck_tmo_event (healthcheck);
  1303. } else {
  1304. error = SA_AIS_ERR_INVALID_PARAM;
  1305. }
  1306. } else {
  1307. error = SA_AIS_ERR_INVALID_PARAM;
  1308. }
  1309. return error;
  1310. }
  1311. void amf_comp_init (void)
  1312. {
  1313. log_init ("AMF");
  1314. }