blob: 1d4c5486e8ecb71d1aa0f2d14ffa7a0fb07ed90a [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -07002/*
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>
Ilya Moiseenko172763c2011-10-28 13:21:53 -070019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070020 */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070021
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070022#include "ccnx-l3-protocol.h"
23
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070024#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070025#include "ns3/node.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/log.h"
27#include "ns3/callback.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028#include "ns3/uinteger.h"
29#include "ns3/trace-source-accessor.h"
30#include "ns3/object-vector.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031#include "ns3/boolean.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070033#include "ns3/ccnx-header-helper.h"
34
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070035#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070036#include "ccnx-forwarding-strategy.h"
37#include "ccnx-interest-header.h"
38#include "ccnx-content-object-header.h"
39
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080040#include "ccnx-net-device-face.h"
41
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070042#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080043#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080044#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080045
46using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080047namespace ll = boost::lambda;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048
49NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
50
51namespace ns3 {
52
Alexander Afanasyev7112f482011-08-17 14:05:57 -070053const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054
55NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
56
57TypeId
58CcnxL3Protocol::GetTypeId (void)
59{
60 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
61 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070062 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080064 .AddAttribute ("BucketLeakInterval",
65 "Interval to leak buckets",
66 StringValue ("10ms"),
67 MakeTimeAccessor (&CcnxPit::GetBucketLeakInterval, &CcnxPit::SetBucketLeakInterval),
68 MakeTimeChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070069 ;
70 return tid;
71}
72
73CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070074: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070075{
76 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070077
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070078 m_pit = CreateObject<CcnxPit> ();
79 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080}
81
82CcnxL3Protocol::~CcnxL3Protocol ()
83{
84 NS_LOG_FUNCTION (this);
85}
86
87void
88CcnxL3Protocol::SetNode (Ptr<Node> node)
89{
90 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070091 m_fib = m_node->GetObject<CcnxFib> ();
92 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
93
94 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070095}
96
97/*
98 * This method is called by AddAgregate and completes the aggregation
99 * by setting the node in the ccnx stack
100 */
101void
102CcnxL3Protocol::NotifyNewAggregate ()
103{
104 if (m_node == 0)
105 {
106 Ptr<Node>node = this->GetObject<Node>();
107 // verify that it's a valid node and that
108 // the node has not been set before
109 if (node != 0)
110 {
111 this->SetNode (node);
112 }
113 }
114 Object::NotifyNewAggregate ();
115}
116
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700117void
118CcnxL3Protocol::DoDispose (void)
119{
120 NS_LOG_FUNCTION (this);
121
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800122 if (m_bucketLeakEvent.IsRunning ())
123 m_bucketLeakEvent.Cancel ();
124
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700125 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700126 {
127 *i = 0;
128 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700129 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800131
132 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800133 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800134 m_pit = 0;
135 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800136 m_fib = 0;
137
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700138 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139 Object::DoDispose ();
140}
141
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700142void
143CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
144{
145 NS_LOG_FUNCTION (this);
146 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700147 // m_forwardingStrategy->SetCcnx (this);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700148}
149
150Ptr<CcnxForwardingStrategy>
151CcnxL3Protocol::GetForwardingStrategy (void) const
152{
153 return m_forwardingStrategy;
154}
155
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700157CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700159 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700160
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700161 face->SetNode (m_node);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700162 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700164 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700165 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700167 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800168 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700169 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170}
171
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172void
173CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
174{
175 // ask face to register in lower-layer stack
176 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800177
178 // just to be on a safe side. Do the process in two steps
179 list<CcnxPitEntryContainer::type::iterator> entriesToRemoves;
180 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
181 {
182 m_pit->modify (m_pit->iterator_to (pitEntry),
183 ll::bind (CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
184
185 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
186 // Thus, we have to remove the whole PIT entry
187 if (m_pit->m_fibEntry.size () == 1 &&
188 m_pit->m_fibEntry.m_faces.begin ()->m_face == face)
189 {
190 entriesToRemoves.push_back (m_pit->iterator_to (pitEntry));
191 }
192 }
193 BOOST_FOREACH (CcnxPitEntryContainer::type::iterator entry, entriesToRemoves)
194 {
195 m_pit->erase (entry);
196 }
197
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
199 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
200 m_faces.erase (face_it);
201}
202
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700203Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700204CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700205{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700206 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700208 if (face->GetId () == index)
209 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700210 }
211 return 0;
212}
213
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800214Ptr<CcnxFace>
215CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
216{
217 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
218 {
219 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
220 if (netDeviceFace == 0) continue;
221
222 if (netDeviceFace->GetNetDevice () == netDevice)
223 return face;
224 }
225 return 0;
226}
227
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700229CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700231 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700232}
233
Alexander Afanasyev98256102011-08-14 01:00:02 -0700234// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700235void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700236CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700238 if (!face->IsUp ())
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700239 {
240 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700241 // m_dropTrace (p, INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700242 return;
243 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700244 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700246 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700247 try
248 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700250 switch (type)
251 {
252 case CcnxHeaderHelper::INTEREST:
253 {
254 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700255
256 // Deserialization. Exception may be thrown
257 packet->RemoveHeader (*header);
258 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800259
260 if (header->GetNack () > 0)
261 OnNack (face, header, p/*original packet*/);
262 else
263 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700264 break;
265 }
266 case CcnxHeaderHelper::CONTENT_OBJECT:
267 {
268 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700269
270 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
271
272 // Deserialization. Exception may be thrown
273 packet->RemoveHeader (*header);
274 packet->RemoveTrailer (contentObjectTrailer);
275
276 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700277 break;
278 }
279 }
280
281 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700282 }
283 catch (CcnxUnknownHeaderException)
284 {
285 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
286 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700287}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700288
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800289void
290CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &face,
291 Ptr<CcnxInterestHeader> &header,
292 const Ptr<const Packet> &p)
293{
294 NS_LOG_FUNCTION (face << header << p);
295
296 // Huh... Ignore all this for now
297
298 /*if( header->IsCongested () == false )
299 m_pit->LeakBucket(incomingFace,1);
300
301
302 m_droppedInterestsTrace (header, DROP_CONGESTION,
303 m_node->GetObject<Ccnx> (), incomingFace);
304
305 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));*/
306
307 // No matter is it duplicate or not, if it is a NACK message, remove all possible incoming
308 // entries for this interface (NACK means that neighbor gave up trying and there is no
309 // point of sending data in this direction)
310
311 // NS_LOG_INFO("Before (header->IsNack()) && (pitEntry != m_pit->end ())");
312 // if ((header->IsNack()) && (pitEntry != m_pit->end ()))
313 // {
314 // //m_pit->erase (pitEntry);
315 // NS_LOG_INFO("TRUE");
316 // m_pit->modify(pitEntry, CcnxPitEntry::DeleteIncoming(incomingFace));
317 // }
318
319 // m_fib->modify (m_fib->iterator_to (pitEntry->m_fibEntry),
320 // CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
321
322 // if (!pitEntry.AreThereMoreFacesToTry ())
323 // {
324 // BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry.m_incoming)
325 // {
326 // // check all entries if the name of RIT entry matches the name of interest
327 // for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
328 // {
329 // if (it->m_prefix == iter->GetPrefix() )
330 // {
331 // header->SetNonce(it->m_nonce);
332 // header->SetNack(true);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800333 // face.m_face->SendWithoutLimit (packet->Copy());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800334 // }
335 // }
336 // }
337
338 // m_pit->erase(pitEntry);
339
340 // return;
341 // }
342}
343
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700344// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800345//
346// !!! Key point.
347// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700348void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
349 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700350 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700351{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800352 NS_LOG_FUNCTION (incomingFace << header << packet);
353 // m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700354
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800355 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800356 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
357 CcnxPitEntry const& pitEntry = ret.get<0> ();
358 // bool isNew = ret.get<1> ();
359 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800360
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800361 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700362 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800363 /**
364 * This condition will handle "routing" loops and also recently satisfied interests.
365 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
366 * is kept for another small chunk of time.
367 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700368
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800369 // //Trace duplicate interest
370 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700371
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800372 header->SetNack (CcnxInterestHeader::NACK_LOOP);
373 Ptr<Packet> packet = Create<Packet> ();
374 packet->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700375
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800376 incomingFace->SendWithoutLimit (packet);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700377
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800378 // //Trace duplicate interest
379 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700380 return;
381 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800382
383 Ptr<Packet> contentObject;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800384 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800385 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
386 if (contentObject != 0)
387 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800388 NS_ASSERT (contentObjectHeader != 0);
389
390 NS_LOG_LOGIC("Found in cache");
391
392 // TransmittedDataTrace (contentObject, CACHED,
393 // m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800394 incomingFace->SendWithoutLimit (contentObject);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800395
396 // Set pruning timout on PIT entry (instead of deleting the record)
397 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800398 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
399 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800400 return;
401 }
402
403 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700404
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700405 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800406 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
407 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
408
409 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700410 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800411 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700412
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800413 // this is almost definitely a retransmission. But should we trust the user on that?
414 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700415 else
416 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800417 m_pit->modify (m_pit->iterator_to (pitEntry),
418 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800419 }
420
421 if (outFace != pitEntry.m_outgoing.end ())
422 {
423 // got a non-duplicate interest from the face we have sent interest to
424 // Probably, there is no point in waiting data from that face... Not sure yet
425
426 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
427 // Mark interface YELLOW, but keep a small hope that data will come eventually.
428
429 // ?? not sure if we need to do that ?? ...
430
431 m_fib->modify(m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800432 ll::bind (&CcnxFibEntry::UpdateStatus,
433 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800434
435 // suppress?
436 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800437 else if (pitEntry.m_outgoing.size() > 0) // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800438
439 {
440 // We are already expecting data later in future. Suppress the interest
441 // m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
442 return;
443 }
444
445 /////////////////////////////////////////////////////////////////////
446 // Propagate
447 /////////////////////////////////////////////////////////////////////
448
449 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
450
451 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800452 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800453
454 // ForwardingStrategy will try its best to forward packet to at least one interface.
455 // If no interests was propagated, then there is not other option for forwarding or
456 // ForwardingStrategy failed to find it.
457 if (!propagated)
458 {
459 Ptr<Packet> packet = Create<Packet> ();
460 header->SetNack (CcnxInterestHeader::NACK_CONGESTION);
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800461 packet->AddHeader (*header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800462
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800463 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800465 incoming.m_face->SendWithoutLimit (packet->Copy ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800466
467 // m_droppedInterestsTrace (header, DROP_CONGESTION,
468 // m_node->GetObject<Ccnx> (), incomingFace);
469 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800470 // All incoming interests cannot be satisfied. Remove them
471 m_pit->modify (m_pit->iterator_to (pitEntry),
472 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800473
474 // Set pruning timout on PIT entry (instead of deleting the record)
475 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800476 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
477 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800478 }
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700479}
480
481// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700482void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
483 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700484 Ptr<Packet> &payload,
485 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700486{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700487
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800488 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800489 // m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700490
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700491 // 1. Lookup PIT entry
492 try
493 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700494 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700495
496 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700497
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700498 // Update metric status for the incoming interface in the corresponding FIB entry
499 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800500 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
501 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700502
503 // Add or update entry in the content store
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700504 m_contentStore->Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700505
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800506 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700507
508 // If we have sent interest for this data via this face, then update stats.
509 if (out != pitEntry.m_outgoing.end ())
510 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800511 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800512 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
513 ll::_1,
514 incomingFace,
515 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700516 }
517 else
518 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800519 // Unsolicited data, but we're interested in it... should we get it?
520 // Potential hole for attacks
521
522 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
523 ". PIT entry for "<< header->GetName ()<<" is valid, "
524 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
525
526 NS_ASSERT (false); // temporary put false here
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700527 }
528
529 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800530 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700531 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800532 if (incoming.m_face != incomingFace)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800533 incoming.m_face->SendWithoutLimit (packet->Copy ());
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700534
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800535 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700536 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800537 // All incoming interests are satisfied. Remove them
538 m_pit->modify (m_pit->iterator_to (pitEntry),
539 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
540
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800541 // Set pruning timout on PIT entry (instead of deleting the record)
542 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800543 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800544 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700545 }
546 catch (CcnxPitEntryNotFound)
547 {
548 // 2. Drop data packet if PIT entry is not found
549 // (unsolicited data packets should not "poison" content store)
550
551 //drop dulicated or not requested data packet
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800552 // m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700553 return; // do not process unsoliced data packets
554 }
555}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700556
557void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800558CcnxL3Protocol::SetBucketLeakInterval (Time interval)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700559{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800560 m_bucketLeakInterval = interval;
561
562 if (m_bucketLeakEvent.IsRunning ())
563 m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700564
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800565 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
566 &CcnxL3Protocol::LeakBuckets, this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700567}
568
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800569Time
570CcnxL3Protocol::GetBucketLeakInterval () const
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700571{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800572 return m_bucketLeakInterval;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700573}
574
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800575void
576CcnxPit::LeakBuckets ()
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700577{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800578 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
579 {
580 face->LeakBucket (m_bucketLeakInterval);
581 }
582
583 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
584 &CcnxL3Protocol::LeakBuckets, this);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700585}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800586
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700587} //namespace ns3