def testAttributes(self): ## ## Yes, I know, the GetAttribute interface for Python is ## horrible, we should fix this soon, I hope. ## queue = ns3.DropTailQueue() queue.SetAttribute("MaxPackets", ns3.UintegerValue(123456)) limit = ns3.UintegerValue() queue.GetAttribute("MaxPackets", limit) self.assertEqual(limit.Get(), 123456) ## -- object pointer values mobility = ns3.RandomWaypointMobilityModel() ptr = ns3.PointerValue() mobility.GetAttribute("PositionAllocator", ptr) self.assertEqual(ptr.GetObject(), None) pos = ns3.ListPositionAllocator() mobility.SetAttribute("PositionAllocator", ns3.PointerValue(pos)) ptr = ns3.PointerValue() mobility.GetAttribute("PositionAllocator", ptr) self.assert_(ptr.GetObject() is not None)
olsr = ns3.OlsrHelper() #Add the IPv4 protocol stack to the nodes in our container internet=ns3.InternetStackHelper() internet.SetRoutingHelper (olsr) internet.Install (nodes) ipAddrss= ns3.Ipv4AddressHelper() ipAddrss.SetBase( ns3.Ipv4Address("192.168.0.0"), ns3.Ipv4Mask("255.255.255.0")); ipContainer = ipAddrss.Assign(devices); mobility = ns3.MobilityHelper() positionAlloc = ns3.ListPositionAllocator() positionAlloc.Add(ns3.Vector (100,100,0.0)) positionAlloc.Add(ns3.Vector (100,200,0.0)) mobility.SetPositionAllocator(positionAlloc) mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); mobility.Install(nodes) pingHelper = ns3.V4PingHelper(ipContainer.GetAddress(0)) pingHelper.SetAttribute("Interval", ns3.TimeValue(ns3.Seconds(1.0))) apps = pingHelper.Install(nodes.Get(0)) apps.Start(ns.core.Seconds(1.0)) apps.Stop(ns.core.Seconds(10.0)) ns.core.Simulator.Run()
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. # ns3.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns3.StringValue("210")) ns3.Config.SetDefault("ns3::OnOffApplication::DataRate", ns3.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 = ns3.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 = ns3.NodeContainer() backbone.Create(backboneNodes) # # Create the backbone wifi net devices and install them into the nodes in # our container # wifi = ns3.WifiHelper() mac = ns3.NqosWifiMacHelper.Default() mac.SetType("ns3::AdhocWifiMac") wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", ns3.StringValue("OfdmRate54Mbps")) wifiPhy = ns3.YansWifiPhyHelper.Default() wifiChannel = ns3.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 = ns3.InternetStackHelper() olsr = ns3.OlsrHelper() internet.SetRoutingHelper(olsr) 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 = ns3.Ipv4AddressHelper() ipAddrs.SetBase(ns3.Ipv4Address("192.168.0.0"), ns3.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 = ns3.MobilityHelper() positionAlloc = ns3.ListPositionAllocator() x = 0.0 for i in range(backboneNodes): positionAlloc.Add(ns3.Vector(x, 0.0, 0.0)) x += 5.0 mobility.SetPositionAllocator(positionAlloc) mobility.SetMobilityModel( "ns3::RandomDirection2dMobilityModel", "Bounds", ns3.RectangleValue(ns3.Rectangle(0, 1000, 0, 1000)), "Speed", ns3.RandomVariableValue(ns3.ConstantVariable(2000)), "Pause", ns3.RandomVariableValue(ns3.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(ns3.Ipv4Address("172.16.0.0"), ns3.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 = ns3.NodeContainer() newLanNodes.Create(lanNodes - 1) # Now, create the container with all nodes on this link lan = ns3.NodeContainer(ns3.NodeContainer(backbone.Get(i)), newLanNodes) # # Create the CSMA net devices and install them into the nodes in our # collection. # csma = ns3.CsmaHelper() csma.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(5000000))) csma.SetChannelAttribute("Delay", ns3.TimeValue(ns3.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(ns3.Ipv4Address("10.0.0.0"), ns3.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 = ns3.NodeContainer() stas.Create(infraNodes - 1) # Now, create the container with all nodes on this link infra = ns3.NodeContainer(ns3.NodeContainer(backbone.Get(i)), stas) # # Create another ad hoc network and devices # ssid = ns3.Ssid('wifi-infra' + str(i)) wifiInfra = ns3.WifiHelper.Default() wifiPhy.SetChannel(wifiChannel.Create()) wifiInfra.SetRemoteStationManager('ns3::ArfWifiManager') macInfra = ns3.NqosWifiMacHelper.Default() macInfra.SetType("ns3::NqstaWifiMac", "Ssid", ns3.SsidValue(ssid), "ActiveProbing", ns3.BooleanValue(False)) # setup stas staDevices = wifiInfra.Install(wifiPhy, macInfra, stas) # setup ap. macInfra.SetType("ns3::NqapWifiMac", "Ssid", ns3.SsidValue(ssid), "BeaconGeneration", ns3.BooleanValue(True), "BeaconInterval", ns3.TimeValue(ns3.Seconds(2.5))) apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i)) # Collect all of these new devices infraDevices = ns3.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 = ns3.ListPositionAllocator() for j in range(infra.GetN()): subnetAlloc.Add(ns3.Vector(0.0, j, 0.0)) mobility.PushReferenceMobilityModel(backbone.Get(i)) mobility.SetPositionAllocator(subnetAlloc) mobility.SetMobilityModel( "ns3::RandomDirection2dMobilityModel", "Bounds", ns3.RectangleValue(ns3.Rectangle(-25, 25, -25, 25)), "Speed", ns3.RandomVariableValue(ns3.ConstantVariable(30)), "Pause", ns3.RandomVariableValue(ns3.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 = ns3.NodeList.GetNode(11) appSink = ns3.NodeList.GetNode(13) remoteAddr = ns3.Ipv4Address("172.16.0.5") onoff = ns3.OnOffHelper( "ns3::UdpSocketFactory", ns3.Address(ns3.InetSocketAddress(remoteAddr, port))) onoff.SetAttribute("OnTime", ns3.RandomVariableValue(ns3.ConstantVariable(1))) onoff.SetAttribute("OffTime", ns3.RandomVariableValue(ns3.ConstantVariable(0))) apps = onoff.Install(ns3.NodeContainer(appSource)) apps.Start(ns3.Seconds(3.0)) apps.Stop(ns3.Seconds(20.0)) # Create a packet sink to receive these packets sink = ns3.PacketSinkHelper( "ns3::UdpSocketFactory", ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), port)) apps = sink.Install(ns3.NodeContainer(appSink)) apps.Start(ns3.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 = ns3.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 = ns3.CsmaHelper() csma.EnablePcapAll("mixed-wireless", False) # #ifdef ENABLE_FOR_TRACING_EXAMPLE # Config.Connect("/NodeList/*/$MobilityModel/CourseChange", # MakeCallback(&CourseChangeCallback)) # #endif # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Run simulation # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # print "Run Simulation." ns3.Simulator.Stop(ns3.Seconds(stopTime)) ns3.Simulator.Run() ns3.Simulator.Destroy()
def create_network(netinfo): """Create a network Struct from a RadioMobile parsed text report.""" nodes = {} for name, attrs in netinfo["units"].iteritems(): node = lib.Struct("Node", name=name, location=attrs["location"], ns3_node=ns3.Node(), devices={}) nodes[name] = node ns3node_to_node = dict( (node.ns3_node.GetId(), node) for node in nodes.values()) def get_node_from_ns3node(ns3_node): return ns3node_to_node[ns3_node.GetId()] # Internet stack stack = ns3.InternetStackHelper() for name, node in nodes.iteritems(): stack.Install(node.ns3_node) networks = {} for net_index, (net_name, network) in enumerate(netinfo["networks"].iteritems()): # Nodes node = network["node"] node_member = node["name"] terminal_members = [ terminal["name"] for terminal in network["terminals"] ] ap_node = nodes[node_member].ns3_node sta_nodes = ns3.NodeContainer() for name in terminal_members: sta_nodes.Add(nodes[name].ns3_node) networks[name] = lib.Struct("network", node=node_member, terminals=terminal_members) mode = network["mode"] network_info = dict( (d["name"], d) for d in [network["node"]] + network["terminals"]) # Configure WiFi or WiMax devices if mode["standard"].startswith("wifi"): wifi_network(network_info, net_index, net_name, mode["wifi_mode"], nodes, get_node_from_ns3node, node_member, terminal_members) elif mode["standard"].startswith("wimax"): scheduler = getattr( ns3.WimaxHelper, "SCHED_TYPE_" + mode["wimax_scheduler"].upper()) wimax_network(network_info, net_index, net_name, nodes, get_node_from_ns3node, node_member, terminal_members, scheduler) else: raise ValueError, ("Network name must be 'name [wifi_with_ns3_mode" + "| wimax-scheduler]': %s") % ns3_mode # Mobility mobility = ns3.MobilityHelper() mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel") for member in [node_member] + terminal_members: node = nodes[member] allocator = ns3.ListPositionAllocator() position = tuple(node.location) + (0, ) #position = (0, 0, 0) # debug allocator.Add(ns3.Vector(*position)) mobility.SetPositionAllocator(allocator) mobility.Install(node.ns3_node) ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables() return lib.Struct("Network", nodes=nodes, networks=networks)
def main(argv): # # We are interacting with the outside, real, world. This means we have to # interact in real-time and therefore we have to use the real-time simulator # and take the time to calculate checksums. # ns3.GlobalValue.Bind("SimulatorImplementationType", ns3.StringValue("ns3::RealtimeSimulatorImpl")) ns3.GlobalValue.Bind("ChecksumEnabled", ns3.BooleanValue("true")) # # Create two ghost nodes. The first will represent the virtual machine host # on the left side of the network; and the second will represent the VM on # the right side. # nodes = ns3.NodeContainer() nodes.Create(2) # # We're going to use 802.11 A so set up a wifi helper to reflect that. # wifi = ns3.WifiHelper.Default() wifi.SetStandard(ns3.WIFI_PHY_STANDARD_80211a) wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", ns3.StringValue("wifia-54mbs")) # # No reason for pesky access points, so we'll use an ad-hoc network. # wifiMac = ns3.NqosWifiMacHelper.Default() wifiMac.SetType("ns3::AdhocWifiMac") # # Configure the physcial layer. # wifiChannel = ns3.YansWifiChannelHelper.Default() wifiPhy = ns3.YansWifiPhyHelper.Default() wifiPhy.SetChannel(wifiChannel.Create()) # # Install the wireless devices onto our ghost nodes. # devices = wifi.Install(wifiPhy, wifiMac, nodes) # # We need location information since we are talking about wifi, so add a # constant position to the ghost nodes. # mobility = ns3.MobilityHelper() positionAlloc = ns3.ListPositionAllocator() positionAlloc.Add(ns3.Vector(0.0, 0.0, 0.0)) positionAlloc.Add(ns3.Vector(5.0, 0.0, 0.0)) mobility.SetPositionAllocator(positionAlloc) mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel") mobility.Install(nodes) # # Use the TapBridgeHelper to connect to the pre-configured tap devices for # the left side. We go with "UseLocal" mode since the wifi devices do not # support promiscuous mode (because of their natures0. This is a special # case mode that allows us to extend a linux bridge into ns-3 IFF we will # only see traffic from one other device on that bridge. That is the case # for this configuration. # tapBridge = ns3.TapBridgeHelper() tapBridge.SetAttribute("Mode", ns3.StringValue("UseLocal")) tapBridge.SetAttribute("DeviceName", ns3.StringValue("tap-left")) tapBridge.Install(nodes.Get(0), devices.Get(0)) # # Connect the right side tap to the right side wifi device on the right-side # ghost node. # tapBridge.SetAttribute("DeviceName", ns3.StringValue("tap-right")) tapBridge.Install(nodes.Get(1), devices.Get(1)) # # Run the simulation for ten minutes to give the user time to play around # ns3.Simulator.Stop(ns3.Seconds(600)) ns3.Simulator.Run(signal_check_frequency=-1) ns3.Simulator.Destroy() return 0