blob: 4536783097d59c62c9c2b38d2f452df11110c80b [file] [log] [blame]
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070019 */
20
21#include "per-fib-limits.h"
22
23#include "ns3/ndn-interest-header.h"
24#include "ns3/ndn-content-object-header.h"
25#include "ns3/ndn-pit.h"
26#include "ns3/ndn-pit-entry.h"
27
28#include "ns3/assert.h"
29#include "ns3/log.h"
30#include "ns3/simulator.h"
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070031#include "ns3/random-variable.h"
Alexander Afanasyevccbd8342012-08-16 16:54:39 -070032#include "ns3/double.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070033
34#include <boost/foreach.hpp>
35#include <boost/lambda/lambda.hpp>
36#include <boost/lambda/bind.hpp>
37namespace ll = boost::lambda;
38
39NS_LOG_COMPONENT_DEFINE ("ndn.fw.PerFibLimits");
40
41namespace ns3 {
42namespace ndn {
43namespace fw {
44
45NS_OBJECT_ENSURE_REGISTERED (PerFibLimits);
46
47TypeId
48PerFibLimits::GetTypeId (void)
49{
50 static TypeId tid = TypeId ("ns3::ndn::fw::PerFibLimits")
51 .SetGroupName ("Ndn")
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070052 .SetParent <super> ()
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070053 .AddConstructor <PerFibLimits> ()
54 ;
55 return tid;
56}
57
58PerFibLimits::PerFibLimits ()
59{
60}
61
62void
63PerFibLimits::DoDispose ()
64{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070065 super::DoDispose ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070066}
67
Alexander Afanasyev5db92172012-08-21 16:52:07 -070068void
69PerFibLimits::RemoveFace (Ptr<Face> face)
70{
Alexander Afanasyev5db92172012-08-21 16:52:07 -070071 for (PitQueueMap::iterator item = m_pitQueues.begin ();
72 item != m_pitQueues.end ();
73 item ++)
74 {
75 item->second.Remove (face);
76 }
77 m_pitQueues.erase (face);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070078
79 super::RemoveFace (face);
Alexander Afanasyev5db92172012-08-21 16:52:07 -070080}
81
82
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070083bool
Alexander Afanasyev5db92172012-08-21 16:52:07 -070084PerFibLimits::TrySendOutInterest (Ptr<Face> inFace,
85 Ptr<Face> outFace,
86 Ptr<const InterestHeader> header,
87 Ptr<const Packet> origPacket,
88 Ptr<pit::Entry> pitEntry)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070089{
90 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev5db92172012-08-21 16:52:07 -070091 // totally override all (if any) parent processing
Alexander Afanasyevff033952012-08-23 15:45:52 -070092
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070093 if (pitEntry->GetFwTag<PitQueueTag> () != boost::shared_ptr<PitQueueTag> ())
94 {
95 pitEntry->UpdateLifetime (Seconds (0.10));
96 NS_LOG_DEBUG ("Packet is still in queue and is waiting for its processing");
97 return true; // already in the queue
98 }
99
Alexander Afanasyevff033952012-08-23 15:45:52 -0700100 if (header->GetInterestLifetime () < Seconds (0.1))
101 {
102 NS_LOG_DEBUG( "What the fuck? Why interest lifetime is so short? [" << header->GetInterestLifetime ().ToDouble (Time::S) << "s]");
103 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700104
105 pit::Entry::out_iterator outgoing =
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700106 pitEntry->GetOutgoing ().find (outFace);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700107
108 if (outgoing != pitEntry->GetOutgoing ().end ())
109 {
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700110 // just suppress without any other action
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700111 return false;
112 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700113
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700114 NS_LOG_DEBUG ("Limit: " << outFace->GetLimits ().m_curMaxLimit << ", outstanding: " << outFace->GetLimits ().m_outstanding);
115
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700116 if (outFace->GetLimits ().IsBelowLimit ())
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700117 {
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700118 pitEntry->AddOutgoing (outFace);
119
120 //transmission
121 Ptr<Packet> packetToSend = origPacket->Copy ();
122 outFace->Send (packetToSend);
123
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700124 DidSendOutInterest (outFace, header, origPacket, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700125 return true;
126 }
127 else
128 {
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700129 NS_LOG_DEBUG ("Face limit for " << header->GetName ());
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700130 }
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700131
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700132 // hack
133 // offset lifetime, so we don't keep entries in queue for too long
Alexander Afanasyevff033952012-08-23 15:45:52 -0700134 // pitEntry->OffsetLifetime (Seconds (0.010) + );
135 // std::cerr << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) * 1000 << "ms" << std::endl;
136 pitEntry->OffsetLifetime (Seconds (-pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
137 pitEntry->UpdateLifetime (Seconds (0.10));
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700138
139 // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()[header->GetName ()].incoming ().find (inFace)->second;
140 const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second;
141 double weight = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ());
142 if (weight < 0)
143 {
144 // if stats is unknown, gracefully accept interest with normal priority
145 weight = 1.0;
146 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700147
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700148 bool enqueued = m_pitQueues[outFace].Enqueue (inFace, pitEntry, weight);
Alexander Afanasyevff033952012-08-23 15:45:52 -0700149
150 // if (Simulator::GetContext () == 6)
151 // {
152 // // std::cerr << "Attempt to enqueue packet for " << pitEntry->GetPrefix () << ": " << (enqueued?"succeeded":"failed") << std::endl;
153 // }
154
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700155 if (enqueued)
156 {
157 NS_LOG_DEBUG ("PIT entry is enqueued for delayed processing. Telling that we forwarding possible");
158 return true;
159 }
160 else
161 return false;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700162}
163
164void
165PerFibLimits::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
166{
167 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700168 super::WillEraseTimedOutPendingInterest (pitEntry);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700169
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700170 if (pitEntry->GetOutgoing ().size () == 0)
171 {
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700172 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700173
174 NS_ASSERT (pitEntry->GetFwTag<PitQueueTag> () != boost::shared_ptr<PitQueueTag> ());
175 if (pitEntry->GetFwTag<PitQueueTag> ()->IsLastOneInQueues ())
176 {
177 nackHeader->SetNack (100);
178 }
179 else
180 {
181 nackHeader->SetNack (101);
182 }
183
184 Ptr<Packet> pkt = Create<Packet> ();
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700185 pkt->AddHeader (*nackHeader);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700186
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700187 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
188 face != pitEntry->GetIncoming ().end ();
189 face ++)
190 {
191 face->m_face->Send (pkt->Copy ());
192 }
193 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700194
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700195 PitQueue::Remove (pitEntry);
196
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700197 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
198 face != pitEntry->GetOutgoing ().end ();
199 face ++)
200 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700201 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700202 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700203
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700204 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700205}
206
207
208void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700209PerFibLimits::WillSatisfyPendingInterest (Ptr<Face> inFace,
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700210 Ptr<pit::Entry> pitEntry)
211{
212 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700213 super::WillSatisfyPendingInterest (inFace, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700214
215 PitQueue::Remove (pitEntry);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700216
217 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
218 face != pitEntry->GetOutgoing ().end ();
219 face ++)
220 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700221 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700222 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700223
224 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700225}
226
227
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700228void
229PerFibLimits::ProcessFromQueue ()
230{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700231 NS_LOG_FUNCTION (this);
232
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700233 for (PitQueueMap::iterator queue = m_pitQueues.begin ();
234 queue != m_pitQueues.end ();
235 queue++)
236 {
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700237 Ptr<Face> outFace = queue->first;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700238
239 NS_LOG_DEBUG ("Processing " << *outFace);
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700240
241 while (!queue->second.IsEmpty () && outFace->GetLimits ().IsBelowLimit ())
242 {
243 // now we have enqueued packet and have slot available. Send out delayed packet
244 Ptr<pit::Entry> pitEntry = queue->second.Pop ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700245 NS_ASSERT_MSG (pitEntry != 0, "There *have to* be an entry in queue");
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700246
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700247 // hack
248 // offset lifetime back, so PIT entry wouldn't prematurely expire
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700249
Alexander Afanasyevff033952012-08-23 15:45:52 -0700250 // std::cerr << Simulator::GetContext () << ", Lifetime before " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
251 pitEntry->OffsetLifetime (Seconds (-0.10) + Seconds (pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
252 // std::cerr << Simulator::GetContext () << ", Lifetime after " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
253
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700254 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700255
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700256 Ptr<Packet> packetToSend = Create<Packet> ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700257 Ptr<InterestHeader> header = Create<InterestHeader> (*pitEntry->GetInterest ());
258 NS_LOG_DEBUG ("Adjust interest lifetime to " << pitEntry->GetExpireTime () - Simulator::Now () << "s");
259 // header->SetInterestLifetime (
260 // // header->GetInterestLifetime () - ()
261 // pitEntry->GetExpireTime () - Simulator::Now ()
262 // );
263 // std::cerr << "New lifetime: " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
264 packetToSend->AddHeader (*header);
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700265
266 NS_LOG_DEBUG ("Delayed sending for " << pitEntry->GetPrefix ());
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700267 outFace->Send (packetToSend);
268 DidSendOutInterest (outFace, pitEntry->GetInterest (), packetToSend, pitEntry);
269 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700270 }
271}
272
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700273void
274PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace,
275 uint32_t nackCode,
276 Ptr<pit::Entry> pitEntry)
277{
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700278 // NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700279 PitQueue::Remove (pitEntry);
280
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700281
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700282 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700283 nackHeader->SetNack (100);
284 Ptr<Packet> pkt = Create<Packet> ();
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700285 pkt->AddHeader (*nackHeader);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700286
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700287 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
288 face != pitEntry->GetIncoming ().end ();
289 face ++)
290 {
291 face->m_face->Send (pkt->Copy ());
292 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700293
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700294
295
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700296 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
297 face != pitEntry->GetOutgoing ().end ();
298 face ++)
299 {
300 face->m_face->GetLimits ().RemoveOutstanding ();
301 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700302
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700303 m_pit->MarkErased (pitEntry);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700304
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700305 ProcessFromQueue ();
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700306}
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700307
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700308
309} // namespace fw
310} // namespace ndn
311} // namespace ns3