A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Pasquale Imputato <p.imputato@gmail.com>
8 * Author: Stefano Avallone <stefano.avallone@unina.it>
9 * Author: Surya Seetharaman <suryaseetharaman.9@gmail.com> - ported from ns-3
10 * RedQueueDisc traffic-control example to accommodate TbfQueueDisc example.
11 */
12
13#include "ns3/applications-module.h"
14#include "ns3/core-module.h"
15#include "ns3/internet-module.h"
16#include "ns3/network-module.h"
17#include "ns3/point-to-point-module.h"
18#include "ns3/traffic-control-module.h"
19
20// This simple example shows how to use TrafficControlHelper to install a
21// QueueDisc on a device.
22//
23// Network topology
24//
25// 10.1.1.0
26// n0 -------------- n1
27// point-to-point
28//
29// The output will consist of all the traced changes in
30// the number of tokens in TBF's first and second buckets:
31//
32// FirstBucketTokens 0 to x
33// SecondBucketTokens 0 to x
34// FirstBucketTokens x to 0
35// SecondBucketTokens x to 0
36//
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("TbfExample");
41
42void
44{
45 std::cout << "FirstBucketTokens " << oldValue << " to " << newValue << std::endl;
46}
47
48void
50{
51 std::cout << "SecondBucketTokens " << oldValue << " to " << newValue << std::endl;
52}
53
54int
55main(int argc, char* argv[])
56{
57 double simulationTime = 10; // seconds
58 uint32_t burst = 10000;
59 uint32_t mtu = 0;
60 DataRate rate = DataRate("1Mbps");
61 DataRate peakRate = DataRate("0bps");
62
63 CommandLine cmd(__FILE__);
64 cmd.AddValue("burst", "Size of first bucket in bytes", burst);
65 cmd.AddValue("mtu", "Size of second bucket in bytes", mtu);
66 cmd.AddValue("rate", "Rate of tokens arriving in first bucket", rate);
67 cmd.AddValue("peakRate", "Rate of tokens arriving in second bucket", peakRate);
68
69 cmd.Parse(argc, argv);
70
72 nodes.Create(2);
73
75 pointToPoint.SetDeviceAttribute("DataRate", StringValue("2Mb/s"));
76 pointToPoint.SetChannelAttribute("Delay", StringValue("0ms"));
77
79 devices = pointToPoint.Install(nodes);
80
82 stack.Install(nodes);
83
85 tch.SetRootQueueDisc("ns3::TbfQueueDisc",
86 "Burst",
87 UintegerValue(burst),
88 "Mtu",
89 UintegerValue(mtu),
90 "Rate",
92 "PeakRate",
93 DataRateValue(DataRate(peakRate)));
94 QueueDiscContainer qdiscs = tch.Install(devices);
95
96 Ptr<QueueDisc> q = qdiscs.Get(1);
97 q->TraceConnectWithoutContext("TokensInFirstBucket", MakeCallback(&FirstBucketTokensTrace));
98 q->TraceConnectWithoutContext("TokensInSecondBucket", MakeCallback(&SecondBucketTokensTrace));
99
101 address.SetBase("10.1.1.0", "255.255.255.0");
102
103 Ipv4InterfaceContainer interfaces = address.Assign(devices);
104
105 // Flow
106 uint16_t port = 7;
108 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
109 ApplicationContainer sinkApp = packetSinkHelper.Install(nodes.Get(0));
110
111 sinkApp.Start(Seconds(0.0));
112 sinkApp.Stop(Seconds(simulationTime + 0.1));
113
114 uint32_t payloadSize = 1448;
115 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
116
117 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
118 onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
119 onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0.2]"));
120 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
121 onoff.SetAttribute("DataRate", StringValue("1.1Mb/s")); // bit/s
123
124 InetSocketAddress rmt(interfaces.GetAddress(0), port);
125 onoff.SetAttribute("Remote", AddressValue(rmt));
126 onoff.SetAttribute("Tos", UintegerValue(0xb8));
127
128 apps.Add(onoff.Install(nodes.Get(1)));
129 apps.Start(Seconds(1.0));
130 apps.Stop(Seconds(simulationTime + 0.1));
131
132 Simulator::Stop(Seconds(simulationTime + 5));
134
136
137 std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
138 std::cout << q->GetStats() << std::endl;
139 return 0;
140}
a polymophic address class
Definition address.h:90
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.
Class for representing data rates.
Definition data-rate.h:78
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
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:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
address
Definition first.py:36
pointToPoint
Definition first.py:27
devices
Definition first.py:31
stack
Definition first.py:33
interfaces
Definition first.py:39
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:684
void SecondBucketTokensTrace(uint32_t oldValue, uint32_t newValue)
void FirstBucketTokensTrace(uint32_t oldValue, uint32_t newValue)