Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -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 | |
| 26 | #include "congestion-mark-strategy.hpp" |
| 27 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 28 | namespace nfd::fw { |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 29 | |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 30 | NFD_REGISTER_STRATEGY(CongestionMarkStrategy); |
| 31 | |
| 32 | CongestionMarkStrategy::CongestionMarkStrategy(Forwarder& forwarder, const Name& name) |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 33 | // Specifying BestRouteStrategy's own name in its constructor prevents an exception from occuring |
Eric Newberry | 78ef901 | 2017-11-28 13:01:17 -0700 | [diff] [blame] | 34 | // when specifying parameters to CongestionMarkStrategy |
Davide Pesavento | 7922d12 | 2021-03-05 22:41:23 -0500 | [diff] [blame] | 35 | : BestRouteStrategy(forwarder, BestRouteStrategy::getStrategyName()) |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 36 | { |
| 37 | ParsedInstanceName parsed = parseInstanceName(name); |
| 38 | switch (parsed.parameters.size()) { |
| 39 | case 2: |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 40 | if (parsed.parameters.at(1).toUri() != "true" && parsed.parameters.at(1).toUri() != "false") { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 41 | NDN_THROW(std::invalid_argument( |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 42 | "Second parameter to CongestionMarkStrategy must be either 'true' or 'false'")); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 43 | } |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 44 | m_shouldPreserveMark = parsed.parameters.at(1).toUri() == "true"; |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 45 | [[fallthrough]]; |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 46 | case 1: |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 47 | try { |
| 48 | auto s = parsed.parameters.at(0).toUri(); |
| 49 | if (!s.empty() && s[0] == '-') |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 50 | NDN_THROW(boost::bad_lexical_cast()); |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 51 | m_congestionMark = boost::lexical_cast<uint64_t>(s); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 52 | } |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 53 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 54 | NDN_THROW(std::invalid_argument( |
Eric Newberry | 4738f15 | 2017-12-18 13:10:32 -0700 | [diff] [blame] | 55 | "First parameter to CongestionMarkStrategy must be a non-negative integer")); |
| 56 | } |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 57 | [[fallthrough]]; |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 58 | case 0: |
| 59 | break; |
| 60 | default: |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 61 | NDN_THROW(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters")); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 65 | NDN_THROW(std::invalid_argument( |
Alexander Afanasyev | 0c63c63 | 2017-12-05 11:17:09 -0500 | [diff] [blame] | 66 | "CongestionMarkStrategy does not support version " + to_string(*parsed.version))); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 67 | } |
| 68 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
| 69 | } |
| 70 | |
| 71 | const Name& |
| 72 | CongestionMarkStrategy::getStrategyName() |
| 73 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 74 | static const auto strategyName = Name("/localhost/nfd/strategy/congestion-mark").appendVersion(1); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 75 | return strategyName; |
| 76 | } |
| 77 | |
| 78 | void |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 79 | CongestionMarkStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 80 | const shared_ptr<pit::Entry>& pitEntry) |
| 81 | { |
| 82 | auto mark = interest.getCongestionMark(); |
| 83 | if (mark != m_congestionMark && (!m_shouldPreserveMark || mark == 0)) { |
| 84 | Interest markedInterest(interest); |
| 85 | markedInterest.setCongestionMark(m_congestionMark); |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 86 | BestRouteStrategy::afterReceiveInterest(markedInterest, ingress, pitEntry); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 87 | } |
| 88 | else { |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 89 | BestRouteStrategy::afterReceiveInterest(interest, ingress, pitEntry); |
Eric Newberry | 69b63dc | 2017-11-04 15:42:46 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 93 | } // namespace nfd::fw |