A Discrete-Event Network Simulator
API
virtual-net-device.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008,2009 INESC Porto
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
18  */
19 
20 #include "virtual-net-device.h"
21 
22 #include "ns3/channel.h"
23 #include "ns3/error-model.h"
24 #include "ns3/llc-snap-header.h"
25 #include "ns3/log.h"
26 #include "ns3/mac48-address.h"
27 #include "ns3/simulator.h"
28 #include "ns3/trace-source-accessor.h"
29 #include "ns3/uinteger.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("VirtualNetDevice");
35 
36 NS_OBJECT_ENSURE_REGISTERED(VirtualNetDevice);
37 
38 TypeId
40 {
41  static TypeId tid =
42  TypeId("ns3::VirtualNetDevice")
44  .SetGroupName("VirtualNetDevice")
45  .AddConstructor<VirtualNetDevice>()
46  .AddAttribute(
47  "Mtu",
48  "The MAC-level Maximum Transmission Unit",
49  UintegerValue(1500),
51  MakeUintegerChecker<uint16_t>())
52  .AddTraceSource("MacTx",
53  "Trace source indicating a packet has arrived "
54  "for transmission by this device",
56  "ns3::Packet::TracedCallback")
57  .AddTraceSource("MacPromiscRx",
58  "A packet has been received by this device, "
59  "has been passed up from the physical layer "
60  "and is being forwarded up the local protocol stack. "
61  "This is a promiscuous trace,",
63  "ns3::Packet::TracedCallback")
64  .AddTraceSource("MacRx",
65  "A packet has been received by this device, "
66  "has been passed up from the physical layer "
67  "and is being forwarded up the local protocol stack. "
68  "This is a non-promiscuous trace,",
70  "ns3::Packet::TracedCallback")
71  //
72  // Trace sources designed to simulate a packet sniffer facility (tcpdump).
73  //
74  .AddTraceSource("Sniffer",
75  "Trace source simulating a non-promiscuous "
76  "packet sniffer attached to the device",
78  "ns3::Packet::TracedCallback")
79  .AddTraceSource("PromiscSniffer",
80  "Trace source simulating a promiscuous "
81  "packet sniffer attached to the device",
83  "ns3::Packet::TracedCallback");
84  return tid;
85 }
86 
88 {
89  m_needsArp = false;
90  m_supportsSendFrom = true;
91  m_isPointToPoint = true;
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 
126 {
128 }
129 
130 void
132 {
134  m_node = nullptr;
136 }
137 
138 bool
140  uint16_t protocol,
141  const Address& source,
142  const Address& destination,
143  PacketType packetType)
144 {
145  //
146  // Check if this device is configure as an OpenFlow switch port.
147  //
149  {
150  // For all kinds of packetType we receive, we hit the promiscuous sniffer
151  // hook. If the packet is addressed to this device (which is not supposed
152  // to happen in normal situations), we also hit the non-promiscuous
153  // sniffer hook, but in both cases we don't forward the packet up the
154  // stack.
155  m_promiscSnifferTrace(packet);
156  if (packetType != PACKET_OTHERHOST)
157  {
158  m_snifferTrace(packet);
159  }
160 
161  // We then forward the original packet to the OpenFlow receive callback
162  // for all kinds of packetType we receive (broadcast, multicast, host or
163  // other host).
164  m_openFlowRxCallback(this, packet, protocol, source, destination, packetType);
165  return true;
166  }
167 
168  //
169  // For all kinds of packetType we receive, we hit the promiscuous sniffer
170  // hook and pass a copy up to the promiscuous callback. Pass a copy to
171  // make sure that nobody messes with our packet.
172  //
173  m_promiscSnifferTrace(packet);
175  {
176  m_macPromiscRxTrace(packet);
177  m_promiscRxCallback(this, packet, protocol, source, destination, packetType);
178  }
179 
180  //
181  // If this packet is not destined for some other host, it must be for us
182  // as either a broadcast, multicast or unicast. We need to hit the mac
183  // packet received trace hook and forward the packet up the stack.
184  //
185  if (packetType != PACKET_OTHERHOST)
186  {
187  m_snifferTrace(packet);
188  m_macRxTrace(packet);
189  return m_rxCallback(this, packet, protocol, source);
190  }
191  return true;
192 }
193 
194 void
195 VirtualNetDevice::SetIfIndex(const uint32_t index)
196 {
197  m_index = index;
198 }
199 
200 uint32_t
202 {
203  return m_index;
204 }
205 
208 {
209  return Ptr<Channel>();
210 }
211 
212 Address
214 {
215  return m_myAddress;
216 }
217 
218 void
220 {
221  m_myAddress = addr;
222 }
223 
224 uint16_t
226 {
227  return m_mtu;
228 }
229 
230 bool
232 {
233  return true;
234 }
235 
236 void
238 {
239 }
240 
241 bool
243 {
244  return true;
245 }
246 
247 Address
249 {
250  return Mac48Address("ff:ff:ff:ff:ff:ff");
251 }
252 
253 bool
255 {
256  return false;
257 }
258 
259 Address
261 {
262  return Mac48Address("ff:ff:ff:ff:ff:ff");
263 }
264 
265 Address
267 {
268  return Mac48Address("ff:ff:ff:ff:ff:ff");
269 }
270 
271 bool
273 {
274  return m_isPointToPoint;
275 }
276 
277 bool
278 VirtualNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
279 {
280  m_macTxTrace(packet);
281  if (m_sendCb(packet, GetAddress(), dest, protocolNumber))
282  {
283  return true;
284  }
285  return false;
286 }
287 
288 bool
290  const Address& source,
291  const Address& dest,
292  uint16_t protocolNumber)
293 {
295  m_macTxTrace(packet);
296  if (m_sendCb(packet, source, dest, protocolNumber))
297  {
298  return true;
299  }
300  return false;
301 }
302 
303 Ptr<Node>
305 {
306  return m_node;
307 }
308 
309 void
311 {
312  m_node = node;
313 }
314 
315 bool
317 {
318  return m_needsArp;
319 }
320 
321 void
323 {
324  m_rxCallback = cb;
325 }
326 
327 void
329 {
330  m_promiscRxCallback = cb;
331 }
332 
333 bool
335 {
336  return m_supportsSendFrom;
337 }
338 
339 bool
341 {
342  return false;
343 }
344 
345 void
347 {
348  NS_LOG_FUNCTION(&cb);
350 }
351 
352 } // namespace ns3
a polymophic address class
Definition: address.h:100
bool IsNull() const
Check for null implementation.
Definition: callback.h:572
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Describes an IPv6 address.
Definition: ipv6-address.h:50
an EUI-48 address
Definition: mac48-address.h:46
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Hold an unsigned integer type.
Definition: uinteger.h:45
A virtual device, similar to Linux TUN/TAP interfaces.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
static TypeId GetTypeId()
Get the type ID.
Ptr< Node > m_node
Pointer to the node.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
Promisc Sniffer trace.
uint32_t GetIfIndex() const override
bool SetMtu(const uint16_t mtu) override
Configure the reported MTU for the virtual device.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
Address GetAddress() const override
uint32_t m_index
Device index.
bool SupportsSendFrom() const override
bool IsMulticast() const override
bool IsLinkUp() const override
void SetAddress(Address address) override
Set the address of this interface.
void AddLinkChangeCallback(Callback< void > callback) override
TracedCallback< Ptr< const Packet > > m_macRxTrace
Rx trace.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
void SetSupportsSendFrom(bool supportsSendFrom)
Configure whether the virtual device supports SendFrom.
void SetNode(Ptr< Node > node) override
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
NetDevice::PromiscReceiveCallback m_openFlowRxCallback
The OpenFlow receive callback.
TracedCallback< Ptr< const Packet > > m_snifferTrace
Sniffer trace.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void SetNeedsArp(bool needsArp)
Configure whether the virtual device needs ARP.
bool NeedsArp() const override
void DoDispose() override
Destructor implementation.
Ptr< Channel > GetChannel() const override
bool m_needsArp
True if the device needs ARP.
bool IsBroadcast() const override
void SetIfIndex(const uint32_t index) override
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
Promisc Rx trace.
void SetOpenFlowReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Set the callback used to notify the OpenFlow when a packet has been received by this device.
bool m_supportsSendFrom
True if the device supports SendFrm.
void SetIsPointToPoint(bool isPointToPoint)
Configure whether the virtual device is point-to-point.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Ptr< Node > GetNode() const override
Address GetBroadcast() const override
Address m_myAddress
MAC address.
uint16_t GetMtu() const override
PromiscReceiveCallback m_promiscRxCallback
Promisc Rx callback.
ReceiveCallback m_rxCallback
Rx callback.
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
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.
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
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:46
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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.