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