blob: 0992810db5aa1ea20deec86f65a6df800a0e9fb5 [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",
Alexander Afanasyev082dbca2012-02-14 18:43:13 -080079 BooleanValue (false),
Alexander Afanasyevbab81b12012-02-04 14:20:09 -080080 MakeBooleanAccessor (&CcnxL3Protocol::m_nacksEnabled),
81 MakeBooleanChecker ())
Alexander Afanasyevde009992012-02-04 18:54:54 -080082 .AddAttribute ("CacheUnsolicitedData", "Cache overheard data that have not been requested",
83 BooleanValue (false),
84 MakeBooleanAccessor (&CcnxL3Protocol::m_cacheUnsolicitedData),
85 MakeBooleanChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086 ;
87 return tid;
88}
89
90CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070091: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070092{
93 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070094
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070095 m_pit = CreateObject<CcnxPit> ();
96 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070097}
98
99CcnxL3Protocol::~CcnxL3Protocol ()
100{
101 NS_LOG_FUNCTION (this);
102}
103
104void
105CcnxL3Protocol::SetNode (Ptr<Node> node)
106{
107 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700108 m_fib = m_node->GetObject<CcnxFib> ();
109 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
110
111 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700112}
113
114/*
115 * This method is called by AddAgregate and completes the aggregation
116 * by setting the node in the ccnx stack
117 */
118void
119CcnxL3Protocol::NotifyNewAggregate ()
120{
121 if (m_node == 0)
122 {
123 Ptr<Node>node = this->GetObject<Node>();
124 // verify that it's a valid node and that
125 // the node has not been set before
126 if (node != 0)
127 {
128 this->SetNode (node);
129 }
130 }
131 Object::NotifyNewAggregate ();
132}
133
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700134void
135CcnxL3Protocol::DoDispose (void)
136{
137 NS_LOG_FUNCTION (this);
138
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700139 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140 {
141 *i = 0;
142 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700143 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700144 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800145
146 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800147 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800148 m_pit = 0;
149 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800150 m_fib = 0;
151
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700152 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700153 Object::DoDispose ();
154}
155
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700156void
157CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
158{
159 NS_LOG_FUNCTION (this);
160 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800161 m_forwardingStrategy->SetPit (m_pit);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700162}
163
164Ptr<CcnxForwardingStrategy>
165CcnxL3Protocol::GetForwardingStrategy (void) const
166{
167 return m_forwardingStrategy;
168}
169
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700171CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700173 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700175 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700177 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700178 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700179
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700180 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700182 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700183}
184
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700185void
186CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
187{
188 // ask face to register in lower-layer stack
189 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800190
191 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800192 std::list<boost::reference_wrapper<const CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800193 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
194 {
195 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800196 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800197
198 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
199 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800200 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
201 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800202 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800203 entriesToRemoves.push_back (boost::cref (pitEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800204 }
205 }
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800206 BOOST_FOREACH (const CcnxPitEntry &removedEntry, entriesToRemoves)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800207 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800208 m_pit->erase (m_pit->iterator_to (removedEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800209 }
210
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700211 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
212 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
213 m_faces.erase (face_it);
214}
215
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700216Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700217CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700218{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700219 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 -0700220 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700221 if (face->GetId () == index)
222 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223 }
224 return 0;
225}
226
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800227Ptr<CcnxFace>
228CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
229{
230 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
231 {
232 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
233 if (netDeviceFace == 0) continue;
234
235 if (netDeviceFace->GetNetDevice () == netDevice)
236 return face;
237 }
238 return 0;
239}
240
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700241uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700242CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700243{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700244 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245}
246
Alexander Afanasyev98256102011-08-14 01:00:02 -0700247// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700248void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700249CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700251 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700252
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700253 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700254 try
255 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700256 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700257 switch (type)
258 {
259 case CcnxHeaderHelper::INTEREST:
260 {
261 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700262
263 // Deserialization. Exception may be thrown
264 packet->RemoveHeader (*header);
265 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800266
267 if (header->GetNack () > 0)
268 OnNack (face, header, p/*original packet*/);
269 else
270 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700271 break;
272 }
273 case CcnxHeaderHelper::CONTENT_OBJECT:
274 {
275 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700276
277 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
278
279 // Deserialization. Exception may be thrown
280 packet->RemoveHeader (*header);
281 packet->RemoveTrailer (contentObjectTrailer);
282
283 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700284 break;
285 }
286 }
287
288 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700289 }
290 catch (CcnxUnknownHeaderException)
291 {
292 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
293 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700294}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700295
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800296void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800297CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800298 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800299 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800300{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800301 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800302 m_inNacks (header, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800303
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800304 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
305 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800306 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800307 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800308
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800309 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800310 {
311 // somebody is doing something bad
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800312 m_dropNacks (header, NON_DUPLICATED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800313 return;
314 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800315
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800316 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
317 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800318
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800319 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800320 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800321// NS_ASSERT_MSG (false,
322// "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
323// "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800324
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800325 // m_dropNacks (header, NON_DUPLICATE, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800326 return;
327 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800328
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800329 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
330 // at least when there is only one client
331 //
332 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800333
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800334 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
335
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800336 m_pit->modify (m_pit->iterator_to (pitEntry),
337 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800338
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800339 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
340 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800341
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800342 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
343 {
344 m_pit->modify (m_pit->iterator_to (pitEntry),
345 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
346 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800347
Alexander Afanasyev07827182011-12-13 01:07:32 -0800348 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
349 ll::bind (&CcnxFibEntry::UpdateStatus,
350 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800351
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800352 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
353 {
354 // no need to do anything
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800355 m_dropNacks (header, AFTER_SATISFIED, incomingFace);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800356 return;
357 }
358
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800359 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800360 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800361 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800362 // suppress
363 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800364 m_dropNacks (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800365 return;
366 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800367
368 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800369
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800370 Ptr<Packet> nonNackInterest = Create<Packet> ();
371 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
372 nonNackInterest->AddHeader (*header);
373
374 bool propagated = m_forwardingStrategy->
375 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
376
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800377 // // ForwardingStrategy will try its best to forward packet to at least one interface.
378 // // If no interests was propagated, then there is not other option for forwarding or
379 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800380 if (!propagated)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800381 {
382 m_dropNacks (header, NO_FACES, incomingFace); // this headers doesn't have NACK flag set
383 GiveUpInterest (pitEntry, header);
384 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800385}
386
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700387// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800388//
389// !!! Key point.
390// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700391void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
392 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700393 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700394{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800395 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800396 m_inInterests (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700397
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800398 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800399 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
400 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyev4e970942012-03-09 10:58:11 -0800401 bool isNew = ret.get<1> ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800402 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800403
Alexander Afanasyev8d11fcc2011-12-30 12:42:31 -0800404 // NS_LOG_DEBUG ("isNew: " << isNew << ", isDup: " << isDuplicated);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800405
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800406 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700407 {
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800408 m_dropInterests (header, DUPLICATED, incomingFace);
409
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800410 /**
411 * This condition will handle "routing" loops and also recently satisfied interests.
412 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
413 * is kept for another small chunk of time.
414 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700415
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800416 if (m_nacksEnabled)
417 {
418 NS_LOG_DEBUG ("Sending NACK_LOOP");
419 header->SetNack (CcnxInterestHeader::NACK_LOOP);
420 Ptr<Packet> nack = Create<Packet> ();
421 nack->AddHeader (*header);
422
423 incomingFace->Send (nack);
424 m_outNacks (header, incomingFace);
425 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700426
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700427 return;
428 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800429
430 Ptr<Packet> contentObject;
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800431 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
432 Ptr<const Packet> payload; // used for tracing
433 tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800434 if (contentObject != 0)
435 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800436 NS_ASSERT (contentObjectHeader != 0);
437
438 NS_LOG_LOGIC("Found in cache");
439
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800440 incomingFace->Send (contentObject);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800441 m_outData (contentObjectHeader, payload, true, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800442
443 // Set pruning timout on PIT entry (instead of deleting the record)
444 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800445 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
446 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800447
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800448 return;
449 }
450
451 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700452
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700453 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800454 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
455 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
456
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800457 bool isRetransmitted = false;
458
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800459 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700460 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800461 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800462
463 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 // this is almost definitely a retransmission. But should we trust the user on that?
465 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700466 else
467 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800468 m_pit->modify (m_pit->iterator_to (pitEntry),
469 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800470 }
471
Alexander Afanasyev4e970942012-03-09 10:58:11 -0800472 // NS_LOG_DEBUG ("IsRetx: " << isRetransmitted);
473 // NS_LOG_DEBUG (*m_pit);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800474
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800475 // update PIT entry lifetime
476 m_pit->modify (m_pit->iterator_to (pitEntry),
477 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
478 header->GetInterestLifetime ()));
479
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800480 if (outFace != pitEntry.m_outgoing.end ())
481 {
Alexander Afanasyev4e970942012-03-09 10:58:11 -0800482 NS_LOG_DEBUG ("Non duplicate interests from the face we have sent interest to");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800483 // got a non-duplicate interest from the face we have sent interest to
484 // Probably, there is no point in waiting data from that face... Not sure yet
485
486 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
487 // Mark interface YELLOW, but keep a small hope that data will come eventually.
488
489 // ?? not sure if we need to do that ?? ...
490
Alexander Afanasyev07827182011-12-13 01:07:32 -0800491 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
492 ll::bind (&CcnxFibEntry::UpdateStatus,
493 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800494 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800495
Alexander Afanasyev4e970942012-03-09 10:58:11 -0800496 // if (!isNew &&
497 // !isRetransmitted &&
498 // pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
499 // { // Suppress this interest if we're still expecting data from some other face
500 // NS_LOG_DEBUG ("Suppress interests");
501 // m_dropInterests (header, SUPPRESSED, incomingFace);
502 // return;
503 // }
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800504
Alexander Afanasyev4e970942012-03-09 10:58:11 -0800505 if (!isNew && !isRetransmitted)
506 {
507 // Suppress this interest if we're still expecting data from some other face
508 NS_LOG_DEBUG ("Suppress interests");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800509 m_dropInterests (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800510 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800511 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800512
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800513 /////////////////////////////////////////////////////////////////////
514 // Propagate
515 /////////////////////////////////////////////////////////////////////
516
517 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
518
519 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800520 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800521
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800522 if (isRetransmitted) //give another chance if retransmitted
523 {
524 // increase max number of allowed retransmissions
525 m_pit->modify (m_pit->iterator_to (pitEntry),
526 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
527
528 // try again
529 propagated = m_forwardingStrategy->
530 PropagateInterest (pitEntry, incomingFace, header, packet);
531 }
532
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800533 // ForwardingStrategy will try its best to forward packet to at least one interface.
534 // If no interests was propagated, then there is not other option for forwarding or
535 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800536 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800537 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800538 NS_LOG_DEBUG ("Not propagated");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800539 m_dropInterests (header, NO_FACES, incomingFace);
540 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800541 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800542}
543
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700544// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800545void
546CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
547 Ptr<CcnxContentObjectHeader> &header,
548 Ptr<Packet> &payload,
549 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700550{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700551
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800552 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800553 m_inData (header, payload, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700554
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700555 // 1. Lookup PIT entry
556 try
557 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700558 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700559
560 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700561
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800562 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700563
564 // If we have sent interest for this data via this face, then update stats.
565 if (out != pitEntry.m_outgoing.end ())
566 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800567 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
568 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
569 ll::_1,
570 incomingFace,
571 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700572 }
573 else
574 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800575 // Unsolicited data, but we're interested in it... should we get it?
576 // Potential hole for attacks
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800577
Alexander Afanasyevde009992012-02-04 18:54:54 -0800578 if (m_cacheUnsolicitedData)
579 {
580 // Optimistically add or update entry in the content store
581 m_contentStore->Add (header, payload);
582 }
583 else
584 {
585 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
586 ". PIT entry for "<< header->GetName ()<<" is valid, "
587 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
588 }
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800589 // ignore unsolicited data
590 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700591 }
592
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800593 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800594 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
595 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
596 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800597
598 // Add or update entry in the content store
599 m_contentStore->Add (header, payload);
600
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700601 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800602 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700603 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800604 if (incoming.m_face != incomingFace)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800605 {
606 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800607 m_outData (header, payload, false, incoming.m_face);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800608 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700609
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800610 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700611 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800612 // All incoming interests are satisfied. Remove them
613 m_pit->modify (m_pit->iterator_to (pitEntry),
614 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
615
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800616 // Set pruning timout on PIT entry (instead of deleting the record)
617 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800618 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800619 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700620 }
621 catch (CcnxPitEntryNotFound)
622 {
Alexander Afanasyevde009992012-02-04 18:54:54 -0800623 if (m_cacheUnsolicitedData)
624 {
625 // Optimistically add or update entry in the content store
626 m_contentStore->Add (header, payload);
627 }
628 else
629 {
630 // Drop data packet if PIT entry is not found
631 // (unsolicited data packets should not "poison" content store)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700632
Alexander Afanasyevde009992012-02-04 18:54:54 -0800633 //drop dulicated or not requested data packet
634 m_dropData (header, payload, UNSOLICITED, incomingFace);
635 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700636 return; // do not process unsoliced data packets
637 }
638}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700639
640void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800641CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
642 Ptr<CcnxInterestHeader> header)
643{
Alexander Afanasyevde009992012-02-04 18:54:54 -0800644 NS_LOG_FUNCTION (this << &pitEntry);
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800645 if (m_nacksEnabled)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800646 {
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800647 Ptr<Packet> packet = Create<Packet> ();
648 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
649 packet->AddHeader (*header);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800650
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800651 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
652 {
Alexander Afanasyevde009992012-02-04 18:54:54 -0800653 NS_LOG_DEBUG ("Send NACK for " << boost::cref (header->GetName ()) << " to " << boost::cref (*incoming.m_face));
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800654 incoming.m_face->Send (packet->Copy ());
655
656 m_outNacks (header, incoming.m_face);
657 }
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800658 }
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800659
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800660 // All incoming interests cannot be satisfied. Remove them
661 m_pit->modify (m_pit->iterator_to (pitEntry),
662 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
663
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800664 // Remove also outgoing
665 m_pit->modify (m_pit->iterator_to (pitEntry),
666 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
667
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800668 // Set pruning timout on PIT entry (instead of deleting the record)
669 m_pit->modify (m_pit->iterator_to (pitEntry),
670 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
671 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
672}
673
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700674} //namespace ns3