blob: 31673273d032d146dbcfb52bbb4139bc082cdab5 [file] [log] [blame]
Junxiao Shi99540072017-01-27 19:57:33 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "process-nack-traits.hpp"
27#include "core/logger.hpp"
28
29namespace nfd {
30namespace fw {
31
32NFD_LOG_INIT("ProcessNackTraits");
33
34/** \brief compare NackReason for severity
35 *
36 * lp::NackReason::NONE is treated as most severe
37 */
38static bool
39isLessSevere(lp::NackReason x, lp::NackReason y)
40{
41 if (x == lp::NackReason::NONE) {
42 return false;
43 }
44 if (y == lp::NackReason::NONE) {
45 return true;
46 }
47
48 return static_cast<int>(x) < static_cast<int>(y);
49}
50
51void
52ProcessNackTraitsBase::processNack(const Face& inFace, const lp::Nack& nack,
53 const shared_ptr<pit::Entry>& pitEntry)
54{
55 int nOutRecordsNotNacked = 0;
56 Face* lastFaceNotNacked = nullptr;
57 lp::NackReason leastSevereReason = lp::NackReason::NONE;
58 for (const pit::OutRecord& outR : pitEntry->getOutRecords()) {
59 const lp::NackHeader* inNack = outR.getIncomingNack();
60 if (inNack == nullptr) {
61 ++nOutRecordsNotNacked;
62 lastFaceNotNacked = &outR.getFace();
63 continue;
64 }
65
66 if (isLessSevere(inNack->getReason(), leastSevereReason)) {
67 leastSevereReason = inNack->getReason();
68 }
69 }
70
71 lp::NackHeader outNack;
72 outNack.setReason(leastSevereReason);
73
74 if (nOutRecordsNotNacked == 1) {
75 BOOST_ASSERT(lastFaceNotNacked != nullptr);
76 pit::InRecordCollection::iterator inR = pitEntry->getInRecord(*lastFaceNotNacked);
77 if (inR != pitEntry->in_end()) {
78 // one out-record not Nacked, which is also a downstream
79 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
80 " nack=" << nack.getReason() <<
81 " nack-to(bidirectional)=" << lastFaceNotNacked->getId() <<
82 " out-nack=" << outNack.getReason());
83 this->sendNackForProcessNackTraits(pitEntry, *lastFaceNotNacked, outNack);
84 return;
85 }
86 }
87
88 if (nOutRecordsNotNacked > 0) {
89 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
90 " nack=" << nack.getReason() <<
91 " waiting=" << nOutRecordsNotNacked);
92 // continue waiting
93 return;
94 }
95
96 NFD_LOG_DEBUG(nack.getInterest() << " nack-from=" << inFace.getId() <<
97 " nack=" << nack.getReason() <<
98 " nack-to=all out-nack=" << outNack.getReason());
99 this->sendNacksForProcessNackTraits(pitEntry, outNack);
100}
101
102} // namespace fw
103} // namespace nfd