inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/tcpip/transport/udp/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 udp 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 *udpPacket) saveReceivedAt() int64 { 29 return p.receivedAt.UnixNano() 30 } 31 32 // loadReceivedAt is invoked by stateify. 33 func (p *udpPacket) loadReceivedAt(nsec int64) { 34 p.receivedAt = time.Unix(0, nsec) 35 } 36 37 // saveData saves udpPacket.data field. 38 func (p *udpPacket) saveData() buffer.VectorisedView { 39 return p.data.Clone(nil) 40 } 41 42 // loadData loads udpPacket.data field. 43 func (p *udpPacket) loadData(data buffer.VectorisedView) { 44 p.data = data 45 } 46 47 // afterLoad is invoked by stateify. 48 func (e *endpoint) afterLoad() { 49 stack.StackFromEnv.RegisterRestoredEndpoint(e) 50 } 51 52 // beforeSave is invoked by stateify. 53 func (e *endpoint) beforeSave() { 54 e.freeze() 55 } 56 57 // Resume implements tcpip.ResumableEndpoint.Resume. 58 func (e *endpoint) Resume(s *stack.Stack) { 59 e.thaw() 60 61 e.mu.Lock() 62 defer e.mu.Unlock() 63 64 e.net.Resume(s) 65 66 e.stack = s 67 e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits) 68 69 switch state := e.net.State(); state { 70 case transport.DatagramEndpointStateInitial, transport.DatagramEndpointStateClosed: 71 case transport.DatagramEndpointStateBound, transport.DatagramEndpointStateConnected: 72 // Our saved state had a port, but we don't actually have a 73 // reservation. We need to remove the port from our state, but still 74 // pass it to the reservation machinery. 75 var err tcpip.Error 76 id := e.net.Info().ID 77 id.LocalPort = e.localPort 78 id.RemotePort = e.remotePort 79 id, e.boundBindToDevice, err = e.registerWithStack(e.effectiveNetProtos, id) 80 if err != nil { 81 panic(err) 82 } 83 e.localPort = id.LocalPort 84 e.remotePort = id.RemotePort 85 default: 86 panic(fmt.Sprintf("unhandled state = %s", state)) 87 } 88 }