gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"gvisor.dev/gvisor/pkg/tcpip"
    23  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    24  )
    25  
    26  // saveReceivedAt is invoked by stateify.
    27  func (p *rawPacket) saveReceivedAt() int64 {
    28  	return p.receivedAt.UnixNano()
    29  }
    30  
    31  // loadReceivedAt is invoked by stateify.
    32  func (p *rawPacket) loadReceivedAt(_ context.Context, nsec int64) {
    33  	p.receivedAt = time.Unix(0, nsec)
    34  }
    35  
    36  // afterLoad is invoked by stateify.
    37  func (e *endpoint) afterLoad(ctx context.Context) {
    38  	stack.RestoreStackFromContext(ctx).RegisterRestoredEndpoint(e)
    39  }
    40  
    41  // beforeSave is invoked by stateify.
    42  func (e *endpoint) beforeSave() {
    43  	e.setReceiveDisabled(true)
    44  	e.stack.RegisterResumableEndpoint(e)
    45  }
    46  
    47  // Restore implements tcpip.RestoredEndpoint.Restore.
    48  func (e *endpoint) Restore(s *stack.Stack) {
    49  	e.net.Resume(s)
    50  
    51  	e.setReceiveDisabled(false)
    52  	e.stack = s
    53  	e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits)
    54  
    55  	if e.associated {
    56  		netProto := e.net.NetProto()
    57  		if err := e.stack.RegisterRawTransportEndpoint(netProto, e.transProto, e); err != nil {
    58  			panic(fmt.Sprintf("e.stack.RegisterRawTransportEndpoint(%d, %d, _): %s", netProto, e.transProto, err))
    59  		}
    60  	}
    61  }
    62  
    63  // Resume implements tcpip.ResumableEndpoint.Resume.
    64  func (e *endpoint) Resume() {
    65  	e.setReceiveDisabled(false)
    66  }