NDN-DPDK
High-Speed Named Data Networking Forwarder
Loading...
Searching...
No Matches
name.h
Go to the documentation of this file.
1#ifndef NDNDPDK_NDNI_NAME_H
2#define NDNDPDK_NDNI_NAME_H
3
5
6#include "common.h"
7
8extern uint64_t LName_EmptyHash_;
9
11typedef struct LName {
12 const uint8_t* value;
13 uint16_t length;
15
16__attribute__((nonnull)) static __rte_always_inline bool
17LName_ParseVarNum_(LName name, uint16_t* restrict pos, uint16_t* restrict n, uint16_t minTail) {
18 if (unlikely(*pos + 1 + minTail > name.length)) {
19 return false;
20 }
21
22 *n = name.value[*pos];
23 *pos += 1;
24 if (likely(*n < 0xFD)) {
25 return true;
26 }
27
28 if (unlikely(*n > 0xFD) || unlikely(*pos + 2 + minTail > name.length)) {
29 return false;
30 }
31
32 *n = rte_be_to_cpu_16(*(unaligned_uint16_t*)&name.value[*pos]);
33 *pos += 2;
34 return true;
35}
36
47__attribute__((nonnull)) static inline bool
48LName_Component(LName name, uint16_t* restrict pos, uint16_t* restrict type,
49 uint16_t* restrict length) {
50 return LName_ParseVarNum_(name, pos, type, 1) && likely(*type != 0) &&
51 LName_ParseVarNum_(name, pos, length, 0) && *pos + *length <= name.length;
52}
53
60static inline int
61LName_IsPrefix(LName a, LName b) {
62 if (a.length > b.length || memcmp(a.value, b.value, a.length) != 0) {
63 return -1;
64 }
65 return b.length - a.length;
66}
67
68static __rte_always_inline LName
69LName_SliceByte_(LName name, uint16_t start, uint16_t end) {
70 return (LName){.length = end - start, .value = RTE_PTR_ADD(name.value, start)};
71}
72
73static __rte_always_inline LName
74LName_Slice_(LName name, uint16_t start, uint16_t end) {
75 uint16_t i = 0, pos = 0, type = 0, length = 0;
76 uint16_t posStart = likely(start == 0) ? 0 : name.length;
77 uint16_t posEnd = name.length;
78 while (likely(LName_Component(name, &pos, &type, &length))) {
79 ++i;
80 pos += length;
81 if (i == start) {
82 posStart = pos;
83 } else if (i == end) {
84 posEnd = pos;
85 break;
86 }
87 }
88 return LName_SliceByte_(name, posStart, posEnd);
89}
90
96static inline LName
97LName_Slice(LName name, uint16_t start, uint16_t end) {
98 if (unlikely(start >= end)) {
99 return (LName){0};
100 }
101 return LName_Slice_(name, start, end);
102}
103
105uint64_t
107
118__attribute__((nonnull)) static inline int
119LNamePrefixFilter_Find(LName name, int maxPrefix, const uint16_t* prefixL, const uint8_t* prefixV) {
120 size_t offset = 0;
121 for (int i = 0; i < maxPrefix; ++i) {
122 if (prefixL[i] == UINT16_MAX) {
123 break;
124 }
125
126 LName prefix = {
127 .value = RTE_PTR_ADD(prefixV, offset),
128 .length = prefixL[i],
129 };
130 if (LName_IsPrefix(prefix, name) >= 0) {
131 return i;
132 }
133 offset += prefix.length;
134 }
135 return -1;
136}
137
139typedef struct __rte_packed_begin PName {
140 const uint8_t* value;
141 uint16_t length;
142 uint16_t nComps;
143 struct {
144 int16_t firstNonGeneric : 12;
145 bool hasDigestComp : 1;
146 bool hasHashes_ : 1;
147 uint32_t a_ : 2;
152// maximum component index must fit in firstNonGeneric
153static_assert((NameMaxLength / 2) <= (1 << 11), "");
154
156static __rte_always_inline LName
157PName_ToLName(const PName* p) {
158 static_assert(offsetof(LName, value) == offsetof(PName, value), "");
159 static_assert(offsetof(LName, length) == offsetof(PName, length), "");
160 return *(const LName*)p;
161}
162
164__attribute__((nonnull)) bool
165PName_Parse(PName* p, LName l);
166
167__attribute__((nonnull)) static __rte_noinline LName
168PName_Slice_Uncached_(const PName* p, int16_t start, int16_t end) {
169 return LName_Slice_(PName_ToLName(p), (uint16_t)start, (uint16_t)end);
170}
171
177__attribute__((nonnull)) static __rte_always_inline LName
178PName_Slice(const PName* p, int16_t start, int16_t end) {
179 if (unlikely(start < 0)) {
180 start += p->nComps;
181 }
182 start = CLAMP(start, 0, (int16_t)p->nComps);
183
184 if (unlikely(end < 0)) {
185 end += p->nComps;
186 }
187 end = CLAMP(end, 0, (int16_t)p->nComps);
188
189 if (unlikely(start >= end)) {
190 return (LName){0};
191 }
192
193 if (unlikely(end > PNameCachedComponents)) {
194 return PName_Slice_Uncached_(p, start, end);
195 }
196
197 return LName_SliceByte_(PName_ToLName(p), likely(start == 0) ? 0 : p->comp_[start - 1],
198 p->comp_[end - 1]);
199}
200
205__attribute__((nonnull)) static __rte_always_inline LName
206PName_GetPrefix(const PName* p, int16_t n) {
207 return PName_Slice(p, 0, n);
208}
209
210__attribute__((nonnull)) void
212
217__attribute__((nonnull)) static inline uint64_t
218PName_ComputePrefixHash(const PName* p, uint16_t i) {
219 NDNDPDK_ASSERT(i <= p->nComps);
220 if (unlikely(i == 0)) {
221 return LName_EmptyHash_;
222 }
223 if (unlikely(i > PNameCachedComponents)) {
224 return LName_ComputeHash(PName_GetPrefix(p, i));
225 }
226
227 if (!p->hasHashes_) {
229 }
230 return p->hash_[i - 1];
231}
232
234__attribute__((nonnull)) static inline uint64_t
235PName_ComputeHash(const PName* p) {
236 return PName_ComputePrefixHash(p, p->nComps);
237}
238
239#endif // NDNDPDK_NDNI_NAME_H
struct __rte_packed_begin vlanhdr __rte_packed_end
#define NDNDPDK_ASSERT(x)
Definition common.h:60
#define CLAMP(x, lo, hi)
Definition common.h:106
uint64_t LName_EmptyHash_
Definition name.c:7
uint64_t LName_ComputeHash(LName name)
Compute hash for a name.
Definition name.c:10
bool PName_Parse(PName *p, LName l)
Parse a name from memory buffer.
Definition name.c:32
void PName_PrepareHashes_(PName *p)
Definition name.c:57
@ NameMaxLength
Definition enum.h:10
@ PNameCachedComponents
Definition enum.h:13
Name in linear buffer.
Definition name.h:11
const uint8_t * value
Definition name.h:12
uint16_t length
Definition name.h:13
Parsed name.
Definition name.h:139
uint16_t comp_[PNameCachedComponents]
end offset of i-th component
Definition name.h:149
uint16_t length
TLV-LENGTH.
Definition name.h:141
const uint8_t * value
TLV-VALUE.
Definition name.h:140
uint16_t nComps
number of components
Definition name.h:142
bool hasHashes_
hash_ computed?
Definition name.h:146
uint32_t a_
Definition name.h:147
int16_t firstNonGeneric
index of first non-generic component
Definition name.h:144
bool hasDigestComp
ends with digest component?
Definition name.h:145
uint64_t hash_[PNameCachedComponents]
hash of i+1-component prefix
Definition name.h:150