A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
virtual-net-device.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008,2009 INESC Porto
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
7 */
8
10
11#include "ns3/channel.h"
12#include "ns3/error-model.h"
13#include "ns3/llc-snap-header.h"
14#include "ns3/log.h"
15#include "ns3/mac48-address.h"
16#include "ns3/simulator.h"
17#include "ns3/trace-source-accessor.h"
18#include "ns3/uinteger.h"
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("VirtualNetDevice");
24
25NS_OBJECT_ENSURE_REGISTERED(VirtualNetDevice);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::VirtualNetDevice")
33 .SetGroupName("VirtualNetDevice")
34 .AddConstructor<VirtualNetDevice>()
35 .AddAttribute(
36 "Mtu",
37 "The MAC-level Maximum Transmission Unit",
38 UintegerValue(1500),
41 .AddTraceSource("MacTx",
42 "Trace source indicating a packet has arrived "
43 "for transmission by this device",
45 "ns3::Packet::TracedCallback")
46 .AddTraceSource("MacPromiscRx",
47 "A packet has been received by this device, "
48 "has been passed up from the physical layer "
49 "and is being forwarded up the local protocol stack. "
50 "This is a promiscuous trace,",
52 "ns3::Packet::TracedCallback")
53 .AddTraceSource("MacRx",
54 "A packet has been received by this device, "
55 "has been passed up from the physical layer "
56 "and is being forwarded up the local protocol stack. "
57 "This is a non-promiscuous trace,",
59 "ns3::Packet::TracedCallback")
60 //
61 // Trace sources designed to simulate a packet sniffer facility (tcpdump).
62 //
63 .AddTraceSource("Sniffer",
64 "Trace source simulating a non-promiscuous "
65 "packet sniffer attached to the device",
67 "ns3::Packet::TracedCallback")
68 .AddTraceSource("PromiscSniffer",
69 "Trace source simulating a promiscuous "
70 "packet sniffer attached to the device",
72 "ns3::Packet::TracedCallback");
73 return tid;
74}
75
82
83void
88
89void
91{
92 m_needsArp = needsArp;
93}
94
95void
97{
98 m_supportsSendFrom = supportsSendFrom;
99}
100
101void
103{
104 m_isPointToPoint = isPointToPoint;
105}
106
107bool
108VirtualNetDevice::SetMtu(const uint16_t mtu)
109{
110 m_mtu = mtu;
111 return true;
112}
113
118
119void
126
127bool
129 uint16_t protocol,
130 const Address& source,
131 const Address& destination,
132 PacketType packetType)
133{
134 //
135 // For all kinds of packetType we receive, we hit the promiscuous sniffer
136 // hook and pass a copy up to the promiscuous callback. Pass a copy to
137 // make sure that nobody messes with our packet.
138 //
139 m_promiscSnifferTrace(packet);
141 {
142 m_macPromiscRxTrace(packet);
143 m_promiscRxCallback(this, packet, protocol, source, destination, packetType);
144 }
145
146 //
147 // If this packet is not destined for some other host, it must be for us
148 // as either a broadcast, multicast or unicast. We need to hit the mac
149 // packet received trace hook and forward the packet up the stack.
150 //
151 if (packetType != PACKET_OTHERHOST)
152 {
153 m_snifferTrace(packet);
154 m_macRxTrace(packet);
155 return m_rxCallback(this, packet, protocol, source);
156 }
157 return true;
158}
159
160void
162{
163 m_index = index;
164}
165
168{
169 return m_index;
170}
171
174{
175 return Ptr<Channel>();
176}
177
180{
181 return m_myAddress;
182}
183
184void
189
190uint16_t
192{
193 return m_mtu;
194}
195
196bool
198{
199 return true;
200}
201
202void
206
207bool
209{
210 return true;
211}
212
218
219bool
221{
222 return false;
223}
224
227{
229}
230
236
237bool
242
243bool
244VirtualNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
245{
246 m_macTxTrace(packet);
247 return m_sendCb(packet, GetAddress(), dest, protocolNumber);
248}
249
250bool
252 const Address& source,
253 const Address& dest,
254 uint16_t protocolNumber)
255{
257 m_macTxTrace(packet);
258 return m_sendCb(packet, source, dest, protocolNumber);
259}
260
263{
264 return m_node;
265}
266
267void
269{
270 m_node = node;
271}
272
273bool
275{
276 return m_needsArp;
277}
278
279void
284
285void
290
291bool
296
297bool
299{
300 return false;
301}
302
303} // namespace ns3
a polymophic address class
Definition address.h:90
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 Mac48Address GetBroadcast()
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_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.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
A virtual device, similar to Linux TUN/TAP interfaces.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
static TypeId GetTypeId()
Get the type ID.
Ptr< Node > m_node
Pointer to the node.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
Promisc Sniffer trace.
uint32_t GetIfIndex() const override
bool SetMtu(const uint16_t mtu) override
Configure the reported MTU for the virtual device.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
Address GetAddress() const override
uint32_t m_index
Device index.
bool SupportsSendFrom() const override
bool IsMulticast() const override
bool IsLinkUp() const override
void SetAddress(Address address) override
Set the address of this interface.
void AddLinkChangeCallback(Callback< void > callback) override
TracedCallback< Ptr< const Packet > > m_macRxTrace
Rx trace.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
void SetSupportsSendFrom(bool supportsSendFrom)
Configure whether the virtual device supports SendFrom.
void SetNode(Ptr< Node > node) override
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
TracedCallback< Ptr< const Packet > > m_snifferTrace
Sniffer trace.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void SetNeedsArp(bool needsArp)
Configure whether the virtual device needs ARP.
bool NeedsArp() const override
void DoDispose() override
Destructor implementation.
Ptr< Channel > GetChannel() const override
bool m_needsArp
True if the device needs ARP.
bool IsBroadcast() const override
void SetIfIndex(const uint32_t index) override
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
Promisc Rx trace.
bool m_supportsSendFrom
True if the device supports SendFrm.
void SetIsPointToPoint(bool isPointToPoint)
Configure whether the virtual device is point-to-point.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Ptr< Node > GetNode() const override
Address GetBroadcast() const override
Address m_myAddress
MAC address.
uint16_t GetMtu() const override
PromiscReceiveCallback m_promiscRxCallback
Promisc Rx callback.
ReceiveCallback m_rxCallback
Rx callback.
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool m_isPointToPoint
True if the device is a PointToPoint type device.
SendCallback m_sendCb
send callback
TracedCallback< Ptr< const Packet > > m_macTxTrace
Tx trace.
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
#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
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition uinteger.h:35
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.