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