A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
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
,
48
&
VirtualNetDevice::GetMtu
),
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
83
VirtualNetDevice::SetSendCallback
(
SendCallback
sendCb)
84
{
85
m_sendCb
= sendCb;
86
}
87
88
void
89
VirtualNetDevice::SetNeedsArp
(
bool
needsArp)
90
{
91
m_needsArp
= needsArp;
92
}
93
94
void
95
VirtualNetDevice::SetSupportsSendFrom
(
bool
supportsSendFrom)
96
{
97
m_supportsSendFrom
= supportsSendFrom;
98
}
99
100
void
101
VirtualNetDevice::SetIsPointToPoint
(
bool
isPointToPoint)
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
{
116
NS_LOG_FUNCTION_NOARGS
();
117
}
118
119
120
void
VirtualNetDevice::DoDispose
()
121
{
122
NS_LOG_FUNCTION_NOARGS
();
123
m_node
= 0;
124
NetDevice::DoDispose
();
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
166
VirtualNetDevice::GetIfIndex
(
void
)
const
167
{
168
return
m_index
;
169
}
170
171
Ptr<Channel>
172
VirtualNetDevice::GetChannel
(
void
)
const
173
{
174
return
Ptr<Channel>
();
175
}
176
177
Address
178
VirtualNetDevice::GetAddress
(
void
)
const
179
{
180
return
m_myAddress
;
181
}
182
183
void
184
VirtualNetDevice::SetAddress
(
Address
addr)
185
{
186
m_myAddress
= addr;
187
}
188
189
uint16_t
190
VirtualNetDevice::GetMtu
(
void
)
const
191
{
192
return
m_mtu
;
193
}
194
195
bool
196
VirtualNetDevice::IsLinkUp
(
void
)
const
197
{
198
return
true
;
199
}
200
201
void
202
VirtualNetDevice::AddLinkChangeCallback
(
Callback<void>
callback)
203
{
204
}
205
206
bool
207
VirtualNetDevice::IsBroadcast
(
void
)
const
208
{
209
return
true
;
210
}
211
212
Address
213
VirtualNetDevice::GetBroadcast
(
void
)
const
214
{
215
return
Mac48Address
(
"ff:ff:ff:ff:ff:ff"
);
216
}
217
218
bool
219
VirtualNetDevice::IsMulticast
(
void
)
const
220
{
221
return
false
;
222
}
223
224
Address
VirtualNetDevice::GetMulticast
(
Ipv4Address
multicastGroup)
const
225
{
226
return
Mac48Address
(
"ff:ff:ff:ff:ff:ff"
);
227
}
228
229
Address
VirtualNetDevice::GetMulticast
(
Ipv6Address
addr)
const
230
{
231
return
Mac48Address
(
"ff:ff:ff:ff:ff:ff"
);
232
}
233
234
235
bool
236
VirtualNetDevice::IsPointToPoint
(
void
)
const
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>
265
VirtualNetDevice::GetNode
(
void
)
const
266
{
267
return
m_node
;
268
}
269
270
void
271
VirtualNetDevice::SetNode
(
Ptr<Node>
node)
272
{
273
m_node
= node;
274
}
275
276
bool
277
VirtualNetDevice::NeedsArp
(
void
)
const
278
{
279
return
m_needsArp
;
280
}
281
282
void
283
VirtualNetDevice::SetReceiveCallback
(
NetDevice::ReceiveCallback
cb)
284
{
285
m_rxCallback
= cb;
286
}
287
288
void
289
VirtualNetDevice::SetPromiscReceiveCallback
(
NetDevice::PromiscReceiveCallback
cb)
290
{
291
m_promiscRxCallback
= cb;
292
}
293
294
bool
295
VirtualNetDevice::SupportsSendFrom
()
const
296
{
297
return
m_supportsSendFrom
;
298
}
299
300
bool
VirtualNetDevice::IsBridge
(
void
)
const
301
{
302
return
false
;
303
}
304
305
306
}
// namespace ns3
src
virtual-net-device
model
virtual-net-device.cc
Generated on Fri Dec 21 2012 19:00:48 for ns-3 by
1.8.1.2