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
aloha-noack-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) 2010 CTTC
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: Nicola Baldo <nbaldo@cttc.es>
19
*/
20
21
#include "ns3/log.h"
22
#include "ns3/queue.h"
23
#include "ns3/simulator.h"
24
#include "ns3/enum.h"
25
#include "ns3/boolean.h"
26
#include "ns3/uinteger.h"
27
#include "ns3/pointer.h"
28
#include "ns3/channel.h"
29
#include "ns3/trace-source-accessor.h"
30
#include "
aloha-noack-mac-header.h
"
31
#include "
aloha-noack-net-device.h
"
32
#include "ns3/llc-snap-header.h"
33
34
NS_LOG_COMPONENT_DEFINE
(
"AlohaNoackNetDevice"
);
35
36
37
namespace
ns3 {
38
39
40
std::ostream&
operator<<
(std::ostream& os,
AlohaNoackNetDevice::State
state)
41
{
42
switch
(state)
43
{
44
case
AlohaNoackNetDevice::IDLE
:
45
os <<
"IDLE"
;
46
break
;
47
case
AlohaNoackNetDevice::TX
:
48
os <<
"TX"
;
49
break
;
50
case
AlohaNoackNetDevice::RX
:
51
os <<
"RX"
;
52
break
;
53
}
54
return
os;
55
}
56
57
58
NS_OBJECT_ENSURE_REGISTERED
(AlohaNoackNetDevice);
59
60
TypeId
61
AlohaNoackNetDevice::GetTypeId
(
void
)
62
{
63
static
TypeId
tid =
TypeId
(
"ns3::AlohaNoackNetDevice"
)
64
.
SetParent
<
NetDevice
> ()
65
.AddConstructor<AlohaNoackNetDevice> ()
66
.AddAttribute (
"Address"
,
67
"The MAC address of this device."
,
68
Mac48AddressValue
(
Mac48Address
(
"12:34:56:78:90:12"
)),
69
MakeMac48AddressAccessor (&
AlohaNoackNetDevice::m_address
),
70
MakeMac48AddressChecker ())
71
.AddAttribute (
"Queue"
,
72
"packets being transmitted get queued here"
,
73
PointerValue
(),
74
MakePointerAccessor (&
AlohaNoackNetDevice::m_queue
),
75
MakePointerChecker<Queue> ())
76
.AddAttribute (
"Mtu"
,
"The Maximum Transmission Unit"
,
77
UintegerValue
(1500),
78
MakeUintegerAccessor (&
AlohaNoackNetDevice::SetMtu
,
79
&
AlohaNoackNetDevice::GetMtu
),
80
MakeUintegerChecker<uint16_t> (1,65535))
81
.AddAttribute (
"Phy"
,
"The PHY layer attached to this device."
,
82
PointerValue
(),
83
MakePointerAccessor (&
AlohaNoackNetDevice::GetPhy
,
84
&
AlohaNoackNetDevice::SetPhy
),
85
MakePointerChecker<Object> ())
86
.AddTraceSource (
"MacTx"
,
87
"Trace source indicating a packet has arrived for transmission by this device"
,
88
MakeTraceSourceAccessor
(&
AlohaNoackNetDevice::m_macTxTrace
))
89
.AddTraceSource (
"MacTxDrop"
,
90
"Trace source indicating a packet has been dropped by the device before transmission"
,
91
MakeTraceSourceAccessor
(&
AlohaNoackNetDevice::m_macTxDropTrace
))
92
.AddTraceSource (
"MacPromiscRx"
,
93
"A packet has been received by this device, has been passed up from the physical layer "
94
"and is being forwarded up the local protocol stack. This is a promiscuous trace,"
,
95
MakeTraceSourceAccessor
(&
AlohaNoackNetDevice::m_macPromiscRxTrace
))
96
.AddTraceSource (
"MacRx"
,
97
"A packet has been received by this device, has been passed up from the physical layer "
98
"and is being forwarded up the local protocol stack. This is a non-promiscuous trace,"
,
99
MakeTraceSourceAccessor
(&
AlohaNoackNetDevice::m_macRxTrace
))
100
;
101
return
tid;
102
}
103
104
AlohaNoackNetDevice::AlohaNoackNetDevice
()
105
: m_state (
IDLE
)
106
{
107
NS_LOG_FUNCTION
(
this
);
108
}
109
110
AlohaNoackNetDevice::~AlohaNoackNetDevice
()
111
{
112
NS_LOG_FUNCTION
(
this
);
113
m_queue
= 0;
114
}
115
116
void
117
AlohaNoackNetDevice::DoDispose
()
118
{
119
NS_LOG_FUNCTION
(
this
);
120
m_queue
= 0;
121
m_node
= 0;
122
m_channel
= 0;
123
m_currentPkt
= 0;
124
m_phy
= 0;
125
m_phyMacTxStartCallback
= MakeNullCallback< bool, Ptr<Packet> > ();
126
NetDevice::DoDispose
();
127
}
128
129
130
void
131
AlohaNoackNetDevice::SetIfIndex
(
const
uint32_t index)
132
{
133
NS_LOG_FUNCTION
(index);
134
m_ifIndex
= index;
135
}
136
137
uint32_t
138
AlohaNoackNetDevice::GetIfIndex
(
void
)
const
139
{
140
NS_LOG_FUNCTION
(
this
);
141
return
m_ifIndex
;
142
}
143
144
bool
145
AlohaNoackNetDevice::SetMtu
(uint16_t mtu)
146
{
147
NS_LOG_FUNCTION
(mtu);
148
m_mtu
= mtu;
149
return
true
;
150
}
151
152
uint16_t
153
AlohaNoackNetDevice::GetMtu
(
void
)
const
154
{
155
NS_LOG_FUNCTION
(
this
);
156
return
m_mtu
;
157
}
158
159
160
void
161
AlohaNoackNetDevice::SetQueue
(
Ptr<Queue>
q)
162
{
163
NS_LOG_FUNCTION
(q);
164
m_queue
= q;
165
}
166
167
168
void
169
AlohaNoackNetDevice::SetAddress
(
Address
address)
170
{
171
NS_LOG_FUNCTION
(
this
);
172
m_address
=
Mac48Address::ConvertFrom
(address);
173
}
174
175
Address
176
AlohaNoackNetDevice::GetAddress
(
void
)
const
177
{
178
NS_LOG_FUNCTION
(
this
);
179
return
m_address
;
180
}
181
182
bool
183
AlohaNoackNetDevice::IsBroadcast
(
void
)
const
184
{
185
NS_LOG_FUNCTION
(
this
);
186
return
true
;
187
}
188
189
Address
190
AlohaNoackNetDevice::GetBroadcast
(
void
)
const
191
{
192
NS_LOG_FUNCTION
(
this
);
193
return
Mac48Address
(
"ff:ff:ff:ff:ff:ff"
);
194
}
195
196
bool
197
AlohaNoackNetDevice::IsMulticast
(
void
)
const
198
{
199
NS_LOG_FUNCTION
(
this
);
200
return
true
;
201
}
202
203
Address
204
AlohaNoackNetDevice::GetMulticast
(
Ipv4Address
addr)
const
205
{
206
NS_LOG_FUNCTION
(addr);
207
Mac48Address
ad =
Mac48Address::GetMulticast
(addr);
208
return
ad;
209
}
210
211
212
Address
AlohaNoackNetDevice::GetMulticast
(
Ipv6Address
addr)
const
213
{
214
NS_LOG_FUNCTION
(addr);
215
Mac48Address
ad =
Mac48Address::GetMulticast
(addr);
216
return
ad;
217
}
218
219
220
bool
221
AlohaNoackNetDevice::IsPointToPoint
(
void
)
const
222
{
223
NS_LOG_FUNCTION
(
this
);
224
return
false
;
225
}
226
227
bool
228
AlohaNoackNetDevice::IsBridge
(
void
)
const
229
{
230
NS_LOG_FUNCTION
(
this
);
231
return
false
;
232
}
233
234
235
Ptr<Node>
236
AlohaNoackNetDevice::GetNode
(
void
)
const
237
{
238
NS_LOG_FUNCTION
(
this
);
239
return
m_node
;
240
}
241
242
void
243
AlohaNoackNetDevice::SetNode
(
Ptr<Node>
node)
244
{
245
NS_LOG_FUNCTION
(node);
246
247
m_node
= node;
248
}
249
250
void
251
AlohaNoackNetDevice::SetPhy
(
Ptr<Object>
phy)
252
{
253
NS_LOG_FUNCTION
(
this
<< phy);
254
m_phy
= phy;
255
}
256
257
258
Ptr<Object>
259
AlohaNoackNetDevice::GetPhy
()
const
260
{
261
NS_LOG_FUNCTION
(
this
);
262
return
m_phy
;
263
}
264
265
266
void
267
AlohaNoackNetDevice::SetChannel
(
Ptr<Channel>
c)
268
{
269
NS_LOG_FUNCTION
(
this
<< c);
270
m_channel
= c;
271
}
272
273
274
Ptr<Channel>
275
AlohaNoackNetDevice::GetChannel
(
void
)
const
276
{
277
NS_LOG_FUNCTION
(
this
);
278
return
m_channel
;
279
}
280
281
282
bool
283
AlohaNoackNetDevice::NeedsArp
(
void
)
const
284
{
285
NS_LOG_FUNCTION
(
this
);
286
return
true
;
287
}
288
289
bool
290
AlohaNoackNetDevice::IsLinkUp
(
void
)
const
291
{
292
NS_LOG_FUNCTION
(
this
);
293
return
m_linkUp
;
294
}
295
296
void
297
AlohaNoackNetDevice::AddLinkChangeCallback
(
Callback<void>
callback)
298
{
299
NS_LOG_FUNCTION
(&callback);
300
m_linkChangeCallbacks
.
ConnectWithoutContext
(callback);
301
}
302
303
void
304
AlohaNoackNetDevice::SetReceiveCallback
(
NetDevice::ReceiveCallback
cb)
305
{
306
NS_LOG_FUNCTION
(&cb);
307
m_rxCallback
= cb;
308
}
309
310
void
311
AlohaNoackNetDevice::SetPromiscReceiveCallback
(
NetDevice::PromiscReceiveCallback
cb)
312
{
313
NS_LOG_FUNCTION
(&cb);
314
m_promiscRxCallback
= cb;
315
}
316
317
bool
318
AlohaNoackNetDevice::SupportsSendFrom
()
const
319
{
320
NS_LOG_FUNCTION
(
this
);
321
return
true
;
322
}
323
324
325
bool
326
AlohaNoackNetDevice::Send
(
Ptr<Packet>
packet,
const
Address
& dest, uint16_t protocolNumber)
327
{
328
NS_LOG_FUNCTION
(packet << dest << protocolNumber);
329
return
SendFrom
(packet,
m_address
, dest, protocolNumber);
330
}
331
332
bool
333
AlohaNoackNetDevice::SendFrom
(
Ptr<Packet>
packet,
const
Address
& src,
const
Address
& dest, uint16_t protocolNumber)
334
{
335
NS_LOG_FUNCTION
(packet << src << dest << protocolNumber);
336
337
LlcSnapHeader
llc;
338
llc.
SetType
(protocolNumber);
339
packet->
AddHeader
(llc);
340
341
AlohaNoackMacHeader
header;
342
header.
SetSource
(
Mac48Address::ConvertFrom
(src));
343
header.
SetDestination
(
Mac48Address::ConvertFrom
(dest));
344
packet->
AddHeader
(header);
345
346
m_macTxTrace
(packet);
347
348
349
bool
sendOk =
true
;
350
//
351
// If the device is idle, transmission starts immediately. Otherwise,
352
// the transmission will be started by NotifyTransmissionEnd
353
//
354
NS_LOG_LOGIC
(
this
<<
" state="
<<
m_state
);
355
if
(
m_state
==
IDLE
)
356
{
357
if
(
m_queue
->
IsEmpty
())
358
{
359
NS_LOG_LOGIC
(
"new packet is head of queue, starting TX immediately"
);
360
m_currentPkt
= packet;
361
StartTransmission
();
362
}
363
else
364
{
365
NS_LOG_LOGIC
(
"enqueueing new packet"
);
366
if
(
m_queue
->
Enqueue
(packet) ==
false
)
367
{
368
m_macTxDropTrace
(packet);
369
sendOk =
false
;
370
}
371
}
372
}
373
else
374
{
375
NS_LOG_LOGIC
(
"deferring TX, enqueueing new packet"
);
376
NS_ASSERT
(
m_queue
);
377
if
(
m_queue
->
Enqueue
(packet) ==
false
)
378
{
379
m_macTxDropTrace
(packet);
380
sendOk =
false
;
381
}
382
}
383
return
sendOk;
384
}
385
386
void
387
AlohaNoackNetDevice::SetGenericPhyTxStartCallback
(
GenericPhyTxStartCallback
c)
388
{
389
NS_LOG_FUNCTION
(
this
);
390
m_phyMacTxStartCallback
= c;
391
}
392
393
void
394
AlohaNoackNetDevice::StartTransmission
()
395
{
396
NS_LOG_FUNCTION
(
this
);
397
398
NS_ASSERT
(
m_currentPkt
!= 0);
399
NS_ASSERT
(
m_state
==
IDLE
);
400
401
if
(
m_phyMacTxStartCallback
(
m_currentPkt
))
402
{
403
NS_LOG_WARN
(
"PHY refused to start TX"
);
404
}
405
else
406
{
407
m_state
=
TX
;
408
}
409
}
410
411
412
413
void
414
AlohaNoackNetDevice::NotifyTransmissionEnd
(
Ptr<const Packet>
)
415
{
416
NS_LOG_FUNCTION
(
this
);
417
NS_ASSERT_MSG
(
m_state
==
TX
,
"TX end notified while state != TX"
);
418
m_state
=
IDLE
;
419
NS_ASSERT
(
m_queue
);
420
if
(
m_queue
->
IsEmpty
() ==
false
)
421
{
422
m_currentPkt
=
m_queue
->
Dequeue
();
423
NS_ASSERT
(
m_currentPkt
);
424
NS_LOG_LOGIC
(
"scheduling transmission now"
);
425
Simulator::ScheduleNow
(&
AlohaNoackNetDevice::StartTransmission
,
this
);
426
}
427
}
428
429
430
void
431
AlohaNoackNetDevice::NotifyReceptionStart
()
432
{
433
NS_LOG_FUNCTION
(
this
);
434
}
435
436
437
438
void
439
AlohaNoackNetDevice::NotifyReceptionEndError
()
440
{
441
NS_LOG_FUNCTION
(
this
);
442
}
443
444
445
446
447
448
void
449
AlohaNoackNetDevice::NotifyReceptionEndOk
(
Ptr<Packet>
packet)
450
{
451
NS_LOG_FUNCTION
(
this
<< packet);
452
AlohaNoackMacHeader
header;
453
packet->
RemoveHeader
(header);
454
NS_LOG_LOGIC
(
"packet "
<< header.
GetSource
() <<
" --> "
<< header.
GetDestination
() <<
" (here: "
<<
m_address
<<
")"
);
455
456
LlcSnapHeader
llc;
457
packet->
RemoveHeader
(llc);
458
459
PacketType
packetType;
460
if
(header.
GetDestination
().
IsBroadcast
())
461
{
462
packetType =
PACKET_BROADCAST
;
463
}
464
else
if
(header.
GetDestination
().
IsGroup
())
465
{
466
packetType =
PACKET_MULTICAST
;
467
}
468
else
if
(header.
GetDestination
() ==
m_address
)
469
{
470
packetType =
PACKET_HOST
;
471
}
472
else
473
{
474
packetType =
PACKET_OTHERHOST
;
475
}
476
477
NS_LOG_LOGIC
(
"packet type = "
<< packetType);
478
479
if
(!
m_promiscRxCallback
.
IsNull
())
480
{
481
m_promiscRxCallback
(
this
, packet->
Copy
(), llc.GetType (), header.
GetSource
(), header.
GetDestination
(), packetType);
482
}
483
484
if
(packetType !=
PACKET_OTHERHOST
)
485
{
486
m_rxCallback
(
this
, packet, llc.GetType (), header.
GetSource
() );
487
}
488
}
489
490
491
492
}
// namespace ns3
src
spectrum
model
aloha-noack-net-device.cc
Generated on Tue Nov 13 2012 10:32:22 for ns-3 by
1.8.1.2