[controller] Workaround python 2.7 / ipaddres bug

This commit is contained in:
Nico Schottelius 2019-02-23 18:14:34 +01:00
parent c2133dc40f
commit f9ec915449

View file

@ -13,9 +13,11 @@ import re
import logging
import argparse
import ipaddress
import subprocess
# Broken in python2
#import ipaddress
logging.basicConfig()
log = logging.getLogger("main")
@ -26,20 +28,39 @@ class L2Controller(object):
self.modes = ['base']
# Network / egress
self.v6_mask = "/64"
self.v6_routes = {}
self.v6_routes[None] = []
self.v6_routes['base'] = []
self.v6_routes['base'].append({ "net": "2001:db8:61::/64", "port": "1"})
self.v6_routes['base'].append({ "net": "2001:db8:62::/64", "port": "2"})
self.v6_routes['base'].append({ "net": "2001:db8:61::", "port": "1"})
self.v6_routes['base'].append({ "net": "2001:db8:62::", "port": "2"})
self.v4_mask = "/24"
self.v4_routes = {}
self.v4_routes[None] = []
self.v4_routes['base'] = []
self.v4_routes['base'].append({ "net": "10.0.41.0/24", "port": "3"})
self.v4_routes['base'].append({ "net": "10.0.42.0/24", "port": "4"})
self.v4_routes['base'].append({ "net": "10.0.41.", "port": "3"})
self.v4_routes['base'].append({ "net": "10.0.42.", "port": "4"})
self.init_boilerplate(sw_name)
@staticmethod
def prefix_to_net(net, mask):
""" work around ipaddress bug """
return "{}0/{}".format(net, mask)
@staticmethod
def prefix_to_host(net, mask):
""" work around ipaddress bug """
return "{}1/{}".format(net, mask)
def add_host_ips(host, net, ipaddr, dev):
subprocess.run(["mx", host, "ip", "addr", "flush", "dev", dev])
subprocess.run(["mx", host, "sysctl", "net.ipv6.conf.lo.disable_ipv6=0"])
subprocess.run(["mx", host, "sysctl", "net.ipv6.conf.{}.disable_ipv6=0".format(dev)])
subprocess.run(["mx", host, "ip", "addr", "add", ipaddr, "dev", dev])
def init_boilerplate(self, sw_name):
self.topo = Topology(db="topology.db")
self.sw_name = sw_name
@ -59,10 +80,12 @@ class L2Controller(object):
def fill_tables(self):
self.controller.table_clear("v6_routing")
for v6route in self.v6_routes[self.mode]:
self.controller.table_add("v6_routing", "set_egress_port", [v6route['net']], [v6route['port']])
net = self.prefix_to_net(v6route['net'], self.v6_mask)
self.controller.table_add("v6_routing", "set_egress_port", [net], [v6route['port']])
self.controller.table_clear("v4_routing")
for v4route in self.v4_routes[self.mode]:
net = self.prefix_to_net(v4route['net'], self.v4_mask)
self.controller.table_add("v4_routing", "set_egress_port", [v4route['net']], [v4route['port']])
def config_hosts(self):
@ -74,13 +97,12 @@ class L2Controller(object):
for v6route in self.v6_routes[self.mode]:
host = "h{}".format(v6route['port'])
dev = "{}-eth0".format(host)
net = ipaddress.ip_network(v6route['net'])
ipaddr = "{}/{}".format(net[1],net.prefix_length)
#net = ipaddress.ip_network(v6route['net'])
#ipaddr = "{}/{}".format(net[1],net.prefix_length)
net = self.prefix_to_net(v6route['net'], self.v6_mask)
ipaddr = self.prefix_to_host(v6route['net'], self.v6_mask)
subprocess.run(["mx", host, "ip", "addr", "flush", "dev", dev])
subprocess.run(["mx", host, "sysctl", "net.ipv6.conf.lo.disable_ipv6=0"])
subprocess.run(["mx", host, "sysctl", "net.ipv6.conf.{}.disable_ipv6=0".format(dev)])
subprocess.run(["mx", host, "ip", "addr", "add", ipaddr, "dev", dev])
self.add_host_ips(host, net, ipaddr, dev)
# mx h$i "ip -6 route add default via 2001:db8:6::42"