examples/simple-alternate-routing.cc
changeset 1776 0d5be0c3d229
child 2230 9f13ac3291e0
equal deleted inserted replaced
1775:5abf6f0d1eca 1776:0d5be0c3d229
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * This program is free software; you can redistribute it and/or modify
       
     4  * it under the terms of the GNU General Public License version 2 as
       
     5  * published by the Free Software Foundation;
       
     6  *
       
     7  * This program is distributed in the hope that it will be useful,
       
     8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
     9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    10  * GNU General Public License for more details.
       
    11  *
       
    12  * You should have received a copy of the GNU General Public License
       
    13  * along with this program; if not, write to the Free Software
       
    14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    15  *
       
    16  */
       
    17 
       
    18 //
       
    19 // Network topology
       
    20 //
       
    21 //  n0
       
    22 //     \ 5 Mb/s, 2ms
       
    23 //      \          1.5Mb/s, 10ms
       
    24 //       n2 ------------------------n3
       
    25 //      /                          /
       
    26 //     / 5 Mb/s, 2ms              /
       
    27 //   n1--------------------------
       
    28 //          1.5 Mb/s, 100ms
       
    29 //
       
    30 // this is a modification of simple-global-routing to allow for
       
    31 // a single hop but higher-cost path between n1 and n3
       
    32 // 
       
    33 // - Tracing of queues and packet receptions to file "simple-rerouting.tr"
       
    34 
       
    35 #include <iostream>
       
    36 #include <fstream>
       
    37 #include <string>
       
    38 #include <cassert>
       
    39 
       
    40 #include "ns3/log.h"
       
    41 
       
    42 #include "ns3/command-line.h"
       
    43 #include "ns3/default-value.h"
       
    44 #include "ns3/ptr.h"
       
    45 #include "ns3/random-variable.h"
       
    46 
       
    47 #include "ns3/simulator.h"
       
    48 #include "ns3/nstime.h"
       
    49 #include "ns3/data-rate.h"
       
    50 
       
    51 #include "ns3/ascii-trace.h"
       
    52 #include "ns3/pcap-trace.h"
       
    53 #include "ns3/internet-node.h"
       
    54 #include "ns3/point-to-point-channel.h"
       
    55 #include "ns3/point-to-point-net-device.h"
       
    56 #include "ns3/ipv4-address.h"
       
    57 #include "ns3/ipv4.h"
       
    58 #include "ns3/socket.h"
       
    59 #include "ns3/inet-socket-address.h"
       
    60 #include "ns3/ipv4-route.h"
       
    61 #include "ns3/point-to-point-topology.h"
       
    62 #include "ns3/onoff-application.h"
       
    63 #include "ns3/packet-sink.h"
       
    64 #include "ns3/global-route-manager.h"
       
    65 
       
    66 using namespace ns3;
       
    67 
       
    68 NS_LOG_COMPONENT_DEFINE ("SimpleAlternateRoutingExample");
       
    69 
       
    70 int 
       
    71 main (int argc, char *argv[])
       
    72 {
       
    73   // Users may find it convenient to turn on explicit debugging
       
    74   // for selected modules; the below lines suggest how to do this
       
    75 #if 0 
       
    76   LogComponentEnable("GlobalRouteManager", LOG_LOGIC);
       
    77   LogComponentEnable("GlobalRouter", LOG_LOGIC);
       
    78   LogComponentEnable("Object", LOG_LEVEL_ALL);
       
    79   LogComponentEnable("Queue", LOG_LEVEL_ALL);
       
    80   LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
       
    81   LogComponentEnable("Channel", LOG_LEVEL_ALL);
       
    82   LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
       
    83   LogComponentEnable("NetDevice", LOG_LEVEL_ALL);
       
    84   LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
       
    85   LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
       
    86   LogComponentEnable("PacketSocket", LOG_LEVEL_ALL);
       
    87   LogComponentEnable("Socket", LOG_LEVEL_ALL);
       
    88   LogComponentEnable("UdpSocket", LOG_LEVEL_ALL);
       
    89   LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL);
       
    90   LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
       
    91   LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL);
       
    92   LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
       
    93   LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL);
       
    94   LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL);
       
    95   LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL);
       
    96   LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL);
       
    97   LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
       
    98   LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
       
    99 #endif
       
   100   // Set up some default values for the simulation.  Use the 
       
   101   // DefaultValue::Bind () technique to tell the system what subclass of 
       
   102   // Queue to use, and what the queue limit is
       
   103 
       
   104   // The below Bind command tells the queue factory which class to
       
   105   // instantiate, when the queue factory is invoked in the topology code
       
   106   DefaultValue::Bind ("Queue", "DropTailQueue");
       
   107 
       
   108   DefaultValue::Bind ("OnOffApplicationPacketSize", "210");
       
   109   DefaultValue::Bind ("OnOffApplicationDataRate", "300b/s");
       
   110 
       
   111   // The below metric, if set to 3 or higher, will cause packets between
       
   112   // n1 and n3 to take the 2-hop route through n2
       
   113   // 
       
   114   // Additionally, we plumb this metric into the default value / command 
       
   115   // line argument system as well, for exemplary purposes.  This means 
       
   116   // that it can be resettable at the command-line to the program, 
       
   117   // rather than recompiling
       
   118   // e.g. waf --run "simple-alternate-routing --AlternateCost=5"
       
   119   uint16_t sampleMetric = 1;
       
   120   CommandLine::AddArgValue ("AlternateCost",
       
   121     "This metric is used in the example script between n3 and n1 ", 
       
   122     sampleMetric);
       
   123 
       
   124   // Allow the user to override any of the defaults and the above
       
   125   // DefaultValue::Bind ()s at run-time, via command-line arguments
       
   126   CommandLine::Parse (argc, argv);
       
   127 
       
   128   // Here, we will explicitly create four nodes.  In more sophisticated
       
   129   // topologies, we could configure a node factory.
       
   130   NS_LOG_INFO ("Create nodes.");
       
   131   Ptr<Node> n0 = Create<InternetNode> ();
       
   132   Ptr<Node> n1 = Create<InternetNode> (); 
       
   133   Ptr<Node> n2 = Create<InternetNode> (); 
       
   134   Ptr<Node> n3 = Create<InternetNode> ();
       
   135 
       
   136   // We create the channels first without any IP addressing information
       
   137   NS_LOG_INFO ("Create channels.");
       
   138   Ptr<PointToPointChannel> channel0 = 
       
   139     PointToPointTopology::AddPointToPointLink (
       
   140       n0, n2, DataRate (5000000), MilliSeconds (2));
       
   141 
       
   142   Ptr<PointToPointChannel> channel1 = 
       
   143     PointToPointTopology::AddPointToPointLink (
       
   144       n1, n2, DataRate (5000000), MilliSeconds (2));
       
   145   
       
   146   Ptr<PointToPointChannel> channel2 = 
       
   147     PointToPointTopology::AddPointToPointLink (
       
   148       n2, n3, DataRate (1500000), MilliSeconds (10));
       
   149   
       
   150   Ptr<PointToPointChannel> channel3 = 
       
   151     PointToPointTopology::AddPointToPointLink (
       
   152       n1, n3, DataRate (1500000), MilliSeconds (100));
       
   153   
       
   154   // Later, we add IP addresses.  The middle two octets correspond to 
       
   155  // the channel number.  
       
   156   NS_LOG_INFO ("Assign IP Addresses.");
       
   157   PointToPointTopology::AddIpv4Addresses (
       
   158       channel0, n0, Ipv4Address ("10.0.0.1"),
       
   159       n2, Ipv4Address ("10.0.0.2"));
       
   160   
       
   161   PointToPointTopology::AddIpv4Addresses (
       
   162       channel1, n1, Ipv4Address ("10.1.1.1"),
       
   163       n2, Ipv4Address ("10.1.1.2"));
       
   164   
       
   165   PointToPointTopology::AddIpv4Addresses (
       
   166       channel2, n2, Ipv4Address ("10.2.2.1"),
       
   167       n3, Ipv4Address ("10.2.2.2"));
       
   168 
       
   169   PointToPointTopology::AddIpv4Addresses (
       
   170       channel3, n1, Ipv4Address ("10.3.3.1"),
       
   171       n3, Ipv4Address ("10.3.3.2"));
       
   172 
       
   173   PointToPointTopology::SetIpv4Metric (
       
   174       channel3, n1, n3, sampleMetric);
       
   175 
       
   176   // Create router nodes, initialize routing database and set up the routing
       
   177   // tables in the nodes.
       
   178   GlobalRouteManager::PopulateRoutingTables ();
       
   179 
       
   180   // Create the OnOff application to send UDP datagrams 
       
   181   NS_LOG_INFO ("Create Application.");
       
   182   uint16_t port = 9;   // Discard port (RFC 863)
       
   183 
       
   184   // Create a flow from n3 to n1, starting at time 1.1 seconds
       
   185   Ptr<OnOffApplication> ooff = Create<OnOffApplication> (
       
   186     n3, 
       
   187     InetSocketAddress ("10.1.1.1", port),
       
   188     "Udp",
       
   189     ConstantVariable (1), 
       
   190     ConstantVariable (0));
       
   191   // Start the application
       
   192   ooff->Start (Seconds (1.1));
       
   193   ooff->Stop (Seconds (10.0));
       
   194 
       
   195   // Create a packet sink to receive these packets
       
   196   Ptr<PacketSink> sink = Create<PacketSink> (
       
   197     n1, 
       
   198     InetSocketAddress (Ipv4Address::GetAny (), port), 
       
   199     "Udp");
       
   200   // Start the sink
       
   201   sink->Start (Seconds (1.1));
       
   202   sink->Stop (Seconds (10.0));
       
   203 
       
   204   // Configure tracing of all enqueue, dequeue, and NetDevice receive events
       
   205   // Trace output will be sent to the simple-alternate-routing.tr file
       
   206   NS_LOG_INFO ("Configure Tracing.");
       
   207   AsciiTrace asciitrace ("simple-alternate-routing.tr");
       
   208   asciitrace.TraceAllQueues ();
       
   209   asciitrace.TraceAllNetDeviceRx ();
       
   210 
       
   211   // Also configure some tcpdump traces; each interface will be traced
       
   212   // The output files will be named simple-p2p.pcap-<nodeId>-<interfaceId>
       
   213   // and can be read by the "tcpdump -r" command (use "-tt" option to
       
   214   // display timestamps correctly)
       
   215   PcapTrace pcaptrace ("simple-alternate-routing.pcap");
       
   216   pcaptrace.TraceAllIp ();
       
   217 
       
   218   NS_LOG_INFO ("Run Simulation.");
       
   219   Simulator::Run ();
       
   220   Simulator::Destroy ();
       
   221   NS_LOG_INFO ("Done.");
       
   222 
       
   223   return 0;
       
   224 }