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 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("VirtualNetDevice");
35 
36 NS_OBJECT_ENSURE_REGISTERED (VirtualNetDevice);
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::VirtualNetDevice")
42  .SetParent<NetDevice> ()
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 
94 void
96 {
97  m_sendCb = sendCb;
98 }
99 
100 void
102 {
103  m_needsArp = needsArp;
104 }
105 
106 void
108 {
109  m_supportsSendFrom = supportsSendFrom;
110 }
111 
112 void
114 {
115  m_isPointToPoint = isPointToPoint;
116 }
117 
118 bool
119 VirtualNetDevice::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 
139 bool
140 VirtualNetDevice::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);
150  if (!m_promiscRxCallback.IsNull ())
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 
171 void
172 VirtualNetDevice::SetIfIndex (const uint32_t index)
173 {
174  m_index = index;
175 }
176 
177 uint32_t
179 {
180  return m_index;
181 }
182 
185 {
186  return Ptr<Channel> ();
187 }
188 
189 Address
191 {
192  return m_myAddress;
193 }
194 
195 void
197 {
198  m_myAddress = addr;
199 }
200 
201 uint16_t
203 {
204  return m_mtu;
205 }
206 
207 bool
209 {
210  return true;
211 }
212 
213 void
215 {
216 }
217 
218 bool
220 {
221  return true;
222 }
223 
224 Address
226 {
227  return Mac48Address ("ff:ff:ff:ff:ff:ff");
228 }
229 
230 bool
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 
247 bool
249 {
250  return m_isPointToPoint;
251 }
252 
253 bool
254 VirtualNetDevice::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 
264 bool
265 VirtualNetDevice::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 
276 Ptr<Node>
278 {
279  return m_node;
280 }
281 
282 void
284 {
285  m_node = node;
286 }
287 
288 bool
290 {
291  return m_needsArp;
292 }
293 
294 void
296 {
297  m_rxCallback = cb;
298 }
299 
300 void
302 {
303  m_promiscRxCallback = cb;
304 }
305 
306 bool
308 {
309  return m_supportsSendFrom;
310 }
311 
312 bool VirtualNetDevice::IsBridge (void) const
313 {
314  return false;
315 }
316 
317 
318 } // 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:45
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:296
#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:304
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:95
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:914