blob: 6a4fae6b109cea6e0b3dda08dc46ff9513bc81f0 [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 Moiseenkod83eb0d2011-11-16 15:23:46 -0800287
288 if( header->IsNack () )
289 {
290 NS_LOG_INFO("============");
291 NS_LOG_INFO("NACK");
292 NS_LOG_INFO("==========");
293 /*if( header->IsCongested () == false )
294 m_pit->LeakBucket(incomingFace,1);
295
296
297 m_droppedInterestsTrace (header, DROP_CONGESTION,
298 m_node->GetObject<Ccnx> (), incomingFace);
299
300 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));*/
301 }
302
303
304
305
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700306 // Lookup of Pit and Fib entries for this Interest
307 CcnxFibEntryContainer::type::iterator fibEntry;
308 CcnxPitEntryContainer::type::iterator pitEntry = m_pit->Lookup (*header, fibEntry);
309
310 // No matter is it duplicate or not, if it is a NACK message, remove all possible incoming
311 // entries for this interface (NACK means that neighbor gave up trying and there is no
312 // point of sending data in this direction)
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700313 NS_LOG_INFO("Before (header->IsNack()) && (pitEntry != m_pit->end ())");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700314 if ((header->IsNack()) && (pitEntry != m_pit->end ()))
315 {
316 //m_pit->erase (pitEntry);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700317 NS_LOG_INFO("TRUE");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700318 m_pit->modify(pitEntry, CcnxPitEntry::DeleteIncoming(incomingFace));
319 }
320
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700321 NS_LOG_INFO("Before WasRecentlySatisfied");
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800322 /*if (m_rit->WasRecentlySatisfied (*header))
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700323 {
324 return;
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800325 }*/
326 if (m_rit->WasRecentlySatisfied (*header))
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700327 {
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800328 NS_LOG_INFO("------------");
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700329 NS_LOG_INFO("Entering WasRecentlySatisfied");
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800330 NS_LOG_INFO("------------");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700331 // duplicate interests (same nonce) from applications are just ignored
332 if (incomingFace->IsLocal() == true)
333 return;
334
335 // Update metric status for the incoming interface in the corresponding FIB entry
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800336 /*if (fibEntry != m_fib->end())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700337 m_fib->modify (m_fib->iterator_to (pitEntry->m_fibEntry),
338 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
339
340 //Trace duplicate interest
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700341 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700342 m_node->GetObject<Ccnx> (), incomingFace);
343
344 bool isMine = false;
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700345 //TypeId tid = TypeId ("ns3::CcnxProducer");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700346 for(uint32_t i=0; i<m_node->GetNApplications();i++)
347 {
348 Ptr<Application> app = m_node->GetApplication(i);
Ilya Moiseenkoc9266042011-11-02 17:49:21 -0700349 NS_LOG_INFO("ApplicationName = " << app->GetTypeId().GetName());
350 if(app->GetTypeId().GetName() == "ns3::CcnxProducer")
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700351 {
352 if((DynamicCast<CcnxProducer>(app))->GetPrefix () == header->GetName ())
353 {
354 isMine = true;
355 break;
356 }
357 }
358 }
359
360 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
361 if ((isMine == true) || (contentObject != NULL))
362 {
363 //never respond with NACK to NACK
364 if(header->IsNack () )
365 return;
366
367 // always return a duplicate packet
368 header->SetNack(true);
369 //Trace duplicate interest
370 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
371 m_node->GetObject<Ccnx> (), incomingFace);
372
373 SendInterest(incomingFace, header, packet->Copy());
374
375 return;
376 }
377
378
379 // check PIT. or there is no outgoing entry for this interface,
380 // silently drop the duplicate packet
381
382 // If no entry found, silently drop
383 if( pitEntry == m_pit->end() )
384 return;
385
386 // If PIT entry timed out, silently drop
387 if( pitEntry->m_timerExpired == true )
388 return;
389
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700390 // loop?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700391
392 // Check if there is no outgoing entry for the interface or different nonce
393 // (i.e., got a duplicate packet, but we haven't sent interest to this
394 // interface)
395 //
396 // This case means that there is a loop in the network.
397 // So, prune this link, but do not remove PIT entry
398
399 // Alex, check this condition!!
400 if(pitEntry->m_outgoing.size () == 0)
401 {
402 //never respond with NACK to NACK
403 if(header->IsNack () )
404 return;
405
406 // always return a duplicate packet
407 header->SetNack(true);
408 //Trace duplicate interest
409 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST,
410 m_node->GetObject<Ccnx> (), incomingFace);
411
412 SendInterest(incomingFace, header, packet->Copy());
413 return;
414 }
415
416
417 // At this point:
418 // - there is a non-expired PIT entry,
419 // - there is an outgoing interest to the interface, and
420 // - a nonce in outgoing entry is equal to a nonce in the received duplicate packet
421
422 // Should perform:
423 // Cleaning outgoing entry
424 // If there are no outgoing interests and available interfaces left (pe->availableInterfaces),
425 // prune all incoming interests, otherwise allow forwarding of the interest
426 if( header->IsNack () )
427 {
428 if( header->IsCongested () == false )
429 m_pit->LeakBucket(incomingFace,1);
430
431 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));
432 }
433 else
434 {
435 //poit->waitingInVain = true;
436 }
437
438
439 // prune all incoming interests
440 if((pitEntry->m_outgoing.size() ==0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
441 {
442 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
443 {
444 if(face.m_face->IsLocal() == false)
445 {
446 // check all entries if the name of RIT entry matches the name of interest
447 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
448 {
449 if (it->m_prefix == pitEntry->GetPrefix() )
450 {
451
452 header->SetNonce(it->m_nonce);
453 header->SetNack(true);
454 SendInterest(face.m_face, header, packet->Copy());
455 break;
456 }
457 }
458 }
459 }
460
461 // Finally, remote the PIT entry
462 m_pit->erase (pitEntry);
463
464 return; // stop processing
465 }
466
467 if(pitEntry->m_fibEntry.m_faces.size() == 0)
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800468 return;*/
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700469 return;
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800470 }
471
472
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700473
474 // Otherwise,
475 // propagate the interest
476 //
477 // method `propagateInterest' can/should try different interface
478 // from `availableInterfaces' list
479
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700480 NS_LOG_INFO("Before SetRecentlySatisfied");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700481 m_rit->SetRecentlySatisfied (*header);
482
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700483 NS_LOG_INFO("Cache Lookup for " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700484 Ptr<Packet> contentObject = m_contentStore->Lookup (header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700485 if (contentObject != NULL)
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700486 {
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700487 NS_LOG_INFO("Found in cache");
488
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700489 TransmittedDataTrace (contentObject, CACHED,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700490 m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700491 incomingFace->Send (contentObject);
492 return;
493 }
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700494
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700495 // Data is not in cache
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700496 NS_LOG_INFO("Before inFace and OutFace");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700497 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry->m_incoming.find (incomingFace);
498 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry->m_outgoing.find (incomingFace);
499
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700500 NS_LOG_INFO("Before (pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false)");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700501 if ((pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false))
502 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700503 NS_LOG_INFO("Entering (pitEntry != m_pit->end()) && (pitEntry->m_timerExpired == false)");
504
505 if(inFace->m_face == NULL)
506 NS_LOG_INFO("in face is null");
507 if(outFace->m_face == NULL)
508 NS_LOG_INFO("outface is null");
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800509 if(outFace == pitEntry->m_outgoing.end())
510 NS_LOG_INFO("OUTFACE = END");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700511
512 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
513 // Give up this interface, but keep a small hope when the returned packet doesn't have PRUNE status
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800514 if(outFace != pitEntry->m_outgoing.end()) // this is correct
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700515 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700516 NS_LOG_INFO("Entering outFace != pitEntry->m_outgoing.end()");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700517 if( header->IsCongested() == true )
518 {
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700519 NS_LOG_INFO("Entering header->IsCongested() == true");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700520 m_pit->LeakBucket(incomingFace, 1);
521 m_pit->modify (pitEntry, CcnxPitEntry::DeleteOutgoing(outFace->m_face));
522 }
523 //else
524 // poit->waitingInVain = true;
525
526 // Update metric status for the incoming interface in the corresponding FIB entry
527 if(fibEntry != m_fib->end())
528 m_fib->modify(m_fib->iterator_to (pitEntry->m_fibEntry),
529 CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
530 }
531 }
532
Ilya Moiseenkob405d9b2011-10-28 16:23:11 -0700533 NS_LOG_INFO("Before (pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0)");
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700534 if((pitEntry->m_outgoing.size() == 0) && (pitEntry->m_fibEntry.m_faces.size() == 0))
535 // prune all incoming interests
536 {
537
538 for(CcnxPitEntryContainer::type::iterator iter = m_pit->begin();
539 iter != m_pit->end();
540 iter++)
541 {
542 /*for(CcnxPitEntryIncomingFaceContainer::type::iterator face = iter->m_incoming.begin();
543 face != iter->m_incoming.end();
544 face++)*/
545 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, iter->m_incoming)
546 {
547 if(face.m_face->IsLocal() == true)
548 {
549 //returnInterestToApp( pkt, -piit->interfaceIndex );
550 //continue;
551 }
552
553 // check all entries if the name of RIT entry matches the name of interest
554 for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
555 {
556 if (it->m_prefix == iter->GetPrefix() )
557 {
558 header->SetNonce(it->m_nonce);
559 header->SetNack(true);
560 SendInterest(face.m_face, header, packet->Copy());
561 }
562 }
563 }
564
565 }
566
567 m_pit->erase(pitEntry);
568
569 return; // there is nothing else to do
570 }
571
572 // Suppress this interest only if we're still expecting data from some other interface
573 if( pitEntry->m_outgoing.size() > 0 )
574 {
575 return; //ok. Now we can suppress this interest
576 }
577
578
579 // Prune and delete PIT entry if there are no available interfaces to propagate interest
580 if( pitEntry->m_fibEntry.m_faces.size() == 0)
581 {
582 //if no match is found in the FIB, drop packet
583 //printf( "Node %d: cannot process Interest packet %s (no interfaces left)\n", _node->nodeId, pkt->contentName );
584
585 if(incomingFace->IsLocal() == false)
586 {
587 header->SetNack(true);
588 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
589 m_node->GetObject<Ccnx> (), incomingFace);
590 SendInterest(incomingFace, header, packet->Copy());
591 }
592
593 m_pit->erase(pitEntry);
594
595 }
596
597
598
599 // otherwise, try one of the available interfaces
600
601 // suppress interest if
602 /*if (pitEntry->m_incoming.size () != 0 && // not a new PIT entry and
603 inFace != pitEntry->m_incoming.end ()) // existing entry, but interest received via different face
604 {
605 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST,
606 m_node->GetObject<Ccnx> (), incomingFace);
607 return;
608 }*/
609
610
611 //just in case of bug
612 header->SetNack(false);
613 header->SetCongested(false);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700614
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800615 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700616
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800617 m_pit->modify (pitEntry, CcnxPitEntry::AddIncoming(incomingFace));
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700618
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800619 bool propagated = m_forwardingStrategy->
620 PropagateInterest (pitEntry, fibEntry,incomingFace, header, packet,
621 MakeCallback (&CcnxL3Protocol::SendInterest, this)
622 );
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700623
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800624 // If interest wasn't propagated further (probably, a limit is reached),
625 // prune and delete PIT entry if there are no outstanding interests.
626 // Stop processing otherwise.
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800627 if( (!propagated) && (pitEntry->m_outgoing.size() == 0)) // this line works
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800628 {
629 BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry->m_incoming)
630 {
631 header->SetNack(true);
632 header->SetCongested(true);
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800633 NS_LOG_INFO("Sending CONGESTION packet");
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800634 SendInterest (face.m_face, header, packet->Copy());
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700635
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800636 m_droppedInterestsTrace (header, DROP_CONGESTION,
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700637 m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800638 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700639
640 m_pit->erase (pitEntry);
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800641 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700642 /*}
643 else
644 {
645 m_droppedInterestsTrace (header, NDN_PIT_TIMER_EXPIRED,
646 m_node->GetObject<Ccnx> (), incomingFace);
647 return;
648 }*/
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700649}
650
651// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700652void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
653 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700654 Ptr<Packet> &payload,
655 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700656{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700657
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700658 NS_LOG_LOGIC ("Receiving contentObject from " << &incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700659 m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700660
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700661 // 1. Lookup PIT entry
662 try
663 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700664 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700665
666 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700667
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700668 // Update metric status for the incoming interface in the corresponding FIB entry
669 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
670 CcnxFibEntry::UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700671
672 // Add or update entry in the content store
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700673 NS_LOG_INFO("Cached " << header->GetName());
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700674 m_contentStore->Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700675
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700676 CcnxPitEntryOutgoingFaceContainer::type::iterator
677 out = pitEntry.m_outgoing.find (incomingFace);
678
679 // If we have sent interest for this data via this face, then update stats.
680 if (out != pitEntry.m_outgoing.end ())
681 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700682 m_pit->modify (m_pit->iterator_to (pitEntry),
683 CcnxPitEntry::EstimateRttAndRemoveFace(out, m_fib));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700684 // face will be removed in the above call
685 }
686 else
687 {
688 NS_LOG_WARN ("Node "<< m_node->GetId() <<
689 ". PIT entry for "<< header->GetName ()<<" is valid, "
690 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
691 }
692
693 //satisfy all pending incoming Interests
694 BOOST_FOREACH (const CcnxPitEntryIncomingFace &interest, pitEntry.m_incoming)
695 {
696 if (interest.m_face == incomingFace) continue;
697
698 // may not work either because of 'const' thing
699 interest.m_face->Send (packet->Copy ()); // unfortunately, we have to copy packet...
700 m_transmittedDataTrace (header, payload, FORWARDED, m_node->GetObject<Ccnx> (), interest.m_face);
701 }
702
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700703 m_pit->modify (m_pit->iterator_to (pitEntry), CcnxPitEntry::ClearIncoming()); // satisfy all incoming interests
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700704
705 if( pitEntry.m_outgoing.size()==0 ) // remove PIT when all outgoing interests are "satisfied"
706 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700707 m_pit->erase (m_pit->iterator_to (pitEntry));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700708 }
709
710 }
711 catch (CcnxPitEntryNotFound)
712 {
713 // 2. Drop data packet if PIT entry is not found
714 // (unsolicited data packets should not "poison" content store)
715
716 //drop dulicated or not requested data packet
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700717 m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700718 return; // do not process unsoliced data packets
719 }
720}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700721
722void
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700723CcnxL3Protocol::SendInterest (const Ptr<CcnxFace> &face,
724 const Ptr<CcnxInterestHeader> &header,
725 const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700726{
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700727 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700728 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
729
730 if (face->IsUp ())
731 {
732 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700733 m_transmittedInterestsTrace (header, m_node->GetObject<Ccnx> (), face);
734 face->Send (packet);
735 }
736 else
737 {
738 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
739 m_droppedInterestsTrace (header, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
740 }
741}
742
743void
744CcnxL3Protocol::SendContentObject (const Ptr<CcnxFace> &face,
745 const Ptr<CcnxContentObjectHeader> &header,
746 const Ptr<Packet> &packet)
747{
748 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
749 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
750
751 NS_ASSERT_MSG (false, "Should not be called for now");
752
753 if (face->IsUp ())
754 {
755 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700756 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700757 face->Send (packet);
758 }
759 else
760 {
761 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700762 // m_dropTrace (packet, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700763 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700764}
765
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700766Ptr<CcnxPit>
767CcnxL3Protocol::GetPit()
768{
769 return m_pit;
770}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800771
772void
773CcnxL3Protocol::ScheduleLeakage()
774{
775 m_pit->LeakBuckets();
776 Time interval = MilliSeconds (NDN_INTEREST_RESET_PERIOD);
777 Simulator::Schedule (interval, &CcnxL3Protocol::ScheduleLeakage, this);
778}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700779} //namespace ns3