forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheuca-to-mido.c
10562 lines (9590 loc) · 412 KB
/
euca-to-mido.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 2016 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/euca-to-mido.c
//! Need definition
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include <curl/curl.h>
#include <json-c/json.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 "ipt_handler.h"
#include "ips_handler.h"
#include "ebt_handler.h"
#include "dev_handler.h"
#include "euca_gni.h"
#include "midonet-api.h"
#include "euca-to-mido.h"
#include "eucanetd_util.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
extern int midonet_api_system_changed;
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
static int maint_c = 0;
int midocache_invalid = 0;
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
/**
* Deletes all VPC metaproxy namespace interfaces and metabr.
* @param mido [in] data structure that holds all discovered MidoNet configuration/resources.
* @return 0 on success. 1 otherwise.
*/
int do_delete_meta_nslinks(mido_config *mido) {
int ret = 0, rc = 0;
char cmd[EUCA_MAX_PATH], sid[16];
char devpath[EUCA_MAX_PATH];
sequence_executor cmds;
mido_vpc *vpc;
se_init(&cmds, mido->config->cmdprefix, 4, 1);
for (int i = 0; i < mido->max_vpcs; i++) {
vpc = &(mido->vpcs[i]);
sscanf(vpc->name, "vpc-%8s", sid);
snprintf(devpath, EUCA_MAX_PATH, "/sys/class/net/vn2_%s/", sid);
if (!check_directory(devpath)) {
snprintf(cmd, EUCA_MAX_PATH, "ip link del vn2_%s", sid);
se_add(&cmds, cmd, NULL, ignore_exit);
}
}
rc = se_execute(&cmds);
if (rc) {
LOGWARN("failed to delete metaproxy ip namespace veth pairs\n");
ret = 1;
}
delete_mido_meta_core(mido);
se_free(&cmds);
return (ret);
}
/**
* Deletes all VPC instances/interfaces MidoNet chains.
* @param mido [in] data structure that holds all discovered MidoNet configuration/resources.
* @return 0 on success. 1 otherwise.
*/
int do_delete_vpceni_chains(mido_config *mido) {
int ret = 0;
if (!mido) {
LOGWARN("Invalid argument: cannot delete VPC ifs chains - NULL config.\n");
return (1);
}
// Remove all ic_* chains
midoname **chains = NULL;
int max_chains = 0;
mido_get_chains(VPCMIDO_TENANT, &chains, &max_chains);
for (int i = 0; i < max_chains; i++) {
midoname *chain = chains[i];
if (strstr(chain->name, "ic_")) {
mido_delete_chain(chain);
}
}
EUCA_FREE(chains);
return (ret);
}
/**
* Removes MidoNet objects that are needed to implement metaproxy-based VPC MD.
* @param mido [in] data structure that holds all discovered MidoNet configuration/resources.
* @return 0 on success. 1 otherwise.
*/
int do_metaproxy_disable(mido_config *mido) {
int ret = 0;
int rc = 0;
if (!mido) {
LOGWARN("Invalid argument: cannot disable metaproxy - NULL config.\n");
return (1);
}
if (!mido->config->enable_mido_md) {
return (0);
}
// Tear down metaproxies
rc = do_metaproxy_teardown(mido);
if (rc) {
LOGERROR("failed to teardown metadata proxies\n");
ret++;
}
// Disconnect instances/interfaces from md
for (int i = 0; i < mido->max_vpcs; i++) {
mido_vpc *vpc = &(mido->vpcs[i]);
for (int j = 0; j < vpc->max_subnets; j++) {
mido_vpc_subnet *subnet = &(vpc->subnets[j]);
u32 subnet_addr = 0;
// Get the Subnet CIDR
boolean found = FALSE;
for (int k = 0; vpc->vpcrt && k < vpc->vpcrt->max_routes && !found; k++) {
midoname *route = vpc->vpcrt->routes[k];
if (!route || !subnet->midos[SUBN_VPCRT_BRPORT]) {
continue;
}
if (!strcmp(route->route->nexthopport, subnet->midos[SUBN_VPCRT_BRPORT]->uuid) &&
!route->route->nexthopgateway && strcmp(route->route->dstlen, "32")) {
found = TRUE;
subnet_addr = dot2hex(route->route->dstnet);
LOGTRACE("\t%s : %s\n", subnet->name, route->route->dstnet);
}
}
// Due to dot2 dns nat rules, removing metaproxy nat rules is not sufficient
for (int k = 0; k < subnet->max_instances && FALSE; k++) {
mido_vpc_instance *vpcif = &(subnet->instances[k]);
if (!vpcif->prechain || !vpcif->postchain) {
continue;
}
midoname *ptmpmn = NULL;
// Search metaproxy DNAT rule
char *pt_buf = hex2dot(subnet_addr + 3);
mido_find_rule_from_list(vpcif->prechain->rules, vpcif->prechain->max_rules, &ptmpmn,
"type", "dnat", "flowAction", "continue",
"ipAddrGroupDst", mido->midocore->midos[CORE_METADATA_IPADDRGROUP]->uuid,
"nwProto", "6", "tpDst", "jsonjson", "tpDst:start", "80", "tpDst:end", "80",
"tpDst:END", "END", "natTargets", "jsonlist", "natTargets:addressTo", pt_buf,
"natTargets:addressFrom", pt_buf, "natTargets:portFrom",
"8008", "natTargets:portTo", "8008", "natTargets:END", "END", NULL);
if (ptmpmn) {
LOGTRACE("%s: deleting DNAT rule %s\n", vpcif->name, ptmpmn->uuid);
mido_delete_rule(vpcif->prechain, ptmpmn);
}
// Search metaproxy SNAT rule
mido_find_rule_from_list(vpcif->postchain->rules, vpcif->postchain->max_rules, &ptmpmn,
"type", "snat", "flowAction", "continue",
"nwSrcAddress", pt_buf, "nwSrcLength", "32", "nwProto", "6",
"tpSrc", "jsonjson", "tpSrc:start", "8008", "tpSrc:end", "8008", "tpSrc:END", "END",
"natTargets", "jsonlist", "natTargets:addressTo", "169.254.169.254",
"natTargets:addressFrom", "169.254.169.254", "natTargets:portFrom", "80",
"natTargets:portTo", "80", "natTargets:END", "END", NULL);
if (ptmpmn) {
LOGTRACE("%s: deleting SNAT rule %s\n", vpcif->name, ptmpmn->uuid);
mido_delete_rule(vpcif->postchain, ptmpmn);
}
EUCA_FREE(pt_buf);
}
}
}
return (ret);
}
/**
* Terminate metaproxy (nginx) processes of all VPCs
* @param mido [in] data structure that holds MidoNet configuration
* @return 0 on success. Positive integer otherwise.
*/
int do_metaproxy_teardown(mido_config *mido) {
LOGDEBUG("tearing down metaproxy subsystem\n");
return (do_metaproxy_maintain(mido, 1));
}
/**
* Start metaproxy (nginx) processes for all VPCs
* @param mido [in] data structure that holds MidoNet configuration
* @return 0 on success. Positive integer otherwise.
*/
int do_metaproxy_setup(mido_config *mido) {
return (do_metaproxy_maintain(mido, 0));
}
/**
* Maintenance of metaproxy (nginx) process(es)
* @param mido [in] data structure that holds MidoNet configuration
* @param mode [in] 0 to create. 1 to terminate.
* @return 0 on success. Positive integer otherwise.
*
* @note both core and vpc nginx processes hold the network namespace open. In order
* to properly cleanup network namespaces, core nginx needs to be terminated when
* deleting VPCs.
*/
int do_metaproxy_maintain(mido_config *mido, int mode) {
int ret = 0, rc, i = 0, dorun = 0;
pid_t npid = 0;
char cmd[EUCA_MAX_PATH], *pidstr = NULL, pidfile[EUCA_MAX_PATH];
sequence_executor cmds;
if (!mido || mode < 0 || mode > 1) {
LOGERROR("invalid argument: unable to maintain metaproxy for NULL mido\n");
return (1);
}
rc = se_init(&cmds, mido->config->cmdprefix, 4, 1);
dorun = 0;
snprintf(pidfile, EUCA_MAX_PATH, EUCALYPTUS_RUN_DIR "/nginx_localproxy.pid", mido->eucahome);
if (!check_file(pidfile)) {
pidstr = file2str(pidfile);
if (pidstr) {
npid = atoi(pidstr);
} else {
npid = 0;
}
EUCA_FREE(pidstr);
} else {
npid = 0;
}
if (mode == 0) {
if (npid > 1) {
if (check_process(npid, "nginx")) {
unlink(pidfile);
dorun = 1;
}
} else {
dorun = 1;
}
} else if (mode == 1) {
if (npid > 1 && !check_process(npid, "nginx")) {
dorun = 1;
}
}
if (dorun) {
if (mode == 0) {
LOGTRACE("core proxy not running, starting new core proxy\n");
snprintf(cmd, EUCA_MAX_PATH,
"nginx -p . -c " EUCALYPTUS_DATA_DIR "/nginx_proxy.conf -g 'pid "
EUCALYPTUS_RUN_DIR "/nginx_localproxy.pid; env NEXTHOP=127.0.0.1; "
"env NEXTHOPPORT=8773; env EUCAHOME=%s;'",
mido->eucahome, mido->eucahome, mido->eucahome);
} else if (mode == 1) {
LOGTRACE("core proxy running, terminating core proxy\n");
snprintf(cmd, EUCA_MAX_PATH, "kill %d", npid);
}
rc = se_add(&cmds, cmd, NULL, ignore_exit);
} else {
LOGTRACE("not maintaining proxy, no action to take for pid (%d)\n", npid);
}
for (i = 0; i < mido->max_vpcs; i++) {
if (strlen(mido->vpcs[i].name)) {
dorun = 0;
snprintf(pidfile, EUCA_MAX_PATH, EUCALYPTUS_RUN_DIR "/nginx_vpcproxy_%s.pid",
mido->eucahome, mido->vpcs[i].name);
if (!check_file(pidfile)) {
pidstr = file2str(pidfile);
if (pidstr) {
npid = atoi(pidstr);
} else {
npid = 0;
}
EUCA_FREE(pidstr);
} else {
npid = 0;
}
if (mode == 0) {
if (npid > 1) {
if (check_process(npid, "nginx")) {
unlink(pidfile);
dorun = 1;
}
} else {
dorun = 1;
}
} else if (mode == 1) {
if (npid > 1 && !check_process(npid, "nginx")) {
dorun = 1;
}
}
if (dorun) {
if (mode == 0) {
LOGTRACE("VPC (%s) proxy not running, starting new proxy\n", mido->vpcs[i].name);
snprintf(cmd, EUCA_MAX_PATH,
"nsenter --net=/var/run/netns/%s nginx -p . -c " EUCALYPTUS_DATA_DIR
"/nginx_proxy.conf -g 'pid " EUCALYPTUS_RUN_DIR "/nginx_vpcproxy_%s.pid; "
"env VPCID=%s; env NEXTHOP=169.254.0.1; env NEXTHOPPORT=8009; env EUCAHOME=%s;'",
mido->vpcs[i].name, mido->eucahome, mido->eucahome, mido->vpcs[i].name, mido->vpcs[i].name, mido->eucahome);
} else if (mode == 1) {
LOGTRACE("VPC (%s) proxy running, terminating VPC proxy\n", mido->vpcs[i].name);
snprintf(cmd, EUCA_MAX_PATH, "kill %d", npid);
}
rc = se_add(&cmds, cmd, NULL, ignore_exit);
} else {
LOGTRACE("not maintaining VPC (%s) proxy, no action to take on pid (%d)\n", mido->vpcs[i].name, npid);
}
}
}
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute meta core bridge setup/proxy commands: see above log entries for details\n");
ret = 1;
}
se_free(&cmds);
return (ret);
}
/**
* Controls metadata nginx instance
* @param mido [in] data structure that holds MidoNet configuration
* @param mode [in] VPCMIDO_NGINX_START to create. VPCMIDO_NGINX_STOP to terminate.
* @return 0 on success. Positive integer otherwise.
*/
int do_md_nginx_maintain(mido_config *mido, enum vpcmido_nginx_t mode) {
int ret = 0;
int rc = 0;
boolean do_start = FALSE;
boolean do_stop = FALSE;
char cmd[EUCA_MAX_PATH];
if (!mido || !mido->config || mode < VPCMIDO_NGINX_START || mode > VPCMIDO_NGINX_STOP) {
LOGERROR("invalid argument: unable to maintain md nginx\n");
return (1);
}
snprintf(cmd, EUCA_MAX_PATH, "%s %s is-active %s", mido->config->cmdprefix,
mido->config->systemctl, EUCANETD_NGINX_UNIT);
rc = timeshell_nb(cmd, 10, FALSE);
if (mode == VPCMIDO_NGINX_START) {
if (rc != 0) {
do_start = TRUE;
}
} else if (mode == VPCMIDO_NGINX_STOP) {
if (rc == 0) {
do_stop = TRUE;
}
}
if (do_start) {
LOGINFO("\tstarting md nginx process\n");
snprintf(cmd, EUCA_MAX_PATH, "%s %s start %s", mido->config->cmdprefix,
mido->config->systemctl, EUCANETD_NGINX_UNIT);
rc = timeshell_nb(cmd, 10, FALSE);
if (rc != 0) {
LOGWARN("failed to start eucanetd-nginx\n");
}
}
if (do_stop) {
LOGINFO("\tstopping md nginx process\n");
snprintf(cmd, EUCA_MAX_PATH, "%s %s start %s", mido->config->cmdprefix,
mido->config->systemctl, EUCANETD_NGINX_UNIT);
rc = timeshell_nb(cmd, 10, FALSE);
if (rc != 0) {
LOGWARN("failed to stop eucanetd-nginx\n");
}
}
return (ret);
}
/**
* Remove metadata core artifacts from the system.
* @param mido [in] data structure that holds MidoNet configuration
* @return 0 on success. Positive integer otherwise.
*/
int delete_mido_meta_core(mido_config *mido) {
int ret = 0, rc = 0;
char cmd[EUCA_MAX_PATH];
char devpath[EUCA_MAX_PATH];
sequence_executor cmds;
rc = se_init(&cmds, mido->config->cmdprefix, 4, 1);
snprintf(devpath, EUCA_MAX_PATH, "/sys/class/net/metabr/");
if (!check_directory(devpath)) {
snprintf(cmd, EUCA_MAX_PATH, "ip link set metabr down");
rc = se_add(&cmds, cmd, NULL, ignore_exit2);
snprintf(cmd, EUCA_MAX_PATH, "brctl delbr metabr");
rc = se_add(&cmds, cmd, NULL, ignore_exit);
}
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute meta core bridge setup/proxy commands: see above log entries for details\n");
ret = 1;
}
se_free(&cmds);
return (ret);
}
/**
* Creates the metadata core artifacts.
* @param mido [in] data structure that holds MidoNet configuration
* @return 0 on success. Positive integer otherwise.
*/
int create_mido_meta_core(mido_config *mido) {
int ret = 0, rc = 0;
char cmd[EUCA_MAX_PATH];
sequence_executor cmds;
rc = se_init(&cmds, mido->config->cmdprefix, 4, 1);
snprintf(cmd, EUCA_MAX_PATH, "brctl addbr metabr");
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "brctl setfd metabr 2");
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "brctl sethello metabr 2");
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "ip addr add 169.254.0.1/16 dev metabr");
rc = se_add(&cmds, cmd, NULL, ignore_exit2);
snprintf(cmd, EUCA_MAX_PATH, "ip link set metabr up");
rc = se_add(&cmds, cmd, NULL, ignore_exit2);
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute meta core bridge setup/proxy commands: see above log entries for details\n");
ret = 1;
}
se_free(&cmds);
return (ret);
}
/**
* Deletes metadata artifacts created for VPC vpc.
* @param mido [in] data structure that holds MidoNet configuration
* @param vpcname [in] vpc name/id of interest
* @return 0 on success. Positive integer otherwise.
*/
int delete_mido_meta_vpc_namespace(mido_config *mido, char *vpcname) {
int ret = 0, rc = 0;
char cmd[EUCA_MAX_PATH], sid[16];
char devpath[EUCA_MAX_PATH];
sequence_executor cmds;
struct timeval tv;
eucanetd_timer_usec(&tv);
sscanf(vpcname, "vpc-%8s", sid);
rc = se_init(&cmds, mido->config->cmdprefix, 10, 1);
// snprintf(cmd, EUCA_MAX_PATH, "ip link set vn2_%s down", sid);
// rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(devpath, EUCA_MAX_PATH, "/sys/class/net/vn2_%s/", sid);
if (!check_directory(devpath)) {
snprintf(cmd, EUCA_MAX_PATH, "ip link del vn2_%s", sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
}
snprintf(cmd, EUCA_MAX_PATH, "ip netns del %s", vpcname);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute netns for VPC or create/ip assign commands: see above log entries for details\n");
ret = 1;
}
se_free(&cmds);
LOGINFO("\tVPC ip namespace deleted in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (ret);
}
/**
* Creates metadata artifacts for the VPC vpc.
* @param mido [in] data structure that holds MidoNet configuration
* @param vpc [in] pointer to the mido_vpc data structure of interest
* @return 0 on success. Positive integer otherwise.
*/
int create_mido_meta_vpc_namespace(mido_config *mido, mido_vpc *vpc) {
int ret = 0, rc = 0;
u32 nw, ip;
char cmd[EUCA_MAX_PATH], *ipstr = NULL, sid[16];
sequence_executor cmds;
struct timeval tv;
eucanetd_timer_usec(&tv);
rc = read_mido_meta_vpc_namespace(mido, vpc);
if (!rc) {
LOGTRACE("namespace (%s) already exists, skipping create\n", vpc->name);
return (0);
}
// create meta tap namespace/devices
sscanf(vpc->name, "vpc-%8s", sid);
rc = se_init(&cmds, mido->config->cmdprefix, 4, 1);
snprintf(cmd, EUCA_MAX_PATH, "ip netns add %s", vpc->name);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
if (!mido->config->enable_mido_md) {
snprintf(cmd, EUCA_MAX_PATH, "ip link add vn2_%s type veth peer name vn3_%s", sid, sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "ip link set vn2_%s up", sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "brctl addif metabr vn2_%s", sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "ip link set vn3_%s netns %s", sid, vpc->name);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
nw = dot2hex("169.254.0.0");
ip = nw + vpc->rtid;
ipstr = hex2dot(ip);
snprintf(cmd, EUCA_MAX_PATH, "nsenter --net=/var/run/netns/%s ip addr add %s/16 dev vn3_%s", vpc->name, ipstr, sid);
EUCA_FREE(ipstr);
se_add(&cmds, cmd, NULL, ignore_exit2);
snprintf(cmd, EUCA_MAX_PATH, "nsenter --net=/var/run/netns/%s ip link set vn3_%s up", vpc->name, sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
}
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute netns for VPC or create/ip assign commands: see above log entries for details\n");
ret = 1;
}
se_free(&cmds);
LOGINFO("\tVPC ip namespace created in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (ret);
}
/**
* Checks whether metadata artifacts for VPC vpc is present in the system.
* @param mido [in] data structure that holds MidoNet configuration
* @param vpc [in] pointer to the mido_vpc data structure of interest
* @return 0 on success. Positive integer otherwise.
*/
int read_mido_meta_vpc_namespace(mido_config *mido, mido_vpc *vpc) {
char cmd[EUCA_MAX_PATH], sid[16];
// create a meta tap namespace/devices
sscanf(vpc->name, "vpc-%8s", sid);
snprintf(cmd, EUCA_MAX_PATH, "/var/run/netns/%s", vpc->name);
if (check_path(cmd)) {
LOGTRACE("cannot find VPC netns: %s\n", cmd);
return (1);
} else {
LOGTRACE("found VPC netns: %s\n", cmd);
}
if (!mido->config->enable_mido_md) {
snprintf(cmd, EUCA_MAX_PATH, "vn2_%s", sid);
if (!dev_exist(cmd)) {
LOGTRACE("cannot find VPC metataps vn2_%s\n", sid);
return (1);
} else {
LOGTRACE("found VPC metataps vn2_%s\n", sid);
}
}
return (0);
}
/**
* Deletes the metadata veth interface pair created for subnet name.
* @param mido [in] data structure that holds MidoNet configuration
* @param name [in] name of the subnet of interest
* @return 0 on success. Positive integer otherwise.
*/
int delete_mido_meta_subnet_veth(mido_config *mido, char *name) {
int ret = 0, rc = 0;
char cmd[EUCA_MAX_PATH], sid[16];
sequence_executor cmds;
struct timeval tv;
eucanetd_timer_usec(&tv);
sscanf(name, "subnet-%8s", sid);
rc = se_init(&cmds, mido->config->cmdprefix, 4, 1);
// snprintf(cmd, EUCA_MAX_PATH, "ip link set vn0_%s down", sid);
// rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "ip link del vn0_%s", sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not delete subnet tap ifaces: see above log entries for details\n");
ret = 1;
}
se_free(&cmds);
LOGINFO("\tVPC subnet metadata veth deleted in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (ret);
}
/**
* Creates a metadata veth interface pair for subnet name in VPC vpc with CIDR subnet/slashnet.
* @param mido [in] data structure that holds MidoNet configuration
* @param vpc [in] pointer to the mido_vpc data structure of interest
* @param name [in] name of the subnet of interest
* @param subnet [in] network address of the subnet
* @param slashnet [in] network mask of the subnet
* @param tapiface [out] name of the veth interface (to be bound to MN bridge)
* @return 0 on success. Positive integer otherwise.
*/
int create_mido_meta_subnet_veth(mido_config *mido, mido_vpc *vpc, char *name, char *subnet, char *slashnet, char **tapiface) {
int ret = 0, rc = 0;
u32 nw, gw;
char cmd[EUCA_MAX_PATH], *gateway = NULL, sid[16];
sequence_executor cmds;
struct timeval tv;
eucanetd_timer_usec(&tv);
// create a meta tap
sscanf(name, "subnet-%8s", sid);
snprintf(cmd, EUCA_MAX_PATH, "vn0_%s", sid);
if (dev_exist(cmd)) {
LOGTRACE("subnet device (%s) already exists, skipping\n", cmd);
*tapiface = EUCA_ZALLOC_C(16, sizeof (char));
snprintf(*tapiface, 16, "vn0_%s", sid);
return (0);
}
rc = se_init(&cmds, mido->config->cmdprefix, 4, 1);
snprintf(cmd, EUCA_MAX_PATH, "ip link add vn0_%s type veth peer name vn1_%s", sid, sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "ip link set vn0_%s up", sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
snprintf(cmd, EUCA_MAX_PATH, "ip link set vn1_%s netns %s", sid, vpc->name);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
nw = dot2hex(subnet);
gw = nw + 3;
gateway = hex2dot(gw);
snprintf(cmd, EUCA_MAX_PATH, "nsenter --net=/var/run/netns/%s ip addr add %s/%s dev vn1_%s", vpc->name, gateway, slashnet, sid);
EUCA_FREE(gateway);
se_add(&cmds, cmd, NULL, ignore_exit2);
snprintf(cmd, EUCA_MAX_PATH, "nsenter --net=/var/run/netns/%s ip link set vn1_%s up", vpc->name, sid);
rc = se_add(&cmds, cmd, NULL, ignore_exit);
se_print(&cmds);
rc = se_execute(&cmds);
if (rc) {
LOGERROR("could not execute tap interface create/ip assign commands: see above log entries for details\n");
*tapiface = NULL;
ret = 1;
} else {
*tapiface = EUCA_ZALLOC_C(16, sizeof (char));
snprintf(*tapiface, 16, "vn0_%s", sid);
}
se_free(&cmds);
LOGINFO("\tVPC subnet metadata veth created in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (ret);
}
/**
* Populates euca VPC models (data structures) from MidoNet models.
* @param mido [in] data structure that holds MidoNet configuration
* @return 0 on success. 1 on any failure.
*/
int do_midonet_populate(mido_config *mido) {
int rc = 0;
struct timeval tv;
eucanetd_timer_usec(&tv);
rc = reinitialize_mido(mido);
if (rc) {
LOGERROR("failed to initialize euca-mido model data structures.\n");
}
LOGTRACE("\treinitialize_mido() in %ld us.\n", eucanetd_timer_usec(&tv));
// populated core
eucanetd_timer_usec(&tv);
rc = populate_mido_core(mido, mido->midocore);
if (rc) {
return (1);
}
// populate md
if (mido_get_router(VPCMIDO_MDRT)) {
mido->config->populate_mido_md = TRUE;
} else {
mido->config->populate_mido_md = FALSE;
}
if (mido->config->enable_mido_md || mido->config->populate_mido_md) {
rc = populate_mido_md(mido);
if (rc) {
return (1);
}
}
LOGINFO("\tmido_core populated in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (do_midonet_populate_vpcs(mido));
}
/**
* Populates euca VPC models (data structures) from MidoNet models.
* @param mido [in] data structure that holds MidoNet configuration
* @return 0 on success. 1 on any failure.
*/
int do_midonet_populate_vpcs(mido_config *mido) {
// pattern
// - find all VPC routers (and populate VPCs)
// - for each VPC, find all subnets (and populate subnets)
// - for each VPC, for each subnet, find all instances (and populate instances)
int i = 0, j = 0, k = 0, rc = 0, rtid = 0, natgrtid = 0, ret = 0;
char subnetname[VPC_SUBNET_ID_LEN];
char vpcname[VPC_ID_LEN];
char aclname[NETWORK_ACL_ID_LEN];
char sgname[SECURITY_GROUP_ID_LEN];
char instanceId[INSTANCE_ID_LEN];
char natgname[NATG_ID_LEN];
char tmpstr[MIDO_NAME_LEN];
char *pattern;
midoname **chains = NULL;
int max_chains = 0;
mido_vpc_secgroup *vpcsecgroup = NULL;
mido_vpc_instance *vpcinstance = NULL;
mido_vpc_subnet *vpcsubnet = NULL;
mido_vpc_natgateway *vpcnatg = NULL;
mido_vpc_nacl *vpcnacl = NULL;
mido_vpc *vpc = NULL;
struct timeval tv;
eucanetd_timer_usec(&tv);
// VPCs
midonet_api_router **routers = NULL;
int max_routers = 0;
rc = midonet_api_cache_get_routers(&routers, &max_routers);
if (max_routers > 0) {
mido->vpcs = EUCA_ZALLOC_C(max_routers, sizeof (mido_vpc));
}
for (i = 0; i < max_routers; i++) {
LOGTRACE("inspecting mido router '%s'\n", routers[i]->obj->name);
memset(vpcname, 0, VPC_ID_LEN);
if ((strlen(routers[i]->obj->name) > 15) && (routers[i]->obj->name[15] == '_')) {
pattern = "vr_%12s_%d";
} else {
pattern = "vr_%21s_%d";
}
sscanf(routers[i]->obj->name, pattern, vpcname, &rtid);
if ((sscanf(routers[i]->obj->name, pattern, vpcname, &rtid) == 2) &&
strlen(vpcname) && rtid) {
vpc = &(mido->vpcs[mido->max_vpcs]);
mido->max_vpcs++;
LOGTRACE("discovered VPC installed in midonet: %s\n", vpcname);
snprintf(vpc->name, sizeof (vpc->name), "%s", vpcname);
set_router_id(mido, rtid);
vpc->rtid = rtid;
vpc->vpcrt = routers[i];
vpc->midos[VPC_VPCRT] = routers[i]->obj;
populate_mido_vpc(mido, mido->midocore, vpc);
}
}
LOGINFO("\tvpcs populated in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
// SUBNETS
midonet_api_router **natgrouters = NULL;
int max_natgrouters = 0;
rc = midonet_api_cache_get_natg_routers(&natgrouters, &max_natgrouters);
midonet_api_bridge **bridges = NULL;
int max_bridges;
rc = midonet_api_cache_get_bridges(&bridges, &max_bridges);
for (i = 0; i < max_bridges; i++) {
LOGTRACE("inspecting bridge '%s'\n", bridges[i]->obj->name);
memset(vpcname, 0, VPC_ID_LEN);
memset(subnetname, 0, VPC_SUBNET_ID_LEN);
if ((strlen(bridges[i]->obj->name) > 15) && (bridges[i]->obj->name[15] == '_')) {
pattern = "vb_%12s_%24s";
} else {
pattern = "vb_%21s_%24s";
}
sscanf(bridges[i]->obj->name, pattern, vpcname, subnetname);
if (strlen(vpcname) && strlen(subnetname)) {
LOGTRACE("discovered VPC subnet installed in midonet: %s/%s\n", vpcname, subnetname);
find_mido_vpc(mido, vpcname, &vpc);
if (vpc) {
LOGTRACE("found VPC matching discovered subnet: %s/%s\n", vpc->name, subnetname);
vpc->subnets = EUCA_REALLOC_C(vpc->subnets, (vpc->max_subnets + 1), sizeof (mido_vpc_subnet));
vpcsubnet = &(vpc->subnets[vpc->max_subnets]);
vpc->max_subnets++;
bzero(vpcsubnet, sizeof (mido_vpc_subnet));
snprintf(vpcsubnet->name, VPC_SUBNET_ID_LEN, "%s", subnetname);
snprintf(vpcsubnet->vpcname, VPC_ID_LEN, "%s", vpcname);
vpcsubnet->vpc = vpc;
vpcsubnet->subnetbr = bridges[i];
vpcsubnet->midos[SUBN_BR] = bridges[i]->obj;
if (bridges[i]->max_dhcps) {
LOGTRACE("%d dhcp for bridge %s\n", bridges[i]->max_dhcps, bridges[i]->obj->name);
vpcsubnet->midos[SUBN_BR_DHCP] = bridges[i]->dhcps[0]->obj;
}
populate_mido_vpc_subnet(mido, vpc, vpcsubnet);
// Search for NAT Gateways
for (j = 0; j < max_natgrouters; j++) {
if (natgrouters[j] == NULL) {
continue;
}
natgname[0] = '\0';
natgrtid = 0;
snprintf(tmpstr, MIDO_NAME_LEN, "natr_%%21s_%s_%%d", subnetname);
sscanf(natgrouters[j]->obj->name, tmpstr, natgname, &natgrtid);
if ((strlen(natgname)) && (natgrtid != 0)) {
LOGTRACE("discovered %s in %s installed in midonet\n", natgname, subnetname);
vpcsubnet->natgateways = EUCA_REALLOC_C(vpcsubnet->natgateways, vpcsubnet->max_natgateways + 1, sizeof (mido_vpc_natgateway));
vpcnatg = &(vpcsubnet->natgateways[vpcsubnet->max_natgateways]);
(vpcsubnet->max_natgateways)++;
bzero(vpcnatg, sizeof (mido_vpc_natgateway));
snprintf(vpcnatg->name, sizeof (vpcnatg->name), "%s", natgname);
set_router_id(mido, natgrtid);
vpcnatg->rtid = natgrtid;
vpcnatg->natgrt = natgrouters[j];
vpcnatg->midos[NATG_RT] = natgrouters[j]->obj;
rc = populate_mido_vpc_natgateway(mido, vpc, vpcsubnet, vpcnatg);
if (rc) {
LOGERROR("cannot populate %s: check midonet health\n", natgname);
natgrouters[j] = NULL;
ret++;
}
}
}
}
}
}
EUCA_FREE(bridges);
EUCA_FREE(routers);
EUCA_FREE(natgrouters);
LOGINFO("\tvpc subnets populated in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
// Network ACLs
mido_get_chains(VPCMIDO_TENANT, &chains, &max_chains);
for (i = 0; i < max_chains; i++) {
LOGTRACE("inspecting chain '%s'\n", chains[i]->name);
memset(vpcname, 0, VPC_ID_LEN);
memset(aclname, 0, NETWORK_ACL_ID_LEN);
if ((strlen(chains[i]->name) > 24) && (chains[i]->name[24] == '_')) {
pattern = "acl_ingress_%12s_%21s";
} else {