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/queue.h"
23 #include "ns3/simulator.h"
24 #include "ns3/mac48-address.h"
25 #include "ns3/llc-snap-header.h"
26 #include "ns3/error-model.h"
27 #include "virtual-net-device.h"
28 #include "ns3/channel.h"
29 #include "ns3/trace-source-accessor.h"
30 #include "ns3/uinteger.h"
31 
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("VirtualNetDevice");
36 
37 NS_OBJECT_ENSURE_REGISTERED (VirtualNetDevice);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::VirtualNetDevice")
43  .SetParent<NetDevice> ()
44  .SetGroupName ("VirtualNetDevice")
45  .AddConstructor<VirtualNetDevice> ()
46  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
47  UintegerValue (1500),
50  MakeUintegerChecker<uint16_t> ())
51  .AddTraceSource ("MacTx",
52  "Trace source indicating a packet has arrived "
53  "for transmission by this device",
55  "ns3::Packet::TracedCallback")
56  .AddTraceSource ("MacPromiscRx",
57  "A packet has been received by this device, "
58  "has been passed up from the physical layer "
59  "and is being forwarded up the local protocol stack. "
60  "This is a promiscuous trace,",
62  "ns3::Packet::TracedCallback")
63  .AddTraceSource ("MacRx",
64  "A packet has been received by this device, "
65  "has been passed up from the physical layer "
66  "and is being forwarded up the local protocol stack. "
67  "This is a non-promiscuous trace,",
69  "ns3::Packet::TracedCallback")
70  //
71  // Trace sources designed to simulate a packet sniffer facility (tcpdump).
72  //
73  .AddTraceSource ("Sniffer",
74  "Trace source simulating a non-promiscuous "
75  "packet sniffer attached to the device",
77  "ns3::Packet::TracedCallback")
78  .AddTraceSource ("PromiscSniffer",
79  "Trace source simulating a promiscuous "
80  "packet sniffer attached to the device",
82  "ns3::Packet::TracedCallback")
83  ;
84  return tid;
85 }
86 
88 {
89  m_needsArp = false;
90  m_supportsSendFrom = true;
91  m_isPointToPoint = true;
92 }
93 
94 
95 void
97 {
98  m_sendCb = sendCb;
99 }
100 
101 void
103 {
104  m_needsArp = needsArp;
105 }
106 
107 void
109 {
110  m_supportsSendFrom = supportsSendFrom;
111 }
112 
113 void
115 {
116  m_isPointToPoint = isPointToPoint;
117 }
118 
119 bool
120 VirtualNetDevice::SetMtu (const uint16_t mtu)
121 {
122  m_mtu = mtu;
123  return true;
124 }
125 
126 
128 {
130 }
131 
132 
134 {
136  m_node = 0;
138 }
139 
140 bool
141 VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
142  const Address &source, 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);
151  if (!m_promiscRxCallback.IsNull ())
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 
171 
172 void
173 VirtualNetDevice::SetIfIndex (const uint32_t index)
174 {
175  m_index = index;
176 }
177 
178 uint32_t
180 {
181  return m_index;
182 }
183 
186 {
187  return Ptr<Channel> ();
188 }
189 
190 Address
192 {
193  return m_myAddress;
194 }
195 
196 void
198 {
199  m_myAddress = addr;
200 }
201 
202 uint16_t
204 {
205  return m_mtu;
206 }
207 
208 bool
210 {
211  return true;
212 }
213 
214 void
216 {
217 }
218 
219 bool
221 {
222  return true;
223 }
224 
225 Address
227 {
228  return Mac48Address ("ff:ff:ff:ff:ff:ff");
229 }
230 
231 bool
233 {
234  return false;
235 }
236 
238 {
239  return Mac48Address ("ff:ff:ff:ff:ff:ff");
240 }
241 
243 {
244  return Mac48Address ("ff:ff:ff:ff:ff:ff");
245 }
246 
247 
248 bool
250 {
251  return m_isPointToPoint;
252 }
253 
254 bool
255 VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
256 {
257  m_macTxTrace (packet);
258  if (m_sendCb (packet, GetAddress (), dest, protocolNumber))
259  {
260  return true;
261  }
262  return false;
263 }
264 
265 bool
266 VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
267 {
269  m_macTxTrace (packet);
270  if (m_sendCb (packet, source, dest, protocolNumber))
271  {
272  return true;
273  }
274  return false;
275 }
276 
277 Ptr<Node>
279 {
280  return m_node;
281 }
282 
283 void
285 {
286  m_node = node;
287 }
288 
289 bool
291 {
292  return m_needsArp;
293 }
294 
295 void
297 {
298  m_rxCallback = cb;
299 }
300 
301 void
303 {
304  m_promiscRxCallback = cb;
305 }
306 
307 bool
309 {
310  return m_supportsSendFrom;
311 }
312 
313 bool VirtualNetDevice::IsBridge (void) const
314 {
315  return false;
316 }
317 
318 
319 } // namespace ns3
void SetNeedsArp(bool needsArp)
Configure whether the virtual device needs ARP.
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual bool SupportsSendFrom() const
virtual void DoDispose(void)
Destructor implementation.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
#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
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:606
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool SetMtu(const uint16_t mtu)
Configure the reported MTU for the virtual device.
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual bool NeedsArp(void) const
a polymophic address class
Definition: address.h:90
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
virtual bool IsBroadcast(void) const
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
TracedCallback< Ptr< const Packet > > m_snifferTrace
virtual Address GetAddress(void) const
virtual uint16_t GetMtu(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool IsMulticast(void) const
Hold an unsigned integer type.
Definition: uinteger.h:44
virtual Ptr< Channel > GetChannel(void) const
virtual void SetAddress(Address address)
Set the address of this interface.
virtual void SetIfIndex(const uint32_t index)
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
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.
void SetSupportsSendFrom(bool supportsSendFrom)
Configure whether the virtual device supports SendFrom.
virtual void SetNode(Ptr< Node > node)
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Packet addressed to someone else.
Definition: net-device.h:614
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
an EUI-48 address
Definition: mac48-address.h:43
PromiscReceiveCallback m_promiscRxCallback
TracedCallback< Ptr< const Packet > > m_macRxTrace
virtual void AddLinkChangeCallback(Callback< void > callback)
TracedCallback< Ptr< const Packet > > m_macTxTrace
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Ptr< Node > GetNode(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:48
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Network layer to device interface.
Definition: net-device.h:405
A virtual device, similar to Linux TUN/TAP interfaces.
ReceiveCallback m_rxCallback
virtual uint32_t GetIfIndex(void) const
static TypeId GetTypeId(void)
virtual bool IsLinkUp(void) const
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
virtual Address GetBroadcast(void) const
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:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904