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 | #ifndef _CCNX_PIT_H_ |
| 22 | #define _CCNX_PIT_H_ |
| 23 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 24 | #include "ns3/object.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 25 | #include "ns3/nstime.h" |
| 26 | #include "ns3/event-id.h" |
| 27 | |
| 28 | #include "hash-helper.h" |
| 29 | #include "ccnx-pit-entry.h" |
| 30 | |
| 31 | #include <boost/multi_index_container.hpp> |
| 32 | #include <boost/multi_index/tag.hpp> |
| 33 | #include <boost/multi_index/ordered_index.hpp> |
| 34 | #include <boost/multi_index/composite_key.hpp> |
| 35 | #include <boost/multi_index/hashed_index.hpp> |
| 36 | #include <boost/multi_index/member.hpp> |
| 37 | #include <boost/multi_index/mem_fun.hpp> |
| 38 | #include <boost/multi_index/sequenced_index.hpp> |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 39 | #include <map> |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 40 | #include <iostream> |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 41 | #include <algorithm> |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 42 | |
| 43 | namespace ns3 { |
| 44 | |
| 45 | class Ccnx; |
| 46 | class CcnxFace; |
| 47 | class CcnxContentObjectHeader; |
| 48 | class CcnxInterestHeader; |
| 49 | |
| 50 | /** |
| 51 | * \ingroup ccnx |
| 52 | * \private |
| 53 | * \brief Private namespace for CCNx PIT implementation |
| 54 | */ |
| 55 | namespace __ccnx_private |
| 56 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 57 | // class i_prefix{}; ///< tag for prefix hash |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 58 | class i_timestamp {}; ///< tag for timestamp-ordered records (for cleanup optimization) |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * \ingroup ccnx |
| 63 | * \brief Typedef for RIT container implemented as a Boost.MultiIndex container |
| 64 | * |
| 65 | * - First index (tag<i_prefix>) is a unique hash index based on |
| 66 | * prefixes |
| 67 | * - Second index (tag<i_timestamp>) is a sequenced index based on |
| 68 | * arrival order (for clean-up optimizations) |
| 69 | * |
| 70 | * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library |
| 71 | */ |
| 72 | struct CcnxPitEntryContainer |
| 73 | { |
| 74 | typedef |
| 75 | boost::multi_index::multi_index_container< |
| 76 | CcnxPitEntry, |
| 77 | boost::multi_index::indexed_by< |
| 78 | // indexed by hash |
| 79 | boost::multi_index::hashed_unique< |
| 80 | boost::multi_index::tag<__ccnx_private::i_prefix>, |
| 81 | boost::multi_index::const_mem_fun<CcnxPitEntry, const CcnxNameComponents&, &CcnxPitEntry::GetPrefix>, |
| 82 | CcnxPrefixHash |
| 83 | >, |
| 84 | // sequenced to implement MRU |
| 85 | boost::multi_index::sequenced< |
| 86 | boost::multi_index::tag<__ccnx_private::i_timestamp> > |
| 87 | > |
| 88 | > type; |
| 89 | }; |
| 90 | |
| 91 | // typedef std::map<int,int> PitCounter; |
| 92 | // typedef std::map<int,int>::iterator PitCounterIterator; |
| 93 | |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 94 | typedef std::map<int,double> PitBucket; |
| 95 | typedef std::map<int,double>::iterator PitBucketIterator; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | //////////////////////////////////////////////////////////////////////// |
| 99 | //////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | /** |
| 102 | * \ingroup ccnx |
| 103 | * \brief Class implementing Pending Interests Table |
| 104 | */ |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 105 | class CcnxPit : public CcnxPitEntryContainer::type, public Object |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 106 | { |
| 107 | public: |
| 108 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 109 | * \brief Interface ID |
| 110 | * |
| 111 | * \return interface ID |
| 112 | */ |
| 113 | static TypeId GetTypeId (); |
| 114 | |
| 115 | /** |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 116 | * \brief PIT constructor |
| 117 | */ |
| 118 | CcnxPit (); |
| 119 | |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 120 | /*CcnxPitEntryContainer::type::iterator |
| 121 | Add (const CcnxInterestHeader &header, CcnxFibEntryContainer::type::iterator fibEntry, Ptr<CcnxFace> face);*/ |
| 122 | |
| 123 | bool |
| 124 | TryAddOutgoing(CcnxPitEntryContainer::type::iterator pitEntry, Ptr<CcnxFace> face); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 125 | /** |
| 126 | * \brief Find corresponding PIT entry for the given content name |
| 127 | * \param prefix Prefix for which to lookup the entry |
| 128 | * \returns const reference to Pit entry. If record not found, |
| 129 | * CcnxPitEntryNotFound exception will be thrown |
| 130 | */ |
| 131 | const CcnxPitEntry& |
| 132 | Lookup (const CcnxContentObjectHeader &header) const; |
| 133 | |
| 134 | /** |
| 135 | * \brief Find corresponding PIT entry for the given content name |
| 136 | * \param prefix Prefix for which to lookup the entry |
| 137 | * \returns const reference to Pit entry. If record does not exist, it will be created |
| 138 | */ |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 139 | CcnxPitEntryContainer::type::iterator |
| 140 | Lookup (const CcnxInterestHeader &header,CcnxFibEntryContainer::type::iterator &outFibEntry); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 141 | |
| 142 | // remove a PIT entry |
| 143 | //void erase (const string &contentName); |
| 144 | |
| 145 | // Reset pending state in outgoing interests |
| 146 | // void resetPendingState( PitEntry &pe ); |
| 147 | |
| 148 | // // Check if there are any interfaces that we haven't sent data to yet |
| 149 | // bool areFreeInterfaces( PitEntry &pe, int interface ); |
| 150 | |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 151 | // Periodically generate pre-calculated number of tokens (leak buckets) |
| 152 | void LeakBuckets( ); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 153 | |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 154 | // Selectively leak a bucket |
| 155 | void LeakBucket (Ptr<CcnxFace> face, int amount); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * \brief Set cleanup timeout |
| 159 | * |
| 160 | * Side effect: current clean up even (if any) will be cancelled and a new event started |
| 161 | * |
| 162 | * \param timeout cleanup timeout |
| 163 | */ |
| 164 | void SetCleanupTimeout (const Time &timeout); |
| 165 | |
| 166 | /** |
| 167 | * \brief Get cleanup timeout |
| 168 | * |
| 169 | * \returns cleanup timeout |
| 170 | */ |
| 171 | Time GetCleanupTimeout () const; |
| 172 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 173 | /** |
| 174 | * \brief Set FIB table |
| 175 | */ |
| 176 | void SetFib (Ptr<CcnxFib> fib); |
| 177 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 178 | public: |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 179 | PitBucket maxBucketsPerFace; // maximum number of buckets. Automatically computed based on link capacity |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 180 | // // averaging over 1 second (bandwidth * 1second) |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 181 | PitBucket leakSize; // size of a periodic bucket leak |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 182 | |
| 183 | private: |
| 184 | /** \brief Remove expired records from PIT */ |
| 185 | void CleanExpired (); |
| 186 | |
| 187 | friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib); |
| 188 | |
| 189 | private: |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 190 | Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working |
| 191 | EventId m_cleanupEvent; ///< \brief Cleanup event |
| 192 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 193 | Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table |
Ilya Moiseenko | 1c73cb5 | 2011-10-28 13:13:11 -0700 | [diff] [blame] | 194 | PitBucket m_bucketsPerFace; ///< \brief pending interface counter per face |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /////////////////////////////////////////////////////////////////////////////// |
| 198 | /////////////////////////////////////////////////////////////////////////////// |
| 199 | |
| 200 | std::ostream& operator<< (std::ostream& os, const CcnxPit &fib); |
| 201 | std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry); |
| 202 | // std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric); |
| 203 | |
| 204 | class CcnxPitEntryNotFound {}; |
| 205 | |
| 206 | } // namespace ns3 |
| 207 | |
| 208 | #endif /* CCNX_PIT_H */ |