A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
loopback-net-device.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "ns3/channel.h"
11#include "ns3/log.h"
12#include "ns3/node.h"
13#include "ns3/packet.h"
14#include "ns3/simulator.h"
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("LoopbackNetDevice");
20
21NS_OBJECT_ENSURE_REGISTERED(LoopbackNetDevice);
22
23TypeId
25{
26 static TypeId tid = TypeId("ns3::LoopbackNetDevice")
28 .SetGroupName("Internet")
29 .AddConstructor<LoopbackNetDevice>();
30 return tid;
31}
32
34 : m_node(nullptr),
35 m_mtu(0xffff),
36 m_ifIndex(0),
37 m_address(Mac48Address("00:00:00:00:00:00"))
38{
39 NS_LOG_FUNCTION(this);
40}
41
42void
44 uint16_t protocol,
45 Mac48Address to,
46 Mac48Address from)
47{
48 NS_LOG_FUNCTION(packet << " " << protocol << " " << to << " " << from);
49 NetDevice::PacketType packetType;
50 if (to == m_address || to.IsBroadcast())
51 {
52 packetType = NetDevice::PACKET_HOST;
53 }
54 else if (to.IsGroup())
55 {
56 packetType = NetDevice::PACKET_MULTICAST;
57 }
58 else
59 {
60 packetType = NetDevice::PACKET_OTHERHOST;
61 }
62 m_rxCallback(this, packet, protocol, from);
64 {
65 m_promiscCallback(this, packet, protocol, from, to, packetType);
66 }
67}
68
69void
71{
72 m_ifIndex = index;
73}
74
77{
78 return m_ifIndex;
79}
80
83{
84 return nullptr;
85}
86
87void
92
95{
96 return m_address;
97}
98
99bool
100LoopbackNetDevice::SetMtu(const uint16_t mtu)
101{
102 m_mtu = mtu;
103 return true;
104}
105
106uint16_t
108{
109 return m_mtu;
110}
111
112bool
114{
115 return true;
116}
117
118void
122
123bool
125{
126 return true;
127}
128
131{
132 // This is typically set to all zeros rather than all ones in real systems
133 return Mac48Address("00:00:00:00:00:00");
134}
135
136bool
138{
139 // Multicast loopback will need to be supported for outgoing
140 // datagrams but this will probably be handled in multicast sockets
141 return false;
142}
143
146{
147 return Mac48Address::GetMulticast(multicastGroup);
148}
149
155
156bool
158{
159 return false;
160}
161
162bool
164{
165 return false;
166}
167
168bool
169LoopbackNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
170{
171 NS_LOG_FUNCTION(packet << " " << dest << " " << protocolNumber);
173 NS_ASSERT_MSG(to == GetBroadcast() || to == m_address, "Invalid destination address");
175 Seconds(0.0),
177 this,
178 packet,
179 protocolNumber,
180 to,
181 m_address);
182 return true;
183}
184
185bool
187 const Address& source,
188 const Address& dest,
189 uint16_t protocolNumber)
190{
191 NS_LOG_FUNCTION(packet << " " << source << " " << dest << " " << protocolNumber);
194 NS_ASSERT_MSG(to.IsBroadcast() || to == m_address, "Invalid destination address");
196 Seconds(0.0),
198 this,
199 packet,
200 protocolNumber,
201 to,
202 from);
203 return true;
204}
205
208{
209 return m_node;
210}
211
212void
214{
215 m_node = node;
216}
217
218bool
220{
221 return false;
222}
223
224void
229
230void
236
237void
242
243bool
245{
246 return true;
247}
248
249} // namespace ns3
a polymophic address class
Definition address.h:90
Callback template class.
Definition callback.h:422
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.
Virtual network interface that loops back any data sent to it to be immediately received on the same ...
static TypeId GetTypeId()
Get the type ID.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void DoDispose() override
Destructor implementation.
uint32_t m_ifIndex
interface index
bool IsMulticast() const override
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from tge Loopback NetDevice.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool NeedsArp() const override
Ptr< Node > GetNode() const override
uint16_t GetMtu() const override
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
Ptr< Node > m_node
the node this NetDevice is associated with
bool IsBroadcast() const override
void SetIfIndex(const uint32_t index) override
bool IsLinkUp() const override
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
Mac48Address m_address
NetDevice MAC address.
uint32_t GetIfIndex() const override
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void AddLinkChangeCallback(Callback< void > callback) override
Address GetAddress() const override
bool SupportsSendFrom() const override
NetDevice::PromiscReceiveCallback m_promiscCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
void SetAddress(Address address) override
Set the address of this interface.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
Ptr< Channel > GetChannel() const override
bool SetMtu(const uint16_t mtu) override
Address GetBroadcast() const override
void SetNode(Ptr< Node > node) override
an EUI-48 address
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup() const
static Mac48Address ConvertFrom(const Address &address)
bool IsBroadcast() const
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
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition net-device.h:294
uint32_t GetId() const
Definition node.cc:106
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
Smart pointer class similar to boost::intrusive_ptr.
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
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_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.