blob: b719e99c088a4f3c8b9db0ddf2a96a3490b38115 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -07002/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenko172763c2011-10-28 13:21:53 -070019 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070020 */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070021
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070022#include "ccnx-l3-protocol.h"
23
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070024#include "ns3/packet.h"
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070025#include "ns3/node.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/log.h"
27#include "ns3/callback.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028#include "ns3/uinteger.h"
29#include "ns3/trace-source-accessor.h"
30#include "ns3/object-vector.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031#include "ns3/boolean.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070033#include "ns3/ccnx-header-helper.h"
34
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070035#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070036#include "ccnx-forwarding-strategy.h"
37#include "ccnx-interest-header.h"
38#include "ccnx-content-object-header.h"
39
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080040#include "ccnx-net-device-face.h"
41
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070042#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080043#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080044#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080045
46using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080047namespace ll = boost::lambda;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048
49NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
50
51namespace ns3 {
52
Alexander Afanasyev7112f482011-08-17 14:05:57 -070053const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054
55NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
56
57TypeId
58CcnxL3Protocol::GetTypeId (void)
59{
60 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
61 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070062 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev7112f482011-08-17 14:05:57 -070064 // .AddTraceSource ("Tx", "Send ccnx packet to outgoing interface.",
65 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_txTrace))
66 // .AddTraceSource ("Rx", "Receive ccnx packet from incoming interface.",
67 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_rxTrace))
68 // .AddTraceSource ("Drop", "Drop ccnx packet",
69 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_dropTrace))
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070070 // .AddAttribute ("InterfaceList", "The set of Ccnx interfaces associated to this Ccnx stack.",
71 // ObjectVectorValue (),
72 // MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
73 // MakeObjectVectorChecker<CcnxFace> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074
Alexander Afanasyev7112f482011-08-17 14:05:57 -070075 // .AddTraceSource ("SendOutgoing", "A newly-generated packet by this node is about to be queued for transmission",
76 // MakeTraceSourceAccessor (&CcnxL3Protocol::m_sendOutgoingTrace))
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077
78 ;
79 return tid;
80}
81
82CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070083: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084{
85 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070086
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070087 m_pit = CreateObject<CcnxPit> ();
88 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089}
90
91CcnxL3Protocol::~CcnxL3Protocol ()
92{
93 NS_LOG_FUNCTION (this);
94}
95
96void
97CcnxL3Protocol::SetNode (Ptr<Node> node)
98{
99 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700100 m_fib = m_node->GetObject<CcnxFib> ();
101 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
102
103 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700104}
105
106/*
107 * This method is called by AddAgregate and completes the aggregation
108 * by setting the node in the ccnx stack
109 */
110void
111CcnxL3Protocol::NotifyNewAggregate ()
112{
113 if (m_node == 0)
114 {
115 Ptr<Node>node = this->GetObject<Node>();
116 // verify that it's a valid node and that
117 // the node has not been set before
118 if (node != 0)
119 {
120 this->SetNode (node);
121 }
122 }
123 Object::NotifyNewAggregate ();
124}
125
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700126void
127CcnxL3Protocol::DoDispose (void)
128{
129 NS_LOG_FUNCTION (this);
130
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700131 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700132 {
133 *i = 0;
134 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700135 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700136 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800137
138 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800139 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800140 m_pit = 0;
141 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800142 m_fib = 0;
143
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700144 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700145 Object::DoDispose ();
146}
147
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700148void
149CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
150{
151 NS_LOG_FUNCTION (this);
152 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700153 // m_forwardingStrategy->SetCcnx (this);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700154}
155
156Ptr<CcnxForwardingStrategy>
157CcnxL3Protocol::GetForwardingStrategy (void) const
158{
159 return m_forwardingStrategy;
160}
161
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700163CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700164{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700165 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700167 face->SetNode (m_node);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700168 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700169
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700170 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700171 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700173 m_faces.push_back (face);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700174 m_faceCounter ++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700175 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176}
177
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178void
179CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
180{
181 // ask face to register in lower-layer stack
182 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
183 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
184 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
185 m_faces.erase (face_it);
186}
187
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700188Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700189CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700190{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700191 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 -0700192 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700193 if (face->GetId () == index)
194 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195 }
196 return 0;
197}
198
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800199Ptr<CcnxFace>
200CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
201{
202 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
203 {
204 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
205 if (netDeviceFace == 0) continue;
206
207 if (netDeviceFace->GetNetDevice () == netDevice)
208 return face;
209 }
210 return 0;
211}
212
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700214CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700216 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217}
218
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700219void
220CcnxL3Protocol::TransmittedDataTrace (Ptr<Packet> packet,
221 ContentObjectSource type,
222 Ptr<Ccnx> ccnx, Ptr<const CcnxFace> face)
223{
224 // a "small" inefficiency for logging purposes
225 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
226 static CcnxContentObjectTail tail;
227 packet->RemoveHeader (*header);
228 packet->RemoveTrailer (tail);
229
230 m_transmittedDataTrace (header, packet/*payload*/, type, ccnx, face);
231
232 packet->AddHeader (*header);
233 packet->AddTrailer (tail);
234}
235
236
Alexander Afanasyev98256102011-08-14 01:00:02 -0700237// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700238void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700239CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700240{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700241 if (!face->IsUp ())
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700242 {
243 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700244 // m_dropTrace (p, INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700245 return;
246 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700247 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700248
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700249 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700250 try
251 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700252 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700253 switch (type)
254 {
255 case CcnxHeaderHelper::INTEREST:
256 {
257 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700258
259 // Deserialization. Exception may be thrown
260 packet->RemoveHeader (*header);
261 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800262
263 if (header->GetNack () > 0)
264 OnNack (face, header, p/*original packet*/);
265 else
266 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700267 break;
268 }
269 case CcnxHeaderHelper::CONTENT_OBJECT:
270 {
271 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700272
273 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
274
275 // Deserialization. Exception may be thrown
276 packet->RemoveHeader (*header);
277 packet->RemoveTrailer (contentObjectTrailer);
278
279 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700280 break;
281 }
282 }
283
284 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700285 }
286 catch (CcnxUnknownHeaderException)
287 {
288 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
289 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700290}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700291
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800292void
293CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &face,
294 Ptr<CcnxInterestHeader> &header,
295 const Ptr<const Packet> &p)
296{
297 NS_LOG_FUNCTION (face << header << p);
298
299 // Huh... Ignore all this for now
300
301 /*if( header->IsCongested () == false )
302 m_pit->LeakBucket(incomingFace,1);
303
304
305 m_droppedInterestsTrace (header, DROP_CONGESTION,
306 m_node->GetObject<Ccnx> (), incomingFace);
307
308 m_pit->modify(pitEntry, CcnxPitEntry::DeleteOutgoing(incomingFace));*/
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)
313
314 // NS_LOG_INFO("Before (header->IsNack()) && (pitEntry != m_pit->end ())");
315 // if ((header->IsNack()) && (pitEntry != m_pit->end ()))
316 // {
317 // //m_pit->erase (pitEntry);
318 // NS_LOG_INFO("TRUE");
319 // m_pit->modify(pitEntry, CcnxPitEntry::DeleteIncoming(incomingFace));
320 // }
321
322 // m_fib->modify (m_fib->iterator_to (pitEntry->m_fibEntry),
323 // CcnxFibEntry::UpdateStatus(incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
324
325 // if (!pitEntry.AreThereMoreFacesToTry ())
326 // {
327 // BOOST_FOREACH (const CcnxPitEntryIncomingFace face, pitEntry.m_incoming)
328 // {
329 // // check all entries if the name of RIT entry matches the name of interest
330 // for (CcnxRitByNonce::type::iterator it = m_rit->begin(); it != m_rit->end(); it++)
331 // {
332 // if (it->m_prefix == iter->GetPrefix() )
333 // {
334 // header->SetNonce(it->m_nonce);
335 // header->SetNack(true);
336 // SendInterest(face.m_face, header, packet->Copy());
337 // }
338 // }
339 // }
340
341 // m_pit->erase(pitEntry);
342
343 // return;
344 // }
345}
346
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700347// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800348//
349// !!! Key point.
350// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700351void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
352 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700353 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700354{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800355 NS_LOG_FUNCTION (incomingFace << header << packet);
356 // m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700357
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800358 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800359 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
360 CcnxPitEntry const& pitEntry = ret.get<0> ();
361 // bool isNew = ret.get<1> ();
362 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800363
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800364 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700365 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800366 /**
367 * This condition will handle "routing" loops and also recently satisfied interests.
368 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
369 * is kept for another small chunk of time.
370 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700371
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800372 // //Trace duplicate interest
373 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700374
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800375 header->SetNack (CcnxInterestHeader::NACK_LOOP);
376 Ptr<Packet> packet = Create<Packet> ();
377 packet->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700378
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800379 SendInterest (incomingFace, header, packet);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700380
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800381 // //Trace duplicate interest
382 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700383 return;
384 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800385
386 Ptr<Packet> contentObject;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800387 Ptr<const CcnxContentObjectHeader> contentObjectHeader;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800388 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
389 if (contentObject != 0)
390 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800391 // NS_ASSERT_MSG (pitEntry.m_incoming.size () == 0,
392 // "Something strange. Data is cached, but size of incoming interests is not zero...");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800393 NS_ASSERT (contentObjectHeader != 0);
394
395 NS_LOG_LOGIC("Found in cache");
396
397 // TransmittedDataTrace (contentObject, CACHED,
398 // m_node->GetObject<Ccnx> (), incomingFace);
399 SendContentObject (incomingFace, contentObjectHeader, contentObject);
400
401 // Set pruning timout on PIT entry (instead of deleting the record)
402 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800403 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
404 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800405 return;
406 }
407
408 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700409
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700410 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800411 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
412 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
413
414 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700415 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800416 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700417
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800418 // this is almost definitely a retransmission. But should we trust the user on that?
419 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700420 else
421 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800422 m_pit->modify (m_pit->iterator_to (pitEntry),
423 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800424 }
425
426 if (outFace != pitEntry.m_outgoing.end ())
427 {
428 // got a non-duplicate interest from the face we have sent interest to
429 // Probably, there is no point in waiting data from that face... Not sure yet
430
431 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
432 // Mark interface YELLOW, but keep a small hope that data will come eventually.
433
434 // ?? not sure if we need to do that ?? ...
435
436 m_fib->modify(m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800437 ll::bind (&CcnxFibEntry::UpdateStatus,
438 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800439
440 // suppress?
441 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800442 else if (pitEntry.m_outgoing.size() > 0) // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800443
444 {
445 // We are already expecting data later in future. Suppress the interest
446 // m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
447 return;
448 }
449
450 /////////////////////////////////////////////////////////////////////
451 // Propagate
452 /////////////////////////////////////////////////////////////////////
453
454 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
455
456 bool propagated = m_forwardingStrategy->
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800457 PropagateInterest (pitEntry, incomingFace, header, packet,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800458 MakeCallback (&CcnxL3Protocol::SendInterest, this)
459 );
460
461 // ForwardingStrategy will try its best to forward packet to at least one interface.
462 // If no interests was propagated, then there is not other option for forwarding or
463 // ForwardingStrategy failed to find it.
464 if (!propagated)
465 {
466 Ptr<Packet> packet = Create<Packet> ();
467 header->SetNack (CcnxInterestHeader::NACK_CONGESTION);
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800468 packet->AddHeader (*header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800469
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800470 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800471 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800472 SendInterest (incoming.m_face, header, packet->Copy ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800473
474 // m_droppedInterestsTrace (header, DROP_CONGESTION,
475 // m_node->GetObject<Ccnx> (), incomingFace);
476 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800477 // All incoming interests cannot be satisfied. Remove them
478 m_pit->modify (m_pit->iterator_to (pitEntry),
479 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800480
481 // Set pruning timout on PIT entry (instead of deleting the record)
482 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800483 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
484 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800485 }
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700486}
487
488// Processing ContentObjects
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700489void CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
490 Ptr<CcnxContentObjectHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700491 Ptr<Packet> &payload,
492 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700493{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700494
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800495 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800496 // m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700497
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700498 // 1. Lookup PIT entry
499 try
500 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700501 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700502
503 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700504
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700505 // Update metric status for the incoming interface in the corresponding FIB entry
506 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800507 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
508 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700509
510 // Add or update entry in the content store
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700511 m_contentStore->Add (header, payload);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700512
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800513 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700514
515 // If we have sent interest for this data via this face, then update stats.
516 if (out != pitEntry.m_outgoing.end ())
517 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800518 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800519 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
520 ll::_1,
521 incomingFace,
522 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700523 }
524 else
525 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800526 // Unsolicited data, but we're interested in it... should we get it?
527 // Potential hole for attacks
528
529 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
530 ". PIT entry for "<< header->GetName ()<<" is valid, "
531 "but outgoing entry for interface "<< incomingFace <<" doesn't exist\n");
532
533 NS_ASSERT (false); // temporary put false here
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700534 }
535
536 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800537 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700538 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800539 if (incoming.m_face != incomingFace)
540 SendContentObject (incoming.m_face, header, packet->Copy ());
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700541
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800542 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700543 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800544 // All incoming interests are satisfied. Remove them
545 m_pit->modify (m_pit->iterator_to (pitEntry),
546 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
547
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800548 // Set pruning timout on PIT entry (instead of deleting the record)
549 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800550 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800551 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700552 }
553 catch (CcnxPitEntryNotFound)
554 {
555 // 2. Drop data packet if PIT entry is not found
556 // (unsolicited data packets should not "poison" content store)
557
558 //drop dulicated or not requested data packet
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800559 // m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700560 return; // do not process unsoliced data packets
561 }
562}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700563
564void
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700565CcnxL3Protocol::SendInterest (const Ptr<CcnxFace> &face,
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800566 const Ptr<const CcnxInterestHeader> &header,
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700567 const Ptr<Packet> &packet)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700568{
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700569 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700570 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
571
572 if (face->IsUp ())
573 {
574 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800575 // m_transmittedInterestsTrace (header, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700576 face->Send (packet);
577 }
578 else
579 {
580 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800581 // m_droppedInterestsTrace (header, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700582 }
583}
584
585void
586CcnxL3Protocol::SendContentObject (const Ptr<CcnxFace> &face,
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800587 const Ptr<const CcnxContentObjectHeader> &header,
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700588 const Ptr<Packet> &packet)
589{
590 NS_LOG_FUNCTION (this << "packet: " << &packet << ", face: "<< &face);
591 NS_ASSERT_MSG (face != 0, "Face should never be NULL");
592
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700593 if (face->IsUp ())
594 {
595 NS_LOG_LOGIC ("Sending via face " << &face); //
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700596 // m_txTrace (packet, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700597 face->Send (packet);
598 }
599 else
600 {
601 NS_LOG_LOGIC ("Dropping -- outgoing interface is down: " << &face);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700602 // m_dropTrace (packet, INTERFACE_DOWN, m_node->GetObject<Ccnx> (), face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700603 }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700604}
605
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700606Ptr<CcnxPit>
607CcnxL3Protocol::GetPit()
608{
609 return m_pit;
610}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800611
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800612// void
613// CcnxL3Protocol::ScheduleLeakage()
614// {
615// m_pit->LeakBuckets();
616// Time interval = MilliSeconds (NDN_INTEREST_RESET_PERIOD);
617
618// Simulator::Schedule (interval, &CcnxL3Protocol::ScheduleLeakage, this);
619// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700620} //namespace ns3