github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/tcpip/header/virtionet.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package header
    16  
    17  import "encoding/binary"
    18  
    19  // These constants are declared in linux/virtio_net.h.
    20  const (
    21  	_VIRTIO_NET_HDR_F_NEEDS_CSUM = 1
    22  	_VIRTIO_NET_HDR_GSO_NONE     = 0
    23  	_VIRTIO_NET_HDR_GSO_TCPV4    = 1
    24  	_VIRTIO_NET_HDR_GSO_TCPV6    = 4
    25  )
    26  
    27  const (
    28  	// VirtioNetHeaderSize is the size of VirtioNetHeader in bytes.
    29  	VirtioNetHeaderSize = 10
    30  )
    31  
    32  // Offsets for fields in the virtio net header.
    33  const (
    34  	flags      = 0
    35  	gsoType    = 1
    36  	hdrLen     = 2
    37  	gsoSize    = 4
    38  	csumStart  = 6
    39  	csumOffset = 8
    40  )
    41  
    42  // VirtioNetHeaderFields is the Go equivalent of the struct declared in
    43  // linux/virtio_net.h.
    44  type VirtioNetHeaderFields struct {
    45  	Flags      uint8
    46  	GSOType    uint8
    47  	HdrLen     uint16
    48  	GSOSize    uint16
    49  	CSumStart  uint16
    50  	CSumOffset uint16
    51  }
    52  
    53  // VirtioNetHeader represents a virtio net header stored in a byte array.
    54  type VirtioNetHeader []byte
    55  
    56  // Flags returns the "flags" field of the virtio net header.
    57  func (v VirtioNetHeader) Flags() uint8 {
    58  	return uint8(v[flags])
    59  }
    60  
    61  // GSOType returns the "gsoType" field of the virtio net header.
    62  func (v VirtioNetHeader) GSOType() uint8 {
    63  	return uint8(v[gsoType])
    64  }
    65  
    66  // HdrLen returns the "hdrLen" field of the virtio net header.
    67  func (v VirtioNetHeader) HdrLen() uint16 {
    68  	return binary.BigEndian.Uint16(v[hdrLen:])
    69  }
    70  
    71  // GSOSize returns the "gsoSize" field of the virtio net header.
    72  func (v VirtioNetHeader) GSOSize() uint16 {
    73  	return binary.BigEndian.Uint16(v[gsoSize:])
    74  }
    75  
    76  // CSumStart returns the "csumStart" field of the virtio net header.
    77  func (v VirtioNetHeader) CSumStart() uint16 {
    78  	return binary.BigEndian.Uint16(v[csumStart:])
    79  }
    80  
    81  // CSumOffset returns the "csumOffset" field of the virtio net header.
    82  func (v VirtioNetHeader) CSumOffset() uint16 {
    83  	return binary.BigEndian.Uint16(v[csumOffset:])
    84  }
    85  
    86  // Encode encodes all the fields of the virtio net header.
    87  func (v VirtioNetHeader) Encode(f *VirtioNetHeaderFields) {
    88  	v[flags] = uint8(f.Flags)
    89  	v[gsoType] = uint8(f.GSOType)
    90  	binary.BigEndian.PutUint16(v[hdrLen:], f.HdrLen)
    91  	binary.BigEndian.PutUint16(v[gsoSize:], f.GSOSize)
    92  	binary.BigEndian.PutUint16(v[csumStart:], f.CSumStart)
    93  	binary.BigEndian.PutUint16(v[csumOffset:], f.CSumOffset)
    94  }