blob: ed724fe6f25ed393ba7fa026bbb2a7663f292426 [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -07001/* -*- 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 Afanasyevcf133f02011-09-06 12:13:48 -070024#include "ns3/object.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070025#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 Moiseenko1c73cb52011-10-28 13:13:11 -070039#include <map>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070040#include <iostream>
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070041#include <algorithm>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070042
43namespace ns3 {
44
45class Ccnx;
46class CcnxFace;
47class CcnxContentObjectHeader;
48class CcnxInterestHeader;
49
50/**
51 * \ingroup ccnx
52 * \private
53 * \brief Private namespace for CCNx PIT implementation
54 */
55namespace __ccnx_private
56{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070057// class i_prefix{}; ///< tag for prefix hash
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070058class 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 */
72struct 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 Moiseenko1c73cb52011-10-28 13:13:11 -070094 typedef std::map<int,double> PitBucket;
95 typedef std::map<int,double>::iterator PitBucketIterator;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070096
97
98////////////////////////////////////////////////////////////////////////
99////////////////////////////////////////////////////////////////////////
100
101/**
102 * \ingroup ccnx
103 * \brief Class implementing Pending Interests Table
104 */
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700105class CcnxPit : public CcnxPitEntryContainer::type, public Object
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106{
107public:
108 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700109 * \brief Interface ID
110 *
111 * \return interface ID
112 */
113 static TypeId GetTypeId ();
114
115 /**
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700116 * \brief PIT constructor
117 */
118 CcnxPit ();
119
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700120 /*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 Afanasyeva98cdd22011-08-29 17:32:37 -0700125 /**
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 Moiseenko1c73cb52011-10-28 13:13:11 -0700139 CcnxPitEntryContainer::type::iterator
140 Lookup (const CcnxInterestHeader &header,CcnxFibEntryContainer::type::iterator &outFibEntry);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700141
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 Moiseenko1c73cb52011-10-28 13:13:11 -0700151 // Periodically generate pre-calculated number of tokens (leak buckets)
152 void LeakBuckets( );
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700153
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700154 // Selectively leak a bucket
155 void LeakBucket (Ptr<CcnxFace> face, int amount);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700156
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 Afanasyevc5a23e22011-09-07 00:37:36 -0700173 /**
174 * \brief Set FIB table
175 */
176 void SetFib (Ptr<CcnxFib> fib);
177
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700178public:
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700179 PitBucket maxBucketsPerFace; // maximum number of buckets. Automatically computed based on link capacity
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700180 // // averaging over 1 second (bandwidth * 1second)
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700181 PitBucket leakSize; // size of a periodic bucket leak
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700182
183private:
184 /** \brief Remove expired records from PIT */
185 void CleanExpired ();
186
187 friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
188
189private:
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700190 Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working
191 EventId m_cleanupEvent; ///< \brief Cleanup event
192
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700193 Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700194 PitBucket m_bucketsPerFace; ///< \brief pending interface counter per face
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700195};
196
197///////////////////////////////////////////////////////////////////////////////
198///////////////////////////////////////////////////////////////////////////////
199
200std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
201std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry);
202// std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
203
204class CcnxPitEntryNotFound {};
205
206} // namespace ns3
207
208#endif /* CCNX_PIT_H */