blob: 4824bb1706cf8536cd19062f78171fa9ef3aba72 [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 Afanasyev3a4a0b32012-06-28 14:14:22 -070024#include "ns3/uinteger.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070025#include "ns3/simulator.h"
26#include "ccnx-interest-header.h"
27#include "ccnx-content-object-header.h"
28
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070029#include <boost/lambda/bind.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080030#include <boost/lambda/lambda.hpp>
31
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070032NS_LOG_COMPONENT_DEFINE ("CcnxPit");
33
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080034using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080035using namespace boost;
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070036namespace ll = boost::lambda;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080037
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070038namespace ns3 {
39
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080040NS_OBJECT_ENSURE_REGISTERED (CcnxPit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070041
42using namespace __ccnx_private;
43
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080044TypeId
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070045CcnxPit::GetTypeId ()
46{
47 static TypeId tid = TypeId ("ns3::CcnxPit")
48 .SetGroupName ("Ccnx")
49 .SetParent<Object> ()
50 .AddConstructor<CcnxPit> ()
51 .AddAttribute ("CleanupTimeout",
52 "Timeout defining how frequent RIT should be cleaned up",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080053 StringValue ("1s"),
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070054 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
55 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080056
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080057 .AddAttribute ("PitEntryPruningTimout",
58 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
59 StringValue ("100ms"),
60 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
61 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080062
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080063 .AddAttribute ("PitEntryDefaultLifetime",
64 "Default lifetime of PIT entry (aka default Interest lifetime)",
65 StringValue("4s"),
66 MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime),
67 MakeTimeChecker ())
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070068
69 .AddAttribute ("MaxSize",
70 "Set maximum number of entries in PIT. If 0, limit is not enforced",
71 StringValue ("0"),
72 MakeUintegerAccessor (&CcnxPit::m_maxSize),
73 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070074 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070075
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070076 return tid;
77}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070078
79CcnxPit::CcnxPit ()
80{
81}
82
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080083CcnxPit::~CcnxPit ()
84{
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080085}
86
Alexander Afanasyev18252852011-11-21 13:35:31 -080087void
88CcnxPit::NotifyNewAggregate ()
89{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070090 if (m_fib == 0)
91 {
92 m_fib = GetObject<CcnxFib> ();
93 }
Alexander Afanasyev18252852011-11-21 13:35:31 -080094}
95
96void
97CcnxPit::DoDispose ()
98{
99 if (m_cleanupEvent.IsRunning ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100 m_cleanupEvent.Cancel ();
101
Alexander Afanasyev18252852011-11-21 13:35:31 -0800102 clear ();
103}
104
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700105void
106CcnxPit::SetCleanupTimeout (const Time &timeout)
107{
108 m_cleanupTimeout = timeout;
109 if (m_cleanupEvent.IsRunning ())
110 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
111
112 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800113 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700114 &CcnxPit::CleanExpired, this);
115}
116
117Time
118CcnxPit::GetCleanupTimeout () const
119{
120 return m_cleanupTimeout;
121}
122
123void CcnxPit::CleanExpired ()
124{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700125 // NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700126 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800127
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800128 // uint32_t count = 0;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129 while (!empty ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700130 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800131 CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin ();
132 if (entry->GetExpireTime () <= now) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700133 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800134 get<i_timestamp> ().erase (entry);
135 // count ++;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700136 }
137 else
138 break; // nothing else to do. All later records will not be stale
139 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800140
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700141 // schedule next event
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800142 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700143 &CcnxPit::CleanExpired, this);
144}
145
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700146CcnxPit::iterator
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700147CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
148{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700149 iterator entry = end ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700150
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700151 // do the longest prefix match
152 const CcnxNameComponents &name = header.GetName ();
153 for (size_t componentsCount = name.GetComponents ().size ()+1;
154 componentsCount > 0;
155 componentsCount--)
156 {
157 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1));
158
159 entry = get<i_prefix> ().find (subPrefix);
160 if (entry != end())
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700161 return entry;
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700162 }
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700163
164 return end ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700165}
166
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700167CcnxPit::iterator
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800168CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700169{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700170 NS_LOG_FUNCTION (header.GetName ());
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700171 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
172
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700173 iterator entry = get<i_prefix> ().find (header.GetName ());
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174 if (entry == end ())
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700175 return end ();
176
177 return entry;
178}
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800179
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700180bool
181CcnxPit::CheckIfDuplicate (CcnxPit::iterator entry, const CcnxInterestHeader &header)
182{
183 if (!entry->IsNonceSeen (header.GetNonce ()))
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800184 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800185 modify (entry,
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700186 boost::bind(&CcnxPitEntry::AddSeenNonce, ll::_1, header.GetNonce ()));
187 return false;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800188 }
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700189 else
190 return true;
Ilya Moiseenko60491402011-10-28 13:10:16 -0700191}
192
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700193CcnxPit::iterator
194CcnxPit::Create (const CcnxInterestHeader &header)
195{
196 NS_ASSERT_MSG (get<i_prefix> ().find (header.GetName ()) == end (),
197 "Entry already exists, Create must not be called!!!");
198
199 if (m_maxSize > 0 &&
200 size () >= m_maxSize)
201 {
202 // remove old record
203 get<i_timestamp> ().erase (get<i_timestamp> ().begin ());
204 }
205
206 CcnxFib::iterator fibEntry = m_fib->LongestPrefixMatch (header);
207 // NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (),
208 // "There should be at least default route set" << " Prefix = "<<header.GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
209
210 return insert (end (),
211 CcnxPitEntry (ns3::Create<CcnxNameComponents> (header.GetName ()),
212 header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
213 : header.GetInterestLifetime (),
214 fibEntry));
215}
216
217
218void
219CcnxPit::MarkErased (CcnxPit::iterator entry)
220{
221 modify (entry,
222 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
223 Simulator::Now () + m_PitEntryPruningTimout));
224}
225
226
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800227std::ostream& operator<< (std::ostream& os, const CcnxPit &pit)
228{
229 BOOST_FOREACH (const CcnxPitEntry &entry, pit)
230 {
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700231 if (entry.m_incoming.size () == 0 && entry.m_outgoing.size () == 0)
232 continue; // these are stale to-be-removed records, so there is no need to print them out
233
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700234 os << entry << std::endl;
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800235 }
236
237 return os;
238}
239
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700240} // namespace ns3