inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/tcpip/transport/packet/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 packet
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"inet.af/netstack/tcpip"
    22  	"inet.af/netstack/tcpip/buffer"
    23  	"inet.af/netstack/tcpip/stack"
    24  )
    25  
    26  // saveReceivedAt is invoked by stateify.
    27  func (p *packet) saveReceivedAt() int64 {
    28  	return p.receivedAt.UnixNano()
    29  }
    30  
    31  // loadReceivedAt is invoked by stateify.
    32  func (p *packet) loadReceivedAt(nsec int64) {
    33  	p.receivedAt = time.Unix(0, nsec)
    34  }
    35  
    36  // saveData saves packet.data field.
    37  func (p *packet) saveData() buffer.VectorisedView {
    38  	return p.data.Clone(nil)
    39  }
    40  
    41  // loadData loads packet.data field.
    42  func (p *packet) loadData(data buffer.VectorisedView) {
    43  	p.data = data
    44  }
    45  
    46  // beforeSave is invoked by stateify.
    47  func (ep *endpoint) beforeSave() {
    48  	ep.rcvMu.Lock()
    49  	defer ep.rcvMu.Unlock()
    50  	ep.rcvDisabled = true
    51  }
    52  
    53  // afterLoad is invoked by stateify.
    54  func (ep *endpoint) afterLoad() {
    55  	ep.mu.Lock()
    56  	defer ep.mu.Unlock()
    57  
    58  	ep.stack = stack.StackFromEnv
    59  	ep.ops.InitHandler(ep, ep.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits)
    60  
    61  	if err := ep.stack.RegisterPacketEndpoint(ep.boundNIC, ep.boundNetProto, ep); err != nil {
    62  		panic(fmt.Sprintf("RegisterPacketEndpoint(%d, %d, _): %s", ep.boundNIC, ep.boundNetProto, err))
    63  	}
    64  
    65  	ep.rcvMu.Lock()
    66  	ep.rcvDisabled = false
    67  	ep.rcvMu.Unlock()
    68  }