forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmidonet-api.c
10213 lines (9495 loc) · 357 KB
/
midonet-api.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/midonet-api.c
//! Need description
//!
/*----------------------------------------------------------------------------*\
| |
| 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 <curl/curl.h>
#include <json-c/json.h>
#include <pthread.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 <log.h>
#include "ipt_handler.h"
#include "ips_handler.h"
#include "ebt_handler.h"
#include "euca_gni.h"
#include "midonet-api.h"
#include "euca-to-mido.h"
#include "eucalyptus-config.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
struct mem_params_t {
char *mem;
size_t size;
};
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
extern int midocache_invalid;
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
midonet_api_cache *midocache = NULL;
midoname_list *midocache_midos = NULL;
int midonet_api_system_changed = 0;
int http_gets = 0;
int http_posts = 0;
int http_puts = 0;
int http_deletes = 0;
double http_gets_time = 0.0;
double http_posts_time = 0.0;
double http_puts_time = 0.0;
double http_deletes_time = 0.0;
static int http_gets_prev = 0;
static int http_posts_prev = 0;
static int http_puts_prev = 0;
static int http_deletes_prev = 0;
static char midonet_api_uribase[URI_LEN] = {0};
char midonet_api_version[16] = {0};
static char midonet_api_mtypes[APPLICATION_MAX_INDEX][MIDO_MTYPE_MAX_LEN];
static int mido_libcurl_initialized = 0;
static mido_libcurl_handles libcurl_handles;
static pthread_mutex_t libcurl_handles_mutex;
static pthread_mutex_t mido_buffer_mutex;
static pthread_mutex_t mido_cache_ports_mutex;
static size_t header_find_location(char *content, size_t size, size_t nmemb, void *params);
static size_t mem_writer(void *contents, size_t size, size_t nmemb, void *in_params);
static size_t mem_reader(void *contents, size_t size, size_t nmemb, void *in_params);
/**
* Prepares an array of mido_cache_thread_params structures: divides ntasks to
* nthreads blocks, and sets the start and end indices appropriately.
* @param ntasks [in] number of tasks.
* @param nthreads [in] number of threads.
* @return pointer to an array of mido_cache_thread_param structures. Caller is
* responsible to release the allocated memory.
*/
mido_cache_worker_thread_params *prep_thread_params(int ntasks, int nthreads) {
mido_cache_worker_thread_params *tparams = EUCA_ZALLOC_C(nthreads, sizeof (mido_cache_worker_thread_params));
if (ntasks > 100) {
int step = (ntasks + nthreads - 1) / nthreads;
tparams[0].start = 0;
tparams[0].end = step;
for (int i = 1; i < (nthreads - 1); i++) {
tparams[i].start = tparams[i - 1].end;
tparams[i].end = (i + 1) * step;
}
tparams[nthreads - 1].start = tparams[nthreads - 2].end;
tparams[nthreads - 1].end = ntasks;
}
return (tparams);
}
/**
* Converts a list of comma separated IP address strings into an array of strings,
* containing 1 IP address per entry.
* @param iplist [in] a string containing a list of IP addresses
* @param outiparr [out] array of strings, each string representing an IP address.
* @param max_outiparr [out] number of entries in the array.
* @return 0 on success. 1 otherwise.
* @note Caller responsible to release memory allocated for outiparr.
*/
int iplist_split(char *iplist, char ***outiparr, int *max_outiparr) {
char *list = NULL;
char *list_iter = NULL;
char *tok = NULL;
char **arr = NULL;
int max_arr = 0;
if (iplist == NULL) {
return (1);
}
if (!outiparr || !max_outiparr || !strlen(iplist)) {
return (1);
}
list = strdup(iplist);
list_iter = list;
while ((tok = strchr(list_iter, ','))) {
*tok = '\0';
if (strlen(list_iter)) {
arr = EUCA_REALLOC_C(arr, max_arr + 1, sizeof (char *));
arr[max_arr] = strdup(list_iter);
max_arr++;
}
list_iter = tok + 1;
}
if (strlen(list_iter)) {
arr = EUCA_REALLOC_C(arr, max_arr + 1, sizeof (char *));
arr[max_arr] = strdup(list_iter);
max_arr++;
}
*outiparr = arr;
*max_outiparr = max_arr;
EUCA_FREE(list);
return (0);
}
/**
* Releases memory allocated by iplist_split().
* @param iparr array of strings.
* @param max_iparr number of strings in the array.
* @return always 0.
*/
int iplist_arr_free(char **iparr, int max_iparr) {
if (!iparr || !max_iparr) {
return (0);
}
for (int i = 0; i < max_iparr; i++) {
EUCA_FREE(iparr[i]);
}
EUCA_FREE(iparr);
return (0);
}
/**
* Splits eucanetd VPCMIDO router names (in the form of name_rtid) into name and
* rtid parts.
* @param routername [in] name of eucanetd VPCMIDO router of interest
* @param name [out] string that represents the name of name_rtid. Memory allocated
* for name should be released by the caller.
* @param id [out] integer that represents the rtid of name_rtid
* @return 0 on success. 1 on failure.
*/
int routername_split(char *routername, char **name, int *id) {
if (!routername || !name || !id) {
return (1);
}
char *instr = strdup(routername);
char *rtid = NULL;
if (!instr) {
return (1);
}
for (int i = strlen(instr) - 2; i >= 0; i--) {
if (instr[i] == '_') {
instr[i] = '\0';
rtid = &(instr[i + 1]);
break;
}
}
if (rtid && strlen(instr) && strlen(rtid)) {
*name = strdup(instr);
*id = atoi(rtid);
}
EUCA_FREE(instr);
return (0);
}
/**
* Logs the information in the given midoname data structure.
* @param name [in] midoname data structure of interest.
*/
void mido_print_midoname(midoname *name) {
if (name == NULL) {
LOGWARN("Invalid argument: NULL midoname\n");
} else {
LOGTRACE("init=%d tenant=%s name=%s uuid=%s resource_type=%s media_type=%s\n",
name->init, SP(name->tenant), SP(name->name), SP(name->uuid), SP(name->resource_type),
SP(name->media_type));
}
}
/**
* Logs MidoNet-API information
*/
void mido_info_midonetapi(void) {
if (strlen(midonet_api_version) && strlen(midonet_api_uribase)) {
LOGINFO("\nmido API %s at %s\n", midonet_api_version, midonet_api_uribase);
}
}
/**
* Logs MIDOCACHE information.
*/
void mido_info_midocache(void) {
int routers = 0;
int rtports = 0;
int rtroutes = 0;
int bridges = 0;
int brports = 0;
int brdhcps = 0;
int brdhcphosts = 0;
int chains = 0;
int chrules = 0;
int ipaddrgroups = 0;
int ipagips = 0;
int hosts = 0;
int haddrs = 0;
int portgroups = 0;
int pgports = 0;
int tunnelzones = 0;
int tzhosts = 0;
if (midocache == NULL) {
return;
}
for (int i = 0; i < midocache->max_routers; i++) {
if (midocache->routers[i]) {
routers++;
for (int j = 0; j < midocache->routers[i]->max_ports; j++) {
if (midocache->routers[i]->ports[j]) {
rtports++;
}
}
for (int j = 0; j < midocache->routers[i]->max_routes; j++) {
if (midocache->routers[i]->routes[j]) {
rtroutes++;
}
}
}
}
for (int i = 0; i < midocache->max_bridges; i++) {
if (midocache->bridges[i]) {
bridges++;
for (int j = 0; j < midocache->bridges[i]->max_ports; j++) {
if (midocache->bridges[i]->ports[j]) {
brports++;
}
}
for (int j = 0; j < midocache->bridges[i]->max_dhcps; j++) {
if (midocache->bridges[i]->dhcps[j]) {
brdhcps++;
for (int k = 0; k < midocache->bridges[i]->dhcps[j]->max_dhcphosts; k++) {
if (midocache->bridges[i]->dhcps[j]->dhcphosts[k]) {
brdhcphosts++;
}
}
}
}
}
}
for (int i = 0; i < midocache->max_chains; i++) {
if (midocache->chains[i]) {
chains++;
for (int j = 0; j < midocache->chains[i]->max_rules; j++) {
if (midocache->chains[i]->rules[j]) {
chrules++;
}
}
}
}
for (int i = 0; i < midocache->max_ipaddrgroups; i++) {
if (midocache->ipaddrgroups[i]) {
ipaddrgroups++;
for (int j = 0; j < midocache->ipaddrgroups[i]->max_ips; j++) {
if (midocache->ipaddrgroups[i]->ips[j]) {
ipagips++;
}
}
}
}
for (int i = 0; i < midocache->max_hosts; i++) {
if (midocache->hosts[i]) {
hosts++;
if (midocache->hosts[i]->max_addresses) {
haddrs += midocache->hosts[i]->max_addresses;
}
}
}
for (int i = 0; i < midocache->max_portgroups; i++) {
if (midocache->portgroups[i]) {
portgroups++;
for (int j = 0; j < midocache->portgroups[i]->max_ports; j++) {
if (midocache->portgroups[i]->ports[j]) {
pgports++;
}
}
}
}
for (int i = 0; i < midocache->max_tunnelzones; i++) {
if (midocache->tunnelzones[i]) {
tunnelzones++;
for (int j = 0; j < midocache->tunnelzones[i]->max_hosts; j++) {
if (midocache->tunnelzones[i]->hosts[j]) {
tzhosts++;
}
}
}
}
LOGINFO("MIDOCACHE: mnbuffer %d allocated / %d released\n", midocache_midos->size, midocache_midos->released);
LOGINFO("\t%d routers (%d ports, %d routes)\n", routers, rtports, rtroutes);
LOGINFO("\t%d bridges (%d ports, %d dhcps, %d dhcphosts)\n", bridges, brports, brdhcps, brdhcphosts);
LOGINFO("\t%d chains (%d rules)\n", chains, chrules);
LOGINFO("\t%d ipags (%d ips)\n", ipaddrgroups, ipagips);
LOGINFO("\t%d hosts (%d addrs), %d portgroups (%d ports)\n", hosts, haddrs, portgroups, pgports);
LOGINFO("\t%d tunnelzones (%d hosts)\n", tunnelzones, tzhosts);
}
/**
* Logs the API HTTP request counts (diff from a previous call)
*
* @see mido_info_http_count_total() for cumulative count.
*/
void mido_info_http_count(void) {
LOGINFO("MidoNet API requests: %d gets, %d puts, %d posts, %d deletes\n",
http_gets - http_gets_prev, http_puts - http_puts_prev,
http_posts - http_posts_prev, http_deletes - http_deletes_prev);
http_gets_prev = http_gets;
http_puts_prev = http_puts;
http_posts_prev = http_posts;
http_deletes_prev = http_deletes;
}
/**
* Logs the API HTTP request counts (cumulative count)
*
* @see mido_info_http_count() for counts between calls.
*/
void mido_info_http_count_total(void)
{
LOGINFO("Total mido requests: %d gets, %d puts, %d posts, %d deletes\n",
http_gets, http_puts, http_posts, http_deletes);
long int getdiv = http_gets ? http_gets : (http_gets + 1);
long int putdiv = http_puts ? http_puts : (http_puts + 1);
long int postdiv = http_posts ? http_posts : (http_posts + 1);
long int deldiv = http_deletes ? http_deletes : (http_deletes + 1);
LOGINFO("\t%.2f ms/get, %.2f ms/put, %.2f ms/post, %.2f ms/del\n",
http_gets_time / getdiv / 1000.0, http_puts_time / putdiv / 1000.0,
http_posts_time / postdiv / 1000.0, http_deletes_time / deldiv / 1000.0);
}
/**
* Clears the mido_parsed_route structure in the argument. Allocated memory is released.
*
* @param route [in] parsed route entry of interest
*/
void mido_free_mido_parsed_route(mido_parsed_route *route) {
if (!route) {
return;
}
mido_free_midoname(&(route->router));
mido_free_midoname(&(route->rport));
EUCA_FREE(route->src_net);
EUCA_FREE(route->src_length);
EUCA_FREE(route->dst_net);
EUCA_FREE(route->dst_length);
EUCA_FREE(route->next_hop_ip);
EUCA_FREE(route->weight);
bzero(route, sizeof(mido_parsed_route));
}
/**
* Clears the list of mido_parsed_route structures in the argument.
*
* @param routes [in] array of mido_parsed_route structures
* @param max_routes [in] number of array elements
*/
void mido_free_mido_parsed_route_list(mido_parsed_route *routes, int max_routes) {
int i = 0;
if (!routes) return;
for (i = 0; i < max_routes; i++) {
mido_free_mido_parsed_route(&(routes[i]));
}
}
/**
* Releases memory allocated for the given array of midoname data structures
*
* @param name [in] array of midoname data structures of interest
* @param max_name [in] entries in the array
*/
void mido_free_midoname_list(midoname *name, int max_name) {
for (int i = 0; i < max_name; i++) {
mido_free_midoname(&(name[i]));
}
}
/**
* Releases memory allocated for the given midoname data structure.
* @param name [in] pointer to the midoname data structure of interest.
*/
void mido_free_midoname(midoname *name) {
if (!name) {
return;
}
EUCA_FREE(name->name);
EUCA_FREE(name->uuid);
EUCA_FREE(name->tenant);
EUCA_FREE(name->jsonbuf);
EUCA_FREE(name->resource_type);
EUCA_FREE(name->media_type);
EUCA_FREE(name->uri);
if (name->ipagip) {
EUCA_FREE(name->ipagip->ip);
EUCA_FREE(name->ipagip);
}
if (name->rule) {
EUCA_FREE(name->rule->type);
EUCA_FREE(name->rule->nwdstaddress);
EUCA_FREE(name->rule->nwdstlength);
EUCA_FREE(name->rule->nwsrcaddress);
EUCA_FREE(name->rule->nwsrclength);
EUCA_FREE(name->rule->nattarget);
EUCA_FREE(name->rule->jumpchainid);
EUCA_FREE(name->rule);
}
if (name->port) {
EUCA_FREE(name->port->type);
EUCA_FREE(name->port->hostid);
EUCA_FREE(name->port->peerid);
EUCA_FREE(name->port->ifname);
EUCA_FREE(name->port->netaddr);
EUCA_FREE(name->port->netlen);
EUCA_FREE(name->port->portaddr);
EUCA_FREE(name->port->portmac);
EUCA_FREE(name->port);
}
if (name->route) {
EUCA_FREE(name->route->srcnet);
EUCA_FREE(name->route->srclen);
EUCA_FREE(name->route->dstnet);
EUCA_FREE(name->route->dstlen);
EUCA_FREE(name->route->type);
EUCA_FREE(name->route->nexthopport);
EUCA_FREE(name->route->nexthopgateway);
EUCA_FREE(name->route->weight);
EUCA_FREE(name->route);
}
if (name->ip4mac) {
EUCA_FREE(name->ip4mac->ip);
EUCA_FREE(name->ip4mac->mac);
EUCA_FREE(name->ip4mac);
}
if (name->macport) {
EUCA_FREE(name->macport->macAddr);
EUCA_FREE(name->macport->portId);
EUCA_FREE(name->macport);
}
bzero(name, sizeof(midoname));
}
/**
* Retrieves an element that corresponds to the given key from name.
*
* @param name [in] midoname data structure from where the information will be extracted
* @param key [in] key of interest
* @param val [out] value of interest
*
* @return 0 on success. 1 on failure.
*/
int mido_getel_midoname(midoname *name, char *key, char **val) {
int ret = 0;
json_object *jobj = NULL;
if (!name || !key || !val || (!name->init)) {
return (1);
}
*val = NULL;
jobj = json_tokener_parse(name->jsonbuf);
if (jobj) {
json_object_object_foreach(jobj, elkey, elval) {
if (!*val && elkey && elval) {
if (!strcmp(elkey, key)) {
*val = strdup(SP(json_object_get_string(elval)));
}
}
}
json_object_put(jobj);
}
if (*val == NULL) {
ret = 1;
}
return (ret);
}
/**
* Parses an json array that is a value of the given key, and returns as an array
* of strings.
*
* @param name [in] midoname containing the jsonbuf of interest.
* @param key [in] json key of interest.
* @param values [out] array of parsed strings.
* @param max_values [out] number of elements in the returning array.
*
* @return 0 on success. 1 on failure.
*
* @note caller is responsible for releasing memory allocated for results.
*/
int mido_getarr_midoname(midoname *name, char *key, char ***values, int *max_values) {
int ret = 0;
json_object *jobj = NULL;
json_object *jarr = NULL;
json_object *jarrel = NULL;
int jarr_len = 0;
char **res;
if (!name || !key || !values || !max_values || (!name->init)) {
LOGWARN("Invalid argument: NULL pointer.\n");
return (1);
}
LOGEXTREME("searching for %s", key);
*values = NULL;
*max_values = 0;
jobj = json_tokener_parse(name->jsonbuf);
if (jobj) {
json_object_object_get_ex(jobj, key, &jarr);
if ((jarr == NULL) || (!json_object_is_type(jarr, json_type_array))) {
ret = 1;
} else {
jarr_len = json_object_array_length(jarr);
LOGEXTREME("\tfound %d\n", jarr_len);
if (jarr_len > 0) {
res = EUCA_ZALLOC_C(jarr_len, sizeof (char *));
for (int i = 0; i < jarr_len; i++) {
jarrel = json_object_array_get_idx(jarr, i);
res[i] = strdup(json_object_get_string(jarrel));
LOGEXTREME("\t%d %s\n", i, res[i]);
}
*values = res;
*max_values = jarr_len;
}
}
json_object_put(jobj);
}
return (ret);
}
/**
* Creates a tunnel-zone named name in MidoNet.
* @param name [in] name of the tunnel-zone to be created.
* @param type [in] type type of the tunnel-zone (gre|vxlan)
* @param outname [i/o] pointer to an extant MidoNet tunnel-zone (parameters will be checked
* to avoid duplicate resource creation. If outname points to NULL, a newly allocated
* midoname structure will be returned. If outname is NULL, the newly created resource
* will not be returned.
* @return 0 on success. 1 otherwise.
*/
midonet_api_tunnelzone *mido_create_tunnelzone(char *name, char *type, midoname **outname) {
int rc;
midoname myname;
midoname *out = NULL;
midonet_api_tunnelzone *tz = NULL;
if (outname && *outname) {
out = *outname;
}
if (out && out->init) {
if (!strcmp(name, out->name)) {
LOGEXTREME("%s already in mido - abort create\n", name);
return (midonet_api_cache_lookup_tunnelzone(out));
}
out = NULL;
} else {
midoname tmp;
tmp.name = strdup(name);
tz = midonet_api_cache_lookup_tunnelzone(&tmp);
EUCA_FREE(tmp.name);
if (tz) {
LOGEXTREME("%s already in mido - abort create\n", name);
if (outname) {
*outname = tz->obj;
}
return (tz);
}
}
memset(&myname, 0, sizeof(midoname));
myname.tenant = strdup(VPCMIDO_TENANT);
myname.name = strdup(name);
myname.resource_type = strdup("tunnel_zones");
myname.media_type = strdup(midonet_api_mtypes[APPLICATION_TUNNEL_ZONE_JSON]);
tz = NULL;
rc = mido_create_resource(NULL, 0, &myname, &out, "name", myname.name, "type", type, NULL);
if (rc == 0) {
// cache newly created tunnelzone
tz = midonet_api_cache_add_tunnelzone(out);
}
if (outname) {
*outname = out;
}
mido_free_midoname(&myname);
return (tz);
}
/**
* Retrieves an array of pointers to midonet object representing tunnel-zone.
* @param tenant [in] name of the MidoNet tenant.
* @param outnames [out] an array of pointers to midonet objects representing tunnel-zone, to be returned
* @param outnames_max [out] number of elements in the outnames array
* @return 0 on success. 1 otherwise.
*/
int mido_get_tunnelzones(char *tenant, midoname ***outnames, int *outnames_max) {
if (midocache != NULL) {
*outnames = NULL;
*outnames_max = 0;
midonet_api_tunnelzone **tzones = midocache->tunnelzones;
if ((tzones != NULL) && (midocache->max_tunnelzones > 0)) {
*outnames_max = midocache->max_tunnelzones;
*outnames = EUCA_ZALLOC_C(*outnames_max, sizeof (midoname *));
midoname **names = *outnames;
for (int i = 0; i < *outnames_max; i++) {
names[i] = tzones[i]->obj;
}
}
return (0);
}
return (mido_get_resources(NULL, 0, tenant, "tunnel_zones",
midonet_api_mtypes[APPLICATION_COLLECTION_TUNNEL_ZONE_JSON],
midonet_api_mtypes[APPLICATION_TUNNEL_ZONE_JSON],
outnames, outnames_max));
}
/**
* Retrieves an array of pointers to midonet object representing GRE tunnel-zone hosts.
* @param tenant [in] name of the MidoNet tenant.
* @param outnames [out] an array of pointers to midonet objects representing GRE tunnel-zone host, to be returned
* @param outnames_max [out] number of elements in the outnames array
* @return 0 on success. 1 otherwise.
*/
int mido_get_tunnelzone_hosts(midoname *tzone, midoname ***outnames, int *outnames_max) {
if (midocache != NULL) {
midonet_api_tunnelzone *tunnelzone = midonet_api_cache_lookup_tunnelzone(tzone);
if (tunnelzone == NULL) {
LOGWARN("Unable to find %s in midocache\n", tzone->name);
return (1);
}
*outnames_max = tunnelzone->max_hosts;
*outnames = EUCA_ZALLOC_C(*outnames_max, sizeof (midoname *));
midoname **names = *outnames;
for (int i = 0; i < *outnames_max; i++) {
names[i] = tunnelzone->hosts[i];
}
return (0);
}
return (mido_get_resources(tzone, 1, tzone->tenant, "hosts",
midonet_api_mtypes[APPLICATION_COLLECTION_TUNNEL_ZONE_HOST_JSON],
midonet_api_mtypes[APPLICATION_TUNNEL_ZONE_HOST_JSON],
outnames, outnames_max));
}
/**
* Creates a new tunnel-zone member as specified in the argument.
*
* @param tz [in] midonet_api_tunnelzone structure of interest. This parameter
* has priority over tunnelzone. If tz is NULL, tzmn is used to search
* midocache.
* @param tzmn [in] tunnel-zone of interest.
* @param host [in] host that will be added to the tunnel-zone.
* @param ip [in] IP address of the host to be used in the tunnel-zone.
* @param outname [i/o] pointer to an extant MidoNet member host (parameters will be checked
* to avoid duplicate host creation. If outname points to NULL, a newly allocated
* midoname structure representing the newly created host will be returned.
* If outname is NULL, the newly created object will not be returned.
* @return 0 if the host is successfully created/found. 1 otherwise.
*/
int mido_create_tunnelzone_member(midonet_api_tunnelzone *tz, midoname *tzmn,
midoname *host, char *ip, midoname **outname) {
int rc = 0, ret = 0, max_hosts = 0, found = 0;
midoname **hosts = NULL;
midoname myname = { 0 };
midoname *out = NULL;
char *hostId = NULL;
char *ipAddress = NULL;
if (!tz && !tzmn) {
LOGWARN("Invalid argument: cannot create member in a NULL tunnelzone.\n");
return (1);
}
midonet_api_tunnelzone *ctz = NULL;
if (tz != NULL) {
ctz = tz;
} else {
ctz = midonet_api_cache_lookup_tunnelzone(tzmn);
}
if (ctz == NULL) {
LOGWARN("Unable to find %s in midocache.\n", tzmn->name);
return (1);
} else {
hosts = ctz->hosts;
max_hosts = ctz->max_hosts;
}
if (outname && *outname) {
out = *outname;
}
if (out && out->init) {
mido_getel_midoname(out, "hostId", &hostId);
mido_getel_midoname(out, "ipAddress", &ipAddress);
if (hostId && ipAddress) {
LOGEXTREME("found %s %s\n", hostId, ipAddress);
if (!strcmp(hostId, host->uuid) && !strcmp(ipAddress, ip)) {
found = 1;
LOGTRACE("tz member already in mido - abort create\n");
}
}
EUCA_FREE(hostId);
EUCA_FREE(ipAddress);
}
if (!found) {
for (int i = 0; i < max_hosts && !found; i++) {
mido_getel_midoname(hosts[i], "hostId", &hostId);
mido_getel_midoname(hosts[i], "ipAddress", &ipAddress);
if (hostId && ipAddress) {
if (!strcmp(hostId, host->uuid) && !strcmp(ipAddress, ip)) {
found = 1;
if (outname) {
*outname = hosts[i];
}
LOGTRACE("tz member already in mido - abort create\n");
}
}
EUCA_FREE(hostId);
EUCA_FREE(ipAddress);
}
}
if (!found) {
memset(&myname, 0, sizeof (midoname));
myname.tenant = strdup(ctz->obj->tenant);
myname.resource_type = strdup("hosts");
myname.media_type = strdup(midonet_api_mtypes[APPLICATION_TUNNEL_ZONE_HOST_JSON]);
LOGTRACE("\tadding %s/%s to %s\n", host->name, ip, ctz->obj->name);
rc = mido_create_resource(ctz->obj, 1, &myname, &out, "ipAddress", ip, "hostId", host->uuid, NULL);
if (rc == 0) {
if (outname) {
*outname = out;
}
midonet_api_cache_add_tunnelzone_host(ctz, out);
ret = 0;
} else if (rc < 0) {
ret = 0;
} else {
ret = 1;
}
mido_free_midoname(&myname);
}
return (ret);
}
/**
* Creates a router in MidoNet.
* @param tenant [in] name of the MidoNet tenant.
* @param name [in] name of the router to be created.
* @param outname [i/o] pointer to an extant MidoNet router (parameters will be checked
* to avoid duplicate router creation. If outname points to NULL, a newly allocated
* midoname structure will be returned. If outname is NULL, the newly created router
* will not be returned.
* @return Pointer to the newly created router. NULL otherwise.
*/
midonet_api_router *mido_create_router(char *tenant, char *name, midoname **outname) {
int rc = 0;
midoname myname;
midoname *out = NULL;
midonet_api_router *rt = NULL;
if (outname && *outname) {
out = *outname;
}
if (out && out->init) {
if (!strcmp(name, out->name)) {
LOGEXTREME("%s already in mido - abort create\n", name);
return (midonet_api_cache_lookup_router(out, NULL));
}
out = NULL;
} else {
midoname tmp;
tmp.name = strdup(name);
rt = midonet_api_cache_lookup_router(&tmp, NULL);
EUCA_FREE(tmp.name);
if (rt) {
LOGEXTREME("%s already in mido - abort create\n", name);
if (outname) {
*outname = rt->obj;
}
return (rt);
}
}
bzero(&myname, sizeof(midoname));
myname.tenant = strdup(tenant);
myname.name = strdup(name);
myname.resource_type = strdup("routers");
myname.media_type = strdup(midonet_api_mtypes[APPLICATION_ROUTER_JSON]);
rt = NULL;
rc = mido_create_resource(NULL, 0, &myname, &out, "name", myname.name, NULL);
if (rc == 0) {
// cache newly created router
rt = midonet_api_cache_add_router(out);
}
if (outname) {
*outname = out;
}
mido_free_midoname(&myname);
return (rt);
}
/**
* Updates information about a router with the parameters in the variable argument section.
* @param [in] name midoname structure of the router of interest (checks are not performed)
* @param [in] ... variable argument section (key-value pairs)
* @return 0 on success. -1 if update is not needed (all parameters are already in
* place). 1 on failure.
*/
int mido_update_router(midoname *name, ...) {
va_list al = { {0} };
int ret = 0;
va_start(al, name);
ret = mido_update_resource(name, &al);
va_end(al);
return (ret);
}
/**
* Logs the information about a router represented by name.
* @param name [in] MN router of interest
* @return 0 on success. 1 on failure.
*/
int mido_print_router(midoname *name) {
return (mido_print_resource("routers", name));
}
/**
* Deletes the given router from MidoNet.
* @param name [in] router (assumed without check) of interest.
* @return 0 on success. 1 otherwise.
*/
int mido_delete_router(midoname *name) {
int rc = 0;
if (!name || !name->name) {
return (1);
}
midonet_api_cache_del_router(name);
rc = mido_delete_resource(NULL, name);
return (rc);
}
/**
* Creates a bridge in MidoNet.
* @param tenant [in] name of the MidoNet tenant.
* @param name [in] name of the bridge to be created.
* @param outname [i/o] pointer to an extant MidoNet bridge (parameters will be checked
* to avoid duplicate bridge creation. If outname points to NULL, a newly allocated
* midoname structure will be returned. If outname is NULL, the newly created bridge
* will not be returned.
* @return 0 on success. 1 otherwise.
*/
midonet_api_bridge *mido_create_bridge(char *tenant, char *name, midoname **outname) {
int rc;
midoname myname;
midoname *out = NULL;
midonet_api_bridge *br = NULL;
if (outname && *outname) {
out = *outname;
}
if (out && out->init) {
if (!strcmp(name, out->name)) {
LOGEXTREME("%s already in mido - abort create\n", name);
return (midonet_api_cache_lookup_bridge(out, NULL));
}
out = NULL;
} else {
midoname tmp;
tmp.name = strdup(name);
br = midonet_api_cache_lookup_bridge(&tmp, NULL);
EUCA_FREE(tmp.name);
if (br) {
LOGEXTREME("%s already in mido - abort create\n", name);
if (outname) {
*outname = br->obj;
}
return (br);
}