2019-02-21 22:38:09 +00:00
|
|
|
import nnpy
|
|
|
|
import struct
|
|
|
|
|
|
|
|
from p4utils.utils.topology import Topology
|
|
|
|
from p4utils.utils.sswitch_API import SimpleSwitchAPI
|
|
|
|
|
|
|
|
from scapy.all import sniff, get_if_list, Ether, get_if_hwaddr, sendp
|
|
|
|
from scapy.all import IP, Raw, IPv6, TCP, TCP_client
|
|
|
|
from scapy.all import Ether, sniff, Packet, BitField
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
|
|
|
import logging
|
2019-02-23 13:22:46 +00:00
|
|
|
import argparse
|
|
|
|
import ipaddress
|
|
|
|
|
2019-02-21 22:38:09 +00:00
|
|
|
logging.basicConfig()
|
|
|
|
log = logging.getLogger("main")
|
|
|
|
|
|
|
|
class L2Controller(object):
|
|
|
|
def __init__(self, sw_name):
|
2019-02-21 22:38:45 +00:00
|
|
|
self.init_boilerplate(sw_name)
|
2019-02-21 22:38:09 +00:00
|
|
|
self.init()
|
|
|
|
|
2019-02-23 13:22:46 +00:00
|
|
|
self.modes = ['base']
|
|
|
|
|
|
|
|
# Network / egress
|
|
|
|
self.v6_routes = {}
|
|
|
|
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.v4_routes = {}
|
|
|
|
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"})
|
|
|
|
|
2019-02-21 22:38:09 +00:00
|
|
|
def init_boilerplate(self, sw_name):
|
|
|
|
self.topo = Topology(db="topology.db")
|
|
|
|
self.sw_name = sw_name
|
|
|
|
self.thrift_port = self.topo.get_thrift_port(sw_name)
|
|
|
|
self.cpu_port = self.topo.get_cpu_port_index(self.sw_name)
|
|
|
|
self.controller = SimpleSwitchAPI(self.thrift_port)
|
|
|
|
self.intf = str(self.topo.get_cpu_port_intf(self.sw_name).replace("eth0", "eth1"))
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
self.controller.reset_state()
|
|
|
|
self.fill_tables()
|
2019-02-23 13:22:46 +00:00
|
|
|
self.config_hosts()
|
2019-02-21 22:38:09 +00:00
|
|
|
self.add_mirror()
|
|
|
|
|
|
|
|
def add_mirror(self):
|
|
|
|
if self.cpu_port:
|
|
|
|
self.controller.mirroring_add(100, self.cpu_port)
|
|
|
|
|
|
|
|
def fill_tables(self):
|
2019-02-23 13:22:46 +00:00
|
|
|
for v6route in self.v6_routes[self.mode]:
|
|
|
|
self.controller.table_add("v6_routing", "set_egress_port", [v6route['net']], [v6route['port']])
|
|
|
|
|
|
|
|
for v4route in self.v4_routes[self.mode]:
|
|
|
|
self.controller.table_add("v4_routing", "set_egress_port", [v4route['net']], [v4route['port']])
|
|
|
|
|
|
|
|
def config_hosts(self):
|
|
|
|
""" Assumptions:
|
|
|
|
- all routes are networks (no /128 v6 or /32 v4
|
|
|
|
- hosts get the first ip address in the network
|
|
|
|
"""
|
2019-02-21 22:38:09 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def debug_print_pkg(self, pkg, msg="INCOMING"):
|
|
|
|
log.info("{}: {}".format(msg, self.debug_format_pkg(pkg)))
|
|
|
|
|
|
|
|
def debug_format_pkg(self, pkg):
|
|
|
|
packet = Ether(str(pkg))
|
|
|
|
|
|
|
|
if packet.type == 0x800:
|
|
|
|
ip = pkg.getlayer(IP)
|
|
|
|
elif packet.type == 0x86dd:
|
|
|
|
ip = pkg.getlayer(IPv6)
|
|
|
|
|
|
|
|
# tcp = pkg.getlayer(TCP)
|
|
|
|
|
|
|
|
# raw = pkg.getlayer(Raw)
|
|
|
|
|
|
|
|
# return "{}:{} => {}:{}: flags={} seq={} ack={} raw={}".format(
|
|
|
|
# ip.src, tcp.sport,
|
|
|
|
# ip.dst, tcp.dport,
|
|
|
|
# tcp.flags,
|
|
|
|
# tcp.seq,
|
|
|
|
# tcp.ack,
|
|
|
|
# raw)
|
|
|
|
|
|
|
|
|
|
|
|
def recv_msg_cpu(self, pkg):
|
|
|
|
packet = Ether(str(pkg))
|
|
|
|
|
|
|
|
if packet.type == 0x800:
|
|
|
|
pass
|
|
|
|
elif packet.type == 0x86dd:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print("Broken pkg: {}".format(pkg))
|
|
|
|
return
|
|
|
|
|
|
|
|
def run_cpu_port_loop(self):
|
|
|
|
sniff(iface=self.intf, prn=self.recv_msg_cpu)
|
|
|
|
|
2019-02-23 13:22:46 +00:00
|
|
|
def commandline(self):
|
|
|
|
parser = argparse.ArgumentParser(description='controller++')
|
|
|
|
parser.add_argument('--mode', help='Select mode / settings to use', choices=self.modes)
|
|
|
|
args = parser.parse_args()
|
|
|
|
self.mode = args.mode
|
|
|
|
|
|
|
|
|
2019-02-21 22:38:09 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
if "DEBUG" in os.environ:
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
else:
|
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
log.info("Booting...")
|
|
|
|
log.debug("Debug enabled.")
|
|
|
|
|
|
|
|
sw_name = "s1"
|
2019-02-23 13:22:46 +00:00
|
|
|
controller = L2Controller(sw_name)
|
|
|
|
|
|
|
|
controller.commandline()
|
|
|
|
controller.run_cpu_port_loop()
|