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