A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-errorbuff.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
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
20#include "dsr-errorbuff.h"
21
22#include "ns3/ipv4-route.h"
23#include "ns3/log.h"
24#include "ns3/socket.h"
25
26#include <algorithm>
27#include <functional>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("DsrErrorBuffer");
33
34namespace dsr
35{
36
39{
40 Purge();
41 return m_errorBuffer.size();
42}
43
44bool
46{
47 Purge();
48 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
49 {
50 NS_LOG_INFO("packet id " << i->GetPacket()->GetUid() << " " << entry.GetPacket()->GetUid()
51 << " source " << i->GetSource() << " " << entry.GetSource()
52 << " next hop " << i->GetNextHop() << " " << entry.GetNextHop()
53 << " dst " << i->GetDestination() << " "
54 << entry.GetDestination());
55
56 /// \todo check the source and destination over here
57 if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
58 (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetSource()) &&
59 (i->GetDestination() == entry.GetDestination()))
60 {
61 return false;
62 }
63 }
64
65 entry.SetExpireTime(m_errorBufferTimeout); // Initialize the send buffer timeout
66 /*
67 * Drop the most aged packet when buffer reaches to max
68 */
69 if (m_errorBuffer.size() >= m_maxLen)
70 {
71 Drop(m_errorBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
72 m_errorBuffer.erase(m_errorBuffer.begin());
73 }
74 // enqueue the entry
75 m_errorBuffer.push_back(entry);
76 return true;
77}
78
79void
81{
82 NS_LOG_FUNCTION(this << source << nextHop);
83 Purge();
84 std::vector<Ipv4Address> list;
85 list.push_back(source);
86 list.push_back(nextHop);
87 const std::vector<Ipv4Address> link = list;
88 /*
89 * Drop the packet with the error link source----------nextHop
90 */
91 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
92 {
93 if ((i->GetSource() == link[0]) && (i->GetNextHop() == link[1]))
94 {
95 DropLink(*i, "DropPacketForErrLink");
96 }
97 }
98
99 auto new_end =
100 std::remove_if(m_errorBuffer.begin(),
101 m_errorBuffer.end(),
102 [&](const DsrErrorBuffEntry& en) {
103 return (en.GetSource() == link[0]) && (en.GetNextHop() == link[1]);
104 });
105 m_errorBuffer.erase(new_end, m_errorBuffer.end());
106}
107
108bool
110{
111 Purge();
112 /*
113 * Dequeue the entry with destination address dst
114 */
115 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
116 {
117 if (i->GetDestination() == dst)
118 {
119 entry = *i;
120 i = m_errorBuffer.erase(i);
121 NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
122 return true;
123 }
124 }
125 return false;
126}
127
128bool
130{
131 /*
132 * Make sure if the send buffer contains entry with certain dst
133 */
134 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
135 {
136 if (i->GetDestination() == dst)
137 {
138 NS_LOG_DEBUG("Found the packet");
139 return true;
140 }
141 }
142 return false;
143}
144
145/// IsExpired structure
146struct IsExpired
147{
148 /**
149 * \brief comparison operator
150 * \param e entry to compare
151 * \return true if entry expired
152 */
153 bool operator()(const DsrErrorBuffEntry& e) const
154 {
155 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ());
156 return (e.GetExpireTime() < Seconds(0));
157 }
158};
159
160void
162{
163 /*
164 * Purge the buffer to eliminate expired entries
165 */
166 NS_LOG_DEBUG("The error buffer size " << m_errorBuffer.size());
167 IsExpired pred;
168 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
169 {
170 if (pred(*i))
171 {
172 NS_LOG_DEBUG("Dropping Queue Packets");
173 Drop(*i, "Drop out-dated packet ");
174 }
175 }
176 m_errorBuffer.erase(std::remove_if(m_errorBuffer.begin(), m_errorBuffer.end(), pred),
177 m_errorBuffer.end());
178}
179
180void
182{
183 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
184 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
185 // Socket::ERROR_NOROUTETOHOST);
186}
187
188void
190{
191 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
192 << en.GetNextHop());
193 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
194 // Socket::ERROR_NOROUTETOHOST);
195}
196} // namespace dsr
197} // namespace ns3
Ipv4 addresses are stored in host order in this class.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
uint64_t GetUid() const
Returns the packet's Uid.
Definition packet.cc:401
DSR Error Buffer Entry.
Ptr< const Packet > GetPacket() const
Get packet from entry.
void SetExpireTime(Time exp)
Set expire time.
Ipv4Address GetNextHop() const
Get next hop.
Ipv4Address GetSource() const
Get source address.
Time GetExpireTime() const
Get expire time.
Ipv4Address GetDestination() const
Get destination address.
Time m_errorBufferTimeout
The maximum period of time that a routing protocol is allowed to buffer a packet for,...
uint32_t m_maxLen
The maximum number of packets that we allow a routing protocol to buffer.
bool Enqueue(DsrErrorBuffEntry &entry)
Push entry in queue, if there is no entry with the same packet and destination address in queue.
void DropLink(DsrErrorBuffEntry en, std::string reason)
Notify that packet is dropped from queue by link error.
bool Dequeue(Ipv4Address dst, DsrErrorBuffEntry &entry)
Return first found (the earliest) entry for given destination.
bool Find(Ipv4Address dst)
Finds whether a packet with destination dst exists in the queue.
void DropPacketForErrLink(Ipv4Address source, Ipv4Address nextHop)
Remove all packets with the error link.
uint32_t GetSize()
Returns the number of entries in the queue.
std::vector< DsrErrorBuffEntry > m_errorBuffer
The send buffer to cache unsent packet.
void Drop(DsrErrorBuffEntry en, std::string reason)
Notify that packet is dropped from queue by timeout.
void Purge()
Remove all expired entries.
#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 ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define list
IsExpired structure.
bool operator()(const DsrErrorBuffEntry &e) const
comparison operator