blob: 99dd6b5724f275fe0ac2540894959e070ae64ea1 [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
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -070023#include "ns3/ndn-l3-protocol.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070024#include "ns3/ndn-interest-header.h"
25#include "ns3/ndn-content-object-header.h"
26#include "ns3/ndn-pit.h"
27#include "ns3/ndn-pit-entry.h"
28
29#include "ns3/assert.h"
30#include "ns3/log.h"
31#include "ns3/simulator.h"
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070032#include "ns3/random-variable.h"
Alexander Afanasyevccbd8342012-08-16 16:54:39 -070033#include "ns3/double.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070034
35#include <boost/foreach.hpp>
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -070036#include <boost/lexical_cast.hpp>
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070037#include <boost/lambda/lambda.hpp>
38#include <boost/lambda/bind.hpp>
39namespace ll = boost::lambda;
40
41NS_LOG_COMPONENT_DEFINE ("ndn.fw.PerFibLimits");
42
43namespace ns3 {
44namespace ndn {
45namespace fw {
46
47NS_OBJECT_ENSURE_REGISTERED (PerFibLimits);
48
49TypeId
50PerFibLimits::GetTypeId (void)
51{
52 static TypeId tid = TypeId ("ns3::ndn::fw::PerFibLimits")
53 .SetGroupName ("Ndn")
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070054 .SetParent <super> ()
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070055 .AddConstructor <PerFibLimits> ()
Alexander Afanasyevc7719612012-08-30 17:42:20 -070056
Alexander Afanasyev0f83f902012-09-03 13:30:12 -070057 .AddAttribute ("QueueDropNotifications", "Enable explicit notifications (using nacks) that packet was dropped from queue",
58 BooleanValue (true),
59 MakeBooleanAccessor (&PerFibLimits::m_queueDropNotifications),
60 MakeBooleanChecker ())
Alexander Afanasyevcaeade22012-09-04 16:31:12 -070061
62 .AddAttribute ("WeightedRobin", "Enable weighted round robin for output queues",
63 BooleanValue (false),
64 MakeBooleanAccessor (&PerFibLimits::m_weightedRoundRobin),
65 MakeBooleanChecker ())
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070066 ;
67 return tid;
68}
69
70PerFibLimits::PerFibLimits ()
71{
72}
73
74void
75PerFibLimits::DoDispose ()
76{
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -070077 m_announceEvent.Cancel ();
78
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070079 super::DoDispose ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070080}
81
Alexander Afanasyev5db92172012-08-21 16:52:07 -070082void
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -070083PerFibLimits::NotifyNewAggregate ()
84{
85 super::NotifyNewAggregate ();
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -070086}
87
88void
Alexander Afanasyev5db92172012-08-21 16:52:07 -070089PerFibLimits::RemoveFace (Ptr<Face> face)
90{
Alexander Afanasyev5db92172012-08-21 16:52:07 -070091 for (PitQueueMap::iterator item = m_pitQueues.begin ();
92 item != m_pitQueues.end ();
93 item ++)
94 {
95 item->second.Remove (face);
96 }
97 m_pitQueues.erase (face);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070098
99 super::RemoveFace (face);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700100}
101
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700102bool
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700103PerFibLimits::TrySendOutInterest (Ptr<Face> inFace,
104 Ptr<Face> outFace,
105 Ptr<const InterestHeader> header,
106 Ptr<const Packet> origPacket,
107 Ptr<pit::Entry> pitEntry)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700108{
109 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700110 // totally override all (if any) parent processing
Alexander Afanasyevff033952012-08-23 15:45:52 -0700111
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700112 if (pitEntry->GetFwTag<PitQueueTag> () != boost::shared_ptr<PitQueueTag> ())
113 {
114 pitEntry->UpdateLifetime (Seconds (0.10));
115 NS_LOG_DEBUG ("Packet is still in queue and is waiting for its processing");
116 return true; // already in the queue
117 }
118
Alexander Afanasyevff033952012-08-23 15:45:52 -0700119 if (header->GetInterestLifetime () < Seconds (0.1))
120 {
Alexander Afanasyevcaeade22012-09-04 16:31:12 -0700121 NS_LOG_DEBUG( "Interest lifetime is so short? [" << header->GetInterestLifetime ().ToDouble (Time::S) << "s]");
Alexander Afanasyevff033952012-08-23 15:45:52 -0700122 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700123
124 pit::Entry::out_iterator outgoing =
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700125 pitEntry->GetOutgoing ().find (outFace);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700126
127 if (outgoing != pitEntry->GetOutgoing ().end ())
128 {
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700129 // just suppress without any other action
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700130 return false;
131 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700132
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700133 NS_LOG_DEBUG ("Limit: " << outFace->GetLimits ().m_curMaxLimit << ", outstanding: " << outFace->GetLimits ().m_outstanding);
134
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700135 if (outFace->GetLimits ().IsBelowLimit ())
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700136 {
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700137 pitEntry->AddOutgoing (outFace);
138
139 //transmission
140 Ptr<Packet> packetToSend = origPacket->Copy ();
141 outFace->Send (packetToSend);
142
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700143 DidSendOutInterest (outFace, header, origPacket, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700144 return true;
145 }
146 else
147 {
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700148 NS_LOG_DEBUG ("Face limit for " << header->GetName ());
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700149 }
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700150
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700151 // hack
152 // offset lifetime, so we don't keep entries in queue for too long
Alexander Afanasyevff033952012-08-23 15:45:52 -0700153 // pitEntry->OffsetLifetime (Seconds (0.010) + );
154 // std::cerr << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) * 1000 << "ms" << std::endl;
155 pitEntry->OffsetLifetime (Seconds (-pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
156 pitEntry->UpdateLifetime (Seconds (0.10));
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700157
Alexander Afanasyevcaeade22012-09-04 16:31:12 -0700158 double weight = 1.0;
159 if (m_weightedRoundRobin)
160 {
161 // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()[header->GetName ()].incoming ().find (inFace)->second;
162 const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second;
163 weight = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ());
164 // std::cout << ">>>> stats: " << stats << std::endl;
165 }
166 bool enqueued = m_pitQueues[outFace].Enqueue (inFace, pitEntry, weight);
Alexander Afanasyevff033952012-08-23 15:45:52 -0700167
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700168 if (enqueued)
169 {
170 NS_LOG_DEBUG ("PIT entry is enqueued for delayed processing. Telling that we forwarding possible");
171 return true;
172 }
173 else
174 return false;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700175}
176
177void
178PerFibLimits::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
179{
180 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
181
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700182 if (pitEntry->GetOutgoing ().size () == 0)
183 {
184 super::DidReceiveValidNack (0, 0, pitEntry); // semi safe
Alexander Afanasyev0f83f902012-09-03 13:30:12 -0700185
186 if (m_queueDropNotifications)
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700187 {
Alexander Afanasyev0f83f902012-09-03 13:30:12 -0700188 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
189
190 NS_ASSERT (pitEntry->GetFwTag<PitQueueTag> () != boost::shared_ptr<PitQueueTag> ());
191 if (pitEntry->GetFwTag<PitQueueTag> ()->IsLastOneInQueues ())
192 {
193 nackHeader->SetNack (100);
194 }
195 else
196 {
197 nackHeader->SetNack (101);
198 }
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700199
Alexander Afanasyev0f83f902012-09-03 13:30:12 -0700200 Ptr<Packet> pkt = Create<Packet> ();
201 pkt->AddHeader (*nackHeader);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700202
Alexander Afanasyev0f83f902012-09-03 13:30:12 -0700203 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
204 face != pitEntry->GetIncoming ().end ();
205 face ++)
206 {
207 face->m_face->Send (pkt->Copy ());
208 }
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700209 }
210 }
211 else
212 {
213 // make bad stats only for "legitimately" timed out interests
214 super::WillEraseTimedOutPendingInterest (pitEntry);
215 }
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700216
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700217 PitQueue::Remove (pitEntry);
218
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700219 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
220 face != pitEntry->GetOutgoing ().end ();
221 face ++)
222 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700223 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700224 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700225
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700226 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700227}
228
229
230void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700231PerFibLimits::WillSatisfyPendingInterest (Ptr<Face> inFace,
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700232 Ptr<pit::Entry> pitEntry)
233{
234 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700235 super::WillSatisfyPendingInterest (inFace, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700236
237 PitQueue::Remove (pitEntry);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700238
239 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
240 face != pitEntry->GetOutgoing ().end ();
241 face ++)
242 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700243 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700244 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700245
246 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700247}
248
249
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700250void
251PerFibLimits::ProcessFromQueue ()
252{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700253 NS_LOG_FUNCTION (this);
254
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700255 for (PitQueueMap::iterator queue = m_pitQueues.begin ();
256 queue != m_pitQueues.end ();
257 queue++)
258 {
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700259 Ptr<Face> outFace = queue->first;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700260
261 NS_LOG_DEBUG ("Processing " << *outFace);
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700262
263 while (!queue->second.IsEmpty () && outFace->GetLimits ().IsBelowLimit ())
264 {
265 // now we have enqueued packet and have slot available. Send out delayed packet
266 Ptr<pit::Entry> pitEntry = queue->second.Pop ();
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700267 if (pitEntry == 0)
268 {
269 outFace->GetLimits ().RemoveOutstanding ();
270 NS_LOG_DEBUG ("Though there are Interests in queue, weighted round robin decided that packet is not allowed yet");
271 break;
272 }
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700273
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700274 // hack
275 // offset lifetime back, so PIT entry wouldn't prematurely expire
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700276
Alexander Afanasyevff033952012-08-23 15:45:52 -0700277 // std::cerr << Simulator::GetContext () << ", Lifetime before " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
278 pitEntry->OffsetLifetime (Seconds (-0.10) + Seconds (pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
279 // std::cerr << Simulator::GetContext () << ", Lifetime after " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
280
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700281 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700282
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700283 Ptr<Packet> packetToSend = Create<Packet> ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700284 Ptr<InterestHeader> header = Create<InterestHeader> (*pitEntry->GetInterest ());
285 NS_LOG_DEBUG ("Adjust interest lifetime to " << pitEntry->GetExpireTime () - Simulator::Now () << "s");
286 // header->SetInterestLifetime (
287 // // header->GetInterestLifetime () - ()
288 // pitEntry->GetExpireTime () - Simulator::Now ()
289 // );
290 // std::cerr << "New lifetime: " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
291 packetToSend->AddHeader (*header);
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700292
293 NS_LOG_DEBUG ("Delayed sending for " << pitEntry->GetPrefix ());
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700294 outFace->Send (packetToSend);
295 DidSendOutInterest (outFace, pitEntry->GetInterest (), packetToSend, pitEntry);
296 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700297 }
298}
299
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700300void
301PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace,
302 uint32_t nackCode,
303 Ptr<pit::Entry> pitEntry)
304{
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700305 super::DidReceiveValidNack (inFace, nackCode, pitEntry); // will reset count stats
Alexander Afanasyev187cba22012-08-28 11:16:52 -0700306
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700307 // NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
308 PitQueue::Remove (pitEntry);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700309
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700310 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
311 nackHeader->SetNack (100);
312 Ptr<Packet> pkt = Create<Packet> ();
313 pkt->AddHeader (*nackHeader);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700314
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700315 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
316 face != pitEntry->GetIncoming ().end ();
317 face ++)
318 {
319 face->m_face->Send (pkt->Copy ());
320 }
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700321
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700322 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
323 face != pitEntry->GetOutgoing ().end ();
324 face ++)
325 {
326 face->m_face->GetLimits ().RemoveOutstanding ();
327 }
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -0700328
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700329 m_pit->MarkErased (pitEntry);
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -0700330
Alexander Afanasyevc7719612012-08-30 17:42:20 -0700331 ProcessFromQueue ();
Alexander Afanasyevb1ec2492012-08-28 17:33:33 -0700332}
333
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700334
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700335
336} // namespace fw
337} // namespace ndn
338} // namespace ns3