-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsynexec_master_comm.c
633 lines (568 loc) · 16.3 KB
/
synexec_master_comm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* ------------------------------------
* synexec - Synchronised Executioner
* ------------------------------------
* synexec_master_comm.c
* -----------------------
* Copyright 2014 (c) Citrix
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Read the README file for the changelog and information on how to
* compile and use this program.
*/
// Header files
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <unistd.h>
#include "synexec_netops.h"
#include "synexec_comm.h"
#include "synexec_common.h"
#include "synexec_master_slaveset.h"
#include "synexec_master_comm.h"
// Global variables
extern struct in_addr net_ifip;
extern struct in_addr net_ifbc;
extern char * net_ifname;
extern uint16_t net_port;
extern uint32_t session;
extern int verbose;
/*
* static int
* comm_tcp_accept(int sock, struct timeval *timeout, slaveset_t *slaveset);
* -------------------------------------------------------------------------
* This function accepts TCP connections from slaves and inserts them into
* 'slaveset'.
*
* Mandatory params: sock, slaveset
* Optional params : timeout
*
* NOTES:
* If 'timeout' is not specified, the function may wait forever.
*
* Return values:
* -1: Error
* 0: Timeout while waiting
* 1: One slave added
*/
static int
comm_tcp_accept(int sock, struct timeval *timeout, slaveset_t *slaveset){
// Local variables
fd_set fds; // Select fd_set
int slave_sock = -1; // Socket to accept new slaves
struct sockaddr_in slave_addr; // Slave's address
socklen_t slave_len; // Address length
synexec_msg_t net_msg; // synexec msg
int err = 0; // Return code
// Wait until there is something to read
FD_ZERO(&fds);
FD_SET(sock, &fds);
err = select(sock+1, &fds, NULL, NULL, timeout);
if (err == -1){
perror("select");
fprintf(stderr, "%s: Error waiting to receive response from slaves.\n", __FUNCTION__);
goto out;
}else
if (err == 0){
goto out;
}
// There is a connection on the pipe
slave_len = sizeof(slave_addr);
if ((slave_sock = accept(sock, (struct sockaddr *)&slave_addr, &slave_len)) < 0){
perror("accept");
fprintf(stderr, "%s: Error accepting new connection.\n", __FUNCTION__);
goto err;
}
if (verbose > 0){
printf("%s: Accepted connection from '%s:%hu'.\n", __FUNCTION__,
inet_ntoa(slave_addr.sin_addr), ntohs(slave_addr.sin_port));
fflush(stdout);
}
// Process hello
if (comm_recv(slave_sock, &net_msg, NULL, NULL, NULL) <= 0){
(void)close(slave_sock);
err = 0;
goto out;
}
if (net_msg.command != MT_SYNEXEC_MSG_REPLY){
(void)close(slave_sock);
err = 0;
goto out;
}
// Add the connection to the slaveset
err = slave_add(slaveset, &slave_addr, slave_sock);
if (err && (verbose > 1)){
printf("%s: Added slave: %s:%hu (%d).\n", __FUNCTION__,
inet_ntoa(slave_addr.sin_addr), ntohs(slave_addr.sin_port), slave_sock);
fflush(stdout);
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* static int
* comm_udp_broadcast(int sock);
* -----------------------------
* Create a UDP broadcast probe message and send it over 'sock'.
*
* Mandatory params: sock
* Optional params :
*
* Return values:
* -1: Error
* 0: Success
*/
static int
comm_udp_broadcast(int sock){
// Local variables
fd_set fds; // Select fd_set
struct timeval fds_timeout; // Select timeout;
struct sockaddr_in net_udpaddr; // Sock addr for sending
synexec_msg_t net_msg; // synexec msg
int i; // Temporary integer
int err = 0; // Return code
// Ensure socket is ready to send
FD_ZERO(&fds);
FD_SET(sock, &fds);
fds_timeout.tv_sec = 1;
fds_timeout.tv_usec = 0;
i = select(sock+1, NULL, &fds, NULL, &fds_timeout);
if (i != 1){
if (i != 0){
perror("select");
}
fprintf(stderr, "%s: UDP Broadcast socket not ready to transmit in time.\n", __FUNCTION__);
goto err;
}
// Setup sending address
net_udpaddr.sin_family = AF_INET;
memcpy(&net_udpaddr.sin_addr, &net_ifbc, sizeof(net_udpaddr.sin_addr));
net_udpaddr.sin_port = htons(net_port);
// Setup synexec msg
memset(&net_msg, 0, sizeof(net_msg));
net_msg.version = MT_SYNEXEC_VERSION;
net_msg.session = session;
net_msg.command = MT_SYNEXEC_MSG_PROBE;
net_msg.datalen = 0;
net_msg_hton(&net_msg);
// Send broadcast
if (sendto(sock, &net_msg, sizeof(net_msg), 0, (struct sockaddr *)&net_udpaddr, sizeof(net_udpaddr)) < 0){
perror("sendto");
fprintf(stderr, "%s: Error sending UDP broadcast.\n", __FUNCTION__);
goto err;
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* slave_fd_probe(int sock);
* -------------------------
* Probe the slave at the other end of 'sock'.
*
* Mandatory params: sock
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
slave_fd_probe(int sock){
// Local variables
synexec_msg_t net_msg; // synexec msg
int err = 0; // Return code
// Probe the slave
if (comm_send(sock, MT_SYNEXEC_MSG_PROBE, NULL, NULL, 0) <= 0){
goto err;
}
// Process the reply
if (comm_recv(sock, &net_msg, NULL, NULL, NULL) <= 0){
goto err;
}
if (net_msg.command != MT_SYNEXEC_MSG_REPLY){
goto err;
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* slave_probe(slave_t *slave_aux);
* --------------------------------
* Probe the TCP fd for 'slave_aux'.
*
* Mandatory params: slave_aux
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
slave_probe(slave_t *slave_aux){
// Local variables
int err = 0; // Return code
if (verbose > 0){
printf("%s: Probing slave (%s:%hu).\n", __FUNCTION__,
inet_ntoa(slave_aux->slave_addr.sin_addr), ntohs(slave_aux->slave_addr.sin_port));
}
// Probe the slave
if (slave_fd_probe(slave_aux->slave_fd) < 0){
if (verbose > 0){
printf("%s: Error probing slave (%s:%hu).\n", __FUNCTION__,
inet_ntoa(slave_aux->slave_addr.sin_addr), ntohs(slave_aux->slave_addr.sin_port));
}
goto err;
} else {
if (verbose > 0){
printf("%s: Slave (%s:%hu) replied to probe.\n", __FUNCTION__,
inet_ntoa(slave_aux->slave_addr.sin_addr), ntohs(slave_aux->slave_addr.sin_port));
}
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* wait_slaves(slaveset_t *slaveset);
* ----------------------------------
* This function implements the main loop that sends UDP broadcasts and awaits
* TCP connections potential slaves. It returns when all required slaves have
* joined the session.
*
* Mandatory params: slaveset
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
wait_slaves(slaveset_t *slaveset){
// Local variables
int net_udpfd = -1; // UDP socket
socklen_t net_udplen = 0; // UDP socket len
int net_tcpfd = -1; // TCP socket
struct sockaddr_in net_tcpaddr; // TCP address
struct timeval fds_timeout; // select timeout
int i; // Temporary integer
int err = 0; // Return code
// Setup UDP socket to broadcast
if ((net_udpfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket");
fprintf(stderr, "%s: Error creating UDP socket.\n", __FUNCTION__);
goto err;
}
i = 1; // SO_BROADCAST = true
net_udplen = sizeof(net_udpfd);
if (setsockopt(net_udpfd, SOL_SOCKET, SO_BROADCAST, &i, net_udplen) < 0){
perror("setsockopt");
fprintf(stderr, "%s: Error setting socket to broadcast mode.\n", __FUNCTION__);
goto err;
}
if (strcmp(net_ifname, "any")){
if (setsockopt(net_udpfd, SOL_SOCKET, SO_BINDTODEVICE, net_ifname, IFNAMSIZ-1) < 0){
perror("SO_BINDTODEVICE");
fprintf(stderr, "%s: Error binding UDP socket to interface\n", __FUNCTION__);
goto err;
}
}
// Setup TCP socket to accept new connections
if ((net_tcpfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
fprintf(stderr, "%s: Error creating TCP socket.\n", __FUNCTION__);
goto err;
}
i = 1; // SO_REUSEADDR = true
if (setsockopt(net_tcpfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&i, sizeof(int)) < 0){
perror("setsockopt");
fprintf(stderr, "%s: Error setting SO_REUSEADDR to TCP socket.\n", __FUNCTION__);
goto err;
}
memset(&net_tcpaddr, 0, sizeof(net_tcpaddr));
net_tcpaddr.sin_family = AF_INET;
memcpy(&net_tcpaddr.sin_addr, &net_ifip, sizeof(net_tcpaddr.sin_addr));
net_tcpaddr.sin_port = htons(net_port);
if (bind(net_tcpfd, (struct sockaddr *)&net_tcpaddr, sizeof(net_tcpaddr)) < 0){
perror("bind");
fprintf(stderr, "%s: Error binding TCP socket.\n", __FUNCTION__);
goto err;
}
listen(net_tcpfd, 128);
// Send UDP broadcast query every second until I have my slaves up
do {
// Send the probe broadcast
if (verbose > 0){
printf("%s: Sending UDP Probe broadcast...\n", __FUNCTION__);
fflush(stdout);
}
comm_udp_broadcast(net_udpfd);
// Set time to wait for replies
fds_timeout.tv_sec = SYNEXEC_MASTER_COMM_PROBE_WAIT;
fds_timeout.tv_usec = 0;
do {
i = comm_tcp_accept(net_tcpfd, &fds_timeout, slaveset);
slaveset->active += i;
} while ((i > 0) && (slaveset->active < slaveset->slaves));
if (verbose > 1){
printf("%s: Done waiting for UDP replies.\n", __FUNCTION__);
fflush(stdout);
}
if (slaveset_probe(slaveset) < 0){
goto err;
}
}
while (slaveset->slaves != slaveset->active);
out:
// Free resources
if (net_udpfd != -1){
close(net_udpfd);
}
if (net_tcpfd != -1){
close(net_tcpfd);
}
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* static int
* config_slave(slave_t *slave, char *conf_ptr, off_t conf_len);
* -------------------------------------------------------------
* This function sends the session configuration file to 'slave' and confirms
* that he is happy with the contents.
*
* Mandatory params: slave, conf_ptr
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
static int
config_slave(slave_t *slave, char *conf_ptr, off_t conf_len){
// Local variables
synexec_msg_t net_msg; // Synexec msg
int err = 0; // Return code
// Send configuration to slave
if (comm_send(slave->slave_fd, MT_SYNEXEC_MSG_CONF, NULL, conf_ptr, conf_len) < 0){
goto err;
}
if (verbose > 0){
printf("%s: Configuration sent to slave (%s:%hu).\n", __FUNCTION__, inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
fflush(stdout);
}
// Await reply
if (comm_recv(slave->slave_fd, &net_msg, NULL, NULL, NULL) < 0){
goto err;
}
if (net_msg.command != MT_SYNEXEC_MSG_CONF_OK){
fprintf(stderr, "%s: Slave (%s:%hu) refused configuration file.\n", __FUNCTION__, inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
goto err;
}
if (verbose > 0){
printf("%s: Configuration OK from slave (%s:%hu).\n", __FUNCTION__, inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
fflush(stdout);
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* config_slaves(slaveset_t *slaveset, char *conf_ptr, off_t conf_len);
* --------------------------------------------------------------------
* This function sends the session configuration file to all the slaves and
* confirms that they are happy with the contents.
*
* Mandatory params: slaveset, conf_ptr
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
config_slaves(slaveset_t *slaveset, char *conf_ptr, off_t conf_len){
// Local variables
slave_t *slave; // Temporary slave
int err = 0; // Return code
// Iterate through slaves
slave = slaveset->slave;
while(slave){
if (config_slave(slave, conf_ptr, conf_len) != 0){
goto err;
}
slave = slave->next;
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* execute_slaves(slaveset_t *slaveset);
* -------------------------------------
* This function sends the execution command to all the slaves, without
* waiting for acknowledgement, causing them to start the execution
* immediately.
*
* Mandatory params: slaveset
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
execute_slaves(slaveset_t *slaveset){
// Local variables
slave_t *slave = NULL; // Temporary slave
int err = 0;
// Iterate through slaves
slave = slaveset->slave;
while(slave){
if (comm_send(slave->slave_fd, MT_SYNEXEC_MSG_EXEC, NULL, NULL, 0) <= 0){
goto err;
}
slave = slave->next;
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* join_slaves(slaveset_t *slaveset);
* ----------------------------------
* This function keep track of the slaves that are executing, waiting for
* them to finish their work and return the time values.
*
* Mandatory params: slaveset
* Optional params :
*
* Return values:
* -1 Error
* 0 Success
*/
int
join_slaves(slaveset_t *slaveset){
// Local variables
fd_set fds; // Select fd_set
int mfd; // Largest fd
synexec_msg_t net_msg; // Synexec msg
char *data; // Transfer buffer
int i; // Temporary integer
slave_t *slave = NULL; // Temporary slave
int err = 0; // Return code
// Loop until all slaves have finished
do {
// Set all slaves into the fd_set
FD_ZERO(&fds);
mfd = -1;
slave = slaveset->slave;
while(slave){
if (!memcmp(&(slave->slave_time[2]), &(slave->slave_time[1]), sizeof(slave->slave_time[2]))){
FD_SET(slave->slave_fd, &fds);
if (slave->slave_fd > mfd){
mfd = slave->slave_fd;
}
}
slave = slave->next;
}
if (mfd == -1){
// All slaves in set have completed
goto out;
}
// Check which slaves have something to say
// TODO: This should timeout and then I need to probe the slaves
i = select(mfd+1, &fds, NULL, NULL, NULL);
if (i <= 0){
perror("select");
goto err;
}
slave = slaveset->slave;
while(slave){
if (FD_ISSET(slave->slave_fd, &fds)){
// Read from this slave
data = NULL;
i = comm_recv(slave->slave_fd, &net_msg, NULL, (void**)&data, NULL);
if (i < 0)
goto err;
if ((i > 0) && (net_msg.command == MT_SYNEXEC_MSG_FINISHD)){
struct {
int64_t tv_sec;
int64_t tv_usec;
} net_time[3];
if (net_msg.datalen != sizeof(net_time)){
fprintf(stderr, "%s: Wrong datalen for FINISHD (slave %s:%hu).\n", __FUNCTION__,
inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
fflush(stderr);
continue;
}
// Unmarshal data
memcpy(net_time, data, sizeof(net_time));
slave->slave_time[0].tv_sec = net_time[0].tv_sec; slave->slave_time[0].tv_usec = net_time[0].tv_usec;
slave->slave_time[1].tv_sec = net_time[1].tv_sec; slave->slave_time[1].tv_usec = net_time[1].tv_usec;
slave->slave_time[2].tv_sec = net_time[2].tv_sec; slave->slave_time[2].tv_usec = net_time[2].tv_usec;
printf("%s: Slave (%s:%hu) completed\n", __FUNCTION__,
inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port));
fflush(stdout);
}
}
slave = slave->next;
}
} while(1);
out:
// Return
return(err);
err:
err = -1;
goto out;
}