blob: 87217bcdaa4fe01d3657331d8329a4ff21152018 [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 Afanasyev52e9aa92011-11-15 20:23:20 -080041#include "ccnx-net-device-face.h"
42
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070043#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070044
45NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
46
47namespace ns3 {
48
Alexander Afanasyev7112f482011-08-17 14:05:57 -070049const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050
51NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
52
53TypeId
54CcnxL3Protocol::GetTypeId (void)
55{
56 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
57 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070058 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev7112f482011-08-17 14:05:57 -070060 // .AddTraceSource ("Tx", "Send ccnx packet to outgoing interface.",
61 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_txTrace))
62 // .AddTraceSource ("Rx", "Receive ccnx packet from incoming interface.",
63 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_rxTrace))
64 // .AddTraceSource ("Drop", "Drop ccnx packet",
65 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_dropTrace))
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070066 // .AddAttribute ("InterfaceList", "The set of Ccnx interfaces associated to this Ccnx stack.",
67 // ObjectVectorValue (),
68 // MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
69 // MakeObjectVectorChecker<CcnxFace> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070070
Alexander Afanasyev7112f482011-08-17 14:05:57 -070071 // .AddTraceSource ("SendOutgoing", "A newly-generated packet by this node is about to be queued for transmission",
72 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_sendOutgoingTrace))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070073
74 ;
75 return tid;
76}
77
78CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070079: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080{
81 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070082
83 m_rit = CreateObject<CcnxRit> ();
84 m_pit = CreateObject<CcnxPit> ();
85 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086}
87
88CcnxL3Protocol::~CcnxL3Protocol ()
89{
90 NS_LOG_FUNCTION (this);
91}
92
93void
94CcnxL3Protocol::SetNode (Ptr<Node> node)
95{
96 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070097 m_fib = m_node->GetObject<CcnxFib> ();
98 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
99
100 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101}
102
103/*
104 * This method is called by AddAgregate and completes the aggregation
105 * by setting the node in the ccnx stack
106 */
107void
108CcnxL3Protocol::NotifyNewAggregate ()
109{
110 if (m_node == 0)
111 {
112 Ptr<Node>node = this->GetObject<Node>();
113 // verify that it's a valid node and that
114 // the node has not been set before
115 if (node != 0)
116 {
117 this->SetNode (node);
118 }
119 }
120 Object::NotifyNewAggregate ();
121}
122
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123void
124CcnxL3Protocol::DoDispose (void)
125{
126 NS_LOG_FUNCTION (this);
127
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700128 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129 {
130 *i = 0;
131 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700132 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133 m_node = 0;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700134 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135 Object::DoDispose ();
136}
137
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700138void
139CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
140{
141 NS_LOG_FUNCTION (this);
142 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700143 // m_forwardingStrategy->SetCcnx (this);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700144}
145
146Ptr<CcnxForwardingStrategy>
147CcnxL3Protocol::GetForwardingStrategy (void) const
148{
149 return m_forwardingStrategy;
150}
151
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700152uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700153CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700155 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700157 face->SetNode (m_node);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700158 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700160 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700161 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700163 m_faces.push_back (face);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700164 m_faceCounter ++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700165 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166}
167
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700168void
169CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
170{
171 // ask face to register in lower-layer stack
172 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
173 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
174 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
175 m_faces.erase (face_it);
176}
177
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700178Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700179CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700180{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700181 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 -0700182 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700183 if (face->GetId () == index)
184 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700185 }
186 return 0;
187}
188
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800189Ptr<CcnxFace>
190CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
191{
192 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
193 {
194 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
195 if (netDeviceFace == 0) continue;
196
197 if (netDeviceFace->GetNetDevice () == netDevice)
198 return face;
199 }
200 return 0;
201}
202
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700203uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700204CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700205{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700206 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207}
208
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700209void
210CcnxL3Protocol::TransmittedDataTrace (Ptr<Packet> packet,
211 ContentObjectSource type,
212 Ptr<Ccnx> ccnx, Ptr<const CcnxFace> face)
213{
214 // a "small" inefficiency for logging purposes
215 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
216 static CcnxContentObjectTail tail;
217 packet->RemoveHeader (*header);
218 packet->RemoveTrailer (tail);
219
220 m_transmittedDataTrace (header, packet/*payload*/, type, ccnx, face);
221
222 packet->AddHeader (*header);
223 packet->AddTrailer (tail);
224}
225
226
Alexander Afanasyev98256102011-08-14 01:00:02 -0700227// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700229CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700231 if (!face->IsUp ())
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700232 {
233 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700234 // m_dropTrace (p, INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700235 return;
236 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700237 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700238
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700239 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700240 try
241 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700242 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700243 switch (type)
244 {
245 case CcnxHeaderHelper::INTEREST:
246 {
247 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700248
249 // Deserialization. Exception may be thrown
250 packet->RemoveHeader (*header);
251 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
252
253 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700254 break;
255 }
256 case CcnxHeaderHelper::CONTENT_OBJECT:
257 {
258 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700259
260 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
261
262 // Deserialization. Exception may be thrown
263 packet->RemoveHeader (*header);
264 packet->RemoveTrailer (contentObjectTrailer);
265
266 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700267 break;
268 }
269 }
270
271 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700272 }
273 catch (CcnxUnknownHeaderException)
274 {
275 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
276 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700277}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700278
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700279// Processing Interests
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700280void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
281 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700282 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700283{
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700284 NS_LOG_LOGIC ("Receiving interest from " << &incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700285 m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700286
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700287 // Lookup of Pit and Fib entries for this Interest
288 CcnxFibEntryContainer::type::iterator fibEntry;
289 CcnxPitEntryContainer::type::iterator pitEntry = m_pit->Lookup (*header, fibEntry);
290
291 // No matter is it duplicate or not, if it is a NACK message, remove all possible incoming
292 // entries for this interface (NACK means that neighbor gave up trying and there is no
293 // point of sending data in this direction)
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700294 NS_LOG_INFO("Before (header->IsNack()) && (pitEntry != m_pit->end ())");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700295 if ((header->IsNack()) && (pitEntry != m_pit->end ()))
296 {
297 //m_pit->erase (pitEntry);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700298 NS_LOG_INFO("TRUE");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700299 m_pit->modify(pitEntry, CcnxPitEntry::DeleteIncoming(incomingFace));
300 }
301
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700302 NS_LOG_INFO("Before WasRecentlySatisfied");
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700303 if (m_rit->WasRecentlySatisfied (*header))
304 {
305 return;
306 }
307 /*if (m_rit->WasRecentlySatisfied (*header))
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700308 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700309 NS_LOG_INFO("Entering WasRecentlySatisfied");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700310 // duplicate interests (same nonce) from applications are just ignored
311 if (incomingFace->IsLocal() == true)
312 return;
313
314 // Update metric status for the incoming interface in the corresponding FIB entry
315 if (fibEntry != m_fib->end())
316 m_fib->modify (m_fib->iterator_to (pitEntry->m_fibEntry),
317 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
318
319 //Trace duplicate interest
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700320 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700321 m_node->GetObject<Ccnx> (), incomingFace);
322
323 bool isMine = false;
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700324 //TypeId tid = TypeId ("ns3::CcnxProducer");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700325 for(uint32_t i=0; i<m_node->GetNApplications();i++)
326 {
327 Ptr<Application> app = m_node->GetApplication(i);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700328 NS_LOG_INFO("ApplicationName = " << app->GetTypeId().GetName());
329 if(app->GetTypeId().GetName() == "ns3::CcnxProducer")
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700330 {
331 if((DynamicCast<CcnxProducer>(app))->GetPrefix () == header->GetName ())
332 {
333 isMine = true;
334 break;
335 }
336 }
337 }
338
339 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
340 if ((isMine == true) || (contentObject != NULL))
341 {
342 //never respond with NACK to NACK
343 if(header->IsNack () )
344 return;
345
346 // always return a duplicate packet
347 header->SetNack(true);
348 //Trace duplicate interest
349 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
350 m_node->GetObject<Ccnx> (), incomingFace);
351
352 SendInterest(incomingFace, header, packet->Copy());
353
354 return;
355 }
356
357
358 // check PIT. or there is no outgoing entry for this interface,
359 // silently drop the duplicate packet
360
361 // If no entry found, silently drop
362 if( pitEntry == m_pit->end() )
363 return;
364
365 // If PIT entry timed out, silently drop
366 if( pitEntry->m_timerExpired == true )
367 return;
368
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700369 // loop?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700370
371 // Check if there is no outgoing entry for the interface or different nonce
372 // (i.e., got a duplicate packet, but we haven't sent interest to this
373 // interface)
374 //
375 // This case means that there is a loop in the network.
376 // So, prune this link, but do not remove PIT entry
377
378 // Alex, check this condition!!
379 if(pitEntry->m_outgoing.size () == 0)
380 {
381 //never respond with NACK to NACK
382 if(header->IsNack () )
383 return;
384
385 // always return a duplicate packet
386 header->SetNack(true);
387 //Trace duplicate interest
388 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
389 m_node->GetObject<Ccnx> (), incomingFace);
390
391 SendInterest(incomingFace, header, packet->Copy());
392 return;
393 }
394
395
396 // At this point:
397 // - there is a non-expired PIT entry,
398 // - there is an outgoing interest to the interface, and
399 // - a nonce in outgoing entry is equal to a nonce in the received duplicate packet
400
401 // Should perform:
402 // Cleaning outgoing entry
403 // If there are no outgoing interests and available interfaces left (pe->availableInterfaces),
404 // prune all incoming interests, otherwise allow forwarding of the interest
405 if( header->IsNack () )
406 {
407 if( header->IsCongested () == false )
408 m_pit->LeakBucket(incomingFace,1);
409
410 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));
411 }
412 else
413 {
414 //poit->waitingInVain = true;
415 }
416
417
418 // prune all incoming interests
419 if((pitEntry->m_outgoing.size() ==0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
420 {
421 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
422 {
423 if(face.m_face->IsLocal() == false)
424 {
425 // check all entries if the name of RIT entry matches the name of interest
426 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
427 {
428 if (it->m_prefix == pitEntry->GetPrefix() )
429 {
430
431 header->SetNonce(it->m_nonce);
432 header->SetNack(true);
433 SendInterest(face.m_face, header, packet->Copy());
434 break;
435 }
436 }
437 }
438 }
439
440 // Finally, remote the PIT entry
441 m_pit->erase (pitEntry);
442
443 return; // stop processing
444 }
445
446 if(pitEntry->m_fibEntry.m_faces.size() == 0)
447 return;
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700448 }*/
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700449
450 // Otherwise,
451 // propagate the interest
452 //
453 // method `propagateInterest' can/should try different interface
454 // from `availableInterfaces' list
455
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700456 NS_LOG_INFO("Before SetRecentlySatisfied");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700457 m_rit->SetRecentlySatisfied (*header);
458
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700459 NS_LOG_INFO("Cache Lookup for " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700460 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700461 if (contentObject != NULL)
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700462 {
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700463 NS_LOG_INFO("Found in cache");
464
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700465 TransmittedDataTrace (contentObject, CACHED,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700466 m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700467 incomingFace->Send (contentObject);
468 return;
469 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700470
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700471 // Data is not in cache
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700472 NS_LOG_INFO("Before inFace and OutFace");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700473 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry->m_incoming.find (incomingFace);
474 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry->m_outgoing.find (incomingFace);
475
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700476 NS_LOG_INFO("Before (pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false)");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700477 if ((pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false))
478 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700479 NS_LOG_INFO("Entering (pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false)");
480
481 if(inFace->m_face == NULL)
482 NS_LOG_INFO("in face is null");
483 if(outFace->m_face == NULL)
484 NS_LOG_INFO("outface is null");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700485
486 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
487 // 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 -0700488 if( outFace != pitEntry->m_outgoing.end())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700489 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700490 NS_LOG_INFO("Entering outFace != pitEntry->m_outgoing.end()");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700491 if( header->IsCongested() == true )
492 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700493 NS_LOG_INFO("Entering header->IsCongested() == true");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700494 m_pit->LeakBucket(incomingFace, 1);
495 m_pit->modify (pitEntry, CcnxPitEntry::DeleteOutgoing(outFace->m_face));
496 }
497 //else
498 // poit->waitingInVain = true;
499
500 // Update metric status for the incoming interface in the corresponding FIB entry
501 if(fibEntry != m_fib->end())
502 m_fib->modify(m_fib->iterator_to (pitEntry->m_fibEntry),
503 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
504 }
505 }
506
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700507 NS_LOG_INFO("Before (pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0)");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700508 if((pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
509 // prune all incoming interests
510 {
511
512 for(CcnxPitEntryContainer::type::iterator iter = m_pit->begin();
513 iter != m_pit->end();
514 iter++)
515 {
516 /*for(CcnxPitEntryIncomingFaceContainer::type::iterator face = iter->m_incoming.begin();
517 face != iter->m_incoming.end();
518 face++)*/
519 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, iter->m_incoming)
520 {
521 if(face.m_face->IsLocal() == true)
522 {
523 //returnInterestToApp( pkt, -piit->interfaceIndex );
524 //continue;
525 }
526
527 // check all entries if the name of RIT entry matches the name of interest
528 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
529 {
530 if (it->m_prefix == iter->GetPrefix() )
531 {
532 header->SetNonce(it->m_nonce);
533 header->SetNack(true);
534 SendInterest(face.m_face, header, packet->Copy());
535 }
536 }
537 }
538
539 }
540
541 m_pit->erase(pitEntry);
542
543 return; // there is nothing else to do
544 }
545
546 // Suppress this interest only if we're still expecting data from some other interface
547 if( pitEntry->m_outgoing.size() > 0 )
548 {
549 return; //ok. Now we can suppress this interest
550 }
551
552
553 // Prune and delete PIT entry if there are no available interfaces to propagate interest
554 if( pitEntry->m_fibEntry.m_faces.size() == 0)
555 {
556 //if no match is found in the FIB, drop packet
557 //printf( "Node %d: cannot process Interest packet %s (no interfaces left)\n", _node->nodeId, pkt->contentName );
558
559 if(incomingFace->IsLocal() == false)
560 {
561 header->SetNack(true);
562 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
563 m_node->GetObject<Ccnx> (), incomingFace);
564 SendInterest(incomingFace, header, packet->Copy());
565 }
566
567 m_pit->erase(pitEntry);
568
569 }
570
571
572
573 // otherwise, try one of the available interfaces
574
575 // suppress interest if
576 /*if (pitEntry->m_incoming.size () != 0 && // not a new PIT entry and
577 inFace != pitEntry->m_incoming.end ()) // existing entry, but interest received via different face
578 {
579 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
580 m_node->GetObject<Ccnx> (), incomingFace);
581 return;
582 }*/
583
584
585 //just in case of bug
586 header->SetNack(false);
587 header->SetCongested(false);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700588
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800589 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700590
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800591 m_pit->modify (pitEntry, CcnxPitEntry::AddIncoming(incomingFace));
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700592
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800593 bool propagated = m_forwardingStrategy->
594 PropagateInterest (pitEntry, fibEntry,incomingFace, header, packet,
595 MakeCallback (&CcnxL3Protocol::SendInterest, this)
596 );
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700597
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800598 // If interest wasn't propagated further (probably, a limit is reached),
599 // prune and delete PIT entry if there are no outstanding interests.
600 // Stop processing otherwise.
601 if( (!propagated) && (pitEntry->m_outgoing.size() == 0))
602 {
603 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
604 {
605 header->SetNack(true);
606 header->SetCongested(true);
607 NS_LOG_INFO("Sending CONGESTION packet");
608 SendInterest (face.m_face, header, packet->Copy());
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700609
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800610 m_droppedInterestsTrace (header, DROP_CONGESTION,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700611 m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800612 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700613
614 m_pit->erase (pitEntry);
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800615 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700616 /*}
617 else
618 {
619 m_droppedInterestsTrace (header, NDN_PIT_TIMER_EXPIRED,
620 m_node->GetObject<Ccnx> (), incomingFace);
621 return;
622 }*/
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700623}
624
625// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700626void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
627 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700628 Ptr<Packet> &payload,
629 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700630{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700631
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700632 NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700633 m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700634
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700635 // 1. Lookup PIT entry
636 try
637 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700638 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700639
640 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700641
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700642 // Update metric status for the incoming interface in the corresponding FIB entry
643 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
644 CcnxFibEntry::UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700645
646 // Add or update entry in the content store
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700647 NS_LOG_INFO("Cached " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700648 m_contentStore->Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700649
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700650 CcnxPitEntryOutgoingFaceContainer::type::iterator
651 out = pitEntry.m_outgoing.find (incomingFace);
652
653 // If we have sent interest for this data via this face, then update stats.
654 if (out != pitEntry.m_outgoing.end ())
655 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700656 m_pit->modify (m_pit->iterator_to (pitEntry),
657 CcnxPitEntry::EstimateRttAndRemoveFace(out, m_fib));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700658 // face will be removed in the above call
659 }
660 else
661 {
662 NS_LOG_WARN ("Node "<< m_node->GetId() <<
663 ". PIT entry for "<< header->GetName ()<<" is valid, "
664 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
665 }
666
667 //satisfy all pending incoming Interests
668 BOOST_FOREACH (const CcnxPitEntryIncomingFace &interest, pitEntry.m_incoming)
669 {
670 if (interest.m_face == incomingFace) continue;
671
672 // may not work either because of 'const' thing
673 interest.m_face->Send (packet->Copy ()); // unfortunately, we have to copy packet...
674 m_transmittedDataTrace (header, payload, FORWARDED, m_node->GetObject<Ccnx> (), interest.m_face);
675 }
676
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700677 m_pit->modify (m_pit->iterator_to (pitEntry), CcnxPitEntry::ClearIncoming()); // satisfy all incoming interests
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700678
679 if( pitEntry.m_outgoing.size()==0 ) // remove PIT when all outgoing interests are "satisfied"
680 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700681 m_pit->erase (m_pit->iterator_to (pitEntry));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700682 }
683
684 }
685 catch (CcnxPitEntryNotFound)
686 {
687 // 2. Drop data packet if PIT entry is not found
688 // (unsolicited data packets should not "poison" content store)
689
690 //drop dulicated or not requested data packet
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700691 m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700692 return; // do not process unsoliced data packets
693 }
694}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700695
696void
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700697CcnxL3Protocol::SendInterest (const Ptr<CcnxFace> &face,
698 const Ptr<CcnxInterestHeader> &header,
699 const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700700{
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700701 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700702 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
703
704 if (face->IsUp ())
705 {
706 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700707 m_transmittedInterestsTrace (header, m_node->GetObject<Ccnx> (), face);
708 face->Send (packet);
709 }
710 else
711 {
712 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
713 m_droppedInterestsTrace (header, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
714 }
715}
716
717void
718CcnxL3Protocol::SendContentObject (const Ptr<CcnxFace> &face,
719 const Ptr<CcnxContentObjectHeader> &header,
720 const Ptr<Packet> &packet)
721{
722 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
723 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
724
725 NS_ASSERT_MSG (false, "Should not be called for now");
726
727 if (face->IsUp ())
728 {
729 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700730 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700731 face->Send (packet);
732 }
733 else
734 {
735 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700736 // m_dropTrace (packet, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700737 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700738}
739
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700740Ptr<CcnxPit>
741CcnxL3Protocol::GetPit()
742{
743 return m_pit;
744}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700745} //namespace ns3