blob: 4186af4e59957fb35588adaac387644b3e5b880c [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 Afanasyeva5bbe0e2011-11-22 17:28:39 -080029#include <boost/bind.hpp>
30#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 Afanasyeva46844b2011-11-21 19:13:26 -080036
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070037namespace ns3 {
38
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080039NS_OBJECT_ENSURE_REGISTERED (CcnxPit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070040
41using namespace __ccnx_private;
42
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080043TypeId
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070044CcnxPit::GetTypeId ()
45{
46 static TypeId tid = TypeId ("ns3::CcnxPit")
47 .SetGroupName ("Ccnx")
48 .SetParent<Object> ()
49 .AddConstructor<CcnxPit> ()
50 .AddAttribute ("CleanupTimeout",
51 "Timeout defining how frequent RIT should be cleaned up",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080052 StringValue ("1s"),
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070053 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
54 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080055
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080056 .AddAttribute ("PitEntryPruningTimout",
57 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
58 StringValue ("100ms"),
59 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
60 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080061
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080062 .AddAttribute ("PitEntryDefaultLifetime",
63 "Default lifetime of PIT entry (aka default Interest lifetime)",
64 StringValue("4s"),
65 MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime),
66 MakeTimeChecker ())
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070067
68 .AddAttribute ("MaxSize",
69 "Set maximum number of entries in PIT. If 0, limit is not enforced",
70 StringValue ("0"),
71 MakeUintegerAccessor (&CcnxPit::m_maxSize),
72 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070073 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070074
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070075 return tid;
76}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070077
78CcnxPit::CcnxPit ()
79{
80}
81
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080082CcnxPit::~CcnxPit ()
83{
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080084}
85
Alexander Afanasyev18252852011-11-21 13:35:31 -080086void
87CcnxPit::NotifyNewAggregate ()
88{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070089 if (m_fib == 0)
90 {
91 m_fib = GetObject<CcnxFib> ();
92 }
Alexander Afanasyev18252852011-11-21 13:35:31 -080093}
94
95void
96CcnxPit::DoDispose ()
97{
98 if (m_cleanupEvent.IsRunning ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 m_cleanupEvent.Cancel ();
100
Alexander Afanasyev18252852011-11-21 13:35:31 -0800101 clear ();
102}
103
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700104void
105CcnxPit::SetCleanupTimeout (const Time &timeout)
106{
107 m_cleanupTimeout = timeout;
108 if (m_cleanupEvent.IsRunning ())
109 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
110
111 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800112 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700113 &CcnxPit::CleanExpired, this);
114}
115
116Time
117CcnxPit::GetCleanupTimeout () const
118{
119 return m_cleanupTimeout;
120}
121
122void CcnxPit::CleanExpired ()
123{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800124 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700125 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800126
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800127 // uint32_t count = 0;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800128 while (!empty ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700129 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800130 CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin ();
131 if (entry->GetExpireTime () <= now) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700132 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800133 get<i_timestamp> ().erase (entry);
134 // count ++;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700135 }
136 else
137 break; // nothing else to do. All later records will not be stale
138 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800139
140 // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700141 // schedule next even
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800142
143 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700144 &CcnxPit::CleanExpired, this);
145}
146
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700147CcnxPitEntryContainer::type::iterator
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700148CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
149{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800150 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700151
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700152 CcnxPitEntryContainer::type::iterator entry = end ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700153
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700154 // do the longest prefix match
155 const CcnxNameComponents &name = header.GetName ();
156 for (size_t componentsCount = name.GetComponents ().size ()+1;
157 componentsCount > 0;
158 componentsCount--)
159 {
160 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1));
161
162 entry = get<i_prefix> ().find (subPrefix);
163 if (entry != end())
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700164 return entry;
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700165 }
166
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700167 throw CcnxPitEntryNotFound();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700168}
169
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800170boost::tuple<const CcnxPitEntry&, bool, bool>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800171CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700172{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700173 NS_LOG_FUNCTION (header.GetName ());
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
175
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800176 bool isDuplicate = false;
177 bool isNew = true;
178
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700179 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700180 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700181
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700182 if (entry == end ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800183 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700184 if (m_maxSize > 0 &&
185 size () >= m_maxSize)
186 {
187 // remove old record
188 get<i_timestamp> ().erase (get<i_timestamp> ().begin ());
189 }
190
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800191 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
Alexander Afanasyev07827182011-12-13 01:07:32 -0800192 NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (),
Ilya Moiseenkoad9e8ab2012-01-11 19:58:34 -0800193 "There should be at least default route set" << " Prefix = "<<header.GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800194
195 entry = insert (end (),
196 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800197 header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
198 : header.GetInterestLifetime (),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800199 *fibEntry));
200
201 // isDuplicate = false; // redundant
202 // isNew = true; // also redundant
203 }
204 else
205 {
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700206 NS_LOG_INFO ("ExpireTime: " << entry->m_expireTime.ToDouble (Time::S));
207 if (entry->m_expireTime - Simulator::Now () < MilliSeconds (10))
208 {
209 modify (entry,
210 boost::bind(&CcnxPitEntry::ClearIncoming, boost::lambda::_1));
211
212 modify (entry,
213 boost::bind(&CcnxPitEntry::ClearOutgoing, boost::lambda::_1));
214 }
215
216 isNew = entry->m_incoming.size () == 0 && entry->m_outgoing.size () == 0; // entry was preserved to detect loops, but technically removed
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800217 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800218 }
219
220 if (!isDuplicate)
221 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800222 modify (entry,
223 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800224 }
225
226 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700227}
228
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800229std::ostream& operator<< (std::ostream& os, const CcnxPit &pit)
230{
231 BOOST_FOREACH (const CcnxPitEntry &entry, pit)
232 {
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700233 if (entry.m_incoming.size () == 0 && entry.m_outgoing.size () == 0)
234 continue; // these are stale to-be-removed records, so there is no need to print them out
235
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700236 os << entry << std::endl;
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800237 }
238
239 return os;
240}
241
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700242} // namespace ns3