amfcomp.c 46 KB

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