mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-22 16:34:25 +01:00
pull deps in for optimization, maybe slice back out later
This commit is contained in:
3
overlay/virtio/doc.go
Normal file
3
overlay/virtio/doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package virtio contains some generic types and concepts related to the virtio
|
||||
// protocol.
|
||||
package virtio
|
||||
136
overlay/virtio/features.go
Normal file
136
overlay/virtio/features.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package virtio
|
||||
|
||||
// Feature contains feature bits that describe a virtio device or driver.
|
||||
type Feature uint64
|
||||
|
||||
// Device-independent feature bits.
|
||||
//
|
||||
// Source: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-6600006
|
||||
const (
|
||||
// FeatureIndirectDescriptors indicates that the driver can use descriptors
|
||||
// with an additional layer of indirection.
|
||||
FeatureIndirectDescriptors Feature = 1 << 28
|
||||
|
||||
// FeatureVersion1 indicates compliance with version 1.0 of the virtio
|
||||
// specification.
|
||||
FeatureVersion1 Feature = 1 << 32
|
||||
)
|
||||
|
||||
// Feature bits for networking devices.
|
||||
//
|
||||
// Source: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-2200003
|
||||
const (
|
||||
// FeatureNetDeviceCsum indicates that the device can handle packets with
|
||||
// partial checksum (checksum offload).
|
||||
FeatureNetDeviceCsum Feature = 1 << 0
|
||||
|
||||
// FeatureNetDriverCsum indicates that the driver can handle packets with
|
||||
// partial checksum.
|
||||
FeatureNetDriverCsum Feature = 1 << 1
|
||||
|
||||
// FeatureNetCtrlDriverOffloads indicates support for dynamic offload state
|
||||
// reconfiguration.
|
||||
FeatureNetCtrlDriverOffloads Feature = 1 << 2
|
||||
|
||||
// FeatureNetMTU indicates that the device reports a maximum MTU value.
|
||||
FeatureNetMTU Feature = 1 << 3
|
||||
|
||||
// FeatureNetMAC indicates that the device provides a MAC address.
|
||||
FeatureNetMAC Feature = 1 << 5
|
||||
|
||||
// FeatureNetDriverTSO4 indicates that the driver supports the TCP
|
||||
// segmentation offload for received IPv4 packets.
|
||||
FeatureNetDriverTSO4 Feature = 1 << 7
|
||||
|
||||
// FeatureNetDriverTSO6 indicates that the driver supports the TCP
|
||||
// segmentation offload for received IPv6 packets.
|
||||
FeatureNetDriverTSO6 Feature = 1 << 8
|
||||
|
||||
// FeatureNetDriverECN indicates that the driver supports the TCP
|
||||
// segmentation offload with ECN for received packets.
|
||||
FeatureNetDriverECN Feature = 1 << 9
|
||||
|
||||
// FeatureNetDriverUFO indicates that the driver supports the UDP
|
||||
// fragmentation offload for received packets.
|
||||
FeatureNetDriverUFO Feature = 1 << 10
|
||||
|
||||
// FeatureNetDeviceTSO4 indicates that the device supports the TCP
|
||||
// segmentation offload for received IPv4 packets.
|
||||
FeatureNetDeviceTSO4 Feature = 1 << 11
|
||||
|
||||
// FeatureNetDeviceTSO6 indicates that the device supports the TCP
|
||||
// segmentation offload for received IPv6 packets.
|
||||
FeatureNetDeviceTSO6 Feature = 1 << 12
|
||||
|
||||
// FeatureNetDeviceECN indicates that the device supports the TCP
|
||||
// segmentation offload with ECN for received packets.
|
||||
FeatureNetDeviceECN Feature = 1 << 13
|
||||
|
||||
// FeatureNetDeviceUFO indicates that the device supports the UDP
|
||||
// fragmentation offload for received packets.
|
||||
FeatureNetDeviceUFO Feature = 1 << 14
|
||||
|
||||
// FeatureNetMergeRXBuffers indicates that the driver can handle merged
|
||||
// receive buffers.
|
||||
// When this feature is negotiated, devices may merge multiple descriptor
|
||||
// chains together to transport large received packets. [NetHdr.NumBuffers]
|
||||
// will then contain the number of merged descriptor chains.
|
||||
FeatureNetMergeRXBuffers Feature = 1 << 15
|
||||
|
||||
// FeatureNetStatus indicates that the device configuration status field is
|
||||
// available.
|
||||
FeatureNetStatus Feature = 1 << 16
|
||||
|
||||
// FeatureNetCtrlVQ indicates that a control channel virtqueue is
|
||||
// available.
|
||||
FeatureNetCtrlVQ Feature = 1 << 17
|
||||
|
||||
// FeatureNetCtrlRX indicates support for RX mode control (e.g. promiscuous
|
||||
// or all-multicast) for packet receive filtering.
|
||||
FeatureNetCtrlRX Feature = 1 << 18
|
||||
|
||||
// FeatureNetCtrlVLAN indicates support for VLAN filtering through the
|
||||
// control channel.
|
||||
FeatureNetCtrlVLAN Feature = 1 << 19
|
||||
|
||||
// FeatureNetDriverAnnounce indicates that the driver can send gratuitous
|
||||
// packets.
|
||||
FeatureNetDriverAnnounce Feature = 1 << 21
|
||||
|
||||
// FeatureNetMQ indicates that the device supports multiqueue with automatic
|
||||
// receive steering.
|
||||
FeatureNetMQ Feature = 1 << 22
|
||||
|
||||
// FeatureNetCtrlMACAddr indicates that the MAC address can be set through
|
||||
// the control channel.
|
||||
FeatureNetCtrlMACAddr Feature = 1 << 23
|
||||
|
||||
// FeatureNetDeviceUSO indicates that the device supports the UDP
|
||||
// segmentation offload for received packets.
|
||||
FeatureNetDeviceUSO Feature = 1 << 56
|
||||
|
||||
// FeatureNetHashReport indicates that the device can report a per-packet
|
||||
// hash value and type.
|
||||
FeatureNetHashReport Feature = 1 << 57
|
||||
|
||||
// FeatureNetDriverHdrLen indicates that the driver can provide the exact
|
||||
// header length value (see [NetHdr.HdrLen]).
|
||||
// Devices may benefit from knowing the exact header length.
|
||||
FeatureNetDriverHdrLen Feature = 1 << 59
|
||||
|
||||
// FeatureNetRSS indicates that the device supports RSS (receive-side
|
||||
// scaling) with configurable hash parameters.
|
||||
FeatureNetRSS Feature = 1 << 60
|
||||
|
||||
// FeatureNetRSCExt indicates that the device can process duplicated ACKs
|
||||
// and report the number of coalesced segments and duplicated ACKs.
|
||||
FeatureNetRSCExt Feature = 1 << 61
|
||||
|
||||
// FeatureNetStandby indicates that the device may act as a standby for a
|
||||
// primary device with the same MAC address.
|
||||
FeatureNetStandby Feature = 1 << 62
|
||||
|
||||
// FeatureNetSpeedDuplex indicates that the device can report link speed and
|
||||
// duplex mode.
|
||||
FeatureNetSpeedDuplex Feature = 1 << 63
|
||||
)
|
||||
77
overlay/virtio/net_hdr.go
Normal file
77
overlay/virtio/net_hdr.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package virtio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Workaround to make Go doc links work.
|
||||
var _ unix.Errno
|
||||
|
||||
// NetHdrSize is the number of bytes needed to store a [NetHdr] in memory.
|
||||
const NetHdrSize = 12
|
||||
|
||||
// ErrNetHdrBufferTooSmall is returned when a buffer is too small to fit a
|
||||
// virtio_net_hdr.
|
||||
var ErrNetHdrBufferTooSmall = errors.New("the buffer is too small to fit a virtio_net_hdr")
|
||||
|
||||
// NetHdr defines the virtio_net_hdr as described by the virtio specification.
|
||||
type NetHdr struct {
|
||||
// Flags that describe the packet.
|
||||
// Possible values are:
|
||||
// - [unix.VIRTIO_NET_HDR_F_NEEDS_CSUM]
|
||||
// - [unix.VIRTIO_NET_HDR_F_DATA_VALID]
|
||||
// - [unix.VIRTIO_NET_HDR_F_RSC_INFO]
|
||||
Flags uint8
|
||||
// GSOType contains the type of segmentation offload that should be used for
|
||||
// the packet.
|
||||
// Possible values are:
|
||||
// - [unix.VIRTIO_NET_HDR_GSO_NONE]
|
||||
// - [unix.VIRTIO_NET_HDR_GSO_TCPV4]
|
||||
// - [unix.VIRTIO_NET_HDR_GSO_UDP]
|
||||
// - [unix.VIRTIO_NET_HDR_GSO_TCPV6]
|
||||
// - [unix.VIRTIO_NET_HDR_GSO_UDP_L4]
|
||||
// - [unix.VIRTIO_NET_HDR_GSO_ECN]
|
||||
GSOType uint8
|
||||
// HdrLen contains the length of the headers that need to be replicated by
|
||||
// segmentation offloads. It's the number of bytes from the beginning of the
|
||||
// packet to the beginning of the transport payload.
|
||||
// Only used when [FeatureNetDriverHdrLen] is negotiated.
|
||||
HdrLen uint16
|
||||
// GSOSize contains the maximum size of each segmented packet beyond the
|
||||
// header (payload size). In case of TCP, this is the MSS.
|
||||
GSOSize uint16
|
||||
// CsumStart contains the offset within the packet from which on the
|
||||
// checksum should be computed.
|
||||
CsumStart uint16
|
||||
// CsumOffset specifies how many bytes after [NetHdr.CsumStart] the computed
|
||||
// 16-bit checksum should be inserted.
|
||||
CsumOffset uint16
|
||||
// NumBuffers contains the number of merged descriptor chains when
|
||||
// [FeatureNetMergeRXBuffers] is negotiated.
|
||||
// This field is only used for packets received by the driver and should be
|
||||
// zero for transmitted packets.
|
||||
NumBuffers uint16
|
||||
}
|
||||
|
||||
// Decode decodes the [NetHdr] from the given byte slice. The slice must contain
|
||||
// at least [NetHdrSize] bytes.
|
||||
func (v *NetHdr) Decode(data []byte) error {
|
||||
if len(data) < NetHdrSize {
|
||||
return ErrNetHdrBufferTooSmall
|
||||
}
|
||||
copy(unsafe.Slice((*byte)(unsafe.Pointer(v)), NetHdrSize), data[:NetHdrSize])
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encode encodes the [NetHdr] into the given byte slice. The slice must have
|
||||
// room for at least [NetHdrSize] bytes.
|
||||
func (v *NetHdr) Encode(data []byte) error {
|
||||
if len(data) < NetHdrSize {
|
||||
return ErrNetHdrBufferTooSmall
|
||||
}
|
||||
copy(data[:NetHdrSize], unsafe.Slice((*byte)(unsafe.Pointer(v)), NetHdrSize))
|
||||
return nil
|
||||
}
|
||||
43
overlay/virtio/net_hdr_test.go
Normal file
43
overlay/virtio/net_hdr_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package virtio
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestNetHdr_Size(t *testing.T) {
|
||||
assert.EqualValues(t, NetHdrSize, unsafe.Sizeof(NetHdr{}))
|
||||
}
|
||||
|
||||
func TestNetHdr_Encoding(t *testing.T) {
|
||||
vnethdr := NetHdr{
|
||||
Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
|
||||
GSOType: unix.VIRTIO_NET_HDR_GSO_UDP_L4,
|
||||
HdrLen: 42,
|
||||
GSOSize: 1472,
|
||||
CsumStart: 34,
|
||||
CsumOffset: 6,
|
||||
NumBuffers: 16,
|
||||
}
|
||||
|
||||
buf := make([]byte, NetHdrSize)
|
||||
require.NoError(t, vnethdr.Encode(buf))
|
||||
|
||||
assert.Equal(t, []byte{
|
||||
0x01, 0x05,
|
||||
0x2a, 0x00,
|
||||
0xc0, 0x05,
|
||||
0x22, 0x00,
|
||||
0x06, 0x00,
|
||||
0x10, 0x00,
|
||||
}, buf)
|
||||
|
||||
var decoded NetHdr
|
||||
require.NoError(t, decoded.Decode(buf))
|
||||
|
||||
assert.Equal(t, vnethdr, decoded)
|
||||
}
|
||||
Reference in New Issue
Block a user