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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8#include "mock-net-device.h"
9
10#include "ns3/boolean.h"
11#include "ns3/channel.h"
12#include "ns3/log.h"
13#include "ns3/mac16-address.h"
14#include "ns3/mac48-address.h"
15#include "ns3/mac64-address.h"
16#include "ns3/mac8-address.h"
17#include "ns3/node.h"
18#include "ns3/packet.h"
19#include "ns3/pointer.h"
20#include "ns3/simulator.h"
21#include "ns3/trace-source-accessor.h"
22
23namespace ns3
24{
25
26NS_LOG_COMPONENT_DEFINE("MockNetDevice");
27NS_OBJECT_ENSURE_REGISTERED(MockNetDevice);
28
29TypeId
31{
32 static TypeId tid = TypeId("ns3::MockNetDevice")
34 .SetGroupName("Network")
35 .AddConstructor<MockNetDevice>()
36 .AddAttribute("PointToPointMode",
37 "The device is configured in Point to Point mode",
38 BooleanValue(false),
41 return tid;
42}
43
45 : m_node(nullptr),
46 m_mtu(0xffff),
47 m_ifIndex(0),
48 m_linkUp(true)
49{
50 NS_LOG_FUNCTION(this);
51}
52
53void
55 uint16_t protocol,
56 Address to,
57 Address from,
58 NetDevice::PacketType packetType)
59{
60 NS_LOG_FUNCTION(this << packet << protocol << to << from);
61
62 if (packetType != NetDevice::PACKET_OTHERHOST)
63 {
64 m_rxCallback(this, packet, protocol, from);
65 }
66
68 {
69 m_promiscCallback(this, packet, protocol, from, to, packetType);
70 }
71}
72
73void
75{
76 NS_LOG_FUNCTION(this << index);
77 m_ifIndex = index;
78}
79
82{
83 NS_LOG_FUNCTION(this);
84 return m_ifIndex;
85}
86
89{
90 NS_LOG_FUNCTION(this);
91 return nullptr;
92}
93
94void
96{
97 NS_LOG_FUNCTION(this << address);
98 m_address = address;
99}
100
103{
104 NS_LOG_FUNCTION(this);
105 return m_address;
106}
107
108bool
109MockNetDevice::SetMtu(const uint16_t mtu)
110{
111 NS_LOG_FUNCTION(this << mtu);
112 m_mtu = mtu;
113 return true;
114}
115
116uint16_t
118{
119 NS_LOG_FUNCTION(this);
120 return m_mtu;
121}
122
123bool
125{
126 NS_LOG_FUNCTION(this);
127 return m_linkUp;
128}
129
130void
136
137bool
139{
140 NS_LOG_FUNCTION(this);
142 {
143 return false;
144 }
146 {
147 return false;
148 }
150 {
151 return false;
152 }
153
154 return true;
155}
156
159{
160 NS_LOG_FUNCTION(this);
161
162 Address address;
163
165 {
166 address = Mac48Address::GetBroadcast();
167 }
169 {
170 address = Mac16Address::GetBroadcast();
171 }
172
173 return address;
174}
175
176bool
178{
179 NS_LOG_FUNCTION(this);
181 {
182 return false;
183 }
185 {
186 return false;
187 }
189 {
190 return false;
191 }
192
193 return true;
194}
195
198{
199 NS_LOG_FUNCTION(this << multicastGroup);
200
201 Address address;
202
204 {
205 address = Mac48Address::GetMulticast(multicastGroup);
206 }
208 {
210 }
211
212 return address;
213}
214
217{
218 NS_LOG_FUNCTION(this << addr);
219 Address address;
220
222 {
223 address = Mac48Address::GetMulticast(addr);
224 }
226 {
227 address = Mac16Address::GetMulticast(addr);
228 }
229
230 return address;
231}
232
233bool
235{
236 NS_LOG_FUNCTION(this);
237 return m_pointToPointMode;
238}
239
240bool
242{
243 NS_LOG_FUNCTION(this);
244 return false;
245}
246
247bool
248MockNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
249{
250 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
251
252 return SendFrom(packet, m_address, dest, protocolNumber);
253}
254
255bool
257 const Address& source,
258 const Address& dest,
259 uint16_t protocolNumber)
260{
261 NS_LOG_FUNCTION(this << p << source << dest << protocolNumber);
262 if (p->GetSize() > GetMtu())
263 {
264 return false;
265 }
266
267 if (!m_sendCallback.IsNull())
268 {
269 m_sendCallback(this, p, protocolNumber, source, dest, NetDevice::PACKET_HOST);
270 }
271
272 return true;
273}
274
277{
278 NS_LOG_FUNCTION(this);
279 return m_node;
280}
281
282void
284{
285 NS_LOG_FUNCTION(this << node);
286 m_node = node;
287}
288
289bool
291{
292 NS_LOG_FUNCTION(this);
293 return !m_pointToPointMode;
294}
295
296void
302
303void
313
314void
320
321bool
323{
324 NS_LOG_FUNCTION(this);
325 return true;
326}
327
328void
334
335} // namespace ns3
a polymophic address class
Definition address.h:90
Callback template class.
Definition callback.h:422
void Nullify()
Discard the implementation, set it to null.
Definition callback.h:561
bool IsNull() const
Check for null implementation.
Definition callback.h:555
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
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.
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:87
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:289
@ PACKET_HOST
Packet addressed to us.
Definition net-device.h:290
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition net-device.h:296
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
Smart pointer class similar to boost::intrusive_ptr.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70