Skip to content

Commit

Permalink
fw: refactor everything to use packet interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Dec 25, 2024
1 parent 6dd1b2b commit bb7f890
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 115 deletions.
9 changes: 8 additions & 1 deletion fw/defn/pkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ package defn

import (
enc "github.com/named-data/ndnd/std/encoding"
"github.com/named-data/ndnd/std/ndn"
spec "github.com/named-data/ndnd/std/ndn/spec_2022"
)

// Pkt represents a pending packet to be sent or recently
// received on the link, plus any associated metadata.
type Pkt struct {
Name enc.Name
L3 *spec.Packet
L3 PacketIntf
Raw []byte

PitToken []byte
Expand All @@ -25,3 +26,9 @@ type Pkt struct {
NextHopFaceID *uint64
CachePolicy *uint64
}

type PacketIntf struct {
LpPacket *spec.LpPacket
Interest ndn.Interest
Data ndn.Data
}
4 changes: 2 additions & 2 deletions fw/face/link-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (l *linkServiceBase) dispatchInterest(pkt *defn.Pkt) {
}

// Store name for easy access
pkt.Name = pkt.L3.Interest.NameV
pkt.Name = pkt.L3.Interest.Name()

// Hash name to thread
thread := fw.HashNameToFwThread(pkt.Name)
Expand All @@ -240,7 +240,7 @@ func (l *linkServiceBase) dispatchData(pkt *defn.Pkt) {
}

// Store name for easy access
pkt.Name = pkt.L3.Data.NameV
pkt.Name = pkt.L3.Data.Name()

// Decode PitToken. If it's for us, it's a uint16 + uint32.
if len(pkt.PitToken) == 6 {
Expand Down
17 changes: 15 additions & 2 deletions fw/face/ndnlp-link-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,21 @@ func (op *NDNLPLinkServiceOptions) Flags() (ret uint64) {
}

// Reads a packet without validating the internal fields
func ReadPacketUnverified(reader enc.ParseReader) (*spec.Packet, error) {
func ReadPacketUnverified(reader enc.ParseReader) (ret defn.PacketIntf, err error) {
context := spec.PacketParsingContext{}
context.Init()
return context.Parse(reader, false)
packet, err := context.Parse(reader, false)
if err != nil {
return
}

if packet.LpPacket != nil {
ret.LpPacket = packet.LpPacket
} else if packet.Interest != nil {
ret.Interest = packet.Interest
} else if packet.Data != nil {
ret.Data = packet.Data
}

return ret, nil
}
2 changes: 1 addition & 1 deletion fw/fw/bestroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *BestRoute) AfterReceiveInterest(
// If there is an out record less than suppression interval ago, drop the
// retransmission to suppress it (only if the nonce is different)
for _, outRecord := range pitEntry.OutRecords() {
if outRecord.LatestNonce != *packet.L3.Interest.NonceV &&
if outRecord.LatestNonce != *packet.L3.Interest.Nonce() &&
outRecord.LatestTimestamp.Add(BestRouteSuppressionTime).After(time.Now()) {
core.LogDebug(s, "AfterReceiveInterest: Suppressed Interest=", packet.Name, " - DROP")
return
Expand Down
2 changes: 1 addition & 1 deletion fw/fw/multicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *Multicast) AfterReceiveInterest(
// If there is an out record less than suppression interval ago, drop the
// retransmission to suppress it (only if the nonce is different)
for _, outRecord := range pitEntry.OutRecords() {
if outRecord.LatestNonce != *packet.L3.Interest.NonceV &&
if outRecord.LatestNonce != *packet.L3.Interest.Nonce() &&
outRecord.LatestTimestamp.Add(MulticastSuppressionTime).After(time.Now()) {
core.LogDebug(s, "AfterReceiveInterest: Suppressed Interest=", packet.Name, " - DROP")
return
Expand Down
44 changes: 22 additions & 22 deletions fw/fw/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,21 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {
return
}

if interest.HopLimitV != nil {
core.LogTrace(t, "Interest ", packet.Name, " has HopLimit=", *interest.HopLimitV)
if *interest.HopLimitV == 0 {
if interest.HopLimit() != nil {
core.LogTrace(t, "Interest ", packet.Name, " has HopLimit=", *interest.HopLimit())
if *interest.HopLimit() == 0 {
return
}
*interest.HopLimitV -= 1
*interest.HopLimit() -= 1
}

// Log PIT token (if any)
core.LogTrace(t, "OnIncomingInterest: ", packet.Name, ", FaceID=", incomingFace.FaceID(), ", PitTokenL=", len(packet.PitToken))

// Check if violates /localhost
if incomingFace.Scope() == defn.NonLocal &&
len(interest.NameV) > 0 &&
bytes.Equal(interest.NameV[0].Val, LOCALHOST) {
len(interest.Name()) > 0 &&
bytes.Equal(interest.Name()[0].Val, LOCALHOST) {
core.LogWarn(t, "Interest ", packet.Name, " from non-local face=", incomingFace.FaceID(), " violates /localhost scope - DROP")
return
}
Expand All @@ -208,10 +208,10 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {
// Check for forwarding hint and, if present, determine if reaching producer region (and then strip forwarding hint)
isReachingProducerRegion := true
var fhName enc.Name = nil
hint := interest.ForwardingHintV
if hint != nil && len(hint.Names) > 0 {
hint := interest.ForwardingHint()
if hint != nil && len(hint) > 0 {
isReachingProducerRegion = false
for _, fh := range hint.Names {
for _, fh := range hint {
if table.NetworkRegion.IsProducer(fh) {
isReachingProducerRegion = true
break
Expand All @@ -227,14 +227,14 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {
}

// Drop packet if no nonce is found
if interest.NonceV == nil {
if interest.Nonce() == nil {
core.LogDebug(t, "Interest ", packet.Name, " is missing Nonce - DROP")
return
}

// Check if packet is in dead nonce list
if exists := t.deadNonceList.Find(interest.NameV, *interest.NonceV); exists {
core.LogDebug(t, "Interest ", packet.Name, " is dropped by DeadNonce: ", *interest.NonceV)
if exists := t.deadNonceList.Find(interest.Name(), *interest.Nonce()); exists {
core.LogDebug(t, "Interest ", packet.Name, " is dropped by DeadNonce: ", *interest.Nonce())
return
}

Expand All @@ -248,7 +248,7 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {
}

// Get strategy for name
strategyName := table.FibStrategyTable.FindStrategyEnc(interest.NameV)
strategyName := table.FibStrategyTable.FindStrategyEnc(interest.Name())
strategy := t.strategies[strategyName.Hash()]

// Add in-record and determine if already pending
Expand All @@ -271,7 +271,7 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {
packet.L3.Data = csData
packet.L3.Interest = nil
packet.Raw = csWire
packet.Name = csData.NameV
packet.Name = csData.Name()
strategy.AfterContentStoreHit(packet, pitEntry, incomingFace.FaceID())
return
} else if err != nil {
Expand All @@ -287,7 +287,7 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {

// Add the previous nonce to the dead nonce list to prevent further looping
// TODO: review this design, not specified in NFD dev guide
t.deadNonceList.Insert(interest.NameV, prevNonce)
t.deadNonceList.Insert(interest.Name(), prevNonce)
}

// Update PIT entry expiration timer
Expand All @@ -309,7 +309,7 @@ func (t *Thread) processIncomingInterest(packet *defn.Pkt) {
}

// Use forwarding hint if present
lookupName := interest.NameV
lookupName := interest.Name()
if fhName != nil {
lookupName = fhName
}
Expand Down Expand Up @@ -356,7 +356,7 @@ func (t *Thread) processOutgoingInterest(
}

// Drop if HopLimit (if present) on Interest going to non-local face is 0. If so, drop
if interest.HopLimitV != nil && int(*interest.HopLimitV) == 0 &&
if interest.HopLimit() != nil && int(*interest.HopLimit()) == 0 &&
outgoingFace.Scope() == defn.NonLocal {
core.LogDebug(t, "Attempting to send Interest=", packet.Name, " with HopLimit=0 to non-local face - DROP")
return false
Expand Down Expand Up @@ -424,7 +424,7 @@ func (t *Thread) processIncomingData(packet *defn.Pkt) {

// Check if violates /localhost
if incomingFace.Scope() == defn.NonLocal && len(packet.Name) > 0 &&
bytes.Equal(data.NameV[0].Val, LOCALHOST) {
bytes.Equal(data.Name()[0].Val, LOCALHOST) {
core.LogWarn(t, "Data ", packet.Name, " from non-local FaceID=", *packet.IncomingFaceID, " violates /localhost scope - DROP")
return
}
Expand All @@ -443,7 +443,7 @@ func (t *Thread) processIncomingData(packet *defn.Pkt) {
}

// Get strategy for name
strategyName := table.FibStrategyTable.FindStrategyEnc(data.NameV)
strategyName := table.FibStrategyTable.FindStrategyEnc(data.Name())
strategy := t.strategies[strategyName.Hash()]

if len(pitEntries) == 1 {
Expand All @@ -463,7 +463,7 @@ func (t *Thread) processIncomingData(packet *defn.Pkt) {

// Insert into dead nonce list
for _, outRecord := range pitEntry.OutRecords() {
t.deadNonceList.Insert(data.NameV, outRecord.LatestNonce)
t.deadNonceList.Insert(data.Name(), outRecord.LatestNonce)
}

// Clear out records from PIT entry
Expand Down Expand Up @@ -496,7 +496,7 @@ func (t *Thread) processIncomingData(packet *defn.Pkt) {

// Insert into dead nonce list
for _, outRecord := range pitEntries[0].GetOutRecords() {
t.deadNonceList.Insert(data.NameV, outRecord.LatestNonce)
t.deadNonceList.Insert(data.Name(), outRecord.LatestNonce)
}

// Clear PIT entry's in- and out-records
Expand Down Expand Up @@ -533,7 +533,7 @@ func (t *Thread) processOutgoingData(
}

// Check if violates /localhost
if outgoingFace.Scope() == defn.NonLocal && len(data.NameV) > 0 && bytes.Equal(data.NameV[0].Val, LOCALHOST) {
if outgoingFace.Scope() == defn.NonLocal && len(data.Name()) > 0 && bytes.Equal(data.Name()[0].Val, LOCALHOST) {
core.LogWarn(t, "Data ", packet.Name, " cannot be sent to non-local FaceID=", nexthop, " since violates /localhost scope - DROP")
return
}
Expand Down
6 changes: 3 additions & 3 deletions fw/table/cs-lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package table
import (
"container/list"

spec "github.com/named-data/ndnd/std/ndn/spec_2022"
"github.com/named-data/ndnd/std/ndn"
)

// CsLRU is a least recently used (LRU) replacement policy for the Content Store.
Expand All @@ -30,12 +30,12 @@ func NewCsLRU(cs PitCsTable) *CsLRU {
}

// AfterInsert is called after a new entry is inserted into the Content Store.
func (l *CsLRU) AfterInsert(index uint64, wire []byte, data *spec.Data) {
func (l *CsLRU) AfterInsert(index uint64, wire []byte, data ndn.Data) {
l.locations[index] = l.queue.PushBack(index)
}

// AfterRefresh is called after a new data packet refreshes an existing entry in the Content Store.
func (l *CsLRU) AfterRefresh(index uint64, wire []byte, data *spec.Data) {
func (l *CsLRU) AfterRefresh(index uint64, wire []byte, data ndn.Data) {
if location, ok := l.locations[index]; ok {
l.queue.Remove(location)
}
Expand Down
8 changes: 5 additions & 3 deletions fw/table/cs-replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

package table

import spec "github.com/named-data/ndnd/std/ndn/spec_2022"
import (
"github.com/named-data/ndnd/std/ndn"
)

// CsReplacementPolicy represents a cache replacement policy for the Content Store.
type CsReplacementPolicy interface {
// AfterInsert is called after a new entry is inserted into the Content Store.
AfterInsert(index uint64, wire []byte, data *spec.Data)
AfterInsert(index uint64, wire []byte, data ndn.Data)

// AfterRefresh is called after a new data packet refreshes an existing entry in the Content Store.
AfterRefresh(index uint64, wire []byte, data *spec.Data)
AfterRefresh(index uint64, wire []byte, data ndn.Data)

// BeforeErase is called before an entry is erased from the Content Store through management.
BeforeErase(index uint64, wire []byte)
Expand Down
8 changes: 4 additions & 4 deletions fw/table/dead-nonce-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func NewDeadNonceList() *DeadNonceList {
}

// Find returns whether the specified name and nonce combination are present in the Dead Nonce List.
func (d *DeadNonceList) Find(name enc.Name, nonce uint32) bool {
_, ok := d.list[name.Hash()+uint64(nonce)]
func (d *DeadNonceList) Find(name enc.Name, nonce uint64) bool {
_, ok := d.list[name.Hash()+nonce]
return ok
}

// Insert inserts an entry in the Dead Nonce List with the specified name and nonce.
// Returns whether nonce already present.
func (d *DeadNonceList) Insert(name enc.Name, nonce uint32) bool {
hash := name.Hash() + uint64(nonce)
func (d *DeadNonceList) Insert(name enc.Name, nonce uint64) bool {
hash := name.Hash() + nonce
_, exists := d.list[hash]

if !exists {
Expand Down
Loading

0 comments on commit bb7f890

Please sign in to comment.