blob: 9543fda6497771ef8edfc42c8b7b70180e26f4f1 [file] [log] [blame]
Eric Newberry69b63dc2017-11-04 15:42:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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)
Eric Newberry78ef9012017-11-28 13:01:17 -070034 // 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 Newberry69b63dc2017-11-04 15:42:46 -070037 , m_congestionMark(1)
38 , m_shouldPreserveMark(true)
39{
40 ParsedInstanceName parsed = parseInstanceName(name);
41 switch (parsed.parameters.size()) {
42 case 2:
Eric Newberry4738f152017-12-18 13:10:32 -070043 if (parsed.parameters.at(1).toUri() != "true" && parsed.parameters.at(1).toUri() != "false") {
Davide Pesavento19779d82019-02-14 13:40:04 -050044 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070045 "Second parameter to CongestionMarkStrategy must be either 'true' or 'false'"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070046 }
Eric Newberry4738f152017-12-18 13:10:32 -070047 m_shouldPreserveMark = parsed.parameters.at(1).toUri() == "true";
Eric Newberry69b63dc2017-11-04 15:42:46 -070048 NDN_CXX_FALLTHROUGH;
49 case 1:
Eric Newberry4738f152017-12-18 13:10:32 -070050 try {
51 auto s = parsed.parameters.at(0).toUri();
52 if (!s.empty() && s[0] == '-')
Davide Pesavento19779d82019-02-14 13:40:04 -050053 NDN_THROW(boost::bad_lexical_cast());
Eric Newberry4738f152017-12-18 13:10:32 -070054 m_congestionMark = boost::lexical_cast<uint64_t>(s);
Eric Newberry69b63dc2017-11-04 15:42:46 -070055 }
Eric Newberry4738f152017-12-18 13:10:32 -070056 catch (const boost::bad_lexical_cast&) {
Davide Pesavento19779d82019-02-14 13:40:04 -050057 NDN_THROW(std::invalid_argument(
Eric Newberry4738f152017-12-18 13:10:32 -070058 "First parameter to CongestionMarkStrategy must be a non-negative integer"));
59 }
Eric Newberry69b63dc2017-11-04 15:42:46 -070060 NDN_CXX_FALLTHROUGH;
61 case 0:
62 break;
63 default:
Davide Pesavento19779d82019-02-14 13:40:04 -050064 NDN_THROW(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters"));
Eric Newberry69b63dc2017-11-04 15:42:46 -070065 }
66
67 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050068 NDN_THROW(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050069 "CongestionMarkStrategy does not support version " + to_string(*parsed.version)));
Eric Newberry69b63dc2017-11-04 15:42:46 -070070 }
71 this->setInstanceName(makeInstanceName(name, getStrategyName()));
72}
73
74const Name&
75CongestionMarkStrategy::getStrategyName()
76{
77 static Name strategyName("/localhost/nfd/strategy/congestion-mark/%FD%01");
78 return strategyName;
79}
80
81void
ashiqopuc7079482019-02-20 05:34:37 +000082CongestionMarkStrategy::afterReceiveInterest(const FaceEndpoint& ingress, const Interest& interest,
Eric Newberry69b63dc2017-11-04 15:42:46 -070083 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);
ashiqopuc7079482019-02-20 05:34:37 +000089 BestRouteStrategy2::afterReceiveInterest(ingress, markedInterest, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070090 }
91 else {
ashiqopuc7079482019-02-20 05:34:37 +000092 BestRouteStrategy2::afterReceiveInterest(ingress, interest, pitEntry);
Eric Newberry69b63dc2017-11-04 15:42:46 -070093 }
94}
95
96} // namespace fw
97} // namespace nfd