Refactor code for ipaddress support

This commit is contained in:
Nico Schottelius 2019-03-04 15:30:38 +01:00
parent 3047750add
commit 77e92b12be

View file

@ -36,57 +36,61 @@ class L2Controller(object):
self.modes = ['base', 'router'] self.modes = ['base', 'router']
self.info={} self.info={}
self.info['ndp_multicast'] = ipaddress.ip_network(u"ff02::1:ff00:0/104") self.info['ndp_multicast'] = ipaddress.ip_network("ff02::1:ff00:0/104")
self.address_suffix = 42 self.info['v6_mask'] = "64"
self.info['v6_base'] = ipaddress.ip_network("2001:db8::/32")
self.info['v6_gen'] = self.info['v6_base'].subnets(new_prefix=self.info['v6_mask'])
# Network / egress self.info['v4_mask'] = "24"
self.v6_mask = "64" self.info['v4_base'] = ipaddress.ip_network("10.0.0.0/8")
self.info['v4_gen'] = self.info['v4_base'].subnets(new_prefix=self.info['v4_mask'])
self.info['switch_suffix'] = 42
self.v6_routes = {} self.v6_routes = {}
self.v6_routes[None] = [] self.v6_routes[None] = []
self.v6_routes['base'] = [] self.v6_routes['base'] = []
self.v6_routes['base'].append({ "net": "2001:db8:61::", "port": "1"})
self.v6_routes['base'].append({ "net": "2001:db8:62::", "port": "2"}) for port in range(1,3):
net = self.info['v6_gen'].next()
self.v6_routes['base'].append({
"net": net,
"port": port}
)
self.v6_routes['router'] = self.v6_routes['base'] self.v6_routes['router'] = self.v6_routes['base']
self.v4_mask = "24"
self.v4_routes = {} self.v4_routes = {}
self.v4_routes[None] = [] self.v4_routes[None] = []
self.v4_routes['base'] = [] self.v4_routes['base'] = []
self.v4_routes['base'].append({ "net": "10.0.41.", "port": "3"}) for port in range(3,5):
self.v4_routes['base'].append({ "net": "10.0.42.", "port": "4"}) net = self.info['v4_gen'].next()
self.v4_routes['base'].append({
"net": net,
"port": port}
)
self.v4_routes['router'] = self.v4_routes['base'] self.v4_routes['router'] = self.v4_routes['base']
self.v6_addresses = {} self.v6_addresses = {}
self.v6_addresses[None] = [] self.v6_addresses[None] = []
self.v6_addresses['base'] = [] self.v6_addresses['base'] = []
self.v6_addresses['router'] = [ { "addr": "{}{}".format(n['net'], self.address_suffix), "port": n['port'] } for n in self.v6_routes['router'] ] self.v6_addresses['router'] = [ net['net'][42] for net in self.v6_routes['router'] ]
self.v4_addresses = {} self.v4_addresses = {}
self.v4_addresses[None] = [] self.v4_addresses[None] = []
self.v4_addresses['base'] = [] self.v4_addresses['base'] = []
self.v4_addresses['router'] = [ { "addr": "{}{}".format(n['net'], self.address_suffix), "port": n['port'] } for n in self.v4_routes['router'] ] self.v4_addresses['router'] = [ net['net'][42] for net in self.v4_routes['router'] ]
self.init_boilerplate(sw_name) self.init_boilerplate(sw_name)
@staticmethod
def prefix_to_net(net, mask):
""" work around ipaddress bug """
net = "{}0/{}".format(net, mask)
log.debug("net: {}".format(net))
return net
@staticmethod
def prefix_to_host(net, mask):
""" work around ipaddress bug """
host = "{}1/{}".format(net, mask)
log.debug("host: {}".format(host))
return "{}1/{}".format(net, mask)
def gen_ndp_multicast_addr(self, addr): def gen_ndp_multicast_addr(self, addr):
""" append the 24 bit of the address to the multicast address""" """ append the 24 bit of the address to the multicast address"""
return ipaddress.ip_address(int(self.info['ndp_multicast'].network_address) + (int(addr) & 0xffffff))
last_24 = int(addr) & 0xffffff
addr = self.info['ndp_multicast'][last_24]
return addr
@staticmethod @staticmethod
def add_host_ips(host, net, ipaddr, dev): def add_host_ips(host, net, ipaddr, dev):
@ -149,29 +153,26 @@ class L2Controller(object):
def listen_to_icmp6_multicast(self): def listen_to_icmp6_multicast(self):
"""Only needed for debugging""" """Only needed for debugging"""
net = str(self.info['ndp_multicast']) net = self.info['ndp_multicast']
self.controller.table_add("v6_networks", "controller_debug", [net]) self.controller.table_add("v6_networks", "controller_debug", [str(net)])
def fill_tables(self): def fill_tables(self):
self.controller.table_clear("v6_networks") self.controller.table_clear("v6_networks")
for v6route in self.v6_routes[self.mode]: for v6route in self.v6_routes[self.mode]:
net = self.prefix_to_net(v6route['net'], self.v6_mask) self.controller.table_add("v6_networks", "set_egress_port", [str(v6route['net'])], [str(v6route['port'])])
self.controller.table_add("v6_networks", "set_egress_port", [net], [v6route['port']])
self.listen_to_icmp6_multicast() self.listen_to_icmp6_multicast()
self.controller.table_clear("v4_routing") self.controller.table_clear("v4_routing")
for v4route in self.v4_routes[self.mode]: for v4route in self.v4_routes[self.mode]:
net = self.prefix_to_net(v4route['net'], self.v4_mask) self.controller.table_add("v4_networks", "set_egress_port", [str(v4route['net'])], [str(v4route['port'])])
self.controller.table_add("v4_routing", "set_egress_port", [net], [v4route['port']])
self.controller.table_clear("v6_addresses") self.controller.table_clear("v6_addresses")
for v6addr in self.v6_addresses[self.mode]: for v6addr in self.v6_addresses[self.mode]:
icmp6_addr = str(self.gen_ndp_multicast_addr(v6addr['addr'])) icmp6_addr = self.gen_ndp_multicast_addr(v6addr)
self.controller.table_add("v6_addresses", "controller_reply", [v6addr['addr']])
self.controller.table_add("v6_addresses", "controller_reply", [icmp6_addr])
self.controller.table_add("v6_addresses", "controller_reply", [str(v6addr)])
self.controller.table_add("v6_addresses", "controller_reply", [str(icmp6_addr)])
def config_hosts(self): def config_hosts(self):
""" Assumptions: """ Assumptions:
@ -182,28 +183,12 @@ class L2Controller(object):
for v6route in self.v6_routes[self.mode]: for v6route in self.v6_routes[self.mode]:
host = "h{}".format(v6route['port']) host = "h{}".format(v6route['port'])
dev = "{}-eth0".format(host) dev = "{}-eth0".format(host)
#net = ipaddress.ip_network(v6route['net']) net = ipaddress.ip_network(v6route['net'])
#ipaddr = "{}/{}".format(net[1],net.prefix_length) ipaddr = net[1]
net = self.prefix_to_net(v6route['net'], self.v6_mask)
ipaddr = self.prefix_to_host(v6route['net'], self.v6_mask)
self.add_host_ips(host, net, ipaddr, dev) self.add_host_ips(host, net, ipaddr, dev)
# mx h$i "ip -6 route add default via 2001:db8:6::42"
#for v4route in self.v4_routes[self.mode]:
# Todo: add v4 routes / ip addresses
# Fake mac addresses - this gets REALLY messy now
# for host in range(4):
# macrange = [1, 2, 3, 4, 42]
# for mac in macrange:
# subprocess.call(["mx", host, "ip", "addr", "add", ipaddr, "dev", dev])
def debug_print_pkg(self, pkg, msg="INCOMING"): def debug_print_pkg(self, pkg, msg="INCOMING"):
#log.debug("{}: {}".format(msg, self.debug_format_pkg(pkg)))
log.debug("{}: {}".format(msg, pkg.__repr__())) log.debug("{}: {}".format(msg, pkg.__repr__()))
def debug_format_pkg(self, pkg): def debug_format_pkg(self, pkg):