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