blob: 7f5f1958eb7522ac684801c5123fa5fda9e18a5b [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 Afanasyev09c7deb2011-11-23 14:50:10 -0800426 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800427 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
428 if (contentObject != 0)
429 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800430 NS_ASSERT (contentObjectHeader != 0);
431
432 NS_LOG_LOGIC("Found in cache");
433
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800434 incomingFace->Send (contentObject);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800435 m_outData (contentObjectHeader, true, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800436
437 // Set pruning timout on PIT entry (instead of deleting the record)
438 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800439 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
440 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800441
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800442 return;
443 }
444
445 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700446
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700447 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800448 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
449 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
450
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800451 bool isRetransmitted = false;
452
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800453 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700454 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800455 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800456
457 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800458 // this is almost definitely a retransmission. But should we trust the user on that?
459 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700460 else
461 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800462 m_pit->modify (m_pit->iterator_to (pitEntry),
463 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 }
465
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800466 NS_LOG_DEBUG ("IsRetx: " << isRetransmitted);
467
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800468 // update PIT entry lifetime
469 m_pit->modify (m_pit->iterator_to (pitEntry),
470 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
471 header->GetInterestLifetime ()));
472
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800473 if (outFace != pitEntry.m_outgoing.end ())
474 {
475 // got a non-duplicate interest from the face we have sent interest to
476 // Probably, there is no point in waiting data from that face... Not sure yet
477
478 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
479 // Mark interface YELLOW, but keep a small hope that data will come eventually.
480
481 // ?? not sure if we need to do that ?? ...
482
Alexander Afanasyev07827182011-12-13 01:07:32 -0800483 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
484 ll::bind (&CcnxFibEntry::UpdateStatus,
485 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800486 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800487
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800488 if (!isRetransmitted &&
489 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800490 { // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800491
492 m_dropInterests (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800493 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800494 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800495
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800496 /////////////////////////////////////////////////////////////////////
497 // Propagate
498 /////////////////////////////////////////////////////////////////////
499
500 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
501
502 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800503 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800504
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800505 if (isRetransmitted) //give another chance if retransmitted
506 {
507 // increase max number of allowed retransmissions
508 m_pit->modify (m_pit->iterator_to (pitEntry),
509 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
510
511 // try again
512 propagated = m_forwardingStrategy->
513 PropagateInterest (pitEntry, incomingFace, header, packet);
514 }
515
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800516 // ForwardingStrategy will try its best to forward packet to at least one interface.
517 // If no interests was propagated, then there is not other option for forwarding or
518 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800519 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800520 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800521 NS_LOG_DEBUG ("Not propagated");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800522 m_dropInterests (header, NO_FACES, incomingFace);
523 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800524 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800525}
526
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700527// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800528void
529CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
530 Ptr<CcnxContentObjectHeader> &header,
531 Ptr<Packet> &payload,
532 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700533{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700534
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800535 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800536 m_inData (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700537
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700538 // 1. Lookup PIT entry
539 try
540 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700541 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700542
543 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700544
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800545 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700546
547 // If we have sent interest for this data via this face, then update stats.
548 if (out != pitEntry.m_outgoing.end ())
549 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800550 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
551 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
552 ll::_1,
553 incomingFace,
554 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700555 }
556 else
557 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800558 // Unsolicited data, but we're interested in it... should we get it?
559 // Potential hole for attacks
560
561 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
562 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800563 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800564
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800565 // ignore unsolicited data
566 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700567 }
568
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800569 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800570 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
571 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
572 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800573
574 // Add or update entry in the content store
575 m_contentStore->Add (header, payload);
576
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700577 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800578 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700579 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800580 if (incoming.m_face != incomingFace)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800581 {
582 incoming.m_face->Send (packet->Copy ());
583 m_outData (header, false, incoming.m_face);
584 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700585
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800586 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700587 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800588 // All incoming interests are satisfied. Remove them
589 m_pit->modify (m_pit->iterator_to (pitEntry),
590 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
591
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800592 // Set pruning timout on PIT entry (instead of deleting the record)
593 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800594 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800595 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700596 }
597 catch (CcnxPitEntryNotFound)
598 {
599 // 2. Drop data packet if PIT entry is not found
600 // (unsolicited data packets should not "poison" content store)
601
602 //drop dulicated or not requested data packet
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800603 m_dropData (header, UNSOLICITED, incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700604 return; // do not process unsoliced data packets
605 }
606}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700607
608void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800609CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
610 Ptr<CcnxInterestHeader> header)
611{
612 Ptr<Packet> packet = Create<Packet> ();
613 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
614 packet->AddHeader (*header);
615
616 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
617 {
618 incoming.m_face->Send (packet->Copy ());
619
620 m_outNacks (header, incoming.m_face);
621 }
622 // All incoming interests cannot be satisfied. Remove them
623 m_pit->modify (m_pit->iterator_to (pitEntry),
624 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
625
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800626 // Remove also outgoing
627 m_pit->modify (m_pit->iterator_to (pitEntry),
628 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
629
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800630 // Set pruning timout on PIT entry (instead of deleting the record)
631 m_pit->modify (m_pit->iterator_to (pitEntry),
632 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
633 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
634}
635
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800636// void
637// CcnxL3Protocol::SetBucketLeakInterval (Time interval)
638// {
639// m_bucketLeakInterval = interval;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800640
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800641// if (m_bucketLeakEvent.IsRunning ())
642// m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700643
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800644// m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
645// &CcnxL3Protocol::LeakBuckets, this);
646// }
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700647
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800648// Time
649// CcnxL3Protocol::GetBucketLeakInterval () const
650// {
651// return m_bucketLeakInterval;
652// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700653
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800654// void
655// CcnxL3Protocol::LeakBuckets ()
656// {
657// // NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800658
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800659// BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
660// {
661// face->LeakBucket (m_bucketLeakInterval);
662// }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800663
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800664// m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
665// &CcnxL3Protocol::LeakBuckets,
666// this);
667// }
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800668
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700669} //namespace ns3