github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/tcpip/checksum/checksum_noasm_unsafe.go (about)

     1  // Copyright 2023 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  //go:build !amd64
    16  // +build !amd64
    17  
    18  package checksum
    19  
    20  import (
    21  	"math"
    22  	"math/bits"
    23  	"unsafe"
    24  )
    25  
    26  // Note: odd indicates whether initial is a partial checksum over an odd number
    27  // of bytes.
    28  func calculateChecksum(buf []byte, odd bool, initial uint16) (uint16, bool) {
    29  	// Note: we can probably remove unrolledCalculateChecksum altogether,
    30  	// but I don't have any 32 bit machines to benchmark on.
    31  	if bits.UintSize != 64 {
    32  		return unrolledCalculateChecksum(buf, odd, initial)
    33  	}
    34  
    35  	// Utilize byte order independence and parallel summation as
    36  	// described in RFC 1071 1.2.
    37  
    38  	// It doesn't matter what endianness we use, only that it's
    39  	// consistent throughout the calculation. See RFC 1071 1.2.B.
    40  	acc := uint(((initial & 0xff00) >> 8) | ((initial & 0x00ff) << 8))
    41  
    42  	// Account for initial having been calculated over an odd number of
    43  	// bytes.
    44  	if odd {
    45  		acc += uint(buf[0]) << 8
    46  		buf = buf[1:]
    47  	}
    48  
    49  	// See whether we're checksumming an odd number of bytes. If
    50  	// so, the final byte is a big endian most significant byte.
    51  	odd = len(buf)%2 != 0
    52  	if odd {
    53  		acc += uint(buf[len(buf)-1])
    54  		buf = buf[:len(buf)-1]
    55  	}
    56  
    57  	// Compute the checksum 8 bytes at a time.
    58  	var carry uint
    59  	for len(buf) >= 8 {
    60  		acc, carry = bits.Add(acc, *(*uint)(unsafe.Pointer(&buf[0])), carry)
    61  		buf = buf[8:]
    62  	}
    63  
    64  	// Compute the remainder 2 bytes at a time. We are guaranteed that
    65  	// len(buf) is even due to the above handling of odd-length buffers.
    66  	for len(buf) > 0 {
    67  		acc, carry = bits.Add(acc, uint(*(*uint16)(unsafe.Pointer(&buf[0]))), carry)
    68  		buf = buf[2:]
    69  	}
    70  	acc += carry
    71  
    72  	// Fold the checksum into 16 bits.
    73  	for acc > math.MaxUint16 {
    74  		acc = (acc & 0xffff) + acc>>16
    75  	}
    76  
    77  	// Swap the byte order before returning.
    78  	acc = ((acc & 0xff00) >> 8) | ((acc & 0x00ff) << 8)
    79  	return uint16(acc), odd
    80  }