-
Notifications
You must be signed in to change notification settings - Fork 153
/
framebuf.go
187 lines (172 loc) · 5.35 KB
/
framebuf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2017 ETH Zurich
// Copyright 2019 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dataplane
import (
"context"
"encoding/binary"
"fmt"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/private/ringbuf"
)
const (
// frameBufCap is the size of a preallocated frame buffer.
frameBufCap = 65535
// freeFramesCap is the number of preallocated Framebuf objects.
freeFramesCap = 1024
)
var (
// Cache of the frame buffers free to be used.
freeFrames *ringbuf.Ring
)
func newFrameBufs(frames ringbuf.EntryList) int {
if freeFrames == nil {
initFreeFrames()
}
n, _ := freeFrames.Read(frames, true)
return n
}
// frameBuf is a struct used to reassemble encapsulated packets spread over
// multiple SIG frames. It contains the raw bytes and metadata needed for reassembly.
type frameBuf struct {
// Session Id of the frame.
sessId uint8
// Sequence number of the frame.
seqNr uint64
// Index of the frame.
index int
// Total length of the frame (including 16-byte header).
frameLen int
// Start of the fragment that starts a new packet. 0 means that there
// is no such fragment. This points to the start of the header of the packet,
// i.e., the 2-byte packet len preceding the packet header is not included.
frag0Start int
// Whether fragment 0 has been processed already when reassembling.
frag0Processed bool
// Whether fragment N has been processed already when reassembling. Fragment N
// denotes the fragment that completes a packet. Note that with the way packets
// are in encapsulated, such a fragment will always be at the start of a frame
// (if there is one).
fragNProcessed bool
// Whether all packets completely contained in the frame have been processed.
completePktsProcessed bool
// The packet len of the packet that starts at fragment0. Has no meaning
// if there is no such fragment.
pktLen int
// The raw bytes buffer for the frame.
raw []byte
// The sender object for the frame.
snd ingressSender
}
func newFrameBuf() *frameBuf {
buf := &frameBuf{raw: make([]byte, frameBufCap)}
buf.Reset()
return buf
}
// Reset resets the metadata of a FrameBuf.
func (fb *frameBuf) Reset() {
fb.sessId = 0
fb.seqNr = 0xffffffffffffffff
fb.index = -1
fb.frameLen = 0
fb.frag0Start = 0
fb.frag0Processed = false
fb.fragNProcessed = false
fb.completePktsProcessed = false
fb.pktLen = 0
fb.snd = nil
}
// Release reset the FrameBuf and releases it back to the ringbuf (if set).
func (fb *frameBuf) Release() {
fb.Reset()
if freeFrames == nil {
initFreeFrames()
}
freeFrames.Write(ringbuf.EntryList{fb}, true)
}
// ProcessCompletePkts write all complete packets in the frame to the wire and
// sets the correct metadata in case there is a fragment at the end of the frame.
func (fb *frameBuf) ProcessCompletePkts(ctx context.Context) {
logger := log.FromCtx(ctx)
if fb.completePktsProcessed || fb.index == 0xffff {
fb.completePktsProcessed = true
return
}
offset := fb.index + sigHdrSize
var pktLen int
for offset < fb.frameLen {
// Make sure that the frame contains the entire IPv4 or IPv6 header.
// Get the payload length.
ipVersion := fb.raw[offset] >> 4
switch ipVersion {
case 4:
if fb.frameLen-offset < 20 {
fb.completePktsProcessed = true
return
}
pktLen = int(binary.BigEndian.Uint16(fb.raw[offset+2 : offset+4]))
if pktLen < 20 {
fb.completePktsProcessed = true
return
}
case 6:
if fb.frameLen-offset < 40 {
fb.completePktsProcessed = true
return
}
pktLen = int(binary.BigEndian.Uint16(fb.raw[offset+4 : offset+6]))
pktLen += 40
default:
fb.completePktsProcessed = true
return
}
rawPkt := fb.raw[offset:fb.frameLen]
if len(rawPkt) < pktLen {
break
}
// We got everything for the packet. Write it out to the wire.
if err := fb.snd.send(rawPkt[:pktLen]); err != nil {
logger.Error("Unable to send packet", "err", err)
}
offset += pktLen
}
if offset < fb.frameLen {
// There is an incomplete packet at the end of the frame.
fb.frag0Start = offset
fb.pktLen = pktLen
}
fb.completePktsProcessed = true
fb.frag0Processed = fb.frag0Start == 0
}
// Processed returns true if all fragments in the frame have been processed,
func (fb *frameBuf) Processed() bool {
return (fb.completePktsProcessed && fb.fragNProcessed &&
(fb.frag0Start == 0 || fb.frag0Processed))
}
// SetProcessed marks a frame as being processed.
func (fb *frameBuf) SetProcessed() {
fb.completePktsProcessed = true
fb.fragNProcessed = true
fb.frag0Processed = true
}
func (fb *frameBuf) String() string {
return fmt.Sprintf("SeqNr: %d Index: %d Len: %d frag0Start: %d processed: (%t, %t, %t)",
fb.seqNr, fb.index, fb.frameLen, fb.frag0Start, fb.fragNProcessed, fb.frag0Processed,
fb.completePktsProcessed)
}
func initFreeFrames() {
freeFrames = ringbuf.New(freeFramesCap, func() interface{} {
return newFrameBuf()
}, "ingress_free")
}