A Discrete-Event Network Simulator
API
mock-net-device.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Universita' di Firenze, Italy
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19#include "mock-net-device.h"
20
21#include "ns3/boolean.h"
22#include "ns3/channel.h"
23#include "ns3/log.h"
24#include "ns3/mac16-address.h"
25#include "ns3/mac48-address.h"
26#include "ns3/mac64-address.h"
27#include "ns3/mac8-address.h"
28#include "ns3/node.h"
29#include "ns3/packet.h"
30#include "ns3/pointer.h"
31#include "ns3/simulator.h"
32#include "ns3/trace-source-accessor.h"
33
34namespace ns3
35{
36
37NS_LOG_COMPONENT_DEFINE("MockNetDevice");
38NS_OBJECT_ENSURE_REGISTERED(MockNetDevice);
39
40TypeId
42{
43 static TypeId tid = TypeId("ns3::MockNetDevice")
45 .SetGroupName("Network")
46 .AddConstructor<MockNetDevice>()
47 .AddAttribute("PointToPointMode",
48 "The device is configured in Point to Point mode",
49 BooleanValue(false),
52 return tid;
53}
54
56 : m_node(nullptr),
57 m_mtu(0xffff),
58 m_ifIndex(0),
59 m_linkUp(true)
60{
61 NS_LOG_FUNCTION(this);
62}
63
64void
66 uint16_t protocol,
67 Address to,
68 Address from,
69 NetDevice::PacketType packetType)
70{
71 NS_LOG_FUNCTION(this << packet << protocol << to << from);
72
73 if (packetType != NetDevice::PACKET_OTHERHOST)
74 {
75 m_rxCallback(this, packet, protocol, from);
76 }
77
79 {
80 m_promiscCallback(this, packet, protocol, from, to, packetType);
81 }
82}
83
84void
86{
87 NS_LOG_FUNCTION(this << index);
88 m_ifIndex = index;
89}
90
93{
94 NS_LOG_FUNCTION(this);
95 return m_ifIndex;
96}
97
100{
101 NS_LOG_FUNCTION(this);
102 return nullptr;
103}
104
105void
107{
108 NS_LOG_FUNCTION(this << address);
110}
111
114{
115 NS_LOG_FUNCTION(this);
116 return m_address;
117}
118
119bool
120MockNetDevice::SetMtu(const uint16_t mtu)
121{
122 NS_LOG_FUNCTION(this << mtu);
123 m_mtu = mtu;
124 return true;
125}
126
127uint16_t
129{
130 NS_LOG_FUNCTION(this);
131 return m_mtu;
132}
133
134bool
136{
137 NS_LOG_FUNCTION(this);
138 return m_linkUp;
139}
140
141void
143{
144 NS_LOG_FUNCTION(this << &callback);
146}
147
148bool
150{
151 NS_LOG_FUNCTION(this);
153 {
154 return false;
155 }
157 {
158 return false;
159 }
161 {
162 return false;
163 }
164
165 return true;
166}
167
170{
171 NS_LOG_FUNCTION(this);
172
174
176 {
178 }
180 {
182 }
183
184 return address;
185}
186
187bool
189{
190 NS_LOG_FUNCTION(this);
192 {
193 return false;
194 }
196 {
197 return false;
198 }
200 {
201 return false;
202 }
203
204 return true;
205}
206
209{
210 NS_LOG_FUNCTION(this << multicastGroup);
211
213
215 {
216 address = Mac48Address::GetMulticast(multicastGroup);
217 }
219 {
221 }
222
223 return address;
224}
225
228{
229 NS_LOG_FUNCTION(this << addr);
231
233 {
235 }
237 {
239 }
240
241 return address;
242}
243
244bool
246{
247 NS_LOG_FUNCTION(this);
249 {
250 return true;
251 }
252 return false;
253}
254
255bool
257{
258 NS_LOG_FUNCTION(this);
259 return false;
260}
261
262bool
263MockNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
264{
265 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
266
267 return SendFrom(packet, m_address, dest, protocolNumber);
268}
269
270bool
272 const Address& source,
273 const Address& dest,
274 uint16_t protocolNumber)
275{
276 NS_LOG_FUNCTION(this << p << source << dest << protocolNumber);
277 if (p->GetSize() > GetMtu())
278 {
279 return false;
280 }
281
282 if (!m_sendCallback.IsNull())
283 {
284 m_sendCallback(this, p, protocolNumber, source, dest, NetDevice::PACKET_HOST);
285 }
286
287 return true;
288}
289
292{
293 NS_LOG_FUNCTION(this);
294 return m_node;
295}
296
297void
299{
300 NS_LOG_FUNCTION(this << node);
301 m_node = node;
302}
303
304bool
306{
307 NS_LOG_FUNCTION(this);
309 {
310 return false;
311 }
312 return true;
313}
314
315void
317{
318 NS_LOG_FUNCTION(this << &cb);
319 m_rxCallback = cb;
320}
321
322void
324{
325 NS_LOG_FUNCTION(this);
326 m_node = nullptr;
331}
332
333void
335{
336 NS_LOG_FUNCTION(this << &cb);
338}
339
340bool
342{
343 NS_LOG_FUNCTION(this);
344 return true;
345}
346
347void
349{
350 NS_LOG_FUNCTION(this << &cb);
351 m_sendCallback = cb;
352}
353
354} // namespace ns3
a polymophic address class
Definition: address.h:92
AttributeValue implementation for Boolean.
Definition: boolean.h:37
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:562
bool IsNull() const
Check for null implementation.
Definition: callback.h:556
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Describes an IPv6 address.
Definition: ipv6-address.h:50
static Ipv6Address MakeIpv4MappedAddress(Ipv4Address addr)
Make the Ipv4-mapped IPv6 address.
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static bool IsMatchingType(const Address &address)
static Mac16Address GetBroadcast()
static Mac48Address GetMulticast(Ipv4Address address)
static bool IsMatchingType(const Address &address)
static Mac48Address GetBroadcast()
static bool IsMatchingType(const Address &address)
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:70
This device assumes 48-bit mac addressing; there is also the possibility to add an ErrorModel if you ...
Ptr< Node > m_node
Node this netDevice is associated to.
void DoDispose() override
Destructor implementation.
uint32_t m_ifIndex
Interface index.
void AddLinkChangeCallback(Callback< void > callback) override
uint16_t GetMtu() const override
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
bool IsLinkUp() const override
void SetSendCallback(PromiscReceiveCallback cb)
Add a callback to be invoked when the MockNetDevice has a packet to "send".
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
bool IsMulticast() const override
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
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.
Ptr< Node > GetNode() const override
void SetIfIndex(const uint32_t index) override
static TypeId GetTypeId()
Get the type ID.
NetDevice::PromiscReceiveCallback m_sendCallback
Send callback.
bool SupportsSendFrom() const override
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Address GetBroadcast() const override
bool IsBroadcast() const override
void SetNode(Ptr< Node > node) override
Address GetAddress() const override
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
bool m_pointToPointMode
Enabling this will disable Broadcast and Arp.
Ptr< Channel > GetChannel() const override
Address m_address
MAC address.
bool m_linkUp
Flag indicating whether or not the link is up.
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
void SetAddress(Address address) override
Set the address of this interface.
bool NeedsArp() const override
uint32_t GetIfIndex() const override
bool SetMtu(const uint16_t mtu) override
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_HOST
Packet addressed to us.
Definition: net-device.h:301
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:45
address
Definition: first.py:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.