blob: e5f7f0cd0b259829fddbb8103bc3a81c4dae2106 [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"
36#include "ccnx-route.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070037#include "ccnx-forwarding-strategy.h"
38#include "ccnx-interest-header.h"
39#include "ccnx-content-object-header.h"
40
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070041#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070042
43NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
44
45namespace ns3 {
46
Alexander Afanasyev7112f482011-08-17 14:05:57 -070047const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048
49NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
50
51TypeId
52CcnxL3Protocol::GetTypeId (void)
53{
54 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
55 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070056 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev7112f482011-08-17 14:05:57 -070058 // .AddTraceSource ("Tx", "Send ccnx packet to outgoing interface.",
59 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_txTrace))
60 // .AddTraceSource ("Rx", "Receive ccnx packet from incoming interface.",
61 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_rxTrace))
62 // .AddTraceSource ("Drop", "Drop ccnx packet",
63 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_dropTrace))
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070064 // .AddAttribute ("InterfaceList", "The set of Ccnx interfaces associated to this Ccnx stack.",
65 // ObjectVectorValue (),
66 // MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
67 // MakeObjectVectorChecker<CcnxFace> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068
Alexander Afanasyev7112f482011-08-17 14:05:57 -070069 // .AddTraceSource ("SendOutgoing", "A newly-generated packet by this node is about to be queued for transmission",
70 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_sendOutgoingTrace))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070071
72 ;
73 return tid;
74}
75
76CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070077: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078{
79 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070080
81 m_rit = CreateObject<CcnxRit> ();
82 m_pit = CreateObject<CcnxPit> ();
83 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084}
85
86CcnxL3Protocol::~CcnxL3Protocol ()
87{
88 NS_LOG_FUNCTION (this);
89}
90
91void
92CcnxL3Protocol::SetNode (Ptr<Node> node)
93{
94 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070095 m_fib = m_node->GetObject<CcnxFib> ();
96 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
97
98 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070099}
100
101/*
102 * This method is called by AddAgregate and completes the aggregation
103 * by setting the node in the ccnx stack
104 */
105void
106CcnxL3Protocol::NotifyNewAggregate ()
107{
108 if (m_node == 0)
109 {
110 Ptr<Node>node = this->GetObject<Node>();
111 // verify that it's a valid node and that
112 // the node has not been set before
113 if (node != 0)
114 {
115 this->SetNode (node);
116 }
117 }
118 Object::NotifyNewAggregate ();
119}
120
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121void
122CcnxL3Protocol::DoDispose (void)
123{
124 NS_LOG_FUNCTION (this);
125
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700126 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700127 {
128 *i = 0;
129 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700130 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131 m_node = 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700132 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133 Object::DoDispose ();
134}
135
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700136void
137CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
138{
139 NS_LOG_FUNCTION (this);
140 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700141 // m_forwardingStrategy->SetCcnx (this);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700142}
143
144Ptr<CcnxForwardingStrategy>
145CcnxL3Protocol::GetForwardingStrategy (void) const
146{
147 return m_forwardingStrategy;
148}
149
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700150uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700151CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700152{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700153 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700155 face->SetNode (m_node);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700156 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700157
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700158 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700159 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700160
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700161 m_faces.push_back (face);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700162 m_faceCounter ++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700163 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700164}
165
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700166void
167CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
168{
169 // ask face to register in lower-layer stack
170 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
171 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
172 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
173 m_faces.erase (face_it);
174}
175
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700176Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700177CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700178{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700179 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 -0700180 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700181 if (face->GetId () == index)
182 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700183 }
184 return 0;
185}
186
187uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700188CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700189{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700190 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700191}
192
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700193void
194CcnxL3Protocol::TransmittedDataTrace (Ptr<Packet> packet,
195 ContentObjectSource type,
196 Ptr<Ccnx> ccnx, Ptr<const CcnxFace> face)
197{
198 // a "small" inefficiency for logging purposes
199 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
200 static CcnxContentObjectTail tail;
201 packet->RemoveHeader (*header);
202 packet->RemoveTrailer (tail);
203
204 m_transmittedDataTrace (header, packet/*payload*/, type, ccnx, face);
205
206 packet->AddHeader (*header);
207 packet->AddTrailer (tail);
208}
209
210
Alexander Afanasyev98256102011-08-14 01:00:02 -0700211// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700212void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700213CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700215 if (!face->IsUp ())
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700216 {
217 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700218 // m_dropTrace (p, INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700219 return;
220 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700221 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700222
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700223 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700224 try
225 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700226 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700227 switch (type)
228 {
229 case CcnxHeaderHelper::INTEREST:
230 {
231 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700232
233 // Deserialization. Exception may be thrown
234 packet->RemoveHeader (*header);
235 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
236
237 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700238 break;
239 }
240 case CcnxHeaderHelper::CONTENT_OBJECT:
241 {
242 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700243
244 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
245
246 // Deserialization. Exception may be thrown
247 packet->RemoveHeader (*header);
248 packet->RemoveTrailer (contentObjectTrailer);
249
250 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700251 break;
252 }
253 }
254
255 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700256 }
257 catch (CcnxUnknownHeaderException)
258 {
259 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
260 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700261}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700262
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700263// Processing Interests
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700264void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
265 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700266 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700267{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700268 NS_LOG_LOGIC ("Receiving interest from " << &incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700269 m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700270
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700271 // Lookup of Pit and Fib entries for this Interest
272 CcnxFibEntryContainer::type::iterator fibEntry;
273 CcnxPitEntryContainer::type::iterator pitEntry = m_pit->Lookup (*header, fibEntry);
274
275 // No matter is it duplicate or not, if it is a NACK message, remove all possible incoming
276 // entries for this interface (NACK means that neighbor gave up trying and there is no
277 // point of sending data in this direction)
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700278 NS_LOG_INFO("Before (header->IsNack()) && (pitEntry != m_pit->end ())");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700279 if ((header->IsNack()) && (pitEntry != m_pit->end ()))
280 {
281 //m_pit->erase (pitEntry);
282 m_pit->modify(pitEntry, CcnxPitEntry::DeleteIncoming(incomingFace));
283 }
284
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700285 NS_LOG_INFO("Before WasRecentlySatisfied");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700286 if (m_rit->WasRecentlySatisfied (*header))
287 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700288 NS_LOG_INFO("Entering WasRecentlySatisfied");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700289 // duplicate interests (same nonce) from applications are just ignored
290 if (incomingFace->IsLocal() == true)
291 return;
292
293 // Update metric status for the incoming interface in the corresponding FIB entry
294 if (fibEntry != m_fib->end())
295 m_fib->modify (m_fib->iterator_to (pitEntry->m_fibEntry),
296 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
297
298 //Trace duplicate interest
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700299 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700300 m_node->GetObject<Ccnx> (), incomingFace);
301
302 bool isMine = false;
303 TypeId tid = TypeId ("ns3::CcnxProducer");
304 for(uint32_t i=0; i<m_node->GetNApplications();i++)
305 {
306 Ptr<Application> app = m_node->GetApplication(i);
307 if(app->GetTypeId() == tid)
308 {
309 if((DynamicCast<CcnxProducer>(app))->GetPrefix () == header->GetName ())
310 {
311 isMine = true;
312 break;
313 }
314 }
315 }
316
317 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
318 if ((isMine == true) || (contentObject != NULL))
319 {
320 //never respond with NACK to NACK
321 if(header->IsNack () )
322 return;
323
324 // always return a duplicate packet
325 header->SetNack(true);
326 //Trace duplicate interest
327 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
328 m_node->GetObject<Ccnx> (), incomingFace);
329
330 SendInterest(incomingFace, header, packet->Copy());
331
332 return;
333 }
334
335
336 // check PIT. or there is no outgoing entry for this interface,
337 // silently drop the duplicate packet
338
339 // If no entry found, silently drop
340 if( pitEntry == m_pit->end() )
341 return;
342
343 // If PIT entry timed out, silently drop
344 if( pitEntry->m_timerExpired == true )
345 return;
346
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700347 // loop?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700348
349 // Check if there is no outgoing entry for the interface or different nonce
350 // (i.e., got a duplicate packet, but we haven't sent interest to this
351 // interface)
352 //
353 // This case means that there is a loop in the network.
354 // So, prune this link, but do not remove PIT entry
355
356 // Alex, check this condition!!
357 if(pitEntry->m_outgoing.size () == 0)
358 {
359 //never respond with NACK to NACK
360 if(header->IsNack () )
361 return;
362
363 // always return a duplicate packet
364 header->SetNack(true);
365 //Trace duplicate interest
366 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
367 m_node->GetObject<Ccnx> (), incomingFace);
368
369 SendInterest(incomingFace, header, packet->Copy());
370 return;
371 }
372
373
374 // At this point:
375 // - there is a non-expired PIT entry,
376 // - there is an outgoing interest to the interface, and
377 // - a nonce in outgoing entry is equal to a nonce in the received duplicate packet
378
379 // Should perform:
380 // Cleaning outgoing entry
381 // If there are no outgoing interests and available interfaces left (pe->availableInterfaces),
382 // prune all incoming interests, otherwise allow forwarding of the interest
383 if( header->IsNack () )
384 {
385 if( header->IsCongested () == false )
386 m_pit->LeakBucket(incomingFace,1);
387
388 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));
389 }
390 else
391 {
392 //poit->waitingInVain = true;
393 }
394
395
396 // prune all incoming interests
397 if((pitEntry->m_outgoing.size() ==0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
398 {
399 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
400 {
401 if(face.m_face->IsLocal() == false)
402 {
403 // check all entries if the name of RIT entry matches the name of interest
404 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
405 {
406 if (it->m_prefix == pitEntry->GetPrefix() )
407 {
408
409 header->SetNonce(it->m_nonce);
410 header->SetNack(true);
411 SendInterest(face.m_face, header, packet->Copy());
412 break;
413 }
414 }
415 }
416 }
417
418 // Finally, remote the PIT entry
419 m_pit->erase (pitEntry);
420
421 return; // stop processing
422 }
423
424 if(pitEntry->m_fibEntry.m_faces.size() == 0)
425 return;
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700426 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700427
428 // Otherwise,
429 // propagate the interest
430 //
431 // method `propagateInterest' can/should try different interface
432 // from `availableInterfaces' list
433
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700434 NS_LOG_INFO("Before SetRecentlySatisfied");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700435 m_rit->SetRecentlySatisfied (*header);
436
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700437 NS_LOG_INFO("Cache Lookup for " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700438 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700439 if (contentObject != NULL)
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700440 {
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700441 NS_LOG_INFO("Found in cache");
442
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700443 TransmittedDataTrace (contentObject, CACHED,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700444 m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700445 incomingFace->Send (contentObject);
446 return;
447 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700448
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700449 // Data is not in cache
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700450 NS_LOG_INFO("Before inFace and OutFace");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700451 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry->m_incoming.find (incomingFace);
452 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry->m_outgoing.find (incomingFace);
453
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700454 NS_LOG_INFO("Before (pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false)");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700455 if ((pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false))
456 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700457 NS_LOG_INFO("Entering (pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false)");
458
459 if(inFace->m_face == NULL)
460 NS_LOG_INFO("in face is null");
461 if(outFace->m_face == NULL)
462 NS_LOG_INFO("outface is null");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700463
464 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
465 // Give up this interface, but keep a small hope when the returned packet doesn't have PRUNE status
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700466 if( outFace != pitEntry->m_outgoing.end())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700467 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700468 NS_LOG_INFO("Entering outFace != pitEntry->m_outgoing.end()");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700469 if( header->IsCongested() == true )
470 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700471 NS_LOG_INFO("Entering header->IsCongested() == true");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700472 m_pit->LeakBucket(incomingFace, 1);
473 m_pit->modify (pitEntry, CcnxPitEntry::DeleteOutgoing(outFace->m_face));
474 }
475 //else
476 // poit->waitingInVain = true;
477
478 // Update metric status for the incoming interface in the corresponding FIB entry
479 if(fibEntry != m_fib->end())
480 m_fib->modify(m_fib->iterator_to (pitEntry->m_fibEntry),
481 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
482 }
483 }
484
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700485 NS_LOG_INFO("Before (pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0)");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700486 if((pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
487 // prune all incoming interests
488 {
489
490 for(CcnxPitEntryContainer::type::iterator iter = m_pit->begin();
491 iter != m_pit->end();
492 iter++)
493 {
494 /*for(CcnxPitEntryIncomingFaceContainer::type::iterator face = iter->m_incoming.begin();
495 face != iter->m_incoming.end();
496 face++)*/
497 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, iter->m_incoming)
498 {
499 if(face.m_face->IsLocal() == true)
500 {
501 //returnInterestToApp( pkt, -piit->interfaceIndex );
502 //continue;
503 }
504
505 // check all entries if the name of RIT entry matches the name of interest
506 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
507 {
508 if (it->m_prefix == iter->GetPrefix() )
509 {
510 header->SetNonce(it->m_nonce);
511 header->SetNack(true);
512 SendInterest(face.m_face, header, packet->Copy());
513 }
514 }
515 }
516
517 }
518
519 m_pit->erase(pitEntry);
520
521 return; // there is nothing else to do
522 }
523
524 // Suppress this interest only if we're still expecting data from some other interface
525 if( pitEntry->m_outgoing.size() > 0 )
526 {
527 return; //ok. Now we can suppress this interest
528 }
529
530
531 // Prune and delete PIT entry if there are no available interfaces to propagate interest
532 if( pitEntry->m_fibEntry.m_faces.size() == 0)
533 {
534 //if no match is found in the FIB, drop packet
535 //printf( "Node %d: cannot process Interest packet %s (no interfaces left)\n", _node->nodeId, pkt->contentName );
536
537 if(incomingFace->IsLocal() == false)
538 {
539 header->SetNack(true);
540 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
541 m_node->GetObject<Ccnx> (), incomingFace);
542 SendInterest(incomingFace, header, packet->Copy());
543 }
544
545 m_pit->erase(pitEntry);
546
547 }
548
549
550
551 // otherwise, try one of the available interfaces
552
553 // suppress interest if
554 /*if (pitEntry->m_incoming.size () != 0 && // not a new PIT entry and
555 inFace != pitEntry->m_incoming.end ()) // existing entry, but interest received via different face
556 {
557 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
558 m_node->GetObject<Ccnx> (), incomingFace);
559 return;
560 }*/
561
562
563 //just in case of bug
564 header->SetNack(false);
565 header->SetCongested(false);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700566
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700567 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700568
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700569 m_pit->modify (pitEntry, CcnxPitEntry::AddIncoming(incomingFace));
570
571 bool propagated = m_forwardingStrategy->
572 PropagateInterest (pitEntry, fibEntry,incomingFace, header, packet,
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700573 MakeCallback (&CcnxL3Protocol::SendInterest, this)
574 );
575
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700576 // If interest wasn't propagated further (probably, a limit is reached),
577 // prune and delete PIT entry if there are no outstanding interests.
578 // Stop processing otherwise.
579 if( (!propagated) && (pitEntry->m_outgoing.size() == 0))
580 {
581 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
582 {
583
584
585 header->SetNack(true);
586 header->SetCongested(true);
587 SendInterest (face.m_face, header, packet->Copy());
588
589 m_droppedInterestsTrace (header, DROP_CONGESTION,
590 m_node->GetObject<Ccnx> (), incomingFace);
591 }
592
593 m_pit->erase (pitEntry);
594 }
595 /*}
596 else
597 {
598 m_droppedInterestsTrace (header, NDN_PIT_TIMER_EXPIRED,
599 m_node->GetObject<Ccnx> (), incomingFace);
600 return;
601 }*/
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700602}
603
604// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700605void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
606 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700607 Ptr<Packet> &payload,
608 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700609{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700610
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700611 NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700612 m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700613
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700614 // 1. Lookup PIT entry
615 try
616 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700617 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700618
619 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700620
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700621 // Update metric status for the incoming interface in the corresponding FIB entry
622 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
623 CcnxFibEntry::UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700624
625 // Add or update entry in the content store
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700626 NS_LOG_INFO("Cached " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700627 m_contentStore->Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700628
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700629 CcnxPitEntryOutgoingFaceContainer::type::iterator
630 out = pitEntry.m_outgoing.find (incomingFace);
631
632 // If we have sent interest for this data via this face, then update stats.
633 if (out != pitEntry.m_outgoing.end ())
634 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700635 m_pit->modify (m_pit->iterator_to (pitEntry),
636 CcnxPitEntry::EstimateRttAndRemoveFace(out, m_fib));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700637 // face will be removed in the above call
638 }
639 else
640 {
641 NS_LOG_WARN ("Node "<< m_node->GetId() <<
642 ". PIT entry for "<< header->GetName ()<<" is valid, "
643 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
644 }
645
646 //satisfy all pending incoming Interests
647 BOOST_FOREACH (const CcnxPitEntryIncomingFace &interest, pitEntry.m_incoming)
648 {
649 if (interest.m_face == incomingFace) continue;
650
651 // may not work either because of 'const' thing
652 interest.m_face->Send (packet->Copy ()); // unfortunately, we have to copy packet...
653 m_transmittedDataTrace (header, payload, FORWARDED, m_node->GetObject<Ccnx> (), interest.m_face);
654 }
655
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700656 m_pit->modify (m_pit->iterator_to (pitEntry), CcnxPitEntry::ClearIncoming()); // satisfy all incoming interests
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700657
658 if( pitEntry.m_outgoing.size()==0 ) // remove PIT when all outgoing interests are "satisfied"
659 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700660 m_pit->erase (m_pit->iterator_to (pitEntry));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700661 }
662
663 }
664 catch (CcnxPitEntryNotFound)
665 {
666 // 2. Drop data packet if PIT entry is not found
667 // (unsolicited data packets should not "poison" content store)
668
669 //drop dulicated or not requested data packet
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700670 m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700671 return; // do not process unsoliced data packets
672 }
673}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700674
675void
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700676CcnxL3Protocol::SendInterest (const Ptr<CcnxFace> &face,
677 const Ptr<CcnxInterestHeader> &header,
678 const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700679{
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700680 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700681 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
682
683 if (face->IsUp ())
684 {
685 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700686 m_transmittedInterestsTrace (header, m_node->GetObject<Ccnx> (), face);
687 face->Send (packet);
688 }
689 else
690 {
691 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
692 m_droppedInterestsTrace (header, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
693 }
694}
695
696void
697CcnxL3Protocol::SendContentObject (const Ptr<CcnxFace> &face,
698 const Ptr<CcnxContentObjectHeader> &header,
699 const Ptr<Packet> &packet)
700{
701 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
702 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
703
704 NS_ASSERT_MSG (false, "Should not be called for now");
705
706 if (face->IsUp ())
707 {
708 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700709 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700710 face->Send (packet);
711 }
712 else
713 {
714 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700715 // m_dropTrace (packet, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700716 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700717}
718
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700719Ptr<CcnxPit>
720CcnxL3Protocol::GetPit()
721{
722 return m_pit;
723}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700724} //namespace ns3