blob: bffc91746d292e635e54e0ae99945869615a8784 [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 Afanasyev07827182011-12-13 01:07:32 -080032#include "ns3/string.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070033
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070034#include "ns3/ccnx-header-helper.h"
35
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070036#include "ccnx-face.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070037#include "ccnx-forwarding-strategy.h"
38#include "ccnx-interest-header.h"
39#include "ccnx-content-object-header.h"
40
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -080041#include "ccnx-net-device-face.h"
42
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070043#include <boost/foreach.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080044#include <boost/lambda/lambda.hpp>
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080045#include <boost/lambda/bind.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080046
47using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080048namespace ll = boost::lambda;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070049
50NS_LOG_COMPONENT_DEFINE ("CcnxL3Protocol");
51
52namespace ns3 {
53
Alexander Afanasyev7112f482011-08-17 14:05:57 -070054const uint16_t CcnxL3Protocol::ETHERNET_FRAME_TYPE = 0x7777;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055
Alexander Afanasyev07827182011-12-13 01:07:32 -080056
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057NS_OBJECT_ENSURE_REGISTERED (CcnxL3Protocol);
58
59TypeId
60CcnxL3Protocol::GetTypeId (void)
61{
62 static TypeId tid = TypeId ("ns3::CcnxL3Protocol")
63 .SetParent<Ccnx> ()
Alexander Afanasyev070aa482011-08-20 00:38:25 -070064 .SetGroupName ("Ccnx")
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065 .AddConstructor<CcnxL3Protocol> ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080066 .AddAttribute ("BucketLeakInterval",
67 "Interval to leak buckets",
68 StringValue ("10ms"),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080069 MakeTimeAccessor (&CcnxL3Protocol::GetBucketLeakInterval,
70 &CcnxL3Protocol::SetBucketLeakInterval),
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080071 MakeTimeChecker ())
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072 ;
73 return tid;
74}
75
76CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070077: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078{
79 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070080
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070081 m_pit = CreateObject<CcnxPit> ();
82 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083}
84
85CcnxL3Protocol::~CcnxL3Protocol ()
86{
87 NS_LOG_FUNCTION (this);
88}
89
90void
91CcnxL3Protocol::SetNode (Ptr<Node> node)
92{
93 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070094 m_fib = m_node->GetObject<CcnxFib> ();
95 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
96
97 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070098}
99
100/*
101 * This method is called by AddAgregate and completes the aggregation
102 * by setting the node in the ccnx stack
103 */
104void
105CcnxL3Protocol::NotifyNewAggregate ()
106{
107 if (m_node == 0)
108 {
109 Ptr<Node>node = this->GetObject<Node>();
110 // verify that it's a valid node and that
111 // the node has not been set before
112 if (node != 0)
113 {
114 this->SetNode (node);
115 }
116 }
117 Object::NotifyNewAggregate ();
118}
119
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700120void
121CcnxL3Protocol::DoDispose (void)
122{
123 NS_LOG_FUNCTION (this);
124
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 if (m_bucketLeakEvent.IsRunning ())
126 m_bucketLeakEvent.Cancel ();
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 Afanasyevd02a5d62011-11-21 11:01:51 -0800137 m_pit = 0;
138 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800139 m_fib = 0;
140
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700141 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700142 Object::DoDispose ();
143}
144
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700145void
146CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
147{
148 NS_LOG_FUNCTION (this);
149 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800150 m_forwardingStrategy->SetPit (m_pit);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700151}
152
153Ptr<CcnxForwardingStrategy>
154CcnxL3Protocol::GetForwardingStrategy (void) const
155{
156 return m_forwardingStrategy;
157}
158
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700160CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700162 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700164 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700165
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700166 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700167 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700168
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700169 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800170 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700171 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172}
173
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174void
175CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
176{
177 // ask face to register in lower-layer stack
178 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179
180 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800181 std::list<boost::reference_wrapper<const CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800182 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
183 {
184 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800185 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186
187 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
188 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800189 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
190 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800191 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800192 entriesToRemoves.push_back (boost::cref (pitEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800193 }
194 }
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800195 BOOST_FOREACH (const CcnxPitEntry &removedEntry, entriesToRemoves)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800196 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800197 m_pit->erase (m_pit->iterator_to (removedEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800198 }
199
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700200 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
201 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
202 m_faces.erase (face_it);
203}
204
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700205Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700206CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700208 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 -0700209 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700210 if (face->GetId () == index)
211 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700212 }
213 return 0;
214}
215
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800216Ptr<CcnxFace>
217CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
218{
219 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
220 {
221 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
222 if (netDeviceFace == 0) continue;
223
224 if (netDeviceFace->GetNetDevice () == netDevice)
225 return face;
226 }
227 return 0;
228}
229
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700231CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700232{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700233 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700234}
235
Alexander Afanasyev98256102011-08-14 01:00:02 -0700236// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700238CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700239{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700240 if (!face->IsUp ())
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700241 {
242 NS_LOG_LOGIC ("Dropping received packet -- interface is down");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700243 // m_dropTrace (p, INTERFACE_DOWN, m_node->GetObject<Ccnx> ()/*this*/, face);
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700244 return;
245 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700246 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700248 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700249 try
250 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700251 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700252 switch (type)
253 {
254 case CcnxHeaderHelper::INTEREST:
255 {
256 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700257
258 // Deserialization. Exception may be thrown
259 packet->RemoveHeader (*header);
260 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800261
262 if (header->GetNack () > 0)
263 OnNack (face, header, p/*original packet*/);
264 else
265 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700266 break;
267 }
268 case CcnxHeaderHelper::CONTENT_OBJECT:
269 {
270 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700271
272 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
273
274 // Deserialization. Exception may be thrown
275 packet->RemoveHeader (*header);
276 packet->RemoveTrailer (contentObjectTrailer);
277
278 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700279 break;
280 }
281 }
282
283 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700284 }
285 catch (CcnxUnknownHeaderException)
286 {
287 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
288 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700289}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700290
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800291void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800292CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800293 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800294 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800295{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800296 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800297
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800298 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
299 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800300 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800301 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800302
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800303 // NS_ASSERT_MSG (isDuplicated,
304 // "NACK should be a duplicated interest");
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800305 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800306 {
307 // somebody is doing something bad
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800308 return;
309 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800310
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800311 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
312 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800313
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800314 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800315 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800316 NS_ASSERT_MSG (false,
317 "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
318 "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800319
320 return;
321 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800322
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800323 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
324 // at least when there is only one client
325 //
326 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800327
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800328 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
329
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800330 m_pit->modify (m_pit->iterator_to (pitEntry),
331 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800332
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800333 // m_droppedInterestsTrace (header, DROP_CONGESTION, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800334
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800335 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
336 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800337
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800338 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
339 {
340 m_pit->modify (m_pit->iterator_to (pitEntry),
341 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
342 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800343
Alexander Afanasyev07827182011-12-13 01:07:32 -0800344 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
345 ll::bind (&CcnxFibEntry::UpdateStatus,
346 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800347
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800348 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
349 {
350 // no need to do anything
351 return;
352 }
353
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800354 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800355 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800356 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800357 // suppress
358 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800359 return;
360 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800361
362 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800363
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800364 Ptr<Packet> nonNackInterest = Create<Packet> ();
365 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
366 nonNackInterest->AddHeader (*header);
367
368 bool propagated = m_forwardingStrategy->
369 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
370
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800371 // // ForwardingStrategy will try its best to forward packet to at least one interface.
372 // // If no interests was propagated, then there is not other option for forwarding or
373 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800374 if (!propagated)
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800375 GiveUpInterest (pitEntry, header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800376}
377
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700378// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800379//
380// !!! Key point.
381// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700382void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
383 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700384 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700385{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800386 NS_LOG_FUNCTION (incomingFace << header << packet);
387 // m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700388
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800389 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800390 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
391 CcnxPitEntry const& pitEntry = ret.get<0> ();
392 // bool isNew = ret.get<1> ();
393 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800394
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800395 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700396 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800397 /**
398 * This condition will handle "routing" loops and also recently satisfied interests.
399 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
400 * is kept for another small chunk of time.
401 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700402
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800403 // //Trace duplicate interest
404 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700405
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800406 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800407 header->SetNack (CcnxInterestHeader::NACK_LOOP);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800408 Ptr<Packet> nack = Create<Packet> ();
409 nack->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700410
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800411 incomingFace->Send (nack);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700412
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800413 // //Trace duplicate interest
414 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700415 return;
416 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800417
418 Ptr<Packet> contentObject;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800419 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800420 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
421 if (contentObject != 0)
422 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800423 NS_ASSERT (contentObjectHeader != 0);
424
425 NS_LOG_LOGIC("Found in cache");
426
427 // TransmittedDataTrace (contentObject, CACHED,
428 // m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800429 incomingFace->Send (contentObject);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800430
431 // Set pruning timout on PIT entry (instead of deleting the record)
432 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800433 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
434 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800435 return;
436 }
437
438 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700439
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700440 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800441 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
442 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
443
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800444 bool isRetransmitted = false;
445
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800446 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700447 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800448 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800449
450 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800451 // this is almost definitely a retransmission. But should we trust the user on that?
452 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700453 else
454 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800455 m_pit->modify (m_pit->iterator_to (pitEntry),
456 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800457 }
458
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800459 // update PIT entry lifetime
460 m_pit->modify (m_pit->iterator_to (pitEntry),
461 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
462 header->GetInterestLifetime ()));
463
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800464 if (outFace != pitEntry.m_outgoing.end ())
465 {
466 // got a non-duplicate interest from the face we have sent interest to
467 // Probably, there is no point in waiting data from that face... Not sure yet
468
469 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
470 // Mark interface YELLOW, but keep a small hope that data will come eventually.
471
472 // ?? not sure if we need to do that ?? ...
473
Alexander Afanasyev07827182011-12-13 01:07:32 -0800474 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
475 ll::bind (&CcnxFibEntry::UpdateStatus,
476 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800477 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800478
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800479 if (!isRetransmitted &&
480 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800481 { // Suppress this interest if we're still expecting data from some other face
482
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800483 // We are already expecting data later in future. Suppress the interest
484 // m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800485 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800486 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800487
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800488 /////////////////////////////////////////////////////////////////////
489 // Propagate
490 /////////////////////////////////////////////////////////////////////
491
492 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
493
494 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800495 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800496
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800497 if (isRetransmitted) //give another chance if retransmitted
498 {
499 // increase max number of allowed retransmissions
500 m_pit->modify (m_pit->iterator_to (pitEntry),
501 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
502
503 // try again
504 propagated = m_forwardingStrategy->
505 PropagateInterest (pitEntry, incomingFace, header, packet);
506 }
507
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800508 // ForwardingStrategy will try its best to forward packet to at least one interface.
509 // If no interests was propagated, then there is not other option for forwarding or
510 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800511 if (!propagated)
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800512 GiveUpInterest (pitEntry, header);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700513}
514
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800515void
516CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
517 Ptr<CcnxInterestHeader> header)
518{
519 Ptr<Packet> packet = Create<Packet> ();
520 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
521 packet->AddHeader (*header);
522
523 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
524 {
525 incoming.m_face->Send (packet->Copy ());
526
527 // m_droppedInterestsTrace (header, DROP_CONGESTION,
528 // m_node->GetObject<Ccnx> (), incomingFace);
529 }
530 // All incoming interests cannot be satisfied. Remove them
531 m_pit->modify (m_pit->iterator_to (pitEntry),
532 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
533
534 // Set pruning timout on PIT entry (instead of deleting the record)
535 m_pit->modify (m_pit->iterator_to (pitEntry),
536 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
537 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
538}
539
540
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700541// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800542void
543CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
544 Ptr<CcnxContentObjectHeader> &header,
545 Ptr<Packet> &payload,
546 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700547{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700548
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800549 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800550 // m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> ()/*this*/, incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700551
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700552 // 1. Lookup PIT entry
553 try
554 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700555 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700556
557 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700558
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800559 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700560
561 // If we have sent interest for this data via this face, then update stats.
562 if (out != pitEntry.m_outgoing.end ())
563 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800564 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
565 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
566 ll::_1,
567 incomingFace,
568 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700569 }
570 else
571 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800572 // Unsolicited data, but we're interested in it... should we get it?
573 // Potential hole for attacks
574
575 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
576 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800577 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800578
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800579 // ignore unsolicited data
580 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700581 }
582
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800583 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800584 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
585 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
586 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800587
588 // Add or update entry in the content store
589 m_contentStore->Add (header, payload);
590
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700591 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800592 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700593 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800594 if (incoming.m_face != incomingFace)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800595 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700596
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800597 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700598 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800599 // All incoming interests are satisfied. Remove them
600 m_pit->modify (m_pit->iterator_to (pitEntry),
601 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
602
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800603 // Set pruning timout on PIT entry (instead of deleting the record)
604 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800605 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800606 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700607 }
608 catch (CcnxPitEntryNotFound)
609 {
610 // 2. Drop data packet if PIT entry is not found
611 // (unsolicited data packets should not "poison" content store)
612
613 //drop dulicated or not requested data packet
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800614 // m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700615 return; // do not process unsoliced data packets
616 }
617}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700618
619void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800620CcnxL3Protocol::SetBucketLeakInterval (Time interval)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700621{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800622 m_bucketLeakInterval = interval;
623
624 if (m_bucketLeakEvent.IsRunning ())
625 m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700626
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800627 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
628 &CcnxL3Protocol::LeakBuckets, this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700629}
630
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800631Time
632CcnxL3Protocol::GetBucketLeakInterval () const
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700633{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800634 return m_bucketLeakInterval;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700635}
636
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800637void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800638CcnxL3Protocol::LeakBuckets ()
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700639{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800640 // NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800641
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800642 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
643 {
644 face->LeakBucket (m_bucketLeakInterval);
645 }
646
647 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800648 &CcnxL3Protocol::LeakBuckets,
649 this);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700650}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800651
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700652} //namespace ns3