A Discrete-Event Network Simulator
API
tbf-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II"
3  * 2017 Kungliga Tekniska Högskolan
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: Pasquale Imputato <p.imputato@gmail.com>
19  * Author: Stefano Avallone <stefano.avallone@unina.it>
20  * Author: Surya Seetharaman <suryaseetharaman.9@gmail.com> - ported from ns-3
21  * RedQueueDisc traffic-control example to accommodate TbfQueueDisc example.
22  */
23 
24 #include "ns3/applications-module.h"
25 #include "ns3/core-module.h"
26 #include "ns3/internet-module.h"
27 #include "ns3/network-module.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/traffic-control-module.h"
30 
31 // This simple example shows how to use TrafficControlHelper to install a
32 // QueueDisc on a device.
33 //
34 // Network topology
35 //
36 // 10.1.1.0
37 // n0 -------------- n1
38 // point-to-point
39 //
40 // The output will consist of all the traced changes in
41 // the number of tokens in TBF's first and second buckets:
42 //
43 // FirstBucketTokens 0 to x
44 // SecondBucketTokens 0 to x
45 // FirstBucketTokens x to 0
46 // SecondBucketTokens x to 0
47 //
48 
49 using namespace ns3;
50 
51 NS_LOG_COMPONENT_DEFINE("TbfExample");
52 
53 void
54 FirstBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
55 {
56  std::cout << "FirstBucketTokens " << oldValue << " to " << newValue << std::endl;
57 }
58 
59 void
60 SecondBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
61 {
62  std::cout << "SecondBucketTokens " << oldValue << " to " << newValue << std::endl;
63 }
64 
65 int
66 main(int argc, char* argv[])
67 {
68  double simulationTime = 10; // seconds
69  uint32_t burst = 10000;
70  uint32_t mtu = 0;
71  DataRate rate = DataRate("1Mbps");
72  DataRate peakRate = DataRate("0bps");
73 
74  CommandLine cmd(__FILE__);
75  cmd.AddValue("burst", "Size of first bucket in bytes", burst);
76  cmd.AddValue("mtu", "Size of second bucket in bytes", mtu);
77  cmd.AddValue("rate", "Rate of tokens arriving in first bucket", rate);
78  cmd.AddValue("peakRate", "Rate of tokens arriving in second bucket", peakRate);
79 
80  cmd.Parse(argc, argv);
81 
83  nodes.Create(2);
84 
86  pointToPoint.SetDeviceAttribute("DataRate", StringValue("2Mb/s"));
87  pointToPoint.SetChannelAttribute("Delay", StringValue("0ms"));
88 
90  devices = pointToPoint.Install(nodes);
91 
93  stack.Install(nodes);
94 
96  tch.SetRootQueueDisc("ns3::TbfQueueDisc",
97  "Burst",
98  UintegerValue(burst),
99  "Mtu",
100  UintegerValue(mtu),
101  "Rate",
102  DataRateValue(DataRate(rate)),
103  "PeakRate",
104  DataRateValue(DataRate(peakRate)));
105  QueueDiscContainer qdiscs = tch.Install(devices);
106 
107  Ptr<QueueDisc> q = qdiscs.Get(1);
110 
112  address.SetBase("10.1.1.0", "255.255.255.0");
113 
115 
116  // Flow
117  uint16_t port = 7;
119  PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
120  ApplicationContainer sinkApp = packetSinkHelper.Install(nodes.Get(0));
121 
122  sinkApp.Start(Seconds(0.0));
123  sinkApp.Stop(Seconds(simulationTime + 0.1));
124 
125  uint32_t payloadSize = 1448;
126  Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
127 
128  OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
129  onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
130  onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0.2]"));
131  onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
132  onoff.SetAttribute("DataRate", StringValue("1.1Mb/s")); // bit/s
134 
135  InetSocketAddress rmt(interfaces.GetAddress(0), port);
136  rmt.SetTos(0xb8);
137  AddressValue remoteAddress(rmt);
138  onoff.SetAttribute("Remote", remoteAddress);
139  apps.Add(onoff.Install(nodes.Get(1)));
140  apps.Start(Seconds(1.0));
141  apps.Stop(Seconds(simulationTime + 0.1));
142 
143  Simulator::Stop(Seconds(simulationTime + 5));
144  Simulator::Run();
145 
147 
148  std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
149  std::cout << q->GetStats() << std::endl;
150  return 0;
151 }
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Parse command-line arguments.
Definition: command-line.h:232
Class for representing data rates.
Definition: data-rate.h:89
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()
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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
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()
Retrieve all the collected statistics.
Definition: queue-disc.cc:412
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
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:45
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
address
Definition: first.py:47
pointToPoint
Definition: first.py:38
devices
Definition: first.py:42
stack
Definition: first.py:44
interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40
void SecondBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
Definition: tbf-example.cc:60
void FirstBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
Definition: tbf-example.cc:54