|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2011 Yufei Cheng |
|
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: Yufei Cheng <yfcheng@ittc.ku.edu> |
|
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 |
|
32 #include "dsr-errorbuff.h" |
|
33 #include <algorithm> |
|
34 #include <functional> |
|
35 #include "ns3/ipv4-route.h" |
|
36 #include "ns3/socket.h" |
|
37 #include "ns3/log.h" |
|
38 |
|
39 NS_LOG_COMPONENT_DEFINE ("DsrErrorBuffer"); |
|
40 |
|
41 namespace ns3 { |
|
42 namespace dsr { |
|
43 |
|
44 uint32_t |
|
45 ErrorBuffer::GetSize () |
|
46 { |
|
47 Purge (); |
|
48 return m_errorBuffer.size (); |
|
49 } |
|
50 |
|
51 bool |
|
52 ErrorBuffer::Enqueue (ErrorBuffEntry & entry) |
|
53 { |
|
54 Purge (); |
|
55 for (std::vector<ErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i |
|
56 != m_errorBuffer.end (); ++i) |
|
57 { |
|
58 NS_LOG_INFO ("packet id " << i->GetPacket ()->GetUid () << " " << entry.GetPacket ()->GetUid () << " source " << i->GetSource () << " " << entry.GetSource () |
|
59 << " next hop " << i->GetNextHop () << " " << entry.GetNextHop () << " dst " << i->GetDestination () << " " << entry.GetDestination ()); |
|
60 |
|
61 if ((i->GetPacket ()->GetUid () == entry.GetPacket ()->GetUid ()) && (i->GetSource () == entry.GetSource ()) && (i->GetNextHop () == entry.GetSource ()) |
|
62 && (i->GetDestination () == entry.GetDestination ())) |
|
63 { |
|
64 return false; |
|
65 } |
|
66 } |
|
67 |
|
68 entry.SetExpireTime (m_errorBufferTimeout); // Initialize the send buffer timeout |
|
69 /* |
|
70 * Drop the most aged packet when buffer reaches to max |
|
71 */ |
|
72 if (m_errorBuffer.size () >= m_maxLen) |
|
73 { |
|
74 Drop (m_errorBuffer.front (), "Drop the most aged packet"); // Drop the most aged packet |
|
75 m_errorBuffer.erase (m_errorBuffer.begin ()); |
|
76 } |
|
77 // enqueue the entry |
|
78 m_errorBuffer.push_back (entry); |
|
79 return true; |
|
80 } |
|
81 |
|
82 void |
|
83 ErrorBuffer::DropPacketForErrLink (Ipv4Address source, Ipv4Address nextHop) |
|
84 { |
|
85 NS_LOG_FUNCTION (this << source << nextHop); |
|
86 Purge (); |
|
87 std::vector<Ipv4Address> list; |
|
88 list.push_back (source); |
|
89 list.push_back (nextHop); |
|
90 const std::vector<Ipv4Address> link = list; |
|
91 /* |
|
92 * Drop the packet with the error link source----------nextHop |
|
93 */ |
|
94 for (std::vector<ErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i |
|
95 != m_errorBuffer.end (); ++i) |
|
96 { |
|
97 if (LinkEqual (*i, link)) |
|
98 { |
|
99 DropLink (*i, "DropPacketForErrLink"); |
|
100 } |
|
101 } |
|
102 m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (), |
|
103 std::bind2nd (std::ptr_fun (ErrorBuffer::LinkEqual), link)), m_errorBuffer.end ()); |
|
104 } |
|
105 |
|
106 bool |
|
107 ErrorBuffer::Dequeue (Ipv4Address dst, ErrorBuffEntry & entry) |
|
108 { |
|
109 Purge (); |
|
110 /* |
|
111 * Dequeue the entry with destination address dst |
|
112 */ |
|
113 for (std::vector<ErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i != m_errorBuffer.end (); ++i) |
|
114 { |
|
115 if (i->GetDestination () == dst) |
|
116 { |
|
117 entry = *i; |
|
118 m_errorBuffer.erase (i); |
|
119 NS_LOG_DEBUG ("Packet size while dequeuing " << entry.GetPacket ()->GetSize ()); |
|
120 return true; |
|
121 } |
|
122 } |
|
123 return false; |
|
124 } |
|
125 |
|
126 bool |
|
127 ErrorBuffer::Find (Ipv4Address dst) |
|
128 { |
|
129 /* |
|
130 * Make sure if the send buffer contains entry with certain dst |
|
131 */ |
|
132 for (std::vector<ErrorBuffEntry>::const_iterator i = m_errorBuffer.begin (); i |
|
133 != m_errorBuffer.end (); ++i) |
|
134 { |
|
135 if (i->GetDestination () == dst) |
|
136 { |
|
137 NS_LOG_DEBUG ("Found the packet"); |
|
138 return true; |
|
139 } |
|
140 } |
|
141 return false; |
|
142 } |
|
143 |
|
144 struct IsExpired |
|
145 { |
|
146 bool |
|
147 operator() (ErrorBuffEntry const & e) const |
|
148 { |
|
149 // NS_LOG_DEBUG("Expire time for packet in req queue: "<<e.GetExpireTime ()); |
|
150 return (e.GetExpireTime () < Seconds (0)); |
|
151 } |
|
152 }; |
|
153 |
|
154 void |
|
155 ErrorBuffer::Purge () |
|
156 { |
|
157 /* |
|
158 * Purge the buffer to eliminate expired entries |
|
159 */ |
|
160 NS_LOG_DEBUG ("The error buffer size " << m_errorBuffer.size ()); |
|
161 IsExpired pred; |
|
162 for (std::vector<ErrorBuffEntry>::iterator i = m_errorBuffer.begin (); i |
|
163 != m_errorBuffer.end (); ++i) |
|
164 { |
|
165 if (pred (*i)) |
|
166 { |
|
167 NS_LOG_DEBUG ("Dropping Queue Packets"); |
|
168 Drop (*i, "Drop out-dated packet "); |
|
169 } |
|
170 } |
|
171 m_errorBuffer.erase (std::remove_if (m_errorBuffer.begin (), m_errorBuffer.end (), pred), |
|
172 m_errorBuffer.end ()); |
|
173 } |
|
174 |
|
175 void |
|
176 ErrorBuffer::Drop (ErrorBuffEntry en, std::string reason) |
|
177 { |
|
178 NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetDestination ()); |
|
179 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (), |
|
180 // Socket::ERROR_NOROUTETOHOST); |
|
181 return; |
|
182 } |
|
183 |
|
184 void |
|
185 ErrorBuffer::DropLink (ErrorBuffEntry en, std::string reason) |
|
186 { |
|
187 NS_LOG_LOGIC (reason << en.GetPacket ()->GetUid () << " " << en.GetSource () << " " << en.GetNextHop ()); |
|
188 // en.GetErrorCallback () (en.GetPacket (), en.GetDestination (), |
|
189 // Socket::ERROR_NOROUTETOHOST); |
|
190 return; |
|
191 } |
|
192 } // namespace dsr |
|
193 } // namespace ns3 |