blob: 87c0881c998a9606c25fd7f45fa777e461760a8b [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 Afanasyevbab81b12012-02-04 14:20:09 -080078 .AddAttribute ("EnableNACKs", "Enabling support of NACKs",
79 BooleanValue (true),
80 MakeBooleanAccessor (&CcnxL3Protocol::m_nacksEnabled),
81 MakeBooleanChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070082 ;
83 return tid;
84}
85
86CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070087: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070088{
89 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070090
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070091 m_pit = CreateObject<CcnxPit> ();
92 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070093}
94
95CcnxL3Protocol::~CcnxL3Protocol ()
96{
97 NS_LOG_FUNCTION (this);
98}
99
100void
101CcnxL3Protocol::SetNode (Ptr<Node> node)
102{
103 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700104 m_fib = m_node->GetObject<CcnxFib> ();
105 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
106
107 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108}
109
110/*
111 * This method is called by AddAgregate and completes the aggregation
112 * by setting the node in the ccnx stack
113 */
114void
115CcnxL3Protocol::NotifyNewAggregate ()
116{
117 if (m_node == 0)
118 {
119 Ptr<Node>node = this->GetObject<Node>();
120 // verify that it's a valid node and that
121 // the node has not been set before
122 if (node != 0)
123 {
124 this->SetNode (node);
125 }
126 }
127 Object::NotifyNewAggregate ();
128}
129
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130void
131CcnxL3Protocol::DoDispose (void)
132{
133 NS_LOG_FUNCTION (this);
134
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700135 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700136 {
137 *i = 0;
138 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700139 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800141
142 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800143 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800144 m_pit = 0;
145 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800146 m_fib = 0;
147
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700148 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700149 Object::DoDispose ();
150}
151
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700152void
153CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
154{
155 NS_LOG_FUNCTION (this);
156 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800157 m_forwardingStrategy->SetPit (m_pit);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700158}
159
160Ptr<CcnxForwardingStrategy>
161CcnxL3Protocol::GetForwardingStrategy (void) const
162{
163 return m_forwardingStrategy;
164}
165
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700167CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700169 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700171 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700173 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700174 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700175
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700176 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800177 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700178 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700179}
180
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700181void
182CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
183{
184 // ask face to register in lower-layer stack
185 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186
187 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800188 std::list<boost::reference_wrapper<const CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800189 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
190 {
191 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800192 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800193
194 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
195 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800196 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
197 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800198 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800199 entriesToRemoves.push_back (boost::cref (pitEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800200 }
201 }
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800202 BOOST_FOREACH (const CcnxPitEntry &removedEntry, entriesToRemoves)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800203 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800204 m_pit->erase (m_pit->iterator_to (removedEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800205 }
206
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700207 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
208 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
209 m_faces.erase (face_it);
210}
211
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700212Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700213CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700215 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 -0700216 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700217 if (face->GetId () == index)
218 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219 }
220 return 0;
221}
222
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800223Ptr<CcnxFace>
224CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
225{
226 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
227 {
228 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
229 if (netDeviceFace == 0) continue;
230
231 if (netDeviceFace->GetNetDevice () == netDevice)
232 return face;
233 }
234 return 0;
235}
236
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700238CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700239{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700240 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700241}
242
Alexander Afanasyev98256102011-08-14 01:00:02 -0700243// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700244void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700245CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700246{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700247 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700248
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700249 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700250 try
251 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700252 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700253 switch (type)
254 {
255 case CcnxHeaderHelper::INTEREST:
256 {
257 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700258
259 // Deserialization. Exception may be thrown
260 packet->RemoveHeader (*header);
261 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800262
263 if (header->GetNack () > 0)
264 OnNack (face, header, p/*original packet*/);
265 else
266 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700267 break;
268 }
269 case CcnxHeaderHelper::CONTENT_OBJECT:
270 {
271 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700272
273 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
274
275 // Deserialization. Exception may be thrown
276 packet->RemoveHeader (*header);
277 packet->RemoveTrailer (contentObjectTrailer);
278
279 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700280 break;
281 }
282 }
283
284 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700285 }
286 catch (CcnxUnknownHeaderException)
287 {
288 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
289 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700290}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700291
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800292void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800293CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800294 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800295 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800296{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800297 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800298 m_inNacks (header, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800299
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800300 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
301 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800302 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800303 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800304
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800305 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800306 {
307 // somebody is doing something bad
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800308 m_dropNacks (header, NON_DUPLICATED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800309 return;
310 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800311
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800312 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
313 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800314
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800315 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800316 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800317// NS_ASSERT_MSG (false,
318// "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
319// "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800320
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800321 // m_dropNacks (header, NON_DUPLICATE, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800322 return;
323 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800324
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800325 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
326 // at least when there is only one client
327 //
328 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800329
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800330 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
331
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800332 m_pit->modify (m_pit->iterator_to (pitEntry),
333 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800334
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800335 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
336 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800337
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800338 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
339 {
340 m_pit->modify (m_pit->iterator_to (pitEntry),
341 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
342 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800343
Alexander Afanasyev07827182011-12-13 01:07:32 -0800344 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
345 ll::bind (&CcnxFibEntry::UpdateStatus,
346 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800347
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800348 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
349 {
350 // no need to do anything
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800351 m_dropNacks (header, AFTER_SATISFIED, incomingFace);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800352 return;
353 }
354
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800355 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800356 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800357 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800358 // suppress
359 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800360 m_dropNacks (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800361 return;
362 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800363
364 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800365
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800366 Ptr<Packet> nonNackInterest = Create<Packet> ();
367 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
368 nonNackInterest->AddHeader (*header);
369
370 bool propagated = m_forwardingStrategy->
371 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
372
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800373 // // ForwardingStrategy will try its best to forward packet to at least one interface.
374 // // If no interests was propagated, then there is not other option for forwarding or
375 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800376 if (!propagated)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800377 {
378 m_dropNacks (header, NO_FACES, incomingFace); // this headers doesn't have NACK flag set
379 GiveUpInterest (pitEntry, header);
380 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800381}
382
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700383// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800384//
385// !!! Key point.
386// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700387void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
388 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700389 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700390{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800391 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800392 m_inInterests (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700393
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800394 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800395 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
396 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyev8d11fcc2011-12-30 12:42:31 -0800397 // bool isNew = ret.get<1> ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800398 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800399
Alexander Afanasyev8d11fcc2011-12-30 12:42:31 -0800400 // NS_LOG_DEBUG ("isNew: " << isNew << ", isDup: " << isDuplicated);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800401
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800402 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700403 {
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800404 m_dropInterests (header, DUPLICATED, incomingFace);
405
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800406 /**
407 * This condition will handle "routing" loops and also recently satisfied interests.
408 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
409 * is kept for another small chunk of time.
410 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700411
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800412 if (m_nacksEnabled)
413 {
414 NS_LOG_DEBUG ("Sending NACK_LOOP");
415 header->SetNack (CcnxInterestHeader::NACK_LOOP);
416 Ptr<Packet> nack = Create<Packet> ();
417 nack->AddHeader (*header);
418
419 incomingFace->Send (nack);
420 m_outNacks (header, incomingFace);
421 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700422
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700423 return;
424 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800425
426 Ptr<Packet> contentObject;
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800427 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
428 Ptr<const Packet> payload; // used for tracing
429 tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800430 if (contentObject != 0)
431 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800432 NS_ASSERT (contentObjectHeader != 0);
433
434 NS_LOG_LOGIC("Found in cache");
435
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800436 incomingFace->Send (contentObject);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800437 m_outData (contentObjectHeader, payload, true, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800438
439 // Set pruning timout on PIT entry (instead of deleting the record)
440 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800441 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
442 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800443
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800444 return;
445 }
446
447 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700448
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700449 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800450 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
451 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
452
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800453 bool isRetransmitted = false;
454
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800455 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700456 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800457 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800458
459 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800460 // this is almost definitely a retransmission. But should we trust the user on that?
461 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700462 else
463 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800464 m_pit->modify (m_pit->iterator_to (pitEntry),
465 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800466 }
467
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800468 NS_LOG_DEBUG ("IsRetx: " << isRetransmitted);
469
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800470 // update PIT entry lifetime
471 m_pit->modify (m_pit->iterator_to (pitEntry),
472 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
473 header->GetInterestLifetime ()));
474
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800475 if (outFace != pitEntry.m_outgoing.end ())
476 {
477 // got a non-duplicate interest from the face we have sent interest to
478 // Probably, there is no point in waiting data from that face... Not sure yet
479
480 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
481 // Mark interface YELLOW, but keep a small hope that data will come eventually.
482
483 // ?? not sure if we need to do that ?? ...
484
Alexander Afanasyev07827182011-12-13 01:07:32 -0800485 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
486 ll::bind (&CcnxFibEntry::UpdateStatus,
487 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800488 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800489
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800490 if (!isRetransmitted &&
491 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800492 { // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800493
494 m_dropInterests (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800495 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800496 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800497
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800498 /////////////////////////////////////////////////////////////////////
499 // Propagate
500 /////////////////////////////////////////////////////////////////////
501
502 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
503
504 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800505 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800506
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800507 if (isRetransmitted) //give another chance if retransmitted
508 {
509 // increase max number of allowed retransmissions
510 m_pit->modify (m_pit->iterator_to (pitEntry),
511 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
512
513 // try again
514 propagated = m_forwardingStrategy->
515 PropagateInterest (pitEntry, incomingFace, header, packet);
516 }
517
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800518 // ForwardingStrategy will try its best to forward packet to at least one interface.
519 // If no interests was propagated, then there is not other option for forwarding or
520 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800521 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800522 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800523 NS_LOG_DEBUG ("Not propagated");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800524 m_dropInterests (header, NO_FACES, incomingFace);
525 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800526 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800527}
528
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700529// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800530void
531CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
532 Ptr<CcnxContentObjectHeader> &header,
533 Ptr<Packet> &payload,
534 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700535{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700536
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800537 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800538 m_inData (header, payload, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700539
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700540 // 1. Lookup PIT entry
541 try
542 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700543 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700544
545 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700546
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800547 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700548
549 // If we have sent interest for this data via this face, then update stats.
550 if (out != pitEntry.m_outgoing.end ())
551 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800552 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
553 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
554 ll::_1,
555 incomingFace,
556 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700557 }
558 else
559 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800560 // Unsolicited data, but we're interested in it... should we get it?
561 // Potential hole for attacks
562
563 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
564 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800565 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800566
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800567 // ignore unsolicited data
568 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700569 }
570
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800571 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800572 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
573 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
574 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800575
576 // Add or update entry in the content store
577 m_contentStore->Add (header, payload);
578
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700579 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800580 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700581 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800582 if (incoming.m_face != incomingFace)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800583 {
584 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800585 m_outData (header, payload, false, incoming.m_face);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800586 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700587
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800588 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700589 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800590 // All incoming interests are satisfied. Remove them
591 m_pit->modify (m_pit->iterator_to (pitEntry),
592 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
593
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800594 // Set pruning timout on PIT entry (instead of deleting the record)
595 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800596 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800597 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700598 }
599 catch (CcnxPitEntryNotFound)
600 {
601 // 2. Drop data packet if PIT entry is not found
602 // (unsolicited data packets should not "poison" content store)
603
604 //drop dulicated or not requested data packet
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800605 m_dropData (header, payload, UNSOLICITED, incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700606 return; // do not process unsoliced data packets
607 }
608}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700609
610void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800611CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
612 Ptr<CcnxInterestHeader> header)
613{
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800614 if (m_nacksEnabled)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800615 {
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800616 Ptr<Packet> packet = Create<Packet> ();
617 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
618 packet->AddHeader (*header);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800619
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800620 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
621 {
622 incoming.m_face->Send (packet->Copy ());
623
624 m_outNacks (header, incoming.m_face);
625 }
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800626 }
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800627
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800628 // All incoming interests cannot be satisfied. Remove them
629 m_pit->modify (m_pit->iterator_to (pitEntry),
630 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
631
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800632 // Remove also outgoing
633 m_pit->modify (m_pit->iterator_to (pitEntry),
634 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
635
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800636 // Set pruning timout on PIT entry (instead of deleting the record)
637 m_pit->modify (m_pit->iterator_to (pitEntry),
638 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
639 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
640}
641
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700642} //namespace ns3