github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/transport/tcp/segment_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 tcp 16 17 import ( 18 "github.com/SagerNet/gvisor/pkg/tcpip/buffer" 19 ) 20 21 // saveData is invoked by stateify. 22 func (s *segment) saveData() buffer.VectorisedView { 23 // We cannot save s.data directly as s.data.views may alias to s.views, 24 // which is not allowed by state framework (in-struct pointer). 25 vs := make([]buffer.View, len(s.data.Views())) 26 for i, v := range s.data.Views() { 27 vs[i] = v 28 } 29 return buffer.NewVectorisedView(s.data.Size(), vs) 30 } 31 32 // loadData is invoked by stateify. 33 func (s *segment) loadData(data buffer.VectorisedView) { 34 // NOTE: We cannot do the s.data = data.Clone(s.views[:]) optimization 35 // here because data.views is not guaranteed to be loaded by now. Plus, 36 // data.views will be allocated anyway so there really is little point 37 // of utilizing s.views for data.views. 38 s.data = data 39 } 40 41 // saveOptions is invoked by stateify. 42 func (s *segment) saveOptions() []byte { 43 // We cannot save s.options directly as it may point to s.data's trimmed 44 // tail, which is not allowed by state framework (in-struct pointer). 45 b := make([]byte, 0, cap(s.options)) 46 return append(b, s.options...) 47 } 48 49 // loadOptions is invoked by stateify. 50 func (s *segment) loadOptions(options []byte) { 51 // NOTE: We cannot point s.options back into s.data's trimmed tail. But 52 // it is OK as they do not need to aliased. Plus, options is already 53 // allocated so there is no cost here. 54 s.options = options 55 }