A Discrete-Event Network Simulator
API
loopback-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) 2008 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "loopback-net-device.h"
21 #include "ns3/log.h"
22 #include "ns3/simulator.h"
23 #include "ns3/channel.h"
24 #include "ns3/node.h"
25 #include "ns3/packet.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("LoopbackNetDevice");
30 
31 NS_OBJECT_ENSURE_REGISTERED (LoopbackNetDevice);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::LoopbackNetDevice")
37  .SetParent<NetDevice> ()
38  .SetGroupName ("Internet")
39  .AddConstructor<LoopbackNetDevice> ()
40  ;
41  return tid;
42 }
43 
45  : m_node (0),
46  m_mtu (0xffff),
47  m_ifIndex (0),
48  m_address (Mac48Address ("00:00:00:00:00:00"))
49 {
50  NS_LOG_FUNCTION (this);
51 }
52 
53 void
54 LoopbackNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
55  Mac48Address to, Mac48Address from)
56 {
57  NS_LOG_FUNCTION (packet << " " << protocol << " " << to << " " << from);
58  NetDevice::PacketType packetType;
59  if (to == m_address)
60  {
61  packetType = NetDevice::PACKET_HOST;
62  }
63  else if (to.IsBroadcast ())
64  {
65  packetType = NetDevice::PACKET_HOST;
66  }
67  else if (to.IsGroup ())
68  {
69  packetType = NetDevice::PACKET_MULTICAST;
70  }
71  else
72  {
73  packetType = NetDevice::PACKET_OTHERHOST;
74  }
75  m_rxCallback (this, packet, protocol, from);
76  if (!m_promiscCallback.IsNull ())
77  {
78  m_promiscCallback (this, packet, protocol, from, to, packetType);
79  }
80 }
81 
82 void
83 LoopbackNetDevice::SetIfIndex (const uint32_t index)
84 {
85  m_ifIndex = index;
86 }
87 
88 uint32_t
90 {
91  return m_ifIndex;
92 }
93 
96 {
97  return 0;
98 }
99 
100 void
102 {
104 }
105 
106 Address
108 {
109  return m_address;
110 }
111 
112 bool
113 LoopbackNetDevice::SetMtu (const uint16_t mtu)
114 {
115  m_mtu = mtu;
116  return true;
117 }
118 
119 uint16_t
121 {
122  return m_mtu;
123 }
124 
125 bool
127 {
128  return true;
129 }
130 
131 void
133 {}
134 
135 bool
137 {
138  return true;
139 }
140 
141 Address
143 {
144  // This is typically set to all zeros rather than all ones in real systems
145  return Mac48Address ("00:00:00:00:00:00");
146 }
147 
148 bool
150 {
151  // Multicast loopback will need to be supported for outgoing
152  // datagrams but this will probably be handled in multicast sockets
153  return false;
154 }
155 
156 Address
158 {
159  return Mac48Address::GetMulticast (multicastGroup);
160 }
161 
163 {
164  return Mac48Address::GetMulticast (addr);
165 }
166 
167 bool
169 {
170  return false;
171 }
172 
173 bool
175 {
176  return false;
177 }
178 
179 bool
180 LoopbackNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
181 {
182  NS_LOG_FUNCTION (packet << " " << dest << " " << protocolNumber);
184  NS_ASSERT_MSG (to == GetBroadcast () || to == m_address, "Invalid destination address");
185  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.0), &LoopbackNetDevice::Receive, this, packet, protocolNumber, to, m_address);
186  return true;
187 }
188 
189 bool
190 LoopbackNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
191 {
192  NS_LOG_FUNCTION (packet << " " << source << " " << dest << " " << protocolNumber);
194  Mac48Address from = Mac48Address::ConvertFrom (source);
195  NS_ASSERT_MSG (to.IsBroadcast () || to == m_address, "Invalid destination address");
196  Simulator::ScheduleWithContext (m_node->GetId (), Seconds (0.0), &LoopbackNetDevice::Receive, this, packet, protocolNumber, to, from);
197  return true;
198 }
199 
200 Ptr<Node>
202 {
203  return m_node;
204 }
205 
206 void
208 {
209  m_node = node;
210 }
211 
212 bool
214 {
215  return false;
216 }
217 
218 void
220 {
221  m_rxCallback = cb;
222 }
223 
224 void
226 {
227  m_node = 0;
229 }
230 
231 
232 void
234 {
235  m_promiscCallback = cb;
236 }
237 
238 bool
240 {
241  return true;
242 }
243 
244 } // namespace ns3
a polymophic address class
Definition: address.h:91
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Describes an IPv6 address.
Definition: ipv6-address.h:50
Virtual network interface that loops back any data sent to it to be immediately received on the same ...
virtual bool SetMtu(const uint16_t mtu)
uint32_t m_ifIndex
interface index
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from tge Loopback NetDevice.
virtual void SetIfIndex(const uint32_t index)
virtual bool IsLinkUp(void) const
virtual Address GetAddress(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual bool IsBroadcast(void) const
virtual uint16_t GetMtu(void) const
Ptr< Node > m_node
the node this NetDevice is associated with
virtual bool IsMulticast(void) const
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
Mac48Address m_address
NetDevice MAC address.
virtual bool NeedsArp(void) const
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
virtual Ptr< Node > GetNode(void) const
virtual void AddLinkChangeCallback(Callback< void > callback)
static TypeId GetTypeId(void)
Get the type ID.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Ptr< Channel > GetChannel(void) const
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual uint32_t GetIfIndex(void) const
virtual Address GetBroadcast(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool SupportsSendFrom(void) const
NetDevice::PromiscReceiveCallback m_promiscCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
uint16_t m_mtu
device MTU
virtual void DoDispose(void)
Destructor implementation.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual void SetAddress(Address address)
Set the address of this interface.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual void SetNode(Ptr< Node > node)
an EUI-48 address
Definition: mac48-address.h:44
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup(void) const
bool IsBroadcast(void) const
static Mac48Address ConvertFrom(const Address &address)
Network layer to device interface.
Definition: net-device.h:96
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:302
uint32_t GetId(void) const
Definition: node.cc:109
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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:88
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.