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