net_hdr.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package virtio
  2. import (
  3. "errors"
  4. "unsafe"
  5. "golang.org/x/sys/unix"
  6. )
  7. // Workaround to make Go doc links work.
  8. var _ unix.Errno
  9. // NetHdrSize is the number of bytes needed to store a [NetHdr] in memory.
  10. const NetHdrSize = 12
  11. // ErrNetHdrBufferTooSmall is returned when a buffer is too small to fit a
  12. // virtio_net_hdr.
  13. var ErrNetHdrBufferTooSmall = errors.New("the buffer is too small to fit a virtio_net_hdr")
  14. // NetHdr defines the virtio_net_hdr as described by the virtio specification.
  15. type NetHdr struct {
  16. // Flags that describe the packet.
  17. // Possible values are:
  18. // - [unix.VIRTIO_NET_HDR_F_NEEDS_CSUM]
  19. // - [unix.VIRTIO_NET_HDR_F_DATA_VALID]
  20. // - [unix.VIRTIO_NET_HDR_F_RSC_INFO]
  21. Flags uint8
  22. // GSOType contains the type of segmentation offload that should be used for
  23. // the packet.
  24. // Possible values are:
  25. // - [unix.VIRTIO_NET_HDR_GSO_NONE]
  26. // - [unix.VIRTIO_NET_HDR_GSO_TCPV4]
  27. // - [unix.VIRTIO_NET_HDR_GSO_UDP]
  28. // - [unix.VIRTIO_NET_HDR_GSO_TCPV6]
  29. // - [unix.VIRTIO_NET_HDR_GSO_UDP_L4]
  30. // - [unix.VIRTIO_NET_HDR_GSO_ECN]
  31. GSOType uint8
  32. // HdrLen contains the length of the headers that need to be replicated by
  33. // segmentation offloads. It's the number of bytes from the beginning of the
  34. // packet to the beginning of the transport payload.
  35. // Only used when [FeatureNetDriverHdrLen] is negotiated.
  36. HdrLen uint16
  37. // GSOSize contains the maximum size of each segmented packet beyond the
  38. // header (payload size). In case of TCP, this is the MSS.
  39. GSOSize uint16
  40. // CsumStart contains the offset within the packet from which on the
  41. // checksum should be computed.
  42. CsumStart uint16
  43. // CsumOffset specifies how many bytes after [NetHdr.CsumStart] the computed
  44. // 16-bit checksum should be inserted.
  45. CsumOffset uint16
  46. // NumBuffers contains the number of merged descriptor chains when
  47. // [FeatureNetMergeRXBuffers] is negotiated.
  48. // This field is only used for packets received by the driver and should be
  49. // zero for transmitted packets.
  50. NumBuffers uint16
  51. }
  52. // Decode decodes the [NetHdr] from the given byte slice. The slice must contain
  53. // at least [NetHdrSize] bytes.
  54. func (v *NetHdr) Decode(data []byte) error {
  55. if len(data) < NetHdrSize {
  56. return ErrNetHdrBufferTooSmall
  57. }
  58. copy(unsafe.Slice((*byte)(unsafe.Pointer(v)), NetHdrSize), data[:NetHdrSize])
  59. return nil
  60. }
  61. // Encode encodes the [NetHdr] into the given byte slice. The slice must have
  62. // room for at least [NetHdrSize] bytes.
  63. func (v *NetHdr) Encode(data []byte) error {
  64. if len(data) < NetHdrSize {
  65. return ErrNetHdrBufferTooSmall
  66. }
  67. copy(data[:NetHdrSize], unsafe.Slice((*byte)(unsafe.Pointer(v)), NetHdrSize))
  68. return nil
  69. }