A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-interface.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006,2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "ipv4-interface.h"
10
11#include "arp-cache.h"
12#include "arp-l3-protocol.h"
13#include "ipv4-l3-protocol.h"
15#include "loopback-net-device.h"
16
17#include "ns3/iana-ieee802-numbers.h"
18#include "ns3/log.h"
19#include "ns3/net-device.h"
20#include "ns3/node.h"
21#include "ns3/packet.h"
22#include "ns3/pointer.h"
23#include "ns3/traffic-control-layer.h"
24
25namespace ns3
26{
27
28NS_LOG_COMPONENT_DEFINE("Ipv4Interface");
29
31
34{
35 static TypeId tid =
36 TypeId("ns3::Ipv4Interface")
38 .SetGroupName("Internet")
39 .AddAttribute(
40 "ArpCache",
41 "The arp cache for this ipv4 interface",
42 PointerValue(nullptr),
45 .AddTraceSource("InterfaceStatus",
46 "Interface has been set up or down.",
48 "ns3::Ipv4Address::TracedCallback");
49 return tid;
50}
51
52/**
53 * By default, Ipv4 interface are created in the "down" state
54 * with no IP addresses. Before becoming usable, the user must
55 * invoke SetUp on them once an Ipv4 address and mask have been set.
56 */
58 : m_ifup(false),
59 m_forwarding(true),
60 m_metric(1),
61 m_node(nullptr),
62 m_device(nullptr),
63 m_tc(nullptr),
64 m_cache(nullptr)
65{
66 NS_LOG_FUNCTION(this);
67}
68
73
74void
76{
77 NS_LOG_FUNCTION(this);
78 m_node = nullptr;
79 m_device = nullptr;
80 m_tc = nullptr;
81 m_cache = nullptr;
83}
84
85void
87{
88 NS_LOG_FUNCTION(this << node);
89 m_node = node;
90 DoSetup();
91}
92
93void
95{
96 NS_LOG_FUNCTION(this << device);
97 m_device = device;
98 DoSetup();
99}
100
101void
107
108void
110{
111 NS_LOG_FUNCTION(this);
112 if (!m_node || !m_device)
113 {
114 return;
115 }
116 if (!m_device->NeedsArp())
117 {
118 return;
119 }
120 Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol>();
121 m_cache = arp->CreateCache(m_device, this);
122}
123
126{
127 NS_LOG_FUNCTION(this);
128 return m_device;
129}
130
131void
133{
134 NS_LOG_FUNCTION(this << metric);
135 m_metric = metric;
136}
137
138uint16_t
140{
141 NS_LOG_FUNCTION(this);
142 return m_metric;
143}
144
145void
151
154{
155 NS_LOG_FUNCTION(this);
156 return m_cache;
157}
158
159/**
160 * These are IP interface states and may be distinct from
161 * NetDevice states, such as found in real implementations
162 * (where the device may be down but IP interface state is still up).
163 */
164bool
166{
167 NS_LOG_FUNCTION(this);
168 return m_ifup;
169}
170
171bool
173{
174 NS_LOG_FUNCTION(this);
175 return !m_ifup;
176}
177
178void
180{
181 NS_LOG_FUNCTION(this);
182 m_ifup = true;
183
184 Ptr<Ipv4> ip = m_node->GetObject<Ipv4>();
185 NS_ASSERT_MSG(ip, "IPv4 not installed on node.");
186 auto ifIndex = ip->GetInterfaceForDevice(m_device);
187 m_interfaceStatus(m_ifup, ifIndex);
188}
189
190void
192{
193 NS_LOG_FUNCTION(this);
194 m_ifup = false;
195
196 Ptr<Ipv4> ip = m_node->GetObject<Ipv4>();
197 NS_ASSERT_MSG(ip, "IPv4 not installed on node.");
198 auto ifIndex = ip->GetInterfaceForDevice(m_device);
199 m_interfaceStatus(m_ifup, ifIndex);
200}
201
202bool
204{
205 NS_LOG_FUNCTION(this);
206 return m_forwarding;
207}
208
209void
211{
212 NS_LOG_FUNCTION(this << val);
213 m_forwarding = val;
214}
215
216void
218{
219 NS_LOG_FUNCTION(this << *p << dest);
220 if (!IsUp())
221 {
222 return;
223 }
224
225 // Check for a loopback device, if it's the case we don't pass through
226 // traffic control layer
228 {
229 /// @todo additional checks needed here (such as whether multicast
230 /// goes to loopback)?
231 p->AddHeader(hdr);
232 m_device->Send(p, m_device->GetBroadcast(), iana::ieee802numbers::IPV4);
233 return;
234 }
235
237
238 // is this packet aimed at a local interface ?
239 for (auto i = m_ifaddrs.begin(); i != m_ifaddrs.end(); ++i)
240 {
241 if (dest == (*i).GetLocal())
242 {
243 p->AddHeader(hdr);
245 m_tc,
246 m_device,
247 p,
249 m_device->GetBroadcast(),
250 m_device->GetBroadcast(),
252 return;
253 }
254 }
255 if (m_device->NeedsArp())
256 {
257 NS_LOG_LOGIC("Needs ARP " << dest);
258 Ptr<ArpL3Protocol> arp = m_node->GetObject<ArpL3Protocol>();
259 Address hardwareDestination;
260 bool found = false;
261 if (dest.IsBroadcast())
262 {
263 NS_LOG_LOGIC("All-network Broadcast");
264 hardwareDestination = m_device->GetBroadcast();
265 found = true;
266 }
267 else if (dest.IsMulticast())
268 {
269 NS_LOG_LOGIC("IsMulticast");
270 NS_ASSERT_MSG(m_device->IsMulticast(),
271 "ArpIpv4Interface::SendTo (): Sending multicast packet over "
272 "non-multicast device");
273
274 hardwareDestination = m_device->GetMulticast(dest);
275 found = true;
276 }
277 else
278 {
279 for (auto i = m_ifaddrs.begin(); i != m_ifaddrs.end(); ++i)
280 {
281 if (dest.IsSubnetDirectedBroadcast((*i).GetMask()))
282 {
283 NS_LOG_LOGIC("Subnetwork Broadcast");
284 hardwareDestination = m_device->GetBroadcast();
285 found = true;
286 break;
287 }
288 }
289 if (!found)
290 {
291 NS_LOG_LOGIC("ARP Lookup");
292 found = arp->Lookup(p, hdr, dest, m_device, m_cache, &hardwareDestination);
293 }
294 }
295
296 if (found)
297 {
298 NS_LOG_LOGIC("Address Resolved. Send.");
299 m_tc->Send(
300 m_device,
301 Create<Ipv4QueueDiscItem>(p, hardwareDestination, iana::ieee802numbers::IPV4, hdr));
302 }
303 }
304 else
305 {
306 NS_LOG_LOGIC("Doesn't need ARP");
307 m_tc->Send(m_device,
309 m_device->GetBroadcast(),
311 hdr));
312 }
313}
314
317{
318 NS_LOG_FUNCTION(this);
319 return m_ifaddrs.size();
320}
321
322bool
324{
325 NS_LOG_FUNCTION(this << addr);
326 m_ifaddrs.push_back(addr);
327 if (!m_addAddressCallback.IsNull())
328 {
329 m_addAddressCallback(this, addr);
330 }
331 return true;
332}
333
336{
337 NS_LOG_FUNCTION(this << index);
338 if (index >= m_ifaddrs.size())
339 {
340 NS_FATAL_ERROR("index " << index << " out of bounds");
341 }
342
343 uint32_t tmp = 0;
344 for (auto i = m_ifaddrs.begin(); i != m_ifaddrs.end(); i++)
345 {
346 if (tmp == index)
347 {
348 return *i;
349 }
350 ++tmp;
351 }
352
353 return {}; // quiet compiler
354}
355
358{
359 NS_LOG_FUNCTION(this << index);
360 if (index >= m_ifaddrs.size())
361 {
362 NS_FATAL_ERROR("Bug in Ipv4Interface::RemoveAddress");
363 }
364
365 uint32_t tmp = 0;
366 for (auto it = m_ifaddrs.begin(); it != m_ifaddrs.end(); it++)
367 {
368 if (tmp == index)
369 {
370 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
371 Ipv4InterfaceAddress addr = *it;
372
373 m_ifaddrs.erase(it);
374 if (!m_removeAddressCallback.IsNull())
375 {
376 m_removeAddressCallback(this, addr);
377 }
378 return addr;
379 }
380 ++tmp;
381 }
382 NS_FATAL_ERROR("Address " << index << " not found");
383 return {}; // quiet compiler
384}
385
388{
389 NS_LOG_FUNCTION(this << address);
390
391 if (address == Ipv4Address::GetLoopback())
392 {
393 NS_LOG_WARN("Cannot remove loopback address.");
394 return Ipv4InterfaceAddress();
395 }
396
397 for (auto it = m_ifaddrs.begin(); it != m_ifaddrs.end(); it++)
398 {
399 if ((*it).GetLocal() == address)
400 {
401 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
402 Ipv4InterfaceAddress ifAddr = *it;
403
404 m_ifaddrs.erase(it);
405 if (!m_removeAddressCallback.IsNull())
406 {
407 m_removeAddressCallback(this, ifAddr);
408 }
409 return ifAddr;
410 }
411 }
412 return {};
413}
414
415void
417 Callback<void, Ptr<Ipv4Interface>, Ipv4InterfaceAddress> removeAddressCallback)
418{
419 NS_LOG_FUNCTION(this << &removeAddressCallback);
420 m_removeAddressCallback = removeAddressCallback;
421}
422
423void
425 Callback<void, Ptr<Ipv4Interface>, Ipv4InterfaceAddress> addAddressCallback)
426{
427 NS_LOG_FUNCTION(this << &addAddressCallback);
428 m_addAddressCallback = addAddressCallback;
429}
430
431} // namespace ns3
a polymophic address class
Definition address.h:114
An implementation of the ARP protocol.
Callback template class.
Definition callback.h:428
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetLoopback()
bool IsMulticast() const
bool IsSubnetDirectedBroadcast(const Ipv4Mask &mask) const
Generate subnet-directed broadcast address corresponding to mask.
bool IsBroadcast() const
Packet header for IPv4.
Definition ipv4-header.h:23
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
The IPv4 representation of a network interface.
static TypeId GetTypeId()
Get the type ID.
uint32_t GetNAddresses() const
Ptr< Node > m_node
The associated node.
void SetArpCache(Ptr< ArpCache > arpCache)
Set ARP cache used by this interface.
Ipv4Interface()
By default, Ipv4 interface are created in the "down" state with no IP addresses.
~Ipv4Interface() override
void RemoveAddressCallback(Callback< void, Ptr< Ipv4Interface >, Ipv4InterfaceAddress > removeAddressCallback)
This callback is set when an address is removed from an interface with auto-generated Arp cache and i...
void SetNode(Ptr< Node > node)
Set node associated with interface.
Ipv4InterfaceAddress GetAddress(uint32_t index) const
Ptr< TrafficControlLayer > m_tc
The associated TrafficControlLayer.
bool AddAddress(Ipv4InterfaceAddress address)
void SetTrafficControl(Ptr< TrafficControlLayer > tc)
Set the TrafficControlLayer.
bool m_forwarding
Forwarding state.
uint16_t GetMetric() const
ns3::TracedCallback< bool, int32_t > m_interfaceStatus
The trace fired when the interface state changes.
Ptr< NetDevice > m_device
The associated NetDevice.
bool IsUp() const
These are IP interface states and may be distinct from NetDevice states, such as found in real implem...
void SetUp()
Enable this interface.
Ptr< ArpCache > GetArpCache() const
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice.
void DoSetup()
Initialize interface.
Ptr< NetDevice > GetDevice() const
Ipv4InterfaceAddressList m_ifaddrs
Address list.
Callback< void, Ptr< Ipv4Interface >, Ipv4InterfaceAddress > m_addAddressCallback
add address callback
uint16_t m_metric
Interface metric.
void SetDown()
Disable this interface.
bool IsForwarding() const
void AddAddressCallback(Callback< void, Ptr< Ipv4Interface >, Ipv4InterfaceAddress > addAddressCallback)
This callback is set when an address is added from an interface with auto-generated Arp cache and it ...
void DoDispose() override
Destructor implementation.
Ptr< ArpCache > m_cache
ARP cache.
Callback< void, Ptr< Ipv4Interface >, Ipv4InterfaceAddress > m_removeAddressCallback
remove address callback
void Send(Ptr< Packet > p, const Ipv4Header &hdr, Ipv4Address dest)
bool m_ifup
The state of this interface.
void SetForwarding(bool val)
void SetMetric(uint16_t metric)
Ipv4InterfaceAddress RemoveAddress(uint32_t index)
@ PACKET_HOST
Packet addressed to us.
Definition net-device.h:315
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
virtual void DoDispose()
Destructor implementation.
Definition object.cc:431
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:614
virtual void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Called by NetDevices, incoming packet.
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:76
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:250
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:273
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:260
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
constexpr uint16_t IPV4
IPv4 EtherType [RFC894].
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643