blob: a148794c6a362a992280f97589198a7b7a20cd1a [file] [log] [blame]
Alexander Afanasyev996b4872012-07-17 17:07:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
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>
19 */
20
21#include "nacks.h"
22
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070023#include "ns3/ndn-pit.h"
24#include "ns3/ndn-pit-entry.h"
25#include "ns3/ndn-interest-header.h"
26#include "ns3/ndn-content-object-header.h"
27#include "ns3/ndn-pit.h"
28#include "ns3/ndn-fib.h"
29#include "ns3/ndn-content-store.h"
Alexander Afanasyev996b4872012-07-17 17:07:56 -070030
31#include "ns3/assert.h"
32#include "ns3/ptr.h"
33#include "ns3/log.h"
34#include "ns3/simulator.h"
35#include "ns3/boolean.h"
36#include "ns3/string.h"
37
38#include <boost/ref.hpp>
39#include <boost/foreach.hpp>
40#include <boost/lambda/lambda.hpp>
41#include <boost/lambda/bind.hpp>
42#include <boost/tuple/tuple.hpp>
43namespace ll = boost::lambda;
44
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045NS_LOG_COMPONENT_DEFINE ("ndn.fw.Nacks");
Alexander Afanasyev996b4872012-07-17 17:07:56 -070046
47namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048namespace ndn {
49namespace fw {
Alexander Afanasyev996b4872012-07-17 17:07:56 -070050
51NS_OBJECT_ENSURE_REGISTERED (Nacks);
52
53TypeId
54Nacks::GetTypeId (void)
55{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056 static TypeId tid = TypeId ("ns3::ndn::fw::Nacks")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070057 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 .SetParent<ForwardingStrategy> ()
Alexander Afanasyev996b4872012-07-17 17:07:56 -070059
60 ////////////////////////////////////////////////////////////////////
61 ////////////////////////////////////////////////////////////////////
62
63 .AddTraceSource ("OutNacks", "OutNacks", MakeTraceSourceAccessor (&Nacks::m_outNacks))
64 .AddTraceSource ("InNacks", "InNacks", MakeTraceSourceAccessor (&Nacks::m_inNacks))
65 .AddTraceSource ("DropNacks", "DropNacks", MakeTraceSourceAccessor (&Nacks::m_dropNacks))
66
67 .AddAttribute ("EnableNACKs", "Enabling support of NACKs",
68 BooleanValue (false),
69 MakeBooleanAccessor (&Nacks::m_nacksEnabled),
70 MakeBooleanChecker ())
71 ;
72 return tid;
73}
74
75void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076Nacks::OnInterest (const Ptr<Face> &incomingFace,
77 Ptr<InterestHeader> &header,
Alexander Afanasyev996b4872012-07-17 17:07:56 -070078 const Ptr<const Packet> &packet)
79{
80 if (header->GetNack () > 0)
81 OnNack (incomingFace, header, packet/*original packet*/);
82 else
83 super::OnInterest (incomingFace, header, packet/*original packet*/);
84}
85
86void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087Nacks::OnNack (const Ptr<Face> &incomingFace,
88 Ptr<InterestHeader> &header,
Alexander Afanasyev786936a2012-07-17 19:48:15 -070089 const Ptr<const Packet> &packet)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070090{
Alexander Afanasyev786936a2012-07-17 19:48:15 -070091 NS_ASSERT (m_nacksEnabled);
92
93 // NS_LOG_FUNCTION (incomingFace << header << packet);
94 m_inNacks (header, incomingFace);
95
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 Ptr<pit::Entry> pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070097 if (pitEntry == 0)
98 {
99 // somebody is doing something bad
100 m_dropNacks (header, incomingFace);
101 return;
102 }
103
104 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
105 // at least when there is only one client
106 //
107 // incomingFace->LeakBucketByOnePacket ();
108
109 pitEntry->SetWaitingInVain (incomingFace);
110
111 DidReceiveValidNack (incomingFace, header->GetNack (), pitEntry);
112
113 if (!pitEntry->AreAllOutgoingInVain ()) // not all ougtoing are in vain
114 {
115 NS_LOG_DEBUG ("Not all outgoing are in vain");
116 // suppress
117 // Don't do anything, we are still expecting data from some other face
118 m_dropNacks (header, incomingFace);
119 return;
120 }
121
122 Ptr<Packet> nonNackInterest = Create<Packet> ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123 header->SetNack (InterestHeader::NORMAL_INTEREST);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700124 nonNackInterest->AddHeader (*header);
125
126 bool propagated = DoPropagateInterest (incomingFace, header, nonNackInterest, pitEntry);
127 if (!propagated)
128 {
129 DidExhaustForwardingOptions (incomingFace, header, nonNackInterest, pitEntry);
130 }
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700131}
132
133void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700134Nacks::DidReceiveDuplicateInterest (const Ptr<Face> &incomingFace,
135 Ptr<InterestHeader> &header,
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700136 const Ptr<const Packet> &packet,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700138{
139 super::DidReceiveDuplicateInterest (incomingFace, header, packet, pitEntry);
140
141 if (m_nacksEnabled)
142 {
143 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700144 header->SetNack (InterestHeader::NACK_LOOP);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700145 Ptr<Packet> nack = Create<Packet> ();
146 nack->AddHeader (*header);
147
148 incomingFace->Send (nack);
149 m_outNacks (header, incomingFace);
150 }
151}
152
153void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700154Nacks::DidExhaustForwardingOptions (const Ptr<Face> &incomingFace,
155 Ptr<InterestHeader> header,
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700156 const Ptr<const Packet> &packet,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700158{
159 super::DidExhaustForwardingOptions (incomingFace, header, packet, pitEntry);
160
161 if (m_nacksEnabled)
162 {
163 Ptr<Packet> packet = Create<Packet> ();
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164 header->SetNack (InterestHeader::NACK_GIVEUP_PIT);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700165 packet->AddHeader (*header);
166
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167 BOOST_FOREACH (const pit::IncomingFace &incoming, pitEntry->GetIncoming ())
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700168 {
169 NS_LOG_DEBUG ("Send NACK for " << boost::cref (header->GetName ()) << " to " << boost::cref (*incoming.m_face));
170 incoming.m_face->Send (packet->Copy ());
171
172 m_outNacks (header, incoming.m_face);
173 }
174
175 // All incoming interests cannot be satisfied. Remove them
176 pitEntry->ClearIncoming ();
177
178 // Remove also outgoing
179 pitEntry->ClearOutgoing ();
180
181 // Set pruning timout on PIT entry (instead of deleting the record)
182 m_pit->MarkErased (pitEntry);
183 }
184}
185
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700186void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187Nacks::DidReceiveValidNack (const Ptr<Face> &incomingFace,
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700188 uint32_t nackCode,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700190{
191 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
192 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193 if (nackCode == InterestHeader::NACK_GIVEUP_PIT)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700194 {
195 pitEntry->RemoveIncoming (incomingFace);
196 }
197
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700198 pitEntry->GetFibEntry ()->UpdateStatus (incomingFace, fib::FaceMetric::NDN_FIB_YELLOW);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700199}
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700200
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201} // namespace fw
202} // namespace ndn
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700203} // namespace ns3