From f76d64021e0c3828e5fef79c62c9d4e8e569565b Mon Sep 17 00:00:00 2001 From: Kevin Krakauer Date: Wed, 20 Dec 2023 14:54:18 -0800 Subject: [PATCH] xdp: update the incorrectly named "Readonly*" names PiperOrigin-RevId: 592664806 --- pkg/tcpip/link/xdp/endpoint.go | 4 ++-- pkg/xdp/xdp.go | 28 +++++++++++++--------------- pkg/xdp/xdp_unsafe.go | 8 ++++---- tools/xdp/cmd/tcpdump.go | 4 ++-- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pkg/tcpip/link/xdp/endpoint.go b/pkg/tcpip/link/xdp/endpoint.go index 95009c591..914a0f7c6 100644 --- a/pkg/tcpip/link/xdp/endpoint.go +++ b/pkg/tcpip/link/xdp/endpoint.go @@ -151,13 +151,13 @@ func New(opts *Options) (stack.LinkEndpoint, error) { umemSize = 1 << 21 nFrames = umemSize / frameSize ) - xdpOpts := xdp.ReadOnlySocketOpts{ + xdpOpts := xdp.Opts{ NFrames: nFrames, FrameSize: frameSize, NDescriptors: nFrames / 2, Bind: opts.Bind, } - ep.control, err = xdp.ReadOnlyFromSocket(opts.FD, uint32(opts.InterfaceIndex), 0 /* queueID */, xdpOpts) + ep.control, err = xdp.NewFromSocket(opts.FD, uint32(opts.InterfaceIndex), 0 /* queueID */, xdpOpts) if err != nil { return nil, fmt.Errorf("failed to create AF_XDP dispatcher: %v", err) } diff --git a/pkg/xdp/xdp.go b/pkg/xdp/xdp.go index 941401a33..5863d4221 100644 --- a/pkg/xdp/xdp.go +++ b/pkg/xdp/xdp.go @@ -63,10 +63,8 @@ type ControlBlock struct { Completion CompletionQueue } -// TODO(b/240191988): None of this is read-only anymore. - -// ReadOnlySocketOpts configure a read-only AF_XDP socket. -type ReadOnlySocketOpts struct { +// Opts configure an AF_XDP socket. +type Opts struct { NFrames uint32 FrameSize uint32 NDescriptors uint32 @@ -74,11 +72,11 @@ type ReadOnlySocketOpts struct { UseNeedWakeup bool } -// DefaultReadOnlyOpts provides recommended default options for initializing a -// readonly AF_XDP socket. AF_XDP setup is extremely finnicky and can fail if -// incorrect values are used. -func DefaultReadOnlyOpts() ReadOnlySocketOpts { - return ReadOnlySocketOpts{ +// DefaultOpts provides recommended default options for initializing an AF_XDP +// socket. AF_XDP setup is extremely finnicky and can fail if incorrect values +// are used. +func DefaultOpts() Opts { + return Opts{ NFrames: 4096, // Frames must be 2048 or 4096 bytes, although not all drivers support // both. @@ -87,19 +85,19 @@ func DefaultReadOnlyOpts() ReadOnlySocketOpts { } } -// ReadOnlySocket returns an initialized read-only AF_XDP socket bound to a -// particular interface and queue. -func ReadOnlySocket(ifaceIdx, queueID uint32, opts ReadOnlySocketOpts) (*ControlBlock, error) { +// New returns an initialized AF_XDP socket bound to a particular interface and +// queue. +func New(ifaceIdx, queueID uint32, opts Opts) (*ControlBlock, error) { sockfd, err := unix.Socket(unix.AF_XDP, unix.SOCK_RAW, 0) if err != nil { return nil, fmt.Errorf("failed to create AF_XDP socket: %v", err) } - return ReadOnlyFromSocket(sockfd, ifaceIdx, queueID, opts) + return NewFromSocket(sockfd, ifaceIdx, queueID, opts) } -// ReadOnlyFromSocket takes an AF_XDP socket, initializes it, and binds it to a +// NewFromSocket takes an AF_XDP socket, initializes it, and binds it to a // particular interface and queue. -func ReadOnlyFromSocket(sockfd int, ifaceIdx, queueID uint32, opts ReadOnlySocketOpts) (*ControlBlock, error) { +func NewFromSocket(sockfd int, ifaceIdx, queueID uint32, opts Opts) (*ControlBlock, error) { if opts.FrameSize != 2048 && opts.FrameSize != 4096 { return nil, fmt.Errorf("invalid frame size %d: must be either 2048 or 4096", opts.FrameSize) } diff --git a/pkg/xdp/xdp_unsafe.go b/pkg/xdp/xdp_unsafe.go index ab99a7f36..75ff263ed 100644 --- a/pkg/xdp/xdp_unsafe.go +++ b/pkg/xdp/xdp_unsafe.go @@ -61,7 +61,7 @@ func sizeOfTXQueueDesc() uint64 { return uint64(unsafe.Sizeof(unix.XDPDesc{})) } -func (fq *FillQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts) { +func (fq *FillQueue) init(off unix.XDPMmapOffsets, opts Opts) { fillQueueRingHdr := (*reflect.SliceHeader)(unsafe.Pointer(&fq.ring)) fillQueueRingHdr.Data = uintptr(unsafe.Pointer(&fq.mem[off.Fr.Desc])) fillQueueRingHdr.Len = int(opts.NDescriptors) @@ -71,7 +71,7 @@ func (fq *FillQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts) { fq.flags = (*atomicbitops.Uint32)(unsafe.Pointer(&fq.mem[off.Fr.Flags])) } -func (rq *RXQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts) { +func (rq *RXQueue) init(off unix.XDPMmapOffsets, opts Opts) { rxQueueRingHdr := (*reflect.SliceHeader)(unsafe.Pointer(&rq.ring)) rxQueueRingHdr.Data = uintptr(unsafe.Pointer(&rq.mem[off.Rx.Desc])) rxQueueRingHdr.Len = int(opts.NDescriptors) @@ -85,7 +85,7 @@ func (rq *RXQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts) { rq.cachedConsumer = rq.consumer.Load() } -func (cq *CompletionQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts) { +func (cq *CompletionQueue) init(off unix.XDPMmapOffsets, opts Opts) { completionQueueRingHdr := (*reflect.SliceHeader)(unsafe.Pointer(&cq.ring)) completionQueueRingHdr.Data = uintptr(unsafe.Pointer(&cq.mem[off.Cr.Desc])) completionQueueRingHdr.Len = int(opts.NDescriptors) @@ -99,7 +99,7 @@ func (cq *CompletionQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts cq.cachedConsumer = cq.consumer.Load() } -func (tq *TXQueue) init(off unix.XDPMmapOffsets, opts ReadOnlySocketOpts) { +func (tq *TXQueue) init(off unix.XDPMmapOffsets, opts Opts) { txQueueRingHdr := (*reflect.SliceHeader)(unsafe.Pointer(&tq.ring)) txQueueRingHdr.Data = uintptr(unsafe.Pointer(&tq.mem[off.Tx.Desc])) txQueueRingHdr.Len = int(opts.NDescriptors) diff --git a/tools/xdp/cmd/tcpdump.go b/tools/xdp/cmd/tcpdump.go index 96988ef6c..6802791b1 100644 --- a/tools/xdp/cmd/tcpdump.go +++ b/tools/xdp/cmd/tcpdump.go @@ -106,8 +106,8 @@ func (pc *TcpdumpCommand) execute() error { } defer cleanup() - controlBlock, err := xdp.ReadOnlySocket( - uint32(iface.Index), 0 /* queueID */, xdp.DefaultReadOnlyOpts()) + controlBlock, err := xdp.New( + uint32(iface.Index), 0 /* queueID */, xdp.DefaultOpts()) if err != nil { return fmt.Errorf("failed to create socket: %v", err) }