def main(): "Create a simple network" get_ip_setting() topo = TwoServerClientAndRouterTopology() info('*** Creating network\n') net = mininet.net.Mininet(topo=topo, controller=mininet.node.RemoteController, ipBase=IPBASE) net.start() server1, server2, client, router = net.get('server1', 'server2', 'client', 'sw0') s1intf = server1.defaultIntf() s1intf.setIP('%s/8' % IP_SETTING['server1']) s2intf = server2.defaultIntf() s2intf.setIP('%s/8' % IP_SETTING['server2']) clintf = client.defaultIntf() clintf.setIP('%s/8' % IP_SETTING['client']) for host in server1, server2, client: set_default_route(host) # set_default_route_client(client) starthttp(server1) starthttp(server2) CLI(net) stophttp() net.stop()
def main(): mininet.log.setLogLevel("info") net = mininet.net.Mininet(topo=Topo(), switch=mininet.node.UserSwitch, controller=None) net.addController(ip="127.0.0.1", controller=mininet.node.RemoteController) net.start() mininet.cli.CLI(net) net.stop()
environment={ 'MY_IP': '10.0.3.2', 'CLOUD_HOST': '10.0.1.1', 'CLOUD_PORT': 8883, 'START_TIME': 37, 'RUN_INTERVAL': 1, 'RUN_COUNT': 50, 'EMULATION_MODE': 'dev', 'LOG_FILE': '/data/log/d1' + runtime_id + '.log' }, volumes=['/home/hujuntao/log/device:/data/log'], cpu_quota=100000, mem_limit='128m') net.addLink(d1, s4, cls=mininet.link.TCLink, delay="1ms", bw=10) net.start() net.ping([c0, f0, f1, d0, d1]) client = docker.from_env().api while True: device_count = 0 for c in client.containers(): if 'dx_device' in c['Image']: device_count += 1 if device_count == 0: break else: time.sleep(60) # mininet.cli.CLI(net) net.stop()
def WifiNet(): """ Create an Wifi network and add nodes to it. """ net = Mininet() info('*** Adding controller\n') net.addController('c0', controller=RemoteController, ip='127.0.0.1', port=6633) """ Initialize the WifiSegment, please refer ns3.py """ wifi = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifinodes = [] """ Initialize nodes """ for n in nodes: """ Get attributes """ nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodevel = n.get('velocity', None) nodeip = n.get('ip', None) """ Assign the addfunc, please refer Mininet for more details about addHost and addSwitch """ if nodetype is 'host': addfunc = net.addHost elif nodetype is 'switch': addfunc = net.addSwitch else: addfunc = None if nodename is None or addfunc is None: continue """ Add the node into Mininet """ node = addfunc(nodename, ip=nodeip) """ Set the mobility model """ mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) if nodevel is not None: mininet.ns3.setVelocity(node, nodevel[0], nodevel[1], nodevel[2]) """ Append the node into wifinodes """ wifinodes.append(node) """ Initialize Wifi Interfaces """ for wi in wifiintfs: """ Get attributes """ winodename = wi.get('nodename', None) witype = wi.get('type', None) wichannel = wi.get('channel', None) wissid = wi.get('ssid', None) """ Assign the addfunc, please refer the WifiSegment in ns3.py """ if witype is 'sta': addfunc = wifi.addSta elif witype is 'ap': addfunc = wifi.addAp else: addfunc = None if winodename is None or addfunc is None or wichannel is None: continue """ Get wifi node and add it to the TapBridge """ node = getWifiNode(wifinodes, winodename) addfunc(node, wichannel, wissid) """ Initialize Ehternet links between switches """ for cl in links: clnodename1 = cl.get('nodename1', None) clnodename2 = cl.get('nodename2', None) if clnodename1 is None or clnodename2 is None: continue clnode1 = getWifiNode(wifinodes, clnodename1) clnode2 = getWifiNode(wifinodes, clnodename2) if clnode1 is None or clnode2 is None: continue CSMALink(clnode1, clnode2) """ Enable Pcap output""" pcap = Pcap() pcap.enable() print pcap """ Enable netanim output""" anim = Netanim("/tmp/xml/wifi-wired-bridged4.xml", nodes) print anim """ Update node descriptions in the netanim """ for node in wifinodes: anim.UpdateNodeDescription(node.nsNode, str(node) + '-' + str(node.nsNode.GetId())) if isinstance(node, mininet.node.OVSSwitch): color = (0, 255, 0) elif isinstance(node, mininet.node.Host): color = (0, 0, 255) anim.UpdateNodeColor(node.nsNode, color[0], color[1], color[2]) """ Start the simulation """ info('*** Starting network\n') net.start() mininet.ns3.start() info('Testing network connectivity\n') wifinodes[0].cmdPrint('ping 10.10.10.2 -c 3') CLI(net) info('*** Stopping network\n') mininet.ns3.stop() info('*** mininet.ns3.stop()\n') mininet.ns3.clear() info('*** mininet.ns3.clear()\n') net.stop() info('*** net.stop()\n')
def Network(argv): cmd = ns.core.CommandLine() cmd.tcp = None cmd.AddValue("tcp", "iperf bandwidth") cmd.udp = None cmd.AddValue("udp", "iperf jitter and packet loss") cmd.json = None cmd.AddValue("json", "iper3 in JSON format") cmd.distance = None cmd.AddValue("distance", "set distance between routers") cmd.Parse(argv) if cmd.distance is not None: distance = float(cmd.distance) setLogLevel( 'info' ) info( "*** Create an Wifi network and add nodes to it\n" ) net = Mininet(controller=RemoteController, link=CSMALink) info( '*** Adding controller\n' ) net.addController('c0', ip='127.0.0.1', port=6633) info( "*** Creating mobility models\n" ) """ Constant position """ mobilityHelper1 = ns.mobility.MobilityHelper() mobilityHelper1.SetMobilityModel("ns3::ConstantPositionMobilityModel") """ Random walk """ mobilityHelper2 = ns.mobility.MobilityHelper() mobilityHelper2.SetMobilityModel("ns3::RandomWalk2dMobilityModel") info( "*** Creating swtich\n" ) s0 = net.addSwitch('s0', listenPort=6634) info( "*** Creating server node h0\n" ) h0 = net.addHost('h0', mac='aa:aa:aa:aa:aa:01', ip="10.0.1.100/24") info( "*** Creating mobile node h1\n" ) h1 = net.addHost('h1', mac='aa:aa:aa:aa:aa:02', ip="10.0.2.100/24") info( "*** Creating mobile node h2\n" ) h2 = net.addHost('h2', mac='aa:aa:aa:aa:aa:03', ip="10.0.3.100/24") info( "*** Creating access point h3\n" ) h3 = net.addHost('h3', mac='aa:aa:aa:aa:aa:04', ip="10.0.1.1") info( "*** Creating access point h4\n" ) h4 = net.addHost('h4', mac='aa:aa:aa:aa:aa:05', ip="10.0.1.2") info( "*** Adding wired links\n" ) csma0 = CSMALink(s0, h0, DataRate="10Mbps", Delay="1ms") csma1 = CSMALink(s0, h3, DataRate="10Mbps", Delay="1ms") csma2 = CSMALink(s0, h4, DataRate="10Mbps", Delay="1ms") info( "*** Adding wireless links\n" ) wifi1_channelNumber = 1 wifi2_channelNumber = 11 wifiChannelHelper = ns.wifi.YansWifiChannelHelper.Default() wifiChannel = wifiChannelHelper.Create() wifiPhy1 = ns.wifi.YansWifiPhyHelper.Default() wifiPhy2 = ns.wifi.YansWifiPhyHelper.Default() wifi1 = WIFISegment() wifi2 = WIFISegment() wifi1.channel = wifiChannel wifi1.phyhelper = wifiPhy1 if wifi1_channelNumber <= 0 or wifi1_channelNumber > maxChannelNumber: wifi1_channelNumber = random.randint(1, maxChannelNumber) warn("illegal channel number, choose a random channel number %s.\n", wifi1_channelNumber) else: wifiPhy1.Set("ChannelNumber", ns.core.UintegerValue(wifi1_channelNumber)) wifi2.channel = wifiChannel wifi2.phyhelper = wifiPhy2 if wifi2_channelNumber <= 0 or wifi2_channelNumber > maxChannelNumber: wifi2_channelNumber = random.randint(1, maxChannelNumber) warn("illegal channel number, choose a random channel number %s.\n", wifi2_channelNumber) else: wifiPhy2.Set("ChannelNumber", ns.core.UintegerValue(wifi2_channelNumber)) # same YansWifiPhyHelper for the 2 WifiAPs for interference wifiPhy1.SetChannel(wifiChannel) wifiPhy2.SetChannel(wifiChannel) tb_h3 = wifi1.addAp(h3, ssid='ssid1') tb_h1 = wifi1.addSta(h1, ssid='ssid1') tb_h4 = wifi2.addAp(h4, ssid='ssid2') tb_h2 = wifi2.addSta(h2, ssid='ssid2') """ Set IPs for access points """ h3.setIP('10.0.2.1', intf='h3-eth1') h4.setIP('10.0.3.1', intf='h4-eth1') # set mobility model (before setting positions !!!) info("*** Mobility models\n") mininet.ns3.setMobilityModel(s0, mobilityHelper1) print mininet.ns3.getMobilityModel(s0) mininet.ns3.setMobilityModel(h0, mobilityHelper1) print mininet.ns3.getMobilityModel(h0) mininet.ns3.setMobilityModel(h1, mobilityHelper1) print mininet.ns3.getMobilityModel(h1) mininet.ns3.setMobilityModel(h2, mobilityHelper1) print mininet.ns3.getMobilityModel(h2) mininet.ns3.setMobilityModel(h3, mobilityHelper1) print mininet.ns3.getMobilityModel(h3) mininet.ns3.setMobilityModel(h4, mobilityHelper1) print mininet.ns3.getMobilityModel(h4) y_h3 = -distance/2 y_h4 = distance/2 # set position (after wifi.addAp/Sta() otherwise node position become 0.0,0.0) info("*** Positions\n") mininet.ns3.setPosition(s0, -3.0, 0.0, 0.0) print mininet.ns3.getPosition(s0) mininet.ns3.setPosition(h0, -5.0, 0.0, 0.0) print mininet.ns3.getPosition(h0) mininet.ns3.setPosition(h1, 1.5, -1.5, 0.0) print mininet.ns3.getPosition(h1) mininet.ns3.setPosition(h2, 1.5, 1.5, 0.0) print mininet.ns3.getPosition(h2) mininet.ns3.setPosition(h3, 0.0, y_h3, 0.0) print mininet.ns3.getPosition(h3) mininet.ns3.setPosition(h4, 0.0, y_h4, 0.0) print mininet.ns3.getPosition(h4) """ # Wireshark files rv = os.path.isdir("/tmp/pcap/interference_scenario") if rv is False: os.mkdir("/tmp/pcap/interference_scenario") ns.wifi.YansWifiPhyHelper().Default().EnablePcapAll("/tmp/pcap/wifi") ns.csma.CsmaHelper().EnablePcapAll("/tmp/pcap/csma") """ """ NetAnim configuration """ rv = os.path.isdir("/tmp/xml") if rv is False: os.mkdir("/tmp/xml") anim = ns.netanim.AnimationInterface("/tmp/xml/interference_scenario.xml") anim.EnablePacketMetadata(True) anim.UpdateNodeDescription(s0.nsNode, 's0 - switch '+str(s0.nsNode.GetId())) anim.UpdateNodeColor(s0.nsNode, switch_color[0], switch_color[1], switch_color[2]) anim.UpdateNodeDescription(h0.nsNode, 'h0 - server node ' + str(h0.nsNode.GetId())) anim.UpdateNodeColor(h0.nsNode, server_color[0], server_color[1], server_color[2]) anim.UpdateNodeDescription(h1.nsNode, 'h1 - mobile node ' + str(h1.nsNode.GetId())) anim.UpdateNodeColor(h1.nsNode, mobile_node_color[0], mobile_node_color[1], mobile_node_color[2]) anim.UpdateNodeDescription(h2.nsNode, 'h2 - mobile node ' + str(h2.nsNode.GetId())) anim.UpdateNodeColor(h2.nsNode, mobile_node_color[0], mobile_node_color[1], mobile_node_color[2]) anim.UpdateNodeDescription(h3.nsNode, 'h3 - access point ' + str(h3.nsNode.GetId())) anim.UpdateNodeColor(h3.nsNode, access_point_color[0], access_point_color[1], access_point_color[2]) anim.UpdateNodeDescription(h4.nsNode, 'h4 - access point ' + str(h4.nsNode.GetId())) anim.UpdateNodeColor(h4.nsNode, access_point_color[0], access_point_color[1], access_point_color[2]) info( '*** Starting network\n' ) net.start() mininet.ns3.start() """ Adding route entries """ h0.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.1.1 dev h0-eth0') h0.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.1.2 dev h0-eth0') h1.cmd('route add default gw 10.0.2.1') h2.cmd('route add default gw 10.0.3.1') h3.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 dev h3-eth1') h3.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 gw 10.0.1.2 dev h3-eth0') h4.cmd('route add -net 10.0.3.0 netmask 255.255.255.0 dev h4-eth1') h4.cmd('route add -net 10.0.2.0 netmask 255.255.255.0 gw 10.0.1.1 dev h4-eth0') print "*** Dumping host connections" dumpNodeConnections(net.hosts) """ static measurements """ # h1, h2 -> h0 if cmd.udp is not None: if cmd.json is not None: h2.cmdPrint('ping 10.0.1.100 -c 30 > /home/luca/ping_h2_h0_udp &') h0.cmdPrint('iperf3 -s -p 5003 -i 1 -J > /home/luca/iperf_interference_h0_server_h1_client_udp.json &') #h0.cmdPrint('iperf3 -s -p 5004 -i 1 > /home/luca/iperf_interference_h0_server_h2_client &') h1.cmdPrint('iperf3 -c 10.0.1.100 -u -b 2M -p 5003 -n 5M -J > /home/luca/iperf_interference_h1_client_udp.json &') #h2.cmdPrint('iperf3 -c 10.0.1.100 -u -b 2M -p 5004 -n 5M > /home/luca/iperf_interference_h2_client &') else: """ h2.cmdPrint('ping 10.0.1.100 -c 30 > /home/luca/stats/interference_scenario/ping_h2_h0_udp &') #h1.cmdPrint('ping 10.0.1.100 -c 30 > /home/luca/ping_h1_h0_udp &') h0.cmdPrint('iperf3 -s -p 5003 -i 1 > /home/luca/stats/interference_scenario/iperf_interference_h0_server_h1_client_udp &') #h0.cmdPrint('iperf3 -s -p 5004 -i 1 > /home/luca/iperf_interference_h0_server_h2_client &') h1.cmdPrint('iperf3 -c 10.0.1.100 -u -b 2M -p 5003 -n 5M > /home/luca/stats/interference_scenario/iperf_interference_h1_client_udp &') #h2.cmdPrint('iperf3 -c 10.0.1.100 -u -b 2M -p 5004 -n 5M > /home/luca/iperf_interference_h2_client &') """ h2.cmdPrint('ping 10.0.1.100 -c 100 -i 0.5 > /home/luca/stats/interference_scenario/ping_h2_h0_udp_'+str(distance)+' &') src1, src2, dst = net.hosts[1], net.hosts[2], net.hosts[0] net.iperf([src1, dst], l4Type='UDP', udpBw='2M', seconds=55) if cmd.tcp is not None: if cmd.json is not None: h2.cmdPrint('ping 10.0.1.100 -c 30 > /home/luca/ping_h2_h0_tcp &') h0.cmdPrint('iperf3 -s -p 5003 -i 1 -J > /home/luca/iperf_interference_h0_server_h1_client_tcp.json &') h1.cmdPrint('iperf3 -c 10.0.1.100 -p 5003 -J > /home/luca/iperf_interference_h1_client_tcp.json &') else: h2.cmdPrint('ping 10.0.1.100 -c 30 -i 0.5 > /home/luca/ping_h2_h0_tcp &') h0.cmdPrint('iperf3 -s -p 5003 -i 1 > /home/luca/iperf_interference_h0_server_h1_client_tcp &') h1.cmdPrint('iperf3 -c 10.0.1.100 -p 5003 > /home/luca/iperf_interference_h1_client_tcp &') # h0 -> h1, h2 #h1.cmdPrint('iperf3 -s -p 5003 -i 1 > /home/luca/iperf_interference_h0_server_h1_client &') #h2.cmdPrint('iperf3 -s -p 5003 -i 1 > /home/luca/iperf_interference_h0_server_h2_client &') #h0.cmdPrint('iperf3 -c 10.0.2.100 -u -b 2M -p 5003 -n 5M > /home/luca/iperf_interference_h1_client &') #h0.cmdPrint('iperf3 -c 10.0.3.100 -u -b 2M -p 5003 -n 5M > /home/luca/iperf_interference_h2_client &') # h3 -> h0 #h0.cmdPrint( 'iperf3 -s -p 5003 -i 1 -J > /home/luca/iperf_static_h0_server.json &' ) #h3.cmdPrint( 'iperf3 -c 192.168.0.1 -u -b 3M -p 5003 -J > /home/luca/iperf_static_h3_client.json &' ) # h0 -> h1 #h1.cmdPrint( 'iperf3 -s -p 5003 -i 1 -J > /home/luca/iperf_static_h1_server.json &' ) #h0.cmdPrint( 'iperf3 -c 192.168.0.2 -u -b 3M -p 5003 -J > /home/luca/iperf_static_h0_client.json &' ) # h1 -> h2 #h2.cmdPrint( 'iperf3 -s -p 5003 -i 1 -J > /home/luca/iperf_static_h2_server.json &' ) #h1.cmdPrint( 'iperf3 -c 192.168.0.3 -u -b 3M -p 5003 -J > /home/luca/iperf_static_h1_client.json &' ) info( 'Testing network connectivity\n' ) #net.pingAll() """ src, dst = net.hosts[0], net.hosts[2] packet_loss_perc = net.ping([src, dst], timeout) src, dst = net.hosts[0], net.hosts[2] bandwidth = net.iperf([src, dst], seconds=5) """ #CLI(net) info( '*** Stopping network\n' ) mininet.ns3.stop() info( '*** mininet.ns3.stop()\n' ) mininet.ns3.clear() info( '*** mininet.ns3.clear()\n' ) net.stop() info( '*** net.stop()\n' )
def WifiNet(): """ Create an Wifi network and add nodes to it. """ net = Mininet() info( '*** Adding controller\n' ) net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 ) """ Initialize the WifiSegment, please refer ns3.py """ wifi = WifiSegment(standard = ns.wifi.WIFI_PHY_STANDARD_80211g) wifinodes = [] """ Initialize nodes """ for n in nodes: """ Get attributes """ nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodevel = n.get('velocity', None) nodeip = n.get('ip', None) """ Assign the addfunc, please refer Mininet for more details about addHost and addSwitch """ if nodetype is 'host': addfunc = net.addHost elif nodetype is 'switch': addfunc = net.addSwitch else: addfunc = None if nodename is None or addfunc is None: continue """ Add the node into Mininet """ node = addfunc (nodename, ip=nodeip) """ Set the mobility model """ mininet.ns3.setMobilityModel (node, nodemob) if nodepos is not None: mininet.ns3.setPosition (node, nodepos[0], nodepos[1], nodepos[2]) if nodevel is not None: mininet.ns3.setVelocity (node, nodevel[0], nodevel[1], nodevel[2]) """ Append the node into wifinodes """ wifinodes.append (node) """ Initialize Wifi Interfaces """ for wi in wifiintfs: """ Get attributes """ winodename = wi.get('nodename', None) witype = wi.get('type', None) wichannel = wi.get('channel', None) wissid = wi.get('ssid', None) """ Assign the addfunc, please refer the WifiSegment in ns3.py """ if witype is 'sta': addfunc = wifi.addSta elif witype is 'ap': addfunc = wifi.addAp else: addfunc = None if winodename is None or addfunc is None or wichannel is None: continue """ Get wifi node and add it to the TapBridge """ node = getWifiNode (wifinodes, winodename) addfunc (node, wichannel, wissid) """ Initialize Ehternet links between switches """ for cl in links: clnodename1 = cl.get('nodename1', None) clnodename2 = cl.get('nodename2', None) if clnodename1 is None or clnodename2 is None: continue clnode1 = getWifiNode (wifinodes, clnodename1) clnode2 = getWifiNode (wifinodes, clnodename2) if clnode1 is None or clnode2 is None: continue net.addLink( clnode1, clnode2 ) """ Enable Pcap output""" pcap = Pcap() pcap.enable() print pcap """ Enable netanim output""" anim = Netanim("/tmp/xml/wifi-wired-bridged4.xml", nodes) print anim """ Update node descriptions in the netanim """ for node in wifinodes: anim.UpdateNodeDescription (node.nsNode, str(node) + '-' + str(node.nsNode.GetId())) if isinstance(node, mininet.node.OVSSwitch): color = (0, 255, 0) elif isinstance(node, mininet.node.Host): color = (0, 0, 255) anim.UpdateNodeColor (node.nsNode, color[0], color[1], color[2]) """ Start the simulation """ info( '*** Starting network\n' ) net.start() mininet.ns3.start() info( 'Testing network connectivity\n' ) wifinodes[0].cmdPrint( 'ping 10.10.10.2 -c 3' ) CLI( net ) info( '*** Stopping network\n' ) mininet.ns3.stop() info( '*** mininet.ns3.stop()\n' ) mininet.ns3.clear() info( '*** mininet.ns3.clear()\n' ) net.stop() info( '*** net.stop()\n' )
def main(): parser = argparse.ArgumentParser(description="TCP DoS simulator.") parser.add_argument( '--burst', '-b', help="Burst duration in seconds of each DoS attack.", type=float, default=0.15) parser.add_argument( '--cong', help="Congestion control algorithm to use.", default='reno') parser.add_argument( '--suffix', '-s', help="Suffix for output directory", type=str, default='default') parser.add_argument( '--period', '-p', help="Seconds between low-rate DoS attacks, e.g. 0.5", type=float, default=0.5) parser.add_argument( '--nc', '-n', help="Number of TCP connections, e.g. 1", type=int, default=1) parser.add_argument( '--rto', '-r', help="rto_min value, in ms", type=int, default=1000) args = parser.parse_args() # Initialize kernel parameters. subprocess.check_call( 'sysctl -q -w net.ipv4.tcp_congestion_control=%s' % args.cong, shell=True) subprocess.check_call('sysctl -q -w net.ipv4.tcp_sack=0', shell=True) subprocess.check_call('sysctl -q -w net.ipv4.tcp_dsack=0', shell=True) subprocess.check_call('sysctl -q -w net.ipv4.tcp_fack=0', shell=True) topo = Topo() net = mininet.net.Mininet( topo=topo, host=mininet.node.CPULimitedHost, link=mininet.link.TCLink) net.start() set_interface(net, args.rto) print('Alice\'s IP is {}.'.format(net.get('alice').IP())) print('Bob\'s IP is {}.'.format(net.get('bob').IP())) print('Mallory\'s IP is {}.'.format(net.get('mallory').IP())) print('') output_dir = 'results-{}'.format(args.suffix) if not os.path.isdir(output_dir): os.mkdir(output_dir) time_file = os.path.join(output_dir, 't-{}-{}.txt'.format( args.period, args.burst)) cwnd_file = os.path.join(output_dir, 'cwnd-{}-{}.txt'.format( args.period, args.burst)) print("NUmber of TCP connection(s) = {}\n".format(args.nc)) attack = start_attack(net, args.period, args.burst) t = run_flow(net, args.nc, cwnd_file=cwnd_file) print('Sending completed in %.4f seconds.' % t) with open(time_file, 'w') as f: f.write(str(t) + '\n') attack.terminate() net.stop()
def WifiNet(): "Create an Wifi network and add nodes to it." net = Mininet() info( '*** Adding controller\n' ) net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 ) wifi = WifiSegment(standard = ns.wifi.WIFI_PHY_STANDARD_80211g) wifinodes = [] for n in nodes: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodevel = n.get('velocity', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc (nodename, ip=nodeip) mininet.ns3.setMobilityModel (node, nodemob) if nodepos is not None: mininet.ns3.setPosition (node, nodepos[0], nodepos[1], nodepos[2]) if nodevel is not None: mininet.ns3.setVelocity (node, nodevel[0], nodevel[1], nodevel[2]) wifinodes.append (node) for wi in wifiintfs: winodename = wi.get('nodename', None) witype = wi.get('type', None) wichannel = wi.get('channel', None) wissid = wi.get('ssid', None) wiip = wi.get('ip', None) if witype is 'sta': addfunc = wifi.addSta elif witype is 'ap': addfunc = wifi.addAp else: addfunc = None if winodename is None or addfunc is None or wichannel is None: continue node = getWifiNode (wifinodes, winodename) tb = addfunc (node, wichannel, wissid) for cl in csmalinks: clnodename1 = cl.get('nodename1', None) clnodename2 = cl.get('nodename2', None) if clnodename1 is None or clnodename2 is None: continue clnode1 = getWifiNode (wifinodes, clnodename1) clnode2 = getWifiNode (wifinodes, clnodename2) if clnode1 is None or clnode2 is None: continue CSMALink( clnode1, clnode2, DataRate="100Mbps") rv = os.path.isdir("/tmp/pcap") if rv is False: os.mkdir("/tmp/pcap") ns.wifi.YansWifiPhyHelper().Default().EnablePcapAll("/tmp/pcap/wifi") ns.csma.CsmaHelper().EnablePcapAll("/tmp/pcap/csma") rv = os.path.isdir("/tmp/xml") if rv is False: os.mkdir("/tmp/xml") anim = ns.netanim.AnimationInterface("/tmp/xml/wifi-wired-bridged4.xml") anim.EnablePacketMetadata (True) for n in nodes: anim.UpdateNodeDescription (node.nsNode, nodename+'-'+str(node.nsNode.GetId())) anim.UpdateNodeColor (node.nsNode, color[0], color[1], color[2]) info( '*** Starting network\n' ) net.start() mininet.ns3.start() info( 'Testing network connectivity\n' ) wifinodes[0].cmdPrint( 'ping 10.10.10.2 -c 3' ) CLI( net ) info( '*** Stopping network\n' ) mininet.ns3.stop() info( '*** mininet.ns3.stop()\n' ) mininet.ns3.clear() info( '*** mininet.ns3.clear()\n' ) net.stop() info( '*** net.stop()\n' )
def WifiLTENet(): "Create a Wifi+LTE network and add nodes to it." net = Mininet() info('*** Adding controller\n') net.addController('c0', controller=RemoteController, ip='127.0.0.1', port=6633) wifi1 = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifi2 = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifi3 = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifi4 = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifi5 = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifi99 = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) lte1 = LTESegment() lte2 = LTESegment() wifi1nodes = [] wifi2nodes = [] wifi3nodes = [] wifi4nodes = [] wifi5nodes = [] wifi99nodes = [] lte1nodes = [] lte2nodes = [] for n in nodes_w1: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) wifi1nodes.append(node) for n in nodes_w2: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) wifi2nodes.append(node) for n in nodes_w3: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) wifi3nodes.append(node) for n in nodes_w4: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) wifi4nodes.append(node) for n in nodes_w5: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) wifi5nodes.append(node) for n in nodes_w99: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) wifi99nodes.append(node) for n in nodes_l1: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) lte1nodes.append(node) for n in nodes_l2: nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodeip = n.get('ip', None) if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue node = addfunc(nodename, ip=nodeip) mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) lte2nodes.append(node) print 'node creation done' for a in w1intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = wifi1.addSta elif atype is 'ap': addfunc = wifi1.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(wifi1nodes, anodename) tb = addfunc(node, achannel, assid) for a in w2intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = wifi2.addSta elif atype is 'ap': addfunc = wifi2.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(wifi2nodes, anodename) tb = addfunc(node, achannel, assid) for a in w3intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = wifi3.addSta elif atype is 'ap': addfunc = wifi3.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(wifi3nodes, anodename) tb = addfunc(node, achannel, assid) for a in w4intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = wifi4.addSta elif atype is 'ap': addfunc = wifi4.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(wifi4nodes, anodename) tb = addfunc(node, achannel, assid) for a in w5intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = wifi5.addSta elif atype is 'ap': addfunc = wifi5.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(wifi5nodes, anodename) tb = addfunc(node, achannel, assid) for a in w99intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = wifi99.addSta elif atype is 'ap': addfunc = wifi99.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(wifi99nodes, anodename) tb = addfunc(node, achannel, assid) for a in l1intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = lte1.addSta elif atype is 'ap': addfunc = lte1.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(lte1nodes, anodename) tb = addfunc(node, achannel, assid) for a in l2intfs: anodename = a.get('nodename', None) atype = a.get('type', None) achannel = a.get('channel', None) assid = a.get('ssid', None) aip = a.get('ip', None) if atype is 'sta': addfunc = lte2.addSta elif atype is 'ap': addfunc = lte2.addAp else: addfunc = None if anodename is None or addfunc is None or achannel is None: continue node = getNode(lte2nodes, anodename) tb = addfunc(node, achannel, assid) apnodes = [ wifi1nodes[-1], wifi2nodes[-1], wifi3nodes[-1], wifi4nodes[-1], wifi5nodes[-1], lte1nodes[-1], lte2nodes[-1] ] for cl in csmalinks: clnodename1 = cl.get('nodename1', None) clnodename2 = cl.get('nodename2', None) if clnodename1 is None or clnodename2 is None: continue clnode1 = getNode(apnodes, clnodename1) clnode2 = getNode(wifi99nodes, clnodename2) if clnode1 is None or clnode2 is None: continue CSMALink(clnode1, clnode2, DataRate="54Mbps") print 'interface creation done' #anim = ns.netanim.AnimationInterface("opt_case_out.xml") #anim.EnablePacketMetadata (True) info('*** Starting network\n') net.start() print 'mininet started' mininet.ns3.start() print 'ns3 started' info('***Starting Simulation\n') print 'starting simulation' time.sleep(10) #prev_out_ap = np.loadtxt('out_ap.csv') #print prev_out_ap fp = open('allpos.txt', 'r') lines = fp.readlines() fp.close() count = 0 while (count < len(lines)): for i in range(n_cv): f = lines[count] pos = filter(None, re.split(' +', f.strip("\n").strip("[]"))) pos = [float(j) for j in pos] pos = np.array(pos) count = count + 1 mininet.ns3.setPosition(wifi2nodes[i], pos[0], pos[1], pos[2]) mininet.ns3.setPosition(wifi3nodes[i], pos[0], pos[1], pos[2]) mininet.ns3.setPosition(wifi4nodes[i], pos[0], pos[1], pos[2]) mininet.ns3.setPosition(wifi5nodes[i], pos[0], pos[1], pos[2]) mininet.ns3.setPosition(lte1nodes[i], pos[0], pos[1], pos[2]) mininet.ns3.setPosition(lte2nodes[i], pos[0], pos[1], pos[2]) if (i == 0): with open('pos.txt', 'w') as f: f.write(str(pos) + '\n') else: with open('pos.txt', 'a') as f: f.write(str(pos) + '\n') time.sleep(0.1) out_ap = np.loadtxt('out_ap.csv') if (len(out_ap) == n_cv): for i in range(n_cv): if (out_ap[i] == 1): while (1): sf = lte1.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break lte1nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') elif (out_ap[i] == 2): while (1): sf = lte2.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break lte2nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') elif (out_ap[i] == 3): while (1): sf = wifi1.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break wifi1nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') elif (out_ap[i] == 4): while (1): sf = wifi2.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break wifi2nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') elif (out_ap[i] == 5): while (1): sf = wifi3.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break wifi3nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') elif (out_ap[i] == 6): while (1): sf = wifi4.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break wifi4nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') elif (out_ap[i] == 7): while (1): sf = wifi5.stas[i].nsDevice.GetPhy().IsStateSwitching() sf2 = wifi99.stas[0].nsDevice.GetPhy( ).IsStateSwitching() if (sf == False and sf2 == False): break wifi5nodes[i].cmdPrint( 'ping -c 2 -s 1024 10.10.20.100 >> out_' + str('%02d' % (i + 1)) + '.txt') time.sleep(0.1) #prev_out_ap = out_ap #CLI( net ) info('*** Stopping network\n') mininet.ns3.stop() info('*** mininet.ns3.stop()\n') mininet.ns3.clear() info('*** mininet.ns3.clear()\n') net.stop() info('*** net.stop()\n')
'which odl\'s controller is listening') parser.add_argument('--xmls', default=None, help='generete tests only ' 'from some xmls (i.e. 1,3,34) ') args = parser.parse_args() # set and start mininet net = MininetTools.create_network(args.odlhost, args.mnport) net.start() time.sleep(15) try: # set host and port of ODL controller for test cases TestOpenFlowXml.port = args.odlport TestOpenFlowXml.host = args.odlhost TestOpenFlowXml.mn_port = args.mnport # set keyword dictionaries keywords = FileLoaderTools.load_file_to_dict('keywords.csv') match_keywords = FileLoaderTools.load_file_to_dict('match-keywords.csv') action_keywords = FileLoaderTools.load_file_to_dict('action-keywords.csv') ignore_keywords = FileLoaderTools.load_file_to_dict('ignore-keywords.csv') # # fix arguments for unittest # del sys.argv[1:] # odl_suite = suite(test_class=TestOpenFlowXml) unittest.TextTestRunner().run(odl_suite) finally: # stop mininet net.stop()
def main(args): "Create a simple network" get_ip_setting() topo = TwoServerClientAndRouterTopology() info( '*** Creating network\n' ) net = mininet.net.Mininet(topo=topo, controller=mininet.node.RemoteController, ipBase=IPBASE ) net.start() server1, server2, client, router = net.get( 'server1', 'server2', 'client', 'sw0') s1intf = server1.defaultIntf() s1intf.setIP('%s/8' % IP_SETTING['server1']) s2intf = server2.defaultIntf() s2intf.setIP('%s/8' % IP_SETTING['server2']) clintf = client.defaultIntf() clintf.setIP('%s/8' % IP_SETTING['client']) for host in server1, server2, client: set_default_route(host) # set_default_route_client(client) starthttp( server1 ) starthttp( server2 ) # CLI( net ,script="test.sh") # total credit credit = 85 strict_mode = False if(len(args)==2): if args[1]=='-s' or args[1]=='--strict': strict_mode = True print("Enable strict mode...") # test client ping router interface print("Start test ping...") deduction = test_ping("client ping 192.168.2.1",client,"192.168.2.1",strict_mode=strict_mode) credit-=deduction deduction = test_ping("client ping 172.64.3.1",client,"172.64.3.1",strict_mode=strict_mode) credit-=deduction deduction = test_ping("client ping 172.64.3.1",client,"10.0.1.1",strict_mode=strict_mode) credit-=deduction deduction = test_ping("client ping server1",client,server1.IP(),strict_mode=strict_mode) credit-=deduction deduction = test_ping("client ping server2",client,server2.IP(),strict_mode=strict_mode) credit-=deduction # test client ping error ip deduction = test_ping("client ping 192.168.2.3",client,"192.168.2.3",False,strict_mode=strict_mode) credit-=deduction # test client traceroute print("Start test traceroute...") deduction = test_traceroute("client traceroute 192.168.2.1",client,"192.168.2.1",["10.0.1.1"],strict_mode=strict_mode); credit-=deduction deduction = test_traceroute("client traceroute 172.64.3.1",client,"172.64.3.1",["10.0.1.1"],strict_mode=strict_mode); credit-=deduction deduction = test_traceroute("client traceroute 10.0.1.1",client,"10.0.1.1",["10.0.1.1"],strict_mode=strict_mode); credit-=deduction deduction = test_traceroute("client traceroute server1",client,server1.IP(),["10.0.1.1","192.168.2.2"],strict_mode=strict_mode); credit-=deduction deduction = test_traceroute("client traceroute server2",client,server2.IP(),["10.0.1.1","172.64.3.10"],strict_mode=strict_mode); credit-=deduction # test wget download file print("Start test wget...") deduction = test_wget("client wget http://192.168.2.2/index.html",client,"http://192.168.2.2/index.html",strict_mode=strict_mode) credit-=deduction deduction = test_wget("client wget http://172.64.3.10/index.html",client,"http://172.64.3.10/index.html",strict_mode=strict_mode) credit-=deduction print("Start test wget big file...") deduction = test_wget("client wget http://192.168.2.2/tmp",client,"http://192.168.2.2/tmp",strict_mode=strict_mode) credit-=deduction print("Congratulations: Your credit is {}".format(credit)) stophttp() net.stop()
def WifiNet(): """ Create an Wifi network and add nodes to it. """ net = Mininet() info("*** Adding controller\n") net.addController("c0", controller=RemoteController, ip="127.0.0.1", port=6633) """ Initialize the WifiSegment, please refer ns3.py """ wifi = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifinodes = [] """ Initialize nodes """ for n in nodes: """ Get attributes """ nodename = n.get("name", None) nodetype = n.get("type", None) nodemob = n.get("mobility", None) nodepos = n.get("position", None) nodevel = n.get("velocity", None) nodeip = n.get("ip", None) """ Assign the addfunc, please refer Mininet for more details about addHost and addSwitch """ if nodetype is "host": addfunc = net.addHost color = (255, 0, 0) elif nodetype is "switch": addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue """ Add the node into Mininet """ node = addfunc(nodename, ip=nodeip) """ Set the mobility model """ mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) if nodevel is not None: mininet.ns3.setVelocity(node, nodevel[0], nodevel[1], nodevel[2]) """ Append the node into wifinodes """ wifinodes.append(node) """ Initialize Wifi Interfaces """ for wi in wifiintfs: """ Get attributes """ winodename = wi.get("nodename", None) witype = wi.get("type", None) wichannel = wi.get("channel", None) wissid = wi.get("ssid", None) wiip = wi.get("ip", None) """ Assign the addfunc, please refer the WifiSegment in ns3.py """ if witype is "sta": addfunc = wifi.addSta elif witype is "ap": addfunc = wifi.addAp else: addfunc = None if winodename is None or addfunc is None or wichannel is None: continue """ Get wifi node and add it to the TapBridge """ node = getWifiNode(wifinodes, winodename) tb = addfunc(node, wichannel, wissid) """ Initialize Ehternet links between switches """ for cl in csmalinks: clnodename1 = cl.get("nodename1", None) clnodename2 = cl.get("nodename2", None) if clnodename1 is None or clnodename2 is None: continue clnode1 = getWifiNode(wifinodes, clnodename1) clnode2 = getWifiNode(wifinodes, clnodename2) if clnode1 is None or clnode2 is None: continue CSMALink(clnode1, clnode2, DataRate="100Mbps") """ Create /tmp/pcap if it does not exist """ rv = os.path.isdir("/tmp/pcap") if rv is False: os.mkdir("/tmp/pcap") """ Enable Pcap output """ ns.wifi.YansWifiPhyHelper().Default().EnablePcapAll("/tmp/pcap/wifi") ns.csma.CsmaHelper().EnablePcapAll("/tmp/pcap/csma") """ Create /tmp/xml if it does not exist """ rv = os.path.isdir("/tmp/xml") if rv is False: os.mkdir("/tmp/xml") """ Enable netanim output """ anim = ns.netanim.AnimationInterface("/tmp/xml/wifi-wired-bridged4.xml") anim.EnablePacketMetadata(True) """ Update node descriptions in the netanim """ for n in nodes: anim.UpdateNodeDescription(node.nsNode, nodename + "-" + str(node.nsNode.GetId())) anim.UpdateNodeColor(node.nsNode, color[0], color[1], color[2]) """ Start the simulation """ info("*** Starting network\n") net.start() mininet.ns3.start() info("Testing network connectivity\n") wifinodes[0].cmdPrint("ping 10.10.10.2 -c 3") CLI(net) info("*** Stopping network\n") mininet.ns3.stop() info("*** mininet.ns3.stop()\n") mininet.ns3.clear() info("*** mininet.ns3.clear()\n") net.stop() info("*** net.stop()\n")
def WifiNet(): """ Create an Wifi network and add nodes to it. """ net = Mininet() info('*** Adding controller\n') net.addController('c0', controller=RemoteController, ip='127.0.0.1', port=6633) """ Initialize the WifiSegment, please refer ns3.py """ wifi = WifiSegment(standard=ns.wifi.WIFI_PHY_STANDARD_80211g) wifinodes = [] """ Initialize nodes """ for n in nodes: """ Get attributes """ nodename = n.get('name', None) nodetype = n.get('type', None) nodemob = n.get('mobility', None) nodepos = n.get('position', None) nodevel = n.get('velocity', None) nodeip = n.get('ip', None) """ Assign the addfunc, please refer Mininet for more details about addHost and addSwitch """ if nodetype is 'host': addfunc = net.addHost color = (255, 0, 0) elif nodetype is 'switch': addfunc = net.addSwitch color = (0, 0, 255) else: addfunc = None if nodename is None or addfunc is None: continue """ Add the node into Mininet """ node = addfunc(nodename, ip=nodeip) """ Set the mobility model """ mininet.ns3.setMobilityModel(node, nodemob) if nodepos is not None: mininet.ns3.setPosition(node, nodepos[0], nodepos[1], nodepos[2]) if nodevel is not None: mininet.ns3.setVelocity(node, nodevel[0], nodevel[1], nodevel[2]) """ Append the node into wifinodes """ wifinodes.append(node) """ Initialize Wifi Interfaces """ for wi in wifiintfs: """ Get attributes """ winodename = wi.get('nodename', None) witype = wi.get('type', None) wichannel = wi.get('channel', None) wissid = wi.get('ssid', None) wiip = wi.get('ip', None) """ Assign the addfunc, please refer the WifiSegment in ns3.py """ if witype is 'sta': addfunc = wifi.addSta elif witype is 'ap': addfunc = wifi.addAp else: addfunc = None if winodename is None or addfunc is None or wichannel is None: continue """ Get wifi node and add it to the TapBridge """ node = getWifiNode(wifinodes, winodename) tb = addfunc(node, wichannel, wissid) """ Initialize Ehternet links between switches """ for cl in csmalinks: clnodename1 = cl.get('nodename1', None) clnodename2 = cl.get('nodename2', None) if clnodename1 is None or clnodename2 is None: continue clnode1 = getWifiNode(wifinodes, clnodename1) clnode2 = getWifiNode(wifinodes, clnodename2) if clnode1 is None or clnode2 is None: continue CSMALink(clnode1, clnode2, DataRate="100Mbps") """ Create /tmp/pcap if it does not exist """ rv = os.path.isdir("/tmp/pcap") if rv is False: os.mkdir("/tmp/pcap") """ Enable Pcap output """ ns.wifi.YansWifiPhyHelper().Default().EnablePcapAll("/tmp/pcap/wifi") ns.csma.CsmaHelper().EnablePcapAll("/tmp/pcap/csma") """ Create /tmp/xml if it does not exist """ rv = os.path.isdir("/tmp/xml") if rv is False: os.mkdir("/tmp/xml") """ Enable netanim output """ anim = ns.netanim.AnimationInterface("/tmp/xml/wifi-wired-bridged4.xml") anim.EnablePacketMetadata(True) """ Update node descriptions in the netanim """ for n in nodes: anim.UpdateNodeDescription(node.nsNode, nodename + '-' + str(node.nsNode.GetId())) anim.UpdateNodeColor(node.nsNode, color[0], color[1], color[2]) """ Start the simulation """ info('*** Starting network\n') net.start() mininet.ns3.start() info('Testing network connectivity\n') wifinodes[0].cmdPrint('ping 10.10.10.2 -c 3') CLI(net) info('*** Stopping network\n') mininet.ns3.stop() info('*** mininet.ns3.stop()\n') mininet.ns3.clear() info('*** mininet.ns3.clear()\n') net.stop() info('*** net.stop()\n')