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