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