Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 23 | #include "ns3/string.h" |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame^] | 24 | #include "ns3/uinteger.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 25 | #include "ns3/simulator.h" |
| 26 | #include "ccnx-interest-header.h" |
| 27 | #include "ccnx-content-object-header.h" |
| 28 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 29 | #include <boost/bind.hpp> |
| 30 | #include <boost/lambda/lambda.hpp> |
| 31 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE ("CcnxPit"); |
| 33 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 34 | using namespace boost::tuples; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 35 | using namespace boost; |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 37 | namespace ns3 { |
| 38 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 39 | NS_OBJECT_ENSURE_REGISTERED (CcnxPit); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 40 | |
| 41 | using namespace __ccnx_private; |
| 42 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 43 | TypeId |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 44 | CcnxPit::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 Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 52 | StringValue ("1s"), |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 53 | MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout), |
| 54 | MakeTimeChecker ()) |
Alexander Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 56 | .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 Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 62 | .AddAttribute ("PitEntryDefaultLifetime", |
| 63 | "Default lifetime of PIT entry (aka default Interest lifetime)", |
| 64 | StringValue("4s"), |
| 65 | MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime), |
| 66 | MakeTimeChecker ()) |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame^] | 67 | |
| 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 Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 73 | ; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 75 | return tid; |
| 76 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 77 | |
| 78 | CcnxPit::CcnxPit () |
| 79 | { |
| 80 | } |
| 81 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 82 | CcnxPit::~CcnxPit () |
| 83 | { |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 86 | void |
| 87 | CcnxPit::NotifyNewAggregate () |
| 88 | { |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame^] | 89 | if (m_fib == 0) |
| 90 | { |
| 91 | m_fib = GetObject<CcnxFib> (); |
| 92 | } |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | CcnxPit::DoDispose () |
| 97 | { |
| 98 | if (m_cleanupEvent.IsRunning ()) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 99 | m_cleanupEvent.Cancel (); |
| 100 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 101 | clear (); |
| 102 | } |
| 103 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 104 | void |
| 105 | CcnxPit::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 Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 112 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 113 | &CcnxPit::CleanExpired, this); |
| 114 | } |
| 115 | |
| 116 | Time |
| 117 | CcnxPit::GetCleanupTimeout () const |
| 118 | { |
| 119 | return m_cleanupTimeout; |
| 120 | } |
| 121 | |
| 122 | void CcnxPit::CleanExpired () |
| 123 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 124 | NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 125 | Time now = Simulator::Now (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 127 | // uint32_t count = 0; |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 128 | while (!empty ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 129 | { |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 130 | CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin (); |
| 131 | if (entry->GetExpireTime () <= now) // is the record stale? |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 132 | { |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 133 | get<i_timestamp> ().erase (entry); |
| 134 | // count ++; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | break; // nothing else to do. All later records will not be stale |
| 138 | } |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 139 | |
| 140 | // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 141 | // schedule next even |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 142 | |
| 143 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 144 | &CcnxPit::CleanExpired, this); |
| 145 | } |
| 146 | |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 147 | CcnxPitEntryContainer::type::iterator |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 148 | CcnxPit::Lookup (const CcnxContentObjectHeader &header) const |
| 149 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 150 | // NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 152 | CcnxPitEntryContainer::type::iterator entry = end (); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 154 | // 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 Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 164 | return entry; |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 167 | throw CcnxPitEntryNotFound(); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 170 | boost::tuple<const CcnxPitEntry&, bool, bool> |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 171 | CcnxPit::Lookup (const CcnxInterestHeader &header) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 172 | { |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 173 | NS_LOG_FUNCTION (header.GetName ()); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 174 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
| 175 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 176 | bool isDuplicate = false; |
| 177 | bool isNew = true; |
| 178 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 179 | CcnxPitEntryContainer::type::iterator entry = |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 180 | get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 182 | if (entry == end ()) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 183 | { |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame^] | 184 | 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 Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 191 | CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header); |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 192 | NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (), |
Ilya Moiseenko | ad9e8ab | 2012-01-11 19:58:34 -0800 | [diff] [blame] | 193 | "There should be at least default route set" << " Prefix = "<<header.GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 194 | |
| 195 | entry = insert (end (), |
| 196 | CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()), |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 197 | header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime |
| 198 | : header.GetInterestLifetime (), |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 199 | *fibEntry)); |
| 200 | |
| 201 | // isDuplicate = false; // redundant |
| 202 | // isNew = true; // also redundant |
| 203 | } |
| 204 | else |
| 205 | { |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 206 | 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 Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 217 | isDuplicate = entry->IsNonceSeen (header.GetNonce ()); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (!isDuplicate) |
| 221 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 222 | modify (entry, |
| 223 | boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ())); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return make_tuple (cref(*entry), isNew, isDuplicate); |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 229 | std::ostream& operator<< (std::ostream& os, const CcnxPit &pit) |
| 230 | { |
| 231 | BOOST_FOREACH (const CcnxPitEntry &entry, pit) |
| 232 | { |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 233 | 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 Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 236 | os << entry << std::endl; |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return os; |
| 240 | } |
| 241 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 242 | } // namespace ns3 |