def connectToInternet(network, switch='s1', rootip='10.254', subnet='10.0/8'): """Connect the network to the internet switch: switch to connect to root namespace rootip: address for interface in root namespace subnet: Mininet subnet""" switch = network.get(switch) prefixLen = subnet.split('/')[1] # Create a node in root namespace root = Node('root', inNamespace=False) # Prevent network-manager from interfering with our interface fixNetworkManager(root, 'root-eth0') # Create link between root NS and switch link = network.addLink(root, switch) link.intf1.setIP(rootip, prefixLen) # Start network that now includes link to root namespace network.start() # Start NAT and establish forwarding startNAT(root) # Establish routes from end hosts for host in network.hosts: host.cmd('ip route flush root 0/0') host.cmd('route add -net', subnet, 'dev', host.defaultIntf()) host.cmd('route add default gw', rootip) # set up MTU root.cmd('ifconfig root-eth0 mtu 1300') return root
class MyTopo(object): def __init__(self, cname='onos', cips=['10.0.3.1']): # Create network with multiple controllers self.net = Mininet(controller=RemoteController, switch=OVSKernelSwitch, build=False) # Add controllers with input IPs to the network ctrls = [RemoteController(cname, cip, 6633) for cip in cips] for ctrl in ctrls: print ctrl.ip self.net.addController(ctrl) # Add switch self.s2 = self.net.addSwitch('s2', dpid='00000000000000a2') # Connect root namespace to the switch self.root = Node('root', inNamespace=False) intf = self.net.addLink(self.root, self.s2).intf1 self.root.setIP('10.0.0.1/32', intf=intf) # Add host h2 = self.net.addHost('h2', ip='10.0.0.12/24', mac='00:00:00:00:00:02') self.net.addLink(h2, self.s2) def run(self): self.net.build() self.net.start() self.s2.cmd('ovs-vsctl set bridge s2 protocols=OpenFlow13') self.s2.cmd('ovs-vsctl add-port s2 vxlan2') self.s2.cmd('ovs-vsctl set interface vxlan2 type=vxlan option:remote_ip=104.236.158.75 option:key=flow') self.root.cmd('route add -net 10.0.0.0/24 dev root-eth0') CLI(self.net) self.net.stop()
def checkOVS(self): root = Node('root', inNamespace=False) modinfo = root.cmd( "modinfo openvswitch | grep version: |awk -F':' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'" ) versions = modinfo.split("\n") version = versions[0] print "modinfo openviswitch : " + version # SS 2017-10-21 I've disabled the version check because in the packaged openvswitch there is no version info # modversion = float(version[:3]) # if modversion < 2.3: # error( 'OVS Kernel Module does not respect version requirement\nPlease check your OVS installation\n' ) # exit( 1 ) vswitchdinfo = root.cmd( "ovs-vswitchd --version | grep ovs-vswitchd |awk -F')' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'" ) versions = vswitchdinfo.split("\n") version = versions[0] print "ovs-vswitchd --version : " + version vswitchdversion = float(version[:3]) if vswitchdversion < 2.3: error( 'OVS vswitchd does not respect version requirement\nPlease check your OVS installation\n' ) exit(1) # SS 2017-10-21 I've disabled the version check because in the packaged openvswitch there is no version info # if modversion != vswitchdversion: # error( 'OVS Kernel module version and OVS vswitchd version are different\nPlease check your OVS installation\n' ) # exit( 1) openvswitchd = root.cmd('ls %s 2> /dev/null | wc -l' % self.ovs_initd)
def checkOVS(self): root = Node('root', inNamespace=False) modinfo = root.cmd("modinfo openvswitch | grep version: |awk -F':' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'") versions = modinfo.split("\n") version = versions[0] print "modinfo openviswitch : " + version # SS 2017-10-21 I've disabled the version check because in the packaged openvswitch there is no version info # modversion = float(version[:3]) # if modversion < 2.3: # error( 'OVS Kernel Module does not respect version requirement\nPlease check your OVS installation\n' ) # exit( 1 ) vswitchdinfo = root.cmd("ovs-vswitchd --version | grep ovs-vswitchd |awk -F')' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'") versions = vswitchdinfo.split("\n") version = versions[0] print "ovs-vswitchd --version : " + version #vswitchdversion = float(version[:3]) #if vswitchdversion < 2.3: major = version.split(".")[0] minor = version.split(".")[1] if major < 2: error( 'OVS vswitchd does not respect version requirement\nPlease check your OVS installation\n' ) exit( 1 ) if minor < 3: error( 'OVS vswitchd does not respect version requirement\nPlease check your OVS installation\n' ) exit( 1 ) # SS 2017-10-21 I've disabled the version check because in the packaged openvswitch there is no version info # if modversion != vswitchdversion: # error( 'OVS Kernel module version and OVS vswitchd version are different\nPlease check your OVS installation\n' ) # exit( 1) openvswitchd = root.cmd('ls %s 2> /dev/null | wc -l' % self.ovs_initd)
def connectToRootNS(network, switch, ip, routes): root = Node('root', inNamespace=False) intf = network.addLink(root, switch).intf1 root.setIP(ip, intf=intf) network.start() for route in routes: root.cmd('route add -net {} dev {} '.format(route, intf))
def checkOVS(self): root = Node('root', inNamespace=False) modinfo = root.cmd("modinfo openvswitch | grep version: |awk -F':' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'") versions = modinfo.split("\n") version = versions[0] modversion = float(version[:3]) if modversion < 2.3: error( 'OVS Kernel Module does not respect version requirement\nPlease check your OVS installation\n' ) exit( 1 ) vswitchdinfo = root.cmd("ovs-vswitchd --version | grep ovs-vswitchd |awk -F')' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'") versions = vswitchdinfo.split("\n") version = versions[0] vswitchdversion = float(version[:3]) if vswitchdversion < 2.3: error( 'OVS vswitchd does not respect version requirement\nPlease check your OVS installation\n' ) exit( 1 ) if modversion != vswitchdversion: error( 'OVS Kernel module version and OVS vswitchd version are different\nPlease check your OVS installation\n' ) exit( 1) openvswitchd = root.cmd('ls %s 2> /dev/null | wc -l' % self.ovs_initd) if '1' not in openvswitchd: error( 'Cannot find required executable /etc/init.d/openvswitchd\nPlease make sure that OVS is properly installed\n') exit( 1 )
def startNetwork(network, switch, ip, routes): root = Node('root', inNamespace=False) intf = Link(root, switch).intf1 root.setIP(ip, intf=intf) network.start() for route in routes: root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) )
def connectToRootNS(net, ip='10.123.123.1', mac='00123456789A', prefixLen=8, routes=['10.0.0.0/8']): print "*** Creating controller" c0 = net.addController('c0', ip='127.0.0.1', port=6633) rootswitch = net.addSwitch('roots1', ) rootswitch.dpid = 'FFFFFFFFFFFFFFFF' # Connect hosts to root namespace via switch. Starts network. # network: Mininet() network object # ip: IP address for root namespace node # prefixLen: IP address prefix length (e.g. 8, 16, 24) # routes: host networks to route to" # Create a node in root namespace and link to switch 0 root = Node('root', inNamespace=False) intf = Link(root, rootswitch).intf1 intf.setMAC(mac) root.setIP(ip, prefixLen, intf) print "*** Added Interface", str(intf), "Connected To Root-NameSpace" fixNetworkManager(str(intf)) for host in net.hosts: net.addLink(host, rootswitch) # Add routes from root ns to hosts for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf)) root.cmd('service network-manager restart') return rootswitch
def connectToRootNS(network, switch, ip, routes): root = Node('root', inNamespace=False) intf = network.addLink(root, switch).intf1 root.setIP(ip, intf=intf) network.start() for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf))
def start(): global net, attacker, running if running: return '\nServer already running.\n' setLogLevel('info') topo = MixTopo() net = Mininet(topo=topo) s1 = net['s1'] plc2 = net['plc2'] plc3 = net['plc3'] s2, rtu2a, scada = net.get('s2', 'rtu2a', 'scada') rtu2b, attacker2 = net.get('rtu2b', 'attacker2') s3 = net.get('s3') # NOTE: root-eth0 interface on the host root = Node('root', inNamespace=False) intf = net.addLink(root, s3).intf1 print('DEBUG root intf: {}'.format(intf)) root.setIP('10.0.0.30', intf=intf) # NOTE: all packet from root to the 10.0.0.0 network root.cmd('route add -net ' + '10.0.0.0' + ' dev ' + str(intf)) net.start() info('Welcome') # NOTE: use for debugging #s1.cmd('tcpdump -i s1-eth1 -w /tmp/s1-eth1.pcap &') #s1.cmd('tcpdump -i s1-eth2 -w /tmp/s1-eth2.pcap &') SLEEP = 0.5 # NOTE: swat challenge 1 and 2 plc3.cmd(sys.executable + ' plc3.py &') sleep(SLEEP) plc2.cmd(sys.executable + ' plc2.py &') sleep(SLEEP) # NOTE: wadi challenge 1 scada.cmd(sys.executable + ' scada.py &') sleep(SLEEP) rtu2a.cmd(sys.executable + ' rtu2a.py &') sleep(SLEEP) # NOTE: wadi challenge 2 rtu2b.cmd(sys.executable + ' rtu2b.py &') sleep(SLEEP) running = True return '\nServer started.\n'
def start(): global net, attacker, running if running: return '\nServer already running.\n' setLogLevel('info') topo = MixTopo() net = Mininet(topo=topo) s1 = net['s1'] plc2 = net['plc2'] plc3 = net['plc3'] s2, rtu2a, scada = net.get('s2', 'rtu2a', 'scada') rtu2b, attacker2 = net.get('rtu2b', 'attacker2') s3 = net.get('s3') # NOTE: root-eth0 interface on the host root = Node('root', inNamespace=False) intf = net.addLink(root, s3).intf1 print('DEBUG root intf: {}'.format(intf)) root.setIP('10.0.0.30', intf=intf) # NOTE: all packet from root to the 10.0.0.0 network root.cmd('route add -net ' + '10.0.0.0' + ' dev ' + str(intf)) net.start() info('Welcome') # NOTE: use for debugging #s1.cmd('tcpdump -i s1-eth1 -w /tmp/s1-eth1.pcap &') #s1.cmd('tcpdump -i s1-eth2 -w /tmp/s1-eth2.pcap &') SLEEP = 0.5 # NOTE: swat challenge 1 and 2 plc3.cmd(sys.executable + ' plc3.py &') sleep(SLEEP) plc2.cmd(sys.executable + ' plc2.py &') sleep(SLEEP) # NOTE: wadi challenge 1 scada.cmd(sys.executable + ' scada.py &') sleep(SLEEP) rtu2a.cmd(sys.executable + ' rtu2a.py &') sleep(SLEEP) # NOTE: wadi challenge 2 rtu2b.cmd(sys.executable + ' rtu2b.py &') sleep(SLEEP) running = True return '\nServer started.\n'
def sshd(net): root = Node('root', inNamespace=False) intf = net.addLink(root, net['s3']).intf1 root.setIP('10.0.0.4/24', intf=intf) net.start() root.cmd('route add -net 10.0.0.0/24 dev ' + str(intf)) for host in net.hosts: host.cmd('/usr/sbin/sshd -D -o UseDNS=no -u0&')
def configure_l2_accessnetwork(): print "*** Configure L2 Access Networks" root = Node( 'root', inNamespace=False ) print "*** Configure L2 Access Ports" for key, value in ACCESS_TO_TAG.iteritems(): print "*** Configure", key, "As Access Port, TAG=", value root.cmd("ovs-vsctl set port %s tag=%s" %(key, value)) print "*** Configure L2 Trunk Ports" for key, value in TRUNK_TO_TAG.iteritems(): print "*** Configure", key, "As Trunk Port, TAG=", value root.cmd("ovs-vsctl set port %s trunks=%s" %(key, value))
def fixSwitchIntf(swi): for i in range(0, len(swi)): for obj in swi[i].nameToIntf: if 'lo' not in obj: fixNetworkManager(obj) fixNetworkManager(swi[i]) root = Node('root', inNamespace=False) print "Restarting Network Manager" time.sleep(10) root.cmd('service network-manager restart') time.sleep(2)
def fixSwitchIntf(swi): for i in range(0, len(swi)): for obj in swi[i].nameToIntf: if 'lo' not in obj: fixNetworkManager(obj) fixNetworkManager(swi[i]) root = Node( 'root', inNamespace=False ) print "Restarting Network Manager" time.sleep(10) root.cmd('service network-manager restart') time.sleep(2)
def checkQuagga(self): root = Node( 'root', inNamespace=False ) zebra = root.cmd('ls %s 2> /dev/null | wc -l' % self.zebra_exec) if '1' not in zebra: error( 'Cannot find required executable zebra\nPlease make sure that Zebra is properly installed in ' + self.quaggaPath + '\n' 'Otherwise change quaggaPath variable according to your configuration\n' ) exit( 1 ) ospfd = root.cmd('ls %s 2> /dev/null | wc -l' % self.ospfd_exec) if '1' not in ospfd: error( 'Cannot find required executable ospfd\nPlease make sure that OSPFD is properly installed in ' + self.quaggaPath + '\n' 'Otherwise change quaggaPath variable according to your configuration\n' ) exit( 1 )
def DeltaNetwork(): #Make topology net = Mininet(topo=None, controller=None, build=False) #Add switch proto = sys.argv[5] s0 = net.addSwitch('s0', dpid='00:00:00:00:00:01', protocols=proto) s1 = net.addSwitch('s1', dpid='00:00:00:00:00:02', protocols=proto) # for connections with DELTA #Add hosts h1 = net.addHost('h1', ip='10.0.0.1/24', mac='00:00:00:00:00:11') h2 = net.addHost('h2', ip='10.0.0.2/24', mac='00:00:00:00:00:22') #Add links net.addLink(s0, h1) net.addLink(s0, h2) net.addLink(s1, h1, intfName2='host-eth') root = Node('root', inNamespace=False) intf = net.addLink(root, s1).intf1 root.setIP('10.0.1.1', intf=intf) # net.build() net.start() f = os.popen( 'ifconfig eth0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') ip = f.read()[:-1] root.cmd('route add -net 10.0.1.0/24 dev ' + str(intf)) h1.cmd("ifconfig host-eth 10.0.1.2/24 netmask 255.255.255.0") h1.cmd('route add -net 10.0.3.0/24 dev host-eth') h1.cmd('route add -net 10.0.3.0/24 gw ' + str(ip) + ' dev host-eth') #Set ip os.system( "sudo ovs-ofctl -O OpenFlow13 add-flow s1 in_port=1,actions=output:2") os.system( "sudo ovs-ofctl -O OpenFlow13 add-flow s1 in_port=2,actions=output:1") #h1.cmd("dhclient eth0") #connect a controller os.system("sudo ovs-vsctl set-controller s0 tcp:" + sys.argv[1] + ":" + sys.argv[2]) h1.cmd("java -jar $HOME/delta-agent-host-1.0-SNAPSHOT.jar " + sys.argv[3] + " " + sys.argv[4]) CLI(net) net.stop()
def scratchNet(cname='controller', cargs='-v ptcp:'): "Create network from scratch using Open vSwitch." info("*** Creating nodes\n") controller = Node('c0', inNamespace=False) switch = Node('s0', inNamespace=False) h0 = Node('h0') h1 = Node('h1') info("*** Creating links\n") Link(h0, switch) Link(h1, switch) info("*** Configuring hosts\n") h0.setIP('192.168.123.1/24') h1.setIP('192.168.123.2/24') info(str(h0) + '\n') info(str(h1) + '\n') info("*** Starting network using Open vSwitch\n") controller.cmd(cname + ' ' + cargs + '&') switch.cmd('ovs-vsctl del-br dp0') switch.cmd('ovs-vsctl add-br dp0') for intf in switch.intfs.values(): print switch.cmd('ovs-vsctl add-port dp0 %s' % intf) # Note: controller and switch are in root namespace, and we # can connect via loopback interface s_cmd = 'ovs-vsctl set-controller dp0 tcp:[{}]:{}'.format( CTLR_IP, CTLR_PRT) print s_cmd switch.cmd(s_cmd) ping_results = [ 'received,host,jitter,packet_loss,avgping,minping,time,sent,maxping\n' ] try: h0.cmd('echo "" > pings.txt') if mode == 0: step_wise_testing(h0, h1, ping_results) else: continuous_testing(h0, h1, ping_results) except KeyboardInterrupt: print "Warning: Caught KeyboardInterrupt, stopping network" tm_local = time.localtime() dt = time.gmtime() file_name = 'pings_{}_{}_{}-{}_{}_{}.csv'.format( dt.tm_year, dt.tm_mon, dt.tm_mday, tm_local.tm_hour, tm_local.tm_min, tm_local.tm_sec) f = open(file_name, 'w+') for item in ping_results: f.write(item) stop_net(controller, cname, switch)
def enableSSH(self, net): print 'Enabling ssh' routes = ['10.0.0.0/24'] ip = '10.123.123.1/32' switch = self.CoreSwitchList[0] print switch switch = net[switch] root = Node('root', inNamespace=False) intf = Link(root, switch).intf1 root.setIP(ip, intf=intf) net.start() for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf))
def __init__(self, verbose=False): self.checkPATHs() Mininet.__init__(self, build=False) self.cr_oshis = [] self.pe_oshis = [] self.ce_routers = [] self.ctrls = [] self.nodes_in_rn = [] self.node_to_data = defaultdict(list) self.node_to_node = {} self.node_to_default_via = {} self.coex = {} self.verbose = verbose lg.setLogLevel('info') self.vlls = [] self.node_to_pw_data = defaultdict(list) self.pws = [] self.cer_to_customer = {} self.customer_to_vtepallocator = {} self.vsfs = [] self.pe_cer_to_vsf = {} self.is_vs = False self.vss = [] self.vss_data = [] self.id_peo_to_vs = {} self.last_ipnet = IPv4Network(u'0.0.0.0/24') self.id_to_node = {} self.ip_to_mac = {} self.overall_info = {} self.mgmt = None root = Node( 'root', inNamespace=False ) root.cmd('/etc/init.d/network-manager stop') mylog("*** Stop Network Manager\n") self.cluster_to_ctrl = defaultdict(list) self.cluster_to_nodes = defaultdict(list) self.nodes_to_cluster = {}
def __init__(self, verbose=False): self.checkPATHs() Mininet.__init__(self, build=False) self.cr_oshis = [] self.pe_oshis = [] self.ce_routers = [] self.ctrls = [] self.nodes_in_rn = [] self.node_to_data = defaultdict(list) self.node_to_node = {} self.node_to_default_via = {} self.coex = {} self.verbose = verbose lg.setLogLevel('info') self.vlls = [] self.node_to_pw_data = defaultdict(list) self.pws = [] self.cer_to_customer = {} self.customer_to_vtepallocator = {} self.vsfs = [] self.pe_cer_to_vsf = {} self.is_vs = False self.vss = [] self.vss_data = [] self.id_peo_to_vs = {} self.last_ipnet = IPv4Network(u'0.0.0.0/24') self.id_to_node = {} self.ip_to_mac = {} self.overall_info = {} self.mgmt = None root = Node('root', inNamespace=False) root.cmd('/etc/init.d/network-manager stop') mylog("*** Stop Network Manager\n") self.cluster_to_ctrl = defaultdict(list) self.cluster_to_nodes = defaultdict(list) self.nodes_to_cluster = {}
def connectToRootNS(network, switch, ip, routes): root = Node('root', inNamespace=False) intf = network.addLink(root, switch).intf1 root.setIP(ip, intf=intf) root.setMAC("10:00:00:00:00:00") network.start() sleep(10) for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf)) print("route add completed=================") for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf)) print("route add completed=================")
def connectToRootNS( network, switch, ip, routes ): """Connect hosts to root namespace via switch. Starts network. network: Mininet() network object switch: switch to connect to root namespace ip: IP address for root namespace node routes: host networks to route to""" # Create a node in root namespace and link to switch 0 root = Node( 'root', inNamespace=False ) intf = network.addLink( root, switch ).intf1 root.setIP( ip, intf=intf ) # Start network that now includes link to root namespace network.start() # Add routes from root ns to hosts for route in routes: root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) )
def connectToRootNS(network, switch, ip, routes): """Connect hosts to root namespace via switch. Starts network. network: Mininet() network object switch: switch to connect to root namespace ip: IP address for root namespace node routes: host networks to route to""" # Create a node in root namespace and link to switch 0 root = Node('root', inNamespace=False) intf = network.addLink(root, switch).intf1 root.setIP(ip, intf=intf) # Start network that now includes link to root namespace network.start() # Add routes from root ns to hosts for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf))
def connectToRootNS(network, switch, ip, prefixLen, routes): "Connect hosts to root namespace via switch. Starts network." "network: Mininet() network object" "switch: switch to connect to root namespace" "ip: IP address for root namespace node" "prefixLen: IP address prefix length (e.g. 8, 16, 24)" "routes: host networks to route to" # Create a node in root namespace and link to switch 0 root = Node('root', inNamespace=False) intf = TCLink(root, switch).intf1 root.setIP(ip, prefixLen, intf) # Start network that now includes link to root namespace network.start() # Add routes from root ns to hosts for route in routes: root.cmd('route add -net ' + route + ' dev ' + str(intf))
def checkSR(self): root = Node( 'root', inNamespace=False) sr = root.cmd('ls %s 2> /dev/null | wc -l' % self.SR_exec) if '1' not in sr: error( 'Cannot find required executable fpm-of.bin\nPlease make sure that fpm-of.bin is properly installed in ' + self.SR_path + '\n' 'Otherwise change sr_path variable according to your configuration\n' ) exit( 1 )
def start_network(): ''' Start mininet network ''' lg.setLogLevel('info') topo = TreeTopo(1, options.hosts) net = Mininet(topo=topo, switch=OVSBridge, controller=None, waitConnected=True) switch = net['s1'] routes = ['10.0.0.0/24'] root = Node('root', inNamespace=False) interface = net.addLink(root, switch).intf1 root.setIP('10.123.123.1/32', intf=interface) net.start() for route in routes: root.cmd('route add -net {} dev {}'.format(route, interface)) return net
def connectToRootNS( network, switch, ip, prefixLen, routes ): "Connect hosts to root namespace via switch. Starts network." "network: Mininet() network object" "switch: switch to connect to root namespace" "ip: IP address for root namespace node" "prefixLen: IP address prefix length (e.g. 8, 16, 24)" "routes: host networks to route to" # Create a node in root namespace and link to switch 0 root = Node( 'root', inNamespace=False ) intf = TCLink( root, switch ).intf1 root.setIP( ip, prefixLen, intf ) # Start network that now includes link to root namespace network.start() # Add routes from root ns to hosts for route in routes: root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) )
def _ping_set(self, src: Node, dst_dict: Mapping[Node, Union[IPv4Address, IPv6Address, str]], timeout: Optional[str], v4=True) -> Tuple[int, int]: """Do the actual ping to the dict of {dst: dst_ip} from src :param src: origin of the ping :param dst_dict: destinations {dst: dst_ip} of the ping :param timeout: the time to wait for a response, as string :param v4: whether IPv4 or IPv6 is used :param return: a tuple (lost packets, sent packets)""" if len(dst_dict) == 0: return 0, 0 lost = 0 packets = 0 opts = '' if timeout: opts = '-W %s' % timeout log.output("%s --%s--> " % (src.name, "IPv4" if v4 else "IPv6")) for dst, dst_ip in dst_dict.items(): result = src.cmd('%s -c1 %s %s' % ("ping" if v4 else PING6_CMD, opts, dst_ip)) sent, received = self._parsePing(result) lost += sent - received packets += sent log.output("%s " % dst.name if received else "X ") log.output('\n') return lost, packets
def run(): topo = MyTopo() net = Mininet(topo=topo, controller=None, link=TCLink) net.addController('c0', controller=RemoteController, ip='127.0.0.1', port=6633) print("=== Add root ===") root = Node('root', inNamespace=False) intf = net.addLink(root, net['s1']).intf1 root.setIP('192.168.44.100/32', intf=intf) net.start() s8 = net.getNodeByName('s8') h9 = net.getNodeByName('h9') net.configLinkStatus(s8, h9, 'down') print("=== Run apache server ===") portal = net.getNodeByName('portal') portal.cmdPrint('/etc/init.d/apache2 restart') print("=== Run DHCP server ===") dhcp = net.getNodeByName('dhcp') # dhcp.cmdPrint('service isc-dhcp-server restart &') dhcp.cmdPrint( '/usr/sbin/dhcpd -q 4 -pf /run/dhcp-server-dhcpd.pid -cf ./dhcpd.conf %s' % dhcp.defaultIntf()) print("=== Request IP ===") for host in net.hosts: if str(host) != 'portal' and str(host) != 'dhcp' and str( host) != 'web' and str(host) != 'h9': host.cmdPrint('dhclient ' + host.defaultIntf().name) print("=== Open simple HTTP server ===") web = net.getNodeByName('web') web.cmdPrint('python -m SimpleHTTPServer 80 &') print("=== Set route ===") for route in ['192.168.44.0/24']: root.cmd('route add -net ' + route + ' dev ' + str(intf)) CLI(net) net.stop()
def checkQuagga(self): root = Node('root', inNamespace=False) zebra = root.cmd('ls %s 2> /dev/null | wc -l' % self.zebra_exec) if '1' not in zebra: error( 'Cannot find required executable zebra\nPlease make sure that Zebra is properly installed in ' + self.quaggaPath + '\n' 'Otherwise change quaggaPath variable according to your configuration\n' ) exit(1) ospfd = root.cmd('ls %s 2> /dev/null | wc -l' % self.ospfd_exec) if '1' not in ospfd: error( 'Cannot find required executable ospfd\nPlease make sure that OSPFD is properly installed in ' + self.quaggaPath + '\n' 'Otherwise change quaggaPath variable according to your configuration\n' ) exit(1)
def checkQuagga(self): root = Node( 'root', inNamespace=False ) zebra = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.zebra_exec) if '1' not in zebra: OSHI.zebra_exec = OSHI.zebra_exec_2 zebra = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.zebra_exec) if '1' not in zebra: error( 'Cannot find required executable zebra\nPlease make sure that Zebra is properly installed in ' + OSHI.quaggaPath_msg + '\n' 'Otherwise change configuration in Dreamer-Mininet-Extensions/nodes.py \n' ) exit( 1 ) ospfd = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.ospfd_exec) if '1' not in ospfd: OSHI.ospfd_exec = OSHI.ospfd_exec_2 ospfd = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.ospfd_exec) if '1' not in ospfd: error( 'Cannot find required executable ospfd\nPlease make sure that OSPFD is properly installed in ' + OSHI.quaggaPath_msg + '\n' 'Otherwise change configuration in Dreamer-Mininet-Extensions/nodes.py \n' ) exit( 1 )
def checkSR(self): root = Node('root', inNamespace=False) sr = root.cmd('ls %s 2> /dev/null | wc -l' % self.SR_exec) if '1' not in sr: error( 'Cannot find required executable fpm-of.bin\nPlease make sure that fpm-of.bin is properly installed in ' + self.SR_path + '\n' 'Otherwise change sr_path variable according to your configuration\n' ) exit(1)
def configure_vll_pusher(net): print "*** Create Configuration File For Vll Pusher" path = vll_path + "vll_pusher.cfg" vll_pusher_cfg = open(path,"w") for i in range(0, len(LHS_tunnel_aoshi)): aoshi = LHS_tunnel_aoshi[i] lhs_dpid = net.getNodeByName(aoshi).dpid lhs_dpid = ':'.join(s.encode('hex') for s in lhs_dpid.decode('hex')) port = LHS_tunnel_port[i] lhs_port = port aoshi = RHS_tunnel_aoshi[i] rhs_dpid = net.getNodeByName(aoshi).dpid rhs_dpid = ':'.join(s.encode('hex') for s in rhs_dpid.decode('hex')) port = RHS_tunnel_port[i] rhs_port = port vll_pusher_cfg.write("%s|%s|%s|%s|%d|%d|\n" % (lhs_dpid, rhs_dpid, lhs_port, rhs_port, LHS_tunnel_vlan[i], RHS_tunnel_vlan[i])) vll_pusher_cfg.close() root = Node( 'root', inNamespace=False ) root.cmd("chmod 777 %s" %(path))
def fixEnvironment(self): mylog("*** Fix environment\n") for node in self.nodes_in_rn: fixIntf(node) root = Node('root', inNamespace=False) mylog("*** Stop unwanted traffic\n") root.cmd('stop avahi-daemon') #root.cmd('killall dhclient') mylog("*** Kill old processes\n") root.cmd('killall -r zebra') root.cmd('killall -r ospfd') root.cmd('killall sshd') cfile = '/etc/environment' line1 = 'VTYSH_PAGER=more\n' config = open(cfile).read() if (line1) not in config: mylog('*** Adding %s to %s\n' % (line1.strip(), cfile)) with open(cfile, 'a') as f: f.write(line1) f.close() if os.path.exists(self.temp_cfg): os.remove(self.temp_cfg)
def fixEnvironment(self): mylog("*** Fix environment\n") for node in self.nodes_in_rn: fixIntf(node) root = Node( 'root', inNamespace=False ) mylog("*** Stop unwanted traffic\n") root.cmd('stop avahi-daemon') #root.cmd('killall dhclient') mylog("*** Kill old processes\n") root.cmd('killall -r zebra') root.cmd('killall -r ospfd') root.cmd('killall sshd') cfile = '/etc/environment' line1 = 'VTYSH_PAGER=more\n' config = open( cfile ).read() if ( line1 ) not in config: mylog( '*** Adding %s to %s\n' %(line1.strip(), cfile)) with open( cfile, 'a' ) as f: f.write( line1 ) f.close(); if os.path.exists(self.temp_cfg): os.remove(self.temp_cfg)
def scratchNet(cname='controller', cargs='-v ptcp:'): "Create network from scratch using Open vSwitch." info("*** Creating nodes\n") controller = Node('c0', inNamespace=False) switch = Node('s0', inNamespace=False) h0 = Node('h0') h1 = Node('h1') info("*** Creating links\n") Link(h0, switch) Link(h1, switch) info("*** Configuring hosts\n") h0.setIP('192.168.123.1/24') h1.setIP('192.168.123.2/24') info(str(h0) + '\n') info(str(h1) + '\n') info("*** Starting network using Open vSwitch\n") controller.cmd(cname + ' ' + cargs + '&') switch.cmd('ovs-vsctl del-br dp0') switch.cmd('ovs-vsctl add-br dp0') for intf in switch.intfs.values(): print switch.cmd('ovs-vsctl add-port dp0 %s' % intf) # Note: controller and switch are in root namespace, and we # can connect via loopback interface s_cmd = 'ovs-vsctl set-controller dp0 tcp:[{}]:{}'.format( CTLR_IP, CTLR_PRT) print s_cmd switch.cmd(s_cmd) info('*** Waiting for switch to connect to controller') try: while 'is_connected' not in quietRun('ovs-vsctl show'): sleep(1) info('.') info('\n') while True: info("*** Running test\n") h0.cmdPrint('ping -c1 ' + h1.IP()) info("*** Sleep\n") sleep(2) except KeyboardInterrupt: print "Warning: Caught KeyboardInterrupt, stopping network" stop_net(controller, cname, switch)
def checkOVS(self): root = Node('root', inNamespace=False) modinfo = root.cmd( "modinfo openvswitch | grep version: |awk -F':' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'" ) versions = modinfo.split("\n") version = versions[0] modversion = float(version[:3]) if modversion < 2.3: error( 'OVS Kernel Module does not respect version requirement\nPlease check your OVS installation\n' ) exit(1) vswitchdinfo = root.cmd( "ovs-vswitchd --version | grep ovs-vswitchd |awk -F')' '{print $2}' | awk '{ gsub (\" \", \"\", $0); print}'" ) versions = vswitchdinfo.split("\n") version = versions[0] vswitchdversion = float(version[:3]) if vswitchdversion < 2.3: error( 'OVS vswitchd does not respect version requirement\nPlease check your OVS installation\n' ) exit(1) if modversion != vswitchdversion: error( 'OVS Kernel module version and OVS vswitchd version are different\nPlease check your OVS installation\n' ) exit(1) openvswitchd = root.cmd('ls %s 2> /dev/null | wc -l' % self.ovs_initd) if '1' not in openvswitchd: error( 'Cannot find required executable /etc/init.d/openvswitchd\nPlease make sure that OVS is properly installed\n' ) exit(1)
def checkQuagga(self): root = Node('root', inNamespace=False) zebra = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.zebra_exec) if '1' not in zebra: OSHI.zebra_exec = OSHI.zebra_exec_2 zebra = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.zebra_exec) if '1' not in zebra: error( 'Cannot find required executable zebra\nPlease make sure that Zebra is properly installed in ' + OSHI.quaggaPath_msg + '\n' 'Otherwise change configuration in Dreamer-Mininet-Extensions/nodes.py \n' ) exit(1) ospfd = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.ospfd_exec) if '1' not in ospfd: OSHI.ospfd_exec = OSHI.ospfd_exec_2 ospfd = root.cmd('ls %s 2> /dev/null | wc -l' % OSHI.ospfd_exec) if '1' not in ospfd: error( 'Cannot find required executable ospfd\nPlease make sure that OSPFD is properly installed in ' + OSHI.quaggaPath_msg + '\n' 'Otherwise change configuration in Dreamer-Mininet-Extensions/nodes.py \n' ) exit(1)
def connectToInternet( network, switch='s1', rootip='10.254', subnet='10.0/8'): """Connect the network to the internet switch: switch to connect to root namespace rootip: address for interface in root namespace subnet: Mininet subnet""" switch = network.get( switch ) prefixLen = subnet.split( '/' )[ 1 ] routes = [ subnet ] # host networks to route to # Create a node in root namespace root = Node( 'root', inNamespace=False ) # Stop network-manager so it won't interfere root.cmd( 'service network-manager stop' ) # Create link between root NS and switch link = network.addLink( root, switch ) link.intf1.setIP( rootip, prefixLen ) # Start network that now includes link to root namespace network.start() # Start NAT and establish forwarding startNAT( root ) # Establish routes from end hosts for host in network.hosts: host.cmd( 'ip route flush root 0/0' ) host.cmd( 'route add -net', subnet, 'dev', host.defaultIntf() ) host.cmd( 'route add default gw', rootip ) print "*** Hosts are running and should have internet connectivity" print "*** Type 'exit' or control-D to shut down network" CLI( network ) stopNAT( root )
def connectToRootNS(net, num_cloudlets): root = Node('root', inNamespace=False) switch = net['cs0'] ip = config.master_root_ip main_intf = net.addLink(root, switch).intf1 root.setIP(ip, intf=main_intf) # TODO better id? dpid = '%010x' % 130013 device_switch = net.addSwitch('ds', dpid=dpid) intf = net.addLink(root, device_switch).intf1 root.setIP(config.master_to_device_ip, intf=intf) intf = net.addLink(net['device'], device_switch, intf1Name="dev_root", intf2Name="root_dev").intf1 net['device'].setIP(config.device_to_master_ip, intf=intf) # TODO do we need routing rules for device? routes = [ get_ip(a=x, suffix=16) for x in ( config.mesh_network_prefix, config.tower_network_prefix, ) ] for i in range(1, num_cloudlets): routes.append(get_ip(a=config.cloudlet_network_prefix, c=i, suffix=24)) default_route_ip = get_ip(a=config.cloudlet_network_prefix, d=1) print "root routes" for route in routes: print 'ip route add to %s via %s dev %s' % (route, default_route_ip, main_intf) root.cmd('ip route add to %s via %s dev %s' % (route, default_route_ip, main_intf))
def connectToRootNS( net, ip='10.123.123.1', mac='00123456789A', prefixLen=8, routes=['10.0.0.0/8']): print "*** Creating controller" c0 = net.addController( 'c0', ip='127.0.0.1', port=6633 ) rootswitch = net.addSwitch('roots1',) rootswitch.dpid = 'FFFFFFFFFFFFFFFF' # Connect hosts to root namespace via switch. Starts network. # network: Mininet() network object # ip: IP address for root namespace node # prefixLen: IP address prefix length (e.g. 8, 16, 24) # routes: host networks to route to" # Create a node in root namespace and link to switch 0 root = Node( 'root', inNamespace=False ) intf = Link( root, rootswitch ).intf1 intf.setMAC(mac) root.setIP( ip, prefixLen, intf ) print "*** Added Interface", str(intf), "Connected To Root-NameSpace" fixNetworkManager(str(intf)) for host in net.hosts: net.addLink(host,rootswitch) # Add routes from root ns to hosts for route in routes: root.cmd( 'route add -net ' + route + ' dev ' + str( intf ) ) root.cmd('service network-manager restart') return rootswitch
class testHwintf(unittest.TestCase): prompt = 'mininet>' def setUp(self): self.h3 = Node('t0', ip='10.0.0.3/8') self.n0 = Node('t1', inNamespace=False) Link(self.h3, self.n0) self.h3.configDefault() def testLocalPing(self): "Verify connectivity between virtual hosts using pingall" p = pexpect.spawn('python -m mininet.examples.hwintf %s' % self.n0.intf()) p.expect(self.prompt) p.sendline('pingall') p.expect('(\d+)% dropped') percent = int(p.match.group(1)) if p.match else -1 self.assertEqual(percent, 0) p.expect(self.prompt) p.sendline('exit') p.wait() def testExternalPing(self): "Verify connnectivity between virtual host and virtual-physical 'external' host " p = pexpect.spawn('python -m mininet.examples.hwintf %s' % self.n0.intf()) p.expect(self.prompt) # test ping external to internal expectStr = '(\d+) packets transmitted, (\d+) received' m = re.search(expectStr, self.h3.cmd('ping -v -c 1 10.0.0.1')) tx = m.group(1) rx = m.group(2) self.assertEqual(tx, rx) # test ping internal to external p.sendline('h1 ping -c 1 10.0.0.3') p.expect(expectStr) tx = p.match.group(1) rx = p.match.group(2) self.assertEqual(tx, rx) p.expect(self.prompt) p.sendline('exit') p.wait() def tearDown(self): self.h3.stop(deleteIntfs=True) self.n0.stop(deleteIntfs=True)
class testHwintf( unittest.TestCase ): prompt = 'mininet>' def setUp( self ): self.h3 = Node( 't0', ip='10.0.0.3/8' ) self.n0 = Node( 't1', inNamespace=False ) Link( self.h3, self.n0 ) self.h3.configDefault() def testLocalPing( self ): "Verify connectivity between virtual hosts using pingall" p = pexpect.spawn( 'python -m mininet.examples.hwintf %s' % self.n0.intf() ) p.expect( self.prompt ) p.sendline( 'pingall' ) p.expect ( '(\d+)% dropped' ) percent = int( p.match.group( 1 ) ) if p.match else -1 self.assertEqual( percent, 0 ) p.expect( self.prompt ) p.sendline( 'exit' ) p.wait() def testExternalPing( self ): "Verify connnectivity between virtual host and virtual-physical 'external' host " p = pexpect.spawn( 'python -m mininet.examples.hwintf %s' % self.n0.intf() ) p.expect( self.prompt ) # test ping external to internal expectStr = '(\d+) packets transmitted, (\d+) received' m = re.search( expectStr, self.h3.cmd( 'ping -v -c 1 10.0.0.1' ) ) tx = m.group( 1 ) rx = m.group( 2 ) self.assertEqual( tx, rx ) # test ping internal to external p.sendline( 'h1 ping -c 1 10.0.0.3') p.expect( expectStr ) tx = p.match.group( 1 ) rx = p.match.group( 2 ) self.assertEqual( tx, rx ) p.expect( self.prompt ) p.sendline( 'exit' ) p.wait() def tearDown( self ): self.h3.stop( deleteIntfs=True ) self.n0.stop( deleteIntfs=True )
core=numberOfCpuCores, cpu=cpuUsagePercent) virtualNetwork.addLink(node1=host, node2=switch) print " Creating root node ..." rootNode = Node(name="root", inNamespace=False, ip="10.0.3.0") configFileName = "/etc/network/interfaces" with open(configFileName, "r") as configFile: config = configFile.read() manualConfigLine = "\niface root-eth0 inet manual\n" if manualConfigLine not in config: print " Adding manual configuration to", configFileName, "..." with open(configFileName, "a") as configFile: configFile.write(manualConfigLine) rootNode.cmd("service network-manager restart") print " Linking root namespace to switch ..." rootLink = virtualNetwork.addLink(rootNode, switch) rootLink.intf1.setIP("10.254", "8") virtualNetwork.start() print "Network created." print print "Testing network:" virtualNetwork.pingAll() print "Network test complete." print def startNAT( root, inetIntf='eth0', subnet='10.0/8' ):
def init_net(net): "Init Function" root = Node( 'root', inNamespace=False ) root.cmd('stop avahi-daemon') root.cmd('killall dhclient') root.cmd('killall zebra') root.cmd('killall ospfd') fixEnvironment() print "*** Restarting Network Manager" time.sleep(10) root.cmd('service network-manager restart') time.sleep(2) net.start() # Configure the ctrl for ctrl in ctrls: configure_env_ctrl(ctrl) for oshi in oshis: configure_env_oshi(oshi) for oshi in oshis: strip_ip(oshi) path_quagga_conf = "/tmp/" + oshi.name + "/quagga" oshi.cmd("%szebra -f %s/zebra.conf -A 127.0.0.1 &" %(path_quagga_exec, path_quagga_conf)) oshi.cmd("%sospfd -f %s/ospfd.conf -A 127.0.0.1 &" %(path_quagga_exec, path_quagga_conf)) for aoshi in aoshis: configure_env_oshi(aoshi) for aoshi in aoshis: strip_ip(aoshi) path_quagga_conf = "/tmp/" + aoshi.name + "/quagga" aoshi.cmd("%szebra -f %s/zebra.conf -A 127.0.0.1 &" %(path_quagga_exec, path_quagga_conf)) aoshi.cmd("%sospfd -f %s/ospfd.conf -A 127.0.0.1 &" %(path_quagga_exec, path_quagga_conf)) for ctrl in ctrls: path_quagga_conf = "/tmp/" + ctrl.name + "/quagga" ctrl.cmd("%szebra -f %s/zebra.conf -A 127.0.0.1 &" %(path_quagga_exec, path_quagga_conf)) ctrl.cmd("%sospfd -f %s/ospfd.conf -A 127.0.0.1 &" %(path_quagga_exec, path_quagga_conf)) print "*** Configuring Hosts" i = 0 for i in range(len(hosts)): host = net.getNodeByName(hosts[i]) configure_node(host) configure_standalone_sw(switches) configure_l2_accessnetwork() # Configure VLL Pusher configure_vll_pusher(net) print "*** Type 'exit' or control-D to shut down network" CLI( net ) net.stop() subprocess.call(["sudo", "mn", "-c"], stdout=None, stderr=None) for oshi in oshis: clean_env(oshi) for aoshi in aoshis: clean_env(aoshi) for ctrl in ctrls: clean_env(ctrl) path = vll_path + "vlls.json" if(os.path.exists(path)): print "*** Remove Vlls DB File" os.remove(path) print '*** Unmounting host bind mounts' root.cmd('service network-manager restart') root.cmd('start avahi-daemon') root.cmd('killall ovsdb-server') root.cmd('killall ovs-vswitchd') root.cmd('killall zebra') root.cmd('killall ospfd') root.cmd('/etc/init.d/openvswitch-switch restart') unmountAll()
info("*** Read Configuration File For VS deployer\n") path = "vsf.cfg" if os.path.exists(path): conf = open(path,'r') deployer_cfg = json.load(conf) conf.close() else: error("No Configuration File Find In %s\n" % path) exit(-2) if __name__ == '__main__': lg.setLogLevel('info') read_conf_file() root = Node( 'root', inNamespace=False ) mgt_ip = (root.cmd("ip -4 addr show dev eth0 | grep -m 1 \"inet \" | awk '{print $2}' | cut -d \"/\" -f 1")[:-1]) mgt_ip = mgt_ip.strip(' \t\n\r') vsfs = [] for vsf in deployer_cfg["vsfs"]: vm = vsf['vm'].strip(' \t\n\r') if mgt_ip == vm: vsfs = vsf['vsfs'] if len(vsfs) > 0: info("*** Create %s vsfs\n" % len(vsfs)) net = MininetVS(deployer_cfg['tableIP']) i = 0 for vsf in vsfs: vsfname = str(vsf['name']) info("*** Create %s\n" % vsfname) net.addVSF(vsfname)
def scratchNet(cname="controller", cargs="-v ptcp:"): "Create network from scratch using Open vSwitch." info("*** Creating nodes\n") controller = Node("c0", inNamespace=False) switch = Node("s0", inNamespace=False) h0 = Node("h0") h1 = Node("h1") info("*** Creating links\n") Link(h0, switch) Link(h1, switch) info("*** Configuring hosts\n") h0.setIP("192.168.123.1/24") h1.setIP("192.168.123.2/24") info(str(h0) + "\n") info(str(h1) + "\n") info("*** Starting network using Open vSwitch\n") controller.cmd(cname + " " + cargs + "&") switch.cmd("ovs-vsctl del-br dp0") switch.cmd("ovs-vsctl add-br dp0") for intf in switch.intfs.values(): print(switch.cmd("ovs-vsctl add-port dp0 %s" % intf)) # Note: controller and switch are in root namespace, and we # can connect via loopback interface switch.cmd("ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633") info("*** Waiting for switch to connect to controller") while "is_connected" not in quietRun("ovs-vsctl show"): sleep(1) info(".") info("\n") info("*** Running test\n") h0.cmdPrint("ping -c1 " + h1.IP()) info("*** Stopping network\n") controller.cmd("kill %" + cname) switch.cmd("ovs-vsctl del-br dp0") switch.deleteIntfs() info("\n")
def scratchNet( cname='controller', cargs='ptcp:' ): "Create network from scratch using kernel switch." info( "*** Creating nodes\n" ) controller = Node( 'c0', inNamespace=False ) switch = Node( 's0', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) info( "*** Creating links\n" ) createLink( node1=h0, node2=switch, port1=0, port2=0 ) createLink( node1=h1, node2=switch, port1=0, port2=1 ) info( "*** Configuring hosts\n" ) h0.setIP( h0.intfs[ 0 ], '192.168.123.1', 24 ) h1.setIP( h1.intfs[ 0 ], '192.168.123.2', 24 ) info( str( h0 ) + '\n' ) info( str( h1 ) + '\n' ) info( "*** Starting network using Open vSwitch kernel datapath\n" ) controller.cmd( cname + ' ' + cargs + '&' ) switch.cmd( 'ovs-dpctl del-dp dp0' ) switch.cmd( 'ovs-dpctl add-dp dp0' ) for intf in switch.intfs.values(): print switch.cmd( 'ovs-dpctl add-if dp0 ' + intf ) print switch.cmd( 'ovs-openflowd dp0 tcp:127.0.0.1 &' ) info( "*** Running test\n" ) h0.cmdPrint( 'ping -c1 ' + h1.IP() ) info( "*** Stopping network\n" ) controller.cmd( 'kill %' + cname ) switch.cmd( 'ovs-dpctl del-dp dp0' ) switch.cmd( 'kill %ovs-openflowd' ) switch.deleteIntfs() info( '\n' )
def scratchNetUser( cname='controller', cargs='ptcp:' ): "Create network from scratch using user switch." # It's not strictly necessary for the controller and switches # to be in separate namespaces. For performance, they probably # should be in the root namespace. However, it's interesting to # see how they could work even if they are in separate namespaces. info( '*** Creating Network\n' ) controller = Node( 'c0' ) switch = Node( 's0') h0 = Node( 'h0' ) h1 = Node( 'h1' ) cintf, sintf = createLink( controller, switch ) h0intf, sintf1 = createLink( h0, switch ) h1intf, sintf2 = createLink( h1, switch ) info( '*** Configuring control network\n' ) controller.setIP( cintf, '10.0.123.1', 24 ) switch.setIP( sintf, '10.0.123.2', 24 ) info( '*** Configuring hosts\n' ) h0.setIP( h0intf, '192.168.123.1', 24 ) h1.setIP( h1intf, '192.168.123.2', 24 ) info( '*** Network state:\n' ) for node in controller, switch, h0, h1: info( str( node ) + '\n' ) info( '*** Starting controller and user datapath\n' ) controller.cmd( cname + ' ' + cargs + '&' ) switch.cmd( 'ifconfig lo 127.0.0.1' ) intfs = [ sintf1, sintf2 ] switch.cmd( 'ofdatapath -i ' + ','.join( intfs ) + ' ptcp: &' ) switch.cmd( 'ofprotocol tcp:' + controller.IP() + ' tcp:localhost &' ) info( '*** Running test\n' ) h0.cmdPrint( 'ping -c1 ' + h1.IP() ) info( '*** Stopping network\n' ) controller.cmd( 'kill %' + cname ) switch.cmd( 'kill %ofdatapath' ) switch.cmd( 'kill %ofprotocol' ) switch.deleteIntfs() info( '\n' )
def scratchNet( cname='controller', cargs='-v ptcp:' ): "Create network from scratch using Open vSwitch." info( "*** Creating nodes\n" ) controller = Node( 'c0', inNamespace=False ) switch = Node( 's0', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) info( "*** Creating links\n" ) Link( h0, switch ) Link( h1, switch ) info( "*** Configuring hosts\n" ) h0.setIP( '192.168.123.1/24' ) h1.setIP( '192.168.123.2/24' ) info( str( h0 ) + '\n' ) info( str( h1 ) + '\n' ) info( "*** Starting network using Open vSwitch\n" ) controller.cmd( cname + ' ' + cargs + '&' ) switch.cmd( 'ovs-vsctl del-br dp0' ) switch.cmd( 'ovs-vsctl add-br dp0' ) for intf in switch.intfs.values(): print switch.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) # Note: controller and switch are in root namespace, and we # can connect via loopback interface switch.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' ) info( '*** Waiting for switch to connect to controller' ) while 'is_connected' not in quietRun( 'ovs-vsctl show' ): sleep( 1 ) info( '.' ) info( '\n' ) info( "*** Running test\n" ) h0.cmdPrint( 'ping -c1 ' + h1.IP() ) info( "*** Stopping network\n" ) controller.cmd( 'kill %' + cname ) switch.cmd( 'ovs-vsctl del-br dp0' ) switch.deleteIntfs() info( '\n' )
core=numberOfCpuCores, cpu=cpuUsagePercent) virtualNetwork.addLink(node1=host, node2=switch) print " Creating root node ..." rootNode = Node(name="root", inNamespace=False, ip="10.0.3.0") configFileName = "/etc/network/interfaces" with open(configFileName, "r") as configFile: config = configFile.read() manualConfigLine = "\niface root-eth0 inet manual\n" if manualConfigLine not in config: print " Adding manual configuration to", configFileName, "..." with open(configFileName, "a") as configFile: configFile.write(manualConfigLine) rootNode.cmd("service network-manager restart") print " Linking root namespace to switch ..." rootLink = virtualNetwork.addLink(rootNode, switch) rootLink.intf1.setIP("10.254", "8") virtualNetwork.start() print "Network created." print print "Testing network:" virtualNetwork.pingAll() print "Network test complete." print
def init_net(net): "Init Function" print "*** Init Environment" if toCOMPILE == 1: print "*** Compiling Conet Client, Conet Server and CacheServer" subprocess.call(["sh", "../build_all"], stdout=None, stderr=None) i = 0 j = 0 modulo = len(net.hosts)/3 print "*** Kill Previous Service SNMP/SSH If Active" subprocess.call(["killall", "snmpd"], stdout=None, stderr=None) subprocess.call(["killall", "sshd"], stdout=None, stderr=None) subprocess.call(["killall", "mrtg"], stdout=None, stderr=None) shutil.copy("./install/mrtg/mrtg-temp.cfg", "/home/mrtg/cfg/") mrtg_conf = open("/etc/mrtg-rrd.conf","w") mrtg_conf.write("/home/mrtg/cfg/mrtg-temp.cfg\n") mrtg_conf.close() for host in net.hosts: clean_environment(host.name) print "*** Generating Environment For", host.name os.mkdir("./" + host.name) if 'cli' in (host.name): client_config(host.name, i+1) elif 'ser' in (host.name): server_config(host.name, i+1) elif 'cse' in (host.name): cserver_config(host.name, i+1) host_mrtg_config(host.name, (i+1), (j+1)) i = (i + 1)%modulo j = j + 1 subprocess.call(["sudo", "env", "LANG=C", "/usr/bin/mrtg", "--user=nobody", "--group=nogroup", "/home/mrtg/cfg/mrtg-temp.cfg"],stdout=None,stderr=None) i = 0 j = 0 ip = "" host_conf = open("/opt/lampp/cgi-bin/host.conf","w") host_conf.write("%s\n" %(TOPO)) host_conf.write("%s\n" %(COLUMN)) for i in range(0,modulo): host_conf.write("%s|192.168.0.%s\n" %(net.hosts[i],i+1)) host_conf.write("%s|192.168.64.%s\n" %(net.hosts[i+modulo],i+1)) host_conf.write("%s|192.168.128.%s\n" %(net.hosts[i+(2*modulo)],i+1)) for host in net.hosts: host_snmp_config(host.name) host.cmd('/usr/sbin/snmpd -Lsd -Lf /dev/null -u snmp -g snmp -I -smux -p /var/run/' + host.name + '-snmp.pid -c /tmp/' + host.name + '/snmp/snmpd.conf -C &') host.cmd('/usr/sbin/sshd -D &') print "***", host.name, "is running snmpd and sshd on " + host.name + "-eth0 10.0.0.%s" % (j+1) j = j + 1 switch_conf = open("/opt/lampp/cgi-bin/switch.conf","w") for sw in net.switches: if 'FFFFFFFFFFFFFFFF' not in (sw.dpid): switch_conf.write("%s|%s\n" %(sw.name,sw.dpid)) if '_c' in sw.name: host_conf.write("csw%s|%s\n" %(sw.name[:-2],sw.dpid)) #cswic3|0001000000000001 switch_conf.close() host_conf.close() root = Node( 'root', inNamespace=False ) root.cmd('stop avahi-daemon') root.cmd('killall dhclient') conf_portsToBeMonitored() net.start() print "*** Type 'exit' or control-D to shut down network" CLI( net ) net.stop() subprocess.call(["sudo", "mn", "-c"], stdout=None, stderr=None) for host in net.hosts: print "*** Cleaning Environment For", host.name clean_environment(host.name) if 'cse' not in (host.name): print "*** Kill ", host.name + "-ccnd" subprocess.call(["sudo", "killall", host.name + "-ccnd"], stdout=None, stderr=None) print "Kill Service SNMP/SSH" subprocess.call(["killall", "snmpd"], stdout=None, stderr=None) subprocess.call(["killall", "sshd"], stdout=None, stderr=None) subprocess.call(["killall", "mrtg"], stdout=None, stderr=None) os.remove("/home/mrtg/cfg/mrtg-temp.cfg") root.cmd('service network-manager restart') root.cmd('start avahi-daemon')
def start(self): """Start mininet emulation returns the overall_info with all the nodes and link information """ self.fixEnvironment() if not self.built: self.build() ip_to_mac_file = open('/tmp/ip_to_mac.cfg', 'w') #TODO it does not work in multiple users environment ip_to_mac_file.write(json.dumps(self.ip_to_mac, sort_keys=True, indent=4)) ip_to_mac_file.close() mylog( '*** Starting %s cr oshis\n' % len(self.cr_oshis) ) for cr_oshi in self.cr_oshis: cluster = self.nodes_to_cluster[cr_oshi.name] ctrls_names = [] ctrls_names = self.cluster_to_ctrl[cluster] ctrls = [] for ctrl_name in ctrls_names: ctrls.append(self.getNodeByName(ctrl_name)) cr_oshi.start(ctrls, self.node_to_data[cr_oshi.name], self.coex) coexFactory = CoexFactory() coex = coexFactory.getCoex(self.coex['coex_type'], self.coex['coex_data'], [], [], "", OSHI.OF_V) mylog( '\n' ) mylog( '*** Starting %s pe oshis\n' % len(self.pe_oshis) ) for pe_oshi in self.pe_oshis: cluster = self.nodes_to_cluster[pe_oshi.name] ctrls_names = self.cluster_to_ctrl[cluster] ctrls = [] for ctrl_name in ctrls_names: ctrls.append(self.getNodeByName(ctrl_name)) pe_oshi.start(ctrls, self.node_to_data[pe_oshi.name], self.coex) mylog( '\n' ) mylog( '*** Starting %s vsfs\n' % len(self.vsfs) ) for vsf in self.vsfs: vsf.start(self.node_to_pw_data[vsf.name]) mylog( '\n' ) mylog( '*** Starting %s in band controllers\n' % len(self.ctrls) ) for controller in self.ctrls: controller.start(self.node_to_default_via[controller.name]) mylog( '\n' ) mylog( '*** Starting %s ce routers\n' % len(self.ce_routers) ) for ce_router in self.ce_routers: ce_router.start(self.node_to_default_via[ce_router.name]) mylog( '\n' ) mylog( '*** Starting management server\n') self.mgmt.start(self.node_to_default_via[self.mgmt.name]) mylog( '\n' ) vscfg_file = open('vs_selector.cfg', 'w') vscfg = {} vscfg['tableSBP'] = coex.tableSBP vscfg['tableIP'] = coex.tableIP customers = {} for customer, vtep_allocator in self.customer_to_vtepallocator.iteritems(): customers[customer]= vtep_allocator.next_hostAddress() vscfg['customers_vtep']=customers vscfg['last_ipnet']=self.last_ipnet.__str__() vscfg['vss']=self.vss_data vscfg_file.write(json.dumps(vscfg, sort_keys=True, indent=4)) vscfg_file.close() if self.is_vs: """ if 'DISPLAY' not in os.environ: error( "Error starting terms: Cannot connect to display\n" ) return mylog( "*** Running ctrls terms on %s\n" % os.environ[ 'DISPLAY' ] ) cleanUpScreens() self.terms += makeTerms( self.ctrls, 'controller' ) self.terms += makeTerms( self.ctrls, 'controller2' ) mylog("*** Waiting for the creation of the file %s" % self.temp_cfg) mylog("\n") """ mylog("*** Starting VS selection\n") shutil.copyfile("vs_selector.cfg", "ext/vs_selector.cfg") controller = self.ctrls[0] mylog("*** Launch RYU Controller\n") controller.cmd('cd', MininetOSHI.RYU_PATH) controller.cmd('ryu-manager', '--observe-links', 'rest_topology.py', 'ofctl_rest.py', "&") controller.cmd('cd', "%s/ext/" % MininetOSHI.PROJECT_PATH) mylog("*** Launch VS Selector\n") while not os.path.exists(self.temp_cfg): controller.cmd('./vs_selector.py','--controller localhost:8080', MininetOSHI.VS_OPTION) time.sleep(5) root = Node( 'root', inNamespace=False ) mylog("*** Kill all processes started\n") root.cmd('killall ryu-manager') self.configureVS() mylog( '*** Starting and configuring %s vss\n' % len(self.vss) ) for vs in self.vss: vs.start(self.node_to_pw_data[vs.name]) mylog( '\n' ) for cr_oshi in self.cr_oshis: cr_oshi.start_pw(coex.tableIP, self.node_to_pw_data[cr_oshi.name]) for pe_oshi in self.pe_oshis: pe_oshi.start_pw(coex.tableIP, self.node_to_pw_data[pe_oshi.name]) vllcfg_file = open('vll_pusher.cfg','w') vllcfg = {} vllcfg['tableSBP'] = coex.tableSBP vllcfg['tableIP'] = coex.tableIP vllcfg['vlls'] = self.vlls vllcfg['pws'] = self.pws vllcfg_file.write(json.dumps(vllcfg, sort_keys=True, indent=4)) vllcfg_file.close() mylog("*** Nodes are running sshd at the following addresses\n") for host in self.hosts: if "vs" not in host.name: mylog("*** %s is running sshd at the following address %s\n" %(host.name, host.IP())) self.overall_info[host.name]['mgt_IP']=host.IP() return (self.overall_info)
def myNet(cname='controller', cargs='-v ptcp:'): "Create network from scratch using Open vSwitch." info( "*** Creating nodes\n" ) controller = Node( 'c0', inNamespace=False ) s0 = Node( 's0', inNamespace=False ) s1 = Node( 's1', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) h2 = Node( 'h2' ) info( "*** Creating links..." ) linkopts0=dict(bw=10, delay='1ms', loss=0) info( '\nLink h0-s0 | ' ) TCLink( h0, s0, **linkopts0) info( '\nLink h1-s0 | ' ) TCLink( h1, s0, **linkopts0) info( '\nLink s0-s1 | ' ) TCLink( s0, s1, **linkopts0) info( '\nLink s1-h2 | ' ) TCLink( s1, h2, **linkopts0) info( '\n' ) info( "*** Configuring hosts...\n" ) h0.setIP( '192.168.1.1/24' ) h1.setIP( '192.168.1.2/24' ) h2.setIP( '192.168.1.3/24' ) info( "*** Starting network using Open vSwitch...\n" ) s0.cmd( 'ovs-vsctl del-br s0' ) s0.cmd( 'ovs-vsctl add-br s0' ) s1.cmd( 'ovs-vsctl del-br s1' ) s1.cmd( 'ovs-vsctl add-br s1' ) controller.cmd( cname + ' ' + cargs + '&' ) for intf in s0.intfs.values(): print intf print s0.cmd( 'ovs-vsctl add-port s0 %s' % intf ) for intf in s1.intfs.values(): print intf print s1.cmd( 'ovs-vsctl add-port s1 %s' % intf ) # Note: controller and switch are in root namespace, and we # can connect via loopback interface info( '*** Connect to controller with tcp port...' ) s0.cmd( 'ovs-vsctl set-controller s0 tcp:127.0.0.1:6633' ) s1.cmd( 'ovs-vsctl set-controller s0 tcp:127.0.0.1:6633' ) info( '\n' ) info( '*** Waiting for switch to connect to controller..' ) while 'is_connected' not in quietRun( 'ovs-vsctl show' ): sleep( 1 ) info( '.' ) info( 'connected!\n' ) #print s0.cmd('ovs-ofctl show dp0') info( "*** Running test...\n" ) info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' ) h0.cmdPrint( 'ping -c 2 ' + h2.IP() ) info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' ) h1.cmdPrint( 'ping -c 2 ' + h2.IP() ) info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' ) info( '*** Starting iperf Server...\n' ) h2.cmdPrint('iperf -s -i 1 > bandwidth_result &') info( '*** Original link bandwidth testing...\n' ) print "iperf: h0--s0--s1--h2" h0.cmdPrint('iperf -c 192.168.1.3 -t 15') info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' ) print "iperf: h1--s0--s1--h2" h1.cmdPrint('iperf -c 192.168.1.3 -t 15') info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' ) print "!!! Limiting the bandwidth for flow entry [h0] -> [h2]" s0.cmdPrint('ethtool -K s0-eth2 gro off') s0.cmdPrint('tc qdisc del dev s0-eth2 root') s0.cmdPrint('tc qdisc add dev s0-eth2 root handle 1: cbq avpkt 1000 bandwidth 10Mbit') s0.cmdPrint('tc class add dev s0-eth2 parent 1: classid 1:1 cbq rate 512kbit allot 1500 prio 5 bounded isolated') s0.cmdPrint('tc filter add dev s0-eth2 parent 1: protocol ip prio 16 u32 match ip src 192.168.1.1 flowid 1:1') s0.cmdPrint('tc qdisc add dev s0-eth2 parent 1:1 sfq perturb 10') info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' ) info( '\n*** Limited link bandwidth testing...\n' ) print "iperf: h0--s0--s1--h2" h0.cmdPrint('iperf -c 192.168.1.3 -t 15') info( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' ) print "iperf: h1--s0--s1--h2" h1.cmdPrint('iperf -c 192.168.1.3 -t 15') info( '*** Done...\n' ) info( "*** Stopping network...\n" ) controller.cmd( 'kill %' + cname ) s0.cmd( 'ovs-vsctl del-br s0' ) s0.deleteIntfs() s1.cmd( 'ovs-vsctl del-br s1' ) s1.deleteIntfs() info( '\n' )
s1 = net['s1'] plc2 = net['plc2'] plc3 = net['plc3'] s2, rtu2a, scada = net.get('s2', 'rtu2a', 'scada') rtu2b, attacker2 = net.get('rtu2b', 'attacker2') s3 = net.get('s3') # NOTE: root-eth0 interface on the host root = Node('root', inNamespace=False) intf = net.addLink(root, s3).intf1 print('DEBUG root intf: {}'.format(intf)) root.setIP('10.0.0.30', intf=intf) # NOTE: all packet from root to the 10.0.0.0 network root.cmd('route add -net ' + '10.0.0.0' + ' dev ' + str(intf)) net.start() # NOTE: use for debugging #s1.cmd('tcpdump -i s1-eth1 -w /tmp/s1-eth1.pcap &') #s1.cmd('tcpdump -i s1-eth2 -w /tmp/s1-eth2.pcap &') SLEEP = 0.5 # NOTE: swat challenge 1 and 2 plc3.cmd(sys.executable + ' plc3.py &') sleep(SLEEP) plc2.cmd(sys.executable + ' plc2.py &') sleep(SLEEP)
def scratchNet(cname='controller', cargs='-v ptcp:'): "Create network from scratch using Open vSwitch." info("*** Creating nodes\n") controller = Node('c0', inNamespace=False) switch0 = Node('s0', inNamespace=False) h0 = Node('h0') h1 = Node('h1') info("*** Creating links\n") Link(h0, switch0) Link(h1, switch0) info("*** Configuring hosts\n") h0.setIP('192.168.123.1/24') h1.setIP('192.168.123.2/24') info(str(h0) + '\n') info(str(h1) + '\n') info("*** Starting network using Open vSwitch\n") controller.cmd(cname + ' ' + cargs + '&') switch0.cmd('ovs-vsctl del-br dp0') switch0.cmd('ovs-vsctl add-br dp0') for intf in switch0.intfs.values(): print intf print switch0.cmd('ovs-vsctl add-port dp0 %s' % intf) # Note: controller and switch are in root namespace, and we # can connect via loopback interface switch0.cmd('ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633') switch0.cmd('ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"') switch0.cmd('ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"') info('*** Waiting for switch to connect to controller') while 'is_connected' not in quietRun('ovs-vsctl show'): sleep(1) info('.') info('\n') info("*** Running test\n") h0.cmdPrint('ping -c3 ' + h1.IP()) h1.cmdPrint('ping -c3 ' + h0.IP()) info("*** Stopping network\n") controller.cmd('kill %' + cname) switch0.cmd('ovs-vsctl del-br dp0') switch0.deleteIntfs() info('\n')
def cleanEnvironment(self): mylog("*** Clean environment!!!\n") subprocess.call(["sudo", "mn", "-c"], stdout=None, stderr=None) root = Node( 'root', inNamespace=False ) mylog("*** Restart network-manager\n") root.cmd('/etc/init.d/network-manager restart') mylog("*** Kill all processes started\n") root.cmd('killall ovsdb-server') root.cmd('killall ovs-vswitchd') root.cmd('killall -r zebra') root.cmd('killall -r ospfd') root.cmd('killall sshd') mylog("*** Restart Avahi, Open vSwitch, and sshd\n") root.cmd('/etc/init.d/avahi-daemon start') #root.cmd('/etc/init.d/openvswitchd start') root.cmd('service openvswitch-switch start') root.cmd('/etc/init.d/ssh start') mylog('*** Unmounting host bind mounts\n') unmountAll()