Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -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 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "per-fib-limits.h" |
| 23 | |
| 24 | #include "ns3/ndn-interest-header.h" |
| 25 | #include "ns3/ndn-content-object-header.h" |
| 26 | #include "ns3/ndn-pit.h" |
| 27 | #include "ns3/ndn-pit-entry.h" |
| 28 | |
| 29 | #include "ns3/assert.h" |
| 30 | #include "ns3/log.h" |
| 31 | #include "ns3/simulator.h" |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 32 | #include "ns3/random-variable.h" |
Alexander Afanasyev | ccbd834 | 2012-08-16 16:54:39 -0700 | [diff] [blame] | 33 | #include "ns3/double.h" |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 34 | |
| 35 | #include <boost/foreach.hpp> |
| 36 | #include <boost/lambda/lambda.hpp> |
| 37 | #include <boost/lambda/bind.hpp> |
| 38 | namespace ll = boost::lambda; |
| 39 | |
| 40 | NS_LOG_COMPONENT_DEFINE ("ndn.fw.PerFibLimits"); |
| 41 | |
| 42 | namespace ns3 { |
| 43 | namespace ndn { |
| 44 | namespace fw { |
| 45 | |
| 46 | NS_OBJECT_ENSURE_REGISTERED (PerFibLimits); |
| 47 | |
| 48 | TypeId |
| 49 | PerFibLimits::GetTypeId (void) |
| 50 | { |
| 51 | static TypeId tid = TypeId ("ns3::ndn::fw::PerFibLimits") |
| 52 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 53 | .SetParent <super> () |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 54 | .AddConstructor <PerFibLimits> () |
Alexander Afanasyev | ccbd834 | 2012-08-16 16:54:39 -0700 | [diff] [blame] | 55 | |
| 56 | .AddAttribute ("Threshold", "Minimum number of incoming interests to enable dropping decision", |
| 57 | DoubleValue (0.25), |
| 58 | MakeDoubleAccessor (&PerFibLimits::m_threshold), |
| 59 | MakeDoubleChecker<double> ()) |
| 60 | |
| 61 | .AddAttribute ("GraceAcceptProbability", "Probability to accept Interest even though stats telling that satisfaction ratio is 0", |
| 62 | DoubleValue (0.01), |
| 63 | MakeDoubleAccessor (&PerFibLimits::m_graceAcceptProbability), |
| 64 | MakeDoubleChecker<double> ()) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 65 | ; |
| 66 | return tid; |
| 67 | } |
| 68 | |
| 69 | PerFibLimits::PerFibLimits () |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | PerFibLimits::DoDispose () |
| 75 | { |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 76 | super::DoDispose (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 77 | m_decayLimitsEvent.Cancel (); |
| 78 | } |
| 79 | |
| 80 | bool |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 81 | PerFibLimits::WillSendOutInterest (Ptr<Face> outFace, |
| 82 | Ptr<const InterestHeader> header, |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 83 | Ptr<pit::Entry> pitEntry) |
| 84 | { |
| 85 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 86 | // override all (if any) parent processing |
| 87 | |
| 88 | pit::Entry::out_iterator outgoing = |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 89 | pitEntry->GetOutgoing ().find (outFace); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 90 | |
| 91 | if (outgoing != pitEntry->GetOutgoing ().end ()) |
| 92 | { |
| 93 | return false; |
| 94 | } |
| 95 | |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 96 | // check stats |
| 97 | Ptr<Face> inFace = pitEntry->GetIncoming ().begin ()->m_face; |
| 98 | // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()[header->GetName ()].incoming ().find (inFace)->second; |
| 99 | const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second; |
| 100 | |
Alexander Afanasyev | ccbd834 | 2012-08-16 16:54:39 -0700 | [diff] [blame] | 101 | if (stats.count ().GetStats ().get<0> () >= m_threshold * pitEntry->GetFibEntry ()->GetLimits ().GetMaxLimit ()) |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 102 | { |
| 103 | double ratio = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ()); |
| 104 | // NS_ASSERT_MSG (ratio > 0, "If count is a reasonable value, ratio cannot be negative"); |
| 105 | UniformVariable randAccept (0, 1); |
| 106 | double dice = randAccept.GetValue (); |
Alexander Afanasyev | ccbd834 | 2012-08-16 16:54:39 -0700 | [diff] [blame] | 107 | if (ratio < 0 || dice < ratio + m_graceAcceptProbability) |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 108 | { |
| 109 | // ok, accepting the interests |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // boo. bad luck |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 118 | if (pitEntry->GetFibEntry ()->GetLimits ().IsBelowLimit ()) |
| 119 | { |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 120 | if (outFace->GetLimits ().IsBelowLimit ()) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 121 | { |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 122 | pitEntry->AddOutgoing (outFace); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | else |
| 126 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 127 | NS_LOG_DEBUG ("Face limit. Reverting back per-prefix allowance"); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 128 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | PerFibLimits::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry) |
| 137 | { |
| 138 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 139 | |
| 140 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 141 | face != pitEntry->GetOutgoing ().end (); |
| 142 | face ++) |
| 143 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 144 | face->m_face->GetLimits ().RemoveOutstanding (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 145 | // face->m_face->GetLimits ()->DecreaseLimit (); !!! do not decrease per-face limit. it doesn't make sense !!! |
| 146 | } |
| 147 | |
| 148 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 149 | // pitEntry->GetFibEntry ()->GetLimits ().DecreaseLimit (); // multiplicative decrease |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 150 | |
| 151 | if (!m_decayLimitsEvent.IsRunning ()) |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 152 | { |
| 153 | UniformVariable rand (0,5); |
| 154 | m_decayLimitsEvent = Simulator::Schedule (Seconds (1.0) + Seconds (0.001 * rand.GetValue ()), &PerFibLimits::DecayLimits, this); |
| 155 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | void |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 160 | PerFibLimits::WillSatisfyPendingInterest (Ptr<Face> inFace, |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 161 | Ptr<pit::Entry> pitEntry) |
| 162 | { |
| 163 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 164 | |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 165 | super::WillSatisfyPendingInterest (inFace, pitEntry); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 166 | |
| 167 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 168 | face != pitEntry->GetOutgoing ().end (); |
| 169 | face ++) |
| 170 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 171 | face->m_face->GetLimits ().RemoveOutstanding (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 172 | // face->m_face->GetLimits ()->IncreaseLimit (); !!! do not increase (as do not decrease) per-face limit. again, it doesn't make sense |
| 173 | } |
| 174 | |
| 175 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
Alexander Afanasyev | 70426a0 | 2012-08-15 15:39:18 -0700 | [diff] [blame] | 176 | // pitEntry->GetFibEntry ()->GetLimits ().IncreaseLimit (); // additive increase |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
| 180 | // void |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 181 | // PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace, |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 182 | // uint32_t nackCode, |
| 183 | // Ptr<pit::Entry> pitEntry) |
| 184 | // { |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 185 | // // super::DidReceiveValidNack (inFace, nackCode, pitEntry); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 186 | |
| 187 | // // ?? |
| 188 | // } |
| 189 | |
| 190 | void |
| 191 | PerFibLimits::DecayLimits () |
| 192 | { |
| 193 | for (Ptr<fib::Entry> entry = m_fib->Begin (); |
| 194 | entry != m_fib->End (); |
| 195 | entry = m_fib->Next (entry)) |
| 196 | { |
| 197 | entry->GetLimits ().DecayCurrentLimit (); |
| 198 | } |
| 199 | |
| 200 | m_decayLimitsEvent = Simulator::Schedule (Seconds (1.0), &PerFibLimits::DecayLimits, this); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | } // namespace fw |
| 205 | } // namespace ndn |
| 206 | } // namespace ns3 |