blob: 672e9ef1e9ddfe809d8d5080ffb9a1d5c547bdc0 [file] [log] [blame]
Eric Newberry69b63dc2017-11-04 15:42:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento7922d122021-03-05 22:41:23 -05003 * Copyright (c) 2014-2021, 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
Eric Newberry69b63dc2017-11-04 15:42:46 -070028namespace nfd {
29namespace fw {
30
Eric Newberry69b63dc2017-11-04 15:42:46 -070031NFD_REGISTER_STRATEGY(CongestionMarkStrategy);
32
33CongestionMarkStrategy::CongestionMarkStrategy(Forwarder& forwarder, const Name& name)
Davide Pesavento7922d122021-03-05 22:41:23 -050034 // Specifying BestRouteStrategy's own name in its constructor prevents an exception from occuring
Eric Newberry78ef9012017-11-28 13:01:17 -070035 // when specifying parameters to CongestionMarkStrategy
Davide Pesavento7922d122021-03-05 22:41:23 -050036 : BestRouteStrategy(forwarder, BestRouteStrategy::getStrategyName())
Eric Newberry69b63dc2017-11-04 15:42:46 -070037{
38 ParsedInstanceName parsed = parseInstanceName(name);
39 switch (parsed.parameters.size()) {
40 case 2:
Eric Newberry4738f152017-12-18 13:10:32 -070041 if (parsed.parameters.at(1).toUri() != "true" && parsed.parameters.at(1).toUri() != "false") {
Davide Pesavento19779d82019-02-14 13:40:04 -050042 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070043 "Second parameter to CongestionMarkStrategy must be either 'true' or 'false'"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070044 }
Eric Newberry4738f152017-12-18 13:10:32 -070045 m_shouldPreserveMark = parsed.parameters.at(1).toUri() == "true";
Eric Newberry69b63dc2017-11-04 15:42:46 -070046 NDN_CXX_FALLTHROUGH;
47 case 1:
Eric Newberry4738f152017-12-18 13:10:32 -070048 try {
49 auto s = parsed.parameters.at(0).toUri();
50 if (!s.empty() && s[0] == '-')
Davide Pesavento19779d82019-02-14 13:40:04 -050051 NDN_THROW(boost::bad_lexical_cast());
Eric Newberry4738f152017-12-18 13:10:32 -070052 m_congestionMark = boost::lexical_cast<uint64_t>(s);
Eric Newberry69b63dc2017-11-04 15:42:46 -070053 }
Eric Newberry4738f152017-12-18 13:10:32 -070054 catch (const boost::bad_lexical_cast&) {
Davide Pesavento19779d82019-02-14 13:40:04 -050055 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070056 "First parameter to CongestionMarkStrategy must be a non-negative integer"));
57 }
Eric Newberry69b63dc2017-11-04 15:42:46 -070058 NDN_CXX_FALLTHROUGH;
59 case 0:
60 break;
61 default:
Davide Pesavento19779d82019-02-14 13:40:04 -050062 NDN_THROW(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070063 }
64
65 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050066 NDN_THROW(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050067 "CongestionMarkStrategy does not support version " + to_string(*parsed.version)));
Eric Newberry69b63dc2017-11-04 15:42:46 -070068 }
69 this->setInstanceName(makeInstanceName(name, getStrategyName()));
70}
71
72const Name&
73CongestionMarkStrategy::getStrategyName()
74{
75 static Name strategyName("/localhost/nfd/strategy/congestion-mark/%FD%01");
76 return strategyName;
77}
78
79void
ashiqopuc7079482019-02-20 05:34:37 +000080CongestionMarkStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
Eric Newberry69b63dc2017-11-04 15:42:46 -070081 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 Pesavento7922d122021-03-05 22:41:23 -050087 BestRouteStrategy::afterReceiveInterest(ingress, markedInterest, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070088 }
89 else {
Davide Pesavento7922d122021-03-05 22:41:23 -050090 BestRouteStrategy::afterReceiveInterest(ingress, interest, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070091 }
92}
93
94} // namespace fw
95} // namespace nfd