PadicoTM

Documentation

« back to PM2 home.
remotecontrol.c
Go to the documentation of this file.
1/*
2 * Padico -- a High Performance Parallel and Distributed Computing Environment
3 * Copyright (c) 2002-2025 INRIA and the University of Rennes 1
4 * Alexandre DENIS <Alexandre.Denis@inria.fr>
5 * Christian PEREZ <Christian.Perez@inria.fr>
6 *
7 * The software has been registered at the Agency for the Protection of
8 * Programs (APP) under the number IDDN.FR.001.260013.000.S.P.2002.000.10000.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39
40#include <Padico/Puk.h>
41#include <Padico/PadicoTM.h>
42#include <Padico/Module.h>
43#include <Padico/SysIO.h>
44#include <Padico/PadicoControl.h>
45#include <Padico/SocketFactory.h>
46#include <Padico/NetSelector.h>
47#include <Padico/Connector.h>
48
49
50static int remote_control_init(void);
51static void remote_control_finalize(void);
52
54
55PADICO_MODULE_ATTR(host, "ControlHost", "control host for RemoteControl", string, "");
56PADICO_MODULE_ATTR(port, "ControlPort", "control port for RemoteControl", int, -1);
57PADICO_MODULE_ATTR(connection, "ControlConnection", "connection mode for RemoteControl", string, "none");
58
59
60static void remote_control_event_listener(void*arg);
62
63static const int REMOTE_CONTROL_ISRC = 0;
64static const int REMOTE_CONTROL_NEW_NODE = 1;
65static const int REMOTE_CONTROL_DEL_NODE = 2;
66
73
76
77static struct
78{
79 int route; /* determine if we will handle commands to other known nodes */
80 int fd;
84 struct remotecontrol_node_hashtable_s nodes; /* hash: node -> node */
87
88/* ********************************************************* */
89
90
92{
93 hdr->node_size = htonl(hdr->node_size);
94 hdr->msg_size = htonl(hdr->msg_size);
95 hdr->flag = htonl(hdr->flag);
96}
97
99{
100 hdr->node_size = ntohl(hdr->node_size);
101 hdr->msg_size = ntohl(hdr->msg_size);
102 hdr->flag = ntohl(hdr->flag);
103}
104
106{
107 int size = hdr->node_size + hdr->msg_size;
109
114}
115
117{
118 char*node_name = key;
119 assert(node_name != NULL);
121 int name_len = strlen(node_name);
122 int text_len = strlen(rc_text);
123
124 char*msg = padico_malloc(name_len + text_len + 17);
125 msg[0] = 0;
126 strcat(msg, node_name);
128 {
129 strcat(msg, "<error>");
130 strcat(msg, rc_text);
131 strcat(msg, "</error>\n");
132 text_len += 16;
133 }
134 else
135 {
136 strcat(msg, "<ok>");
137 strcat(msg, rc_text);
138 strcat(msg, "</ok>\n");
139 text_len += 10;
140 }
141 struct remote_control_header_s hdr = {
143 .msg_size = text_len,
144 .flag = REMOTE_CONTROL_ISRC
145 };
146
147 padico_out(20, "return to node=%s msg=%s.\n", node_name, rc_text);
149
151 padico_free(node_name);
152 padico_free(msg);
153}
154
156{
158 char*msg;
159};
160
162{
163 struct remote_control_command_s*cmd = arg;
164 char*node_name = cmd->node_name;
166 {
168 remote_control_node_callback(pe.rc, node_name);
169 }
170 else if(node_name != NULL && padico_topo_getnodebyname(node_name) != NULL)
171 {
174 }
175 else
176 {
177 padico_warning("Received a command for an unknown node (%s).\n", node_name);
179 }
180 padico_free(cmd->msg);
181 padico_free(cmd);
182 return NULL;
183}
184
186{
187 remote_control.fd = -1;
188 padico_fatal("lost connection.\n");
189}
190
192{
193 int again = 1;
194 int sysrc = -1;
195 struct remote_control_header_s hdr;
196
197 /* receive header */
198 assert(remote_control.fd == fd);
199 padico_out(40, "entering...\n");
200 sysrc = padico_sysio_in(fd, &hdr, sizeof(struct remote_control_header_s));
201 padico_out(40, "header received- rc=%d\n", sysrc);
202 if(sysrc <= 0)
203 {
204 again = 0;
206 return again;
207 }
209
210 /* receive body */
211 char*data = NULL;
212 const size_t size = hdr.node_size + hdr.msg_size;
213 padico_out(40, "expecting %d bytes\n", (int)size);
214 if(size > 0)
215 {
218 padico_out(40, "body received- rc=%d\n", sysrc);
219 if(sysrc < size)
220 {
222 again = 0;
223 return again;
224 }
225 }
226 char*msg = padico_malloc(hdr.msg_size + 1);
227 memcpy(msg, data + hdr.node_size, hdr.msg_size);
228 msg[hdr.msg_size] = 0;
229 char*node_name = data;
230 node_name[hdr.node_size] = 0;
231
232 /* deliver the command */
233 padico_out(20, "received commmand- len=%d; to=%s msg=*%s*\n", (int)size, node_name, msg);
235 cmd->node_name = node_name;
236 cmd->msg = msg;
237 padico_tm_bgthread_start(remote_control.pool, &remote_control_node_send, cmd, "remote_control_node_send");
238 return again;
239}
240
241static void remote_control_connector(void*key, int fd)
242{
243 remote_control.fd = fd;
244 /* activate polling */
247 //?unregister desactivate?
248 padico_out(40, "ready.\n");
249
250 /* authorize to send */
252 /* add itself */
254
255 if(remote_control.route)
257}
258
260{
262 if(n == NULL)
263 {
264 padico_out(10, "add node *%s*\n", padico_topo_node_getname(node));
266 struct remote_control_header_s hdr = {
267 .node_size = strlen(node_name),
268 .msg_size = 0,
270 };
271 remote_control_control_send(&hdr, node_name);
273 }
274}
275
277{
279 if(n != NULL)
280 {
281 const char*node_name = padico_topo_node_getname(node);
282 struct remote_control_header_s hdr = {
283 .node_size = strlen(node_name),
284 .msg_size = 0,
286 };
287 remote_control_control_send(&hdr, node_name);
289 padico_out(10, "delete node *%s*\n", padico_topo_node_getname(node));
290 }
291}
292
294{
296 padico_out(20, "event kind: %d\n", event->kind);
297 switch(event->kind)
298 {
300 {
301 const padico_topo_node_vect_t nodes = event->INIT_CONTROLER.nodes;
305 }
306 break;
307
309 {
310 const padico_topo_node_t node = event->NODE.node;
312 }
313 break;
314
316 {
317 const padico_topo_node_t node = event->NODE.node;
319 }
320 break;
321
322 default:
323 break;
324 }
325}
326
331
332static int remote_control_init(void)
333{
334 int rc;
338 remote_control.fd = -1;
339 remote_control.in_io = NULL;
341 remote_control.pool = padico_tm_bgthread_pool_create("RemoteControl");
342 if((strlen(control_host) == 0) ||
343 (control_port == -1))
344 {
345 padico_warning("missing module attributes- expected: ControlHost, ControlPort, ControlConnection\n");
346 return -1;
347 }
348 if(strcmp(control_connection, "all") == 0)
349 {
350 remote_control.route = 0;
351 }
352 else if(strcmp(control_connection, "one") == 0)
353 {
354 if(padico_na_rank() != 0)
355 return 0;
356 remote_control.route = 1;
357 }
358 else
359 {
360 padico_print("no connection required. exit.\n");
361 return 0;
362 }
363
364 marcel_mutex_init(&remote_control.lock_send, 0);
366
367 /* declare XML action */
369 .xml_tag = "getranks",
370 .start_handler = NULL,
371 .end_handler = &remotecontrol_getranks_handler,
372 .required_level = PUK_TRUST_OUTSIDE,
373 .help = "RemoteControl: get session size and ranks"
374 });
375
376 padico_print("connecting to %s:%d\n", control_host, control_port);
377 /* resolve address */
379 struct in_addr inaddr;
381 if(control_host[0] >= '0' && control_host[0] <= '9' )
382 {
383 /* doted IP address */
385 if(!rc)
386 {
387 padico_fatal("cannot parse control host IP address %s.\n", control_host);
388 }
390 }
391 else
392 {
394 if(!peer_host)
395 {
396 padico_fatal("cannot resolve control host name %s\n", control_host);
397 }
399 }
400 if(!peer_host)
401 {
402 padico_fatal("cannot resolve host address %s.\n", inet_ntoa(inaddr));
403 }
404 /* build address */
405 struct sockaddr_in remote_sa =
406 {
407 .sin_family = AF_INET,
408 .sin_port = htons(control_port),
410 };
411 /* connect the socket */
415
416 rc = (*remote_control.sf.driver->create)(remote_control.sf._status, AF_INET, SOCK_STREAM, 0);
417 if(rc == -1)
418 {
419 padico_warning("socket_create: cannot create the socket.\n");
420 return -1;
421 }
422 rc = (*remote_control.sf.driver->connect)(remote_control.sf._status, (struct sockaddr*) &remote_sa, sizeof(remote_sa),
424 if(rc == 0 || rc == -EINPROGRESS)
425 {
426 padico_out(40, "Connection launched.\n");
427 return 0;
428 }
429 else
430 {
432 padico_warning("cannot connect to %s:%d; rc=%d (%s). We tried with the following component: \n%s\n",
435 remote_control.sf.driver = NULL;
436 return -1;
437 }
438}
439
440static void remote_control_finalize(void)
441{
443 if(remote_control.in_io)
445 if(remote_control.sf.driver != NULL)
446 (*(remote_control.sf.driver)->close)(remote_control.sf._status);
448}
assert(slab_size > 0)
struct ns_rulebucket_hash_entry_s * e
const const struct ns_rule_bucket_s * key
ssize_t n
ns_rulebucket_hash_entry_slice_lfstack_init & a
return NULL
Definition Shm.h:88
static puk_iface_t puk_iface_SocketFactory(void)
static void puk_instance_indirect_SocketFactory(puk_instance_t instance, const char *label, struct puk_receptacle_SocketFactory_s *receptacle)
padico_topo_host_t host
static int done
Definition console-mod.c:49
static int size
Definition datacheck.c:34
puk_component_t padico_ns_host_selector(padico_topo_host_t host, const char *profile, puk_iface_t iface, uint16_t port, int protocol)
static void padico_control_send_async(padico_topo_node_t node, const char *cmd, padico_task_notifier_t notify, void *key)
Send an async request on the control channel.
static void padico_control_event_subscribe(padico_event_listener_t l)
void padico_tm_bgthread_start(padico_tm_bgthread_pool_t pool, padico_thread_fun_t f, void *arg, const char *name)
Definition tm-rttask.c:126
void padico_tm_bgthread_pool_wait(padico_tm_bgthread_pool_t pool)
finalize a pool of threads: waits for all thread to complete
Definition tm-rttask.c:78
padico_tm_bgthread_pool_t padico_tm_bgthread_pool_create(const char *name)
create a new thread pool
Definition tm-rttask.c:56
void * arg
Definition PadicoTM.h:167
padico_rc_t rc
Definition PadicoTM.h:146
#define PADICO_IO_EVENT_READ
the fildes is readable
Definition SysIO.h:62
ssize_t padico_sysio_in(int fildes, void *buf, size_t nbyte)
Definition sysio.c:792
void padico_io_activate(padico_sysio_t io)
Activates the I/O handle - when not activated, the callback is not triggered.
Definition sysio.c:278
void padico_io_unregister(padico_sysio_t io)
Unregister the I/O handle.
Definition sysio.c:298
padico_sysio_t padico_io_register(int fd, unsigned int what, padico_sysio_callback_t callback, void *key)
Registers a file descriptor for polling.
Definition sysio.c:205
#define padico_io_t
for backwards compatibility with legacy codes
Definition SysIO.h:53
ssize_t padico_sysio_out(int fildes, const void *buf, size_t nbyte)
Definition sysio.c:808
padico_topo_host_t padico_topo_host_resolve_byinaddr(const struct in_addr *addr)
Get a host description record from an inet address (create if it didn't exist)
Definition topology.c:2452
padico_topo_node_t padico_topo_getlocalnode(void)
Definition topology.c:1891
const char * padico_topo_node_getname(padico_topo_node_t node)
Get the canonical name of a node.
Definition topology.c:1917
#define padico_na_size()
Definition Topology.h:323
const struct in_addr * padico_topo_host_getaddr(padico_topo_host_t host)
Get the canonical IP address of a host.
Definition topology.c:1977
typdef struct padico_topo_node_vect_s * padico_topo_node_vect_t
Definition Topology.h:84
padico_topo_node_t padico_topo_getnodebyname(const char *nodename)
node lookup, by node name
Definition topology.c:2389
#define padico_na_rank()
Definition Topology.h:324
padico_topo_host_t padico_topo_host_resolvebyname(const char *hostname)
resolve a host from name
Definition topology.c:2681
padico_topo_node_t node
Definition na-control.c:65
static const int REMOTE_CONTROL_DEL_NODE
static void remote_control_connector(void *key, int fd)
static puk_opt_type_int_t padico_module_attr_port_getvalue(void)
static const int REMOTE_CONTROL_ISRC
struct remotecontrol_node_hashtable_s nodes
static void remotecontrol_getranks_handler(puk_parse_entity_t e)
static int remote_control_control_callback(int fd, void *key)
padico_tm_bgthread_pool_t pool
static void remote_control_connection_lost()
static void remote_control_control_send(struct remote_control_header_s *hdr, const char *data)
int fd
static void * remote_control_node_send(void *arg)
static puk_opt_type_string_t padico_module_attr_connection_getvalue(void)
static void remote_control_finalize(void)
struct puk_receptacle_SocketFactory_s sf
static puk_opt_type_string_t padico_module_attr_host_getvalue(void)
pthread_mutex_t lock_send
int route
static struct @64 remote_control
static void remote_control_event_listener(void *arg)
static void remote_control_unpack_hdr(struct remote_control_header_s *hdr)
static const int REMOTE_CONTROL_NEW_NODE
static void remote_control_new_node(const padico_topo_node_t node)
static void remote_control_pack_hdr(struct remote_control_header_s *hdr)
static void remote_control_node_callback(padico_rc_t rc, void *key)
static void remote_control_del_node(const padico_topo_node_t node)
static int remote_control_init(void)
padico_sysio_t in_io
puk_iface_t iface_sf
Definition sf-rfw.c:111
@ PADICO_CONTROL_EVENT_INIT_CONTROLER
initial event sent to new controlers to get the past events and current topology state
@ PADICO_CONTROL_EVENT_DELETE_NODE
a node was deleted/disconnected/became unreachable
@ PADICO_CONTROL_EVENT_NEW_NODE
a new node appeared in the known topology
a thread pool, to control and wait running threads
Definition PadicoTM.h:174
marcel_cond_t event
Definition tm-boot.c:65
void * data
place-holder for actual data
static int port
Definition vrp-test.c:15