blob: 4a4245cf5972068d187d119e47d2d461c6eefc03 [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 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 Afanasyev31cb4692012-08-17 13:08:20 -070083 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 Afanasyev31cb4692012-08-17 13:08:20 -070091 // NS_LOG_FUNCTION (inFace << header << origPacket);
92 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 }
101
102 // This was done in error. Never, never do anything, except normal leakage. This way we ensure that we will not have losses,
103 // at least when there is only one client
104 //
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700105 // inFace->LeakBucketByOnePacket ();
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700106
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700107 // pitEntry->SetWaitingInVain (inFace);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700108
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700109 DidReceiveValidNack (inFace, header->GetNack (), pitEntry);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700110
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700111 // if (!pitEntry->AreAllOutgoingInVain ()) // not all ougtoing are in vain
112 // {
113 // NS_LOG_DEBUG ("Not all outgoing are in vain");
114 // // suppress
115 // // Don't do anything, we are still expecting data from some other face
116 // m_dropNacks (header, inFace);
117 // return;
118 // }
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700119
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700120 // Ptr<Packet> nonNackInterest = Create<Packet> ();
121 // Ptr<InterestHeader> nonNackHeader = Create<InterestHeader> (*header);
122 // nonNackHeader->SetNack (InterestHeader::NORMAL_INTEREST);
123 // nonNackInterest->AddHeader (*nonNackHeader);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700124
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700125 // bool propagated = DoPropagateInterest (inFace, nonNackHeader, nonNackInterest, pitEntry);
126 // if (!propagated)
127 // {
128 // DidExhaustForwardingOptions (inFace, nonNackHeader, nonNackInterest, pitEntry);
129 // }
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700130}
131
132void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700133Nacks::DidReceiveDuplicateInterest (Ptr<Face> inFace,
134 Ptr<const InterestHeader> header,
135 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700136 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700137{
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700138 super::DidReceiveDuplicateInterest (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700139
140 if (m_nacksEnabled)
141 {
142 NS_LOG_DEBUG ("Sending NACK_LOOP");
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700143 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*header);
144 nackHeader->SetNack (InterestHeader::NACK_LOOP);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700145 Ptr<Packet> nack = Create<Packet> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700146 nack->AddHeader (*nackHeader);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700147
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700148 inFace->Send (nack);
149 m_outNacks (nackHeader, inFace);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700150 }
151}
152
153void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700154Nacks::DidExhaustForwardingOptions (Ptr<Face> inFace,
155 Ptr<const InterestHeader> header,
156 Ptr<const Packet> origPacket,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700158{
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700159 if (m_nacksEnabled)
160 {
161 Ptr<Packet> packet = Create<Packet> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700162 Ptr<InterestHeader> nackHeader = Create<InterestHeader> (*header);
163 nackHeader->SetNack (InterestHeader::NACK_GIVEUP_PIT);
164 packet->AddHeader (*nackHeader);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700165
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700166 BOOST_FOREACH (const pit::IncomingFace &incoming, pitEntry->GetIncoming ())
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700167 {
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700168 NS_LOG_DEBUG ("Send NACK for " << boost::cref (nackHeader->GetName ()) << " to " << boost::cref (*incoming.m_face));
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700169 incoming.m_face->Send (packet->Copy ());
170
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700171 m_outNacks (nackHeader, incoming.m_face);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700172 }
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700173 }
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700174
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700175 super::DidExhaustForwardingOptions (inFace, header, origPacket, pitEntry);
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700176}
177
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700178void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700179Nacks::DidReceiveValidNack (Ptr<Face> inFace,
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700180 uint32_t nackCode,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700182{
183 // 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 Afanasyev31cb4692012-08-17 13:08:20 -0700190 pitEntry->GetFibEntry ()->UpdateStatus (inFace, fib::FaceMetric::NDN_FIB_YELLOW);
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700191}
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700192
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700193} // namespace fw
194} // namespace ndn
Alexander Afanasyev996b4872012-07-17 17:07:56 -0700195} // namespace ns3