blob: 6e63550bc7ed325400fe4920e9b8492fcebca800 [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",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080051 StringValue ("1s"),
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070052 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
53 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080054
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080055 .AddAttribute ("PitEntryPruningTimout",
56 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
57 StringValue ("100ms"),
58 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
59 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080060
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080061 .AddAttribute ("PitEntryDefaultLifetime",
62 "Default lifetime of PIT entry (aka default Interest lifetime)",
63 StringValue("4s"),
64 MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime),
65 MakeTimeChecker ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070066 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070067
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070068 return tid;
69}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070070
71CcnxPit::CcnxPit ()
72{
73}
74
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080075CcnxPit::~CcnxPit ()
76{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077 DoDispose ();
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080078}
79
Alexander Afanasyev18252852011-11-21 13:35:31 -080080void
81CcnxPit::NotifyNewAggregate ()
82{
83}
84
85void
86CcnxPit::DoDispose ()
87{
88 if (m_cleanupEvent.IsRunning ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080089 m_cleanupEvent.Cancel ();
90
Alexander Afanasyev18252852011-11-21 13:35:31 -080091 clear ();
92}
93
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070094void
95CcnxPit::SetCleanupTimeout (const Time &timeout)
96{
97 m_cleanupTimeout = timeout;
98 if (m_cleanupEvent.IsRunning ())
99 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
100
101 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800102 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700103 &CcnxPit::CleanExpired, this);
104}
105
106Time
107CcnxPit::GetCleanupTimeout () const
108{
109 return m_cleanupTimeout;
110}
111
112void CcnxPit::CleanExpired ()
113{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800114 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700115 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800116
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800117 // uint32_t count = 0;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 while (!empty ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700119 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800120 CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin ();
121 if (entry->GetExpireTime () <= now) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700122 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800123 get<i_timestamp> ().erase (entry);
124 // 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
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700143CcnxPitEntryContainer::type::iterator
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700144CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
145{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800146 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700147
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700148 CcnxPitEntryContainer::type::iterator entry = end ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700149
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700150 // do the longest prefix match
151 const CcnxNameComponents &name = header.GetName ();
152 for (size_t componentsCount = name.GetComponents ().size ()+1;
153 componentsCount > 0;
154 componentsCount--)
155 {
156 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1));
157
158 entry = get<i_prefix> ().find (subPrefix);
159 if (entry != end())
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700160 return entry;
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700161 }
162
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700163 throw CcnxPitEntryNotFound();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700164}
165
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800166boost::tuple<const CcnxPitEntry&, bool, bool>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800167CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700168{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700169 NS_LOG_FUNCTION (header.GetName ());
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700170 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
171
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800172 bool isDuplicate = false;
173 bool isNew = true;
174
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 Afanasyeva46844b2011-11-21 19:13:26 -0800179 {
180 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
Alexander Afanasyev07827182011-12-13 01:07:32 -0800181 NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (),
Ilya Moiseenkoad9e8ab2012-01-11 19:58:34 -0800182 "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 -0800183
184 entry = insert (end (),
185 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800186 header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
187 : header.GetInterestLifetime (),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800188 *fibEntry));
189
190 // isDuplicate = false; // redundant
191 // isNew = true; // also redundant
192 }
193 else
194 {
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700195 NS_LOG_INFO ("ExpireTime: " << entry->m_expireTime.ToDouble (Time::S));
196 if (entry->m_expireTime - Simulator::Now () < MilliSeconds (10))
197 {
198 modify (entry,
199 boost::bind(&CcnxPitEntry::ClearIncoming, boost::lambda::_1));
200
201 modify (entry,
202 boost::bind(&CcnxPitEntry::ClearOutgoing, boost::lambda::_1));
203 }
204
205 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 -0800206 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800207 }
208
209 if (!isDuplicate)
210 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800211 modify (entry,
212 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800213 }
214
215 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700216}
217
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800218std::ostream& operator<< (std::ostream& os, const CcnxPit &pit)
219{
220 BOOST_FOREACH (const CcnxPitEntry &entry, pit)
221 {
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700222 if (entry.m_incoming.size () == 0 && entry.m_outgoing.size () == 0)
223 continue; // these are stale to-be-removed records, so there is no need to print them out
224
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700225 os << entry << std::endl;
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800226 }
227
228 return os;
229}
230
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700231} // namespace ns3