1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
9 // Description: Header file for MainController
10 // Author: Janos Zoltan Szabo
11 // mail: tmpjsz@eth.ericsson.se
13 // Copyright (c) 2000-2015 Ericsson Telecom AB
15 #ifndef MCTR_MAINCONTROLLER_H
16 #define MCTR_MAINCONTROLLER_H
17 //----------------------------------------------------------------------------
20 #include <sys/types.h>
21 #include <netinet/in.h>
23 #include "../../core/Types.h"
24 #include "../../common/NetworkHandler.hh"
27 #include "UserInterface.h"
37 //----------------------------------------------------------------------------
41 //----------------------------------------------------------------------------
43 /* Type definitions */
45 /** For representing the global state of MC */
47 MC_INACTIVE
, MC_LISTENING
, MC_LISTENING_CONFIGURED
, MC_HC_CONNECTED
,
48 MC_CONFIGURING
, MC_ACTIVE
, MC_SHUTDOWN
, MC_CREATING_MTC
, MC_READY
,
49 MC_TERMINATING_MTC
, MC_EXECUTING_CONTROL
, MC_EXECUTING_TESTCASE
,
50 MC_TERMINATING_TESTCASE
, MC_PAUSED
53 /** Data structure for unknown incoming connections (before receiving
54 * the first message) */
55 struct unknown_connection
{
59 unknown_connection
*prev
, *next
;
60 bool unix_socket
; // true only if the connection is through unix domain socket
63 /** Data structure for describing the component location
70 /** Data structure for describing the component location
72 struct host_group_struct
{
74 boolean has_all_hosts
, has_all_components
;
75 string_set host_members
, assigned_components
;
78 /** Possible states of a HC */
79 enum hc_state_enum
{ HC_IDLE
, HC_CONFIGURING
, HC_ACTIVE
, HC_OVERLOADED
,
80 HC_CONFIGURING_OVERLOADED
, HC_EXITING
, HC_DOWN
};
82 /** Data structure for each host (and the corresponding HC) */
85 char *hostname
; /**< hostname retrieved from DNS */
86 char *hostname_local
; /**< hostname sent in VERSION message */
91 boolean transport_supported
[TRANSPORT_NUM
];
93 hc_state_enum hc_state
;
97 component
*components
;
98 /* to implement load balancing mechanisms */
99 string_set allowed_components
;
100 boolean all_components_allowed
;
101 boolean local_hostname_different
;
102 int n_active_components
;
105 struct component_struct
;
107 /** Container of test components (when a pending operation can be
108 * requested by several components) */
109 struct requestor_struct
{
112 component_struct
*the_component
;
113 component_struct
**components
;
117 /** Possible states of a port connection or mapping */
118 enum conn_state_enum
{ CONN_LISTENING
, CONN_CONNECTING
, CONN_CONNECTED
,
119 CONN_DISCONNECTING
, CONN_MAPPING
, CONN_MAPPED
, CONN_UNMAPPING
};
121 /** Data structure for representing a port connection */
122 struct port_connection
{
123 conn_state_enum conn_state
;
124 transport_type_enum transport_type
;
128 port_connection
*next
, *prev
;
130 requestor_struct requestors
;
134 /** Structure for timers */
135 struct timer_struct
{
139 component_struct
*component_ptr
;
141 timer_struct
*prev
, *next
;
144 /** Possible states of a TC (MTC or PTC) */
145 enum tc_state_enum
{ TC_INITIAL
, TC_IDLE
, TC_CREATE
, TC_START
, TC_STOP
, TC_KILL
,
146 TC_CONNECT
, TC_DISCONNECT
, TC_MAP
, TC_UNMAP
, TC_STOPPING
, TC_EXITING
,
148 MTC_CONTROLPART
, MTC_TESTCASE
, MTC_ALL_COMPONENT_STOP
,
149 MTC_ALL_COMPONENT_KILL
, MTC_TERMINATING_TESTCASE
, MTC_PAUSED
,
150 PTC_FUNCTION
, PTC_STARTING
, PTC_STOPPED
, PTC_KILLING
, PTC_STOPPING_KILLING
,
151 PTC_STALE
, TC_SYSTEM
};
153 /** Data structure for each TC */
154 struct component_struct
{
156 qualified_name comp_type
;
158 char *log_source
; /**< used for console log messages. format: name\@host */
159 host_struct
*comp_location
;
160 tc_state_enum tc_state
;
161 verdicttype local_verdict
;
162 char* verdict_reason
;
165 /** Identifier of the TTCN-3 testcase or function that is currently being
166 * executed on the test component */
167 qualified_name tc_fn_name
;
168 /* fields for implementing the construct 'value returning done' */
170 int return_value_len
;
173 boolean stop_requested
; /**< only for 'all component.running' */
174 boolean process_killed
;
176 /** used in state TC_INITIAL */
178 component_struct
*create_requestor
;
181 /** used in state PTC_STARTING */
183 component_struct
*start_requestor
;
186 requestor_struct cancel_done_sent_to
;
188 /** used in states TC_STOPPING, PTC_STOPPING_KILLING, PTC_KILLING */
190 requestor_struct stop_requestors
;
191 requestor_struct kill_requestors
;
194 requestor_struct done_requestors
;
195 requestor_struct killed_requestors
;
196 requestor_struct cancel_done_sent_for
;
197 timer_struct
*kill_timer
;
198 /* fields for registering port connections */
199 port_connection
*conn_head_list
, *conn_tail_list
;
200 int conn_head_count
, conn_tail_count
;
203 /** Selector for the table of file descriptors */
204 enum fd_type_enum
{ FD_UNUSED
, FD_PIPE
, FD_SERVER
, FD_UNKNOWN
, FD_HC
, FD_TC
};
206 /** Element of the file descriptor table. The table is indexed by the
208 struct fd_table_struct
{
209 fd_type_enum fd_type
;
211 unknown_connection
*unknown_ptr
;
212 host_struct
*host_ptr
;
213 component_struct
*component_ptr
;
218 /** Structure for storing the checksum of a module */
219 struct module_version_info
{
222 unsigned char *module_checksum
;
225 /** Possible reasons for waking up the MC thread from the main thread. */
226 enum wakeup_reason_t
{ REASON_NOTHING
, REASON_SHUTDOWN
, REASON_MTC_KILL_TIMER
};
228 /** The MainController class. The collection of all functions and data
230 class MainController
{
231 /* private members */
232 static UserInterface
*ui
;
233 static NetworkHandler nh
;
235 static mc_state_enum mc_state
;
236 static char *mc_hostname
;
238 static int server_fd
;
239 static int server_fd_unix
; // for efficient local communication
240 static boolean server_fd_disabled
;
241 static void disable_server_fd();
242 static void enable_server_fd();
244 static pthread_mutex_t mutex
;
246 static void unlock();
249 static const int EPOLL_SIZE_HINT
= 1000;
250 static const int EPOLL_MAX_EVENTS
= 250;
251 static epoll_event
*epoll_events
;
254 static unsigned int nfds
, new_nfds
;
255 static struct pollfd
*ufds
, *new_ufds
;
256 static boolean pollfds_modified
;
257 static void update_pollfds();
259 static void add_poll_fd(int fd
);
260 static void remove_poll_fd(int fd
);
262 static int fd_table_size
;
263 static fd_table_struct
*fd_table
;
264 static void add_fd_to_table(int fd
);
265 static void remove_fd_from_table(int fd
);
267 static void set_close_on_exec(int fd
);
269 static unknown_connection
*unknown_head
, *unknown_tail
;
270 static unknown_connection
*new_unknown_connection(bool unix_socket
);
271 static void delete_unknown_connection(unknown_connection
*conn
);
272 static void close_unknown_connection(unknown_connection
*conn
);
274 static void init_string_set(string_set
*set
);
275 static void free_string_set(string_set
*set
);
276 static void add_string_to_set(string_set
*set
, const char *str
);
277 static void remove_string_from_set(string_set
*set
, const char *str
);
278 static boolean
set_has_string(const string_set
*set
, const char *str
);
279 static const char *get_string_from_set(const string_set
*set
, int index
);
281 static int n_host_groups
;
282 static host_group_struct
*host_groups
;
283 static string_set assigned_components
;
284 static boolean all_components_assigned
;
285 static host_group_struct
*add_host_group(const char *group_name
);
286 static host_group_struct
*lookup_host_group(const char *group_name
);
287 static boolean
is_similar_hostname(const char *host1
, const char *host2
);
288 static boolean
host_has_name(const host_struct
*host
, const char *name
);
289 static boolean
member_of_group(const host_struct
*host
,
290 const host_group_struct
*group
);
291 static void add_allowed_components(host_struct
*host
);
292 static host_struct
*choose_ptc_location(const char *component_type
,
293 const char *component_name
, const char *component_location
);
296 static host_struct
**hosts
;
297 static char *config_str
;
298 static host_struct
*add_new_host(unknown_connection
*conn
);
299 static void close_hc_connection(host_struct
*hc
);
300 static boolean
is_hc_in_state(hc_state_enum checked_state
);
301 static boolean
all_hc_in_state(hc_state_enum checked_state
);
302 static void configure_host(host_struct
*host
, boolean should_notify
);
303 static void check_all_hc_configured();
304 static void add_component_to_host(host_struct
*host
,
305 component_struct
*comp
);
306 static void remove_component_from_host(component_struct
*comp
);
308 static boolean version_known
;
309 static int n_modules
;
310 static module_version_info
*modules
;
311 static boolean
check_version(unknown_connection
*conn
);
313 static int n_components
, n_active_ptcs
, max_ptcs
;
314 static component_struct
**components
;
315 static component_struct
*mtc
, *system
;
316 static component next_comp_ref
, tc_first_comp_ref
;
317 static boolean any_component_done_requested
, any_component_done_sent
,
318 all_component_done_requested
, any_component_killed_requested
,
319 all_component_killed_requested
;
320 static void add_component(component_struct
*comp
);
321 static component_struct
*lookup_component(component comp_ref
);
322 static void destroy_all_components();
323 static void close_tc_connection(component_struct
*comp
);
324 static boolean stop_after_tc
, stop_requested
;
325 static boolean
ready_to_finish_testcase();
326 static void finish_testcase();
327 static boolean
message_expected(component_struct
*from
,
328 const char *message_name
);
329 static boolean
request_allowed(component_struct
*from
,
330 const char *message_name
);
331 static boolean
valid_endpoint(component component_reference
,
332 boolean new_connection
, component_struct
*requestor
,
333 const char *operation
);
334 static void destroy_connection(port_connection
*conn
, component_struct
*tc
);
335 static void destroy_mapping(port_connection
*conn
);
336 static boolean
stop_all_components();
337 static void check_all_component_stop();
338 static void send_stop_ack_to_requestors(component_struct
*tc
);
339 static boolean
kill_all_components(boolean testcase_ends
);
340 static void check_all_component_kill();
341 static void send_kill_ack_to_requestors(component_struct
*tc
);
342 static void send_component_status_to_requestor(component_struct
*tc
,
343 component_struct
*requestor
, boolean done_status
,
344 boolean killed_status
);
345 static void component_stopped(component_struct
*tc
);
346 static void component_terminated(component_struct
*tc
);
347 static void done_cancelled(component_struct
*from
,
348 component_struct
*started_tc
);
349 static void start_kill_timer(component_struct
*tc
);
351 static boolean
component_is_alive(component_struct
*tc
);
352 static boolean
component_is_running(component_struct
*tc
);
353 static boolean
component_is_done(component_struct
*tc
);
354 static boolean
is_any_component_alive();
355 static boolean
is_all_component_alive();
356 static boolean
is_any_component_running();
357 static boolean
is_all_component_running();
358 static boolean
is_any_component_done();
360 static void init_connections(component_struct
*tc
);
361 static void add_connection(port_connection
*c
);
362 static void remove_connection(port_connection
*c
);
363 static port_connection
*find_connection(component head_comp
,
364 const char *head_port
, component tail_comp
, const char *tail_port
);
365 static void remove_all_connections(component head_or_tail
);
366 static transport_type_enum
choose_port_connection_transport(
367 component head_comp
, component tail_comp
);
368 static void send_connect_ack_to_requestors(port_connection
*conn
);
369 static void send_error_to_connect_requestors(port_connection
*conn
,
370 const char *fmt
, ...)
371 __attribute__ ((__format__ (__printf__
, 2, 3)));
372 static void send_disconnect_to_server(port_connection
*conn
);
373 static void send_disconnect_ack_to_requestors(port_connection
*conn
);
375 static void init_requestors(requestor_struct
*reqs
, component_struct
*tc
);
376 static void add_requestor(requestor_struct
*reqs
, component_struct
*tc
);
377 static void remove_requestor(requestor_struct
*reqs
, component_struct
*tc
);
378 static boolean
has_requestor(const requestor_struct
*reqs
,
379 component_struct
*tc
);
380 static component_struct
*get_requestor(const requestor_struct
*reqs
,
382 static void free_requestors(requestor_struct
*reqs
);
384 static void init_qualified_name(qualified_name
*name
);
385 static void free_qualified_name(qualified_name
*name
);
387 static double kill_timer
;
388 static double time_now();
389 static timer_struct
*timer_head
, *timer_tail
;
390 static void register_timer(timer_struct
*timer
);
391 static void cancel_timer(timer_struct
*timer
);
392 static int get_poll_timeout();
393 static void handle_expired_timers();
394 static void handle_kill_timer(timer_struct
*timer
);
396 // Custom signal handling for termination signals to remove temporary
397 // files /tmp/ttcn3-mctr-*. Related to HP67376.
398 static struct sigaction new_action
, old_action
;
399 static void register_termination_handlers();
400 static void termination_handler(int signum
);
403 static void error(const char *fmt
, ...)
404 __attribute__ ((__format__ (__printf__
, 1, 2)));
406 static void notify(const char *fmt
, ...)
407 __attribute__ ((__format__ (__printf__
, 1, 2)));
408 static void notify(const struct timeval
*timestamp
, const char *source
,
409 int severity
, const char *message
);
410 static void status_change();
412 static void fatal_error(const char *fmt
, ...)
413 __attribute__ ((__format__ (__printf__
, 1, 2), __noreturn__
));
415 static void *thread_main(void *arg
);
416 static void dispatch_socket_event(int fd
);
417 static int pipe_fd
[2];
418 static wakeup_reason_t wakeup_reason
;
419 static void wakeup_thread(wakeup_reason_t reason
);
421 static void handle_pipe();
422 static void handle_incoming_connection(int p_serverfd
);
423 static int recv_to_buffer(int fd
, Text_Buf
& text_buf
,
424 boolean recv_from_socket
);
425 static void handle_unknown_data(unknown_connection
*conn
);
426 static void handle_hc_data(host_struct
*hc
, boolean recv_from_socket
);
427 static void handle_tc_data(component_struct
*tc
, boolean recv_from_socket
);
429 static void unlink_unix_socket(int socket_fd
);
431 static void shutdown_server();
432 static void perform_shutdown();
434 static void clean_up();
436 static const char *get_host_name(const struct in_addr
*ip_address
);
437 static boolean
get_ip_address(struct in_addr
*ip_address
,
438 const char *host_name
);
440 /* Messages to HCs */
441 static void send_configure(host_struct
*hc
, const char *config_file
);
442 static void send_exit_hc(host_struct
*hc
);
443 static void send_create_mtc(host_struct
*hc
);
444 static void send_create_ptc(host_struct
*hc
, component component_reference
,
445 const qualified_name
& component_type
, const char *component_name
,
446 boolean is_alive
, const qualified_name
& current_testcase
);
447 static void send_kill_process(host_struct
*hc
,
448 component component_reference
);
450 /* Messages to TCs */
451 static void send_create_ack(component_struct
*tc
,
452 component component_reference
);
453 static void send_start_ack(component_struct
*tc
);
454 static void send_stop(component_struct
*tc
);
455 static void send_stop_ack(component_struct
*tc
);
456 static void send_kill_ack(component_struct
*tc
);
457 static void send_running(component_struct
*tc
, boolean answer
);
458 static void send_alive(component_struct
*tc
, boolean answer
);
459 static void send_done_ack(component_struct
*tc
, boolean answer
,
460 const char *return_type
, int return_value_len
,
461 const void *return_value
);
462 static void send_killed_ack(component_struct
*tc
, boolean answer
);
463 static void send_connect_listen(component_struct
*tc
,
464 const char *local_port
, component remote_comp
,
465 const char *remote_comp_name
, const char *remote_port
,
466 transport_type_enum transport_type
);
467 static void send_connect(component_struct
*tc
,
468 const char *local_port
, component remote_comp
,
469 const char *remote_comp_name
, const char *remote_port
,
470 transport_type_enum transport_type
, int remote_address_len
,
471 const void *remote_address
);
472 static void send_connect_ack(component_struct
*tc
);
473 static void send_disconnect(component_struct
*tc
,
474 const char *local_port
, component remote_comp
, const char *remote_port
);
475 static void send_disconnect_ack(component_struct
*tc
);
476 static void send_map(component_struct
*tc
,
477 const char *local_port
, const char *system_port
);
478 static void send_map_ack(component_struct
*tc
);
479 static void send_unmap(component_struct
*tc
,
480 const char *local_port
, const char *system_port
);
481 static void send_unmap_ack(component_struct
*tc
);
483 /* Messages to MTC */
484 static void send_cancel_done_mtc(component component_reference
,
486 static void send_component_status_mtc(component component_reference
,
487 boolean is_done
, boolean is_killed
, boolean is_any_done
,
488 boolean is_all_done
, boolean is_any_killed
, boolean is_all_killed
,
489 const char *return_type
, int return_value_len
,
490 const void *return_value
);
491 static void send_execute_control(const char *module_name
);
492 static void send_execute_testcase(const char *module_name
,
493 const char *testcase_name
);
494 static void send_ptc_verdict(boolean continue_execution
);
495 static void send_continue();
496 static void send_exit_mtc();
498 /** Messages to PTCs */
499 static void send_cancel_done_ptc(component_struct
*tc
,
500 component component_reference
);
501 static void send_component_status_ptc(component_struct
*tc
,
502 component component_reference
,
503 boolean is_done
, boolean is_killed
, const char *return_type
,
504 int return_value_len
, const void *return_value
);
505 static void send_start(component_struct
*tc
,
506 const qualified_name
& function_name
, int arg_len
, const void *arg_ptr
);
507 static void send_kill(component_struct
*tc
);
509 static void send_error(int fd
, const char *fmt
, ...)
510 __attribute__ ((__format__ (__printf__
, 2, 3)));
511 static void send_error_str(int fd
, const char *reason
);
512 static void send_message(int fd
, Text_Buf
& text_buf
);
514 /* Incoming messages on unknown connections (generic and first messages) */
515 static void process_error(unknown_connection
*conn
);
516 static void process_log(unknown_connection
*conn
);
517 static void process_version(unknown_connection
*conn
);
518 static void process_mtc_created(unknown_connection
*conn
);
519 static void process_ptc_created(unknown_connection
*conn
);
521 /* Incoming messages from HCs */
522 static void process_error(host_struct
*hc
);
523 static void process_log(host_struct
*hc
);
524 static void process_configure_ack(host_struct
*hc
);
525 static void process_configure_nak(host_struct
*hc
);
526 static void process_create_nak(host_struct
*hc
);
527 static void process_hc_ready(host_struct
*hc
);
529 /* Incoming messages from TCs */
530 static void process_error(component_struct
*tc
);
531 static void process_log(component_struct
*tc
);
532 static void process_create_req(component_struct
*tc
);
533 static void process_start_req(component_struct
*tc
, int message_end
);
534 static void process_stop_req(component_struct
*tc
);
535 static void process_kill_req(component_struct
*tc
);
536 static void process_is_running(component_struct
*tc
);
537 static void process_is_alive(component_struct
*tc
);
538 static void process_done_req(component_struct
*tc
);
539 static void process_killed_req(component_struct
*tc
);
540 static void process_cancel_done_ack(component_struct
*tc
);
541 static void process_connect_req(component_struct
*tc
);
542 static void process_connect_listen_ack(component_struct
*tc
, int message_end
);
543 static void process_connected(component_struct
*tc
);
544 static void process_connect_error(component_struct
*tc
);
545 static void process_disconnect_req(component_struct
*tc
);
546 static void process_disconnected(component_struct
*tc
);
547 static void process_map_req(component_struct
*tc
);
548 static void process_mapped(component_struct
*tc
);
549 static void process_unmap_req(component_struct
*tc
);
550 static void process_unmapped(component_struct
*tc
);
552 /* Incoming messages from MTC */
553 static void process_testcase_started();
554 static void process_testcase_finished();
555 static void process_mtc_ready();
557 /* Incoming messages from PTCs */
558 static void process_stopped(component_struct
*tc
, int message_end
);
559 static void process_stopped_killed(component_struct
*tc
, int message_end
);
560 static void process_killed(component_struct
*tc
);
563 static void initialize(UserInterface
& par_ui
, int par_max_ptcs
);
564 static void terminate();
566 static void add_host(const char *group_name
, const char *host_name
);
567 static void assign_component(const char *host_or_group
,
568 const char *component_id
);
569 static void destroy_host_groups();
571 static void set_kill_timer(double timer_val
);
573 static unsigned short start_session(const char *local_address
,
574 unsigned short tcp_port
, bool unix_sockets_enabled
);
575 static void shutdown_session();
577 static void configure(const char *config_file
);
579 static void create_mtc(int host_index
);
580 static void exit_mtc();
582 static void execute_control(const char *module_name
);
583 static void execute_testcase(const char *module_name
,
584 const char *testcase_name
);
585 static void stop_after_testcase(boolean new_state
);
586 static void continue_testcase();
587 static void stop_execution();
589 static mc_state_enum
get_state();
590 static boolean
get_stop_after_testcase();
592 static int get_nof_hosts();
593 static host_struct
*get_host_data(int host_index
);
594 static component_struct
*get_component_data(int component_reference
);
595 static void release_data();
597 static const char *get_mc_state_name(mc_state_enum state
);
598 static const char *get_hc_state_name(hc_state_enum state
);
599 static const char *get_tc_state_name(tc_state_enum state
);
600 static const char *get_transport_name(transport_type_enum transport
);
603 //----------------------------------------------------------------------------
605 } /* namespace mctr */
607 //----------------------------------------------------------------------------
608 #endif // MCTR_MAINCONTROLLER_H
612 // indent-tabs-mode: nil