blob: da8e352650c66c06ec472fe6d88e72c770fa32c6 [file] [log] [blame]
Eric Newberry69b63dc2017-11-04 15:42:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Eric Newberry69b63dc2017-11-04 15:42:46 -07004 * 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 Pesavento2c9d2ca2024-01-27 16:36:51 -050028#include <boost/lexical_cast.hpp>
29
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::fw {
Eric Newberry69b63dc2017-11-04 15:42:46 -070031
Eric Newberry69b63dc2017-11-04 15:42:46 -070032NFD_REGISTER_STRATEGY(CongestionMarkStrategy);
33
34CongestionMarkStrategy::CongestionMarkStrategy(Forwarder& forwarder, const Name& name)
Davide Pesavento7922d122021-03-05 22:41:23 -050035 // Specifying BestRouteStrategy's own name in its constructor prevents an exception from occuring
Eric Newberry78ef9012017-11-28 13:01:17 -070036 // when specifying parameters to CongestionMarkStrategy
Davide Pesavento7922d122021-03-05 22:41:23 -050037 : BestRouteStrategy(forwarder, BestRouteStrategy::getStrategyName())
Eric Newberry69b63dc2017-11-04 15:42:46 -070038{
39 ParsedInstanceName parsed = parseInstanceName(name);
40 switch (parsed.parameters.size()) {
41 case 2:
Eric Newberry4738f152017-12-18 13:10:32 -070042 if (parsed.parameters.at(1).toUri() != "true" && parsed.parameters.at(1).toUri() != "false") {
Davide Pesavento19779d82019-02-14 13:40:04 -050043 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070044 "Second parameter to CongestionMarkStrategy must be either 'true' or 'false'"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070045 }
Eric Newberry4738f152017-12-18 13:10:32 -070046 m_shouldPreserveMark = parsed.parameters.at(1).toUri() == "true";
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040047 [[fallthrough]];
Eric Newberry69b63dc2017-11-04 15:42:46 -070048 case 1:
Eric Newberry4738f152017-12-18 13:10:32 -070049 try {
50 auto s = parsed.parameters.at(0).toUri();
51 if (!s.empty() && s[0] == '-')
Davide Pesavento19779d82019-02-14 13:40:04 -050052 NDN_THROW(boost::bad_lexical_cast());
Eric Newberry4738f152017-12-18 13:10:32 -070053 m_congestionMark = boost::lexical_cast<uint64_t>(s);
Eric Newberry69b63dc2017-11-04 15:42:46 -070054 }
Eric Newberry4738f152017-12-18 13:10:32 -070055 catch (const boost::bad_lexical_cast&) {
Davide Pesavento19779d82019-02-14 13:40:04 -050056 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070057 "First parameter to CongestionMarkStrategy must be a non-negative integer"));
58 }
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040059 [[fallthrough]];
Eric Newberry69b63dc2017-11-04 15:42:46 -070060 case 0:
61 break;
62 default:
Davide Pesavento19779d82019-02-14 13:40:04 -050063 NDN_THROW(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070064 }
65
66 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050067 NDN_THROW(std::invalid_argument("CongestionMarkStrategy does not support version " +
68 std::to_string(*parsed.version)));
Eric Newberry69b63dc2017-11-04 15:42:46 -070069 }
70 this->setInstanceName(makeInstanceName(name, getStrategyName()));
71}
72
73const Name&
74CongestionMarkStrategy::getStrategyName()
75{
Eric Newberry358414d2021-03-21 20:56:42 -070076 static const auto strategyName = Name("/localhost/nfd/strategy/congestion-mark").appendVersion(1);
Eric Newberry69b63dc2017-11-04 15:42:46 -070077 return strategyName;
78}
79
80void
Davide Pesavento0498ce82021-06-14 02:02:21 -040081CongestionMarkStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Eric Newberry69b63dc2017-11-04 15:42:46 -070082 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 Pesavento0498ce82021-06-14 02:02:21 -040088 BestRouteStrategy::afterReceiveInterest(markedInterest, ingress, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070089 }
90 else {
Davide Pesavento0498ce82021-06-14 02:02:21 -040091 BestRouteStrategy::afterReceiveInterest(interest, ingress, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070092 }
93}
94
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040095} // namespace nfd::fw