From bd9dbb12b798a1bfe0651cfb7bcae22058ae456b Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 19 Jan 2020 11:30:41 +0100 Subject: [PATCH] Cleanup networking --- uncloud/hack/main.py | 23 +++++++++++++--------- uncloud/hack/net.py | 45 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/uncloud/hack/main.py b/uncloud/hack/main.py index 4ccb74a..cb9fd7b 100644 --- a/uncloud/hack/main.py +++ b/uncloud/hack/main.py @@ -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('--get-new-mac', action='store_true') -arg_parser.add_argument('--init-network', help="Initialise networking") -arg_parser.add_argument('--management-network', help="IPv6 management network") +arg_parser.add_argument('--init-network', help="Initialise networking", action='store_true') +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', help="Provide router advertisements and DNS resolution via dnsmasq") @@ -37,15 +40,17 @@ def main(arguments): if arguments['get_new_mac']: print(MAC(config).get_next()) - if arguments['init_networking!']: - if not arguments['management_network']: - raise UncloudException("Initialising the network requires an IPv6 network. You can use fd00::/64 for testing (non production!)") - vb = VXLANBridge(arguments['management_network']) - vb.setup() + #if arguments['init_network']: + if arguments['create_vxlan']: + if not arguments['network'] or not arguments['vni'] or not arguments['vxlan_uplink_device']: + 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 = VXLANBridge(vni=arguments['vni'], + uplinkdev=arguments['vxlan_uplink_device']) + vb._setup_vxlan() 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!)") - dnsra = DNSRA(arguments['management_network']) + dnsra = DNSRA(arguments['network']) dnsra.setup() diff --git a/uncloud/hack/net.py b/uncloud/hack/net.py index 11649b8..170e7b9 100644 --- a/uncloud/hack/net.py +++ b/uncloud/hack/net.py @@ -1,21 +1,48 @@ import subprocess +import ipaddress + +from uncloud import UncloudException -class ManagementBridge(VXLANBridge): - pass class VXLANBridge(object): - def __init__(self, vni, bridgedev=None, uplinkdev=None): - 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_create_vxlan = "ip -6 link add {vxlandev} type vxlan id {vni_dec} dstport 4789 group {multicast_address} dev {uplinkdev} ttl 5" cmd_up_dev = "ip link set {dev} up" cmd_create_bridge="ip link add {bridgedev} type bridge" cmd_add_to_bridge="ip link set {vxlandev} master {bridgedev} up" cmd_add_addr="ip addr add {ip} dev {bridgedev}" - def setup_networking(dev=wlan0, v6net): - vxlandev=vxlan${netid} - bridgedev=br${netid} + # VXLAN ids are at maximum 24 bit - use a /104 + multicast_network = ipaddress.IPv6Network("ff05::/104") + 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):