-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtypes.go
594 lines (515 loc) · 11.5 KB
/
types.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2018 Axel Wagner
//
// 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 nbd
import (
"errors"
"fmt"
"strconv"
)
const (
nbdMagic = 0x4e42444d41474943
optMagic = 0x49484156454F5054
repMagic = 0x0003e889045565a9
reqMagic = 0x25609513
simpleReplyMagic = 0x67446698
structuredReplyMagic = 0x668e33ef
flagFixedNewstyle = 1 << 0
flagNoZeroes = 1 << 1
flagDefaults = flagFixedNewstyle | flagNoZeroes
maxOptionLength = 4 << 10
)
type optionRequest interface {
encode(*encoder)
decode(*encoder, uint32) errno
code() uint32
}
func decodeOption(e *encoder) (uint32, interface{}, errno) {
magic := e.uint64()
if magic != optMagic {
e.check(fmt.Errorf("invalid option magic 0x%x", magic))
}
option := e.uint32()
length := e.uint32()
if length > maxOptionLength {
return option, nil, errTooBig
}
var o interface{ decode(*encoder, uint32) errno }
switch option {
case cOptExportName:
o = new(optExportName)
case cOptAbort:
o = new(optAbort)
case cOptList:
o = new(optList)
case cOptInfo:
o = &optInfo{done: false}
case cOptGo:
o = &optInfo{done: true}
}
if o == nil {
return option, nil, errUnsup
}
return option, o, o.decode(e, length)
}
const (
cOptExportName = 1
cOptAbort = 2
cOptList = 3
cOptStartTLS = 5
cOptInfo = 6
cOptGo = 7
cOptStructuredReply = 8
cOptListMetaContext = 9
cOptSetMetaContext = 10
)
type optExportName struct {
name string
}
func (o *optExportName) decode(e *encoder, l uint32) errno {
name := make([]byte, l)
e.read(name)
o.name = string(name)
return 0
}
type optAbort struct{}
func (o *optAbort) code() uint32 { return cOptAbort }
func (o *optAbort) encode(e *encoder) {}
func (o *optAbort) decode(e *encoder, l uint32) errno {
if l != 0 {
return errInvalid
}
return 0
}
type optList struct{}
func (o *optList) code() uint32 { return cOptList }
func (o *optList) decode(e *encoder, l uint32) errno {
if l != 0 {
return errInvalid
}
return 0
}
func (o *optList) encode(e *encoder) {}
type optInfo struct {
done bool
name string
reqs []uint16
}
func (o *optInfo) code() uint32 {
if o.done {
return cOptGo
}
return cOptInfo
}
func (o *optInfo) decode(e *encoder, l uint32) errno {
nlen := e.uint32()
if l < 6 || nlen > l-6 {
return errInvalid
}
name := make([]byte, nlen)
e.read(name)
o.name = string(name)
nreqs := e.uint16()
if (l-nlen-6)%2 != 0 || (l-nlen-6)/2 != uint32(nreqs) {
return errInvalid
}
for ; nreqs > 0; nreqs-- {
o.reqs = append(o.reqs, e.uint16())
}
return 0
}
func (o *optInfo) encode(e *encoder) {
e.writeUint32(uint32(len(o.name)))
e.writeString(o.name)
e.writeUint16(uint16(len(o.reqs)))
for _, r := range o.reqs {
e.writeUint16(r)
}
}
type errno uint32
const (
_ errno = (1 << 31) + iota
errUnsup
errPolicy
errInvalid
errPlatform
errTLSReqd
errUnknown
errShutdown
errBlockSizeReqd
errTooBig
)
func (e errno) String() string {
switch e {
case errUnsup:
return "ERR_UNSUP"
case errPolicy:
return "ERR_POLICY"
case errInvalid:
return "ERR_INVALID"
case errPlatform:
return "ERR_PLATFORM"
case errTLSReqd:
return "ERR_TLS_REQD"
case errUnknown:
return "ERR_UNKNOWN"
case errShutdown:
return "ERR_SHUTDOWN"
case errBlockSizeReqd:
return "ERR_BLOCK_SIZE_REQD"
case errTooBig:
return "ERR_TOO_BIG"
default:
return "0x" + strconv.FormatUint(uint64(e), 16)
}
}
type optionReply interface {
code() uint32
encode(*encoder)
decode(*encoder, uint32)
}
func encodeReply(e *encoder, option uint32, reply optionReply) {
e.writeUint64(repMagic)
e.writeUint32(option)
e.writeUint32(reply.code())
e.buf = []byte{}
var buf []byte
reply.encode(e)
buf, e.buf = e.buf, nil
e.writeUint32(uint32(len(buf)))
e.write(buf)
}
const (
cRepAck = 1
cRepServer = 2
cRepInfo = 3
)
type repAck struct{}
func (r *repAck) code() uint32 { return cRepAck }
func (r *repAck) encode(*encoder) {}
func (r *repAck) decode(e *encoder, l uint32) {
if l != 0 {
e.check(errors.New("invalid ack response"))
}
}
type repServer struct {
name string
details string
}
func (r *repServer) code() uint32 { return cRepServer }
func (r *repServer) encode(e *encoder) {
e.writeUint32(uint32(len(r.name)))
e.writeString(r.name)
e.writeString(r.details)
}
func (r *repServer) decode(e *encoder, l uint32) {
if l < 4 {
e.check(errors.New("invalid server response"))
}
length := e.uint32()
if length > l-4 {
e.check(errors.New("invalid server response"))
}
b := make([]byte, l-4)
e.read(b)
r.name = string(b[:length])
r.details = string(b[length:])
}
const (
cInfoExport = 0
cInfoName = 1
cInfoDescription = 2
cInfoBlockSize = 3
)
func decodeInfo(e *encoder, l uint32) optionReply {
if l < 2 {
e.check(errors.New("invalid length for info reply"))
}
code := e.uint16()
var rep optionReply
switch code {
case cInfoExport:
rep = new(infoExport)
case cInfoName:
rep = new(infoName)
case cInfoDescription:
rep = new(infoDescription)
case cInfoBlockSize:
rep = new(infoBlockSize)
default:
e.discard(l - 2)
return nil
}
rep.decode(e, l-2)
return rep
}
type infoExport struct {
size uint64
flags uint16
}
func (r *infoExport) code() uint32 { return cRepInfo }
func (r *infoExport) encode(e *encoder) {
e.writeUint16(cInfoExport)
e.writeUint64(r.size)
e.writeUint16(r.flags)
}
func (r *infoExport) decode(e *encoder, l uint32) {
if l != 10 {
e.check(errors.New("invalid length for info reply"))
}
r.size = e.uint64()
r.flags = e.uint16()
}
type infoName struct {
name string
}
func (r *infoName) code() uint32 { return cRepInfo }
func (r *infoName) encode(e *encoder) {
e.writeUint16(cInfoName)
e.writeString(r.name)
}
func (r *infoName) decode(e *encoder, l uint32) {
if l > (4 << 10) {
e.check(errors.New("name too large"))
}
b := make([]byte, l)
e.read(b)
r.name = string(b)
}
type infoDescription struct {
description string
}
func (r *infoDescription) code() uint32 { return cRepInfo }
func (r *infoDescription) encode(e *encoder) {
e.writeUint16(cInfoDescription)
e.writeString(r.description)
}
func (r *infoDescription) decode(e *encoder, l uint32) {
if l > (4 << 10) {
e.check(errors.New("description too large"))
}
b := make([]byte, l)
e.read(b)
r.description = string(b)
}
type infoBlockSize struct {
min uint32
preferred uint32
max uint32
}
func (r *infoBlockSize) code() uint32 { return cRepInfo }
func (r *infoBlockSize) encode(e *encoder) {
e.writeUint16(cInfoBlockSize)
e.writeUint32(r.min)
e.writeUint32(r.preferred)
e.writeUint32(r.max)
}
func (r *infoBlockSize) decode(e *encoder, l uint32) {
if l != 12 {
e.check(errors.New("invalid length for block size info"))
}
r.min = e.uint32()
r.preferred = e.uint32()
r.max = e.uint32()
}
type repError struct {
errno errno
msg string
}
func (r *repError) code() uint32 { return uint32(r.errno) }
func (r *repError) encode(e *encoder) {}
func (r *repError) decode(e *encoder, l uint32) {
if l > (4 << 20) {
e.check(errors.New("error string too larg"))
}
b := make([]byte, l)
e.read(b)
r.msg = string(b)
}
func (r *repError) Error() string {
if r.msg != "" {
return fmt.Sprintf("%s (%s)", r.msg, r.errno)
}
return r.errno.String()
}
const (
cmdRead = 0
cmdWrite = 1
cmdDisc = 2
cmdFlush = 3
cmdTrim = 4
cmdCache = 5
cmdWriteZeroes = 6
cmdBlockStatus = 7
cmdResize = 8
)
// Errno is an error code suitable to be sent over the wire. It mostly
// corresponds to syscall.Errno, though the constants in this package are
// specified and so are the only ones to be safe to be sent over the wire and
// understood by all NBD servers/clients.
type Errno uint32
// See https://manpages.debian.org/stretch/manpages-dev/errno.3.en.html for a
// description of error numbers.
const (
EPERM Errno = 1
EIO Errno = 5
ENOMEM Errno = 12
EINVAL Errno = 22
ENOSPC Errno = 28
EOVERFLOW Errno = 75
ESHUTDOWN Errno = 108
)
var errStr = map[Errno]string{
EPERM: "Operation not permitted",
EIO: "Input/output error",
ENOMEM: "Cannot allocate memory",
EINVAL: "Invalid argument",
ENOSPC: "No space left on device",
EOVERFLOW: "Value too large for defined data type",
ESHUTDOWN: "Cannot send after transport endpoint shutdown",
}
func (e Errno) Error() string {
if msg, ok := errStr[e]; ok {
return msg
}
return fmt.Sprintf("NBD_ERROR(%d)", uint32(e))
}
// Errno returns e.
func (e Errno) Errno() Errno {
return e
}
type errf struct {
errno Errno
error
}
func (e errf) Errno() Errno {
return e.errno
}
// Errorf returns an error implementing Error, returning code from Errno.
func Errorf(code Errno, msg string, v ...interface{}) Error {
if len(v) > 0 {
return errf{code, fmt.Errorf(msg, v...)}
}
return errf{code, errors.New(msg)}
}
const (
cmdFlagFUA = 1 << 0
cmdFlagNoHole = 1 << 1
cmdFlagDF = 1 << 2
cmdFlagReqOne = 1 << 3
)
const (
replyFlagDone = 1 << 0
)
const (
replyTypeNone = 0
replyTypeOffsetData = 1
replyTypeOffsetHole = 2
replyTypeBlockStatus = 5
replyTypeError = (1 << 15) + 1
replyTypeErrorOffset = (1 << 15) + 2
)
type request struct {
flags uint16
typ uint16
handle uint64
offset uint64
length uint32
data []byte
}
func (r *request) encode(e *encoder) {
e.writeUint32(reqMagic)
e.writeUint16(r.flags)
e.writeUint16(r.typ)
e.writeUint64(r.handle)
e.writeUint64(r.offset)
e.writeUint32(uint32(len(r.data)))
e.write(r.data)
}
func (r *request) decode(e *encoder) Error {
if e.uint32() != reqMagic {
e.check(errors.New("invalid magic for request"))
}
r.flags = e.uint16()
r.typ = e.uint16()
r.handle = e.uint64()
r.offset = e.uint64()
r.length = e.uint32()
if r.offset&(1<<63) != 0 {
return EOVERFLOW
}
if r.typ != cmdWrite {
return nil
}
if r.length > 4<<20 {
e.discard(r.length)
return EOVERFLOW
}
buf := make([]byte, r.length)
e.read(buf)
r.data = buf
return nil
}
type simpleReply struct {
errno uint32
handle uint64
data []byte
length uint32
}
func (r *simpleReply) encode(e *encoder) {
e.writeUint32(simpleReplyMagic)
e.writeUint32(r.errno)
e.writeUint64(r.handle)
e.write(r.data)
}
func (r *simpleReply) decode(e *encoder) Error {
if e.uint32() != simpleReplyMagic {
e.check(errors.New("invalid magic for reply"))
}
r.handle = e.uint64()
buf := make([]byte, r.length)
e.read(buf)
return nil
}
type structuredReply struct {
flags uint16
typ uint16
handle uint64
length uint32
data []byte
}
func (r *structuredReply) encode(e *encoder) {
e.writeUint32(structuredReplyMagic)
e.writeUint16(r.flags)
e.writeUint16(r.typ)
e.writeUint64(r.handle)
e.writeUint32(r.length)
e.write(r.data)
}
func (r *structuredReply) decode(e *encoder) Error {
if e.uint64() != structuredReplyMagic {
e.check(errors.New("invalid magic for reply"))
}
r.flags = e.uint16()
r.typ = e.uint16()
r.handle = e.uint64()
r.length = e.uint32()
if r.length > 4<<20 {
e.discard(r.length)
return EOVERFLOW
}
buf := make([]byte, r.length)
e.read(buf)
r.data = buf
return nil
}