Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -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 | |
| 21 | #include "stats-based-randomized-interest-accept.h" |
| 22 | |
| 23 | #include "ns3/ndn-interest-header.h" |
| 24 | #include "ns3/ndn-content-object-header.h" |
| 25 | #include "ns3/ndn-pit.h" |
| 26 | #include "ns3/ndn-pit-entry.h" |
| 27 | |
| 28 | #include "ns3/assert.h" |
| 29 | #include "ns3/log.h" |
| 30 | #include "ns3/simulator.h" |
| 31 | #include "ns3/random-variable.h" |
| 32 | #include "ns3/double.h" |
| 33 | |
| 34 | #include <boost/foreach.hpp> |
| 35 | #include <boost/lambda/lambda.hpp> |
| 36 | #include <boost/lambda/bind.hpp> |
| 37 | namespace ll = boost::lambda; |
| 38 | |
| 39 | NS_LOG_COMPONENT_DEFINE ("ndn.fw.StatsBasedRandomizedInterestAccept"); |
| 40 | |
| 41 | namespace ns3 { |
| 42 | namespace ndn { |
| 43 | namespace fw { |
| 44 | |
| 45 | NS_OBJECT_ENSURE_REGISTERED (StatsBasedRandomizedInterestAccept); |
| 46 | |
| 47 | TypeId |
| 48 | StatsBasedRandomizedInterestAccept::GetTypeId (void) |
| 49 | { |
| 50 | static TypeId tid = TypeId ("ns3::ndn::fw::StatsBasedRandomizedInterestAccept") |
| 51 | .SetGroupName ("Ndn") |
| 52 | .SetParent <super> () |
| 53 | .AddConstructor <StatsBasedRandomizedInterestAccept> () |
| 54 | |
| 55 | .AddAttribute ("Threshold", "Minimum number of incoming interests to enable dropping decision", |
| 56 | DoubleValue (0.25), |
| 57 | MakeDoubleAccessor (&StatsBasedRandomizedInterestAccept::m_threshold), |
| 58 | MakeDoubleChecker<double> ()) |
| 59 | |
| 60 | .AddAttribute ("GraceAcceptProbability", "Probability to accept Interest even though stats telling that satisfaction ratio is 0", |
| 61 | DoubleValue (0.01), |
| 62 | MakeDoubleAccessor (&StatsBasedRandomizedInterestAccept::m_graceAcceptProbability), |
| 63 | MakeDoubleChecker<double> ()) |
| 64 | ; |
| 65 | return tid; |
| 66 | } |
| 67 | |
| 68 | StatsBasedRandomizedInterestAccept::StatsBasedRandomizedInterestAccept () |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | StatsBasedRandomizedInterestAccept::DoDispose () |
| 74 | { |
| 75 | super::DoDispose (); |
| 76 | } |
| 77 | |
| 78 | bool |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 79 | StatsBasedRandomizedInterestAccept::TrySendOutInterest (Ptr<Face> inFace, |
| 80 | Ptr<Face> outFace, |
| 81 | Ptr<const InterestHeader> header, |
| 82 | Ptr<const Packet> origPacket, |
| 83 | Ptr<pit::Entry> pitEntry) |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 84 | { |
| 85 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 86 | // override all (if any) parent processing |
| 87 | |
| 88 | pit::Entry::out_iterator outgoing = |
| 89 | pitEntry->GetOutgoing ().find (outFace); |
| 90 | |
| 91 | if (outgoing != pitEntry->GetOutgoing ().end ()) |
| 92 | { |
| 93 | return false; |
| 94 | } |
| 95 | |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 96 | // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()[header->GetName ()].incoming ().find (inFace)->second; |
| 97 | const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second; |
| 98 | |
| 99 | if (stats.count ().GetStats ().get<0> () >= m_threshold * pitEntry->GetFibEntry ()->GetLimits ().GetMaxLimit ()) |
Alexander Afanasyev | caeade2 | 2012-09-04 16:31:12 -0700 | [diff] [blame] | 100 | { |
| 101 | double ratio = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ()); |
| 102 | if (ratio < 0) ratio = 0.5; |
| 103 | // NS_ASSERT_MSG (ratio > 0, "If count is a reasonable value, ratio cannot be negative"); |
| 104 | UniformVariable randAccept (0, 1); |
| 105 | double dice = randAccept.GetValue (); |
| 106 | |
| 107 | if (ratio < 0 || dice < ratio + m_graceAcceptProbability) |
| 108 | { |
| 109 | // ok, accepting the interests |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // boo. bad luck |
| 114 | return false; |
| 115 | } |
| 116 | } |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 117 | |
| 118 | if (pitEntry->GetFibEntry ()->GetLimits ().IsBelowLimit ()) |
| 119 | { |
| 120 | if (outFace->GetLimits ().IsBelowLimit ()) |
| 121 | { |
| 122 | pitEntry->AddOutgoing (outFace); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 123 | |
| 124 | //transmission |
| 125 | Ptr<Packet> packetToSend = origPacket->Copy (); |
| 126 | outFace->Send (packetToSend); |
| 127 | |
| 128 | DidSendOutInterest (outFace, header, origPacket, pitEntry); |
| 129 | |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | NS_LOG_DEBUG ("Face limit. Reverting back per-prefix allowance"); |
| 135 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | StatsBasedRandomizedInterestAccept::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry) |
| 144 | { |
| 145 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 146 | |
| 147 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 148 | face != pitEntry->GetOutgoing ().end (); |
| 149 | face ++) |
| 150 | { |
| 151 | face->m_face->GetLimits ().RemoveOutstanding (); |
| 152 | } |
| 153 | |
| 154 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void |
| 159 | StatsBasedRandomizedInterestAccept::WillSatisfyPendingInterest (Ptr<Face> inFace, |
| 160 | Ptr<pit::Entry> pitEntry) |
| 161 | { |
| 162 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 163 | |
| 164 | super::WillSatisfyPendingInterest (inFace, pitEntry); |
| 165 | |
| 166 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 167 | face != pitEntry->GetOutgoing ().end (); |
| 168 | face ++) |
| 169 | { |
| 170 | face->m_face->GetLimits ().RemoveOutstanding (); |
| 171 | } |
| 172 | |
| 173 | pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding (); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | } // namespace fw |
| 178 | } // namespace ndn |
| 179 | } // namespace ns3 |