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