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