blob: 596b36a8b2cbd67031e2caa5c63f8faf95eaf14b [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 Afanasyev08d984e2011-08-13 19:20:22 -070031#include "ns3/boolean.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070033#include "ns3/ccnx-header-helper.h"
34
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070035#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070036#include "ccnx-forwarding-strategy.h"
37#include "ccnx-interest-header.h"
38#include "ccnx-content-object-header.h"
39
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080040#include "ccnx-net-device-face.h"
41
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070042#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080043#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080044#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080045
46using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080047namespace ll = boost::lambda;
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
55NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
56
57TypeId
58CcnxL3Protocol::GetTypeId (void)
59{
60 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
61 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070062 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080064 .AddAttribute ("BucketLeakInterval",
65 "Interval to leak buckets",
66 StringValue ("10ms"),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080067 MakeTimeAccessor (&CcnxL3Protocol::GetBucketLeakInterval,
68 &CcnxL3Protocol::SetBucketLeakInterval),
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069 MakeTimeChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070070 ;
71 return tid;
72}
73
74CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070075: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070076{
77 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070078
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070079 m_pit = CreateObject<CcnxPit> ();
80 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081}
82
83CcnxL3Protocol::~CcnxL3Protocol ()
84{
85 NS_LOG_FUNCTION (this);
86}
87
88void
89CcnxL3Protocol::SetNode (Ptr<Node> node)
90{
91 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070092 m_fib = m_node->GetObject<CcnxFib> ();
93 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
94
95 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070096}
97
98/*
99 * This method is called by AddAgregate and completes the aggregation
100 * by setting the node in the ccnx stack
101 */
102void
103CcnxL3Protocol::NotifyNewAggregate ()
104{
105 if (m_node == 0)
106 {
107 Ptr<Node>node = this->GetObject<Node>();
108 // verify that it's a valid node and that
109 // the node has not been set before
110 if (node != 0)
111 {
112 this->SetNode (node);
113 }
114 }
115 Object::NotifyNewAggregate ();
116}
117
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700118void
119CcnxL3Protocol::DoDispose (void)
120{
121 NS_LOG_FUNCTION (this);
122
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800123 if (m_bucketLeakEvent.IsRunning ())
124 m_bucketLeakEvent.Cancel ();
125
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700126 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700127 {
128 *i = 0;
129 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700130 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800132
133 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800134 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800135 m_pit = 0;
136 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800137 m_fib = 0;
138
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700139 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140 Object::DoDispose ();
141}
142
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700143void
144CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
145{
146 NS_LOG_FUNCTION (this);
147 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800148 m_forwardingStrategy->SetPit (m_pit);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700149}
150
151Ptr<CcnxForwardingStrategy>
152CcnxL3Protocol::GetForwardingStrategy (void) const
153{
154 return m_forwardingStrategy;
155}
156
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700157uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700158CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700160 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700162 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700164 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700165 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700167 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800168 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700169 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700170}
171
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172void
173CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
174{
175 // ask face to register in lower-layer stack
176 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800177
178 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800179 std::list<CcnxPitEntryContainer::type::iterator> entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800180 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
181 {
182 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800183 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800184
185 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
186 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800187 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
188 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800189 {
190 entriesToRemoves.push_back (m_pit->iterator_to (pitEntry));
191 }
192 }
193 BOOST_FOREACH (CcnxPitEntryContainer::type::iterator entry, entriesToRemoves)
194 {
195 m_pit->erase (entry);
196 }
197
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
199 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
200 m_faces.erase (face_it);
201}
202
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700203Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700204CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700205{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700206 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 -0700207 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700208 if (face->GetId () == index)
209 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700210 }
211 return 0;
212}
213
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800214Ptr<CcnxFace>
215CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
216{
217 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
218 {
219 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
220 if (netDeviceFace == 0) continue;
221
222 if (netDeviceFace->GetNetDevice () == netDevice)
223 return face;
224 }
225 return 0;
226}
227
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700228uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700229CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700231 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700232}
233
Alexander Afanasyev98256102011-08-14 01:00:02 -0700234// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700235void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700236CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700238 if (!face->IsUp ())
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700239 {
240 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700241 // m_dropTrace (p, INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700242 return;
243 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700244 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700246 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700247 try
248 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700250 switch (type)
251 {
252 case CcnxHeaderHelper::INTEREST:
253 {
254 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700255
256 // Deserialization. Exception may be thrown
257 packet->RemoveHeader (*header);
258 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800259
260 if (header->GetNack () > 0)
261 OnNack (face, header, p/*original packet*/);
262 else
263 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700264 break;
265 }
266 case CcnxHeaderHelper::CONTENT_OBJECT:
267 {
268 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700269
270 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
271
272 // Deserialization. Exception may be thrown
273 packet->RemoveHeader (*header);
274 packet->RemoveTrailer (contentObjectTrailer);
275
276 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700277 break;
278 }
279 }
280
281 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700282 }
283 catch (CcnxUnknownHeaderException)
284 {
285 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
286 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700287}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700288
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800289void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800290CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800291 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800292 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800293{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800294 NS_LOG_FUNCTION (incomingFace << header << packet);
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 Afanasyeva7a2b8b2011-11-28 18:19:09 -0800301 // NS_ASSERT_MSG (isDuplicated,
302 // "NACK should be a duplicated interest");
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800303 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800304 {
305 // somebody is doing something bad
306 NS_ASSERT (false); // temporary assert
307 return;
308 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800309
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800310 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
311 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800312
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800313 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800314 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800315 NS_ASSERT_MSG (false,
316 "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
317 "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800318
319 return;
320 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800321
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800322 NS_ASSERT_MSG (incomingFace == outFace->m_face, "Something is wrong");
323 incomingFace->LeakBucketByOnePacket ();
324
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800325 m_pit->modify (m_pit->iterator_to (pitEntry),
326 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800327
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800328 // m_droppedInterestsTrace (header, DROP_CONGESTION, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800329
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800330 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
331 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800332
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800333 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
334 {
335 m_pit->modify (m_pit->iterator_to (pitEntry),
336 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
337 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800338
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800339 m_fib->modify(m_fib->iterator_to (pitEntry.m_fibEntry),
340 ll::bind (&CcnxFibEntry::UpdateStatus,
341 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800342
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800343 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800344 {
345 // suppress
346 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800347 return;
348 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800349
350 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800351
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800352 Ptr<Packet> nonNackInterest = Create<Packet> ();
353 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
354 nonNackInterest->AddHeader (*header);
355
356 bool propagated = m_forwardingStrategy->
357 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
358
359 // ForwardingStrategy will try its best to forward packet to at least one interface.
360 // If no interests was propagated, then there is not other option for forwarding or
361 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800362 if (!propagated)
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800363 GiveUpInterest (pitEntry, header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800364}
365
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700366// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800367//
368// !!! Key point.
369// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700370void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
371 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700372 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700373{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800374 NS_LOG_FUNCTION (incomingFace << header << packet);
375 // m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700376
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800377 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800378 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
379 CcnxPitEntry const& pitEntry = ret.get<0> ();
380 // bool isNew = ret.get<1> ();
381 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800382
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800383 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700384 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800385 /**
386 * This condition will handle "routing" loops and also recently satisfied interests.
387 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
388 * is kept for another small chunk of time.
389 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700390
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800391 // //Trace duplicate interest
392 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700393
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800394 header->SetNack (CcnxInterestHeader::NACK_LOOP);
395 Ptr<Packet> packet = Create<Packet> ();
396 packet->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700397
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800398 incomingFace->Send (packet);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700399
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800400 // //Trace duplicate interest
401 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700402 return;
403 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800404
405 Ptr<Packet> contentObject;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800406 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800407 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
408 if (contentObject != 0)
409 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800410 NS_ASSERT (contentObjectHeader != 0);
411
412 NS_LOG_LOGIC("Found in cache");
413
414 // TransmittedDataTrace (contentObject, CACHED,
415 // m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800416 incomingFace->Send (contentObject);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800417
418 // Set pruning timout on PIT entry (instead of deleting the record)
419 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800420 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
421 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800422 return;
423 }
424
425 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700426
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700427 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800428 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
429 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
430
431 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700432 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800433 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700434
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800435 // this is almost definitely a retransmission. But should we trust the user on that?
436 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700437 else
438 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800439 m_pit->modify (m_pit->iterator_to (pitEntry),
440 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800441 }
442
443 if (outFace != pitEntry.m_outgoing.end ())
444 {
445 // got a non-duplicate interest from the face we have sent interest to
446 // Probably, there is no point in waiting data from that face... Not sure yet
447
448 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
449 // Mark interface YELLOW, but keep a small hope that data will come eventually.
450
451 // ?? not sure if we need to do that ?? ...
452
453 m_fib->modify(m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800454 ll::bind (&CcnxFibEntry::UpdateStatus,
455 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800456 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800457
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800458 if (pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
459 { // Suppress this interest if we're still expecting data from some other face
460
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800461 // We are already expecting data later in future. Suppress the interest
462 // m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800463 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800465
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800466 /////////////////////////////////////////////////////////////////////
467 // Propagate
468 /////////////////////////////////////////////////////////////////////
469
470 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
471
472 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800473 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800474
475 // ForwardingStrategy will try its best to forward packet to at least one interface.
476 // If no interests was propagated, then there is not other option for forwarding or
477 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800478 if (!propagated)
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800479 GiveUpInterest (pitEntry, header);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700480}
481
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800482void
483CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
484 Ptr<CcnxInterestHeader> header)
485{
486 Ptr<Packet> packet = Create<Packet> ();
487 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
488 packet->AddHeader (*header);
489
490 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
491 {
492 incoming.m_face->Send (packet->Copy ());
493
494 // m_droppedInterestsTrace (header, DROP_CONGESTION,
495 // m_node->GetObject<Ccnx> (), incomingFace);
496 }
497 // All incoming interests cannot be satisfied. Remove them
498 m_pit->modify (m_pit->iterator_to (pitEntry),
499 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
500
501 // Set pruning timout on PIT entry (instead of deleting the record)
502 m_pit->modify (m_pit->iterator_to (pitEntry),
503 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
504 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
505}
506
507
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700508// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800509void
510CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
511 Ptr<CcnxContentObjectHeader> &header,
512 Ptr<Packet> &payload,
513 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700514{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700515
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800516 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800517 // m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700518
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700519 // 1. Lookup PIT entry
520 try
521 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700522 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700523
524 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700525
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800526 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700527
528 // If we have sent interest for this data via this face, then update stats.
529 if (out != pitEntry.m_outgoing.end ())
530 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800531 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800532 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800533 ll::_1,
534 incomingFace,
535 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700536 }
537 else
538 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800539 // Unsolicited data, but we're interested in it... should we get it?
540 // Potential hole for attacks
541
542 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
543 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800544 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800545
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800546 // ignore unsolicited data
547 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700548 }
549
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800550 // Update metric status for the incoming interface in the corresponding FIB entry
551 m_fib->modify (m_fib->iterator_to (pitEntry.m_fibEntry),
552 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
553 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
554
555 // Add or update entry in the content store
556 m_contentStore->Add (header, payload);
557
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700558 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800559 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700560 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800561 if (incoming.m_face != incomingFace)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800562 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700563
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800564 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700565 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800566 // All incoming interests are satisfied. Remove them
567 m_pit->modify (m_pit->iterator_to (pitEntry),
568 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
569
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800570 // Set pruning timout on PIT entry (instead of deleting the record)
571 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800572 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800573 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700574 }
575 catch (CcnxPitEntryNotFound)
576 {
577 // 2. Drop data packet if PIT entry is not found
578 // (unsolicited data packets should not "poison" content store)
579
580 //drop dulicated or not requested data packet
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800581 // m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700582 return; // do not process unsoliced data packets
583 }
584}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700585
586void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800587CcnxL3Protocol::SetBucketLeakInterval (Time interval)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700588{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800589 m_bucketLeakInterval = interval;
590
591 if (m_bucketLeakEvent.IsRunning ())
592 m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700593
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800594 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
595 &CcnxL3Protocol::LeakBuckets, this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700596}
597
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800598Time
599CcnxL3Protocol::GetBucketLeakInterval () const
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700600{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800601 return m_bucketLeakInterval;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700602}
603
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800604void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800605CcnxL3Protocol::LeakBuckets ()
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700606{
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800607 NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800608 NS_LOG_ERROR ("Bucket Interval: " << m_bucketLeakInterval.ToDouble(Time::S));
609
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800610 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
611 {
612 face->LeakBucket (m_bucketLeakInterval);
613 }
614
615 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800616 &CcnxL3Protocol::LeakBuckets,
617 this);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700618}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800619
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700620} //namespace ns3