This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
data.c
1255 lines (1117 loc) · 41 KB
/
data.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 2009-2013 Eucalyptus Systems, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* 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. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/
//!
//! @file util/data.c
//! Need to provide description
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU
#include <string.h>
#include <assert.h>
#include "data.h"
#include "euca_string.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
//! List of string to convert the hypervisor capability types enumeration
const char *hypervisorCapabilityTypeNames[] = {
"unknown",
"xen",
"hw",
"xen+hw",
NULL,
};
//! List of string to convert the LIBVIRT device type enumeration
const char *libvirtDevTypeNames[] = {
"disk",
"floppy",
"cdrom",
NULL,
};
//! List of string to convert the LIBVIRT bus types enumeration
const char *libvirtBusTypeNames[] = {
"ide",
"scsi",
"virtio",
"xen",
NULL,
};
//! List of string to convert the LIBVIRT source types enumeration
const char *libvirtSourceTypeNames[] = {
"file",
"block",
NULL,
};
//! List of string to convert the LIBVIRT NIC types enumeration
const char *libvirtNicTypeNames[] = {
"none",
"e1000",
"rtl8139",
"virtio",
NULL,
};
//! List of string to convert the NC resource types enumeration
const char *ncResourceTypeNames[] = {
"image",
"ramdisk",
"kernel",
"ephemeral",
"swap",
"ebs",
"boot",
NULL,
};
//! List of strings that match ncResourceLocationType enums, for XML encoding
const char *ncResourceLocationTypeNames[] = {
"url",
"objectstorage",
"clc",
"sc",
"none",
NULL,
};
//! List of strings that match ncResourceFormatType enums, for XML encoding
const char *ncResourceFormatTypeNames[] = {
"none",
"ext2",
"ext3",
"ntfs",
"swap",
NULL,
};
//! String value of each instance state enumeration entry
const char *instance_state_names[] = {
"Unknown",
"Running",
"Waiting",
"Paused",
"Shutdown",
"Shutoff",
"Crashed",
"Staging",
"Booting",
"Canceled",
"Bundling-Shutdown",
"Bundling-Shutoff",
"CreateImage-Shutdown",
"CreateImage-Shutoff",
"Pending",
"Extant",
"Teardown",
NULL,
};
//! String value of each bundling progress state enumeration entry
const char *bundling_progress_names[] = {
"none",
"bundling",
"succeeded",
"failed",
"cancelled",
NULL,
};
//! String value of each create image progress state enumeration entry
const char *createImage_progress_names[] = {
"none",
"creating",
"succeeded",
"failed",
"cancelled",
NULL,
};
//! String value of each migrate-related state enumeration entry
const char *migration_state_names[] = {
"none",
"preparing",
"ready",
"migrating",
"cleaning",
NULL,
};
//! String value of each error enumeration entry
const char *euca_error_names[] = {
"ok",
"operation error",
"fatal operation error",
"entry not found",
"memory failure",
"I/O error",
"hypervisor error",
"thread error",
"duplicate entry error",
"invalid arguments",
"overflow error",
"operation unsupported error",
"operation not permitted error",
"access denied error",
"no space available error",
"timeout error",
"unknown",
NULL,
};
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
static ncVolume *find_volume(ncInstance * pInstance, const char *sVolumeId);
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
//!
//! Initialize a virtual machine structure from another
//!
//! @param[out] pVirtMachineOut a pointer to the resulting virtual machine structure
//! @param[in] pVirtMachingIn a pointer to the virtual machine structure to duplicate
//!
//! @return EUCA_OK on success or EUCA_ERROR on failure.
//!
//! @pre Both \p pVirtMachineOut and \p pVirtMachineIn fields must not be NULL
//!
//! @post The \p pVirtMachineOut structure is set with the values from \p pVirtMachineIn
//!
int allocate_virtualMachine(virtualMachine * pVirtMachineOut, const virtualMachine * pVirtMachingIn)
{
u32 i = 0;
virtualBootRecord *pVbrOut = NULL;
const virtualBootRecord *pVbrIn = NULL;
if ((pVirtMachineOut != NULL) && (pVirtMachingIn != NULL)) {
//
// Initialize the outgoing virtual machine with the incoming
//
pVirtMachineOut->mem = pVirtMachingIn->mem;
pVirtMachineOut->disk = pVirtMachingIn->disk;
pVirtMachineOut->cores = pVirtMachingIn->cores;
snprintf(pVirtMachineOut->name, 64, "%s", pVirtMachingIn->name);
//! @todo dan ask dmitrii
for (i = 0; ((i < EUCA_MAX_VBRS) && (i < pVirtMachingIn->virtualBootRecordLen)); i++) {
pVbrOut = pVirtMachineOut->virtualBootRecord + i;
pVbrIn = pVirtMachingIn->virtualBootRecord + i;
//
// Initialize the outgoing virtual bood record with the incoming.
//
strncpy(pVbrOut->resourceLocation, pVbrIn->resourceLocation, sizeof(pVbrOut->resourceLocation));
strncpy(pVbrOut->guestDeviceName, pVbrIn->guestDeviceName, sizeof(pVbrOut->guestDeviceName));
strncpy(pVbrOut->id, pVbrIn->id, sizeof(pVbrOut->id));
strncpy(pVbrOut->typeName, pVbrIn->typeName, sizeof(pVbrOut->typeName));
pVbrOut->sizeBytes = pVbrIn->sizeBytes;
strncpy(pVbrOut->formatName, pVbrIn->formatName, sizeof(pVbrOut->formatName));
}
return (EUCA_OK);
}
return (EUCA_ERROR);
}
//!
//! Initialize a network configuration structure with given values.
//!
//! @param[out] pNetCfg a pointer to the resulting network configuration structure
//! @param[in] sPvMac the private MAC string
//! @param[in] sPvIp the private IP string
//! @param[in] sPbIp the public IP string
//! @param[in] vlan the network Virtual LAN
//! @param[in] networkIndex the network index
//!
//! @return EUCA_OK on success or EUCA_ERROR on failure.
//!
//! @pre The \p pNetCfg field must not be NULL.
//!
//! @post The network configuration structure is updated with the provided information
//!
int allocate_netConfig(netConfig * pNetCfg, const char *sPvMac, const char *sPvIp, const char *sPbIp, int vlan, int networkIndex)
{
// make sure our netconfig parameter isn't NULL
if (pNetCfg != NULL) {
if (sPvMac)
euca_strncpy(pNetCfg->privateMac, sPvMac, MAC_BUFFER_SIZE);
if (sPvIp)
euca_strncpy(pNetCfg->privateIp, sPvIp, IP_BUFFER_SIZE);
if (sPbIp)
euca_strncpy(pNetCfg->publicIp, sPbIp, IP_BUFFER_SIZE);
pNetCfg->networkIndex = networkIndex;
pNetCfg->vlan = vlan;
return (EUCA_OK);
}
return (EUCA_ERROR);
}
//!
//! Allocate a metadata structure and initialize it. Metadata is present in every type of nc request
//!
//! @param[in] sCorrelationId the correlation identifier string
//! @param[in] sUserId the user identifier
//!
//! @return a pointer to the newly allocated metadata structure or NULL if any error occured.
//!
//! @see free_metadata()
//!
//! @post A metadata structure is allocated and initialized with the provided information
//!
//! @note Caller is responsible for freeing the allocated memory using free_metadata() call.
//!
ncMetadata *allocate_metadata(const char *sCorrelationId, const char *sUserId)
{
ncMetadata *pMeta;
// Try to allocate the structure
if ((pMeta = EUCA_ZALLOC(1, sizeof(ncMetadata))) == NULL)
return (NULL);
//
// Initialize with the provided information
//
pMeta->correlationId = ((sCorrelationId != NULL) ? strdup(sCorrelationId) : NULL);
pMeta->userId = ((sUserId != NULL) ? strdup(sUserId) : NULL);
return (pMeta);
}
//!
//! Frees an allocated metadata structure.
//!
//! @param[in,out] ppMeta a pointer to the node controller (NC) metadata structure
//!
//! @see allocate_metadata()
//!
//! @pre The \p ppMeta field should not be NULL
//!
//! @post If the metadata pointer is valid, the structure is freed and \p (*ppMeta) will be set to NULL.
//!
void free_metadata(ncMetadata ** ppMeta)
{
ncMetadata *pMeta = NULL;
if (ppMeta != NULL) {
if ((pMeta = (*ppMeta)) != NULL) {
EUCA_FREE(pMeta->correlationId);
EUCA_FREE(pMeta->userId);
EUCA_FREE(pMeta);
*ppMeta = NULL;
}
}
}
//!
//! Allocate and initialize an instance structure with given information. Instances are
//! present in instance-related requests.
//!
//! @param[in] sUUID the unique user identifier string
//! @param[in] sInstanceId the instance identifier string (i-XXXXXXXX)
//! @param[in] sReservationId the reservation identifier string
//! @param[in] pVirtMachine a pointer to our virtual machine parametes
//! @param[in] sStateName the current instance state name string
//! @param[in] stateCode the current instance state code
//! @param[in] sUserId the user identifier string
//! @param[in] sOwnerId the owner identifier string
//! @param[in] sAccountId the account identifier string
//! @param[in] pNetCfg a pointer to the network configuration of this instance
//! @param[in] sKeyName the SSH key name to use
//! @param[in] sUserData user data string to pass to the instance
//! @param[in] sLaunchIndex the instance's launch index
//! @param[in] sPlatform the instance's platform type
//! @param[in] expiryTime the instance's expiration time before it reaches running
//! @param[in] asGroupNames an array list of group name string
//! @param[in] groupNamesSize the number of group name in the groupNames list
//!
//! @return a pointer to the newly allocated instance structure or NULL if any error occured.
//!
//! @see add_instance()
//! @see free_instance()
//!
//! @post On succes an instance structure is allocated and initialized with the given information.
//!
ncInstance *allocate_instance(const char *sUUID, const char *sInstanceId, const char *sReservationId, virtualMachine * pVirtMachine,
const char *sStateName, int stateCode, const char *sUserId, const char *sOwnerId, const char *sAccountId,
netConfig * pNetCfg, const char *sKeyName, const char *sUserData, const char *sLaunchIndex, const char *sPlatform,
int expiryTime, char **asGroupNames, int groupNamesSize)
{
u32 i = 0;
ncInstance *pInstance = NULL;
/* zeroed out for cleaner-looking checkpoints and strings that are empty unless set */
if ((pInstance = EUCA_ZALLOC(1, sizeof(ncInstance))) == NULL)
return (NULL);
if (sUserData)
euca_strncpy(pInstance->userData, sUserData, CHAR_BUFFER_SIZE * 32);
if (sLaunchIndex)
euca_strncpy(pInstance->launchIndex, sLaunchIndex, CHAR_BUFFER_SIZE);
if (sPlatform)
euca_strncpy(pInstance->platform, sPlatform, CHAR_BUFFER_SIZE);
pInstance->groupNamesSize = groupNamesSize;
if ((asGroupNames != NULL) && (groupNamesSize > 0)) {
for (i = 0; i < groupNamesSize && asGroupNames[i]; i++)
euca_strncpy(pInstance->groupNames[i], asGroupNames[i], CHAR_BUFFER_SIZE);
}
if (pNetCfg != NULL)
memcpy(&(pInstance->ncnet), pNetCfg, sizeof(netConfig));
if (sUUID)
euca_strncpy(pInstance->uuid, sUUID, CHAR_BUFFER_SIZE);
if (sInstanceId)
euca_strncpy(pInstance->instanceId, sInstanceId, CHAR_BUFFER_SIZE);
if (sKeyName)
euca_strncpy(pInstance->keyName, sKeyName, CHAR_BUFFER_SIZE * 4);
if (sReservationId)
euca_strncpy(pInstance->reservationId, sReservationId, CHAR_BUFFER_SIZE);
if (sStateName)
euca_strncpy(pInstance->stateName, sStateName, CHAR_BUFFER_SIZE);
if (sUserId)
euca_strncpy(pInstance->userId, sUserId, CHAR_BUFFER_SIZE);
if (sOwnerId)
euca_strncpy(pInstance->ownerId, sOwnerId, CHAR_BUFFER_SIZE);
if (sAccountId)
euca_strncpy(pInstance->accountId, sAccountId, CHAR_BUFFER_SIZE);
if (pVirtMachine)
memcpy(&(pInstance->params), pVirtMachine, sizeof(virtualMachine));
pInstance->stateCode = stateCode;
euca_strncpy(pInstance->bundleTaskStateName, bundling_progress_names[NOT_BUNDLING], CHAR_BUFFER_SIZE);
pInstance->expiryTime = expiryTime;
return (pInstance);
}
//!
//! Clones an existing instance structure
//!
//! @param[in] old_instance a pointer to the instance to duplicate
//!
//! @return A clone of the existing instance of NULL on failure
//!
//! @see free_instance(), allocate_instance()
//!
//! @pre The \p old_instance field must not be NULL
//!
//! @post A clone of our existing instance is created.
//!
//! @note The caller is responsible to free the allocated instance using the free_instance() API.
//!
ncInstance *clone_instance(const ncInstance * old_instance)
{
ncInstance *new_instance;
// zeroed out for cleaner-looking checkpoints and strings that are empty unless set
if ((new_instance = EUCA_ZALLOC(1, sizeof(ncInstance))) == NULL)
return (NULL);
//! @TODO do not just copy everything
memcpy(new_instance, old_instance, sizeof(ncInstance));
return new_instance;
}
//!
//! Frees an allocated instance structure.
//!
//! @param[in,out] ppInstance a pointer to the instance structure pointer to free
//!
//! @see remove_instance()
//! @see allocate_instance()
//!
//! @pre \li The \p ppInstance field should not be NULL
//! \li The instance should have been removed from any list using remove_instance()
//!
//! @post The instance is freed and the value pointed by \p ppInstance is set to NULL.
//!
void free_instance(ncInstance ** ppInstance)
{
if (ppInstance != NULL) {
EUCA_FREE((*ppInstance));
}
}
//!
//! Adds an instance to an instance linked list
//!
//! @param[in,out] ppHead a pointer to the pointer to the head of the list
//! @param[in] pInstance a pointer to the instance to add to the list
//!
//! @return EUCA_OK on success or the following error code:
//! \li EUCA_MEMORY_ERROR: if we fail to allocate memory
//! \li EUCA_INVALID_ERROR: if any of our parameter does not meet the pre-condition
//! \li EUCA_DUPLICATE_ERROR: if the instance is already part of this list
//!
//! @pre \li Both \p ppHead and \p pInstance field must not be NULL.
//! \li The instance must not be part of the list
//!
//! @post The instance is added to the list. If this is the first instance in the list,
//! the \p ppHead value is updated to point to this instance.
//!
int add_instance(bunchOfInstances ** ppHead, ncInstance * pInstance)
{
bunchOfInstances *pNew = NULL;
bunchOfInstances *pLast = NULL;
bunchOfInstances *pNext = NULL;
// Make sure our paramters are valid
if ((ppHead == NULL) || (pInstance == NULL))
return (EUCA_INVALID_ERROR);
// Try to allocate memory for our instance list node
if ((pNew = EUCA_ZALLOC(1, sizeof(bunchOfInstances))) == NULL)
return (EUCA_MEMORY_ERROR);
// Initialize our node
pNew->instance = pInstance;
pNew->next = NULL;
// Are we the first item in this list?
if (*ppHead == NULL) {
*ppHead = pNew;
(*ppHead)->count = 1;
} else {
pNext = *ppHead;
//
// Process the list to make sure we're not trying to add a duplicate
//
do {
pLast = pNext;
if (!strcmp(pLast->instance->instanceId, pInstance->instanceId)) {
EUCA_FREE(pNew);
return (EUCA_DUPLICATE_ERROR);
}
pNext = pLast->next;
} while (pLast->next);
// We're at the end so add it there.
pLast->next = pNew;
(*ppHead)->count++;
}
return (EUCA_OK);
}
//!
//! Removes an instance from an instance linked list
//!
//! @param[in,out] ppHead a pointer to the pointer to the head of the list
//! @param[in] pInstance a pointer to the instance to remove from the list
//!
//! @return EUCA_OK on success or the following error code:
//! \li EUCA_INVALID_ERROR: if any of our parameters do not meet the pre-conditions
//! \li EUCA_NOT_FOUND_ERROR: if the instance is not part of this list
//!
//! @pre \li Both \p ppHead and \p pInstance field must not be NULL
//! \li The instance must exist in this list
//!
//! @post The instance is removed from the list. If this instance was the head of the list,
//! the \p ppHead field will be updated to point to the new head (next instance in list
//! from previous head).
//!
int remove_instance(bunchOfInstances ** ppHead, ncInstance * pInstance)
{
u32 count = 0;
bunchOfInstances *pHead = NULL;
bunchOfInstances *pPrevious = NULL;
// Make sure our parameters are valid
if (ppHead && pInstance) {
for (pHead = *ppHead; pHead; pPrevious = pHead, pHead = pHead->next) {
count = (*ppHead)->count;
if (!strcmp(pHead->instance->instanceId, pInstance->instanceId)) {
if (pPrevious) {
pPrevious->next = pHead->next;
} else {
*ppHead = pHead->next;
}
if (*ppHead) {
(*ppHead)->count = count - 1;
}
EUCA_FREE(pHead);
return (EUCA_OK);
}
}
return (EUCA_NOT_FOUND_ERROR);
}
return (EUCA_INVALID_ERROR);
}
//!
//! Helper to do something on each instance of a given list
//!
//! @param[in] ppHead a pointer to the pointer to the head of the list
//! @param[in] pFunction a pointer to the function to execute on each node
//! @param[in] pParam a transparent pointer to provide to pFunction
//!
//! @return EUCA_OK on success or the following error code:
//! \li EUCA_INVALID_ERROR: if any of our parameters do not meet the pre-conditions
//!
//! @pre Both \p ppHead and \p pFunction fields must not be NULL
//!
//! @post The function \p pFunction is applied to each member of the instance list.
//!
int for_each_instance(bunchOfInstances ** ppHead, void (*pFunction) (bunchOfInstances **, ncInstance *, void *), void *pParam)
{
bunchOfInstances *pHead = NULL;
// Make sure our parameters aren't NULL
if (ppHead && pFunction) {
for (pHead = *ppHead; pHead; pHead = pHead->next) {
pFunction(ppHead, pHead->instance, pParam);
}
return (EUCA_OK);
}
return (EUCA_INVALID_ERROR);
}
//!
//! Finds an instance in a given list based on the given instance identifier
//!
//! @param[in] ppHead a pointer to the pointer to the head of the list
//! @param[in] sInstanceId the instance identifier string (i-XXXXXXXX)
//!
//! @return a pointer to the instance if found. Otherwise, NULL is returned.
//!
//! @pre Both \p ppHead and \p sInstanceId must not be NULL.
//!
ncInstance *find_instance(bunchOfInstances ** ppHead, const char *sInstanceId)
{
bunchOfInstances *pHead = NULL;
// Make sure our parameters aren't NULL
if (ppHead && sInstanceId) {
for (pHead = *ppHead; pHead; pHead = pHead->next) {
if (!strcmp(pHead->instance->instanceId, sInstanceId)) {
return (pHead->instance);
}
}
}
return (NULL);
}
//!
//! Retrieves the next instance in the list
//!
//! @param[in] ppHead a pointer to the pointer to the head of the list
//!
//! @return a pointer ot the next instance in the list or NULL if no list is set
//!
//! @pre The \p ppHead field must not be NULL if the static list pointer is NULL.
//!
ncInstance *get_instance(bunchOfInstances ** ppHead)
{
static bunchOfInstances *pCurrent = NULL;
// advance static variable, wrapping to head if at the end
if (pCurrent == NULL)
pCurrent = ((ppHead == NULL) ? NULL : (*ppHead));
else
pCurrent = pCurrent->next;
// return the new value, if any
if (pCurrent == NULL)
return (NULL);
return (pCurrent->instance);
}
//!
//! Returns the number of instances assigned to a given instance list
//!
//! @param[in] ppHead a pointer to the pointer to the head of the list
//!
//! @return number of instances in the list. If \p ppHead is NULL or \p (*ppHead) is NULL, 0 will be returned.
//!
//! @pre The \p ppHead field must not be NULL
//!
int total_instances(bunchOfInstances ** ppHead)
{
if (ppHead) {
if (*ppHead)
return ((*ppHead)->count);
}
return (0);
}
//!
//! Allocate and initialize a resource structure with given information. Resource is
//! used to return information about resources
//!
//! @param[in] sNodeStatus the current node status string
//! @param[in] migrationCapable flag indicating whether node can participate in live migration
//! @param[in] sIQN
//! @param[in] memorySizeMax the maximum amount of memory available on this node
//! @param[in] memorySizeAvailable the current amount of memory available on this node
//! @param[in] diskSizeMax the maximum amount of disk space available on this node
//! @param[in] diskSizeAvailable the current amount of disk space available on this node
//! @param[in] numberOfCoresMax the maximum number of cores available on this node
//! @param[in] numberOfCoresAvailable the current number of cores available on this node
//! @param[in] sPublicSubnets the available public subnet for this node
//!
//! @return a pointer to the newly allocated resource structure or NULL if any error occured.
//!
//! @see free_resource()
//!
//! @pre The \p sNodeStatus field must not be NULL.
//!
//! @post On success, a resource structure is allocated and initialized with the given information
//!
//! @note Caller is responsible to free the allocated memory using the free_resource() function call.
//!
ncResource *allocate_resource(const char *sNodeStatus, boolean migrationCapable, const char *sIQN, int memorySizeMax, int memorySizeAvailable, int diskSizeMax,
int diskSizeAvailable, int numberOfCoresMax, int numberOfCoresAvailable, const char *sPublicSubnets)
{
ncResource *pResource = NULL;
// Make sure we have a valid parameter
if (sNodeStatus == NULL)
return (NULL);
// See if we can allocate our resource structure
if ((pResource = EUCA_ZALLOC(1, sizeof(ncResource))) == NULL)
return (NULL);
//
// Initialize the structure with the given values
//
euca_strncpy(pResource->nodeStatus, sNodeStatus, CHAR_BUFFER_SIZE);
if (sIQN)
euca_strncpy(pResource->iqn, sIQN, CHAR_BUFFER_SIZE);
pResource->migrationCapable = migrationCapable;
if (sPublicSubnets)
euca_strncpy(pResource->publicSubnets, sPublicSubnets, CHAR_BUFFER_SIZE);
pResource->memorySizeMax = memorySizeMax;
pResource->memorySizeAvailable = memorySizeAvailable;
pResource->diskSizeMax = diskSizeMax;
pResource->diskSizeAvailable = diskSizeAvailable;
pResource->numberOfCoresMax = numberOfCoresMax;
pResource->numberOfCoresAvailable = numberOfCoresAvailable;
return (pResource);
}
//!
//! Frees an allocated resource structure.
//!
//! @param[in,out] ppResource a pointer to the resource structure pointer to free
//!
//! @see allocate_resource()
//!
//! @pre The \p ppResource field should not be NULL
//!
//! @post The resource will be freed and \p (*ppResource) will be set to NULL.
//!
void free_resource(ncResource ** ppResource)
{
if (ppResource != NULL) {
EUCA_FREE((*ppResource));
}
}
//!
//! Finds a matching volume OR returns a pointer to the next empty/avail volume slot
//! OR if full, returns NULL.
//!
//! @param[in] pInstance a pointer to the instance structure the volume should be under
//! @param[in] sVolumeId the volume identifier string (vol-XXXXXXXX)
//!
//! @return a pointer to the matching volume OR returns a pointer to the next empty/avail
//! volume slot OR if full, returns NULL.
//!
//! @pre Both \p pInstance and \p sVolumeId fields must not be NULL
//!
//! @todo There's gotta be a way to improve and not scan the whole list all the time
//!
static ncVolume *find_volume(ncInstance * pInstance, const char *sVolumeId)
{
ncVolume *pVol = NULL;
ncVolume *pMatch = NULL;
ncVolume *pAvail = NULL;
ncVolume *pEmpty = NULL;
register u32 i = 0;
// Make sure our given parameters aren't NULL
if ((pInstance != NULL) && (sVolumeId != NULL)) {
for (i = 0, pVol = pInstance->volumes; i < EUCA_MAX_VOLUMES; i++, pVol++) {
// look for matches
if (!strncmp(pVol->volumeId, sVolumeId, CHAR_BUFFER_SIZE)) {
assert(pMatch == NULL);
pMatch = pVol;
}
// look for the first empty and available slot
if (!strnlen(pVol->volumeId, CHAR_BUFFER_SIZE)) {
if (pEmpty == NULL)
pEmpty = pVol;
} else if (!is_volume_used(pVol)) {
if (pAvail == NULL)
pAvail = pVol;
}
}
// Return match first if any are found
if (pMatch)
return (pMatch);
// then return the empty slot
if (pEmpty)
return (pEmpty);
// If nothing else, return the first available slot.
return (pAvail);
}
return (NULL);
}
//
//!
//! Checks wether or not a volume is in use
//!
//! @param[in] pVolume a pointer to the volume to validate
//!
//! @return FALSE if volume slot is not in use or if NULL and TRUE if it is in use
//!
//! @pre The \p pVol field must not be NULL.
//!
boolean is_volume_used(const ncVolume * pVolume)
{
if (pVolume != NULL) {
if (strlen(pVolume->stateName) == 0)
return (FALSE);
return (strcmp(pVolume->stateName, VOL_STATE_ATTACHING_FAILED) && strcmp(pVolume->stateName, VOL_STATE_DETACHED));
}
return (FALSE);
}
//!
//! Records volume's information in the instance struct, updating the non-NULL values if the record
//! already exists
//!
//! @param[in] pInstance a pointer to our instance containing the volume information to save
//! @param[in] sVolumeId the volume identifier string (vol-XXXXXXXX)
//! @param[in] sVolumeAttachmentToken the attachment token associated with this volume and attachment
//! @param[in] sConnectionString the connection string info specific to this host's volume attachment
//! @param[in] sLocalDev the local device name
//! @param[in] sLocalDevReal the local real device name
//! @param[in] sStateName the current volume state name
//!
//! @return a pointer to the volume if found. Otherwise NULL is returned.
//!
//! @pre \li Both \p pInstance and \p sVolumeId fields must not be NULL
//! \li A volume with \p sVolumeId for \p pInstance should exists
//! \li If such volume does not exists, we must have an empty slot in the volume list
//!
//! @post \li If any of \p pInstance or \p sVolumeId is NULL, the application will throw a SIGABRT signal
//! \li If the volume is found or if we have an empty slot, the volume information will be saved
//! \li If the volume is not found and if we do not have empty slot, NULL is returned and nothing is saved
//!
ncVolume *save_volume(ncInstance * pInstance, const char *sVolumeId, const char *sVolumeAttachmentToken, const char *sConnectionString, const char *sLocalDev,
const char *sLocalDevReal, const char *sStateName)
{
ncVolume *pVol = NULL;
// Make sure pInstance and sVolumeId aren't NULL
assert(pInstance != NULL);
assert(sVolumeId != NULL);
// Lookup for our device
if ((pVol = find_volume(pInstance, sVolumeId)) != NULL) {
//
// Save our volume information
//
euca_strncpy(pVol->volumeId, sVolumeId, CHAR_BUFFER_SIZE);
if (sVolumeAttachmentToken)
euca_strncpy(pVol->attachmentToken, sVolumeAttachmentToken, CHAR_BUFFER_SIZE);
if (sConnectionString)
euca_strncpy(pVol->connectionString, sConnectionString, VERY_BIG_CHAR_BUFFER_SIZE);
if (sLocalDev)
euca_strncpy(pVol->localDev, sLocalDev, CHAR_BUFFER_SIZE);
if (sLocalDevReal)
euca_strncpy(pVol->localDevReal, sLocalDevReal, CHAR_BUFFER_SIZE);
if (sStateName)
euca_strncpy(pVol->stateName, sStateName, CHAR_BUFFER_SIZE);
}
return (pVol);
}
//!
//! Zeroes out the volume's slot in the instance struct (no longer used)
//!
//! @param[in] pInstance a pointer to the instance to free a volume from
//! @param[in] sVolumeId the volume identifier string (vol-XXXXXXXX)