header_linux.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //go:build linux && !android
  2. // +build linux,!android
  3. package virtio
  4. import "encoding/binary"
  5. // Size is the on-wire length of struct virtio_net_hdr the kernel
  6. // prepends/expects on a TUN opened with IFF_VNET_HDR (TUNSETVNETHDRSZ
  7. // not set).
  8. const Size = 10
  9. // Hdr is the Go view of the legacy virtio_net_hdr.
  10. type Hdr struct {
  11. Flags uint8
  12. GSOType uint8
  13. HdrLen uint16
  14. GSOSize uint16
  15. CsumStart uint16
  16. CsumOffset uint16
  17. }
  18. // Decode reads a virtio_net_hdr in host byte order (TUN default; we never
  19. // call TUNSETVNETLE so the kernel matches our endianness).
  20. func (h *Hdr) Decode(b []byte) {
  21. h.Flags = b[0]
  22. h.GSOType = b[1]
  23. h.HdrLen = binary.NativeEndian.Uint16(b[2:4])
  24. h.GSOSize = binary.NativeEndian.Uint16(b[4:6])
  25. h.CsumStart = binary.NativeEndian.Uint16(b[6:8])
  26. h.CsumOffset = binary.NativeEndian.Uint16(b[8:10])
  27. }
  28. // Encode is the inverse of Decode: writes the virtio_net_hdr fields into b
  29. // (must be at least Size bytes). Used to emit a TSO superpacket on egress.
  30. func (h *Hdr) Encode(b []byte) {
  31. b[0] = h.Flags
  32. b[1] = h.GSOType
  33. binary.NativeEndian.PutUint16(b[2:4], h.HdrLen)
  34. binary.NativeEndian.PutUint16(b[4:6], h.GSOSize)
  35. binary.NativeEndian.PutUint16(b[6:8], h.CsumStart)
  36. binary.NativeEndian.PutUint16(b[8:10], h.CsumOffset)
  37. }