A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
virtual-net-device.cc
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 TypeId
40 VirtualNetDevice::GetTypeId (void)
41 {
42  static TypeId tid = TypeId ("ns3::VirtualNetDevice")
43  .SetParent<NetDevice> ()
44  .AddConstructor<VirtualNetDevice> ()
45  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
46  UintegerValue (1500),
47  MakeUintegerAccessor (&VirtualNetDevice::SetMtu,
49  MakeUintegerChecker<uint16_t> ())
50  .AddTraceSource ("MacTx",
51  "Trace source indicating a packet has arrived for transmission by this device",
52  MakeTraceSourceAccessor (&VirtualNetDevice::m_macTxTrace))
53  .AddTraceSource ("MacPromiscRx",
54  "A packet has been received by this device, has been passed up from the physical layer "
55  "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
56  MakeTraceSourceAccessor (&VirtualNetDevice::m_macPromiscRxTrace))
57  .AddTraceSource ("MacRx",
58  "A packet has been received by this device, has been passed up from the physical layer "
59  "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
60  MakeTraceSourceAccessor (&VirtualNetDevice::m_macRxTrace))
61  //
62  // Trace sources designed to simulate a packet sniffer facility (tcpdump).
63  //
64  .AddTraceSource ("Sniffer",
65  "Trace source simulating a non-promiscuous packet sniffer attached to the device",
66  MakeTraceSourceAccessor (&VirtualNetDevice::m_snifferTrace))
67  .AddTraceSource ("PromiscSniffer",
68  "Trace source simulating a promiscuous packet sniffer attached to the device",
69  MakeTraceSourceAccessor (&VirtualNetDevice::m_promiscSnifferTrace))
70  ;
71  return tid;
72 }
73 
74 VirtualNetDevice::VirtualNetDevice ()
75 {
76  m_needsArp = false;
77  m_supportsSendFrom = true;
78  m_isPointToPoint = true;
79 }
80 
81 
82 void
84 {
85  m_sendCb = sendCb;
86 }
87 
88 void
90 {
91  m_needsArp = needsArp;
92 }
93 
94 void
96 {
97  m_supportsSendFrom = supportsSendFrom;
98 }
99 
100 void
102 {
103  m_isPointToPoint = isPointToPoint;
104 }
105 
106 bool
107 VirtualNetDevice::SetMtu (const uint16_t mtu)
108 {
109  m_mtu = mtu;
110  return true;
111 }
112 
113 
114 VirtualNetDevice::~VirtualNetDevice()
115 {
117 }
118 
119 
121 {
123  m_node = 0;
125 }
126 
127 bool
128 VirtualNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
129  const Address &source, const Address &destination,
130  PacketType packetType)
131 {
132  //
133  // For all kinds of packetType we receive, we hit the promiscuous sniffer
134  // hook and pass a copy up to the promiscuous callback. Pass a copy to
135  // make sure that nobody messes with our packet.
136  //
137  m_promiscSnifferTrace (packet);
138  if (!m_promiscRxCallback.IsNull ())
139  {
140  m_macPromiscRxTrace (packet);
141  m_promiscRxCallback (this, packet, protocol, source, destination, packetType);
142  }
143 
144  //
145  // If this packet is not destined for some other host, it must be for us
146  // as either a broadcast, multicast or unicast. We need to hit the mac
147  // packet received trace hook and forward the packet up the stack.
148  //
149  if (packetType != PACKET_OTHERHOST)
150  {
151  m_snifferTrace (packet);
152  m_macRxTrace (packet);
153  return m_rxCallback (this, packet, protocol, source);
154  }
155  return true;
156 }
157 
158 
159 void
160 VirtualNetDevice::SetIfIndex (const uint32_t index)
161 {
162  m_index = index;
163 }
164 
165 uint32_t
167 {
168  return m_index;
169 }
170 
173 {
174  return Ptr<Channel> ();
175 }
176 
177 Address
179 {
180  return m_myAddress;
181 }
182 
183 void
185 {
186  m_myAddress = addr;
187 }
188 
189 uint16_t
191 {
192  return m_mtu;
193 }
194 
195 bool
197 {
198  return true;
199 }
200 
201 void
203 {
204 }
205 
206 bool
208 {
209  return true;
210 }
211 
212 Address
214 {
215  return Mac48Address ("ff:ff:ff:ff:ff:ff");
216 }
217 
218 bool
220 {
221  return false;
222 }
223 
225 {
226  return Mac48Address ("ff:ff:ff:ff:ff:ff");
227 }
228 
230 {
231  return Mac48Address ("ff:ff:ff:ff:ff:ff");
232 }
233 
234 
235 bool
237 {
238  return m_isPointToPoint;
239 }
240 
241 bool
242 VirtualNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
243 {
244  m_macTxTrace (packet);
245  if (m_sendCb (packet, GetAddress (), dest, protocolNumber))
246  {
247  return true;
248  }
249  return false;
250 }
251 
252 bool
253 VirtualNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
254 {
255  NS_ASSERT (m_supportsSendFrom);
256  m_macTxTrace (packet);
257  if (m_sendCb (packet, source, dest, protocolNumber))
258  {
259  return true;
260  }
261  return false;
262 }
263 
264 Ptr<Node>
266 {
267  return m_node;
268 }
269 
270 void
272 {
273  m_node = node;
274 }
275 
276 bool
278 {
279  return m_needsArp;
280 }
281 
282 void
284 {
285  m_rxCallback = cb;
286 }
287 
288 void
290 {
291  m_promiscRxCallback = cb;
292 }
293 
294 bool
296 {
297  return m_supportsSendFrom;
298 }
299 
300 bool VirtualNetDevice::IsBridge (void) const
301 {
302  return false;
303 }
304 
305 
306 } // 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.
virtual bool SupportsSendFrom() const
virtual void DoDispose(void)
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
bool SetMtu(const uint16_t mtu)
Configure the reported MTU for the virtual device.
virtual void DoDispose(void)
Definition: object.cc:335
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
virtual bool NeedsArp(void) const
a polymophic address class
Definition: address.h:86
virtual bool IsBroadcast(void) const
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
virtual Address GetAddress(void) const
virtual uint16_t GetMtu(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool IsMulticast(void) const
virtual Ptr< Channel > GetChannel(void) const
virtual void SetAddress(Address address)
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)
an EUI-48 address
Definition: mac48-address.h:41
virtual void AddLinkChangeCallback(Callback< void > callback)
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:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
virtual uint32_t GetIfIndex(void) const
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