A Discrete-Event Network Simulator
API
dsdv-packet-queue.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Hemanth Narra
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Hemanth Narra <hemanth@ittc.ku.com>
18  *
19  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20  * ResiliNets Research Group https://resilinets.org/
21  * Information and Telecommunication Technology Center (ITTC)
22  * and Department of Electrical Engineering and Computer Science
23  * The University of Kansas Lawrence, KS USA.
24  *
25  * Work supported in part by NSF FIND (Future Internet Design) Program
26  * under grant CNS-0626918 (Postmodern Internet Architecture),
27  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28  * US Department of Defense (DoD), and ITTC at The University of Kansas.
29  */
30 #include "dsdv-packet-queue.h"
31 
32 #include "ns3/ipv4-route.h"
33 #include "ns3/log.h"
34 #include "ns3/socket.h"
35 
36 #include <algorithm>
37 #include <functional>
38 
39 namespace ns3
40 {
41 
42 NS_LOG_COMPONENT_DEFINE("DsdvPacketQueue");
43 
44 namespace dsdv
45 {
46 uint32_t
48 {
49  Purge();
50  return m_queue.size();
51 }
52 
53 bool
55 {
56  NS_LOG_FUNCTION("Enqueuing packet destined for" << entry.GetIpv4Header().GetDestination());
57  Purge();
58  uint32_t numPacketswithdst;
59  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
60  {
61  if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
62  (i->GetIpv4Header().GetDestination() == entry.GetIpv4Header().GetDestination()))
63  {
64  return false;
65  }
66  }
67  numPacketswithdst = GetCountForPacketsWithDst(entry.GetIpv4Header().GetDestination());
68  NS_LOG_DEBUG("Number of packets with this destination: " << numPacketswithdst);
70  if (numPacketswithdst >= m_maxLenPerDst || m_queue.size() >= m_maxLen)
71  {
72  NS_LOG_DEBUG("Max packets reached for this destination. Not queuing any further packets");
73  return false;
74  }
75  else
76  {
77  // NS_LOG_DEBUG("Packet size while enqueuing "<<entry.GetPacket()->GetSize());
79  m_queue.push_back(entry);
80  return true;
81  }
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION("Dropping packet to " << dst);
88  Purge();
89  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
90  {
91  if (i->GetIpv4Header().GetDestination() == dst)
92  {
93  Drop(*i, "DropPacketWithDst ");
94  }
95  }
96  auto new_end = std::remove_if(m_queue.begin(), m_queue.end(), [&](const QueueEntry& en) {
97  return en.GetIpv4Header().GetDestination() == dst;
98  });
99  m_queue.erase(new_end, m_queue.end());
100 }
101 
102 bool
104 {
105  NS_LOG_FUNCTION("Dequeueing packet destined for" << dst);
106  Purge();
107  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
108  {
109  if (i->GetIpv4Header().GetDestination() == dst)
110  {
111  entry = *i;
112  m_queue.erase(i);
113  return true;
114  }
115  }
116  return false;
117 }
118 
119 bool
121 {
122  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
123  {
124  if (i->GetIpv4Header().GetDestination() == dst)
125  {
126  NS_LOG_DEBUG("Find");
127  return true;
128  }
129  }
130  return false;
131 }
132 
133 uint32_t
135 {
136  uint32_t count = 0;
137  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
138  {
139  if (i->GetIpv4Header().GetDestination() == dst)
140  {
141  count++;
142  }
143  }
144  return count;
145 }
146 
150 struct IsExpired
151 {
157  bool operator()(const QueueEntry& e) const
158  {
159  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
160  return (e.GetExpireTime() < Seconds(0));
161  }
162 };
163 
164 void
166 {
167  // NS_LOG_DEBUG("Purging Queue");
168  IsExpired pred;
169  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
170  {
171  if (pred(*i))
172  {
173  NS_LOG_DEBUG("Dropping outdated Packets");
174  Drop(*i, "Drop outdated packet ");
175  }
176  }
177  m_queue.erase(std::remove_if(m_queue.begin(), m_queue.end(), pred), m_queue.end());
178 }
179 
180 void
181 PacketQueue::Drop(QueueEntry en, std::string reason)
182 {
183  NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetIpv4Header().GetDestination());
184  // en.GetErrorCallback () (en.GetPacket (), en.GetIpv4Header (),
185  // Socket::ERROR_NOROUTETOHOST);
186 }
187 
188 } // namespace dsdv
189 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Ipv4Address GetDestination() const
Definition: ipv4-header.cc:316
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
std::vector< QueueEntry > m_queue
the queue
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
bool Enqueue(QueueEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
bool Dequeue(Ipv4Address dst, QueueEntry &entry)
Return first found (the earliest) entry for given destination.
void DropPacketWithDst(Ipv4Address dst)
Remove all packets with destination IP address dst.
uint32_t GetSize()
Get the number of entries.
void Drop(QueueEntry en, std::string reason)
Notify that the packet is dropped from queue due to timeout.
uint32_t GetCountForPacketsWithDst(Ipv4Address dst)
Get count of packets with destination dst in the queue.
uint32_t m_maxLenPerDst
The maximum number of packets that we allow per destination to buffer.
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
void Purge()
Remove all expired entries.
Time m_queueTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
DSDV Queue Entry.
void SetExpireTime(Time exp)
Set expire time.
Ptr< const Packet > GetPacket() const
Get packet.
Time GetExpireTime() const
Get expire time.
Ipv4Header GetIpv4Header() const
Get IP header.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(const QueueEntry &e) const
Check for expired entry.