This commit is contained in:
JackDoan 2025-11-11 17:00:40 -06:00
parent cd30e5aa01
commit 400fdace9d
4 changed files with 19 additions and 35 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"sync"
"syscall"
"github.com/slackhq/nebula/overlay/eventfd"
"golang.org/x/sys/unix"
@ -30,16 +31,6 @@ type SplitQueue struct {
// chains and put them in the used ring.
callEventFD eventfd.EventFD
// UsedChains is a chanel that receives [UsedElement]s for descriptor chains
// that were used by the device.
UsedChains chan UsedElement
// moreFreeDescriptors is a channel that signals when any descriptors were
// put back into the free chain of the descriptor table. This is used to
// unblock methods waiting for available room in the queue to create new
// descriptor chains again.
moreFreeDescriptors chan struct{}
// stop is used by [SplitQueue.Close] to cancel the goroutine that handles
// used buffer notifications. It blocks until the goroutine ended.
stop func() error
@ -131,10 +122,6 @@ func NewSplitQueue(queueSize int) (_ *SplitQueue, err error) {
return nil, fmt.Errorf("initialize descriptors: %w", err)
}
// Initialize channels.
sq.UsedChains = make(chan UsedElement, queueSize)
sq.moreFreeDescriptors = make(chan struct{})
sq.epoll, err = eventfd.NewEpoll()
if err != nil {
return nil, err
@ -366,7 +353,8 @@ func (sq *SplitQueue) OfferOutDescriptorChains(prepend []byte, outBuffers [][]by
// Wait for more free descriptors to be put back into the queue.
// If the number of free descriptors is still not sufficient, we'll
// land here again.
<-sq.moreFreeDescriptors
//todo should never happen
syscall.Syscall(syscall.SYS_SCHED_YIELD, 0, 0, 0) // Cheap barrier
continue
}
return nil, fmt.Errorf("create descriptor chain: %w", err)
@ -400,9 +388,9 @@ func (sq *SplitQueue) GetDescriptorChain(head uint16) (outBuffers, inBuffers [][
return sq.descriptorTable.getDescriptorChain(head)
}
func (sq *SplitQueue) GetDescriptorChainContents(head uint16, out []byte) (int, error) {
func (sq *SplitQueue) GetDescriptorChainContents(head uint16, out []byte, maxLen int) (int, error) {
sq.ensureInitialized()
return sq.descriptorTable.getDescriptorChainContents(head, out)
return sq.descriptorTable.getDescriptorChainContents(head, out, maxLen)
}
// FreeDescriptorChain frees the descriptor chain with the given head index.
@ -415,20 +403,11 @@ func (sq *SplitQueue) GetDescriptorChainContents(head uint16, out []byte) (int,
// When there are outstanding calls for [SplitQueue.OfferDescriptorChain] that
// are waiting for free room in the queue, they may become unblocked by this.
func (sq *SplitQueue) FreeDescriptorChain(head uint16) error {
sq.ensureInitialized()
//not called under lock
if err := sq.descriptorTable.freeDescriptorChain(head); err != nil {
return fmt.Errorf("free: %w", err)
}
// There is more free room in the descriptor table now.
// This is a fire-and-forget signal, so do not block when nobody listens.
select { //todo eliminate
case sq.moreFreeDescriptors <- struct{}{}:
default:
}
return nil
}
@ -473,10 +452,6 @@ func (sq *SplitQueue) Close() error {
errs = append(errs, fmt.Errorf("stop consume used ring: %w", err))
}
// The stop function blocked until the goroutine ended, so the channel
// can now safely be closed.
close(sq.UsedChains)
// Make sure that this code block is executed only once.
sq.stop = nil
}