blob: 30178574bcc212f4452056ab76bc0ef5fcb54067 [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"
23#include "ns3/simulator.h"
24#include "ccnx-interest-header.h"
25#include "ccnx-content-object-header.h"
26
27NS_LOG_COMPONENT_DEFINE ("CcnxPit");
28
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080029using namespace boost::tuples;
30
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070031namespace ns3 {
32
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080033NS_OBJECT_ENSURE_REGISTERED (CcnxPit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070034
35using namespace __ccnx_private;
36
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080037TypeId
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070038CcnxPit::GetTypeId ()
39{
40 static TypeId tid = TypeId ("ns3::CcnxPit")
41 .SetGroupName ("Ccnx")
42 .SetParent<Object> ()
43 .AddConstructor<CcnxPit> ()
44 .AddAttribute ("CleanupTimeout",
45 "Timeout defining how frequent RIT should be cleaned up",
46 TimeValue (Seconds (1)),
47 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
48 MakeTimeChecker ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080049 .AddAttribute ("PitEntryPruningTimout",
50 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
51 StringValue ("100ms"),
52 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
53 MakeTimeChecker ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070054 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070055
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070056 return tid;
57}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070058
59CcnxPit::CcnxPit ()
60{
61}
62
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080063CcnxPit::~CcnxPit ()
64{
65 if (m_cleanupEvent.IsRunning ())
66 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
67
68 clear ();
69}
70
Alexander Afanasyev18252852011-11-21 13:35:31 -080071void
72CcnxPit::NotifyNewAggregate ()
73{
74}
75
76void
77CcnxPit::DoDispose ()
78{
79 if (m_cleanupEvent.IsRunning ())
80 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
81
82 clear ();
83}
84
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070085void
86CcnxPit::SetCleanupTimeout (const Time &timeout)
87{
88 m_cleanupTimeout = timeout;
89 if (m_cleanupEvent.IsRunning ())
90 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
91
92 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080093 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070094 &CcnxPit::CleanExpired, this);
95}
96
97Time
98CcnxPit::GetCleanupTimeout () const
99{
100 return m_cleanupTimeout;
101}
102
103void CcnxPit::CleanExpired ()
104{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800105 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800107
108 uint32_t count = 0;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700109 while( !empty() )
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700110 {
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700111 if( get<i_timestamp> ().front ().GetExpireTime () <= now ) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700112 {
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700113 get<i_timestamp> ().pop_front( );
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800114 count ++;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700115 }
116 else
117 break; // nothing else to do. All later records will not be stale
118 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800119
120 // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700121 // schedule next even
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800122
123 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700124 &CcnxPit::CleanExpired, this);
125}
126
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700127void
128CcnxPit::SetFib (Ptr<CcnxFib> fib)
129{
130 m_fib = fib;
131}
132
Ilya Moiseenko60491402011-10-28 13:10:16 -0700133/*CcnxPitEntryContainer::type::iterator
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800134 CcnxPit::Add (const CcnxInterestHeader &header, CcnxFibEntryContainer::type::iterator fibEntry, Ptr<CcnxFace> face)
135 {
136 if( m_bucketsPerFace[face->GetId()]+1.0 >= maxBucketsPerFace[face->GetId()] )
137 {
138 // printf( "DEBUG: bucket overflow. Should not forward anything to interface %d\n", interest.interfaceIndex );
139 return end();
140 }
Ilya Moiseenko60491402011-10-28 13:10:16 -0700141
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800142 CcnxPitEntryContainer::type::iterator entry = insert (end (),
143 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
144 *fibEntry));
145 return entry;
146 }*/
Ilya Moiseenko60491402011-10-28 13:10:16 -0700147
Ilya Moiseenko60491402011-10-28 13:10:16 -0700148bool
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800149CcnxPit::TryAddOutgoing (CcnxPitEntryContainer::type::iterator pitEntry, Ptr<CcnxFace> face)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700150{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800151 NS_LOG_INFO ("Face has " << m_bucketsPerFace[face->GetId()] << " packets with max allowance " << maxBucketsPerFace[face->GetId()]);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700152
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800153 if((face->IsLocal() == false)
154 && (m_bucketsPerFace[face->GetId()]+1.0 >= maxBucketsPerFace[face->GetId()] ))
155 {
156 NS_LOG_INFO("********LIMIT**************");
157 return false;
158 }
Ilya Moiseenko60491402011-10-28 13:10:16 -0700159
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800160 m_bucketsPerFace[face->GetId()] = m_bucketsPerFace[face->GetId()] + 1.0;
Ilya Moiseenko60491402011-10-28 13:10:16 -0700161
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800162 NS_LOG_INFO(this->size());
163 NS_LOG_INFO("before modify");
164 NS_LOG_INFO(pitEntry->GetPrefix());
165 modify (pitEntry, CcnxPitEntry::AddOutgoing(face));
166 NS_LOG_INFO("after modify");
167 return true;
Ilya Moiseenko60491402011-10-28 13:10:16 -0700168}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700169
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700170const CcnxPitEntry&
171CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
172{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800173 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700175 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700176 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700177
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178 if (entry == end ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700179 throw CcnxPitEntryNotFound();
180
181 return *entry;
182}
183
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800184std::pair<CcnxPitEntryContainer::type::iterator,std::pair<bool, bool> >
185CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700186{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700187 NS_LOG_FUNCTION_NOARGS ();
188 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
189
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800190 bool isDuplicate = false;
191 bool isNew = true;
192
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700193 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700194 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700195
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700196 if (entry == end ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800197 {
198 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
199 NS_ASSERT_MSG (fibEntry != m_fib->end (),
200 "There should be at least default route set");
201
202 entry = insert (end (),
203 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
204 Simulator::Now () +
205 (header.GetInterestLifetime ().IsZero ()?DEFAULT_INTEREST_LIFETIME:
206 header.GetInterestLifetime ())
207 *fibEntry));
208
209 // isDuplicate = false; // redundant
210 // isNew = true; // also redundant
211 }
212 else
213 {
214 isNew = false;
215 isDuplicate = entry->IsNonceSeen (header->GetNonce ());
216 }
217
218 if (!isDuplicate)
219 {
220 modify (entry, boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header->GetNonce ()));
221 }
222
223 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700224}
225
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800226///////////////////////////////////////////////////////////////////////////////////////////
227
Ilya Moiseenko60491402011-10-28 13:10:16 -0700228void
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800229CcnxPit::LeakBuckets ()
Ilya Moiseenko60491402011-10-28 13:10:16 -0700230{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800231 for (PitBucketIterator it = m_bucketsPerFace.begin();
232 it != m_bucketsPerFace.end();
233 it++)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700234 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800235 it->second = std::max (0.0, it->second - leakSize[it->first]);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700236 }
237}
238
239void
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800240CcnxPit::LeakBucket (Ptr<CcnxFace> face, int amount)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700241{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800242 m_bucketsPerFace[face->GetId()] = std::max (0.0, m_bucketsPerFace[face->GetId()] - amount);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700243}
244
245
246} // namespace ns3