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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
18 */
19
20#include "virtual-net-device.h"
21
22#include "ns3/channel.h"
23#include "ns3/error-model.h"
24#include "ns3/llc-snap-header.h"
25#include "ns3/log.h"
26#include "ns3/mac48-address.h"
27#include "ns3/simulator.h"
28#include "ns3/trace-source-accessor.h"
29#include "ns3/uinteger.h"
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("VirtualNetDevice");
35
36NS_OBJECT_ENSURE_REGISTERED(VirtualNetDevice);
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::VirtualNetDevice")
44 .SetGroupName("VirtualNetDevice")
45 .AddConstructor<VirtualNetDevice>()
46 .AddAttribute(
47 "Mtu",
48 "The MAC-level Maximum Transmission Unit",
49 UintegerValue(1500),
51 MakeUintegerChecker<uint16_t>())
52 .AddTraceSource("MacTx",
53 "Trace source indicating a packet has arrived "
54 "for transmission by this device",
56 "ns3::Packet::TracedCallback")
57 .AddTraceSource("MacPromiscRx",
58 "A packet has been received by this device, "
59 "has been passed up from the physical layer "
60 "and is being forwarded up the local protocol stack. "
61 "This is a promiscuous trace,",
63 "ns3::Packet::TracedCallback")
64 .AddTraceSource("MacRx",
65 "A packet has been received by this device, "
66 "has been passed up from the physical layer "
67 "and is being forwarded up the local protocol stack. "
68 "This is a non-promiscuous trace,",
70 "ns3::Packet::TracedCallback")
71 //
72 // Trace sources designed to simulate a packet sniffer facility (tcpdump).
73 //
74 .AddTraceSource("Sniffer",
75 "Trace source simulating a non-promiscuous "
76 "packet sniffer attached to the device",
78 "ns3::Packet::TracedCallback")
79 .AddTraceSource("PromiscSniffer",
80 "Trace source simulating a promiscuous "
81 "packet sniffer attached to the device",
83 "ns3::Packet::TracedCallback");
84 return tid;
85}
86
88{
89 m_needsArp = false;
90 m_supportsSendFrom = true;
91 m_isPointToPoint = true;
92}
93
94void
96{
97 m_sendCb = sendCb;
98}
99
100void
102{
103 m_needsArp = needsArp;
104}
105
106void
108{
109 m_supportsSendFrom = supportsSendFrom;
110}
111
112void
114{
115 m_isPointToPoint = isPointToPoint;
116}
117
118bool
119VirtualNetDevice::SetMtu(const uint16_t mtu)
120{
121 m_mtu = mtu;
122 return true;
123}
124
126{
128}
129
130void
132{
134 m_node = nullptr;
136}
137
138bool
140 uint16_t protocol,
141 const Address& source,
142 const Address& destination,
143 PacketType packetType)
144{
145 //
146 // For all kinds of packetType we receive, we hit the promiscuous sniffer
147 // hook and pass a copy up to the promiscuous callback. Pass a copy to
148 // make sure that nobody messes with our packet.
149 //
150 m_promiscSnifferTrace(packet);
152 {
153 m_macPromiscRxTrace(packet);
154 m_promiscRxCallback(this, packet, protocol, source, destination, packetType);
155 }
156
157 //
158 // If this packet is not destined for some other host, it must be for us
159 // as either a broadcast, multicast or unicast. We need to hit the mac
160 // packet received trace hook and forward the packet up the stack.
161 //
162 if (packetType != PACKET_OTHERHOST)
163 {
164 m_snifferTrace(packet);
165 m_macRxTrace(packet);
166 return m_rxCallback(this, packet, protocol, source);
167 }
168 return true;
169}
170
171void
173{
174 m_index = index;
175}
176
179{
180 return m_index;
181}
182
185{
186 return Ptr<Channel>();
187}
188
191{
192 return m_myAddress;
193}
194
195void
197{
198 m_myAddress = addr;
199}
200
201uint16_t
203{
204 return m_mtu;
205}
206
207bool
209{
210 return true;
211}
212
213void
215{
216}
217
218bool
220{
221 return true;
222}
223
226{
228}
229
230bool
232{
233 return false;
234}
235
238{
240}
241
244{
246}
247
248bool
250{
251 return m_isPointToPoint;
252}
253
254bool
255VirtualNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
256{
257 m_macTxTrace(packet);
258 return m_sendCb(packet, GetAddress(), dest, protocolNumber);
259}
260
261bool
263 const Address& source,
264 const Address& dest,
265 uint16_t protocolNumber)
266{
268 m_macTxTrace(packet);
269 return m_sendCb(packet, source, dest, protocolNumber);
270}
271
274{
275 return m_node;
276}
277
278void
280{
281 m_node = node;
282}
283
284bool
286{
287 return m_needsArp;
288}
289
290void
292{
293 m_rxCallback = cb;
294}
295
296void
298{
300}
301
302bool
304{
305 return m_supportsSendFrom;
306}
307
308bool
310{
311 return false;
312}
313
314} // namespace ns3
a polymophic address class
Definition: address.h:101
bool IsNull() const
Check for null implementation.
Definition: callback.h:571
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Mac48Address GetBroadcast()
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Hold an unsigned integer type.
Definition: uinteger.h:45
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:66
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:46
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.