8751
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* Copyright (c) 2011 Yufei Cheng
|
|
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: Yufei Cheng <yfcheng@ittc.ku.edu>
|
|
19 |
*
|
|
20 |
* James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
|
|
21 |
* ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
|
|
22 |
* Information and Telecommunication Technology Center (ITTC)
|
|
23 |
* and Department of Electrical Engineering and Computer Science
|
|
24 |
* The University of Kansas Lawrence, KS USA.
|
|
25 |
*
|
|
26 |
* Work supported in part by NSF FIND (Future Internet Design) Program
|
|
27 |
* under grant CNS-0626918 (Postmodern Internet Architecture),
|
|
28 |
* NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
|
|
29 |
* US Department of Defense (DoD), and ITTC at The University of Kansas.
|
|
30 |
*/
|
|
31 |
|
|
32 |
#define NS_LOG_APPEND_CONTEXT \
|
|
33 |
if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
|
|
34 |
|
|
35 |
#include <list>
|
|
36 |
#include <ctime>
|
|
37 |
#include <map>
|
|
38 |
#include <limits>
|
|
39 |
#include <algorithm>
|
|
40 |
#include <iostream>
|
|
41 |
|
8752
|
42 |
#include "ns3/enum.h"
|
8751
|
43 |
#include "ns3/string.h"
|
|
44 |
#include "ns3/ptr.h"
|
|
45 |
#include "ns3/log.h"
|
|
46 |
#include "ns3/assert.h"
|
|
47 |
#include "ns3/uinteger.h"
|
8752
|
48 |
#include "ns3/packet.h"
|
|
49 |
#include "ns3/boolean.h"
|
|
50 |
#include "ns3/node-list.h"
|
|
51 |
#include "ns3/double.h"
|
|
52 |
#include "ns3/pointer.h"
|
8751
|
53 |
#include "ns3/object-vector.h"
|
|
54 |
#include "ns3/ipv4-address.h"
|
|
55 |
#include "ns3/ipv4-header.h"
|
|
56 |
#include "ns3/ipv4-l3-protocol.h"
|
|
57 |
#include "ns3/ipv4-route.h"
|
|
58 |
#include "ns3/trace-source-accessor.h"
|
|
59 |
#include "ns3/random-variable.h"
|
|
60 |
#include "ns3/icmpv4-l4-protocol.h"
|
|
61 |
#include "ns3/adhoc-wifi-mac.h"
|
|
62 |
#include "ns3/wifi-net-device.h"
|
|
63 |
#include "ns3/inet-socket-address.h"
|
|
64 |
#include "ns3/udp-l4-protocol.h"
|
|
65 |
#include "ns3/udp-socket-factory.h"
|
|
66 |
#include "ns3/tcp-socket-factory.h"
|
|
67 |
|
|
68 |
#include "dsr-rreq-table.h"
|
|
69 |
#include "dsr-rcache.h"
|
|
70 |
#include "dsr-routing.h"
|
|
71 |
#include "dsr-fs-header.h"
|
|
72 |
#include "dsr-options.h"
|
|
73 |
|
|
74 |
NS_LOG_COMPONENT_DEFINE ("DsrRouting");
|
|
75 |
|
|
76 |
namespace ns3 {
|
|
77 |
namespace dsr {
|
|
78 |
|
|
79 |
NS_OBJECT_ENSURE_REGISTERED (DsrRouting);
|
|
80 |
|
|
81 |
/* see http://www.iana.org/assignments/protocol-numbers */
|
|
82 |
const uint8_t DsrRouting::PROT_NUMBER = 48;
|
|
83 |
/*
|
|
84 |
* The extension header is the fixed size dsr header, it is response for recognizing DSR option types
|
|
85 |
* and demux to right options to process the packet.
|
|
86 |
*
|
|
87 |
* The header format with neighboring layers is as follows:
|
|
88 |
*
|
|
89 |
+-+-+-+-+-+-+-+-+-+-+-
|
|
90 |
| Application Header |
|
|
91 |
+-+-+-+-+-+-+-+-+-+-+-+
|
|
92 |
| Transport Header |
|
|
93 |
+-+-+-+-+-+-+-+-+-+-+-+
|
|
94 |
| Fixed DSR Header |
|
|
95 |
+---------------------+
|
|
96 |
| DSR Options |
|
|
97 |
+-+-+-+-+-+-+-+-+-+-+-+
|
|
98 |
| IP Header |
|
|
99 |
+-+-+-+-+-+-+-+-+-+-+-+
|
|
100 |
*/
|
|
101 |
|
|
102 |
TypeId DsrRouting::GetTypeId ()
|
|
103 |
{
|
|
104 |
static TypeId tid = TypeId ("ns3::dsr::DsrRouting")
|
8753
|
105 |
.SetParent<IpL4Protocol> ()
|
8751
|
106 |
.AddConstructor<DsrRouting> ()
|
|
107 |
.AddAttribute ("RouteCache", "The route cache for saving routes from route discovery process.",
|
|
108 |
PointerValue (0),
|
|
109 |
MakePointerAccessor (&DsrRouting::SetRouteCache,
|
|
110 |
&DsrRouting::GetRouteCache),
|
|
111 |
MakePointerChecker<RouteCache> ())
|
|
112 |
.AddAttribute ("RreqTable", "The request table to manage route requests.",
|
|
113 |
PointerValue (0),
|
|
114 |
MakePointerAccessor (&DsrRouting::SetRequestTable,
|
|
115 |
&DsrRouting::GetRequestTable),
|
|
116 |
MakePointerChecker<RreqTable> ())
|
|
117 |
.AddAttribute ("MaxSendBuffLen","Maximum number of packets that can be stored in send buffer.",
|
|
118 |
UintegerValue (64),
|
|
119 |
MakeUintegerAccessor (&DsrRouting::m_maxSendBuffLen),
|
|
120 |
MakeUintegerChecker<uint32_t> ())
|
|
121 |
.AddAttribute ("MaxSendBuffTime","Maximum time packets can be queued in the send buffer .",
|
|
122 |
TimeValue (Seconds (30)),
|
|
123 |
MakeTimeAccessor (&DsrRouting::m_sendBufferTimeout),
|
|
124 |
MakeTimeChecker ())
|
|
125 |
.AddAttribute ("MaxMaintLen","Maximum number of packets that can be stored in maintenance buffer.",
|
|
126 |
UintegerValue (50),
|
|
127 |
MakeUintegerAccessor (&DsrRouting::m_maxMaintainLen),
|
|
128 |
MakeUintegerChecker<uint32_t> ())
|
|
129 |
.AddAttribute ("MaxMaintTime","Maximum time packets can be queued in maintenance buffer.",
|
|
130 |
TimeValue (Seconds (30)),
|
|
131 |
MakeTimeAccessor (&DsrRouting::m_maxMaintainTime),
|
|
132 |
MakeTimeChecker ())
|
|
133 |
.AddAttribute ("MaxCacheLen","Maximum number of route entries that can be stored in route cache.",
|
|
134 |
UintegerValue (64),
|
|
135 |
MakeUintegerAccessor (&DsrRouting::m_maxCacheLen),
|
|
136 |
MakeUintegerChecker<uint32_t> ())
|
|
137 |
.AddAttribute ("RouteCacheTimeout","Maximum time the route cache can be queued in route cache.",
|
|
138 |
TimeValue (Seconds (300)),
|
|
139 |
MakeTimeAccessor (&DsrRouting::m_maxCacheTime),
|
|
140 |
MakeTimeChecker ())
|
|
141 |
.AddAttribute ("MaxEntriesEachDst","Maximum number of route entries for a single destination to respond.",
|
|
142 |
UintegerValue (20),
|
|
143 |
MakeUintegerAccessor (&DsrRouting::m_maxEntriesEachDst),
|
|
144 |
MakeUintegerChecker<uint32_t> ())
|
|
145 |
.AddAttribute ("SendBuffInterval","How often to check send buffer for packet with route.",
|
8752
|
146 |
TimeValue (Seconds (500)),
|
8751
|
147 |
MakeTimeAccessor (&DsrRouting::m_sendBuffInterval),
|
|
148 |
MakeTimeChecker ())
|
|
149 |
.AddAttribute ("NodeTraversalTime","The time it takes to traverse two neighboring nodes.",
|
8752
|
150 |
TimeValue (MilliSeconds (40)),
|
8751
|
151 |
MakeTimeAccessor (&DsrRouting::m_nodeTraversalTime),
|
|
152 |
MakeTimeChecker ())
|
|
153 |
.AddAttribute ("RreqRetries","Maximum number of retransmissions for request discovery of a route.",
|
|
154 |
UintegerValue (16),
|
|
155 |
MakeUintegerAccessor (&DsrRouting::m_rreqRetries),
|
|
156 |
MakeUintegerChecker<uint32_t> ())
|
|
157 |
.AddAttribute ("MaintenanceRetries","Maximum number of retransmissions for data packets from maintenance buffer.",
|
8752
|
158 |
DoubleValue (2),
|
|
159 |
MakeDoubleAccessor (&DsrRouting::m_maxMaintRexmt),
|
|
160 |
MakeDoubleChecker<uint32_t> ())
|
8751
|
161 |
.AddAttribute ("RequestTableSize","Maximum number of request entries in the request table.",
|
|
162 |
UintegerValue (64),
|
|
163 |
MakeUintegerAccessor (&DsrRouting::m_requestTableSize),
|
|
164 |
MakeUintegerChecker<uint32_t> ())
|
|
165 |
.AddAttribute ("RequestIdSize","Maximum number of request source Ids in the request table.",
|
|
166 |
UintegerValue (16),
|
|
167 |
MakeUintegerAccessor (&DsrRouting::m_requestTableIds),
|
8752
|
168 |
MakeUintegerChecker<uint32_t> ())
|
8751
|
169 |
.AddAttribute ("UniqueRequestIdSize","Maximum number of request Ids in the request table for a single destination.",
|
|
170 |
UintegerValue (256),
|
|
171 |
MakeUintegerAccessor (&DsrRouting::m_maxRreqId),
|
|
172 |
MakeUintegerChecker<uint16_t> ())
|
|
173 |
.AddAttribute ("NonPropRequestTimeout","The timeout value for non-propagation request.",
|
|
174 |
TimeValue (MilliSeconds (30)),
|
|
175 |
MakeTimeAccessor (&DsrRouting::m_nonpropRequestTimeout),
|
|
176 |
MakeTimeChecker ())
|
|
177 |
.AddAttribute ("DiscoveryHopLimit","The max discovery hop limit for route requests.",
|
8752
|
178 |
DoubleValue (255),
|
|
179 |
MakeDoubleAccessor (&DsrRouting::m_discoveryHopLimit),
|
|
180 |
MakeDoubleChecker<uint32_t> ())
|
8751
|
181 |
.AddAttribute ("MaxSalvageCount","The max salvage count for a single data packet.",
|
|
182 |
UintegerValue (15),
|
|
183 |
MakeUintegerAccessor (&DsrRouting::m_maxSalvageCount),
|
|
184 |
MakeUintegerChecker<uint8_t> ())
|
|
185 |
.AddAttribute ("BlacklistTimeout","The time for a neighbor to stay in blacklist.",
|
|
186 |
TimeValue (Seconds (3)),
|
|
187 |
MakeTimeAccessor (&DsrRouting::m_blacklistTimeout),
|
|
188 |
MakeTimeChecker ())
|
|
189 |
.AddAttribute ("GratReplyHoldoff","The time for gratuitous reply entry to expire.",
|
|
190 |
TimeValue (Seconds (1)),
|
|
191 |
MakeTimeAccessor (&DsrRouting::m_gratReplyHoldoff),
|
|
192 |
MakeTimeChecker ())
|
|
193 |
.AddAttribute ("BroadcastJitter","The jitter time to avoid collision for broadcast packets.",
|
|
194 |
UintegerValue (10),
|
|
195 |
MakeUintegerAccessor (&DsrRouting::m_broadcastJitter),
|
|
196 |
MakeUintegerChecker<uint16_t> ())
|
|
197 |
.AddAttribute ("PassiveAckTimeout","The time a packet in maintenance buffer wait for passive acknowledgment.",
|
8752
|
198 |
TimeValue (MilliSeconds (100)),
|
8751
|
199 |
MakeTimeAccessor (&DsrRouting::m_passiveAckTimeout),
|
|
200 |
MakeTimeChecker ())
|
|
201 |
.AddAttribute ("TryPassiveAcks","The number of passive acknowledgment to use.",
|
|
202 |
UintegerValue (1),
|
|
203 |
MakeUintegerAccessor (&DsrRouting::m_tryPassiveAcks),
|
|
204 |
MakeUintegerChecker<uint32_t> ())
|
|
205 |
.AddAttribute ("RequestPeriod","The base time interval between route requests.",
|
|
206 |
TimeValue (MilliSeconds (500)),
|
|
207 |
MakeTimeAccessor (&DsrRouting::m_requestPeriod),
|
|
208 |
MakeTimeChecker ())
|
|
209 |
.AddAttribute ("MaxRequestPeriod","The max time interval between route requests.",
|
|
210 |
TimeValue (Seconds (10)),
|
|
211 |
MakeTimeAccessor (&DsrRouting::m_maxRequestPeriod),
|
|
212 |
MakeTimeChecker ())
|
|
213 |
.AddAttribute ("GraReplyTableSize","The gratuitous reply table size.",
|
|
214 |
UintegerValue (64),
|
|
215 |
MakeUintegerAccessor (&DsrRouting::m_graReplyTableSize),
|
|
216 |
MakeUintegerChecker<uint32_t> ())
|
|
217 |
.AddAttribute ("CacheType","Use Link Cache or use Path Cache",
|
|
218 |
StringValue ("LinkCache"),
|
|
219 |
MakeStringAccessor (&DsrRouting::m_cacheType),
|
|
220 |
MakeStringChecker ())
|
|
221 |
.AddAttribute ("StabilityDecrFactor","The stability decrease factor for link cache",
|
8752
|
222 |
UintegerValue (2),
|
|
223 |
MakeUintegerAccessor (&DsrRouting::m_stabilityDecrFactor),
|
|
224 |
MakeUintegerChecker<uint64_t> ())
|
8751
|
225 |
.AddAttribute ("StabilityIncrFactor","The stability increase factor for link cache",
|
8752
|
226 |
UintegerValue (4),
|
|
227 |
MakeUintegerAccessor (&DsrRouting::m_stabilityIncrFactor),
|
|
228 |
MakeUintegerChecker<uint64_t> ())
|
8751
|
229 |
.AddAttribute ("InitStability","The initial stability factor for link cache",
|
8752
|
230 |
TimeValue (Seconds (25)),
|
|
231 |
MakeTimeAccessor (&DsrRouting::m_initStability),
|
|
232 |
MakeTimeChecker ())
|
8751
|
233 |
.AddAttribute ("MinLifeTime","The minimal life time for link cache",
|
8752
|
234 |
TimeValue (Seconds (1)),
|
|
235 |
MakeTimeAccessor (&DsrRouting::m_minLifeTime),
|
|
236 |
MakeTimeChecker ())
|
8751
|
237 |
.AddAttribute ("UseExtends","The extension time for link cache",
|
8752
|
238 |
TimeValue (Seconds (120)),
|
|
239 |
MakeTimeAccessor (&DsrRouting::m_useExtends),
|
|
240 |
MakeTimeChecker ())
|
8751
|
241 |
.AddAttribute ("EnableSubRoute","Enables saving of sub route when receiving route error messages, only available when using path route cache",
|
|
242 |
BooleanValue (true),
|
|
243 |
MakeBooleanAccessor (&DsrRouting::m_subRoute),
|
|
244 |
MakeBooleanChecker ())
|
8752
|
245 |
.AddAttribute ("RetransIncr","The increase time for retransmission timer when facing network congestion",
|
|
246 |
TimeValue (MilliSeconds (20)),
|
|
247 |
MakeTimeAccessor (&DsrRouting::m_retransIncr),
|
|
248 |
MakeTimeChecker ())
|
|
249 |
.AddAttribute ("MaxNetworkQueueSize","The max number of packet to save in the network queue.",
|
|
250 |
UintegerValue (400),
|
|
251 |
MakeUintegerAccessor (&DsrRouting::m_maxNetworkSize),
|
|
252 |
MakeUintegerChecker<uint32_t> ())
|
|
253 |
.AddAttribute ("MaxNetworkQueueDelay","The max time for a packet to stay in the network queue.",
|
|
254 |
TimeValue (Seconds (30.0)),
|
|
255 |
MakeTimeAccessor (&DsrRouting::m_maxNetworkDelay),
|
|
256 |
MakeTimeChecker ())
|
|
257 |
.AddAttribute ("NumPriorityQueues","The max number of packet to save in the network queue.",
|
|
258 |
UintegerValue (2),
|
|
259 |
MakeUintegerAccessor (&DsrRouting::m_numPriorityQueues),
|
|
260 |
MakeUintegerChecker<uint32_t> ())
|
8751
|
261 |
.AddTraceSource ("Tx", "Send DSR packet.",
|
|
262 |
MakeTraceSourceAccessor (&DsrRouting::m_txPacketTrace))
|
|
263 |
.AddTraceSource ("Drop", "Drop DSR packet",
|
|
264 |
MakeTraceSourceAccessor (&DsrRouting::m_dropTrace))
|
|
265 |
;
|
|
266 |
return tid;
|
|
267 |
}
|
|
268 |
|
|
269 |
DsrRouting::DsrRouting ()
|
|
270 |
{
|
|
271 |
NS_LOG_FUNCTION_NOARGS ();
|
|
272 |
/*
|
|
273 |
* The following Ptr statements created objects for all the options header for DSR, and each of them have
|
|
274 |
* distinct option number assigned, when DSR Routing received a packet from higher layer, it will find
|
|
275 |
* the following options based on the option number, and pass the packet to the appropriate option to
|
|
276 |
* process it. After the option processing, it will pass the packet back to DSR Routing to send down layer.
|
|
277 |
*/
|
|
278 |
Ptr<dsr::DsrOptionPad1> pad1Option = CreateObject<dsr::DsrOptionPad1> ();
|
|
279 |
Ptr<dsr::DsrOptionPadn> padnOption = CreateObject<dsr::DsrOptionPadn> ();
|
|
280 |
Ptr<dsr::DsrOptionRreq> rreqOption = CreateObject<dsr::DsrOptionRreq> ();
|
|
281 |
Ptr<dsr::DsrOptionRrep> rrepOption = CreateObject<dsr::DsrOptionRrep> ();
|
|
282 |
Ptr<dsr::DsrOptionSR> srOption = CreateObject<dsr::DsrOptionSR> ();
|
|
283 |
Ptr<dsr::DsrOptionRerr> rerrOption = CreateObject<dsr::DsrOptionRerr> ();
|
|
284 |
Ptr<dsr::DsrOptionAckReq> ackReq = CreateObject<dsr::DsrOptionAckReq> ();
|
|
285 |
Ptr<dsr::DsrOptionAck> ack = CreateObject<dsr::DsrOptionAck> ();
|
|
286 |
|
|
287 |
Insert (pad1Option);
|
|
288 |
Insert (padnOption);
|
|
289 |
Insert (rreqOption);
|
|
290 |
Insert (rrepOption);
|
|
291 |
Insert (srOption);
|
|
292 |
Insert (rerrOption);
|
|
293 |
Insert (ackReq);
|
|
294 |
Insert (ack);
|
|
295 |
|
|
296 |
// Check the send buffer for sending packets
|
|
297 |
m_sendBuffTimer.SetFunction (&DsrRouting::SendBuffTimerExpire, this);
|
8752
|
298 |
m_sendBuffTimer.Schedule (Seconds (100));
|
8751
|
299 |
}
|
|
300 |
|
|
301 |
DsrRouting::~DsrRouting ()
|
|
302 |
{
|
|
303 |
NS_LOG_FUNCTION_NOARGS ();
|
|
304 |
}
|
|
305 |
|
|
306 |
void
|
|
307 |
DsrRouting::NotifyNewAggregate ()
|
|
308 |
{
|
|
309 |
NS_LOG_FUNCTION (this << "NotifyNewAggregate");
|
|
310 |
if (m_node == 0)
|
|
311 |
{
|
|
312 |
Ptr<Node> node = this->GetObject<Node> ();
|
|
313 |
if (node != 0)
|
|
314 |
{
|
|
315 |
m_ipv4 = this->GetObject<Ipv4L3Protocol> ();
|
|
316 |
if (m_ipv4 != 0)
|
|
317 |
{
|
|
318 |
this->SetNode (node);
|
|
319 |
m_ipv4->Insert (this);
|
|
320 |
this->SetDownTarget (MakeCallback (&Ipv4L3Protocol::Send, m_ipv4));
|
|
321 |
}
|
|
322 |
|
|
323 |
m_ip = node->GetObject<Ipv4> ();
|
|
324 |
if (m_ip != 0)
|
|
325 |
{
|
|
326 |
NS_LOG_DEBUG ("Ipv4 started");
|
|
327 |
}
|
|
328 |
}
|
|
329 |
}
|
|
330 |
Object::NotifyNewAggregate ();
|
|
331 |
Simulator::ScheduleNow (&DsrRouting::Start, this);
|
|
332 |
}
|
|
333 |
|
|
334 |
void DsrRouting::Start ()
|
|
335 |
{
|
|
336 |
NS_LOG_FUNCTION (this << "Start DSR Routing protocol");
|
8752
|
337 |
Ptr<dsr::RouteCache> routeCache = CreateObject<dsr::RouteCache> ();
|
|
338 |
// Configure the path cache parameters
|
|
339 |
routeCache->SetCacheType (m_cacheType);
|
|
340 |
routeCache->SetSubRoute (m_subRoute);
|
|
341 |
routeCache->SetMaxCacheLen (m_maxCacheLen);
|
|
342 |
routeCache->SetCacheTimeout (m_maxCacheTime);
|
|
343 |
routeCache->SetMaxEntriesEachDst (m_maxEntriesEachDst);
|
|
344 |
// Parameters for link cache
|
|
345 |
routeCache->SetStabilityDecrFactor (m_stabilityDecrFactor);
|
|
346 |
routeCache->SetStabilityIncrFactor (m_stabilityIncrFactor);
|
|
347 |
routeCache->SetInitStability (m_initStability);
|
|
348 |
routeCache->SetMinLifeTime (m_minLifeTime);
|
|
349 |
routeCache->SetUseExtends (m_useExtends);
|
|
350 |
routeCache->ScheduleTimer ();
|
|
351 |
// The call back to handle link error and send error message to appropriate nodes
|
|
352 |
routeCache->SetCallback (MakeCallback (&DsrRouting::SendRerrWhenBreaksLinkToNextHop, this));
|
|
353 |
SetRouteCache (routeCache);
|
|
354 |
|
8751
|
355 |
if (m_mainAddress == Ipv4Address ())
|
|
356 |
{
|
|
357 |
Ipv4Address loopback ("127.0.0.1");
|
|
358 |
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
|
|
359 |
{
|
|
360 |
// Use primary address, if multiple
|
|
361 |
Ipv4Address addr = m_ipv4->GetAddress (i, 0).GetLocal ();
|
8752
|
362 |
m_broadcast = m_ipv4->GetAddress (i, 0).GetBroadcast();
|
8751
|
363 |
NS_LOG_DEBUG ("The addr " << addr);
|
|
364 |
if (addr != loopback)
|
|
365 |
{
|
|
366 |
m_mainAddress = addr;
|
|
367 |
NS_LOG_DEBUG ("The node Address " << m_mainAddress);
|
|
368 |
|
|
369 |
m_ipv4->GetNetDevice (1)->SetPromiscReceiveCallback (MakeCallback (&DsrRouting::PromiscReceive, this));
|
|
370 |
|
|
371 |
// Allow neighbor manager use this interface for layer 2 feedback if possible
|
|
372 |
Ptr<NetDevice> dev = m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (addr));
|
|
373 |
Ptr<WifiNetDevice> wifi = dev->GetObject<WifiNetDevice> ();
|
|
374 |
if (wifi == 0)
|
|
375 |
{
|
|
376 |
break;
|
|
377 |
}
|
|
378 |
Ptr<WifiMac> mac = wifi->GetMac ();
|
|
379 |
if (mac == 0)
|
|
380 |
{
|
|
381 |
break;
|
|
382 |
}
|
|
383 |
|
|
384 |
// trace back to link mac drop event to process tx error call back
|
8752
|
385 |
mac->TraceConnectWithoutContext ("TxErrHeader", routeCache->GetTxErrorCallback ());
|
|
386 |
routeCache->AddArpCache (m_ipv4->GetInterface (i)->GetArpCache ());
|
8751
|
387 |
break;
|
|
388 |
}
|
|
389 |
}
|
8752
|
390 |
NS_ASSERT (m_mainAddress != Ipv4Address () && m_broadcast != Ipv4Address());
|
8751
|
391 |
}
|
|
392 |
|
8752
|
393 |
NS_LOG_DEBUG ("The number queues " << m_numPriorityQueues);
|
|
394 |
for (uint32_t i = 0; i < m_numPriorityQueues; i++)
|
|
395 |
{
|
|
396 |
// Set the network queue max size and the delay
|
|
397 |
NS_LOG_DEBUG ("The network size " << m_maxNetworkSize << " and the network delay " << m_maxNetworkDelay.GetSeconds());
|
|
398 |
Ptr<dsr::DsrNetworkQueue> queue_i = CreateObject<dsr::DsrNetworkQueue> (m_maxNetworkSize,m_maxNetworkDelay);
|
|
399 |
std::pair<std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator, bool> result_i = m_priorityQueue.insert (std::make_pair (i, queue_i));
|
|
400 |
NS_ASSERT_MSG (result_i.second, "Error in creating queues");
|
|
401 |
}
|
|
402 |
Ptr<dsr::RreqTable> rreqTable = CreateObject<dsr::RreqTable> ();
|
8751
|
403 |
// Set the initial hop limit
|
8752
|
404 |
rreqTable->SetInitHopLimit (m_discoveryHopLimit);
|
8751
|
405 |
// Configure the request table parameters
|
8752
|
406 |
rreqTable->SetRreqTableSize (m_requestTableSize);
|
|
407 |
rreqTable->SetRreqIdSize (m_requestTableIds);
|
|
408 |
rreqTable->SetUniqueRreqIdSize (m_maxRreqId);
|
|
409 |
SetRequestTable (rreqTable);
|
8751
|
410 |
// Set the send buffer parameters
|
|
411 |
m_sendBuffer.SetMaxQueueLen (m_maxSendBuffLen);
|
|
412 |
m_sendBuffer.SetSendBufferTimeout (m_sendBufferTimeout);
|
8752
|
413 |
// Set the error buffer parameters using just the send buffer parameters
|
|
414 |
m_errorBuffer.SetMaxQueueLen (m_maxSendBuffLen);
|
|
415 |
m_errorBuffer.SetErrorBufferTimeout (m_sendBufferTimeout);
|
8751
|
416 |
// Set the maintenance buffer parameters
|
|
417 |
m_maintainBuffer.SetMaxQueueLen (m_maxMaintainLen);
|
|
418 |
m_maintainBuffer.SetMaintainBufferTimeout (m_maxMaintainTime);
|
|
419 |
// Set the gratuitous reply table size
|
|
420 |
m_graReply.SetGraTableSize (m_graReplyTableSize);
|
|
421 |
NS_LOG_DEBUG ("Starting DSR on node " << m_mainAddress);
|
|
422 |
}
|
|
423 |
|
|
424 |
void
|
8752
|
425 |
DsrRouting::DoDispose (void)
|
|
426 |
{
|
|
427 |
NS_LOG_FUNCTION_NOARGS ();
|
|
428 |
m_node = 0;
|
|
429 |
for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
|
|
430 |
{
|
|
431 |
// Disable layer 2 link state monitoring (if possible)
|
|
432 |
Ptr<NetDevice> dev = m_ipv4->GetNetDevice (i);
|
|
433 |
Ptr<WifiNetDevice> wifi = dev->GetObject<WifiNetDevice> ();
|
|
434 |
if (wifi != 0)
|
|
435 |
{
|
|
436 |
Ptr<WifiMac> mac = wifi->GetMac ()->GetObject<AdhocWifiMac> ();
|
|
437 |
if (mac != 0)
|
|
438 |
{
|
|
439 |
mac->TraceDisconnectWithoutContext ("TxErrHeader",
|
|
440 |
m_routeCache->GetTxErrorCallback ());
|
|
441 |
m_routeCache->DelArpCache (m_ipv4->GetInterface (i)->GetArpCache ());
|
|
442 |
}
|
|
443 |
}
|
|
444 |
}
|
8753
|
445 |
IpL4Protocol::DoDispose ();
|
8752
|
446 |
}
|
|
447 |
|
|
448 |
void
|
8751
|
449 |
DsrRouting::SetNode (Ptr<Node> node)
|
|
450 |
{
|
|
451 |
m_node = node;
|
|
452 |
}
|
|
453 |
|
|
454 |
Ptr<Node>
|
|
455 |
DsrRouting::GetNode () const
|
|
456 |
{
|
|
457 |
NS_LOG_FUNCTION_NOARGS ();
|
|
458 |
return m_node;
|
|
459 |
}
|
|
460 |
|
|
461 |
void DsrRouting::SetRouteCache (Ptr<dsr::RouteCache> r)
|
|
462 |
{
|
|
463 |
// / Set the route cache to use
|
|
464 |
m_routeCache = r;
|
|
465 |
}
|
|
466 |
|
|
467 |
Ptr<dsr::RouteCache>
|
|
468 |
DsrRouting::GetRouteCache () const
|
|
469 |
{
|
|
470 |
// / Get the route cache to use
|
|
471 |
return m_routeCache;
|
|
472 |
}
|
|
473 |
|
|
474 |
void DsrRouting::SetRequestTable (Ptr<dsr::RreqTable> q)
|
|
475 |
{
|
|
476 |
// / Set the request table to use
|
|
477 |
m_rreqTable = q;
|
|
478 |
}
|
|
479 |
|
|
480 |
Ptr<dsr::RreqTable>
|
|
481 |
DsrRouting::GetRequestTable () const
|
|
482 |
{
|
|
483 |
// / Get the request table to use
|
|
484 |
return m_rreqTable;
|
|
485 |
}
|
|
486 |
|
8752
|
487 |
bool DsrRouting::IsLinkCache ()
|
|
488 |
{
|
|
489 |
return m_routeCache->IsLinkCache ();
|
|
490 |
}
|
|
491 |
|
|
492 |
void DsrRouting::UseExtends (RouteCacheEntry::IP_VECTOR rt)
|
|
493 |
{
|
|
494 |
m_routeCache->UseExtends (rt);
|
|
495 |
}
|
|
496 |
|
|
497 |
bool DsrRouting::LookupRoute (Ipv4Address id, RouteCacheEntry & rt)
|
|
498 |
{
|
|
499 |
return m_routeCache->LookupRoute (id, rt);
|
|
500 |
}
|
|
501 |
|
|
502 |
bool DsrRouting::AddRoute_Link (RouteCacheEntry::IP_VECTOR nodelist, Ipv4Address source)
|
|
503 |
{
|
|
504 |
Ipv4Address nextHop = SearchNextHop (source, nodelist);
|
|
505 |
m_errorBuffer.DropPacketForErrLink (source, nextHop);
|
|
506 |
return m_routeCache->AddRoute_Link (nodelist, source);
|
|
507 |
}
|
|
508 |
|
|
509 |
bool DsrRouting::AddRoute (RouteCacheEntry & rt)
|
|
510 |
{
|
|
511 |
std::vector<Ipv4Address> nodelist = rt.GetVector ();
|
|
512 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodelist);
|
|
513 |
m_errorBuffer.DropPacketForErrLink (m_mainAddress, nextHop);
|
|
514 |
return m_routeCache->AddRoute (rt);
|
|
515 |
}
|
|
516 |
|
|
517 |
void DsrRouting::DeleteAllRoutesIncludeLink (Ipv4Address errorSrc, Ipv4Address unreachNode, Ipv4Address node)
|
|
518 |
{
|
|
519 |
m_routeCache->DeleteAllRoutesIncludeLink (errorSrc, unreachNode, node);
|
|
520 |
}
|
|
521 |
|
|
522 |
bool DsrRouting::UpdateRouteEntry (Ipv4Address dst)
|
|
523 |
{
|
|
524 |
return m_routeCache->UpdateRouteEntry (dst);
|
|
525 |
}
|
|
526 |
|
8751
|
527 |
Ipv4Address
|
|
528 |
DsrRouting::GetIPfromMAC (Mac48Address address)
|
|
529 |
{
|
8752
|
530 |
NS_LOG_FUNCTION (this << address);
|
8751
|
531 |
int32_t nNodes = NodeList::GetNNodes ();
|
|
532 |
for (int32_t i = 0; i < nNodes; ++i)
|
|
533 |
{
|
|
534 |
Ptr<Node> node = NodeList::GetNode (i);
|
|
535 |
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
|
|
536 |
Ptr<NetDevice> netDevice = ipv4->GetNetDevice (1);
|
|
537 |
|
|
538 |
if (netDevice->GetAddress () == address)
|
|
539 |
{
|
|
540 |
return ipv4->GetAddress (1, 0).GetLocal ();
|
|
541 |
}
|
|
542 |
}
|
|
543 |
return 0;
|
|
544 |
}
|
|
545 |
|
|
546 |
void DsrRouting::PrintVector (std::vector<Ipv4Address>& vec)
|
|
547 |
{
|
8752
|
548 |
NS_LOG_FUNCTION (this);
|
8751
|
549 |
/*
|
|
550 |
* Check elements in a route vector
|
|
551 |
*/
|
|
552 |
if (!vec.size ())
|
|
553 |
{
|
|
554 |
NS_LOG_DEBUG ("The vector is empty");
|
|
555 |
}
|
|
556 |
else
|
|
557 |
{
|
|
558 |
NS_LOG_DEBUG ("Print all the elements in a vector");
|
|
559 |
for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
|
|
560 |
{
|
|
561 |
NS_LOG_DEBUG ("The ip address " << *i);
|
|
562 |
}
|
|
563 |
}
|
|
564 |
}
|
|
565 |
|
|
566 |
Ipv4Address DsrRouting::SearchNextHop (Ipv4Address ipv4Address, std::vector<Ipv4Address>& vec)
|
|
567 |
{
|
8752
|
568 |
NS_LOG_FUNCTION (this << ipv4Address);
|
8751
|
569 |
Ipv4Address nextHop;
|
8752
|
570 |
NS_LOG_DEBUG ("the vector size " << vec.size ());
|
8751
|
571 |
if (vec.size () == 2)
|
|
572 |
{
|
|
573 |
NS_LOG_DEBUG ("The two nodes are neighbors");
|
|
574 |
nextHop = vec[1];
|
|
575 |
return nextHop;
|
|
576 |
}
|
|
577 |
else
|
|
578 |
{
|
|
579 |
if (ipv4Address == vec.back ())
|
|
580 |
{
|
|
581 |
NS_LOG_DEBUG ("We have reached to the final destination " << ipv4Address << " " << vec.back ());
|
|
582 |
return ipv4Address;
|
|
583 |
}
|
8752
|
584 |
for (std::vector<Ipv4Address>::const_iterator i = vec.begin (); i != vec.end (); ++i)
|
8751
|
585 |
{
|
|
586 |
if (ipv4Address == (*i))
|
|
587 |
{
|
8752
|
588 |
NS_LOG_DEBUG (ipv4Address << " and " << *i);
|
8751
|
589 |
nextHop = *(++i);
|
|
590 |
return nextHop;
|
|
591 |
}
|
|
592 |
}
|
|
593 |
}
|
|
594 |
NS_LOG_DEBUG ("Next hop address not found");
|
|
595 |
Ipv4Address none = "0.0.0.0";
|
|
596 |
return none;
|
|
597 |
}
|
|
598 |
|
|
599 |
Ptr<Ipv4Route>
|
|
600 |
DsrRouting::SetRoute (Ipv4Address nextHop, Ipv4Address srcAddress)
|
|
601 |
{
|
|
602 |
NS_LOG_FUNCTION (this << nextHop << srcAddress);
|
|
603 |
m_ipv4Route = Create<Ipv4Route> ();
|
|
604 |
m_ipv4Route->SetDestination (nextHop);
|
|
605 |
m_ipv4Route->SetGateway (nextHop);
|
|
606 |
m_ipv4Route->SetSource (srcAddress);
|
|
607 |
return m_ipv4Route;
|
|
608 |
}
|
|
609 |
|
|
610 |
int
|
|
611 |
DsrRouting::GetProtocolNumber (void) const
|
|
612 |
{
|
|
613 |
// / This is the protocol number for DSR which is 48
|
|
614 |
return PROT_NUMBER;
|
|
615 |
}
|
|
616 |
|
8752
|
617 |
uint16_t
|
8751
|
618 |
DsrRouting::GetIDfromIP (Ipv4Address address)
|
|
619 |
{
|
|
620 |
int32_t nNodes = NodeList::GetNNodes ();
|
|
621 |
for (int32_t i = 0; i < nNodes; ++i)
|
|
622 |
{
|
|
623 |
Ptr<Node> node = NodeList::GetNode (i);
|
|
624 |
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
|
|
625 |
if (ipv4->GetAddress (1, 0).GetLocal () == address)
|
|
626 |
{
|
8752
|
627 |
return uint16_t(i);
|
8751
|
628 |
}
|
|
629 |
}
|
8752
|
630 |
return 256;
|
8751
|
631 |
}
|
|
632 |
|
|
633 |
Ipv4Address
|
8752
|
634 |
DsrRouting::GetIPfromID (uint16_t id)
|
8751
|
635 |
{
|
8752
|
636 |
if (id >= 256)
|
8751
|
637 |
{
|
|
638 |
NS_LOG_DEBUG ("Exceed the node range");
|
|
639 |
return "0.0.0.0";
|
|
640 |
}
|
|
641 |
else
|
|
642 |
{
|
8752
|
643 |
Ptr<Node> node = NodeList::GetNode (uint32_t (id));
|
8751
|
644 |
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
|
|
645 |
return ipv4->GetAddress (1, 0).GetLocal ();
|
|
646 |
}
|
|
647 |
}
|
|
648 |
|
8752
|
649 |
uint32_t
|
|
650 |
DsrRouting::GetPriority (DsrMessageType messageType)
|
|
651 |
{
|
|
652 |
if (messageType == DSR_CONTROL_PACKET)
|
|
653 |
{
|
|
654 |
return 0;
|
|
655 |
}
|
|
656 |
else
|
|
657 |
{
|
|
658 |
return 1;
|
|
659 |
}
|
|
660 |
}
|
|
661 |
|
8751
|
662 |
void DsrRouting::SendRerrWhenBreaksLinkToNextHop (Ipv4Address nextHop, uint8_t protocol)
|
|
663 |
{
|
|
664 |
NS_LOG_FUNCTION (this << nextHop << (uint32_t)protocol);
|
|
665 |
MaintainBuffEntry entry;
|
|
666 |
// Find the packet in send buffer
|
|
667 |
if (m_maintainBuffer.Find (nextHop))
|
|
668 |
{
|
|
669 |
NS_LOG_DEBUG ("Trying to dequeue");
|
|
670 |
|
|
671 |
if (m_maintainBuffer.Dequeue (nextHop, entry))
|
|
672 |
{
|
|
673 |
NS_LOG_DEBUG ("creating new packet");
|
|
674 |
/*
|
|
675 |
* Copy the packet and save a copy to the send buffer.
|
8752
|
676 |
* if only queue the original packet to the buffer,
|
8751
|
677 |
* when dequeue the packet, it turns to be empty.
|
|
678 |
*/
|
|
679 |
Ptr<Packet> dequeP = ConstCast<Packet> (entry.GetPacket ());
|
|
680 |
Ptr<Packet> newPacket = dequeP->Copy ();
|
|
681 |
Ptr<Packet> p = dequeP->Copy ();
|
|
682 |
|
|
683 |
Ipv4Address source = entry.GetSrc ();
|
|
684 |
Ipv4Address destination = entry.GetDst ();
|
|
685 |
|
8752
|
686 |
// Send the data packet out before schedule the next packet transmission
|
|
687 |
SendPacket (dequeP, source, nextHop, protocol);
|
|
688 |
|
8751
|
689 |
DsrRoutingHeader dsrRoutingHeader;
|
|
690 |
p->RemoveHeader (dsrRoutingHeader);
|
8752
|
691 |
Ptr<Packet> cleanP = p->Copy ();
|
8751
|
692 |
uint8_t offset = dsrRoutingHeader.GetDsrOptionsOffset ();
|
|
693 |
newPacket->RemoveAtStart (offset);
|
|
694 |
|
|
695 |
// Get the number of routers' address field
|
|
696 |
uint8_t buf[2];
|
|
697 |
newPacket->CopyData (buf, sizeof(buf));
|
|
698 |
uint8_t numberAddress = (buf[1] - 2) / 4;
|
|
699 |
|
|
700 |
DsrOptionSRHeader sourceRoute;
|
|
701 |
sourceRoute.SetNumberAddress (numberAddress);
|
|
702 |
newPacket->RemoveHeader (sourceRoute);
|
|
703 |
uint8_t salvage = sourceRoute.GetSalvage ();
|
|
704 |
|
8752
|
705 |
// TODO
|
8751
|
706 |
DsrOptionAckReqHeader ackReq;
|
|
707 |
newPacket->RemoveHeader (ackReq);
|
|
708 |
/*
|
|
709 |
* Get the node list address
|
|
710 |
*/
|
|
711 |
std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
|
8752
|
712 |
Ipv4Address address1 = nodeList[1];
|
8751
|
713 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodeList);
|
|
714 |
NS_LOG_DEBUG ("The next hop address" << nextHop);
|
|
715 |
if (nextHop == "0.0.0.0")
|
|
716 |
{
|
8752
|
717 |
PacketNewRoute (cleanP, m_mainAddress, destination, protocol);
|
8751
|
718 |
return;
|
|
719 |
}
|
|
720 |
RouteCacheEntry salvageRoute;
|
|
721 |
bool findRoute = m_routeCache->LookupRoute (destination, salvageRoute);
|
|
722 |
// Check the salvage value in header, if salvage is needed, we should find alternative route
|
|
723 |
if (findRoute && (salvage < m_maxSalvageCount))
|
|
724 |
{
|
|
725 |
// Need to salvage the packet instead of discard it
|
|
726 |
std::vector<Ipv4Address> nodeList = salvageRoute.GetVector ();
|
|
727 |
DsrOptionSRHeader newSR;
|
|
728 |
newSR.SetNodesAddress (nodeList);
|
|
729 |
newSR.SetSegmentsLeft ((nodeList.size () - 2));
|
|
730 |
newSR.SetSalvage (salvage + 1);
|
|
731 |
if (m_routeCache->IsLinkCache ())
|
|
732 |
{
|
|
733 |
m_routeCache->UseExtends (nodeList);
|
|
734 |
}
|
8752
|
735 |
|
|
736 |
NetworkKey networkKey;
|
|
737 |
networkKey.m_ackId = entry.GetAckId ();
|
|
738 |
networkKey.m_ourAdd = entry.GetOurAdd ();
|
|
739 |
networkKey.m_nextHop = entry.GetNextHop ();
|
|
740 |
networkKey.m_source = entry.GetSrc ();
|
|
741 |
networkKey.m_destination = entry.GetDst ();
|
|
742 |
|
|
743 |
PassiveKey passiveKey;
|
|
744 |
passiveKey.m_ackId = 0;
|
|
745 |
passiveKey.m_source = entry.GetSrc ();
|
|
746 |
passiveKey.m_destination = entry.GetDst ();
|
|
747 |
passiveKey.m_segsLeft = entry.GetSegsLeft ();
|
|
748 |
|
|
749 |
m_addressForwardCnt[networkKey] = 0;
|
|
750 |
m_passiveCnt[passiveKey] = 0;
|
|
751 |
|
|
752 |
if (nextHop != destination)
|
|
753 |
{
|
|
754 |
SchedulePassivePacketRetry (entry, false, protocol);
|
|
755 |
}
|
|
756 |
else
|
|
757 |
{
|
|
758 |
// This is the first network retry
|
|
759 |
ScheduleNetworkPacketRetry (entry, true, protocol);
|
|
760 |
}
|
8751
|
761 |
}
|
|
762 |
else
|
|
763 |
{
|
|
764 |
/*
|
|
765 |
* This code block create a packet and attach a route error option to it
|
|
766 |
*/
|
|
767 |
m_routeCache->DeleteAllRoutesIncludeLink (source, nextHop, m_mainAddress);
|
|
768 |
/*
|
|
769 |
* If the salvage is not 0, use the first address in the route as the error dst in error header
|
|
770 |
* otherwise use the source of packet as the error destination
|
|
771 |
*/
|
8752
|
772 |
Ipv4Address errorDst;
|
8751
|
773 |
if (salvage)
|
|
774 |
{
|
8752
|
775 |
errorDst = address1;
|
8751
|
776 |
}
|
|
777 |
else
|
|
778 |
{
|
8752
|
779 |
errorDst = source;
|
8751
|
780 |
}
|
8752
|
781 |
SendUnreachError (nextHop, errorDst, destination, salvage, protocol);
|
|
782 |
/*
|
|
783 |
* here we cancel the packet retransmission time for all the packets have next hop address
|
|
784 |
* as nextHop
|
|
785 |
*/
|
8751
|
786 |
}
|
|
787 |
if (m_maintainBuffer.GetSize () != 0 && m_maintainBuffer.Find (nextHop))
|
|
788 |
{
|
|
789 |
Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
|
|
790 |
&DsrRouting::SendRerrWhenBreaksLinkToNextHop,this,nextHop,protocol);
|
|
791 |
}
|
|
792 |
}
|
|
793 |
}
|
|
794 |
}
|
|
795 |
|
|
796 |
void DsrRouting::SendBuffTimerExpire ()
|
|
797 |
{
|
|
798 |
if (m_sendBuffTimer.IsRunning ())
|
|
799 |
{
|
|
800 |
m_sendBuffTimer.Cancel ();
|
|
801 |
}
|
|
802 |
m_sendBuffTimer.Schedule (m_sendBuffInterval);
|
|
803 |
CheckSendBuffer ();
|
|
804 |
}
|
|
805 |
|
|
806 |
void DsrRouting::CheckSendBuffer ()
|
|
807 |
{
|
|
808 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
809 |
<< " Checking send buffer at " << m_mainAddress << " with size " << m_sendBuffer.GetSize ());
|
|
810 |
|
|
811 |
for (std::vector<SendBuffEntry>::iterator i = m_sendBuffer.GetBuffer ().begin (); i != m_sendBuffer.GetBuffer ().end (); )
|
|
812 |
{
|
|
813 |
NS_LOG_DEBUG ("Here we try to find the data packet in the send buffer");
|
|
814 |
Ipv4Address destination = i->GetDestination ();
|
|
815 |
RouteCacheEntry toDst;
|
|
816 |
bool findRoute = m_routeCache->LookupRoute (destination, toDst);
|
|
817 |
if (findRoute)
|
|
818 |
{
|
|
819 |
NS_LOG_INFO ("We have found a route for the packet");
|
|
820 |
Ptr<const Packet> packet = i->GetPacket ();
|
|
821 |
Ptr<Packet> cleanP = packet->Copy ();
|
|
822 |
uint8_t protocol = i->GetProtocol ();
|
|
823 |
|
|
824 |
m_sendBuffer.GetBuffer ().erase (i);
|
|
825 |
|
|
826 |
DsrRoutingHeader dsrRoutingHeader;
|
|
827 |
Ptr<Packet> copyP = packet->Copy ();
|
8752
|
828 |
Ptr<Packet> dsrPacket = packet->Copy ();
|
|
829 |
dsrPacket->RemoveHeader (dsrRoutingHeader);
|
8751
|
830 |
uint32_t offset = dsrRoutingHeader.GetDsrOptionsOffset ();
|
|
831 |
copyP->RemoveAtStart (offset); // Here the processed size is 8 bytes, which is the fixed sized extension header
|
|
832 |
// The packet to get ipv4 header
|
|
833 |
Ptr<Packet> ipv4P = copyP->Copy ();
|
|
834 |
/*
|
|
835 |
* Peek data to get the option type as well as length and segmentsLeft field
|
|
836 |
*/
|
|
837 |
uint32_t size = copyP->GetSize ();
|
|
838 |
uint8_t *data = new uint8_t[size];
|
|
839 |
copyP->CopyData (data, size);
|
|
840 |
|
|
841 |
uint8_t optionType = 0;
|
|
842 |
optionType = *(data);
|
|
843 |
|
|
844 |
if (optionType == 3)
|
|
845 |
{
|
|
846 |
Ptr<dsr::DsrOptions> dsrOption;
|
|
847 |
DsrOptionHeader dsrOptionHeader;
|
|
848 |
uint8_t errorType = *(data + 2);
|
|
849 |
|
|
850 |
if (errorType == 1) // This is the Route Error Option
|
|
851 |
{
|
|
852 |
DsrOptionRerrUnreachHeader rerr;
|
|
853 |
copyP->RemoveHeader (rerr);
|
|
854 |
NS_ASSERT (copyP->GetSize () == 0);
|
|
855 |
|
|
856 |
DsrOptionRerrUnreachHeader newUnreach;
|
|
857 |
newUnreach.SetErrorType (1);
|
|
858 |
newUnreach.SetErrorSrc (rerr.GetErrorSrc ());
|
|
859 |
newUnreach.SetUnreachNode (rerr.GetUnreachNode ());
|
|
860 |
newUnreach.SetErrorDst (rerr.GetErrorDst ());
|
|
861 |
newUnreach.SetSalvage (rerr.GetSalvage ()); // Set the value about whether to salvage a packet or not
|
|
862 |
|
|
863 |
DsrOptionSRHeader sourceRoute;
|
|
864 |
std::vector<Ipv4Address> errorRoute = toDst.GetVector ();
|
|
865 |
sourceRoute.SetNodesAddress (errorRoute);
|
|
866 |
if (m_routeCache->IsLinkCache ())
|
|
867 |
{
|
|
868 |
m_routeCache->UseExtends (errorRoute);
|
|
869 |
}
|
|
870 |
sourceRoute.SetSegmentsLeft ((errorRoute.size () - 2));
|
|
871 |
uint8_t salvage = 0;
|
|
872 |
sourceRoute.SetSalvage (salvage);
|
|
873 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, errorRoute); // Get the next hop address
|
|
874 |
|
|
875 |
if (nextHop == "0.0.0.0")
|
|
876 |
{
|
8752
|
877 |
PacketNewRoute (dsrPacket, m_mainAddress, destination, protocol);
|
8751
|
878 |
return;
|
|
879 |
}
|
|
880 |
|
|
881 |
SetRoute (nextHop, m_mainAddress);
|
|
882 |
uint8_t length = (sourceRoute.GetLength () + newUnreach.GetLength ());
|
|
883 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
884 |
dsrRoutingHeader.SetMessageType (1);
|
|
885 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
|
|
886 |
dsrRoutingHeader.SetDestId (255);
|
8752
|
887 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
|
8751
|
888 |
dsrRoutingHeader.AddDsrOption (newUnreach);
|
|
889 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
890 |
|
|
891 |
Ptr<Packet> newPacket = Create<Packet> ();
|
|
892 |
newPacket->AddHeader (dsrRoutingHeader); // Add the routing header with rerr and sourceRoute attached to it
|
|
893 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
894 |
m_ipv4Route->SetOutputDevice (dev);
|
8752
|
895 |
|
|
896 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
897 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
898 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
899 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
900 |
|
|
901 |
DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
|
|
902 |
|
|
903 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
904 |
{
|
|
905 |
Scheduler (priority);
|
|
906 |
}
|
|
907 |
else
|
|
908 |
{
|
|
909 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
910 |
}
|
8751
|
911 |
}
|
|
912 |
}
|
|
913 |
else
|
|
914 |
{
|
|
915 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
916 |
dsrRoutingHeader.SetMessageType (2);
|
|
917 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
|
|
918 |
dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
|
|
919 |
|
|
920 |
DsrOptionSRHeader sourceRoute;
|
|
921 |
std::vector<Ipv4Address> nodeList = toDst.GetVector (); // Get the route from the route entry we found
|
|
922 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodeList); // Get the next hop address for the route
|
|
923 |
if (nextHop == "0.0.0.0")
|
|
924 |
{
|
8752
|
925 |
PacketNewRoute (dsrPacket, m_mainAddress, destination, protocol);
|
8751
|
926 |
return;
|
|
927 |
}
|
|
928 |
uint8_t salvage = 0;
|
|
929 |
sourceRoute.SetNodesAddress (nodeList); // Save the whole route in the source route header of the packet
|
|
930 |
sourceRoute.SetSegmentsLeft ((nodeList.size () - 2)); // The segmentsLeft field will indicate the hops to go
|
|
931 |
sourceRoute.SetSalvage (salvage);
|
|
932 |
|
8752
|
933 |
uint8_t length = sourceRoute.GetLength ();
|
|
934 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
8751
|
935 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
936 |
cleanP->AddHeader (dsrRoutingHeader);
|
8752
|
937 |
// Send the data packet out before schedule the next packet transmission
|
|
938 |
SendPacket (cleanP, m_mainAddress, nextHop, protocol);
|
8751
|
939 |
Ptr<const Packet> mtP = cleanP->Copy ();
|
|
940 |
// Put the data packet in the maintenance queue for data packet retransmission
|
|
941 |
MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*Ipv4Address=*/ m_mainAddress, /*nextHop=*/ nextHop,
|
8752
|
942 |
/*source=*/ m_mainAddress, /*destination=*/ destination, /*ackId=*/ 0,
|
|
943 |
/*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
|
8751
|
944 |
bool result = m_maintainBuffer.Enqueue (newEntry); // Enqueue the packet the the maintenance buffer
|
|
945 |
if (result)
|
|
946 |
{
|
8752
|
947 |
NetworkKey networkKey;
|
|
948 |
networkKey.m_ackId = newEntry.GetAckId ();
|
|
949 |
networkKey.m_ourAdd = newEntry.GetOurAdd ();
|
|
950 |
networkKey.m_nextHop = newEntry.GetNextHop ();
|
|
951 |
networkKey.m_source = newEntry.GetSrc ();
|
|
952 |
networkKey.m_destination = newEntry.GetDst ();
|
|
953 |
|
|
954 |
PassiveKey passiveKey;
|
|
955 |
passiveKey.m_ackId = 0;
|
|
956 |
passiveKey.m_source = newEntry.GetSrc ();
|
|
957 |
passiveKey.m_destination = newEntry.GetDst ();
|
|
958 |
passiveKey.m_segsLeft = newEntry.GetSegsLeft ();
|
|
959 |
|
|
960 |
m_addressForwardCnt[networkKey] = 0;
|
|
961 |
m_passiveCnt[passiveKey] = 0;
|
|
962 |
if (nextHop != destination)
|
|
963 |
{
|
|
964 |
SchedulePassivePacketRetry (newEntry, false, protocol);
|
|
965 |
}
|
|
966 |
else
|
|
967 |
{
|
|
968 |
// This is the first network retry
|
|
969 |
ScheduleNetworkPacketRetry (newEntry, true, protocol);
|
|
970 |
}
|
8751
|
971 |
}
|
8752
|
972 |
// we need to suspend the normal timer that checks the send buffer
|
|
973 |
// until we are done sending packets
|
8751
|
974 |
if (!m_sendBuffTimer.IsSuspended ())
|
|
975 |
{
|
|
976 |
m_sendBuffTimer.Suspend ();
|
|
977 |
}
|
8752
|
978 |
Simulator::Schedule (m_sendBuffInterval, &DsrRouting::SendBuffTimerExpire, this);
|
8751
|
979 |
return;
|
|
980 |
}
|
|
981 |
}
|
|
982 |
else
|
|
983 |
{
|
|
984 |
++i;
|
|
985 |
}
|
|
986 |
}
|
|
987 |
//after going through the entire send buffer and send all packets found route,
|
|
988 |
//we need to resume the timer if it has been suspended
|
|
989 |
if (m_sendBuffTimer.IsSuspended ())
|
|
990 |
{
|
|
991 |
NS_LOG_DEBUG ("Resume the send buffer timer");
|
|
992 |
m_sendBuffTimer.Resume ();
|
|
993 |
}
|
|
994 |
}
|
|
995 |
|
|
996 |
bool DsrRouting::PromiscReceive (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, const Address &from,
|
|
997 |
const Address &to, NetDevice::PacketType packetType)
|
|
998 |
{
|
|
999 |
// Receive only IP packets and packets destined for other hosts
|
|
1000 |
if (protocol == Ipv4L3Protocol::PROT_NUMBER && packetType == NetDevice::PACKET_OTHERHOST)
|
|
1001 |
{
|
|
1002 |
Ptr<Packet> p = packet->Copy ();
|
|
1003 |
//pull off IP header
|
|
1004 |
Ipv4Header ipv4Header;
|
|
1005 |
p->RemoveHeader (ipv4Header);
|
|
1006 |
|
|
1007 |
// Process only data packets with DSR header
|
|
1008 |
if (ipv4Header.GetProtocol () == DsrRouting::PROT_NUMBER)
|
|
1009 |
{
|
|
1010 |
//just to minimize debug output
|
|
1011 |
NS_LOG_INFO (this << from << to << packetType << *p);
|
|
1012 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1013 |
//pull of DSR header to check option type
|
|
1014 |
Ptr<Packet> dsrPacket = p->Copy ();
|
|
1015 |
dsrPacket->RemoveHeader (dsrRoutingHeader);
|
|
1016 |
uint8_t offset = dsrRoutingHeader.GetDsrOptionsOffset (); // Get the offset for option header, 4 bytes in this case
|
|
1017 |
uint8_t nextHeader = dsrRoutingHeader.GetNextHeader ();
|
|
1018 |
uint32_t sourceId = dsrRoutingHeader.GetSourceId ();
|
|
1019 |
Ipv4Address source = GetIPfromID (sourceId);
|
|
1020 |
|
|
1021 |
// This packet is used to peek option type
|
|
1022 |
p->RemoveAtStart (offset);
|
|
1023 |
/*
|
|
1024 |
* Peek data to get the option type as well as length and segmentsLeft field
|
|
1025 |
*/
|
|
1026 |
uint32_t size = p->GetSize ();
|
|
1027 |
uint8_t *data = new uint8_t[size];
|
|
1028 |
p->CopyData (data, size);
|
|
1029 |
uint8_t optionType = 0;
|
8753
|
1030 |
optionType = *(data);
|
8751
|
1031 |
|
|
1032 |
Ptr<dsr::DsrOptions> dsrOption;
|
|
1033 |
|
|
1034 |
if (optionType == 96) // This is the source route option
|
|
1035 |
{
|
|
1036 |
dsrOption = GetOption (optionType); // Get the relative DSR option and demux to the process function
|
|
1037 |
Ipv4Address fromAddr = GetIPfromMAC (Mac48Address::ConvertFrom (from));
|
|
1038 |
Ipv4Address toAddr = GetIPfromMAC (Mac48Address::ConvertFrom (to));
|
|
1039 |
|
|
1040 |
NS_LOG_DEBUG (Simulator::Now ().GetSeconds ()
|
|
1041 |
<< " DSR node " << m_mainAddress <<
|
|
1042 |
" overhearing packet PID: " << p->GetUid () << " from " << fromAddr << " to " << toAddr <<
|
|
1043 |
" with source IP " << ipv4Header.GetSource () <<
|
|
1044 |
" and destination IP " << ipv4Header.GetDestination () <<
|
|
1045 |
" and packet : " << *dsrPacket);
|
|
1046 |
bool isPromisc = true; // Set the boolean value isPromisc as true
|
8753
|
1047 |
dsrOption->Process (p, dsrPacket, m_mainAddress, source, ipv4Header, nextHeader, isPromisc);
|
8751
|
1048 |
return true;
|
|
1049 |
}
|
|
1050 |
}
|
|
1051 |
}
|
|
1052 |
return false;
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
void
|
|
1056 |
DsrRouting::PacketNewRoute (Ptr<Packet> packet,
|
|
1057 |
Ipv4Address source,
|
|
1058 |
Ipv4Address destination,
|
|
1059 |
uint8_t protocol)
|
|
1060 |
{
|
|
1061 |
NS_LOG_FUNCTION (this << packet << source << destination << (uint32_t)protocol);
|
|
1062 |
// Look up routes for the specific destination
|
|
1063 |
RouteCacheEntry toDst;
|
|
1064 |
bool findRoute = m_routeCache->LookupRoute (destination, toDst);
|
|
1065 |
// Queue the packet if there is no route pre-existing
|
|
1066 |
if (!findRoute)
|
|
1067 |
{
|
|
1068 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
1069 |
<< "s " << m_mainAddress << " there is no route for this packet, queue the packet");
|
|
1070 |
|
|
1071 |
Ptr<Packet> p = packet->Copy ();
|
|
1072 |
SendBuffEntry newEntry (p, destination, m_sendBufferTimeout, protocol); // Create a new entry for send buffer
|
|
1073 |
bool result = m_sendBuffer.Enqueue (newEntry); // Enqueue the packet in send buffer
|
|
1074 |
if (result)
|
|
1075 |
{
|
|
1076 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
1077 |
<< "s Add packet PID: " << packet->GetUid () << " to queue. Packet: " << *packet);
|
|
1078 |
|
|
1079 |
NS_LOG_LOGIC ("Send RREQ to" << destination);
|
|
1080 |
if ((m_addressReqTimer.find (destination) == m_addressReqTimer.end ()) && (m_nonPropReqTimer.find (destination) == m_nonPropReqTimer.end ()))
|
|
1081 |
{
|
|
1082 |
/*
|
|
1083 |
* Call the send request function, it will update the request table entry and ttl there
|
|
1084 |
*/
|
|
1085 |
SendInitialRequest (source, destination, protocol);
|
|
1086 |
}
|
|
1087 |
}
|
|
1088 |
}
|
|
1089 |
else
|
|
1090 |
{
|
|
1091 |
Ptr<Packet> cleanP = packet->Copy ();
|
|
1092 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1093 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
1094 |
dsrRoutingHeader.SetMessageType (2);
|
|
1095 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
|
|
1096 |
dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
|
|
1097 |
|
|
1098 |
DsrOptionSRHeader sourceRoute;
|
|
1099 |
std::vector<Ipv4Address> nodeList = toDst.GetVector (); // Get the route from the route entry we found
|
|
1100 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodeList); // Get the next hop address for the route
|
|
1101 |
if (nextHop == "0.0.0.0")
|
|
1102 |
{
|
|
1103 |
PacketNewRoute (cleanP, source, destination, protocol);
|
|
1104 |
return;
|
|
1105 |
}
|
|
1106 |
uint8_t salvage = 0;
|
|
1107 |
sourceRoute.SetNodesAddress (nodeList); // Save the whole route in the source route header of the packet
|
|
1108 |
if (m_routeCache->IsLinkCache ())
|
|
1109 |
{
|
|
1110 |
m_routeCache->UseExtends (nodeList);
|
|
1111 |
}
|
|
1112 |
sourceRoute.SetSegmentsLeft ((nodeList.size () - 2)); // The segmentsLeft field will indicate the hops to go
|
|
1113 |
sourceRoute.SetSalvage (salvage);
|
|
1114 |
|
8752
|
1115 |
uint8_t length = sourceRoute.GetLength ();
|
|
1116 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
8751
|
1117 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
1118 |
cleanP->AddHeader (dsrRoutingHeader);
|
8752
|
1119 |
// Send the data packet out before schedule the next packet transmission
|
|
1120 |
SendPacket (cleanP, source, nextHop, protocol);
|
8751
|
1121 |
Ptr<const Packet> mtP = cleanP->Copy ();
|
|
1122 |
SetRoute (nextHop, m_mainAddress);
|
|
1123 |
// Put the data packet in the maintenance queue for data packet retransmission
|
|
1124 |
MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*Ipv4Address=*/ m_mainAddress, /*nextHop=*/ nextHop,
|
8752
|
1125 |
/*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
|
|
1126 |
/*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
|
8751
|
1127 |
bool result = m_maintainBuffer.Enqueue (newEntry); // Enqueue the packet the the maintenance buffer
|
|
1128 |
|
|
1129 |
if (result)
|
|
1130 |
{
|
8752
|
1131 |
NetworkKey networkKey;
|
|
1132 |
networkKey.m_ackId = newEntry.GetAckId ();
|
|
1133 |
networkKey.m_ourAdd = newEntry.GetOurAdd ();
|
|
1134 |
networkKey.m_nextHop = newEntry.GetNextHop ();
|
|
1135 |
networkKey.m_source = newEntry.GetSrc ();
|
|
1136 |
networkKey.m_destination = newEntry.GetDst ();
|
|
1137 |
|
|
1138 |
PassiveKey passiveKey;
|
|
1139 |
passiveKey.m_ackId = 0;
|
|
1140 |
passiveKey.m_source = newEntry.GetSrc ();
|
|
1141 |
passiveKey.m_destination = newEntry.GetDst ();
|
|
1142 |
passiveKey.m_segsLeft = newEntry.GetSegsLeft ();
|
|
1143 |
|
|
1144 |
m_addressForwardCnt[networkKey] = 0;
|
|
1145 |
m_passiveCnt[passiveKey] = 0;
|
|
1146 |
if (nextHop != destination)
|
|
1147 |
{
|
|
1148 |
SchedulePassivePacketRetry (newEntry, false, protocol);
|
|
1149 |
}
|
|
1150 |
else
|
|
1151 |
{
|
|
1152 |
// This is the first network retry
|
|
1153 |
ScheduleNetworkPacketRetry (newEntry, true, protocol);
|
|
1154 |
}
|
8751
|
1155 |
}
|
|
1156 |
}
|
|
1157 |
}
|
|
1158 |
|
|
1159 |
void
|
8752
|
1160 |
DsrRouting::SendUnreachError (Ipv4Address errorHop, Ipv4Address destination, Ipv4Address originalDst, uint8_t salvage, uint8_t protocol)
|
8751
|
1161 |
{
|
8752
|
1162 |
NS_LOG_FUNCTION (this << errorHop << destination << originalDst << (uint32_t)salvage << (uint32_t)protocol);
|
8751
|
1163 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1164 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
1165 |
dsrRoutingHeader.SetMessageType (1);
|
|
1166 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
|
|
1167 |
dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
|
|
1168 |
|
|
1169 |
DsrOptionRerrUnreachHeader rerrUnreachHeader;
|
|
1170 |
rerrUnreachHeader.SetErrorType (1);
|
|
1171 |
rerrUnreachHeader.SetErrorSrc (m_mainAddress);
|
|
1172 |
rerrUnreachHeader.SetUnreachNode (errorHop);
|
|
1173 |
rerrUnreachHeader.SetErrorDst (destination);
|
8752
|
1174 |
rerrUnreachHeader.SetOriginalDst (originalDst);
|
|
1175 |
rerrUnreachHeader.SetSalvage (salvage); // Set the value about whether to salvage a packet or not
|
8751
|
1176 |
uint8_t rerrLength = rerrUnreachHeader.GetLength ();
|
|
1177 |
|
|
1178 |
RouteCacheEntry toDst;
|
|
1179 |
bool findRoute = m_routeCache->LookupRoute (destination, toDst);
|
|
1180 |
// Queue the packet if there is no route pre-existing
|
8752
|
1181 |
Ptr<Packet> newPacket = Create<Packet> ();
|
8751
|
1182 |
if (!findRoute)
|
|
1183 |
{
|
|
1184 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
1185 |
<< "s " << m_mainAddress << " there is no route for this packet, queue the packet");
|
|
1186 |
|
|
1187 |
dsrRoutingHeader.SetPayloadLength (rerrLength + 2);
|
|
1188 |
dsrRoutingHeader.AddDsrOption (rerrUnreachHeader);
|
|
1189 |
newPacket->AddHeader (dsrRoutingHeader);
|
|
1190 |
Ptr<Packet> p = newPacket->Copy ();
|
8752
|
1191 |
// Save the error packet in the error buffer
|
|
1192 |
ErrorBuffEntry newEntry (p, destination, m_mainAddress, errorHop, m_sendBufferTimeout, protocol);
|
|
1193 |
bool result = m_errorBuffer.Enqueue (newEntry); // Enqueue the packet in send buffer
|
8751
|
1194 |
if (result)
|
|
1195 |
{
|
|
1196 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
1197 |
<< "s Add packet PID: " << p->GetUid () << " to queue. Packet: " << *p);
|
|
1198 |
NS_LOG_LOGIC ("Send RREQ to" << destination);
|
|
1199 |
if ((m_addressReqTimer.find (destination) == m_addressReqTimer.end ()) && (m_nonPropReqTimer.find (destination) == m_nonPropReqTimer.end ()))
|
|
1200 |
{
|
|
1201 |
NS_LOG_DEBUG ("When there is no existing route request for " << destination << ", initialize one");
|
|
1202 |
/*
|
|
1203 |
* Call the send request function, it will update the request table entry and ttl there
|
|
1204 |
*/
|
|
1205 |
SendInitialRequest (m_mainAddress, destination, protocol);
|
|
1206 |
}
|
|
1207 |
}
|
|
1208 |
}
|
|
1209 |
else
|
|
1210 |
{
|
|
1211 |
std::vector<Ipv4Address> nodeList = toDst.GetVector ();
|
|
1212 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodeList);
|
|
1213 |
if (nextHop == "0.0.0.0")
|
|
1214 |
{
|
8752
|
1215 |
NS_LOG_DEBUG ("The route is not right");
|
|
1216 |
PacketNewRoute (newPacket, m_mainAddress, destination, protocol);
|
8751
|
1217 |
return;
|
|
1218 |
}
|
|
1219 |
DsrOptionSRHeader sourceRoute;
|
|
1220 |
sourceRoute.SetNodesAddress (nodeList);
|
|
1221 |
if (m_routeCache->IsLinkCache ())
|
|
1222 |
{
|
|
1223 |
m_routeCache->UseExtends (nodeList);
|
|
1224 |
}
|
|
1225 |
sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));
|
|
1226 |
uint8_t srLength = sourceRoute.GetLength ();
|
|
1227 |
uint8_t length = (srLength + rerrLength);
|
|
1228 |
|
8752
|
1229 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
|
8751
|
1230 |
dsrRoutingHeader.AddDsrOption (rerrUnreachHeader);
|
|
1231 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
1232 |
newPacket->AddHeader (dsrRoutingHeader);
|
|
1233 |
|
|
1234 |
SetRoute (nextHop, m_mainAddress);
|
|
1235 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
1236 |
m_ipv4Route->SetOutputDevice (dev);
|
8752
|
1237 |
NS_LOG_INFO ("Send the packet to the next hop address " << nextHop << " from " << m_mainAddress << " with the size " << newPacket->GetSize());
|
|
1238 |
|
|
1239 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
1240 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
1241 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
1242 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
1243 |
|
|
1244 |
DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
|
|
1245 |
|
|
1246 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
1247 |
{
|
|
1248 |
Scheduler (priority);
|
|
1249 |
}
|
|
1250 |
else
|
|
1251 |
{
|
|
1252 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
1253 |
}
|
8751
|
1254 |
}
|
|
1255 |
}
|
|
1256 |
|
|
1257 |
void
|
|
1258 |
DsrRouting::ForwardErrPacket (DsrOptionRerrUnreachHeader &rerr,
|
|
1259 |
DsrOptionSRHeader &sourceRoute,
|
|
1260 |
Ipv4Address nextHop,
|
|
1261 |
uint8_t protocol,
|
|
1262 |
Ptr<Ipv4Route> route)
|
|
1263 |
{
|
|
1264 |
NS_LOG_FUNCTION (this << rerr << sourceRoute << nextHop << (uint32_t)protocol << route);
|
|
1265 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
1266 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1267 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
1268 |
dsrRoutingHeader.SetMessageType (1);
|
|
1269 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (rerr.GetErrorSrc ()));
|
|
1270 |
dsrRoutingHeader.SetDestId (GetIDfromIP (rerr.GetErrorDst ()));
|
|
1271 |
|
|
1272 |
uint8_t length = (sourceRoute.GetLength () + rerr.GetLength ());
|
8752
|
1273 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
|
8751
|
1274 |
dsrRoutingHeader.AddDsrOption (rerr);
|
|
1275 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
1276 |
Ptr<Packet> packet = Create<Packet> ();
|
|
1277 |
packet->AddHeader (dsrRoutingHeader);
|
|
1278 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
1279 |
route->SetOutputDevice (dev);
|
8752
|
1280 |
|
|
1281 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
1282 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
1283 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
1284 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
1285 |
|
|
1286 |
DsrNetworkQueueEntry newEntry (packet, m_mainAddress, nextHop, Simulator::Now (), route);
|
|
1287 |
|
|
1288 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
1289 |
{
|
|
1290 |
Scheduler (priority);
|
|
1291 |
}
|
|
1292 |
else
|
|
1293 |
{
|
|
1294 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
1295 |
}
|
8751
|
1296 |
}
|
|
1297 |
|
|
1298 |
void
|
|
1299 |
DsrRouting::Send (Ptr<Packet> packet,
|
|
1300 |
Ipv4Address source,
|
|
1301 |
Ipv4Address destination,
|
|
1302 |
uint8_t protocol,
|
|
1303 |
Ptr<Ipv4Route> route)
|
|
1304 |
{
|
|
1305 |
NS_LOG_FUNCTION (this << packet << source << destination << (uint32_t)protocol << route);
|
|
1306 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
1307 |
|
|
1308 |
if (protocol == 1)
|
|
1309 |
{
|
|
1310 |
NS_LOG_INFO ("Drop packet. Not handling ICMP packet for now");
|
|
1311 |
}
|
|
1312 |
else
|
|
1313 |
{
|
|
1314 |
// Look up routes for the specific destination
|
|
1315 |
RouteCacheEntry toDst;
|
|
1316 |
bool findRoute = m_routeCache->LookupRoute (destination, toDst);
|
|
1317 |
// Queue the packet if there is no route pre-existing
|
|
1318 |
if (!findRoute)
|
|
1319 |
{
|
|
1320 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
1321 |
<< "s " << m_mainAddress << " there is no route for this packet, queue the packet");
|
|
1322 |
|
|
1323 |
Ptr<Packet> p = packet->Copy ();
|
|
1324 |
SendBuffEntry newEntry (p, destination, m_sendBufferTimeout, protocol); // Create a new entry for send buffer
|
|
1325 |
bool result = m_sendBuffer.Enqueue (newEntry); // Enqueue the packet in send buffer
|
|
1326 |
if (result)
|
|
1327 |
{
|
|
1328 |
NS_LOG_INFO (Simulator::Now ().GetSeconds ()
|
|
1329 |
<< "s Add packet PID: " << packet->GetUid () << " to queue. Packet: " << *packet);
|
|
1330 |
NS_LOG_LOGIC ("Send RREQ to " << destination);
|
|
1331 |
// Only when there is no existing route request timer when new route request is scheduled
|
|
1332 |
if ((m_addressReqTimer.find (destination) == m_addressReqTimer.end ()) && (m_nonPropReqTimer.find (destination) == m_nonPropReqTimer.end ()))
|
|
1333 |
{
|
|
1334 |
NS_LOG_DEBUG ("When there is no existing route request for " << destination << ", initialize one");
|
|
1335 |
/*
|
|
1336 |
* Call the send request function, it will update the request table entry and ttl there
|
|
1337 |
*/
|
|
1338 |
SendInitialRequest (source, destination, protocol);
|
|
1339 |
}
|
8752
|
1340 |
else
|
|
1341 |
{
|
|
1342 |
NS_LOG_DEBUG ("There is existing route request timer and the request count here " << m_rreqTable->GetRreqCnt (destination));
|
|
1343 |
}
|
8751
|
1344 |
}
|
|
1345 |
}
|
|
1346 |
else
|
|
1347 |
{
|
|
1348 |
Ptr<Packet> cleanP = packet->Copy ();
|
|
1349 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1350 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
1351 |
dsrRoutingHeader.SetMessageType (2);
|
|
1352 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
|
|
1353 |
dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
|
|
1354 |
|
|
1355 |
DsrOptionSRHeader sourceRoute;
|
|
1356 |
std::vector<Ipv4Address> nodeList = toDst.GetVector (); // Get the route from the route entry we found
|
|
1357 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodeList); // Get the next hop address for the route
|
|
1358 |
if (nextHop == "0.0.0.0")
|
|
1359 |
{
|
|
1360 |
PacketNewRoute (cleanP, source, destination, protocol);
|
|
1361 |
return;
|
|
1362 |
}
|
|
1363 |
uint8_t salvage = 0;
|
|
1364 |
sourceRoute.SetNodesAddress (nodeList); // Save the whole route in the source route header of the packet
|
|
1365 |
if (m_routeCache->IsLinkCache ())
|
|
1366 |
{
|
|
1367 |
m_routeCache->UseExtends (nodeList);
|
|
1368 |
}
|
|
1369 |
sourceRoute.SetSegmentsLeft ((nodeList.size () - 2)); // The segmentsLeft field will indicate the hops to go
|
|
1370 |
sourceRoute.SetSalvage (salvage);
|
|
1371 |
|
8752
|
1372 |
uint8_t length = sourceRoute.GetLength ();
|
8751
|
1373 |
|
8752
|
1374 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
8751
|
1375 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
1376 |
cleanP->AddHeader (dsrRoutingHeader);
|
8752
|
1377 |
// Send the data packet out before schedule the next packet transmission
|
|
1378 |
SendPacket (cleanP, source, nextHop, protocol);
|
8751
|
1379 |
|
|
1380 |
Ptr<const Packet> mtP = cleanP->Copy ();
|
8752
|
1381 |
NS_LOG_DEBUG ("maintain packet size " << cleanP->GetSize());
|
8751
|
1382 |
// Put the data packet in the maintenance queue for data packet retransmission
|
|
1383 |
MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
|
8752
|
1384 |
/*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
|
|
1385 |
/*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
|
8751
|
1386 |
bool result = m_maintainBuffer.Enqueue (newEntry); // Enqueue the packet the the maintenance buffer
|
|
1387 |
if (result)
|
|
1388 |
{
|
8752
|
1389 |
NetworkKey networkKey;
|
|
1390 |
networkKey.m_ackId = newEntry.GetAckId ();
|
|
1391 |
networkKey.m_ourAdd = newEntry.GetOurAdd ();
|
|
1392 |
networkKey.m_nextHop = newEntry.GetNextHop ();
|
|
1393 |
networkKey.m_source = newEntry.GetSrc ();
|
|
1394 |
networkKey.m_destination = newEntry.GetDst ();
|
|
1395 |
|
|
1396 |
PassiveKey passiveKey;
|
|
1397 |
passiveKey.m_ackId = 0;
|
|
1398 |
passiveKey.m_source = newEntry.GetSrc ();
|
|
1399 |
passiveKey.m_destination = newEntry.GetDst ();
|
|
1400 |
passiveKey.m_segsLeft = newEntry.GetSegsLeft ();
|
|
1401 |
|
8751
|
1402 |
m_addressForwardCnt[networkKey] = 0;
|
8752
|
1403 |
m_passiveCnt[passiveKey] = 0;
|
|
1404 |
if (nextHop != destination)
|
|
1405 |
{
|
|
1406 |
SchedulePassivePacketRetry (newEntry, false, protocol);
|
|
1407 |
}
|
|
1408 |
else
|
|
1409 |
{
|
|
1410 |
// This is the first network retry
|
|
1411 |
ScheduleNetworkPacketRetry (newEntry, true, protocol);
|
|
1412 |
}
|
8751
|
1413 |
}
|
|
1414 |
// Try to send packet from *previously* queued entries from send buffer if any
|
8752
|
1415 |
Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
|
|
1416 |
&DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
|
8751
|
1417 |
}
|
|
1418 |
}
|
|
1419 |
}
|
|
1420 |
|
8752
|
1421 |
uint16_t
|
|
1422 |
DsrRouting::AddAckReqHeader (Ptr<Packet>& packet, Ipv4Address nextHop)
|
|
1423 |
{
|
|
1424 |
NS_LOG_FUNCTION (this << packet << nextHop);
|
|
1425 |
// This packet is used to peek option type
|
|
1426 |
Ptr<Packet> dsrP = packet->Copy ();
|
|
1427 |
Ptr<Packet> tmpP = packet->Copy ();
|
|
1428 |
|
|
1429 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1430 |
dsrP->RemoveHeader (dsrRoutingHeader); // Remove the DSR header in whole
|
|
1431 |
uint8_t protocol = dsrRoutingHeader.GetNextHeader ();
|
|
1432 |
uint32_t sourceId = dsrRoutingHeader.GetSourceId ();
|
|
1433 |
uint32_t destinationId = dsrRoutingHeader.GetDestId ();
|
|
1434 |
uint32_t offset = dsrRoutingHeader.GetDsrOptionsOffset ();
|
|
1435 |
tmpP->RemoveAtStart (offset); // Here the processed size is 8 bytes, which is the fixed sized extension header
|
|
1436 |
|
|
1437 |
// Get the number of routers' address field
|
|
1438 |
uint8_t buf[2];
|
|
1439 |
tmpP->CopyData (buf, sizeof(buf));
|
|
1440 |
uint8_t numberAddress = (buf[1] - 2) / 4;
|
|
1441 |
DsrOptionSRHeader sourceRoute;
|
|
1442 |
sourceRoute.SetNumberAddress (numberAddress);
|
|
1443 |
tmpP->RemoveHeader (sourceRoute); // this is a clean packet without any dsr involved headers
|
|
1444 |
|
|
1445 |
DsrOptionAckReqHeader ackReq;
|
|
1446 |
m_ackId = m_routeCache->CheckUniqueAckId (nextHop);
|
|
1447 |
ackReq.SetAckId (m_ackId);
|
|
1448 |
|
|
1449 |
uint8_t length = (sourceRoute.GetLength () + ackReq.GetLength ());
|
|
1450 |
DsrRoutingHeader newDsrRoutingHeader;
|
|
1451 |
newDsrRoutingHeader.SetNextHeader (protocol);
|
|
1452 |
newDsrRoutingHeader.SetMessageType (2);
|
|
1453 |
newDsrRoutingHeader.SetSourceId (sourceId);
|
|
1454 |
newDsrRoutingHeader.SetDestId (destinationId);
|
|
1455 |
newDsrRoutingHeader.SetPayloadLength (length + 4);
|
|
1456 |
newDsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
1457 |
newDsrRoutingHeader.AddDsrOption (ackReq);
|
|
1458 |
dsrP->AddHeader (newDsrRoutingHeader);
|
|
1459 |
// give the dsrP value to packet and then return
|
|
1460 |
packet = dsrP;
|
|
1461 |
return m_ackId;
|
|
1462 |
}
|
|
1463 |
|
8751
|
1464 |
void
|
8752
|
1465 |
DsrRouting::SendPacket (Ptr<Packet> packet, Ipv4Address source, Ipv4Address nextHop, uint8_t protocol)
|
|
1466 |
{
|
|
1467 |
NS_LOG_FUNCTION (this << packet << source << nextHop << (uint32_t)protocol);
|
|
1468 |
// Send out the data packet
|
|
1469 |
m_ipv4Route = SetRoute (nextHop, m_mainAddress);
|
|
1470 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
1471 |
m_ipv4Route->SetOutputDevice (dev);
|
|
1472 |
|
|
1473 |
uint32_t priority = GetPriority (DSR_DATA_PACKET);
|
|
1474 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
1475 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
1476 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
1477 |
|
|
1478 |
DsrNetworkQueueEntry newEntry (packet, source, nextHop, Simulator::Now (), m_ipv4Route);
|
|
1479 |
|
|
1480 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
1481 |
{
|
|
1482 |
Scheduler (priority);
|
|
1483 |
}
|
|
1484 |
else
|
|
1485 |
{
|
|
1486 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
1487 |
}
|
|
1488 |
}
|
|
1489 |
|
|
1490 |
void
|
|
1491 |
DsrRouting::Scheduler (uint32_t priority)
|
|
1492 |
{
|
|
1493 |
NS_LOG_FUNCTION (this);
|
|
1494 |
PriorityScheduler (priority, true);
|
|
1495 |
}
|
|
1496 |
|
|
1497 |
void
|
|
1498 |
DsrRouting::PriorityScheduler (uint32_t priority, bool continueWithFirst)
|
8751
|
1499 |
{
|
8752
|
1500 |
NS_LOG_FUNCTION (this << priority << continueWithFirst);
|
|
1501 |
NS_LOG_DEBUG ("Scheduler looking for packets in network queue");
|
|
1502 |
uint32_t numPriorities;
|
|
1503 |
if (continueWithFirst)
|
|
1504 |
{
|
|
1505 |
numPriorities = 0;
|
|
1506 |
}
|
|
1507 |
else
|
|
1508 |
{
|
|
1509 |
numPriorities = priority;
|
|
1510 |
}
|
|
1511 |
// priorities range from 0 to m_numPriorityQueues, with 0 as the highest priority
|
|
1512 |
for (uint32_t i = priority; numPriorities < m_numPriorityQueues; numPriorities++)
|
|
1513 |
{
|
|
1514 |
std::map<uint32_t, Ptr<DsrNetworkQueue> >::iterator q = m_priorityQueue.find (i);
|
|
1515 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = q->second;
|
|
1516 |
uint32_t queueSize = dsrNetworkQueue->GetSize ();
|
|
1517 |
if (queueSize == 0)
|
|
1518 |
{
|
|
1519 |
if ((i == (m_numPriorityQueues - 1)) && continueWithFirst)
|
|
1520 |
{
|
|
1521 |
i = 0;
|
|
1522 |
}
|
|
1523 |
else
|
|
1524 |
{
|
|
1525 |
i++;
|
|
1526 |
}
|
|
1527 |
}
|
|
1528 |
else
|
|
1529 |
{
|
|
1530 |
uint32_t totalQueueSize;
|
|
1531 |
for (std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator j = m_priorityQueue.begin (); j != m_priorityQueue.end (); j++)
|
|
1532 |
{
|
|
1533 |
NS_LOG_DEBUG ("The size of the network queue for " << j->first << " is " << j->second->GetSize());
|
|
1534 |
totalQueueSize += j->second->GetSize ();
|
|
1535 |
NS_LOG_DEBUG ("And the total size is " << totalQueueSize);
|
|
1536 |
}
|
|
1537 |
if (totalQueueSize > 5)
|
|
1538 |
{
|
|
1539 |
// Here the queue size is larger than 5, we need to increase the retransmission timer for each packet in the network queue
|
|
1540 |
IncreaseRetransTimer ();
|
|
1541 |
}
|
|
1542 |
DsrNetworkQueueEntry newEntry;
|
|
1543 |
dsrNetworkQueue->Dequeue (newEntry);
|
|
1544 |
if (SendRealDown (newEntry))
|
|
1545 |
{
|
|
1546 |
NS_LOG_DEBUG("Packet sent by Dsr. Calling PriorityScheduler after some time");
|
|
1547 |
//packet was successfully sent down. call scheduler after some time
|
|
1548 |
Simulator::Schedule (MicroSeconds (UniformVariable ().GetInteger (0, 1000)),
|
|
1549 |
&DsrRouting::PriorityScheduler,this, i, false);
|
|
1550 |
}
|
|
1551 |
else
|
|
1552 |
{
|
|
1553 |
// packet was dropped by Dsr. Call scheduler immediately so that we can
|
|
1554 |
// send another packet immediately.
|
|
1555 |
NS_LOG_DEBUG("Packet dropped by Dsr. Calling PriorityScheduler immediately");
|
|
1556 |
Simulator::Schedule (Seconds (0), &DsrRouting::PriorityScheduler, this, i, false);
|
|
1557 |
}
|
|
1558 |
if ((i == (m_numPriorityQueues - 1)) && continueWithFirst)
|
|
1559 |
{
|
|
1560 |
i = 0;
|
|
1561 |
}
|
|
1562 |
else
|
|
1563 |
{
|
|
1564 |
i++;
|
|
1565 |
}
|
|
1566 |
}
|
|
1567 |
}
|
|
1568 |
}
|
|
1569 |
|
|
1570 |
void
|
|
1571 |
DsrRouting::IncreaseRetransTimer ()
|
|
1572 |
{
|
|
1573 |
NS_LOG_FUNCTION (this);
|
|
1574 |
// We may want to get the queue first and then we need to save a vector of the entries here and then find
|
|
1575 |
uint32_t priority = GetPriority (DSR_DATA_PACKET);
|
|
1576 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
1577 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
1578 |
|
|
1579 |
std::vector<DsrNetworkQueueEntry> newNetworkQueue = dsrNetworkQueue->GetQueue ();
|
|
1580 |
for (std::vector<DsrNetworkQueueEntry>::iterator i = newNetworkQueue.begin (); i != newNetworkQueue.end (); i++)
|
|
1581 |
{
|
|
1582 |
Ipv4Address nextHop = i->GetNextHopAddress ();
|
|
1583 |
for (std::map<NetworkKey, Timer>::iterator j = m_addressForwardTimer.begin (); j != m_addressForwardTimer.end (); j++)
|
|
1584 |
{
|
|
1585 |
if (nextHop == j->first.m_nextHop)
|
|
1586 |
{
|
|
1587 |
NS_LOG_DEBUG ("The network delay left is " << j->second.GetDelayLeft());
|
|
1588 |
j->second.SetDelay (j->second.GetDelayLeft () + m_retransIncr);
|
|
1589 |
NS_LOG_DEBUG ("The new network delay time is " << j->second.GetDelayLeft ());
|
|
1590 |
}
|
|
1591 |
}
|
|
1592 |
}
|
|
1593 |
}
|
|
1594 |
|
|
1595 |
bool
|
|
1596 |
DsrRouting::SendRealDown (DsrNetworkQueueEntry & newEntry)
|
|
1597 |
{
|
|
1598 |
NS_LOG_FUNCTION (this);
|
|
1599 |
Ipv4Address source = newEntry.GetSourceAddress ();
|
|
1600 |
Ipv4Address nextHop = newEntry.GetNextHopAddress ();
|
|
1601 |
Ptr<Packet> packet = newEntry.GetPacket ()->Copy ();
|
|
1602 |
Ptr<Ipv4Route> route = newEntry.GetIpv4Route ();
|
|
1603 |
m_downTarget (packet, source, nextHop, GetProtocolNumber(), route);
|
|
1604 |
return true;
|
|
1605 |
}
|
|
1606 |
|
|
1607 |
void
|
|
1608 |
DsrRouting::SendPacketFromBuffer (DsrOptionSRHeader const &sourceRoute, Ipv4Address nextHop, uint8_t protocol)
|
|
1609 |
{
|
|
1610 |
NS_LOG_FUNCTION (this << nextHop << (uint32_t)protocol);
|
8751
|
1611 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
1612 |
|
|
1613 |
// Reconstruct the route and Retransmit the data packet
|
|
1614 |
std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
|
|
1615 |
Ipv4Address destination = nodeList.back ();
|
|
1616 |
Ipv4Address source = nodeList.front (); // Get the source address
|
|
1617 |
|
|
1618 |
NS_LOG_INFO ("The nexthop address " << nextHop << " the source " << source << " the destination " << destination);
|
|
1619 |
|
8752
|
1620 |
/*
|
|
1621 |
* Here we try to find data packet from send buffer, if packet with this destiantion found, send it out
|
|
1622 |
*/
|
8751
|
1623 |
if (m_sendBuffer.Find (destination))
|
|
1624 |
{
|
|
1625 |
SendBuffEntry entry;
|
|
1626 |
if (m_sendBuffer.Dequeue (destination, entry))
|
|
1627 |
{
|
|
1628 |
Ptr<Packet> packet = entry.GetPacket ()->Copy ();
|
|
1629 |
NS_LOG_DEBUG ("The queued packet size " << packet->GetSize ());
|
|
1630 |
|
8752
|
1631 |
NS_LOG_DEBUG ("This is the data packet");
|
|
1632 |
Ptr<Packet> p = packet->Copy (); // get a copy of the packet
|
|
1633 |
// Set the source route option
|
|
1634 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1635 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
1636 |
dsrRoutingHeader.SetMessageType (2);
|
|
1637 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
|
|
1638 |
dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
|
|
1639 |
|
|
1640 |
uint8_t length = sourceRoute.GetLength ();
|
|
1641 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
|
1642 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
1643 |
|
|
1644 |
p->AddHeader (dsrRoutingHeader);
|
|
1645 |
// Send the data packet out before schedule the next packet transmission
|
|
1646 |
NS_LOG_DEBUG ("Send out the data packet");
|
|
1647 |
SendPacket (p, source, nextHop, protocol);
|
|
1648 |
|
|
1649 |
Ptr<const Packet> mtP = p->Copy ();
|
|
1650 |
// Put the data packet in the maintenance queue for data packet retransmission
|
|
1651 |
MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
|
|
1652 |
/*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
|
|
1653 |
/*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
|
|
1654 |
bool result = m_maintainBuffer.Enqueue (newEntry); // Enqueue the packet the the maintenance buffer
|
|
1655 |
|
|
1656 |
if (result)
|
|
1657 |
{
|
|
1658 |
NetworkKey networkKey;
|
|
1659 |
networkKey.m_ackId = newEntry.GetAckId ();
|
|
1660 |
networkKey.m_ourAdd = newEntry.GetOurAdd ();
|
|
1661 |
networkKey.m_nextHop = newEntry.GetNextHop ();
|
|
1662 |
networkKey.m_source = newEntry.GetSrc ();
|
|
1663 |
networkKey.m_destination = newEntry.GetDst ();
|
|
1664 |
|
|
1665 |
PassiveKey passiveKey;
|
|
1666 |
passiveKey.m_ackId = 0;
|
|
1667 |
passiveKey.m_source = newEntry.GetSrc ();
|
|
1668 |
passiveKey.m_destination = newEntry.GetDst ();
|
|
1669 |
passiveKey.m_segsLeft = newEntry.GetSegsLeft ();
|
|
1670 |
|
|
1671 |
m_addressForwardCnt[networkKey] = 0;
|
|
1672 |
m_passiveCnt[passiveKey] = 0;
|
|
1673 |
if (nextHop != destination)
|
|
1674 |
{
|
|
1675 |
SchedulePassivePacketRetry (newEntry, false, protocol);
|
|
1676 |
}
|
|
1677 |
else
|
|
1678 |
{
|
|
1679 |
// This is the first network retry
|
|
1680 |
ScheduleNetworkPacketRetry (newEntry, true, protocol);
|
|
1681 |
}
|
|
1682 |
}
|
|
1683 |
|
|
1684 |
if (m_sendBuffer.GetSize () != 0 && m_sendBuffer.Find (destination))
|
|
1685 |
{
|
|
1686 |
NS_LOG_DEBUG ("Schedule sending the next packet in send buffer");
|
|
1687 |
Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
|
|
1688 |
&DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
|
|
1689 |
}
|
|
1690 |
}
|
|
1691 |
else
|
|
1692 |
{
|
|
1693 |
NS_LOG_DEBUG ("All queued packets are out-dated for the destination in send buffer");
|
|
1694 |
}
|
|
1695 |
}
|
|
1696 |
/*
|
|
1697 |
* Here we try to find data packet from send buffer, if packet with this destiantion found, send it out
|
|
1698 |
*/
|
|
1699 |
else if (m_errorBuffer.Find (destination))
|
|
1700 |
{
|
|
1701 |
ErrorBuffEntry entry;
|
|
1702 |
if (m_errorBuffer.Dequeue (destination, entry))
|
|
1703 |
{
|
|
1704 |
Ptr<Packet> packet = entry.GetPacket ()->Copy ();
|
|
1705 |
NS_LOG_DEBUG ("The queued packet size " << packet->GetSize ());
|
|
1706 |
|
8751
|
1707 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1708 |
Ptr<Packet> copyP = packet->Copy ();
|
8752
|
1709 |
Ptr<Packet> dsrPacket = packet->Copy ();
|
|
1710 |
dsrPacket->RemoveHeader (dsrRoutingHeader);
|
8751
|
1711 |
uint32_t offset = dsrRoutingHeader.GetDsrOptionsOffset ();
|
|
1712 |
copyP->RemoveAtStart (offset); // Here the processed size is 8 bytes, which is the fixed sized extension header
|
|
1713 |
/*
|
|
1714 |
* Peek data to get the option type as well as length and segmentsLeft field
|
|
1715 |
*/
|
|
1716 |
uint32_t size = copyP->GetSize ();
|
|
1717 |
uint8_t *data = new uint8_t[size];
|
|
1718 |
copyP->CopyData (data, size);
|
|
1719 |
|
|
1720 |
uint8_t optionType = 0;
|
|
1721 |
optionType = *(data);
|
8752
|
1722 |
NS_LOG_DEBUG ("The option type value in send packet " << (uint32_t)optionType);
|
8751
|
1723 |
if (optionType == 3)
|
|
1724 |
{
|
8752
|
1725 |
NS_LOG_DEBUG ("The packet is error packet");
|
8751
|
1726 |
Ptr<dsr::DsrOptions> dsrOption;
|
|
1727 |
DsrOptionHeader dsrOptionHeader;
|
|
1728 |
|
|
1729 |
uint8_t errorType = *(data + 2);
|
8752
|
1730 |
NS_LOG_DEBUG ("The error type");
|
8751
|
1731 |
if (errorType == 1)
|
|
1732 |
{
|
8752
|
1733 |
NS_LOG_DEBUG ("The packet is route error unreach packet");
|
8751
|
1734 |
DsrOptionRerrUnreachHeader rerr;
|
|
1735 |
copyP->RemoveHeader (rerr);
|
|
1736 |
NS_ASSERT (copyP->GetSize () == 0);
|
|
1737 |
uint8_t length = (sourceRoute.GetLength () + rerr.GetLength ());
|
|
1738 |
|
|
1739 |
DsrOptionRerrUnreachHeader newUnreach;
|
|
1740 |
newUnreach.SetErrorType (1);
|
|
1741 |
newUnreach.SetErrorSrc (rerr.GetErrorSrc ());
|
|
1742 |
newUnreach.SetUnreachNode (rerr.GetUnreachNode ());
|
|
1743 |
newUnreach.SetErrorDst (rerr.GetErrorDst ());
|
8752
|
1744 |
newUnreach.SetOriginalDst (rerr.GetOriginalDst ());
|
8751
|
1745 |
newUnreach.SetSalvage (rerr.GetSalvage ()); // Set the value about whether to salvage a packet or not
|
|
1746 |
|
|
1747 |
std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
|
|
1748 |
DsrRoutingHeader newRoutingHeader;
|
|
1749 |
newRoutingHeader.SetNextHeader (protocol);
|
|
1750 |
newRoutingHeader.SetMessageType (1);
|
|
1751 |
newRoutingHeader.SetSourceId (GetIDfromIP (rerr.GetErrorSrc ()));
|
|
1752 |
newRoutingHeader.SetDestId (GetIDfromIP (rerr.GetErrorDst ()));
|
8752
|
1753 |
newRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
|
8751
|
1754 |
newRoutingHeader.AddDsrOption (newUnreach);
|
|
1755 |
newRoutingHeader.AddDsrOption (sourceRoute);
|
|
1756 |
if (m_routeCache->IsLinkCache ())
|
|
1757 |
{
|
|
1758 |
m_routeCache->UseExtends (nodeList);
|
|
1759 |
}
|
|
1760 |
SetRoute (nextHop, m_mainAddress);
|
|
1761 |
Ptr<Packet> newPacket = Create<Packet> ();
|
|
1762 |
newPacket->AddHeader (newRoutingHeader); // Add the extension header with rerr and sourceRoute attached to it
|
|
1763 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
1764 |
m_ipv4Route->SetOutputDevice (dev);
|
|
1765 |
|
8752
|
1766 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
1767 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
1768 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
1769 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
8751
|
1770 |
|
8752
|
1771 |
DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
|
8751
|
1772 |
|
8752
|
1773 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
1774 |
{
|
|
1775 |
Scheduler (priority);
|
|
1776 |
}
|
|
1777 |
else
|
|
1778 |
{
|
|
1779 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
1780 |
}
|
8751
|
1781 |
}
|
|
1782 |
}
|
|
1783 |
|
8752
|
1784 |
if (m_errorBuffer.GetSize () != 0 && m_errorBuffer.Find (destination))
|
|
1785 |
{
|
|
1786 |
NS_LOG_DEBUG ("Schedule sending the next packet in send buffer");
|
|
1787 |
Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
|
|
1788 |
&DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
|
|
1789 |
}
|
8751
|
1790 |
}
|
|
1791 |
}
|
|
1792 |
else
|
|
1793 |
{
|
8752
|
1794 |
NS_LOG_DEBUG ("Packet not found in either the send or error buffer");
|
8751
|
1795 |
}
|
|
1796 |
}
|
|
1797 |
|
|
1798 |
bool
|
8752
|
1799 |
DsrRouting::FindSamePackets (Ptr<Packet> packet, Ipv4Address source, Ipv4Address destination,
|
8751
|
1800 |
uint8_t segsLeft)
|
|
1801 |
{
|
8752
|
1802 |
NS_LOG_FUNCTION (this << packet << source << destination << (uint32_t)segsLeft);
|
8751
|
1803 |
|
|
1804 |
Ptr<Packet> p = packet->Copy ();
|
8752
|
1805 |
// Here the segments left value need to plus one to check the earlier hop maintain buffer entry
|
|
1806 |
MaintainBuffEntry newEntry;
|
|
1807 |
newEntry.SetPacket (p);
|
|
1808 |
newEntry.SetSrc (source);
|
|
1809 |
newEntry.SetDst (destination);
|
|
1810 |
newEntry.SetAckId (0);
|
|
1811 |
newEntry.SetSegsLeft (segsLeft + 1);
|
|
1812 |
|
|
1813 |
if (m_maintainBuffer.PromiscEqual (newEntry))
|
|
1814 |
{
|
|
1815 |
// The PromiscEqual function will remove the maintain buffer entry if equal value found
|
|
1816 |
// It only compares the source and destination address, ackId, and the segments left value
|
|
1817 |
CancelPassivePacketTimer (newEntry);
|
|
1818 |
return true;
|
|
1819 |
}
|
|
1820 |
return false;
|
8751
|
1821 |
}
|
|
1822 |
|
|
1823 |
void
|
|
1824 |
DsrRouting::CallCancelPacketTimer (uint16_t ackId, Ipv4Header const& ipv4Header, Ipv4Address realSrc, Ipv4Address realDst)
|
|
1825 |
{
|
|
1826 |
NS_LOG_FUNCTION (this << (uint32_t)ackId << ipv4Header << realSrc << realDst);
|
|
1827 |
Ipv4Address sender = ipv4Header.GetDestination ();
|
|
1828 |
Ipv4Address receiver = ipv4Header.GetSource ();
|
|
1829 |
/*
|
|
1830 |
* Create a packet to fill maintenance buffer, not used to compare
|
|
1831 |
* The reason is ack header doesn't have the original packet copy
|
|
1832 |
*/
|
|
1833 |
Ptr<Packet> mainP = Create<Packet> ();
|
|
1834 |
MaintainBuffEntry newEntry (/*Packet=*/ mainP, /*ourAddress=*/ sender, /*nextHop=*/ receiver,
|
8752
|
1835 |
/*source=*/ realSrc, /*destination=*/ realDst, /*ackId=*/ ackId,
|
|
1836 |
/*SegsLeft=*/0, /*expire time=*/ Simulator::Now ());
|
8751
|
1837 |
CancelNetworkPacketTimer (newEntry);
|
|
1838 |
}
|
|
1839 |
|
|
1840 |
void
|
|
1841 |
DsrRouting::CancelNetworkPacketTimer (MaintainBuffEntry & mb)
|
|
1842 |
{
|
|
1843 |
NS_LOG_FUNCTION (this);
|
8752
|
1844 |
NetworkKey networkKey;
|
|
1845 |
networkKey.m_ackId = mb.GetAckId ();
|
|
1846 |
networkKey.m_ourAdd = mb.GetOurAdd ();
|
|
1847 |
networkKey.m_nextHop = mb.GetNextHop ();
|
|
1848 |
networkKey.m_source = mb.GetSrc ();
|
|
1849 |
networkKey.m_destination = mb.GetDst ();
|
8751
|
1850 |
/*
|
|
1851 |
* Here we have found the entry for send retries, so we get the value and increase it by one
|
|
1852 |
*/
|
8752
|
1853 |
m_addressForwardCnt[networkKey] = 0;
|
|
1854 |
m_addressForwardCnt.erase (networkKey);
|
8751
|
1855 |
|
|
1856 |
NS_LOG_INFO ("ackId " << mb.GetAckId () << " ourAdd " << mb.GetOurAdd () << " nextHop " << mb.GetNextHop ()
|
|
1857 |
<< " source " << mb.GetSrc () << " destination " << mb.GetDst ()
|
|
1858 |
<< " segsLeft " << (uint32_t)mb.GetSegsLeft ()
|
8752
|
1859 |
);
|
8751
|
1860 |
// Find the network acknowledgment timer
|
8752
|
1861 |
std::map<NetworkKey, Timer>::const_iterator i =
|
|
1862 |
m_addressForwardTimer.find (networkKey);
|
8751
|
1863 |
if (i == m_addressForwardTimer.end ())
|
|
1864 |
{
|
8752
|
1865 |
NS_LOG_INFO ("did not find the packet timer");
|
8751
|
1866 |
}
|
|
1867 |
else
|
|
1868 |
{
|
8752
|
1869 |
NS_LOG_INFO ("did find the packet timer");
|
8751
|
1870 |
/*
|
|
1871 |
* Schedule the packet retry
|
|
1872 |
* Push back the nextHop, source, destination address
|
|
1873 |
*/
|
8752
|
1874 |
m_addressForwardTimer[networkKey].Cancel ();
|
|
1875 |
m_addressForwardTimer[networkKey].Remove ();
|
|
1876 |
if (m_addressForwardTimer[networkKey].IsRunning ())
|
8751
|
1877 |
{
|
8752
|
1878 |
NS_LOG_INFO ("Timer not canceled");
|
8751
|
1879 |
}
|
8752
|
1880 |
m_addressForwardTimer.erase (networkKey);
|
8751
|
1881 |
}
|
8752
|
1882 |
// Erase the maintenance entry
|
|
1883 |
// yet this does not check the segments left value here
|
|
1884 |
if (m_maintainBuffer.NetworkEqual (mb))
|
|
1885 |
{
|
|
1886 |
NS_LOG_INFO ("Remove same maintenance buffer entry based on network acknowledgment");
|
|
1887 |
}
|
8751
|
1888 |
}
|
|
1889 |
|
|
1890 |
void
|
|
1891 |
DsrRouting::CancelPassivePacketTimer (MaintainBuffEntry & mb)
|
|
1892 |
{
|
|
1893 |
NS_LOG_FUNCTION (this);
|
8752
|
1894 |
PassiveKey passiveKey;
|
8751
|
1895 |
passiveKey.m_ackId = 0;
|
|
1896 |
passiveKey.m_source = mb.GetSrc ();
|
|
1897 |
passiveKey.m_destination = mb.GetDst ();
|
|
1898 |
passiveKey.m_segsLeft = mb.GetSegsLeft ();
|
|
1899 |
|
8752
|
1900 |
m_passiveCnt[passiveKey] = 0;
|
|
1901 |
m_passiveCnt.erase (passiveKey);
|
8751
|
1902 |
|
|
1903 |
// Find the passive acknowledgment timer
|
8752
|
1904 |
std::map<PassiveKey, Timer>::const_iterator j =
|
|
1905 |
m_passiveAckTimer.find (passiveKey);
|
8751
|
1906 |
if (j == m_passiveAckTimer.end ())
|
|
1907 |
{
|
8752
|
1908 |
NS_LOG_INFO ("did not find the passive timer");
|
8751
|
1909 |
}
|
|
1910 |
else
|
|
1911 |
{
|
8752
|
1912 |
NS_LOG_INFO ("find the passive timer");
|
8751
|
1913 |
/*
|
|
1914 |
* Cancel passive acknowledgment timer
|
|
1915 |
*/
|
|
1916 |
m_passiveAckTimer[passiveKey].Cancel ();
|
|
1917 |
m_passiveAckTimer[passiveKey].Remove ();
|
|
1918 |
if (m_passiveAckTimer[passiveKey].IsRunning ())
|
|
1919 |
{
|
8752
|
1920 |
NS_LOG_INFO ("Timer not canceled");
|
8751
|
1921 |
}
|
|
1922 |
m_passiveAckTimer.erase (passiveKey);
|
|
1923 |
}
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
void
|
|
1927 |
DsrRouting::CancelPacketTimerNextHop (Ipv4Address nextHop, uint8_t protocol)
|
|
1928 |
{
|
|
1929 |
NS_LOG_FUNCTION (this << nextHop << (uint32_t)protocol);
|
|
1930 |
MaintainBuffEntry entry;
|
|
1931 |
if (m_maintainBuffer.Dequeue (nextHop, entry))
|
|
1932 |
{
|
|
1933 |
Ptr<const Packet> packet = entry.GetPacket ()->Copy ();
|
|
1934 |
Ipv4Address source = entry.GetSrc ();
|
|
1935 |
Ipv4Address destination = entry.GetDst ();
|
|
1936 |
/*
|
|
1937 |
* Cancel the packet timer and then salvage the data packet
|
|
1938 |
*/
|
|
1939 |
CancelNetworkPacketTimer (entry);
|
|
1940 |
SalvagePacket (packet, source, destination, protocol);
|
|
1941 |
|
|
1942 |
if (m_maintainBuffer.GetSize () && m_maintainBuffer.Find (nextHop))
|
|
1943 |
{
|
8752
|
1944 |
NS_LOG_INFO ("Cancel the packet timer for next maintenance entry");
|
8751
|
1945 |
Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
|
|
1946 |
&DsrRouting::CancelPacketTimerNextHop,this,nextHop,protocol);
|
|
1947 |
}
|
|
1948 |
}
|
|
1949 |
else
|
|
1950 |
{
|
8752
|
1951 |
NS_LOG_INFO ("Maintenance buffer entry not found");
|
8751
|
1952 |
}
|
|
1953 |
}
|
|
1954 |
|
|
1955 |
void
|
|
1956 |
DsrRouting::SalvagePacket (Ptr<const Packet> packet, Ipv4Address source, Ipv4Address dst, uint8_t protocol)
|
|
1957 |
{
|
|
1958 |
NS_LOG_FUNCTION (this << packet << source << dst << (uint32_t)protocol);
|
|
1959 |
// Create two copies of packet
|
|
1960 |
Ptr<Packet> p = packet->Copy ();
|
|
1961 |
Ptr<Packet> newPacket = packet->Copy ();
|
|
1962 |
// Remove the routing header in a whole to get a clean packet
|
|
1963 |
DsrRoutingHeader dsrRoutingHeader;
|
|
1964 |
p->RemoveHeader (dsrRoutingHeader);
|
|
1965 |
// Remove offset of dsr routing header
|
|
1966 |
uint8_t offset = dsrRoutingHeader.GetDsrOptionsOffset ();
|
|
1967 |
newPacket->RemoveAtStart (offset);
|
|
1968 |
|
|
1969 |
// Get the number of routers' address field
|
|
1970 |
uint8_t buf[2];
|
|
1971 |
newPacket->CopyData (buf, sizeof(buf));
|
|
1972 |
uint8_t numberAddress = (buf[1] - 2) / 4;
|
|
1973 |
|
|
1974 |
DsrOptionSRHeader sourceRoute;
|
|
1975 |
sourceRoute.SetNumberAddress (numberAddress);
|
|
1976 |
newPacket->RemoveHeader (sourceRoute);
|
|
1977 |
uint8_t salvage = sourceRoute.GetSalvage ();
|
8752
|
1978 |
/*
|
|
1979 |
* Look in the route cache for other routes for this destination
|
|
1980 |
*/
|
|
1981 |
RouteCacheEntry toDst;
|
8751
|
1982 |
bool findRoute = m_routeCache->LookupRoute (dst, toDst);
|
|
1983 |
if (findRoute && (salvage < m_maxSalvageCount))
|
|
1984 |
{
|
|
1985 |
NS_LOG_DEBUG ("We have found a route for the packet");
|
|
1986 |
DsrRoutingHeader newDsrRoutingHeader;
|
|
1987 |
newDsrRoutingHeader.SetNextHeader (protocol);
|
|
1988 |
newDsrRoutingHeader.SetMessageType (2);
|
|
1989 |
newDsrRoutingHeader.SetSourceId (GetIDfromIP (source));
|
|
1990 |
newDsrRoutingHeader.SetDestId (GetIDfromIP (dst));
|
|
1991 |
|
|
1992 |
std::vector<Ipv4Address> nodeList = toDst.GetVector (); // Get the route from the route entry we found
|
|
1993 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, nodeList); // Get the next hop address for the route
|
|
1994 |
if (nextHop == "0.0.0.0")
|
|
1995 |
{
|
|
1996 |
PacketNewRoute (p, source, dst, protocol);
|
|
1997 |
return;
|
|
1998 |
}
|
|
1999 |
// Increase the salvage count by 1
|
|
2000 |
salvage++;
|
|
2001 |
DsrOptionSRHeader sourceRoute;
|
|
2002 |
sourceRoute.SetSalvage (salvage);
|
|
2003 |
sourceRoute.SetNodesAddress (nodeList); // Save the whole route in the source route header of the packet
|
8752
|
2004 |
sourceRoute.SetSegmentsLeft ((nodeList.size () - 2)); // The segmentsLeft field will indicate the hops to go
|
|
2005 |
DsrOptionAckReqHeader ackReq;
|
|
2006 |
m_ackId = m_routeCache->CheckUniqueAckId (nextHop);
|
|
2007 |
ackReq.SetAckId (m_ackId);
|
8751
|
2008 |
if (m_routeCache->IsLinkCache ())
|
|
2009 |
{
|
|
2010 |
m_routeCache->UseExtends (nodeList);
|
|
2011 |
}
|
|
2012 |
|
|
2013 |
uint8_t length = (sourceRoute.GetLength () + ackReq.GetLength ());
|
|
2014 |
NS_LOG_INFO ("length of source route header " << (uint32_t)(sourceRoute.GetLength ())
|
|
2015 |
<< " length of ack request header " << (uint32_t)(ackReq.GetLength ()));
|
8752
|
2016 |
newDsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
|
8751
|
2017 |
newDsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
2018 |
newDsrRoutingHeader.AddDsrOption (ackReq);
|
|
2019 |
p->AddHeader (newDsrRoutingHeader);
|
|
2020 |
|
|
2021 |
SetRoute (nextHop, m_mainAddress);
|
|
2022 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
2023 |
m_ipv4Route->SetOutputDevice (dev);
|
|
2024 |
// Send out the data packet
|
8752
|
2025 |
|
|
2026 |
uint32_t priority = GetPriority (DSR_DATA_PACKET);
|
|
2027 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
2028 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
2029 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
2030 |
|
|
2031 |
DsrNetworkQueueEntry newEntry (p, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
|
|
2032 |
|
|
2033 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
2034 |
{
|
|
2035 |
Scheduler (priority);
|
|
2036 |
}
|
|
2037 |
else
|
|
2038 |
{
|
|
2039 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
2040 |
}
|
8751
|
2041 |
/*
|
|
2042 |
* Mark the next hop address in blacklist
|
|
2043 |
*/
|
|
2044 |
// NS_LOG_DEBUG ("Save the next hop node in blacklist");
|
|
2045 |
// m_rreqTable->MarkLinkAsUnidirectional (nextHop, m_blacklistTimeout);
|
|
2046 |
}
|
|
2047 |
else
|
|
2048 |
{
|
|
2049 |
NS_LOG_DEBUG ("Will not salvage this packet, silently drop");
|
|
2050 |
}
|
|
2051 |
}
|
|
2052 |
|
|
2053 |
void
|
8752
|
2054 |
DsrRouting::SchedulePassivePacketRetry (MaintainBuffEntry & mb,
|
|
2055 |
bool onlyPassive,
|
|
2056 |
uint8_t protocol)
|
8751
|
2057 |
{
|
8752
|
2058 |
NS_LOG_FUNCTION (this << onlyPassive << (uint32_t)protocol);
|
8751
|
2059 |
|
8752
|
2060 |
PassiveKey passiveKey;
|
|
2061 |
passiveKey.m_ackId = 0;
|
|
2062 |
passiveKey.m_source = mb.GetSrc ();
|
|
2063 |
passiveKey.m_destination = mb.GetDst ();
|
|
2064 |
passiveKey.m_segsLeft = mb.GetSegsLeft ();
|
8751
|
2065 |
|
8752
|
2066 |
if (m_passiveAckTimer.find (passiveKey) == m_passiveAckTimer.end ())
|
8751
|
2067 |
{
|
8752
|
2068 |
Timer timer (Timer::CANCEL_ON_DESTROY);
|
|
2069 |
m_passiveAckTimer[passiveKey] = timer;
|
8751
|
2070 |
}
|
8752
|
2071 |
NS_LOG_DEBUG ("The passive acknowledgment option for data packet");
|
|
2072 |
m_passiveAckTimer[passiveKey].SetFunction (&DsrRouting::PassiveScheduleTimerExpire, this);
|
|
2073 |
m_passiveAckTimer[passiveKey].Remove ();
|
|
2074 |
m_passiveAckTimer[passiveKey].SetArguments (mb, onlyPassive, protocol);
|
|
2075 |
m_passiveAckTimer[passiveKey].Schedule (m_passiveAckTimeout);
|
|
2076 |
}
|
|
2077 |
|
|
2078 |
void
|
|
2079 |
DsrRouting::ScheduleNetworkPacketRetry (MaintainBuffEntry & mb,
|
|
2080 |
bool isFirst,
|
|
2081 |
uint8_t protocol)
|
|
2082 |
{
|
|
2083 |
Ptr<Packet> p = Create<Packet> ();
|
|
2084 |
Ptr<Packet> dsrP = Create<Packet> ();
|
|
2085 |
// The new entry will be used for retransmission
|
|
2086 |
NetworkKey networkKey;
|
|
2087 |
Ipv4Address nextHop = mb.GetNextHop ();
|
|
2088 |
NS_LOG_DEBUG ("is the first retry or not " << isFirst);
|
|
2089 |
if (isFirst)
|
8751
|
2090 |
{
|
8752
|
2091 |
// This is the very first network packet retry
|
|
2092 |
p = mb.GetPacket ()->Copy ();
|
|
2093 |
// Here we add the ack request header to the data packet for network acknowledgement
|
|
2094 |
uint16_t ackId = AddAckReqHeader (p, nextHop);
|
|
2095 |
dsrP = p->Copy ();
|
|
2096 |
MaintainBuffEntry newEntry = mb;
|
|
2097 |
// The function AllEqual will find the exact entry and delete it if found
|
|
2098 |
m_maintainBuffer.AllEqual (mb);
|
|
2099 |
newEntry.SetPacket (dsrP);
|
|
2100 |
newEntry.SetAckId (ackId);
|
|
2101 |
newEntry.SetExpireTime (m_maxMaintainTime);
|
|
2102 |
|
|
2103 |
networkKey.m_ackId = newEntry.GetAckId ();
|
|
2104 |
networkKey.m_ourAdd = newEntry.GetOurAdd ();
|
|
2105 |
networkKey.m_nextHop = newEntry.GetNextHop ();
|
|
2106 |
networkKey.m_source = newEntry.GetSrc ();
|
|
2107 |
networkKey.m_destination = newEntry.GetDst ();
|
|
2108 |
|
|
2109 |
m_addressForwardCnt[networkKey] = 0;
|
|
2110 |
m_maintainBuffer.Enqueue (newEntry);
|
8751
|
2111 |
|
|
2112 |
if (m_addressForwardTimer.find (networkKey) == m_addressForwardTimer.end ())
|
|
2113 |
{
|
|
2114 |
Timer timer (Timer::CANCEL_ON_DESTROY);
|
|
2115 |
m_addressForwardTimer[networkKey] = timer;
|
|
2116 |
}
|
8752
|
2117 |
|
|
2118 |
// After m_tryPassiveAcks, schedule the packet retransmission using network acknowledgment option
|
|
2119 |
m_addressForwardTimer[networkKey].SetFunction (&DsrRouting::NetworkScheduleTimerExpire, this);
|
|
2120 |
m_addressForwardTimer[networkKey].Remove ();
|
|
2121 |
m_addressForwardTimer[networkKey].SetArguments (newEntry, protocol);
|
|
2122 |
NS_LOG_DEBUG ("The packet retries time for " << newEntry.GetAckId() << " is " << m_sendRetries
|
|
2123 |
<< " and the delay time is " << Time (2 * m_nodeTraversalTime));
|
|
2124 |
// Back-off mechanism
|
|
2125 |
m_addressForwardTimer[networkKey].Schedule (Time (2 * m_nodeTraversalTime));
|
|
2126 |
}
|
|
2127 |
else
|
|
2128 |
{
|
|
2129 |
networkKey.m_ackId = mb.GetAckId ();
|
|
2130 |
networkKey.m_ourAdd = mb.GetOurAdd ();
|
|
2131 |
networkKey.m_nextHop = mb.GetNextHop ();
|
|
2132 |
networkKey.m_source = mb.GetSrc ();
|
|
2133 |
networkKey.m_destination = mb.GetDst ();
|
8751
|
2134 |
/*
|
|
2135 |
* Here we have found the entry for send retries, so we get the value and increase it by one
|
|
2136 |
*/
|
|
2137 |
m_sendRetries = m_addressForwardCnt[networkKey];
|
|
2138 |
NS_LOG_DEBUG ("The packet retry we have done " << m_sendRetries);
|
8752
|
2139 |
|
|
2140 |
p = mb.GetPacket ()->Copy ();
|
|
2141 |
dsrP = mb.GetPacket ()->Copy ();
|
|
2142 |
|
|
2143 |
NS_LOG_DEBUG ("The packet with dsr header " << dsrP->GetSize());
|
|
2144 |
networkKey.m_ackId = mb.GetAckId ();
|
|
2145 |
networkKey.m_ourAdd = mb.GetOurAdd ();
|
|
2146 |
networkKey.m_nextHop = mb.GetNextHop ();
|
|
2147 |
networkKey.m_source = mb.GetSrc ();
|
|
2148 |
networkKey.m_destination = mb.GetDst ();
|
8751
|
2149 |
/*
|
|
2150 |
* If a data packet has been attempted SendRetries times at the maximum TTL without
|
|
2151 |
* receiving any ACK, all data packets destined for the corresponding destination SHOULD be
|
|
2152 |
* dropped from the send buffer
|
|
2153 |
*
|
|
2154 |
* The maxMaintRexmt also needs to decrease one for the passive ack packet
|
|
2155 |
*/
|
8752
|
2156 |
/*
|
|
2157 |
* Check if the send retry time for a certain packet has already passed max maintenance retransmission
|
|
2158 |
* time or not
|
|
2159 |
*/
|
8751
|
2160 |
|
8752
|
2161 |
// After m_tryPassiveAcks, schedule the packet retransmission using network acknowledgment option
|
|
2162 |
m_addressForwardTimer[networkKey].SetFunction (&DsrRouting::NetworkScheduleTimerExpire, this);
|
|
2163 |
m_addressForwardTimer[networkKey].Remove ();
|
|
2164 |
m_addressForwardTimer[networkKey].SetArguments (mb, protocol);
|
|
2165 |
NS_LOG_DEBUG ("The packet retries time for " << mb.GetAckId() << " is " << m_sendRetries
|
|
2166 |
<< " and the delay time is " << Time (2 * m_sendRetries * m_nodeTraversalTime));
|
|
2167 |
// Back-off mechanism
|
|
2168 |
m_addressForwardTimer[networkKey].Schedule (Time (2 * m_sendRetries * m_nodeTraversalTime));
|
8751
|
2169 |
}
|
|
2170 |
}
|
|
2171 |
|
|
2172 |
void
|
8752
|
2173 |
DsrRouting::PassiveScheduleTimerExpire (MaintainBuffEntry & mb,
|
|
2174 |
bool onlyPassive,
|
|
2175 |
uint8_t protocol)
|
8751
|
2176 |
{
|
8752
|
2177 |
NS_LOG_FUNCTION (this << onlyPassive << (uint32_t)protocol);
|
8751
|
2178 |
Ipv4Address nextHop = mb.GetNextHop ();
|
|
2179 |
Ipv4Address source = mb.GetSrc ();
|
|
2180 |
Ptr<const Packet> packet = mb.GetPacket ();
|
|
2181 |
SetRoute (nextHop, m_mainAddress);
|
|
2182 |
Ptr<Packet> p = packet->Copy ();
|
8752
|
2183 |
|
|
2184 |
PassiveKey pk;
|
|
2185 |
pk.m_ackId = 0;
|
|
2186 |
pk.m_source = mb.GetSrc ();
|
|
2187 |
pk.m_destination = mb.GetDst ();
|
|
2188 |
pk.m_segsLeft = mb.GetSegsLeft ();
|
|
2189 |
|
|
2190 |
// Cancel passive ack timer
|
|
2191 |
m_passiveAckTimer[pk].Cancel ();
|
|
2192 |
m_passiveAckTimer[pk].Remove ();
|
|
2193 |
if (m_passiveAckTimer[pk].IsRunning ())
|
8751
|
2194 |
{
|
8752
|
2195 |
NS_LOG_DEBUG ("Timer not canceled");
|
|
2196 |
}
|
|
2197 |
m_passiveAckTimer.erase (pk);
|
|
2198 |
// Send the data packet out before schedule the next packet transmission
|
|
2199 |
SendPacket (p, source, nextHop, protocol);
|
|
2200 |
// Increase the send retry times
|
|
2201 |
m_passiveRetries = m_passiveCnt[pk];
|
|
2202 |
if (m_passiveRetries < m_tryPassiveAcks)
|
|
2203 |
{
|
|
2204 |
m_passiveCnt[pk] = ++m_passiveRetries;
|
|
2205 |
SchedulePassivePacketRetry (mb, onlyPassive, protocol);
|
|
2206 |
}
|
|
2207 |
else if (!onlyPassive)
|
|
2208 |
{
|
|
2209 |
// This is the first network acknowledgement retry
|
|
2210 |
// Cancel the passive packet timer now and remove maintenance buffer entry for it
|
|
2211 |
CancelPassivePacketTimer (mb);
|
|
2212 |
ScheduleNetworkPacketRetry (mb, true, protocol);
|
8751
|
2213 |
}
|
|
2214 |
else
|
|
2215 |
{
|
8752
|
2216 |
// This is the end of the data retransmission retries
|
|
2217 |
CancelPassivePacketTimer (mb);
|
|
2218 |
// The function AllEqual will find the exact entry and delete it if found
|
|
2219 |
m_maintainBuffer.AllEqual (mb);
|
8751
|
2220 |
}
|
8752
|
2221 |
}
|
|
2222 |
|
|
2223 |
void
|
|
2224 |
DsrRouting::NetworkScheduleTimerExpire (MaintainBuffEntry & mb,
|
|
2225 |
uint8_t protocol)
|
|
2226 |
{
|
|
2227 |
Ptr<Packet> p = mb.GetPacket ()->Copy ();
|
|
2228 |
Ipv4Address source = mb.GetSrc ();
|
|
2229 |
Ipv4Address nextHop = mb.GetNextHop ();
|
|
2230 |
Ipv4Address dst = mb.GetDst ();
|
|
2231 |
|
|
2232 |
NetworkKey networkKey;
|
|
2233 |
networkKey.m_ackId = mb.GetAckId ();
|
|
2234 |
networkKey.m_ourAdd = mb.GetOurAdd ();
|
|
2235 |
networkKey.m_nextHop = nextHop;
|
|
2236 |
networkKey.m_source = source;
|
|
2237 |
networkKey.m_destination = dst;
|
|
2238 |
|
|
2239 |
// Send the data packet out before schedule the next packet transmission
|
|
2240 |
SendPacket (p, source, nextHop, protocol);
|
|
2241 |
// Increase the send retry times
|
|
2242 |
m_sendRetries = m_addressForwardCnt[networkKey];
|
|
2243 |
NS_LOG_DEBUG ("The send retry time is " << m_sendRetries);
|
|
2244 |
if (m_sendRetries >= m_maxMaintRexmt)
|
|
2245 |
{
|
|
2246 |
Ptr<Packet> dsrP = mb.GetPacket ()->Copy ();
|
|
2247 |
// The packet retries time has exceed the max maintenance retransmission times
|
|
2248 |
NS_LOG_LOGIC ("Packet transmissions to " << nextHop << " has been attempted SendRetries times for " << networkKey.m_ackId);
|
|
2249 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2250 |
dsrP->RemoveHeader (dsrRoutingHeader); // Remove the dsr header in whole
|
|
2251 |
uint32_t offset = dsrRoutingHeader.GetDsrOptionsOffset ();
|
|
2252 |
p->RemoveAtStart (offset);
|
|
2253 |
|
|
2254 |
// Get the number of routers' address field
|
|
2255 |
uint8_t buf[2];
|
|
2256 |
p->CopyData (buf, sizeof(buf));
|
|
2257 |
uint8_t numberAddress = (buf[1] - 2) / 4;
|
|
2258 |
NS_LOG_DEBUG ("The number of addresses " << (uint32_t)numberAddress);
|
|
2259 |
DsrOptionSRHeader sourceRoute;
|
|
2260 |
sourceRoute.SetNumberAddress (numberAddress);
|
|
2261 |
p->RemoveHeader (sourceRoute);
|
|
2262 |
std::vector<Ipv4Address> nodeList = sourceRoute.GetNodesAddress ();
|
|
2263 |
uint8_t salvage = sourceRoute.GetSalvage ();
|
|
2264 |
Ipv4Address address1 = nodeList[1];
|
|
2265 |
NS_LOG_DEBUG ("address1 " << address1);
|
|
2266 |
PrintVector (nodeList);
|
|
2267 |
|
|
2268 |
// Delete all the routes including the links
|
|
2269 |
m_routeCache->DeleteAllRoutesIncludeLink (m_mainAddress, nextHop, m_mainAddress);
|
|
2270 |
/*
|
|
2271 |
* If the salvage is not 0, use the first address in the route as the error dst in error header
|
|
2272 |
* otherwise use the source of packet as the error destination
|
|
2273 |
*/
|
|
2274 |
Ipv4Address errorDst;
|
|
2275 |
if (salvage)
|
|
2276 |
{
|
|
2277 |
errorDst = address1;
|
|
2278 |
}
|
|
2279 |
else
|
|
2280 |
{
|
|
2281 |
errorDst = source;
|
|
2282 |
}
|
|
2283 |
SendUnreachError (nextHop, errorDst, dst, salvage, protocol);
|
|
2284 |
/*
|
|
2285 |
* here we cancel the packet retransmission time for all the packets have next hop address as nextHop
|
|
2286 |
* Also salvage the packet for the all the packet destined for the nextHop address
|
|
2287 |
*/
|
|
2288 |
CancelPacketTimerNextHop (nextHop, protocol);
|
|
2289 |
}
|
|
2290 |
else
|
|
2291 |
{
|
|
2292 |
m_addressForwardCnt[networkKey] = ++m_sendRetries;
|
|
2293 |
ScheduleNetworkPacketRetry (mb, false, protocol);
|
|
2294 |
}
|
8751
|
2295 |
}
|
|
2296 |
|
|
2297 |
void
|
|
2298 |
DsrRouting::ForwardPacket (Ptr<const Packet> packet,
|
|
2299 |
DsrOptionSRHeader &sourceRoute,
|
|
2300 |
Ipv4Header const& ipv4Header,
|
|
2301 |
Ipv4Address source,
|
|
2302 |
Ipv4Address nextHop,
|
|
2303 |
Ipv4Address targetAddress,
|
|
2304 |
uint8_t protocol,
|
|
2305 |
Ptr<Ipv4Route> route)
|
|
2306 |
{
|
|
2307 |
NS_LOG_FUNCTION (this << packet << sourceRoute << source << nextHop << targetAddress << (uint32_t)protocol << route);
|
|
2308 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
2309 |
|
|
2310 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2311 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
2312 |
dsrRoutingHeader.SetMessageType (2);
|
|
2313 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
|
|
2314 |
dsrRoutingHeader.SetDestId (GetIDfromIP (targetAddress));
|
|
2315 |
|
|
2316 |
// We get the salvage value in sourceRoute header and set it to route error header if triggered error
|
|
2317 |
Ptr<Packet> p = packet->Copy ();
|
8752
|
2318 |
uint8_t length = sourceRoute.GetLength ();
|
|
2319 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
|
2320 |
dsrRoutingHeader.AddDsrOption (sourceRoute);
|
|
2321 |
p->AddHeader (dsrRoutingHeader);
|
8751
|
2322 |
|
8752
|
2323 |
// Send the data packet out before schedule the next packet transmission
|
|
2324 |
SendPacket (p, source, nextHop, protocol);
|
|
2325 |
|
8751
|
2326 |
Ptr<const Packet> mtP = p->Copy ();
|
|
2327 |
|
|
2328 |
MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
|
|
2329 |
/*source=*/ source, /*destination=*/ targetAddress,
|
8752
|
2330 |
/*ackId=*/ m_ackId, /*SegsLeft=*/sourceRoute.GetSegmentsLeft(), /*expire time=*/ m_maxMaintainTime);
|
8751
|
2331 |
bool result = m_maintainBuffer.Enqueue (newEntry);
|
|
2332 |
|
|
2333 |
if (result)
|
|
2334 |
{
|
8752
|
2335 |
NetworkKey networkKey;
|
|
2336 |
networkKey.m_ackId = newEntry.GetAckId ();
|
|
2337 |
networkKey.m_ourAdd = newEntry.GetOurAdd ();
|
|
2338 |
networkKey.m_nextHop = newEntry.GetNextHop ();
|
|
2339 |
networkKey.m_source = newEntry.GetSrc ();
|
|
2340 |
networkKey.m_destination = newEntry.GetDst ();
|
|
2341 |
|
|
2342 |
PassiveKey passiveKey;
|
|
2343 |
passiveKey.m_ackId = 0;
|
|
2344 |
passiveKey.m_source = newEntry.GetSrc ();
|
|
2345 |
passiveKey.m_destination = newEntry.GetDst ();
|
|
2346 |
passiveKey.m_segsLeft = newEntry.GetSegsLeft ();
|
|
2347 |
|
8751
|
2348 |
m_addressForwardCnt[networkKey] = 0;
|
8752
|
2349 |
m_passiveCnt[passiveKey] = 0;
|
|
2350 |
if (nextHop != targetAddress)
|
|
2351 |
{
|
|
2352 |
SchedulePassivePacketRetry (newEntry, false, protocol);
|
|
2353 |
}
|
|
2354 |
else
|
|
2355 |
{
|
|
2356 |
// This is the first network retry
|
|
2357 |
ScheduleNetworkPacketRetry (newEntry, true, protocol);
|
|
2358 |
}
|
8751
|
2359 |
}
|
|
2360 |
}
|
|
2361 |
|
|
2362 |
void
|
|
2363 |
DsrRouting::SendInitialRequest (Ipv4Address source,
|
|
2364 |
Ipv4Address destination,
|
|
2365 |
uint8_t protocol)
|
|
2366 |
{
|
|
2367 |
NS_LOG_FUNCTION (this << source << destination << (uint32_t)protocol);
|
|
2368 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
2369 |
Ptr<Packet> packet = Create<Packet> ();
|
|
2370 |
// Create an empty Ipv4 route ptr
|
|
2371 |
Ptr<Ipv4Route> route;
|
|
2372 |
/*
|
|
2373 |
* Construct the route request option header
|
|
2374 |
*/
|
|
2375 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2376 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
2377 |
dsrRoutingHeader.SetMessageType (1);
|
|
2378 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
|
|
2379 |
dsrRoutingHeader.SetDestId (255);
|
|
2380 |
|
8752
|
2381 |
DsrOptionRreqHeader rreqHeader; // has an alignment of 4n+0
|
|
2382 |
rreqHeader.AddNodeAddress (m_mainAddress); // Add our own address in the header
|
8751
|
2383 |
rreqHeader.SetTarget (destination);
|
8752
|
2384 |
m_requestId = m_rreqTable->CheckUniqueRreqId (destination); // Check the Id cache for duplicate ones
|
8751
|
2385 |
rreqHeader.SetId (m_requestId);
|
|
2386 |
|
8752
|
2387 |
dsrRoutingHeader.AddDsrOption (rreqHeader); // Add the rreqHeader to the dsr extension header
|
8751
|
2388 |
uint8_t length = rreqHeader.GetLength ();
|
8752
|
2389 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
8751
|
2390 |
packet->AddHeader (dsrRoutingHeader);
|
|
2391 |
|
|
2392 |
// Schedule the route requests retry with non-propagation set true
|
|
2393 |
bool nonProp = true;
|
8752
|
2394 |
std::vector<Ipv4Address> address;
|
|
2395 |
address.push_back (source);
|
|
2396 |
address.push_back (destination);
|
|
2397 |
/*
|
|
2398 |
* Add the socket ip ttl tag to the packet to limit the scope of route requests
|
|
2399 |
*/
|
|
2400 |
SocketIpTtlTag tag;
|
|
2401 |
tag.SetTtl (0);
|
|
2402 |
Ptr<Packet> nonPropPacket = packet->Copy ();
|
|
2403 |
nonPropPacket->AddPacketTag (tag);
|
|
2404 |
SendRequest (nonPropPacket, source);
|
|
2405 |
// Schedule the next route request
|
|
2406 |
ScheduleRreqRetry (packet, address, nonProp, m_requestId, protocol);
|
8751
|
2407 |
}
|
|
2408 |
|
|
2409 |
void
|
|
2410 |
DsrRouting::SendErrorRequest (DsrOptionRerrUnreachHeader &rerr, uint8_t protocol)
|
|
2411 |
{
|
|
2412 |
NS_LOG_FUNCTION (this << (uint32_t)protocol);
|
|
2413 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
8752
|
2414 |
uint8_t salvage = rerr.GetSalvage ();
|
|
2415 |
Ipv4Address dst = rerr.GetOriginalDst ();
|
|
2416 |
NS_LOG_DEBUG ("our own address here " << m_mainAddress << " error source " << rerr.GetErrorSrc () << " error destination " << rerr.GetErrorDst ()
|
|
2417 |
<< " error next hop " << rerr.GetUnreachNode () << " original dst " << rerr.GetOriginalDst ()
|
|
2418 |
);
|
|
2419 |
RouteCacheEntry toDst;
|
|
2420 |
if (m_routeCache->LookupRoute (dst, toDst))
|
8751
|
2421 |
{
|
|
2422 |
/*
|
8752
|
2423 |
* Found a route the dst, construct the source route option header
|
8751
|
2424 |
*/
|
8752
|
2425 |
DsrOptionSRHeader sourceRoute;
|
|
2426 |
std::vector<Ipv4Address> ip = toDst.GetVector ();
|
|
2427 |
sourceRoute.SetNodesAddress (ip);
|
|
2428 |
if (m_routeCache->IsLinkCache ())
|
|
2429 |
{
|
|
2430 |
m_routeCache->UseExtends (ip);
|
|
2431 |
}
|
|
2432 |
sourceRoute.SetSegmentsLeft ((ip.size () - 2));
|
|
2433 |
sourceRoute.SetSalvage (salvage);
|
|
2434 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, ip); // Get the next hop address
|
|
2435 |
NS_LOG_DEBUG ("The nextHop address " << nextHop);
|
|
2436 |
Ptr<Packet> packet = Create<Packet> ();
|
|
2437 |
if (nextHop == "0.0.0.0")
|
|
2438 |
{
|
|
2439 |
NS_LOG_DEBUG ("Error next hop address");
|
|
2440 |
PacketNewRoute (packet, m_mainAddress, dst, protocol);
|
|
2441 |
return;
|
|
2442 |
}
|
|
2443 |
SetRoute (nextHop, m_mainAddress);
|
|
2444 |
CancelRreqTimer (dst, true);
|
|
2445 |
SendPacketFromBuffer (sourceRoute, nextHop, protocol);
|
|
2446 |
NS_LOG_LOGIC ("Route to " << dst << " found");
|
|
2447 |
return;
|
|
2448 |
}
|
|
2449 |
else
|
|
2450 |
{
|
|
2451 |
NS_LOG_INFO ("No route found, initiate route error request");
|
|
2452 |
Ptr<Packet> packet = Create<Packet> ();
|
|
2453 |
Ipv4Address originalDst = rerr.GetOriginalDst ();
|
|
2454 |
// Create an empty route ptr
|
|
2455 |
Ptr<Ipv4Route> route = 0;
|
|
2456 |
/*
|
|
2457 |
* Construct the route request option header
|
|
2458 |
*/
|
|
2459 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2460 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
2461 |
dsrRoutingHeader.SetMessageType (1);
|
|
2462 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
|
|
2463 |
dsrRoutingHeader.SetDestId (255);
|
|
2464 |
|
8751
|
2465 |
Ptr<Packet> dstP = Create<Packet> ();
|
|
2466 |
DsrOptionRreqHeader rreqHeader; // has an alignment of 4n+0
|
|
2467 |
rreqHeader.AddNodeAddress (m_mainAddress); // Add our own address in the header
|
8752
|
2468 |
rreqHeader.SetTarget (originalDst);
|
|
2469 |
m_requestId = m_rreqTable->CheckUniqueRreqId (originalDst); // Check the Id cache for duplicate ones
|
8751
|
2470 |
rreqHeader.SetId (m_requestId);
|
|
2471 |
|
|
2472 |
dsrRoutingHeader.AddDsrOption (rreqHeader); // Add the rreqHeader to the dsr extension header
|
|
2473 |
dsrRoutingHeader.AddDsrOption (rerr);
|
|
2474 |
uint8_t length = rreqHeader.GetLength () + rerr.GetLength ();
|
8752
|
2475 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
|
8751
|
2476 |
dstP->AddHeader (dsrRoutingHeader);
|
|
2477 |
// Schedule the route requests retry, propagate the route request message as it contains error
|
|
2478 |
bool nonProp = false;
|
8752
|
2479 |
std::vector<Ipv4Address> address;
|
|
2480 |
address.push_back (m_mainAddress);
|
|
2481 |
address.push_back (originalDst);
|
8751
|
2482 |
/*
|
8752
|
2483 |
* Add the socket ip ttl tag to the packet to limit the scope of route requests
|
8751
|
2484 |
*/
|
8752
|
2485 |
SocketIpTtlTag tag;
|
|
2486 |
tag.SetTtl ((uint8_t)m_discoveryHopLimit);
|
|
2487 |
Ptr<Packet> propPacket = dstP->Copy ();
|
|
2488 |
propPacket->AddPacketTag (tag);
|
8751
|
2489 |
|
8752
|
2490 |
if ((m_addressReqTimer.find (originalDst) == m_addressReqTimer.end ()) && (m_nonPropReqTimer.find (originalDst) == m_nonPropReqTimer.end ()))
|
|
2491 |
{
|
|
2492 |
NS_LOG_INFO ("Only when there is no existing route request time when the initial route request is scheduled");
|
|
2493 |
SendRequest (propPacket, m_mainAddress);
|
|
2494 |
ScheduleRreqRetry (dstP, address, nonProp, m_requestId, protocol);
|
|
2495 |
}
|
|
2496 |
else
|
8751
|
2497 |
{
|
8752
|
2498 |
NS_LOG_INFO ("There is existing route request, find the existing route request entry");
|
|
2499 |
/*
|
|
2500 |
* Cancel the route request timer first before scheduling the route request
|
|
2501 |
* in this case, we do not want to remove the route request entry, so the isRemove value is false
|
|
2502 |
*/
|
|
2503 |
CancelRreqTimer (originalDst, false);
|
|
2504 |
ScheduleRreqRetry (dstP, address, nonProp, m_requestId, protocol);
|
8751
|
2505 |
}
|
|
2506 |
}
|
|
2507 |
}
|
|
2508 |
|
|
2509 |
void
|
8752
|
2510 |
DsrRouting::CancelRreqTimer (Ipv4Address dst, bool isRemove)
|
8751
|
2511 |
{
|
8752
|
2512 |
NS_LOG_FUNCTION (this << dst << isRemove);
|
8751
|
2513 |
// Cancel the non propagation request timer if found
|
|
2514 |
if (m_nonPropReqTimer.find (dst) == m_nonPropReqTimer.end ())
|
|
2515 |
{
|
|
2516 |
NS_LOG_DEBUG ("Did not find the non-propagation timer");
|
|
2517 |
}
|
|
2518 |
else
|
|
2519 |
{
|
|
2520 |
NS_LOG_DEBUG ("did find the non-propagation timer");
|
|
2521 |
}
|
|
2522 |
m_nonPropReqTimer[dst].Cancel ();
|
|
2523 |
m_nonPropReqTimer[dst].Remove ();
|
|
2524 |
|
|
2525 |
if (m_nonPropReqTimer[dst].IsRunning ())
|
|
2526 |
{
|
|
2527 |
NS_LOG_DEBUG ("Timer not canceled");
|
|
2528 |
}
|
|
2529 |
m_nonPropReqTimer.erase (dst);
|
|
2530 |
|
|
2531 |
// Cancel the address request timer if found
|
|
2532 |
if (m_addressReqTimer.find (dst) == m_addressReqTimer.end ())
|
|
2533 |
{
|
|
2534 |
NS_LOG_DEBUG ("Did not find the propagation timer");
|
|
2535 |
}
|
|
2536 |
else
|
|
2537 |
{
|
|
2538 |
NS_LOG_DEBUG ("did find the propagation timer");
|
|
2539 |
}
|
|
2540 |
m_addressReqTimer[dst].Cancel ();
|
|
2541 |
m_addressReqTimer[dst].Remove ();
|
|
2542 |
if (m_addressReqTimer[dst].IsRunning ())
|
|
2543 |
{
|
|
2544 |
NS_LOG_DEBUG ("Timer not canceled");
|
|
2545 |
}
|
|
2546 |
m_addressReqTimer.erase (dst);
|
8752
|
2547 |
/*
|
|
2548 |
* If the route request is scheduled to remove the route request entry
|
|
2549 |
* Remove the route request entry with the route retry times done for certain destination
|
|
2550 |
*/
|
|
2551 |
if (isRemove)
|
|
2552 |
{
|
|
2553 |
// remove the route request entry from route request table
|
|
2554 |
m_rreqTable->RemoveRreqEntry (dst);
|
|
2555 |
}
|
8751
|
2556 |
}
|
|
2557 |
|
|
2558 |
void
|
8752
|
2559 |
DsrRouting::ScheduleRreqRetry (Ptr<Packet> packet, std::vector<Ipv4Address> address, bool nonProp, uint32_t requestId, uint8_t protocol)
|
8751
|
2560 |
{
|
8752
|
2561 |
NS_LOG_FUNCTION (this << packet << nonProp << requestId << (uint32_t)protocol);
|
|
2562 |
Ipv4Address source = address[0];
|
|
2563 |
Ipv4Address dst = address[1];
|
8751
|
2564 |
if (nonProp)
|
|
2565 |
{
|
8752
|
2566 |
// The nonProp route request is only sent out only and is already used
|
8751
|
2567 |
if (m_nonPropReqTimer.find (dst) == m_nonPropReqTimer.end ())
|
|
2568 |
{
|
|
2569 |
Timer timer (Timer::CANCEL_ON_DESTROY);
|
|
2570 |
m_nonPropReqTimer[dst] = timer;
|
|
2571 |
}
|
8752
|
2572 |
std::vector<Ipv4Address> address;
|
|
2573 |
address.push_back (source);
|
|
2574 |
address.push_back (dst);
|
|
2575 |
m_nonPropReqTimer[dst].SetFunction (&DsrRouting::RouteRequestTimerExpire, this);
|
8751
|
2576 |
m_nonPropReqTimer[dst].Remove ();
|
8752
|
2577 |
m_nonPropReqTimer[dst].SetArguments (packet, address, requestId, protocol);
|
8751
|
2578 |
m_nonPropReqTimer[dst].Schedule (m_nonpropRequestTimeout);
|
|
2579 |
}
|
|
2580 |
else
|
|
2581 |
{
|
|
2582 |
// Cancel the non propagation request timer if found
|
|
2583 |
m_nonPropReqTimer[dst].Cancel ();
|
|
2584 |
m_nonPropReqTimer[dst].Remove ();
|
|
2585 |
if (m_nonPropReqTimer[dst].IsRunning ())
|
|
2586 |
{
|
|
2587 |
NS_LOG_DEBUG ("Timer not canceled");
|
|
2588 |
}
|
|
2589 |
m_nonPropReqTimer.erase (dst);
|
8752
|
2590 |
|
8751
|
2591 |
if (m_addressReqTimer.find (dst) == m_addressReqTimer.end ())
|
|
2592 |
{
|
|
2593 |
Timer timer (Timer::CANCEL_ON_DESTROY);
|
|
2594 |
m_addressReqTimer[dst] = timer;
|
|
2595 |
}
|
8752
|
2596 |
std::vector<Ipv4Address> address;
|
|
2597 |
address.push_back (source);
|
|
2598 |
address.push_back (dst);
|
8751
|
2599 |
m_addressReqTimer[dst].SetFunction (&DsrRouting::RouteRequestTimerExpire, this);
|
|
2600 |
m_addressReqTimer[dst].Remove ();
|
8752
|
2601 |
m_addressReqTimer[dst].SetArguments (packet, address, requestId, protocol);
|
|
2602 |
Time rreqDelay;
|
8751
|
2603 |
// back off mechanism for sending route requests
|
8752
|
2604 |
if (m_rreqTable->GetRreqCnt (dst))
|
|
2605 |
{
|
|
2606 |
// When the route request count is larger than 0
|
|
2607 |
rreqDelay = Time (pow (m_rreqTable->GetRreqCnt (dst), 2) * m_requestPeriod);
|
|
2608 |
}
|
|
2609 |
else
|
|
2610 |
{
|
|
2611 |
// This is the first route request retry
|
|
2612 |
rreqDelay = m_requestPeriod;
|
|
2613 |
}
|
8751
|
2614 |
NS_LOG_DEBUG ("The request count for the destination " << dst << " " << m_rreqTable->GetRreqCnt (dst) << " with time value " << rreqDelay);
|
|
2615 |
if (rreqDelay > m_maxRequestPeriod)
|
|
2616 |
{
|
|
2617 |
// use the max request period
|
8752
|
2618 |
NS_LOG_DEBUG ("The max request delay time " << m_maxRequestPeriod.GetSeconds());
|
8751
|
2619 |
m_addressReqTimer[dst].Schedule (m_maxRequestPeriod);
|
|
2620 |
}
|
|
2621 |
else
|
|
2622 |
{
|
8752
|
2623 |
NS_LOG_DEBUG ("The request delay time " << rreqDelay.GetSeconds());
|
8751
|
2624 |
m_addressReqTimer[dst].Schedule (rreqDelay);
|
|
2625 |
}
|
8752
|
2626 |
|
8751
|
2627 |
}
|
|
2628 |
}
|
|
2629 |
|
|
2630 |
void
|
8752
|
2631 |
DsrRouting::RouteRequestTimerExpire (Ptr<Packet> packet, std::vector<Ipv4Address> address, uint32_t requestId, uint8_t protocol)
|
8751
|
2632 |
{
|
8752
|
2633 |
NS_LOG_FUNCTION (this << packet << requestId << (uint32_t)protocol);
|
|
2634 |
// Get a clean packet without dsr header
|
|
2635 |
Ptr<Packet> dsrP = packet->Copy ();
|
|
2636 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2637 |
dsrP->RemoveHeader (dsrRoutingHeader); // Remove the dsr header in whole
|
|
2638 |
|
|
2639 |
Ipv4Address source = address[0];
|
|
2640 |
Ipv4Address dst = address[1];
|
8751
|
2641 |
RouteCacheEntry toDst;
|
|
2642 |
if (m_routeCache->LookupRoute (dst, toDst))
|
|
2643 |
{
|
|
2644 |
/*
|
|
2645 |
* Found a route the dst, construct the source route option header
|
|
2646 |
*/
|
|
2647 |
DsrOptionSRHeader sourceRoute;
|
|
2648 |
std::vector<Ipv4Address> ip = toDst.GetVector ();
|
|
2649 |
sourceRoute.SetNodesAddress (ip);
|
|
2650 |
if (m_routeCache->IsLinkCache ())
|
|
2651 |
{
|
|
2652 |
m_routeCache->UseExtends (ip);
|
|
2653 |
}
|
|
2654 |
sourceRoute.SetSegmentsLeft ((ip.size () - 2));
|
|
2655 |
uint8_t salvage = 0;
|
|
2656 |
sourceRoute.SetSalvage (salvage);
|
|
2657 |
Ipv4Address nextHop = SearchNextHop (m_mainAddress, ip); // Get the next hop address
|
|
2658 |
NS_LOG_DEBUG ("The nextHop address " << nextHop);
|
|
2659 |
if (nextHop == "0.0.0.0")
|
|
2660 |
{
|
8752
|
2661 |
NS_LOG_DEBUG ("Error next hop address");
|
|
2662 |
PacketNewRoute (dsrP, source, dst, protocol);
|
8751
|
2663 |
return;
|
|
2664 |
}
|
|
2665 |
SetRoute (nextHop, m_mainAddress);
|
8752
|
2666 |
CancelRreqTimer (dst, true);
|
|
2667 |
SendPacketFromBuffer (sourceRoute, nextHop, protocol);
|
8751
|
2668 |
NS_LOG_LOGIC ("Route to " << dst << " found");
|
|
2669 |
return;
|
|
2670 |
}
|
|
2671 |
/*
|
|
2672 |
* If a route discovery has been attempted m_rreqRetries times at the maximum TTL without
|
|
2673 |
* receiving any RREP, all data packets destined for the corresponding destination SHOULD be
|
|
2674 |
* dropped from the buffer and a Destination Unreachable message SHOULD be delivered to the application.
|
|
2675 |
*/
|
|
2676 |
NS_LOG_DEBUG ("The new request count for " << dst << " is " << m_rreqTable->GetRreqCnt (dst) << " the max " << m_rreqRetries);
|
|
2677 |
if (m_rreqTable->GetRreqCnt (dst) >= m_rreqRetries)
|
|
2678 |
{
|
|
2679 |
NS_LOG_LOGIC ("Route discovery to " << dst << " has been attempted " << m_rreqRetries << " times");
|
8752
|
2680 |
CancelRreqTimer (dst, true);
|
8751
|
2681 |
NS_LOG_DEBUG ("Route not found. Drop packet with dst " << dst);
|
|
2682 |
m_sendBuffer.DropPacketWithDst (dst);
|
|
2683 |
}
|
|
2684 |
else
|
|
2685 |
{
|
8752
|
2686 |
SocketIpTtlTag tag;
|
|
2687 |
tag.SetTtl ((uint8_t)m_discoveryHopLimit);
|
|
2688 |
Ptr<Packet> propPacket = packet->Copy ();
|
|
2689 |
propPacket->AddPacketTag (tag);
|
|
2690 |
// Increase the request count
|
|
2691 |
m_rreqTable->FindAndUpdate (dst);
|
|
2692 |
SendRequest (propPacket, source);
|
|
2693 |
NS_LOG_DEBUG ("Check the route request entry " << source << " " << dst);
|
|
2694 |
ScheduleRreqRetry (packet, address, false, requestId, protocol);
|
8751
|
2695 |
}
|
|
2696 |
return;
|
|
2697 |
}
|
|
2698 |
|
|
2699 |
void
|
|
2700 |
DsrRouting::SendRequest (Ptr<Packet> packet,
|
|
2701 |
Ipv4Address source)
|
|
2702 |
{
|
|
2703 |
NS_LOG_FUNCTION (this << packet << source);
|
|
2704 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
2705 |
/*
|
|
2706 |
* The destination address here is directed broadcast address
|
|
2707 |
*/
|
8752
|
2708 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
2709 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
2710 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
2711 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
2712 |
|
|
2713 |
DsrNetworkQueueEntry newEntry (packet, source, m_broadcast, Simulator::Now (), 0);
|
|
2714 |
|
|
2715 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
2716 |
{
|
|
2717 |
Scheduler (priority);
|
|
2718 |
}
|
|
2719 |
else
|
|
2720 |
{
|
|
2721 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
2722 |
}
|
8751
|
2723 |
}
|
|
2724 |
|
|
2725 |
void
|
|
2726 |
DsrRouting::ScheduleInterRequest (Ptr<Packet> packet)
|
|
2727 |
{
|
|
2728 |
NS_LOG_FUNCTION (this << packet);
|
|
2729 |
/*
|
8752
|
2730 |
* This is a forwarding case when sending route requests, a random delay time [0, m_broadcastJitter]
|
8751
|
2731 |
* used before forwarding as link-layer broadcast
|
|
2732 |
*/
|
|
2733 |
Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0, m_broadcastJitter)), &DsrRouting::SendRequest, this,
|
|
2734 |
packet, m_mainAddress);
|
|
2735 |
}
|
|
2736 |
|
|
2737 |
void
|
|
2738 |
DsrRouting::SendGratuitousReply (Ipv4Address source, Ipv4Address srcAddress, std::vector<Ipv4Address> &nodeList, uint8_t protocol)
|
|
2739 |
{
|
|
2740 |
NS_LOG_FUNCTION (this << source << srcAddress << (uint32_t)protocol);
|
|
2741 |
if (!(m_graReply.FindAndUpdate (source, srcAddress, m_gratReplyHoldoff))) // Find the gratuitous reply entry
|
|
2742 |
{
|
|
2743 |
NS_LOG_LOGIC ("Update gratuitous reply " << source);
|
|
2744 |
GraReplyEntry graReplyEntry (source, srcAddress, m_gratReplyHoldoff + Simulator::Now ());
|
|
2745 |
m_graReply.AddEntry (graReplyEntry);
|
|
2746 |
/*
|
|
2747 |
* Automatic route shortening
|
|
2748 |
*/
|
|
2749 |
m_finalRoute.clear (); // Clear the final route vector
|
|
2750 |
/**
|
|
2751 |
* Push back the node addresses other than those between srcAddress and our own ip address
|
|
2752 |
*/
|
|
2753 |
std::vector<Ipv4Address>::iterator before = find (nodeList.begin (), nodeList.end (), srcAddress);
|
|
2754 |
for (std::vector<Ipv4Address>::iterator i = nodeList.begin (); i != before; ++i)
|
|
2755 |
{
|
|
2756 |
m_finalRoute.push_back (*i);
|
|
2757 |
}
|
|
2758 |
m_finalRoute.push_back (srcAddress);
|
|
2759 |
std::vector<Ipv4Address>::iterator after = find (nodeList.begin (), nodeList.end (), m_mainAddress);
|
|
2760 |
for (std::vector<Ipv4Address>::iterator j = after; j != nodeList.end (); ++j)
|
|
2761 |
{
|
|
2762 |
m_finalRoute.push_back (*j);
|
|
2763 |
}
|
|
2764 |
DsrOptionRrepHeader rrep;
|
|
2765 |
rrep.SetNodesAddress (m_finalRoute); // Set the node addresses in the route reply header
|
|
2766 |
// Get the real reply source and destination
|
|
2767 |
Ipv4Address replySrc = m_finalRoute.back ();
|
|
2768 |
Ipv4Address replyDst = m_finalRoute.front ();
|
|
2769 |
/*
|
|
2770 |
* Set the route and use it in send back route reply
|
|
2771 |
*/
|
|
2772 |
m_ipv4Route = SetRoute (srcAddress, m_mainAddress);
|
|
2773 |
/*
|
|
2774 |
* This part adds DSR header to the packet and send reply
|
|
2775 |
*/
|
|
2776 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2777 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
2778 |
dsrRoutingHeader.SetMessageType (1);
|
|
2779 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (replySrc));
|
|
2780 |
dsrRoutingHeader.SetDestId (GetIDfromIP (replyDst));
|
|
2781 |
|
|
2782 |
uint8_t length = rrep.GetLength (); // Get the length of the rrep header excluding the type header
|
8752
|
2783 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
8751
|
2784 |
dsrRoutingHeader.AddDsrOption (rrep);
|
|
2785 |
Ptr<Packet> newPacket = Create<Packet> ();
|
|
2786 |
newPacket->AddHeader (dsrRoutingHeader);
|
|
2787 |
/*
|
|
2788 |
* Send gratuitous reply
|
|
2789 |
*/
|
|
2790 |
NS_LOG_INFO ("Send back gratuitous route reply");
|
|
2791 |
SendReply (newPacket, m_mainAddress, srcAddress, m_ipv4Route);
|
|
2792 |
}
|
|
2793 |
else
|
|
2794 |
{
|
|
2795 |
NS_LOG_INFO ("The same gratuitous route reply has already sent");
|
|
2796 |
}
|
|
2797 |
}
|
|
2798 |
|
|
2799 |
void
|
|
2800 |
DsrRouting::SendReply (Ptr<Packet> packet,
|
|
2801 |
Ipv4Address source,
|
|
2802 |
Ipv4Address nextHop,
|
|
2803 |
Ptr<Ipv4Route> route)
|
|
2804 |
{
|
|
2805 |
NS_LOG_FUNCTION (this << packet << source << nextHop);
|
|
2806 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
2807 |
Ptr<NetDevice> dev = m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (m_mainAddress));
|
|
2808 |
route->SetOutputDevice (dev);
|
|
2809 |
NS_LOG_INFO ("The output device " << dev << " packet is: " << *packet);
|
8752
|
2810 |
|
|
2811 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
2812 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
2813 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
2814 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
2815 |
|
|
2816 |
DsrNetworkQueueEntry newEntry (packet, source, nextHop, Simulator::Now (), route);
|
|
2817 |
|
|
2818 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
2819 |
{
|
|
2820 |
Scheduler (priority);
|
|
2821 |
}
|
|
2822 |
else
|
|
2823 |
{
|
|
2824 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
2825 |
}
|
8751
|
2826 |
}
|
|
2827 |
|
|
2828 |
void
|
|
2829 |
DsrRouting::ScheduleInitialReply (Ptr<Packet> packet,
|
|
2830 |
Ipv4Address source,
|
|
2831 |
Ipv4Address nextHop,
|
|
2832 |
Ptr<Ipv4Route> route)
|
|
2833 |
{
|
|
2834 |
NS_LOG_FUNCTION (this << packet << source << nextHop);
|
|
2835 |
Simulator::ScheduleNow (&DsrRouting::SendReply, this,
|
|
2836 |
packet, source, nextHop, route);
|
|
2837 |
}
|
|
2838 |
|
|
2839 |
void
|
|
2840 |
DsrRouting::ScheduleCachedReply (Ptr<Packet> packet,
|
|
2841 |
Ipv4Address source,
|
|
2842 |
Ipv4Address destination,
|
|
2843 |
Ptr<Ipv4Route> route,
|
8752
|
2844 |
double hops)
|
8751
|
2845 |
{
|
|
2846 |
NS_LOG_FUNCTION (this << packet << source << destination);
|
|
2847 |
Simulator::Schedule (Time (2 * m_nodeTraversalTime * (hops - 1 + UniformVariable ().GetValue (0,1))), &DsrRouting::SendReply, this, packet, source, destination, route);
|
|
2848 |
}
|
|
2849 |
|
|
2850 |
void
|
|
2851 |
DsrRouting::SendAck (uint16_t ackId,
|
|
2852 |
Ipv4Address destination,
|
|
2853 |
Ipv4Address realSrc,
|
|
2854 |
Ipv4Address realDst,
|
|
2855 |
uint8_t protocol,
|
|
2856 |
Ptr<Ipv4Route> route)
|
|
2857 |
{
|
|
2858 |
NS_LOG_FUNCTION (this << ackId << destination << realSrc << realDst << (uint32_t)protocol << route);
|
|
2859 |
NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
|
|
2860 |
|
|
2861 |
// This is a route reply option header
|
|
2862 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2863 |
dsrRoutingHeader.SetNextHeader (protocol);
|
|
2864 |
dsrRoutingHeader.SetMessageType (1);
|
|
2865 |
dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
|
|
2866 |
dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
|
|
2867 |
|
|
2868 |
DsrOptionAckHeader ack;
|
|
2869 |
/*
|
|
2870 |
* Set the ack Id and set the ack source address and destination address
|
|
2871 |
*/
|
|
2872 |
ack.SetAckId (ackId);
|
|
2873 |
ack.SetRealSrc (realSrc);
|
|
2874 |
ack.SetRealDst (realDst);
|
|
2875 |
|
|
2876 |
uint8_t length = ack.GetLength ();
|
8752
|
2877 |
dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
|
8751
|
2878 |
dsrRoutingHeader.AddDsrOption (ack);
|
|
2879 |
|
|
2880 |
Ptr<Packet> packet = Create<Packet> ();
|
|
2881 |
packet->AddHeader (dsrRoutingHeader);
|
|
2882 |
Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
|
|
2883 |
route->SetOutputDevice (dev);
|
|
2884 |
NS_LOG_DEBUG ("Send out the ACK");
|
8752
|
2885 |
|
|
2886 |
uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
|
|
2887 |
std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
|
|
2888 |
Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
|
|
2889 |
NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
|
|
2890 |
|
|
2891 |
DsrNetworkQueueEntry newEntry (packet, m_mainAddress, destination, Simulator::Now (), route);
|
|
2892 |
|
|
2893 |
if (dsrNetworkQueue->Enqueue (newEntry))
|
|
2894 |
{
|
|
2895 |
Scheduler (priority);
|
|
2896 |
}
|
|
2897 |
else
|
|
2898 |
{
|
|
2899 |
NS_LOG_INFO ("Packet dropped as dsr network queue is full");
|
|
2900 |
}
|
8751
|
2901 |
}
|
|
2902 |
|
8753
|
2903 |
enum IpL4Protocol::RxStatus
|
8751
|
2904 |
DsrRouting::Receive (Ptr<Packet> p,
|
|
2905 |
Ipv4Header const &ip,
|
|
2906 |
Ptr<Ipv4Interface> incomingInterface)
|
|
2907 |
{
|
|
2908 |
NS_LOG_FUNCTION (this << p << ip << incomingInterface);
|
|
2909 |
|
|
2910 |
NS_LOG_INFO ("Our own IP address " << m_mainAddress << " The incoming interface address " << incomingInterface);
|
|
2911 |
m_node = GetNode (); // Get the node
|
|
2912 |
Ptr<Packet> packet = p->Copy (); // Save a copy of the received packet
|
|
2913 |
/*
|
|
2914 |
* When forwarding or local deliver packets, this one should be used always!!
|
|
2915 |
*/
|
|
2916 |
DsrRoutingHeader dsrRoutingHeader;
|
|
2917 |
packet->RemoveHeader (dsrRoutingHeader); // Remove the DSR header in whole
|
|
2918 |
Ptr<Packet> copy = packet->Copy ();
|
|
2919 |
uint8_t protocol = dsrRoutingHeader.GetNextHeader ();
|
|
2920 |
uint32_t sourceId = dsrRoutingHeader.GetSourceId ();
|
|
2921 |
Ipv4Address source = GetIPfromID (sourceId);
|
8752
|
2922 |
NS_LOG_DEBUG ("The source address " << source << " with source id " << sourceId);
|
8751
|
2923 |
/*
|
|
2924 |
* Get the IP source and destination address
|
|
2925 |
*/
|
|
2926 |
Ipv4Address src = ip.GetSource ();
|
|
2927 |
|
|
2928 |
bool isPromisc = false;
|
|
2929 |
uint32_t offset = dsrRoutingHeader.GetDsrOptionsOffset (); // Get the offset for option header, 8 bytes in this case
|
|
2930 |
|
|
2931 |
// This packet is used to peek option type
|
|
2932 |
p->RemoveAtStart (offset);
|
|
2933 |
|
|
2934 |
Ptr<dsr::DsrOptions> dsrOption;
|
|
2935 |
DsrOptionHeader dsrOptionHeader;
|
|
2936 |
/*
|
|
2937 |
* Peek data to get the option type as well as length and segmentsLeft field
|
|
2938 |
*/
|
|
2939 |
uint32_t size = p->GetSize ();
|
|
2940 |
uint8_t *data = new uint8_t[size];
|
|
2941 |
p->CopyData (data, size);
|
|
2942 |
|
|
2943 |
uint8_t optionType = 0;
|
|
2944 |
uint8_t optionLength = 0;
|
|
2945 |
uint8_t segmentsLeft = 0;
|
|
2946 |
|
|
2947 |
optionType = *(data);
|
8752
|
2948 |
NS_LOG_LOGIC ("The option type value " << (uint32_t)optionType << " with packet size " << p->GetSize());
|
8751
|
2949 |
dsrOption = GetOption (optionType); // Get the relative dsr option and demux to the process function
|
|
2950 |
|
|
2951 |
if (optionType == 1) // This is the request option
|
|
2952 |
{
|
|
2953 |
BlackList *blackList = m_rreqTable->FindUnidirectional (src);
|
|
2954 |
if (blackList)
|
|
2955 |
{
|
|
2956 |
NS_LOG_DEBUG ("Discard this packet due to unidirectional link");
|
|
2957 |
m_dropTrace (p);
|
|
2958 |
}
|
|
2959 |
|
|
2960 |
dsrOption = GetOption (optionType);
|
|
2961 |
optionLength = dsrOption->Process (p, packet, m_mainAddress, source, ip, protocol, isPromisc);
|
|
2962 |
|
|
2963 |
if (optionLength == 0)
|
|
2964 |
{
|
|
2965 |
NS_LOG_DEBUG ("Discard this packet");
|
|
2966 |
m_dropTrace (p);
|
|
2967 |
}
|
|
2968 |
}
|
|
2969 |
else if (optionType == 2)
|
|
2970 |
{
|
|
2971 |
dsrOption = GetOption (optionType);
|
|
2972 |
optionLength = dsrOption->Process (p, packet, m_mainAddress, source, ip, protocol, isPromisc);
|
|
2973 |
|
|
2974 |
if (optionLength == 0)
|
|
2975 |
{
|
|
2976 |
NS_LOG_DEBUG ("Discard this packet");
|
|
2977 |
m_dropTrace (p);
|
|
2978 |
}
|
|
2979 |
}
|
|
2980 |
|
|
2981 |
else if (optionType == 32) // This is the ACK option
|
|
2982 |
{
|
|
2983 |
NS_LOG_DEBUG ("This is the ack option");
|
|
2984 |
dsrOption = GetOption (optionType);
|
|
2985 |
optionLength = dsrOption->Process (p, packet, m_mainAddress, source, ip, protocol, isPromisc);
|
|
2986 |
|
|
2987 |
if (optionLength == 0)
|
|
2988 |
{
|
|
2989 |
NS_LOG_DEBUG ("Discard this packet");
|
|
2990 |
m_dropTrace (p);
|
|
2991 |
}
|
|
2992 |
}
|
|
2993 |
|
|
2994 |
else if (optionType == 3) // This is a route error header
|
|
2995 |
{
|
|
2996 |
// populate this route error
|
|
2997 |
NS_LOG_DEBUG ("The option type value " << (uint32_t)optionType);
|
|
2998 |
|
|
2999 |
dsrOption = GetOption (optionType);
|
|
3000 |
optionLength = dsrOption->Process (p, packet, m_mainAddress, source, ip, protocol, isPromisc);
|
|
3001 |
|
|
3002 |
if (optionLength == 0)
|
|
3003 |
{
|
|
3004 |
NS_LOG_DEBUG ("Discard this packet");
|
|
3005 |
m_dropTrace (p);
|
|
3006 |
}
|
|
3007 |
NS_LOG_DEBUG ("The option Length " << (uint32_t)optionLength);
|
|
3008 |
}
|
|
3009 |
|
|
3010 |
else if (optionType == 96) // This is the source route option
|
|
3011 |
{
|
|
3012 |
NS_LOG_DEBUG ("This is the source route option " << (uint32_t)optionType);
|
|
3013 |
dsrOption = GetOption (optionType);
|
|
3014 |
optionLength = dsrOption->Process (p, packet, m_mainAddress, source, ip, protocol, isPromisc);
|
|
3015 |
|
|
3016 |
segmentsLeft = *(data + 3);
|
|
3017 |
NS_LOG_DEBUG ("The segments left in source route header " << (uint32_t)segmentsLeft);
|
|
3018 |
if (optionLength == 0)
|
|
3019 |
{
|
|
3020 |
NS_LOG_DEBUG ("Discard this packet");
|
|
3021 |
m_dropTrace (p);
|
|
3022 |
}
|
|
3023 |
else
|
|
3024 |
{
|
8752
|
3025 |
if (segmentsLeft == 0)
|
8751
|
3026 |
{
|
8752
|
3027 |
// / Get the next header
|
8751
|
3028 |
uint8_t nextHeader = dsrRoutingHeader.GetNextHeader ();
|
|
3029 |
Ptr<Ipv4L3Protocol> l3proto = m_node->GetObject<Ipv4L3Protocol> ();
|
8753
|
3030 |
Ptr<IpL4Protocol> nextProto = l3proto->GetProtocol (nextHeader);
|
8751
|
3031 |
if (nextProto != 0)
|
|
3032 |
{
|
|
3033 |
// we need to make a copy in the unlikely event we hit the
|
|
3034 |
// RX_ENDPOINT_UNREACH code path
|
|
3035 |
// Here we can use the packet that has been get off whole DSR header
|
8752
|
3036 |
NS_LOG_DEBUG ("The packet size here " << copy->GetSize());
|
8751
|
3037 |
NS_LOG_DEBUG ("The packet received " << *copy);
|
8753
|
3038 |
enum IpL4Protocol::RxStatus status =
|
8751
|
3039 |
nextProto->Receive (copy, ip, incomingInterface);
|
|
3040 |
NS_LOG_DEBUG ("The receive status " << status);
|
|
3041 |
switch (status)
|
|
3042 |
{
|
8753
|
3043 |
case IpL4Protocol::RX_OK:
|
8751
|
3044 |
// fall through
|
8753
|
3045 |
case IpL4Protocol::RX_ENDPOINT_CLOSED:
|
8751
|
3046 |
// fall through
|
8753
|
3047 |
case IpL4Protocol::RX_CSUM_FAILED:
|
8751
|
3048 |
break;
|
8753
|
3049 |
case IpL4Protocol::RX_ENDPOINT_UNREACH:
|
8752
|
3050 |
if (ip.GetDestination ().IsBroadcast () == true
|
|
3051 |
|| ip.GetDestination ().IsMulticast () == true)
|
|
3052 |
{
|
|
3053 |
break; // Do not reply to broadcast or multicast
|
|
3054 |
}
|
|
3055 |
// Another case to suppress ICMP is a subnet-directed broadcast
|
8751
|
3056 |
}
|
8752
|
3057 |
return status;
|
8751
|
3058 |
}
|
|
3059 |
}
|
8752
|
3060 |
else
|
|
3061 |
{
|
|
3062 |
NS_LOG_INFO ("This is not the final destination, the packet has already been forward to next hop");
|
|
3063 |
}
|
8751
|
3064 |
}
|
|
3065 |
}
|
|
3066 |
else
|
|
3067 |
{
|
|
3068 |
NS_LOG_LOGIC ("Unknown Option. Drop!");
|
|
3069 |
/*
|
|
3070 |
* Initialize the salvage value to 0
|
|
3071 |
*/
|
|
3072 |
uint8_t salvage = 0;
|
|
3073 |
|
|
3074 |
DsrOptionRerrUnsupportHeader rerrUnsupportHeader;
|
|
3075 |
rerrUnsupportHeader.SetErrorType (3); // The error type 3 means Option not supported
|
|
3076 |
rerrUnsupportHeader.SetErrorSrc (m_mainAddress); // The error source address is our own address
|
|
3077 |
rerrUnsupportHeader.SetUnsupported (optionType); // The unsupported option type number
|
|
3078 |
rerrUnsupportHeader.SetErrorDst (src); // Error destination address is the destination of the data packet
|
|
3079 |
rerrUnsupportHeader.SetSalvage (salvage); // Set the value about whether to salvage a packet or not
|
|
3080 |
|
|
3081 |
/*
|
|
3082 |
* The unknow option error is not supported currently in this implementation, and it's also not likely to
|
|
3083 |
* happen in simulations
|
|
3084 |
*/
|
|
3085 |
// SendError (rerrUnsupportHeader, 0, protocol); // Send the error packet
|
|
3086 |
}
|
8753
|
3087 |
return IpL4Protocol::RX_OK;
|
|
3088 |
}
|
|
3089 |
|
|
3090 |
enum IpL4Protocol::RxStatus
|
|
3091 |
DsrRouting::Receive (Ptr<Packet> p,
|
|
3092 |
Ipv6Address &src,
|
|
3093 |
Ipv6Address &dst,
|
|
3094 |
Ptr<Ipv6Interface> incomingInterface)
|
|
3095 |
{
|
|
3096 |
NS_LOG_FUNCTION (this << p << src << dst << incomingInterface);
|
|
3097 |
return IpL4Protocol::RX_ENDPOINT_UNREACH;
|
8751
|
3098 |
}
|
|
3099 |
|
|
3100 |
void
|
|
3101 |
DsrRouting::SetDownTarget (DownTargetCallback callback)
|
|
3102 |
{
|
|
3103 |
m_downTarget = callback;
|
|
3104 |
}
|
|
3105 |
|
8753
|
3106 |
void
|
|
3107 |
DsrRouting::SetDownTarget6 (DownTargetCallback6 callback)
|
|
3108 |
{
|
|
3109 |
NS_FATAL_ERROR ("Unimplemented");
|
|
3110 |
}
|
|
3111 |
|
|
3112 |
|
|
3113 |
IpL4Protocol::DownTargetCallback
|
8751
|
3114 |
DsrRouting::GetDownTarget (void) const
|
|
3115 |
{
|
|
3116 |
return m_downTarget;
|
|
3117 |
}
|
|
3118 |
|
8753
|
3119 |
IpL4Protocol::DownTargetCallback6
|
|
3120 |
DsrRouting::GetDownTarget6 (void) const
|
|
3121 |
{
|
|
3122 |
NS_FATAL_ERROR ("Unimplemented");
|
|
3123 |
return MakeNullCallback<void,Ptr<Packet>, Ipv6Address, Ipv6Address, uint8_t, Ptr<Ipv6Route> > ();
|
|
3124 |
}
|
|
3125 |
|
8751
|
3126 |
void DsrRouting::Insert (Ptr<dsr::DsrOptions> option)
|
|
3127 |
{
|
|
3128 |
m_options.push_back (option);
|
|
3129 |
}
|
|
3130 |
|
|
3131 |
Ptr<dsr::DsrOptions> DsrRouting::GetOption (int optionNumber)
|
|
3132 |
{
|
|
3133 |
for (DsrOptionList_t::iterator i = m_options.begin (); i != m_options.end (); ++i)
|
|
3134 |
{
|
|
3135 |
if ((*i)->GetOptionNumber () == optionNumber)
|
|
3136 |
{
|
|
3137 |
return *i;
|
|
3138 |
}
|
|
3139 |
}
|
|
3140 |
return 0;
|
|
3141 |
}
|
|
3142 |
} /* namespace dsr */
|
|
3143 |
} /* namespace ns3 */
|