examples/csma-one-subnet.cc
changeset 2801 edbd094336b5
parent 2602 d9262bff6df2
child 2838 d120c4ca2d24
equal deleted inserted replaced
2800:b3e87b8fa9d2 2801:edbd094336b5
    23 //
    23 //
    24 // - CBR/UDP flows from n0 to n1 and from n3 to n0
    24 // - CBR/UDP flows from n0 to n1 and from n3 to n0
    25 // - DropTail queues 
    25 // - DropTail queues 
    26 // - Tracing of queues and packet receptions to file "csma-one-subnet.tr"
    26 // - Tracing of queues and packet receptions to file "csma-one-subnet.tr"
    27 
    27 
    28 #include "ns3/command-line.h"
    28 #include <iostream>
    29 #include "ns3/ptr.h"
    29 #include <fstream>
    30 #include "ns3/random-variable.h"
    30 
    31 #include "ns3/log.h"
    31 #include "ns3/simulator-module.h"
    32 #include "ns3/simulator.h"
    32 #include "ns3/core-module.h"
    33 #include "ns3/nstime.h"
    33 #include "ns3/helper-module.h"
    34 #include "ns3/data-rate.h"
       
    35 #include "ns3/ascii-trace.h"
       
    36 #include "ns3/pcap-trace.h"
       
    37 #include "ns3/internet-node.h"
       
    38 #include "ns3/csma-channel.h"
       
    39 #include "ns3/csma-net-device.h"
       
    40 #include "ns3/csma-topology.h"
       
    41 #include "ns3/csma-ipv4-topology.h"
       
    42 #include "ns3/mac48-address.h"
       
    43 #include "ns3/ipv4-address.h"
       
    44 #include "ns3/inet-socket-address.h"
       
    45 #include "ns3/ipv4.h"
       
    46 #include "ns3/socket.h"
       
    47 #include "ns3/ipv4-route.h"
       
    48 #include "ns3/onoff-application.h"
       
    49 
    34 
    50 using namespace ns3;
    35 using namespace ns3;
    51 
    36 
    52 NS_LOG_COMPONENT_DEFINE ("CsmaOneSubnetExample");
    37 NS_LOG_COMPONENT_DEFINE ("CsmaOneSubnetExample");
    53 
    38 
    58 // Users may find it convenient to turn on explicit debugging
    43 // Users may find it convenient to turn on explicit debugging
    59 // for selected modules; the below lines suggest how to do this
    44 // for selected modules; the below lines suggest how to do this
    60 //
    45 //
    61 #if 0 
    46 #if 0 
    62   LogComponentEnable ("CsmaOneSubnetExample", LOG_LEVEL_INFO);
    47   LogComponentEnable ("CsmaOneSubnetExample", LOG_LEVEL_INFO);
    63 
       
    64   LogComponentEnable("Object", LOG_LEVEL_ALL);
       
    65   LogComponentEnable("Queue", LOG_LEVEL_ALL);
       
    66   LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
       
    67   LogComponentEnable("Channel", LOG_LEVEL_ALL);
       
    68   LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
       
    69   LogComponentEnable("NetDevice", LOG_LEVEL_ALL);
       
    70   LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
       
    71   LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
       
    72   LogComponentEnable("PacketSocket", LOG_LEVEL_ALL);
       
    73   LogComponentEnable("Socket", LOG_LEVEL_ALL);
       
    74   LogComponentEnable("UdpSocket", LOG_LEVEL_ALL);
       
    75   LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL);
       
    76   LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
       
    77   LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL);
       
    78   LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
       
    79   LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL);
       
    80   LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL);
       
    81   LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL);
       
    82   LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL);
       
    83   LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
       
    84   LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
       
    85 #endif
    48 #endif
    86 //
    49 //
    87 //
    50 //
    88 // Allow the user to override any of the defaults and the above Bind() at
    51 // Allow the user to override any of the defaults and the above Bind() at
    89 // run-time, via command-line arguments
    52 // run-time, via command-line arguments
    92   cmd.Parse (argc, argv);
    55   cmd.Parse (argc, argv);
    93 //
    56 //
    94 // Explicitly create the nodes required by the topology (shown above).
    57 // Explicitly create the nodes required by the topology (shown above).
    95 //
    58 //
    96   NS_LOG_INFO ("Create nodes.");
    59   NS_LOG_INFO ("Create nodes.");
    97   Ptr<Node> n0 = CreateObject<InternetNode> ();
    60   NodeContainer c;
    98   Ptr<Node> n1 = CreateObject<InternetNode> (); 
    61   c.Create (4);
    99   Ptr<Node> n2 = CreateObject<InternetNode> (); 
       
   100   Ptr<Node> n3 = CreateObject<InternetNode> ();
       
   101 
    62 
   102   NS_LOG_INFO ("Create channels.");
    63   NS_LOG_INFO ("Build Topology");
   103 //
    64   CsmaHelper csma;
   104 // Explicitly create the channels required by the topology (shown above).
    65   csma.SetChannelParameter ("BitRate", DataRate (5000000));
   105 //
    66   csma.SetChannelParameter ("Delay", MilliSeconds (2));
   106   Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel(
       
   107     DataRate(5000000), MilliSeconds(2));
       
   108 
       
   109   NS_LOG_INFO ("Build Topology.");
       
   110 //
    67 //
   111 // Now fill out the topology by creating the net devices required to connect
    68 // Now fill out the topology by creating the net devices required to connect
   112 // the nodes to the channels and hooking them up.  AddIpv4CsmaNetDevice will
    69 // the nodes to the channels and hooking them up.  AddIpv4CsmaNetDevice will
   113 // create a net device, add a MAC address (in memory of the pink flamingo) and
    70 // create a net device, add a MAC address (in memory of the pink flamingo) and
   114 // connect the net device to a nodes and also to a channel. the 
    71 // connect the net device to a nodes and also to a channel. the 
   115 // AddIpv4CsmaNetDevice method returns a net device index for the net device
    72 // AddIpv4CsmaNetDevice method returns a net device index for the net device
   116 // created on the node.  Interpret nd0 as the net device we created for node
    73 // created on the node.  Interpret nd0 as the net device we created for node
   117 // zero.
    74 // zero.
   118 //
    75 //
   119   uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
    76   NetDeviceContainer nd0 = csma.Build (c);
   120     Mac48Address("08:00:2e:00:00:00"));
       
   121 
    77 
   122   uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
    78   InternetStackHelper internet;
   123     Mac48Address("08:00:2e:00:00:01"));
    79   internet.Build (c);
   124 
    80 
   125   uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
       
   126     Mac48Address("08:00:2e:00:00:02"));
       
   127 
       
   128   uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
       
   129     Mac48Address("08:00:2e:00:00:03"));
       
   130 //
       
   131 // We've got the "hardware" in place.  Now we need to add IP addresses.
    81 // We've got the "hardware" in place.  Now we need to add IP addresses.
   132 //
    82 //
   133   NS_LOG_INFO ("Assign IP Addresses.");
    83   NS_LOG_INFO ("Assign IP Addresses.");
   134 //
    84   Ipv4AddressHelper ipv4;
   135 // XXX BUGBUG
    85   ipv4.SetBase ("10.1.1.0", "255.255.255.0");
   136 // Need a better way to get the interface index.  The point-to-point topology
    86   ipv4.Allocate (nd0);
   137 // as implemented can't return the index since it creates interfaces on both
       
   138 // sides (i.e., it does AddIpv4Addresses, not AddIpv4Address).  We need a
       
   139 // method on Ipv4 to find the interface index corresponding to a given ipv4 
       
   140 // address.
       
   141 //
       
   142 // Assign IP addresses to the net devices and associated interfaces
       
   143 // on the lan.  The AddIpv4Address method returns an Ipv4 interface index
       
   144 // which we do not need here.
       
   145 //
       
   146   CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address("10.1.1.1"), 
       
   147     Ipv4Mask("255.255.255.0"));
       
   148 
    87 
   149   CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address("10.1.1.2"), 
       
   150     Ipv4Mask("255.255.255.0"));
       
   151 
       
   152   CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address("10.1.1.3"), 
       
   153     Ipv4Mask("255.255.255.0"));
       
   154   
       
   155   CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address("10.1.1.4"), 
       
   156     Ipv4Mask("255.255.255.0"));
       
   157 //
    88 //
   158 // Create an OnOff application to send UDP datagrams from node zero to node 1.
    89 // Create an OnOff application to send UDP datagrams from node zero to node 1.
   159 //
    90 //
   160   NS_LOG_INFO ("Create Applications.");
    91   NS_LOG_INFO ("Create Applications.");
   161   uint16_t port = 9;   // Discard port (RFC 863)
    92   uint16_t port = 9;   // Discard port (RFC 863)
   162   Ptr<OnOffApplication> ooff = 
       
   163     CreateObject<OnOffApplication> ("Remote", Address (InetSocketAddress ("10.1.1.2", port)), 
       
   164                                     "Protocol", TypeId::LookupByName ("ns3::Udp"),
       
   165                                     "OnTime", ConstantVariable(1), 
       
   166                                     "OffTime", ConstantVariable(0));
       
   167   n0->AddApplication (ooff);
       
   168 
    93 
   169 //
    94   OnOffHelper onoff;
   170 // Tell the application when to start and stop.
    95   onoff.SetUdpRemote (Ipv4Address ("10.1.1.2"), port);
   171 //
    96   onoff.SetAppAttribute ("OnTime", ConstantVariable (1));
   172   ooff->Start(Seconds(1.0));
    97   onoff.SetAppAttribute ("OffTime", ConstantVariable (0));
   173   ooff->Stop (Seconds(10.0));
    98 
       
    99   ApplicationContainer app = onoff.Build (c.Get (0));
       
   100   // Start the application
       
   101   app.Start (Seconds (1.0));
       
   102   app.Stop (Seconds (10.0));
       
   103 
       
   104   // Create an optional packet sink to receive these packets
       
   105   PacketSinkHelper sink;
       
   106   sink.SetupUdp (Ipv4Address::GetAny (), port);
       
   107   sink.Build (c.Get (1));
       
   108 
   174 // 
   109 // 
   175 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
   110 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
   176 //
   111 //
   177   ooff = CreateObject<OnOffApplication> ("Remote", Address (InetSocketAddress ("10.1.1.1", port)), 
   112   onoff.SetUdpRemote (Ipv4Address("10.1.1.1"), port);
   178                                          "Protocol", TypeId::LookupByName ("ns3::Udp"),
   113   ApplicationContainer app2 = onoff.Build (c.Get (3));
   179                                          "OnTime", ConstantVariable(1), 
       
   180                                          "OffTime", ConstantVariable(0));
       
   181   n3->AddApplication (ooff);
       
   182 
   114 
   183   ooff->Start(Seconds(1.1));
   115   sink.Build (c.Get (0));
   184   ooff->Stop (Seconds(10.0));
   116 
       
   117   app2.Start(Seconds (1.1));
       
   118   app2.Stop (Seconds (10.0));
   185 //
   119 //
   186 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
   120 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
   187 // Trace output will be sent to the file "csma-one-subnet.tr"
   121 // Trace output will be sent to the file "csma-one-subnet.tr"
   188 //
   122 //
   189    NS_LOG_INFO ("Configure Tracing.");
   123   NS_LOG_INFO ("Configure Tracing.");
   190   AsciiTrace asciitrace ("csma-one-subnet.tr");
   124   std::ofstream ascii;
   191   asciitrace.TraceAllNetDeviceRx ();
   125   ascii.open ("csma-one-subnet.tr");
   192   asciitrace.TraceAllQueues ();
   126   CsmaHelper::EnableAscii (ascii);
   193 //
   127 //
   194 // Also configure some tcpdump traces; each interface will be traced.
   128 // Also configure some tcpdump traces; each interface will be traced.
   195 // The output files will be named:
   129 // The output files will be named:
   196 //     csma-one-subnet.pcap-<nodeId>-<interfaceId>
   130 //     csma-one-subnet.pcap-<nodeId>-<interfaceId>
   197 // and can be read by the "tcpdump -r" command (use "-tt" option to
   131 // and can be read by the "tcpdump -r" command (use "-tt" option to
   198 // display timestamps correctly)
   132 // display timestamps correctly)
   199 //
   133 //
   200   PcapTrace pcaptrace ("csma-one-subnet.pcap");
   134   CsmaHelper::EnablePcap ("csma-one-subnet.pcap");
   201   pcaptrace.TraceAllIp ();
       
   202 //
   135 //
   203 // Now, do the actual simulation.
   136 // Now, do the actual simulation.
   204 //
   137 //
   205   NS_LOG_INFO ("Run Simulation.");
   138   NS_LOG_INFO ("Run Simulation.");
   206   Simulator::Run ();
   139   Simulator::Run ();