Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 25e4d8f | 2019-07-29 13:44:04 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, 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 |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 25e4d8f | 2019-07-29 13:44:04 -0400 | [diff] [blame] | 32 | #include "daemon/common/logger.hpp" |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 33 | |
| 34 | NFD_LOG_INIT("RandomLoadBalancerStrategy"); |
| 35 | |
| 36 | namespace nfd { |
| 37 | namespace fw { |
| 38 | |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 39 | RandomLoadBalancerStrategy::RandomLoadBalancerStrategy(Forwarder& forwarder, const Name& name) |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 40 | : Strategy(forwarder) |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 41 | { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 42 | this->setInstanceName(makeInstanceName(name, getStrategyName())); |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | RandomLoadBalancerStrategy::~RandomLoadBalancerStrategy() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | static bool |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 50 | canForwardToNextHop(const Face& inFace, shared_ptr<pit::Entry> pitEntry, const fib::NextHop& nexthop) |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 51 | { |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame^] | 52 | return !wouldViolateScope(inFace, pitEntry->getInterest(), nexthop.getFace()); |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static bool |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 56 | hasFaceForForwarding(const Face& inFace, const fib::NextHopList& nexthops, const shared_ptr<pit::Entry>& pitEntry) |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 57 | { |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 58 | return std::find_if(nexthops.begin(), nexthops.end(), bind(&canForwardToNextHop, cref(inFace), pitEntry, _1)) |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 59 | != nexthops.end(); |
| 60 | } |
| 61 | |
| 62 | void |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame^] | 63 | RandomLoadBalancerStrategy::afterReceiveInterest(const Interest& interest, const FaceEndpoint& ingress, |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 64 | const shared_ptr<pit::Entry>& pitEntry) |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 65 | { |
| 66 | NFD_LOG_TRACE("afterReceiveInterest"); |
| 67 | |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 68 | if (hasPendingOutRecords(*pitEntry)) { |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 69 | // not a new Interest, don't forward |
| 70 | return; |
| 71 | } |
| 72 | |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 73 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 74 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 75 | |
| 76 | // Ensure there is at least 1 Face is available for forwarding |
Alexander Afanasyev | 25e4d8f | 2019-07-29 13:44:04 -0400 | [diff] [blame] | 77 | if (!hasFaceForForwarding(ingress.face, nexthops, pitEntry)) { |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 78 | this->rejectPendingInterest(pitEntry); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | fib::NextHopList::const_iterator selected; |
| 83 | do { |
| 84 | boost::random::uniform_int_distribution<> dist(0, nexthops.size() - 1); |
| 85 | const size_t randomIndex = dist(m_randomGenerator); |
| 86 | |
| 87 | uint64_t currentIndex = 0; |
| 88 | |
| 89 | for (selected = nexthops.begin(); selected != nexthops.end() && currentIndex != randomIndex; |
| 90 | ++selected, ++currentIndex) { |
| 91 | } |
Alexander Afanasyev | 25e4d8f | 2019-07-29 13:44:04 -0400 | [diff] [blame] | 92 | } while (!canForwardToNextHop(ingress.face, pitEntry, *selected)); |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | f007a99 | 2022-05-05 15:57:08 -0400 | [diff] [blame^] | 94 | this->sendInterest(interest, selected->getFace(), pitEntry); |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 97 | const Name& |
| 98 | RandomLoadBalancerStrategy::getStrategyName() |
| 99 | { |
| 100 | static Name strategyName("ndn:/localhost/nfd/strategy/random-load-balancer/%FD%01"); |
| 101 | return strategyName; |
| 102 | } |
| 103 | |
Spyridon Mastorakis | 77b6366 | 2014-11-25 19:22:51 -0800 | [diff] [blame] | 104 | } // namespace fw |
| 105 | } // namespace nfd |