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 * Modified by: Tommaso Pecorella <tommaso.pecorella@unifi.it>
8 * Lorenzo Bartolini <l.bartolini02@gmail.com>
9 *
10 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
11 * ResiliNets Research Group https://resilinets.org/
12 * Information and Telecommunication Technology Center (ITTC)
13 * and Department of Electrical Engineering and Computer Science
14 * The University of Kansas Lawrence, KS USA.
15 *
16 * Work supported in part by NSF FIND (Future Internet Design) Program
17 * under grant CNS-0626918 (Postmodern Internet Architecture),
18 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
19 * US Department of Defense (DoD), and ITTC at The University of Kansas.
20 */
21
22#include "dsr-errorbuff.h"
23
24#include "ns3/ipv4-route.h"
25#include "ns3/log.h"
26#include "ns3/socket.h"
27
28#include <algorithm>
29#include <functional>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("DsrErrorBuffer");
35
36namespace dsr
37{
38
41{
42 Purge();
43 return m_errorBuffer.size();
44}
45
46bool
48{
49 Purge();
50 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
51 {
52 NS_LOG_INFO("packet id " << i->GetPacket()->GetUid() << " " << entry.GetPacket()->GetUid()
53 << " source " << i->GetSource() << " " << entry.GetSource()
54 << " next hop " << i->GetNextHop() << " " << entry.GetNextHop()
55 << " dst " << i->GetDestination() << " "
56 << entry.GetDestination());
57
58 // TODO check the source and destination over here
59 if ((i->GetPacket()->GetUid() == entry.GetPacket()->GetUid()) &&
60 (i->GetSource() == entry.GetSource()) && (i->GetNextHop() == entry.GetSource()) &&
61 (i->GetDestination() == entry.GetDestination()))
62 {
63 return false;
64 }
65 }
66
67 entry.SetExpireTime(m_errorBufferTimeout); // Initialize the send buffer timeout
68 /*
69 * Drop the most aged packet when buffer reaches to max
70 */
71 if (m_errorBuffer.size() >= m_maxLen)
72 {
73 Drop(m_errorBuffer.front(), "Drop the most aged packet"); // Drop the most aged packet
74 m_errorBuffer.erase(m_errorBuffer.begin());
75 }
76 // enqueue the entry
77 m_errorBuffer.push_back(entry);
78 return true;
79}
80
81void
83{
84 NS_LOG_FUNCTION(this << source << nextHop);
85 Purge();
86 std::vector<Ipv4Address> list;
87 list.push_back(source);
88 list.push_back(nextHop);
89 const std::vector<Ipv4Address> link = list;
90 /*
91 * Drop the packet with the error link source----------nextHop
92 */
93 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
94 {
95 if ((i->GetSource() == link[0]) && (i->GetNextHop() == link[1]))
96 {
97 DropLink(*i, "DropPacketForErrLink");
98 }
99 }
100
101 auto new_end =
102 std::remove_if(m_errorBuffer.begin(),
103 m_errorBuffer.end(),
104 [&](const DsrErrorBuffEntry& en) {
105 return (en.GetSource() == link[0]) && (en.GetNextHop() == link[1]);
106 });
107 m_errorBuffer.erase(new_end, m_errorBuffer.end());
108}
109
110bool
112{
113 Purge();
114 /*
115 * Dequeue the entry with destination address dst
116 */
117 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
118 {
119 if (i->GetDestination() == dst)
120 {
121 entry = *i;
122 i = m_errorBuffer.erase(i);
123 NS_LOG_DEBUG("Packet size while dequeuing " << entry.GetPacket()->GetSize());
124 return true;
125 }
126 }
127 return false;
128}
129
130bool
132{
133 /*
134 * Make sure if the send buffer contains entry with certain dst
135 */
136 for (auto i = m_errorBuffer.begin(); i != m_errorBuffer.end(); ++i)
137 {
138 if (i->GetDestination() == dst)
139 {
140 NS_LOG_DEBUG("Found the packet");
141 return true;
142 }
143 }
144 return false;
145}
146
147void
149{
150 /*
151 * Purge the buffer to eliminate expired entries
152 */
153 NS_LOG_DEBUG("The error buffer size " << m_errorBuffer.size());
154
155 for (const auto& item : m_errorBuffer)
156 {
157 if (item.GetExpireTime().IsStrictlyNegative())
158 {
159 NS_LOG_DEBUG("Dropping Queue Packets");
160 Drop(item, "Drop out-dated packet ");
161 }
162 }
163
164 [[maybe_unused]] auto erasedElementsNum =
165 std::erase_if(m_errorBuffer, [](const DsrErrorBuffEntry& e) {
167 });
168 NS_LOG_DEBUG("Purged " << erasedElementsNum << " from Error Buffer");
169}
170
171void
173{
174 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetDestination());
175 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
176 // Socket::ERROR_NOROUTETOHOST);
177}
178
179void
181{
182 NS_LOG_LOGIC(reason << en.GetPacket()->GetUid() << " " << en.GetSource() << " "
183 << en.GetNextHop());
184 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (),
185 // Socket::ERROR_NOROUTETOHOST);
186}
187} // namespace dsr
188} // namespace ns3
Ipv4 addresses are stored in host order in this class.
bool IsStrictlyNegative() const
Exactly equivalent to t < 0.
Definition nstime.h:332
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:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#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:274
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define list