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