A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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);
109 m_address = 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
173 Address address;
174
176 {
177 address = Mac48Address::GetBroadcast();
178 }
180 {
181 address = Mac16Address::GetBroadcast();
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
212 Address address;
213
215 {
216 address = Mac48Address::GetMulticast(multicastGroup);
217 }
219 {
221 }
222
223 return address;
224}
225
228{
229 NS_LOG_FUNCTION(this << addr);
230 Address address;
231
233 {
234 address = Mac48Address::GetMulticast(addr);
235 }
237 {
238 address = Mac16Address::GetMulticast(addr);
239 }
240
241 return address;
242}
243
244bool
246{
247 NS_LOG_FUNCTION(this);
248 return m_pointToPointMode;
249}
250
251bool
253{
254 NS_LOG_FUNCTION(this);
255 return false;
256}
257
258bool
259MockNetDevice::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
266bool
268 const Address& source,
269 const Address& dest,
270 uint16_t protocolNumber)
271{
272 NS_LOG_FUNCTION(this << p << source << dest << protocolNumber);
273 if (p->GetSize() > GetMtu())
274 {
275 return false;
276 }
277
278 if (!m_sendCallback.IsNull())
279 {
280 m_sendCallback(this, p, protocolNumber, source, dest, NetDevice::PACKET_HOST);
281 }
282
283 return true;
284}
285
288{
289 NS_LOG_FUNCTION(this);
290 return m_node;
291}
292
293void
295{
296 NS_LOG_FUNCTION(this << node);
297 m_node = node;
298}
299
300bool
302{
303 NS_LOG_FUNCTION(this);
304 return !m_pointToPointMode;
305}
306
307void
309{
310 NS_LOG_FUNCTION(this << &cb);
311 m_rxCallback = cb;
312}
313
314void
316{
317 NS_LOG_FUNCTION(this);
318 m_node = nullptr;
323}
324
325void
327{
328 NS_LOG_FUNCTION(this << &cb);
330}
331
332bool
334{
335 NS_LOG_FUNCTION(this);
336 return true;
337}
338
339void
341{
342 NS_LOG_FUNCTION(this << &cb);
343 m_sendCallback = cb;
344}
345
346} // namespace ns3
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Callback template class.
Definition: callback.h:438
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:575
bool IsNull() const
Check for null implementation.
Definition: callback.h:569
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
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:65
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:444
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:81
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:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.