blob: aa5be0007483d74d701045337f8e316d5e140b1a [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 Afanasyevff8c5d62012-04-25 15:14:51 -070035#include "ns3/random-variable.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070036
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070037#include "ns3/ccnx-header-helper.h"
38
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070039#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070040#include "ccnx-forwarding-strategy.h"
41#include "ccnx-interest-header.h"
42#include "ccnx-content-object-header.h"
43
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080044#include "ccnx-net-device-face.h"
45
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070046#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080047#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080048#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080049
50using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080051namespace ll = boost::lambda;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052
53NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
54
55namespace ns3 {
56
Alexander Afanasyev7112f482011-08-17 14:05:57 -070057const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058
Alexander Afanasyev07827182011-12-13 01:07:32 -080059
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070060NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
61
62TypeId
63CcnxL3Protocol::GetTypeId (void)
64{
65 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
66 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070067 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080069 .AddAttribute ("FaceList", "List of faces associated with CCNx stack",
70 ObjectVectorValue (),
71 MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
72 MakeObjectVectorChecker<CcnxFace> ())
73
Alexander Afanasyevbab81b12012-02-04 14:20:09 -080074 .AddAttribute ("EnableNACKs", "Enabling support of NACKs",
Alexander Afanasyev082dbca2012-02-14 18:43:13 -080075 BooleanValue (false),
Alexander Afanasyevbab81b12012-02-04 14:20:09 -080076 MakeBooleanAccessor (&CcnxL3Protocol::m_nacksEnabled),
77 MakeBooleanChecker ())
Alexander Afanasyevde009992012-02-04 18:54:54 -080078 .AddAttribute ("CacheUnsolicitedData", "Cache overheard data that have not been requested",
79 BooleanValue (false),
80 MakeBooleanAccessor (&CcnxL3Protocol::m_cacheUnsolicitedData),
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);
90}
91
92CcnxL3Protocol::~CcnxL3Protocol ()
93{
94 NS_LOG_FUNCTION (this);
95}
96
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070097/*
98 * This method is called by AddAgregate and completes the aggregation
99 * by setting the node in the ccnx stack
100 */
101void
102CcnxL3Protocol::NotifyNewAggregate ()
103{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700104 // not really efficient, but this will work only once
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700105 if (m_node == 0)
106 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700107 m_node = GetObject<Node> ();
108 if (m_node != 0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700110 NS_ASSERT_MSG (m_pit != 0 && m_fib != 0 && m_contentStore != 0 && m_forwardingStrategy != 0,
111 "PIT, FIB, and ContentStore should be aggregated before CcnxL3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700112 }
113 }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700114 if (m_pit == 0)
115 {
116 m_pit = GetObject<CcnxPit> ();
117 }
118 if (m_fib == 0)
119 {
120 m_fib = GetObject<CcnxFib> ();
121 }
122 if (m_forwardingStrategy == 0)
123 {
124 m_forwardingStrategy = GetObject<CcnxForwardingStrategy> ();
125 }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700126 if (m_contentStore == 0)
127 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700128 m_contentStore = GetObject<CcnxContentStore> ();
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700129 }
130
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131 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 Afanasyev08d984e2011-08-13 19:20:22 -0700152 Object::DoDispose ();
153}
154
155uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700156CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700157{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700158 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700160 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700162 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700163 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700164
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700165 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700167 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168}
169
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700170void
171CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
172{
173 // ask face to register in lower-layer stack
174 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800175
176 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800177 std::list<boost::reference_wrapper<const CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800178 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
179 {
180 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800181 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800182
183 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
184 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800185 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
186 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800187 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800188 entriesToRemoves.push_back (boost::cref (pitEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800189 }
190 }
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800191 BOOST_FOREACH (const CcnxPitEntry &removedEntry, entriesToRemoves)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800192 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800193 m_pit->erase (m_pit->iterator_to (removedEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800194 }
195
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700196 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
197 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
198 m_faces.erase (face_it);
199}
200
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700201Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700202CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700203{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700204 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 -0700205 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700206 if (face->GetId () == index)
207 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208 }
209 return 0;
210}
211
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800212Ptr<CcnxFace>
213CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
214{
215 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
216 {
217 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
218 if (netDeviceFace == 0) continue;
219
220 if (netDeviceFace->GetNetDevice () == netDevice)
221 return face;
222 }
223 return 0;
224}
225
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700226uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700227CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700229 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230}
231
Alexander Afanasyev98256102011-08-14 01:00:02 -0700232// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700234CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700235{
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700236 if (!face->IsUp ())
237 return;
238
239 NS_LOG_DEBUG (*p);
240
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700241 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700242
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700243 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700244 try
245 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700246 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700247 switch (type)
248 {
249 case CcnxHeaderHelper::INTEREST:
250 {
251 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700252
253 // Deserialization. Exception may be thrown
254 packet->RemoveHeader (*header);
255 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800256
257 if (header->GetNack () > 0)
258 OnNack (face, header, p/*original packet*/);
259 else
260 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700261 break;
262 }
263 case CcnxHeaderHelper::CONTENT_OBJECT:
264 {
265 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700266
267 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
268
269 // Deserialization. Exception may be thrown
270 packet->RemoveHeader (*header);
271 packet->RemoveTrailer (contentObjectTrailer);
272
273 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700274 break;
275 }
276 }
277
278 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700279 }
280 catch (CcnxUnknownHeaderException)
281 {
282 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700283 NS_LOG_ERROR ("Unknown CCNx header. Should not happen");
284 return;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700285 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700286}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700287
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800288void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800289CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800290 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800291 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800292{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800293 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800294 m_inNacks (header, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800295
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800296 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
297 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800298 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800299 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800300
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800301 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800302 {
303 // somebody is doing something bad
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800304 m_dropNacks (header, NON_DUPLICATED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800305 return;
306 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800307
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800308 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
309 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800310
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800311 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800312 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800313// NS_ASSERT_MSG (false,
314// "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
315// "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800316
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800317 // m_dropNacks (header, NON_DUPLICATE, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800318 return;
319 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800320
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800321 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
322 // at least when there is only one client
323 //
324 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800325
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800326 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
327
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800328 m_pit->modify (m_pit->iterator_to (pitEntry),
329 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800330
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800331 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
332 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800333
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800334 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
335 {
336 m_pit->modify (m_pit->iterator_to (pitEntry),
337 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
338 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800339
Alexander Afanasyev07827182011-12-13 01:07:32 -0800340 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
341 ll::bind (&CcnxFibEntry::UpdateStatus,
342 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800343
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800344 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
345 {
346 // no need to do anything
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800347 m_dropNacks (header, AFTER_SATISFIED, incomingFace);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800348 return;
349 }
350
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800351 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800352 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800353 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800354 // suppress
355 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800356 m_dropNacks (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800357 return;
358 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800359
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800360 Ptr<Packet> nonNackInterest = Create<Packet> ();
361 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
362 nonNackInterest->AddHeader (*header);
363
364 bool propagated = m_forwardingStrategy->
365 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
366
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800367 // // ForwardingStrategy will try its best to forward packet to at least one interface.
368 // // If no interests was propagated, then there is not other option for forwarding or
369 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800370 if (!propagated)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800371 {
372 m_dropNacks (header, NO_FACES, incomingFace); // this headers doesn't have NACK flag set
373 GiveUpInterest (pitEntry, header);
374 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800375}
376
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700377// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800378//
379// !!! Key point.
380// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700381void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
382 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700383 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700384{
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800385 m_inInterests (header, incomingFace);
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700386 // NS_LOG_DEBUG (*m_pit);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700387
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800388 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800389 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
390 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyev4e970942012-03-09 10:58:11 -0800391 bool isNew = ret.get<1> ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800392 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800393
Alexander Afanasyev8d11fcc2011-12-30 12:42:31 -0800394 // NS_LOG_DEBUG ("isNew: " << isNew << ", isDup: " << isDuplicated);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800395
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700396 NS_LOG_FUNCTION (header->GetName () << header->GetNonce () << boost::cref (*incomingFace) << isDuplicated);
397
398 /////////////////////////////////////////////////////////////////////////////////////////
399 /////////////////////////////////////////////////////////////////////////////////////////
400 /////////////////////////////////////////////////////////////////////////////////////////
401 // //
402 // !!!! IMPORTANT CHANGE !!!! Duplicate interests will create incoming face entry !!!! //
403 // //
404 /////////////////////////////////////////////////////////////////////////////////////////
405 /////////////////////////////////////////////////////////////////////////////////////////
406 /////////////////////////////////////////////////////////////////////////////////////////
407
408 // Data is not in cache
409 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
410 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
411
412 bool isRetransmitted = false;
413
414 if (inFace != pitEntry.m_incoming.end ())
415 {
416 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
417
418 isRetransmitted = true;
419 // this is almost definitely a retransmission. But should we trust the user on that?
420 }
421 else
422 {
423 m_pit->modify (m_pit->iterator_to (pitEntry),
424 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
425 }
426 //////////////////////////////////////////////////////////////////////////////////
427 //////////////////////////////////////////////////////////////////////////////////
428 //////////////////////////////////////////////////////////////////////////////////
429
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800430 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700431 {
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700432 NS_LOG_DEBUG ("Received duplicatie interest on " << *incomingFace);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800433 m_dropInterests (header, DUPLICATED, incomingFace);
434
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800435 /**
436 * This condition will handle "routing" loops and also recently satisfied interests.
437 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
438 * is kept for another small chunk of time.
439 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700440
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800441 if (m_nacksEnabled)
442 {
443 NS_LOG_DEBUG ("Sending NACK_LOOP");
444 header->SetNack (CcnxInterestHeader::NACK_LOOP);
445 Ptr<Packet> nack = Create<Packet> ();
446 nack->AddHeader (*header);
447
448 incomingFace->Send (nack);
449 m_outNacks (header, incomingFace);
450 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700451
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700452 return;
453 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800454
455 Ptr<Packet> contentObject;
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800456 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
457 Ptr<const Packet> payload; // used for tracing
458 tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800459 if (contentObject != 0)
460 {
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700461 NS_ASSERT (contentObjectHeader != 0);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800462 NS_LOG_LOGIC("Found in cache");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800463
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700464 OnDataDelayed (contentObjectHeader, payload, contentObject);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800465 return;
466 }
467
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800468 // update PIT entry lifetime
469 m_pit->modify (m_pit->iterator_to (pitEntry),
470 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
471 header->GetInterestLifetime ()));
472
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800473 if (outFace != pitEntry.m_outgoing.end ())
474 {
Alexander Afanasyeve192a2a2012-04-09 14:57:54 -0700475 NS_LOG_DEBUG ("Non duplicate interests from the face we have sent interest to. Don't suppress");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800476 // got a non-duplicate interest from the face we have sent interest to
477 // Probably, there is no point in waiting data from that face... Not sure yet
478
479 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
480 // Mark interface YELLOW, but keep a small hope that data will come eventually.
481
482 // ?? not sure if we need to do that ?? ...
483
Alexander Afanasyev07827182011-12-13 01:07:32 -0800484 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
485 ll::bind (&CcnxFibEntry::UpdateStatus,
486 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800487 }
Alexander Afanasyeve192a2a2012-04-09 14:57:54 -0700488 else
489 if (!isNew && !isRetransmitted)
490 {
491 // Suppress this interest if we're still expecting data from some other face
492 NS_LOG_DEBUG ("Suppress interests");
493 m_dropInterests (header, SUPPRESSED, incomingFace);
494 return;
495 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800496
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800497 /////////////////////////////////////////////////////////////////////
498 // Propagate
499 /////////////////////////////////////////////////////////////////////
500
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800501 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800502 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800503
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700504 if (!propagated && isRetransmitted) //give another chance if retransmitted
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800505 {
506 // increase max number of allowed retransmissions
507 m_pit->modify (m_pit->iterator_to (pitEntry),
508 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
509
510 // try again
511 propagated = m_forwardingStrategy->
512 PropagateInterest (pitEntry, incomingFace, header, packet);
513 }
514
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800515 // ForwardingStrategy will try its best to forward packet to at least one interface.
516 // If no interests was propagated, then there is not other option for forwarding or
517 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800518 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800519 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800520 NS_LOG_DEBUG ("Not propagated");
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800521 m_dropInterests (header, NO_FACES, incomingFace);
522 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800523 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800524}
525
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700526void
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700527CcnxL3Protocol::OnDataDelayed (Ptr<const CcnxContentObjectHeader> header,
528 Ptr<const Packet> payload,
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700529 const Ptr<const Packet> &packet)
530{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700531 // 1. Lookup PIT entry
532 try
533 {
534 CcnxPitEntryContainer::type::iterator pitEntry = m_pit->Lookup (*header);
535
536 //satisfy all pending incoming Interests
537 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry->m_incoming)
538 {
539 incoming.m_face->Send (packet->Copy ());
540 m_outData (header, payload, false, incoming.m_face);
541 NS_LOG_DEBUG ("Satisfy " << *incoming.m_face);
542
543 // successfull forwarded data trace
544 }
545
546 if (pitEntry->m_incoming.size () > 0)
547 {
548 // All incoming interests are satisfied. Remove them
549 m_pit->modify (pitEntry,
550 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
551
552 // Remove all outgoing faces
553 m_pit->modify (pitEntry,
554 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
555
556 // Set pruning timout on PIT entry (instead of deleting the record)
557 m_pit->modify (pitEntry,
558 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
559 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
560 }
561 }
562 catch (CcnxPitEntryNotFound)
563 {
564 NS_LOG_DEBUG ("Pit entry not found (was satisfied and removed before)");
565 return; // do not process unsoliced data packets
566 }
567}
568
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700569// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800570void
571CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
572 Ptr<CcnxContentObjectHeader> &header,
573 Ptr<Packet> &payload,
574 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700575{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700576
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -0700577 NS_LOG_FUNCTION (incomingFace << header->GetName () << payload << packet);
Alexander Afanasyevc86c2832011-12-23 02:56:22 -0800578 m_inData (header, payload, incomingFace);
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700579 // NS_LOG_DEBUG (*m_pit);
580
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700581 // 1. Lookup PIT entry
582 try
583 {
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700584 CcnxPitEntryContainer::type::iterator pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700585
586 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700587
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700588 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry->m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700589
590 // If we have sent interest for this data via this face, then update stats.
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700591 if (out != pitEntry->m_outgoing.end ())
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700592 {
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700593 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry->m_fibEntry),
Alexander Afanasyev07827182011-12-13 01:07:32 -0800594 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
595 ll::_1,
596 incomingFace,
597 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700598 }
599 else
600 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800601 // Unsolicited data, but we're interested in it... should we get it?
602 // Potential hole for attacks
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800603
Alexander Afanasyevde009992012-02-04 18:54:54 -0800604 if (m_cacheUnsolicitedData)
605 {
606 // Optimistically add or update entry in the content store
607 m_contentStore->Add (header, payload);
608 }
609 else
610 {
611 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
612 ". PIT entry for "<< header->GetName ()<<" is valid, "
613 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
614 }
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800615 // ignore unsolicited data
616 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700617 }
618
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800619 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700620 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry->m_fibEntry),
Alexander Afanasyev07827182011-12-13 01:07:32 -0800621 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
622 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800623
624 // Add or update entry in the content store
625 m_contentStore->Add (header, payload);
626
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700627 m_pit->modify (pitEntry,
628 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
629
630 if (pitEntry->m_incoming.size () == 0)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700631 {
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700632 // Set pruning timout on PIT entry (instead of deleting the record)
633 m_pit->modify (pitEntry,
634 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
635 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
636 }
637 else
638 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700639 OnDataDelayed (header, payload, packet);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700640 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700641 }
642 catch (CcnxPitEntryNotFound)
643 {
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -0700644 NS_LOG_DEBUG ("Pit entry not found");
Alexander Afanasyevde009992012-02-04 18:54:54 -0800645 if (m_cacheUnsolicitedData)
646 {
647 // Optimistically add or update entry in the content store
648 m_contentStore->Add (header, payload);
649 }
650 else
651 {
652 // Drop data packet if PIT entry is not found
653 // (unsolicited data packets should not "poison" content store)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700654
Alexander Afanasyevde009992012-02-04 18:54:54 -0800655 //drop dulicated or not requested data packet
656 m_dropData (header, payload, UNSOLICITED, incomingFace);
657 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700658 return; // do not process unsoliced data packets
659 }
660}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700661
662void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800663CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
664 Ptr<CcnxInterestHeader> header)
665{
Alexander Afanasyevde009992012-02-04 18:54:54 -0800666 NS_LOG_FUNCTION (this << &pitEntry);
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700667 // NS_LOG_DEBUG (*m_pit);
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800668 if (m_nacksEnabled)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800669 {
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800670 Ptr<Packet> packet = Create<Packet> ();
671 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
672 packet->AddHeader (*header);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800673
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800674 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
675 {
Alexander Afanasyevde009992012-02-04 18:54:54 -0800676 NS_LOG_DEBUG ("Send NACK for " << boost::cref (header->GetName ()) << " to " << boost::cref (*incoming.m_face));
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800677 incoming.m_face->Send (packet->Copy ());
678
679 m_outNacks (header, incoming.m_face);
680 }
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800681
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -0700682 // All incoming interests cannot be satisfied. Remove them
683 m_pit->modify (m_pit->iterator_to (pitEntry),
684 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800685
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -0700686 // Remove also outgoing
687 m_pit->modify (m_pit->iterator_to (pitEntry),
688 ll::bind (&CcnxPitEntry::ClearOutgoing, ll::_1));
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800689
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -0700690 // Set pruning timout on PIT entry (instead of deleting the record)
691 m_pit->modify (m_pit->iterator_to (pitEntry),
692 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
693 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
694 }
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800695}
696
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700697} //namespace ns3