blob: 3e2ccce6002b0e39d21dc039b16a6b7453f1aea4 [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)
37 : BestRouteStrategy2(forwarder, name)
38 , m_congestionMark(1)
39 , m_shouldPreserveMark(true)
40{
41 ParsedInstanceName parsed = parseInstanceName(name);
42 switch (parsed.parameters.size()) {
43 case 2:
44 if (!parsed.parameters.at(1).isNumber()) {
45 BOOST_THROW_EXCEPTION(
46 std::invalid_argument("Second parameter to CongestionMarkStrategy must be a non-negative integer"));
47 }
48 m_shouldPreserveMark = parsed.parameters.at(1).toNumber() != 0;
49 NDN_CXX_FALLTHROUGH;
50 case 1:
51 if (!parsed.parameters.at(0).isNumber()) {
52 BOOST_THROW_EXCEPTION(
53 std::invalid_argument("First parameter to CongestionMarkStrategy must be a non-negative integer"));
54 }
55 m_congestionMark = parsed.parameters.at(0).toNumber();
56 NDN_CXX_FALLTHROUGH;
57 case 0:
58 break;
59 default:
60 BOOST_THROW_EXCEPTION(std::invalid_argument("CongestionMarkStrategy does not accept more than 2 parameters"));
61 }
62
63 if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
64 BOOST_THROW_EXCEPTION(std::invalid_argument(
Alexander Afanasyev0c63c632017-12-05 11:17:09 -050065 "CongestionMarkStrategy does not support version " + to_string(*parsed.version)));
Eric Newberry69b63dc2017-11-04 15:42:46 -070066 }
67 this->setInstanceName(makeInstanceName(name, getStrategyName()));
68}
69
70const Name&
71CongestionMarkStrategy::getStrategyName()
72{
73 static Name strategyName("/localhost/nfd/strategy/congestion-mark/%FD%01");
74 return strategyName;
75}
76
77void
78CongestionMarkStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
79 const shared_ptr<pit::Entry>& pitEntry)
80{
81 auto mark = interest.getCongestionMark();
82 if (mark != m_congestionMark && (!m_shouldPreserveMark || mark == 0)) {
83 Interest markedInterest(interest);
84 markedInterest.setCongestionMark(m_congestionMark);
85 BestRouteStrategy2::afterReceiveInterest(inFace, markedInterest, pitEntry);
86 }
87 else {
88 BestRouteStrategy2::afterReceiveInterest(inFace, interest, pitEntry);
89 }
90}
91
92} // namespace fw
93} // namespace nfd