PadicoTM

Documentation

« back to PM2 home.
g5k-topo.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
30#include <Padico/Puk.h>
31#include <Padico/Module.h>
32#include <Padico/PadicoTM.h>
33#include <Padico/Topology.h>
34
35#include <string.h>
36#include <ctype.h>
37#include <net/if_arp.h>
38#include <sys/socket.h>
39#include <netinet/in.h>
40#include <arpa/inet.h>
41#include <sys/types.h>
42#include <signal.h>
43#include <unistd.h>
44#include <jansson.h>
45#include <curl/curl.h>
46
47#define SSHPORT (3443 + getpid())
48#define APIADDR "https://localhost"
49#define APIVERSION "2.0"
50#define REQUEST_RETRY_MAX 10
51#define URL_SIZE 512
52#define SSH_CONNECT_TIMEOUT 1500
53
54static int g5k_module_init(void);
55static void g5k_module_finalize(void);
56
58
59PADICO_MODULE_ATTR(access, "G5K_ACCESS", "access point for g5k", string, "");
60
61static int ssh_pid = -1;
62
64{
65 char *data;
66 size_t pos;
67};
68
73static int g5k_get_netmask(const char*site, struct in_addr*netmask_addr)
74{
75 int cidr = -1;
76 if(strcmp(site, "bordeaux") == 0) //172.16.0.0
77 cidr = 20;
78 else if(strcmp(site, "grenoble") == 0) //172.16.16.0 || 172.16.18.0
79 cidr = 24;
80 else if(strcmp(site, "lille") == 0) //192.168.159.0
81 cidr = 20;
82 else if(strcmp(site, "luxembourg") == 0) //172.16.176.0
83 cidr = 20;
84 else if(strcmp(site, "lyon") == 0) //10.69.0.0
85 cidr = 21;
86 else if(strcmp(site, "nancy") == 0) //172.16.64.0
87 cidr = 20;
88 else if(strcmp(site, "orsay") == 0) //172.16.80.0
89 cidr = 20;
90 else if(strcmp(site, "reims") == 0) //172.16.160.0
91 cidr = 20;
92 else if(strcmp(site, "rennes") == 0) //172.16.96.0
93 cidr = 20;
94 else if(strcmp(site, "sophia") == 0) //138.96.20.0
95 cidr = 22;
96 else if(strcmp(site, "toulouse") == 0) //172.16.112.0
97 cidr = 20;
98 assert(cidr > 0 && cidr < 32);
99 uint32_t netmask = (0xffffffff << (32 - cidr));
101 netmask_addr->s_addr = netmask;
102 return cidr;
103}
104
107static size_t write_response(void *contents, size_t size, size_t nmemb, void *userp)
108{
109 size_t realsize = size * nmemb;
110 struct write_result *mem = (struct write_result *)userp;
111 mem->data = padico_realloc(mem->data, mem->pos + realsize + 1);
112 if (mem->data == NULL)
113 {
114 padico_print("not enough memory (realloc returned NULL)\n");
116 }
117 memcpy(mem->data + mem->pos, contents, realsize);
118 mem->pos += realsize;
119 return realsize;
120}
121
122static char *request(const char *url)
123{
124 padico_print("request(%s)\n", url);
125 CURL *curl;
126 CURLcode status;
127 long code;
128 int retry_cpt = 0;
130 if(!curl)
131 return NULL;
132 struct write_result write_result = {
133 .data = padico_malloc(1),
134 .pos = 0,
135 };
136 struct curl_slist *requestHeader = NULL;
137 requestHeader = curl_slist_append(requestHeader, "Accept: application/json");
143
144 retry:
145 if(retry_cpt > 0)
146 {
147 write_result.pos = 0;
148 padico_tm_msleep(200);
149 }
150 status = curl_easy_perform(curl);
151 if(status != 0)
152 {
153 fprintf(stderr, "curl error: %s\n", curl_easy_strerror(status));
155 {
156 retry_cpt++;
157 goto retry;
158 }
160 return NULL;
161 }
163 if(code != 200)
164 {
165 padico_print("server responded with code %ld\n", code);
167 {
168 retry_cpt++;
169 goto retry;
170 }
172 return NULL;
173 }
178 return write_result.data;
179}
180
181static json_t* get_href_by_title(const json_t *array, const char* title_str)
182{
183 json_t *cell, *title, *href;
184 if (!json_is_array(array))
185 return NULL;
186 int i = 0;
187 for(i = 0; i < json_array_size(array); i++)
188 {
191 {
192 title = json_object_get(cell, "title");
194 {
196 {
197 href = json_object_get(cell, "href");
199 {
201 return href;
202 }
203 }
204 }
205 }
206 }
207 return NULL;
208}
209
210static char *mac_address(const char *mac)
211{
212 int mac_size = strlen(mac);
213 char *tmp = padico_malloc(mac_size);
214 int i;
215 int j = 0;
216 for(i = 0; i < mac_size; i++)
217 {
218 if(mac[i] != ':' && mac[i] != '-' )
219 {
220 tmp[j] = toupper(mac[i]);
221 j++;
222 }
223 }
224 tmp[j] = 0;
225 return tmp;
226}
227
239
240static void g5k_compute_networks(const json_t *networks, const padico_topo_host_t host, const char* site)
241{
242 if(json_is_array(networks))
243 {
244 int j = 0;
245 for(j = 0; j < json_array_size(networks); j++)
246 {
247 json_t *network_cell = json_array_get(networks, j);
249 if(json_is_string(ip))
250 {
251 struct in_addr host_addr;
253 {
256 json_t *network_address = json_object_get(network_cell, "network_address");
257 const char *network_address_str = NULL;
258 padico_topo_hwaddr_t hwaddr = NULL;
260 {
263 }
265 {
267 size_t addr_len = strlen(mac_str);
268 char *addr = puk_hex_decode(mac_str, &addr_len, NULL);
270 addr_len, addr);
272 padico_free(addr);
274 }
275 struct in_addr netmask_addr;
276 struct in_addr network_addr;
278 if(cidr > 0)
279 {
280 network_addr.s_addr = netmask_addr.s_addr & host_addr.s_addr;
286 if(hwaddr)
291 {
296 }
297 }
298 }
299 }
300 }
301 }
302}
303
304static void g5k_compute_nodes(const json_t *nodes, const char* site)
305{
307 {
308 int i = 0;
309 for(i = 0; i < json_array_size(nodes); i++)
310 {
312 json_t *uid = json_object_get(cell, "uid");
313 if(uid && json_is_string(uid))
314 {
316 json_t *networks = json_object_get(cell, "network_components");
317 g5k_compute_networks(networks, host, site);
318 }
319 }
320 }
321}
322
324{
325 int i = 0;
327 char *sites_str;
328 json_t *root;
329 json_error_t error;
330 snprintf(request_url, URL_SIZE, "%s:%d/%s/grid5000/sites", APIADDR, SSHPORT, APIVERSION);
332 if(sites_str)
333 {
334 root = json_loads(sites_str, 0, &error);
335 json_t *items = json_object_get(root, "items");
336 for(i=0; i<json_array_size(items); i++)
337 {
339 json_t *uid = json_object_get(site, "uid");
340 json_t *links = json_object_get(site, "links");
345 if(clusters_str)
346 {
350 int j = 0;
351 for(j = 0; j<json_array_size(clusters_items); j++)
352 {
358 if(nodes_str)
359 {
361 json_t *nodes_root = json_loads(nodes_str, 0, &error);
366 }
367 }
369 }
370 }
373 }
375}
376
378{
379 int sock = socket(AF_INET, SOCK_STREAM, 0);
380 if (sock == -1) {
381 return 0;
382 }
383
384 struct sockaddr_in sin;
385 sin.sin_family = AF_INET;
386 sin.sin_port = htons(SSHPORT);
387 sin.sin_addr.s_addr = (uint32_t)htonl(0x7F000001);
388 struct timeval nowtime;
389 struct timeval starttime;
392
393 while (((nowtime.tv_sec * 1000) + (nowtime.tv_usec / 1000)) -
394 ((starttime.tv_sec * 1000) + (starttime.tv_usec / 1000)) < SSH_CONNECT_TIMEOUT)
395 {
396 if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) == 0)
397 {
398 close(sock);
399 return 1;
400 }
402 padico_tm_msleep(200);
403 }
404 close(sock);
405 return 0;
406}
407
408static int g5k_connect()
409{
410 int access_index = 0;
412 const char *g5k_default_access[] =
413 {
414 "access.grid5000.fr",
415 "access-south.grid5000.fr",
416 "access-north.grid5000.fr"
417 };
418 const int g5k_default_access_size = sizeof(g5k_default_access) / sizeof(char *);
420 snprintf(g5k_api_ssh_tun, URL_SIZE, "%d:api.grid5000.fr:443", SSHPORT);
421
422 const char *argv[] =
423 {
424 "/usr/bin/ssh",
425 "-o",
426 "StrictHostKeyChecking=no",
427 "-NL",
430 NULL
431 };
433 {
435 ssh_pid = fork();
437 if(ssh_pid == -1)
438 {
439 padico_warning("fork() failed!\n");
440 }
441 else if(ssh_pid == 0)
442 {
443 int rc = -1, err = -1;
444 putenv("LD_LIBRARY_PATH=");
445 puk_abi_real_errno() = 0;
446 rc = PUK_ABI_PROC_REAL(execv)("/usr/bin/ssh", (char**)argv);
449 padico_warning("exec() failed for ssh- rc=%d; errno=%d (%s).\n", rc, err, strerror(err));
450 return 0;
451 }
452 else
453 {
455 {
456 return 1;
457 }
458 else
459 {
461 ssh_pid = -1;
462 padico_print("%s is unreachable with ssh\n",argv[5]);
463 if(argv[5] != g5k_user_access)
464 access_index++;
466 }
467 }
468 }
469 return 0;
470}
471
472
473static int g5k_module_init(void)
474{
475 if (g5k_connect())
476 {
478 if(ssh_pid > 0)
479 {
483 }
484 }
485 return 0;
486}
487
488static void g5k_module_finalize(void)
489{
490}
assert(slab_size > 0)
puk_allocator_cell_clear & cell(struct ns_rulebucket_hash_entry_s)
return NULL
Definition Shm.h:88
goto retry
Definition Shm.h:88
padico_topo_host_t host
void *static bench_vect_buffer_queue_t v
Definition bench_vect.c:22
static int done
Definition console-mod.c:49
padico_topo_node_vect_t nodes
static int size
Definition datacheck.c:34
static void g5k_compute_nodes(const json_t *nodes, const char *site)
Definition g5k-topo.c:304
static padico_topo_network_t g5k_create_network(const struct in_addr *network_addr, int cidr)
Definition g5k-topo.c:228
static char * mac_address(const char *mac)
Definition g5k-topo.c:210
#define SSHPORT
Definition g5k-topo.c:47
static int g5k_connect_wait_ssh()
Definition g5k-topo.c:377
static int g5k_module_init(void)
Definition g5k-topo.c:473
static void g5k_describe_topology()
Definition g5k-topo.c:323
static char * request(const char *url)
Definition g5k-topo.c:122
static void g5k_module_finalize(void)
Definition g5k-topo.c:488
static size_t write_response(void *contents, size_t size, size_t nmemb, void *userp)
function used by curl to write the server response in a buffer
Definition g5k-topo.c:107
static void g5k_compute_networks(const json_t *networks, const padico_topo_host_t host, const char *site)
Definition g5k-topo.c:240
#define REQUEST_RETRY_MAX
Definition g5k-topo.c:50
#define URL_SIZE
Definition g5k-topo.c:51
static int g5k_connect()
Definition g5k-topo.c:408
#define APIVERSION
Definition g5k-topo.c:49
static int g5k_get_netmask(const char *site, struct in_addr *netmask_addr)
cidr netmask is not available in Grid5000 API if Grid5000's admin change network configuration,...
Definition g5k-topo.c:73
#define SSH_CONNECT_TIMEOUT
Definition g5k-topo.c:52
#define APIADDR
Definition g5k-topo.c:48
static puk_opt_type_string_t padico_module_attr_access_getvalue(void)
Definition g5k-topo.c:59
static json_t * get_href_by_title(const json_t *array, const char *title_str)
Definition g5k-topo.c:181
static int ssh_pid
Definition g5k-topo.c:61
static void puk_spinlock_release(void)
Definition PadicoTM.h:83
#define PUK_ABI_PROC_REAL(SYM)
Definition PadicoTM.h:91
static void puk_spinlock_acquire(void)
Definition PadicoTM.h:81
#define puk_abi_real_errno()
Definition PadicoTM.h:110
padico_rc_t rc
Definition PadicoTM.h:146
void padico_tm_msleep(long msec)
Definition tm-boot.c:384
void padico_topo_host_add_name(padico_topo_host_t host, const char *_name, int resolvable)
Add a name to a host;.
Definition topology.c:1525
padico_topo_addr_visibility_t
Definition Topology.h:100
padico_topo_network_t padico_topo_network_create(const char *id, padico_topo_binding_scope_t scope, size_t addr_size, int family)
Definition topology.c:1853
padico_topo_hwaddr_t padico_topo_hwaddr_create(const char *name, int index, int type, size_t len, const void *addr)
Definition topology.c:126
padico_topo_network_t padico_topo_network_getbyid(const char *network_id)
Definition topology.c:2351
void padico_topo_binding_add_hostname(padico_topo_binding_t binding, const char *name)
Definition topology.c:240
void padico_topo_binding_add_filter(padico_topo_binding_t binding, padico_topo_filter_policy_t policy, uint16_t port, int protocol)
Definition topology.c:262
padico_topo_host_t padico_topo_host_create(const char *hostname)
Create a new 'host' record from a given canonical name.
Definition topology.c:1463
void padico_topo_hwaddr_add(padico_topo_host_t host, padico_topo_hwaddr_t *_hwaddr)
Definition topology.c:153
padico_topo_addr_visibility_t padico_topo_inet_visibility(in_addr_t _inaddr)
Definition topology.c:1197
padico_topo_binding_t padico_topo_host_bind(padico_topo_host_t host, padico_topo_network_t network, const void *address)
Declare a new binding for a host.
Definition topology.c:1609
void padico_topo_binding_add_hwaddr(padico_topo_binding_t binding, padico_topo_hwaddr_t hwaddr)
Definition topology.c:196
#define PORT_ANY
Definition Topology.h:122
@ PADICO_TOPO_ADDR_VISIBILITY_PUBLIC
public address
Definition Topology.h:105
@ PADICO_TOPO_FILTER_BLOCK_OUT
Definition Topology.h:112
@ PADICO_TOPO_FILTER_BLOCK_IN
Definition Topology.h:111
@ PADICO_TOPO_SCOPE_HOST
Definition Topology.h:95
padico_topo_network_t network
Definition psp-mx.c:113
a binding of an entity with a network (protocol level)
a hardware address- identifies a host
a network, containing entities with bindings
size_t pos
Definition g5k-topo.c:66
char * data
Definition g5k-topo.c:65