Esempio n. 1
0
r1 = ns.nodes.CreatObject()
r2 = ns.nodes.CreatObject()
sta2 = ns.nodes.CreatObject()

net1 = ns.network.NodeContainer(stal, r1, r2)
net2 = ns.network.NodeContainer(r2, sta2)
all1 = ns.network.NodeContainer(
    stal, r1, r2,
    sta2)  #all is python keyword hence all1 is used instead of all.

internetv6 = ns.internet.InternetStackHelper()
internetv6.Install(all1)

print "Create channels."
csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.core.DataRateValue(5000000))
csma.SetChannelAttribute("Delay",
                         ns.core.TimeValue(ns.core.MilliSeconds(6560)))
ndc1 = csma.Install(net1)
ndc2 = csma.Install(net2)

print "Assign IPv6 Addresses."
ipv6 = ns.internet.Ipv6AddressHelper()

ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64))
iic1 = ipv6.Assign(ndc1)
iic1.SetForwarding(2, True)
iic1.SetForwarding(1, True)
iic1.SetDefaultRouteInAllNodes(1)

ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64))
Esempio n. 2
0
def main(argv):
    print("RadvdExample")

    cmd = ns.core.CommandLine()
    cmd.AddValue("verbose", "turn on log components")
    cmd.verbose = False
    cmd.Parse(argv)
    verbose = bool(cmd.verbose)

    if verbose:
        ns.core.LogComponentEnable("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6RawSocketImpl", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6Interface", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("RadvdApplication", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ping6Application", ns.core.LOG_LEVEL_ALL)

    print("Create nodes.")
    n0 = ns.network.Node()
    r = ns.network.Node()
    n1 = ns.network.Node()

    net1 = ns.network.NodeContainer()
    net1.Add(n0)
    net1.Add(r)
    net2 = ns.network.NodeContainer()
    net2.Add(r)
    net2.Add(n1)
    all = ns.network.NodeContainer()
    all.Add(n0)
    all.Add(r)
    all.Add(n1)

    print("Create IPv6 Internet Stack")
    internetv6 = ns.internet.InternetStackHelper()
    internetv6.Install(all)

    print("Create channels.")
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    d1 = csma.Install(net1)  # n0 - R
    d2 = csma.Install(net2)  # R - n1

    print("Create networks and assign IPv6 Addresses.")
    ipv6 = ns.internet.Ipv6AddressHelper()

    # first subnet
    ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64))
    tmp = ns.network.NetDeviceContainer()
    tmp.Add(d1.Get(0))  # n0
    iic1 = ipv6.AssignWithoutAddress(tmp)  # n0 interface

    tmp2 = ns.network.NetDeviceContainer()
    tmp2.Add(d1.Get(1))  # R
    iicr1 = ipv6.Assign(
        tmp2)  # R interface to the first subnet is just statically assigned
    iicr1.SetForwarding(0, True)
    iic1.Add(iicr1)

    # second subnet R - n1
    ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64))
    tmp3 = ns.network.NetDeviceContainer()
    tmp3.Add(d2.Get(0))  # R
    iicr2 = ipv6.Assign(tmp3)  # R interface
    iicr2.SetForwarding(0, True)

    tmp4 = ns.network.NetDeviceContainer()
    tmp4.Add(d2.Get(1))  # n1
    iic2 = ipv6.AssignWithoutAddress(tmp4)
    iic2.Add(iicr2)

    # radvd configuration
    radvdHelper = ns.internet_apps.RadvdHelper()

    # R interface (n0 - R)
    # n0 will receive unsolicited (periodic) RA
    radvdHelper.AddAnnouncedPrefix(iic1.GetInterfaceIndex(1),
                                   ns.network.Ipv6Address("2001:1::0"), 64)

    # R interface (R - n1)
    # n1 will have to use RS, as RA are not sent automatically
    radvdHelper.AddAnnouncedPrefix(iic2.GetInterfaceIndex(1),
                                   ns.network.Ipv6Address("2001:2::0"), 64)
    radvdHelper.GetRadvdInterface(
        iic2.GetInterfaceIndex(1)).SetSendAdvert(False)

    radvdApps = radvdHelper.Install(r)
    radvdApps.Start(ns.core.Seconds(1.0))
    radvdApps.Stop(ns.core.Seconds(10.0))

    # Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via R
    packetSize = 1024
    maxPacketCount = 5
    interPacketInterval = ns.core.Seconds(1.)
    ping6 = ns.internet_apps.Ping6Helper()

    # ping6.SetLocal (iic1.GetAddress (0, 1));
    ping6.SetRemote(ns.network.Ipv6Address("2001:2::200:ff:fe00:4")
                    )  # should be n1 address after autoconfiguration
    ping6.SetIfIndex(iic1.GetInterfaceIndex(0))

    ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
    ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
    ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
    apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)))
    apps.Start(ns.core.Seconds(2.0))
    apps.Stop(ns.core.Seconds(7.0))

    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("radvd.tr"))
    csma.EnablePcapAll("radvd", True)

    print("Run Simulation.")
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    print("Done.")
def main(argv):
  
  
  
  
  cmd = ns.core.CommandLine()
  cmd.Parse(argv)

  
  
  
  
  ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl"))

  
  
  
  print "Create nodes."
  n = ns.network.NodeContainer()
  n.Create(4)

  internet = ns.internet.InternetStackHelper()
  internet.Install(n)

  
  
  
  print ("Create channels.")
  csma = ns.csma.CsmaHelper()
  csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
  csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
  csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
  d = csma.Install(n)

  
  
  
  print ("Assign IP Addresses.")
  ipv4 = ns.internet.Ipv4AddressHelper()
  ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
  i = ipv4.Assign(d)

  print ("Create Applications.")

  
  
  
  port = 9  
  server = ns.applications.UdpEchoServerHelper(port)
  apps = server.Install(n.Get(1))
  apps.Start(ns.core.Seconds(1.0))
  apps.Stop(ns.core.Seconds(10.0))

  
  
  
  
  packetSize = 1024
  maxPacketCount = 500
  interPacketInterval = ns.core.Seconds(0.01)
  client = ns.applications.UdpEchoClientHelper(i.GetAddress (1), port)
  client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
  client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
  client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
  apps = client.Install(n.Get(0))
  apps.Start(ns.core.Seconds(2.0))
  apps.Stop(ns.core.Seconds(10.0))

  ascii = ns.network.AsciiTraceHelper()
  csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"))
  csma.EnablePcapAll("realtime-udp-echo", False)

  
  
  
  print ("Run Simulation.")
  ns.core.Simulator.Run()
  ns.core.Simulator.Destroy()
  print ("Done.")
Esempio n. 4
0
def main(argv):
    #
    #  First, we initialize a few local variables that control some
    #  simulation parameters.
    #

    cmd = ns.core.CommandLine()
    cmd.backboneNodes = 10
    cmd.infraNodes = 2
    cmd.lanNodes = 2
    cmd.stopTime = 20

    #
    #  Simulation defaults are typically set next, before command line
    #  arguments are parsed.
    #
    ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize",
                              ns.core.StringValue("1472"))
    ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate",
                              ns.core.StringValue("100kb/s"))

    #
    #  For convenience, we add the local variables to the command line argument
    #  system so that they can be overridden with flags such as
    #  "--backboneNodes=20"
    #

    cmd.AddValue("backboneNodes", "number of backbone nodes")
    cmd.AddValue("infraNodes", "number of leaf nodes")
    cmd.AddValue("lanNodes", "number of LAN nodes")
    cmd.AddValue("stopTime", "simulation stop time(seconds)")

    #
    #  The system global variables and the local values added to the argument
    #  system can be overridden by command line arguments by using this call.
    #
    cmd.Parse(argv)

    backboneNodes = int(cmd.backboneNodes)
    infraNodes = int(cmd.infraNodes)
    lanNodes = int(cmd.lanNodes)
    stopTime = int(cmd.stopTime)

    if (stopTime < 10):
        print("Use a simulation stop time >= 10 seconds")
        exit(1)
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the backbone                                                #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #
    #  Create a container to manage the nodes of the adhoc(backbone) network.
    #  Later we'll create the rest of the nodes we'll need.
    #
    backbone = ns.network.NodeContainer()
    backbone.Create(backboneNodes)
    #
    #  Create the backbone wifi net devices and install them into the nodes in
    #  our container
    #
    wifi = ns.wifi.WifiHelper()
    mac = ns.wifi.WifiMacHelper()
    mac.SetType("ns3::AdhocWifiMac")
    wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode",
                                 ns.core.StringValue("OfdmRate54Mbps"))
    wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
    wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
    wifiPhy.SetChannel(wifiChannel.Create())
    backboneDevices = wifi.Install(wifiPhy, mac, backbone)
    #
    #  Add the IPv4 protocol stack to the nodes in our container
    #
    print("Enabling OLSR routing on all backbone nodes")
    internet = ns.internet.InternetStackHelper()
    olsr = ns.olsr.OlsrHelper()
    internet.SetRoutingHelper(olsr)
    # has effect on the next Install ()
    internet.Install(backbone)
    # re-initialize for non-olsr routing.
    # internet.Reset()
    #
    #  Assign IPv4 addresses to the device drivers(actually to the associated
    #  IPv4 interfaces) we just created.
    #
    ipAddrs = ns.internet.Ipv4AddressHelper()
    ipAddrs.SetBase(ns.network.Ipv4Address("192.168.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))
    ipAddrs.Assign(backboneDevices)

    #
    #  The ad-hoc network nodes need a mobility model so we aggregate one to
    #  each of the nodes we just finished building.
    #
    mobility = ns.mobility.MobilityHelper()
    mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX",
                                  ns.core.DoubleValue(20.0), "MinY",
                                  ns.core.DoubleValue(20.0), "DeltaX",
                                  ns.core.DoubleValue(20.0), "DeltaY",
                                  ns.core.DoubleValue(20.0), "GridWidth",
                                  ns.core.UintegerValue(5), "LayoutType",
                                  ns.core.StringValue("RowFirst"))
    mobility.SetMobilityModel(
        "ns3::RandomDirection2dMobilityModel", "Bounds",
        ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500,
                                                         500)), "Speed",
        ns.core.StringValue("ns3::ConstantRandomVariable[Constant=2]"),
        "Pause",
        ns.core.StringValue("ns3::ConstantRandomVariable[Constant=0.2]"))
    mobility.Install(backbone)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the LANs                                                    #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #  Reset the address base-- all of the CSMA networks will be in
    #  the "172.16 address space
    ipAddrs.SetBase(ns.network.Ipv4Address("172.16.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        print("Configuring local area network for backbone node ", i)
        #
        #  Create a container to manage the nodes of the LAN.  We need
        #  two containers here; one with all of the new nodes, and one
        #  with all of the nodes including new and existing nodes
        #
        newLanNodes = ns.network.NodeContainer()
        newLanNodes.Create(lanNodes - 1)
        #  Now, create the container with all nodes on this link
        lan = ns.network.NodeContainer(
            ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
        #
        #  Create the CSMA net devices and install them into the nodes in our
        #  collection.
        #
        csma = ns.csma.CsmaHelper()
        csma.SetChannelAttribute(
            "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
        csma.SetChannelAttribute("Delay",
                                 ns.core.TimeValue(ns.core.MilliSeconds(2)))
        lanDevices = csma.Install(lan)
        #
        #  Add the IPv4 protocol stack to the new LAN nodes
        #
        internet.Install(newLanNodes)
        #
        #  Assign IPv4 addresses to the device drivers(actually to the
        #  associated IPv4 interfaces) we just created.
        #
        ipAddrs.Assign(lanDevices)
        #
        #  Assign a new network prefix for the next LAN, according to the
        #  network mask initialized above
        #
        ipAddrs.NewNetwork()
        #
        # The new LAN nodes need a mobility model so we aggregate one
        # to each of the nodes we just finished building.
        #
        mobilityLan = ns.mobility.MobilityHelper()
        positionAlloc = ns.mobility.ListPositionAllocator()
        for j in range(newLanNodes.GetN()):
            positionAlloc.Add(ns.core.Vector(0.0, (j * 10 + 10), 0.0))

        mobilityLan.SetPositionAllocator(positionAlloc)
        mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
        mobilityLan.SetMobilityModel("ns3::ConstantPositionMobilityModel")
        mobilityLan.Install(newLanNodes)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the mobile networks                                         #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #  Reset the address base-- all of the 802.11 networks will be in
    #  the "10.0" address space
    ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        print("Configuring wireless network for backbone node ", i)
        #
        #  Create a container to manage the nodes of the LAN.  We need
        #  two containers here; one with all of the new nodes, and one
        #  with all of the nodes including new and existing nodes
        #
        stas = ns.network.NodeContainer()
        stas.Create(infraNodes - 1)
        #  Now, create the container with all nodes on this link
        infra = ns.network.NodeContainer(
            ns.network.NodeContainer(backbone.Get(i)), stas)
        #
        #  Create another ad hoc network and devices
        #
        ssid = ns.wifi.Ssid('wifi-infra' + str(i))
        wifiInfra = ns.wifi.WifiHelper()
        wifiPhy.SetChannel(wifiChannel.Create())
        wifiInfra.SetRemoteStationManager('ns3::ArfWifiManager')
        macInfra = ns.wifi.WifiMacHelper()
        macInfra.SetType("ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue(ssid))

        # setup stas
        staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
        # setup ap.
        macInfra.SetType("ns3::ApWifiMac", "Ssid", ns.wifi.SsidValue(ssid))
        apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
        # Collect all of these new devices
        infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)

        #  Add the IPv4 protocol stack to the nodes in our container
        #
        internet.Install(stas)
        #
        #  Assign IPv4 addresses to the device drivers(actually to the associated
        #  IPv4 interfaces) we just created.
        #
        ipAddrs.Assign(infraDevices)
        #
        #  Assign a new network prefix for each mobile network, according to
        #  the network mask initialized above
        #
        ipAddrs.NewNetwork()
        #
        #  The new wireless nodes need a mobility model so we aggregate one
        #  to each of the nodes we just finished building.
        #
        subnetAlloc = ns.mobility.ListPositionAllocator()
        for j in range(infra.GetN()):
            subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))

        mobility.PushReferenceMobilityModel(backbone.Get(i))
        mobility.SetPositionAllocator(subnetAlloc)
        mobility.SetMobilityModel(
            "ns3::RandomDirection2dMobilityModel", "Bounds",
            ns.mobility.RectangleValue(ns.mobility.Rectangle(-10, 10, -10,
                                                             10)), "Speed",
            ns.core.StringValue("ns3::ConstantRandomVariable[Constant=3]"),
            "Pause",
            ns.core.StringValue("ns3::ConstantRandomVariable[Constant=0.4]"))
        mobility.Install(stas)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Application configuration                                             #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #  Create the OnOff application to send UDP datagrams of size
    #  210 bytes at a rate of 448 Kb/s, between two nodes
    print("Create Applications.")
    port = 9  #  Discard port(RFC 863)

    appSource = ns.network.NodeList.GetNode(backboneNodes)
    lastNodeIndex = backboneNodes + backboneNodes * (
        lanNodes - 1) + backboneNodes * (infraNodes - 1) - 1
    appSink = ns.network.NodeList.GetNode(lastNodeIndex)
    # Let's fetch the IP address of the last node, which is on Ipv4Interface 1
    remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(
        1, 0).GetLocal()

    onoff = ns.applications.OnOffHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
    apps = onoff.Install(ns.network.NodeContainer(appSource))
    apps.Start(ns.core.Seconds(3))
    apps.Stop(ns.core.Seconds(stopTime - 1))

    #  Create a packet sink to receive these packets
    sink = ns.applications.PacketSinkHelper(
        "ns3::UdpSocketFactory",
        ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
    apps = sink.Install(ns.network.NodeContainer(appSink))
    apps.Start(ns.core.Seconds(3))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Tracing configuration                                                 #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    print("Configure Tracing.")
    csma = ns.csma.CsmaHelper()
    #
    #  Let's set up some ns-2-like ascii traces, using another helper class
    #
    ascii = ns.network.AsciiTraceHelper()
    stream = ascii.CreateFileStream("mixed-wireless.tr")
    wifiPhy.EnableAsciiAll(stream)
    csma.EnableAsciiAll(stream)
    internet.EnableAsciiIpv4All(stream)

    #  Csma captures in non-promiscuous mode
    csma.EnablePcapAll("mixed-wireless", False)
    #  Let's do a pcap trace on the backbone devices
    wifiPhy.EnablePcap("mixed-wireless", backboneDevices)
    wifiPhy.EnablePcap("mixed-wireless", appSink.GetId(), 0)

    #   #ifdef ENABLE_FOR_TRACING_EXAMPLE
    #     Config.Connect("/NodeList/*/$MobilityModel/CourseChange",
    #       MakeCallback(&CourseChangeCallback))
    #   #endif

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #                                                                        #
    #  Run simulation                                                        #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    print("Run Simulation.")
    ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
Esempio n. 5
0
def main(argv):

    #
    # Allow the user to override any of the defaults and the above Bind() at
    # run-time, via command-line arguments
    #
    cmd = ns.core.CommandLine()
    cmd.Parse(argv)

    #
    # Explicitly create the nodes required by the topology(shown above).
    #
    #print "Create nodes."
    terminals = ns.network.NodeContainer()
    terminals.Create(4)

    csmaSwitch = ns.network.NodeContainer()
    csmaSwitch.Create(1)

    #print "Build Topology"
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))

    # Create the csma links, from each terminal to the switch

    terminalDevices = ns.network.NetDeviceContainer()
    switchDevices = ns.network.NetDeviceContainer()

    for i in range(4):
        link = csma.Install(
            ns.network.NodeContainer(
                ns.network.NodeContainer(terminals.Get(i)), csmaSwitch))
        terminalDevices.Add(link.Get(0))
        switchDevices.Add(link.Get(1))

    # Create the bridge netdevice, which will do the packet switching
    switchNode = csmaSwitch.Get(0)
    bridgeDevice = ns.bridge.BridgeNetDevice()
    switchNode.AddDevice(bridgeDevice)

    for portIter in range(switchDevices.GetN()):
        bridgeDevice.AddBridgePort(switchDevices.Get(portIter))

    # Add internet stack to the terminals
    internet = ns.internet.InternetStackHelper()
    internet.Install(terminals)

    # We've got the "hardware" in place.  Now we need to add IP addresses.
    #
    #print "Assign IP Addresses."
    ipv4 = ns.internet.Ipv4AddressHelper()
    ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"),
                 ns.network.Ipv4Mask("255.255.255.0"))
    ipv4.Assign(terminalDevices)

    #
    # Create an OnOff application to send UDP datagrams from node zero to node 1.
    #
    #print "Create Applications."
    port = 9  # Discard port(RFC 863)

    onoff = ns.applications.OnOffHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(
            ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.2"),
                                         port)))
    onoff.SetConstantRate(ns.network.DataRate("500kb/s"))

    app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
    # Start the application
    app.Start(ns.core.Seconds(1.0))
    app.Stop(ns.core.Seconds(10.0))

    # Create an optional packet sink to receive these packets
    sink = ns.applications.PacketSinkHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(
            ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(),
                                         port)))
    app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
    app.Start(ns.core.Seconds(0.0))

    #
    # Create a similar flow from n3 to n0, starting at time 1.1 seconds
    #
    onoff.SetAttribute(
        "Remote",
        ns.network.AddressValue(
            ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"),
                                         port)))
    app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
    app.Start(ns.core.Seconds(1.1))
    app.Stop(ns.core.Seconds(10.0))

    app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
    app.Start(ns.core.Seconds(0.0))

    #
    # Configure tracing of all enqueue, dequeue, and NetDevice receive events.
    # Trace output will be sent to the file "csma-bridge.tr"
    #
    #print "Configure Tracing."
    #ascii = ns.network.AsciiTraceHelper();
    #csma.EnableAsciiAll(ascii.CreateFileStream ("csma-bridge.tr"));

    #
    # Also configure some tcpdump traces; each interface will be traced.
    # The output files will be named:
    #     csma-bridge.pcap-<nodeId>-<interfaceId>
    # and can be read by the "tcpdump -r" command(use "-tt" option to
    # display timestamps correctly)
    #
    csma.EnablePcapAll("csma-bridge", False)

    #
    # Now, do the actual simulation.
    #
    #print "Run Simulation."
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
p2p = ns.point_to_point.PointToPointHelper()
p2p.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
p2p.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
d0d2 = p2p.Install(n0n2)
d1d6 = p2p.Install(n1n6)

d1d2 = p2p.Install(n1n2)

p2p.SetDeviceAttribute("DataRate", ns.core.StringValue("1500kbps"))
p2p.SetChannelAttribute("Delay", ns.core.StringValue("10ms"))
d5d6 = p2p.Install(n5n6)

#We create the channels first without any IP addressing information
print("Create channels.")
csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.core.StringValue("5Mbps"))
csma.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
d2345 = csma.Install(n2345)

#Later, we add IP addresses.
print("Assign IP Addresses.")
ipv4 = ns.internet.Ipv4AddressHelper()
ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"),
             ns.network.Ipv4Mask("255.255.255.0"))
ipv4.Assign(d0d2)

ipv4.SetBase(ns.network.Ipv4Address("10.1.2.0"),
             ns.network.Ipv4Mask("255.255.255.0"))
ipv4.Assign(d1d2)

ipv4.SetBase(ns.network.Ipv4Address("10.1.3.0"),
Esempio n. 7
0
cmd = ns.core.CommandLine ()
cmd.MaxNodes = 4
cmd.runtime = 3
cmd.AddValue ("nodes", "Number of nodes in the network (must be > 1)")
cmd.AddValue ("runtime", "How long the applications should send data (default 3 seconds)")
cmd.Parse (sys.argv)

ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize",ns.core.UintegerValue(2048))
ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate",ns.core.StringValue ("8kbps"))

if cmd.MaxNodes < 2 :
	print("Error: --nodes: must be >= 2 \n " )
	sys.exit(0)


csma.SetChannelAttribute ("DataRate", ns.network.DataRateValue(ns.network.DataRate(100000000)))
csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.MicroSeconds (200)))

nodes = ns.network.NodeContainer()
nodes.Create(cmd.MaxNodes)

ethInterfaces = ns.network.NetDeviceContainer(csma.Install(nodes))

internet = ns.internet.InternetStackHelper ()
internet.SetTcp ("ns3::NscTcpL4Protocol","Library",ns.core.StringValue("liblinux2.6.26.so"))

'''
 this switches nodes 0 and 1 to NSCs Linux 2.6.26 stack.
'''
internet.Install (nodes.Get (0));
internet.Install (nodes.Get (1));
Esempio n. 8
0
def main(argv):
	ns.core.LogComponentEnable("UdpClient", ns.core.LOG_LEVEL_INFO)
	ns.core.LogComponentEnable("UdpServer", ns.core.LOG_LEVEL_INFO)
	cmd = ns.core.CommandLine ()
	cmd.useIpv6 = "False"
	cmd.AddValue ("useIpv6", "Use Ipv6")
	cmd.Parse (argv)

	print "Create nodes."
	n = ns.network.NodeContainer ()
	n.Create (2)

	internet = ns.internet.InternetStackHelper ()
	internet.Install (n)

	print "Create channels."
	csma = ns.csma.CsmaHelper ()
	csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("5000000"))
	csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.MilliSeconds (2)))
	csma.SetDeviceAttribute ("Mtu", ns.core.UintegerValue (1400))
	d = csma.Install (n)

	print "Assign IP Addresses."
	if (cmd.useIpv6 == "False"):
		ipv4 = ns.internet.Ipv4AddressHelper ()
		ipv4.SetBase (ns.network.Ipv4Address ("10.1.1.0"), ns.network.Ipv4Mask ("255.255.255.0"))
		i = ipv4.Assign (d)
		serverAddress = ns.network.Address (i.GetAddress (1))

	else:
		ipv6 = ns.internet.Ipv6AddressHelper ()
		ipv6.SetBase (ns.network.Ipv6Address ("2001:0000:f00d:cafe::"), ns.network.Ipv6Prefix (64))
		i6 = ipv6.Assign (d)
		serverAddress = ns.network.Address (i6.GetAddress (1, 1))


	print "Create Applications."


	port = 4000
	server = ns.applications.UdpEchoServerHelper (port)
	serverapps = server.Install (n.Get (1))
	serverapps.Start (ns.core.Seconds (1.0))
	serverapps.Stop (ns.core.Seconds (10.0))

	MaxPacketSize = 1024;
	interPacketInterval = ns.core.Seconds (0.05)
	maxPacketCount = 320;

	client = ns.applications.UdpEchoClientHelper ( serverAddress, port)


	client.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount))
  	client.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval))
 	client.SetAttribute ("PacketSize", ns.core.UintegerValue (MaxPacketSize))

	clientapps = client.Install (n.Get (0))
	serverapps.Start (ns.core.Seconds (2.0))
	serverapps.Stop (ns.core.Seconds (10.0))



	ascii = ns.network.AsciiTraceHelper ()
	csma.EnableAsciiAll (ascii.CreateFileStream ("udp-client-serv.tr"))
	csma.EnablePcapAll ("udp-client-serv", False)
	print "Run Simulation."
	ns.core.Simulator.Run ()
	ns.core.Simulator.Destroy ()
	print "Done."
Esempio n. 9
0
def main(argv):

    cmd = ns.core.CommandLine()

    cmd.Parse(argv)

    print "Create nodes"
    n0 = ns.network.Node()
    r = ns.network.Node()
    n1 = ns.network.Node()

    net1 = ns.network.NodeContainer()
    net1.Add(n0)
    net1.Add(r)
    net2 = ns.network.NodeContainer()
    net2.Add(r)
    net2.Add(n1)
    all = ns.network.NodeContainer()
    all.Add(n0)
    all.Add(r)
    all.Add(n1)

    internetv6 = ns.internet.InternetStackHelper()
    internetv6.Install(all)

    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    d1 = csma.Install(net1)
    d2 = csma.Install(net2)

    print "Addressing"
    ipv6 = ns.internet.Ipv6AddressHelper()
    ipv6.NewNetwork(ns.network.Ipv6Address("2001:1::"),
                    ns.network.Ipv6Prefix(64))
    i1 = ipv6.Assign(d1)
    i1.SetRouter(1, True)
    ipv6.NewNetwork(ns.network.Ipv6Address("2001:2::"),
                    ns.network.Ipv6Prefix(64))
    i2 = ipv6.Assign(d2)
    i2.SetRouter(0, True)

    print "Application"
    packetSize = 1024
    maxPacketCount = 5
    interPacketInterval = ns.core.Seconds(1.)
    ping6 = ns.applications.Ping6Helper()

    ping6.SetLocal(i1.GetAddress(0, 1))
    ping6.SetRemote(i2.GetAddress(1, 1))

    ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
    ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
    ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))

    apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)))
    apps.Start(ns.core.Seconds(2.0))
    apps.Stop(ns.core.Seconds(20.0))

    print "Tracing"
    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"))
    csma.EnablePcapAll("simple-routing-ping6", True)

    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
Esempio n. 10
0
Net6.Add(d)
Net7 = ns.network.NodeContainer()
Net7.Add(b)
Net7.Add(dst)
routers = ns.network.NodeContainer()
routers.Add(a)
routers.Add(b)
routers.Add(c)
routers.Add(d)
nodes = ns.network.NodeContainer()
nodes.Add(src)
nodes.Add(dst)

print "Create channels."
csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.core.StringValue('5000000'))
csma.SetChannelAttribute("Delay", ns.core.StringValue('0.02'))
ndc1 = ns.network.NetDeviceContainer()
ndc1 = csma.Install(Net1)
ndc2 = ns.network.NetDeviceContainer()
ndc2 = csma.Install(Net2)
ndc3 = ns.network.NetDeviceContainer()
ndc3 = csma.Install(Net3)
ndc4 = ns.network.NetDeviceContainer()
ndc4 = csma.Install(Net4)
ndc5 = ns.network.NetDeviceContainer()
ndc5 = csma.Install(Net5)
ndc6 = ns.network.NetDeviceContainer()
ndc6 = csma.Install(Net6)
ndc7 = ns.network.NetDeviceContainer()
ndc7 = csma.Install(Net7)
Esempio n. 11
0
def main(argv): 
    
    
    
    
    backboneNodes = 10
    infraNodes = 5
    lanNodes = 5
    stopTime = 10

    
    
    
    
    ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.StringValue("210"))
    ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate", ns.core.StringValue("448kb/s"))

    
    
    
    
    
    cmd = ns.core.CommandLine()
    
    
    
    

    
    
    
    
    cmd.Parse(argv)

    
    
    
    
    

    
    
    
    
    backbone = ns.network.NodeContainer()
    backbone.Create(backboneNodes)
    
    
    
    
    wifi = ns.wifi.WifiHelper()
    mac = ns.wifi.NqosWifiMacHelper.Default()
    mac.SetType("ns3::AdhocWifiMac")
    wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
                                  "DataMode", ns.core.StringValue("OfdmRate54Mbps"))
    wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
    wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
    wifiPhy.SetChannel(wifiChannel.Create())
    backboneDevices = wifi.Install(wifiPhy, mac, backbone)
    
    
    
    print "Enabling OLSR routing on all backbone nodes"
    internet = ns.internet.InternetStackHelper()
    olsr = ns.olsr.OlsrHelper()
    internet.SetRoutingHelper(olsr); 
    internet.Install(backbone);
    
    internet.Reset()
    
    
    
    
    ipAddrs = ns.internet.Ipv4AddressHelper()
    ipAddrs.SetBase(ns.network.Ipv4Address("192.168.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
    ipAddrs.Assign(backboneDevices)

    
    
    
    
    mobility = ns.mobility.MobilityHelper()
    positionAlloc = ns.mobility.ListPositionAllocator()
    x = 0.0
    for i in range(backboneNodes):
        positionAlloc.Add(ns.core.Vector(x, 0.0, 0.0))
        x += 5.0
    mobility.SetPositionAllocator(positionAlloc)
    mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
                               "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(0, 1000, 0, 1000)),
                               "Speed", ns.core.RandomVariableValue(ns.core.ConstantVariable(2000)),
                               "Pause", ns.core.RandomVariableValue(ns.core.ConstantVariable(0.2)))
    mobility.Install(backbone)

    
    
    
    
    

    
    
    ipAddrs.SetBase(ns.network.Ipv4Address("172.16.0.0"), ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        print "Configuring local area network for backbone node ", i
        
        
        
        
        
        newLanNodes = ns.network.NodeContainer()
        newLanNodes.Create(lanNodes - 1)
        
        lan = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
        
        
        
        
        csma = ns.csma.CsmaHelper()
        csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
        csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
        lanDevices = csma.Install(lan)
        
        
        
        internet.Install(newLanNodes)
        
        
        
        
        ipAddrs.Assign(lanDevices)
        
        
        
        
        ipAddrs.NewNetwork()

    
    
    
    
    

    
    
    ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"), ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        print "Configuring wireless network for backbone node ", i
        
        
        
        
        
        stas = ns.network.NodeContainer()
        stas.Create(infraNodes - 1)
        
        infra = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), stas)
        
        
        
        ssid = ns.wifi.Ssid('wifi-infra' + str(i))
        wifiInfra = ns.wifi.WifiHelper.Default()
        wifiPhy.SetChannel(wifiChannel.Create())
        wifiInfra.SetRemoteStationManager('ns3::ArfWifiManager')
        macInfra = ns.wifi.NqosWifiMacHelper.Default();
        macInfra.SetType("ns3::StaWifiMac",
                         "Ssid", ns.wifi.SsidValue(ssid),
                         "ActiveProbing", ns.core.BooleanValue(False))

        
        staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
        
        macInfra.SetType("ns3::ApWifiMac",
                         "Ssid", ns.wifi.SsidValue(ssid),
                         "BeaconGeneration", ns.core.BooleanValue(True),
                         "BeaconInterval", ns.core.TimeValue(ns.core.Seconds(2.5)))
        apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
        
        infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)

        
        
        internet.Install(stas)
        
        
        
        
        ipAddrs.Assign(infraDevices)
        
        
        
        
        ipAddrs.NewNetwork()
        
        
        
        
        subnetAlloc = ns.mobility.ListPositionAllocator()
        for j in range(infra.GetN()):
            subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))

        mobility.PushReferenceMobilityModel(backbone.Get(i))
        mobility.SetPositionAllocator(subnetAlloc)
        mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
                                  "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-25, 25, -25, 25)),
                                  "Speed", ns.core.RandomVariableValue(ns.core.ConstantVariable(30)),
                                  "Pause", ns.core.RandomVariableValue(ns.core.ConstantVariable(0.4)))
        mobility.Install(infra)

    
    
    
    
    

    
    
    print "Create Applications."
    port = 9   

    
    
    assert(lanNodes >= 5)
    appSource = ns.network.NodeList.GetNode(11)
    appSink = ns.network.NodeList.GetNode(13)
    remoteAddr = ns.network.Ipv4Address("172.16.0.5")

    onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory", 
                            ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
    onoff.SetAttribute("OnTime", ns.core.RandomVariableValue(ns.core.ConstantVariable(1)))
    onoff.SetAttribute("OffTime", ns.core.RandomVariableValue(ns.core.ConstantVariable(0)))
    apps = onoff.Install(ns.network.NodeContainer(appSource))
    apps.Start(ns.core.Seconds(3.0))
    apps.Stop(ns.core.Seconds(20.0))

    
    sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory", 
                                ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
    apps = sink.Install(ns.network.NodeContainer(appSink))
    apps.Start(ns.core.Seconds(3.0))

    
    
    
    
    

    print "Configure Tracing."
    
    
    
    
    
    
    
    
    print "(tracing not done for Python)"
    
    
    

    
    wifiPhy.EnablePcap("mixed-wireless", backboneDevices)
    
    csma = ns.csma.CsmaHelper()
    csma.EnablePcapAll("mixed-wireless", False)







    
    
    
    
    

    print "Run Simulation."
    ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    def run(self):
        cmd = ns.core.CommandLine()
        cmd.nCsma = 3
        cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices")
        cmd.AddValue("verbose", "Tell echo applications to log if true")
        cmd.Parse(sys.argv)
        nCsma = int(cmd.nCsma)

        nCsma = 1 if int(nCsma) == 0 else int(nCsma)

        p2pNodes = ns.network.NodeContainer()
        p2pNodes.Create(2)

        csmaNodes = ns.network.NodeContainer()
        csmaNodes.Add(p2pNodes.Get(1))
        csmaNodes.Create(nCsma)

        pointToPoint = ns.point_to_point.PointToPointHelper()
        pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
        pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))

        p2pDevices = pointToPoint.Install(p2pNodes)

        csma = ns.csma.CsmaHelper()
        csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
        csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))

        csmaDevices = csma.Install(csmaNodes)

        stack = ns.internet.InternetStackHelper()
        stack.Install(p2pNodes.Get(0))
        stack.Install(csmaNodes)

        address = ns.internet.Ipv4AddressHelper()
        address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
        p2pInterfaces = address.Assign(p2pDevices)

        address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))
        csmaInterfaces = address.Assign(csmaDevices)

        echoServer = ns.applications.UdpEchoServerHelper(9)
        echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)

        serverApps = echoServer.Install(csmaNodes.Get(nCsma))
        serverApps.Start(ns.core.Seconds(1.0))
        serverApps.Stop(ns.core.Seconds(10.0))

        echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
        echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
        echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))

        clientApps = echoClient.Install(p2pNodes.Get(0))
        clientApps.Start(ns.core.Seconds(2.0))
        clientApps.Stop(ns.core.Seconds(10.0))

        ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()

        pointToPoint.EnablePcapAll("second")
        csma.EnablePcap ("second", csmaDevices.Get (1), True)
        ns.core.Simulator.Run()
        ns.core.Simulator.Destroy()
    def run_tapcsma(self):
        mode = "ConfigureLocal"
        tapName = "thetap"

        cmd = ns.core.CommandLine()
        cmd.AddValue("mode", "Mode setting of TapBridge", mode)
        cmd.AddValue("tapName", "Name of the OS tap device", tapName)
        cmd.Parse(sys.argv)
        # CommandLine cmd;
        # cmd.AddValue ("mode", "Mode setting of TapBridge", mode);
        # cmd.AddValue ("tapName", "Name of the OS tap device", tapName);
        # cmd.Parse (argc, argv);

        # GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
        # GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));

        # NodeContainer nodes;
        # nodes.Create (4);
        nodes = ns.network.NodeContainer()
        nodes.Create(2)

        # CsmaHelper csma;
        # csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
        # csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
        csma = ns.csma.CsmaHelper()
        csma.SetChannelAttribute("DataRate", ns.core.StringValue("5Mbps"))
        csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds (2)))

        # NetDeviceContainer devices = csma.Install (nodes);
        devices = csma.Install(nodes)

        # InternetStackHelper stack;
        # stack.Install (nodes);
        stack = ns.internet.InternetStackHelper()
        stack.Install(nodes)

        # Ipv4AddressHelper addresses;
        # addresses.SetBase ("10.1.1.0", "255.255.255.0");
        # Ipv4InterfaceContainer interfaces = addresses.Assign (devices);
        address = ns.internet.Ipv4AddressHelper()
        address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
        p2pInterfaces = address.Assign(devices)

        # TapBridgeHelper tapBridge;
        # tapBridge.SetAttribute ("Mode", StringValue (mode));
        # tapBridge.SetAttribute ("DeviceName", StringValue (tapName));
        # tapBridge.Install (nodes.Get (0), devices.Get (0));
        tapBridge = ns.bridge.BridgeHelper()
        tapBridge.SetAttribute("Mode", ns.core.StringValue (mode))
        tapBridge.SetAttribute("DeviceName", ns.core.StringValue (tapName))
        tapBridge.Install(nodes.Get (0), devices.Get (0))

        # csma.EnablePcapAll ("tap-csma", false);
        csma.EnablePcapAll("tap-csma", false)
        # Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

        # Simulator::Stop (Seconds (60.));
        # Simulator::Run ();
        # Simulator::Destroy ();
        ns.core.Simulator.Stop(ns.core.Seconds(60.0))
        ns.core.Simulator.Run()
        ns.core.Simulator.Destroy()
        pass
Esempio n. 14
0
n0 = ns.network.CreateObject < Node > ()
r = ns.network.CreateObject < Node > ()
n1 = ns.network.CreateObject < Node > ()

net1 = ns.network.NodeContainer(n0, r)
net2 = ns.network.NodeContainer(r, n1)
allnet = ns.network.NodeContainer(n0, r,
                                  n1)  # allnet is like all in cpp program

print "Create IPv6 Internet Stack"
internetv6 = ns.internet.InternetStackHelper()
internetv6.Install(all)

print "Create channels."
csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", DataRateValue(5000000))
csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)))
d1 = csma.Install(net1)  #/* n0 - R */
d2 = csma.Install(net2)  #/* R - n1 */

print "Create networks and assign IPv6 Addresses."
ipv6 = ns.internet.Ipv6AddressHelper

#/* first subnet */
ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64))
tmp = ns.network.NetDeviceContainer()
tmp.Add(d1.Get(0))  #/* n0 */
iic1 = ns.internet.Ipv6InterfaceContainer()
iic1 = ipv6.AssignWithoutAddress(tmp)  #/* n0 interface */

tmp2 = ns.network.NetDeviceContainer()
Esempio n. 15
0
def main(argv):
    #
    # Allow the user to override any of the defaults and the above Bind() at
    # run-time, via command-line arguments
    #
    cmd = ns.core.CommandLine()
    cmd.Parse(argv)

    #
    # But since this is a realtime script, don't allow the user to mess with
    # that.
    #
    ns.core.GlobalValue.Bind("SimulatorImplementationType",
                             ns.core.StringValue("ns3::RealtimeSimulatorImpl"))

    #
    # Explicitly create the nodes required by the topology (shown above).
    #
    print "Create nodes."
    n = ns.network.NodeContainer()
    n.Create(4)

    internet = ns.internet.InternetStackHelper()
    internet.Install(n)

    #
    # Explicitly create the channels required by the topology (shown above).
    #
    print("Create channels.")
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
    d = csma.Install(n)

    #
    # We've got the "hardware" in place.  Now we need to add IP addresses.
    #
    print("Assign IP Addresses.")
    ipv4 = ns.internet.Ipv4AddressHelper()
    ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"),
                 ns.network.Ipv4Mask("255.255.255.0"))
    i = ipv4.Assign(d)

    print("Create Applications.")

    #
    # Create a UdpEchoServer application on node one.
    #
    port = 9  # well-known echo port number
    server = ns.applications.UdpEchoServerHelper(port)
    apps = server.Install(n.Get(1))
    apps.Start(ns.core.Seconds(1.0))
    apps.Stop(ns.core.Seconds(10.0))

    #
    # Create a UdpEchoClient application to send UDP datagrams from node zero to
    # node one.
    #
    packetSize = 1024
    maxPacketCount = 500
    interPacketInterval = ns.core.Seconds(0.01)
    client = ns.applications.UdpEchoClientHelper(i.GetAddress(1), port)
    client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
    client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
    client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
    apps = client.Install(n.Get(0))
    apps.Start(ns.core.Seconds(2.0))
    apps.Stop(ns.core.Seconds(10.0))

    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"))
    csma.EnablePcapAll("realtime-udp-echo", False)

    #
    # Now, do the actual simulation.
    #
    print("Run Simulation.")
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    print("Done.")
Esempio n. 16
0
def main(argv):

    #
    # Allow the user to override any of the defaults and the above Bind() at
    # run-time, via command-line arguments
    #
    cmd = ns.core.CommandLine()

    cmd.packetSize = 1024
    cmd.packetCount = 10
    cmd.packetInterval = 1.0

    # Socket options for IPv4, currently TOS, TTL, RECVTOS, and RECVTTL
    cmd.ipTos = 0
    cmd.ipRecvTos = True
    cmd.ipTtl = 0
    cmd.ipRecvTtl = True

    cmd.AddValue("PacketSize", "Packet size in bytes")
    cmd.AddValue("PacketCount", "Number of packets to send")
    cmd.AddValue("Interval", "Interval between packets")
    cmd.AddValue("IP_TOS", "IP_TOS")
    cmd.AddValue("IP_RECVTOS", "IP_RECVTOS")
    cmd.AddValue("IP_TTL", "IP_TTL")
    cmd.AddValue("IP_RECVTTL", "IP_RECVTTL")
    cmd.Parse(argv)

    packetSize = int(cmd.packetSize)
    packetCount = int(cmd.packetCount)
    packetInterval = float(cmd.packetInterval)
    ipTos = int(cmd.ipTos)
    ipRecvTos = bool(cmd.ipRecvTos)
    ipTtl = int(cmd.ipTtl)
    ipRecvTtl = bool(cmd.ipRecvTtl)

    print("Create nodes.")
    n = ns.network.NodeContainer()
    n.Create(2)

    internet = ns.internet.InternetStackHelper()
    internet.Install(n)

    print("Create channels.")
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
    d = csma.Install(n)

    print("Assign IP addresses.")
    ipv4 = ns.internet.Ipv4AddressHelper()
    ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"),
                 ns.network.Ipv4Mask("255.255.255.0"))
    i = ipv4.Assign(d)
    serverAddress = ns.network.Address(i.GetAddress(1))

    print("Create sockets.")
    # Receiver socket on n1
    tid = ns3.TypeId.LookupByName("ns3::UdpSocketFactory")
    recvSink = ns3.Socket.CreateSocket(n.Get(1), tid)
    local = ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), 4477)
    recvSink.SetIpRecvTos(ipRecvTos)
    recvSink.SetIpRecvTtl(ipRecvTtl)
    recvSink.Bind(local)
    recvSink.SetRecvCallback(ReceivePacket)

    # Sender socket on n0
    source = ns3.Socket.CreateSocket(n.Get(0), tid)
    remote = ns.network.InetSocketAddress(i.GetAddress(1), 4477)

    # Set socket options, it is also possible to set the options after the socket has been created/connected.
    if ipTos > 0:
        source.SetIpTos(ipTos)

    if ipTtl > 0:
        source.SetIpTtl(ipTtl)
    source.Connect(remote)

    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("socket-options-ipv4.tr"))
    csma.EnablePcapAll("socket-options-ipv4", False)

    # Schedule SendPacket
    interPacketInterval = ns.core.Seconds(packetInterval)
    ns.core.Simulator.ScheduleWithContext(source.GetNode().GetId(),
                                          ns.core.Seconds(1.0), SendPacket,
                                          source, packetSize, packetCount,
                                          interPacketInterval)

    print("Run Simulation.")
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    print("Done.")
def main(argv):
    print("FragmentationIpv6Example")

    cmd = ns.core.CommandLine()
    cmd.AddValue("verbose", "turn on log components")
    cmd.verbose = False
    cmd.Parse(argv)
    verbose = bool(cmd.verbose)

    if verbose:
        ns.core.LogComponentEnable("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6Interface", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ping6Application", ns.core.LOG_LEVEL_ALL)

    print("Create nodes.")
    n0 = ns.network.Node()
    r = ns.network.Node()
    n1 = ns.network.Node()

    net1 = ns.network.NodeContainer()
    net1.Add(n0)
    net1.Add(r)
    net2 = ns.network.NodeContainer()
    net2.Add(r)
    net2.Add(n1)
    all = ns.network.NodeContainer()
    all.Add(n0)
    all.Add(r)
    all.Add(n1)

    print("Create IPv6 Internet Stack")
    internetv6 = ns.internet.InternetStackHelper()
    internetv6.Install(all)

    print("Create channels.")
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    d1 = csma.Install(net1)
    d2 = csma.Install(net2)

    print("Create networks and assign IPv6 Addresses.")
    ipv6 = ns.internet.Ipv6AddressHelper()
    ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64))
    i1 = ipv6.Assign(d1)
    i1.SetForwarding(1, True)
    i1.SetDefaultRouteInAllNodes(1)
    ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64))
    i2 = ipv6.Assign(d2)
    i2.SetForwarding(0, True)
    i2.SetDefaultRouteInAllNodes(0)

    routingHelper = ns.internet.Ipv6StaticRoutingHelper()
    routingStream = ns.network.OutputStreamWrapper("fragmentation-ipv6.routes",
                                                   ns.network.STD_IOS_OUT)
    routingHelper.PrintRoutingTableAt(ns.core.Seconds(0), n0, routingStream)

    #  Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r
    packetSize = 4096
    maxPacketCount = 5
    interPacketInterval = ns.core.Seconds(1.0)
    ping6 = ns.internet_apps.Ping6Helper()

    ping6.SetLocal(i1.GetAddress(0, 1))
    ping6.SetRemote(i2.GetAddress(1, 1))

    ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
    ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
    ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
    apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)))
    apps.Start(ns.core.Seconds(2.0))
    apps.Stop(ns.core.Seconds(20.0))

    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("fragmentation-ipv6.tr"))
    csma.EnablePcapAll("fragmentation-ipv6", True)

    print("Run Simulation.")
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    print("Done.")
Esempio n. 18
0
def main():
    framework.start()

    # First, we initialize a few local variables that control some
    #  simulation parameters.
    cmd = ns.core.CommandLine()
    cmd.backboneNodes = {{backbone_nodes}}
    cmd.infraNodes = {{infra_nodes}}
    cmd.lanNodes = {{lan_nodes}}
    cmd.stopTime = {{exec_time}}

    #  Simulation defaults are typically set next, before command line
    #  arguments are parsed.
    ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize",
                              ns.core.StringValue("1472"))
    ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate",
                              ns.core.StringValue("100kb/s"))

    backboneNodes = int(cmd.backboneNodes)
    infraNodes = int(cmd.infraNodes)
    lanNodes = int(cmd.lanNodes)
    stopTime = int(cmd.stopTime)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the backbone                                                #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #  Create a container to manage the nodes of the adhoc(backbone) network.
    #  Later we'll create the rest of the nodes we'll need.
    backbone = ns.network.NodeContainer()
    backbone.Create(backboneNodes)

    #  Create the backbone wifi net devices and install them into the nodes in
    #  our container
    wifi = ns.wifi.WifiHelper()
    mac = ns.wifi.WifiMacHelper()
    mac.SetType("ns3::AdhocWifiMac")
    wifi.SetRemoteStationManager(
        "ns3::ConstantRateWifiManager", "DataMode",
        ns.core.StringValue("OfdmRate{}Mbps".format({{ofdm_rate}})))
    wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
    wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
    wifiPhy.SetChannel(wifiChannel.Create())
    backboneDevices = wifi.Install(wifiPhy, mac, backbone)

    #  Add the IPv4 protocol stack to the nodes in our container
    print("Enabling OLSR routing on all backbone nodes")
    internet = ns.internet.InternetStackHelper()
    olsr = ns.olsr.OlsrHelper()
    internet.SetRoutingHelper(olsr)
    # has effect on the next Install ()
    internet.Install(backbone)

    #  Assign IPv4 addresses to the device drivers(actually to the associated
    #  IPv4 interfaces) we just created.
    ipAddrs = ns.internet.Ipv4AddressHelper()
    ipAddrs.SetBase(ns.network.Ipv4Address("192.168.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))
    ipAddrs.Assign(backboneDevices)

    #  The ad-hoc network nodes need a mobility model so we aggregate one to
    #  each of the nodes we just finished building.
    mobility = ns.mobility.MobilityHelper()
    mobility.SetPositionAllocator("ns3::GridPositionAllocator", "MinX",
                                  ns.core.DoubleValue(20.0), "MinY",
                                  ns.core.DoubleValue(20.0), "DeltaX",
                                  ns.core.DoubleValue(20.0), "DeltaY",
                                  ns.core.DoubleValue(20.0), "GridWidth",
                                  ns.core.UintegerValue(5), "LayoutType",
                                  ns.core.StringValue("RowFirst"))
    mobility.SetMobilityModel(
        "ns3::RandomDirection2dMobilityModel", "Bounds",
        ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500,
                                                         500)), "Speed",
        ns.core.StringValue("ns3::ConstantRandomVariable[Constant=2]"),
        "Pause",
        ns.core.StringValue("ns3::ConstantRandomVariable[Constant=0.2]"))
    mobility.Install(backbone)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the LANs                                                    #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #  Reset the address base-- all of the CSMA networks will be in
    #  the "172.16 address space
    ipAddrs.SetBase(ns.network.Ipv4Address("172.16.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        #  Create a container to manage the nodes of the LAN.  We need
        #  two containers here; one with all of the new nodes, and one
        #  with all of the nodes including new and existing nodes
        newLanNodes = ns.network.NodeContainer()
        newLanNodes.Create(lanNodes - 1)
        lan = ns.network.NodeContainer(
            ns.network.NodeContainer(backbone.Get(i)), newLanNodes)

        #  Create the CSMA net devices and install them into the nodes in our
        #  collection.
        csma = ns.csma.CsmaHelper()
        csma.SetChannelAttribute(
            "DataRate",
            ns.network.DataRateValue(ns.network.DataRate({{datarate}})))
        csma.SetChannelAttribute(
            "Delay", ns.core.TimeValue(ns.core.MilliSeconds({{delay}})))
        lanDevices = csma.Install(lan)

        #  Add the IPv4 protocol stack to the new LAN nodes
        internet.Install(newLanNodes)

        #  Assign IPv4 addresses to the device drivers(actually to the
        #  associated IPv4 interfaces) we just created.
        ipAddrs.Assign(lanDevices)

        #  Assign a new network prefix for the next LAN, according to the
        #  network mask initialized above
        ipAddrs.NewNetwork()

        # The new LAN nodes need a mobility model so we aggregate one
        # to each of the nodes we just finished building.
        mobilityLan = ns.mobility.MobilityHelper()
        positionAlloc = ns.mobility.ListPositionAllocator()
        for j in range(newLanNodes.GetN()):
            positionAlloc.Add(ns.core.Vector(0.0, (j * 10 + 10), 0.0))

        mobilityLan.SetPositionAllocator(positionAlloc)
        mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
        mobilityLan.SetMobilityModel("ns3::ConstantPositionMobilityModel")
        mobilityLan.Install(newLanNodes)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the mobile networks                                         #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #  Reset the address base-- all of the 802.11 networks will be in
    #  the "10.0" address space
    ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        #  Create a container to manage the nodes of the LAN.  We need
        #  two containers here; one with all of the new nodes, and one
        #  with all of the nodes including new and existing nodes
        stas = ns.network.NodeContainer()
        stas.Create(infraNodes - 1)
        infra = ns.network.NodeContainer(
            ns.network.NodeContainer(backbone.Get(i)), stas)

        #  Create another ad hoc network and devices
        ssid = ns.wifi.Ssid('wifi-infra' + str(i))
        wifiInfra = ns.wifi.WifiHelper()
        wifiPhy.SetChannel(wifiChannel.Create())
        wifiInfra.SetRemoteStationManager('ns3::ArfWifiManager')
        macInfra = ns.wifi.WifiMacHelper()
        macInfra.SetType("ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue(ssid))

        # setup stas
        staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
        # setup ap.
        macInfra.SetType("ns3::ApWifiMac", "Ssid", ns.wifi.SsidValue(ssid),
                         "BeaconInterval",
                         ns.core.TimeValue(ns.core.Seconds(2.5)))
        apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
        # Collect all of these new devices
        infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)

        #  Add the IPv4 protocol stack to the nodes in our container
        internet.Install(stas)

        #  Assign IPv4 addresses to the device drivers(actually to the associated
        #  IPv4 interfaces) we just created.
        ipAddrs.Assign(infraDevices)

        #  Assign a new network prefix for each mobile network, according to
        #  the network mask initialized above
        ipAddrs.NewNetwork()

        #  The new wireless nodes need a mobility model so we aggregate one
        #  to each of the nodes we just finished building.
        subnetAlloc = ns.mobility.ListPositionAllocator()
        for j in range(infra.GetN()):
            subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))

        mobility.PushReferenceMobilityModel(backbone.Get(i))
        mobility.SetPositionAllocator(subnetAlloc)
        mobility.SetMobilityModel(
            "ns3::RandomDirection2dMobilityModel", "Bounds",
            ns.mobility.RectangleValue(ns.mobility.Rectangle(-10, 10, -10,
                                                             10)), "Speed",
            ns.core.StringValue("ns3::ConstantRandomVariable[Constant=3]"),
            "Pause",
            ns.core.StringValue("ns3::ConstantRandomVariable[Constant=0.4]"))
        mobility.Install(stas)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Application configuration                                             #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #  Create the OnOff application to send UDP datagrams of size
    #  210 bytes at a rate of 448 Kb/s, between two nodes
    port = 9  #  Discard port(RFC 863)

    appSource = ns.network.NodeList.GetNode(backboneNodes)
    lastNodeIndex = backboneNodes + backboneNodes * (
        lanNodes - 1) + backboneNodes * (infraNodes - 1) - 1
    appSink = ns.network.NodeList.GetNode(lastNodeIndex)
    # Let's fetch the IP address of the last node, which is on Ipv4Interface 1
    remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(
        1, 0).GetLocal()

    onoff = ns.applications.OnOffHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
    apps = onoff.Install(ns.network.NodeContainer(appSource))
    apps.Start(ns.core.Seconds(3))
    apps.Stop(ns.core.Seconds(stopTime - 1))

    #  Create a packet sink to receive these packets
    sink = ns.applications.PacketSinkHelper(
        "ns3::UdpSocketFactory",
        ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
    apps = sink.Install(ns.network.NodeContainer(appSink))
    apps.Start(ns.core.Seconds(3))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Tracing configuration                                                 #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    csma = ns.csma.CsmaHelper()

    #  Let's set up some ns-2-like ascii traces, using another helper class
    #
    ascii = ns.network.AsciiTraceHelper()
    stream = ascii.CreateFileStream("mixed-wireless.tr")
    wifiPhy.EnableAsciiAll(stream)
    csma.EnableAsciiAll(stream)
    internet.EnableAsciiIpv4All(stream)

    mob = ascii.CreateFileStream("mixed-wireless.mob")
    mobility.EnableAsciiAll(mob)

    #  Csma captures in non-promiscuous mode
    csma.EnablePcapAll("mixed-wireless", False)
    #  Let's do a pcap trace on the backbone devices
    wifiPhy.EnablePcap("mixed-wireless", backboneDevices)
    wifiPhy.EnablePcap("mixed-wireless", appSink.GetId(), 0)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #                                                                        #
    #  Run simulation                                                        #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()

    framework.addBinaryFile("mixed-wireless.tr")
    framework.addBinaryFile("mixed-wireless.mob")

    path = "*.pcap"
    for filename in glob.glob(path):
        framework.addBinaryFile(filename)

    framework.stop()
Esempio n. 19
0
def main(argv):
		cmd = ns.core.CommandLine()
		cmd.AddValue ("verbose", "turn on log components")
		cmd.verbose = False
		cmd.Parse(argv)
		verbose = bool(cmd.verbose)

		if verbose :
			ns.core.LogComponentEnable("Icmpv6RedirectExample", ns.core.LOG_LEVEL_INFO)
			ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_INFO)
			ns.core.LogComponentEnable("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL)
			ns.core.LogComponentEnable("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL)
			ns.core.LogComponentEnable("Ipv6Interface", ns.core.LOG_LEVEL_ALL)
			ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL)
			ns.core.LogComponentEnable("NdiscCache", ns.core.LOG_LEVEL_ALL)

		print ("Creat nodes.")
		sta1 = ns.network.Node()
		r1 = ns.network.Node()
		r2 = ns.network.Node()
		sta2 = ns.network.Node()

		net1 = ns.network.NodeContainer()
		net1.Add(sta1)
		net1.Add(r1)
		net1.Add(r2)
		net2 = ns.network.NodeContainer()
		net2.Add(r2)
		net2.Add(sta2)
		all = ns.network.NodeContainer()  
		all.Add(sta1)
		all.Add(r1)
		all.Add(r2)
		all.Add(sta2)

		internetv6 = ns.internet.InternetStackHelper()
		internetv6.Install(all)

		print ("Create channels.")
		csma = ns.csma.CsmaHelper()
		csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
		csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
		ndc1 = csma.Install(net1)
		ndc2 = csma.Install(net2)

		print ("Assign IPv6 Addresses.")
		ipv6 = ns.internet.Ipv6AddressHelper()
		ipv6.SetBase (ns.network.Ipv6Address ("2001:1::"), ns.network.Ipv6Prefix (64))
		iic1 = ipv6.Assign (ndc1)
		iic1.SetForwarding (2, True)
		iic1.SetForwarding (1, True)
		iic1.SetDefaultRouteInAllNodes (1)

		ipv6.SetBase (ns.network.Ipv6Address ("2001:2::"), ns.network.Ipv6Prefix (64))
		iic2 = ipv6.Assign (ndc2)
		iic2.SetForwarding (0, True)
		iic2.SetDefaultRouteInAllNodes (0)

		routingHelper = ns.internet.Ipv6StaticRoutingHelper()

		# manually inject a static route to the second router.
		routing = routingHelper.GetStaticRouting (r1.GetObject(ns.internet.Ipv6.GetTypeId()))
		routing.AddHostRouteTo (iic2.GetAddress (1, 1), iic1.GetAddress (2, 0), iic1.GetInterfaceIndex (1))

		routingStream = ns.network.OutputStreamWrapper ("icmpv6-redirect.routes", ns.network.STD_IOS_OUT)
		routingHelper.PrintRoutingTableAt (ns.core.Seconds (0.0), r1, routingStream)
		routingHelper.PrintRoutingTableAt (ns.core.Seconds (3.0), sta1, routingStream)

		print ("Create Applications.")
		packetSize = 1024
		maxPacketCount = 5
		interPacketInterval = ns.core.Seconds (1.)
		ping6 = ns.internet_apps.Ping6Helper()

		ping6.SetLocal (iic1.GetAddress (0, 1))
		ping6.SetRemote (iic2.GetAddress (1, 1))
		ping6.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount))
		ping6.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval))
		ping6.SetAttribute ("PacketSize", ns.core.UintegerValue (packetSize))
		apps = ping6.Install(ns.network.NodeContainer(sta1))
		apps.Start (ns.core.Seconds (2.0))
		apps.Stop (ns.core.Seconds (10.0))

		ascii = ns.network.AsciiTraceHelper()
		csma.EnableAsciiAll (ascii.CreateFileStream ("icmpv6-redirect.tr"))
		csma.EnablePcapAll ("icmpv6-redirect", True)

		# Now, do the actual simulation.
		print ("Run Simulation.")
		ns.core.Simulator.Run ()
		ns.core.Simulator.Destroy ()
		print ("Done.")
Esempio n. 20
0
def main(argv):
    #
    # Allow the user to override any of the defaults and the above Bind() at
    # run-time, via command-line arguments
    #
    cmd = ns.core.CommandLine()

    cmd.packetSize = 1024
    cmd.packetCount = 10
    cmd.packetInterval = 1.0

    #Socket options for IPv6, currently TCLASS, HOPLIMIT, RECVTCLASS, and RECVHOPLIMIT
    cmd.ipv6Tclass = 0
    cmd.ipv6RecvTclass = True
    cmd.ipv6Hoplimit = 0
    cmd.ipv6RecvHoplimit = True

    cmd.AddValue("PacketSize", "Packet size in bytes")
    cmd.AddValue("PacketCount", "Number of packets to send")
    cmd.AddValue("Interval", "Interval between packets")
    cmd.AddValue("IPV6_TCLASS", "IPV6_TCLASS")
    cmd.AddValue("IPV6_RECVTCLASS", "IPV6_RECVTCLASS")
    cmd.AddValue("IPV6_HOPLIMIT", "IPV6_HOPLIMIT")
    cmd.AddValue("IPV6_RECVHOPLIMIT", "IPV6_RECVHOPLIMIT")
    cmd.Parse(argv)

    packetSize = int(cmd.packetSize)
    packetCount = int(cmd.packetCount)
    packetInterval = float(cmd.packetInterval)
    ipv6Tclass = int(cmd.ipv6Tclass)
    ipv6RecvTclass = bool(cmd.ipv6RecvTclass)
    ipv6Hoplimit = int(cmd.ipv6Hoplimit)
    ipv6RecvHoplimit = bool(cmd.ipv6RecvHoplimit)

    print("Create nodes.")
    n = ns.network.NodeContainer()
    n.Create(2)

    internet = ns.internet.InternetStackHelper()
    internet.Install(n)

    print("Create channels.")
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute("DataRate", ns.core.StringValue("5000000"))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
    d = csma.Install(n)

    print("Assign IP Addresses.")
    ipv6 = ns.internet.Ipv6AddressHelper()
    ipv6.SetBase(ns.network.Ipv6Address("2001:0000:f00d:cafe::"),
                 ns.network.Ipv6Prefix(64))
    i6 = ipv6.Assign(d)
    serverAddress = ns.network.Address(i6.GetAddress(1, 1))

    print("Create sockets.")
    #Receiver socket on n1
    tid = ns3.TypeId.LookupByName("ns3::UdpSocketFactory")
    recvSink = ns3.Socket.CreateSocket(n.Get(1), tid)
    local = ns3.Inet6SocketAddress(ns3.Ipv6Address.GetAny(), 4477)
    recvSink.SetIpv6RecvTclass(ipv6RecvTclass)
    recvSink.SetIpv6RecvHopLimit(ipv6RecvHoplimit)
    recvSink.Bind(local)
    recvSink.SetRecvCallback(ReceivePacket)

    #Sender socket on n0
    source = ns3.Socket.CreateSocket(n.Get(0), tid)
    remote = ns3.Inet6SocketAddress(i6.GetAddress(1, 1), 4477)

    #Set socket options, it is also possible to set the options after the socket has been created/connected.
    if ipv6Tclass != 0:
        source.SetIpv6Tclass(ipv6Tclass)

    if ipv6Hoplimit > 0:
        source.SetIpv6HopLimit(ipv6Hoplimit)

    source.Connect(remote)

    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("socket-options-ipv6-py.tr"))
    csma.EnablePcapAll("socket-options-ipv6", False)

    #Schedule SendPacket
    interPacketInterval = ns.core.Seconds(packetInterval)
    ns.core.Simulator.ScheduleWithContext(source.GetNode().GetId(),
                                          ns.core.Seconds(1.0), SendPacket,
                                          source, packetSize, packetCount,
                                          interPacketInterval)

    print("Run Simulation.")
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    print("Done.")
Esempio n. 21
0
def main(argv):
    #
    #  First, we declare and initialize a few local variables that control some
    #  simulation parameters.
    #
    backboneNodes = 10
    infraNodes = 5
    lanNodes = 5
    stopTime = 10

    #
    #  Simulation defaults are typically set next, before command line
    #  arguments are parsed.
    #
    ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize",
                              ns.core.StringValue("210"))
    ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate",
                              ns.core.StringValue("448kb/s"))

    #
    #  For convenience, we add the local variables to the command line argument
    #  system so that they can be overridden with flags such as
    #  "--backboneNodes=20"
    #
    cmd = ns.core.CommandLine()
    #cmd.AddValue("backboneNodes", "number of backbone nodes", backboneNodes)
    #cmd.AddValue("infraNodes", "number of leaf nodes", infraNodes)
    #cmd.AddValue("lanNodes", "number of LAN nodes", lanNodes)
    #cmd.AddValue("stopTime", "simulation stop time(seconds)", stopTime)

    #
    #  The system global variables and the local values added to the argument
    #  system can be overridden by command line arguments by using this call.
    #
    cmd.Parse(argv)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the backbone                                                #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #
    #  Create a container to manage the nodes of the adhoc(backbone) network.
    #  Later we'll create the rest of the nodes we'll need.
    #
    backbone = ns.network.NodeContainer()
    backbone.Create(backboneNodes)
    #
    #  Create the backbone wifi net devices and install them into the nodes in
    #  our container
    #
    wifi = ns.wifi.WifiHelper()
    mac = ns.wifi.NqosWifiMacHelper.Default()
    mac.SetType("ns3::AdhocWifiMac")
    wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode",
                                 ns.core.StringValue("OfdmRate54Mbps"))
    wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
    wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
    wifiPhy.SetChannel(wifiChannel.Create())
    backboneDevices = wifi.Install(wifiPhy, mac, backbone)
    #
    #  Add the IPv4 protocol stack to the nodes in our container
    #
    print "Enabling OLSR routing on all backbone nodes"
    internet = ns.internet.InternetStackHelper()
    olsr = ns.olsr.OlsrHelper()
    internet.SetRoutingHelper(olsr)
    # has effect on the next Install ()
    internet.Install(backbone)
    # re-initialize for non-olsr routing.
    internet.Reset()
    #
    #  Assign IPv4 addresses to the device drivers(actually to the associated
    #  IPv4 interfaces) we just created.
    #
    ipAddrs = ns.internet.Ipv4AddressHelper()
    ipAddrs.SetBase(ns.network.Ipv4Address("192.168.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))
    ipAddrs.Assign(backboneDevices)

    #
    #  The ad-hoc network nodes need a mobility model so we aggregate one to
    #  each of the nodes we just finished building.
    #
    mobility = ns.mobility.MobilityHelper()
    positionAlloc = ns.mobility.ListPositionAllocator()
    x = 0.0
    for i in range(backboneNodes):
        positionAlloc.Add(ns.core.Vector(x, 0.0, 0.0))
        x += 5.0
    mobility.SetPositionAllocator(positionAlloc)
    mobility.SetMobilityModel(
        "ns3::RandomDirection2dMobilityModel", "Bounds",
        ns.mobility.RectangleValue(ns.mobility.Rectangle(0, 1000, 0,
                                                         1000)), "Speed",
        ns.core.RandomVariableValue(ns.core.ConstantVariable(2000)), "Pause",
        ns.core.RandomVariableValue(ns.core.ConstantVariable(0.2)))
    mobility.Install(backbone)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the LANs                                                    #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #  Reset the address base-- all of the CSMA networks will be in
    #  the "172.16 address space
    ipAddrs.SetBase(ns.network.Ipv4Address("172.16.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        print "Configuring local area network for backbone node ", i
        #
        #  Create a container to manage the nodes of the LAN.  We need
        #  two containers here; one with all of the new nodes, and one
        #  with all of the nodes including new and existing nodes
        #
        newLanNodes = ns.network.NodeContainer()
        newLanNodes.Create(lanNodes - 1)
        #  Now, create the container with all nodes on this link
        lan = ns.network.NodeContainer(
            ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
        #
        #  Create the CSMA net devices and install them into the nodes in our
        #  collection.
        #
        csma = ns.csma.CsmaHelper()
        csma.SetChannelAttribute(
            "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
        csma.SetChannelAttribute("Delay",
                                 ns.core.TimeValue(ns.core.MilliSeconds(2)))
        lanDevices = csma.Install(lan)
        #
        #  Add the IPv4 protocol stack to the new LAN nodes
        #
        internet.Install(newLanNodes)
        #
        #  Assign IPv4 addresses to the device drivers(actually to the
        #  associated IPv4 interfaces) we just created.
        #
        ipAddrs.Assign(lanDevices)
        #
        #  Assign a new network prefix for the next LAN, according to the
        #  network mask initialized above
        #
        ipAddrs.NewNetwork()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Construct the mobile networks                                         #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #  Reset the address base-- all of the 802.11 networks will be in
    #  the "10.0" address space
    ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"),
                    ns.network.Ipv4Mask("255.255.255.0"))

    for i in range(backboneNodes):
        print "Configuring wireless network for backbone node ", i
        #
        #  Create a container to manage the nodes of the LAN.  We need
        #  two containers here; one with all of the new nodes, and one
        #  with all of the nodes including new and existing nodes
        #
        stas = ns.network.NodeContainer()
        stas.Create(infraNodes - 1)
        #  Now, create the container with all nodes on this link
        infra = ns.network.NodeContainer(
            ns.network.NodeContainer(backbone.Get(i)), stas)
        #
        #  Create another ad hoc network and devices
        #
        ssid = ns.wifi.Ssid('wifi-infra' + str(i))
        wifiInfra = ns.wifi.WifiHelper.Default()
        wifiPhy.SetChannel(wifiChannel.Create())
        wifiInfra.SetRemoteStationManager('ns3::ArfWifiManager')
        macInfra = ns.wifi.NqosWifiMacHelper.Default()
        macInfra.SetType("ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue(ssid),
                         "ActiveProbing", ns.core.BooleanValue(False))

        # setup stas
        staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
        # setup ap.
        macInfra.SetType("ns3::ApWifiMac", "Ssid",
                         ns.wifi.SsidValue(ssid), "BeaconGeneration",
                         ns.core.BooleanValue(True), "BeaconInterval",
                         ns.core.TimeValue(ns.core.Seconds(2.5)))
        apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
        # Collect all of these new devices
        infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)

        #  Add the IPv4 protocol stack to the nodes in our container
        #
        internet.Install(stas)
        #
        #  Assign IPv4 addresses to the device drivers(actually to the associated
        #  IPv4 interfaces) we just created.
        #
        ipAddrs.Assign(infraDevices)
        #
        #  Assign a new network prefix for each mobile network, according to
        #  the network mask initialized above
        #
        ipAddrs.NewNetwork()
        #
        #  The new wireless nodes need a mobility model so we aggregate one
        #  to each of the nodes we just finished building.
        #
        subnetAlloc = ns.mobility.ListPositionAllocator()
        for j in range(infra.GetN()):
            subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))

        mobility.PushReferenceMobilityModel(backbone.Get(i))
        mobility.SetPositionAllocator(subnetAlloc)
        mobility.SetMobilityModel(
            "ns3::RandomDirection2dMobilityModel", "Bounds",
            ns.mobility.RectangleValue(ns.mobility.Rectangle(-25, 25, -25,
                                                             25)), "Speed",
            ns.core.RandomVariableValue(ns.core.ConstantVariable(30)), "Pause",
            ns.core.RandomVariableValue(ns.core.ConstantVariable(0.4)))
        mobility.Install(infra)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Application configuration                                             #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    #  Create the OnOff application to send UDP datagrams of size
    #  210 bytes at a rate of 448 Kb/s, between two nodes
    print "Create Applications."
    port = 9  #  Discard port(RFC 863)

    #  Let's make sure that the user does not define too few LAN nodes
    #  to make this example work.  We need lanNodes >= 5
    assert (lanNodes >= 5)
    appSource = ns.network.NodeList.GetNode(11)
    appSink = ns.network.NodeList.GetNode(13)
    remoteAddr = ns.network.Ipv4Address("172.16.0.5")

    onoff = ns.applications.OnOffHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
    onoff.SetAttribute(
        "OnTime", ns.core.RandomVariableValue(ns.core.ConstantVariable(1)))
    onoff.SetAttribute(
        "OffTime", ns.core.RandomVariableValue(ns.core.ConstantVariable(0)))
    apps = onoff.Install(ns.network.NodeContainer(appSource))
    apps.Start(ns.core.Seconds(3.0))
    apps.Stop(ns.core.Seconds(20.0))

    #  Create a packet sink to receive these packets
    sink = ns.applications.PacketSinkHelper(
        "ns3::UdpSocketFactory",
        ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
    apps = sink.Install(ns.network.NodeContainer(appSink))
    apps.Start(ns.core.Seconds(3.0))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
    #                                                                        #
    #  Tracing configuration                                                 #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /

    print "Configure Tracing."
    #
    #  Let's set up some ns-2-like ascii traces, using another helper class
    #
    #std.ofstream ascii
    #ascii = ns.core.AsciiTraceHelper();
    #stream = ascii.CreateFileStream("mixed-wireless.tr");
    #wifiPhy.EnableAsciiAll(stream);
    #csma.EnableAsciiAll(stream);
    print "(tracing not done for Python)"
    #  Look at nodes 11, 13 only
    # WifiHelper.EnableAscii(ascii, 11, 0);
    # WifiHelper.EnableAscii(ascii, 13, 0);

    #  Let's do a pcap trace on the backbone devices
    wifiPhy.EnablePcap("mixed-wireless", backboneDevices)
    #  Let's additionally trace the application Sink, ifIndex 0
    csma = ns.csma.CsmaHelper()
    csma.EnablePcapAll("mixed-wireless", False)

    #   #ifdef ENABLE_FOR_TRACING_EXAMPLE
    #     Config.Connect("/NodeList/*/$MobilityModel/CourseChange",
    #       MakeCallback(&CourseChangeCallback))
    #   #endif

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    #                                                                        #
    #  Run simulation                                                        #
    #                                                                        #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    print "Run Simulation."
    ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
Esempio n. 22
0
def main(argv):
	#
	# Users may find it convenient to turn on explicit debugging
	# for selected modules; the below lines suggest how to do this
	#
	# ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_ALL)
	# ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_ALL)

	#
	# Allow the user to override any of the defaults at
	# run-time, via command-line arguments
	#
	cmd = ns.core.CommandLine ()
	cmd.useIpv6 = "False"
	cmd.AddValue ("useIpv6", "Use Ipv6")
	cmd.Parse (argv)
	
	# 
	# Explicitly create the nodes required by the topology (shown above).
	# 
	print ("Create nodes.")
	n = ns.network.NodeContainer ()
	n.Create (4)
	
	internetstack = ns.internet.InternetStackHelper ()
	internetstack.Install (n)
	
	# 
	# Explicitly create the channels required by the topology (shown above).
	# 
	print ("Create channels.")
	csma = ns.csma.CsmaHelper ()
	csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("5000000"))
	csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.MilliSeconds (2)))
	csma.SetDeviceAttribute ("Mtu", ns.core.UintegerValue (1400))
	d = csma.Install (n)
	
	# 
	# We've got the "hardware" in place.  Now we need to add IP addresses.
	# 
	print ("Assign IP Addresses.")
	if (cmd.useIpv6 == "False"):
		ipv4 = ns.internet.Ipv4AddressHelper ()
		ipv4.SetBase (ns.network.Ipv4Address ("10.1.1.0"), ns.network.Ipv4Mask ("255.255.255.0"))
		i = ipv4.Assign (d)
		serverAddress = ns.network.Address (i.GetAddress (1))
	    
	else:
		ipv6 = ns.internet.Ipv6AddressHelper ()
		ipv6.SetBase (ns.network.Ipv6Address ("2001:0000:f00d:cafe::"), ns.network.Ipv6Prefix (64))
		i6 = ipv6.Assign (d)
		serverAddress = ns.network.Address (i6.GetAddress (1, 1))

	print ("Create Applications.")
	# 
	# Create a UdpEchoServer application on node one.
	#
	port = 9  # well-known echo port number
	server = ns.applications.UdpEchoServerHelper (port)
	serverapps = server.Install (n.Get (1))
	serverapps.Start (ns.core.Seconds (1.0))
	serverapps.Stop (ns.core.Seconds (10.0))
	
	# 
	# Create a UdpEchoClient application to send UDP datagrams from node zero to
	# node one
	#
	packetSize = 1024
	maxPacketCount = 1
	interPacketInterval = ns.core.Seconds (1.0)
	
	client = ns.applications.UdpEchoClientHelper (serverAddress, port)
	client.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount))
	client.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval))
	client.SetAttribute ("PacketSize", ns.core.UintegerValue (packetSize))
	
	apps = client.Install (n.Get (0))
	apps.Start (ns.core.Seconds (2.0))
	apps.Stop (ns.core.Seconds (10.0))
	
	#
	# Users may find it convenient to initialize echo packets with actual data;
	# the below lines suggest how to do this
	#
 	# client.SetFill (apps.Get (0), "Hello World")
	#
	# client.SetFill (apps.Get (0), 0xa5, 1024)
	#
	# Following does not work as intended.
	# fill = [0, 1, 2, 3, 4, 5, 6]
	# client.SetFill (apps.Get (0), fill[0], 1024)

	asciitracer = ns.network.AsciiTraceHelper ()
	csma.EnableAsciiAll (asciitracer.CreateFileStream ("udp-echo-py.tr"))
	csma.EnablePcapAll ("udp-echo-py", False)
	
	# 
	# Now, do the actual simulation.
	# 
	print ("Run Simulation.")
	ns.core.Simulator.Run ()
	ns.core.Simulator.Destroy ()
	print ("Done.")
Esempio n. 23
0
def main(argv):

    cmd = ns.core.CommandLine();

    cmd.Parse(argv);

    # Create nodes
    print "Create nodes"
    n0 = ns.network.Node();
    r = ns.network.Node();
    n1 = ns.network.Node();

    net1 = ns.network.NodeContainer();
    net1.Add(n0);
    net1.Add(r);
    net2 = ns.network.NodeContainer();
    net2.Add(r);
    net2.Add(n1);
    all = ns.network.NodeContainer();
    all.Add(n0);
    all.Add(r);
    all.Add(n1);

    # Create IPv6 Internet Stack
    internetv6 = ns.internet.InternetStackHelper();
    internetv6.Install(all);

    # Create channels
    csma = ns.csma.CsmaHelper();
    csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)));
    csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
    d1 = csma.Install(net1);
    d2 = csma.Install(net2);

    # Create networks and assign IPv6 Addresses
    print "Addressing"
    ipv6 = ns.internet.Ipv6AddressHelper();
    ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64));
    i1 = ipv6.Assign(d1);
    i1.SetForwarding(1, True);
    i1.SetDefaultRouteInAllNodes(1);
    ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64));
    i2 = ipv6.Assign(d2);
    i2.SetForwarding(0, True);
    i2.SetDefaultRouteInAllNodes(0);

    # Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r 
    print "Application"
    packetSize = 1024;
    maxPacketCount = 5;
    interPacketInterval = ns.core.Seconds(1.);
    ping6 = ns.applications.Ping6Helper();

    ping6.SetLocal(i1.GetAddress(0, 1));
    ping6.SetRemote(i2.GetAddress(1, 1)); 

    ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount));
    ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval));
    ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize));

    apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)));
    apps.Start(ns.core.Seconds(2.0));
    apps.Stop(ns.core.Seconds(20.0));

    print "Tracing"
    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"))
    csma.EnablePcapAll("simple-routing-ping6", True)

    # Run Simulation     
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
Esempio n. 24
0
def main(argv):

    cmd = ns.core.CommandLine()
    cmd.Parse(argv)

    terminals = ns.network.NodeContainer()
    terminals.Create(4)

    csmaSwitch = ns.network.NodeContainer()
    csmaSwitch.Create(1)

    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))

    terminalDevices = ns.network.NetDeviceContainer()
    switchDevices = ns.network.NetDeviceContainer()

    for i in range(4):
        link = csma.Install(
            ns.network.NodeContainer(
                ns.network.NodeContainer(terminals.Get(i)), csmaSwitch))
        terminalDevices.Add(link.Get(0))
        switchDevices.Add(link.Get(1))

    switchNode = csmaSwitch.Get(0)
    bridgeDevice = ns.bridge.BridgeNetDevice()
    switchNode.AddDevice(bridgeDevice)

    for portIter in range(switchDevices.GetN()):
        bridgeDevice.AddBridgePort(switchDevices.Get(portIter))

    internet = ns.internet.InternetStackHelper()
    internet.Install(terminals)

    ipv4 = ns.internet.Ipv4AddressHelper()
    ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"),
                 ns.network.Ipv4Mask("255.255.255.0"))
    ipv4.Assign(terminalDevices)

    port = 9

    onoff = ns.applications.OnOffHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(
            ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.2"),
                                         port)))
    onoff.SetAttribute(
        "OnTime", ns.core.RandomVariableValue(ns.core.ConstantVariable(1)))
    onoff.SetAttribute(
        "OffTime", ns.core.RandomVariableValue(ns.core.ConstantVariable(0)))

    app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))

    app.Start(ns.core.Seconds(1.0))
    app.Stop(ns.core.Seconds(10.0))

    sink = ns.applications.PacketSinkHelper(
        "ns3::UdpSocketFactory",
        ns.network.Address(
            ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(),
                                         port)))
    app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
    app.Start(ns.core.Seconds(0.0))

    onoff.SetAttribute(
        "Remote",
        ns.network.AddressValue(
            ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"),
                                         port)))
    app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
    app.Start(ns.core.Seconds(1.1))
    app.Stop(ns.core.Seconds(10.0))

    app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
    app.Start(ns.core.Seconds(0.0))

    csma.EnablePcapAll("csma-bridge", False)

    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
Esempio n. 25
0
p2pNodes2.Create(2)

csmaNodes = ns.network.NodeContainer()
csmaNodes.Add(p2pNodes.Get(1))
csmaNodes.Add(p2pNodes2.Get(0))
csmaNodes.Create(nCsma)

pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))

p2pDevices = pointToPoint.Install(p2pNodes)
p2pDevices2 = pointToPoint.Install(p2pNodes2)

csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))

csmaDevices = csma.Install(csmaNodes)

stack = ns.internet.InternetStackHelper()
stack.Install(p2pNodes.Get(0))
stack.Install(csmaNodes)
stack.Install(p2pNodes2.Get(1))

address = ns.internet.Ipv4AddressHelper()
address.SetBase(ns.network.Ipv4Address("10.1.1.0"),
                ns.network.Ipv4Mask("255.255.255.0"))
p2pInterfaces = address.Assign(p2pDevices)

address.SetBase(ns.network.Ipv4Address("10.1.2.0"),
def main(argv):

    print("Ping6Example")

    cmd = ns.core.CommandLine()
    cmd.AddValue("verbose", "turn on log components")
    cmd.verbose = False
    cmd.Parse(argv)
    verbose = bool(cmd.verbose)

    if verbose:
        ns.core.LogComponentEnable("Ping6Example", ns.core.LOG_LEVEL_INFO)
        ns.core.LogComponentEnable("Ipv6EndPointDemux", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6L3Protocol", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6StaticRouting", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6ListRouting", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ipv6Interface", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Icmpv6L4Protocol", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("Ping6Application", ns.core.LOG_LEVEL_ALL)
        ns.core.LogComponentEnable("NdiscCache", ns.core.LOG_LEVEL_ALL)

    print("Create nodes.")
    n = ns.network.NodeContainer()
    n.Create(4)

    #  Install IPv4/IPv6 stack
    internetv6 = ns.internet.InternetStackHelper()
    internetv6.SetIpv4StackInstall(False)
    internetv6.Install(n)

    print("Create channels.")
    csma = ns.csma.CsmaHelper()
    csma.SetChannelAttribute(
        "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
    csma.SetChannelAttribute("Delay",
                             ns.core.TimeValue(ns.core.MilliSeconds(2)))
    d = csma.Install(n)

    ipv6 = ns.internet.Ipv6AddressHelper()
    print("Assign IPv6 addresses")
    i = ipv6.Assign(d)

    print("Create applications.")

    # Create a Ping6 application to send ICMPv6 echo request from node zero to
    # all-nodes (ff02::1).
    #
    packetSize = 1024
    maxPacketCount = 5
    interPacketInterval = ns.core.Seconds(1.)
    ping6 = ns.internet_apps.Ping6Helper()

    # ping6.SetLocal (i.GetAddress (0, 1))
    # ping6.SetRemote(i.GetAddress(1, 1))
    ping6.SetIfIndex(i.GetInterfaceIndex(0))
    ping6.SetRemote(ns.network.Ipv6Address.GetAllNodesMulticast())

    ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
    ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
    ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
    apps = ping6.Install(ns.network.NodeContainer(n.Get(0)))
    apps.Start(ns.core.Seconds(2.0))
    apps.Stop(ns.core.Seconds(10.0))

    ascii = ns.network.AsciiTraceHelper()
    csma.EnableAsciiAll(ascii.CreateFileStream("ping6.tr"))
    csma.EnablePcapAll("ping6", True)

    print("Run simulation")
    ns.core.Simulator.Run()
    ns.core.Simulator.Destroy()
    print("Done")