github.com/noisysockets/netstack@v0.6.0/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 "context" 19 "fmt" 20 "time" 21 22 "github.com/noisysockets/netstack/pkg/tcpip" 23 "github.com/noisysockets/netstack/pkg/tcpip/stack" 24 "github.com/noisysockets/netstack/pkg/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(_ context.Context, nsec int64) { 34 p.receivedAt = time.Unix(0, nsec) 35 } 36 37 // afterLoad is invoked by stateify. 38 func (e *endpoint) afterLoad(ctx context.Context) { 39 stack.RestoreStackFromContext(ctx).RegisterRestoredEndpoint(e) 40 } 41 42 // beforeSave is invoked by stateify. 43 func (e *endpoint) beforeSave() { 44 e.freeze() 45 e.stack.RegisterResumableEndpoint(e) 46 } 47 48 // Restore implements tcpip.RestoredEndpoint.Restore. 49 func (e *endpoint) Restore(s *stack.Stack) { 50 e.thaw() 51 52 e.mu.Lock() 53 defer e.mu.Unlock() 54 55 e.net.Resume(s) 56 57 e.stack = s 58 e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits) 59 60 switch state := e.net.State(); state { 61 case transport.DatagramEndpointStateInitial, transport.DatagramEndpointStateClosed: 62 case transport.DatagramEndpointStateBound, transport.DatagramEndpointStateConnected: 63 // Our saved state had a port, but we don't actually have a 64 // reservation. We need to remove the port from our state, but still 65 // pass it to the reservation machinery. 66 var err tcpip.Error 67 id := e.net.Info().ID 68 id.LocalPort = e.localPort 69 id.RemotePort = e.remotePort 70 id, e.boundBindToDevice, err = e.registerWithStack(e.effectiveNetProtos, id) 71 if err != nil { 72 panic(err) 73 } 74 e.localPort = id.LocalPort 75 e.remotePort = id.RemotePort 76 default: 77 panic(fmt.Sprintf("unhandled state = %s", state)) 78 } 79 } 80 81 // Resume implements tcpip.ResumableEndpoint.Resume. 82 func (e *endpoint) Resume() { 83 e.thaw() 84 }