A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
traffic-control-layer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Natale Patriciello <natale.patriciello@gmail.com>
3 * 2016 Stefano Avallone <stavallo@unina.it>
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#ifndef TRAFFICCONTROLLAYER_H
19#define TRAFFICCONTROLLAYER_H
20
21#include "ns3/address.h"
22#include "ns3/net-device.h"
23#include "ns3/node.h"
24#include "ns3/object.h"
25#include "ns3/queue-item.h"
26#include "ns3/traced-callback.h"
27
28#include <map>
29#include <vector>
30
31namespace ns3
32{
33
34class Packet;
35class QueueDisc;
36class NetDeviceQueueInterface;
37
38/**
39 * \defgroup traffic-control Traffic Control model
40 */
41
42/**
43 * The Traffic Control layer aims at introducing an equivalent of the Linux Traffic
44 * Control infrastructure into ns-3. The Traffic Control layer sits in between
45 * the NetDevices (L2) and any network protocol (e.g., IP). It is in charge of
46 * processing packets and performing actions on them: scheduling, dropping,
47 * marking, policing, etc.
48 *
49 * \ingroup traffic-control
50 *
51 * \brief Traffic control layer class
52 *
53 * This object represents the main interface of the Traffic Control Module.
54 * Basically, we manage both IN and OUT directions (sometimes called RX and TX,
55 * respectively). The OUT direction is easy to follow, since it involves
56 * direct calls: upper layer (e.g. IP) calls the Send method on an instance of
57 * this class, which then calls the Enqueue method of the QueueDisc associated
58 * with the device. The Dequeue method of the QueueDisc finally calls the Send
59 * method of the NetDevice.
60 *
61 * The IN direction uses a little trick to reduce dependencies between modules.
62 * In simple words, we use Callbacks to connect upper layer (which should register
63 * their Receive callback through RegisterProtocolHandler) and NetDevices.
64 *
65 * An example of the IN connection between this layer and IP layer is the following:
66 *\verbatim
67 Ptr<TrafficControlLayer> tc = m_node->GetObject<TrafficControlLayer> ();
68
69 NS_ASSERT (tc != 0);
70
71 m_node->RegisterProtocolHandler (MakeCallback (&TrafficControlLayer::Receive, tc),
72 Ipv4L3Protocol::PROT_NUMBER, device);
73 m_node->RegisterProtocolHandler (MakeCallback (&TrafficControlLayer::Receive, tc),
74 ArpL3Protocol::PROT_NUMBER, device);
75
76 tc->RegisterProtocolHandler (MakeCallback (&Ipv4L3Protocol::Receive, this),
77 Ipv4L3Protocol::PROT_NUMBER, device);
78 tc->RegisterProtocolHandler (MakeCallback (&ArpL3Protocol::Receive, PeekPointer
79 (GetObject<ArpL3Protocol> ())), ArpL3Protocol::PROT_NUMBER, device); \endverbatim
80 * On the node, for IPv4 and ARP packet, is registered the
81 * TrafficControlLayer::Receive callback. At the same time, on the TrafficControlLayer
82 * object, is registered the callbacks associated to the upper layers (IPv4 or ARP).
83 *
84 * When the node receives an IPv4 or ARP packet, it calls the Receive method
85 * on TrafficControlLayer, that calls the right upper-layer callback once it
86 * finishes the operations on the packet received.
87 *
88 * Discrimination through callbacks (in other words: what is the right upper-layer
89 * callback for this packet?) is done through checks over the device and the
90 * protocol number.
91 */
93{
94 public:
95 /**
96 * \brief Get the type ID.
97 * \return the object TypeId
98 */
99 static TypeId GetTypeId();
100
101 /**
102 * \brief Get the type ID for the instance
103 * \return the instance TypeId
104 */
105 TypeId GetInstanceTypeId() const override;
106
107 /**
108 * \brief Constructor
109 */
111
112 ~TrafficControlLayer() override;
113
114 // Delete copy constructor and assignment operator to avoid misuse
117
118 /**
119 * \brief Register an IN handler
120 *
121 * The handler will be invoked when a packet is received to pass it to
122 * upper layers.
123 *
124 * \param handler the handler to register
125 * \param protocolType the type of protocol this handler is
126 * interested in. This protocol type is a so-called
127 * EtherType, as registered here:
128 * http://standards.ieee.org/regauth/ethertype/eth.txt
129 * the value zero is interpreted as matching all
130 * protocols.
131 * \param device the device attached to this handler. If the
132 * value is zero, the handler is attached to all
133 * devices.
134 */
136 uint16_t protocolType,
137 Ptr<NetDevice> device);
138
139 /// Typedef for queue disc vector
140 typedef std::vector<Ptr<QueueDisc>> QueueDiscVector;
141
142 /**
143 * \brief Collect information needed to determine how to handle packets
144 * destined to each of the NetDevices of this node
145 *
146 * Checks whether a NetDeviceQueueInterface objects is aggregated to each of
147 * the NetDevices of this node and sets the required callbacks properly.
148 */
149 virtual void ScanDevices();
150
151 /**
152 * \brief This method can be used to set the root queue disc installed on a device
153 *
154 * \param device the device on which the provided queue disc will be installed
155 * \param qDisc the queue disc to be installed as root queue disc on device
156 */
157 virtual void SetRootQueueDiscOnDevice(Ptr<NetDevice> device, Ptr<QueueDisc> qDisc);
158
159 /**
160 * \brief This method can be used to get the root queue disc installed on a device
161 *
162 * \param device the device on which the requested root queue disc is installed
163 * \return the root queue disc installed on the given device
164 */
166
167 /**
168 * \brief This method can be used to remove the root queue disc (and associated
169 * filters, classes and queues) installed on a device
170 *
171 * \param device the device on which the installed queue disc will be deleted
172 */
173 virtual void DeleteRootQueueDiscOnDevice(Ptr<NetDevice> device);
174
175 /**
176 * \brief Set node associated with this stack.
177 * \param node node to set
178 */
179 void SetNode(Ptr<Node> node);
180
181 /**
182 * \brief Called by NetDevices, incoming packet
183 *
184 * After analyses and scheduling, this method will call the right handler
185 * to pass the packet up in the stack.
186 *
187 * \param device network device
188 * \param p the packet
189 * \param protocol next header value
190 * \param from address of the correspondent
191 * \param to address of the destination
192 * \param packetType type of the packet
193 */
194 virtual void Receive(Ptr<NetDevice> device,
196 uint16_t protocol,
197 const Address& from,
198 const Address& to,
199 NetDevice::PacketType packetType);
200 /**
201 * \brief Called from upper layer to queue a packet for the transmission.
202 *
203 * \param device the device the packet must be sent to
204 * \param item a queue item including a packet and additional information
205 */
206 virtual void Send(Ptr<NetDevice> device, Ptr<QueueDiscItem> item);
207
208 protected:
209 void DoDispose() override;
210 void DoInitialize() override;
211 void NotifyNewAggregate() override;
212
213 private:
214 /**
215 * \brief Protocol handler entry.
216 * This structure is used to demultiplex all the protocols.
217 */
219 {
220 Node::ProtocolHandler handler; //!< the protocol handler
221 Ptr<NetDevice> device; //!< the NetDevice
222 uint16_t protocol; //!< the protocol number
223 bool promiscuous; //!< true if it is a promiscuous handler
224 };
225
226 /**
227 * \brief Information to store for each device
228 */
230 {
231 Ptr<QueueDisc> m_rootQueueDisc; //!< the root queue disc on the device
232 Ptr<NetDeviceQueueInterface> m_ndqi; //!< the netdevice queue interface
233 QueueDiscVector m_queueDiscsToWake; //!< the vector of queue discs to wake
234 };
235
236 /// Typedef for protocol handlers container
237 typedef std::vector<ProtocolHandlerEntry> ProtocolHandlerList;
238
239 /**
240 * \brief Required by the object map accessor
241 * \return the number of devices in the m_netDevices map
242 */
243 uint32_t GetNDevices() const;
244 /**
245 * \brief Required by the object map accessor
246 * \param index the index of the device in the node's device list
247 * \return the root queue disc installed on the specified device
248 */
250
251 /// The node this TrafficControlLayer object is aggregated to
253 /// Map storing the required information for each device with a queue disc installed
254 std::map<Ptr<NetDevice>, NetDeviceInfo> m_netDevices;
255 ProtocolHandlerList m_handlers; //!< List of upper-layer handlers
256
257 /**
258 * The trace source fired when the Traffic Control layer drops a packet because
259 * no queue disc is installed on the device, the device supports flow control and
260 * the device queue is stopped
261 */
263};
264
265} // namespace ns3
266
267#endif // TRAFFICCONTROLLAYER_H
a polymophic address class
Definition: address.h:101
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Forward calls to a chain of Callback.
The Traffic Control layer aims at introducing an equivalent of the Linux Traffic Control infrastructu...
void NotifyNewAggregate() override
Notify all Objects aggregated to this one of a new Object being aggregated.
static TypeId GetTypeId()
Get the type ID.
TypeId GetInstanceTypeId() const override
Get the type ID for the instance.
Ptr< QueueDisc > GetRootQueueDiscOnDeviceByIndex(uint32_t index) const
Required by the object map accessor.
TrafficControlLayer & operator=(const TrafficControlLayer &)=delete
void DoInitialize() override
Initialize() implementation.
void DoDispose() override
Destructor implementation.
TracedCallback< Ptr< const Packet > > m_dropped
The trace source fired when the Traffic Control layer drops a packet because no queue disc is install...
std::vector< ProtocolHandlerEntry > ProtocolHandlerList
Typedef for protocol handlers container.
ProtocolHandlerList m_handlers
List of upper-layer handlers.
Ptr< Node > m_node
The node this TrafficControlLayer object is aggregated to.
uint32_t GetNDevices() const
Required by the object map accessor.
std::map< Ptr< NetDevice >, NetDeviceInfo > m_netDevices
Map storing the required information for each device with a queue disc installed.
virtual void DeleteRootQueueDiscOnDevice(Ptr< NetDevice > device)
This method can be used to remove the root queue disc (and associated filters, classes and queues) in...
std::vector< Ptr< QueueDisc > > QueueDiscVector
Typedef for queue disc vector.
virtual void ScanDevices()
Collect information needed to determine how to handle packets destined to each of the NetDevices of t...
virtual void Send(Ptr< NetDevice > device, Ptr< QueueDiscItem > item)
Called from upper layer to queue a packet for the transmission.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
virtual Ptr< QueueDisc > GetRootQueueDiscOnDevice(Ptr< NetDevice > device) const
This method can be used to get the root queue disc installed on a device.
virtual void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Called by NetDevices, incoming packet.
TrafficControlLayer(const TrafficControlLayer &)=delete
virtual void SetRootQueueDiscOnDevice(Ptr< NetDevice > device, Ptr< QueueDisc > qDisc)
This method can be used to set the root queue disc installed on a device.
void RegisterProtocolHandler(Node::ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device)
Register an IN handler.
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Information to store for each device.
Ptr< NetDeviceQueueInterface > m_ndqi
the netdevice queue interface
Ptr< QueueDisc > m_rootQueueDisc
the root queue disc on the device
QueueDiscVector m_queueDiscsToWake
the vector of queue discs to wake
Protocol handler entry.
Node::ProtocolHandler handler
the protocol handler
bool promiscuous
true if it is a promiscuous handler
uint16_t protocol
the protocol number
Ptr< NetDevice > device
the NetDevice