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 | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 24 | #include "ns3/simulator.h" |
| 25 | #include "ccnx-interest-header.h" |
| 26 | #include "ccnx-content-object-header.h" |
| 27 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 28 | #include <boost/bind.hpp> |
| 29 | #include <boost/lambda/lambda.hpp> |
| 30 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 31 | NS_LOG_COMPONENT_DEFINE ("CcnxPit"); |
| 32 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 33 | using namespace boost::tuples; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 34 | using namespace boost; |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 36 | namespace ns3 { |
| 37 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 38 | NS_OBJECT_ENSURE_REGISTERED (CcnxPit); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 39 | |
| 40 | using namespace __ccnx_private; |
| 41 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 42 | TypeId |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 43 | CcnxPit::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", |
| 51 | TimeValue (Seconds (1)), |
| 52 | MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout), |
| 53 | MakeTimeChecker ()) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 54 | .AddAttribute ("PitEntryPruningTimout", |
| 55 | "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again", |
| 56 | StringValue ("100ms"), |
| 57 | MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout), |
| 58 | MakeTimeChecker ()) |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 59 | .AddAttribute ("PitEntryDefaultLifetime", |
| 60 | "Default lifetime of PIT entry (aka default Interest lifetime)", |
| 61 | StringValue("4s"), |
| 62 | MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime), |
| 63 | MakeTimeChecker ()) |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 64 | ; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 66 | return tid; |
| 67 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 68 | |
| 69 | CcnxPit::CcnxPit () |
| 70 | { |
| 71 | } |
| 72 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 73 | CcnxPit::~CcnxPit () |
| 74 | { |
| 75 | if (m_cleanupEvent.IsRunning ()) |
| 76 | m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events |
| 77 | |
| 78 | clear (); |
| 79 | } |
| 80 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 81 | void |
| 82 | CcnxPit::NotifyNewAggregate () |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | CcnxPit::DoDispose () |
| 88 | { |
| 89 | if (m_cleanupEvent.IsRunning ()) |
| 90 | m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events |
| 91 | |
| 92 | clear (); |
| 93 | } |
| 94 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 95 | void |
| 96 | CcnxPit::SetCleanupTimeout (const Time &timeout) |
| 97 | { |
| 98 | m_cleanupTimeout = timeout; |
| 99 | if (m_cleanupEvent.IsRunning ()) |
| 100 | m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events |
| 101 | |
| 102 | // schedule even with new timeout |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 103 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 104 | &CcnxPit::CleanExpired, this); |
| 105 | } |
| 106 | |
| 107 | Time |
| 108 | CcnxPit::GetCleanupTimeout () const |
| 109 | { |
| 110 | return m_cleanupTimeout; |
| 111 | } |
| 112 | |
| 113 | void CcnxPit::CleanExpired () |
| 114 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 115 | NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 116 | Time now = Simulator::Now (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 117 | |
| 118 | uint32_t count = 0; |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 119 | while( !empty() ) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 120 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 121 | if( get<i_timestamp> ().front ().GetExpireTime () <= now ) // is the record stale? |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 122 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 123 | get<i_timestamp> ().pop_front( ); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 124 | count ++; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 125 | } |
| 126 | else |
| 127 | break; // nothing else to do. All later records will not be stale |
| 128 | } |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 129 | |
| 130 | // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 131 | // schedule next even |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 132 | |
| 133 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 134 | &CcnxPit::CleanExpired, this); |
| 135 | } |
| 136 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 137 | void |
| 138 | CcnxPit::SetFib (Ptr<CcnxFib> fib) |
| 139 | { |
| 140 | m_fib = fib; |
| 141 | } |
| 142 | |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 143 | bool |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 144 | CcnxPit::TryAddOutgoing (const CcnxPitEntry &pitEntry, Ptr<CcnxFace> face) |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 145 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 146 | NS_LOG_FUNCTION ("Face has " << m_bucketsPerFace[face->GetId()] << |
| 147 | " packets with max allowance " << maxBucketsPerFace[face->GetId()]); |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 149 | if (m_bucketsPerFace[face->GetId()]+1.0 >= maxBucketsPerFace[face->GetId()]) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 150 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 151 | NS_LOG_INFO("************ LIMIT **************"); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 155 | m_bucketsPerFace[face->GetId()] = m_bucketsPerFace[face->GetId()] + 1.0; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 156 | |
| 157 | modify (iterator_to (pitEntry), |
| 158 | bind(&CcnxPitEntry::AddOutgoing, lambda::_1, face)); |
| 159 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 160 | return true; |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 161 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 163 | const CcnxPitEntry& |
| 164 | CcnxPit::Lookup (const CcnxContentObjectHeader &header) const |
| 165 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 166 | // NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 168 | CcnxPitEntryContainer::type::iterator entry = |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 169 | get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 171 | if (entry == end ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 172 | throw CcnxPitEntryNotFound(); |
| 173 | |
| 174 | return *entry; |
| 175 | } |
| 176 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 177 | boost::tuple<const CcnxPitEntry&, bool, bool> |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 178 | CcnxPit::Lookup (const CcnxInterestHeader &header) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 179 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 180 | NS_LOG_FUNCTION_NOARGS (); |
| 181 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
| 182 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 183 | bool isDuplicate = false; |
| 184 | bool isNew = true; |
| 185 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 186 | CcnxPitEntryContainer::type::iterator entry = |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 187 | get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 188 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 189 | if (entry == end ()) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 190 | { |
| 191 | CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header); |
| 192 | NS_ASSERT_MSG (fibEntry != m_fib->end (), |
| 193 | "There should be at least default route set"); |
| 194 | |
| 195 | entry = insert (end (), |
| 196 | CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()), |
| 197 | Simulator::Now () + |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 198 | (header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime |
| 199 | : header.GetInterestLifetime ()), |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 200 | *fibEntry)); |
| 201 | |
| 202 | // isDuplicate = false; // redundant |
| 203 | // isNew = true; // also redundant |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | isNew = false; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 208 | isDuplicate = entry->IsNonceSeen (header.GetNonce ()); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (!isDuplicate) |
| 212 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 213 | modify (entry, |
| 214 | boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ())); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return make_tuple (cref(*entry), isNew, isDuplicate); |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 220 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 221 | |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 222 | void |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 223 | CcnxPit::LeakBuckets () |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 224 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 225 | for (PitBucketIterator it = m_bucketsPerFace.begin(); |
| 226 | it != m_bucketsPerFace.end(); |
| 227 | it++) |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 228 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 229 | it->second = std::max (0.0, it->second - leakSize[it->first]); |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | void |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 234 | CcnxPit::LeakBucket (Ptr<CcnxFace> face, int amount) |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 235 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 236 | m_bucketsPerFace[face->GetId()] = std::max (0.0, m_bucketsPerFace[face->GetId()] - amount); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | } // namespace ns3 |