blob: 43355e32b8bfb1b7b1e39cd9cc0d2d6fcdfa42e8 [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
93 if (header->GetInterestLifetime () < Seconds (0.1))
94 {
95 NS_LOG_DEBUG( "What the fuck? Why interest lifetime is so short? [" << header->GetInterestLifetime ().ToDouble (Time::S) << "s]");
96 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070097
98 pit::Entry::out_iterator outgoing =
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070099 pitEntry->GetOutgoing ().find (outFace);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700100
101 if (outgoing != pitEntry->GetOutgoing ().end ())
102 {
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700103 // just suppress without any other action
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700104 return false;
105 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700106
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700107 NS_LOG_DEBUG ("Limit: " << outFace->GetLimits ().m_curMaxLimit << ", outstanding: " << outFace->GetLimits ().m_outstanding);
108
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700109 if (outFace->GetLimits ().IsBelowLimit ())
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700110 {
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700111 pitEntry->AddOutgoing (outFace);
112
113 //transmission
114 Ptr<Packet> packetToSend = origPacket->Copy ();
115 outFace->Send (packetToSend);
116
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700117 DidSendOutInterest (outFace, header, origPacket, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700118 return true;
119 }
120 else
121 {
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700122 NS_LOG_DEBUG ("Face limit for " << header->GetName ());
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700123 }
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700124
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700125 // hack
126 // offset lifetime, so we don't keep entries in queue for too long
Alexander Afanasyevff033952012-08-23 15:45:52 -0700127 // pitEntry->OffsetLifetime (Seconds (0.010) + );
128 // std::cerr << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) * 1000 << "ms" << std::endl;
129 pitEntry->OffsetLifetime (Seconds (-pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
130 pitEntry->UpdateLifetime (Seconds (0.10));
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700131
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700132 bool enqueued = m_pitQueues[outFace].Enqueue (inFace, pitEntry);
Alexander Afanasyevff033952012-08-23 15:45:52 -0700133
134 // if (Simulator::GetContext () == 6)
135 // {
136 // // std::cerr << "Attempt to enqueue packet for " << pitEntry->GetPrefix () << ": " << (enqueued?"succeeded":"failed") << std::endl;
137 // }
138
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700139 if (enqueued)
140 {
141 NS_LOG_DEBUG ("PIT entry is enqueued for delayed processing. Telling that we forwarding possible");
142 return true;
143 }
144 else
145 return false;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700146}
147
148void
149PerFibLimits::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
150{
151 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700152 super::WillEraseTimedOutPendingInterest (pitEntry);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700153
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700154 if (pitEntry->GetOutgoing ().size () == 0)
155 {
156 Ptr<Packet> pkt = Create<Packet> ();
157 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
158 nackHeader->SetNack (99);
159 pkt->AddHeader (*nackHeader);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700160
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700161 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
162 face != pitEntry->GetIncoming ().end ();
163 face ++)
164 {
165 face->m_face->Send (pkt->Copy ());
166 }
167 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700168
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700169 PitQueue::Remove (pitEntry);
170
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700171 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
172 face != pitEntry->GetOutgoing ().end ();
173 face ++)
174 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700175 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700176 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700177
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700178 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700179}
180
181
182void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700183PerFibLimits::WillSatisfyPendingInterest (Ptr<Face> inFace,
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700184 Ptr<pit::Entry> pitEntry)
185{
186 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700187 super::WillSatisfyPendingInterest (inFace, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700188
189 PitQueue::Remove (pitEntry);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700190
191 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
192 face != pitEntry->GetOutgoing ().end ();
193 face ++)
194 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700195 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700196 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700197
198 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700199}
200
201
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700202void
203PerFibLimits::ProcessFromQueue ()
204{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700205 NS_LOG_FUNCTION (this);
206
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700207 for (PitQueueMap::iterator queue = m_pitQueues.begin ();
208 queue != m_pitQueues.end ();
209 queue++)
210 {
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700211 Ptr<Face> outFace = queue->first;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700212
213 NS_LOG_DEBUG ("Processing " << *outFace);
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700214
215 while (!queue->second.IsEmpty () && outFace->GetLimits ().IsBelowLimit ())
216 {
217 // now we have enqueued packet and have slot available. Send out delayed packet
218 Ptr<pit::Entry> pitEntry = queue->second.Pop ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700219 NS_ASSERT_MSG (pitEntry != 0, "There *have to* be an entry in queue");
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700220
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700221 // hack
222 // offset lifetime back, so PIT entry wouldn't prematurely expire
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700223
Alexander Afanasyevff033952012-08-23 15:45:52 -0700224 // std::cerr << Simulator::GetContext () << ", Lifetime before " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
225 pitEntry->OffsetLifetime (Seconds (-0.10) + Seconds (pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
226 // std::cerr << Simulator::GetContext () << ", Lifetime after " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
227
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700228 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700229
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700230 Ptr<Packet> packetToSend = Create<Packet> ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700231 Ptr<InterestHeader> header = Create<InterestHeader> (*pitEntry->GetInterest ());
232 NS_LOG_DEBUG ("Adjust interest lifetime to " << pitEntry->GetExpireTime () - Simulator::Now () << "s");
233 // header->SetInterestLifetime (
234 // // header->GetInterestLifetime () - ()
235 // pitEntry->GetExpireTime () - Simulator::Now ()
236 // );
237 // std::cerr << "New lifetime: " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
238 packetToSend->AddHeader (*header);
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700239
240 NS_LOG_DEBUG ("Delayed sending for " << pitEntry->GetPrefix ());
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700241 outFace->Send (packetToSend);
242 DidSendOutInterest (outFace, pitEntry->GetInterest (), packetToSend, pitEntry);
243 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700244 }
245}
246
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700247void
248PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace,
249 uint32_t nackCode,
250 Ptr<pit::Entry> pitEntry)
251{
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700252 // NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700253
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700254 Ptr<Packet> pkt = Create<Packet> ();
255 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
256 nackHeader->SetNack (99);
257 pkt->AddHeader (*nackHeader);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700258
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700259 for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
260 face != pitEntry->GetIncoming ().end ();
261 face ++)
262 {
263 face->m_face->Send (pkt->Copy ());
264 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700265
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700266 PitQueue::Remove (pitEntry);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700267
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700268 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
269 face != pitEntry->GetOutgoing ().end ();
270 face ++)
271 {
272 face->m_face->GetLimits ().RemoveOutstanding ();
273 }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700274
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700275 m_pit->MarkErased (pitEntry);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700276
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700277 ProcessFromQueue ();
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700278}
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700279
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700280
281} // namespace fw
282} // namespace ndn
283} // namespace ns3