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" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 29 | #include "ns3/names.h" |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 30 | |
| 31 | #define NDN_RTO_ALPHA 0.125 |
| 32 | #define NDN_RTO_BETA 0.25 |
| 33 | #define NDN_RTO_K 4 |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 34 | |
| 35 | //#define NDN_DEBUG_OSPF 0 |
| 36 | //#define NDN_DEBUG_OSPF_NODES 0 |
| 37 | |
| 38 | //#define NDN_DUMP_FIB 0 |
| 39 | namespace ns3 { |
| 40 | |
| 41 | |
| 42 | ////////////////////////////////////////////////////////////////////// |
| 43 | // Helpers |
| 44 | ////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 45 | namespace __ccnx_private { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 46 | |
| 47 | struct CcnxFibFaceMetricByFace |
| 48 | { |
| 49 | typedef CcnxFibFaceMetricContainer::type::index<i_face>::type |
| 50 | type; |
| 51 | }; |
| 52 | |
| 53 | struct ChangeStatus |
| 54 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 55 | ChangeStatus (CcnxFibFaceMetric::Status status) : m_status (status) { } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 56 | void operator() (CcnxFibFaceMetric &entry) |
| 57 | { |
| 58 | entry.m_status = m_status; |
| 59 | } |
| 60 | private: |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 61 | CcnxFibFaceMetric::Status m_status; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 64 | struct ChangeMetric |
| 65 | { |
| 66 | ChangeMetric (int32_t metric) : m_metric (metric) { } |
| 67 | void operator() (CcnxFibFaceMetric &entry) |
| 68 | { |
| 69 | entry.m_routingCost = m_metric; |
| 70 | } |
| 71 | private: |
| 72 | int32_t m_metric; |
| 73 | }; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 75 | // struct SearchByFace { |
| 76 | // /** |
| 77 | // * \brief To perform effective searches by CcnxFace |
| 78 | // */ |
| 79 | // bool |
| 80 | // operator() (const CcnxFibFaceMetric &m, const Ptr<CcnxFace> &face) const |
| 81 | // { |
| 82 | // return *(m.m_face) < *face; |
| 83 | // } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 85 | // /** |
| 86 | // * \brief To perform effective searches by CcnxFace |
| 87 | // */ |
| 88 | // bool |
| 89 | // operator() (const Ptr<CcnxFace> &face, const CcnxFibFaceMetric &m) const |
| 90 | // { |
| 91 | // return *face < *(m.m_face); |
| 92 | // } |
| 93 | // }; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 94 | |
| 95 | } |
| 96 | ////////////////////////////////////////////////////////////////////// |
| 97 | ////////////////////////////////////////////////////////////////////// |
| 98 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 99 | using namespace __ccnx_private; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 101 | TypeId |
| 102 | CcnxFib::GetTypeId (void) |
| 103 | { |
| 104 | static TypeId tid = TypeId ("ns3::CcnxFib") |
| 105 | .SetParent<Object> () |
| 106 | .SetGroupName ("Ccnx") |
| 107 | .AddConstructor<CcnxFib> () |
| 108 | |
| 109 | ; |
| 110 | return tid; |
| 111 | } |
| 112 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 113 | void |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 114 | CcnxFibFaceMetric::UpdateRtt::operator() (CcnxFibFaceMetric &entry) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 115 | { |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 116 | // const Time & this->m_rttSample |
| 117 | |
| 118 | //update srtt and rttvar (RFC 2988) |
| 119 | if (entry.m_sRtt.IsZero ()) |
| 120 | { |
| 121 | //first RTT measurement |
| 122 | NS_ASSERT_MSG (entry.m_rttVar.IsZero (), "SRTT is zero, but variation is not"); |
| 123 | |
| 124 | entry.m_sRtt = m_rttSample; |
| 125 | entry.m_rttVar = Time (entry.m_sRtt / 2.0); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | entry.m_rttVar = Time ((1 - NDN_RTO_BETA) * entry.m_rttVar + NDN_RTO_BETA * Abs(entry.m_sRtt - m_rttSample)); |
| 130 | entry.m_sRtt = Time ((1 - NDN_RTO_ALPHA) * entry.m_sRtt + NDN_RTO_ALPHA * m_rttSample); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 135 | CcnxFibEntry::UpdateStatus::operator () (CcnxFibEntry &entry) |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 136 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 137 | CcnxFibFaceMetricByFace::type::iterator record = entry.m_faces.get<i_face> ().find (m_face); |
| 138 | NS_ASSERT_MSG (record != entry.m_faces.get<i_face> ().end (), |
| 139 | "Update status can be performed only on existing faces of CcxnFibEntry"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 140 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 141 | entry.m_faces.modify (record, ChangeStatus (m_status)); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 142 | |
| 143 | // reordering random access index same way as by metric index |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 144 | entry.m_faces.get<i_nth> ().rearrange (entry.m_faces.get<i_metric> ().begin ()); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 147 | void |
| 148 | CcnxFibEntry::AddOrUpdateRoutingMetric::operator () (CcnxFibEntry &entry) |
| 149 | { |
| 150 | CcnxFibFaceMetricByFace::type::iterator record = entry.m_faces.get<i_face> ().find (m_face); |
| 151 | if (record == entry.m_faces.get<i_face> ().end ()) |
| 152 | { |
| 153 | entry.m_faces.insert (CcnxFibFaceMetric (m_face, m_metric)); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | entry.m_faces.modify (record, ChangeMetric (m_metric)); |
| 158 | } |
| 159 | |
| 160 | // reordering random access index same way as by metric index |
| 161 | entry.m_faces.get<i_nth> ().rearrange (entry.m_faces.get<i_metric> ().begin ()); |
| 162 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 164 | void |
| 165 | CcnxFibEntry::UpdateFaceRtt::operator() (CcnxFibEntry &entry) |
| 166 | { |
| 167 | CcnxFibFaceMetricContainer::type::iterator metric = entry.m_faces.find (m_face); |
| 168 | NS_ASSERT_MSG (metric != entry.m_faces.end (), |
| 169 | "Something wrong. Cannot find entry for the face in FIB"); |
| 170 | |
| 171 | entry.m_faces.modify (metric, CcnxFibFaceMetric::UpdateRtt (m_rttSample)); |
| 172 | } |
| 173 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 174 | Ptr<CcnxFace> |
| 175 | CcnxFibEntry::FindBestCandidate (int skip/* = 0*/) |
| 176 | { |
| 177 | skip = skip % m_faces.size(); |
| 178 | return m_faces.get<i_nth> () [skip].GetFace (); |
| 179 | } |
| 180 | |
| 181 | |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 182 | CcnxFib::CcnxFib () |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 183 | { |
| 184 | } |
| 185 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 186 | void |
| 187 | CcnxFib::NotifyNewAggregate () |
| 188 | { |
| 189 | if (m_node == 0) |
| 190 | m_node = this->GetObject<Node>(); |
| 191 | Object::NotifyNewAggregate (); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | CcnxFib::DoDispose (void) |
| 196 | { |
| 197 | m_node = 0; |
| 198 | Object::DoDispose (); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | CcnxFibEntryContainer::type::iterator |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 203 | CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const |
| 204 | { |
| 205 | const CcnxNameComponents &name = interest.GetName (); |
| 206 | for (size_t componentsCount = name.GetComponents ().size (); |
| 207 | componentsCount > 0; |
| 208 | componentsCount--) |
| 209 | { |
| 210 | CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount)); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 211 | CcnxFibEntryContainer::type::iterator match = find (subPrefix); |
| 212 | if (match != end()) |
| 213 | return match; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 216 | return end (); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | CcnxFibEntryContainer::type::iterator |
| 221 | CcnxFib::Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric) |
| 222 | { |
| 223 | // CcnxFibFaceMetric |
| 224 | CcnxFibEntryContainer::type::iterator entry = find (prefix); |
| 225 | if (entry == end ()) |
| 226 | { |
| 227 | entry = insert (end (), CcnxFibEntry (prefix)); |
| 228 | // insert new |
| 229 | } |
| 230 | modify (entry, CcnxFibEntry::AddOrUpdateRoutingMetric (face, metric)); |
| 231 | |
| 232 | return entry; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib) |
| 236 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 237 | os << "Node " << Names::FindName (fib.m_node) << "\n"; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 238 | os << " Dest prefix Interfaces(Costs) \n"; |
| 239 | os << "+-------------+--------------------------------------+\n"; |
| 240 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 241 | for (CcnxFibEntryContainer::type::iterator entry = fib.begin (); |
| 242 | entry != fib.end (); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 243 | entry++) |
| 244 | { |
| 245 | os << *entry << "\n"; |
| 246 | } |
| 247 | return os; |
| 248 | } |
| 249 | |
| 250 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry) |
| 251 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 252 | os << *entry.m_prefix << "\t"; |
| 253 | |
| 254 | for (CcnxFibFaceMetricContainer::type::index<i_nth>::type::iterator metric = |
| 255 | entry.m_faces.get<i_nth> ().begin (); |
| 256 | metric != entry.m_faces.get<i_nth> ().end (); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 257 | metric++) |
| 258 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 259 | if (metric != entry.m_faces.get<i_nth> ().begin ()) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 260 | os << ", "; |
| 261 | |
| 262 | os << *metric; |
| 263 | } |
| 264 | return os; |
| 265 | } |
| 266 | |
| 267 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric) |
| 268 | { |
| 269 | static const std::string statusString[] = {"","g","y","r"}; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 270 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 271 | os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")"; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 272 | return os; |
| 273 | } |
| 274 | |
| 275 | // void CcnxFib::resetProbing() |
| 276 | // { |
| 277 | // for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++) |
| 278 | // VALUE(fib).needsProbing = true; |
| 279 | // } |
| 280 | |
| 281 | // void CcnxFib::updateInterfaceStatus( int interface, int status ) |
| 282 | // { |
| 283 | // for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ ) |
| 284 | // { |
| 285 | // VALUE(fib).updateStatus( interface, status ); |
| 286 | // } |
| 287 | // } |
| 288 | |
| 289 | } // namespace ns3 |