inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/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  	"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 *rawPacket) saveReceivedAt() int64 {
    28  	return p.receivedAt.UnixNano()
    29  }
    30  
    31  // loadReceivedAt is invoked by stateify.
    32  func (p *rawPacket) loadReceivedAt(nsec int64) {
    33  	p.receivedAt = time.Unix(0, nsec)
    34  }
    35  
    36  // saveData saves rawPacket.data field.
    37  func (p *rawPacket) saveData() buffer.VectorisedView {
    38  	// We cannot save p.data directly as p.data.views may alias to p.views,
    39  	// which is not allowed by state framework (in-struct pointer).
    40  	return p.data.Clone(nil)
    41  }
    42  
    43  // loadData loads rawPacket.data field.
    44  func (p *rawPacket) loadData(data buffer.VectorisedView) {
    45  	// NOTE: We cannot do the p.data = data.Clone(p.views[:]) optimization
    46  	// here because data.views is not guaranteed to be loaded by now. Plus,
    47  	// data.views will be allocated anyway so there really is little point
    48  	// of utilizing p.views for data.views.
    49  	p.data = data
    50  }
    51  
    52  // afterLoad is invoked by stateify.
    53  func (e *endpoint) afterLoad() {
    54  	stack.StackFromEnv.RegisterRestoredEndpoint(e)
    55  }
    56  
    57  // beforeSave is invoked by stateify.
    58  func (e *endpoint) beforeSave() {
    59  	e.freeze()
    60  }
    61  
    62  // Resume implements tcpip.ResumableEndpoint.Resume.
    63  func (e *endpoint) Resume(s *stack.Stack) {
    64  	e.net.Resume(s)
    65  
    66  	e.thaw()
    67  	e.stack = s
    68  	e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits)
    69  
    70  	if e.associated {
    71  		netProto := e.net.NetProto()
    72  		if err := e.stack.RegisterRawTransportEndpoint(netProto, e.transProto, e); err != nil {
    73  			panic(fmt.Sprintf("e.stack.RegisterRawTransportEndpoint(%d, %d, _): %s", netProto, e.transProto, err))
    74  		}
    75  	}
    76  }