From 77e92b12beaa5e48bb9e88c052b70507b9f0bcac Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Mon, 4 Mar 2019 15:30:38 +0100 Subject: [PATCH] Refactor code for ipaddress support --- p4app/controller.py | 93 +++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/p4app/controller.py b/p4app/controller.py index cb3ac2a..e6a4583 100644 --- a/p4app/controller.py +++ b/p4app/controller.py @@ -36,57 +36,61 @@ class L2Controller(object): self.modes = ['base', 'router'] 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.v6_mask = "64" + self.info['v4_mask'] = "24" + 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[None] = [] 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.v4_mask = "24" self.v4_routes = {} self.v4_routes[None] = [] self.v4_routes['base'] = [] - self.v4_routes['base'].append({ "net": "10.0.41.", "port": "3"}) - self.v4_routes['base'].append({ "net": "10.0.42.", "port": "4"}) + for port in range(3,5): + net = self.info['v4_gen'].next() + self.v4_routes['base'].append({ + "net": net, + "port": port} + ) self.v4_routes['router'] = self.v4_routes['base'] self.v6_addresses = {} self.v6_addresses[None] = [] 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[None] = [] 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) - @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): """ 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 def add_host_ips(host, net, ipaddr, dev): @@ -149,29 +153,26 @@ class L2Controller(object): def listen_to_icmp6_multicast(self): """Only needed for debugging""" - net = str(self.info['ndp_multicast']) - self.controller.table_add("v6_networks", "controller_debug", [net]) + net = self.info['ndp_multicast'] + self.controller.table_add("v6_networks", "controller_debug", [str(net)]) def fill_tables(self): self.controller.table_clear("v6_networks") 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", [net], [v6route['port']]) + self.controller.table_add("v6_networks", "set_egress_port", [str(v6route['net'])], [str(v6route['port'])]) self.listen_to_icmp6_multicast() 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", [net], [v4route['port']]) + self.controller.table_add("v4_networks", "set_egress_port", [str(v4route['net'])], [str(v4route['port'])]) self.controller.table_clear("v6_addresses") for v6addr in self.v6_addresses[self.mode]: - icmp6_addr = str(self.gen_ndp_multicast_addr(v6addr['addr'])) - - self.controller.table_add("v6_addresses", "controller_reply", [v6addr['addr']]) - self.controller.table_add("v6_addresses", "controller_reply", [icmp6_addr]) + icmp6_addr = self.gen_ndp_multicast_addr(v6addr) + 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): """ Assumptions: @@ -182,28 +183,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 = self.prefix_to_net(v6route['net'], self.v6_mask) - ipaddr = self.prefix_to_host(v6route['net'], self.v6_mask) + net = ipaddress.ip_network(v6route['net']) + ipaddr = net[1] 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"): - #log.debug("{}: {}".format(msg, self.debug_format_pkg(pkg))) log.debug("{}: {}".format(msg, pkg.__repr__())) def debug_format_pkg(self, pkg):