examples/mixed-wireless.py
changeset 3422 ad0c05e68792
child 3536 087b1b45b3b5
equal deleted inserted replaced
3421:b9424c43753d 3422:ad0c05e68792
       
     1 # /*
       
     2 #  * This program is free software; you can redistribute it and/or modify
       
     3 #  * it under the terms of the GNU General Public License version 2 as
       
     4 #  * published by the Free Software Foundation;
       
     5 #  *
       
     6 #  * This program is distributed in the hope that it will be useful,
       
     7 #  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
     8 #  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
     9 #  * GNU General Public License for more details.
       
    10 #  *
       
    11 #  * You should have received a copy of the GNU General Public License
       
    12 #  * along with this program; if not, write to the Free Software
       
    13 #  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    14 #  *
       
    15 #  */
       
    16 
       
    17 # 
       
    18 #  This ns-3 example demonstrates the use of helper functions to ease 
       
    19 #  the construction of simulation scenarios.  
       
    20 #  
       
    21 #  The simulation topology consists of a mixed wired and wireless
       
    22 #  scenario in which a hierarchical mobility model is used.
       
    23 # 
       
    24 #  The simulation layout consists of N backbone routers interconnected
       
    25 #  by an ad hoc wifi network.
       
    26 #  Each backbone router also has a local 802.11 network and is connected
       
    27 #  to a local LAN.  An additional set of(K-1) nodes are connected to
       
    28 #  this backbone.  Finally, a local LAN is connected to each router
       
    29 #  on the backbone, with L-1 additional hosts.  
       
    30 # 
       
    31 #  The nodes are populated with TCP/IP stacks, and OLSR unicast routing
       
    32 #  on the backbone.  An example UDP transfer is shown.  The simulator
       
    33 #  be configured to output tcpdumps or traces from different nodes.
       
    34 # 
       
    35 # 
       
    36 #           +--------------------------------------------------------+
       
    37 #           |                                                        |
       
    38 #           |              802.11 ad hoc, ns-2 mobility              | 
       
    39 #           |                                                        |
       
    40 #           +--------------------------------------------------------+
       
    41 #                    |       o o o(N backbone routers)       |
       
    42 #                +--------+                               +--------+
       
    43 #      wired LAN | mobile |                     wired LAN | mobile |
       
    44 #     -----------| router |                    -----------| router |
       
    45 #                ---------                                ---------
       
    46 #                    |                                        |
       
    47 #           +----------------+                       +----------------+
       
    48 #           |     802.11     |                       |     802.11     |
       
    49 #           |      net       |                       |       net      |
       
    50 #           |   K-1 hosts    |                       |   K-1 hosts    |
       
    51 #           +----------------+                       +----------------+
       
    52 # 
       
    53 
       
    54 import ns3
       
    55 
       
    56 # # 
       
    57 # #  This function will be used below as a trace sink
       
    58 # #  
       
    59 # static void
       
    60 # CourseChangeCallback(std.string path, Ptr<const MobilityModel> model)
       
    61 # {
       
    62 #   Vector position = model.GetPosition();
       
    63 #   std.cout << "CourseChange " << path << " x=" << position.x << ", y=" << position.y << ", z=" << position.z << std.endl;
       
    64 # }
       
    65 
       
    66 def main(argv): 
       
    67     # 
       
    68     #  First, we declare and initialize a few local variables that control some 
       
    69     #  simulation parameters.
       
    70     # 
       
    71     backboneNodes = 10
       
    72     infraNodes = 5
       
    73     lanNodes = 5
       
    74     stopTime = 10
       
    75 
       
    76     # 
       
    77     #  Simulation defaults are typically set next, before command line
       
    78     #  arguments are parsed.
       
    79     # 
       
    80     ns3.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns3.StringValue("210"))
       
    81     ns3.Config.SetDefault("ns3::OnOffApplication::DataRate", ns3.StringValue("448kb/s"))
       
    82 
       
    83     # 
       
    84     #  For convenience, we add the local variables to the command line argument
       
    85     #  system so that they can be overridden with flags such as 
       
    86     #  "--backboneNodes=20"
       
    87     # 
       
    88     cmd = ns3.CommandLine()
       
    89     #cmd.AddValue("backboneNodes", "number of backbone nodes", backboneNodes)
       
    90     #cmd.AddValue("infraNodes", "number of leaf nodes", infraNodes)
       
    91     #cmd.AddValue("lanNodes", "number of LAN nodes", lanNodes)
       
    92     #cmd.AddValue("stopTime", "simulation stop time(seconds)", stopTime)
       
    93 
       
    94     # 
       
    95     #  The system global variables and the local values added to the argument
       
    96     #  system can be overridden by command line arguments by using this call.
       
    97     # 
       
    98     cmd.Parse(argv)
       
    99 
       
   100     #  The metadata system(off by default) is used by ascii tracing below
       
   101     ns3.Packet.EnableMetadata()
       
   102 
       
   103     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   104     #                                                                        # 
       
   105     #  Construct the backbone                                                # 
       
   106     #                                                                        # 
       
   107     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   108 
       
   109     # 
       
   110     #  Create a container to manage the nodes of the adhoc(backbone) network.
       
   111     #  Later we'll create the rest of the nodes we'll need.
       
   112     # 
       
   113     backbone = ns3.NodeContainer()
       
   114     backbone.Create(backboneNodes)
       
   115     # 
       
   116     #  Create the backbone wifi net devices and install them into the nodes in 
       
   117     #  our container
       
   118     # 
       
   119     wifi = ns3.WifiHelper()
       
   120     wifi.SetMac("ns3::AdhocWifiMac")
       
   121     wifi.SetPhy("ns3::WifiPhy")
       
   122     backboneDevices = wifi.Install(backbone)
       
   123     # 
       
   124     #  Add the IPv4 protocol stack to the nodes in our container
       
   125     # 
       
   126     internet = ns3.InternetStackHelper()
       
   127     internet.Install(backbone)
       
   128     # 
       
   129     #  Assign IPv4 addresses to the device drivers(actually to the associated
       
   130     #  IPv4 interfaces) we just created.
       
   131     # 
       
   132     ipAddrs = ns3.Ipv4AddressHelper()
       
   133     ipAddrs.SetBase(ns3.Ipv4Address("192.168.0.0"), ns3.Ipv4Mask("255.255.255.0"))
       
   134     ipAddrs.Assign(backboneDevices)
       
   135 
       
   136     # 
       
   137     #  The ad-hoc network nodes need a mobility model so we aggregate one to 
       
   138     #  each of the nodes we just finished building.  
       
   139     # 
       
   140     mobility = ns3.MobilityHelper()
       
   141     positionAlloc = ns3.ListPositionAllocator()
       
   142     x = 0.0
       
   143     for i in range(backboneNodes):
       
   144         positionAlloc.Add(ns3.Vector(x, 0.0, 0.0))
       
   145         x += 5.0
       
   146     mobility.SetPositionAllocator(positionAlloc)
       
   147     mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
       
   148                                "Bounds", ns3.RectangleValue(ns3.Rectangle(0, 1000, 0, 1000)),
       
   149                                "Speed", ns3.RandomVariableValue(ns3.ConstantVariable(2000)),
       
   150                                "Pause", ns3.RandomVariableValue(ns3.ConstantVariable(0.2)))
       
   151     mobility.Install(backbone)
       
   152 
       
   153     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   154     #                                                                        # 
       
   155     #  Construct the LANs                                                    # 
       
   156     #                                                                        # 
       
   157     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   158 
       
   159     #  Reset the address base-- all of the CSMA networks will be in
       
   160     #  the "172.16 address space
       
   161     ipAddrs.SetBase(ns3.Ipv4Address("172.16.0.0"), ns3.Ipv4Mask("255.255.255.0"))
       
   162 
       
   163     for i in range(backboneNodes):
       
   164         print "Configuring local area network for backbone node ", i
       
   165         # 
       
   166         #  Create a container to manage the nodes of the LAN.  We need
       
   167         #  two containers here; one with all of the new nodes, and one
       
   168         #  with all of the nodes including new and existing nodes
       
   169         # 
       
   170         newLanNodes = ns3.NodeContainer()
       
   171         newLanNodes.Create(lanNodes - 1)
       
   172         #  Now, create the container with all nodes on this link
       
   173         lan = ns3.NodeContainer(ns3.NodeContainer(backbone.Get(i)), newLanNodes)
       
   174         # 
       
   175         #  Create the CSMA net devices and install them into the nodes in our 
       
   176         #  collection.
       
   177         # 
       
   178         csma = ns3.CsmaHelper()
       
   179         csma.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(5000000)))
       
   180         csma.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(2)))
       
   181         lanDevices = csma.Install(lan)
       
   182         # 
       
   183         #  Add the IPv4 protocol stack to the new LAN nodes
       
   184         # 
       
   185         internet.Install(newLanNodes)
       
   186         # 
       
   187         #  Assign IPv4 addresses to the device drivers(actually to the 
       
   188         #  associated IPv4 interfaces) we just created.
       
   189         # 
       
   190         ipAddrs.Assign(lanDevices)
       
   191         # 
       
   192         #  Assign a new network prefix for the next LAN, according to the
       
   193         #  network mask initialized above
       
   194         # 
       
   195         ipAddrs.NewNetwork()
       
   196 
       
   197     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   198     #                                                                        # 
       
   199     #  Construct the mobile networks                                         # 
       
   200     #                                                                        # 
       
   201     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   202 
       
   203     #  Reset the address base-- all of the 802.11 networks will be in
       
   204     #  the "10.0" address space
       
   205     ipAddrs.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
       
   206 
       
   207     for i in range(backboneNodes):
       
   208         print "Configuring wireless network for backbone node ", i
       
   209         # 
       
   210         #  Create a container to manage the nodes of the LAN.  We need
       
   211         #  two containers here; one with all of the new nodes, and one
       
   212         #  with all of the nodes including new and existing nodes
       
   213         # 
       
   214         newInfraNodes = ns3.NodeContainer()
       
   215         newInfraNodes.Create(infraNodes - 1)
       
   216         #  Now, create the container with all nodes on this link
       
   217         infra = ns3.NodeContainer(ns3.NodeContainer(backbone.Get(i)), newInfraNodes)
       
   218         # 
       
   219         #  Create another ad hoc network and devices
       
   220         # 
       
   221         wifiInfra = ns3.WifiHelper()
       
   222         wifiInfra.SetMac("ns3::AdhocWifiMac")
       
   223         wifiInfra.SetPhy("ns3::WifiPhy")
       
   224         infraDevices = wifiInfra.Install(infra)
       
   225 
       
   226         #  Add the IPv4 protocol stack to the nodes in our container
       
   227         # 
       
   228         internet.Install(newInfraNodes)
       
   229         # 
       
   230         #  Assign IPv4 addresses to the device drivers(actually to the associated
       
   231         #  IPv4 interfaces) we just created.
       
   232         # 
       
   233         ipAddrs.Assign(infraDevices)
       
   234         # 
       
   235         #  Assign a new network prefix for each mobile network, according to 
       
   236         #  the network mask initialized above
       
   237         # 
       
   238         ipAddrs.NewNetwork()
       
   239         # 
       
   240         #  The new wireless nodes need a mobility model so we aggregate one 
       
   241         #  to each of the nodes we just finished building.
       
   242         # 
       
   243         subnetAlloc = ns3.ListPositionAllocator()
       
   244         for j in range(infra.GetN()):
       
   245             subnetAlloc.Add(ns3.Vector(0.0, j, 0.0))
       
   246 
       
   247         mobility.PushReferenceMobilityModel(backbone.Get(i))
       
   248         mobility.SetPositionAllocator(subnetAlloc)
       
   249         mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
       
   250                                   "Bounds", ns3.RectangleValue(ns3.Rectangle(-25, 25, -25, 25)),
       
   251                                   "Speed", ns3.RandomVariableValue(ns3.ConstantVariable(30)),
       
   252                                   "Pause", ns3.RandomVariableValue(ns3.ConstantVariable(0.4)))
       
   253         mobility.Install(infra)
       
   254 
       
   255     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   256     #                                                                        # 
       
   257     #  Routing configuration                                                 # 
       
   258     #                                                                        # 
       
   259     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   260 
       
   261     print "Enabling OLSR routing on all backbone nodes"
       
   262     olsr = ns3.OlsrHelper()
       
   263     olsr.Install(backbone)
       
   264 
       
   265     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   266     #                                                                        # 
       
   267     #  Application configuration                                             # 
       
   268     #                                                                        # 
       
   269     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   270 
       
   271     #  Create the OnOff application to send UDP datagrams of size
       
   272     #  210 bytes at a rate of 448 Kb/s, between two nodes
       
   273     print "Create Applications."
       
   274     port = 9   #  Discard port(RFC 863)
       
   275 
       
   276     #  Let's make sure that the user does not define too few LAN nodes
       
   277     #  to make this example work.  We need lanNodes >= 5
       
   278     assert (lanNodes >= 5)
       
   279     appSource = ns3.NodeList.GetNode(11)
       
   280     appSink = ns3.NodeList.GetNode(13)
       
   281     remoteAddr = ns3.Ipv4Address("172.16.0.5")
       
   282 
       
   283     onoff = ns3.OnOffHelper("ns3::UdpSocketFactory", 
       
   284                             ns3.Address(ns3.InetSocketAddress(remoteAddr, port)))
       
   285     onoff.SetAttribute("OnTime", ns3.RandomVariableValue(ns3.ConstantVariable(1)))
       
   286     onoff.SetAttribute("OffTime", ns3.RandomVariableValue(ns3.ConstantVariable(0)))
       
   287     apps = onoff.Install(ns3.NodeContainer(appSource))
       
   288     apps.Start(ns3.Seconds(3.0))
       
   289     apps.Stop(ns3.Seconds(20.0))
       
   290 
       
   291     #  Create a packet sink to receive these packets
       
   292     sink = ns3.PacketSinkHelper("ns3::UdpSocketFactory", 
       
   293                                 ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), port))
       
   294     apps = sink.Install(ns3.NodeContainer(appSink))
       
   295     apps.Start(ns3.Seconds(3.0))
       
   296 
       
   297     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   298     #                                                                        # 
       
   299     #  Tracing configuration                                                 # 
       
   300     #                                                                        # 
       
   301     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # / 
       
   302 
       
   303     print "Configure Tracing."
       
   304     # 
       
   305     #  Let's set up some ns-2-like ascii traces, using another helper class
       
   306     # 
       
   307     #std.ofstream ascii
       
   308     #ascii.open("mixed-wireless.tr")
       
   309     #WifiHelper.EnableAsciiAll(ascii)
       
   310     #CsmaHelper.EnableAsciiAll(ascii)
       
   311     print "(tracing not done for Python)"
       
   312     #  Look at nodes 11, 13 only
       
   313     # WifiHelper.EnableAscii(ascii, 11, 0); 
       
   314     # WifiHelper.EnableAscii(ascii, 13, 0); 
       
   315 
       
   316     #  Let's do a pcap trace on the backbone devices
       
   317     ns3.WifiHelper.EnablePcap("mixed-wireless", backboneDevices)
       
   318     #  Let's additionally trace the application Sink, ifIndex 0
       
   319     ns3.CsmaHelper.EnablePcap("mixed-wireless", appSink.GetId(), 0)
       
   320 
       
   321 #   #ifdef ENABLE_FOR_TRACING_EXAMPLE
       
   322 #     Config.Connect("/NodeList/*/$MobilityModel/CourseChange",
       
   323 #       MakeCallback(&CourseChangeCallback))
       
   324 #   #endif
       
   325 
       
   326 
       
   327     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
       
   328     #                                                                        # 
       
   329     #  Run simulation                                                        # 
       
   330     #                                                                        # 
       
   331     # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
       
   332 
       
   333     print "Run Simulation."
       
   334     ns3.Simulator.Stop(ns3.Seconds(stopTime))
       
   335     ns3.Simulator.Run()
       
   336     ns3.Simulator.Destroy()
       
   337 
       
   338 
       
   339 if __name__ == '__main__':
       
   340     import sys
       
   341     main(sys.argv)