-
Notifications
You must be signed in to change notification settings - Fork 1
/
transmission.go
341 lines (311 loc) · 9.27 KB
/
transmission.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// SPDX-License-Identifier: Apache-2.0
package nbd
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"unsafe"
"github.com/digitalocean/go-nbd/internal/nbdproto"
)
// TransmissionFlags describe what capabilities the server has
// advertised for this connection.
type TransmissionFlags uint16
const (
TransmissionFlagHasFlags TransmissionFlags = 1 << 0
TransmissionFlagReadOnly TransmissionFlags = 1 << 1
TransmissionFlagSendFlush TransmissionFlags = 1 << 2
TransmissionFlagSendFUA TransmissionFlags = 1 << 3
TransmissionFlagRotational TransmissionFlags = 1 << 4
TransmissionFlagTrim TransmissionFlags = 1 << 5
TransmissionFlagSendWriteZeroes TransmissionFlags = 1 << 6
TransmissionFlagSendDF TransmissionFlags = 1 << 7
TransmissionFlagCanMultiConn TransmissionFlags = 1 << 8
TransmissionFlagSendResize TransmissionFlags = 1 << 9
TransmissionFlagSendCache TransmissionFlags = 1 << 10
TransmissionFlagSendFastZero TransmissionFlags = 1 << 11
TransmissionFlagBlockStatusPayload TransmissionFlags = 1 << 12
)
// CommandFlags allow customizing transmission request behavior.
type CommandFlags uint16
const (
CommandFlagFUA CommandFlags = 1 << 0
CommandFlagNoHole CommandFlags = 1 << 1
CommandFlagDF CommandFlags = 1 << 2
CommandFlagReqOne CommandFlags = 1 << 3
CommandFlagFastZero CommandFlags = 1 << 4
CommandFlagPayloadLen CommandFlags = 1 << 5
)
// ErrStreamClosing indicates the nbd client received data that it
// did not expect or otherwise know how to handle and is therefore
// abandoning the connection.
var ErrStreamClosing = errors.New("nbd stream closing")
// Read is an "enum-like" type. If Data is not nil, then it contains
// data from the corresponding Read call. If Hole is not nil, then
// that extent of the read is a hole. It is a bug if both are nil
// or both are non-nil, please report it.
type Read struct {
Data *ReadData
Hole *ReadHole
}
// ReadData contains data from a call to Read for a specified extent.
// The extent is [ReadData.Offset, len(ReadData.Data)).
type ReadData struct {
Offset uint64
Data []byte
}
func (r *ReadData) UnmarshalNBDReply(data []byte) error {
buf := bytes.NewBuffer(data)
if err := binary.Read(buf, binary.BigEndian, &r.Offset); err != nil {
return fmt.Errorf("read offset: %w", err)
}
r.Data = buf.Bytes()
return nil
}
// ReadHole indicates this extent is a hole.
type ReadHole struct {
Offset uint64
Length uint32
}
func (r *ReadHole) UnmarshalNBDReply(data []byte) error {
buf := bytes.NewBuffer(data)
if err := binary.Read(buf, binary.BigEndian, &r.Offset); err != nil {
return fmt.Errorf("read offset: %w", err)
}
if err := binary.Read(buf, binary.BigEndian, &r.Length); err != nil {
return fmt.Errorf("read length: %w", err)
}
return nil
}
// BlockStatus describes the given meta context. See package "nbdmeta"
// for helper types to interpret the BlockStatusDescriptors' status flags.
type BlockStatus struct {
ID uint32
Descriptors []BlockStatusDescriptor
}
func (b *BlockStatus) UnmarshalNBDReply(data []byte) error {
buf := bytes.NewReader(data)
if err := binary.Read(buf, binary.BigEndian, &b.ID); err != nil {
return fmt.Errorf("block status metadata ID: %w", err)
}
for {
var bd BlockStatusDescriptor
err := binary.Read(buf, binary.BigEndian, &bd.Length)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return fmt.Errorf("block status: read length: %w", err)
}
err = binary.Read(buf, binary.BigEndian, &bd.Status)
if err != nil {
return fmt.Errorf("block status: read status: %w", err)
}
b.Descriptors = append(b.Descriptors, bd)
}
return nil
}
// BlockStatusDescriptor describes a run of blocks, depending
// on the meta context. See package nbdmeta for types that
// help interpret the Status field.
type BlockStatusDescriptor struct {
Length uint32
Status uint32
}
type reply struct {
simple *nbdproto.SimpleReplyHeader
structured *nbdproto.StructuredReplyHeader
buf []byte
err error
}
type stream struct {
replies chan reply
length uint32
}
func (c *Conn) addStream(cookie uint64, length uint32) (replies chan reply, drain func()) {
replies = make(chan reply, 1)
drain = func() {
for range replies {
}
}
c.mu.Lock()
defer c.mu.Unlock()
c.streams[cookie] = stream{
replies: replies,
length: length,
}
return replies, drain
}
func (c *Conn) demuxReplies() (err error) {
defer func() {
c.mu.Lock()
defer c.mu.Unlock()
for _, stream := range c.streams {
stream.replies <- reply{err: errors.Join(ErrStreamClosing, err)}
close(stream.replies)
}
c.streams = nil
c.setState(connectionStateError)
}()
for {
var magic uint32
if err := binary.Read(c.conn, binary.BigEndian, &magic); err != nil {
return fmt.Errorf("route replies: read magic: %w", err)
}
if magic != nbdproto.NBD_SIMPLE_REPLY_MAGIC && magic != nbdproto.NBD_STRUCTURED_REPLY_MAGIC {
return fmt.Errorf("route replies: got invalid magic %x", magic)
}
if magic == nbdproto.NBD_SIMPLE_REPLY_MAGIC {
hdr := nbdproto.SimpleReplyHeader{
Magic: magic,
}
if err := binary.Read(c.conn, binary.BigEndian, &hdr.Error); err != nil {
return fmt.Errorf("route replies: simple: read error: %w", err)
}
if err := binary.Read(c.conn, binary.BigEndian, &hdr.Cookie); err != nil {
return fmt.Errorf("route replies: simple: read cookie: %w", err)
}
var length uint32
func() {
// Avoid reading the expected length from the request
// if this is a simple error. This will leave length
// above to 0 so that we read 0 bytes below.
if hdr.Error != 0 {
return
}
c.mu.Lock()
defer c.mu.Unlock()
stream, ok := c.streams[hdr.Cookie]
if !ok {
return
}
length = stream.length
}()
buf := make([]byte, length)
if _, err := io.ReadFull(c.conn, buf); err != nil {
return fmt.Errorf("route replies: simple: read payload: %w", err)
}
func() {
c.mu.Lock()
defer c.mu.Unlock()
stream, ok := c.streams[hdr.Cookie]
if !ok {
return
}
var err error
if hdr.Error != 0 {
err = &TransmissionError{Code: TransmissionErrorCode(hdr.Error)}
}
r := reply{
simple: &hdr,
buf: buf,
err: err,
}
stream.replies <- r
close(stream.replies)
delete(c.streams, hdr.Cookie)
}()
continue
}
hdr := nbdproto.StructuredReplyHeader{
Magic: nbdproto.NBD_STRUCTURED_REPLY_MAGIC,
}
if err := binary.Read(c.conn, binary.BigEndian, &hdr.Flags); err != nil {
return fmt.Errorf("route replies: structured: read flags: %w", err)
}
if err := binary.Read(c.conn, binary.BigEndian, &hdr.Type); err != nil {
return fmt.Errorf("route replies: structured: read type: %w", err)
}
if err := binary.Read(c.conn, binary.BigEndian, &hdr.Cookie); err != nil {
return fmt.Errorf("route replies: structured: read cookie: %w", err)
}
if err := binary.Read(c.conn, binary.BigEndian, &hdr.Length); err != nil {
return fmt.Errorf("route replies: structured: read length: %w", err)
}
buf := make([]byte, hdr.Length)
if _, err := io.ReadFull(c.conn, buf); err != nil {
return fmt.Errorf("route replies: structured: read payload: %w", err)
}
var replyError error
if isTXError(hdr.Type) {
b := bytes.NewBuffer(buf)
var code uint32
if err := binary.Read(b, binary.BigEndian, &code); err != nil {
return fmt.Errorf("route replies: structured: read error code: %w", err)
}
var length uint16
if err := binary.Read(b, binary.BigEndian, &length); err != nil {
return fmt.Errorf("route replies: structured: read message length: %w", err)
}
var offset uint64
if hdr.Type == nbdproto.REPLY_TYPE_ERROR_OFFSET {
if err := binary.Read(b, binary.BigEndian, &offset); err != nil {
return fmt.Errorf("route replies: structured: read offset: %w", err)
}
}
m := b.String()
replyError = &TransmissionError{
Code: TransmissionErrorCode(code),
Message: NullErrorMessage{
Value: m,
Valid: len(m) > 0,
},
Offset: NullOffset{
Value: offset,
Valid: hdr.Type == nbdproto.REPLY_TYPE_ERROR_OFFSET,
},
}
}
func() {
c.mu.Lock()
defer c.mu.Unlock()
stream, ok := c.streams[hdr.Cookie]
if !ok {
return
}
r := reply{
structured: &hdr,
buf: buf,
err: replyError,
}
if replyError != nil {
r.buf = nil
}
stream.replies <- r
if hdr.Flags&nbdproto.REPLY_FLAG_DONE == 0 {
return
}
close(stream.replies)
delete(c.streams, hdr.Cookie)
}()
}
}
func requestTransmit(server io.Writer, cflags uint16, ty uint16, cookie uint64, offset uint64, length uint32, payload []byte) error {
header := nbdproto.RequestHeader{
Magic: nbdproto.REQUEST_MAGIC,
Flags: cflags,
Type: ty,
Cookie: cookie,
Offset: offset,
Length: length,
}
if l := len(payload); l > 0 {
header.Length = uint32(l)
}
psize := int(unsafe.Sizeof(header)) + len(payload)
packet := bytes.NewBuffer(make([]byte, 0, psize))
if err := binary.Write(packet, binary.BigEndian, header); err != nil {
return err
}
if err := binary.Write(packet, binary.BigEndian, payload); err != nil {
return err
}
if _, err := io.Copy(server, packet); err != nil {
return err
}
return nil
}
func isTXError(type_ uint16) bool {
return type_&(1<<15) != 0
}