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", |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 51 | StringValue ("1s"), |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 52 | MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout), |
| 53 | MakeTimeChecker ()) |
Alexander Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 55 | .AddAttribute ("PitEntryPruningTimout", |
| 56 | "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again", |
| 57 | StringValue ("100ms"), |
| 58 | MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout), |
| 59 | MakeTimeChecker ()) |
Alexander Afanasyev | f9f4eb0 | 2011-12-16 01:51:14 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 61 | .AddAttribute ("PitEntryDefaultLifetime", |
| 62 | "Default lifetime of PIT entry (aka default Interest lifetime)", |
| 63 | StringValue("4s"), |
| 64 | MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime), |
| 65 | MakeTimeChecker ()) |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 66 | ; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 68 | return tid; |
| 69 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 70 | |
| 71 | CcnxPit::CcnxPit () |
| 72 | { |
| 73 | } |
| 74 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 75 | CcnxPit::~CcnxPit () |
| 76 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 77 | DoDispose (); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 80 | void |
| 81 | CcnxPit::NotifyNewAggregate () |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | CcnxPit::DoDispose () |
| 87 | { |
| 88 | if (m_cleanupEvent.IsRunning ()) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 89 | m_cleanupEvent.Cancel (); |
| 90 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 91 | clear (); |
| 92 | } |
| 93 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 94 | void |
| 95 | CcnxPit::SetCleanupTimeout (const Time &timeout) |
| 96 | { |
| 97 | m_cleanupTimeout = timeout; |
| 98 | if (m_cleanupEvent.IsRunning ()) |
| 99 | m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events |
| 100 | |
| 101 | // schedule even with new timeout |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 102 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 103 | &CcnxPit::CleanExpired, this); |
| 104 | } |
| 105 | |
| 106 | Time |
| 107 | CcnxPit::GetCleanupTimeout () const |
| 108 | { |
| 109 | return m_cleanupTimeout; |
| 110 | } |
| 111 | |
| 112 | void CcnxPit::CleanExpired () |
| 113 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 114 | NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 115 | Time now = Simulator::Now (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 117 | // uint32_t count = 0; |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 118 | while (!empty ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 119 | { |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 120 | CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin (); |
| 121 | if (entry->GetExpireTime () <= now) // is the record stale? |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 122 | { |
Alexander Afanasyev | d76f4aa | 2011-12-15 21:30:16 -0800 | [diff] [blame] | 123 | get<i_timestamp> ().erase (entry); |
| 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 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 143 | const CcnxPitEntry& |
| 144 | CcnxPit::Lookup (const CcnxContentObjectHeader &header) const |
| 145 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 146 | // NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 148 | CcnxPitEntryContainer::type::iterator entry = end (); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | ee217df | 2012-04-09 15:01:06 -0700 | [diff] [blame] | 150 | // do the longest prefix match |
| 151 | const CcnxNameComponents &name = header.GetName (); |
| 152 | for (size_t componentsCount = name.GetComponents ().size ()+1; |
| 153 | componentsCount > 0; |
| 154 | componentsCount--) |
| 155 | { |
| 156 | CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1)); |
| 157 | |
| 158 | entry = get<i_prefix> ().find (subPrefix); |
| 159 | if (entry != end()) |
| 160 | return *entry; |
| 161 | } |
| 162 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 163 | if (entry == end ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 164 | throw CcnxPitEntryNotFound(); |
| 165 | |
| 166 | return *entry; |
| 167 | } |
| 168 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 169 | boost::tuple<const CcnxPitEntry&, bool, bool> |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 170 | CcnxPit::Lookup (const CcnxInterestHeader &header) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 171 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 172 | NS_LOG_FUNCTION_NOARGS (); |
| 173 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
| 174 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 175 | bool isDuplicate = false; |
| 176 | bool isNew = true; |
| 177 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 178 | CcnxPitEntryContainer::type::iterator entry = |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 179 | get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 181 | if (entry == end ()) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 182 | { |
| 183 | CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header); |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 184 | NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (), |
Ilya Moiseenko | ad9e8ab | 2012-01-11 19:58:34 -0800 | [diff] [blame] | 185 | "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] | 186 | |
| 187 | entry = insert (end (), |
| 188 | CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()), |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 189 | header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime |
| 190 | : header.GetInterestLifetime (), |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 191 | *fibEntry)); |
| 192 | |
| 193 | // isDuplicate = false; // redundant |
| 194 | // isNew = true; // also redundant |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | isNew = false; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 199 | isDuplicate = entry->IsNonceSeen (header.GetNonce ()); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (!isDuplicate) |
| 203 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 204 | modify (entry, |
| 205 | boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ())); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return make_tuple (cref(*entry), isNew, isDuplicate); |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Alexander Afanasyev | a95b739 | 2012-03-09 10:54:10 -0800 | [diff] [blame] | 211 | std::ostream& operator<< (std::ostream& os, const CcnxPit &pit) |
| 212 | { |
| 213 | BOOST_FOREACH (const CcnxPitEntry &entry, pit) |
| 214 | { |
| 215 | os << entry; |
| 216 | } |
| 217 | |
| 218 | return os; |
| 219 | } |
| 220 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 221 | } // namespace ns3 |