blob: 89d4cc47ae92f6cd3654662aec01ca32ea7e48a9 [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> ());
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700142 // 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 Afanasyevc23a3e32012-08-28 00:16:23 -0700170 // if (pitEntry->GetOutgoing ().size () == 0)
171 // {
172 // Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700173
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700174 // 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 // }
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700183
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700184 // Ptr<Packet> pkt = Create<Packet> ();
185 // pkt->AddHeader (*nackHeader);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700186
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -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 Afanasyev187cba22012-08-28 11:16:52 -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 Afanasyev187cba22012-08-28 11:16:52 -0700245 if (pitEntry == 0)
246 {
247 outFace->GetLimits ().RemoveOutstanding ();
248 NS_LOG_DEBUG ("Though there are Interests in queue, weighted round robin decided that packet is not allowed yet");
249 break;
250 }
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700251
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700252 // hack
253 // offset lifetime back, so PIT entry wouldn't prematurely expire
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700254
Alexander Afanasyevff033952012-08-23 15:45:52 -0700255 // std::cerr << Simulator::GetContext () << ", Lifetime before " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
256 pitEntry->OffsetLifetime (Seconds (-0.10) + Seconds (pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
257 // std::cerr << Simulator::GetContext () << ", Lifetime after " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
258
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700259 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700260
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700261 Ptr<Packet> packetToSend = Create<Packet> ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700262 Ptr<InterestHeader> header = Create<InterestHeader> (*pitEntry->GetInterest ());
263 NS_LOG_DEBUG ("Adjust interest lifetime to " << pitEntry->GetExpireTime () - Simulator::Now () << "s");
264 // header->SetInterestLifetime (
265 // // header->GetInterestLifetime () - ()
266 // pitEntry->GetExpireTime () - Simulator::Now ()
267 // );
268 // std::cerr << "New lifetime: " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
269 packetToSend->AddHeader (*header);
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700270
271 NS_LOG_DEBUG ("Delayed sending for " << pitEntry->GetPrefix ());
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700272 outFace->Send (packetToSend);
273 DidSendOutInterest (outFace, pitEntry->GetInterest (), packetToSend, pitEntry);
274 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700275 }
276}
277
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700278void
279PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace,
280 uint32_t nackCode,
281 Ptr<pit::Entry> pitEntry)
282{
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700283 super::DidReceiveValidNack (inFace, nackCode, pitEntry);
284
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700285 // NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700286 PitQueue::Remove (pitEntry);
287
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700288
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700289 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700290 // nackHeader->SetNack (100);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700291 Ptr<Packet> pkt = Create<Packet> ();
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700292 pkt->AddHeader (*nackHeader);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700293
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700294 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
295 face != pitEntry->GetIncoming ().end ();
296 face ++)
297 {
298 face->m_face->Send (pkt->Copy ());
299 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700300
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700301
302
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700303 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
304 face != pitEntry->GetOutgoing ().end ();
305 face ++)
306 {
307 face->m_face->GetLimits ().RemoveOutstanding ();
308 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700309
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700310 m_pit->MarkErased (pitEntry);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700311
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700312 ProcessFromQueue ();
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700313}
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700314
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700315
316} // namespace fw
317} // namespace ndn
318} // namespace ns3