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