github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/tcpip/transport/raw/endpoint_state.go (about)

     1  // Copyright 2018 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 raw
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/MerlinKodo/gvisor/pkg/tcpip"
    22  	"github.com/MerlinKodo/gvisor/pkg/tcpip/stack"
    23  )
    24  
    25  // saveReceivedAt is invoked by stateify.
    26  func (p *rawPacket) saveReceivedAt() int64 {
    27  	return p.receivedAt.UnixNano()
    28  }
    29  
    30  // loadReceivedAt is invoked by stateify.
    31  func (p *rawPacket) loadReceivedAt(nsec int64) {
    32  	p.receivedAt = time.Unix(0, nsec)
    33  }
    34  
    35  // afterLoad is invoked by stateify.
    36  func (e *endpoint) afterLoad() {
    37  	stack.StackFromEnv.RegisterRestoredEndpoint(e)
    38  }
    39  
    40  // beforeSave is invoked by stateify.
    41  func (e *endpoint) beforeSave() {
    42  	e.setReceiveDisabled(true)
    43  }
    44  
    45  // Resume implements tcpip.ResumableEndpoint.Resume.
    46  func (e *endpoint) Resume(s *stack.Stack) {
    47  	e.net.Resume(s)
    48  
    49  	e.setReceiveDisabled(false)
    50  	e.stack = s
    51  	e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits)
    52  
    53  	if e.associated {
    54  		netProto := e.net.NetProto()
    55  		if err := e.stack.RegisterRawTransportEndpoint(netProto, e.transProto, e); err != nil {
    56  			panic(fmt.Sprintf("e.stack.RegisterRawTransportEndpoint(%d, %d, _): %s", netProto, e.transProto, err))
    57  		}
    58  	}
    59  }