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 
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 }
holds a vector of ns3::Application pointers.
an Inet address class
static Ipv4Address GetAny(void)
void SecondBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
Definition: tbf-example.cc:61
QueueDiscContainer Install(NetDeviceContainer c)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Build a set of PointToPointNetDevice objects.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:34
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Holds a vector of ns3::QueueDisc pointers.
Class for representing data rates.
Definition: data-rate.h:88
nodes
Definition: first.py:25
Hold an unsigned integer type.
Definition: uinteger.h:44
pointToPoint
Definition: first.py:28
holds a vector of ns3::NetDevice pointers
void FirstBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
Definition: tbf-example.cc:55
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Build a set of QueueDisc objects.
const Stats & GetStats(void)
Retrieve all the collected statistics.
Definition: queue-disc.cc:421
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:37
Ptr< QueueDisc > Get(std::size_t i) const
Get the Ptr<QueueDisc> stored in this container at a given index.
uint16_t SetRootQueueDisc(std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue(), std::string n09="", const AttributeValue &v09=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue(), std::string n11="", const AttributeValue &v11=EmptyAttributeValue(), std::string n12="", const AttributeValue &v12=EmptyAttributeValue(), std::string n13="", const AttributeValue &v13=EmptyAttributeValue(), std::string n14="", const AttributeValue &v14=EmptyAttributeValue(), std::string n15="", const AttributeValue &v15=EmptyAttributeValue())
Helper function used to set a root queue disc of the given type and with the given attributes...
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
interfaces
Definition: first.py:41
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
devices
Definition: first.py:32