A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("VirtualNetDevice");
34 
35 namespace ns3 {
36 
37 NS_OBJECT_ENSURE_REGISTERED (VirtualNetDevice)
38  ;
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::VirtualNetDevice")
44  .SetParent<NetDevice> ()
45  .AddConstructor<VirtualNetDevice> ()
46  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
47  UintegerValue (1500),
48  MakeUintegerAccessor (&VirtualNetDevice::SetMtu,
50  MakeUintegerChecker<uint16_t> ())
51  .AddTraceSource ("MacTx",
52  "Trace source indicating a packet has arrived for transmission by this device",
54  .AddTraceSource ("MacPromiscRx",
55  "A packet has been received by this device, has been passed up from the physical layer "
56  "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
58  .AddTraceSource ("MacRx",
59  "A packet has been received by this device, has been passed up from the physical layer "
60  "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
62  //
63  // Trace sources designed to simulate a packet sniffer facility (tcpdump).
64  //
65  .AddTraceSource ("Sniffer",
66  "Trace source simulating a non-promiscuous packet sniffer attached to the device",
68  .AddTraceSource ("PromiscSniffer",
69  "Trace source simulating a promiscuous packet sniffer attached to the device",
71  ;
72  return tid;
73 }
74 
76 {
77  m_needsArp = false;
78  m_supportsSendFrom = true;
79  m_isPointToPoint = true;
80 }
81 
82 
83 void
85 {
86  m_sendCb = sendCb;
87 }
88 
89 void
91 {
92  m_needsArp = needsArp;
93 }
94 
95 void
97 {
98  m_supportsSendFrom = supportsSendFrom;
99 }
100 
101 void
103 {
104  m_isPointToPoint = isPointToPoint;
105 }
106 
107 bool
108 VirtualNetDevice::SetMtu (const uint16_t mtu)
109 {
110  m_mtu = mtu;
111  return true;
112 }
113 
114 
116 {
118 }
119 
120 
122 {
124  m_node = 0;
126 }
127 
128 bool
129 VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
130  const Address &source, const Address &destination,
131  PacketType packetType)
132 {
133  //
134  // For all kinds of packetType we receive, we hit the promiscuous sniffer
135  // hook and pass a copy up to the promiscuous callback. Pass a copy to
136  // make sure that nobody messes with our packet.
137  //
138  m_promiscSnifferTrace (packet);
139  if (!m_promiscRxCallback.IsNull ())
140  {
141  m_macPromiscRxTrace (packet);
142  m_promiscRxCallback (this, packet, protocol, source, destination, packetType);
143  }
144 
145  //
146  // If this packet is not destined for some other host, it must be for us
147  // as either a broadcast, multicast or unicast. We need to hit the mac
148  // packet received trace hook and forward the packet up the stack.
149  //
150  if (packetType != PACKET_OTHERHOST)
151  {
152  m_snifferTrace (packet);
153  m_macRxTrace (packet);
154  return m_rxCallback (this, packet, protocol, source);
155  }
156  return true;
157 }
158 
159 
160 void
161 VirtualNetDevice::SetIfIndex (const uint32_t index)
162 {
163  m_index = index;
164 }
165 
166 uint32_t
168 {
169  return m_index;
170 }
171 
174 {
175  return Ptr<Channel> ();
176 }
177 
178 Address
180 {
181  return m_myAddress;
182 }
183 
184 void
186 {
187  m_myAddress = addr;
188 }
189 
190 uint16_t
192 {
193  return m_mtu;
194 }
195 
196 bool
198 {
199  return true;
200 }
201 
202 void
204 {
205 }
206 
207 bool
209 {
210  return true;
211 }
212 
213 Address
215 {
216  return Mac48Address ("ff:ff:ff:ff:ff:ff");
217 }
218 
219 bool
221 {
222  return false;
223 }
224 
226 {
227  return Mac48Address ("ff:ff:ff:ff:ff:ff");
228 }
229 
231 {
232  return Mac48Address ("ff:ff:ff:ff:ff:ff");
233 }
234 
235 
236 bool
238 {
239  return m_isPointToPoint;
240 }
241 
242 bool
243 VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
244 {
245  m_macTxTrace (packet);
246  if (m_sendCb (packet, GetAddress (), dest, protocolNumber))
247  {
248  return true;
249  }
250  return false;
251 }
252 
253 bool
254 VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
255 {
257  m_macTxTrace (packet);
258  if (m_sendCb (packet, source, dest, protocolNumber))
259  {
260  return true;
261  }
262  return false;
263 }
264 
265 Ptr<Node>
267 {
268  return m_node;
269 }
270 
271 void
273 {
274  m_node = node;
275 }
276 
277 bool
279 {
280  return m_needsArp;
281 }
282 
283 void
285 {
286  m_rxCallback = cb;
287 }
288 
289 void
291 {
292  m_promiscRxCallback = cb;
293 }
294 
295 bool
297 {
298  return m_supportsSendFrom;
299 }
300 
301 bool VirtualNetDevice::IsBridge (void) const
302 {
303  return false;
304 }
305 
306 
307 } // 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.
Packet addressed to someone else.
Definition: net-device.h:278
virtual bool SupportsSendFrom() const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1014
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
bool SetMtu(const uint16_t mtu)
Configure the reported MTU for the virtual device.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
virtual bool NeedsArp(void) const
a polymophic address class
Definition: address.h:86
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
virtual bool IsBroadcast(void) const
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:46
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)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
an EUI-48 address
Definition: mac48-address.h:41
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:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Network layer to device interface.
Definition: net-device.h:75
ReceiveCallback m_rxCallback
virtual uint32_t GetIfIndex(void) const
static TypeId GetTypeId(void)
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
virtual bool IsLinkUp(void) const
NS_LOG_COMPONENT_DEFINE("VirtualNetDeviceExample")
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
virtual Address GetBroadcast(void) const
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611