A Discrete-Event Network Simulator
API
dsdv-packet-queue.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Hemanth Narra
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: Hemanth Narra <hemanth@ittc.ku.com>
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 #include "dsdv-packet-queue.h"
32 #include <algorithm>
33 #include <functional>
34 #include "ns3/ipv4-route.h"
35 #include "ns3/socket.h"
36 #include "ns3/log.h"
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("DsdvPacketQueue");
41 
42 namespace dsdv {
43 uint32_t
45 {
46  Purge ();
47  return m_queue.size ();
48 }
49 
50 bool
52 {
53  NS_LOG_FUNCTION ("Enqueing packet destined for" << entry.GetIpv4Header ().GetDestination ());
54  Purge ();
55  uint32_t numPacketswithdst;
56  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
57  != m_queue.end (); ++i)
58  {
59  if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ())
60  && (i->GetIpv4Header ().GetDestination ()
61  == entry.GetIpv4Header ().GetDestination ()))
62  {
63  return false;
64  }
65  }
66  numPacketswithdst = GetCountForPacketsWithDst (entry.GetIpv4Header ().GetDestination ());
67  NS_LOG_DEBUG ("Number of packets with this destination: " << numPacketswithdst);
69  if (numPacketswithdst >= m_maxLenPerDst || m_queue.size () >= m_maxLen)
70  {
71  NS_LOG_DEBUG ("Max packets reached for this destination. Not queuing any further packets");
72  return false;
73  }
74  else
75  {
76  // NS_LOG_DEBUG("Packet size while enqueing "<<entry.GetPacket()->GetSize());
78  m_queue.push_back (entry);
79  return true;
80  }
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION ("Dropping packet to " << dst);
87  Purge ();
88  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i
89  != 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  m_queue.erase (new_end, m_queue.end ());
99 }
100 
101 bool
103 {
104  NS_LOG_FUNCTION ("Dequeueing packet destined for" << dst);
105  Purge ();
106  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i != m_queue.end (); ++i)
107  {
108  if (i->GetIpv4Header ().GetDestination () == dst)
109  {
110  entry = *i;
111  m_queue.erase (i);
112  return true;
113  }
114  }
115  return false;
116 }
117 
118 bool
120 {
121  for (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
122  != 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 (std::vector<QueueEntry>::const_iterator i = m_queue.begin (); i
138  != m_queue.end (); ++i)
139  {
140  if (i->GetIpv4Header ().GetDestination () == dst)
141  {
142  count++;
143  }
144  }
145  return count;
146 }
147 
151 struct IsExpired
152 {
158  bool
159  operator() (QueueEntry const & e) const
160  {
161  // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
162  return (e.GetExpireTime () < Seconds (0));
163  }
164 };
165 
166 void
168 {
169  // NS_LOG_DEBUG("Purging Queue");
170  IsExpired pred;
171  for (std::vector<QueueEntry>::iterator i = m_queue.begin (); i
172  != m_queue.end (); ++i)
173  {
174  if (pred (*i))
175  {
176  NS_LOG_DEBUG ("Dropping outdated Packets");
177  Drop (*i, "Drop outdated packet ");
178  }
179  }
180  m_queue.erase (std::remove_if (m_queue.begin (), m_queue.end (), pred),
181  m_queue.end ());
182 }
183 
184 void
185 PacketQueue::Drop (QueueEntry en, std::string reason)
186 {
187  NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetIpv4Header ().GetDestination ());
188  // en.GetErrorCallback () (en.GetPacket (), en.GetIpv4Header (),
189  // Socket::ERROR_NOROUTETOHOST);
190  return;
191 }
192 
193 }
194 }
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
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:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
IsExpired structure.
bool operator()(QueueEntry const &e) const
Check for expired entry.