blob: 4e3e460d99a9154e274f3123b9457a927c6c877d [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 Afanasyev08d984e2011-08-13 19:20:22 -070034
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070035#include "ns3/ccnx-header-helper.h"
36
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070037#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070038#include "ccnx-forwarding-strategy.h"
39#include "ccnx-interest-header.h"
40#include "ccnx-content-object-header.h"
41
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080042#include "ccnx-net-device-face.h"
43
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070044#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080045#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080046#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080047
48using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080049namespace ll = boost::lambda;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050
51NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
52
53namespace ns3 {
54
Alexander Afanasyev7112f482011-08-17 14:05:57 -070055const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056
Alexander Afanasyev07827182011-12-13 01:07:32 -080057
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
59
60TypeId
61CcnxL3Protocol::GetTypeId (void)
62{
63 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
64 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070065 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080067 .AddAttribute ("FaceList", "List of faces associated with CCNx stack",
68 ObjectVectorValue (),
69 MakeObjectVectorAccessor (&CcnxL3Protocol::m_faces),
70 MakeObjectVectorChecker<CcnxFace> ())
71
72 .AddAttribute ("ForwardingStrategy", "Forwarding strategy used by CCNx stack",
73 PointerValue (),
74 MakePointerAccessor (&CcnxL3Protocol::SetForwardingStrategy, &CcnxL3Protocol::GetForwardingStrategy),
75 MakePointerChecker<CcnxForwardingStrategy> ())
76
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077 .AddAttribute ("BucketLeakInterval",
78 "Interval to leak buckets",
79 StringValue ("10ms"),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080080 MakeTimeAccessor (&CcnxL3Protocol::GetBucketLeakInterval,
81 &CcnxL3Protocol::SetBucketLeakInterval),
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080082 MakeTimeChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083 ;
84 return tid;
85}
86
87CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070088: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070089{
90 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070091
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070092 m_pit = CreateObject<CcnxPit> ();
93 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094}
95
96CcnxL3Protocol::~CcnxL3Protocol ()
97{
98 NS_LOG_FUNCTION (this);
99}
100
101void
102CcnxL3Protocol::SetNode (Ptr<Node> node)
103{
104 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700105 m_fib = m_node->GetObject<CcnxFib> ();
106 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
107
108 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700109}
110
111/*
112 * This method is called by AddAgregate and completes the aggregation
113 * by setting the node in the ccnx stack
114 */
115void
116CcnxL3Protocol::NotifyNewAggregate ()
117{
118 if (m_node == 0)
119 {
120 Ptr<Node>node = this->GetObject<Node>();
121 // verify that it's a valid node and that
122 // the node has not been set before
123 if (node != 0)
124 {
125 this->SetNode (node);
126 }
127 }
128 Object::NotifyNewAggregate ();
129}
130
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131void
132CcnxL3Protocol::DoDispose (void)
133{
134 NS_LOG_FUNCTION (this);
135
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800136 if (m_bucketLeakEvent.IsRunning ())
137 m_bucketLeakEvent.Cancel ();
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> ();
401 // bool isNew = ret.get<1> ();
402 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800403
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800404 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700405 {
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800406 m_dropInterests (header, DUPLICATED, incomingFace);
407
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800408 /**
409 * This condition will handle "routing" loops and also recently satisfied interests.
410 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
411 * is kept for another small chunk of time.
412 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700413
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800414 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800415 header->SetNack (CcnxInterestHeader::NACK_LOOP);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800416 Ptr<Packet> nack = Create<Packet> ();
417 nack->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700418
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800419 incomingFace->Send (nack);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800420 m_outNacks (header, incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700421
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700422 return;
423 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800424
425 Ptr<Packet> contentObject;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800426 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800427 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
428 if (contentObject != 0)
429 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800430 NS_ASSERT (contentObjectHeader != 0);
431
432 NS_LOG_LOGIC("Found in cache");
433
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800434 incomingFace->Send (contentObject);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800435 m_outData (contentObjectHeader, true, incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800436
437 // Set pruning timout on PIT entry (instead of deleting the record)
438 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800439 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
440 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800441
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800442 return;
443 }
444
445 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700446
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700447 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800448 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
449 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
450
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800451 bool isRetransmitted = false;
452
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800453 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700454 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800455 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800456
457 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800458 // this is almost definitely a retransmission. But should we trust the user on that?
459 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700460 else
461 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800462 m_pit->modify (m_pit->iterator_to (pitEntry),
463 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 }
465
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800466 // update PIT entry lifetime
467 m_pit->modify (m_pit->iterator_to (pitEntry),
468 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
469 header->GetInterestLifetime ()));
470
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800471 if (outFace != pitEntry.m_outgoing.end ())
472 {
473 // got a non-duplicate interest from the face we have sent interest to
474 // Probably, there is no point in waiting data from that face... Not sure yet
475
476 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
477 // Mark interface YELLOW, but keep a small hope that data will come eventually.
478
479 // ?? not sure if we need to do that ?? ...
480
Alexander Afanasyev07827182011-12-13 01:07:32 -0800481 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
482 ll::bind (&CcnxFibEntry::UpdateStatus,
483 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800484 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800485
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800486 if (!isRetransmitted &&
487 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800488 { // Suppress this interest if we're still expecting data from some other face
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800489
490 m_dropInterests (header, SUPPRESSED, incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800491 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800492 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800493
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800494 /////////////////////////////////////////////////////////////////////
495 // Propagate
496 /////////////////////////////////////////////////////////////////////
497
498 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
499
500 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800501 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800502
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800503 if (isRetransmitted) //give another chance if retransmitted
504 {
505 // increase max number of allowed retransmissions
506 m_pit->modify (m_pit->iterator_to (pitEntry),
507 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
508
509 // try again
510 propagated = m_forwardingStrategy->
511 PropagateInterest (pitEntry, incomingFace, header, packet);
512 }
513
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800514 // ForwardingStrategy will try its best to forward packet to at least one interface.
515 // If no interests was propagated, then there is not other option for forwarding or
516 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800517 if (!propagated)
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800518 {
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800519 m_dropInterests (header, NO_FACES, incomingFace);
520 GiveUpInterest (pitEntry, header);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800521 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800522}
523
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700524// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800525void
526CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
527 Ptr<CcnxContentObjectHeader> &header,
528 Ptr<Packet> &payload,
529 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700530{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700531
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800532 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800533 m_inData (header, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700534
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700535 // 1. Lookup PIT entry
536 try
537 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700538 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700539
540 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700541
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800542 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700543
544 // If we have sent interest for this data via this face, then update stats.
545 if (out != pitEntry.m_outgoing.end ())
546 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800547 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
548 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
549 ll::_1,
550 incomingFace,
551 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700552 }
553 else
554 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800555 // Unsolicited data, but we're interested in it... should we get it?
556 // Potential hole for attacks
557
558 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
559 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800560 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800561
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800562 // ignore unsolicited data
563 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700564 }
565
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800566 // Update metric status for the incoming interface in the corresponding FIB entry
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::UpdateStatus, ll::_1,
569 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800570
571 // Add or update entry in the content store
572 m_contentStore->Add (header, payload);
573
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700574 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800575 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700576 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800577 if (incoming.m_face != incomingFace)
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800578 {
579 incoming.m_face->Send (packet->Copy ());
580 m_outData (header, false, incoming.m_face);
581 }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700582
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800583 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700584 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800585 // All incoming interests are satisfied. Remove them
586 m_pit->modify (m_pit->iterator_to (pitEntry),
587 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
588
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800589 // Set pruning timout on PIT entry (instead of deleting the record)
590 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800591 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800592 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700593 }
594 catch (CcnxPitEntryNotFound)
595 {
596 // 2. Drop data packet if PIT entry is not found
597 // (unsolicited data packets should not "poison" content store)
598
599 //drop dulicated or not requested data packet
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800600 m_dropData (header, UNSOLICITED, incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700601 return; // do not process unsoliced data packets
602 }
603}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700604
605void
Alexander Afanasyevf377b332011-12-16 15:32:12 -0800606CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
607 Ptr<CcnxInterestHeader> header)
608{
609 Ptr<Packet> packet = Create<Packet> ();
610 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
611 packet->AddHeader (*header);
612
613 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
614 {
615 incoming.m_face->Send (packet->Copy ());
616
617 m_outNacks (header, incoming.m_face);
618 }
619 // All incoming interests cannot be satisfied. Remove them
620 m_pit->modify (m_pit->iterator_to (pitEntry),
621 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
622
623 // Set pruning timout on PIT entry (instead of deleting the record)
624 m_pit->modify (m_pit->iterator_to (pitEntry),
625 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
626 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
627}
628
629void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800630CcnxL3Protocol::SetBucketLeakInterval (Time interval)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700631{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800632 m_bucketLeakInterval = interval;
633
634 if (m_bucketLeakEvent.IsRunning ())
635 m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700636
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800637 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
638 &CcnxL3Protocol::LeakBuckets, this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700639}
640
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800641Time
642CcnxL3Protocol::GetBucketLeakInterval () const
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700643{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800644 return m_bucketLeakInterval;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700645}
646
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800647void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800648CcnxL3Protocol::LeakBuckets ()
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700649{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800650 // NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800651
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800652 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
653 {
654 face->LeakBucket (m_bucketLeakInterval);
655 }
656
657 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800658 &CcnxL3Protocol::LeakBuckets,
659 this);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700660}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800661
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700662} //namespace ns3