A Discrete-Event Network Simulator
API
mock-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) 2020 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 #include "mock-net-device.h"
21 #include "ns3/node.h"
22 #include "ns3/packet.h"
23 #include "ns3/log.h"
24 #include "ns3/pointer.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/boolean.h"
27 #include "ns3/simulator.h"
28 #include "ns3/channel.h"
29 #include "ns3/mac64-address.h"
30 #include "ns3/mac48-address.h"
31 #include "ns3/mac16-address.h"
32 #include "ns3/mac8-address.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("MockNetDevice");
37 NS_OBJECT_ENSURE_REGISTERED (MockNetDevice);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::MockNetDevice")
43  .SetParent<NetDevice> ()
44  .SetGroupName("Network")
45  .AddConstructor<MockNetDevice> ()
46  .AddAttribute ("PointToPointMode",
47  "The device is configured in Point to Point mode",
48  BooleanValue (false),
51  ;
52  return tid;
53 }
54 
56  : m_node (0),
57  m_mtu (0xffff),
58  m_ifIndex (0),
59  m_linkUp (true)
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
64 void
65 MockNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
66  Address to, Address from, NetDevice::PacketType packetType)
67 {
68  NS_LOG_FUNCTION (this << packet << protocol << to << from);
69 
70  if (packetType != NetDevice::PACKET_OTHERHOST)
71  {
72  m_rxCallback (this, packet, protocol, from);
73  }
74 
75  if (!m_promiscCallback.IsNull ())
76  {
77  m_promiscCallback (this, packet, protocol, from, to, packetType);
78  }
79 }
80 
81 void
82 MockNetDevice::SetIfIndex (const uint32_t index)
83 {
84  NS_LOG_FUNCTION (this << index);
85  m_ifIndex = index;
86 }
87 
88 uint32_t
90 {
91  NS_LOG_FUNCTION (this);
92  return m_ifIndex;
93 }
94 
97 {
98  NS_LOG_FUNCTION (this);
99  return 0;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this << address);
106  m_address = address;
107 }
108 
109 Address
111 {
112  NS_LOG_FUNCTION (this);
113  return m_address;
114 }
115 
116 bool
117 MockNetDevice::SetMtu (const uint16_t mtu)
118 {
119  NS_LOG_FUNCTION (this << mtu);
120  m_mtu = mtu;
121  return true;
122 }
123 
124 uint16_t
126 {
127  NS_LOG_FUNCTION (this);
128  return m_mtu;
129 }
130 
131 bool
133 {
134  NS_LOG_FUNCTION (this);
135  return m_linkUp;
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << &callback);
143 }
144 
145 bool
147 {
148  NS_LOG_FUNCTION (this);
149  if (m_pointToPointMode)
150  {
151  return false;
152  }
154  {
155  return false;
156  }
158  {
159  return false;
160  }
161 
162  return true;
163 }
164 
165 Address
167 {
168  NS_LOG_FUNCTION (this);
169 
171 
173  {
175  }
177  {
179  }
180 
181  return address;
182 }
183 
184 bool
186 {
187  NS_LOG_FUNCTION (this);
188  if (m_pointToPointMode)
189  {
190  return false;
191  }
193  {
194  return false;
195  }
197  {
198  return false;
199  }
200 
201  return true;
202 }
203 
204 Address
206 {
207  NS_LOG_FUNCTION (this << multicastGroup);
208 
210 
212  {
213  address = Mac48Address::GetMulticast (multicastGroup);
214  }
216  {
218  }
219 
220  return address;
221 }
222 
224 {
225  NS_LOG_FUNCTION (this << addr);
227 
229  {
231  }
233  {
235  }
236 
237  return address;
238 }
239 
240 bool
242 {
243  NS_LOG_FUNCTION (this);
244  if (m_pointToPointMode)
245  {
246  return true;
247  }
248  return false;
249 }
250 
251 bool
253 {
254  NS_LOG_FUNCTION (this);
255  return false;
256 }
257 
258 bool
259 MockNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
260 {
261  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
262 
263  return SendFrom (packet, m_address, dest, protocolNumber);
264 }
265 
266 bool
267 MockNetDevice::SendFrom (Ptr<Packet> p, const Address& source, const Address& dest, uint16_t protocolNumber)
268 {
269  NS_LOG_FUNCTION (this << p << source << dest << protocolNumber);
270  if (p->GetSize () > GetMtu ())
271  {
272  return false;
273  }
274 
275  if (!m_sendCallback.IsNull ())
276  {
277  m_sendCallback (this, p, protocolNumber, source, dest, NetDevice::PACKET_HOST);
278  }
279 
280  return true;
281 }
282 
283 
284 Ptr<Node>
286 {
287  NS_LOG_FUNCTION (this);
288  return m_node;
289 }
290 void
292 {
293  NS_LOG_FUNCTION (this << node);
294  m_node = node;
295 }
296 
297 bool
299 {
300  NS_LOG_FUNCTION (this);
301  if (m_pointToPointMode)
302  {
303  return false;
304  }
305  return true;
306 }
307 
308 void
310 {
311  NS_LOG_FUNCTION (this << &cb);
312  m_rxCallback = cb;
313 }
314 
315 void
317 {
318  NS_LOG_FUNCTION (this);
319  m_node = 0;
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this << &cb);
330  m_promiscCallback = cb;
331 }
332 
333 bool
335 {
336  NS_LOG_FUNCTION (this);
337  return true;
338 }
339 
340 void
342 {
343  NS_LOG_FUNCTION (this << &cb);
344  m_sendCallback = cb;
345 }
346 
347 
348 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::MockNetDevice::NeedsArp
virtual bool NeedsArp(void) const
Definition: mock-net-device.cc:298
ns3::MockNetDevice::IsMulticast
virtual bool IsMulticast(void) const
Definition: mock-net-device.cc:185
ns3::Mac48Address::GetMulticast
static Mac48Address GetMulticast(Ipv4Address address)
Definition: mac48-address.cc:191
ns3::Mac48Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: mac48-address.cc:110
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
ns3::Callback< void >
ns3::MockNetDevice::m_ifIndex
uint32_t m_ifIndex
Interface index.
Definition: mock-net-device.h:118
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Callback::IsNull
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
ns3::TracedCallback::ConnectWithoutContext
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Definition: traced-callback.h:135
ns3::Mac64Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: mac64-address.cc:110
ns3::MockNetDevice::IsBroadcast
virtual bool IsBroadcast(void) const
Definition: mock-net-device.cc:146
ns3::MockNetDevice::m_linkUp
bool m_linkUp
Flag indicating whether or not the link is up.
Definition: mock-net-device.h:121
ns3::MockNetDevice::MockNetDevice
MockNetDevice()
Definition: mock-net-device.cc:55
ns3::MockNetDevice::GetMtu
virtual uint16_t GetMtu(void) const
Definition: mock-net-device.cc:125
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::Ipv6Address
Describes an IPv6 address.
Definition: ipv6-address.h:50
ns3::NetDevice::PACKET_HOST
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
ns3::MockNetDevice::m_mtu
uint16_t m_mtu
MTU.
Definition: mock-net-device.h:117
ns3::MockNetDevice::SetIfIndex
virtual void SetIfIndex(const uint32_t index)
Definition: mock-net-device.cc:82
ns3::MockNetDevice::m_pointToPointMode
bool m_pointToPointMode
Enabling this will disable Broadcast and Arp.
Definition: mock-net-device.h:122
ns3::MockNetDevice::IsPointToPoint
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Definition: mock-net-device.cc:241
ns3::MakeBooleanAccessor
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:85
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::MockNetDevice::m_linkChangeCallbacks
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
Definition: mock-net-device.h:127
ns3::MockNetDevice::GetAddress
virtual Address GetAddress(void) const
Definition: mock-net-device.cc:110
ns3::Ptr< Packet >
ns3::Mac48Address::GetBroadcast
static Mac48Address GetBroadcast(void)
Definition: mac48-address.cc:170
ns3::MockNetDevice::SetMtu
virtual bool SetMtu(const uint16_t mtu)
Definition: mock-net-device.cc:117
mock-net-device.h
ns3::MockNetDevice::m_promiscCallback
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
Definition: mock-net-device.h:114
ns3::NetDevice::PACKET_OTHERHOST
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
ns3::MockNetDevice::GetChannel
virtual Ptr< Channel > GetChannel(void) const
Definition: mock-net-device.cc:96
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::MockNetDevice::Receive
void Receive(Ptr< Packet > packet, uint16_t protocol, Address to, Address from, NetDevice::PacketType packetType)
Pretend that a packet has been received from a connected Channel.
Definition: mock-net-device.cc:65
ns3::MockNetDevice::Send
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Definition: mock-net-device.cc:259
ns3::Mac16Address::GetBroadcast
static Mac16Address GetBroadcast(void)
Definition: mac16-address.cc:161
ns3::MakeBooleanChecker
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
ns3::MockNetDevice::SetAddress
virtual void SetAddress(Address address)
Set the address of this interface.
Definition: mock-net-device.cc:103
first.address
address
Definition: first.py:44
ns3::Mac8Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:63
ns3::MockNetDevice::AddLinkChangeCallback
virtual void AddLinkChangeCallback(Callback< void > callback)
Definition: mock-net-device.cc:139
ns3::MockNetDevice::GetNode
virtual Ptr< Node > GetNode(void) const
Definition: mock-net-device.cc:285
ns3::Mac16Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: mac16-address.cc:112
ns3::MockNetDevice
This device assumes 48-bit mac addressing; there is also the possibility to add an ErrorModel if you ...
Definition: mock-net-device.h:48
ns3::MockNetDevice::m_rxCallback
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
Definition: mock-net-device.h:113
ns3::MockNetDevice::SupportsSendFrom
virtual bool SupportsSendFrom(void) const
Definition: mock-net-device.cc:334
ns3::MockNetDevice::m_address
Address m_address
MAC address.
Definition: mock-net-device.h:119
ns3::MockNetDevice::SendFrom
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Definition: mock-net-device.cc:267
ns3::MockNetDevice::SetNode
virtual void SetNode(Ptr< Node > node)
Definition: mock-net-device.cc:291
ns3::MockNetDevice::GetMulticast
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Definition: mock-net-device.cc:205
ns3::MockNetDevice::SetPromiscReceiveCallback
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
Definition: mock-net-device.cc:327
ns3::Mac16Address::GetMulticast
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
Definition: mac16-address.cc:170
ns3::MockNetDevice::IsLinkUp
virtual bool IsLinkUp(void) const
Definition: mock-net-device.cc:132
ns3::MockNetDevice::m_node
Ptr< Node > m_node
Node this netDevice is associated to.
Definition: mock-net-device.h:116
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::NetDevice::PacketType
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
ns3::MockNetDevice::SetSendCallback
void SetSendCallback(PromiscReceiveCallback cb)
Add a callback to be invoked when the MockNetDevice has a packet to "send".
Definition: mock-net-device.cc:341
ns3::Ipv6Address::MakeIpv4MappedAddress
static Ipv6Address MakeIpv4MappedAddress(Ipv4Address addr)
Make the Ipv4-mapped IPv6 address.
Definition: ipv6-address.cc:352
ns3::MockNetDevice::IsBridge
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Definition: mock-net-device.cc:252
ns3::MockNetDevice::GetBroadcast
virtual Address GetBroadcast(void) const
Definition: mock-net-device.cc:166
ns3::MockNetDevice::SetReceiveCallback
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Definition: mock-net-device.cc:309
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::MockNetDevice::m_sendCallback
NetDevice::PromiscReceiveCallback m_sendCallback
Send callback.
Definition: mock-net-device.h:115
ns3::MockNetDevice::GetIfIndex
virtual uint32_t GetIfIndex(void) const
Definition: mock-net-device.cc:89
ns3::NetDevice
Network layer to device interface.
Definition: net-device.h:96
ns3::MockNetDevice::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: mock-net-device.cc:316
ns3::Callback::Nullify
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1391
ns3::MockNetDevice::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: mock-net-device.cc:40