blob: 2e55164d6cff8b91669835787cf8e4cffa3ff595 [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 Afanasyevcbe92ae2011-12-16 13:06:18 -080031#include "ns3/pointer.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032#include "ns3/boolean.h"
Alexander Afanasyev07827182011-12-13 01:07:32 -080033#include "ns3/string.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080034#include "ns3/simulator.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070036#include "ns3/ccnx-header-helper.h"
37
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070038#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070039#include "ccnx-forwarding-strategy.h"
40#include "ccnx-interest-header.h"
41#include "ccnx-content-object-header.h"
42
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080043#include "ccnx-net-device-face.h"
44
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070045#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080046#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080047#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080048
49using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080050namespace ll = boost::lambda;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070051
52NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
53
54namespace ns3 {
55
Alexander Afanasyev7112f482011-08-17 14:05:57 -070056const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057
Alexander Afanasyev07827182011-12-13 01:07:32 -080058
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
60
61TypeId
62CcnxL3Protocol::GetTypeId (void)
63{
64 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
65 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070066 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080068 .AddAttribute ("FaceList", "List of faces associated with CCNx stack",
69 ObjectVectorValue (),
70 MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
71 MakeObjectVectorChecker<CcnxFace> ())
72
73 .AddAttribute ("ForwardingStrategy", "Forwarding strategy used by CCNx stack",
74 PointerValue (),
75 MakePointerAccessor (&CcnxL3Protocol::SetForwardingStrategy, &CcnxL3Protocol::GetForwardingStrategy),
76 MakePointerChecker<CcnxForwardingStrategy> ())
77
Alexander Afanasyev4975f732011-12-20 17:52:19 -080078 // .AddAttribute ("BucketLeakInterval",
79 // "Interval to leak buckets",
80 // StringValue ("100ms"),
81 // MakeTimeAccessor (&CcnxL3Protocol::GetBucketLeakInterval,
82 // &CcnxL3Protocol::SetBucketLeakInterval),
83 // MakeTimeChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084 ;
85 return tid;
86}
87
88CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070089: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070090{
91 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070092
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070093 m_pit = CreateObject<CcnxPit> ();
94 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070095}
96
97CcnxL3Protocol::~CcnxL3Protocol ()
98{
99 NS_LOG_FUNCTION (this);
100}
101
102void
103CcnxL3Protocol::SetNode (Ptr<Node> node)
104{
105 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700106 m_fib = m_node->GetObject<CcnxFib> ();
107 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
108
109 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700110}
111
112/*
113 * This method is called by AddAgregate and completes the aggregation
114 * by setting the node in the ccnx stack
115 */
116void
117CcnxL3Protocol::NotifyNewAggregate ()
118{
119 if (m_node == 0)
120 {
121 Ptr<Node>node = this->GetObject<Node>();
122 // verify that it's a valid node and that
123 // the node has not been set before
124 if (node != 0)
125 {
126 this->SetNode (node);
127 }
128 }
129 Object::NotifyNewAggregate ();
130}
131
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700132void
133CcnxL3Protocol::DoDispose (void)
134{
135 NS_LOG_FUNCTION (this);
136
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700137 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700138 {
139 *i = 0;
140 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700141 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700142 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800143
144 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800145 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800146 m_pit = 0;
147 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800148 m_fib = 0;
149
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700150 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700151 Object::DoDispose ();
152}
153
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700154void
155CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
156{
157 NS_LOG_FUNCTION (this);
158 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800159 m_forwardingStrategy->SetPit (m_pit);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700160}
161
162Ptr<CcnxForwardingStrategy>
163CcnxL3Protocol::GetForwardingStrategy (void) const
164{
165 return m_forwardingStrategy;
166}
167
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700169CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700171 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700173 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700175 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700176 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700177
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700178 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700180 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700181}
182
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183void
184CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
185{
186 // ask face to register in lower-layer stack
187 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800188
189 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800190 std::list<boost::reference_wrapper<const CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800191 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
192 {
193 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800194 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800195
196 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
197 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800198 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
199 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800200 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800201 entriesToRemoves.push_back (boost::cref (pitEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800202 }
203 }
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800204 BOOST_FOREACH (const CcnxPitEntry &removedEntry, entriesToRemoves)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800205 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800206 m_pit->erase (m_pit->iterator_to (removedEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800207 }
208
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
210 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
211 m_faces.erase (face_it);
212}
213
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700214Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700215CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700217 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 -0700218 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700219 if (face->GetId () == index)
220 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700221 }
222 return 0;
223}
224
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800225Ptr<CcnxFace>
226CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
227{
228 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
229 {
230 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
231 if (netDeviceFace == 0) continue;
232
233 if (netDeviceFace->GetNetDevice () == netDevice)
234 return face;
235 }
236 return 0;
237}
238
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700239uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700240CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700241{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700242 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243}
244
Alexander Afanasyev98256102011-08-14 01:00:02 -0700245// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700246void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700247CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700248{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700251 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700252 try
253 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700254 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700255 switch (type)
256 {
257 case CcnxHeaderHelper::INTEREST:
258 {
259 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700260
261 // Deserialization. Exception may be thrown
262 packet->RemoveHeader (*header);
263 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800264
265 if (header->GetNack () > 0)
266 OnNack (face, header, p/*original packet*/);
267 else
268 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700269 break;
270 }
271 case CcnxHeaderHelper::CONTENT_OBJECT:
272 {
273 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700274
275 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
276
277 // Deserialization. Exception may be thrown
278 packet->RemoveHeader (*header);
279 packet->RemoveTrailer (contentObjectTrailer);
280
281 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700282 break;
283 }
284 }
285
286 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700287 }
288 catch (CcnxUnknownHeaderException)
289 {
290 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
291 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700292}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700293
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800294void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800295CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800296 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800297 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800298{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800299 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800300 m_inNacks (header, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800301
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800302 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
303 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800304 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800305 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800306
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800307 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800308 {
309 // somebody is doing something bad
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800310 m_dropNacks (header, NON_DUPLICATED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800311 return;
312 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800313
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800314 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
315 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800316
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800317 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800318 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800319// NS_ASSERT_MSG (false,
320// "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
321// "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800322
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800323 // m_dropNacks (header, NON_DUPLICATE, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800324 return;
325 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800326
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800327 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
328 // at least when there is only one client
329 //
330 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800331
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800332 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
333
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800334 m_pit->modify (m_pit->iterator_to (pitEntry),
335 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800336
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800337 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
338 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800339
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800340 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
341 {
342 m_pit->modify (m_pit->iterator_to (pitEntry),
343 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
344 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800345
Alexander Afanasyev07827182011-12-13 01:07:32 -0800346 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
347 ll::bind (&CcnxFibEntry::UpdateStatus,
348 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800349
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800350 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
351 {
352 // no need to do anything
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800353 m_dropNacks (header, AFTER_SATISFIED, incomingFace);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800354 return;
355 }
356
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800357 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800358 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800359 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800360 // suppress
361 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800362 m_dropNacks (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800363 return;
364 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800365
366 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800367
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800368 Ptr<Packet> nonNackInterest = Create<Packet> ();
369 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
370 nonNackInterest->AddHeader (*header);
371
372 bool propagated = m_forwardingStrategy->
373 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
374
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800375 // // ForwardingStrategy will try its best to forward packet to at least one interface.
376 // // If no interests was propagated, then there is not other option for forwarding or
377 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800378 if (!propagated)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800379 {
380 m_dropNacks (header, NO_FACES, incomingFace); // this headers doesn't have NACK flag set
381 GiveUpInterest (pitEntry, header);
382 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800383}
384
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700385// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800386//
387// !!! Key point.
388// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700389void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
390 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700391 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700392{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800393 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800394 m_inInterests (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700395
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800396 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800397 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
398 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800399 bool isNew = ret.get<1> ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800400 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800401
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800402 NS_LOG_DEBUG ("isNew: " << isNew << ", isDup: " << isDuplicated);
403
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800404 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700405 {
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800406 m_dropInterests (header, DUPLICATED, incomingFace);
407
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800408 /**
409 * This condition will handle "routing" loops and also recently satisfied interests.
410 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
411 * is kept for another small chunk of time.
412 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700413
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800414 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800415 header->SetNack (CcnxInterestHeader::NACK_LOOP);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800416 Ptr<Packet> nack = Create<Packet> ();
417 nack->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700418
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800419 incomingFace->Send (nack);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800420 m_outNacks (header, incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700421
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700422 return;
423 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800424
425 Ptr<Packet> contentObject;
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800426 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
427 Ptr<const Packet> payload; // used for tracing
428 tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800429 if (contentObject != 0)
430 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800431 NS_ASSERT (contentObjectHeader != 0);
432
433 NS_LOG_LOGIC("Found in cache");
434
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800435 incomingFace->Send (contentObject);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800436 m_outData (contentObjectHeader, payload, true, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800437
438 // Set pruning timout on PIT entry (instead of deleting the record)
439 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800440 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
441 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800442
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800443 return;
444 }
445
446 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700447
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700448 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800449 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
450 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
451
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800452 bool isRetransmitted = false;
453
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800454 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700455 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800456 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800457
458 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800459 // this is almost definitely a retransmission. But should we trust the user on that?
460 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700461 else
462 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800463 m_pit->modify (m_pit->iterator_to (pitEntry),
464 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800465 }
466
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800467 NS_LOG_DEBUG ("IsRetx: " << isRetransmitted);
468
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800469 // update PIT entry lifetime
470 m_pit->modify (m_pit->iterator_to (pitEntry),
471 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
472 header->GetInterestLifetime ()));
473
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800474 if (outFace != pitEntry.m_outgoing.end ())
475 {
476 // got a non-duplicate interest from the face we have sent interest to
477 // Probably, there is no point in waiting data from that face... Not sure yet
478
479 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
480 // Mark interface YELLOW, but keep a small hope that data will come eventually.
481
482 // ?? not sure if we need to do that ?? ...
483
Alexander Afanasyev07827182011-12-13 01:07:32 -0800484 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
485 ll::bind (&CcnxFibEntry::UpdateStatus,
486 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800487 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800488
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800489 if (!isRetransmitted &&
490 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800491 { // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800492
493 m_dropInterests (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800494 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800495 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800496
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800497 /////////////////////////////////////////////////////////////////////
498 // Propagate
499 /////////////////////////////////////////////////////////////////////
500
501 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
502
503 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800504 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800505
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800506 if (isRetransmitted) //give another chance if retransmitted
507 {
508 // increase max number of allowed retransmissions
509 m_pit->modify (m_pit->iterator_to (pitEntry),
510 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
511
512 // try again
513 propagated = m_forwardingStrategy->
514 PropagateInterest (pitEntry, incomingFace, header, packet);
515 }
516
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800517 // ForwardingStrategy will try its best to forward packet to at least one interface.
518 // If no interests was propagated, then there is not other option for forwarding or
519 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800520 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800521 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800522 NS_LOG_DEBUG ("Not propagated");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800523 m_dropInterests (header, NO_FACES, incomingFace);
524 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800525 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800526}
527
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700528// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800529void
530CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
531 Ptr<CcnxContentObjectHeader> &header,
532 Ptr<Packet> &payload,
533 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700534{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700535
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800536 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800537 m_inData (header, payload, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700538
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700539 // 1. Lookup PIT entry
540 try
541 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700542 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700543
544 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700545
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800546 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700547
548 // If we have sent interest for this data via this face, then update stats.
549 if (out != pitEntry.m_outgoing.end ())
550 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800551 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
552 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
553 ll::_1,
554 incomingFace,
555 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700556 }
557 else
558 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800559 // Unsolicited data, but we're interested in it... should we get it?
560 // Potential hole for attacks
561
562 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
563 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800564 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800565
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800566 // ignore unsolicited data
567 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700568 }
569
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800570 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800571 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
572 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
573 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800574
575 // Add or update entry in the content store
576 m_contentStore->Add (header, payload);
577
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700578 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800579 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700580 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800581 if (incoming.m_face != incomingFace)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800582 {
583 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800584 m_outData (header, payload, false, incoming.m_face);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800585 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700586
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800587 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700588 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800589 // All incoming interests are satisfied. Remove them
590 m_pit->modify (m_pit->iterator_to (pitEntry),
591 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
592
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800593 // Set pruning timout on PIT entry (instead of deleting the record)
594 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800595 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800596 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700597 }
598 catch (CcnxPitEntryNotFound)
599 {
600 // 2. Drop data packet if PIT entry is not found
601 // (unsolicited data packets should not "poison" content store)
602
603 //drop dulicated or not requested data packet
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800604 m_dropData (header, payload, UNSOLICITED, incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700605 return; // do not process unsoliced data packets
606 }
607}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700608
609void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800610CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
611 Ptr<CcnxInterestHeader> header)
612{
613 Ptr<Packet> packet = Create<Packet> ();
614 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
615 packet->AddHeader (*header);
616
617 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
618 {
619 incoming.m_face->Send (packet->Copy ());
620
621 m_outNacks (header, incoming.m_face);
622 }
623 // All incoming interests cannot be satisfied. Remove them
624 m_pit->modify (m_pit->iterator_to (pitEntry),
625 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
626
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800627 // Remove also outgoing
628 m_pit->modify (m_pit->iterator_to (pitEntry),
629 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
630
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800631 // Set pruning timout on PIT entry (instead of deleting the record)
632 m_pit->modify (m_pit->iterator_to (pitEntry),
633 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
634 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
635}
636
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800637// void
638// CcnxL3Protocol::SetBucketLeakInterval (Time interval)
639// {
640// m_bucketLeakInterval = interval;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800641
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800642// if (m_bucketLeakEvent.IsRunning ())
643// m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700644
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800645// m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
646// &CcnxL3Protocol::LeakBuckets, this);
647// }
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700648
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800649// Time
650// CcnxL3Protocol::GetBucketLeakInterval () const
651// {
652// return m_bucketLeakInterval;
653// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700654
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800655// void
656// CcnxL3Protocol::LeakBuckets ()
657// {
658// // NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800659
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800660// BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
661// {
662// face->LeakBucket (m_bucketLeakInterval);
663// }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800664
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800665// m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
666// &CcnxL3Protocol::LeakBuckets,
667// this);
668// }
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800669
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700670} //namespace ns3