Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -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-fib.h" |
| 22 | |
| 23 | #include "ccnx.h" |
| 24 | #include "ccnx-face.h" |
| 25 | #include "ccnx-interest-header.h" |
| 26 | |
| 27 | #include "ns3/node.h" |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 28 | #include "ns3/assert.h" |
| 29 | |
| 30 | #define NDN_RTO_ALPHA 0.125 |
| 31 | #define NDN_RTO_BETA 0.25 |
| 32 | #define NDN_RTO_K 4 |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 33 | |
| 34 | //#define NDN_DEBUG_OSPF 0 |
| 35 | //#define NDN_DEBUG_OSPF_NODES 0 |
| 36 | |
| 37 | //#define NDN_DUMP_FIB 0 |
| 38 | namespace ns3 { |
| 39 | |
| 40 | |
| 41 | ////////////////////////////////////////////////////////////////////// |
| 42 | // Helpers |
| 43 | ////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 44 | namespace __ccnx_private { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 45 | |
| 46 | struct CcnxFibFaceMetricByFace |
| 47 | { |
| 48 | typedef CcnxFibFaceMetricContainer::type::index<i_face>::type |
| 49 | type; |
| 50 | }; |
| 51 | |
| 52 | struct ChangeStatus |
| 53 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 54 | ChangeStatus (CcnxFibFaceMetric::Status status) : m_status (status) { } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 55 | void operator() (CcnxFibFaceMetric &entry) |
| 56 | { |
| 57 | entry.m_status = m_status; |
| 58 | } |
| 59 | private: |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 60 | CcnxFibFaceMetric::Status m_status; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 64 | // struct SearchByFace { |
| 65 | // /** |
| 66 | // * \brief To perform effective searches by CcnxFace |
| 67 | // */ |
| 68 | // bool |
| 69 | // operator() (const CcnxFibFaceMetric &m, const Ptr<CcnxFace> &face) const |
| 70 | // { |
| 71 | // return *(m.m_face) < *face; |
| 72 | // } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 74 | // /** |
| 75 | // * \brief To perform effective searches by CcnxFace |
| 76 | // */ |
| 77 | // bool |
| 78 | // operator() (const Ptr<CcnxFace> &face, const CcnxFibFaceMetric &m) const |
| 79 | // { |
| 80 | // return *face < *(m.m_face); |
| 81 | // } |
| 82 | // }; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 83 | |
| 84 | } |
| 85 | ////////////////////////////////////////////////////////////////////// |
| 86 | ////////////////////////////////////////////////////////////////////// |
| 87 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 88 | using namespace __ccnx_private; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame^] | 90 | TypeId |
| 91 | CcnxFib::GetTypeId (void) |
| 92 | { |
| 93 | static TypeId tid = TypeId ("ns3::CcnxFib") |
| 94 | .SetParent<Object> () |
| 95 | .SetGroupName ("Ccnx") |
| 96 | .AddConstructor<CcnxFib> () |
| 97 | |
| 98 | ; |
| 99 | return tid; |
| 100 | } |
| 101 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 102 | void |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 103 | CcnxFibFaceMetric::UpdateRtt::operator() (CcnxFibFaceMetric &entry) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 104 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 105 | // const Time & this->m_rttSample |
| 106 | |
| 107 | //update srtt and rttvar (RFC 2988) |
| 108 | if (entry.m_sRtt.IsZero ()) |
| 109 | { |
| 110 | //first RTT measurement |
| 111 | NS_ASSERT_MSG (entry.m_rttVar.IsZero (), "SRTT is zero, but variation is not"); |
| 112 | |
| 113 | entry.m_sRtt = m_rttSample; |
| 114 | entry.m_rttVar = Time (entry.m_sRtt / 2.0); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | entry.m_rttVar = Time ((1 - NDN_RTO_BETA) * entry.m_rttVar + NDN_RTO_BETA * Abs(entry.m_sRtt - m_rttSample)); |
| 119 | entry.m_sRtt = Time ((1 - NDN_RTO_ALPHA) * entry.m_sRtt + NDN_RTO_ALPHA * m_rttSample); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | CcnxFibEntry::UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status) |
| 125 | { |
| 126 | CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 127 | NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (), "Update status can be performed only on existing faces of CcxnFibEntry"); |
| 128 | |
| 129 | m_faces.modify (record, ChangeStatus (status)); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 130 | |
| 131 | // reordering random access index same way as by metric index |
| 132 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 135 | |
| 136 | Ptr<CcnxFace> |
| 137 | CcnxFibEntry::FindBestCandidate (int skip/* = 0*/) |
| 138 | { |
| 139 | skip = skip % m_faces.size(); |
| 140 | return m_faces.get<i_nth> () [skip].GetFace (); |
| 141 | } |
| 142 | |
| 143 | |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 144 | CcnxFib::CcnxFib () |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 145 | { |
| 146 | } |
| 147 | |
| 148 | std::pair<CcnxFibEntryContainer::type::iterator, bool> |
| 149 | CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const |
| 150 | { |
| 151 | const CcnxNameComponents &name = interest.GetName (); |
| 152 | for (size_t componentsCount = name.GetComponents ().size (); |
| 153 | componentsCount > 0; |
| 154 | componentsCount--) |
| 155 | { |
| 156 | CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount)); |
| 157 | CcnxFibEntryContainer::type::iterator match = m_fib.find (subPrefix); |
| 158 | if (match != m_fib.end()) |
| 159 | return std::pair<CcnxFibEntryContainer::type::iterator, bool> (match,true); |
| 160 | } |
| 161 | |
| 162 | return std::pair<CcnxFibEntryContainer::type::iterator, bool> (m_fib.end(),false); |
| 163 | } |
| 164 | |
| 165 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib) |
| 166 | { |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 167 | // os << "Node " << fib.m_node->GetObject<Node> ()->GetId () << "\n"; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 168 | os << " Dest prefix Interfaces(Costs) \n"; |
| 169 | os << "+-------------+--------------------------------------+\n"; |
| 170 | |
| 171 | for (CcnxFibEntryContainer::type::iterator entry = fib.m_fib.begin (); |
| 172 | entry != fib.m_fib.end (); |
| 173 | entry++) |
| 174 | { |
| 175 | os << *entry << "\n"; |
| 176 | } |
| 177 | return os; |
| 178 | } |
| 179 | |
| 180 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry) |
| 181 | { |
| 182 | os << entry.m_prefix << "\t"; |
| 183 | for (CcnxFibFaceMetricByFace::type::iterator metric = entry.m_faces.get<i_face> ().begin (); |
| 184 | metric != entry.m_faces.get<i_face> ().end (); |
| 185 | metric++) |
| 186 | { |
| 187 | if (metric != entry.m_faces.get<i_face> ().begin ()) |
| 188 | os << ", "; |
| 189 | |
| 190 | os << *metric; |
| 191 | } |
| 192 | return os; |
| 193 | } |
| 194 | |
| 195 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric) |
| 196 | { |
| 197 | static const std::string statusString[] = {"","g","y","r"}; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 198 | |
| 199 | os << metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")"; |
| 200 | return os; |
| 201 | } |
| 202 | |
| 203 | // void CcnxFib::resetProbing() |
| 204 | // { |
| 205 | // for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++) |
| 206 | // VALUE(fib).needsProbing = true; |
| 207 | // } |
| 208 | |
| 209 | // void CcnxFib::updateInterfaceStatus( int interface, int status ) |
| 210 | // { |
| 211 | // for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ ) |
| 212 | // { |
| 213 | // VALUE(fib).updateStatus( interface, status ); |
| 214 | // } |
| 215 | // } |
| 216 | |
| 217 | } // namespace ns3 |