blob: 8190329c54e4bb5933be50d3e4fdf06d7a4abee4 [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 Afanasyevff033952012-08-23 15:45:52 -0700154 // Ptr<Packet> pkt = Create<Packet> ();
155 // Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
156 // nackHeader->SetNack (99);
157 // pkt->AddHeader (*nackHeader);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700158
Alexander Afanasyevff033952012-08-23 15:45:52 -0700159 // for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
160 // face != pitEntry->GetIncoming ().end ();
161 // face ++)
162 // {
163 // face->m_face->Send (pkt->Copy ());
164 // }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700165
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700166 PitQueue::Remove (pitEntry);
167
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700168 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
169 face != pitEntry->GetOutgoing ().end ();
170 face ++)
171 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700172 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700173 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700174
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700175 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700176}
177
178
179void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700180PerFibLimits::WillSatisfyPendingInterest (Ptr<Face> inFace,
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700181 Ptr<pit::Entry> pitEntry)
182{
183 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700184 super::WillSatisfyPendingInterest (inFace, pitEntry);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700185
186 PitQueue::Remove (pitEntry);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700187
188 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
189 face != pitEntry->GetOutgoing ().end ();
190 face ++)
191 {
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700192 face->m_face->GetLimits ().RemoveOutstanding ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700193 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700194
195 ProcessFromQueue ();
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700196}
197
198
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700199void
200PerFibLimits::ProcessFromQueue ()
201{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700202 NS_LOG_FUNCTION (this);
203
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700204 for (PitQueueMap::iterator queue = m_pitQueues.begin ();
205 queue != m_pitQueues.end ();
206 queue++)
207 {
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700208 Ptr<Face> outFace = queue->first;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700209
210 NS_LOG_DEBUG ("Processing " << *outFace);
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700211
212 while (!queue->second.IsEmpty () && outFace->GetLimits ().IsBelowLimit ())
213 {
214 // now we have enqueued packet and have slot available. Send out delayed packet
215 Ptr<pit::Entry> pitEntry = queue->second.Pop ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700216 NS_ASSERT_MSG (pitEntry != 0, "There *have to* be an entry in queue");
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700217
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700218 // hack
219 // offset lifetime back, so PIT entry wouldn't prematurely expire
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700220
Alexander Afanasyevff033952012-08-23 15:45:52 -0700221 // std::cerr << Simulator::GetContext () << ", Lifetime before " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
222 pitEntry->OffsetLifetime (Seconds (-0.10) + Seconds (pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S)));
223 // std::cerr << Simulator::GetContext () << ", Lifetime after " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
224
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700225 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700226
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700227 Ptr<Packet> packetToSend = Create<Packet> ();
Alexander Afanasyevff033952012-08-23 15:45:52 -0700228 Ptr<InterestHeader> header = Create<InterestHeader> (*pitEntry->GetInterest ());
229 NS_LOG_DEBUG ("Adjust interest lifetime to " << pitEntry->GetExpireTime () - Simulator::Now () << "s");
230 // header->SetInterestLifetime (
231 // // header->GetInterestLifetime () - ()
232 // pitEntry->GetExpireTime () - Simulator::Now ()
233 // );
234 // std::cerr << "New lifetime: " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl;
235 packetToSend->AddHeader (*header);
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700236
237 NS_LOG_DEBUG ("Delayed sending for " << pitEntry->GetPrefix ());
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700238 outFace->Send (packetToSend);
239 DidSendOutInterest (outFace, pitEntry->GetInterest (), packetToSend, pitEntry);
240 }
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700241 }
242}
243
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700244void
245PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace,
246 uint32_t nackCode,
247 Ptr<pit::Entry> pitEntry)
248{
249 // super::DidReceiveValidNack (inFace, nackCode, pitEntry);
250 // NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700251
Alexander Afanasyevff033952012-08-23 15:45:52 -0700252 // Ptr<Packet> pkt = Create<Packet> ();
253 // Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ());
254 // nackHeader->SetNack (99);
255 // pkt->AddHeader (*nackHeader);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700256
Alexander Afanasyevff033952012-08-23 15:45:52 -0700257 // for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin ();
258 // face != pitEntry->GetIncoming ().end ();
259 // face ++)
260 // {
261 // face->m_face->Send (pkt->Copy ());
262 // }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700263
Alexander Afanasyevff033952012-08-23 15:45:52 -0700264 // PitQueue::Remove (pitEntry);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700265
Alexander Afanasyevff033952012-08-23 15:45:52 -0700266 // for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
267 // face != pitEntry->GetOutgoing ().end ();
268 // face ++)
269 // {
270 // face->m_face->GetLimits ().RemoveOutstanding ();
271 // }
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700272
Alexander Afanasyevff033952012-08-23 15:45:52 -0700273 // m_pit->MarkErased (pitEntry);
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700274
Alexander Afanasyevff033952012-08-23 15:45:52 -0700275 // ProcessFromQueue ();
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700276}
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700277
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700278
279} // namespace fw
280} // namespace ndn
281} // namespace ns3