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 | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 29 | #include <boost/lambda/bind.hpp> |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 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 | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 36 | namespace ll = boost::lambda; |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 38 | namespace ns3 { |
| 39 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 40 | NS_OBJECT_ENSURE_REGISTERED (CcnxPit); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace __ccnx_private; |
| 43 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 44 | TypeId |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 45 | CcnxPit::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 Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 53 | StringValue ("1s"), |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 54 | MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout), |
| 55 | MakeTimeChecker ()) |
Alexander Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 57 | .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 Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 63 | .AddAttribute ("PitEntryDefaultLifetime", |
| 64 | "Default lifetime of PIT entry (aka default Interest lifetime)", |
| 65 | StringValue("4s"), |
| 66 | MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime), |
| 67 | MakeTimeChecker ()) |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 68 | |
| 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 Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 74 | ; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 76 | return tid; |
| 77 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 78 | |
| 79 | CcnxPit::CcnxPit () |
| 80 | { |
| 81 | } |
| 82 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 83 | CcnxPit::~CcnxPit () |
| 84 | { |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 87 | void |
| 88 | CcnxPit::NotifyNewAggregate () |
| 89 | { |
Alexander Afanasyev | 3a4a0b3 | 2012-06-28 14:14:22 -0700 | [diff] [blame] | 90 | if (m_fib == 0) |
| 91 | { |
| 92 | m_fib = GetObject<CcnxFib> (); |
| 93 | } |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | CcnxPit::DoDispose () |
| 98 | { |
| 99 | if (m_cleanupEvent.IsRunning ()) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 100 | m_cleanupEvent.Cancel (); |
| 101 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 102 | clear (); |
| 103 | } |
| 104 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 105 | void |
| 106 | CcnxPit::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 Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 113 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 114 | &CcnxPit::CleanExpired, this); |
| 115 | } |
| 116 | |
| 117 | Time |
| 118 | CcnxPit::GetCleanupTimeout () const |
| 119 | { |
| 120 | return m_cleanupTimeout; |
| 121 | } |
| 122 | |
| 123 | void CcnxPit::CleanExpired () |
| 124 | { |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 125 | // NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 126 | Time now = Simulator::Now (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 128 | // uint32_t count = 0; |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 129 | while (!empty ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 130 | { |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 131 | CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin (); |
| 132 | if (entry->GetExpireTime () <= now) // is the record stale? |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 133 | { |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 134 | get<i_timestamp> ().erase (entry); |
| 135 | // count ++; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | break; // nothing else to do. All later records will not be stale |
| 139 | } |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 141 | // schedule next event |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 142 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 143 | &CcnxPit::CleanExpired, this); |
| 144 | } |
| 145 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 146 | CcnxPit::iterator |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 147 | CcnxPit::Lookup (const CcnxContentObjectHeader &header) const |
| 148 | { |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 149 | iterator entry = end (); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 151 | // 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 Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 161 | return entry; |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 162 | } |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 163 | |
| 164 | return end (); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 167 | CcnxPit::iterator |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 168 | CcnxPit::Lookup (const CcnxInterestHeader &header) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 169 | { |
Alexander Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 170 | NS_LOG_FUNCTION (header.GetName ()); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 171 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
| 172 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 173 | iterator entry = get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 174 | if (entry == end ()) |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 175 | return end (); |
| 176 | |
| 177 | return entry; |
| 178 | } |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 179 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 180 | bool |
| 181 | CcnxPit::CheckIfDuplicate (CcnxPit::iterator entry, const CcnxInterestHeader &header) |
| 182 | { |
| 183 | if (!entry->IsNonceSeen (header.GetNonce ())) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 184 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 185 | modify (entry, |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 186 | boost::bind(&CcnxPitEntry::AddSeenNonce, ll::_1, header.GetNonce ())); |
| 187 | return false; |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 188 | } |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 189 | else |
| 190 | return true; |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Alexander Afanasyev | f034cbd | 2012-06-29 14:28:31 -0700 | [diff] [blame^] | 193 | CcnxPit::iterator |
| 194 | CcnxPit::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 | |
| 218 | void |
| 219 | CcnxPit::MarkErased (CcnxPit::iterator entry) |
| 220 | { |
| 221 | modify (entry, |
| 222 | ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1, |
| 223 | Simulator::Now () + m_PitEntryPruningTimout)); |
| 224 | } |
| 225 | |
| 226 | |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 227 | std::ostream& operator<< (std::ostream& os, const CcnxPit &pit) |
| 228 | { |
| 229 | BOOST_FOREACH (const CcnxPitEntry &entry, pit) |
| 230 | { |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 231 | 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 Afanasyev | ff8c5d6 | 2012-04-25 15:14:51 -0700 | [diff] [blame] | 234 | os << entry << std::endl; |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return os; |
| 238 | } |
| 239 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 240 | } // namespace ns3 |