blob: 8c24157b6cef030a5ad6ef72904cb5dd14fe3731 [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"
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 Afanasyev3a3ce1a2013-01-31 11:26:11 -080059
Alexander Afanasyev996b4872012-07-17 17:07:56 -070060 ////////////////////////////////////////////////////////////////////
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))
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080066
Alexander Afanasyev996b4872012-07-17 17:07:56 -070067 .AddAttribute ("EnableNACKs", "Enabling support of NACKs",
68 BooleanValue (false),
69 MakeBooleanAccessor (&Nacks::m_nacksEnabled),
70 MakeBooleanChecker ())
71 ;
72 return tid;
73}
74
75void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070076Nacks::OnInterest (Ptr<Face> inFace,
77 Ptr<const InterestHeader> header,
78 Ptr<const Packet> origPacket)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070079{
80 if (header->GetNack () > 0)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070081 OnNack (inFace, header, origPacket/*original packet*/);
Alexander Afanasyev996b4872012-07-17 17:07:56 -070082 else
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080083 super::OnInterest (inFace, header, origPacket/*original packet*/);
Alexander Afanasyev996b4872012-07-17 17:07:56 -070084}
85
86void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070087Nacks::OnNack (Ptr<Face> inFace,
88 Ptr<const InterestHeader> header,
89 Ptr<const Packet> origPacket)
Alexander Afanasyev996b4872012-07-17 17:07:56 -070090{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080091 // NS_LOG_FUNCTION (inFace << header->GetName ());
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070092 m_inNacks (header, inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070093
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094 Ptr<pit::Entry> pitEntry = m_pit->Lookup (*header);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070095 if (pitEntry == 0)
96 {
97 // somebody is doing something bad
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070098 m_dropNacks (header, inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -070099 return;
100 }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700101
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700102 DidReceiveValidNack (inFace, header->GetNack (), header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700103}
104
105void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700106Nacks::DidReceiveDuplicateInterest (Ptr<Face> inFace,
107 Ptr<const InterestHeader> header,
108 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700109 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700110{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700111 super::DidReceiveDuplicateInterest (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700112
113 if (m_nacksEnabled)
114 {
115 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700116 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*header);
117 nackHeader->SetNack (InterestHeader::NACK_LOOP);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700118 Ptr<Packet> nack = Create<Packet> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700119 nack->AddHeader (*nackHeader);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700120
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700121 inFace->Send (nack);
122 m_outNacks (nackHeader, inFace);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700123 }
124}
125
126void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700127Nacks::DidExhaustForwardingOptions (Ptr<Face> inFace,
128 Ptr<const InterestHeader> header,
129 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700131{
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700132 if (m_nacksEnabled)
133 {
134 Ptr<Packet> packet = Create<Packet> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700135 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*header);
136 nackHeader->SetNack (InterestHeader::NACK_GIVEUP_PIT);
137 packet->AddHeader (*nackHeader);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700138
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700139 BOOST_FOREACH (const pit::IncomingFace &incoming, pitEntry->GetIncoming ())
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700140 {
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700141 NS_LOG_DEBUG ("Send NACK for " << boost::cref (nackHeader->GetName ()) << " to " << boost::cref (*incoming.m_face));
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700142 incoming.m_face->Send (packet->Copy ());
143
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700144 m_outNacks (nackHeader, incoming.m_face);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700145 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800146
147 pitEntry->ClearOutgoing (); // to force erasure of the record
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700148 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800149
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700150 super::DidExhaustForwardingOptions (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700151}
152
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700153void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700154Nacks::DidReceiveValidNack (Ptr<Face> inFace,
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700155 uint32_t nackCode,
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700156 Ptr<const InterestHeader> header,
157 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700158 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700159{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800160 NS_LOG_DEBUG ("nackCode: " << nackCode << " for [" << header->GetName () << "]");
161
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700162 // If NACK is NACK_GIVEUP_PIT, then neighbor gave up trying to and removed it's PIT entry.
163 // So, if we had an incoming entry to this neighbor, then we can remove it now
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164 if (nackCode == InterestHeader::NACK_GIVEUP_PIT)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700165 {
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700166 pitEntry->RemoveIncoming (inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700167 }
168
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700169 if (nackCode == InterestHeader::NACK_LOOP ||
170 nackCode == InterestHeader::NACK_CONGESTION ||
171 nackCode == InterestHeader::NACK_GIVEUP_PIT)
172 {
173 pitEntry->SetWaitingInVain (inFace);
174
175 if (!pitEntry->AreAllOutgoingInVain ()) // not all ougtoing are in vain
176 {
177 NS_LOG_DEBUG ("Not all outgoing are in vain");
178 // suppress
179 // Don't do anything, we are still expecting data from some other face
180 m_dropNacks (header, inFace);
181 return;
182 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800183
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700184 Ptr<Packet> nonNackInterest = Create<Packet> ();
185 Ptr<InterestHeader> nonNackHeader = Create<InterestHeader> (*header);
186 nonNackHeader->SetNack (InterestHeader::NORMAL_INTEREST);
187 nonNackInterest->AddHeader (*nonNackHeader);
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800188
Alexander Afanasyev0484e772012-10-29 11:02:08 -0700189 bool propagated = DoPropagateInterest (inFace, nonNackHeader, nonNackInterest, pitEntry);
190 if (!propagated)
191 {
192 DidExhaustForwardingOptions (inFace, nonNackHeader, nonNackInterest, pitEntry);
193 }
194 }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700195}
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700196
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700197} // namespace fw
198} // namespace ndn
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700199} // namespace ns3