Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -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 | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 21 | #include "ndn-limits-rate.hpp" |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 22 | |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/simulator.h" |
| 25 | #include "ns3/random-variable.h" |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 26 | #include "ns3/ndn-face.hpp" |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 27 | #include "ns3/node.h" |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 29 | NS_LOG_COMPONENT_DEFINE("ndn.Limits.Rate"); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 34 | NS_OBJECT_ENSURE_REGISTERED(LimitsRate); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 35 | |
| 36 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 37 | LimitsRate::GetTypeId() |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 38 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 39 | static TypeId tid = |
| 40 | TypeId("ns3::ndn::Limits::Rate") |
| 41 | .SetGroupName("Ndn") |
| 42 | .SetParent<Limits>() |
| 43 | .AddConstructor<LimitsRate>() |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | .AddAttribute("RandomizeLeak", "Randomize start time for token bucket leakage. May be " |
| 46 | "helpful to prevent leak synchronizations", |
| 47 | TimeValue(Seconds(0.001)), |
| 48 | MakeTimeAccessor(&LimitsRate::m_leakRandomizationInteral), MakeTimeChecker()) |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 50 | ; |
| 51 | return tid; |
| 52 | } |
| 53 | |
| 54 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 55 | LimitsRate::NotifyNewAggregate() |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 56 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | super::NotifyNewAggregate(); |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | if (!m_isLeakScheduled) { |
| 60 | if (GetObject<Face>() != 0) { |
| 61 | NS_ASSERT_MSG(GetObject<Face>()->GetNode() != 0, "Node object should exist on the face"); |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 63 | m_isLeakScheduled = true; |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | if (!m_leakRandomizationInteral.IsZero()) { |
| 66 | UniformVariable r(0.0, m_leakRandomizationInteral.ToDouble(Time::S)); |
| 67 | Simulator::ScheduleWithContext(GetObject<Face>()->GetNode()->GetId(), Seconds(r.GetValue()), |
| 68 | &LimitsRate::LeakBucket, this, 0.0); |
| 69 | } |
| 70 | else { |
| 71 | Simulator::ScheduleWithContext(GetObject<Face>()->GetNode()->GetId(), Seconds(0), |
| 72 | &LimitsRate::LeakBucket, this, 0.0); |
| 73 | } |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 74 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 78 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 79 | LimitsRate::SetLimits(double rate, double delay) |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 81 | super::SetLimits(rate, delay); |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 82 | |
| 83 | // maximum allowed burst |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 84 | m_bucketMax = GetMaxRate() * GetMaxDelay(); |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 85 | |
| 86 | // amount of packets allowed every second (leak rate) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 87 | m_bucketLeak = GetMaxRate(); |
Alexander Afanasyev | ccd5bb4 | 2012-10-31 17:27:49 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 90 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 91 | LimitsRate::UpdateCurrentLimit(double limit) |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 92 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 93 | NS_ASSERT_MSG(limit >= 0.0, "Limit should be greater or equal to zero"); |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | m_bucketLeak = std::min(limit, GetMaxRate()); |
| 96 | m_bucketMax = m_bucketLeak * GetMaxDelay(); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | LimitsRate::IsBelowLimit() |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 101 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | if (!IsEnabled()) |
| 103 | return true; |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 104 | |
| 105 | return (m_bucketMax - m_bucket >= 1.0); |
| 106 | } |
| 107 | |
| 108 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 109 | LimitsRate::BorrowLimit() |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 110 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 111 | if (!IsEnabled()) |
| 112 | return; |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 114 | NS_ASSERT_MSG(m_bucketMax - m_bucket >= 1.0, |
| 115 | "Should not be possible, unless we IsBelowLimit was not checked correctly"); |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 116 | m_bucket += 1; |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 120 | LimitsRate::ReturnLimit() |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 121 | { |
| 122 | // do nothing |
| 123 | } |
| 124 | |
| 125 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | LimitsRate::LeakBucket(double interval) |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 127 | { |
| 128 | const double leak = m_bucketLeak * interval; |
| 129 | |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 130 | #ifdef NS3_LOG_ENABLE |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 131 | if (m_bucket > 1) { |
| 132 | NS_LOG_DEBUG("Leak from " << m_bucket << " to " << std::max(0.0, m_bucket - leak)); |
| 133 | } |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 134 | #endif |
Alexander Afanasyev | 08ab572 | 2012-11-06 17:22:25 -0800 | [diff] [blame] | 135 | |
| 136 | double bucketOld = m_bucket; |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 138 | m_bucket = std::max(0.0, m_bucket - leak); |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 139 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | // calculate interval so next time we will leak by 1.001, unless such interval would be more than |
| 141 | // 1 second |
Alexander Afanasyev | 7e4235a | 2012-10-31 16:58:44 -0700 | [diff] [blame] | 142 | double newInterval = 1.0; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 143 | if (m_bucketLeak > 1.0) { |
| 144 | newInterval = 1.001 / m_bucketLeak; |
| 145 | } |
Alexander Afanasyev | 08ab572 | 2012-11-06 17:22:25 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | if (m_bucketMax - bucketOld < 1.0 |
| 148 | && m_bucketMax - m_bucket >= 1.0) // limit number of times this stuff is called |
| 149 | { |
| 150 | this->FireAvailableSlotCallback(); |
| 151 | } |
Alexander Afanasyev | dc115ef | 2013-02-13 12:06:55 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 153 | Simulator::Schedule(Seconds(newInterval), &LimitsRate::LeakBucket, this, newInterval); |
Alexander Afanasyev | 6f95e70 | 2012-10-31 16:27:31 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace ndn |
| 157 | } // namespace ns3 |