blob: c12461287c23046929b05010360fd2b89cb72213 [file] [log] [blame]
Alexander Afanasyev31cb4692012-08-17 13:08:20 -07001/* -*- 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>
37namespace ll = boost::lambda;
38
39NS_LOG_COMPONENT_DEFINE ("ndn.fw.StatsBasedRandomizedInterestAccept");
40
41namespace ns3 {
42namespace ndn {
43namespace fw {
44
45NS_OBJECT_ENSURE_REGISTERED (StatsBasedRandomizedInterestAccept);
46
47TypeId
48StatsBasedRandomizedInterestAccept::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
68StatsBasedRandomizedInterestAccept::StatsBasedRandomizedInterestAccept ()
69{
70}
71
72void
73StatsBasedRandomizedInterestAccept::DoDispose ()
74{
75 super::DoDispose ();
76}
77
78bool
Alexander Afanasyev5db92172012-08-21 16:52:07 -070079StatsBasedRandomizedInterestAccept::TrySendOutInterest (Ptr<Face> inFace,
80 Ptr<Face> outFace,
81 Ptr<const InterestHeader> header,
82 Ptr<const Packet> origPacket,
83 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070084{
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 Afanasyev31cb4692012-08-17 13:08:20 -070096 // 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 Afanasyevcaeade22012-09-04 16:31:12 -0700100 {
101 double ratio = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ());
Alexander Afanasyev8db3cba2012-09-11 16:33:44 -0700102 // if (ratio < 0) ratio = 0.5;
Alexander Afanasyevcaeade22012-09-04 16:31:12 -0700103 // 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 Afanasyev31cb4692012-08-17 13:08:20 -0700117
118 if (pitEntry->GetFibEntry ()->GetLimits ().IsBelowLimit ())
119 {
120 if (outFace->GetLimits ().IsBelowLimit ())
121 {
122 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700123
124 //transmission
125 Ptr<Packet> packetToSend = origPacket->Copy ();
126 outFace->Send (packetToSend);
127
128 DidSendOutInterest (outFace, header, origPacket, pitEntry);
129
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700130 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
142void
143StatsBasedRandomizedInterestAccept::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
144{
145 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev8db3cba2012-09-11 16:33:44 -0700146 super::WillEraseTimedOutPendingInterest (pitEntry);
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700147
148 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
149 face != pitEntry->GetOutgoing ().end ();
150 face ++)
151 {
152 face->m_face->GetLimits ().RemoveOutstanding ();
153 }
154
155 pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
156}
157
158
159void
160StatsBasedRandomizedInterestAccept::WillSatisfyPendingInterest (Ptr<Face> inFace,
161 Ptr<pit::Entry> pitEntry)
162{
163 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
164
165 super::WillSatisfyPendingInterest (inFace, pitEntry);
166
167 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
168 face != pitEntry->GetOutgoing ().end ();
169 face ++)
170 {
171 face->m_face->GetLimits ().RemoveOutstanding ();
172 }
173
174 pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
175}
176
177
178} // namespace fw
179} // namespace ndn
180} // namespace ns3