Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1cb619e | 2018-04-10 17:13:53 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [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/>. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_FIB_ENTRY_HPP |
| 27 | #define NFD_DAEMON_TABLE_FIB_ENTRY_HPP |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 29 | #include "core/common.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 31 | namespace nfd { |
| 32 | |
| 33 | namespace face { |
| 34 | class Face; |
| 35 | } // namespace face |
| 36 | using face::Face; |
| 37 | |
| 38 | namespace name_tree { |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 39 | class Entry; |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 40 | } // namespace name_tree |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 42 | namespace fib { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 43 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 44 | class Fib; |
| 45 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 46 | /** |
| 47 | * \brief Represents a nexthop record in a FIB entry. |
| 48 | */ |
| 49 | class NextHop |
| 50 | { |
| 51 | public: |
| 52 | explicit |
| 53 | NextHop(Face& face) noexcept |
| 54 | : m_face(&face) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | Face& |
| 59 | getFace() const noexcept |
| 60 | { |
| 61 | return *m_face; |
| 62 | } |
| 63 | |
| 64 | uint64_t |
| 65 | getCost() const noexcept |
| 66 | { |
| 67 | return m_cost; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | setCost(uint64_t cost) noexcept |
| 72 | { |
| 73 | m_cost = cost; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | Face* m_face; // pointer instead of reference so that NextHop is movable |
| 78 | uint64_t m_cost = 0; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * \brief A collection of nexthops in a FIB entry. |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 83 | */ |
Davide Pesavento | 1cb619e | 2018-04-10 17:13:53 -0400 | [diff] [blame] | 84 | using NextHopList = std::vector<NextHop>; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 85 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 86 | /** |
| 87 | * \brief Represents an entry in the FIB. |
| 88 | * \sa Fib |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 89 | */ |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 90 | class Entry : noncopyable |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 91 | { |
| 92 | public: |
Junxiao Shi | 408a700 | 2014-02-12 17:53:47 -0700 | [diff] [blame] | 93 | explicit |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 94 | Entry(const Name& prefix); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 96 | const Name& |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 97 | getPrefix() const noexcept |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 98 | { |
| 99 | return m_prefix; |
| 100 | } |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 102 | const NextHopList& |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 103 | getNextHops() const noexcept |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 104 | { |
| 105 | return m_nextHops; |
| 106 | } |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 107 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 108 | /** |
| 109 | * \brief Returns whether this Entry has any NextHop records. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 110 | */ |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 111 | bool |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 112 | hasNextHops() const noexcept |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 113 | { |
| 114 | return !m_nextHops.empty(); |
| 115 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 116 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 117 | /** |
| 118 | * \brief Returns whether there is a NextHop record for \p face. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 119 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 120 | bool |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 121 | hasNextHop(const Face& face) const noexcept; |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 122 | |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 123 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 124 | /** \brief Adds a NextHop record to the entry. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 125 | * |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 126 | * If a NextHop record for \p face already exists in the entry, its cost is set to \p cost. |
| 127 | * |
| 128 | * \return the iterator to the new or updated NextHop and a bool indicating whether a new |
| 129 | * NextHop was inserted |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 130 | */ |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 131 | std::pair<NextHopList::iterator, bool> |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 132 | addOrUpdateNextHop(Face& face, uint64_t cost); |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 133 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 134 | /** \brief Removes a NextHop record. |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 135 | * |
| 136 | * If no NextHop record for face exists, do nothing. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 137 | */ |
Ju Pan | d8315bf | 2019-07-31 06:59:07 +0000 | [diff] [blame] | 138 | bool |
Md Ashiqur Rahman | 6be9387 | 2019-08-07 01:25:31 +0000 | [diff] [blame] | 139 | removeNextHop(const Face& face); |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 140 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 141 | /** \note This method is non-const because mutable iterators are needed by callers. |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 142 | */ |
| 143 | NextHopList::iterator |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 144 | findNextHop(const Face& face) noexcept; |
Junxiao Shi | 56a21bf | 2014-11-02 21:11:50 -0700 | [diff] [blame] | 145 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 146 | /** \brief Sorts the nexthop list. |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 147 | */ |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 148 | void |
| 149 | sortNextHops(); |
| 150 | |
| 151 | private: |
| 152 | Name m_prefix; |
| 153 | NextHopList m_nextHops; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 154 | |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 155 | name_tree::Entry* m_nameTreeEntry = nullptr; |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 156 | |
Davide Pesavento | 9a28c3f | 2022-06-11 21:50:01 -0400 | [diff] [blame] | 157 | friend ::nfd::name_tree::Entry; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 158 | friend Fib; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame^] | 161 | } // namespace fib |
| 162 | } // namespace nfd |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 164 | #endif // NFD_DAEMON_TABLE_FIB_ENTRY_HPP |