Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] malloc optimizations #84

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions dv/dv/table_algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,24 @@ func (dv *Router) fibUpdate() {

// Update paths to all routers from RIB
for _, router := range dv.rib.Entries() {
routerName := router.Name()

// Skip if this is us
if router.Name().Equal(dv.config.RouterName()) {
if routerName.Equal(dv.config.RouterName()) {
continue
}

// Get FIB entry to reach this router
fes := dv.rib.GetFibEntries(dv.neighbors, router.Name().Hash())
fes := dv.rib.GetFibEntries(dv.neighbors, routerName.Hash())

// Add entry to the router itself
routerPrefix := append(router.Name(),
routerPrefix := append(routerName,
enc.NewStringComponent(enc.TypeKeywordNameComponent, "DV"),
)
register(routerPrefix, fes)

// Add entries to all prefixes announced by this router
for _, prefix := range dv.pfx.GetRouter(router.Name()).Prefixes {
for _, prefix := range dv.pfx.GetRouter(routerName).Prefixes {
// Use the same nexthop entries as the exit router itself
// De-duplication is done by the fib table update function
register(prefix.Name, fes)
Expand Down
32 changes: 24 additions & 8 deletions dv/tlv/zz_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions fw/core/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ func LogTrace(module interface{}, components ...interface{}) {
log.Debug(generateLogMessage(module, components...))
}
}

func HasTraceLogs() bool {
return shouldPrintTraceLogs
}

func HasDebugLogs() bool {
return logLevel <= log.DebugLevel
}
3 changes: 3 additions & 0 deletions fw/defn/mtu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ package defn

// MaxNDNPacketSize is the maximum allowed NDN packet size
const MaxNDNPacketSize = 8800

// Maximum number of next hops for a given path
const MaxNextHops = 16
11 changes: 9 additions & 2 deletions fw/defn/pkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ 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
CongestionMark *uint64
IncomingFaceID *uint64
IncomingFaceID uint64
NextHopFaceID *uint64
CachePolicy *uint64
}

type PacketIntf struct {
LpPacket *spec.LpPacket
Interest ndn.Interest
Data ndn.Data
}
2 changes: 1 addition & 1 deletion fw/dispatch/face.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Face interface {
type OutPkt struct {
Pkt *defn.Pkt
PitToken []byte
InFace *uint64
InFace uint64
}

// FaceDispatch is used to allow forwarding to interact with faces without a circular dependency issue.
Expand Down
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()
zjkmxy marked this conversation as resolved.
Show resolved Hide resolved

// 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
65 changes: 44 additions & 21 deletions fw/face/ndnlp-link-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type NDNLPLinkService struct {
BufferReader enc.BufferReader
congestionCheck uint64
outFrame []byte
outWire enc.Wire
}

// MakeNDNLPLinkService creates a new NDNLPv2 link service
Expand All @@ -88,7 +89,8 @@ func MakeNDNLPLinkService(transport transport, options NDNLPLinkServiceOptions)
l.nextSequence = 0
l.nextTxSequence = 0
l.congestionCheck = 0
l.outFrame = make([]byte, defn.MaxNDNPacketSize)
l.outFrame = make([]byte, defn.MaxNDNPacketSize*2)
l.outWire = make(enc.Wire, defn.MaxNDNPacketSize)
return l
}

Expand Down Expand Up @@ -217,6 +219,7 @@ func sendPacket(l *NDNLPLinkService, out dispatch.OutPkt) {
}
fragments[i] = &spec.LpPacket{Fragment: frag}
}
reader.Free()
} else {
fragments = []*spec.LpPacket{{Fragment: enc.Wire{wire}}}
}
Expand Down Expand Up @@ -256,32 +259,35 @@ func sendPacket(l *NDNLPLinkService, out dispatch.OutPkt) {
}

// Incoming face indication
if l.options.IsIncomingFaceIndicationEnabled && out.InFace != nil {
fragment.IncomingFaceId = out.InFace
if l.options.IsIncomingFaceIndicationEnabled {
fragment.IncomingFaceId = utils.IdPtr(out.InFace)
}

// Congestion marking
if congestionMark != nil {
fragment.CongestionMark = congestionMark
}

pkt := &spec.Packet{
LpPacket: fragment,
}
pkt := spec.Packet{LpPacket: fragment}

// Use preallocated buffers for outgoing frame
encoder := spec.PacketEncoder{}
encoder.Init(pkt)
frameWire := encoder.Encode(pkt)
if frameWire == nil {
core.LogError(l, "Unable to encode fragment - DROP")
break
wirePlan := encoder.Init(&pkt)
outFrame := l.outFrame
outWire := l.outWire[:len(wirePlan)]

for i, l := range wirePlan {
outWire[i] = outFrame[:l]
outFrame = outFrame[l:]
}
encoder.EncodeInto(&pkt, outWire)

// Use preallocated buffer for outgoing frame
l.outFrame = l.outFrame[:0]
for _, b := range frameWire {
l.outFrame = append(l.outFrame, b...)
// Consolidate fragments. Since we only overwrite behind, there is no conflict.
nbytes := 0
for _, b := range outWire {
nbytes += copy(l.outFrame[nbytes:], b)
}
l.transport.sendFrame(l.outFrame)
l.transport.sendFrame(l.outFrame[:nbytes])
}
}

Expand All @@ -293,10 +299,12 @@ func (l *NDNLPLinkService) handleIncomingFrame(frame []byte) {
// All incoming frames come through a link service
// Attempt to decode buffer into LpPacket
pkt := &defn.Pkt{
IncomingFaceID: utils.IdPtr(l.faceID),
IncomingFaceID: l.faceID,
}

L2, err := ReadPacketUnverified(enc.NewBufferReader(wire))
L2r := enc.NewBufferReader(wire)
L2, err := ReadPacketUnverified(L2r)
L2r.Free()
if err != nil {
core.LogError(l, err)
return
Expand Down Expand Up @@ -370,7 +378,9 @@ func (l *NDNLPLinkService) handleIncomingFrame(frame []byte) {
}

// Parse inner packet in place
L3, err := ReadPacketUnverified(enc.NewBufferReader(wire))
L3r := enc.NewBufferReader(wire)
L3, err := ReadPacketUnverified(L3r)
L3r.Free()
if err != nil {
return
}
Expand Down Expand Up @@ -444,8 +454,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
}
7 changes: 3 additions & 4 deletions fw/face/stream-transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ func readTlvStream(
// Determine whether valid packet received
for {
rdr := enc.NewBufferReader(recvBuf[tlvOff:recvOff])

typ, err := enc.ReadTLNum(rdr)
if err != nil {
// Probably incomplete packet
break
}

len, err := enc.ReadTLNum(rdr)
length, err := enc.ReadTLNum(rdr)
if err != nil {
// Probably incomplete packet
break
}
rdr.Free()

tlvSize := typ.EncodingLength() + len.EncodingLength() + int(len)
tlvSize := typ.EncodingLength() + length.EncodingLength() + int(length)

if recvOff-tlvOff >= tlvSize {
// Packet was successfully received, send up to link service
Expand Down
Loading
Loading