A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsdv-packet-queue.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hemanth Narra
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Hemanth Narra <hemanth@ittc.ku.com>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19#include "dsdv-packet-queue.h"
20
21#include "ns3/ipv4-route.h"
22#include "ns3/log.h"
23#include "ns3/socket.h"
24
25#include <algorithm>
26#include <functional>
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("DsdvPacketQueue");
32
33namespace dsdv
34{
37{
38 Purge();
39 return m_queue.size();
40}
41
42bool
44{
45 NS_LOG_FUNCTION("Enqueuing packet destined for" << entry.GetIpv4Header().GetDestination());
46 Purge();
47 uint32_t numPacketswithdst;
48 for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
49 {
50 if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
51 (i->GetIpv4Header().GetDestination() == entry.GetIpv4Header().GetDestination()))
52 {
53 return false;
54 }
55 }
56 numPacketswithdst = GetCountForPacketsWithDst(entry.GetIpv4Header().GetDestination());
57 NS_LOG_DEBUG("Number of packets with this destination: " << numPacketswithdst);
58 /** For Brock Paper comparison*/
59 if (numPacketswithdst >= m_maxLenPerDst || m_queue.size() >= m_maxLen)
60 {
61 NS_LOG_DEBUG("Max packets reached for this destination. Not queuing any further packets");
62 return false;
63 }
64 else
65 {
66 // NS_LOG_DEBUG("Packet size while enqueuing "<<entry.GetPacket()->GetSize());
68 m_queue.push_back(entry);
69 return true;
70 }
71}
72
73void
75{
76 NS_LOG_FUNCTION("Dropping packet to " << dst);
77 Purge();
78 for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
79 {
80 if (i->GetIpv4Header().GetDestination() == dst)
81 {
82 Drop(*i, "DropPacketWithDst ");
83 }
84 }
85 auto new_end = std::remove_if(m_queue.begin(), m_queue.end(), [&](const QueueEntry& en) {
86 return en.GetIpv4Header().GetDestination() == dst;
87 });
88 m_queue.erase(new_end, m_queue.end());
89}
90
91bool
93{
94 NS_LOG_FUNCTION("Dequeueing packet destined for" << dst);
95 Purge();
96 for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
97 {
98 if (i->GetIpv4Header().GetDestination() == dst)
99 {
100 entry = *i;
101 m_queue.erase(i);
102 return true;
103 }
104 }
105 return false;
106}
107
108bool
110{
111 for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
112 {
113 if (i->GetIpv4Header().GetDestination() == dst)
114 {
115 NS_LOG_DEBUG("Find");
116 return true;
117 }
118 }
119 return false;
120}
121
124{
125 uint32_t count = 0;
126 for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
127 {
128 if (i->GetIpv4Header().GetDestination() == dst)
129 {
130 count++;
131 }
132 }
133 return count;
134}
135
136/**
137 * IsExpired structure
138 */
140{
141 /**
142 * @brief Check for expired entry
143 * @param e QueueEntry to check
144 * @return true if expired
145 */
146 bool operator()(const QueueEntry& e) const
147 {
148 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
149 return (e.GetExpireTime().IsStrictlyNegative());
150 }
151};
152
153void
155{
156 // NS_LOG_DEBUG("Purging Queue");
157 IsExpired pred;
158 for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
159 {
160 if (pred(*i))
161 {
162 NS_LOG_DEBUG("Dropping outdated Packets");
163 Drop(*i, "Drop outdated packet ");
164 }
165 }
166 m_queue.erase(std::remove_if(m_queue.begin(), m_queue.end(), pred), m_queue.end());
167}
168
169void
170PacketQueue::Drop(QueueEntry en, std::string reason)
171{
172 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetIpv4Header().GetDestination());
173 // en.GetErrorCallback () (en.GetPacket (), en.GetIpv4Header (),
174 // Socket::ERROR_NOROUTETOHOST);
175}
176
177} // namespace dsdv
178} // namespace ns3
Ipv4 addresses are stored in host order in this class.
Ipv4Address GetDestination() const
uint64_t GetUid() const
Returns the packet's Uid.
Definition packet.cc:401
bool IsStrictlyNegative() const
Exactly equivalent to t < 0.
Definition nstime.h:331
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.
Ptr< const Packet > GetPacket() const
Get packet.
void SetExpireTime(Time exp)
Set expire time.
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:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
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.