blob: fb1e5e44e93e1ec1ef332e7f7b7bc190e2f10e2d [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 Afanasyevcbe92ae2011-12-16 13:06:18 -080083
Ilya Moiseenko816de832011-12-15 16:32:24 -080084 .AddTraceSource ("TransmittedInterestTrace", "Interests that were transmitted",
85 MakeTraceSourceAccessor (&CcnxL3Protocol::m_transmittedInterestsTrace))
86
87 .AddTraceSource ("ReceivedInterestTrace", "Interests that were received",
88 MakeTraceSourceAccessor (&CcnxL3Protocol::m_receivedInterestsTrace))
89
90 .AddTraceSource ("DroppedInterestTrace", "Interests that were dropped",
91 MakeTraceSourceAccessor (&CcnxL3Protocol::m_droppedInterestsTrace))
92
93 .AddTraceSource ("ReceivedDataTrace", "Data that were received",
94 MakeTraceSourceAccessor (&CcnxL3Protocol::m_receivedDataTrace))
95
96 .AddTraceSource ("TransmittedDataTrace", "Data that were transmitted",
97 MakeTraceSourceAccessor (&CcnxL3Protocol::m_transmittedDataTrace))
98
99 .AddTraceSource ("DroppedDataTrace", "Data that were dropped",
100 MakeTraceSourceAccessor (&CcnxL3Protocol::m_droppedDataTrace))
101
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700102 ;
103 return tid;
104}
105
106CcnxL3Protocol::CcnxL3Protocol()
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700107: m_faceCounter (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700108{
109 NS_LOG_FUNCTION (this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700110
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700111 m_pit = CreateObject<CcnxPit> ();
112 m_contentStore = CreateObject<CcnxContentStore> ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700113}
114
115CcnxL3Protocol::~CcnxL3Protocol ()
116{
117 NS_LOG_FUNCTION (this);
118}
119
120void
121CcnxL3Protocol::SetNode (Ptr<Node> node)
122{
123 m_node = node;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700124 m_fib = m_node->GetObject<CcnxFib> ();
125 NS_ASSERT_MSG (m_fib != 0, "FIB should be created and aggregated to a node before calling Ccnx::SetNode");
126
127 m_pit->SetFib (m_fib);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128}
129
130/*
131 * This method is called by AddAgregate and completes the aggregation
132 * by setting the node in the ccnx stack
133 */
134void
135CcnxL3Protocol::NotifyNewAggregate ()
136{
137 if (m_node == 0)
138 {
139 Ptr<Node>node = this->GetObject<Node>();
140 // verify that it's a valid node and that
141 // the node has not been set before
142 if (node != 0)
143 {
144 this->SetNode (node);
145 }
146 }
147 Object::NotifyNewAggregate ();
148}
149
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700150void
151CcnxL3Protocol::DoDispose (void)
152{
153 NS_LOG_FUNCTION (this);
154
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800155 if (m_bucketLeakEvent.IsRunning ())
156 m_bucketLeakEvent.Cancel ();
157
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700158 for (CcnxFaceList::iterator i = m_faces.begin (); i != m_faces.end (); ++i)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159 {
160 *i = 0;
161 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700162 m_faces.clear ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163 m_node = 0;
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800164
165 // Force delete on objects
Alexander Afanasyev18252852011-11-21 13:35:31 -0800166 m_forwardingStrategy = 0; // there is a reference to PIT stored in here
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800167 m_pit = 0;
168 m_contentStore = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800169 m_fib = 0;
170
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700171 // m_forwardingStrategy = 0;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172 Object::DoDispose ();
173}
174
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700175void
176CcnxL3Protocol::SetForwardingStrategy (Ptr<CcnxForwardingStrategy> forwardingStrategy)
177{
178 NS_LOG_FUNCTION (this);
179 m_forwardingStrategy = forwardingStrategy;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800180 m_forwardingStrategy->SetPit (m_pit);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700181}
182
183Ptr<CcnxForwardingStrategy>
184CcnxL3Protocol::GetForwardingStrategy (void) const
185{
186 return m_forwardingStrategy;
187}
188
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700189uint32_t
Alexander Afanasyev7112f482011-08-17 14:05:57 -0700190CcnxL3Protocol::AddFace (const Ptr<CcnxFace> &face)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700191{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700192 NS_LOG_FUNCTION (this << &face);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700193
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700194 face->SetId (m_faceCounter); // sets a unique ID of the face. This ID serves only informational purposes
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700196 // ask face to register in lower-layer stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700197 face->RegisterProtocolHandler (MakeCallback (&CcnxL3Protocol::Receive, this));
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700198
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700199 m_faces.push_back (face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800200 m_faceCounter++;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700201 return face->GetId ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700202}
203
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700204void
205CcnxL3Protocol::RemoveFace (Ptr<CcnxFace> face)
206{
207 // ask face to register in lower-layer stack
208 face->RegisterProtocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ());
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800209
210 // just to be on a safe side. Do the process in two steps
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800211 std::list<boost::reference_wrapper<const CcnxPitEntry> > entriesToRemoves;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800212 BOOST_FOREACH (const CcnxPitEntry &pitEntry, *m_pit)
213 {
214 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800215 ll::bind (&CcnxPitEntry::RemoveAllReferencesToFace, ll::_1, face));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800216
217 // If this face is the only for the associated FIB entry, then FIB entry will be removed soon.
218 // Thus, we have to remove the whole PIT entry
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800219 if (pitEntry.m_fibEntry.m_faces.size () == 1 &&
220 pitEntry.m_fibEntry.m_faces.begin ()->m_face == face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800221 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800222 entriesToRemoves.push_back (boost::cref (pitEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800223 }
224 }
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800225 BOOST_FOREACH (const CcnxPitEntry &removedEntry, entriesToRemoves)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800226 {
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -0800227 m_pit->erase (m_pit->iterator_to (removedEntry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800228 }
229
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700230 CcnxFaceList::iterator face_it = find (m_faces.begin(), m_faces.end(), face);
231 NS_ASSERT_MSG (face_it != m_faces.end (), "Attempt to remove face that doesn't exist");
232 m_faces.erase (face_it);
233}
234
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700235Ptr<CcnxFace>
Alexander Afanasyev98256102011-08-14 01:00:02 -0700236CcnxL3Protocol::GetFace (uint32_t index) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700237{
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700238 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 -0700239 {
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700240 if (face->GetId () == index)
241 return face;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700242 }
243 return 0;
244}
245
Alexander Afanasyev52e9aa92011-11-15 20:23:20 -0800246Ptr<CcnxFace>
247CcnxL3Protocol::GetFaceByNetDevice (Ptr<NetDevice> netDevice) const
248{
249 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces) // this function is not supposed to be called often, so linear search is fine
250 {
251 Ptr<CcnxNetDeviceFace> netDeviceFace = DynamicCast<CcnxNetDeviceFace> (face);
252 if (netDeviceFace == 0) continue;
253
254 if (netDeviceFace->GetNetDevice () == netDevice)
255 return face;
256 }
257 return 0;
258}
259
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260uint32_t
Alexander Afanasyev98256102011-08-14 01:00:02 -0700261CcnxL3Protocol::GetNFaces (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700262{
Alexander Afanasyev98256102011-08-14 01:00:02 -0700263 return m_faces.size ();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700264}
265
Alexander Afanasyev98256102011-08-14 01:00:02 -0700266// Callback from lower layer
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700267void
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -0700268CcnxL3Protocol::Receive (const Ptr<CcnxFace> &face, const Ptr<const Packet> &p)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700269{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700270 NS_LOG_LOGIC ("Packet from face " << *face << " received on node " << m_node->GetId ());
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700271
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700272 Ptr<Packet> packet = p->Copy (); // give upper layers a rw copy of the packet
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700273 try
274 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700275 CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700276 switch (type)
277 {
278 case CcnxHeaderHelper::INTEREST:
279 {
280 Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700281
282 // Deserialization. Exception may be thrown
283 packet->RemoveHeader (*header);
284 NS_ASSERT_MSG (packet->GetSize () == 0, "Payload of Interests should be zero");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800285
286 if (header->GetNack () > 0)
287 OnNack (face, header, p/*original packet*/);
288 else
289 OnInterest (face, header, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700290 break;
291 }
292 case CcnxHeaderHelper::CONTENT_OBJECT:
293 {
294 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> ();
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700295
296 static CcnxContentObjectTail contentObjectTrailer; //there is no data in this object
297
298 // Deserialization. Exception may be thrown
299 packet->RemoveHeader (*header);
300 packet->RemoveTrailer (contentObjectTrailer);
301
302 OnData (face, header, packet/*payload*/, p/*original packet*/);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700303 break;
304 }
305 }
306
307 // exception will be thrown if packet is not recognized
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700308 }
309 catch (CcnxUnknownHeaderException)
310 {
311 NS_ASSERT_MSG (false, "Unknown CCNx header. Should not happen");
312 }
Alexander Afanasyev98256102011-08-14 01:00:02 -0700313}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700314
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800315void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800316CcnxL3Protocol::OnNack (const Ptr<CcnxFace> &incomingFace,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800317 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800318 const Ptr<const Packet> &packet)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800319{
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800320 NS_LOG_FUNCTION (incomingFace << header << packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800321
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800322 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
323 CcnxPitEntry const& pitEntry = ret.get<0> ();
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800324 bool isNew = ret.get<1> ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800325 bool isDuplicated = ret.get<2> ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800326
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800327 // NS_ASSERT_MSG (isDuplicated,
328 // "NACK should be a duplicated interest");
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800329 if (isNew || !isDuplicated) // potential flow
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800330 {
331 // somebody is doing something bad
Ilya Moiseenko816de832011-12-15 16:32:24 -0800332 m_droppedInterestsTrace (header, NACK_NONDUPLICATE, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800333 return;
334 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800335
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800336 // CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
337 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800338
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800339 if (outFace == pitEntry.m_outgoing.end ())
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800340 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800341 NS_ASSERT_MSG (false,
342 "Node " << GetObject<Node> ()->GetId () << ", outgoing entry should exist for face " << boost::cref(*incomingFace) << "\n" <<
343 "size: " << pitEntry.m_outgoing.size ());
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800344
345 return;
346 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800347
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800348 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
349 // at least when there is only one client
350 //
351 // incomingFace->LeakBucketByOnePacket ();
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800352
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800353 NS_LOG_ERROR ("Nack on " << boost::cref(*incomingFace));
354
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800355 m_pit->modify (m_pit->iterator_to (pitEntry),
356 ll::bind (&CcnxPitEntry::SetWaitingInVain, ll::_1, outFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800357
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800358 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
359 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800360
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800361 if (header->GetNack () == CcnxInterestHeader::NACK_GIVEUP_PIT)
362 {
363 m_pit->modify (m_pit->iterator_to (pitEntry),
364 ll::bind (&CcnxPitEntry::RemoveIncoming, ll::_1, incomingFace));
365 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800366
Alexander Afanasyev07827182011-12-13 01:07:32 -0800367 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
368 ll::bind (&CcnxFibEntry::UpdateStatus,
369 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800370
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800371 if (pitEntry.m_incoming.size () == 0) // interest was actually satisfied
372 {
373 // no need to do anything
Ilya Moiseenko816de832011-12-15 16:32:24 -0800374 m_droppedInterestsTrace (header, NACK_AFTER_SATISFIED, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800375 return;
376 }
377
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800378 if (!pitEntry.AreAllOutgoingInVain ()) // not all ougtoing are in vain
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800379 {
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800380 NS_LOG_DEBUG ("Not all outgoing are in vain");
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800381 // suppress
382 // Don't do anything, we are still expecting data from some other face
Ilya Moiseenko816de832011-12-15 16:32:24 -0800383 m_droppedInterestsTrace (header, NACK_SUPPRESSED, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800384 return;
385 }
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800386
387 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800388
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800389 Ptr<Packet> nonNackInterest = Create<Packet> ();
390 header->SetNack (CcnxInterestHeader::NORMAL_INTEREST);
391 nonNackInterest->AddHeader (*header);
392
393 bool propagated = m_forwardingStrategy->
394 PropagateInterest (pitEntry, incomingFace, header, nonNackInterest);
395
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800396 // // ForwardingStrategy will try its best to forward packet to at least one interface.
397 // // If no interests was propagated, then there is not other option for forwarding or
398 // // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800399 if (!propagated)
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800400 GiveUpInterest (pitEntry, header);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800401}
402
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700403// Processing Interests
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800404//
405// !!! Key point.
406// !!! All interests should be answerred!!! Either later with data, immediately with data, or immediately with NACK
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700407void CcnxL3Protocol::OnInterest (const Ptr<CcnxFace> &incomingFace,
408 Ptr<CcnxInterestHeader> &header,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700409 const Ptr<const Packet> &packet)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700410{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800411 NS_LOG_FUNCTION (incomingFace << header << packet);
Ilya Moiseenko816de832011-12-15 16:32:24 -0800412 //m_receivedInterestsTrace (header, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700413
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800414 // Lookup of Pit (and associated Fib) entry for this Interest
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800415 tuple<const CcnxPitEntry&,bool,bool> ret = m_pit->Lookup (*header);
416 CcnxPitEntry const& pitEntry = ret.get<0> ();
417 // bool isNew = ret.get<1> ();
418 bool isDuplicated = ret.get<2> ();
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800419
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800420 if (isDuplicated)
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700421 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800422 /**
423 * This condition will handle "routing" loops and also recently satisfied interests.
424 * Every time interest is satisfied, PIT entry (with empty incoming and outgoing faces)
425 * is kept for another small chunk of time.
426 */
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700427
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800428 // //Trace duplicate interest
429 // m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700430
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800431 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800432 header->SetNack (CcnxInterestHeader::NACK_LOOP);
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800433 Ptr<Packet> nack = Create<Packet> ();
434 nack->AddHeader (*header);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700435
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800436 incomingFace->Send (nack);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700437
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800438 // //Trace duplicate interest
Ilya Moiseenko816de832011-12-15 16:32:24 -0800439 m_droppedInterestsTrace (header, NDN_DUPLICATE_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700440 return;
441 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800442
443 Ptr<Packet> contentObject;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800444 Ptr<const CcnxContentObjectHeader> contentObjectHeader; // unused for now
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800445 tie (contentObject, contentObjectHeader) = m_contentStore->Lookup (header);
446 if (contentObject != 0)
447 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800448 NS_ASSERT (contentObjectHeader != 0);
449
450 NS_LOG_LOGIC("Found in cache");
451
Ilya Moiseenko816de832011-12-15 16:32:24 -0800452 m_transmittedDataTrace (contentObjectHeader, contentObject, CACHED, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800453 incomingFace->Send (contentObject);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800454
455 // Set pruning timout on PIT entry (instead of deleting the record)
456 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800457 bind (&CcnxPitEntry::SetExpireTime, ll::_1,
458 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Ilya Moiseenko816de832011-12-15 16:32:24 -0800459
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800460 return;
461 }
462
463 // \todo Detect retransmissions. Not yet sure how...
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700464
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700465 // Data is not in cache
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800466 CcnxPitEntryIncomingFaceContainer::type::iterator inFace = pitEntry.m_incoming.find (incomingFace);
467 CcnxPitEntryOutgoingFaceContainer::type::iterator outFace = pitEntry.m_outgoing.find (incomingFace);
468
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800469 bool isRetransmitted = false;
470
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800471 if (inFace != pitEntry.m_incoming.end ())
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700472 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800473 // CcnxPitEntryIncomingFace.m_arrivalTime keeps track arrival time of the first packet... why?
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800474
475 isRetransmitted = true;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800476 // this is almost definitely a retransmission. But should we trust the user on that?
477 }
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700478 else
479 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800480 m_pit->modify (m_pit->iterator_to (pitEntry),
481 ll::var(inFace) = ll::bind (&CcnxPitEntry::AddIncoming, ll::_1, incomingFace));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800482 }
483
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800484 // update PIT entry lifetime
485 m_pit->modify (m_pit->iterator_to (pitEntry),
486 ll::bind (&CcnxPitEntry::UpdateLifetime, ll::_1,
487 header->GetInterestLifetime ()));
488
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800489 if (outFace != pitEntry.m_outgoing.end ())
490 {
491 // got a non-duplicate interest from the face we have sent interest to
492 // Probably, there is no point in waiting data from that face... Not sure yet
493
494 // If we're expecting data from the interface we got the interest from ("producer" asks us for "his own" data)
495 // Mark interface YELLOW, but keep a small hope that data will come eventually.
496
497 // ?? not sure if we need to do that ?? ...
498
Alexander Afanasyev07827182011-12-13 01:07:32 -0800499 m_fib->m_fib.modify(m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
500 ll::bind (&CcnxFibEntry::UpdateStatus,
501 ll::_1, incomingFace, CcnxFibFaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800502 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800503
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800504 if (!isRetransmitted &&
505 pitEntry.AreTherePromisingOutgoingFacesExcept (incomingFace))
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800506 { // Suppress this interest if we're still expecting data from some other face
507
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800508 // We are already expecting data later in future. Suppress the interest
Ilya Moiseenko816de832011-12-15 16:32:24 -0800509 m_droppedInterestsTrace (header, NDN_SUPPRESSED_INTEREST, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800510 return;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800511 }
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800512
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800513 /////////////////////////////////////////////////////////////////////
514 // Propagate
515 /////////////////////////////////////////////////////////////////////
516
517 NS_ASSERT_MSG (m_forwardingStrategy != 0, "Need a forwarding protocol object to process packets");
518
519 bool propagated = m_forwardingStrategy->
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800520 PropagateInterest (pitEntry, incomingFace, header, packet);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800521
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800522 if (isRetransmitted) //give another chance if retransmitted
523 {
524 // increase max number of allowed retransmissions
525 m_pit->modify (m_pit->iterator_to (pitEntry),
526 ll::bind (&CcnxPitEntry::IncreaseAllowedRetxCount, ll::_1));
527
528 // try again
529 propagated = m_forwardingStrategy->
530 PropagateInterest (pitEntry, incomingFace, header, packet);
531 }
532
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800533 // ForwardingStrategy will try its best to forward packet to at least one interface.
534 // If no interests was propagated, then there is not other option for forwarding or
535 // ForwardingStrategy failed to find it.
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800536 if (!propagated)
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800537 GiveUpInterest (pitEntry, header);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700538}
539
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800540void
541CcnxL3Protocol::GiveUpInterest (const CcnxPitEntry &pitEntry,
542 Ptr<CcnxInterestHeader> header)
543{
544 Ptr<Packet> packet = Create<Packet> ();
545 header->SetNack (CcnxInterestHeader::NACK_GIVEUP_PIT);
546 packet->AddHeader (*header);
547
548 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
549 {
550 incoming.m_face->Send (packet->Copy ());
551
Ilya Moiseenko816de832011-12-15 16:32:24 -0800552 //m_droppedInterestsTrace (header, DROP_CONGESTION,
553 // m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800554 }
555 // All incoming interests cannot be satisfied. Remove them
556 m_pit->modify (m_pit->iterator_to (pitEntry),
557 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
558
559 // Set pruning timout on PIT entry (instead of deleting the record)
560 m_pit->modify (m_pit->iterator_to (pitEntry),
561 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
562 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
563}
564
565
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700566// Processing ContentObjects
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800567void
568CcnxL3Protocol::OnData (const Ptr<CcnxFace> &incomingFace,
569 Ptr<CcnxContentObjectHeader> &header,
570 Ptr<Packet> &payload,
571 const Ptr<const Packet> &packet)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700572{
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700573
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800574 NS_LOG_FUNCTION (incomingFace << header << payload << packet);
Ilya Moiseenko816de832011-12-15 16:32:24 -0800575 m_receivedDataTrace (header, payload, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700576
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700577 // 1. Lookup PIT entry
578 try
579 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700580 const CcnxPitEntry &pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700581
582 // Note that with MultiIndex we need to modify entries indirectly
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700583
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800584 CcnxPitEntryOutgoingFaceContainer::type::iterator out = pitEntry.m_outgoing.find (incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700585
586 // If we have sent interest for this data via this face, then update stats.
587 if (out != pitEntry.m_outgoing.end ())
588 {
Alexander Afanasyev07827182011-12-13 01:07:32 -0800589 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
590 ll::bind (&CcnxFibEntry::UpdateFaceRtt,
591 ll::_1,
592 incomingFace,
593 Simulator::Now () - out->m_sendTime));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700594 }
595 else
596 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800597 // Unsolicited data, but we're interested in it... should we get it?
598 // Potential hole for attacks
599
600 NS_LOG_ERROR ("Node "<< m_node->GetId() <<
601 ". PIT entry for "<< header->GetName ()<<" is valid, "
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800602 "but outgoing entry for interface "<< boost::cref(*incomingFace) <<" doesn't exist\n");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800603
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800604 // ignore unsolicited data
605 return;
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700606 }
607
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800608 // Update metric status for the incoming interface in the corresponding FIB entry
Alexander Afanasyev07827182011-12-13 01:07:32 -0800609 m_fib->m_fib.modify (m_fib->m_fib.iterator_to (pitEntry.m_fibEntry),
610 ll::bind (&CcnxFibEntry::UpdateStatus, ll::_1,
611 incomingFace, CcnxFibFaceMetric::NDN_FIB_GREEN));
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800612
613 // Add or update entry in the content store
614 m_contentStore->Add (header, payload);
615
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700616 //satisfy all pending incoming Interests
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800617 BOOST_FOREACH (const CcnxPitEntryIncomingFace &incoming, pitEntry.m_incoming)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700618 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800619 if (incoming.m_face != incomingFace)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800620 incoming.m_face->Send (packet->Copy ());
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700621
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800622 // successfull forwarded data trace
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700623 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800624 // All incoming interests are satisfied. Remove them
625 m_pit->modify (m_pit->iterator_to (pitEntry),
626 ll::bind (&CcnxPitEntry::ClearIncoming, ll::_1));
627
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800628 // Set pruning timout on PIT entry (instead of deleting the record)
629 m_pit->modify (m_pit->iterator_to (pitEntry),
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800630 ll::bind (&CcnxPitEntry::SetExpireTime, ll::_1,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800631 Simulator::Now () + m_pit->GetPitEntryPruningTimeout ()));
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700632 }
633 catch (CcnxPitEntryNotFound)
634 {
635 // 2. Drop data packet if PIT entry is not found
636 // (unsolicited data packets should not "poison" content store)
637
638 //drop dulicated or not requested data packet
Ilya Moiseenko816de832011-12-15 16:32:24 -0800639 m_droppedDataTrace (header, payload, NDN_UNSOLICITED_DATA, m_node->GetObject<Ccnx> (), incomingFace);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700640 return; // do not process unsoliced data packets
641 }
642}
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700643
644void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800645CcnxL3Protocol::SetBucketLeakInterval (Time interval)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700646{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800647 m_bucketLeakInterval = interval;
648
649 if (m_bucketLeakEvent.IsRunning ())
650 m_bucketLeakEvent.Cancel ();
Alexander Afanasyev070aa482011-08-20 00:38:25 -0700651
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800652 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
653 &CcnxL3Protocol::LeakBuckets, this);
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700654}
655
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800656Time
657CcnxL3Protocol::GetBucketLeakInterval () const
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700658{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800659 return m_bucketLeakInterval;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700660}
661
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800662void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800663CcnxL3Protocol::LeakBuckets ()
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700664{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800665 // NS_LOG_FUNCTION (this);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800666
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800667 BOOST_FOREACH (const Ptr<CcnxFace> &face, m_faces)
668 {
669 face->LeakBucket (m_bucketLeakInterval);
670 }
671
672 m_bucketLeakEvent = Simulator::Schedule (m_bucketLeakInterval,
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800673 &CcnxL3Protocol::LeakBuckets,
674 this);
Ilya Moiseenko172763c2011-10-28 13:21:53 -0700675}
Ilya Moiseenkod83eb0d2011-11-16 15:23:46 -0800676
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700677} //namespace ns3