blob: 85b4ecbd5c4e71224dcbc2174cfd0d5c0739f112 [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -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>
19 */
20
21#include "ccnx-pit.h"
22#include "ns3/log.h"
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080023#include "ns3/string.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070024#include "ns3/simulator.h"
25#include "ccnx-interest-header.h"
26#include "ccnx-content-object-header.h"
27
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080028#include <boost/bind.hpp>
29#include <boost/lambda/lambda.hpp>
30
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070031NS_LOG_COMPONENT_DEFINE ("CcnxPit");
32
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080033using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080034using namespace boost;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080035
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070036namespace ns3 {
37
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080038NS_OBJECT_ENSURE_REGISTERED (CcnxPit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070039
40using namespace __ccnx_private;
41
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080042TypeId
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070043CcnxPit::GetTypeId ()
44{
45 static TypeId tid = TypeId ("ns3::CcnxPit")
46 .SetGroupName ("Ccnx")
47 .SetParent<Object> ()
48 .AddConstructor<CcnxPit> ()
49 .AddAttribute ("CleanupTimeout",
50 "Timeout defining how frequent RIT should be cleaned up",
51 TimeValue (Seconds (1)),
52 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
53 MakeTimeChecker ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080054 .AddAttribute ("PitEntryPruningTimout",
55 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
56 StringValue ("100ms"),
57 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
58 MakeTimeChecker ())
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080059 .AddAttribute ("PitEntryDefaultLifetime",
60 "Default lifetime of PIT entry (aka default Interest lifetime)",
61 StringValue("4s"),
62 MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime),
63 MakeTimeChecker ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070064 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070065
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070066 return tid;
67}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070068
69CcnxPit::CcnxPit ()
70{
71}
72
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080073CcnxPit::~CcnxPit ()
74{
75 if (m_cleanupEvent.IsRunning ())
76 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
77
78 clear ();
79}
80
Alexander Afanasyev18252852011-11-21 13:35:31 -080081void
82CcnxPit::NotifyNewAggregate ()
83{
84}
85
86void
87CcnxPit::DoDispose ()
88{
89 if (m_cleanupEvent.IsRunning ())
90 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
91
92 clear ();
93}
94
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070095void
96CcnxPit::SetCleanupTimeout (const Time &timeout)
97{
98 m_cleanupTimeout = timeout;
99 if (m_cleanupEvent.IsRunning ())
100 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
101
102 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800103 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700104 &CcnxPit::CleanExpired, this);
105}
106
107Time
108CcnxPit::GetCleanupTimeout () const
109{
110 return m_cleanupTimeout;
111}
112
113void CcnxPit::CleanExpired ()
114{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800115 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700116 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800117
118 uint32_t count = 0;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700119 while( !empty() )
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700120 {
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700121 if( get<i_timestamp> ().front ().GetExpireTime () <= now ) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700122 {
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700123 get<i_timestamp> ().pop_front( );
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800124 count ++;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700125 }
126 else
127 break; // nothing else to do. All later records will not be stale
128 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800129
130 // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700131 // schedule next even
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800132
133 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700134 &CcnxPit::CleanExpired, this);
135}
136
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700137void
138CcnxPit::SetFib (Ptr<CcnxFib> fib)
139{
140 m_fib = fib;
141}
142
Ilya Moiseenko60491402011-10-28 13:10:16 -0700143bool
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800144CcnxPit::TryAddOutgoing (const CcnxPitEntry &pitEntry, Ptr<CcnxFace> face)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700145{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800146 NS_LOG_FUNCTION ("Face has " << m_bucketsPerFace[face->GetId()] <<
147 " packets with max allowance " << maxBucketsPerFace[face->GetId()]);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700148
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800149 if (m_bucketsPerFace[face->GetId()]+1.0 >= maxBucketsPerFace[face->GetId()])
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800150 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800151 NS_LOG_INFO("************ LIMIT **************");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800152 return false;
153 }
Ilya Moiseenko60491402011-10-28 13:10:16 -0700154
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800155 m_bucketsPerFace[face->GetId()] = m_bucketsPerFace[face->GetId()] + 1.0;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800156
157 modify (iterator_to (pitEntry),
158 bind(&CcnxPitEntry::AddOutgoing, lambda::_1, face));
159
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800160 return true;
Ilya Moiseenko60491402011-10-28 13:10:16 -0700161}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700162
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700163const CcnxPitEntry&
164CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
165{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800166 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700167
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700168 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700169 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700170
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700171 if (entry == end ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700172 throw CcnxPitEntryNotFound();
173
174 return *entry;
175}
176
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800177boost::tuple<const CcnxPitEntry&, bool, bool>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800178CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700179{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700180 NS_LOG_FUNCTION_NOARGS ();
181 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
182
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800183 bool isDuplicate = false;
184 bool isNew = true;
185
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700186 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700187 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700188
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700189 if (entry == end ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800190 {
191 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
192 NS_ASSERT_MSG (fibEntry != m_fib->end (),
193 "There should be at least default route set");
194
195 entry = insert (end (),
196 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
197 Simulator::Now () +
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800198 (header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
199 : header.GetInterestLifetime ()),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800200 *fibEntry));
201
202 // isDuplicate = false; // redundant
203 // isNew = true; // also redundant
204 }
205 else
206 {
207 isNew = false;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800208 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800209 }
210
211 if (!isDuplicate)
212 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800213 modify (entry,
214 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800215 }
216
217 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700218}
219
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800220///////////////////////////////////////////////////////////////////////////////////////////
221
Ilya Moiseenko60491402011-10-28 13:10:16 -0700222void
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800223CcnxPit::LeakBuckets ()
Ilya Moiseenko60491402011-10-28 13:10:16 -0700224{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800225 for (PitBucketIterator it = m_bucketsPerFace.begin();
226 it != m_bucketsPerFace.end();
227 it++)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700228 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800229 it->second = std::max (0.0, it->second - leakSize[it->first]);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700230 }
231}
232
233void
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800234CcnxPit::LeakBucket (Ptr<CcnxFace> face, int amount)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700235{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800236 m_bucketsPerFace[face->GetId()] = std::max (0.0, m_bucketsPerFace[face->GetId()] - amount);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700237}
238
239
240} // namespace ns3