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