ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
fib.cpp
1#include "ndn/fib.hpp"
2
3namespace ndn {
4
5// =============================================================================
6// FibEntry
7// =============================================================================
8
9namespace {
10const FibNexthop INVALID_NEXTHOP{.faceId = FACE_ID_INVALID, .cost = 0};
11} // namespace
12
13const FibNexthop& FibEntry::nexthop(size_t index) const {
14 if (index >= numNexthops_) {
15 return INVALID_NEXTHOP;
16 }
17 return nexthops_[index];
18}
19
20bool FibEntry::addNexthop(FaceId faceId, uint8_t cost) {
21 // Update existing nexthop
22 for (size_t i = 0; i < numNexthops_; ++i) {
23 if (nexthops_[i].faceId == faceId) {
24 nexthops_[i].cost = cost;
25 return true;
26 }
27 }
28
29 // Add new
30 if (numNexthops_ >= FIB_MAX_NEXTHOPS) {
31 return false;
32 }
33
34 nexthops_[numNexthops_].faceId = faceId;
35 nexthops_[numNexthops_].cost = cost;
36 numNexthops_++;
37 return true;
38}
39
41 for (size_t i = 0; i < numNexthops_; ++i) {
42 if (nexthops_[i].faceId == faceId) {
43 // Swap with last element and remove
44 nexthops_[i] = nexthops_[numNexthops_ - 1];
45 numNexthops_--;
46 return true;
47 }
48 }
49 return false;
50}
51
52// =============================================================================
53// Fib
54// =============================================================================
55
56Error Fib::addRoute(const Name& prefix, FaceId faceId, uint8_t cost) {
57 // Search for existing entry
58 FibEntry* entry = findExact(prefix);
59 if (entry != nullptr) {
60 if (!entry->addNexthop(faceId, cost)) {
61 return Error::Full;
62 }
63 return Error::Success;
64 }
65
66 // Create new entry
67 for (auto& e : entries_) {
68 if (!e.inUse_) {
69 e.prefix_ = prefix;
70 e.numNexthops_ = 0;
71 e.addNexthop(faceId, cost);
72 e.inUse_ = true;
73 size_++;
74 return Error::Success;
75 }
76 }
77
78 return Error::Full;
79}
80
81void Fib::removeRoute(const Name& prefix, FaceId faceId) {
82 FibEntry* entry = findExact(prefix);
83 if (entry == nullptr) {
84 return;
85 }
86
87 entry->removeNexthop(faceId);
88
89 // Remove entry if no nexthops remain
90 if (entry->numNexthops_ == 0) {
91 entry->inUse_ = false;
92 size_--;
93 }
94}
95
96void Fib::removeRoute(const Name& prefix) {
97 FibEntry* entry = findExact(prefix);
98 if (entry != nullptr) {
99 entry->inUse_ = false;
100 entry->numNexthops_ = 0;
101 size_--;
102 }
103}
104
106 for (auto& entry : entries_) {
107 if (entry.inUse_) {
108 entry.removeNexthop(faceId);
109 if (entry.numNexthops_ == 0) {
110 entry.inUse_ = false;
111 size_--;
112 }
113 }
114 }
115}
116
117const FibEntry* Fib::findLongestMatch(const Name& name) const {
118 const FibEntry* bestMatch = nullptr;
119 size_t bestMatchLen = 0;
120
121 for (const auto& entry : entries_) {
122 if (entry.inUse_ && entry.prefix_.isPrefixOf(name)) {
123 const size_t prefixLen = entry.prefix_.componentCount();
124 if (prefixLen >= bestMatchLen) {
125 bestMatch = &entry;
126 bestMatchLen = prefixLen;
127 }
128 }
129 }
130
131 return bestMatch;
132}
133
134const FibEntry* Fib::findExact(const Name& prefix) const {
135 for (const auto& entry : entries_) {
136 if (entry.inUse_ && entry.prefix_.equals(prefix)) {
137 return &entry;
138 }
139 }
140 return nullptr;
141}
142
143FibEntry* Fib::findExact(const Name& prefix) {
144 for (auto& entry : entries_) {
145 if (entry.inUse_ && entry.prefix_.equals(prefix)) {
146 return &entry;
147 }
148 }
149 return nullptr;
150}
151
152} // namespace ndn
FIB entry.
Definition fib.hpp:41
const FibNexthop & nexthop(size_t index) const
Get the next-hop at a given index.
Definition fib.cpp:13
bool removeNexthop(FaceId faceId)
Remove a next-hop.
Definition fib.cpp:40
bool addNexthop(FaceId faceId, uint8_t cost=0)
Add a next-hop.
Definition fib.cpp:20
const FibEntry * findLongestMatch(const Name &name) const
Look up using Longest Prefix Match.
Definition fib.cpp:117
void removeRoute(const Name &prefix, FaceId faceId)
Remove a specific next-hop.
Definition fib.cpp:81
const FibEntry * findExact(const Name &prefix) const
Look up using exact match.
Definition fib.cpp:134
void removeFace(FaceId faceId)
Remove a specified Face from all entries.
Definition fib.cpp:105
Error addRoute(const Name &prefix, FaceId faceId, uint8_t cost=0)
Add a route.
Definition fib.cpp:56
NDN Name class.
Definition name.hpp:64
size_t componentCount() const
Get the number of components.
Definition name.hpp:133
constexpr FaceId FACE_ID_INVALID
Invalid Face ID.
Definition common.hpp:99
uint16_t FaceId
Face identifier.
Definition common.hpp:96
Error
Error codes.
Definition common.hpp:24
Forwarding Information Base (FIB)
constexpr size_t FIB_MAX_NEXTHOPS
Maximum number of next-hops per FIB entry.
Definition fib.hpp:23
FIB Nexthop.
Definition fib.hpp:30