Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 26 | #include "algorithm.hpp" |
Davide Pesavento | 5809158 | 2021-03-05 19:02:48 -0500 | [diff] [blame] | 27 | #include "scope-prefix.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 29 | namespace nfd::fw { |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 30 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 31 | bool |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 32 | wouldViolateScope(const Face& inFace, const Interest& interest, const Face& outFace) |
| 33 | { |
| 34 | if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 35 | // forwarding to a local face is always allowed |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | if (scope_prefix::LOCALHOST.isPrefixOf(interest.getName())) { |
| 40 | // localhost Interests cannot be forwarded to a non-local face |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | if (scope_prefix::LOCALHOP.isPrefixOf(interest.getName())) { |
| 45 | // localhop Interests can be forwarded to a non-local face only if it comes from a local face |
| 46 | return inFace.getScope() != ndn::nfd::FACE_SCOPE_LOCAL; |
| 47 | } |
| 48 | |
| 49 | // Interest name is not subject to scope control |
| 50 | return false; |
| 51 | } |
| 52 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 53 | int |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 54 | findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face) |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 55 | { |
| 56 | int dnw = DUPLICATE_NONCE_NONE; |
| 57 | |
| 58 | for (const pit::InRecord& inRecord : pitEntry.getInRecords()) { |
| 59 | if (inRecord.getLastNonce() == nonce) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 60 | if (&inRecord.getFace() == &face) { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 61 | dnw |= DUPLICATE_NONCE_IN_SAME; |
| 62 | } |
| 63 | else { |
| 64 | dnw |= DUPLICATE_NONCE_IN_OTHER; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) { |
| 70 | if (outRecord.getLastNonce() == nonce) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 71 | if (&outRecord.getFace() == &face) { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 72 | dnw |= DUPLICATE_NONCE_OUT_SAME; |
| 73 | } |
| 74 | else { |
| 75 | dnw |= DUPLICATE_NONCE_OUT_OTHER; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return dnw; |
| 81 | } |
| 82 | |
| 83 | bool |
| 84 | hasPendingOutRecords(const pit::Entry& pitEntry) |
| 85 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 86 | auto now = time::steady_clock::now(); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 87 | return std::any_of(pitEntry.out_begin(), pitEntry.out_end(), |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 88 | [&now] (const pit::OutRecord& outRecord) { |
| 89 | return outRecord.getExpiry() >= now && |
| 90 | outRecord.getIncomingNack() == nullptr; |
| 91 | }); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 94 | time::steady_clock::time_point |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 95 | getLastOutgoing(const pit::Entry& pitEntry) |
| 96 | { |
| 97 | pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element( |
| 98 | pitEntry.out_begin(), pitEntry.out_end(), |
| 99 | [] (const pit::OutRecord& a, const pit::OutRecord& b) { |
| 100 | return a.getLastRenewed() < b.getLastRenewed(); |
| 101 | }); |
| 102 | BOOST_ASSERT(lastOutgoing != pitEntry.out_end()); |
| 103 | |
| 104 | return lastOutgoing->getLastRenewed(); |
| 105 | } |
| 106 | |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 107 | fib::NextHopList::const_iterator |
| 108 | findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest, |
| 109 | const fib::NextHopList& nexthops, |
| 110 | const shared_ptr<pit::Entry>& pitEntry) |
| 111 | { |
| 112 | auto found = nexthops.end(); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 113 | auto earliestRenewed = time::steady_clock::time_point::max(); |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 114 | |
| 115 | for (auto it = nexthops.begin(); it != nexthops.end(); ++it) { |
| 116 | if (!isNextHopEligible(inFace, interest, *it, pitEntry)) |
| 117 | continue; |
| 118 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 119 | auto outRecord = pitEntry->getOutRecord(it->getFace()); |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 120 | BOOST_ASSERT(outRecord != pitEntry->out_end()); |
| 121 | if (outRecord->getLastRenewed() < earliestRenewed) { |
| 122 | found = it; |
| 123 | earliestRenewed = outRecord->getLastRenewed(); |
| 124 | } |
| 125 | } |
| 126 | return found; |
| 127 | } |
| 128 | |
| 129 | bool |
| 130 | isNextHopEligible(const Face& inFace, const Interest& interest, |
| 131 | const fib::NextHop& nexthop, |
| 132 | const shared_ptr<pit::Entry>& pitEntry, |
| 133 | bool wantUnused, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 134 | time::steady_clock::time_point now) |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 135 | { |
| 136 | const Face& outFace = nexthop.getFace(); |
| 137 | |
| 138 | // do not forward back to the same face, unless it is ad hoc |
| 139 | if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) || |
| 140 | (wouldViolateScope(inFace, interest, outFace))) |
| 141 | return false; |
| 142 | |
| 143 | if (wantUnused) { |
| 144 | // nexthop must not have unexpired out-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 145 | auto outRecord = pitEntry->getOutRecord(outFace); |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 146 | if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) { |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 153 | } // namespace nfd::fw |