blob: 357427cbf40830ece060e6c82965959305d55475 [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"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080025#include "ns3/ndn-interest.h"
26#include "ns3/ndn-content-object.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070027#include "ns3/ndn-pit.h"
28#include "ns3/ndn-fib.h"
29#include "ns3/ndn-content-store.h"
Xiaoke Jianga7325412013-03-10 00:25:12 +080030#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.h"
Alexander Afanasyev996b4872012-07-17 17:07:56 -070031
32#include "ns3/assert.h"
33#include "ns3/ptr.h"
34#include "ns3/log.h"
35#include "ns3/simulator.h"
36#include "ns3/boolean.h"
37#include "ns3/string.h"
38
39#include <boost/ref.hpp>
40#include <boost/foreach.hpp>
41#include <boost/lambda/lambda.hpp>
42#include <boost/lambda/bind.hpp>
43#include <boost/tuple/tuple.hpp>
44namespace ll = boost::lambda;
45
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046NS_LOG_COMPONENT_DEFINE ("ndn.fw.Nacks");
Alexander Afanasyev996b4872012-07-17 17:07:56 -070047
48namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049namespace ndn {
50namespace fw {
Alexander Afanasyev996b4872012-07-17 17:07:56 -070051
52NS_OBJECT_ENSURE_REGISTERED (Nacks);
53
54TypeId
55Nacks::GetTypeId (void)
56{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057 static TypeId tid = TypeId ("ns3::ndn::fw::Nacks")
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070058 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 .SetParent<ForwardingStrategy> ()
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080060
Alexander Afanasyev996b4872012-07-17 17:07:56 -070061 ////////////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////////////
63
64 .AddTraceSource ("OutNacks", "OutNacks", MakeTraceSourceAccessor (&Nacks::m_outNacks))
65 .AddTraceSource ("InNacks", "InNacks", MakeTraceSourceAccessor (&Nacks::m_inNacks))
66 .AddTraceSource ("DropNacks", "DropNacks", MakeTraceSourceAccessor (&Nacks::m_dropNacks))
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080067
Alexander Afanasyev996b4872012-07-17 17:07:56 -070068 .AddAttribute ("EnableNACKs", "Enabling support of NACKs",
69 BooleanValue (false),
70 MakeBooleanAccessor (&Nacks::m_nacksEnabled),
71 MakeBooleanChecker ())
72 ;
73 return tid;
74}
75
76void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070077Nacks::OnInterest (Ptr<Face> inFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -070078 Ptr<Interest> interest)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070079{
Alexander Afanasyevb989b122013-07-10 17:15:46 -070080 if (interest->GetNack () > 0)
81 OnNack (inFace, header);
Alexander Afanasyev996b4872012-07-17 17:07:56 -070082 else
Alexander Afanasyevb989b122013-07-10 17:15:46 -070083 super::OnInterest (inFace, interest);
Alexander Afanasyev996b4872012-07-17 17:07:56 -070084}
85
86void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070087Nacks::OnNack (Ptr<Face> inFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -070088 Ptr<Interest> nack)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070089{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080090 // NS_LOG_FUNCTION (inFace << header->GetName ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -070091 m_inNacks (nack, inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070092
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093 Ptr<pit::Entry> pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070094 if (pitEntry == 0)
95 {
96 // somebody is doing something bad
Alexander Afanasyevb989b122013-07-10 17:15:46 -070097 m_dropNacks (nack, inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070098 return;
99 }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700100
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700101 DidReceiveValidNack (inFace, header->GetNack (), nack, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700102}
103
104void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700105Nacks::DidReceiveDuplicateInterest (Ptr<Face> inFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700106 Ptr<const Interest> interest,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700108{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700109 super::DidReceiveDuplicateInterest (inFace, interest, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700110
111 if (m_nacksEnabled)
112 {
113 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700114 Ptr<Interest> nack = Create<Interest> (*interest);
115 nack->SetNack (Interest::NACK_LOOP);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700116
Xiaoke Jianga7325412013-03-10 00:25:12 +0800117 FwHopCountTag hopCountTag;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700118 if (interest->GetPayload ()->PeekPacketTag (hopCountTag))
Xiaoke Jianga7325412013-03-10 00:25:12 +0800119 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700120 nack->GetPayload ()->AddPacketTag (hopCountTag);
Xiaoke Jianga7325412013-03-10 00:25:12 +0800121 }
122 else
123 {
124 NS_LOG_DEBUG ("No FwHopCountTag tag associated with received duplicated Interest");
125 }
126
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700127 inFace->SendInterest (nack);
128 m_outNacks (nack, inFace);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700129 }
130}
131
132void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700133Nacks::DidExhaustForwardingOptions (Ptr<Face> inFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700134 Ptr<const Interest> interest,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700135 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700136{
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700137 if (m_nacksEnabled)
138 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700139 Ptr<Interest> nack = Create<Interest> (*header);
140 nack->SetNack (Interest::NACK_GIVEUP_PIT);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700141
Xiaoke Jianga7325412013-03-10 00:25:12 +0800142 FwHopCountTag hopCountTag;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700143 if (interest->GetPayload ()->PeekPacketTag (hopCountTag))
Xiaoke Jianga7325412013-03-10 00:25:12 +0800144 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700145 nack->GetPayload ()->AddPacketTag (hopCountTag);
Xiaoke Jianga7325412013-03-10 00:25:12 +0800146 }
147 else
148 {
149 NS_LOG_DEBUG ("No FwHopCountTag tag associated with original Interest");
150 }
151
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 BOOST_FOREACH (const pit::IncomingFace &incoming, pitEntry->GetIncoming ())
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700153 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700154 NS_LOG_DEBUG ("Send NACK for " << boost::cref (nack->GetName ()) << " to " << boost::cref (*incoming.m_face));
155 incoming.m_face->SendInterest (nack);
156 m_outNacks (nack, incoming.m_face);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700157 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800158
159 pitEntry->ClearOutgoing (); // to force erasure of the record
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700160 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800161
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700162 super::DidExhaustForwardingOptions (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700163}
164
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700165void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700166Nacks::DidReceiveValidNack (Ptr<Face> inFace,
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700167 uint32_t nackCode,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700168 Ptr<const Interest> nack,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700169 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700170{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700171 NS_LOG_DEBUG ("nackCode: " << nackCode << " for [" << nack->GetName () << "]");
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800172
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700173 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
174 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700175 if (nackCode == Interest::NACK_GIVEUP_PIT)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700176 {
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700177 pitEntry->RemoveIncoming (inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700178 }
179
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700180 if (nackCode == Interest::NACK_LOOP ||
181 nackCode == Interest::NACK_CONGESTION ||
182 nackCode == Interest::NACK_GIVEUP_PIT)
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700183 {
184 pitEntry->SetWaitingInVain (inFace);
185
186 if (!pitEntry->AreAllOutgoingInVain ()) // not all ougtoing are in vain
187 {
188 NS_LOG_DEBUG ("Not all outgoing are in vain");
189 // suppress
190 // Don't do anything, we are still expecting data from some other face
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700191 m_dropNacks (nack, inFace);
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700192 return;
193 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800194
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700195 Ptr<Interest> interest = Create<Interest> (*nack);
196 interest->SetNack (Interest::NORMAL_INTEREST);
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800197
Xiaoke Jianga7325412013-03-10 00:25:12 +0800198 FwHopCountTag hopCountTag;
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700199 if (nack->GetPayload ()->PeekPacketTag (hopCountTag))
Xiaoke Jianga7325412013-03-10 00:25:12 +0800200 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700201 interest->GetPayload ()->AddPacketTag (hopCountTag);
Xiaoke Jianga7325412013-03-10 00:25:12 +0800202 }
203 else
204 {
205 NS_LOG_DEBUG ("No FwHopCountTag tag associated with received NACK");
206 }
207
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700208 bool propagated = DoPropagateInterest (inFace, interest, pitEntry);
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700209 if (!propagated)
210 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700211 DidExhaustForwardingOptions (inFace, interest, pitEntry);
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700212 }
213 }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700214}
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700215
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700216} // namespace fw
217} // namespace ndn
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700218} // namespace ns3