1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 NITK Surathkal
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Mohit P. Tahiliani <tahiliani@nitk.edu.in>
19  *
20  */
21 
22 #include "ns3/core-module.h"
23 #include "ns3/network-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/point-to-point-module.h"
26 #include "ns3/applications-module.h"
27 #include "ns3/point-to-point-layout-module.h"
28 #include "ns3/traffic-control-module.h"
29 
30 #include <iostream>
31 #include <iomanip>
32 #include <map>
33 
34 using namespace ns3;
35 
main(int argc,char * argv[])36 int main (int argc, char *argv[])
37 {
38   uint32_t    nLeaf = 10;
39   uint32_t    maxPackets = 100;
40   bool        modeBytes  = false;
41   uint32_t    queueDiscLimitPackets = 1000;
42   double      minTh = 5;
43   double      maxTh = 15;
44   uint32_t    pktSize = 512;
45   std::string appDataRate = "10Mbps";
46   std::string queueDiscType = "RED";
47   uint16_t port = 5001;
48   std::string bottleNeckLinkBw = "1Mbps";
49   std::string bottleNeckLinkDelay = "50ms";
50 
51   CommandLine cmd (__FILE__);
52   cmd.AddValue ("nLeaf",     "Number of left and right side leaf nodes", nLeaf);
53   cmd.AddValue ("maxPackets","Max Packets allowed in the device queue", maxPackets);
54   cmd.AddValue ("queueDiscLimitPackets","Max Packets allowed in the queue disc", queueDiscLimitPackets);
55   cmd.AddValue ("queueDiscType", "Set Queue disc type to RED or ARED", queueDiscType);
56   cmd.AddValue ("appPktSize", "Set OnOff App Packet Size", pktSize);
57   cmd.AddValue ("appDataRate", "Set OnOff App DataRate", appDataRate);
58   cmd.AddValue ("modeBytes", "Set Queue disc mode to Packets (false) or bytes (true)", modeBytes);
59 
60   cmd.AddValue ("redMinTh", "RED queue minimum threshold", minTh);
61   cmd.AddValue ("redMaxTh", "RED queue maximum threshold", maxTh);
62   cmd.Parse (argc,argv);
63 
64   if ((queueDiscType != "RED") && (queueDiscType != "ARED"))
65     {
66       std::cout << "Invalid queue disc type: Use --queueDiscType=RED or --queueDiscType=ARED" << std::endl;
67       exit (1);
68     }
69 
70   Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (pktSize));
71   Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (appDataRate));
72 
73   Config::SetDefault ("ns3::DropTailQueue<Packet>::MaxSize",
74                       StringValue (std::to_string (maxPackets) + "p"));
75 
76   if (!modeBytes)
77     {
78       Config::SetDefault ("ns3::RedQueueDisc::MaxSize",
79                           QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, queueDiscLimitPackets)));
80     }
81   else
82     {
83       Config::SetDefault ("ns3::RedQueueDisc::MaxSize",
84                           QueueSizeValue (QueueSize (QueueSizeUnit::BYTES, queueDiscLimitPackets * pktSize)));
85       minTh *= pktSize;
86       maxTh *= pktSize;
87     }
88 
89   Config::SetDefault ("ns3::RedQueueDisc::MinTh", DoubleValue (minTh));
90   Config::SetDefault ("ns3::RedQueueDisc::MaxTh", DoubleValue (maxTh));
91   Config::SetDefault ("ns3::RedQueueDisc::LinkBandwidth", StringValue (bottleNeckLinkBw));
92   Config::SetDefault ("ns3::RedQueueDisc::LinkDelay", StringValue (bottleNeckLinkDelay));
93   Config::SetDefault ("ns3::RedQueueDisc::MeanPktSize", UintegerValue (pktSize));
94 
95   if (queueDiscType == "ARED")
96     {
97       // Turn on ARED
98       Config::SetDefault ("ns3::RedQueueDisc::ARED", BooleanValue (true));
99       Config::SetDefault ("ns3::RedQueueDisc::LInterm", DoubleValue (10.0));
100     }
101 
102   // Create the point-to-point link helpers
103   PointToPointHelper bottleNeckLink;
104   bottleNeckLink.SetDeviceAttribute  ("DataRate", StringValue (bottleNeckLinkBw));
105   bottleNeckLink.SetChannelAttribute ("Delay", StringValue (bottleNeckLinkDelay));
106 
107   PointToPointHelper pointToPointLeaf;
108   pointToPointLeaf.SetDeviceAttribute    ("DataRate", StringValue ("10Mbps"));
109   pointToPointLeaf.SetChannelAttribute   ("Delay", StringValue ("1ms"));
110 
111   PointToPointDumbbellHelper d (nLeaf, pointToPointLeaf,
112                                 nLeaf, pointToPointLeaf,
113                                 bottleNeckLink);
114 
115   // Install Stack
116   InternetStackHelper stack;
117   for (uint32_t i = 0; i < d.LeftCount (); ++i)
118     {
119       stack.Install (d.GetLeft (i));
120     }
121   for (uint32_t i = 0; i < d.RightCount (); ++i)
122     {
123       stack.Install (d.GetRight (i));
124     }
125 
126   stack.Install (d.GetLeft ());
127   stack.Install (d.GetRight ());
128   TrafficControlHelper tchBottleneck;
129   QueueDiscContainer queueDiscs;
130   tchBottleneck.SetRootQueueDisc ("ns3::RedQueueDisc");
131   tchBottleneck.Install (d.GetLeft ()->GetDevice (0));
132   queueDiscs = tchBottleneck.Install (d.GetRight ()->GetDevice (0));
133 
134   // Assign IP Addresses
135   d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
136                          Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
137                          Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
138 
139   // Install on/off app on all right side nodes
140   OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
141   clientHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
142   clientHelper.SetAttribute ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
143   Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
144   PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
145   ApplicationContainer sinkApps;
146   for (uint32_t i = 0; i < d.LeftCount (); ++i)
147     {
148       sinkApps.Add (packetSinkHelper.Install (d.GetLeft (i)));
149     }
150   sinkApps.Start (Seconds (0.0));
151   sinkApps.Stop (Seconds (30.0));
152 
153   ApplicationContainer clientApps;
154   for (uint32_t i = 0; i < d.RightCount (); ++i)
155     {
156       // Create an on/off app sending packets to the left side
157       AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), port));
158       clientHelper.SetAttribute ("Remote", remoteAddress);
159       clientApps.Add (clientHelper.Install (d.GetRight (i)));
160     }
161   clientApps.Start (Seconds (1.0)); // Start 1 second after sink
162   clientApps.Stop (Seconds (15.0)); // Stop before the sink
163 
164   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
165 
166   std::cout << "Running the simulation" << std::endl;
167   Simulator::Run ();
168 
169   QueueDisc::Stats st = queueDiscs.Get (0)->GetStats ();
170 
171   if (st.GetNDroppedPackets (RedQueueDisc::UNFORCED_DROP) == 0)
172     {
173       std::cout << "There should be some unforced drops" << std::endl;
174       exit (1);
175     }
176 
177   if (st.GetNDroppedPackets (QueueDisc::INTERNAL_QUEUE_DROP) != 0)
178     {
179       std::cout << "There should be zero drops due to queue full" << std::endl;
180       exit (1);
181     }
182 
183   std::cout << "*** Stats from the bottleneck queue disc ***" << std::endl;
184   std::cout << st << std::endl;
185   std::cout << "Destroying the simulation" << std::endl;
186 
187   Simulator::Destroy ();
188   return 0;
189 }
190