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 | { |
Alexander Afanasyev | db31e7c | 2014-07-09 21:48:59 -0700 | [diff] [blame] | 70 | m_rttVar = Time ((1 - NDN_RTO_BETA) * m_rttVar + 1.0 * NDN_RTO_BETA * Abs(m_sRtt - rttSample)); |
| 71 | m_sRtt = Time ((1 - NDN_RTO_ALPHA) * m_sRtt + 1.0 * NDN_RTO_ALPHA * rttSample); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 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 | dc794a3 | 2013-09-26 14:34:18 -0700 | [diff] [blame] | 81 | if (record == m_faces.get<i_face> ().end ()) |
| 82 | { |
| 83 | return; |
| 84 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 85 | |
| 86 | m_faces.modify (record, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 87 | ll::bind (&FaceMetric::UpdateRtt, ll::_1, sample)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 88 | |
| 89 | // reordering random access index same way as by metric index |
| 90 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
| 91 | } |
| 92 | |
| 93 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 94 | Entry::UpdateStatus (Ptr<Face> face, FaceMetric::Status status) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 95 | { |
| 96 | NS_LOG_FUNCTION (this << boost::cref(*face) << status); |
| 97 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 98 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
Alexander Afanasyev | dc794a3 | 2013-09-26 14:34:18 -0700 | [diff] [blame] | 99 | if (record == m_faces.get<i_face> ().end ()) |
| 100 | { |
| 101 | return; |
| 102 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 103 | |
| 104 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 105 | ll::bind (&FaceMetric::SetStatus, ll::_1, status)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 106 | |
| 107 | // reordering random access index same way as by metric index |
| 108 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
| 109 | } |
| 110 | |
| 111 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 112 | Entry::AddOrUpdateRoutingMetric (Ptr<Face> face, int32_t metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 113 | { |
| 114 | NS_LOG_FUNCTION (this); |
| 115 | NS_ASSERT_MSG (face != NULL, "Trying to Add or Update NULL face"); |
| 116 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 117 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 118 | if (record == m_faces.get<i_face> ().end ()) |
| 119 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 120 | m_faces.insert (FaceMetric (face, metric)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | { |
| 124 | // don't update metric to higher value |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 125 | if (record->GetRoutingCost () > metric || record->GetStatus () == FaceMetric::NDN_FIB_RED) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 126 | { |
| 127 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 128 | ll::bind (&FaceMetric::SetRoutingCost, ll::_1, metric)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 129 | |
| 130 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 131 | ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_YELLOW)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 135 | // reordering random access index same way as by metric index |
| 136 | m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ()); |
| 137 | } |
| 138 | |
| 139 | void |
Alexander Afanasyev | 6b0c88f | 2012-12-01 12:24:27 -0800 | [diff] [blame] | 140 | Entry::SetRealDelayToProducer (Ptr<Face> face, Time delay) |
| 141 | { |
| 142 | NS_LOG_FUNCTION (this); |
| 143 | NS_ASSERT_MSG (face != NULL, "Trying to Update NULL face"); |
| 144 | |
| 145 | FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face); |
| 146 | if (record != m_faces.get<i_face> ().end ()) |
| 147 | { |
| 148 | m_faces.modify (record, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 149 | ll::bind (&FaceMetric::SetRealDelay, ll::_1, delay)); |
Alexander Afanasyev | 6b0c88f | 2012-12-01 12:24:27 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 155 | Entry::Invalidate () |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 156 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 157 | for (FaceMetricByFace::type::iterator face = m_faces.begin (); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 158 | face != m_faces.end (); |
| 159 | face++) |
| 160 | { |
| 161 | m_faces.modify (face, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 162 | ll::bind (&FaceMetric::SetRoutingCost, ll::_1, std::numeric_limits<uint16_t>::max ())); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 163 | |
| 164 | m_faces.modify (face, |
Alexander Afanasyev | 06dba7c | 2013-02-21 11:36:26 -0800 | [diff] [blame] | 165 | ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_RED)); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 169 | const FaceMetric & |
| 170 | Entry::FindBestCandidate (uint32_t skip/* = 0*/) const |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 171 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 172 | if (m_faces.size () == 0) throw Entry::NoFaces (); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 173 | skip = skip % m_faces.size(); |
| 174 | return m_faces.get<i_nth> () [skip]; |
| 175 | } |
| 176 | |
Alexander Afanasyev | ff0d9ca | 2013-04-14 23:13:46 -0700 | [diff] [blame] | 177 | Ptr<Fib> |
| 178 | Entry::GetFib () |
| 179 | { |
| 180 | return m_fib; |
| 181 | } |
| 182 | |
| 183 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 184 | std::ostream& operator<< (std::ostream& os, const Entry &entry) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 185 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 186 | for (FaceMetricContainer::type::index<i_nth>::type::iterator metric = |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 187 | entry.m_faces.get<i_nth> ().begin (); |
| 188 | metric != entry.m_faces.get<i_nth> ().end (); |
| 189 | metric++) |
| 190 | { |
| 191 | if (metric != entry.m_faces.get<i_nth> ().begin ()) |
| 192 | os << ", "; |
| 193 | |
| 194 | os << *metric; |
| 195 | } |
| 196 | return os; |
| 197 | } |
| 198 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 199 | std::ostream& operator<< (std::ostream& os, const FaceMetric &metric) |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 200 | { |
| 201 | static const std::string statusString[] = {"","g","y","r"}; |
| 202 | |
| 203 | os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << "," << metric.m_face->GetMetric () << ")"; |
| 204 | return os; |
| 205 | } |
| 206 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 207 | } // namespace fib |
| 208 | } // namespace ndn |
| 209 | } // namespace ns3 |