blob: c39b5f4b76257e6783291ea96d01af9f8d34783f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07002/*
awlane6d7c37f2025-03-07 11:53:58 -06003 * Copyright (c) 2014-2025, The University of Memphis,
Vince Lehmancae33b62015-06-05 09:21:30 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070020 */
Vince Lehmancae33b62015-06-05 09:21:30 -050021
akmhoquefdbddb12014-05-02 18:35:19 -050022#ifndef NLSR_NAME_PREFIX_TABLE_HPP
23#define NLSR_NAME_PREFIX_TABLE_HPP
akmhoque53353462014-04-22 08:43:45 -050024
akmhoquec8a10f72014-04-25 18:42:55 -050025#include "name-prefix-table-entry.hpp"
Nick Gordonb50e51b2016-07-22 16:05:57 -050026#include "routing-table-pool-entry.hpp"
Nick Gordonb7b58392017-08-17 16:29:21 -050027#include "signals.hpp"
Nick Gordonb50e51b2016-07-22 16:05:57 -050028#include "test-access-control.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060029#include "route/fib.hpp"
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070030#include "lsdb.hpp"
akmhoque53353462014-04-22 08:43:45 -050031
Vince Lehmancae33b62015-06-05 09:21:30 -050032#include <list>
Nick Gordonb50e51b2016-07-22 16:05:57 -050033#include <unordered_map>
Vince Lehmancae33b62015-06-05 09:21:30 -050034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
akmhoque53353462014-04-22 08:43:45 -050036
akmhoquec8a10f72014-04-25 18:42:55 -050037class NamePrefixTable
akmhoque53353462014-04-22 08:43:45 -050038{
39public:
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050040 using RoutingTableEntryPool =
41 std::unordered_map<ndn::Name, std::shared_ptr<RoutingTablePoolEntry>>;
42 using NptEntryList = std::list<std::shared_ptr<NamePrefixTableEntry>>;
43 using const_iterator = NptEntryList::const_iterator;
awlane6d7c37f2025-03-07 11:53:58 -060044 using DestNameKey = std::tuple<ndn::Name, ndn::Name>;
Nick Gordonb50e51b2016-07-22 16:05:57 -050045
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070046 NamePrefixTable(const ndn::Name& ownRouterName, Fib& fib, RoutingTable& routingTable,
47 AfterRoutingChange& afterRoutingChangeSignal,
48 Lsdb::AfterLsdbModified& afterLsdbModifiedSignal);
Nick Gordonb7b58392017-08-17 16:29:21 -050049
50 ~NamePrefixTable();
akmhoque53353462014-04-22 08:43:45 -050051
awlane6d7c37f2025-03-07 11:53:58 -060052 NexthopList
53 adjustNexthopCosts(const NexthopList& nhlist, const ndn::Name& nameToCheck, const ndn::Name& destRouterName);
54
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070055 /*! \brief Add, update, or remove Names according to the Lsdb update
56 \param lsa The LSA class pointer
57 \param updateType Update type from Lsdb (INSTALLED, UPDATED, REMOVED)
58 \param namesToAdd If LSA is UPDATED, then these are the names to add
59 \param namesToRemove If LSA is UPDATED, then these are the names to remove
60 */
61 void
62 updateFromLsdb(std::shared_ptr<Lsa> lsa, LsdbUpdate updateType,
awlane6d7c37f2025-03-07 11:53:58 -060063 const std::list<nlsr::PrefixInfo>& namesToAdd,
64 const std::list<nlsr::PrefixInfo>& namesToRemove);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070065
Nick Gordonb50e51b2016-07-22 16:05:57 -050066 /*! \brief Adds a destination to the specified name prefix.
67 \param name The name prefix
68 \param destRouter The destination router prefix
awlanefc0b45a2025-05-07 15:51:02 -050069 \param routeFlags Route inheritance flags from NFD
Nick G97e34942016-07-11 14:46:27 -050070
Nick Gordonb50e51b2016-07-22 16:05:57 -050071 This method adds a router to a name prefix table entry. If the
72 name prefix table entry does not exist, it is created. The method
73 will first look through its local pool of cached entries to find
74 the routing information for destRouter. If it is not found there,
75 it will construct one and fill it with information from an
76 appropriate RoutingTableEntry in the routing table. If there isn't
77 a match, it will instantiate it with no next hops. The FIB will be
78 notified of the change to the NPT entry, too.
79 */
akmhoque53353462014-04-22 08:43:45 -050080 void
awlanefc0b45a2025-05-07 15:51:02 -050081 addEntry(const ndn::Name& name, const ndn::Name& destRouter, uint64_t routeFlags = ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoquefdbddb12014-05-02 18:35:19 -050082
Nick Gordonb50e51b2016-07-22 16:05:57 -050083 /*! \brief Removes a destination from a name prefix table entry.
84 \param name The name prefix
85 \param destRouter The destination.
86
87 This method removes a destination from an entry. It will not fail
88 if an invalid name/destination pair are passed. After removal, if
89 the RoutingTablePoolEntry has a use count of 0, it is deleted from
90 the table. Additionally, if the name prefix has no routing table
91 entries associated with it, it is deleted from the NPT. In any
92 case, the FIB is informed of the changes.
93 */
akmhoquefdbddb12014-05-02 18:35:19 -050094 void
akmhoque31d1d4b2014-05-05 22:08:14 -050095 removeEntry(const ndn::Name& name, const ndn::Name& destRouter);
akmhoque53353462014-04-22 08:43:45 -050096
Nick Gordonb50e51b2016-07-22 16:05:57 -050097 /*! \brief Updates all routing information in the NPT.
98
Nick Gordonb7b58392017-08-17 16:29:21 -050099 Takes in a list of entries that are assumed to be exhaustive, and
100 updates each pool entry with the next hop information contained in
101 the corresponding entry in entries. If no entry is found, it is
102 assumed that the destination for that pool entry is inaccessible,
103 and its next hop information is deleted.
Nick Gordonb50e51b2016-07-22 16:05:57 -0500104 */
akmhoque53353462014-04-22 08:43:45 -0500105 void
Nick Gordonb7b58392017-08-17 16:29:21 -0500106 updateWithNewRoute(const std::list<RoutingTableEntry>& entries);
akmhoque53353462014-04-22 08:43:45 -0500107
Nick Gordonb50e51b2016-07-22 16:05:57 -0500108 /*! \brief Adds a pool entry to the pool.
109 \param rtpe The entry.
110
111 \return A shared_ptr to the entry, now in the pool.
112
113 Adds a RoutingTablePoolEntry to the NPT's local pool. Shared
114 pointers are used because it eliminates complicated hacks to deal
115 with lifetime issues, and to simplify memory management.
116 */
117 std::shared_ptr<RoutingTablePoolEntry>
118 addRtpeToPool(RoutingTablePoolEntry& rtpe);
119
120 /*! \brief Removes a pool entry from the pool.
121 \param rtpePtr The shared_ptr to the entry.
122
123 Removes a pool entry from the pool. Comparing these shared_ptrs
124 should not be a problem, because the same pointer is moved around,
125 all sourced from this central location. A more robust solution is
126 certainly possible, though.
127 */
128 void
129 deleteRtpeFromPool(std::shared_ptr<RoutingTablePoolEntry> rtpePtr);
130
akmhoque53353462014-04-22 08:43:45 -0500131 void
akmhoque674b0b12014-05-20 14:33:28 -0500132 writeLog();
133
Vince Lehmancae33b62015-06-05 09:21:30 -0500134 const_iterator
135 begin() const;
136
137 const_iterator
138 end() const;
139
Nick Gordonb50e51b2016-07-22 16:05:57 -0500140PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick Gordonc0c6bcf2017-08-15 18:11:21 -0500141 RoutingTableEntryPool m_rtpool;
142
Nick Gordonb50e51b2016-07-22 16:05:57 -0500143 NptEntryList m_table;
akmhoque53353462014-04-22 08:43:45 -0500144
145private:
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700146 const ndn::Name& m_ownRouterName;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 Fib& m_fib;
148 RoutingTable& m_routingTable;
Junxiao Shi43f37a02023-08-09 00:09:00 +0000149 ndn::signal::Connection m_afterRoutingChangeConnection;
150 ndn::signal::Connection m_afterLsdbModified;
awlane6d7c37f2025-03-07 11:53:58 -0600151 std::map<std::tuple<ndn::Name, ndn::Name>, double> m_nexthopCost;
akmhoque53353462014-04-22 08:43:45 -0500152};
153
Vince Lehmancae33b62015-06-05 09:21:30 -0500154inline NamePrefixTable::const_iterator
155NamePrefixTable::begin() const
156{
157 return m_table.begin();
158}
akmhoque53353462014-04-22 08:43:45 -0500159
Vince Lehmancae33b62015-06-05 09:21:30 -0500160inline NamePrefixTable::const_iterator
161NamePrefixTable::end() const
162{
163 return m_table.end();
164}
165
166std::ostream&
167operator<<(std::ostream& os, const NamePrefixTable& table);
168
169} // namespace nlsr
170
171#endif // NLSR_NAME_PREFIX_TABLE_HPP