forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheucanetd.c
1829 lines (1664 loc) · 70.4 KB
/
eucanetd.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2017 Ent. Services Development Corporation LP
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
************************************************************************/
//!
//! @file net/eucanetd.c
//! Implementation of the service management layer
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <pwd.h>
#include <dirent.h>
#include <errno.h>
#include <signal.h>
#include <eucalyptus.h>
#include <misc.h>
#include <euca_string.h>
#include <log.h>
#include <hash.h>
#include <math.h>
#include <http.h>
#include <config.h>
#include <sequence_executor.h>
#include <atomic_file.h>
#include <euca_network.h>
#include "ipt_handler.h"
#include "ips_handler.h"
#include "ebt_handler.h"
#include "dev_handler.h"
#include "euca_gni.h"
#include "euca-to-mido.h"
#include "eucanetd.h"
#include "eucanetd_util.h"
#include "eucalyptus-config.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
//! Global Network Information structure pointer.
//globalNetworkInfo *globalnetworkinfo = NULL;
gni_hostname_info *host_info = NULL;
//! Role of the component running alongside this eucanetd service
eucanetd_peer eucanetdPeer = PEER_INVALID;
//! List of configuration keys that are handled when the application starts
configEntry configKeysRestartEUCANETD[] = {
{"EUCALYPTUS", "/"}
,
{"VNET_BRIDGE", NULL}
,
{"VNET_BROADCAST", NULL}
,
{"VNET_DHCPDAEMON", "/usr/sbin/dhcpd"}
,
{"VNET_DHCPUSER", "root"}
,
{"VNET_SYSTEMCTL", "/usr/bin/systemctl"}
,
{"VNET_DNS", NULL}
,
{"VNET_DOMAINNAME", "eucalyptus.internal"}
,
{"VNET_MODE", NETMODE_EDGE}
,
{"VNET_LOCALIP", NULL}
,
{"VNET_NETMASK", NULL}
,
{"VNET_PRIVINTERFACE", NULL}
,
{"VNET_PUBINTERFACE", NULL}
,
{"VNET_PUBLICIPS", NULL}
,
{"VNET_PRIVATEIPS", NULL}
,
{"VNET_SUBNET", NULL}
,
{"VNET_MACPREFIX", "d0:0d"}
,
{"VNET_ADDRSPERNET", "32"}
,
{"EUCA_USER", "eucalyptus"}
,
{"MIDO_INTRT_CIDR", "169.254.0.0/17"}
,
{"MIDO_INTMD_CIDR", "169.254.128.0/17"}
,
{"MIDO_EXTMD_CIDR", "169.254.169.248/29"}
,
{"MIDO_MD_CIDR", "255.0.0.0/8"}
,
{"MIDO_MD_VETH_USE_NETNS", "N"}
,
{"MIDO_MAX_RTID", "10240"}
,
{"MIDO_MAX_ENIID", "1048576"}
,
{"MIDO_VALIDATE_MIDOCONFIG", "Y"}
,
{NULL, NULL}
,
};
//! List of configuration keys that are periodically monitored for changes
configEntry configKeysNoRestartEUCANETD[] = {
{"POLLING_FREQUENCY", "1"}
,
{"DISABLE_L2_ISOLATION", "N"}
,
{"MIDO_ENABLE_ARPTABLE", "Y"}
,
{"MIDO_ENABLE_MIDOMD", "Y"}
,
{"MIDO_MD_254_EGRESS", "tcp:80"}
,
{"MIDO_MD_253_EGRESS", "udp:53 tcp:53 udp:5353 tcp:5353"}
,
{"MIDO_API_URIBASE", ""}
,
{"NC_PROXY", "N"}
,
{"NC_ROUTER", "Y"}
,
{"NC_ROUTER_IP", ""}
,
{"METADATA_USE_VM_PRIVATE", "N"}
,
{"METADATA_IP", ""}
,
{"LOGLEVEL", "INFO"}
,
{"LOGROLLNUMBER", "10"}
,
{"LOGMAXSIZE", "104857600"}
,
{"LOGPREFIX", ""}
,
{"LOGFACILITY", ""}
,
{NULL, NULL}
,
};
//! String representation of the system role
const char *asPeerRoleName[] = {
"INVALID",
"CLC",
"CC",
"NC",
"NON-EUCA-HOST",
"OUT-OF-BOUND",
};
int sig_rcvd = 0;
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
//! Global EUCANETD configuration structure
static eucanetdConfig *config = NULL;
//! Pointer to the proper Network Driver Interface (NDI)
static driver_handler *pDriverHandler = NULL;
static globalNetworkInfo *pGni = NULL;
static globalNetworkInfo *pGniApplied = NULL;
static globalNetworkInfo *gni_a = NULL;
static globalNetworkInfo *gni_b = NULL;
//! Main loop termination condition
static volatile boolean gIsRunning = FALSE;
//! USR1 and USR2 signals
static volatile boolean gUsr1Caught = FALSE;
static volatile boolean gUsr2Caught = FALSE;
static volatile boolean gHupCaught = FALSE;
static volatile boolean gTermCaught = FALSE;
//! Dummy UDP socket
int eucanetd_dummysock = 0;
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
static void eucanetd_sigterm_handler(int signal);
static void eucanetd_sighup_handler(int signal);
static void eucanetd_sigusr1_handler(int signal);
static void eucanetd_sigusr2_handler(int signal);
static void eucanetd_install_signal_handlers(void);
static int eucanetd_daemonize(void);
static int eucanetd_fetch_latest_local_config(void);
static int eucanetd_initialize(void);
static int eucanetd_initialize_network_drivers(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int eucanetd_cleanup(void);
static int eucanetd_read_config_bootstrap(void);
static int eucanetd_setlog_bootstrap(void);
static int eucanetd_read_config(globalNetworkInfo *pGni);
static int eucanetd_initialize_logs(void);
static int eucanetd_fetch_latest_network(boolean *update_globalnet, boolean *config_changed);
static int eucanetd_fetch_latest_euca_network(boolean *update_globalnet);
static int eucanetd_read_latest_network(globalNetworkInfo *pGni, boolean *update_globalnet);
static int eucanetd_detect_peer(globalNetworkInfo *pGni);
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
#ifndef EUCANETD_UNIT_TEST
/**
* EUCANETD application main entry point
*
* @param argc [in] the number of arguments passed on the command line
* @param argv [in] the list of arguments passed on the command line
*
* @return 0 on success or 1 on failure
*/
int main(int argc, char **argv) {
u32 scrubResult = EUCANETD_RUN_NO_API;
int rc = 0;
int opt = 0;
int firstrun = 1;
int counter = 0;
int epoch_updates = 0;
int epoch_failed_updates = 0;
int epoch_checks = 0;
time_t epoch_timer = 0;
struct timeval tv = { 0 };
struct timeval ttv = { 0 };
boolean config_changed = FALSE;
boolean update_globalnet = FALSE;
boolean update_globalnet_failed = FALSE;
boolean update_version_file = FALSE;
// initialize
eucanetd_initialize();
// parse commandline arguments
config->flushmode = FLUSH_NONE;
while ((opt = getopt(argc, argv, "dhHlgfFmMsuUCZT:v:V:z:")) != -1) {
switch (opt) {
case 'd':
config->debug = EUCANETD_DEBUG_TRACE;
break;
case 'l':
config->flushmode = FLUSH_MIDO_LISTVPC;
config->debug = EUCANETD_DEBUG_INFO;
config->multieucanetd_safe = TRUE;
break;
case 'g':
config->flushmode = FLUSH_MIDO_LISTGATEWAYS;
config->debug = EUCANETD_DEBUG_INFO;
config->multieucanetd_safe = TRUE;
break;
case 'F':
config->flushmode = FLUSH_ALL;
config->debug = EUCANETD_DEBUG_INFO;
break;
case 'f':
config->flushmode = FLUSH_DYNAMIC;
config->debug = EUCANETD_DEBUG_INFO;
break;
case 'C':
config->flushmode = FLUSH_MIDO_DYNAMIC;
config->debug = EUCANETD_DEBUG_INFO;
break;
case 'Z':
config->flushmode = FLUSH_MIDO_ALL;
config->debug = EUCANETD_DEBUG_INFO;
break;
case 'm':
config->flushmode = FLUSH_MIDO_CHECKDUPS;
config->debug = EUCANETD_DEBUG_INFO;
config->multieucanetd_safe = TRUE;
break;
case 'M':
config->flushmode = FLUSH_MIDO_DUPS;
config->debug = EUCANETD_DEBUG_INFO;
break;
case 's':
config->systemd = TRUE;
break;
case 'u':
config->flushmode = FLUSH_MIDO_CHECKUNCONNECTED;
config->debug = EUCANETD_DEBUG_INFO;
config->multieucanetd_safe = TRUE;
break;
case 'U':
config->flushmode = FLUSH_MIDO_UNCONNECTED;
config->debug = EUCANETD_DEBUG_INFO;
break;
case 'v':
config->flushmode = FLUSH_MIDO_CHECKVPC;
config->debug = EUCANETD_DEBUG_INFO;
config->flushmodearg = optarg;
config->multieucanetd_safe = TRUE;
break;
case 'V':
config->flushmode = FLUSH_MIDO_VPC;
config->debug = EUCANETD_DEBUG_INFO;
config->flushmodearg = optarg;
break;
case 'T':
config->flushmode = FLUSH_MIDO_TZONE;
config->debug = EUCANETD_DEBUG_INFO;
config->flushmodearg = optarg;
break;
case 'z':
config->flushmode = FLUSH_MIDO_TEST;
config->debug = EUCANETD_DEBUG_INFO;
config->flushmodearg = optarg;
break;
case 'H':
printf("EXPERIMENTAL OPTIONS (USE AT YOUR OWN RISK)\n"
"\t%-12s| list VPCMIDO objects\n"
"\t%-12s| list VPCMIDO gateways (bgp status)\n"
"\t%-12s| detect duplicate objects in MidoNet\n"
"\t%-12s| detect and flush duplicate objects in MidoNet\n"
"\t%-12s| detect unconnected objects in MidoNet\n"
"\t%-12s| detect and flush unconnected objects in MidoNet\n"
"\t%-12s| check a VPC model (i-x | eni-x | vpc-x | subnet-x | nat-x | sg-x)\n"
"\t%-12s| flush a VPC model (i-x | eni-x | vpc-x | subnet-x | nat-x | sg-x)\n"
"\t%-12s| create tunnel-zone using IP on (dev)\n"
"\t\t\tbefore using -T make sure that midolman is running on all hosts\n"
"\t\t\tall hosts are assumed to have device (dev) with 1 IP address\n"
"\t\tlowercase options are read-only, and work with eucanetd service running\n"
"\t\tuppercase options can only be executed with eucanetd service stopped\n"
, "-l", "-g", "-m", "-M", "-u", "-U", "-v (id)", "-V (id)", "-T (dev)");
exit (1);
break;
case 'h':
default:
printf("Eucalyptus version %s - eucanetd\n"
"USAGE: %s OPTIONS\n"
"\t%-12s| debug - run eucanetd in foreground, all output to terminal\n"
"\t%-12s| flush all EDGE eucanetd artifacts\n"
"\t%-12s| flush only dynamic EDGE eucanetd artifacts\n"
"\t%-12s| flush all but core objects that implement VPC models\n"
"\t%-12s| flush all objects (including core) that implement VPC models\n"
"\t\tNote: depending on the size of your cloud, flushing objects\n"
"\t\tfrom MidoNet and subsequently reconstructing them can take\n"
"\t\ta long time. For example, flushing 4,000 instances (or ENIs)\n"
"\t\tcan take over 1 hour (and another hour to reconstruct)\n"
, EUCA_VERSION, argv[0], "-d", "-F", "-f", "-C", "-Z");
exit(1);
break;
}
}
// need just enough config to initialize things and set up logging subsystem
rc = eucanetd_read_config_bootstrap();
if (rc) {
fprintf(stderr, "could not read enough config to bootstrap eucanetd, exiting\n");
exit(1);
}
if (!config->multieucanetd_safe) {
rc = eucanetd_dummy_udpsock();
if (rc == -1) {
LOGERROR("Cannot start eucanetd: another eucanetd might be running\n");
exit(1);
}
}
if (!config->systemd) {
// daemonize this process!
rc = eucanetd_daemonize();
if (rc) {
fprintf(stderr, "failed to eucanetd_daemonize eucanetd, exiting\n");
exit(1);
}
}
eucanetd_setlog_bootstrap();
LOGINFO("eucanetd (%s) started\n", EUCA_VERSION);
// Install the signal handlers
gIsRunning = TRUE;
eucanetd_install_signal_handlers();
gni_a = gni_init();
gni_b = gni_init();
pGni = gni_a;
// spin here until we get the latest config
LOGINFO("eucanetd: starting pre-flight checks\n");
rc = 1;
boolean config_mode_ok = FALSE;
boolean config_peer_ok = FALSE;
for (int i = 0; rc; i++) {
if (config->debug != EUCANETD_DEBUG_NONE) {
// Temporarily disable verbose debug messages (it will be set by eucanetd_read_config)
log_params_set(EUCA_LOG_WARN, 0, 100000);
}
rc = eucanetd_read_config(pGni);
if (rc) {
if (i % 100 == 0) {
LOGINFO("eucanetd: waiting for a valid GNI and/or basic configuration\n");
} else {
LOGTRACE("Failed to perform basic eucanetd configuration, will retry in 1 sec\n");
}
} else {
// At this point we have read a valid global network information
// Sanity check before entering eucanetd main loop
if (config->nmCode == NM_INVALID) {
if (i % 30 == 0) {
LOGWARN("Invalid network mode detected. Waiting for a valid mode in GNI\n");
}
rc = 1;
sleep(1);
continue;
} else {
if (!config_mode_ok) {
LOGINFO("\tconfiguring eucanetd in %s mode\n", config->netMode);
config_mode_ok = TRUE;
}
}
//
// Verify that the kernel configuration parameters are enabled
// only need to check in EDGE mode
//
if (IS_NETMODE_EDGE(pGni)) {
if (!sysctl_enabled(SYSCTL_IP_FORWARD)) {
if ((i % 100) == 0) {
LOGERROR("Kernel parameter: net.ipv4.ip_forward is not enabled\n");
LOGERROR("This parameter is typically enabled by default in /usr/lib/sysctl.d/*-eucanetd.conf\n");
}
rc = 1;
}
if (!check_directory(SYSCTL_BRIDGE_PATH)) {
if (!sysctl_enabled(SYSCTL_BRIDGE_CALLIPTABLES)) {
if ((i % 100) == 0) {
LOGERROR("Kernel parameter: net.bridge.bridge-nf-call-iptables is not enabled\n");
LOGERROR("This parameter is typically enabled by default in /usr/lib/sysctl.d/*-eucanetd.conf\n");
}
rc = 1;
}
} else {
// Proc directory is missing, which probably means that br_netfilter isn't loaded
if ((i % 100) == 0) {
LOGERROR("Unable to find bridge info in proc filesystem\n");
LOGERROR("Ensure that br_netfilter has been properly loaded into the kernel\n");
}
rc = 1;
}
}
if (!IS_NETMODE_VPCMIDO(pGni)) {
eucanetdPeer = eucanetd_detect_peer(pGni);
if ((PEER_IS_NONE(eucanetdPeer)) || (!PEER_IS_NC(eucanetdPeer))) {
if (i % 100 == 0) {
LOGWARN("eucanetd in mode %s requires a NC service peer\n", config->netMode);
}
rc = 1;
} else {
if (!config_peer_ok) {
config_peer_ok = TRUE;
LOGINFO("\teucanetd valid service peer (%s) detected\n", PEER2STR(eucanetdPeer));
}
}
} else {
char *clcip = hex2dot(pGni->enabledCLCIp);
if (!gni_is_self_getifaddrs(clcip)) {
eucanetdPeer = PEER_CLC;
if (!config_peer_ok) {
config_peer_ok = TRUE;
LOGINFO("\teucanetd valid service peer (%s) detected\n", PEER2STR(eucanetdPeer));
}
} else {
if (i % 100 == 0) {
LOGWARN("eucanetd in mode %s requires CLC service peer\n", config->netMode);
}
rc = 1;
}
EUCA_FREE(clcip);
}
}
if (rc && config->flushmode) {
LOGFATAL("Unable to complete eucanetd pre-flight checks. Flush aborted.\n");
exit(1);
}
if (gTermCaught) {
LOGINFO("shutting down eucanetd due to SIGTERM\n");
exit(0);
}
// Initialize our network driver
if (!rc) {
rc = eucanetd_initialize_network_drivers(config, pGni);
if (rc) {
LOGFATAL("Failed to initialize network driver: eucanetd going down\n");
exit(1);
}
}
if (rc) {
sleep(1);
}
}
LOGINFO("eucanetd: pre-flight checks complete.\n");
// got all config, enter main loop
while (gIsRunning) {
eucanetd_timer(&ttv);
counter++;
// fetch all latest networking information from various sources
rc = eucanetd_fetch_latest_network(&update_globalnet, &config_changed);
if (rc) {
LOGWARN("one or more fetches for latest network information was unsuccessful\n");
}
if (config_changed) {
pGniApplied = NULL;
}
// first time we run, force an update
if (firstrun) {
update_globalnet = TRUE;
firstrun = 0;
}
if (gUsr1Caught) {
if (pDriverHandler->handle_signal) {
pDriverHandler->handle_signal(config, pGni, SIGUSR1);
}
gUsr1Caught = FALSE;
}
if (gUsr2Caught) {
if (pDriverHandler->handle_signal) {
pDriverHandler->handle_signal(config, pGni, SIGUSR2);
}
gUsr2Caught = FALSE;
// emulate HUP - force update
gHupCaught = TRUE;
}
// Force an update if SIGHUP is caught
if (gHupCaught) {
update_globalnet = TRUE;
// Invalidate last applied version
config->lastAppliedVersion[0] = '\0';
gHupCaught = FALSE;
}
// if the last update operations failed, regardless of new info, force an update
if (update_globalnet_failed == TRUE) {
LOGDEBUG("last update of network state failed, forcing a retry: update_globalnet_failed=%d\n", update_globalnet_failed);
update_globalnet = TRUE;
}
update_globalnet_failed = FALSE;
if (update_globalnet) {
rc = eucanetd_read_latest_network(pGni, &update_globalnet);
}
if (rc) {
LOGWARN("Failed to populate GNI. skipping update\n");
// if the local read failed for some reason, skip any attempt to update (leave current state in place)
update_globalnet = FALSE;
}
if (update_globalnet && (pGni->nmCode != config->nmCode)) {
LOGWARN("Inconsistent network mode in GNI(%s) and eucalyptus.conf(%s). Skipping update.\n", pGni->sMode, config->netMode);
update_globalnet = FALSE;
}
// Do we need to run the network upgrade stuff?
if (pDriverHandler->upgrade) {
if (pDriverHandler->upgrade(config, pGni) == 0) {
// We no longer need to run it
pDriverHandler->upgrade = NULL;
} else {
if (epoch_failed_updates >= 60) {
LOGERROR("could not complete network upgrade after 60 retries: check above log errors for details\n");
} else {
LOGWARN("retry (%d): could not complete network upgrade: retrying\n", epoch_failed_updates);
}
update_globalnet_failed = TRUE;
}
}
// Do we need to flush all eucalyptus networking artifacts?
if (config->flushmode) {
if (IS_NETMODE_VPCMIDO(pGni) && ((config->flushmode == FLUSH_DYNAMIC) || (config->flushmode == FLUSH_ALL))) {
LOGERROR("options '-f' and '-F' cannot be used in VPCMIDO mode\n");
} else {
if (!IS_NETMODE_VPCMIDO(pGni) && (config->flushmode > FLUSH_DYNAMIC)) {
// invalid flush mode for non-VPCMIDO modes
LOGERROR("Invalid flush mode selected\n");
} else {
eucanetd_timer(&tv);
// Make sure we were given a flush API prior to calling it
if (pDriverHandler->system_flush) {
if (pDriverHandler->system_flush(config, pGni)) {
LOGERROR("flushing of euca networking artifacts failed\n");
}
}
LOGINFO("eucanetd flush executed in %ld ms.\n", eucanetd_timer(&tv));
}
}
update_globalnet = FALSE;
gIsRunning = FALSE;
config->flushmode = FLUSH_NONE;
}
// if information on sec. group rules/membership has changed, apply
if (update_globalnet) {
eucanetd_timer_usec(&tv);
update_version_file = FALSE;
LOGINFO("new networking state: updating system\n");
//
// If we don't have a scrub API, just call all APIs. Any driver design must have this
// API defined but for development purpose it make sense to sometimes bypass it.
//
if (!pDriverHandler->system_scrub) {
// Run ALL
scrubResult = EUCANETD_RUN_ALL_API;
} else {
// Scrub the system so see what needs to be done
scrubResult = pDriverHandler->system_scrub(config, pGni, pGniApplied);
LOGINFO("eucanetd system_scrub executed in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
}
// Make sure the scrub did not fail
if ((scrubResult & EUCANETD_RUN_ERROR_API) == 0) {
// update network artifacts (devices, tunnels, etc.) if the scrub indicate so
if (pDriverHandler->implement_network && (scrubResult & EUCANETD_RUN_NETWORK_API)) {
rc = pDriverHandler->implement_network(config, pGni);
if (rc) {
if (epoch_failed_updates >= 60) {
LOGERROR("could not complete VM network update after 60 retries: check above log errors for details\n");
} else {
LOGWARN("retry (%d): could not complete VM network update: retrying\n", epoch_failed_updates);
}
update_globalnet_failed = TRUE;
} else {
LOGINFO("eucanetd implement_network executed in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
}
}
// update security groups, membership, etc. if the scrub indicate so
if (pDriverHandler->implement_sg && (scrubResult & EUCANETD_RUN_SECURITY_GROUP_API)) {
rc = pDriverHandler->implement_sg(config, pGni);
if (rc) {
LOGERROR("could not complete update of security groups: check above log errors for details\n");
update_globalnet_failed = TRUE;
} else {
LOGINFO("eucanetd implement_sg executed in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
}
}
// update IP addressing, elastic IPs, etc. if the scrub indicate so
if (pDriverHandler->implement_addressing && (scrubResult & EUCANETD_RUN_ADDRESSING_API)) {
rc = pDriverHandler->implement_addressing(config, pGni);
if (rc) {
LOGERROR("could not complete VM addressing update: check above log errors for details\n");
update_globalnet_failed = TRUE;
} else {
LOGINFO("eucanetd implement_addressing executed in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
}
}
} else {
LOGERROR("could not complete VM network update: check above log errors for details\n");
update_globalnet_failed = TRUE;
}
}
if (update_globalnet) {
if (update_globalnet_failed == TRUE) {
epoch_failed_updates++;
if ((scrubResult == EUCANETD_VPCMIDO_IFERROR) ||
(scrubResult == EUCANETD_VPCMIDO_GWERROR)) {
update_version_file = TRUE;
}
} else {
update_version_file = TRUE;
snprintf(config->lastAppliedVersion, GNI_VERSION_LEN, "%s", pGni->version);
config->eucanetd_err = 0;
}
if (update_version_file) {
char versionFile[EUCA_MAX_PATH];
// update was requested and was successful
epoch_updates++;
snprintf(versionFile, EUCA_MAX_PATH, EUCALYPTUS_RUN_DIR "/global_network_info.version", config->eucahome);
if (!strlen(pGni->version) || (str2file(pGni->version, versionFile, O_CREAT | O_TRUNC | O_WRONLY, 0644, FALSE) != EUCA_OK) ) {
LOGWARN("failed to populate GNI version file '%s': check permissions and disk capacity\n", versionFile);
}
if (config->eucanetd_first_update) {
config->eucanetd_first_update = FALSE;
}
}
}
epoch_checks++;
if (epoch_timer >= 300) {
LOGINFO("eucanetd report: tot_checks=%d tot_update_attempts=%d\n\tsuccess_update_attempts=%d fail_update_attempts=%d duty_cycle_minutes=%f\n", epoch_checks,
epoch_updates + epoch_failed_updates, epoch_updates, epoch_failed_updates, (float)epoch_timer / 60.0);
epoch_checks = epoch_updates = epoch_failed_updates = epoch_timer = 0;
}
if ((update_globalnet_failed == FALSE) && (update_globalnet == FALSE) && (gIsRunning == TRUE)) {
if (pDriverHandler->system_maint) {
rc = pDriverHandler->system_maint(config, pGniApplied);
if (rc != 0) {
LOGWARN("Failed to execute maintenance for %s.\n", pDriverHandler->name);
}
}
}
// do it all over again...
if (update_globalnet_failed == TRUE) {
LOGWARN("main loop complete (%ld ms): failures detected sleeping %d seconds before next poll\n", eucanetd_timer(&ttv), 1);
pGniApplied = NULL;
sleep(config->polling_frequency);
} else {
if (update_globalnet == FALSE) {
LOGTRACE("main loop complete (%ld ms): sleeping %d seconds before next poll\n", eucanetd_timer(&ttv), config->polling_frequency);
sleep(config->polling_frequency);
} else {
pGniApplied = pGni;
if (pGni == gni_a) {
pGni = gni_b;
} else {
pGni = gni_a;
}
LOGINFO("main loop complete (%ld ms), applied GNI %s\n", eucanetd_timer(&ttv), config->lastAppliedVersion);
}
}
epoch_timer += config->polling_frequency;
}
LOGINFO("eucanetd going down.\n");
if (pDriverHandler->cleanup) {
if (pDriverHandler->cleanup(config, pGni, FALSE) != 0) {
LOGERROR("Failed to cleanup '%s' network driver.\n", pDriverHandler->name);
}
}
eucanetd_cleanup();
GNI_FREE(gni_a);
GNI_FREE(gni_b);
LOGINFO("=== eucanetd down ===\n");
exit(0);
}
#endif /* ! EUCANETD_UNIT_TEST */
/**
* Handles the SIGTERM signal
* @param signal [in] received signal number (SIGTERM is expected)
*/
static void eucanetd_sigterm_handler(int signal) {
LOGINFO("eucanetd caught SIGTERM signal.\n");
gIsRunning = FALSE;
gTermCaught = TRUE;
sig_rcvd = signal;
}
/**
* Handles the SIGHUP signal
* @param signal [in] received signal number (SIGHUP is expected)
*/
static void eucanetd_sighup_handler(int signal) {
LOGINFO("eucanetd caught a SIGHUP signal.\n");
config->flushmode = FLUSH_NONE;
gHupCaught = TRUE;
sig_rcvd = signal;
}
/**
* Handles SIGUSR1 signal.
* @param signal [in] received signal number.
*/
static void eucanetd_sigusr1_handler(int signal) {
LOGDEBUG("eucanetd caught a SIGUSR1 (%d) signal.\n", signal);
gUsr1Caught = TRUE;
sig_rcvd = signal;
}
/**
* Handles SIGUSR2 signal.
* @param signal [in] received signal number.
*/
static void eucanetd_sigusr2_handler(int signal) {
LOGDEBUG("eucanetd caught a SIGUSR2 (%d) signal.\n", signal);
gUsr2Caught = TRUE;
sig_rcvd = signal;
}
/**
* Emulates the reception of SIGUSR2.
*/
void eucanetd_emulate_sigusr2(void) {
gUsr2Caught = TRUE;
sig_rcvd = SIGUSR2;
}
/**
* Installs signal handlers for this application
*/
static void eucanetd_install_signal_handlers(void) {
struct sigaction act = { {0} };
// Install the SIGTERM signal handler
bzero(&act, sizeof(struct sigaction));
act.sa_handler = &eucanetd_sigterm_handler;
if (sigaction(SIGTERM, &act, NULL) < 0) {
LOGFATAL("Failed to install SIGTERM handler");
exit(1);
}
// Install the SIGHUP signal handler
bzero(&act, sizeof(struct sigaction));
act.sa_handler = &eucanetd_sighup_handler;
if (sigaction(SIGHUP, &act, NULL) < 0) {
LOGFATAL("Failed to install SIGTERM handler");
exit(1);
}
// Install the SIGUSR1 signal handler
bzero(&act, sizeof(struct sigaction));
act.sa_handler = &eucanetd_sigusr1_handler;
if (sigaction(SIGUSR1, &act, NULL) < 0) {
LOGFATAL("Failed to install SIGUSR1 handler");
exit(1);
}
// Install the SIGUSR1 signal handler
bzero(&act, sizeof(struct sigaction));
act.sa_handler = &eucanetd_sigusr2_handler;
if (sigaction(SIGUSR2, &act, NULL) < 0) {
LOGFATAL("Failed to install SIGUSR2 handler");
exit(1);
}
}
/**
* Check eucanetd config files for changes
*
* @return 0 if no changes have been detected. Positive integer if configuration
* parameters not requiring restart has changed.
*
* @note
* Currently only /etc/eucalyptus/eucalyptus.conf is checked
*/
static int eucanetd_fetch_latest_local_config(void) {
int ret = 0;
char *cval = NULL;
if (isConfigModified(config->configFiles, NUM_EUCANETD_CONFIG) > 0) {
// config modification time has changed
if (readConfigFile(config->configFiles, NUM_EUCANETD_CONFIG)) {
// something has changed that can be read in
LOGINFO("ingressing new configuration options\n");
eucanetd_initialize_logs();
if (IS_NETMODE_VPCMIDO(config)) {
cval = configFileValue("MIDO_ENABLE_ARPTABLE");
if (!strcmp(cval, "Y")) {
if (!config->enable_mido_arptable) {
config->mido_arptable_config_changed = TRUE;
}
config->enable_mido_arptable = TRUE;
} else {
if (config->enable_mido_arptable) {
config->mido_arptable_config_changed = TRUE;
}
config->enable_mido_arptable = FALSE;
}
EUCA_FREE(cval);
cval = configFileValue("MIDO_ENABLE_MIDOMD");
if (!strcmp(cval, "Y")) {
if (!config->enable_mido_md) {
config->mido_md_config_changed = TRUE;
}
config->enable_mido_md = TRUE;
} else {
if (config->enable_mido_md) {
config->mido_md_config_changed = TRUE;
}
config->enable_mido_md = FALSE;
}
EUCA_FREE(cval);
cval = configFileValue("MIDO_MD_254_EGRESS");
if (strcmp(cval, config->mido_md_254_egress)) {
config->mido_md_egress_rules_changed = TRUE;
snprintf(config->mido_md_254_egress, 256, "%s", cval);
}
EUCA_FREE(cval);
cval = configFileValue("MIDO_MD_253_EGRESS");
if (strcmp(cval, config->mido_md_253_egress)) {
config->mido_md_egress_rules_changed = TRUE;
snprintf(config->mido_md_253_egress, 256, "%s", cval);
}
EUCA_FREE(cval);
cval = configFileValue("MIDO_API_URIBASE");
if (strcmp(cval, config->mido_api_uribase)) {
LOGINFO("config MIDO_API_URIBASE = %s\n", cval);
snprintf(config->mido_api_uribase, URI_LEN, "%s", cval);
int uribaselen = strlen(config->mido_api_uribase);
if (uribaselen && config->mido_api_uribase[uribaselen - 1] == '/') {
config->mido_api_uribase[uribaselen - 1] = '\0';
}
pDriverHandler->cleanup(config, pGni, FALSE);
}