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 tcp contains internal type definitions that are not expected to be
16 // used by anyone else outside pkg/tcpip.
17 package tcp
18 19 import (
20 "time"
21 22 "gvisor.dev/gvisor/pkg/tcpip"
23 )
24 25 // TSOffset is an offset applied to the value of the TSVal field in the TCP
26 // Timestamp option.
27 //
28 // +stateify savable
29 type TSOffset struct {
30 milliseconds uint32
31 }
32 33 // NewTSOffset creates a new TSOffset from milliseconds.
34 func NewTSOffset(milliseconds uint32) TSOffset {
35 return TSOffset{
36 milliseconds: milliseconds,
37 }
38 }
39 40 // TSVal applies the offset to now and returns the timestamp in milliseconds.
41 func (offset TSOffset) TSVal(now tcpip.MonotonicTime) uint32 {
42 return uint32(now.Sub(tcpip.MonotonicTime{}).Milliseconds()) + offset.milliseconds
43 }
44 45 // Elapsed calculates the elapsed time given now and the echoed back timestamp.
46 func (offset TSOffset) Elapsed(now tcpip.MonotonicTime, tsEcr uint32) time.Duration {
47 return time.Duration(offset.TSVal(now)-tsEcr) * time.Millisecond
48 }
49