blob: 13bf2f0fd2fc0a45beed0d1ac7799a4346af0ff0 [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)
278 if ((header->IsNack()) && (pitEntry != m_pit->end ()))
279 {
280 //m_pit->erase (pitEntry);
281 m_pit->modify(pitEntry, CcnxPitEntry::DeleteIncoming(incomingFace));
282 }
283
284
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700285 if (m_rit->WasRecentlySatisfied (*header))
286 {
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700287 // duplicate interests (same nonce) from applications are just ignored
288 if (incomingFace->IsLocal() == true)
289 return;
290
291 // Update metric status for the incoming interface in the corresponding FIB entry
292 if (fibEntry != m_fib->end())
293 m_fib->modify (m_fib->iterator_to (pitEntry->m_fibEntry),
294 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
295
296 //Trace duplicate interest
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700297 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700298 m_node->GetObject<Ccnx> (), incomingFace);
299
300 bool isMine = false;
301 TypeId tid = TypeId ("ns3::CcnxProducer");
302 for(uint32_t i=0; i<m_node->GetNApplications();i++)
303 {
304 Ptr<Application> app = m_node->GetApplication(i);
305 if(app->GetTypeId() == tid)
306 {
307 if((DynamicCast<CcnxProducer>(app))->GetPrefix () == header->GetName ())
308 {
309 isMine = true;
310 break;
311 }
312 }
313 }
314
315 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
316 if ((isMine == true) || (contentObject != NULL))
317 {
318 //never respond with NACK to NACK
319 if(header->IsNack () )
320 return;
321
322 // always return a duplicate packet
323 header->SetNack(true);
324 //Trace duplicate interest
325 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
326 m_node->GetObject<Ccnx> (), incomingFace);
327
328 SendInterest(incomingFace, header, packet->Copy());
329
330 return;
331 }
332
333
334 // check PIT. or there is no outgoing entry for this interface,
335 // silently drop the duplicate packet
336
337 // If no entry found, silently drop
338 if( pitEntry == m_pit->end() )
339 return;
340
341 // If PIT entry timed out, silently drop
342 if( pitEntry->m_timerExpired == true )
343 return;
344
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700345 // loop?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700346
347 // Check if there is no outgoing entry for the interface or different nonce
348 // (i.e., got a duplicate packet, but we haven't sent interest to this
349 // interface)
350 //
351 // This case means that there is a loop in the network.
352 // So, prune this link, but do not remove PIT entry
353
354 // Alex, check this condition!!
355 if(pitEntry->m_outgoing.size () == 0)
356 {
357 //never respond with NACK to NACK
358 if(header->IsNack () )
359 return;
360
361 // always return a duplicate packet
362 header->SetNack(true);
363 //Trace duplicate interest
364 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
365 m_node->GetObject<Ccnx> (), incomingFace);
366
367 SendInterest(incomingFace, header, packet->Copy());
368 return;
369 }
370
371
372 // At this point:
373 // - there is a non-expired PIT entry,
374 // - there is an outgoing interest to the interface, and
375 // - a nonce in outgoing entry is equal to a nonce in the received duplicate packet
376
377 // Should perform:
378 // Cleaning outgoing entry
379 // If there are no outgoing interests and available interfaces left (pe->availableInterfaces),
380 // prune all incoming interests, otherwise allow forwarding of the interest
381 if( header->IsNack () )
382 {
383 if( header->IsCongested () == false )
384 m_pit->LeakBucket(incomingFace,1);
385
386 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));
387 }
388 else
389 {
390 //poit->waitingInVain = true;
391 }
392
393
394 // prune all incoming interests
395 if((pitEntry->m_outgoing.size() ==0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
396 {
397 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
398 {
399 if(face.m_face->IsLocal() == false)
400 {
401 // check all entries if the name of RIT entry matches the name of interest
402 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
403 {
404 if (it->m_prefix == pitEntry->GetPrefix() )
405 {
406
407 header->SetNonce(it->m_nonce);
408 header->SetNack(true);
409 SendInterest(face.m_face, header, packet->Copy());
410 break;
411 }
412 }
413 }
414 }
415
416 // Finally, remote the PIT entry
417 m_pit->erase (pitEntry);
418
419 return; // stop processing
420 }
421
422 if(pitEntry->m_fibEntry.m_faces.size() == 0)
423 return;
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700424 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700425
426 // Otherwise,
427 // propagate the interest
428 //
429 // method `propagateInterest' can/should try different interface
430 // from `availableInterfaces' list
431
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700432 m_rit->SetRecentlySatisfied (*header);
433
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700434 NS_LOG_INFO("Cache Lookup for " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700435 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700436 if (contentObject != NULL)
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700437 {
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700438 NS_LOG_INFO("Found in cache");
439
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700440 TransmittedDataTrace (contentObject, CACHED,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700441 m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700442 incomingFace->Send (contentObject);
443 return;
444 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700445
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700446 // Data is not in cache
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700447
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700448 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry->m_incoming.find (incomingFace);
449 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry->m_outgoing.find (incomingFace);
450
451 if ((pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false))
452 {
453 //CcnxFibFaceMetricContainer::type m_faces;
454 //pitEntry->m_fibEntry->m_faces
455
456 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
457 // Give up this interface, but keep a small hope when the returned packet doesn't have PRUNE status
458 if( inFace->m_face == outFace->m_face )
459 {
460 if( header->IsCongested() == true )
461 {
462 m_pit->LeakBucket(incomingFace, 1);
463 m_pit->modify (pitEntry, CcnxPitEntry::DeleteOutgoing(outFace->m_face));
464 }
465 //else
466 // poit->waitingInVain = true;
467
468 // Update metric status for the incoming interface in the corresponding FIB entry
469 if(fibEntry != m_fib->end())
470 m_fib->modify(m_fib->iterator_to (pitEntry->m_fibEntry),
471 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
472 }
473 }
474
475
476 if((pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
477 // prune all incoming interests
478 {
479
480 for(CcnxPitEntryContainer::type::iterator iter = m_pit->begin();
481 iter != m_pit->end();
482 iter++)
483 {
484 /*for(CcnxPitEntryIncomingFaceContainer::type::iterator face = iter->m_incoming.begin();
485 face != iter->m_incoming.end();
486 face++)*/
487 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, iter->m_incoming)
488 {
489 if(face.m_face->IsLocal() == true)
490 {
491 //returnInterestToApp( pkt, -piit->interfaceIndex );
492 //continue;
493 }
494
495 // check all entries if the name of RIT entry matches the name of interest
496 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
497 {
498 if (it->m_prefix == iter->GetPrefix() )
499 {
500 header->SetNonce(it->m_nonce);
501 header->SetNack(true);
502 SendInterest(face.m_face, header, packet->Copy());
503 }
504 }
505 }
506
507 }
508
509 m_pit->erase(pitEntry);
510
511 return; // there is nothing else to do
512 }
513
514 // Suppress this interest only if we're still expecting data from some other interface
515 if( pitEntry->m_outgoing.size() > 0 )
516 {
517 return; //ok. Now we can suppress this interest
518 }
519
520
521 // Prune and delete PIT entry if there are no available interfaces to propagate interest
522 if( pitEntry->m_fibEntry.m_faces.size() == 0)
523 {
524 //if no match is found in the FIB, drop packet
525 //printf( "Node %d: cannot process Interest packet %s (no interfaces left)\n", _node->nodeId, pkt->contentName );
526
527 if(incomingFace->IsLocal() == false)
528 {
529 header->SetNack(true);
530 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
531 m_node->GetObject<Ccnx> (), incomingFace);
532 SendInterest(incomingFace, header, packet->Copy());
533 }
534
535 m_pit->erase(pitEntry);
536
537 }
538
539
540
541 // otherwise, try one of the available interfaces
542
543 // suppress interest if
544 /*if (pitEntry->m_incoming.size () != 0 && // not a new PIT entry and
545 inFace != pitEntry->m_incoming.end ()) // existing entry, but interest received via different face
546 {
547 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
548 m_node->GetObject<Ccnx> (), incomingFace);
549 return;
550 }*/
551
552
553 //just in case of bug
554 header->SetNack(false);
555 header->SetCongested(false);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700556
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700557 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700558
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700559 m_pit->modify (pitEntry, CcnxPitEntry::AddIncoming(incomingFace));
560
561 bool propagated = m_forwardingStrategy->
562 PropagateInterest (pitEntry, fibEntry,incomingFace, header, packet,
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700563 MakeCallback (&CcnxL3Protocol::SendInterest, this)
564 );
565
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700566 // If interest wasn't propagated further (probably, a limit is reached),
567 // prune and delete PIT entry if there are no outstanding interests.
568 // Stop processing otherwise.
569 if( (!propagated) && (pitEntry->m_outgoing.size() == 0))
570 {
571 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
572 {
573
574
575 header->SetNack(true);
576 header->SetCongested(true);
577 SendInterest (face.m_face, header, packet->Copy());
578
579 m_droppedInterestsTrace (header, DROP_CONGESTION,
580 m_node->GetObject<Ccnx> (), incomingFace);
581 }
582
583 m_pit->erase (pitEntry);
584 }
585 /*}
586 else
587 {
588 m_droppedInterestsTrace (header, NDN_PIT_TIMER_EXPIRED,
589 m_node->GetObject<Ccnx> (), incomingFace);
590 return;
591 }*/
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700592}
593
594// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700595void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
596 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700597 Ptr<Packet> &payload,
598 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700599{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700600
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700601 NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700602 m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700603
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700604 // 1. Lookup PIT entry
605 try
606 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700607 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700608
609 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700610
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700611 // Update metric status for the incoming interface in the corresponding FIB entry
612 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
613 CcnxFibEntry::UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700614
615 // Add or update entry in the content store
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700616 NS_LOG_INFO("Cached " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700617 m_contentStore->Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700618
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700619 CcnxPitEntryOutgoingFaceContainer::type::iterator
620 out = pitEntry.m_outgoing.find (incomingFace);
621
622 // If we have sent interest for this data via this face, then update stats.
623 if (out != pitEntry.m_outgoing.end ())
624 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700625 m_pit->modify (m_pit->iterator_to (pitEntry),
626 CcnxPitEntry::EstimateRttAndRemoveFace(out, m_fib));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700627 // face will be removed in the above call
628 }
629 else
630 {
631 NS_LOG_WARN ("Node "<< m_node->GetId() <<
632 ". PIT entry for "<< header->GetName ()<<" is valid, "
633 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
634 }
635
636 //satisfy all pending incoming Interests
637 BOOST_FOREACH (const CcnxPitEntryIncomingFace &interest, pitEntry.m_incoming)
638 {
639 if (interest.m_face == incomingFace) continue;
640
641 // may not work either because of 'const' thing
642 interest.m_face->Send (packet->Copy ()); // unfortunately, we have to copy packet...
643 m_transmittedDataTrace (header, payload, FORWARDED, m_node->GetObject<Ccnx> (), interest.m_face);
644 }
645
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700646 m_pit->modify (m_pit->iterator_to (pitEntry), CcnxPitEntry::ClearIncoming()); // satisfy all incoming interests
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700647
648 if( pitEntry.m_outgoing.size()==0 ) // remove PIT when all outgoing interests are "satisfied"
649 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700650 m_pit->erase (m_pit->iterator_to (pitEntry));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700651 }
652
653 }
654 catch (CcnxPitEntryNotFound)
655 {
656 // 2. Drop data packet if PIT entry is not found
657 // (unsolicited data packets should not "poison" content store)
658
659 //drop dulicated or not requested data packet
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700660 m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700661 return; // do not process unsoliced data packets
662 }
663}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700664
665void
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700666CcnxL3Protocol::SendInterest (const Ptr<CcnxFace> &face,
667 const Ptr<CcnxInterestHeader> &header,
668 const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700669{
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700670 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700671 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
672
673 if (face->IsUp ())
674 {
675 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700676 m_transmittedInterestsTrace (header, m_node->GetObject<Ccnx> (), face);
677 face->Send (packet);
678 }
679 else
680 {
681 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
682 m_droppedInterestsTrace (header, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
683 }
684}
685
686void
687CcnxL3Protocol::SendContentObject (const Ptr<CcnxFace> &face,
688 const Ptr<CcnxContentObjectHeader> &header,
689 const Ptr<Packet> &packet)
690{
691 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
692 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
693
694 NS_ASSERT_MSG (false, "Should not be called for now");
695
696 if (face->IsUp ())
697 {
698 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700699 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700700 face->Send (packet);
701 }
702 else
703 {
704 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700705 // m_dropTrace (packet, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700706 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700707}
708
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700709Ptr<CcnxPit>
710CcnxL3Protocol::GetPit()
711{
712 return m_pit;
713}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700714} //namespace ns3