ndn-embeds
0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
fib.cpp
1
#include "
ndn/fib.hpp
"
2
3
namespace
ndn {
4
5
// =============================================================================
6
// FibEntry
7
// =============================================================================
8
9
namespace
{
10
const
FibNexthop INVALID_NEXTHOP{.faceId =
FACE_ID_INVALID
, .cost = 0};
11
}
// namespace
12
13
const
FibNexthop
&
FibEntry::nexthop
(
size_t
index)
const
{
14
if
(index >= numNexthops_) {
15
return
INVALID_NEXTHOP;
16
}
17
return
nexthops_[index];
18
}
19
20
bool
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
40
bool
FibEntry::removeNexthop
(
FaceId
faceId) {
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
56
Error
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
81
void
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
96
void
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
105
void
Fib::removeFace
(
FaceId
faceId) {
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
117
const
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
134
const
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
143
FibEntry
*
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
ndn::FibEntry
FIB entry.
Definition
fib.hpp:41
ndn::FibEntry::nexthop
const FibNexthop & nexthop(size_t index) const
Get the next-hop at a given index.
Definition
fib.cpp:13
ndn::FibEntry::removeNexthop
bool removeNexthop(FaceId faceId)
Remove a next-hop.
Definition
fib.cpp:40
ndn::FibEntry::addNexthop
bool addNexthop(FaceId faceId, uint8_t cost=0)
Add a next-hop.
Definition
fib.cpp:20
ndn::Fib::findLongestMatch
const FibEntry * findLongestMatch(const Name &name) const
Look up using Longest Prefix Match.
Definition
fib.cpp:117
ndn::Fib::removeRoute
void removeRoute(const Name &prefix, FaceId faceId)
Remove a specific next-hop.
Definition
fib.cpp:81
ndn::Fib::findExact
const FibEntry * findExact(const Name &prefix) const
Look up using exact match.
Definition
fib.cpp:134
ndn::Fib::removeFace
void removeFace(FaceId faceId)
Remove a specified Face from all entries.
Definition
fib.cpp:105
ndn::Fib::addRoute
Error addRoute(const Name &prefix, FaceId faceId, uint8_t cost=0)
Add a route.
Definition
fib.cpp:56
ndn::Name
NDN Name class.
Definition
name.hpp:64
ndn::Name::componentCount
size_t componentCount() const
Get the number of components.
Definition
name.hpp:133
ndn::FACE_ID_INVALID
constexpr FaceId FACE_ID_INVALID
Invalid Face ID.
Definition
common.hpp:99
ndn::FaceId
uint16_t FaceId
Face identifier.
Definition
common.hpp:96
ndn::Error
Error
Error codes.
Definition
common.hpp:24
fib.hpp
Forwarding Information Base (FIB)
ndn::FIB_MAX_NEXTHOPS
constexpr size_t FIB_MAX_NEXTHOPS
Maximum number of next-hops per FIB entry.
Definition
fib.hpp:23
ndn::FibNexthop
FIB Nexthop.
Definition
fib.hpp:30
src
fib.cpp
Generated by
1.9.8