A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
135  NS_LOG_FUNCTION (this);
136  Ptr<NetDeviceQueue> devQueue = Create<NetDeviceQueue> ();
137  m_txQueuesVector.push_back (devQueue);
138 }
139 
141 {
142  NS_LOG_FUNCTION (this);
143 }
144 
147 {
148  NS_ASSERT (i < m_txQueuesVector.size ());
149  return m_txQueuesVector[i];
150 }
151 
152 uint8_t
154 {
155  return m_txQueuesVector.size ();
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION (this);
162  m_txQueuesVector.clear ();
164 }
165 
166 void
168 {
169  NS_ASSERT (numTxQueues > 0);
170 
171  // check whether a queue disc has been installed on the device by
172  // verifying whether a wake callback has been set on a transmission queue
173  NS_ABORT_MSG_IF (GetTxQueue (0)->HasWakeCallbackSet (), "Cannot change the number of"
174  " transmission queues after setting up the wake callback.");
175 
176  uint8_t prevNumTxQueues = m_txQueuesVector.size ();
177  m_txQueuesVector.resize (numTxQueues);
178 
179  // Allocate new NetDeviceQueues if the number of queues increased
180  for (uint8_t i = prevNumTxQueues; i < numTxQueues; i++)
181  {
182  Ptr<NetDeviceQueue> devQueue = Create<NetDeviceQueue> ();
183  m_txQueuesVector[i] = devQueue;
184  }
185 }
186 
187 void
189 {
191 }
192 
193 uint8_t
195 {
197  {
198  return m_selectQueueCallback (item);
199  }
200  return 0;
201 }
202 
204 
206 {
207  static TypeId tid = TypeId ("ns3::NetDevice")
208  .SetParent<Object> ()
209  .SetGroupName("Network")
210  ;
211  return tid;
212 }
213 
215 {
216  NS_LOG_FUNCTION (this);
217 }
218 
219 } // 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:1270
#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:200
Ptr< Packet > m_packet
The packet contained in the queue item.
Definition: net-device.h:118
#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:792
void SetSelectQueueCallback(SelectQueueCallback cb)
Set the select queue callback.
Definition: net-device.cc:188
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:340
virtual ~NetDevice()
Definition: net-device.cc:214
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:205
uint8_t GetSelectedQueue(Ptr< QueueItem > item) const
Get the id of the transmission queue selected for the given packet.
Definition: net-device.cc:194
static TypeId GetTypeId(void)
Get the type ID.
Definition: net-device.cc:124
SelectQueueCallback m_selectQueueCallback
Select queue callback.
Definition: net-device.h:295
Network device transmission queue interface.
Definition: net-device.h:219
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
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:294
uint8_t GetTxQueuesN(void) const
Get the number of device transmission queues.
Definition: net-device.cc:153
virtual void DoDispose(void)
Dispose of the object.
Definition: net-device.cc:159
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:345
Ptr< NetDeviceQueue > GetTxQueue(uint8_t i) const
Get the i-th transmission queue of the device.
Definition: net-device.cc:146
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:199
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:827
void SetTxQueuesN(uint8_t numTxQueues)
Set the number of device transmission queues.
Definition: net-device.cc:167
Ptr< Packet > GetPacket(void) const
Definition: net-device.cc:44