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 | |
| 41 | using namespace boost::lambda; |
| 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 | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 103 | CcnxFibEntry::UpdateStatus (const CcnxFace &face, CcnxFibFaceMetric::Status status) |
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 | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 109 | m_faces.modify (record, _1->m_status = status); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 110 | |
| 111 | // reordering random access index same way as by metric index |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 112 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 115 | // void |
| 116 | // CcnxFibEntry::AddOrUpdateRoutingMetric (Ptr<CcnxFace> face, int32_t metric) |
| 117 | // { |
| 118 | // NS_LOG_FUNCTION(this); |
| 119 | // NS_ASSERT_MSG (m_face != NULL, "Trying to Add or Update NULL face"); |
Ilya Moiseenko | 00b3048 | 2011-11-15 17:58:00 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 121 | // CcnxFibFaceMetricByFace::type::iterator record = entry.m_faces.get<i_face> ().find (m_face); |
| 122 | // if (record == entry.m_faces.get<i_face> ().end ()) |
| 123 | // { |
| 124 | // entry.m_faces.insert (CcnxFibFaceMetric (m_face, m_metric)); |
| 125 | // } |
| 126 | // else |
| 127 | // { |
| 128 | // entry.m_faces.modify (record, ChangeMetric (m_metric)); |
| 129 | // } |
| 130 | // // reordering random access index same way as by metric index |
| 131 | // entry.m_faces.get<i_nth> ().rearrange (entry.m_faces.get<i_metric> ().begin ()); |
| 132 | // } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 133 | |
| 134 | Ptr<CcnxFace> |
Ilya Moiseenko | 4e47348 | 2011-10-31 17:58:14 -0700 | [diff] [blame] | 135 | CcnxFibEntry::FindBestCandidate (int skip/* = 0*/) const |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 136 | { |
| 137 | skip = skip % m_faces.size(); |
| 138 | return m_faces.get<i_nth> () [skip].GetFace (); |
| 139 | } |
| 140 | |
| 141 | |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 142 | CcnxFib::CcnxFib () |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 143 | { |
| 144 | } |
| 145 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 146 | void |
| 147 | CcnxFib::NotifyNewAggregate () |
| 148 | { |
| 149 | if (m_node == 0) |
| 150 | m_node = this->GetObject<Node>(); |
| 151 | Object::NotifyNewAggregate (); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | CcnxFib::DoDispose (void) |
| 156 | { |
Alexander Afanasyev | 4fa5e84 | 2011-11-21 13:38:39 -0800 | [diff] [blame] | 157 | clear (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 158 | m_node = 0; |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 159 | clear (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 160 | Object::DoDispose (); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | CcnxFibEntryContainer::type::iterator |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 165 | CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const |
| 166 | { |
| 167 | const CcnxNameComponents &name = interest.GetName (); |
| 168 | for (size_t componentsCount = name.GetComponents ().size (); |
| 169 | componentsCount > 0; |
| 170 | componentsCount--) |
| 171 | { |
| 172 | CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount)); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 173 | CcnxFibEntryContainer::type::iterator match = find (subPrefix); |
| 174 | if (match != end()) |
| 175 | return match; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 178 | return end (); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | CcnxFibEntryContainer::type::iterator |
| 183 | CcnxFib::Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric) |
| 184 | { |
| 185 | // CcnxFibFaceMetric |
Ilya Moiseenko | 00b3048 | 2011-11-15 17:58:00 -0800 | [diff] [blame] | 186 | NS_LOG_FUNCTION(this << prefix << face << metric); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 187 | CcnxFibEntryContainer::type::iterator entry = find (prefix); |
| 188 | if (entry == end ()) |
| 189 | { |
| 190 | entry = insert (end (), CcnxFibEntry (prefix)); |
| 191 | // insert new |
| 192 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 193 | |
Ilya Moiseenko | 00b3048 | 2011-11-15 17:58:00 -0800 | [diff] [blame] | 194 | NS_ASSERT_MSG (face != NULL, "Trying to modify NULL face"); |
| 195 | modify (entry, CcnxFibEntry::AddOrUpdateRoutingMetric (face, metric)); |
| 196 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 197 | return entry; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 198 | } |
Ilya Moiseenko | 4e47348 | 2011-10-31 17:58:14 -0700 | [diff] [blame] | 199 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 200 | |
| 201 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib) |
| 202 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 203 | os << "Node " << Names::FindName (fib.m_node) << "\n"; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 204 | os << " Dest prefix Interfaces(Costs) \n"; |
| 205 | os << "+-------------+--------------------------------------+\n"; |
| 206 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 207 | for (CcnxFibEntryContainer::type::iterator entry = fib.begin (); |
| 208 | entry != fib.end (); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 209 | entry++) |
| 210 | { |
| 211 | os << *entry << "\n"; |
| 212 | } |
| 213 | return os; |
| 214 | } |
| 215 | |
| 216 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry) |
| 217 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 218 | os << *entry.m_prefix << "\t"; |
| 219 | |
| 220 | for (CcnxFibFaceMetricContainer::type::index<i_nth>::type::iterator metric = |
| 221 | entry.m_faces.get<i_nth> ().begin (); |
| 222 | metric != entry.m_faces.get<i_nth> ().end (); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 223 | metric++) |
| 224 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 225 | if (metric != entry.m_faces.get<i_nth> ().begin ()) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 226 | os << ", "; |
| 227 | |
| 228 | os << *metric; |
| 229 | } |
| 230 | return os; |
| 231 | } |
| 232 | |
| 233 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric) |
| 234 | { |
| 235 | static const std::string statusString[] = {"","g","y","r"}; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 236 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 237 | os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")"; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 238 | return os; |
| 239 | } |
| 240 | |
| 241 | // void CcnxFib::resetProbing() |
| 242 | // { |
| 243 | // for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++) |
| 244 | // VALUE(fib).needsProbing = true; |
| 245 | // } |
| 246 | |
| 247 | // void CcnxFib::updateInterfaceStatus( int interface, int status ) |
| 248 | // { |
| 249 | // for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ ) |
| 250 | // { |
| 251 | // VALUE(fib).updateStatus( interface, status ); |
| 252 | // } |
| 253 | // } |
| 254 | |
| 255 | } // namespace ns3 |