blob: 4f6116226fa6a7b46acfa50beeae7ebb16bbaffd [file] [log] [blame]
Spyridon Mastorakis77b63662014-11-25 19:22:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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 "random-load-balancer-strategy.hpp"
27
28#include <boost/random/uniform_int_distribution.hpp>
29
30#include <ndn-cxx/util/random.hpp>
31
32#include "core/logger.hpp"
33
34NFD_LOG_INIT("RandomLoadBalancerStrategy");
35
36namespace nfd {
37namespace fw {
38
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080039RandomLoadBalancerStrategy::RandomLoadBalancerStrategy(Forwarder& forwarder, const Name& name)
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -070040 : Strategy(forwarder)
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080041{
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -070042 this->setInstanceName(makeInstanceName(name, getStrategyName()));
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080043}
44
45RandomLoadBalancerStrategy::~RandomLoadBalancerStrategy()
46{
47}
48
49static bool
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080050canForwardToNextHop(const Face& inFace, shared_ptr<pit::Entry> pitEntry, const fib::NextHop& nexthop)
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080051{
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080052 return !wouldViolateScope(inFace, pitEntry->getInterest(), nexthop.getFace()) &&
53 canForwardToLegacy(*pitEntry, nexthop.getFace());
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080054}
55
56static bool
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080057hasFaceForForwarding(const Face& inFace, const fib::NextHopList& nexthops, const shared_ptr<pit::Entry>& pitEntry)
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080058{
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080059 return std::find_if(nexthops.begin(), nexthops.end(), bind(&canForwardToNextHop, cref(inFace), pitEntry, _1))
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080060 != nexthops.end();
61}
62
63void
64RandomLoadBalancerStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080065 const shared_ptr<pit::Entry>& pitEntry)
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080066{
67 NFD_LOG_TRACE("afterReceiveInterest");
68
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080069 if (hasPendingOutRecords(*pitEntry)) {
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080070 // not a new Interest, don't forward
71 return;
72 }
73
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080074 const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
75 const fib::NextHopList& nexthops = fibEntry.getNextHops();
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080076
77 // Ensure there is at least 1 Face is available for forwarding
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080078 if (!hasFaceForForwarding(inFace, nexthops, pitEntry)) {
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080079 this->rejectPendingInterest(pitEntry);
80 return;
81 }
82
83 fib::NextHopList::const_iterator selected;
84 do {
85 boost::random::uniform_int_distribution<> dist(0, nexthops.size() - 1);
86 const size_t randomIndex = dist(m_randomGenerator);
87
88 uint64_t currentIndex = 0;
89
90 for (selected = nexthops.begin(); selected != nexthops.end() && currentIndex != randomIndex;
91 ++selected, ++currentIndex) {
92 }
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -080093 } while (!canForwardToNextHop(inFace, pitEntry, *selected));
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080094
Spyridon Mastorakise9f6baf2016-12-07 14:34:21 -080095 this->sendInterest(pitEntry, selected->getFace(), interest);
Spyridon Mastorakis77b63662014-11-25 19:22:51 -080096}
97
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -070098const Name&
99RandomLoadBalancerStrategy::getStrategyName()
100{
101 static Name strategyName("ndn:/localhost/nfd/strategy/random-load-balancer/%FD%01");
102 return strategyName;
103}
104
Spyridon Mastorakis77b63662014-11-25 19:22:51 -0800105} // namespace fw
106} // namespace nfd