Пример #1
0
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()
Пример #2
0
import docker
import mininet.cli
import mininet.node
import mininet.link
import mininet.log
import mininet.net
import os
import sys
import time
from functools import partial

mininet.log.setLogLevel('info')

Switch = partial(mininet.node.OVSSwitch, protocols='OpenFlow13')
net = mininet.net.Containernet(switch=Switch)
net.addController('c0', controller=mininet.node.RemoteController, ip='172.17.42.1', port=6652)

runtime_id = os.urandom(2).encode('hex')
# runtime_id = sys.argv[1]
s0 = net.addSwitch("s0")
s1 = net.addSwitch("s1")
s2 = net.addSwitch("s2")
s3 = net.addSwitch("s3")
s4 = net.addSwitch("s4")
net.addLink(s0, s1, cls=mininet.link.TCLink, delay="100ms", bw=40)
net.addLink(s1, s2, cls=mininet.link.TCLink, delay="10ms", bw=20)
net.addLink(s0, s3, cls=mininet.link.TCLink, delay="100ms", bw=40)
net.addLink(s3, s4, cls=mininet.link.TCLink, delay="10ms", bw=20)

c0 = net.addDocker(
    'c0' + runtime_id,
Пример #3
0
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' )
Пример #5
0
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' )
Пример #6
0
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' )      
Пример #7
0
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')
Пример #8
0
def WifiNet():

    "Creation d'un reseau sans fil"

    net = Mininet(switch=OVSSwitch)

    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
        if nodetype is 'host':
            node = addfunc(nodename, ip=nodeip)
        else:
            listenPort = n.get('ListenPort', None)
            node = addfunc(nodename, ip=nodeip, listenPort=listenPort)
        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")

    #Generation d'un fichier xml pour faire l'animation avec NetAnim
    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('*** Liste des STA \n')
    for wi in wifiintfs:
        winodename = wi.get('nodename', None)
        witype = wi.get('type', None)
        wichannel = wi.get('channel', None)
        if witype is 'sta':
            info(winodename, " Channel: ", wichannel, "\n")
    info('*** Liste des AP \n')
    for wi in wifiintfs:
        winodename = wi.get('nodename', None)
        witype = wi.get('type', None)
        wichannel = wi.get('channel', None)
        if witype is 'ap':
            info(winodename, " Channel: ", wichannel, "\n")

    #info( '*** Testing Network connectivity\n' )
    #wifinodes[0].cmdPrint( 'ping 172.31.1.2 -c 3 ' )

    # Explicitly enable OpenFlow 1.3, then run the network
    run("ovs-vsctl set bridge s1 protocols=OpenFlow13")

    CLI(net)

    info('*** Stopping network\n')
    mininet.ns3.stop()
    info('*** mininet.ns3.stop()\n')
    mininet.ns3.clear()
    info('*** mininet.ns3.clear()\n')
Пример #9
0
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")
Пример #10
0
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')