blob: 25e00cb64bdc6bbe4b3705a7953baaa3af37c7d4 [file] [log] [blame]
Eric Newberry69b63dc2017-11-04 15:42:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2017, Regents of the University of California,
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
28#include <ndn-cxx/lp/tags.hpp>
29
30namespace nfd {
31namespace fw {
32
33NFD_LOG_INIT("CongestionMarkStrategy");
34NFD_REGISTER_STRATEGY(CongestionMarkStrategy);
35
36CongestionMarkStrategy::CongestionMarkStrategy(Forwarder& forwarder, const Name& name)
Eric Newberry78ef9012017-11-28 13:01:17 -070037 // Specifying BestRouteStrategy2's own name in its constructor prevents an exception from occuring
38 // when specifying parameters to CongestionMarkStrategy
39 : BestRouteStrategy2(forwarder, BestRouteStrategy2::getStrategyName())
Eric Newberry69b63dc2017-11-04 15:42:46 -070040 , m_congestionMark(1)
41 , m_shouldPreserveMark(true)
42{
43 ParsedInstanceName parsed = parseInstanceName(name);
44 switch (parsed.parameters.size()) {
45 case 2:
46 if (!parsed.parameters.at(1).isNumber()) {
47 BOOST_THROW_EXCEPTION(
48 std::invalid_argument("Second parameter to CongestionMarkStrategy must be a non-negative integer"));
49 }
50 m_shouldPreserveMark = parsed.parameters.at(1).toNumber() != 0;
51 NDN_CXX_FALLTHROUGH;
52 case 1:
53 if (!parsed.parameters.at(0).isNumber()) {
54 BOOST_THROW_EXCEPTION(
55 std::invalid_argument("First parameter to CongestionMarkStrategy must be a non-negative integer"));
56 }
57 m_congestionMark = parsed.parameters.at(0).toNumber();
58 NDN_CXX_FALLTHROUGH;
59 case 0:
60 break;
61 default:
62 BOOST_THROW_EXCEPTION(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters"));
63 }
64
65 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
66 BOOST_THROW_EXCEPTION(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
80CongestionMarkStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
81 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);
87 BestRouteStrategy2::afterReceiveInterest(inFace, markedInterest, pitEntry);
88 }
89 else {
90 BestRouteStrategy2::afterReceiveInterest(inFace, interest, pitEntry);
91 }
92}
93
94} // namespace fw
95} // namespace nfd