A Discrete-Event Network Simulator
API
net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/object.h"
22 #include "ns3/log.h"
23 #include "ns3/abort.h"
24 #include "ns3/uinteger.h"
25 #include "net-device.h"
26 #include "packet.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("NetDevice");
31 
33 {
34  m_packet = p;
35 }
36 
38 {
39  NS_LOG_FUNCTION (this);
40  m_packet = 0;
41 }
42 
45 {
46  return m_packet;
47 }
48 
49 uint32_t
51 {
52  NS_ASSERT (m_packet != 0);
53  return m_packet->GetSize ();
54 }
55 
56 void
57 QueueItem::Print (std::ostream& os) const
58 {
59  os << GetPacket();
60 }
61 
62 std::ostream & operator << (std::ostream &os, const QueueItem &item)
63 {
64  item.Print (os);
65  return os;
66 }
67 
69  : m_stopped (false)
70 {
71  NS_LOG_FUNCTION (this);
72 }
73 
75 {
76  NS_LOG_FUNCTION (this);
77 }
78 
79 bool
81 {
82  return m_stopped;
83 }
84 
85 void
87 {
88  m_stopped = false;
89 }
90 
91 void
93 {
94  m_stopped = true;
95 }
96 
97 void
99 {
100  Start ();
101 
102  // Request the queue disc to dequeue a packet
103  if (!m_wakeCallback.IsNull ())
104  {
105  m_wakeCallback ();
106  }
107 }
108 
109 void
111 {
112  m_wakeCallback = cb;
113 }
114 
115 bool
117 {
118  return (!m_wakeCallback.IsNull ());
119 }
120 
121 
123 
125 {
126  static TypeId tid = TypeId ("ns3::NetDeviceQueueInterface")
127  .SetParent<Object> ()
128  .SetGroupName("Network")
129  ;
130  return tid;
131 }
132 
134  : m_queueDiscInstalled (false)
135 {
136  NS_LOG_FUNCTION (this);
137  Ptr<NetDeviceQueue> devQueue = Create<NetDeviceQueue> ();
138  m_txQueuesVector.push_back (devQueue);
139 }
140 
142 {
143  NS_LOG_FUNCTION (this);
144 }
145 
148 {
149  NS_ASSERT (i < m_txQueuesVector.size ());
150  return m_txQueuesVector[i];
151 }
152 
153 uint8_t
155 {
156  return m_txQueuesVector.size ();
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this);
163  m_txQueuesVector.clear ();
165 }
166 
167 void
169 {
170  NS_ASSERT (numTxQueues > 0);
171 
172  // check whether a queue disc has been installed on the device by
173  // verifying whether a wake callback has been set on a transmission queue
174  NS_ABORT_MSG_IF (GetTxQueue (0)->HasWakeCallbackSet (), "Cannot change the number of"
175  " transmission queues after setting up the wake callback.");
176 
177  uint8_t prevNumTxQueues = m_txQueuesVector.size ();
178  m_txQueuesVector.resize (numTxQueues);
179 
180  // Allocate new NetDeviceQueues if the number of queues increased
181  for (uint8_t i = prevNumTxQueues; i < numTxQueues; i++)
182  {
183  Ptr<NetDeviceQueue> devQueue = Create<NetDeviceQueue> ();
184  m_txQueuesVector[i] = devQueue;
185  }
186 }
187 
188 void
190 {
192 }
193 
194 uint8_t
196 {
198  {
199  return m_selectQueueCallback (item);
200  }
201  return 0;
202 }
203 
204 bool
206 {
207  return m_queueDiscInstalled;
208 }
209 
210 void
212 {
213  NS_LOG_FUNCTION (this << installed);
214  m_queueDiscInstalled = installed;
215 }
216 
217 
219 
221 {
222  static TypeId tid = TypeId ("ns3::NetDevice")
223  .SetParent<Object> ()
224  .SetGroupName("Network")
225  ;
226  return tid;
227 }
228 
230 {
231  NS_LOG_FUNCTION (this);
232 }
233 
234 } // namespace ns3
virtual bool HasWakeCallbackSet(void) const
Check whether a wake callback has been set on this device queue.
Definition: net-device.cc:116
Base class to represent items of packet Queues.
Definition: net-device.h:55
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
QueueItem()
Default constructor.
virtual ~QueueItem()
Definition: net-device.cc:37
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1258
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
WakeCallback m_wakeCallback
Wake callback.
Definition: net-device.h:197
Ptr< Packet > m_packet
Definition: net-device.h:115
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:786
void SetSelectQueueCallback(SelectQueueCallback cb)
Set the select queue callback.
Definition: net-device.cc:189
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
virtual ~NetDevice()
Definition: net-device.cc:229
virtual void Start(void)
Called by the device to start this (hardware) transmission queue.
Definition: net-device.cc:86
bool IsStopped(void) const
Get the status of the device transmission queue.
Definition: net-device.cc:80
virtual uint32_t GetPacketSize(void) const
Use this method (instead of GetPacket ()->GetSize ()) to get the packet size.
Definition: net-device.cc:50
static TypeId GetTypeId(void)
Get the type ID.
Definition: net-device.cc:220
uint8_t GetSelectedQueue(Ptr< QueueItem > item) const
Get the id of the transmission queue selected for the given packet.
Definition: net-device.cc:195
static TypeId GetTypeId(void)
Get the type ID.
Definition: net-device.cc:124
SelectQueueCallback m_selectQueueCallback
Select queue callback.
Definition: net-device.h:300
Network device transmission queue interface.
Definition: net-device.h:216
void SetQueueDiscInstalled(bool installed)
Set the member variable indicating whether a queue disc is installed or not.
Definition: net-device.cc:211
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Stop(void)
Called by the device to stop this (hardware) transmission queue.
Definition: net-device.cc:92
std::vector< Ptr< NetDeviceQueue > > m_txQueuesVector
Device transmission queues.
Definition: net-device.h:299
uint8_t GetTxQueuesN(void) const
Get the number of device transmission queues.
Definition: net-device.cc:154
virtual void DoDispose(void)
Dispose of the object.
Definition: net-device.cc:160
bool m_queueDiscInstalled
Boolean value indicating whether a queue disc is installed or not.
Definition: net-device.h:301
bool IsQueueDiscInstalled(void) const
Return true if a queue disc is installed on the device.
Definition: net-device.cc:205
virtual void Print(std::ostream &os) const
Print the item contents.
Definition: net-device.cc:57
NetDeviceQueueInterface()
Constructor.
Definition: net-device.cc:133
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
virtual void Wake(void)
Called by the device to wake the queue disc associated with this (hardware) transmission queue...
Definition: net-device.cc:98
Network layer to device interface.
Definition: net-device.h:337
Ptr< NetDeviceQueue > GetTxQueue(uint8_t i) const
Get the i-th transmission queue of the device.
Definition: net-device.cc:147
virtual void SetWakeCallback(WakeCallback cb)
Set the wake callback.
Definition: net-device.cc:110
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual ~NetDeviceQueue()
Definition: net-device.cc:74
bool m_stopped
Status of the transmission queue.
Definition: net-device.h:196
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
void SetTxQueuesN(uint8_t numTxQueues)
Set the number of device transmission queues.
Definition: net-device.cc:168
Ptr< Packet > GetPacket(void) const
Definition: net-device.cc:44