blob: d6391c5db330bcdc19c32d5c9372ba8d42fb9925 [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
Ilya Moiseenko1a8be032012-01-18 12:51:09 -0800239Ptr<NetDevice>
240CcnxL3Protocol::GetNetDeviceByFace(Ptr<CcnxFace> face) const
241{
242 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
243 if (netDeviceFace == 0)
244 return 0;
245
246 return netDeviceFace->GetNetDevice ();
247}
248
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700249uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700250CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700251{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700252 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700253}
254
Alexander Afanasyev98256102011-08-14 01:00:02 -0700255// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700256void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700257CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700259 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700261 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700262 try
263 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700264 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700265 switch (type)
266 {
267 case CcnxHeaderHelper::INTEREST:
268 {
269 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700270
271 // Deserialization. Exception may be thrown
272 packet->RemoveHeader (*header);
273 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800274
275 if (header->GetNack () > 0)
276 OnNack (face, header, p/*original packet*/);
277 else
278 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700279 break;
280 }
281 case CcnxHeaderHelper::CONTENT_OBJECT:
282 {
283 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700284
285 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
286
287 // Deserialization. Exception may be thrown
288 packet->RemoveHeader (*header);
289 packet->RemoveTrailer (contentObjectTrailer);
290
291 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700292 break;
293 }
294 }
295
296 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700297 }
298 catch (CcnxUnknownHeaderException)
299 {
300 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
301 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700302}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700303
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800304void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800305CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800306 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800307 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800308{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800309 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800310 m_inNacks (header, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800311
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800312 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
313 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800314 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800315 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800316
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800317 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800318 {
319 // somebody is doing something bad
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800320 m_dropNacks (header, NON_DUPLICATED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800321 return;
322 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800323
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800324 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
325 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800326
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800327 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800328 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800329// NS_ASSERT_MSG (false,
330// "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
331// "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800332
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800333 // m_dropNacks (header, NON_DUPLICATE, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800334 return;
335 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800336
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800337 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
338 // at least when there is only one client
339 //
340 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800341
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800342 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
343
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800344 m_pit->modify (m_pit->iterator_to (pitEntry),
345 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800346
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800347 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
348 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800349
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800350 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
351 {
352 m_pit->modify (m_pit->iterator_to (pitEntry),
353 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
354 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800355
Alexander Afanasyev07827182011-12-13 01:07:32 -0800356 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
357 ll::bind (&CcnxFibEntry::UpdateStatus,
358 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800359
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800360 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
361 {
362 // no need to do anything
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800363 m_dropNacks (header, AFTER_SATISFIED, incomingFace);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800364 return;
365 }
366
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800367 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800368 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800369 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800370 // suppress
371 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800372 m_dropNacks (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800373 return;
374 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800375
376 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800377
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800378 Ptr<Packet> nonNackInterest = Create<Packet> ();
379 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
380 nonNackInterest->AddHeader (*header);
381
382 bool propagated = m_forwardingStrategy->
383 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
384
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800385 // // ForwardingStrategy will try its best to forward packet to at least one interface.
386 // // If no interests was propagated, then there is not other option for forwarding or
387 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800388 if (!propagated)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800389 {
390 m_dropNacks (header, NO_FACES, incomingFace); // this headers doesn't have NACK flag set
391 GiveUpInterest (pitEntry, header);
392 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800393}
394
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700395// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800396//
397// !!! Key point.
398// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700399void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
400 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700401 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700402{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800403 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800404 m_inInterests (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700405
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800406 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800407 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
408 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyev8d11fcc2011-12-30 12:42:31 -0800409 // bool isNew = ret.get<1> ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800410 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800411
Alexander Afanasyev8d11fcc2011-12-30 12:42:31 -0800412 // NS_LOG_DEBUG ("isNew: " << isNew << ", isDup: " << isDuplicated);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800413
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800414 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700415 {
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800416 m_dropInterests (header, DUPLICATED, incomingFace);
417
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800418 /**
419 * This condition will handle "routing" loops and also recently satisfied interests.
420 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
421 * is kept for another small chunk of time.
422 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700423
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800424 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800425 header->SetNack (CcnxInterestHeader::NACK_LOOP);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800426 Ptr<Packet> nack = Create<Packet> ();
427 nack->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700428
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800429 incomingFace->Send (nack);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800430 m_outNacks (header, incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700431
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700432 return;
433 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800434
435 Ptr<Packet> contentObject;
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800436 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
437 Ptr<const Packet> payload; // used for tracing
438 tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800439 if (contentObject != 0)
440 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800441 NS_ASSERT (contentObjectHeader != 0);
442
443 NS_LOG_LOGIC("Found in cache");
444
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800445 incomingFace->Send (contentObject);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800446 m_outData (contentObjectHeader, payload, true, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800447
448 // Set pruning timout on PIT entry (instead of deleting the record)
449 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800450 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
451 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800452
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800453 return;
454 }
455
456 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700457
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700458 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800459 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
460 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
461
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800462 bool isRetransmitted = false;
463
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700465 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800466 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800467
468 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800469 // this is almost definitely a retransmission. But should we trust the user on that?
470 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700471 else
472 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800473 m_pit->modify (m_pit->iterator_to (pitEntry),
474 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800475 }
476
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800477 NS_LOG_DEBUG ("IsRetx: " << isRetransmitted);
478
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800479 // update PIT entry lifetime
480 m_pit->modify (m_pit->iterator_to (pitEntry),
481 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
482 header->GetInterestLifetime ()));
483
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800484 if (outFace != pitEntry.m_outgoing.end ())
485 {
486 // got a non-duplicate interest from the face we have sent interest to
487 // Probably, there is no point in waiting data from that face... Not sure yet
488
489 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
490 // Mark interface YELLOW, but keep a small hope that data will come eventually.
491
492 // ?? not sure if we need to do that ?? ...
493
Alexander Afanasyev07827182011-12-13 01:07:32 -0800494 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
495 ll::bind (&CcnxFibEntry::UpdateStatus,
496 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800497 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800498
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800499 if (!isRetransmitted &&
500 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800501 { // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800502
503 m_dropInterests (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800504 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800505 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800506
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800507 /////////////////////////////////////////////////////////////////////
508 // Propagate
509 /////////////////////////////////////////////////////////////////////
510
511 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
512
513 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800514 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800515
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800516 if (isRetransmitted) //give another chance if retransmitted
517 {
518 // increase max number of allowed retransmissions
519 m_pit->modify (m_pit->iterator_to (pitEntry),
520 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
521
522 // try again
523 propagated = m_forwardingStrategy->
524 PropagateInterest (pitEntry, incomingFace, header, packet);
525 }
526
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800527 // ForwardingStrategy will try its best to forward packet to at least one interface.
528 // If no interests was propagated, then there is not other option for forwarding or
529 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800530 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800531 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800532 NS_LOG_DEBUG ("Not propagated");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800533 m_dropInterests (header, NO_FACES, incomingFace);
534 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800535 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800536}
537
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700538// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800539void
540CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
541 Ptr<CcnxContentObjectHeader> &header,
542 Ptr<Packet> &payload,
543 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700544{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700545
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800546 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800547 m_inData (header, payload, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700548
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700549 // 1. Lookup PIT entry
550 try
551 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700552 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700553
554 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700555
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800556 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700557
558 // If we have sent interest for this data via this face, then update stats.
559 if (out != pitEntry.m_outgoing.end ())
560 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800561 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
562 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
563 ll::_1,
564 incomingFace,
565 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700566 }
567 else
568 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800569 // Unsolicited data, but we're interested in it... should we get it?
570 // Potential hole for attacks
571
572 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
573 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800574 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800575
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800576 // ignore unsolicited data
577 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700578 }
579
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800580 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800581 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
582 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
583 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800584
585 // Add or update entry in the content store
586 m_contentStore->Add (header, payload);
587
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700588 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800589 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700590 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800591 if (incoming.m_face != incomingFace)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800592 {
593 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800594 m_outData (header, payload, false, incoming.m_face);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800595 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700596
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800597 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700598 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800599 // All incoming interests are satisfied. Remove them
600 m_pit->modify (m_pit->iterator_to (pitEntry),
601 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
602
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800603 // Set pruning timout on PIT entry (instead of deleting the record)
604 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800605 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800606 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700607 }
608 catch (CcnxPitEntryNotFound)
609 {
610 // 2. Drop data packet if PIT entry is not found
611 // (unsolicited data packets should not "poison" content store)
612
613 //drop dulicated or not requested data packet
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800614 m_dropData (header, payload, UNSOLICITED, incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700615 return; // do not process unsoliced data packets
616 }
617}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700618
619void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800620CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
621 Ptr<CcnxInterestHeader> header)
622{
623 Ptr<Packet> packet = Create<Packet> ();
624 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
625 packet->AddHeader (*header);
626
627 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
628 {
629 incoming.m_face->Send (packet->Copy ());
630
631 m_outNacks (header, incoming.m_face);
632 }
633 // All incoming interests cannot be satisfied. Remove them
634 m_pit->modify (m_pit->iterator_to (pitEntry),
635 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
636
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800637 // Remove also outgoing
638 m_pit->modify (m_pit->iterator_to (pitEntry),
639 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
640
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800641 // Set pruning timout on PIT entry (instead of deleting the record)
642 m_pit->modify (m_pit->iterator_to (pitEntry),
643 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
644 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
645}
646
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800647// void
648// CcnxL3Protocol::SetBucketLeakInterval (Time interval)
649// {
650// m_bucketLeakInterval = interval;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800651
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800652// if (m_bucketLeakEvent.IsRunning ())
653// m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700654
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800655// m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
656// &CcnxL3Protocol::LeakBuckets, this);
657// }
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700658
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800659// Time
660// CcnxL3Protocol::GetBucketLeakInterval () const
661// {
662// return m_bucketLeakInterval;
663// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700664
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800665// void
666// CcnxL3Protocol::LeakBuckets ()
667// {
668// // NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800669
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800670// BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
671// {
672// face->LeakBucket (m_bucketLeakInterval);
673// }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800674
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800675// m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
676// &CcnxL3Protocol::LeakBuckets,
677// this);
678// }
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800679
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700680} //namespace ns3