Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -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> |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "per-fib-limits.h" |
| 22 | |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 23 | #include "ns3/ndn-l3-protocol.h" |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 24 | #include "ns3/ndn-interest-header.h" |
| 25 | #include "ns3/ndn-content-object-header.h" |
| 26 | #include "ns3/ndn-pit.h" |
| 27 | #include "ns3/ndn-pit-entry.h" |
| 28 | |
| 29 | #include "ns3/assert.h" |
| 30 | #include "ns3/log.h" |
| 31 | #include "ns3/simulator.h" |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 32 | #include "ns3/random-variable.h" |
Alexander Afanasyev | ccbd834 | 2012-08-16 16:54:39 -0700 | [diff] [blame] | 33 | #include "ns3/double.h" |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 34 | |
| 35 | #include <boost/foreach.hpp> |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 36 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 37 | #include <boost/lambda/lambda.hpp> |
| 38 | #include <boost/lambda/bind.hpp> |
| 39 | namespace ll = boost::lambda; |
| 40 | |
| 41 | NS_LOG_COMPONENT_DEFINE ("ndn.fw.PerFibLimits"); |
| 42 | |
| 43 | namespace ns3 { |
| 44 | namespace ndn { |
| 45 | namespace fw { |
| 46 | |
| 47 | NS_OBJECT_ENSURE_REGISTERED (PerFibLimits); |
| 48 | |
| 49 | TypeId |
| 50 | PerFibLimits::GetTypeId (void) |
| 51 | { |
| 52 | static TypeId tid = TypeId ("ns3::ndn::fw::PerFibLimits") |
| 53 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 54 | .SetParent <super> () |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 55 | .AddConstructor <PerFibLimits> () |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 56 | |
| 57 | .AddAttribute ("AnnounceLimits", "Enable limit announcement using scope 0 interests", |
| 58 | BooleanValue (false), |
| 59 | MakeBooleanAccessor (&PerFibLimits::m_announceLimits), |
| 60 | MakeBooleanChecker ()) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 61 | ; |
| 62 | return tid; |
| 63 | } |
| 64 | |
| 65 | PerFibLimits::PerFibLimits () |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | PerFibLimits::DoDispose () |
| 71 | { |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 72 | m_announceEvent.Cancel (); |
| 73 | |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 74 | super::DoDispose (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 77 | void |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 78 | PerFibLimits::NotifyNewAggregate () |
| 79 | { |
| 80 | super::NotifyNewAggregate (); |
| 81 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 82 | if (m_announceLimits) |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 83 | { |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 84 | if (m_pit != 0 && m_fib != 0) |
| 85 | { |
| 86 | m_announceEvent = Simulator::Schedule (Seconds (1.0), |
| 87 | &PerFibLimits::AnnounceLimits, this); |
| 88 | } |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | void |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 93 | PerFibLimits::RemoveFace (Ptr<Face> face) |
| 94 | { |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 95 | for (PitQueueMap::iterator item = m_pitQueues.begin (); |
| 96 | item != m_pitQueues.end (); |
| 97 | item ++) |
| 98 | { |
| 99 | item->second.Remove (face); |
| 100 | } |
| 101 | m_pitQueues.erase (face); |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 102 | |
| 103 | super::RemoveFace (face); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 106 | void |
| 107 | PerFibLimits::OnInterest (Ptr<Face> face, |
| 108 | Ptr<const InterestHeader> header, |
| 109 | Ptr<const Packet> origPacket) |
| 110 | { |
| 111 | if (header->GetScope () != 0) |
| 112 | super::OnInterest (face, header, origPacket); |
| 113 | else |
| 114 | ApplyAnnouncedLimit (face, header); |
| 115 | } |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 117 | bool |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 118 | PerFibLimits::TrySendOutInterest (Ptr<Face> inFace, |
| 119 | Ptr<Face> outFace, |
| 120 | Ptr<const InterestHeader> header, |
| 121 | Ptr<const Packet> origPacket, |
| 122 | Ptr<pit::Entry> pitEntry) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 123 | { |
| 124 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 125 | // totally override all (if any) parent processing |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 127 | if (pitEntry->GetFwTag<PitQueueTag> () != boost::shared_ptr<PitQueueTag> ()) |
| 128 | { |
| 129 | pitEntry->UpdateLifetime (Seconds (0.10)); |
| 130 | NS_LOG_DEBUG ("Packet is still in queue and is waiting for its processing"); |
| 131 | return true; // already in the queue |
| 132 | } |
| 133 | |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 134 | if (header->GetInterestLifetime () < Seconds (0.1)) |
| 135 | { |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 136 | NS_LOG_DEBUG( "??? Why interest lifetime is so short? [" << header->GetInterestLifetime ().ToDouble (Time::S) << "s]"); |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 137 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 138 | |
| 139 | pit::Entry::out_iterator outgoing = |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 140 | pitEntry->GetOutgoing ().find (outFace); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 141 | |
| 142 | if (outgoing != pitEntry->GetOutgoing ().end ()) |
| 143 | { |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 144 | // just suppress without any other action |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 145 | return false; |
| 146 | } |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 148 | NS_LOG_DEBUG ("Limit: " << outFace->GetLimits ().m_curMaxLimit << ", outstanding: " << outFace->GetLimits ().m_outstanding); |
| 149 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 150 | if (outFace->GetLimits ().IsBelowLimit ()) |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 151 | { |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 152 | pitEntry->AddOutgoing (outFace); |
| 153 | |
| 154 | //transmission |
| 155 | Ptr<Packet> packetToSend = origPacket->Copy (); |
| 156 | outFace->Send (packetToSend); |
| 157 | |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 158 | DidSendOutInterest (outFace, header, origPacket, pitEntry); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | else |
| 162 | { |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 163 | NS_LOG_DEBUG ("Face limit for " << header->GetName ()); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 164 | } |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 166 | // hack |
| 167 | // offset lifetime, so we don't keep entries in queue for too long |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 168 | // pitEntry->OffsetLifetime (Seconds (0.010) + ); |
| 169 | // std::cerr << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) * 1000 << "ms" << std::endl; |
| 170 | pitEntry->OffsetLifetime (Seconds (-pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S))); |
| 171 | pitEntry->UpdateLifetime (Seconds (0.10)); |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 172 | |
| 173 | // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()[header->GetName ()].incoming ().find (inFace)->second; |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 174 | // const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second; |
| 175 | // double weight = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ()); |
| 176 | bool enqueued = m_pitQueues[outFace].Enqueue (inFace, pitEntry, 1); |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 178 | if (enqueued) |
| 179 | { |
| 180 | NS_LOG_DEBUG ("PIT entry is enqueued for delayed processing. Telling that we forwarding possible"); |
| 181 | return true; |
| 182 | } |
| 183 | else |
| 184 | return false; |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void |
| 188 | PerFibLimits::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry) |
| 189 | { |
| 190 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 191 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 192 | if (pitEntry->GetOutgoing ().size () == 0) |
| 193 | { |
| 194 | super::DidReceiveValidNack (0, 0, pitEntry); // semi safe |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 195 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 196 | Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ()); |
| 197 | |
| 198 | NS_ASSERT (pitEntry->GetFwTag<PitQueueTag> () != boost::shared_ptr<PitQueueTag> ()); |
| 199 | if (pitEntry->GetFwTag<PitQueueTag> ()->IsLastOneInQueues ()) |
| 200 | { |
| 201 | nackHeader->SetNack (100); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | nackHeader->SetNack (101); |
| 206 | } |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 208 | Ptr<Packet> pkt = Create<Packet> (); |
| 209 | pkt->AddHeader (*nackHeader); |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 210 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 211 | for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin (); |
| 212 | face != pitEntry->GetIncoming ().end (); |
| 213 | face ++) |
| 214 | { |
| 215 | face->m_face->Send (pkt->Copy ()); |
| 216 | } |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | // make bad stats only for "legitimately" timed out interests |
| 221 | super::WillEraseTimedOutPendingInterest (pitEntry); |
| 222 | } |
Alexander Afanasyev | 187cba2 | 2012-08-28 11:16:52 -0700 | [diff] [blame] | 223 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 224 | PitQueue::Remove (pitEntry); |
| 225 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 226 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 227 | face != pitEntry->GetOutgoing ().end (); |
| 228 | face ++) |
| 229 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 230 | face->m_face->GetLimits ().RemoveOutstanding (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 231 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 232 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 233 | ProcessFromQueue (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | |
| 237 | void |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 238 | PerFibLimits::WillSatisfyPendingInterest (Ptr<Face> inFace, |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 239 | Ptr<pit::Entry> pitEntry) |
| 240 | { |
| 241 | NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 242 | super::WillSatisfyPendingInterest (inFace, pitEntry); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 243 | |
| 244 | PitQueue::Remove (pitEntry); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 245 | |
| 246 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 247 | face != pitEntry->GetOutgoing ().end (); |
| 248 | face ++) |
| 249 | { |
Alexander Afanasyev | 6a3bb13 | 2012-08-15 09:47:35 -0700 | [diff] [blame] | 250 | face->m_face->GetLimits ().RemoveOutstanding (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 251 | } |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 252 | |
| 253 | ProcessFromQueue (); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 257 | void |
| 258 | PerFibLimits::ProcessFromQueue () |
| 259 | { |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 260 | NS_LOG_FUNCTION (this); |
| 261 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 262 | for (PitQueueMap::iterator queue = m_pitQueues.begin (); |
| 263 | queue != m_pitQueues.end (); |
| 264 | queue++) |
| 265 | { |
Alexander Afanasyev | 91e1128 | 2012-08-21 17:23:11 -0700 | [diff] [blame] | 266 | Ptr<Face> outFace = queue->first; |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 267 | |
| 268 | NS_LOG_DEBUG ("Processing " << *outFace); |
Alexander Afanasyev | 91e1128 | 2012-08-21 17:23:11 -0700 | [diff] [blame] | 269 | |
| 270 | while (!queue->second.IsEmpty () && outFace->GetLimits ().IsBelowLimit ()) |
| 271 | { |
| 272 | // now we have enqueued packet and have slot available. Send out delayed packet |
| 273 | Ptr<pit::Entry> pitEntry = queue->second.Pop (); |
Alexander Afanasyev | 187cba2 | 2012-08-28 11:16:52 -0700 | [diff] [blame] | 274 | if (pitEntry == 0) |
| 275 | { |
| 276 | outFace->GetLimits ().RemoveOutstanding (); |
| 277 | NS_LOG_DEBUG ("Though there are Interests in queue, weighted round robin decided that packet is not allowed yet"); |
| 278 | break; |
| 279 | } |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 280 | |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 281 | // hack |
| 282 | // offset lifetime back, so PIT entry wouldn't prematurely expire |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 283 | |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 284 | // std::cerr << Simulator::GetContext () << ", Lifetime before " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl; |
| 285 | pitEntry->OffsetLifetime (Seconds (-0.10) + Seconds (pitEntry->GetInterest ()->GetInterestLifetime ().ToDouble (Time::S))); |
| 286 | // std::cerr << Simulator::GetContext () << ", Lifetime after " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl; |
| 287 | |
Alexander Afanasyev | 91e1128 | 2012-08-21 17:23:11 -0700 | [diff] [blame] | 288 | pitEntry->AddOutgoing (outFace); |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 289 | |
Alexander Afanasyev | 91e1128 | 2012-08-21 17:23:11 -0700 | [diff] [blame] | 290 | Ptr<Packet> packetToSend = Create<Packet> (); |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 291 | Ptr<InterestHeader> header = Create<InterestHeader> (*pitEntry->GetInterest ()); |
| 292 | NS_LOG_DEBUG ("Adjust interest lifetime to " << pitEntry->GetExpireTime () - Simulator::Now () << "s"); |
| 293 | // header->SetInterestLifetime ( |
| 294 | // // header->GetInterestLifetime () - () |
| 295 | // pitEntry->GetExpireTime () - Simulator::Now () |
| 296 | // ); |
| 297 | // std::cerr << "New lifetime: " << (pitEntry->GetExpireTime () - Simulator::Now ()).ToDouble (Time::S) << "s" << std::endl; |
| 298 | packetToSend->AddHeader (*header); |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 299 | |
| 300 | NS_LOG_DEBUG ("Delayed sending for " << pitEntry->GetPrefix ()); |
Alexander Afanasyev | 91e1128 | 2012-08-21 17:23:11 -0700 | [diff] [blame] | 301 | outFace->Send (packetToSend); |
| 302 | DidSendOutInterest (outFace, pitEntry->GetInterest (), packetToSend, pitEntry); |
| 303 | } |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 307 | void |
| 308 | PerFibLimits::DidReceiveValidNack (Ptr<Face> inFace, |
| 309 | uint32_t nackCode, |
| 310 | Ptr<pit::Entry> pitEntry) |
| 311 | { |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 312 | super::DidReceiveValidNack (inFace, nackCode, pitEntry); // will reset count stats |
Alexander Afanasyev | 187cba2 | 2012-08-28 11:16:52 -0700 | [diff] [blame] | 313 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 314 | // NS_LOG_FUNCTION (this << pitEntry->GetPrefix ()); |
| 315 | PitQueue::Remove (pitEntry); |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 316 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 317 | Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*pitEntry->GetInterest ()); |
| 318 | nackHeader->SetNack (100); |
| 319 | Ptr<Packet> pkt = Create<Packet> (); |
| 320 | pkt->AddHeader (*nackHeader); |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 321 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 322 | for (pit::Entry::in_container::iterator face = pitEntry->GetIncoming ().begin (); |
| 323 | face != pitEntry->GetIncoming ().end (); |
| 324 | face ++) |
| 325 | { |
| 326 | face->m_face->Send (pkt->Copy ()); |
| 327 | } |
Alexander Afanasyev | 98c16e0 | 2012-08-28 00:02:02 -0700 | [diff] [blame] | 328 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 329 | for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin (); |
| 330 | face != pitEntry->GetOutgoing ().end (); |
| 331 | face ++) |
| 332 | { |
| 333 | face->m_face->GetLimits ().RemoveOutstanding (); |
| 334 | } |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 335 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 336 | m_pit->MarkErased (pitEntry); |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 337 | |
Alexander Afanasyev | c771961 | 2012-08-30 17:42:20 -0700 | [diff] [blame^] | 338 | ProcessFromQueue (); |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
| 342 | PerFibLimits::AnnounceLimits () |
| 343 | { |
| 344 | Ptr<L3Protocol> l3 = GetObject<L3Protocol> (); |
| 345 | NS_ASSERT (l3 != 0); |
| 346 | |
| 347 | if (l3->GetNFaces () < 2) |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 348 | { |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 349 | m_announceEvent = Simulator::Schedule (Seconds (1.0), |
| 350 | &PerFibLimits::AnnounceLimits, this); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | double sumOfWeights = 0; |
| 355 | double weightNormalization = 1.0; |
| 356 | for (uint32_t faceId = 0; faceId < l3->GetNFaces (); faceId ++) |
| 357 | { |
| 358 | Ptr<Face> inFace = l3->GetFace (faceId); |
| 359 | |
| 360 | const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second; |
| 361 | double weight = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ()); |
| 362 | if (weight < 0) weight = 0.5; |
| 363 | |
| 364 | sumOfWeights += weight; |
| 365 | } |
| 366 | if (sumOfWeights >= 1) |
| 367 | { |
| 368 | // disable normalization (not necessary) |
| 369 | weightNormalization = 1.0; |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | // sumOfWeights /= (l3->GetNFaces ()); |
| 374 | weightNormalization = 1 / sumOfWeights; |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame] | 375 | } |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 376 | |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 377 | for (Ptr<fib::Entry> entry = m_fib->Begin (); |
| 378 | entry != m_fib->End (); |
| 379 | entry = m_fib->Next (entry)) |
| 380 | { |
| 381 | InterestHeader announceInterest; |
| 382 | announceInterest.SetScope (0); // link-local |
| 383 | |
| 384 | uint32_t totalAllowance = 0; |
| 385 | for (fib::FaceMetricContainer::type::iterator fibFace = entry->m_faces.begin (); |
| 386 | fibFace != entry->m_faces.end (); |
| 387 | fibFace ++) |
| 388 | { |
| 389 | totalAllowance += fibFace->m_face->GetLimits ().GetMaxLimit (); |
| 390 | } |
| 391 | |
| 392 | if (totalAllowance == 0) |
| 393 | { |
| 394 | // don't announce anything, there is no limit |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | for (uint32_t faceId = 0; faceId < l3->GetNFaces (); faceId ++) |
| 399 | { |
| 400 | Ptr<Face> inFace = l3->GetFace (faceId); |
| 401 | |
| 402 | const ndnSIM::LoadStatsFace &stats = GetStatsTree ()["/"].incoming ().find (inFace)->second; |
| 403 | double weight = std::min (1.0, stats.GetSatisfiedRatio ().get<0> ()); |
| 404 | if (weight < 0) weight = 0.5; |
| 405 | |
| 406 | Ptr<NameComponents> prefixWithLimit = Create<NameComponents> (entry->GetPrefix ()); |
| 407 | (*prefixWithLimit) |
| 408 | ("limit") |
| 409 | (static_cast<uint32_t> (std::max (1.0, weightNormalization * weight * totalAllowance))); |
| 410 | |
| 411 | announceInterest.SetName (prefixWithLimit); |
| 412 | // lifetime is 0 |
| 413 | |
| 414 | Ptr<Packet> pkt = Create<Packet> (); |
| 415 | pkt->AddHeader (announceInterest); |
| 416 | |
| 417 | inFace->Send (pkt); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | m_announceEvent = Simulator::Schedule (Seconds (1.0), |
| 422 | &PerFibLimits::AnnounceLimits, this); |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | PerFibLimits::ApplyAnnouncedLimit (Ptr<Face> inFace, |
| 427 | Ptr<const InterestHeader> header) |
| 428 | { |
| 429 | // Ptr<fib::Entry> fibEntry = m_fib->LongestPrefixMatch (header); |
| 430 | // if (fibEntry == 0) |
| 431 | // return; |
| 432 | |
| 433 | uint32_t limit = boost::lexical_cast<uint32_t> (header->GetName ().GetLastComponent ()); |
| 434 | inFace->GetLimits ().SetMaxLimit (limit); |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 435 | |
Alexander Afanasyev | b1ec249 | 2012-08-28 17:33:33 -0700 | [diff] [blame] | 436 | // if (Simulator::GetContext () == 6 || Simulator::GetContext () == 4) |
| 437 | // { |
| 438 | // std::cerr << Simulator::Now ().ToDouble (Time::S) << "s from:" << *inFace << " " << *header << std::endl; |
| 439 | // std::cerr << header->GetName ().GetLastComponent () << ", " << boost::lexical_cast<uint32_t> (header->GetName ().GetLastComponent ()) << std::endl; |
| 440 | // } |
Alexander Afanasyev | 08b7d9e | 2012-08-23 10:53:46 -0700 | [diff] [blame] | 441 | } |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 442 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 443 | |
| 444 | } // namespace fw |
| 445 | } // namespace ndn |
| 446 | } // namespace ns3 |