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