blob: a1598013dd4e16c1b8c1c075ea71823a4b1fb746 [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 ())
100 {
101 double ratio = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ());
102 // NS_ASSERT_MSG (ratio > 0, "If count is a reasonable value, ratio cannot be negative");
103 UniformVariable randAccept (0, 1);
104 double dice = randAccept.GetValue ();
105 if (ratio < 0 || dice < ratio + m_graceAcceptProbability)
106 {
107 // ok, accepting the interests
108 }
109 else
110 {
111 // boo. bad luck
112 return false;
113 }
114 }
115
116 if (pitEntry->GetFibEntry ()->GetLimits ().IsBelowLimit ())
117 {
118 if (outFace->GetLimits ().IsBelowLimit ())
119 {
120 pitEntry->AddOutgoing (outFace);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700121
122 //transmission
123 Ptr<Packet> packetToSend = origPacket->Copy ();
124 outFace->Send (packetToSend);
125
126 DidSendOutInterest (outFace, header, origPacket, pitEntry);
127
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700128 return true;
129 }
130 else
131 {
132 NS_LOG_DEBUG ("Face limit. Reverting back per-prefix allowance");
133 pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
134 }
135 }
136
137 return false;
138}
139
140void
141StatsBasedRandomizedInterestAccept::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
142{
143 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
144
145 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
146 face != pitEntry->GetOutgoing ().end ();
147 face ++)
148 {
149 face->m_face->GetLimits ().RemoveOutstanding ();
150 }
151
152 pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
153}
154
155
156void
157StatsBasedRandomizedInterestAccept::WillSatisfyPendingInterest (Ptr<Face> inFace,
158 Ptr<pit::Entry> pitEntry)
159{
160 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
161
162 super::WillSatisfyPendingInterest (inFace, pitEntry);
163
164 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
165 face != pitEntry->GetOutgoing ().end ();
166 face ++)
167 {
168 face->m_face->GetLimits ().RemoveOutstanding ();
169 }
170
171 pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
172}
173
174
175} // namespace fw
176} // namespace ndn
177} // namespace ns3