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