blob: 078799bb8f7b500225aa84e2f510d5d03b7e920a [file] [log] [blame]
Eric Newberry69b63dc2017-11-04 15:42:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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 Pesaventoe422f9e2022-06-03 01:30:23 -040028namespace nfd::fw {
Eric Newberry69b63dc2017-11-04 15:42:46 -070029
Eric Newberry69b63dc2017-11-04 15:42:46 -070030NFD_REGISTER_STRATEGY(CongestionMarkStrategy);
31
32CongestionMarkStrategy::CongestionMarkStrategy(Forwarder& forwarder, const Name& name)
Davide Pesavento7922d122021-03-05 22:41:23 -050033 // Specifying BestRouteStrategy's own name in its constructor prevents an exception from occuring
Eric Newberry78ef9012017-11-28 13:01:17 -070034 // when specifying parameters to CongestionMarkStrategy
Davide Pesavento7922d122021-03-05 22:41:23 -050035 : BestRouteStrategy(forwarder, BestRouteStrategy::getStrategyName())
Eric Newberry69b63dc2017-11-04 15:42:46 -070036{
37 ParsedInstanceName parsed = parseInstanceName(name);
38 switch (parsed.parameters.size()) {
39 case 2:
Eric Newberry4738f152017-12-18 13:10:32 -070040 if (parsed.parameters.at(1).toUri() != "true" && parsed.parameters.at(1).toUri() != "false") {
Davide Pesavento19779d82019-02-14 13:40:04 -050041 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070042 "Second parameter to CongestionMarkStrategy must be either 'true' or 'false'"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070043 }
Eric Newberry4738f152017-12-18 13:10:32 -070044 m_shouldPreserveMark = parsed.parameters.at(1).toUri() == "true";
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040045 [[fallthrough]];
Eric Newberry69b63dc2017-11-04 15:42:46 -070046 case 1:
Eric Newberry4738f152017-12-18 13:10:32 -070047 try {
48 auto s = parsed.parameters.at(0).toUri();
49 if (!s.empty() && s[0] == '-')
Davide Pesavento19779d82019-02-14 13:40:04 -050050 NDN_THROW(boost::bad_lexical_cast());
Eric Newberry4738f152017-12-18 13:10:32 -070051 m_congestionMark = boost::lexical_cast<uint64_t>(s);
Eric Newberry69b63dc2017-11-04 15:42:46 -070052 }
Eric Newberry4738f152017-12-18 13:10:32 -070053 catch (const boost::bad_lexical_cast&) {
Davide Pesavento19779d82019-02-14 13:40:04 -050054 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070055 "First parameter to CongestionMarkStrategy must be a non-negative integer"));
56 }
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040057 [[fallthrough]];
Eric Newberry69b63dc2017-11-04 15:42:46 -070058 case 0:
59 break;
60 default:
Davide Pesavento19779d82019-02-14 13:40:04 -050061 NDN_THROW(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070062 }
63
64 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050065 NDN_THROW(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050066 "CongestionMarkStrategy does not support version " + to_string(*parsed.version)));
Eric Newberry69b63dc2017-11-04 15:42:46 -070067 }
68 this->setInstanceName(makeInstanceName(name, getStrategyName()));
69}
70
71const Name&
72CongestionMarkStrategy::getStrategyName()
73{
Eric Newberry358414d2021-03-21 20:56:42 -070074 static const auto strategyName = Name("/localhost/nfd/strategy/congestion-mark").appendVersion(1);
Eric Newberry69b63dc2017-11-04 15:42:46 -070075 return strategyName;
76}
77
78void
Davide Pesavento0498ce82021-06-14 02:02:21 -040079CongestionMarkStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress,
Eric Newberry69b63dc2017-11-04 15:42:46 -070080 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 Pesavento0498ce82021-06-14 02:02:21 -040086 BestRouteStrategy::afterReceiveInterest(markedInterest, ingress, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070087 }
88 else {
Davide Pesavento0498ce82021-06-14 02:02:21 -040089 BestRouteStrategy::afterReceiveInterest(interest, ingress, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070090 }
91}
92
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040093} // namespace nfd::fw