forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheucanetd_template.c
504 lines (432 loc) · 21.7 KB
/
eucanetd_template.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
// -*- 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/eucanetd_template.c
//! Template file containing the necessary information to create a new network driver
//! for EUCANETD.
//!
//! 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, 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)
//!
//! The network driver APIs must implement the following functions:
//! - network_driver_init()
//! - network_driver_cleanup()
//! - network_driver_system_flush()
//! - network_driver_system_scrub()
//! Optional functions:
//! - network_driver_upgrade()
//! - network_driver_implement_network()
//! - network_driver_implement_sg()
//! - network_driver_implement_addressing()
//! - network_driver_system_maint()
//! - network_driver_handle_signal()
//!
//! Any other function implemented within the scope of this network driver must have its name
//! start with the mode name followed by an underscore and the rest of the function name. For example,
//! if the more name is "TEMPLATE", a non-driver API function would be named like: template_create_dhcp_configuration().
//!
/*----------------------------------------------------------------------------*\
| |
| 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 <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 <eucalyptus.h>
#include <misc.h>
#include <euca_string.h>
#include <euca_network.h>
#include <log.h>
#include <hash.h>
#include <math.h>
#include <http.h>
#include <config.h>
#include <sequence_executor.h>
#include <atomic_file.h>
#include "ipt_handler.h"
#include "ips_handler.h"
#include "ebt_handler.h"
#include "dev_handler.h"
#include "euca_gni.h"
#include "eucanetd.h"
#include "eucanetd_util.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
//! Set to TRUE when driver is initialized
static boolean gInitialized = FALSE;
/*----------------------------------------------------------------------------*\
| |
| EXPORTED PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
//! @{
//! @name TEMPLATE Mode Network Driver APIs
static int network_driver_init(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_upgrade(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_cleanup(eucanetdConfig *pConfig, globalNetworkInfo *pGni, boolean forceFlush);
static int network_driver_system_flush(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_system_maint(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static u32 network_driver_system_scrub(eucanetdConfig *pConfig, globalNetworkInfo *pGni, globalNetworkInfo *pGniApplied);
static int network_driver_implement_network(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_implement_sg(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_implement_addressing(eucanetdConfig *pConfig, globalNetworkInfo *pGni);
static int network_driver_handle_signal(eucanetdConfig *pConfig, globalNetworkInfo *pGni, int signal);
//! @}
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
//! Macro to see if the driver has been initialized
#define IS_INITIALIZED() ((gInitialized == TRUE) ? TRUE : FALSE)
//! Macro to get the driver name
#define DRIVER_NAME() templateDriverHandler.name
/*----------------------------------------------------------------------------*\
| |
| CALLBACK STRUCTURE |
| |
\*----------------------------------------------------------------------------*/
//! TEMPLATE driver operation handlers
struct driver_handler_t templateDriverHandler = {
.name = "TEMPLATE",
.init = network_driver_init,
.upgrade = network_driver_upgrade,
.cleanup = network_driver_cleanup,
.system_flush = network_driver_system_flush,
.system_maint = network_driver_system_maint,
.system_scrub = network_driver_system_scrub,
.implement_network = network_driver_implement_network,
.implement_sg = network_driver_implement_sg,
.implement_addressing = network_driver_implement_addressing,
.handle_signal = network_driver_handle_signal,
};
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
/**
* Initialize this network driver.
* - The core application configuration must be completed prior calling
* - The driver should not be already initialized (if its the case, a no-op will occur)
* - The pConfig parameter must not be NULL
*
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_init(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
LOGINFO("Initializing '%s' network driver.\n", DRIVER_NAME());
// Make sure our given pointer is valid
if (!pConfig) {
LOGERROR("Failure to initialize '%s' networking mode. Invalid configuration parameter provided.\n", DRIVER_NAME());
return (1);
}
// Are we already initialized?
if (IS_INITIALIZED()) {
LOGERROR("Networking '%s' mode already initialized. Skipping!\n", DRIVER_NAME());
return (0);
}
if (PEER_IS_NC(eucanetdPeer)) {
} else if (PEER_IS_CC(eucanetdPeer)) {
}
// We are now initialize
gInitialized = TRUE;
return (0);
}
/**
* Perform network driver upgrade. This function should be invoked once when eucanetd
* starts.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_upgrade(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
int ret = 0;
LOGINFO("Upgrade '%s' network driver.\n", DRIVER_NAME());
if (!pConfig || !pGni) {
LOGERROR("Failed to run upgrade for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
gInitialized = FALSE;
return (ret);
}
/**
* Cleans up the network driver. This will work even if the initial initialization
* fail for any reasons. This will reset anything that could have been half-way or
* fully configured. If forceFlush is set, then a network flush will be performed.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @param forceFlush [in] set to TRUE if a network flush needs to be performed
* @return 0 on success. Integer number on failure.
*/
static int network_driver_cleanup(eucanetdConfig *pConfig, globalNetworkInfo *pGni, boolean forceFlush) {
int ret = 0;
LOGINFO("Cleaning up '%s' network driver.\n", DRIVER_NAME());
if (forceFlush) {
if (network_driver_system_flush(pConfig, pGni)) {
LOGERROR("Fail to flush network artifacts during network driver cleanup. See above log errors for details.\n");
ret = 1;
}
}
gInitialized = FALSE;
return (ret);
}
/**
* Responsible for flushing any networking artifacts implemented by this
* network driver.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_system_flush(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
LOGINFO("Flushing '%s' network driver artifacts.\n", DRIVER_NAME());
// Is our driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to flush the networking artifacts for '%s' network driver. Driver not initialized.\n", DRIVER_NAME());
return (1);
}
if (!pConfig || !pGni) {
LOGERROR("Failed to run maintenance activities for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
if (PEER_IS_NC(eucanetdPeer)) {
} else if (PEER_IS_CC(eucanetdPeer)) {
}
return (0);
}
/**
* This API is invoked when eucanetd will potentially be idle. For example, after
* populating the global network state, eucanetd detects that no action needs to
* be taken. Good for pre-populating cache, or flushing dirty cache - so these
* actions are not necessary in the regular iteration.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_system_maint(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
LOGTRACE("Maintenance activities for '%s' network driver.\n", DRIVER_NAME());
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to run maintenance. Driver '%s' not initialized.\n", DRIVER_NAME());
return (1);
}
if (!pConfig || !pGni) {
LOGERROR("Failed to run maintenance activities for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
return (0);
}
/**
* This API checks the new GNI against the system view to decide what really
* needs to be done.
* In practice, network drivers will execute all that needs to be done instead
* of just checking. In many cases, implement artifacts while detecting GNI and
* system view differences is more efficient.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @param pGniApplied [in] a pointer to the previously successfully implemented GNI
* @return A bitmask indicating what needs to be done. The following bits are
* the ones to look for: EUCANETD_RUN_NETWORK_API, EUCANETD_RUN_SECURITY_GROUP_API
* and EUCANETD_RUN_ADDRESSING_API.
*/
static u32 network_driver_system_scrub(eucanetdConfig *pConfig, globalNetworkInfo *pGni, globalNetworkInfo *pGniApplied) {
u32 ret = EUCANETD_RUN_NO_API;
LOGTRACE("Scrubbing for '%s' network driver.\n", DRIVER_NAME());
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to scrub the system for network artifacts. Driver '%s' not initialized.\n", DRIVER_NAME());
return (EUCANETD_RUN_NO_API);
}
// Are the global and local network view structures NULL?
if (!pGni || !pConfig) {
LOGERROR("Failed to scrub '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (EUCANETD_RUN_NO_API);
}
if (PEER_IS_NC(eucanetdPeer)) {
} else if (PEER_IS_CC(eucanetdPeer)) {
}
return (ret);
}
/**
* This takes care of implementing the network artifacts necessary. This will add or
* remove devices, tunnels, etc. as necessary.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_implement_network(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
LOGTRACE("Implementing network artifacts for 'TEMPLATE' network driver.\n");
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to implement network artifacts for '%s' network driver. Driver not initialized.\n", DRIVER_NAME());
return (1);
}
// Are the global and local network view structures NULL?
if (!pGni || !pConfig) {
LOGERROR("Failed to implement network artifacts for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
if (PEER_IS_NC(eucanetdPeer)) {
} else if (PEER_IS_CC(eucanetdPeer)) {
}
return (0);
}
/**
* This takes care of implementing the security-group artifacts necessary. This will add or
* remove networking rules pertaining to the groups and their membership.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_implement_sg(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
LOGTRACE("Implementing security-group artifacts for '%s' network driver.\n", DRIVER_NAME());
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to implement security-group artifacts for '%s' network driver. Driver not initialized.\n", DRIVER_NAME());
return (1);
}
// Are the global and local network view structures NULL?
if (!pGni || !pConfig) {
LOGERROR("Failed to implement security-group artifacts for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
if (PEER_IS_NC(eucanetdPeer)) {
} else if (PEER_IS_CC(eucanetdPeer)) {
}
return (0);
}
/**
* This takes care of implementing the addressing artifacts necessary. This will add or
* remove IP addresses and elastic IPs for each instances.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @return 0 on success. Integer number on failure.
*/
static int network_driver_implement_addressing(eucanetdConfig *pConfig, globalNetworkInfo *pGni) {
LOGTRACE("Implementing addressing artifacts for '%s' network driver.\n", DRIVER_NAME());
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to implement addressing artifacts for '%s' network driver. Driver not initialized.\n", DRIVER_NAME());
return (1);
}
// Are the global and local network view structures NULL?
if (!pGni || !pConfig) {
LOGERROR("Failed to implement addressing artifacts for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
if (PEER_IS_NC(eucanetdPeer)) {
} else if (PEER_IS_CC(eucanetdPeer)) {
}
return (0);
}
/**
* This API is invoked when eucanetd catches an USR1 or USR2 signal.
* @param pConfig [in] a pointer to eucanetd system-wide configuration
* @param pGni [in] a pointer to the Global Network Information structure
* @param signal [in] received signal
* @return 0 on success. Integer number on failure.
*/
static int network_driver_handle_signal(eucanetdConfig *pConfig, globalNetworkInfo *pGni, int signal) {
LOGDEBUG("Handling singal %d for '%s' network driver.\n", signal, DRIVER_NAME());
// Is the driver initialized?
if (!IS_INITIALIZED()) {
LOGERROR("Failed to handle signal. Driver '%s' not initialized.\n", DRIVER_NAME());
return (1);
}
// Is the global network view structure NULL?
if (!pGni || !pConfig) {
LOGERROR("Failed to handle signal for '%s' network driver. Invalid parameters provided.\n", DRIVER_NAME());
return (1);
}
return (0);
}