Cleanup networking

This commit is contained in:
Nico Schottelius 2020-01-19 11:30:41 +01:00
parent 30be791312
commit bd9dbb12b7
2 changed files with 50 additions and 18 deletions

View file

@ -13,8 +13,11 @@ arg_parser.add_argument('--create-vm', action='store_true')
arg_parser.add_argument('--last-used-mac', action='store_true') arg_parser.add_argument('--last-used-mac', action='store_true')
arg_parser.add_argument('--get-new-mac', action='store_true') arg_parser.add_argument('--get-new-mac', action='store_true')
arg_parser.add_argument('--init-network', help="Initialise networking") arg_parser.add_argument('--init-network', help="Initialise networking", action='store_true')
arg_parser.add_argument('--management-network', help="IPv6 management network") arg_parser.add_argument('--create-vxlan', help="Initialise networking", action='store_true')
arg_parser.add_argument('--network', help="/64 IPv6 network")
arg_parser.add_argument('--vxlan-uplink-device', help="The VXLAN underlay device, i.e. eth0")
arg_parser.add_argument('--vni', help="VXLAN ID (decimal)", type=int)
arg_parser.add_argument('--run-dns-ra', action='store_true', arg_parser.add_argument('--run-dns-ra', action='store_true',
help="Provide router advertisements and DNS resolution via dnsmasq") help="Provide router advertisements and DNS resolution via dnsmasq")
@ -37,15 +40,17 @@ def main(arguments):
if arguments['get_new_mac']: if arguments['get_new_mac']:
print(MAC(config).get_next()) print(MAC(config).get_next())
if arguments['init_networking!']: #if arguments['init_network']:
if not arguments['management_network']: if arguments['create_vxlan']:
raise UncloudException("Initialising the network requires an IPv6 network. You can use fd00::/64 for testing (non production!)") if not arguments['network'] or not arguments['vni'] or not arguments['vxlan_uplink_device']:
vb = VXLANBridge(arguments['management_network']) raise UncloudException("Initialising the network requires an IPv6 network and a VNI. You can use fd00::/64 and vni=1 for testing (non production!)")
vb.setup() vb = VXLANBridge(vni=arguments['vni'],
uplinkdev=arguments['vxlan_uplink_device'])
vb._setup_vxlan()
if arguments['run_dns_ra']: if arguments['run_dns_ra']:
if not arguments['management_network']: if not arguments['network']:
raise UncloudException("Providing DNS/RAs requires a /64 IPv6 network. You can use fd00::/64 for testing (non production!)") raise UncloudException("Providing DNS/RAs requires a /64 IPv6 network. You can use fd00::/64 for testing (non production!)")
dnsra = DNSRA(arguments['management_network']) dnsra = DNSRA(arguments['network'])
dnsra.setup() dnsra.setup()

View file

@ -1,21 +1,48 @@
import subprocess import subprocess
import ipaddress
from uncloud import UncloudException
class ManagementBridge(VXLANBridge):
pass
class VXLANBridge(object): class VXLANBridge(object):
def __init__(self, vni, bridgedev=None, uplinkdev=None): cmd_create_vxlan = "ip -6 link add {vxlandev} type vxlan id {vni_dec} dstport 4789 group {multicast_address} dev {uplinkdev} ttl 5"
self.management_vni = 1
cmd_create_vxlan = "ip -6 link add {vxlandev} type vxlan id {netid} dstport 4789 group ff05::{netid} dev {uplinkdev} ttl 5"
cmd_up_dev = "ip link set {dev} up" cmd_up_dev = "ip link set {dev} up"
cmd_create_bridge="ip link add {bridgedev} type bridge" cmd_create_bridge="ip link add {bridgedev} type bridge"
cmd_add_to_bridge="ip link set {vxlandev} master {bridgedev} up" cmd_add_to_bridge="ip link set {vxlandev} master {bridgedev} up"
cmd_add_addr="ip addr add {ip} dev {bridgedev}" cmd_add_addr="ip addr add {ip} dev {bridgedev}"
def setup_networking(dev=wlan0, v6net): # VXLAN ids are at maximum 24 bit - use a /104
vxlandev=vxlan${netid} multicast_network = ipaddress.IPv6Network("ff05::/104")
bridgedev=br${netid} max_vni = (2**24)-1
def __init__(self,
vni,
uplinkdev):
self.config = {}
if vni > self.max_vni:
raise UncloudException("VNI must be in the range of 0 .. {}".format(self.max_vni))
self.config['vni_dec'] = vni
self.config['vni_hex'] = "{:x}".format(vni)
self.config['multicast_address'] = self.multicast_network[vni]
self.config['uplinkdev'] = uplinkdev
self.config['vxlandev'] = "vx{}".format(self.config['vni_hex'])
self.config['bridgedev'] = "br{}".format(self.config['vni_hex'])
def setup_networking(self):
pass
def _setup_vxlan(self):
# check for device first (?)
cmd = self.cmd_create_vxlan.format(**self.config)
print(cmd)
subprocess.run(cmd.split())
class ManagementBridge(VXLANBridge):
pass
class DNSRA(object): class DNSRA(object):