A Discrete-Event Network Simulator
API
tbf-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II"
4  * 2017 Kungliga Tekniska Högskolan
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Pasquale Imputato <p.imputato@gmail.com>
20  * Author: Stefano Avallone <stefano.avallone@unina.it>
21  * Author: Surya Seetharaman <suryaseetharaman.9@gmail.com> - ported from ns-3
22  * RedQueueDisc traffic-control example to accommodate TbfQueueDisc example.
23  */
24 
25 #include "ns3/core-module.h"
26 #include "ns3/network-module.h"
27 #include "ns3/internet-module.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/applications-module.h"
30 #include "ns3/traffic-control-module.h"
31 
32 // This simple example shows how to use TrafficControlHelper to install a
33 // QueueDisc on a device.
34 //
35 // Network topology
36 //
37 // 10.1.1.0
38 // n0 -------------- n1
39 // point-to-point
40 //
41 // The output will consist of all the traced changes in
42 // the number of tokens in TBF's first and second buckets:
43 //
44 // FirstBucketTokens 0 to x
45 // SecondBucketTokens 0 to x
46 // FirstBucketTokens x to 0
47 // SecondBucketTokens x to 0
48 //
49 
50 using namespace ns3;
51 
52 NS_LOG_COMPONENT_DEFINE ("TbfExample");
53 
54 void
55 FirstBucketTokensTrace (uint32_t oldValue, uint32_t newValue)
56 {
57  std::cout << "FirstBucketTokens " << oldValue << " to " << newValue << std::endl;
58 }
59 
60 void
61 SecondBucketTokensTrace (uint32_t oldValue, uint32_t newValue)
62 {
63  std::cout << "SecondBucketTokens " << oldValue << " to " << newValue << std::endl;
64 }
65 
66 int
67 main (int argc, char *argv[])
68 {
69 
70  double simulationTime = 10; //seconds
71  uint32_t burst = 10000;
72  uint32_t mtu = 0;
73  DataRate rate = DataRate ("1Mbps");
74  DataRate peakRate = DataRate ("0bps");
75 
76  CommandLine cmd (__FILE__);
77  cmd.AddValue ("burst", "Size of first bucket in bytes", burst);
78  cmd.AddValue ("mtu", "Size of second bucket in bytes", mtu);
79  cmd.AddValue ("rate", "Rate of tokens arriving in first bucket", rate);
80  cmd.AddValue ("peakRate", "Rate of tokens arriving in second bucket", peakRate);
81 
82  cmd.Parse (argc, argv);
83 
85  nodes.Create (2);
86 
88  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("2Mb/s"));
89  pointToPoint.SetChannelAttribute ("Delay", StringValue ("0ms"));
90 
92  devices = pointToPoint.Install (nodes);
93 
95  stack.Install (nodes);
96 
98  tch.SetRootQueueDisc ("ns3::TbfQueueDisc",
99  "Burst", UintegerValue (burst),
100  "Mtu", UintegerValue (mtu),
101  "Rate", DataRateValue (DataRate (rate)),
102  "PeakRate", DataRateValue (DataRate (peakRate)));
103  QueueDiscContainer qdiscs = tch.Install (devices);
104 
105  Ptr<QueueDisc> q = qdiscs.Get (1);
106  q->TraceConnectWithoutContext ("TokensInFirstBucket", MakeCallback (&FirstBucketTokensTrace));
107  q->TraceConnectWithoutContext ("TokensInSecondBucket", MakeCallback (&SecondBucketTokensTrace));
108 
110  address.SetBase ("10.1.1.0", "255.255.255.0");
111 
113 
114  //Flow
115  uint16_t port = 7;
116  Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
117  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
118  ApplicationContainer sinkApp = packetSinkHelper.Install (nodes.Get (0));
119 
120  sinkApp.Start (Seconds (0.0));
121  sinkApp.Stop (Seconds (simulationTime + 0.1));
122 
123  uint32_t payloadSize = 1448;
124  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
125 
126  OnOffHelper onoff ("ns3::TcpSocketFactory", Ipv4Address::GetAny ());
127  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
128  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.2]"));
129  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
130  onoff.SetAttribute ("DataRate", StringValue ("1.1Mb/s")); //bit/s
132 
133  InetSocketAddress rmt (interfaces.GetAddress (0), port);
134  rmt.SetTos (0xb8);
135  AddressValue remoteAddress (rmt);
136  onoff.SetAttribute ("Remote", remoteAddress);
137  apps.Add (onoff.Install (nodes.Get (1)));
138  apps.Start (Seconds (1.0));
139  apps.Stop (Seconds (simulationTime + 0.1));
140 
141  Simulator::Stop (Seconds (simulationTime + 5));
142  Simulator::Run ();
143 
145 
146  std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
147  std::cout << q->GetStats () << std::endl;
148  return 0;
149 }
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
Definition: address.h:278
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:229
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:364
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Holds a vector of ns3::QueueDisc pointers.
Ptr< QueueDisc > Get(std::size_t i) const
Get the Ptr<QueueDisc> stored in this container at a given index.
const Stats & GetStats(void)
Retrieve all the collected statistics.
Definition: queue-disc.cc:419
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
Build a set of QueueDisc objects.
QueueDiscContainer Install(NetDeviceContainer c)
uint16_t SetRootQueueDisc(const std::string &type, Args &&... args)
Helper function used to set a root queue disc of the given type and with the given attributes.
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:329
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
pointToPoint
Definition: first.py:35
devices
Definition: first.py:39
stack
Definition: first.py:41
nodes
Definition: first.py:32
interfaces
Definition: first.py:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
void SecondBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
Definition: tbf-example.cc:61
void FirstBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
Definition: tbf-example.cc:55