blob: 12087c11856767bd551711a9c3d5f7437788b442 [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 Afanasyev4975f732011-12-20 17:52:19 -080032#include "ns3/simulator.h"
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070033#include "ns3/random-variable.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070035#include "ns3/ccnx-header-helper.h"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070036#include "ns3/ccnx-pit.h"
37#include "ns3/ccnx-interest-header.h"
38#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070039
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070040#include "ns3/ccnx-face.h"
41#include "ns3/ccnx-forwarding-strategy.h"
42
43// #include "fib/ccnx-fib-impl.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070044
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080045#include "ccnx-net-device-face.h"
46
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070047#include <boost/foreach.hpp>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048
49NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
50
51namespace ns3 {
52
Alexander Afanasyev7112f482011-08-17 14:05:57 -070053const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054
Alexander Afanasyev07827182011-12-13 01:07:32 -080055
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
57
58TypeId
59CcnxL3Protocol::GetTypeId (void)
60{
61 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
62 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070063 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080065 .AddAttribute ("FaceList", "List of faces associated with CCNx stack",
66 ObjectVectorValue (),
67 MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
68 MakeObjectVectorChecker<CcnxFace> ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070069 ;
70 return tid;
71}
72
73CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070074: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070075{
76 NS_LOG_FUNCTION (this);
77}
78
79CcnxL3Protocol::~CcnxL3Protocol ()
80{
81 NS_LOG_FUNCTION (this);
82}
83
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084/*
85 * This method is called by AddAgregate and completes the aggregation
86 * by setting the node in the ccnx stack
87 */
88void
89CcnxL3Protocol::NotifyNewAggregate ()
90{
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070091 // not really efficient, but this will work only once
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070092 if (m_node == 0)
93 {
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -070094 m_node = GetObject<Node> ();
95 if (m_node != 0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070096 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070097 // NS_ASSERT_MSG (m_pit != 0 && m_fib != 0 && m_contentStore != 0 && m_forwardingStrategy != 0,
98 // "PIT, FIB, and ContentStore should be aggregated before CcnxL3Protocol");
99 NS_ASSERT_MSG (m_forwardingStrategy != 0,
100 "Forwarding strategy should be aggregated before CcnxL3Protocol");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700101 }
102 }
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700103 // if (m_pit == 0)
104 // {
105 // m_pit = GetObject<CcnxPit> ();
106 // }
107 // if (m_fib == 0)
108 // {
109 // m_fib = GetObject<CcnxFib> ();
110 // }
Alexander Afanasyev3a4a0b32012-06-28 14:14:22 -0700111 if (m_forwardingStrategy == 0)
112 {
113 m_forwardingStrategy = GetObject<CcnxForwardingStrategy> ();
114 }
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700115 // if (m_contentStore == 0)
116 // {
117 // m_contentStore = GetObject<CcnxContentStore> ();
118 // }
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700119
Alexander Afanasyevf249a192012-07-18 16:52:51 -0700120 Ccnx::NotifyNewAggregate ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121}
122
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123void
124CcnxL3Protocol::DoDispose (void)
125{
126 NS_LOG_FUNCTION (this);
127
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700128 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129 {
130 *i = 0;
131 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700132 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800134
135 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800136 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyev18252852011-11-21 13:35:31 -0800137
Alexander Afanasyevf249a192012-07-18 16:52:51 -0700138 Ccnx::DoDispose ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139}
140
141uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700142CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700143{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700144 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700145
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700146 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700147
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700148 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700149 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700150
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700151 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800152 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700153 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154}
155
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700156void
157CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
158{
159 // ask face to register in lower-layer stack
160 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700161 Ptr<CcnxPit> pit = GetObject<CcnxPit> ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162
163 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700164 std::list< Ptr<CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700165 for (Ptr<CcnxPitEntry> pitEntry = pit->Begin (); pitEntry != 0; pitEntry = pit->Next (pitEntry))
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700167 pitEntry->RemoveAllReferencesToFace (face);
168
169 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
170 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700171 if (pitEntry->GetFibEntry ()->m_faces.size () == 1 &&
172 pitEntry->GetFibEntry ()->m_faces.begin ()->m_face == face)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700173 {
174 entriesToRemoves.push_back (pitEntry);
175 }
176 }
177 BOOST_FOREACH (Ptr<CcnxPitEntry> removedEntry, entriesToRemoves)
178 {
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700179 pit->MarkErased (removedEntry);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800180 }
181
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700182 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
183 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
184 m_faces.erase (face_it);
185}
186
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700187Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700188CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700189{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700190 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 -0700191 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700192 if (face->GetId () == index)
193 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700194 }
195 return 0;
196}
197
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800198Ptr<CcnxFace>
199CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
200{
201 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
202 {
203 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
204 if (netDeviceFace == 0) continue;
205
206 if (netDeviceFace->GetNetDevice () == netDevice)
207 return face;
208 }
209 return 0;
210}
211
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700212uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700213CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700214{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700215 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216}
217
Alexander Afanasyev98256102011-08-14 01:00:02 -0700218// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700220CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700221{
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700222 if (!face->IsUp ())
223 return;
224
225 NS_LOG_DEBUG (*p);
226
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700227 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700229 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700230 try
231 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700232 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700233 switch (type)
234 {
235 case CcnxHeaderHelper::INTEREST:
236 {
237 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700238
239 // Deserialization. Exception may be thrown
240 packet->RemoveHeader (*header);
241 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800242
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700243 m_forwardingStrategy->OnInterest (face, header, p/*original packet*/);
244 // if (header->GetNack () > 0)
245 // OnNack (face, header, p/*original packet*/);
246 // else
247 // OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700248 break;
249 }
250 case CcnxHeaderHelper::CONTENT_OBJECT:
251 {
252 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700253
254 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
255
256 // Deserialization. Exception may be thrown
257 packet->RemoveHeader (*header);
258 packet->RemoveTrailer (contentObjectTrailer);
259
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700260 m_forwardingStrategy->OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700261 break;
262 }
263 }
264
265 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700266 }
267 catch (CcnxUnknownHeaderException)
268 {
269 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
Alexander Afanasyevd459ec32012-04-30 13:58:20 -0700270 NS_LOG_ERROR ("Unknown CCNx header. Should not happen");
271 return;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700272 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700273}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700274
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700275// void
276// CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
277// Ptr<CcnxInterestHeader> &header,
278// const Ptr<const Packet> &packet)
279// {
280// NS_LOG_FUNCTION (incomingFace << header << packet);
281// m_inNacks (header, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800282
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700283// Ptr<CcnxPitEntry> pitEntry = m_pit->Lookup (*header);
284// if (pitEntry == 0)
285// {
286// // somebody is doing something bad
287// m_dropNacks (header, NON_DUPLICATED, incomingFace);
288// return;
289// }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800290
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700291// // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry->GetIncoming ().find (incomingFace);
292// CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry->GetOutgoing ().find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800293
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700294// if (outFace == pitEntry->GetOutgoing ().end ())
295// {
296// // NS_ASSERT_MSG (false,
297// // "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
298// // "size: " << pitEntry.GetOutgoing ().size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800299
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700300// // m_dropNacks (header, NON_DUPLICATE, incomingFace);
301// return;
302// }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800303
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700304// // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
305// // at least when there is only one client
306// //
307// // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800308
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700309// NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700310
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700311// pitEntry->SetWaitingInVain (outFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800312
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700313// // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
314// // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800315
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700316// if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
317// {
318// pitEntry->RemoveIncoming (incomingFace);
319// }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800320
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700321// pitEntry->GetFibEntry ()->UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW);
322// // StaticCast<CcnxFibImpl> (m_fib)->modify (pitEntry->GetFibEntry (),
323// // ll::bind (&CcnxFibEntry::UpdateStatus,
324// // ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800325
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700326// if (pitEntry->GetIncoming ().size () == 0) // interest was actually satisfied
327// {
328// // no need to do anything
329// m_dropNacks (header, AFTER_SATISFIED, incomingFace);
330// return;
331// }
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800332
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700333// if (!pitEntry->AreAllOutgoingInVain ()) // not all ougtoing are in vain
334// {
335// NS_LOG_DEBUG ("Not all outgoing are in vain");
336// // suppress
337// // Don't do anything, we are still expecting data from some other face
338// m_dropNacks (header, SUPPRESSED, incomingFace);
339// return;
340// }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800341
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700342// Ptr<Packet> nonNackInterest = Create<Packet> ();
343// header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
344// nonNackInterest->AddHeader (*header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800345
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700346// bool propagated = m_forwardingStrategy->
347// PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800348
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700349// // // ForwardingStrategy will try its best to forward packet to at least one interface.
350// // // If no interests was propagated, then there is not other option for forwarding or
351// // // ForwardingStrategy failed to find it.
352// if (!propagated)
353// {
354// m_dropNacks (header, NO_FACES, incomingFace); // this headers doesn't have NACK flag set
355// GiveUpInterest (pitEntry, header);
356// }
357// }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800358
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700359// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800360//
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700361// // !!! Key point.
362// // !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
363// void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
364// Ptr<CcnxInterestHeader> &header,
365// const Ptr<const Packet> &packet)
366// {
367// m_inInterests (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700368
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700369// Ptr<CcnxPitEntry> pitEntry = m_pit->Lookup (*header);
370// if (pitEntry == 0)
371// {
372// pitEntry = m_pit->Create (header);
373// }
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800374
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700375// if (pitEntry == 0)
376// {
377// // if it is still not created, then give up processing
378// m_dropInterests (header, PIT_LIMIT, incomingFace);
379// return;
380// }
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700381
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700382// bool isNew = pitEntry->GetIncoming ().size () == 0 && pitEntry->GetOutgoing ().size () == 0;
383// bool isDuplicated = true;
384// if (!pitEntry->IsNonceSeen (header->GetNonce ()))
385// {
386// pitEntry->AddSeenNonce (header->GetNonce ());
387// isDuplicated = false;
388// }
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700389
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700390// NS_LOG_FUNCTION (header->GetName () << header->GetNonce () << boost::cref (*incomingFace) << isDuplicated);
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700391
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700392// /////////////////////////////////////////////////////////////////////////////////////////
393// /////////////////////////////////////////////////////////////////////////////////////////
394// /////////////////////////////////////////////////////////////////////////////////////////
395// // //
396// // !!!! IMPORTANT CHANGE !!!! Duplicate interests will create incoming face entry !!!! //
397// // //
398// /////////////////////////////////////////////////////////////////////////////////////////
399// /////////////////////////////////////////////////////////////////////////////////////////
400// /////////////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700401
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700402// // Data is not in cache
403// CcnxPitEntry::in_iterator inFace = pitEntry->GetIncoming ().find (incomingFace);
404// CcnxPitEntry::out_iterator outFace = pitEntry->GetOutgoing ().find (incomingFace);
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700405
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700406// bool isRetransmitted = false;
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700407
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700408// if (inFace != pitEntry->GetIncoming ().end ())
409// {
410// // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700411
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700412// isRetransmitted = true;
413// // this is almost definitely a retransmission. But should we trust the user on that?
414// }
415// else
416// {
417// inFace = pitEntry->AddIncoming (incomingFace);
418// }
419// //////////////////////////////////////////////////////////////////////////////////
420// //////////////////////////////////////////////////////////////////////////////////
421// //////////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700422
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700423// if (isDuplicated)
424// {
425// NS_LOG_DEBUG ("Received duplicatie interest on " << *incomingFace);
426// m_dropInterests (header, DUPLICATED, incomingFace);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800427
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700428// /**
429// * This condition will handle "routing" loops and also recently satisfied interests.
430// * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
431// * is kept for another small chunk of time.
432// */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700433
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700434// if (m_nacksEnabled)
435// {
436// NS_LOG_DEBUG ("Sending NACK_LOOP");
437// header->SetNack (CcnxInterestHeader::NACK_LOOP);
438// Ptr<Packet> nack = Create<Packet> ();
439// nack->AddHeader (*header);
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800440
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700441// incomingFace->Send (nack);
442// m_outNacks (header, incomingFace);
443// }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700444
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700445// return;
446// }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800447
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700448// Ptr<Packet> contentObject;
449// Ptr<const CcnxContentObjectHeader> contentObjectHeader; // used for tracing
450// Ptr<const Packet> payload; // used for tracing
451// tie (contentObject, contentObjectHeader, payload) = m_contentStore->Lookup (header);
452// if (contentObject != 0)
453// {
454// NS_ASSERT (contentObjectHeader != 0);
455// NS_LOG_LOGIC("Found in cache");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800456
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700457// OnDataDelayed (contentObjectHeader, payload, contentObject);
458// return;
459// }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800460
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700461// // update PIT entry lifetime
462// pitEntry->UpdateLifetime (header->GetInterestLifetime ());
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800463
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700464// if (outFace != pitEntry->GetOutgoing ().end ())
465// {
466// NS_LOG_DEBUG ("Non duplicate interests from the face we have sent interest to. Don't suppress");
467// // got a non-duplicate interest from the face we have sent interest to
468// // Probably, there is no point in waiting data from that face... Not sure yet
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800469
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700470// // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
471// // Mark interface YELLOW, but keep a small hope that data will come eventually.
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800472
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700473// // ?? not sure if we need to do that ?? ...
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800474
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700475// pitEntry->GetFibEntry ()->UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW);
476// // StaticCast<CcnxFibImpl> (m_fib)->modify(pitEntry->GetFibEntry (),
477// // ll::bind (&CcnxFibEntry::UpdateStatus,
478// // ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
479// }
480// else
481// if (!isNew && !isRetransmitted)
482// {
483// // Suppress this interest if we're still expecting data from some other face
484// NS_LOG_DEBUG ("Suppress interests");
485// m_dropInterests (header, SUPPRESSED, incomingFace);
486// return;
487// }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800488
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700489// /////////////////////////////////////////////////////////////////////
490// // Propagate
491// /////////////////////////////////////////////////////////////////////
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800492
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700493// bool propagated = m_forwardingStrategy->
494// PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800495
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700496// if (!propagated && isRetransmitted) //give another chance if retransmitted
497// {
498// // increase max number of allowed retransmissions
499// pitEntry->IncreaseAllowedRetxCount ();
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800500
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700501// // try again
502// propagated = m_forwardingStrategy->
503// PropagateInterest (pitEntry, incomingFace, header, packet);
504// }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800505
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700506// // ForwardingStrategy will try its best to forward packet to at least one interface.
507// // If no interests was propagated, then there is not other option for forwarding or
508// // ForwardingStrategy failed to find it.
509// if (!propagated)
510// {
511// NS_LOG_DEBUG ("Not propagated");
512// m_dropInterests (header, NO_FACES, incomingFace);
513// GiveUpInterest (pitEntry, header);
514// }
515// }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800516
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700517// void
518// CcnxL3Protocol::OnDataDelayed (Ptr<const CcnxContentObjectHeader> header,
519// Ptr<const Packet> payload,
520// const Ptr<const Packet> &packet)
521// {
522// // 1. Lookup PIT entry
523// Ptr<CcnxPitEntry> pitEntry = m_pit->Lookup (*header);
524// if (pitEntry != 0)
525// {
526// //satisfy all pending incoming Interests
527// BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry->GetIncoming ())
528// {
529// incoming.m_face->Send (packet->Copy ());
530// m_outData (header, payload, false, incoming.m_face);
531// NS_LOG_DEBUG ("Satisfy " << *incoming.m_face);
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700532
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700533// // successfull forwarded data trace
534// }
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700535
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700536// if (pitEntry->GetIncoming ().size () > 0)
537// {
538// // All incoming interests are satisfied. Remove them
539// pitEntry->ClearIncoming ();
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700540
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700541// // Remove all outgoing faces
542// pitEntry->ClearOutgoing ();
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700543
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700544// // Set pruning timout on PIT entry (instead of deleting the record)
545// m_pit->MarkErased (pitEntry);
546// }
547// }
548// else
549// {
550// NS_LOG_DEBUG ("Pit entry not found (was satisfied and removed before)");
551// return; // do not process unsoliced data packets
552// }
553// }
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700554
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700555// // Processing ContentObjects
556// void
557// CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
558// Ptr<CcnxContentObjectHeader> &header,
559// Ptr<Packet> &payload,
560// const Ptr<const Packet> &packet)
561// {
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700562
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700563// NS_LOG_FUNCTION (incomingFace << header->GetName () << payload << packet);
564// m_inData (header, payload, incomingFace);
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700565
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700566// // 1. Lookup PIT entry
567// Ptr<CcnxPitEntry> pitEntry = m_pit->Lookup (*header);
568// if (pitEntry != 0)
569// {
570// // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700571
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700572// CcnxPitEntry::out_iterator out = pitEntry->GetOutgoing ().find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700573
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700574// // If we have sent interest for this data via this face, then update stats.
575// if (out != pitEntry->GetOutgoing ().end ())
576// {
577// pitEntry->GetFibEntry ()->UpdateFaceRtt (incomingFace, Simulator::Now () - out->m_sendTime);
578// // StaticCast<CcnxFibImpl> (m_fib)->modify (pitEntry->GetFibEntry (),
579// // ll::bind (&CcnxFibEntry::UpdateFaceRtt,
580// // ll::_1,
581// // incomingFace,
582// // Simulator::Now () - out->m_sendTime));
583// }
584// else
585// {
586// // Unsolicited data, but we're interested in it... should we get it?
587// // Potential hole for attacks
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800588
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700589// if (m_cacheUnsolicitedData)
590// {
591// // Optimistically add or update entry in the content store
592// m_contentStore->Add (header, payload);
593// }
594// else
595// {
596// NS_LOG_ERROR ("Node "<< m_node->GetId() <<
597// ". PIT entry for "<< header->GetName ()<<" is valid, "
598// "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
599// }
600// // ignore unsolicited data
601// return;
602// }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700603
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700604// // Update metric status for the incoming interface in the corresponding FIB entry
605// pitEntry->GetFibEntry ()->UpdateStatus (incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN);
606// // StaticCast<CcnxFibImpl>(m_fib)->modify (pitEntry->GetFibEntry (),
607// // ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
608// // incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800609
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700610// // Add or update entry in the content store
611// m_contentStore->Add (header, payload);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800612
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700613// pitEntry->RemoveIncoming (incomingFace);
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -0700614
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700615// if (pitEntry->GetIncoming ().size () == 0)
616// {
617// // Set pruning timout on PIT entry (instead of deleting the record)
618// m_pit->MarkErased (pitEntry);
619// }
620// else
621// {
622// OnDataDelayed (header, payload, packet);
623// }
624// }
625// else
626// {
627// NS_LOG_DEBUG ("Pit entry not found");
628// if (m_cacheUnsolicitedData)
629// {
630// // Optimistically add or update entry in the content store
631// m_contentStore->Add (header, payload);
632// }
633// else
634// {
635// // Drop data packet if PIT entry is not found
636// // (unsolicited data packets should not "poison" content store)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700637
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700638// //drop dulicated or not requested data packet
639// m_dropData (header, payload, UNSOLICITED, incomingFace);
640// }
641// return; // do not process unsoliced data packets
642// }
643// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700644
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700645// void
646// CcnxL3Protocol::GiveUpInterest (Ptr<CcnxPitEntry> pitEntry,
647// Ptr<CcnxInterestHeader> header)
648// {
649// NS_LOG_FUNCTION (this);
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700650
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700651// if (m_nacksEnabled)
652// {
653// Ptr<Packet> packet = Create<Packet> ();
654// header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
655// packet->AddHeader (*header);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800656
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700657// BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry->GetIncoming ())
658// {
659// NS_LOG_DEBUG ("Send NACK for " << boost::cref (header->GetName ()) << " to " << boost::cref (*incoming.m_face));
660// incoming.m_face->Send (packet->Copy ());
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800661
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700662// m_outNacks (header, incoming.m_face);
663// }
Alexander Afanasyevbab81b12012-02-04 14:20:09 -0800664
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700665// // All incoming interests cannot be satisfied. Remove them
666// pitEntry->ClearIncoming ();
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800667
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700668// // Remove also outgoing
669// pitEntry->ClearOutgoing ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800670
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -0700671// // Set pruning timout on PIT entry (instead of deleting the record)
672// m_pit->MarkErased (pitEntry);
673// }
674// }
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800675
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700676} //namespace ns3