Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -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 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ndn-fib-entry.h" |
Alexander Afanasyev | ff0d9ca | 2013-04-14 23:13:46 -0700 | [diff] [blame^] | 22 | #include "ndn-fib.h" |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 24 | #include "ns3/ndn-name.h" |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 25 | #include "ns3/log.h" |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 26 | #include "ns3/simulator.h" |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 27 | |
| 28 | #define NDN_RTO_ALPHA 0.125 |
| 29 | #define NDN_RTO_BETA 0.25 |
| 30 | #define NDN_RTO_K 4 |
| 31 | |
| 32 | #include <boost/ref.hpp> |
| 33 | #include <boost/lambda/lambda.hpp> |
| 34 | #include <boost/lambda/bind.hpp> |
| 35 | namespace ll = boost::lambda; |
| 36 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | NS_LOG_COMPONENT_DEFINE ("ndn.fib.Entry"); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 39 | namespace ns3 { |
| 40 | namespace ndn { |
| 41 | namespace fib { |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 42 | |
| 43 | ////////////////////////////////////////////////////////////////////// |
| 44 | // Helpers |
| 45 | ////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 47 | struct FaceMetricByFace |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 49 | typedef FaceMetricContainer::type::index<i_face>::type |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 50 | type; |
| 51 | }; |
| 52 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 53 | |
| 54 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 55 | FaceMetric::UpdateRtt (const Time &rttSample) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 56 | { |
| 57 | // const Time & this->m_rttSample |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 59 | //update srtt and rttvar (RFC 2988) |
| 60 | if (m_sRtt.IsZero ()) |
| 61 | { |
| 62 | //first RTT measurement |
| 63 | NS_ASSERT_MSG (m_rttVar.IsZero (), "SRTT is zero, but variation is not"); |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 65 | m_sRtt = rttSample; |
| 66 | m_rttVar = Time (m_sRtt / 2.0); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | m_rttVar = Time ((1 - NDN_RTO_BETA) * m_rttVar + NDN_RTO_BETA * Abs(m_sRtt - rttSample)); |
| 71 | m_sRtt = Time ((1 - NDN_RTO_ALPHA) * m_sRtt + NDN_RTO_ALPHA * rttSample); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | ///////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 78 | Entry::UpdateFaceRtt (Ptr<Face> face, const Time &sample) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 79 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 80 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 81 | NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (), |
| 82 | "Update status can be performed only on existing faces of CcxnFibEntry"); |
| 83 | |
| 84 | m_faces.modify (record, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 85 | ll::bind (&FaceMetric::UpdateRtt, ll::_1, sample)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 86 | |
| 87 | // reordering random access index same way as by metric index |
| 88 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
| 89 | } |
| 90 | |
| 91 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 92 | Entry::UpdateStatus (Ptr<Face> face, FaceMetric::Status status) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 93 | { |
| 94 | NS_LOG_FUNCTION (this << boost::cref(*face) << status); |
| 95 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 96 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 97 | NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (), |
| 98 | "Update status can be performed only on existing faces of CcxnFibEntry"); |
| 99 | |
| 100 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 101 | ll::bind (&FaceMetric::SetStatus, ll::_1, status)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 102 | |
| 103 | // reordering random access index same way as by metric index |
| 104 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
| 105 | } |
| 106 | |
| 107 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 108 | Entry::AddOrUpdateRoutingMetric (Ptr<Face> face, int32_t metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 109 | { |
| 110 | NS_LOG_FUNCTION (this); |
| 111 | NS_ASSERT_MSG (face != NULL, "Trying to Add or Update NULL face"); |
| 112 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 113 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 114 | if (record == m_faces.get<i_face> ().end ()) |
| 115 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 116 | m_faces.insert (FaceMetric (face, metric)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
| 120 | // don't update metric to higher value |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 121 | if (record->GetRoutingCost () > metric || record->GetStatus () == FaceMetric::NDN_FIB_RED) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 122 | { |
| 123 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 124 | ll::bind (&FaceMetric::SetRoutingCost, ll::_1, metric)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 125 | |
| 126 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 127 | ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_YELLOW)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 131 | // reordering random access index same way as by metric index |
| 132 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
| 133 | } |
| 134 | |
| 135 | void |
Alexander Afanasyev | 6b0c88f | 2012-12-01 12:24:27 -0800 | [diff] [blame] | 136 | Entry::SetRealDelayToProducer (Ptr<Face> face, Time delay) |
| 137 | { |
| 138 | NS_LOG_FUNCTION (this); |
| 139 | NS_ASSERT_MSG (face != NULL, "Trying to Update NULL face"); |
| 140 | |
| 141 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
| 142 | if (record != m_faces.get<i_face> ().end ()) |
| 143 | { |
| 144 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 145 | ll::bind (&FaceMetric::SetRealDelay, ll::_1, delay)); |
Alexander Afanasyev | 6b0c88f | 2012-12-01 12:24:27 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 151 | Entry::Invalidate () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 152 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 153 | for (FaceMetricByFace::type::iterator face = m_faces.begin (); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 154 | face != m_faces.end (); |
| 155 | face++) |
| 156 | { |
| 157 | m_faces.modify (face, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 158 | ll::bind (&FaceMetric::SetRoutingCost, ll::_1, std::numeric_limits<uint16_t>::max ())); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 159 | |
| 160 | m_faces.modify (face, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 161 | ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_RED)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 165 | const FaceMetric & |
| 166 | Entry::FindBestCandidate (uint32_t skip/* = 0*/) const |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 167 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 168 | if (m_faces.size () == 0) throw Entry::NoFaces (); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 169 | skip = skip % m_faces.size(); |
| 170 | return m_faces.get<i_nth> () [skip]; |
| 171 | } |
| 172 | |
Alexander Afanasyev | ff0d9ca | 2013-04-14 23:13:46 -0700 | [diff] [blame^] | 173 | Ptr<Fib> |
| 174 | Entry::GetFib () |
| 175 | { |
| 176 | return m_fib; |
| 177 | } |
| 178 | |
| 179 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 180 | std::ostream& operator<< (std::ostream& os, const Entry &entry) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 181 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 182 | for (FaceMetricContainer::type::index<i_nth>::type::iterator metric = |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 183 | entry.m_faces.get<i_nth> ().begin (); |
| 184 | metric != entry.m_faces.get<i_nth> ().end (); |
| 185 | metric++) |
| 186 | { |
| 187 | if (metric != entry.m_faces.get<i_nth> ().begin ()) |
| 188 | os << ", "; |
| 189 | |
| 190 | os << *metric; |
| 191 | } |
| 192 | return os; |
| 193 | } |
| 194 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 195 | std::ostream& operator<< (std::ostream& os, const FaceMetric &metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 196 | { |
| 197 | static const std::string statusString[] = {"","g","y","r"}; |
| 198 | |
| 199 | os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << "," << metric.m_face->GetMetric () << ")"; |
| 200 | return os; |
| 201 | } |
| 202 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 203 | } // namespace fib |
| 204 | } // namespace ndn |
| 205 | } // namespace ns3 |