-
Notifications
You must be signed in to change notification settings - Fork 25
/
create_macvlan.py
executable file
·363 lines (295 loc) · 14 KB
/
create_macvlan.py
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
#!/usr/bin/env python3
"""
NAME: create_macvlan.py
PURPOSE: Create and configure one or more MACVLAN ports using the specified parent port.
NOTES: MACVLAN ports can only be created with a Ethernet, Bond, Redir, or 802.1Q VLAN port
as the parent port.
MACVLAN ports are different than 802.1Q VLAN ports.
This script will optionally set IPv4 configuration (static or dynamic), if specified.
The selected IPv4 configuration method will be applied to all ports created by this script.
EXAMPLE: # Single MACVLAN with MACVLAN ID 10 on parent port '1.1.eth3'. No IPv4 configuration specified.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10
# Single MACVLAN with MACVLAN ID 10 on parent port '1.1.eth3'. DHCPv4 enabled.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 \
--dhcpv4
# Single MACVLAN with MACVLAN ID 10 on parent port '1.1.eth3'. Static IPv4 configuration.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 \
--ipv4_address 172.16.0.10 \
--ipv4_netmask 255.255.255.0 \
--ipv4_gateway 172.16.0.1
# Four MACVLANs with MACVLAN IDs 10, 20, 30, and 40 on parent port '1.1.eth3'. DHCPv4 enabled.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 20 30 40 \
--dhcpv4
# Two MACVLANs with MACVLAN IDs 10 and 20 on parent port '1.1.eth3'. Static IPv4 configuration
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 20 \
--ipv4_address 172.16.0.10 172.10.0.20 \
--ipv4_netmask 255.255.255.0 \
--ipv4_gateway 172.16.0.1
SCRIPT_CLASSIFICATION:
Creation
SCRIPT_CATEGORIES:
Functional
STATUS: Functional
VERIFIED_ON:
09-JUN-2023,
GUI Version: 5.4.6
Kernel Version: 5.19.17+
LICENSE: Free to distribute and modify. LANforge systems must be licensed.
Copyright 2024 Candela Technologies Inc.
INCLUDE_IN_README:
False
"""
import sys
import os
import importlib
import argparse
import logging
import time
logger = logging.getLogger(__name__)
if sys.version_info[0] != 3:
logger.critical("This script requires Python 3")
exit(1)
sys.path.append(os.path.join(os.path.abspath(__file__ + "../../../")))
lfcli_base = importlib.import_module("py-json.LANforge.lfcli_base")
LFCliBase = lfcli_base.LFCliBase
LFUtils = importlib.import_module("py-json.LANforge.LFUtils")
add_file_endp = importlib.import_module("py-json.LANforge.add_file_endp")
realm = importlib.import_module("py-json.realm")
Realm = realm.Realm
lf_logger_config = importlib.import_module("py-scripts.lf_logger_config")
class CreateMacVlan(Realm):
def __init__(self,
mgr: str,
mgr_port: int,
parent_port: str,
macvlan_ids: list,
dhcpv4: bool,
ipv4_addresses: list,
ipv4_netmasks: list,
ipv4_gateways: list,
exit_on_error: bool = False,
debug: bool = False,
**kwargs):
super().__init__(mgr, mgr_port, debug_=debug, _exit_on_error=exit_on_error)
self.host = mgr
self.port = mgr_port
self.macvlan_profiles = []
for ix, macvlan_id in enumerate(macvlan_ids):
# Defaults to one MACVLAN port created per profile
# This script assumes the same parent port for all created MACVLAN ports
macvlan_profile = self.new_mvlan_profile()
self.macvlan_profiles.append(macvlan_profile)
# Workaround for MACVLAN profile quirks
shelf, resource, parent, _ = self.name_to_eid(parent_port)
macvlan_profile.shelf = shelf
macvlan_profile.resource = resource
macvlan_profile.macvlan_parent = parent
macvlan_profile.desired_macvlans = ["#" + macvlan_id]
if dhcpv4:
macvlan_profile.dhcp = True
elif ipv4_addresses:
macvlan_profile.dhcp = False
macvlan_profile.first_ip_addr = ipv4_addresses[ix]
# Either apply same subnet mask to all or one specified for each
if len(ipv4_netmasks) == 1:
macvlan_profile.netmask = ipv4_netmasks[0]
else:
macvlan_profile.netmask = ipv4_netmasks[ix]
# Either apply same gateway to all or on especified for each
if len(ipv4_gateways) == 1:
macvlan_profile.gateway = ipv4_gateways[0]
else:
macvlan_profile.gateway = ipv4_gateways[ix]
def cleanup(self):
"""Remove any conflicting LANforge port(s)."""
logger.info("Cleaning up any created or conflicting MACVLAN ports")
for macvlan_profile in self.macvlan_profiles:
macvlan_profile.cleanup()
def build(self):
"""Create LANforge port(s) as specified."""
logger.info("Creating MACVLAN port(s)")
for macvlan_profile in self.macvlan_profiles:
ret = macvlan_profile.create(debug=self.debug, sleep_time=0)
if not ret:
logger.error(
f"Failed to create MACVLAN port with MACVLAN ID: {macvlan_profile.desired_macvlans}")
exit(1)
logger.info("Successfully created MACVLAN port(s)")
def parse_args():
"""Parse CLI arguments."""
parser = LFCliBase.create_bare_argparse(
prog='create_macvlan.py',
# formatter_class=argparse.RawDescriptionHelpFormatter,
formatter_class=argparse.RawTextHelpFormatter,
epilog='''\
Creates MACVLAN endpoints.''',
description='''\
NAME: create_macvlan.py
PURPOSE: Create and configure one or more MACVLAN ports using the specified parent port.
NOTES: MACVLAN ports can only be created with a Ethernet, Bond, Redir, or 802.1Q VLAN port
as the parent port.
MACVLAN ports are different than 802.1Q VLAN ports.
This script will optionally set IPv4 configuration (static or dynamic), if specified.
The selected IPv4 configuration method will be applied to all ports created by this script.
EXAMPLE: # Single MACVLAN with MACVLAN ID 10 on parent port '1.1.eth3'. No IPv4 configuration specified.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10
# Single MACVLAN with MACVLAN ID 10 on parent port '1.1.eth3'. DHCPv4 enabled.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 \
--dhcpv4
# Single MACVLAN with MACVLAN ID 10 on parent port '1.1.eth3'. Static IPv4 configuration.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 \
--ipv4_address 172.16.0.10 \
--ipv4_netmask 255.255.255.0 \
--ipv4_gateway 172.16.0.1
# Four MACVLANs with MACVLAN IDs 10, 20, 30, and 40 on parent port '1.1.eth3'. DHCPv4 enabled.
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 20 30 40 \
--dhcpv4
# Two MACVLANs with MACVLAN IDs 10 and 20 on parent port '1.1.eth3'. Static IPv4 configuration
./create_macvlan.py \
--parent 1.1.eth3 \
--macvlan_ids 10 20 \
--ipv4_address 172.16.0.10 172.10.0.20 \
--ipv4_netmask 255.255.255.0 \
--ipv4_gateway 172.16.0.1
SCRIPT_CLASSIFICATION:
Creation
SCRIPT_CATEGORIES:
Functional
STATUS: Functional
VERIFIED_ON:
09-JUN-2023,
GUI Version: 5.4.6
Kernel Version: 5.19.17+
LICENSE: Free to distribute and modify. LANforge systems must be licensed.
Copyright 2024 Candela Technologies Inc.
INCLUDE_IN_README:
False
''')
# Required arguments
parser.add_argument('--parent', '--parent_port', '--macvlan_parent',
dest='parent_port',
help='Parent port used by created MACVLAN port(s)',
default=None,
required=True)
parser.add_argument('--macvlan_ids',
dest='macvlan_ids',
nargs='+',
help='MACVLAN ID(s) used in creation. '
'One MACVLAN port is created per ID. For static IP configuration, '
'the number of IDs specified in this argument must match the number '
'of static IP addresses specified in the \'--ipv4_addresses\'.',
default=None,
required=True)
# Optional arguments
parser.add_argument('--cleanup',
help='Clean up any created ports before exiting.',
action='store_true')
# Optional IPv4 configuration
#
# Limit users to only one of static or DHCP IPv4 configuration.
# Allow users to specify no L3 configuration.
#
# If static IPv4 configuration, argument validation must check that
# number of static IPv4 addresses matches either number of ports or
# number of specified MACVLAN IDs
ipv4_cfg = parser.add_mutually_exclusive_group(required=False)
ipv4_cfg.add_argument('--dhcp', '--dhcpv4', '--use_dhcp',
dest='dhcpv4',
help='Enable DHCPv4 on created ports',
action='store_true')
ipv4_cfg.add_argument('--ip', '--ips', '--ipv4_address', '--ipv4_addresses',
dest='ipv4_addresses',
type=str,
nargs='+',
help='List of static IPv4 addresses. The number of IPv4 addresses '
'specified must match the number of ports specified in '
'\'--num_ports\'',
default=None)
# Only checked when static configuration specified
parser.add_argument('--netmask', '--netmasks', '--ipv4_netmask', '--ipv4_netmasks',
dest='ipv4_netmasks',
type=str,
nargs='+',
help='IPv4 subnet mask to apply to all created MACVLAN ports '
'when static IPv4 configuration requested',
default=None)
parser.add_argument('--gateway', '--gateways', '--ipv4_gateway', '--ipv4_gateways',
dest='ipv4_gateways',
type=str,
nargs='+',
help='IPv4 gateway to apply to all created MACVLAN ports '
'when static IPv4 configuration requested',
default=None)
return parser.parse_args()
def validate_args(args):
"""Validate CLI arguments."""
# User either specifies DHCPv4, static IPv4 configuration, or no IPv4 configuration
#
# If user specified static configuration, ensure that number of specified ports
# to create matches number of static IPv4 configurations specified
if args.ipv4_addresses:
num_addresses = len(args.ipv4_addresses)
# If multiple static IPv4 addresses specified, then must match number
# of MACVLAN IDs specified
if args.macvlan_ids and num_addresses != len(args.macvlan_ids):
logger.error("Number of static IPv4 addresses does not match \'--macvlan_ids\'")
exit(1)
# IPv4 subnet mask required if static IPv4 configuration specified
# If more than one subnet mask specified, must match number of IPv4 addresses
# If only one, then apply that to all created ports
if not args.ipv4_netmasks:
logger.error("No IPv4 subnet mask specified")
exit(1)
elif len(args.ipv4_netmasks) != 1 and num_addresses != len(args.ipv4_netmasks):
logger.error("Number of IPv4 subnet masks does not match number of IPv4 addresses.")
exit(1)
# IPv4 gateway required if static IPv4 configuration specified
# If more than one gateway specified, must match number of IPv4 addresses
# If only one, then apply that to all created ports
if not args.ipv4_gateways:
# TODO: Should we make this a warn and continue? Will need to fix
# MACVLANProfile code if so
logger.error("No IPv4 gateway specified")
exit(1)
elif len(args.ipv4_gateways) != 1 and num_addresses != len(args.ipv4_gateways):
logger.error("Number of IPv4 gateways does not match number of IPv4 addresses.")
exit(1)
def main():
"""Create LANforge MACVLAN port(s) using specified options."""
args = parse_args()
help_summary = "This script will create and configure one or more MACVLAN ports " \
"using the specified single parent port. Note that MACVLAN ports " \
"are different than 802.1Q VLAN ports."
if args.help_summary:
print(help_summary)
exit(0)
# Configure logging
logger_config = lf_logger_config.lf_logger_config()
logger_config.set_level(level=args.log_level)
logger_config.set_json(json_file=args.lf_logger_config_json)
validate_args(args)
ip_test = CreateMacVlan(**vars(args))
ip_test.build()
if args.cleanup:
time.sleep(5)
ip_test.cleanup()
if __name__ == "__main__":
main()