forked from named-data/YaNFD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pit-cs.go
440 lines (379 loc) · 12.5 KB
/
pit-cs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* YaNFD - Yet another NDN Forwarding Daemon
*
* Copyright (C) 2020-2021 Eric Newberry.
*
* This file is licensed under the terms of the MIT License, as found in LICENSE.md.
*/
package table
import (
"bytes"
"math/rand"
"time"
"github.com/cespare/xxhash"
"github.com/eric135/YaNFD/core"
"github.com/eric135/YaNFD/ndn"
)
// PitCs represents the PIT-CS tree for a thread.
type PitCs struct {
root *PitCsNode
ExpiringPitEntries chan *PitEntry
nPitEntries int // Number of PIT entries in tree
pitTokenMap map[uint32]*PitEntry
nCsEntries int // Number of CS entries in tree
csReplacement CsReplacementPolicy
csMap map[uint64]*CsEntry
}
// PitCsNode represents an entry in a PIT-CS tree.
type PitCsNode struct {
component ndn.NameComponent
depth int
parent *PitCsNode
children []*PitCsNode
pitEntries []*PitEntry
csEntry *CsEntry
}
// PitEntry is an entry in a thread's PIT.
type PitEntry struct {
node *PitCsNode
pitCs *PitCs
Name *ndn.Name
CanBePrefix bool
MustBeFresh bool
ForwardingHint *ndn.Delegation // Interests must match in terms of Forwarding Hint to be aggregated in PIT.
InRecords map[uint64]*PitInRecord // Key is face ID
OutRecords map[uint64]*PitOutRecord // Key is face ID
ExpirationTime time.Time
Satisfied bool
Token uint32
}
// PitInRecord records an incoming Interest on a given face.
type PitInRecord struct {
Face uint64
LatestNonce []byte
LatestTimestamp time.Time
LatestInterest *ndn.Interest
ExpirationTime time.Time
PitToken []byte
}
// PitOutRecord records an outgoing Interest on a given face.
type PitOutRecord struct {
Face uint64
LatestNonce []byte
LatestTimestamp time.Time
LatestInterest *ndn.Interest
ExpirationTime time.Time
}
// CsEntry is an entry in a thread's CS.
type CsEntry struct {
node *PitCsNode
index uint64
Data *ndn.Data
StaleTime time.Time
}
// NewPitCS creates a new combined PIT-CS for a forwarding thread.
func NewPitCS() *PitCs {
pitCs := new(PitCs)
pitCs.root = new(PitCsNode)
pitCs.root.component = nil // Root component will be nil since it represents zero components
pitCs.root.pitEntries = make([]*PitEntry, 0)
pitCs.ExpiringPitEntries = make(chan *PitEntry, tableQueueSize)
pitCs.pitTokenMap = make(map[uint32]*PitEntry)
// This value has already been validated from loading the configuration, so we know it will be one of the following (or else fatal)
switch csReplacementPolicy {
case "lru":
pitCs.csReplacement = NewCsLRU(pitCs)
default:
core.LogFatal(pitCs, "Unknown CS replacement policy ", csReplacementPolicy)
}
pitCs.csMap = make(map[uint64]*CsEntry)
return pitCs
}
func (p *PitCsNode) findExactMatchEntry(name *ndn.Name) *PitCsNode {
if name.Size() > p.depth {
for _, child := range p.children {
if name.At(child.depth - 1).Equals(child.component) {
return child.findExactMatchEntry(name)
}
}
} else if name.Size() == p.depth {
return p
}
return nil
}
func (p *PitCsNode) findLongestPrefixEntry(name *ndn.Name) *PitCsNode {
if name.Size() > p.depth {
for _, child := range p.children {
if name.At(child.depth - 1).Equals(child.component) {
return child.findLongestPrefixEntry(name)
}
}
}
return p
}
func (p *PitCsNode) fillTreeToPrefix(name *ndn.Name) *PitCsNode {
curNode := p.findLongestPrefixEntry(name)
for depth := curNode.depth + 1; depth <= name.Size(); depth++ {
newNode := new(PitCsNode)
newNode.component = name.At(depth - 1).DeepCopy()
newNode.depth = depth
newNode.parent = curNode
curNode.children = append(curNode.children, newNode)
curNode = newNode
}
return curNode
}
func (p *PitCsNode) pruneIfEmpty() {
for curNode := p; curNode.parent != nil && len(curNode.children) == 0 && len(curNode.pitEntries) == 0 && curNode.csEntry == nil; curNode = curNode.parent {
// Remove from parent's children
for i, child := range curNode.parent.children {
if child == p {
if i < len(curNode.parent.children)-1 {
copy(curNode.parent.children[i:], curNode.parent.children[i+1:])
}
curNode.parent.children = curNode.parent.children[:len(curNode.parent.children)-1]
break
}
}
}
}
func (p *PitCs) generateNewPitToken() uint32 {
for {
token := rand.Uint32()
if _, ok := p.pitTokenMap[token]; !ok {
return token
}
}
}
func (p *PitCs) hashCsName(name *ndn.Name) uint64 {
return xxhash.Sum64String(name.String())
}
// PitSize returns the number of entries in the PIT.
func (p *PitCs) PitSize() int {
return p.nPitEntries
}
// CsSize returns the number of entries in the CS.
func (p *PitCs) CsSize() int {
return p.nCsEntries
}
// IsCsAdmitting returns whether the CS is admitting contents.
func (p *PitCs) IsCsAdmitting() bool {
return csAdmit
}
// IsCsServing returns whether the CS is serving contents.
func (p *PitCs) IsCsServing() bool {
return csServe
}
// FindOrInsertPIT inserts an entry in the PIT upon receipt of an Interest. Returns tuple of PIT entry and whether the Nonce is a duplicate.
func (p *PitCs) FindOrInsertPIT(interest *ndn.Interest, hint *ndn.Delegation, inFace uint64) (*PitEntry, bool) {
node := p.root.fillTreeToPrefix(interest.Name())
var entry *PitEntry
for _, curEntry := range node.pitEntries {
if curEntry.CanBePrefix == interest.CanBePrefix() && curEntry.MustBeFresh == interest.MustBeFresh() && ((hint == nil && curEntry.ForwardingHint == nil) || hint.Name().Equals(curEntry.ForwardingHint.Name())) {
entry = curEntry
break
}
}
if entry == nil {
p.nPitEntries++
entry = new(PitEntry)
entry.node = node
entry.pitCs = p
entry.Name = interest.Name()
entry.CanBePrefix = interest.CanBePrefix()
entry.MustBeFresh = interest.MustBeFresh()
entry.ForwardingHint = hint
entry.InRecords = make(map[uint64]*PitInRecord)
entry.OutRecords = make(map[uint64]*PitOutRecord)
entry.Satisfied = false
node.pitEntries = append(node.pitEntries, entry)
entry.Token = p.generateNewPitToken()
p.pitTokenMap[entry.Token] = entry
}
for face, inRecord := range entry.InRecords {
// Only considered a duplicate (loop) if from different face since is just retransmission and not loop if same face
if face != inFace && bytes.Equal(inRecord.LatestNonce, interest.Nonce()) {
return entry, true
}
}
// Cancel expiration time
entry.ExpirationTime = time.Unix(0, 0)
return entry, false
}
// FindPITFromData finds the PIT entries matching a Data packet. Note that this does not consider the effect of MustBeFresh.
func (p *PitCs) FindPITFromData(data *ndn.Data, token *uint32) []*PitEntry {
if token != nil {
if entry, ok := p.pitTokenMap[*token]; ok && entry.Token == *token {
return []*PitEntry{entry}
}
return nil
}
matching := make([]*PitEntry, 0)
dataNameLen := data.Name().Size()
for curNode := p.root.findLongestPrefixEntry(data.Name()); curNode != nil; curNode = curNode.parent {
for _, entry := range curNode.pitEntries {
if entry.CanBePrefix || curNode.depth == dataNameLen {
matching = append(matching, entry)
}
}
}
return matching
}
// RemovePITEntry removes the specified PIT entry.
func (p *PitCs) RemovePITEntry(pitEntry *PitEntry) bool {
for i, entry := range pitEntry.node.pitEntries {
if entry == pitEntry {
if i < len(pitEntry.node.pitEntries)-1 {
copy(pitEntry.node.pitEntries[i:], pitEntry.node.pitEntries[i+1:])
}
pitEntry.node.pitEntries = pitEntry.node.pitEntries[:len(pitEntry.node.pitEntries)-1]
if len(pitEntry.node.pitEntries) == 0 {
entry.node.pruneIfEmpty()
}
p.nPitEntries--
return true
}
}
return false
}
// FindMatchingDataCS finds the best matching entry in the CS (if any). If MustBeFresh is set to true in the Interest, only non-stale CS entries will be returned.
func (p *PitCs) FindMatchingDataCS(interest *ndn.Interest) *CsEntry {
node := p.root.findExactMatchEntry(interest.Name())
if node != nil {
if !interest.CanBePrefix() {
if node.csEntry != nil {
p.csReplacement.BeforeUse(node.csEntry.index, node.csEntry.Data)
}
return node.csEntry
}
return node.findMatchingDataCSPrefix(interest)
}
return nil
}
// InsertDataCS inserts a Data packet into the Content Store.
func (p *PitCs) InsertDataCS(data *ndn.Data) {
index := p.hashCsName(data.Name())
if entry, ok := p.csMap[index]; ok {
// Replace existing entry
entry.Data = data
if data.MetaInfo() == nil || data.MetaInfo().FinalBlockID() == nil {
entry.StaleTime = time.Now()
} else {
entry.StaleTime = time.Now().Add(*data.MetaInfo().FreshnessPeriod())
}
p.csReplacement.AfterRefresh(index, data)
} else {
// New entry
p.nCsEntries++
node := p.root.fillTreeToPrefix(data.Name())
node.csEntry = new(CsEntry)
node.csEntry.node = node
node.csEntry.index = index
node.csEntry.Data = data
p.csMap[index] = node.csEntry
p.csReplacement.AfterInsert(index, data)
// Tell replacement strategy to evict entries if needed
p.csReplacement.EvictEntries()
}
}
// eraseCsDataFromReplacementStrategy allows the replacement strategy to erase the data with the specified name from the Content Store.
func (p *PitCs) eraseCsDataFromReplacementStrategy(index uint64) {
if entry, ok := p.csMap[index]; ok {
entry.node.csEntry = nil
delete(p.csMap, index)
p.nCsEntries--
}
}
func (p *PitCsNode) findMatchingDataCSPrefix(interest *ndn.Interest) *CsEntry {
if p.csEntry != nil && (!interest.MustBeFresh() || time.Now().Before(p.csEntry.StaleTime)) {
return p.csEntry
}
if p.depth < interest.Name().Size() {
for _, child := range p.children {
if interest.Name().At(p.depth).Equals(child.component) {
return child.findMatchingDataCSPrefix(interest)
}
}
}
// If found none, then return
return nil
}
func (e *PitEntry) waitForPitExpiry() {
if !e.ExpirationTime.IsZero() {
time.Sleep(e.ExpirationTime.Sub(time.Now().Add(time.Millisecond * 1))) // Add 1 millisecond to ensure *after* expiration
if e.ExpirationTime.Before(time.Now()) {
// Otherwise, has been updated by another PIT entry
e.pitCs.ExpiringPitEntries <- e
}
}
}
// FindOrInsertInRecord finds or inserts an InRecord for the face, updating the metadata and returning whether there was already an in-record in the entry.
func (e *PitEntry) FindOrInsertInRecord(interest *ndn.Interest, face uint64, incomingPitToken []byte) (*PitInRecord, bool) {
var record *PitInRecord
var ok bool
if record, ok = e.InRecords[face]; !ok {
record := new(PitInRecord)
record.Face = face
record.LatestNonce = interest.Nonce()
record.LatestTimestamp = time.Now()
record.LatestInterest = interest
record.ExpirationTime = time.Now().Add(interest.Lifetime())
record.PitToken = incomingPitToken
e.InRecords[face] = record
return record, len(e.InRecords) > 1
}
// Existing record
record.LatestNonce = interest.Nonce()
record.LatestTimestamp = time.Now()
record.LatestInterest = interest
record.ExpirationTime = time.Now().Add(interest.Lifetime())
return record, true
}
// FindOrInsertOutRecord finds or inserts an OutRecord for the face, updating the metadata.
func (e *PitEntry) FindOrInsertOutRecord(interest *ndn.Interest, face uint64) *PitOutRecord {
var record *PitOutRecord
var ok bool
if record, ok = e.OutRecords[face]; !ok {
record := new(PitOutRecord)
record.Face = face
record.LatestNonce = interest.Nonce()
record.LatestTimestamp = time.Now()
record.LatestInterest = interest
record.ExpirationTime = time.Now().Add(interest.Lifetime())
e.OutRecords[face] = record
return record
}
// Existing record
record.LatestNonce = interest.Nonce()
record.LatestTimestamp = time.Now()
record.LatestInterest = interest
record.ExpirationTime = time.Now().Add(interest.Lifetime())
return record
}
// UpdateExpirationTimer updates the expiration timer to the latest expiration time of any in or out record in the entry.
func (e *PitEntry) UpdateExpirationTimer() {
e.ExpirationTime = time.Unix(0, 0)
for _, record := range e.InRecords {
if record.ExpirationTime.After(e.ExpirationTime) {
e.ExpirationTime = record.ExpirationTime
}
}
for _, record := range e.OutRecords {
if record.ExpirationTime.After(e.ExpirationTime) {
e.ExpirationTime = record.ExpirationTime
}
}
go e.waitForPitExpiry()
}
// SetExpirationTimerToNow updates the expiration timer to the current time.
func (e *PitEntry) SetExpirationTimerToNow() {
e.ExpirationTime = time.Now()
e.pitCs.ExpiringPitEntries <- e
}
// ClearInRecords removes all in-records from the PIT entry.
func (e *PitEntry) ClearInRecords() {
e.InRecords = make(map[uint64]*PitInRecord)
}
// ClearOutRecords removes all out-records from the PIT entry.
func (e *PitEntry) ClearOutRecords() {
e.OutRecords = make(map[uint64]*PitOutRecord)
}