src/dsr/model/dsr-routing.cc
changeset 8756 9a34e618f40b
parent 8753 8d84b96e26bf
child 8784 d1201ff72e0c
equal deleted inserted replaced
8755:78bf735a3d5e 8756:9a34e618f40b
   357       Ipv4Address loopback ("127.0.0.1");
   357       Ipv4Address loopback ("127.0.0.1");
   358       for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
   358       for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
   359         {
   359         {
   360           // Use primary address, if multiple
   360           // Use primary address, if multiple
   361           Ipv4Address addr = m_ipv4->GetAddress (i, 0).GetLocal ();
   361           Ipv4Address addr = m_ipv4->GetAddress (i, 0).GetLocal ();
   362           m_broadcast = m_ipv4->GetAddress (i, 0).GetBroadcast();
   362           m_broadcast = m_ipv4->GetAddress (i, 0).GetBroadcast ();
   363           NS_LOG_DEBUG ("The addr " << addr);
   363           NS_LOG_DEBUG ("The addr " << addr);
   364           if (addr != loopback)
   364           if (addr != loopback)
   365             {
   365             {
   366               m_mainAddress = addr;
   366               m_mainAddress = addr;
   367               NS_LOG_DEBUG ("The node Address " << m_mainAddress);
   367               NS_LOG_DEBUG ("The node Address " << m_mainAddress);
   385               mac->TraceConnectWithoutContext ("TxErrHeader", routeCache->GetTxErrorCallback ());
   385               mac->TraceConnectWithoutContext ("TxErrHeader", routeCache->GetTxErrorCallback ());
   386               routeCache->AddArpCache (m_ipv4->GetInterface (i)->GetArpCache ());
   386               routeCache->AddArpCache (m_ipv4->GetInterface (i)->GetArpCache ());
   387               break;
   387               break;
   388             }
   388             }
   389         }
   389         }
   390       NS_ASSERT (m_mainAddress != Ipv4Address () && m_broadcast != Ipv4Address());
   390       NS_ASSERT (m_mainAddress != Ipv4Address () && m_broadcast != Ipv4Address ());
   391     }
   391     }
   392 
   392 
   393   NS_LOG_DEBUG ("The number queues " << m_numPriorityQueues);
   393   NS_LOG_DEBUG ("The number queues " << m_numPriorityQueues);
   394   for (uint32_t i = 0; i < m_numPriorityQueues; i++)
   394   for (uint32_t i = 0; i < m_numPriorityQueues; i++)
   395   {
   395     {
   396     // Set the network queue max size and the delay
   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());
   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);
   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));
   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");
   400       NS_ASSERT_MSG (result_i.second, "Error in creating queues");
   401   }
   401     }
   402   Ptr<dsr::RreqTable> rreqTable = CreateObject<dsr::RreqTable> ();
   402   Ptr<dsr::RreqTable> rreqTable = CreateObject<dsr::RreqTable> ();
   403   // Set the initial hop limit
   403   // Set the initial hop limit
   404   rreqTable->SetInitHopLimit (m_discoveryHopLimit);
   404   rreqTable->SetInitHopLimit (m_discoveryHopLimit);
   405   // Configure the request table parameters
   405   // Configure the request table parameters
   406   rreqTable->SetRreqTableSize (m_requestTableSize);
   406   rreqTable->SetRreqTableSize (m_requestTableSize);
   622     {
   622     {
   623       Ptr<Node> node = NodeList::GetNode (i);
   623       Ptr<Node> node = NodeList::GetNode (i);
   624       Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
   624       Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
   625       if (ipv4->GetAddress (1, 0).GetLocal () == address)
   625       if (ipv4->GetAddress (1, 0).GetLocal () == address)
   626         {
   626         {
   627           return uint16_t(i);
   627           return uint16_t (i);
   628         }
   628         }
   629     }
   629     }
   630   return 256;
   630   return 256;
   631 }
   631 }
   632 
   632 
   882                   uint8_t length = (sourceRoute.GetLength () + newUnreach.GetLength ());
   882                   uint8_t length = (sourceRoute.GetLength () + newUnreach.GetLength ());
   883                   dsrRoutingHeader.SetNextHeader (protocol);
   883                   dsrRoutingHeader.SetNextHeader (protocol);
   884                   dsrRoutingHeader.SetMessageType (1);
   884                   dsrRoutingHeader.SetMessageType (1);
   885                   dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
   885                   dsrRoutingHeader.SetSourceId (GetIDfromIP (m_mainAddress));
   886                   dsrRoutingHeader.SetDestId (255);
   886                   dsrRoutingHeader.SetDestId (255);
   887                   dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
   887                   dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 4);
   888                   dsrRoutingHeader.AddDsrOption (newUnreach);
   888                   dsrRoutingHeader.AddDsrOption (newUnreach);
   889                   dsrRoutingHeader.AddDsrOption (sourceRoute);
   889                   dsrRoutingHeader.AddDsrOption (sourceRoute);
   890 
   890 
   891                   Ptr<Packet> newPacket = Create<Packet> ();
   891                   Ptr<Packet> newPacket = Create<Packet> ();
   892                   newPacket->AddHeader (dsrRoutingHeader); // Add the routing header with rerr and sourceRoute attached to it
   892                   newPacket->AddHeader (dsrRoutingHeader); // Add the routing header with rerr and sourceRoute attached to it
   894                   m_ipv4Route->SetOutputDevice (dev);
   894                   m_ipv4Route->SetOutputDevice (dev);
   895 
   895 
   896                   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
   896                   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
   897                   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
   897                   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
   898                   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
   898                   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
   899                   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
   899                   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
   900 
   900 
   901                   DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
   901                   DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
   902 
   902 
   903                   if (dsrNetworkQueue->Enqueue (newEntry))
   903                   if (dsrNetworkQueue->Enqueue (newEntry))
   904                     {
   904                     {
   929               sourceRoute.SetNodesAddress (nodeList); // Save the whole route in the source route header of the packet
   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
   930               sourceRoute.SetSegmentsLeft ((nodeList.size () - 2)); // The segmentsLeft field will indicate the hops to go
   931               sourceRoute.SetSalvage (salvage);
   931               sourceRoute.SetSalvage (salvage);
   932 
   932 
   933               uint8_t length = sourceRoute.GetLength ();
   933               uint8_t length = sourceRoute.GetLength ();
   934               dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
   934               dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
   935               dsrRoutingHeader.AddDsrOption (sourceRoute);
   935               dsrRoutingHeader.AddDsrOption (sourceRoute);
   936               cleanP->AddHeader (dsrRoutingHeader);
   936               cleanP->AddHeader (dsrRoutingHeader);
   937               // Send the data packet out before schedule the next packet transmission
   937               // Send the data packet out before schedule the next packet transmission
   938               SendPacket (cleanP, m_mainAddress, nextHop, protocol);
   938               SendPacket (cleanP, m_mainAddress, nextHop, protocol);
   939               Ptr<const Packet> mtP = cleanP->Copy ();
   939               Ptr<const Packet> mtP = cleanP->Copy ();
   940               // Put the data packet in the maintenance queue for data packet retransmission
   940               // Put the data packet in the maintenance queue for data packet retransmission
   941               MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*Ipv4Address=*/ m_mainAddress, /*nextHop=*/ nextHop,
   941               MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*Ipv4Address=*/ m_mainAddress, /*nextHop=*/ nextHop,
   942                                           /*source=*/ m_mainAddress, /*destination=*/ destination, /*ackId=*/ 0,
   942                                                       /*source=*/ m_mainAddress, /*destination=*/ destination, /*ackId=*/ 0,
   943                                           /*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
   943                                                       /*SegsLeft=*/ nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
   944               bool result = m_maintainBuffer.Enqueue (newEntry); // Enqueue the packet the the maintenance buffer
   944               bool result = m_maintainBuffer.Enqueue (newEntry); // Enqueue the packet the the maintenance buffer
   945               if (result)
   945               if (result)
   946                 {
   946                 {
   947                   NetworkKey networkKey;
   947                   NetworkKey networkKey;
   948                   networkKey.m_ackId = newEntry.GetAckId ();
   948                   networkKey.m_ackId = newEntry.GetAckId ();
  1111         }
  1111         }
  1112       sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));     // The segmentsLeft field will indicate the hops to go
  1112       sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));     // The segmentsLeft field will indicate the hops to go
  1113       sourceRoute.SetSalvage (salvage);
  1113       sourceRoute.SetSalvage (salvage);
  1114 
  1114 
  1115       uint8_t length = sourceRoute.GetLength ();
  1115       uint8_t length = sourceRoute.GetLength ();
  1116       dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  1116       dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  1117       dsrRoutingHeader.AddDsrOption (sourceRoute);
  1117       dsrRoutingHeader.AddDsrOption (sourceRoute);
  1118       cleanP->AddHeader (dsrRoutingHeader);
  1118       cleanP->AddHeader (dsrRoutingHeader);
  1119       // Send the data packet out before schedule the next packet transmission
  1119       // Send the data packet out before schedule the next packet transmission
  1120       SendPacket (cleanP, source, nextHop, protocol);
  1120       SendPacket (cleanP, source, nextHop, protocol);
  1121       Ptr<const Packet> mtP = cleanP->Copy ();
  1121       Ptr<const Packet> mtP = cleanP->Copy ();
  1122       SetRoute (nextHop, m_mainAddress);
  1122       SetRoute (nextHop, m_mainAddress);
  1123       // Put the data packet in the maintenance queue for data packet retransmission
  1123       // Put the data packet in the maintenance queue for data packet retransmission
  1124       MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*Ipv4Address=*/ m_mainAddress, /*nextHop=*/ nextHop,
  1124       MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*Ipv4Address=*/ m_mainAddress, /*nextHop=*/ nextHop,
  1125                                               /*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
  1125                                               /*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
  1126                                               /*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
  1126                                               /*SegsLeft=*/ nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
  1127       bool result = m_maintainBuffer.Enqueue (newEntry);     // Enqueue the packet the the maintenance buffer
  1127       bool result = m_maintainBuffer.Enqueue (newEntry);     // Enqueue the packet the the maintenance buffer
  1128 
  1128 
  1129       if (result)
  1129       if (result)
  1130         {
  1130         {
  1131           NetworkKey networkKey;
  1131           NetworkKey networkKey;
  1224         }
  1224         }
  1225       sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));
  1225       sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));
  1226       uint8_t srLength = sourceRoute.GetLength ();
  1226       uint8_t srLength = sourceRoute.GetLength ();
  1227       uint8_t length = (srLength + rerrLength);
  1227       uint8_t length = (srLength + rerrLength);
  1228 
  1228 
  1229       dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
  1229       dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 4);
  1230       dsrRoutingHeader.AddDsrOption (rerrUnreachHeader);
  1230       dsrRoutingHeader.AddDsrOption (rerrUnreachHeader);
  1231       dsrRoutingHeader.AddDsrOption (sourceRoute);
  1231       dsrRoutingHeader.AddDsrOption (sourceRoute);
  1232       newPacket->AddHeader (dsrRoutingHeader);
  1232       newPacket->AddHeader (dsrRoutingHeader);
  1233 
  1233 
  1234       SetRoute (nextHop, m_mainAddress);
  1234       SetRoute (nextHop, m_mainAddress);
  1235       Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
  1235       Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
  1236       m_ipv4Route->SetOutputDevice (dev);
  1236       m_ipv4Route->SetOutputDevice (dev);
  1237       NS_LOG_INFO ("Send the packet to the next hop address " << nextHop << " from " << m_mainAddress << " with the size " << newPacket->GetSize());
  1237       NS_LOG_INFO ("Send the packet to the next hop address " << nextHop << " from " << m_mainAddress << " with the size " << newPacket->GetSize ());
  1238 
  1238 
  1239       uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  1239       uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  1240       std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1240       std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1241       Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1241       Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1242       NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1242       NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1243 
  1243 
  1244       DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
  1244       DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
  1245 
  1245 
  1246       if (dsrNetworkQueue->Enqueue (newEntry))
  1246       if (dsrNetworkQueue->Enqueue (newEntry))
  1247         {
  1247         {
  1268   dsrRoutingHeader.SetMessageType (1);
  1268   dsrRoutingHeader.SetMessageType (1);
  1269   dsrRoutingHeader.SetSourceId (GetIDfromIP (rerr.GetErrorSrc ()));
  1269   dsrRoutingHeader.SetSourceId (GetIDfromIP (rerr.GetErrorSrc ()));
  1270   dsrRoutingHeader.SetDestId (GetIDfromIP (rerr.GetErrorDst ()));
  1270   dsrRoutingHeader.SetDestId (GetIDfromIP (rerr.GetErrorDst ()));
  1271 
  1271 
  1272   uint8_t length = (sourceRoute.GetLength () + rerr.GetLength ());
  1272   uint8_t length = (sourceRoute.GetLength () + rerr.GetLength ());
  1273   dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
  1273   dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 4);
  1274   dsrRoutingHeader.AddDsrOption (rerr);
  1274   dsrRoutingHeader.AddDsrOption (rerr);
  1275   dsrRoutingHeader.AddDsrOption (sourceRoute);
  1275   dsrRoutingHeader.AddDsrOption (sourceRoute);
  1276   Ptr<Packet> packet = Create<Packet> ();
  1276   Ptr<Packet> packet = Create<Packet> ();
  1277   packet->AddHeader (dsrRoutingHeader);
  1277   packet->AddHeader (dsrRoutingHeader);
  1278   Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
  1278   Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
  1279   route->SetOutputDevice (dev);
  1279   route->SetOutputDevice (dev);
  1280 
  1280 
  1281   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  1281   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  1282   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1282   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1283   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1283   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1284   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1284   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1285 
  1285 
  1286    DsrNetworkQueueEntry newEntry (packet, m_mainAddress, nextHop, Simulator::Now (), route);
  1286   DsrNetworkQueueEntry newEntry (packet, m_mainAddress, nextHop, Simulator::Now (), route);
  1287 
  1287 
  1288   if (dsrNetworkQueue->Enqueue (newEntry))
  1288   if (dsrNetworkQueue->Enqueue (newEntry))
  1289     {
  1289     {
  1290       Scheduler (priority);
  1290       Scheduler (priority);
  1291     }
  1291     }
  1369           sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));       // The segmentsLeft field will indicate the hops to go
  1369           sourceRoute.SetSegmentsLeft ((nodeList.size () - 2));       // The segmentsLeft field will indicate the hops to go
  1370           sourceRoute.SetSalvage (salvage);
  1370           sourceRoute.SetSalvage (salvage);
  1371 
  1371 
  1372           uint8_t length = sourceRoute.GetLength ();
  1372           uint8_t length = sourceRoute.GetLength ();
  1373 
  1373 
  1374           dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  1374           dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  1375           dsrRoutingHeader.AddDsrOption (sourceRoute);
  1375           dsrRoutingHeader.AddDsrOption (sourceRoute);
  1376           cleanP->AddHeader (dsrRoutingHeader);
  1376           cleanP->AddHeader (dsrRoutingHeader);
  1377           // Send the data packet out before schedule the next packet transmission
  1377           // Send the data packet out before schedule the next packet transmission
  1378           SendPacket (cleanP, source, nextHop, protocol);
  1378           SendPacket (cleanP, source, nextHop, protocol);
  1379 
  1379 
  1380           Ptr<const Packet> mtP = cleanP->Copy ();
  1380           Ptr<const Packet> mtP = cleanP->Copy ();
  1381           NS_LOG_DEBUG ("maintain packet size " << cleanP->GetSize());
  1381           NS_LOG_DEBUG ("maintain packet size " << cleanP->GetSize ());
  1382           // Put the data packet in the maintenance queue for data packet retransmission
  1382           // Put the data packet in the maintenance queue for data packet retransmission
  1383           MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
  1383           MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
  1384                                       /*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
  1384                                                   /*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
  1385                                       /*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
  1385                                                   /*SegsLeft=*/ nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
  1386           bool result = m_maintainBuffer.Enqueue (newEntry);       // Enqueue the packet the the maintenance buffer
  1386           bool result = m_maintainBuffer.Enqueue (newEntry);       // Enqueue the packet the the maintenance buffer
  1387           if (result)
  1387           if (result)
  1388             {
  1388             {
  1389               NetworkKey networkKey;
  1389               NetworkKey networkKey;
  1390               networkKey.m_ackId = newEntry.GetAckId ();
  1390               networkKey.m_ackId = newEntry.GetAckId ();
  1471   m_ipv4Route->SetOutputDevice (dev);
  1471   m_ipv4Route->SetOutputDevice (dev);
  1472 
  1472 
  1473   uint32_t priority = GetPriority (DSR_DATA_PACKET);
  1473   uint32_t priority = GetPriority (DSR_DATA_PACKET);
  1474   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1474   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1475   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1475   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1476   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1476   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1477 
  1477 
  1478   DsrNetworkQueueEntry newEntry (packet, source, nextHop, Simulator::Now (), m_ipv4Route);
  1478   DsrNetworkQueueEntry newEntry (packet, source, nextHop, Simulator::Now (), m_ipv4Route);
  1479 
  1479 
  1480   if (dsrNetworkQueue->Enqueue (newEntry))
  1480   if (dsrNetworkQueue->Enqueue (newEntry))
  1481     {
  1481     {
  1508     {
  1508     {
  1509       numPriorities = priority;
  1509       numPriorities = priority;
  1510     }
  1510     }
  1511   // priorities range from 0 to m_numPriorityQueues, with 0 as the highest priority
  1511   // priorities range from 0 to m_numPriorityQueues, with 0 as the highest priority
  1512   for (uint32_t i = priority; numPriorities < m_numPriorityQueues; numPriorities++)
  1512   for (uint32_t i = priority; numPriorities < m_numPriorityQueues; numPriorities++)
  1513   {
  1513     {
  1514     std::map<uint32_t, Ptr<DsrNetworkQueue> >::iterator q = m_priorityQueue.find (i);
  1514       std::map<uint32_t, Ptr<DsrNetworkQueue> >::iterator q = m_priorityQueue.find (i);
  1515     Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = q->second;
  1515       Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = q->second;
  1516     uint32_t queueSize = dsrNetworkQueue->GetSize ();
  1516       uint32_t queueSize = dsrNetworkQueue->GetSize ();
  1517     if (queueSize == 0)
  1517       if (queueSize == 0)
  1518     {
  1518         {
  1519       if ((i == (m_numPriorityQueues - 1)) && continueWithFirst)
  1519           if ((i == (m_numPriorityQueues - 1)) && continueWithFirst)
  1520       {
  1520             {
  1521         i = 0;
  1521               i = 0;
  1522       }
  1522             }
       
  1523           else
       
  1524             {
       
  1525               i++;
       
  1526             }
       
  1527         }
  1523       else
  1528       else
  1524       {
  1529         {
  1525         i++;
  1530           uint32_t totalQueueSize;
  1526       }
  1531           for (std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator j = m_priorityQueue.begin (); j != m_priorityQueue.end (); j++)
  1527     }
  1532             {
  1528     else
  1533               NS_LOG_DEBUG ("The size of the network queue for " << j->first << " is " << j->second->GetSize ());
  1529     {
  1534               totalQueueSize += j->second->GetSize ();
  1530       uint32_t totalQueueSize;
  1535               NS_LOG_DEBUG ("And the total size is " << totalQueueSize);
  1531       for (std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator j = m_priorityQueue.begin (); j != m_priorityQueue.end (); j++)
  1536             }
  1532         {
  1537           if (totalQueueSize > 5)
  1533           NS_LOG_DEBUG ("The size of the network queue for " << j->first << " is " << j->second->GetSize());
  1538             {
  1534           totalQueueSize += j->second->GetSize ();
  1539               // Here the queue size is larger than 5, we need to increase the retransmission timer for each packet in the network queue
  1535           NS_LOG_DEBUG ("And the total size is " << totalQueueSize);
  1540               IncreaseRetransTimer ();
  1536         }
  1541             }
  1537       if (totalQueueSize > 5)
  1542           DsrNetworkQueueEntry newEntry;
  1538         {
  1543           dsrNetworkQueue->Dequeue (newEntry);
  1539           // Here the queue size is larger than 5, we need to increase the retransmission timer for each packet in the network queue
  1544           if (SendRealDown (newEntry))
  1540           IncreaseRetransTimer ();
  1545             {
  1541         }
  1546               NS_LOG_DEBUG ("Packet sent by Dsr. Calling PriorityScheduler after some time");
  1542       DsrNetworkQueueEntry newEntry;
  1547               //packet was successfully sent down. call scheduler after some time
  1543       dsrNetworkQueue->Dequeue (newEntry);
  1548               Simulator::Schedule (MicroSeconds (UniformVariable ().GetInteger (0, 1000)),
  1544       if (SendRealDown (newEntry))
  1549                                    &DsrRouting::PriorityScheduler,this, i, false);
  1545       {
  1550             }
  1546         NS_LOG_DEBUG("Packet sent by Dsr. Calling PriorityScheduler after some time");
  1551           else
  1547         //packet was successfully sent down. call scheduler after some time
  1552             {
  1548         Simulator::Schedule (MicroSeconds (UniformVariable ().GetInteger (0, 1000)),
  1553               // packet was dropped by Dsr. Call scheduler immediately so that we can
  1549             &DsrRouting::PriorityScheduler,this, i, false);
  1554               // send another packet immediately.
  1550       }
  1555               NS_LOG_DEBUG ("Packet dropped by Dsr. Calling PriorityScheduler immediately");
  1551       else
  1556               Simulator::Schedule (Seconds (0), &DsrRouting::PriorityScheduler, this, i, false);
  1552       {
  1557             }
  1553         // packet was dropped by Dsr. Call scheduler immediately so that we can
  1558           if ((i == (m_numPriorityQueues - 1)) && continueWithFirst)
  1554         // send another packet immediately.
  1559             {
  1555         NS_LOG_DEBUG("Packet dropped by Dsr. Calling PriorityScheduler immediately");
  1560               i = 0;
  1556         Simulator::Schedule (Seconds (0), &DsrRouting::PriorityScheduler, this, i, false);
  1561             }
  1557       }
  1562           else
  1558       if ((i == (m_numPriorityQueues - 1)) && continueWithFirst)
  1563             {
  1559       {
  1564               i++;
  1560         i = 0;
  1565             }
  1561       }
  1566         }
  1562       else
  1567     }
  1563       {
       
  1564         i++;
       
  1565       }
       
  1566     }
       
  1567   }
       
  1568 }
  1568 }
  1569 
  1569 
  1570 void
  1570 void
  1571 DsrRouting::IncreaseRetransTimer ()
  1571 DsrRouting::IncreaseRetransTimer ()
  1572 {
  1572 {
  1582       Ipv4Address nextHop = i->GetNextHopAddress ();
  1582       Ipv4Address nextHop = i->GetNextHopAddress ();
  1583       for (std::map<NetworkKey, Timer>::iterator j = m_addressForwardTimer.begin (); j != m_addressForwardTimer.end (); j++)
  1583       for (std::map<NetworkKey, Timer>::iterator j = m_addressForwardTimer.begin (); j != m_addressForwardTimer.end (); j++)
  1584         {
  1584         {
  1585           if (nextHop == j->first.m_nextHop)
  1585           if (nextHop == j->first.m_nextHop)
  1586             {
  1586             {
  1587               NS_LOG_DEBUG ("The network delay left is " << j->second.GetDelayLeft());
  1587               NS_LOG_DEBUG ("The network delay left is " << j->second.GetDelayLeft ());
  1588               j->second.SetDelay (j->second.GetDelayLeft () + m_retransIncr);
  1588               j->second.SetDelay (j->second.GetDelayLeft () + m_retransIncr);
  1589               NS_LOG_DEBUG ("The new network delay time is " << j->second.GetDelayLeft ());
  1589               NS_LOG_DEBUG ("The new network delay time is " << j->second.GetDelayLeft ());
  1590             }
  1590             }
  1591         }
  1591         }
  1592     }
  1592     }
  1598   NS_LOG_FUNCTION (this);
  1598   NS_LOG_FUNCTION (this);
  1599   Ipv4Address source = newEntry.GetSourceAddress ();
  1599   Ipv4Address source = newEntry.GetSourceAddress ();
  1600   Ipv4Address nextHop = newEntry.GetNextHopAddress ();
  1600   Ipv4Address nextHop = newEntry.GetNextHopAddress ();
  1601   Ptr<Packet> packet = newEntry.GetPacket ()->Copy ();
  1601   Ptr<Packet> packet = newEntry.GetPacket ()->Copy ();
  1602   Ptr<Ipv4Route> route = newEntry.GetIpv4Route ();
  1602   Ptr<Ipv4Route> route = newEntry.GetIpv4Route ();
  1603   m_downTarget (packet, source, nextHop, GetProtocolNumber(), route);
  1603   m_downTarget (packet, source, nextHop, GetProtocolNumber (), route);
  1604   return true;
  1604   return true;
  1605 }
  1605 }
  1606 
  1606 
  1607 void
  1607 void
  1608 DsrRouting::SendPacketFromBuffer (DsrOptionSRHeader const &sourceRoute, Ipv4Address nextHop, uint8_t protocol)
  1608 DsrRouting::SendPacketFromBuffer (DsrOptionSRHeader const &sourceRoute, Ipv4Address nextHop, uint8_t protocol)
  1636           dsrRoutingHeader.SetMessageType (2);
  1636           dsrRoutingHeader.SetMessageType (2);
  1637           dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
  1637           dsrRoutingHeader.SetSourceId (GetIDfromIP (source));
  1638           dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
  1638           dsrRoutingHeader.SetDestId (GetIDfromIP (destination));
  1639 
  1639 
  1640           uint8_t length = sourceRoute.GetLength ();
  1640           uint8_t length = sourceRoute.GetLength ();
  1641           dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  1641           dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  1642           dsrRoutingHeader.AddDsrOption (sourceRoute);
  1642           dsrRoutingHeader.AddDsrOption (sourceRoute);
  1643 
  1643 
  1644           p->AddHeader (dsrRoutingHeader);
  1644           p->AddHeader (dsrRoutingHeader);
  1645           // Send the data packet out before schedule the next packet transmission
  1645           // Send the data packet out before schedule the next packet transmission
  1646           NS_LOG_DEBUG ("Send out the data packet");
  1646           NS_LOG_DEBUG ("Send out the data packet");
  1648 
  1648 
  1649           Ptr<const Packet> mtP = p->Copy ();
  1649           Ptr<const Packet> mtP = p->Copy ();
  1650           // Put the data packet in the maintenance queue for data packet retransmission
  1650           // Put the data packet in the maintenance queue for data packet retransmission
  1651           MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
  1651           MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
  1652                                                   /*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
  1652                                                   /*source=*/ source, /*destination=*/ destination, /*ackId=*/ 0,
  1653                                                   /*SegsLeft=*/nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
  1653                                                   /*SegsLeft=*/ nodeList.size () - 2, /*expire time=*/ m_maxMaintainTime);
  1654           bool result = m_maintainBuffer.Enqueue (newEntry);       // Enqueue the packet the the maintenance buffer
  1654           bool result = m_maintainBuffer.Enqueue (newEntry);       // Enqueue the packet the the maintenance buffer
  1655 
  1655 
  1656           if (result)
  1656           if (result)
  1657             {
  1657             {
  1658               NetworkKey networkKey;
  1658               NetworkKey networkKey;
  1748                   DsrRoutingHeader newRoutingHeader;
  1748                   DsrRoutingHeader newRoutingHeader;
  1749                   newRoutingHeader.SetNextHeader (protocol);
  1749                   newRoutingHeader.SetNextHeader (protocol);
  1750                   newRoutingHeader.SetMessageType (1);
  1750                   newRoutingHeader.SetMessageType (1);
  1751                   newRoutingHeader.SetSourceId (GetIDfromIP (rerr.GetErrorSrc ()));
  1751                   newRoutingHeader.SetSourceId (GetIDfromIP (rerr.GetErrorSrc ()));
  1752                   newRoutingHeader.SetDestId (GetIDfromIP (rerr.GetErrorDst ()));
  1752                   newRoutingHeader.SetDestId (GetIDfromIP (rerr.GetErrorDst ()));
  1753                   newRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
  1753                   newRoutingHeader.SetPayloadLength (uint16_t (length) + 4);
  1754                   newRoutingHeader.AddDsrOption (newUnreach);
  1754                   newRoutingHeader.AddDsrOption (newUnreach);
  1755                   newRoutingHeader.AddDsrOption (sourceRoute);
  1755                   newRoutingHeader.AddDsrOption (sourceRoute);
  1756                   if (m_routeCache->IsLinkCache ())
  1756                   if (m_routeCache->IsLinkCache ())
  1757                     {
  1757                     {
  1758                       m_routeCache->UseExtends (nodeList);
  1758                       m_routeCache->UseExtends (nodeList);
  1764                   m_ipv4Route->SetOutputDevice (dev);
  1764                   m_ipv4Route->SetOutputDevice (dev);
  1765 
  1765 
  1766                   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  1766                   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  1767                   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1767                   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  1768                   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1768                   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  1769                   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1769                   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  1770 
  1770 
  1771                   DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
  1771                   DsrNetworkQueueEntry newEntry (newPacket, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
  1772 
  1772 
  1773                   if (dsrNetworkQueue->Enqueue (newEntry))
  1773                   if (dsrNetworkQueue->Enqueue (newEntry))
  1774                     {
  1774                     {
  1779                       NS_LOG_INFO ("Packet dropped as dsr network queue is full");
  1779                       NS_LOG_INFO ("Packet dropped as dsr network queue is full");
  1780                     }
  1780                     }
  1781                 }
  1781                 }
  1782             }
  1782             }
  1783 
  1783 
  1784         if (m_errorBuffer.GetSize () != 0 && m_errorBuffer.Find (destination))
  1784           if (m_errorBuffer.GetSize () != 0 && m_errorBuffer.Find (destination))
  1785           {
  1785             {
  1786             NS_LOG_DEBUG ("Schedule sending the next packet in send buffer");
  1786               NS_LOG_DEBUG ("Schedule sending the next packet in send buffer");
  1787             Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
  1787               Simulator::Schedule (MilliSeconds (UniformVariable ().GetInteger (0,100)),
  1788                                  &DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
  1788                                    &DsrRouting::SendPacketFromBuffer,this,sourceRoute,nextHop,protocol);
  1789           }
  1789             }
  1790         }
  1790         }
  1791     }
  1791     }
  1792   else
  1792   else
  1793     {
  1793     {
  1794       NS_LOG_DEBUG ("Packet not found in either the send or error buffer");
  1794       NS_LOG_DEBUG ("Packet not found in either the send or error buffer");
  1830    * Create a packet to fill maintenance buffer, not used to compare
  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
  1831    * The reason is ack header doesn't have the original packet copy
  1832    */
  1832    */
  1833   Ptr<Packet> mainP = Create<Packet> ();
  1833   Ptr<Packet> mainP = Create<Packet> ();
  1834   MaintainBuffEntry newEntry (/*Packet=*/ mainP, /*ourAddress=*/ sender, /*nextHop=*/ receiver,
  1834   MaintainBuffEntry newEntry (/*Packet=*/ mainP, /*ourAddress=*/ sender, /*nextHop=*/ receiver,
  1835                               /*source=*/ realSrc, /*destination=*/ realDst, /*ackId=*/ ackId,
  1835                                           /*source=*/ realSrc, /*destination=*/ realDst, /*ackId=*/ ackId,
  1836                               /*SegsLeft=*/0, /*expire time=*/ Simulator::Now ());
  1836                                           /*SegsLeft=*/ 0, /*expire time=*/ Simulator::Now ());
  1837   CancelNetworkPacketTimer (newEntry);
  1837   CancelNetworkPacketTimer (newEntry);
  1838 }
  1838 }
  1839 
  1839 
  1840 void
  1840 void
  1841 DsrRouting::CancelNetworkPacketTimer (MaintainBuffEntry & mb)
  1841 DsrRouting::CancelNetworkPacketTimer (MaintainBuffEntry & mb)
  1854   m_addressForwardCnt.erase (networkKey);
  1854   m_addressForwardCnt.erase (networkKey);
  1855 
  1855 
  1856   NS_LOG_INFO ("ackId " << mb.GetAckId () << " ourAdd " << mb.GetOurAdd () << " nextHop " << mb.GetNextHop ()
  1856   NS_LOG_INFO ("ackId " << mb.GetAckId () << " ourAdd " << mb.GetOurAdd () << " nextHop " << mb.GetNextHop ()
  1857                         << " source " << mb.GetSrc () << " destination " << mb.GetDst ()
  1857                         << " source " << mb.GetSrc () << " destination " << mb.GetDst ()
  1858                         << " segsLeft " << (uint32_t)mb.GetSegsLeft ()
  1858                         << " segsLeft " << (uint32_t)mb.GetSegsLeft ()
  1859                         );
  1859                );
  1860   // Find the network acknowledgment timer
  1860   // Find the network acknowledgment timer
  1861   std::map<NetworkKey, Timer>::const_iterator i =
  1861   std::map<NetworkKey, Timer>::const_iterator i =
  1862       m_addressForwardTimer.find (networkKey);
  1862     m_addressForwardTimer.find (networkKey);
  1863   if (i == m_addressForwardTimer.end ())
  1863   if (i == m_addressForwardTimer.end ())
  1864     {
  1864     {
  1865       NS_LOG_INFO ("did not find the packet timer");
  1865       NS_LOG_INFO ("did not find the packet timer");
  1866     }
  1866     }
  1867   else
  1867   else
  1877         {
  1877         {
  1878           NS_LOG_INFO ("Timer not canceled");
  1878           NS_LOG_INFO ("Timer not canceled");
  1879         }
  1879         }
  1880       m_addressForwardTimer.erase (networkKey);
  1880       m_addressForwardTimer.erase (networkKey);
  1881     }
  1881     }
  1882     // Erase the maintenance entry
  1882   // Erase the maintenance entry
  1883     // yet this does not check the segments left value here
  1883   // yet this does not check the segments left value here
  1884     if (m_maintainBuffer.NetworkEqual (mb))
  1884   if (m_maintainBuffer.NetworkEqual (mb))
  1885       {
  1885     {
  1886         NS_LOG_INFO ("Remove same maintenance buffer entry based on network acknowledgment");
  1886       NS_LOG_INFO ("Remove same maintenance buffer entry based on network acknowledgment");
  1887       }
  1887     }
  1888 }
  1888 }
  1889 
  1889 
  1890 void
  1890 void
  1891 DsrRouting::CancelPassivePacketTimer (MaintainBuffEntry & mb)
  1891 DsrRouting::CancelPassivePacketTimer (MaintainBuffEntry & mb)
  1892 {
  1892 {
  1900   m_passiveCnt[passiveKey] = 0;
  1900   m_passiveCnt[passiveKey] = 0;
  1901   m_passiveCnt.erase (passiveKey);
  1901   m_passiveCnt.erase (passiveKey);
  1902 
  1902 
  1903   // Find the passive acknowledgment timer
  1903   // Find the passive acknowledgment timer
  1904   std::map<PassiveKey, Timer>::const_iterator j =
  1904   std::map<PassiveKey, Timer>::const_iterator j =
  1905       m_passiveAckTimer.find (passiveKey);
  1905     m_passiveAckTimer.find (passiveKey);
  1906   if (j == m_passiveAckTimer.end ())
  1906   if (j == m_passiveAckTimer.end ())
  1907     {
  1907     {
  1908       NS_LOG_INFO ("did not find the passive timer");
  1908       NS_LOG_INFO ("did not find the passive timer");
  1909     }
  1909     }
  1910   else
  1910   else
  2011         }
  2011         }
  2012 
  2012 
  2013       uint8_t length = (sourceRoute.GetLength () + ackReq.GetLength ());
  2013       uint8_t length = (sourceRoute.GetLength () + ackReq.GetLength ());
  2014       NS_LOG_INFO ("length of source route header " << (uint32_t)(sourceRoute.GetLength ())
  2014       NS_LOG_INFO ("length of source route header " << (uint32_t)(sourceRoute.GetLength ())
  2015                                                     << " length of ack request header " << (uint32_t)(ackReq.GetLength ()));
  2015                                                     << " length of ack request header " << (uint32_t)(ackReq.GetLength ()));
  2016       newDsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
  2016       newDsrRoutingHeader.SetPayloadLength (uint16_t (length) + 4);
  2017       newDsrRoutingHeader.AddDsrOption (sourceRoute);
  2017       newDsrRoutingHeader.AddDsrOption (sourceRoute);
  2018       newDsrRoutingHeader.AddDsrOption (ackReq);
  2018       newDsrRoutingHeader.AddDsrOption (ackReq);
  2019       p->AddHeader (newDsrRoutingHeader);
  2019       p->AddHeader (newDsrRoutingHeader);
  2020 
  2020 
  2021       SetRoute (nextHop, m_mainAddress);
  2021       SetRoute (nextHop, m_mainAddress);
  2024       // Send out the data packet
  2024       // Send out the data packet
  2025 
  2025 
  2026       uint32_t priority = GetPriority (DSR_DATA_PACKET);
  2026       uint32_t priority = GetPriority (DSR_DATA_PACKET);
  2027       std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2027       std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2028       Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2028       Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2029       NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2029       NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2030 
  2030 
  2031       DsrNetworkQueueEntry newEntry (p, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
  2031       DsrNetworkQueueEntry newEntry (p, m_mainAddress, nextHop, Simulator::Now (), m_ipv4Route);
  2032 
  2032 
  2033       if (dsrNetworkQueue->Enqueue (newEntry))
  2033       if (dsrNetworkQueue->Enqueue (newEntry))
  2034         {
  2034         {
  2117 
  2117 
  2118       // After m_tryPassiveAcks, schedule the packet retransmission using network acknowledgment option
  2118       // After m_tryPassiveAcks, schedule the packet retransmission using network acknowledgment option
  2119       m_addressForwardTimer[networkKey].SetFunction (&DsrRouting::NetworkScheduleTimerExpire, this);
  2119       m_addressForwardTimer[networkKey].SetFunction (&DsrRouting::NetworkScheduleTimerExpire, this);
  2120       m_addressForwardTimer[networkKey].Remove ();
  2120       m_addressForwardTimer[networkKey].Remove ();
  2121       m_addressForwardTimer[networkKey].SetArguments (newEntry, protocol);
  2121       m_addressForwardTimer[networkKey].SetArguments (newEntry, protocol);
  2122       NS_LOG_DEBUG ("The packet retries time for " << newEntry.GetAckId() << " is " << m_sendRetries
  2122       NS_LOG_DEBUG ("The packet retries time for " << newEntry.GetAckId () << " is " << m_sendRetries
  2123           << " and the delay time is " << Time (2 * m_nodeTraversalTime));
  2123                                                    << " and the delay time is " << Time (2 * m_nodeTraversalTime));
  2124       // Back-off mechanism
  2124       // Back-off mechanism
  2125       m_addressForwardTimer[networkKey].Schedule (Time (2 * m_nodeTraversalTime));
  2125       m_addressForwardTimer[networkKey].Schedule (Time (2 * m_nodeTraversalTime));
  2126     }
  2126     }
  2127   else
  2127   else
  2128     {
  2128     {
  2138       NS_LOG_DEBUG ("The packet retry we have done " << m_sendRetries);
  2138       NS_LOG_DEBUG ("The packet retry we have done " << m_sendRetries);
  2139 
  2139 
  2140       p = mb.GetPacket ()->Copy ();
  2140       p = mb.GetPacket ()->Copy ();
  2141       dsrP = mb.GetPacket ()->Copy ();
  2141       dsrP = mb.GetPacket ()->Copy ();
  2142 
  2142 
  2143       NS_LOG_DEBUG ("The packet with dsr header " << dsrP->GetSize());
  2143       NS_LOG_DEBUG ("The packet with dsr header " << dsrP->GetSize ());
  2144       networkKey.m_ackId = mb.GetAckId ();
  2144       networkKey.m_ackId = mb.GetAckId ();
  2145       networkKey.m_ourAdd = mb.GetOurAdd ();
  2145       networkKey.m_ourAdd = mb.GetOurAdd ();
  2146       networkKey.m_nextHop = mb.GetNextHop ();
  2146       networkKey.m_nextHop = mb.GetNextHop ();
  2147       networkKey.m_source = mb.GetSrc ();
  2147       networkKey.m_source = mb.GetSrc ();
  2148       networkKey.m_destination = mb.GetDst ();
  2148       networkKey.m_destination = mb.GetDst ();
  2160 
  2160 
  2161       // After m_tryPassiveAcks, schedule the packet retransmission using network acknowledgment option
  2161       // After m_tryPassiveAcks, schedule the packet retransmission using network acknowledgment option
  2162       m_addressForwardTimer[networkKey].SetFunction (&DsrRouting::NetworkScheduleTimerExpire, this);
  2162       m_addressForwardTimer[networkKey].SetFunction (&DsrRouting::NetworkScheduleTimerExpire, this);
  2163       m_addressForwardTimer[networkKey].Remove ();
  2163       m_addressForwardTimer[networkKey].Remove ();
  2164       m_addressForwardTimer[networkKey].SetArguments (mb, protocol);
  2164       m_addressForwardTimer[networkKey].SetArguments (mb, protocol);
  2165       NS_LOG_DEBUG ("The packet retries time for " << mb.GetAckId() << " is " << m_sendRetries
  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));
  2166                                                    << " and the delay time is " << Time (2 * m_sendRetries *  m_nodeTraversalTime));
  2167       // Back-off mechanism
  2167       // Back-off mechanism
  2168       m_addressForwardTimer[networkKey].Schedule (Time (2 * m_sendRetries * m_nodeTraversalTime));
  2168       m_addressForwardTimer[networkKey].Schedule (Time (2 * m_sendRetries * m_nodeTraversalTime));
  2169     }
  2169     }
  2170 }
  2170 }
  2171 
  2171 
  2314   dsrRoutingHeader.SetDestId (GetIDfromIP (targetAddress));
  2314   dsrRoutingHeader.SetDestId (GetIDfromIP (targetAddress));
  2315 
  2315 
  2316   // We get the salvage value in sourceRoute header and set it to route error header if triggered error
  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 ();
  2317   Ptr<Packet> p = packet->Copy ();
  2318   uint8_t length = sourceRoute.GetLength ();
  2318   uint8_t length = sourceRoute.GetLength ();
  2319   dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  2319   dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  2320   dsrRoutingHeader.AddDsrOption (sourceRoute);
  2320   dsrRoutingHeader.AddDsrOption (sourceRoute);
  2321   p->AddHeader (dsrRoutingHeader);
  2321   p->AddHeader (dsrRoutingHeader);
  2322 
  2322 
  2323   // Send the data packet out before schedule the next packet transmission
  2323   // Send the data packet out before schedule the next packet transmission
  2324   SendPacket (p, source, nextHop, protocol);
  2324   SendPacket (p, source, nextHop, protocol);
  2325 
  2325 
  2326   Ptr<const Packet> mtP = p->Copy ();
  2326   Ptr<const Packet> mtP = p->Copy ();
  2327 
  2327 
  2328   MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
  2328   MaintainBuffEntry newEntry (/*Packet=*/ mtP, /*ourAddress=*/ m_mainAddress, /*nextHop=*/ nextHop,
  2329                                           /*source=*/ source, /*destination=*/ targetAddress,
  2329                                           /*source=*/ source, /*destination=*/ targetAddress,
  2330                                           /*ackId=*/ m_ackId, /*SegsLeft=*/sourceRoute.GetSegmentsLeft(), /*expire time=*/ m_maxMaintainTime);
  2330                                           /*ackId=*/ m_ackId, /*SegsLeft=*/ sourceRoute.GetSegmentsLeft (), /*expire time=*/ m_maxMaintainTime);
  2331   bool result = m_maintainBuffer.Enqueue (newEntry);
  2331   bool result = m_maintainBuffer.Enqueue (newEntry);
  2332 
  2332 
  2333   if (result)
  2333   if (result)
  2334     {
  2334     {
  2335       NetworkKey networkKey;
  2335       NetworkKey networkKey;
  2384   m_requestId = m_rreqTable->CheckUniqueRreqId (destination);      // Check the Id cache for duplicate ones
  2384   m_requestId = m_rreqTable->CheckUniqueRreqId (destination);      // Check the Id cache for duplicate ones
  2385   rreqHeader.SetId (m_requestId);
  2385   rreqHeader.SetId (m_requestId);
  2386 
  2386 
  2387   dsrRoutingHeader.AddDsrOption (rreqHeader);                      // Add the rreqHeader to the dsr extension header
  2387   dsrRoutingHeader.AddDsrOption (rreqHeader);                      // Add the rreqHeader to the dsr extension header
  2388   uint8_t length = rreqHeader.GetLength ();
  2388   uint8_t length = rreqHeader.GetLength ();
  2389   dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  2389   dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  2390   packet->AddHeader (dsrRoutingHeader);
  2390   packet->AddHeader (dsrRoutingHeader);
  2391 
  2391 
  2392   // Schedule the route requests retry with non-propagation set true
  2392   // Schedule the route requests retry with non-propagation set true
  2393   bool nonProp = true;
  2393   bool nonProp = true;
  2394   std::vector<Ipv4Address> address;
  2394   std::vector<Ipv4Address> address;
  2412   NS_LOG_FUNCTION (this << (uint32_t)protocol);
  2412   NS_LOG_FUNCTION (this << (uint32_t)protocol);
  2413   NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
  2413   NS_ASSERT_MSG (!m_downTarget.IsNull (), "Error, DsrRouting cannot send downward");
  2414   uint8_t salvage = rerr.GetSalvage ();
  2414   uint8_t salvage = rerr.GetSalvage ();
  2415   Ipv4Address dst = rerr.GetOriginalDst ();
  2415   Ipv4Address dst = rerr.GetOriginalDst ();
  2416   NS_LOG_DEBUG ("our own address here " << m_mainAddress << " error source " << rerr.GetErrorSrc () << " error destination " << rerr.GetErrorDst ()
  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 ()
  2417                                         << " error next hop " << rerr.GetUnreachNode () << " original dst " << rerr.GetOriginalDst ()
  2418      );
  2418                 );
  2419   RouteCacheEntry toDst;
  2419   RouteCacheEntry toDst;
  2420   if (m_routeCache->LookupRoute (dst, toDst))
  2420   if (m_routeCache->LookupRoute (dst, toDst))
  2421     {
  2421     {
  2422       /*
  2422       /*
  2423        * Found a route the dst, construct the source route option header
  2423        * Found a route the dst, construct the source route option header
  2470       rreqHeader.SetId (m_requestId);
  2470       rreqHeader.SetId (m_requestId);
  2471 
  2471 
  2472       dsrRoutingHeader.AddDsrOption (rreqHeader);         // Add the rreqHeader to the dsr extension header
  2472       dsrRoutingHeader.AddDsrOption (rreqHeader);         // Add the rreqHeader to the dsr extension header
  2473       dsrRoutingHeader.AddDsrOption (rerr);
  2473       dsrRoutingHeader.AddDsrOption (rerr);
  2474       uint8_t length = rreqHeader.GetLength () + rerr.GetLength ();
  2474       uint8_t length = rreqHeader.GetLength () + rerr.GetLength ();
  2475       dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 4);
  2475       dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 4);
  2476       dstP->AddHeader (dsrRoutingHeader);
  2476       dstP->AddHeader (dsrRoutingHeader);
  2477       // Schedule the route requests retry, propagate the route request message as it contains error
  2477       // Schedule the route requests retry, propagate the route request message as it contains error
  2478       bool nonProp = false;
  2478       bool nonProp = false;
  2479       std::vector<Ipv4Address> address;
  2479       std::vector<Ipv4Address> address;
  2480       address.push_back (m_mainAddress);
  2480       address.push_back (m_mainAddress);
  2613         }
  2613         }
  2614       NS_LOG_DEBUG ("The request count for the destination " << dst << " " << m_rreqTable->GetRreqCnt (dst) << " with time value " << rreqDelay);
  2614       NS_LOG_DEBUG ("The request count for the destination " << dst << " " << m_rreqTable->GetRreqCnt (dst) << " with time value " << rreqDelay);
  2615       if (rreqDelay > m_maxRequestPeriod)
  2615       if (rreqDelay > m_maxRequestPeriod)
  2616         {
  2616         {
  2617           // use the max request period
  2617           // use the max request period
  2618           NS_LOG_DEBUG ("The max request delay time " << m_maxRequestPeriod.GetSeconds());
  2618           NS_LOG_DEBUG ("The max request delay time " << m_maxRequestPeriod.GetSeconds ());
  2619           m_addressReqTimer[dst].Schedule (m_maxRequestPeriod);
  2619           m_addressReqTimer[dst].Schedule (m_maxRequestPeriod);
  2620         }
  2620         }
  2621       else
  2621       else
  2622         {
  2622         {
  2623           NS_LOG_DEBUG ("The request delay time " << rreqDelay.GetSeconds());
  2623           NS_LOG_DEBUG ("The request delay time " << rreqDelay.GetSeconds ());
  2624           m_addressReqTimer[dst].Schedule (rreqDelay);
  2624           m_addressReqTimer[dst].Schedule (rreqDelay);
  2625         }
  2625         }
  2626 
  2626 
  2627     }
  2627     }
  2628 }
  2628 }
  2706    * The destination address here is directed broadcast address
  2706    * The destination address here is directed broadcast address
  2707    */
  2707    */
  2708   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  2708   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  2709   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2709   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2710   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2710   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2711   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2711   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2712 
  2712 
  2713   DsrNetworkQueueEntry newEntry (packet, source, m_broadcast, Simulator::Now (), 0);
  2713   DsrNetworkQueueEntry newEntry (packet, source, m_broadcast, Simulator::Now (), 0);
  2714 
  2714 
  2715   if (dsrNetworkQueue->Enqueue (newEntry))
  2715   if (dsrNetworkQueue->Enqueue (newEntry))
  2716     {
  2716     {
  2778       dsrRoutingHeader.SetMessageType (1);
  2778       dsrRoutingHeader.SetMessageType (1);
  2779       dsrRoutingHeader.SetSourceId (GetIDfromIP (replySrc));
  2779       dsrRoutingHeader.SetSourceId (GetIDfromIP (replySrc));
  2780       dsrRoutingHeader.SetDestId (GetIDfromIP (replyDst));
  2780       dsrRoutingHeader.SetDestId (GetIDfromIP (replyDst));
  2781 
  2781 
  2782       uint8_t length = rrep.GetLength ();        // Get the length of the rrep header excluding the type header
  2782       uint8_t length = rrep.GetLength ();        // Get the length of the rrep header excluding the type header
  2783       dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  2783       dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  2784       dsrRoutingHeader.AddDsrOption (rrep);
  2784       dsrRoutingHeader.AddDsrOption (rrep);
  2785       Ptr<Packet> newPacket = Create<Packet> ();
  2785       Ptr<Packet> newPacket = Create<Packet> ();
  2786       newPacket->AddHeader (dsrRoutingHeader);
  2786       newPacket->AddHeader (dsrRoutingHeader);
  2787       /*
  2787       /*
  2788        * Send gratuitous reply
  2788        * Send gratuitous reply
  2809   NS_LOG_INFO ("The output device " << dev << " packet is: " << *packet);
  2809   NS_LOG_INFO ("The output device " << dev << " packet is: " << *packet);
  2810 
  2810 
  2811   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  2811   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  2812   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2812   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2813   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2813   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2814   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2814   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2815 
  2815 
  2816   DsrNetworkQueueEntry newEntry (packet, source, nextHop, Simulator::Now (), route);
  2816   DsrNetworkQueueEntry newEntry (packet, source, nextHop, Simulator::Now (), route);
  2817 
  2817 
  2818   if (dsrNetworkQueue->Enqueue (newEntry))
  2818   if (dsrNetworkQueue->Enqueue (newEntry))
  2819     {
  2819     {
  2872   ack.SetAckId (ackId);
  2872   ack.SetAckId (ackId);
  2873   ack.SetRealSrc (realSrc);
  2873   ack.SetRealSrc (realSrc);
  2874   ack.SetRealDst (realDst);
  2874   ack.SetRealDst (realDst);
  2875 
  2875 
  2876   uint8_t length = ack.GetLength ();
  2876   uint8_t length = ack.GetLength ();
  2877   dsrRoutingHeader.SetPayloadLength (uint16_t(length) + 2);
  2877   dsrRoutingHeader.SetPayloadLength (uint16_t (length) + 2);
  2878   dsrRoutingHeader.AddDsrOption (ack);
  2878   dsrRoutingHeader.AddDsrOption (ack);
  2879 
  2879 
  2880   Ptr<Packet> packet = Create<Packet> ();
  2880   Ptr<Packet> packet = Create<Packet> ();
  2881   packet->AddHeader (dsrRoutingHeader);
  2881   packet->AddHeader (dsrRoutingHeader);
  2882   Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
  2882   Ptr<NetDevice> dev = m_ip->GetNetDevice (m_ip->GetInterfaceForAddress (m_mainAddress));
  2884   NS_LOG_DEBUG ("Send out the ACK");
  2884   NS_LOG_DEBUG ("Send out the ACK");
  2885 
  2885 
  2886   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  2886   uint32_t priority = GetPriority (DSR_CONTROL_PACKET);
  2887   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2887   std::map<uint32_t, Ptr<dsr::DsrNetworkQueue> >::iterator i = m_priorityQueue.find (priority);
  2888   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2888   Ptr<dsr::DsrNetworkQueue> dsrNetworkQueue = i->second;
  2889   NS_LOG_DEBUG("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2889   NS_LOG_DEBUG ("Will be inserting into priority queue " << dsrNetworkQueue << " number: " << priority);
  2890 
  2890 
  2891   DsrNetworkQueueEntry newEntry (packet, m_mainAddress, destination, Simulator::Now (), route);
  2891   DsrNetworkQueueEntry newEntry (packet, m_mainAddress, destination, Simulator::Now (), route);
  2892 
  2892 
  2893   if (dsrNetworkQueue->Enqueue (newEntry))
  2893   if (dsrNetworkQueue->Enqueue (newEntry))
  2894     {
  2894     {
  2943   uint8_t optionType = 0;
  2943   uint8_t optionType = 0;
  2944   uint8_t optionLength = 0;
  2944   uint8_t optionLength = 0;
  2945   uint8_t segmentsLeft = 0;
  2945   uint8_t segmentsLeft = 0;
  2946 
  2946 
  2947   optionType = *(data);
  2947   optionType = *(data);
  2948   NS_LOG_LOGIC ("The option type value " << (uint32_t)optionType << " with packet size " << p->GetSize());
  2948   NS_LOG_LOGIC ("The option type value " << (uint32_t)optionType << " with packet size " << p->GetSize ());
  2949   dsrOption = GetOption (optionType);       // Get the relative dsr option and demux to the process function
  2949   dsrOption = GetOption (optionType);       // Get the relative dsr option and demux to the process function
  2950 
  2950 
  2951   if (optionType == 1)        // This is the request option
  2951   if (optionType == 1)        // This is the request option
  2952     {
  2952     {
  2953       BlackList *blackList = m_rreqTable->FindUnidirectional (src);
  2953       BlackList *blackList = m_rreqTable->FindUnidirectional (src);
  3031               if (nextProto != 0)
  3031               if (nextProto != 0)
  3032                 {
  3032                 {
  3033                   // we need to make a copy in the unlikely event we hit the
  3033                   // we need to make a copy in the unlikely event we hit the
  3034                   // RX_ENDPOINT_UNREACH code path
  3034                   // RX_ENDPOINT_UNREACH code path
  3035                   // Here we can use the packet that has been get off whole DSR header
  3035                   // Here we can use the packet that has been get off whole DSR header
  3036                   NS_LOG_DEBUG ("The packet size here " << copy->GetSize());
  3036                   NS_LOG_DEBUG ("The packet size here " << copy->GetSize ());
  3037                   NS_LOG_DEBUG ("The packet received " << *copy);
  3037                   NS_LOG_DEBUG ("The packet received " << *copy);
  3038                   enum IpL4Protocol::RxStatus status =
  3038                   enum IpL4Protocol::RxStatus status =
  3039                     nextProto->Receive (copy, ip, incomingInterface);
  3039                     nextProto->Receive (copy, ip, incomingInterface);
  3040                   NS_LOG_DEBUG ("The receive status " << status);
  3040                   NS_LOG_DEBUG ("The receive status " << status);
  3041                   switch (status)
  3041                   switch (status)
  3042                     {
  3042                     {
  3043                       case IpL4Protocol::RX_OK:
  3043                     case IpL4Protocol::RX_OK:
  3044                       // fall through
  3044                     // fall through
  3045                       case IpL4Protocol::RX_ENDPOINT_CLOSED:
  3045                     case IpL4Protocol::RX_ENDPOINT_CLOSED:
  3046                       // fall through
  3046                     // fall through
  3047                       case IpL4Protocol::RX_CSUM_FAILED:
  3047                     case IpL4Protocol::RX_CSUM_FAILED:
  3048                         break;
  3048                       break;
  3049                       case IpL4Protocol::RX_ENDPOINT_UNREACH:
  3049                     case IpL4Protocol::RX_ENDPOINT_UNREACH:
  3050                       if (ip.GetDestination ().IsBroadcast () == true
  3050                       if (ip.GetDestination ().IsBroadcast () == true
  3051                           || ip.GetDestination ().IsMulticast () == true)
  3051                           || ip.GetDestination ().IsMulticast () == true)
  3052                         {
  3052                         {
  3053                           break;       // Do not reply to broadcast or multicast
  3053                           break;       // Do not reply to broadcast or multicast
  3054                         }
  3054                         }