blob: 00bf840dc9a659e2dc0d7ef9a2d0e86babfa9993 [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,
78 Ptr<const InterestHeader> header,
79 Ptr<const Packet> origPacket)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070080{
81 if (header->GetNack () > 0)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070082 OnNack (inFace, header, origPacket/*original packet*/);
Alexander Afanasyev996b4872012-07-17 17:07:56 -070083 else
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080084 super::OnInterest (inFace, header, origPacket/*original packet*/);
Alexander Afanasyev996b4872012-07-17 17:07:56 -070085}
86
87void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070088Nacks::OnNack (Ptr<Face> inFace,
89 Ptr<const InterestHeader> header,
90 Ptr<const Packet> origPacket)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070091{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080092 // NS_LOG_FUNCTION (inFace << header->GetName ());
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070093 m_inNacks (header, inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070094
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 Ptr<pit::Entry> pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070096 if (pitEntry == 0)
97 {
98 // somebody is doing something bad
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070099 m_dropNacks (header, inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700100 return;
101 }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700102
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700103 DidReceiveValidNack (inFace, header->GetNack (), header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700104}
105
106void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700107Nacks::DidReceiveDuplicateInterest (Ptr<Face> inFace,
108 Ptr<const InterestHeader> header,
109 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700111{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700112 super::DidReceiveDuplicateInterest (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700113
114 if (m_nacksEnabled)
115 {
116 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700117 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*header);
118 nackHeader->SetNack (InterestHeader::NACK_LOOP);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700119 Ptr<Packet> nack = Create<Packet> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700120 nack->AddHeader (*nackHeader);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700121
Xiaoke Jianga7325412013-03-10 00:25:12 +0800122 FwHopCountTag hopCountTag;
123 if (origPacket->PeekPacketTag (hopCountTag))
124 {
125 nack->AddPacketTag (hopCountTag);
126 }
127 else
128 {
129 NS_LOG_DEBUG ("No FwHopCountTag tag associated with received duplicated Interest");
130 }
131
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700132 inFace->Send (nack);
133 m_outNacks (nackHeader, inFace);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700134 }
135}
136
137void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700138Nacks::DidExhaustForwardingOptions (Ptr<Face> inFace,
139 Ptr<const InterestHeader> header,
140 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700141 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700142{
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700143 if (m_nacksEnabled)
144 {
145 Ptr<Packet> packet = Create<Packet> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700146 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*header);
147 nackHeader->SetNack (InterestHeader::NACK_GIVEUP_PIT);
148 packet->AddHeader (*nackHeader);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700149
Xiaoke Jianga7325412013-03-10 00:25:12 +0800150 FwHopCountTag hopCountTag;
151 if (origPacket->PeekPacketTag (hopCountTag))
152 {
153 packet->AddPacketTag (hopCountTag);
154 }
155 else
156 {
157 NS_LOG_DEBUG ("No FwHopCountTag tag associated with original Interest");
158 }
159
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700160 BOOST_FOREACH (const pit::IncomingFace &incoming, pitEntry->GetIncoming ())
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700161 {
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700162 NS_LOG_DEBUG ("Send NACK for " << boost::cref (nackHeader->GetName ()) << " to " << boost::cref (*incoming.m_face));
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700163 incoming.m_face->Send (packet->Copy ());
164
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700165 m_outNacks (nackHeader, incoming.m_face);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700166 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800167
168 pitEntry->ClearOutgoing (); // to force erasure of the record
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700169 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800170
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700171 super::DidExhaustForwardingOptions (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700172}
173
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700174void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700175Nacks::DidReceiveValidNack (Ptr<Face> inFace,
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700176 uint32_t nackCode,
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700177 Ptr<const InterestHeader> header,
178 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700179 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700180{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800181 NS_LOG_DEBUG ("nackCode: " << nackCode << " for [" << header->GetName () << "]");
182
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700183 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
184 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185 if (nackCode == InterestHeader::NACK_GIVEUP_PIT)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700186 {
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700187 pitEntry->RemoveIncoming (inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700188 }
189
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700190 if (nackCode == InterestHeader::NACK_LOOP ||
191 nackCode == InterestHeader::NACK_CONGESTION ||
192 nackCode == InterestHeader::NACK_GIVEUP_PIT)
193 {
194 pitEntry->SetWaitingInVain (inFace);
195
196 if (!pitEntry->AreAllOutgoingInVain ()) // not all ougtoing are in vain
197 {
198 NS_LOG_DEBUG ("Not all outgoing are in vain");
199 // suppress
200 // Don't do anything, we are still expecting data from some other face
201 m_dropNacks (header, inFace);
202 return;
203 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800204
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700205 Ptr<Packet> nonNackInterest = Create<Packet> ();
206 Ptr<InterestHeader> nonNackHeader = Create<InterestHeader> (*header);
207 nonNackHeader->SetNack (InterestHeader::NORMAL_INTEREST);
208 nonNackInterest->AddHeader (*nonNackHeader);
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800209
Xiaoke Jianga7325412013-03-10 00:25:12 +0800210 FwHopCountTag hopCountTag;
211 if (origPacket->PeekPacketTag (hopCountTag))
212 {
213 nonNackInterest->AddPacketTag (hopCountTag);
214 }
215 else
216 {
217 NS_LOG_DEBUG ("No FwHopCountTag tag associated with received NACK");
218 }
219
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700220 bool propagated = DoPropagateInterest (inFace, nonNackHeader, nonNackInterest, pitEntry);
221 if (!propagated)
222 {
223 DidExhaustForwardingOptions (inFace, nonNackHeader, nonNackInterest, pitEntry);
224 }
225 }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700226}
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700227
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700228} // namespace fw
229} // namespace ndn
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700230} // namespace ns3