From bc76b1524771863e3665744aae270e4c57ee3954 Mon Sep 17 00:00:00 2001 From: Kevin Krakauer Date: Tue, 6 Jun 2023 13:11:39 -0700 Subject: [PATCH] netstack: move test-only code into test It doesn't need to live in the regular pacakge. PiperOrigin-RevId: 538271262 --- pkg/tcpip/checksum/checksum.go | 33 ----------------------------- pkg/tcpip/checksum/checksum_test.go | 33 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/pkg/tcpip/checksum/checksum.go b/pkg/tcpip/checksum/checksum.go index d2e019151..7b49ecd86 100644 --- a/pkg/tcpip/checksum/checksum.go +++ b/pkg/tcpip/checksum/checksum.go @@ -30,28 +30,6 @@ func Put(b []byte, xsum uint16) { binary.BigEndian.PutUint16(b, xsum) } -func calculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bool) { - v := initial - - if odd { - v += uint32(buf[0]) - buf = buf[1:] - } - - l := len(buf) - odd = l&1 != 0 - if odd { - l-- - v += uint32(buf[l]) << 8 - } - - for i := 0; i < l; i += 2 { - v += (uint32(buf[i]) << 8) + uint32(buf[i+1]) - } - - return Combine(uint16(v), uint16(v>>16)), odd -} - func unrolledCalculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bool) { v := initial @@ -167,17 +145,6 @@ func unrolledCalculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bo return Combine(uint16(v), uint16(v>>16)), odd } -// Old calculates the checksum (as defined in RFC 1071) of the bytes in -// the given byte array. This function uses a non-optimized implementation. Its -// only retained for reference and to use as a benchmark/test. Most code should -// use the header.Checksum function. -// -// The initial checksum must have been computed on an even number of bytes. -func Old(buf []byte, initial uint16) uint16 { - s, _ := calculateChecksum(buf, false, uint32(initial)) - return s -} - // Checksum calculates the checksum (as defined in RFC 1071) of the bytes in the // given byte array. This function uses an optimized unrolled version of the // checksum algorithm. diff --git a/pkg/tcpip/checksum/checksum_test.go b/pkg/tcpip/checksum/checksum_test.go index 4761f6e93..d462bb11c 100644 --- a/pkg/tcpip/checksum/checksum_test.go +++ b/pkg/tcpip/checksum/checksum_test.go @@ -153,3 +153,36 @@ func BenchmarkChecksum(b *testing.B) { } } } + +// Old calculates the checksum (as defined in RFC 1071) of the bytes in +// the given byte array. This function uses a non-optimized implementation. Its +// only retained for reference and to use as a benchmark/test. Most code should +// use the header.Checksum function. +// +// The initial checksum must have been computed on an even number of bytes. +func Old(buf []byte, initial uint16) uint16 { + s, _ := calculateChecksum(buf, false, uint32(initial)) + return s +} + +func calculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bool) { + v := initial + + if odd { + v += uint32(buf[0]) + buf = buf[1:] + } + + l := len(buf) + odd = l&1 != 0 + if odd { + l-- + v += uint32(buf[l]) << 8 + } + + for i := 0; i < l; i += 2 { + v += (uint32(buf[i]) << 8) + uint32(buf[i+1]) + } + + return Combine(uint16(v), uint16(v>>16)), odd +}