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