forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdev_handler.c
1896 lines (1705 loc) · 65.1 KB
/
dev_handler.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/dev_handler.c
//! Implements a network device handling API. Anything that relates to network devices
//! IP addresses, Ethernet addresses, VLANs, etc. should be covered in this API. All
//! function names MUST start with the 'dev_' string and be properly documented.
//!
//! Coding Standard:
//! Every function that has multiple words must follow the word1_word2_word3() naming
//! convention and variables must follow the 'word1Word2Word3()' convention were no
//! underscore is used and every word, except for the first one, starts with a capitalized
//! letter. Whenever possible (not mendatory but strongly encouraged), prefixing a variable
//! name with one or more of the following qualifier would help reading code:
//! - p - indicates a variable is a pointer (example: int *pAnIntegerPointer)
//! - s - indicates a string variable (examples: char sThisString[10], char *psAnotherString). When 's' is used on its own, this mean a static string.
//! - a - indicates an array of objects (example: int aAnArrayOfInteger[10])
//! - g - indicates a variable with global scope to the file or application (example: static eucanetdConfig gConfig)
//!
//! Any other function implemented must have its name start with "dev" followed by an underscore
//! and the rest of the function name with every words separated with an underscore character. For
//! example: dev_this_is_a_good_function_name().
//!
/*----------------------------------------------------------------------------*\
| |
| 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 <math.h>
#include <config.h>
#include <dirent.h>
#include <errno.h>
#include <netdb.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <linux/if_link.h>
#include <eucalyptus.h>
#include <misc.h>
#include <euca_string.h>
#include <euca_network.h>
#include <log.h>
#include <atomic_file.h>
#include "dev_handler.h"
#include "euca_gni.h"
#include "eucanetd.h"
#include "eucanetd_util.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
#define BRCTL_PATH "/usr/sbin/brctl"
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
//! Array of string mapping to the device types enumeration
const char *asDevTypeNames[] = {
"INVALID",
"INTERFACE",
"BRIDGE",
"TUNNEL",
"ANY",
};
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
//! API to force remove a bridge device
static int dev_remove_bridge_forced(dev_handler *devh, const char *psBridgeName);
static inline void dev_free_list(dev_entry **pList, int nbItems);
static inline void dev_free_ips(in_addr_entry **pList);
static inline void dev_in_addr_entry(in_addr_entry *pEntry, const char *psDeviceName, in_addr_t address, in_addr_t netmask);
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
/**
* Initialize the IP Set handler structure
*
* @param devh [in] pointer to the device handler structure
* @param cmdprefix [in] a string pointer to the prefix to use to run commands
*
* @return 0 on success. 1 on any failure.
*
* @pre
* - The devh pointer should not be NULL
* - We should be able to create temporary files on the system
*
* @post
* - If cmdprefix was provided, the table's cmdprefix field will be set with it
*
*/
int dev_handler_init(dev_handler *devh, const char *cmdprefix) {
if (!devh) {
LOGERROR("invalid argument: cannot initialize NULL dev_handler\n");
return (1);
}
memset(devh, 0, sizeof(dev_handler));
if (cmdprefix) {
snprintf(devh->cmdprefix, EUCA_MAX_PATH, "%s", cmdprefix);
} else {
devh->cmdprefix[0] = '\0';
}
devh->init = 1;
return (0);
}
/**
* Release resources of the given device handler and reinitializes the handler.
* @param devh [in] pointer to the device handler
* @return 0 on success. 1 on failure.
*/
int dev_handler_free(dev_handler *devh) {
char saved_cmdprefix[EUCA_MAX_PATH] = "";
if (!devh || !devh->init) {
return (1);
}
snprintf(saved_cmdprefix, EUCA_MAX_PATH, "%s", devh->cmdprefix);
devh->numberOfDevices = 0;
devh->numberOfNetworks = 0;
EUCA_FREE(devh->pDevices);
EUCA_FREE(devh->pNetworks);
return (dev_handler_init(devh, saved_cmdprefix));
}
/**
* Releases all resources of the given dev_handler.
* @param devh [in] pointer to the device handler
* @return 0 on success. 1 on failure.
*/
int dev_handler_close(dev_handler *devh) {
if (!devh || !devh->init) {
LOGDEBUG("Invalid argument. NULL or uninitialized dev_handler.\n");
return (1);
}
EUCA_FREE(devh->pDevices);
EUCA_FREE(devh->pNetworks);
memset(devh, 0, sizeof (dev_handler));
return (0);
}
/**
* Retrieves the current device state from the system.
* @param devh [in] pointer to device handler
* @return 0 on success. 1 on failure.
*/
int dev_handler_repopulate(dev_handler *devh) {
int rc = 0;
struct timeval tv = { 0 };
eucanetd_timer_usec(&tv);
if (!devh || !devh->init) {
return (1);
}
rc = dev_handler_free(devh);
if (rc) {
LOGERROR("could not reinitialize dev handler.\n");
return (1);
}
// Retrieve our system network device information
if ((rc = dev_get_list(devh, NULL, &devh->pDevices, &devh->numberOfDevices)) != 0) {
LOGERROR("Cannot retrieve system network device information.\n");
dev_handler_free(devh);
return (1);
}
// Retrieve our system network device information
if ((rc = dev_get_ips(NULL, &devh->pNetworks, &devh->numberOfNetworks)) != 0) {
LOGERROR("Cannot retrieve system network information.\n");
dev_handler_free(devh);
return (1);
}
LOGDEBUG("devices populated in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (0);
}
/**
* Retrieves a list of devices that support IP traffic. The caller can filter using the
* cpsSearch parameter. If cpsSearch is set to NULL or "*", the list isn't filtered. If
* cpsSearch is terminated with a "*" character, than any device name that starts with
* the string prepending the "*" will be in the final list and if there are no "*", then
* the list will only contain the interface with the given name.
*
* @param devh [in] pointer to the device handler
* @param cpsSearch [in] a constant string pointer to the filter. (see description)
* @param pDevices [in,out] a pointer to a list of string that will contain the device names
* @param pNbDevices [in,out] a pointer to the integer indicating how many devices we found
* @param deviceType [in] the device type to filter on. The values are define in the dev_type_t enum.
*
* @return 0 on success or 1 on failure
*
* @see dev_free_list()
*
* @pre
* Both ppsDevNames and pNumberOfDevices MUST not be NULL.
*
* @post
* On successful completion, ppsDevNames contains the list of device names found on the system for
* the given search criterias and pNumberOfDevices contains the number of elements in the list. On
* failure, both pNumberOfDevices and ppsDevNames are non-deterministic.
*
* @note
* Caller is responsible to free the dynamically allocated list of device name entries
* using the dev_free_list() API.
*/
int dev_get(dev_handler *devh, const char *cpsSearch, dev_entry **pDevices, int *pNbDevices, dev_type deviceType) {
int i = 0;
dev_entry *pPtr = NULL;
boolean found = FALSE;
struct ifaddrs *pIfa = NULL;
struct ifaddrs *pIfAddr = NULL;
// Make sure we have the proper pointers.
if ((pDevices == NULL) || (pNbDevices == NULL)) {
return (1);
}
// Set the list and number of items to NULL and 0
(*pDevices) = NULL;
(*pNbDevices) = 0;
// get the list of network devices
if (getifaddrs(&pIfAddr) == -1) {
LOGERROR("Failed to retrieve the list of network devices.\n");
return (1);
}
// Scan the list for AF_INET devices type
for (pIfa = pIfAddr; pIfa != NULL; pIfa = pIfa->ifa_next) {
// Validate the ifaddr
if (pIfa->ifa_addr == NULL)
continue;
// Only IP devices
if (pIfa->ifa_addr->sa_family != AF_PACKET)
continue;
// Check if we need to filter this name
if ((cpsSearch != NULL) && strcmp(cpsSearch, "*")) {
// Is this an exact match?
if (cpsSearch[strlen(cpsSearch) - 1] == '*') {
// if the name does not start with the search name then skip
if (strncmp(pIfa->ifa_name, cpsSearch, (strlen(cpsSearch) - 1))) {
continue;
}
} else {
// if the name does not match the search name then skip
if (strcmp(pIfa->ifa_name, cpsSearch)) {
continue;
}
}
}
// Check if we already have this name in the list
for (i = 0, found = FALSE; ((i < (*pNbDevices)) && !found); i++) {
if (!strcmp((*pDevices)[i].sDevName, pIfa->ifa_name)) {
found = TRUE;
}
}
// Did we find it?
if (found)
continue;
// Do we have to filter on type?
if (deviceType != DEV_TYPE_ANY) {
if (deviceType == DEV_TYPE_BRIDGE) {
// Skip if this is not a bridge device
if (!dev_is_bridge(pIfa->ifa_name))
continue;
} else if (deviceType == DEV_TYPE_TUNNEL) {
// Skip if this is not a tunnel device
if (!dev_is_tunnel(pIfa->ifa_name))
continue;
} else if (deviceType == DEV_TYPE_INTERFACE) {
// Skip if we are a bridge or a tunnel device
if (dev_is_bridge(pIfa->ifa_name) || dev_is_tunnel(pIfa->ifa_name))
continue;
}
}
// Alright, new one, allocate some memory
if ((pPtr = EUCA_REALLOC((*pDevices), ((*pNbDevices) + 1), sizeof(dev_entry))) == NULL) {
LOGERROR("Memory allocation failure.\n");
dev_free_list(pDevices, (*pNbDevices));
(*pNbDevices) = 0;
freeifaddrs(pIfAddr);
return (1);
}
// re-adjust out pointers
(*pDevices) = pPtr;
// Store the information we just got
snprintf((*pDevices)[(*pNbDevices)].sDevName, IF_NAME_LEN, "%s", pIfa->ifa_name);
snprintf((*pDevices)[(*pNbDevices)].sMacAddress, ENET_ADDR_LEN, "%s", dev_get_mac(pIfa->ifa_name));
(*pDevices)[(*pNbDevices)].isBridge = dev_is_bridge(pIfa->ifa_name);
(*pNbDevices)++;
}
freeifaddrs(pIfAddr);
return (0);
}
/**
* Checks whether or not a device exists.
*
* @param psDeviceName [in] a string pointer to the device name we are checking
*
* @return TRUE if the device exists otherwise FALSE is returned
*
* @pre
* psDeviceName MUST not be NULL and not empty
*/
boolean dev_exist(const char *psDeviceName) {
#define MAX_PATH_LEN 64
char sPath[MAX_PATH_LEN] = "";
// Make sure our given parameter is not NULL
if (!psDeviceName || (psDeviceName[0] == '\0'))
return (FALSE);
// Each device has its path under /sys/class/net/[device]/
snprintf(sPath, MAX_PATH_LEN, "/sys/class/net/%s/", psDeviceName);
// If the path is a directory, than its a valid device.
if (check_directory(sPath))
return (FALSE);
return (TRUE);
#undef MAX_PATH_LEN
}
/**
* Checks whether or not a given device is currently UP
*
* @param psDeviceName [in] a string pointer to the device name we are checking
*
* @return the return value description
*
* @see euca_strncpy(), euca_strdupcat()
*
* @pre
* - psDeviceName MUST not be NULL
* - psDeviceName must be a valid device
*/
boolean dev_is_up(const char *psDeviceName) {
#define MAX_PATH_LEN 64
#define OPERATING_STATE_LEN 32
char *p = NULL;
char sPath[EUCA_MAX_PATH] = "";
char sOperState[OPERATING_STATE_LEN] = "";
FILE *pFh = NULL;
boolean ret = FALSE;
// Make sure the given string isn't NULL
if (!psDeviceName)
return (FALSE);
// Each device has its path under /sys/class/net/[device]/
snprintf(sPath, MAX_PATH_LEN, "/sys/class/net/%s/operstate", psDeviceName);
// Open the operstate net file... If the device is invalid and does not exists
// then this will fail
if ((pFh = fopen(sPath, "r")) == NULL)
return (FALSE);
// Read the first line. We should get either up or down
if (fgets(sOperState, OPERATING_STATE_LEN, pFh)) {
// remove the '\n' character.
if ((p = strchr(sOperState, '\n')) != NULL)
*p = '\0';
// Does it say down? Up can also be considered unknown for bridge
if (strncmp(sOperState, "down", OPERATING_STATE_LEN)) {
ret = TRUE;
}
}
fclose(pFh);
return (ret);
#undef MAX_PATH_LEN
#undef OPERATING_STATE_LEN
}
/**
* Enables a network device.
*
* @param devh [in] pointer to the device handler
* @param psDeviceName [in] a string pointer to the device name to enable
*
* @return 0 on success or 1 if any failure occured
*
* @see dev_down(), dev_is_up()
*
* @pre
* The newtork device name must be valid and the network device must exists on this system
*
* @post
* On success, the device is enabled. If any failure occured, the device state will
* remain unchanged.
*/
int dev_up(dev_handler *devh, const char *psDeviceName) {
int rc = 0;
if (!devh) {
LOGWARN("Invalid argument: null device handler\n");
return (1);
}
// Make sure we have a valid device
if (!dev_exist(psDeviceName)) {
return (1);
}
// enable the device
if (euca_execlp(&rc, devh->cmdprefix, "ip", "link", "set", "dev", psDeviceName, "up", NULL) != EUCA_OK) {
LOGERROR("Fail to enable device '%s'. error=%d\n", psDeviceName, rc);
return (1);
}
return (0);
}
/**
* Disables a network device.
*
* @param devh [in] pointer to the device handler
* @param psDeviceName [in] a string pointer to the device name to disable
*
* @return 0 on success or 1 if any failure occured
*
* @see dev_up(), dev_is_up()
*
* @pre
* The newtork device name must be valid and the network device must exists on this system
*
* @post
* On success, the device is disabled. If any failure occured, the device state will
* remain unchanged.
*/
int dev_down(dev_handler *devh, const char *psDeviceName) {
int rc = 0;
if (!devh) {
LOGWARN("Invalid argument: null device handler\n");
return (1);
}
// Make sure we have a valid device
if (!dev_exist(psDeviceName)) {
return (1);
}
// disable the device
if (euca_execlp(&rc, devh->cmdprefix, "ip", "link", "set", "dev", psDeviceName, "down", NULL) != EUCA_OK) {
LOGERROR("Fail to enable device '%s'. error=%d\n", psDeviceName, rc);
return (1);
}
return (0);
}
/**
* Renames a device on the system. This will achieve the following tasks:
* - Check if the device is a valid device
* - Check to make sure the new name is valid
* - Make sure the new device name isn't already in use
* - ip link set dev [psDeviceName] down
* - ip link set dev [psDeviceName] name [psNewDevName]
* - ip link set dev [psNewDevName] up
*
* @param devh [in] pointer to the device handler
* @param psDeviceName [in] a constant string pointer to the device name we need to rename
* @param psNewDevName [in] a constant string pointer to the new device name
*
* @return TRUE if the VLAN is valid otherwise FALSE is returned
*
* @see dev_up(), dev_down(), dev_exist()
*
* @pre
* - Both string pointer must not be null
* - The psDeviceName must be the device name of an existing device on this system
* - The psDeviceName must be the device name of a non-existing device on this system
* - The psNewDevName must have at least one character
*
* @post
* - On success the device has been renamed
* - On failure the system state is left undetermined. Either the device has been renamed
* or not or the state of the device is up or down.
*
* @note
*/
int dev_rename(dev_handler *devh, const char *psDeviceName, const char *psNewDevName) {
int rc = 0;
// Make sure both pointers are valid and that the new name is of at least 1 character
if (!psDeviceName || !psNewDevName || (strlen(psNewDevName) == 0)) {
return (1);
}
if (!devh) {
LOGWARN("Invalid argument: null device handler\n");
return (1);
}
// Make sure the old device name exists on this system
if (!dev_exist(psDeviceName)) {
LOGERROR("Fail to rename network device '%s' to '%s'. Device not on this system!\n", psDeviceName, psNewDevName);
return (1);
}
// Make sure the new device name isn't in use
if (dev_exist(psNewDevName)) {
LOGERROR("Fail to rename network device '%s' to '%s'. Device name '%s' already in use!\n", psDeviceName, psNewDevName, psNewDevName);
return (1);
}
// Disable the device
if (dev_down(devh, psDeviceName) != 0) {
LOGERROR("Fail to rename network device '%s' to '%s'. Fail to disable '%s'!\n", psDeviceName, psNewDevName, psDeviceName);
return (1);
}
// disable the device
if (euca_execlp(&rc, devh->cmdprefix, "ip", "link", "set", "dev", psDeviceName, "name", psNewDevName, NULL) != EUCA_OK) {
LOGERROR("Fail to rename network device '%s' to '%s'. error=%d\n", psDeviceName, psNewDevName, rc);
return (1);
}
// Enable the device using the new name and just WARN on error
if (dev_up(devh, psNewDevName) != 0) {
LOGWARN("Fail to rename network device '%s' to '%s'. Fail to enable '%s'!\n", psDeviceName, psNewDevName, psNewDevName);
}
return (0);
}
/**
* Checks whether or not a given device is a bridge device
*
* @param psDeviceName [in] a string pointer to the device name we are checking
*
* @return TRUE if the device is a bridge device otherwise FALSE is returned
*
* @see
*
* @pre
* - psDeviceName MUST not be NULL
* - psDeviceName must be a valid device
*/
boolean dev_is_bridge(const char *psDeviceName) {
#define MAX_PATH_LEN 64
char sPath[MAX_PATH_LEN] = "";
// Make sure the given string isn't NULL
if (!psDeviceName)
return (FALSE);
// Each device has its path under /sys/class/net/[device]/
snprintf(sPath, MAX_PATH_LEN, "/sys/class/net/%s/bridge/", psDeviceName);
// If this device does not have a 'bridge' path, this isn't a bridge device
if (check_directory(sPath))
return (FALSE);
return (TRUE);
#undef MAX_PATH_LEN
}
/**
* Checks whether or not a given device is a bridged interface. If the bridge
* device name is provided, it will also check if the device is a member of
* the given bridge device.
*
* @param psDeviceName [in] a string pointer to the device name we are checking
* @param psBridgeName [in] an optional string pointer to the bridge device name
*
* @return TRUE if this is a bridge interface and, if the bridge name is provided,
* that the device is a member of the bridge. Otherwise FALSE is returned.
* @pre
* - psDeviceName MUST not be NULL
* - Both psDeviceName and psBridgeName must be valid devices
*/
boolean dev_is_bridge_interface(const char *psDeviceName, const char *psBridgeName) {
#define MAX_PATH_LEN 128
char sPath[MAX_PATH_LEN] = "";
// Make sure the given string isn't NULL
if (!psDeviceName)
return (FALSE);
// Each device has its path under /sys/class/net/[device]/
snprintf(sPath, MAX_PATH_LEN, "/sys/class/net/%s/brport/", psDeviceName);
// If this device does not have a 'brport' path, this isn't a bridge device
if (check_directory(sPath))
return (FALSE);
// Do we want to validate if we are part of a given bridge?
if (psBridgeName) {
// Each device has its path under /sys/class/net/[device]/
snprintf(sPath, MAX_PATH_LEN, "/sys/class/net/%s/brif/%s/", psBridgeName, psDeviceName);
// are we part of this bridge?
if (check_directory(sPath)) {
return (FALSE);
}
}
return (TRUE);
#undef MAX_PATH_LEN
}
/**
* Checks whether or not a given bridge device has associated interfaces.
*
* @param devh [in] pointer to the device handler
* @param psBridgeName [in] a constant string pointer to the bridge device name to validate
*
* @return TRUE if this bridge device has associated interface. Otherwise FALSE is returned.
*
* @see dev_is_bridge(), dev_get_bridge_interfaces()
*
* @pre
* - psBridgeName MUST not be NULL
* - psBridgeName must be a valid bridge device on this system
*/
boolean dev_has_bridge_interfaces(dev_handler *devh, const char *psBridgeName) {
int nbInterfaces = 0;
dev_entry *pInterfaces = NULL;
// Make sure we have a valid name
if (!psBridgeName)
return (FALSE);
//
// See if we have any associated interfaces. This will also validate
// psBridgeName to be a valid bridge device on this system
//
if (dev_get_bridge_interfaces(devh, psBridgeName, &pInterfaces, &nbInterfaces) != 0)
return (FALSE);
// Done with the interface list
dev_free_list(&pInterfaces, nbInterfaces);
return ((nbInterfaces > 0) ? TRUE : FALSE);
}
/**
* Retrieves the bridge device name for which the given psDeviceName is a member of
*
* @param devh [in] pointer to the device handler
* @param psDeviceName [in] a string pointer to the device name we are checking
*
* @return a pointer to the dynamically allocated string or NULL on failure.
*
* @see dev_is_bridge_interface()
*
* @pre
* - Both psDeviceName and psOutBridgeName MUST not be NULL
* - psDeviceName must be a valid device
*
* @post
* On success, the psOutBridgeName is set appropriately. On Failure, psOutBridgeName
* is set to NULL.
*
* @note
* Since the result is a dynamic string, the caller is responsible for freeing it.
*/
char *dev_get_interface_bridge(dev_handler *devh, const char *psDeviceName) {
#define INTFC_LINE_STRING "INTERFACE="
#define MAX_LINE_LEN 64
#define MAX_PATH_LEN 64
char *p = NULL;
char *psOutBridgeName = NULL;
char sLine[MAX_LINE_LEN] = "";
char sPath[MAX_PATH_LEN] = "";
FILE *pFh = NULL;
// Make sure the given string aren't NULL
if (!psDeviceName)
return (NULL);
// Is this a bridged interface?
if (!dev_is_bridge_interface(psDeviceName, NULL))
return (NULL);
// Each device has its path under /sys/class/net/[device]/
snprintf(sPath, MAX_PATH_LEN, "/sys/class/net/%s/brport/bridge/uevent", psDeviceName);
// If this device does not have a 'brpor/bridge/ueventt' file, this isn't a bridged device
if ((pFh = fopen(sPath, "r")) == NULL)
return (NULL);
// Read until we reach the line starting with 'INTERFACE='
while (fgets(sLine, MAX_LINE_LEN, pFh)) {
// remove the '\n' character.
if ((p = strchr(sLine, '\n')) != NULL)
*p = '\0';
// Is this the "INTERFACE" line
if (!strncmp(sLine, INTFC_LINE_STRING, strlen(INTFC_LINE_STRING))) {
// We got it, retrieve the bridge device name after the '=' character
p = sLine + strlen(INTFC_LINE_STRING);
psOutBridgeName = strdup(p);
break;
}
}
fclose(pFh);
return (psOutBridgeName);
#undef INTFC_LINE_STRING
#undef MAX_LINE_LEN
#undef MAX_PATH_LEN
}
/**
* Retrieves the list of assigned interfaces to a bridge device.
*
* @param devh [in] pointer to the device handler
* @param psBridgeName [in] a constant string pointer to the bridge device name
* @param pOutDevices [in,out] a pointer to our outgoing device structure
* @param pOutNbDevices [in,out] a pointer to the counter that will contain the number of devices found
*
* @return 0 on success or 1 if any failure occured
*
* @pre
* All of our pointers must not be NULL. The Bridge device must be a valid device and a bridge device.
*
* @post
* On success, the list is filled with the interfaces found. On failure, the list is empty.
*
* @note
* Since this list is dynamically allocated, the caller is responsible to free the list
*/
int dev_get_bridge_interfaces(dev_handler *devh, const char *psBridgeName, dev_entry **pOutDevices, int *pOutNbDevices) {
#define MAX_PATH_LEN 128
DIR *pDh = NULL;
char sBrIfPath[MAX_PATH_LEN] = "";
boolean done = FALSE;
dev_entry *pDevices = NULL;
struct dirent dent = { 0 };
struct dirent *pResult = NULL;
// Make sure the given pointers are valid
if (!psBridgeName || !pOutDevices || !pOutNbDevices)
return (1);
// Set our list and counter to NULL/0
(*pOutDevices) = NULL;
(*pOutNbDevices) = 0;
// The device must be a valid bridge
if (!dev_is_bridge(psBridgeName))
return (1);
// Our assigned interface are listed under /sys/class/net/[device]/brif/
snprintf(sBrIfPath, MAX_PATH_LEN, "/sys/class/net/%s/brif/", psBridgeName);
// Open the directory and scan it for our assigned device name
if ((pDh = opendir(sBrIfPath)) != NULL) {
while (!done && (readdir_r(pDh, &dent, &pResult) == 0)) {
// Make sure the given result is valid
if (pResult == NULL) {
done = TRUE;
continue;
}
// Skip the . and ..
if (strcmp(pResult->d_name, ".") && strcmp(pResult->d_name, "..")) {
// Reallocate the memory as we need
if ((pDevices = EUCA_REALLOC((*pOutDevices), ((*pOutNbDevices) + 1), sizeof(dev_entry))) != NULL) {
(*pOutDevices) = pDevices;
// Setup the structure
snprintf((*pOutDevices)[(*pOutNbDevices)].sDevName, IF_NAME_LEN, "%s", pResult->d_name);
snprintf((*pOutDevices)[(*pOutNbDevices)].sMacAddress, ENET_ADDR_LEN, "%s", dev_get_mac(pResult->d_name));
(*pOutDevices)[(*pOutNbDevices)].isBridge = 0;
(*pOutNbDevices)++;
} else {
LOGERROR("Out of memory!\n");
dev_free_list(pOutDevices, (*pOutNbDevices));
(*pOutDevices) = NULL;
(*pOutNbDevices) = 0;
done = TRUE;
}
}
}
closedir(pDh);
}
return (0);
#undef MAX_PATH_LEN
}
/**
* Sets the STP state on a given bridge device
*
* @param devh [in] pointer to the device handler
* @param psBridgeName [in] a constant string pointer to the bridge device name
* @param psStpState [in] a constant string pointer to the STP state. Must be either "on" or "off"
*
* @return 0 on success or 1 on failure
*
* @see dev_create_bridge()
*
* @pre
* - The psBridgeName parameter must not be NULL and if the device exists, it should be a valid bridge device
* - The psStpState must not be NULL and must be either "on" or "off"
*
* @post
* On success, the bridge device STP state has been updated. On failure, nothing has changed
* on the system.
*
* @note
* Since the return value is dynamically allocated, caller is responsible for freeing the memory
*/
int dev_set_bridge_stp(dev_handler *devh, const char *psBridgeName, const char *psStpState) {
int rc = 0;
if (!devh) {
LOGWARN("Invalid argument: null device handler\n");
return (1);
}
// Make sure the pointer isn't NULL
if (!psBridgeName || !psStpState)
return (1);
// Is the STP state string valid?
if (strcmp(psStpState, BRIDGE_STP_ON) && strcmp(psStpState, BRIDGE_STP_OFF))
return (1);
// If the bridge device does not exists then that's bad!!!
if (!dev_exist(psBridgeName))
return (1);
// Is this a valid bridge device?
if (!dev_is_bridge(psBridgeName))
return (1);
// Set the STP state
if (euca_execlp(&rc, devh->cmdprefix, BRCTL_PATH, "stp", psBridgeName, psStpState, NULL) != EUCA_OK) {
LOGERROR("Fail to set STP to '%s' on bridge device '%s'. error=%d\n", psStpState, psBridgeName, rc);
return (1);
}
return (0);
}
/**
* Creates a bridge device. If the device already exists and is a bridge, this is
* basically a no-op.
*
* @param devh [in] pointer to the device handler
* @param psBridgeName [in] a constant string pointer to the bridge device name
* @param psStpState [in] a constant string pointer to the STP state. Must be either "on" or "off"
*
* @return A pointer to the newly created bridge device or NULL on failure.
*
* @see dev_remove_bridge()
*
* @pre
* - The psBridgeName parameter must not be NULL and if the device exists, it should be a valid bridge device
* - The psStpState must not be NULL and must be either "on" or "off"
*
* @post
* On success, the bridge device is created. On failure, nothing changed on the rc = system
*
* @note
* Since the return value is dynamically allocated, caller is responsible for freeing the memory
*/
dev_entry *dev_create_bridge(dev_handler *devh, const char *psBridgeName, const char *psStpState) {
int rc = 0;
int nbBridges = 0;
dev_entry *pBridge = NULL;
if (!devh) {
LOGWARN("Invalid argument: null device handler\n");
return (NULL);
}
// Make sure the pointer isn't NULL
if (!psBridgeName || !psStpState)
return (NULL);
// Is the STP state string valid?
if (strcmp(psStpState, BRIDGE_STP_ON) && strcmp(psStpState, BRIDGE_STP_OFF))
return (NULL);
// If the bridge already exists, then we're good
if (dev_exist(psBridgeName)) {
// Is this a valid bridge device?
if (!dev_is_bridge(psBridgeName))
return (NULL);
// Retrieve the device it should succeed because we know its a bridge
dev_get_bridges(devh, psBridgeName, &pBridge, &nbBridges);
return (pBridge);
}
// Create the bridge device
if (euca_execlp(&rc, devh->cmdprefix, BRCTL_PATH, "addbr", psBridgeName, NULL) != EUCA_OK) {
LOGERROR("Fail to create bridge device '%s'. error=%d\n", psBridgeName, rc);
}
// Did it work?
if (!dev_exist(psBridgeName))
return (NULL);