amfcomp.c 48 KB

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