A Discrete-Event Network Simulator
API
virtual-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,2009 INESC Porto
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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
19 */
20
21#include "ns3/log.h"
22#include "ns3/simulator.h"
23#include "ns3/mac48-address.h"
24#include "ns3/llc-snap-header.h"
25#include "ns3/error-model.h"
26#include "virtual-net-device.h"
27#include "ns3/channel.h"
28#include "ns3/trace-source-accessor.h"
29#include "ns3/uinteger.h"
30
31
32namespace ns3 {
33
34NS_LOG_COMPONENT_DEFINE ("VirtualNetDevice");
35
36NS_OBJECT_ENSURE_REGISTERED (VirtualNetDevice);
37
38TypeId
40{
41 static TypeId tid = TypeId ("ns3::VirtualNetDevice")
43 .SetGroupName ("VirtualNetDevice")
44 .AddConstructor<VirtualNetDevice> ()
45 .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
46 UintegerValue (1500),
49 MakeUintegerChecker<uint16_t> ())
50 .AddTraceSource ("MacTx",
51 "Trace source indicating a packet has arrived "
52 "for transmission by this device",
54 "ns3::Packet::TracedCallback")
55 .AddTraceSource ("MacPromiscRx",
56 "A packet has been received by this device, "
57 "has been passed up from the physical layer "
58 "and is being forwarded up the local protocol stack. "
59 "This is a promiscuous trace,",
61 "ns3::Packet::TracedCallback")
62 .AddTraceSource ("MacRx",
63 "A packet has been received by this device, "
64 "has been passed up from the physical layer "
65 "and is being forwarded up the local protocol stack. "
66 "This is a non-promiscuous trace,",
68 "ns3::Packet::TracedCallback")
69 //
70 // Trace sources designed to simulate a packet sniffer facility (tcpdump).
71 //
72 .AddTraceSource ("Sniffer",
73 "Trace source simulating a non-promiscuous "
74 "packet sniffer attached to the device",
76 "ns3::Packet::TracedCallback")
77 .AddTraceSource ("PromiscSniffer",
78 "Trace source simulating a promiscuous "
79 "packet sniffer attached to the device",
81 "ns3::Packet::TracedCallback")
82 ;
83 return tid;
84}
85
87{
88 m_needsArp = false;
89 m_supportsSendFrom = true;
90 m_isPointToPoint = true;
91}
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
125
127{
129}
130
131
133{
135 m_node = 0;
137}
138
139bool
140VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
141 const Address &source, const Address &destination,
142 PacketType packetType)
143{
144 //
145 // For all kinds of packetType we receive, we hit the promiscuous sniffer
146 // hook and pass a copy up to the promiscuous callback. Pass a copy to
147 // make sure that nobody messes with our packet.
148 //
149 m_promiscSnifferTrace (packet);
151 {
152 m_macPromiscRxTrace (packet);
153 m_promiscRxCallback (this, packet, protocol, source, destination, packetType);
154 }
155
156 //
157 // If this packet is not destined for some other host, it must be for us
158 // as either a broadcast, multicast or unicast. We need to hit the mac
159 // packet received trace hook and forward the packet up the stack.
160 //
161 if (packetType != PACKET_OTHERHOST)
162 {
163 m_snifferTrace (packet);
164 m_macRxTrace (packet);
165 return m_rxCallback (this, packet, protocol, source);
166 }
167 return true;
168}
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{
227 return Mac48Address ("ff:ff:ff:ff:ff:ff");
228}
229
230bool
232{
233 return false;
234}
235
237{
238 return Mac48Address ("ff:ff:ff:ff:ff:ff");
239}
240
242{
243 return Mac48Address ("ff:ff:ff:ff:ff:ff");
244}
245
246
247bool
249{
250 return m_isPointToPoint;
251}
252
253bool
254VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
255{
256 m_macTxTrace (packet);
257 if (m_sendCb (packet, GetAddress (), dest, protocolNumber))
258 {
259 return true;
260 }
261 return false;
262}
263
264bool
265VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
266{
268 m_macTxTrace (packet);
269 if (m_sendCb (packet, source, dest, protocolNumber))
270 {
271 return true;
272 }
273 return false;
274}
275
278{
279 return m_node;
280}
281
282void
284{
285 m_node = node;
286}
287
288bool
290{
291 return m_needsArp;
292}
293
294void
296{
297 m_rxCallback = cb;
298}
299
300void
302{
304}
305
306bool
308{
309 return m_supportsSendFrom;
310}
311
313{
314 return false;
315}
316
317
318} // 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
an EUI-48 address
Definition: mac48-address.h:44
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_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
A virtual device, similar to Linux TUN/TAP interfaces.
Ptr< Node > m_node
Pointer to the node.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
Promisc Sniffer trace.
virtual Ptr< Channel > GetChannel(void) const
uint32_t m_index
Device index.
bool SetMtu(const uint16_t mtu)
Configure the reported MTU for the virtual device.
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
TracedCallback< Ptr< const Packet > > m_macRxTrace
Rx trace.
void SetSupportsSendFrom(bool supportsSendFrom)
Configure whether the virtual device supports SendFrom.
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
virtual void SetAddress(Address address)
Set the address of this interface.
TracedCallback< Ptr< const Packet > > m_snifferTrace
Sniffer trace.
void SetNeedsArp(bool needsArp)
Configure whether the virtual device needs ARP.
virtual Ptr< Node > GetNode(void) const
virtual void SetIfIndex(const uint32_t index)
virtual Address GetBroadcast(void) const
bool m_needsArp
True if the device needs ARP.
virtual void DoDispose(void)
Destructor implementation.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Address GetAddress(void) const
static TypeId GetTypeId(void)
Get the type ID.
virtual uint16_t GetMtu(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
Promisc Rx trace.
virtual bool IsBroadcast(void) const
bool m_supportsSendFrom
True if the device supports SendFrm.
virtual bool SupportsSendFrom() const
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
void SetIsPointToPoint(bool isPointToPoint)
Configure whether the virtual device is point-to-point.
virtual bool IsLinkUp(void) const
virtual bool IsMulticast(void) const
Address m_myAddress
MAC address.
virtual uint32_t GetIfIndex(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
PromiscReceiveCallback m_promiscRxCallback
Promisc Rx callback.
ReceiveCallback m_rxCallback
Rx callback.
virtual bool NeedsArp(void) const
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
virtual void SetNode(Ptr< Node > node)
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.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:45
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.