RFC: spec: Introduce NBD_REPLY_TYPE_OFFSET_HOLE_EXT
[nbd/ericb.git] / doc / proto.md
blob9fa471a22e5f4b4dccf948424fa942d0030c53f9
1 # The NBD protocol
3 ## Introduction
5 The Network Block Device is a Linux-originated lightweight block access
6 protocol that allows one to export a block device to a client. While the
7 name of the protocol specifically references the concept of block
8 devices, there is nothing inherent in the *protocol* which requires that
9 exports are, in fact, block devices; the protocol only concerns itself
10 with a range of bytes, and several operations of particular lengths at
11 particular offsets within that range of bytes.
13 For matters of clarity, in this document we will refer to an export from
14 a server as a block device, even though the actual backing on the server
15 need not be an actual block device; it may be a block device, a regular
16 file, or a more complex configuration involving several files. That is
17 an implementation detail of the server.
19 ## Conventions
21 In the below protocol descriptions, the label 'C:' is used for
22 messages sent by the client, whereas 'S:' is used for messages sent by
23 the server).  `monotype text` is for literal character data or (when
24 used in comments) constant names, `0xdeadbeef` is used for literal hex
25 numbers (which are always sent in big-endian network byte order), and
26 (brackets) are used for comments. Anything else is a description of
27 the data that is sent.
29 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL",
30 "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED",
31 "MAY", and "OPTIONAL" in this document are to be interpreted as
32 described in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt).
33 The same words in lower case carry their natural meaning.
35 Where this document refers to a string, then unless otherwise stated,
36 that string is a sequence of UTF-8 code points, which is not `NUL`
37 terminated, MUST NOT contain `NUL` characters, SHOULD be no longer than
38 256 bytes and MUST be no longer than 4096 bytes. This applies
39 to export names and error messages (amongst others). The length of a
40 string is always available through information sent earlier in the same
41 message, although it may require some computation based on the size of
42 other data also present in the same message.
44 ## Protocol phases
46 The NBD protocol has two phases: the handshake and the transmission. During the
47 handshake, a connection is established and an exported NBD device along other
48 protocol parameters are negotiated between the client and the server. After a
49 successful handshake, the client and the server proceed to the transmission
50 phase in which the export is read from and written to.
52 On the client side under Linux, the handshake is implemented in
53 userspace, while the transmission phase is implemented in kernel space.
54 To get from the handshake to the transmission phase, the client performs
56     ioctl(nbd, NBD_SET_SOCK, sock)
57     ioctl(nbd, NBD_DO_IT)
59 with `nbd` in the above being a file descriptor for an open `/dev/nbdX`
60 device node, and `sock` being the socket to the server. The second of
61 the above two calls does not return until the client disconnects.
63 Note that there are other `ioctl` calls available, that are used by the
64 client to communicate the options to the kernel which were negotiated
65 with the server during the handshake. This document does not describe
66 those.
68 When handling the client-side transmission phase with the Linux
69 kernel, the socket between the client and server can use either Unix
70 or TCP sockets. For other implementations, the client and server can
71 use any agreeable communication channel (a socket is typical, but it
72 is also possible to implement the NBD protocol over a pair of
73 uni-directional pipes). If TCP sockets are used, both the client and
74 server SHOULD disable Nagle's algorithm (that is, use `setsockopt` to
75 set the `TCP_NODELAY` option to non-zero), to eliminate artificial
76 delays caused by waiting for an ACK response when a large message
77 payload spans multiple network packets.
79 ### Handshake
81 The handshake is the first phase of the protocol. Its main purpose is to
82 provide means for both the client and the server to negotiate which
83 export they are going to use and how.
85 There are three versions of the negotiation. They are referred to as
86 "oldstyle", "newstyle", and "fixed newstyle" negotiation. Oldstyle was
87 the only version of the negotiation until nbd 2.9.16; newstyle was
88 introduced for nbd 2.9.17. A short while later, it was discovered that
89 newstyle was insufficiently structured to allow protocol options to be
90 added while retaining backwards compatibility. The minor changes
91 introduced to fix this problem are, where necessary, referred to as
92 "fixed newstyle" to differentiate from the original version of the
93 newstyle negotiation.
95 #### Oldstyle negotiation
97 S: 64 bits, `0x4e42444d41474943` (ASCII '`NBDMAGIC`') (also known as
98    the `INIT_PASSWD`)  
99 S: 64 bits, `0x00420281861253` (`cliserv_magic`, a magic number)  
100 S: 64 bits, size of the export in bytes (unsigned)  
101 S: 32 bits, flags  
102 S: 124 bytes, zeroes (reserved).  
104 As can be seen, this isn't exactly a negotiation; it's just a matter of
105 the server sending a bunch of data to the client. If the client is
106 unhappy with what he receives, he should disconnect and not look back.
108 The fact that the size of the export was specified before the flags were
109 sent, made it impossible for the protocol to be changed in a
110 backwards-compatible manner to allow for named exports without ugliness.
111 As a result, the old style negotiation is now no longer developed;
112 starting with version 3.10 of the reference implementation, it is also
113 no longer supported.
115 #### Newstyle negotiation
117 A client who wants to use the new style negotiation SHOULD connect on
118 the IANA-reserved port for NBD, 10809. The server MAY listen on other
119 ports as well, but it SHOULD use the old style handshake on those. The
120 server SHOULD refuse to allow oldstyle negotiations on the newstyle
121 port. For debugging purposes, the server MAY change the port on which to
122 listen for newstyle negotiation, but this SHOULD NOT happen for
123 production purposes.
125 The initial few exchanges in newstyle negotiation look as follows:
127 S: 64 bits, `0x4e42444d41474943` (ASCII '`NBDMAGIC`') (as in the old
128    style handshake)  
129 S: 64 bits, `0x49484156454F5054` (ASCII '`IHAVEOPT`') (note different
130    magic number)  
131 S: 16 bits, handshake flags  
132 C: 32 bits, client flags  
134 This completes the initial phase of negotiation; the client and server
135 now both know they understand the first version of the newstyle
136 handshake, with no options. The client SHOULD ignore any handshake flags
137 it does not recognize, while the server MUST close the TCP connection if
138 it does not recognize the client's flags.  What follows is a repeating
139 group of options. In non-fixed newstyle only one option can be set
140 (`NBD_OPT_EXPORT_NAME`), and it is not optional.
142 At this point, we move on to option haggling, during which point the
143 client can send one or (in fixed newstyle) more options to the server.
144 The generic format of setting an option is as follows:
146 C: 64 bits, `0x49484156454F5054` (ASCII '`IHAVEOPT`') (note same
147    newstyle handshake's magic number)  
148 C: 32 bits, option  
149 C: 32 bits, length of option data (unsigned)  
150 C: any data needed for the chosen option, of length as specified above.  
152 The presence of the option length in every option allows the server
153 to skip any options presented by the client that it does not
154 understand.
156 If the value of the option field is `NBD_OPT_EXPORT_NAME` and the server
157 is willing to allow the export, the server replies with information
158 about the used export:
160 S: 64 bits, size of the export in bytes (unsigned)  
161 S: 16 bits, transmission flags  
162 S: 124 bytes, zeroes (reserved) (unless `NBD_FLAG_C_NO_ZEROES` was
163    negotiated by the client)  
165 If the server is unwilling to allow the export, it MUST terminate
166 the session.
168 The reason that the flags field is 16 bits large and not 32 as in the
169 oldstyle negotiation is that there are now 16 bits of transmission flags,
170 and 16 bits of handshake flags. Concatenated together, this results in
171 32 bits, which allows for using a common set of macros for both. If we
172 ever run out of flags, the server will set the most significant flag
173 bit, signalling that an extra flag field will follow, to which the
174 client will have to reply with a flag field of its own before the extra
175 flags are sent. This is not yet implemented.
177 #### Fixed newstyle negotiation
179 Unfortunately, due to a mistake, the server would immediately close the
180 connection when it saw an option it did not understand, rather than
181 signalling this fact to the client, which would've allowed it to retry;
182 and replies from the server were not structured either, which meant that
183 if the server were to send something the client did not understand, it
184 would have to abort negotiation as well.
186 To fix these two issues, the following changes were implemented:
188 - The server will set the handshake flag `NBD_FLAG_FIXED_NEWSTYLE`, to
189   signal that it supports fixed newstyle negotiation.
190 - The client SHOULD reply with `NBD_FLAG_C_FIXED_NEWSTYLE` set in its flags
191   field too, though its side of the protocol does not change incompatibly.
192 - The client MAY now send other options to the server as appropriate, in
193   the generic format for sending an option as described above.
194 - The server will reply to any option apart from `NBD_OPT_EXPORT_NAME`
195   with reply packets in the following format:
197 S: 64 bits, `0x3e889045565a9` (magic number for replies)  
198 S: 32 bits, the option as sent by the client to which this is a reply  
199 S: 32 bits, reply type (e.g., `NBD_REP_ACK` for successful completion,
200    or `NBD_REP_ERR_UNSUP` to mark use of an option not known by this
201    server  
202 S: 32 bits, length of the reply. This MAY be zero for some replies, in
203    which case the next field is not sent  
204 S: any data as required by the reply (e.g., an export name in the case
205    of `NBD_REP_SERVER`)  
207 The client MUST NOT send any option until it has received a final
208 reply to any option it has sent (note that some options e.g.
209 `NBD_OPT_LIST` have multiple replies, and the final reply is
210 the last of those).
212 Some messages the client sends instruct the server to change some of
213 its internal state.  The client SHOULD NOT send such messages more
214 than once; if it does, the server MAY fail the repeated message with
215 `NBD_REP_ERR_INVALID`.
217 #### Termination of the session during option haggling
219 There are three possible mechanisms to end option haggling:
221 * Transmission mode can be entered (by the client sending
222   `NBD_OPT_EXPORT_NAME` or by the server responding to an
223   `NBD_OPT_GO` with `NBD_REP_ACK`). This is documented
224   elsewhere.
226 * The client can send (and the server can reply to) an
227   `NBD_OPT_ABORT`. This MUST be followed by the client
228   shutting down TLS (if it is running), and the client
229   dropping the connection. This is referred to as
230   'initiating a soft disconnect'; soft disconnects can
231   only be initiated by the client.
233 * The client or the server can disconnect the TCP session
234   without activity at the NBD protocol level. If TLS is
235   negotiated, the party initiating the transaction SHOULD
236   shutdown TLS first if it is running. This is referred
237   to as 'initiating a hard disconnect'.
239 This section concerns the second and third of these, together
240 called 'terminating the session', and under which circumstances
241 they are valid.
243 If either the client or the server detects a violation of a
244 mandatory condition ('MUST' etc.) by the other party, it MAY
245 initiate a hard disconnect.
247 A client MAY use a soft disconnect to terminate the session
248 whenever it wishes.
250 A party that is mandated by this document to terminate the
251 session MUST initiate a hard disconnect if it is not possible
252 to use a soft disconnect. Such circumstances include: where
253 that party is the server and it cannot return an error
254 (e.g. after an `NBD_OPT_EXPORT_NAME` it cannot satisfy),
255 and where that party is the client following a failed TLS
256 negotiation.
258 A party MUST NOT initiate a hard disconnect save where set out
259 in this section. Therefore, unless a client's situation falls
260 within the provisions of the previous paragraph or the
261 client detects a breach of a mandatory condition, it MUST NOT
262 use a hard disconnect, and hence its only option to terminate
263 the session is via a soft disconnect.
265 There is no requirement for the client or server to complete a
266 negotiation if it does not wish to do so. Either end MAY simply
267 terminate the session. In the client's case, if it wishes to
268 do so it MUST use soft disconnect.
270 In the server's case it MUST (save where set out above) simply
271 error inbound options until the client gets the hint that it is
272 unwelcome, except that if a server believes a client's behaviour
273 constitutes a denial of service, it MAY initiate a hard disconnect.
274 If the server is in the process of being shut down it MAY
275 error any inflight option and SHOULD error further options received
276 (other than an `NBD_OPT_ABORT`) with `NBD_REP_ERR_SHUTDOWN`.
278 If the client receives `NBD_REP_ERR_SHUTDOWN` it MUST initiate
279 a soft disconnect.
281 ### Transmission
283 There are two general message types in the transmission phase: the
284 request (simple or extended), and the reply (simple, structured, or
285 extended).  The determination of which message headers to use is
286 determined during handshaking phase, based on whether
287 `NBD_OPT_STRUCTURED_REPLY` or `NBD_OPT_EXTENDED_HEADERS` was requested
288 by the client and given a successful response by the server.  The
289 transmission phase consists of a series of transactions, where the
290 client submits requests and the server sends corresponding replies
291 with either a single simple reply or a series of one or more
292 structured or extended reply chunks per request.  The phase continues
293 until either side terminates transmission; this can be performed
294 cleanly only by the client.
296 Note that without client negotiation, the server MUST use only simple
297 replies, and that it is impossible to tell by reading the server
298 traffic in isolation whether a data field will be present; the simple
299 reply is also problematic for error handling of the `NBD_CMD_READ`
300 request.  Therefore, structured or extended replies can be used to
301 create a context-free server stream; see below.
303 The results of client negotiation also determine whether the client
304 and server will utilize only compact requests and replies, or whether
305 both sides will use only extended packets.  Compact messages are the
306 default, but inherently limit single transactions to a 32-bit window
307 starting at a 64-bit offset.  Extended messages make it possible to
308 perform 64-bit transactions (although typically only for commands that
309 do not include a data payload).  Furthermore, when only structured
310 replies have been negotiated, compact messages require the client to
311 perform partial reads to determine which reply packet style (16-byte
312 simple or 20-byte structured) is on the wire before knowing the length
313 of the rest of the reply, which can reduce client performance.  With
314 extended messages, all packet headers have a fixed length of 32 bytes,
315 and although this results in more traffic over the network, the
316 resulting layout is friendlier for performance.
318 Replies need not be sent in the same order as requests (i.e., requests
319 may be handled by the server asynchronously), and structured or
320 extended reply chunks from one request may be interleaved with reply
321 messages from other requests; however, there may be constraints that
322 prevent arbitrary reordering of reply chunks within a given reply.
323 Clients SHOULD use a cookie that is distinct from all other currently
324 pending transactions, but MAY reuse cookies that are no longer in
325 flight; cookies need not be consecutive.  In each reply message
326 (whether simple, structured, or extended), the server MUST use the
327 same value for cookie as was sent by the client in the corresponding
328 request, treating the cookie as an opaque field.  In this way, the
329 client can correlate which request is receiving a response.  Note that
330 earlier versions of this specification referred to a client's cookie
331 as a handle.
333 #### Ordering of messages and writes
335 The server MAY process commands out of order, and MAY reply out of
336 order, except that:
338 * All write commands (that includes `NBD_CMD_WRITE`,
339   `NBD_CMD_WRITE_ZEROES` and `NBD_CMD_TRIM`) that the server
340   completes (i.e. replies to) prior to processing a
341   `NBD_CMD_FLUSH` MUST be written to non-volatile
342   storage prior to replying to that `NBD_CMD_FLUSH`. This
343   paragraph only applies if `NBD_FLAG_SEND_FLUSH` is set within
344   the transmission flags, as otherwise `NBD_CMD_FLUSH` will never
345   be sent by the client to the server.
347 * A client which uses multiple connections to a server to parallelize
348   commands MUST NOT issue an `NBD_CMD_FLUSH` request until it has
349   received the reply for all write commands which it expects to be
350   covered by the flush.
352 * A server MUST NOT reply to a command that has `NBD_CMD_FLAG_FUA` set
353   in its command flags until the data (if any) written by that command
354   is persisted to non-volatile storage. This only applies if
355   `NBD_FLAG_SEND_FUA` is set within the transmission flags, as otherwise
356   `NBD_CMD_FLAG_FUA` will not be set on any commands sent to the server
357   by the client.
359 `NBD_CMD_FLUSH` is modelled on the Linux kernel empty bio with
360 `REQ_PREFLUSH` set. `NBD_CMD_FLAG_FUA` is modelled on the Linux
361 kernel bio with `REQ_FUA` set. In case of ambiguity in this
362 specification, the
363 [kernel documentation](https://www.kernel.org/doc/Documentation/block/writeback_cache_control.txt)
364 may be useful.
366 #### Request message
368 The compact request message is sent by the client when extended
369 transactions are not in use (either the client did not request
370 extended headers during negotiation, or the server responded that
371 `NBD_OPT_EXTENDED_HEADERS` is unsupported), and looks as follows:
373 C: 32 bits, 0x25609513, magic (`NBD_REQUEST_MAGIC`)  
374 C: 16 bits, command flags  
375 C: 16 bits, type  
376 C: 64 bits, cookie  
377 C: 64 bits, offset (unsigned)  
378 C: 32 bits, length (unsigned)  
379 C: (*length* bytes of data if the request is of type `NBD_CMD_WRITE`)  
381 In the compact style, the client MUST NOT use the
382 `NBD_CMD_FLAG_PAYLOAD_LEN` flag; and the only command where *length*
383 represents payload length instead of effect length is `NBD_CMD_WRITE`.
385 If negotiation agreed on extended transactions with
386 `NBD_OPT_EXTENDED_HEADERS`, the client instead uses extended requests:
388 C: 32 bits, 0x21e41c71, magic (`NBD_EXTENDED_REQUEST_MAGIC`)  
389 C: 16 bits, command flags  
390 C: 16 bits, type  
391 C: 64 bits, cookie  
392 C: 64 bits, offset (unsigned)  
393 C: 64 bits, payload/effect length (unsigned)  
394 C: (*length* bytes of data if *flags* includes `NBD_CMD_FLAG_PAYLOAD_LEN`)  
396 With extended headers, the meaning of the *length* field depends on
397 whether *flags* contains `NBD_CMD_FLAG_PAYLOAD_LEN` (that many
398 additional bytes of payload are present), or if the flag is absent
399 (there is no payload, and *length* instead is an effect length
400 describing how much of the export the request operates on).  The
401 command `NBD_CMD_WRITE` MUST use the flag `NBD_CMD_FLAG_PAYLOAD_LEN`
402 in this mode; most other commands omit it, although some like
403 `NBD_CMD_BLOCK_STATUS` optionally support the flag in order to allow
404 the client to pass additional information in the payload (where the
405 command documents what the payload will contain, including the
406 possibility of a separate effect length).  A server SHOULD initiate
407 hard disconnect if a client sets the `NBD_CMD_FLAG_PAYLOAD_LEN` flag
408 and uses a *length* larger than a server's advertised or default
409 maximum payload length (capped at 32 bits by the constraints of
410 `NBD_INFO_BLOCK_SIZE`); in all other cases, a server SHOULD gracefully
411 consume *length* bytes of payload (even if it then replies with an
412 `NBD_EINVAL` failure because the particular command was not expecting
413 a payload), and proceed with the next client command.  Thus, only when
414 *length* is used as an effective length will it utilize a full 64-bit
415 value.
417 #### Simple reply message
419 The simple reply message MUST be sent by the server in response to all
420 requests if neither structured replies (`NBD_OPT_STRUCTURED_REPLY`)
421 nor extended headers (`NBD_OPT_EXTENDED_HEADERS`) have been
422 negotiated.  If structured replies have been negotiated, a simple
423 reply MAY be used as a reply to any request other than `NBD_CMD_READ`,
424 but only if the reply has no data payload.  If extended headers have
425 been negotiated, a simple reply MUST NOT be used.  The message looks
426 as follows:
428 S: 32 bits, 0x67446698, magic (`NBD_SIMPLE_REPLY_MAGIC`; used to be
429    `NBD_REPLY_MAGIC`)  
430 S: 32 bits, error (MAY be zero)  
431 S: 64 bits, cookie (MUST match the request)  
432 S: (*length* bytes of data if the request is of type `NBD_CMD_READ` and
433     *error* is zero)  
435 #### Structured reply chunk message
437 Some of the major downsides of the default simple reply to
438 `NBD_CMD_READ` are as follows.  First, it is not possible to support
439 partial reads or early errors (the command must succeed or fail as a
440 whole; no payload is sent if *error* was set, but if *error* is zero
441 and a later error is detected before *length* bytes are returned, the
442 server must initiate a hard disconnect).  Second, there is no way to
443 efficiently skip over portions of a sparse export that is known to
444 contain all zeroes.  Finally, it is not possible to reliably decode
445 the server traffic without also having context of what pending read
446 requests were sent by the client, to see which *cookie* values will
447 have accompanying payload on success.  Therefore structured replies
448 are also permitted if negotiated.
450 A structured reply in the transmission phase consists of one or
451 more structured reply chunk messages.  The server MUST NOT send
452 this reply type unless the client has successfully negotiated
453 structured replies via `NBD_OPT_STRUCTURED_REPLY`.  Conversely, if
454 structured replies are negotiated, the server MUST use a
455 structured reply for any response with a payload, and MUST NOT use
456 a simple reply for `NBD_CMD_READ` (even for the case of an early
457 `NBD_EINVAL` due to bad flags), but MAY use either a simple reply or a
458 structured reply to all other requests.  The server SHOULD prefer
459 sending errors via a structured reply, as the error can then be
460 accompanied by a string payload to present to a human user.
462 A structured reply MAY occupy multiple structured chunk messages
463 (all with the same value for "cookie"), and the
464 `NBD_REPLY_FLAG_DONE` reply flag is used to identify the final
465 chunk.  Unless further documented by individual requests below,
466 the chunks MAY be sent in any order, except that the chunk with
467 the flag `NBD_REPLY_FLAG_DONE` MUST be sent last.  Even when a
468 command documents further constraints between chunks of one reply,
469 it is always safe to interleave chunks of that reply with messages
470 related to other requests.  A server SHOULD try to minimize the
471 number of chunks sent in a reply, but MUST NOT mark a chunk as
472 final if there is still a possibility of detecting an error before
473 transmission of that chunk completes.  A structured reply is
474 considered successful only if it did not contain any error chunks,
475 although the client MAY be able to determine partial success based
476 on the chunks received.
478 A structured reply chunk message looks as follows:
480 S: 32 bits, 0x668e33ef, magic (`NBD_STRUCTURED_REPLY_MAGIC`)  
481 S: 16 bits, reply flags  
482 S: 16 bits, type  
483 S: 64 bits, cookie (MUST match the request)  
484 S: 32 bits, length of payload (unsigned)  
485 S: *length* bytes of payload data (if *length* is nonzero)  
487 The use of *length* in the reply allows context-free division of
488 the overall server traffic into individual reply messages; the
489 *type* field describes how to further interpret the payload.
491 #### Extended reply chunk message
493 One implementation drawback of the original structured replies via
494 `NBD_OPT_STRUCTURED_REPLY` is that a client must read the magic number
495 to know whether it will then be reading a 16-byte simple or 20-byte
496 structured header from the server.  Another is that an array of
497 20-byte structures is not always cache-line friendly, compared to
498 power-of-2 sizing.  Therefore, a client may instead attempt to
499 negotiate extended headers via `NBD_OPT_EXTENDED_HEADERS` in place of
500 structured replies.
502 If extended headers are negotiated, all server replies MUST use an
503 extended reply; the server MUST NOT use simple replies even when there
504 is no payload.  Most fields in the extended reply have the same
505 semantics as their counterparts in structured replies (including the
506 use of `NBD_REPLY_FLAG_DONE` to end a sequence of one or more chunks
507 comprising the overall reply).  The extended reply has an additional
508 field *offset* which MUST match the *offset* of the client's request
509 (even in reply types such as `NBD_REPLY_TYPE_OFFSET_DATA` where the
510 payload itself contains a different offset representing the middle of
511 the buffer).
513 The other difference between extended replies and structured replies
514 is that the *length* field is 64 bits to match the layout of the
515 extended request header.  However, note that while the request flags
516 determine whether a request uses a 32-bit payload length or a 64-bit
517 effect length, at this time the reply length is used solely for
518 payload lengths and so in practice will never exceed a 32-bit value
519 (see `NBD_INFO_BLOCK_SIZE`); a client MAY initiate hard disconnect if
520 a server's reply length is unexpectedly large.
522 An extended reply chunk message looks as follows:
524 S: 32 bits, 0x6e8a278c, magic (`NBD_EXTENDED_REPLY_MAGIC`)  
525 S: 16 bits, reply flags  
526 S: 16 bits, type  
527 S: 64 bits, cookie (MUST match the request)  
528 S: 64 bits, offset (MUST match the request)  
529 S: 64 bits, length of payload (unsigned)  
530 S: *length* bytes of payload data (if *length* is nonzero)  
532 #### Terminating the transmission phase
534 There are two methods of terminating the transmission phase:
536 * The client sends `NBD_CMD_DISC` whereupon the server MUST
537   close down the TLS session (if one is running) and then
538   close the TCP connection. This is referred to as 'initiating
539   a soft disconnect'. Soft disconnects can only be
540   initiated by the client.
542 * The client or the server drops the TCP session (in which
543   case it SHOULD shut down the TLS session first). This is
544   referred to as 'initiating a hard disconnect'.
546 Together these are referred to as 'terminating transmission'.
548 Either side MAY initiate a hard disconnect if it detects
549 a violation by the other party of a mandatory condition
550 within this document.
552 On a server shutdown, the server SHOULD wait for inflight
553 requests to be serviced prior to initiating a hard disconnect.
554 A server MAY speed this process up by issuing error replies.
555 The error value issued in respect of these requests and
556 any subsequently received requests SHOULD be `NBD_ESHUTDOWN`.
558 If the client receives an `NBD_ESHUTDOWN` error it MUST initiate
559 a soft disconnect.
561 The client MAY issue a soft disconnect at any time, but
562 SHOULD wait until there are no inflight requests first.
564 The client and the server MUST NOT initiate any form
565 of disconnect other than in one of the above circumstances.
567 #### Reserved Magic values
569 The following magic values are reserved and must not be used
570 for future protocol extensions:
572 0x12560953 - Historic value for NBD_REQUEST_MAGIC, used
573              until Linux 2.1.116pre2.
574 0x96744668 - Historic value for NBD_REPLY_MAGIC, used
575              until Linux 2.1.116pre2.
576 0x25609514 - Used by nbd-server to store data log flags in the
577              transaction log. Never sent from/to a client.
579 ## TLS support
581 The NBD protocol supports Transport Layer Security (TLS) (see
582 [RFC5246](https://tools.ietf.org/html/rfc5246)
583 as updated by
584 [RFC6176](https://tools.ietf.org/html/rfc6176)
587 TLS is negotiated with the `NBD_OPT_STARTTLS`
588 option. This is performed as an in-session upgrade. Below the term
589 'negotiation' is used to refer to the sending and receiving of
590 NBD options and option replies, and the term 'initiation' of TLS
591 is used to refer to the actual upgrade to TLS.
593 ### Certificates, authentication and authorisation
595 This standard does not specify what encryption, certification
596 and signature algorithms are used. This standard does not
597 specify authentication and authorisation (for instance
598 whether client and/or server certificates are required and
599 what they should contain); this is implementation dependent.
601 TLS requires fixed newstyle negotiation to have completed.
603 ### Server-side requirements
605 There are three modes of operation for a server. The
606 server MUST support one of these modes.
608 * The server operates entirely without TLS ('NOTLS'); OR
610 * The server insists upon TLS, and forces the client to
611   upgrade by erroring any NBD options other than `NBD_OPT_STARTTLS`
612   or `NBD_OPT_ABORT` with `NBD_REP_ERR_TLS_REQD` ('FORCEDTLS'); this
613   in practice means that all option negotiation (apart from the
614   `NBD_OPT_STARTTLS` itself) is carried out with TLS; OR
616 * The server provides TLS, and it is mandatory on zero or more
617   exports, and is available at the client's option on all
618   other exports ('SELECTIVETLS'). The server does not force
619   the client to upgrade to TLS during option haggling (as
620   if the client ultimately were to choose a non-TLS-only export,
621   stopping TLS is not possible). Instead it permits the client
622   to upgrade as and when it chooses, but unless an upgrade to
623   TLS has already taken place, the server errors attempts
624   to enter transmission mode on TLS-only exports, MAY
625   refuse to provide information about TLS-only exports
626   via `NBD_OPT_INFO`, MAY refuse to provide information
627   about non-existent exports via `NBD_OPT_INFO`, and MAY omit
628   exports that are TLS-only from `NBD_OPT_LIST`.
630 The server MAY determine the mode in which it operates
631 dependent upon the session (for instance it might be
632 more liberal with TCP connections made over the loopback
633 interface) but it MUST be consistent in its mode
634 of operation across the lifespan of a single TCP connection
635 to the server. A client MUST NOT assume indications from
636 a prior TCP session to a given server will be relevant
637 to a subsequent session.
639 The server MUST operate in NOTLS mode unless the server
640 set flag `NBD_FLAG_FIXED_NEWSTYLE` and the client replied
641 with `NBD_FLAG_C_FIXED_NEWSTYLE` in the fixed newstyle
642 negotiation.
644 These modes of operations are described in detail below.
646 #### NOTLS mode
648 If the server receives `NBD_OPT_STARTTLS` it MUST respond with
649 `NBD_REP_ERR_POLICY` (if it does not support TLS for
650 policy reasons), `NBD_REP_ERR_UNSUP` (if it does not
651 support the `NBD_OPT_STARTTLS` option at all) or another
652 error explicitly permitted by this document. The server MUST NOT
653 respond to any option request with `NBD_REP_ERR_TLS_REQD`.
655 #### FORCEDTLS mode
657 If the server receives `NBD_OPT_STARTTLS` prior to negotiating
658 TLS, it MUST reply with `NBD_REP_ACK`. If the server receives
659 `NBD_OPT_STARTTLS` when TLS has already been negotiated, it
660 it MUST reply with `NBD_REP_ERR_INVALID`.
662 After an `NBD_REP_ACK` reply has been sent, the server MUST be
663 prepared for a TLS handshake, and all further data MUST be sent
664 and received over TLS. There is no downgrade to a non-TLS session.
666 As per the TLS standard, the handshake MAY be initiated either
667 by the server (having sent the `NBD_REP_ACK`) or by the client.
668 If the handshake is unsuccessful (for instance the client's
669 certificate does not match) the server MUST terminate the
670 session as by this stage it is too late to continue without TLS
671 as the acknowledgement has been sent.
673 If the server receives any other option, including `NBD_OPT_INFO`
674 and unsupported options, it MUST reply with `NBD_REP_ERR_TLS_REQD`
675 if TLS has not been initiated; `NBD_OPT_INFO` is included as in this
676 mode, all exports are TLS-only. If the server receives a request to
677 enter transmission mode via `NBD_OPT_EXPORT_NAME` when TLS has not
678 been initiated, then as this request cannot error, it MUST
679 terminate the session. If the server receives a request to
680 enter transmission mode via `NBD_OPT_GO` when TLS has not been
681 initiated, it MUST error with `NBD_REP_ERR_TLS_REQD`.
683 The server MUST NOT send `NBD_REP_ERR_TLS_REQD` in reply to
684 any option if TLS has already been initiated.
686 The FORCEDTLS mode of operation has an implementation problem in
687 that the client MAY legally simply send a `NBD_OPT_EXPORT_NAME`
688 to enter transmission mode without previously sending any options.
689 This is avoided by use of `NBD_OPT_INFO` and `NBD_OPT_GO`.
691 #### SELECTIVETLS mode
693 If the server receives `NBD_OPT_STARTTLS` prior to negotiating
694 TLS, it MUST reply with `NBD_REP_ACK` and initiate TLS as set
695 out under 'FORCEDTLS' above. If the server receives
696 `NBD_OPT_STARTTLS` when TLS has already been negotiated, it
697 it MUST reply with `NBD_REP_ERR_INVALID`.
699 If the server receives `NBD_OPT_INFO` or `NBD_OPT_GO` and TLS
700 has not been initiated, it MAY reply with `NBD_REP_ERR_TLS_REQD`
701 if that export is non-existent, and MUST reply with
702 `NBD_REP_ERR_TLS_REQD` if that export is TLS-only.
704 If the server receives a request to enter transmission mode
705 via `NBD_OPT_EXPORT_NAME` on a TLS-only export when TLS has not
706 been initiated, then as this request cannot error, it MUST
707 terminate the session.
709 The server MUST NOT send `NBD_REP_ERR_TLS_REQD` in reply to
710 any option if TLS has already been negotiated. The server
711 MUST NOT send `NBD_REP_ERR_TLS_REQD` in response to any
712 option other than `NBD_OPT_INFO`, `NBD_OPT_GO` and
713 `NBD_OPT_EXPORT_NAME`, and only in those cases in respect of
714 a TLS-only or non-existent export.
716 There is a degenerate case of SELECTIVETLS where all
717 exports are TLS-only. This is permitted in part to make programming
718 of servers easier. Operation is a little different from FORCEDTLS,
719 as the client is not forced to upgrade to TLS prior to any options
720 being processed, and the server MAY choose to give information on
721 non-existent exports via `NBD_OPT_INFO` responses prior to an upgrade
722 to TLS.
724 ### Client-side requirements
726 If the client supports TLS at all, it MUST be prepared
727 to deal with servers operating in any of the above modes.
728 Notwithstanding, a client MAY always terminate the session or
729 refuse to connect to a particular export if TLS is
730 not available and the user requires TLS.
732 The client MUST NOT issue `NBD_OPT_STARTTLS` unless the server
733 set flag `NBD_FLAG_FIXED_NEWSTYLE` and the client replied
734 with `NBD_FLAG_C_FIXED_NEWSTYLE` in the fixed newstyle
735 negotiation.
737 The client MUST NOT issue `NBD_OPT_STARTTLS` if TLS has already
738 been initiated.
740 Subject to the above two limitations, the client MAY send
741 `NBD_OPT_STARTTLS` at any time to initiate a TLS session. If the
742 client receives `NBD_REP_ACK` in response, it MUST immediately
743 upgrade the session to TLS. If it receives `NBD_REP_ERR_UNSUP`,
744 `NBD_REP_ERR_POLICY` or any other error in response, it indicates
745 that the server cannot or will not upgrade the session to TLS,
746 and therefore the client MUST either continue the session
747 without TLS, or terminate the session.
749 A client that prefers to use TLS irrespective of whether
750 the server makes TLS mandatory SHOULD send `NBD_OPT_STARTTLS`
751 as the first option. This will ensure option haggling is subject
752 to TLS, and will thus prevent the possibility of options being
753 compromised by a Man-in-the-Middle attack. Note that the
754 `NBD_OPT_STARTTLS` itself may be compromised - see 'downgrade
755 attacks' for more details. For this reason, a client which only
756 wishes to use TLS SHOULD terminate the session if the
757 `NBD_OPT_STARTTLS` replies with an error.
759 If the TLS handshake is unsuccessful (for instance the server's
760 certificate does not validate) the client MUST terminate the
761 session as by this stage it is too late to continue without TLS.
763 If the client receives an `NBD_REP_ERR_TLS_REQD` in response
764 to any option, it implies that this option cannot be executed
765 unless a TLS upgrade is performed. If the option is any
766 option other than `NBD_OPT_INFO` or `NBD_OPT_GO`, this
767 indicates that no option will succeed unless a TLS upgrade
768 is performed; the client MAY therefore choose to issue
769 an `NBD_OPT_STARTTLS`, or MAY terminate the session (if
770 for instance it does not support TLS or does not have
771 appropriate credentials for this server). If the client
772 receives `NBD_REP_ERR_TLS_REQD` in response to
773 `NBD_OPT_INFO` or `NBD_OPT_GO` this indicates that the
774 export referred to within the option is either non-existent
775 or requires TLS; the client MAY therefore choose to issue
776 an `NBD_OPT_STARTTLS`, MAY terminate the session (if
777 for instance it does not support TLS or does not have
778 appropriate credentials for this server), or MAY continue
779 in another manner without TLS, for instance by querying
780 or using other exports.
782 If a client supports TLS, it SHOULD use `NBD_OPT_GO`
783 (if the server supports it) in place
784 of `NBD_OPT_EXPORT_NAME`. One reason for this is set out in
785 the final paragraphs of the sections under 'FORCEDTLS'
786 and 'SELECTIVETLS': this gives an opportunity for the
787 server to transmit that an error going into transmission
788 mode is due to the client's failure to initiate TLS,
789 and the fact that the client may obtain information about
790 which exports are TLS-only through `NBD_OPT_INFO`.  Another reason is
791 that the handshake flag `NBD_FLAG_C_NO_ZEROES` can be altered by a
792 MitM downgrade attack, which can cause a protocol mismatch with
793 `NBD_OPT_EXPORT_NAME` but not with `NBD_OPT_GO`.
795 ### Security considerations
797 #### TLS versions
799 NBD implementations supporting TLS MUST support TLS version 1.2,
800 SHOULD support any later versions. NBD implementations
801 MAY support older versions but SHOULD NOT do so by default
802 (i.e. they SHOULD only be available by a configuration change).
803 Older versions SHOULD NOT be used where there is a risk of security
804 problems with those older versions or of a downgrade attack
805 against TLS versions.
807 #### Protocol downgrade attacks
809 A danger inherent in any scheme relying on the negotiation
810 of whether TLS should be employed is downgrade attacks within
811 the NBD protocol.
813 There are two main dangers:
815 * A Man-in-the-Middle (MitM) hijacks a session and impersonates the
816   server (possibly by proxying it) claiming not to support TLS (for
817   example, by omitting `NBD_FLAG_FIXED_NEWSTYLE` or changing a
818   response to `NBD_OPT_STARTTLS`). In this manner, the client is
819   confused into operating in a plain-text manner with the MitM (with
820   the session possibly being proxied in plain-text to the server using
821   the method below).
823 * The MitM hijacks a session and impersonates the client (possibly by
824   proxying it) claiming not to support TLS (for example, by omitting
825   `NBD_FLAG_C_FIXED_NEWSTYLE` or eliding a request for
826   `NBD_OPT_STARTTLS`). In this manner the server is confused into
827   operating in a plain-text manner with the MitM (with the session
828   being possibly proxied to the client with the method above).
830 With regard to the first, any client that does not wish
831 to be subject to potential downgrade attack SHOULD ensure
832 that if a TLS endpoint is specified by the client, it
833 ensures that TLS is negotiated prior to sending or
834 requesting sensitive data. To recap, the client MAY send
835 `NBD_OPT_STARTTLS` at any point during option haggling,
836 and MAY terminate the session if `NBD_REP_ACK` is not
837 provided.
839 With regard to the second, any server that does not wish
840 to be subject to a potential downgrade attack SHOULD either
841 used FORCEDTLS mode, or should force TLS on those exports
842 it is concerned about using SELECTIVE mode and TLS-only
843 exports. It is not possible to avoid downgrade attacks
844 on exports which may be served either via TLS or in plain
845 text unless the client insists on TLS.
847 ## Block size constraints
849 During transmission phase, several operations are constrained by the
850 export size sent by the final `NBD_OPT_EXPORT_NAME` or `NBD_OPT_GO`,
851 as well as by three block size constraints defined here (minimum
852 block, preferred block, and maximum payload).
854 If a client can honour server block size constraints (as set out below
855 and under `NBD_INFO_BLOCK_SIZE`), it SHOULD announce this during the
856 handshake phase by using `NBD_OPT_GO` (and `NBD_OPT_INFO` if used) with
857 an `NBD_INFO_BLOCK_SIZE` information request, and MUST use `NBD_OPT_GO`
858 rather than `NBD_OPT_EXPORT_NAME` (except in the case of a fallback
859 where the server did not support `NBD_OPT_INFO` or `NBD_OPT_GO`).
861 A server with block size constraints other than the default SHOULD
862 advertise the block size constraints during handshake phase via
863 `NBD_INFO_BLOCK_SIZE` in response to `NBD_OPT_INFO` or `NBD_OPT_GO`,
864 and MUST do so unless it has agreed on block size constraints via out
865 of band means.
867 Some servers are able to make optimizations, such as opening files
868 with `O_DIRECT`, if they know that the client will obey a particular
869 minimum block size, where it must fall back to safer but slower code
870 if the client might send unaligned requests. For that reason, if a
871 client issues an `NBD_OPT_GO` including an `NBD_INFO_BLOCK_SIZE`
872 information request, it MUST abide by the block size constraints it
873 receives. Clients MAY issue `NBD_OPT_INFO` with `NBD_INFO_BLOCK_SIZE` to
874 learn the server's constraints without committing to them.
876 If block size constraints have not been advertised or agreed on
877 externally, then a server SHOULD support a default minimum block size
878 of 1, a preferred block size of 2^12 (4,096), and a maximum
879 payload size that is at least 2^25 (33,554,432) (even if the export
880 size is smaller); while a client desiring maximum interoperability
881 SHOULD constrain its requests to a minimum block size of 2^9 (512),
882 and limit `NBD_CMD_READ` and `NBD_CMD_WRITE` commands to a maximum
883 payload size of 2^25 (33,554,432).  A server that wants to enforce block
884 sizes other than the defaults specified here MAY refuse to go into
885 transmission phase with a client that uses `NBD_OPT_EXPORT_NAME` (via
886 a hard disconnect) or which uses `NBD_OPT_GO` without requesting
887 `NBD_INFO_BLOCK_SIZE` (via an error reply of
888 `NBD_REP_ERR_BLOCK_SIZE_REQD`); but servers SHOULD NOT refuse clients
889 that do not request sizing information when the server supports
890 default sizing or where sizing constraints can be agreed on
891 externally.  When allowing clients that did not negotiate sizing via
892 NBD, a server that enforces stricter block size constraints than the
893 defaults MUST cleanly error commands that fall outside the constraints
894 without corrupting data; even so, enforcing constraints in this manner
895 may limit interoperability.
897 A client MAY choose to operate as if tighter block size constraints
898 had been specified (for example, even when the server advertises the
899 default minimum block size of 1, a client may safely use a minimum
900 block size of 2^9 (512)).
902 The minimum block size represents the smallest addressable length and
903 alignment within the export, although writing to an area that small
904 may require the server to use a less-efficient read-modify-write
905 action.  If advertised, this value MUST be a power of 2, MUST NOT be
906 larger than 2^16 (65,536), and MAY be as small as 1 for an export
907 backed by a regular file, although the values of 2^9 (512) or 2^12
908 (4,096) are more typical for an export backed by a block device.  If a
909 server advertises a minimum block size, the advertised export size
910 SHOULD be an integer multiple of that block size, since otherwise, the
911 client would be unable to access the final few bytes of the export.
913 The preferred block size represents the minimum size at which aligned
914 requests will have efficient I/O, avoiding behaviour such as
915 read-modify-write.  If advertised, this MUST be a power of 2 at least
916 as large as the maximum of the minimum block size and 2^9 (512),
917 although larger values (such as 4,096, or even the minimum granularity
918 of a hole) are more typical.  The preferred block size MAY be larger
919 than the export size, in which case the client is unable to utilize
920 the preferred block size for that export.  The server MAY advertise an
921 export size that is not an integer multiple of the preferred block
922 size.
924 The maximum payload size represents the maximum payload length that
925 the server is willing to handle in one request from the client.  If
926 advertised, it MAY be something other than a power of 2, but MUST be
927 at least as large as the preferred block size, and SHOULD be at least
928 2^20 (1,048,576) if the export is that large.  Advertising a maximum
929 payload size of 0xffffffff is permitted when the server does not have
930 a fixed limit on client request payloads.  Typically, the advertised
931 maximum payload length is independent of the export size, even though
932 the actual payloads for read and write cannot successfully exceed the
933 constraints given by the export size and offset of a request.
934 Notwithstanding any maximum payload size advertised, either the server
935 or the client MAY initiate a hard disconnect if a payload length of
936 either a request or a reply would be large enough to be deemed a
937 denial of service attack; however, for maximum portability, any
938 payload not exceeding 2^25 (33,554,432) bytes SHOULD NOT be considered
939 a denial of service attack, even if that length is larger than the
940 advertised maximum payload size.
942 For commands that require a payload in either direction and where the
943 client controls the payload length (`NBD_CMD_WRITE`, or `NBD_CMD_READ`
944 with simple replies), the client MUST NOT request a length larger than
945 the maximum payload size. For replies where the payload length is
946 controlled by the server (`NBD_CMD_BLOCK_STATUS` without the flag
947 `NBD_CMD_FLAG_REQ_ONE`, or `NBD_CMD_READ` when structured replies or
948 extended headers are negotiated), the server MAY exceed the maximum
949 payload by the fixed amount of overhead required in the structured
950 reply (for example, a server that advertises a maximum payload of 2^25
951 bytes may return 2^25+8 payload bytes in a single
952 `NBD_REPLY_TYPE_OFFSET_DATA` chunk, rather than splitting the reply
953 across two chunks), although it MUST honor any additional payload
954 constraints documented for a particular command.  For commands that do
955 not require a payload in either direction (such as `NBD_CMD_TRIM` or
956 `NBD_CMD_WRITE_ZEROES`), the client MAY request an effect length
957 larger than the maximum payload size; the server SHOULD NOT
958 disconnect, but MAY reply with an `NBD_EOVERFLOW` or `NBD_EINVAL`
959 error if the oversize request would require too many server resources
960 when compared to the same command with an effect length limited to the
961 maximum payload size (such as an implementation of
962 `NBD_CMD_WRITE_ZEROES` that utilizes a scratch buffer).
964 Where a transmission request can have a nonzero *offset* and/or
965 *length* (such as `NBD_CMD_READ`, `NBD_CMD_WRITE`, or `NBD_CMD_TRIM`),
966 the client MUST ensure that *offset* and *length* are integer
967 multiples of any advertised minimum block size, and SHOULD use integer
968 multiples of any advertised preferred block size where possible.  For
969 those requests, the client MUST NOT use a *length* which, when added to
970 *offset*, would exceed the export size.  The server SHOULD report an
971 `NBD_EINVAL` error if the client's request is not aligned to advertised
972 minimum block size boundaries or would exceed the export size.
974 ## Metadata querying
976 It is often helpful for the client to be able to query the status of a
977 range of blocks. The nature of the status that can be queried is in
978 part implementation dependent. For instance, the status might
979 represent:
981 * in a sparse storage format, whether the relevant blocks are actually
982   present on the backing device for the export; or
984 * whether the relevant blocks are 'dirty'; some storage formats and
985   operations over such formats express a concept of data dirtiness.
986   Whether the operation is block device mirroring, incremental block
987   device backup or any other operation with a concept of data
988   dirtiness, they all share a need to provide a list of ranges that
989   this particular operation treats as dirty.
991 To provide such classes of information, the NBD protocol has a generic
992 framework for querying metadata; however, its use must first be
993 negotiated, and one or more metadata contexts must be selected.
995 The procedure works as follows:
997 - First, during negotiation, if the client wishes to query metadata
998   during transmission, the client MUST select one or more metadata
999   contexts with the `NBD_OPT_SET_META_CONTEXT` command. If needed, the
1000   client can use `NBD_OPT_LIST_META_CONTEXT` to list contexts that the
1001   server supports.  Most metadata contexts expose no more than 32 bits
1002   of information, but some metadata contexts have associated data that
1003   is 64 bits in length; using such contexts requires the client to
1004   first negotiate extended headers with `NBD_OPT_EXTENDED_HEADERS`.
1005 - During transmission, a client can then indicate interest in metadata
1006   for a given region by way of the `NBD_CMD_BLOCK_STATUS` command,
1007   where *offset* and *length* indicate the area of interest. On
1008   success, the server MUST respond with one status type reply chunk
1009   per metadata context selected during negotiation, where each reply
1010   chunk is a list of one or more consecutive extents for that context.
1011   Each extent comes with a *flags* field, the semantics of which are
1012   defined by the metadata context.
1014 The client's requested *length* is only a hint to the server, so the
1015 cumulative extent length contained in a chunk of the server's reply
1016 may be shorter or longer the original request.  When more than one
1017 metadata context was negotiated, the reply chunks for the different
1018 contexts of a single block status request need not have the same
1019 number of extents or cumulative extent length.
1021 In the request, the client may use the `NBD_CMD_FLAG_REQ_ONE` command
1022 flag to further constrain the server's reply so that each chunk
1023 contains exactly one extent whose length does not exceed the client's
1024 original *length*.
1026 A client MUST NOT use `NBD_CMD_BLOCK_STATUS` unless it selected a
1027 nonzero number of metadata contexts during negotiation, and used the
1028 same export name for the subsequent `NBD_OPT_GO` (or
1029 `NBD_OPT_EXPORT_NAME`). Servers SHOULD reply with `NBD_EINVAL` to clients
1030 sending `NBD_CMD_BLOCK_STATUS` without selecting at least one metadata
1031 context.
1033 The reply to a successful `NBD_CMD_BLOCK_STATUS` request MUST be sent
1034 via reply chunks; this implies that in order to use metadata querying,
1035 either structured replies or extended headers MUST be negotiated
1036 first.
1038 Metadata contexts are identified by their names. The name MUST consist
1039 of a namespace, followed by a colon, followed by a leaf-name.  The
1040 namespace must consist entirely of printable non-whitespace UTF-8
1041 characters other than colons, and be non-empty. The entire name
1042 (namespace, colon, and leaf-name) MUST follow the restrictions for
1043 strings as laid out earlier in this document.
1045 Namespaces MUST be consist of one of the following:
1046 - `base`, for metadata contexts defined by this document;
1047 - `nbd-server`, for metadata contexts defined by the implementation
1048   that accompanies this document (none currently);
1049 - `x-*`, where `*` can be replaced by an arbitrary string not
1050   containing colons, for local experiments. This SHOULD NOT be used
1051   by metadata contexts that are expected to be widely used.
1052 - A third-party namespace from the list below.
1054 Third-party implementations can register additional namespaces by
1055 simple request to the mailing-list. The following additional
1056 third-party namespaces are currently registered:
1057 * `qemu`, maintained by [qemu.org](https://gitlab.com/qemu-project/qemu/-/blob/master/docs/interop/nbd.txt)
1059 Save in respect of the `base:` namespace described below, this specification
1060 requires no specific semantics of metadata contexts, except that all the
1061 information they provide MUST be representable within the flags field as
1062 defined for `NBD_REPLY_TYPE_BLOCK_STATUS_EXT`. Likewise, save in respect of
1063 the `base:` namespace, the syntax of query strings is not specified by this
1064 document, other than the recommendation that the empty leaf-name makes
1065 sense as a wildcard for a client query during `NBD_OPT_LIST_META_CONTEXT`,
1066 but SHOULD NOT select any contexts during `NBD_OPT_SET_META_CONTEXT`.
1068 Server implementations SHOULD ensure the syntax for query strings they
1069 support and semantics for resulting metadata context is documented
1070 similarly to this document.
1072 ### The `base:` metadata namespace
1074 This standard defines exactly one metadata context; it is called
1075 `base:allocation`, and it provides information on the basic allocation
1076 status of extents (that is, whether they are allocated at all in a
1077 sparse file context).
1079 The query string within the `base:` metadata context can take one of
1080 two forms:
1082 * `base:` - the server MUST ignore this form during
1083   `NBD_OPT_SET_META_CONTEXT`, and MUST support this as a wildcard
1084   during `NBD_OPT_LIST_META_CONTEXT`, in which case the server's reply
1085   will contain a response for each supported metadata context within
1086   the `base:` namespace (currently just `base:allocation`, although a
1087   future revision of the standard might return multiple contexts); or
1088 * `base:[leaf-name]` to select `[leaf-name]` as a context leaf-name
1089   that might exist within the `base` namespace.  If a `[leaf-name]`
1090   requested by the client is not recognized, the server MUST ignore it
1091   rather than report an error.
1093 #### `base:allocation` metadata context
1095 The `base:allocation` metadata context is the basic "allocated at all"
1096 metadata context. If an extent is marked with `NBD_STATE_HOLE` at that
1097 context, this means that the given extent is not allocated in the
1098 backend storage, and that writing to the extent MAY result in the
1099 `NBD_ENOSPC` error. This supports sparse file semantics on the server
1100 side.  If a server supports the `base:allocation` metadata context,
1101 then writing to an extent which has `NBD_STATE_HOLE` clear MUST NOT
1102 fail with `NBD_ENOSPC` unless for reasons specified in the definition of
1103 another context.
1105 It defines the following flags for the flags field:
1107 - `NBD_STATE_HOLE` (bit 0): if set, the block represents a hole (and
1108   future writes to that area may cause fragmentation or encounter an
1109   `NBD_ENOSPC` error); if clear, the block is allocated or the server
1110   could not otherwise determine its status. Note that the use of
1111   `NBD_CMD_TRIM` is related to this status, but that the server MAY
1112   report a hole even where `NBD_CMD_TRIM` has not been requested, and
1113   also that a server MAY report that the block is allocated even where
1114   `NBD_CMD_TRIM` has been requested.
1115 - `NBD_STATE_ZERO` (bit 1): if set, the block contents read as all
1116   zeroes; if clear, the block contents are not known. Note that the
1117   use of `NBD_CMD_WRITE_ZEROES` is related to this status, but that
1118   the server MAY report zeroes even where `NBD_CMD_WRITE_ZEROES` has
1119   not been requested, and also that a server MAY report unknown
1120   content even where `NBD_CMD_WRITE_ZEROES` has been requested.
1122 It is not an error for a server to report that a region of the export
1123 has both `NBD_STATE_HOLE` set and `NBD_STATE_ZERO` clear. The contents
1124 of such an area are undefined, and a client reading such an area
1125 should make no assumption as to its contents or stability.
1127 For the `base:allocation` context, the remainder of the flags field is
1128 reserved. Servers SHOULD set it to all-zero; clients MUST ignore
1129 unknown flags.  Because fewer than 32 flags are defined, this metadata
1130 context does not require the use of `NBD_OPT_EXTENDED_HEADERS`, and a
1131 server can use `NBD_REPLY_TYPE_BLOCK_STATUS` to return results.
1133 ## Values
1135 This section describes the value and meaning of constants (other than
1136 magic numbers) in the protocol.
1138 When flags fields are specified, they are numbered in network byte
1139 order.
1141 ### Handshake phase
1143 #### Flag fields
1145 ##### Handshake flags
1147 This field of 16 bits is sent by the server after the `INIT_PASSWD` and
1148 the first magic number.
1150 - bit 0, `NBD_FLAG_FIXED_NEWSTYLE`; MUST be set by servers that
1151   support the fixed newstyle protocol
1152 - bit 1, `NBD_FLAG_NO_ZEROES`; if set, and if the client replies with
1153   `NBD_FLAG_C_NO_ZEROES` in the client flags field, the server MUST NOT
1154   send the 124 bytes of zero when the client ends negotiation with
1155   `NBD_OPT_EXPORT_NAME`.
1157 The server MUST NOT set any other flags, and SHOULD NOT change behaviour
1158 unless the client responds with a corresponding flag.  The server MUST
1159 NOT set any of these flags during oldstyle negotiation.
1161 It is unlikely that additional capability flags will be defined in the
1162 NBD protocol since this phase is susceptible to MitM downgrade attacks
1163 when using TLS.  Rather, additional features are best negotiated using
1164 protocol options.
1166 ##### Client flags
1168 This field of 32 bits is sent after initial connection and after
1169 receiving the handshake flags from the server.
1171 - bit 0, `NBD_FLAG_C_FIXED_NEWSTYLE`; SHOULD be set by clients that
1172   support the fixed newstyle protocol. Servers MAY choose to honour
1173   fixed newstyle from clients that didn't set this bit, but relying on
1174   this isn't recommended.
1175 - bit 1, `NBD_FLAG_C_NO_ZEROES`; MUST NOT be set if the server did not
1176   set `NBD_FLAG_NO_ZEROES`. If set, the server MUST NOT send the 124
1177   bytes of zeroes when the client ends negotiation with
1178   `NBD_OPT_EXPORT_NAME`.
1180 Clients MUST NOT set any other flags; the server MUST drop the TCP
1181 connection if the client sets an unknown flag, or a flag that does
1182 not match something advertised by the server.
1184 ##### Transmission flags
1186 This field of 16 bits is sent by the server after option haggling, or
1187 immediately after the handshake flags field in oldstyle negotiation.
1189 Many of these flags allow the server to expose to the client which
1190 features it understands (in which case they are documented below
1191 as "`NBD_FLAG_XXX` exposes feature `YYY`"). In each case, the server
1192 MAY set the flag for features it supports. The server MUST NOT set the
1193 flag for features it does not support. The client MUST NOT use a feature
1194 documented as 'exposed' by a flag unless that flag was set.
1196 The field has the following format:
1198 - bit 0, `NBD_FLAG_HAS_FLAGS`: MUST always be 1.
1199 - bit 1, `NBD_FLAG_READ_ONLY`: The server MAY set this flag to indicate
1200   to the client that the export is read-only (exports might be read-only
1201   in a manner undetectable to the server, for instance because of
1202   permissions). If this flag is set, the server MUST error subsequent
1203   write operations to the export.
1204 - bit 2, `NBD_FLAG_SEND_FLUSH`: exposes support for `NBD_CMD_FLUSH`.
1205 - bit 3, `NBD_FLAG_SEND_FUA`: exposes support for `NBD_CMD_FLAG_FUA`.
1206 - bit 4, `NBD_FLAG_ROTATIONAL`: the server MAY set this flag to 1 to
1207   inform the client that the export has the characteristics of a rotational
1208   medium, and the client MAY schedule I/O accesses in a manner corresponding
1209   to the setting of this flag.
1210 - bit 5, `NBD_FLAG_SEND_TRIM`: exposes support for `NBD_CMD_TRIM`.
1211 - bit 6, `NBD_FLAG_SEND_WRITE_ZEROES`: exposes support for
1212   `NBD_CMD_WRITE_ZEROES` and `NBD_CMD_FLAG_NO_HOLE`.
1213 - bit 7, `NBD_FLAG_SEND_DF`: do not fragment a structured or extended
1214   reply. The server MUST set this transmission flag to 1 if the
1215   `NBD_CMD_READ` request supports the `NBD_CMD_FLAG_DF` flag, and MUST
1216   leave this flag clear if neither structured replies nor extended
1217   headers have been negotiated. Clients MUST NOT set the
1218   `NBD_CMD_FLAG_DF` request flag unless this transmission flag is set.
1219 - bit 8, `NBD_FLAG_CAN_MULTI_CONN`: Indicates that the server operates
1220   entirely without cache, or that the cache it uses is shared among all
1221   connections to the given device. In particular, if this flag is
1222   present, then the effects of `NBD_CMD_FLUSH` and `NBD_CMD_FLAG_FUA`
1223   MUST be visible across all connections when the server sends its reply
1224   to that command to the client. In the absence of this flag, clients
1225   SHOULD NOT multiplex their commands over more than one connection to
1226   the export.
1227 - bit 9, `NBD_FLAG_SEND_RESIZE`: defined by the experimental `RESIZE`
1228   [extension](https://github.com/NetworkBlockDevice/nbd/blob/extension-resize/doc/proto.md).
1229 - bit 10, `NBD_FLAG_SEND_CACHE`: documents that the server understands
1230   `NBD_CMD_CACHE`; however, note that server implementations exist
1231   which support the command without advertising this bit, and
1232   conversely that this bit does not guarantee that the command will
1233   succeed or have an impact.
1234 - bit 11, `NBD_FLAG_SEND_FAST_ZERO`: allow clients to detect whether
1235   `NBD_CMD_WRITE_ZEROES` is faster than a corresponding write. The
1236   server MUST set this transmission flag to 1 if the
1237   `NBD_CMD_WRITE_ZEROES` request supports the `NBD_CMD_FLAG_FAST_ZERO`
1238   flag, and MUST set this transmission flag to 0 if
1239   `NBD_FLAG_SEND_WRITE_ZEROES` is not set. Servers MAY set this this
1240   transmission flag even if it will always use `NBD_ENOTSUP` failures for
1241   requests with `NBD_CMD_FLAG_FAST_ZERO` set (such as if the server
1242   cannot quickly determine whether a particular write zeroes request
1243   will be faster than a regular write). Clients MUST NOT set the
1244   `NBD_CMD_FLAG_FAST_ZERO` request flag unless this transmission flag
1245   is set.
1246 - bit 12, `NBD_FLAG_BLOCK_STATUS_PAYLOAD`: Indicates that the server
1247   understands the use of the `NBD_CMD_FLAG_PAYLOAD_LEN` flag to
1248   `NBD_CMD_BLOCK_STATUS` to allow the client to request that the
1249   server filters its response to a specific subset of negotiated
1250   metacontext ids passed in via a client payload, rather than the
1251   default of replying to all metacontext ids. Servers MUST NOT
1252   advertise this bit unless the client successfully negotiates
1253   extended headers via `NBD_OPT_EXTENDED_HEADERS`, and SHOULD NOT
1254   advertise this bit in response to `NBD_OPT_EXPORT_NAME` or
1255   `NBD_OPT_GO` if the client does not negotiate metacontexts with
1256   `NBD_OPT_SET_META_CONTEXT`; clients SHOULD NOT set the
1257   `NBD_CMD_FLAG_PAYLOAD_LEN` flag for `NBD_CMD_BLOCK_STATUS` unless
1258   this transmission flag is set.
1260 Clients SHOULD ignore unknown flags.
1262 #### Option types
1264 These values are used in the "option" field during the option haggling
1265 of the newstyle negotiation.
1267 * `NBD_OPT_EXPORT_NAME` (1)
1269     Choose the export which the client would like to use, end option
1270     haggling, and proceed to the transmission phase.
1272     Data: String, name of the export, as free-form text.
1273     The length of the name is determined from the option header. If the
1274     chosen export does not exist or requirements for the chosen export
1275     are not met (e.g., the client did not initiate TLS for an export
1276     where the server requires it), the server MUST terminate the
1277     session.
1279     A special, "empty", name (i.e., the length field is zero and no name
1280     is specified), is reserved for a "default" export, to be used in cases
1281     where explicitly specifying an export name makes no sense.
1283     This is the only valid option in nonfixed newstyle negotiation. A
1284     server which wishes to use any other option MUST support fixed
1285     newstyle.
1287     A major problem of this option is that it does not support the
1288     return of error messages to the client in case of problems. To
1289     remedy this, `NBD_OPT_GO` has been introduced (see below).  A
1290     client thus SHOULD use `NBD_OPT_GO` in preference to
1291     `NBD_OPT_EXPORT_NAME` but SHOULD fall back to
1292     `NBD_OPT_EXPORT_NAME` if `NBD_OPT_GO` is not supported (not
1293     falling back will prevent it from connecting to old servers).
1294     Additionally, a client that has used `NBD_OPT_EXTENDED_HEADERS` to
1295     negotiate extended headers MUST NOT use this option.
1297 * `NBD_OPT_ABORT` (2)
1299     The client desires to abort the negotiation and terminate the
1300     session. The server MUST reply with `NBD_REP_ACK`.
1302     The client SHOULD NOT send any additional data with the option;
1303     however, a server SHOULD ignore any data sent by the client rather
1304     than rejecting the request as invalid.
1306     Previous versions of this document were unclear on whether
1307     the server should send a reply to `NBD_OPT_ABORT`. Therefore
1308     the client SHOULD gracefully handle the server closing the
1309     connection after receiving an `NBD_OPT_ABORT` without it
1310     sending a reply. Similarly the server SHOULD gracefully handle
1311     the client sending an `NBD_OPT_ABORT` and closing the connection
1312     without waiting for a reply.
1314 * `NBD_OPT_LIST` (3)
1316     Return zero or more `NBD_REP_SERVER` replies, one for each export,
1317     followed by `NBD_REP_ACK` or an error (such as
1318     `NBD_REP_ERR_SHUTDOWN`). The server MAY omit entries from this
1319     list if TLS has not been negotiated, the server is operating in
1320     SELECTIVETLS mode, and the entry concerned is a TLS-only export.
1322     The client MUST NOT send any additional data with the option, and
1323     the server SHOULD reject a request that includes data with
1324     `NBD_REP_ERR_INVALID`.
1326 * `NBD_OPT_PEEK_EXPORT` (4)
1328     Was defined by the (withdrawn) experimental `PEEK_EXPORT` extension;
1329     not in use.
1331 * `NBD_OPT_STARTTLS` (5)
1333     The client wishes to initiate TLS.
1335     The client MUST NOT send any additional data with the option.  The
1336     server MUST either reply with `NBD_REP_ACK` after which point the
1337     connection is upgraded to TLS, or an error reply explicitly
1338     permitted by this document (for example, `NBD_REP_ERR_INVALID` if
1339     the client included data).
1341     When this command succeeds, the server MUST NOT preserve any
1342     negotiation state (such as a request for
1343     `NBD_OPT_STRUCTURED_REPLY` or `NBD_OPT_EXTENDED_HEADERS`, or
1344     metadata contexts from `NBD_OPT_SET_META_CONTEXT`) issued before
1345     this command.  A client SHOULD defer all stateful option requests
1346     until after it determines whether encryption is available.
1348     See the section on TLS above for further details.
1350 * `NBD_OPT_INFO` (6) and `NBD_OPT_GO` (7)
1352     Both options have identical formats for requests and replies. The only
1353     difference is that after a successful reply to `NBD_OPT_GO` (i.e. one
1354     or more `NBD_REP_INFO` then an `NBD_REP_ACK`), transmission mode is
1355     entered immediately.  Therefore these commands share common
1356     documentation.
1358     `NBD_OPT_INFO`: The client wishes to get details about an export
1359     with the given name for use in the transmission phase, but does
1360     not yet want to move to the transmission phase.  When successful,
1361     this option provides more details than `NBD_OPT_LIST`, but only
1362     for a single export name.
1364     `NBD_OPT_GO`: The client wishes to terminate the handshake phase
1365     and progress to the transmission phase. This client MAY issue this
1366     command after an `NBD_OPT_INFO`, or MAY issue it without a
1367     previous `NBD_OPT_INFO`.  `NBD_OPT_GO` can thus be used as an
1368     improved version of `NBD_OPT_EXPORT_NAME` that is capable of
1369     returning errors.
1371     Data (both commands):
1373     - 32 bits, length of name (unsigned); MUST be no larger than the
1374       option data length - 6
1375     - String: name of the export
1376     - 16 bits, number of information requests
1377     - 16 bits x n - list of `NBD_INFO` information requests
1379     The client MAY list one or more items of specific information it
1380     is seeking in the list of information requests, or it MAY specify
1381     an empty list. The client MUST NOT include any information request
1382     in the list more than once. The server MUST ignore any information
1383     requests it does not understand. The server MAY reply to the
1384     information requests in any order. The server MAY ignore information
1385     requests that it does not wish to supply for policy reasons (other
1386     than `NBD_INFO_EXPORT`). Equally the client MAY refuse to negotiate
1387     if not supplied information it has requested. The server MAY send
1388     information requests back which are not explicitly requested, but
1389     the server MUST NOT assume that such information requests are
1390     understood and respected by the client unless the client explicitly
1391     asked for them. The client MUST ignore information replies it
1392     does not understand.
1394     If no name is specified (i.e. a zero length string is provided),
1395     this specifies the default export (if any), as with
1396     `NBD_OPT_EXPORT_NAME`.
1398     The server replies with a number of `NBD_REP_INFO` replies (as few
1399     as zero if an error is reported, at least one on success), then
1400     concludes the list of information with a final error reply or with
1401     a declaration of success, as follows:
1403     - `NBD_REP_ACK`: The server accepts the chosen export, and has
1404       completed providing information.  In this case, the server MUST
1405       send at least one `NBD_REP_INFO`, with an `NBD_INFO_EXPORT`
1406       information type.
1407     - `NBD_REP_ERR_UNKNOWN`: The chosen export does not exist on this
1408       server.  In this case, the server SHOULD NOT send `NBD_REP_INFO`
1409       replies.
1410     - `NBD_REP_ERR_TLS_REQD`: The server requires the client to
1411       initiate TLS before any revealing any further details about this
1412       export.  In this case, a FORCEDTLS server MUST NOT send
1413       `NBD_REP_INFO` replies, but a SELECTIVETLS server MAY do so if
1414       this is a TLS-only export.
1415     - `NBD_REP_ERR_BLOCK_SIZE_REQD`: The server requires the client to
1416       request block size constraints using `NBD_INFO_BLOCK_SIZE` prior
1417       to entering transmission phase, because the server will be using
1418       non-default block sizes constraints. The server MUST NOT send this
1419       error if block size constraints were requested with
1420       `NBD_INFO_BLOCK_SIZE` with the `NBD_OPT_INFO` or `NBD_OPT_GO`
1421       request. The server SHOULD NOT send this error if it is using
1422       default block size constraints or block size constraints
1423       negotiated out of band. A server sending an
1424       `NBD_REP_ERR_BLOCK_SIZE_REQD` error SHOULD ensure it first
1425       sends an `NBD_INFO_BLOCK_SIZE` information reply in order
1426       to help avoid a potentially unnecessary round trip.
1427     - `NBD_REP_ERR_EXT_HEADER_REQD`: The server requires the client to
1428       utilize extended headers.  While this may make it easier to
1429       implement a server with fewer considerations for backwards
1430       compatibility, it limits connections to only recent clients.
1432     Additionally, if TLS has not been initiated, the server MAY reply
1433     with `NBD_REP_ERR_TLS_REQD` (instead of `NBD_REP_ERR_UNKNOWN`) to
1434     requests for exports that are unknown. This is so that clients
1435     that have not initiated TLS cannot enumerate exports.  A
1436     SELECTIVETLS server that chooses to hide unknown exports in this
1437     manner SHOULD NOT send `NBD_REP_INFO` replies for a TLS-only
1438     export.
1440     For backwards compatibility, clients SHOULD be prepared to also
1441     handle `NBD_REP_ERR_UNSUP` by falling back to using
1442     `NBD_OPT_EXPORT_NAME`; however, if the server has acknowledged a
1443     client's request for `NBD_OPT_EXTENDED_HEADERS`, the server MUST
1444     NOT fail `NBD_OPT_GO` with `NBD_REP_ERR_UNSUP`.
1446     Other errors (such as `NBD_REP_ERR_SHUTDOWN`) are also possible,
1447     as permitted elsewhere in this document, with no constraints on
1448     the number of preceding `NBD_REP_INFO`.
1450     If there are no intervening option requests between a successful
1451     `NBD_OPT_INFO` (that is, one where the reply ended with a final
1452     `NBD_REP_ACK`) and an `NBD_OPT_GO` with the same parameters
1453     (including the list of information items requested), then
1454     the server MUST reply with the same set of information, such as
1455     transmission flags in the `NBD_INFO_EXPORT` reply, although the
1456     ordering of the intermediate `NBD_REP_INFO` messages MAY differ.
1457     Otherwise, due to the intervening option requests or the use of
1458     different parameters, the server MAY send different data in the
1459     successful response, and/or MAY fail the second request.
1461     The reply to an `NBD_OPT_GO` is identical to the reply to
1462     `NBD_OPT_INFO` save that if the reply indicates success (i.e. ends
1463     with `NBD_REP_ACK`), the client and the server both immediately
1464     enter the transmission phase. The server MUST NOT send any zero
1465     padding bytes after the `NBD_REP_ACK` data, whether or not the
1466     client negotiated the `NBD_FLAG_C_NO_ZEROES` flag. The client MUST
1467     NOT send further option requests unless the final reply from the
1468     server indicates an error.
1470 * `NBD_OPT_GO` (7)
1472     See above under `NBD_OPT_INFO`.
1474 * `NBD_OPT_STRUCTURED_REPLY` (8)
1476     The client wishes to use structured replies during the
1477     transmission phase.  The client MUST NOT send any additional data
1478     with the option, and the server SHOULD reject a request that
1479     includes data with `NBD_REP_ERR_INVALID`.
1481     The server replies with the following, or with an error permitted
1482     elsewhere in this document:
1484     - `NBD_REP_ACK`: Structured replies have been negotiated; the
1485       server MUST use structured replies to the `NBD_CMD_READ`
1486       transmission request.  Other extensions that require structured
1487       replies may now be negotiated.
1488     - `NBD_REP_ERR_EXT_HEADER_REQD`: The client has already
1489       successfully negotiated extended headers, which takes precedence
1490       over this option.
1491     - For backwards compatibility, clients SHOULD be prepared to also
1492       handle `NBD_REP_ERR_UNSUP`; in this case, no structured replies
1493       will be sent.
1495     It is envisioned that future extensions will add other new
1496     requests that may require a data payload in the reply.  A server
1497     that supports such extensions SHOULD NOT advertise those
1498     extensions until the client has negotiated either structured
1499     replies or extended headers; and a client MUST NOT make use of
1500     those extensions without first enabling support for reply
1501     payloads.
1503     If the client requests `NBD_OPT_STARTTLS` after this option, it
1504     MUST renegotiate structured replies and any other dependent
1505     extensions that it desires to use.
1507 * `NBD_OPT_LIST_META_CONTEXT` (9)
1509     Return a list of `NBD_REP_META_CONTEXT` replies, one per context,
1510     followed by an `NBD_REP_ACK` or an error.
1512     This option SHOULD NOT be requested unless structured replies or
1513     extended headers have been negotiated first. If a client attempts
1514     to do so, a server MAY send `NBD_REP_ERR_INVALID` or
1515     `NBD_REP_ERR_EXT_HEADER_REQD`.
1517     A server MAY support extension contexts that produce status values
1518     that require more than 32 bits.  The server MAY advertise such
1519     contexts even if the client has not yet negotiated extended
1520     headers, although it SHOULD then conclude the overall response
1521     with the `NBD_REP_ERR_EXT_HEADER_REQD` error to inform the client
1522     that extended headers are required to make full use of all
1523     contexts advertised.  However, since none of the contexts defined
1524     in the "base:" namespace provide more than 32 bits of status, a
1525     server MUST NOT use this failure mode when the response is limited
1526     to the "base:" namespace; nor may the server use this failure mode
1527     when the client has already negotiated extended headers.
1529     Data:
1530     - 32 bits, length of export name.  
1531     - String, name of export for which we wish to list metadata
1532       contexts.  
1533     - 32 bits, number of queries  
1534     - Zero or more queries, each being:  
1535        - 32 bits, length of query.  
1536        - String, query to list a subset of the available metadata
1537          contexts.  The syntax of this query is
1538          implementation-defined, except that it MUST start with a
1539          namespace and a colon.  
1541     For details on the query string, see the "Metadata querying"
1542     section; note that a namespace may document that a different set
1543     of queries are valid for `NBD_OPT_LIST_META_CONTEXT` than for
1544     `NBD_OPT_SET_META_CONTEXT`, such as when using an empty leaf-name
1545     for wildcarding.
1547     If the option request is syntactically invalid (such as a query
1548     length that would require reading beyond the original length given
1549     in the option header), the server MUST fail the request with
1550     `NBD_REP_ERR_INVALID`.  For requests that are semantically invalid
1551     (such as lacking the required colon that delimits the namespace,
1552     or using a leaf name that is invalid for a known namespace), the
1553     server MAY fail the request with `NBD_REP_ERR_INVALID`.  However,
1554     the server MUST ignore query strings belonging to an unknown
1555     namespace.  If none of the query strings find any metadata
1556     contexts, the server MUST send a single reply of type
1557     `NBD_REP_ACK`.
1559     The server MUST reply with a list of zero or more
1560     `NBD_REP_META_CONTEXT` replies, followed by either a final
1561     `NBD_REP_ACK` on success or by an error (for instance
1562     `NBD_REP_ERR_UNSUP` if the option is not supported).  If an error
1563     is returned, the client MUST disregard any context replies that
1564     may have been sent.
1566     If zero queries are sent, then the server MUST return all the
1567     metadata contexts that are available to the client to select on
1568     the given export.  However, this list may include wildcards that
1569     require a further `NBD_OPT_LIST_META_CONTEXT` with the wildcard as
1570     a query, rather than an actual context that is appropriate as a
1571     query to `NBD_OPT_SET_META_CONTEXT`, as set out below.  In this
1572     case, the server SHOULD NOT fail with `NBD_REP_ERR_TOO_BIG`.
1574     If one or more queries are sent, then the server MUST return those
1575     metadata contexts that are available to the client to select on
1576     the given export with `NBD_OPT_SET_META_CONTEXT`, and which match
1577     one or more of the queries given. The support of wildcarding
1578     within the leaf-name portion of the query string is dependent upon
1579     the namespace.  The server MAY send contexts in a different order
1580     than in the client's query.  In this case, the server MAY fail
1581     with `NBD_REP_ERR_TOO_BIG` if too many queries are requested.
1583     In either case, however, for any given namespace the server MAY,
1584     instead of exhaustively listing every matching context available
1585     to select (or every context available to select where no query is
1586     given), send sufficient context records back to allow a client
1587     with knowledge of the namespace to select any context.  This may
1588     be helpful where a client can construct algorithmic queries. For
1589     instance, a client might reply simply with the namespace with no
1590     leaf-name (e.g.  'x-FooBar:') or with a range of values (e.g.
1591     'x-ModifiedDate:20160310-20161214'). The semantics of such a reply
1592     are a matter for the definition of the namespace. However each
1593     namespace returned MUST begin with the relevant namespace,
1594     followed by a colon, and then other UTF-8 characters, with the
1595     entire string following the restrictions for strings set out
1596     earlier in this document.
1598     The metadata context ID in these replies is reserved and SHOULD be
1599     set to zero; clients MUST disregard it.
1601 * `NBD_OPT_SET_META_CONTEXT` (10)
1603     Change the set of active metadata contexts. Issuing this command
1604     replaces all previously-set metadata contexts (including when this
1605     command fails); clients must ensure that all metadata contexts
1606     they are interested in are selected with the final query that they
1607     sent.
1609     This option MUST NOT be requested unless structured replies or
1610     extended headers have been negotiated first. If a client attempts
1611     to do so, a server SHOULD send `NBD_REP_ERR_INVALID` or
1612     `NBD_REP_ERR_EXT_HEADER_REQD`.
1614     If a client requests a metadata context that utilizes 64-bit
1615     status, but has not yet negotiated extended headers, the server
1616     MUST either omit that context from its successful reply, or else
1617     fail the request with `NBD_REP_ERR_EXT_HEADER_REQD`.  The server
1618     MUST NOT use this failure for a client request that is limited to
1619     contexts in the "base:" namespace.
1621     A client MUST NOT send `NBD_CMD_BLOCK_STATUS` unless within the
1622     negotiation phase it sent `NBD_OPT_SET_META_CONTEXT` at least
1623     once, and where the final time it was sent, it referred to the
1624     same export name that was ultimately selected for transmission
1625     phase with no intervening `NBD_OPT_STARTTLS`, and where the server
1626     responded by returning least one metadata context without error.
1628     Data:
1629     - 32 bits, length of export name.  
1630     - String, name of export for which we wish to list metadata
1631       contexts.  
1632     - 32 bits, number of queries  
1633     - Zero or more queries, each being:  
1634        - 32 bits, length of query  
1635        - String, query to select metadata contexts. The syntax of this
1636          query is implementation-defined, except that it MUST start with a
1637          namespace and a colon.  
1639     If zero queries are sent, the server MUST select no metadata
1640     contexts.
1642     The server MAY return `NBD_REP_ERR_TOO_BIG` if a request seeks to
1643     select too many contexts. Otherwise the server MUST reply with a
1644     number of `NBD_REP_META_CONTEXT` replies, one for each selected
1645     metadata context, each with a unique metadata context ID, followed
1646     by `NBD_REP_ACK`. The server MAY ignore queries that do not select
1647     a single metadata context, and MAY return selected contexts in a
1648     different order than in the client's request.  The metadata
1649     context ID is transient and may vary across calls to
1650     `NBD_OPT_SET_META_CONTEXT`; clients MUST therefore treat the ID as
1651     an opaque value and not (for instance) cache it between
1652     connections. It is not an error if a `NBD_OPT_SET_META_CONTEXT`
1653     option does not select any metadata context, provided the client
1654     then does not attempt to issue `NBD_CMD_BLOCK_STATUS` commands.
1656 * `NBD_OPT_EXTENDED_HEADERS` (11)
1658     The client wishes to use extended headers during the transmission
1659     phase.  The client MUST NOT send any additional data with the
1660     option, and the server SHOULD reject a request that includes data
1661     with `NBD_REP_ERR_INVALID`.
1663     When successful, this option takes precedence over structured
1664     replies, and the server MUST also support `NBD_OPT_GO`.  A client
1665     MAY request structured replies first, although a server SHOULD
1666     support this option even if structured replies are not negotiated.
1667     Once extended headers are successfully requested, a client MUST
1668     NOT use `NBD_OPT_EXPORT_NAME`.
1670     It is envisioned that future extensions will add other new
1671     requests that support a data payload in the request or reply.  A
1672     server that supports such extensions SHOULD NOT advertise those
1673     extensions until the client has negotiated extended headers; and a
1674     client MUST NOT make use of those extensions without first
1675     enabling support for reply payloads.
1677     The server replies with the following, or with an error permitted
1678     elsewhere in this document:
1680     - `NBD_REP_ACK`: Extended headers have been negotiated; the client
1681       MUST use the 32-byte extended request header, with proper use of
1682       `NBD_CMD_FLAG_PAYLOAD_LEN` for all commands sending a payload;
1683       and the server MUST use the 32-byte extended reply header.
1684     - For backwards compatibility, clients SHOULD be prepared to also
1685       handle `NBD_REP_ERR_UNSUP`; in this case, only the compact
1686       transmission headers will be used.
1688     Note that a response of `NBD_REP_ERR_BLOCK_SIZE_REQD` does not
1689     make sense in response to this command, but a server MAY fail with
1690     that error for a later `NBD_OPT_GO` without a client request for
1691     `NBD_INFO_BLOCK_SIZE`, since the use of extended headers provides
1692     more incentive for a client to promise to obey block size
1693     constraints.
1695     If the client requests `NBD_OPT_STARTTLS` after this option, it
1696     MUST renegotiate extended headers.
1698 #### Option reply types
1700 These values are used in the "reply type" field, sent by the server
1701 during option haggling in the fixed newstyle negotiation.
1703 * `NBD_REP_ACK` (1)
1705     Will be sent by the server when it accepts the option and no further
1706     information is available, or when sending data related to the option
1707     (in the case of `NBD_OPT_LIST`) has finished. No data.
1709 * `NBD_REP_SERVER` (2)
1711     A description of an export. Data:
1713     - 32 bits, length of name (unsigned); MUST be no larger than the
1714       reply packet header length - 4
1715     - String, name of the export, as expected by `NBD_OPT_EXPORT_NAME`,
1716       `NBD_OPT_INFO`, or `NBD_OPT_GO`
1717     - If length of name < (reply packet header length - 4), then the
1718       rest of the data contains some implementation-specific details
1719       about the export. This is not currently implemented, but future
1720       versions of nbd-server may send along some details about the
1721       export. Therefore, unless explicitly documented otherwise by a
1722       particular client request, this field is defined to be a string
1723       suitable for direct display to a human being.
1725 * `NBD_REP_INFO` (3)
1727     A detailed description about an aspect of an export.  The response
1728     to `NBD_OPT_INFO` and `NBD_OPT_GO` includes zero or more of these
1729     messages prior to a final error reply, or at least one before an
1730     `NBD_REP_ACK` reply indicating success.  The server MUST send an
1731     `NBD_INFO_EXPORT` information type at some point before sending an
1732     `NBD_REP_ACK`, so that `NBD_OPT_GO` can provide a superset of the
1733     information given in response to `NBD_OPT_EXPORT_NAME`; all other
1734     information types are optional.  A particular information type
1735     SHOULD only appear once for a given export unless documented
1736     otherwise.
1738     A client MUST NOT rely on any particular ordering amongst the
1739     `NBD_OPT_INFO` replies, and MUST ignore information types that it
1740     does not recognize.
1742     The acceptable values for the header *length* field are determined
1743     by the information type, and includes the 2 bytes for the type
1744     designator, in the following general layout:
1746     - 16 bits, information type (e.g. `NBD_INFO_EXPORT`)  
1747     - *length - 2* bytes, information payload  
1749     The following information types are defined:
1751     * `NBD_INFO_EXPORT` (0)
1753       Mandatory information before a successful completion of
1754       `NBD_OPT_INFO` or `NBD_OPT_GO`.  Describes the same information
1755       that is sent in response to the older `NBD_OPT_EXPORT_NAME`,
1756       except that there are no trailing zeroes whether or not
1757       `NBD_FLAG_C_NO_ZEROES` was negotiated.  *length* MUST be 12, and
1758       the reply payload is interpreted as follows:
1760       - 16 bits, `NBD_INFO_EXPORT`  
1761       - 64 bits, size of the export in bytes (unsigned)  
1762       - 16 bits, transmission flags  
1764     * `NBD_INFO_NAME` (1)
1766       Represents the server's canonical name of the export. The name
1767       MAY differ from the name presented in the client's option
1768       request, and the information item MAY be omitted if the client
1769       option request already used the canonical name.  This
1770       information type represents the same name that would appear in
1771       the name portion of an `NBD_REP_SERVER` in response to
1772       `NBD_OPT_LIST`. The *length* MUST be at least 2, and the reply
1773       payload is interpreted as:
1775       - 16 bits, `NBD_INFO_NAME`  
1776       - String: name of the export, *length - 2* bytes  
1778     * `NBD_INFO_DESCRIPTION` (2)
1780       A description of the export, suitable for direct display to the
1781       human being.  This information type represents the same optional
1782       description that may appear after the name portion of an
1783       `NBD_REP_SERVER` in response to `NBD_OPT_LIST`. The *length*
1784       MUST be at least 2, and the reply payload is interpreted as:
1786       - 16 bits, `NBD_INFO_DESCRIPTION`  
1787       - String: description of the export, *length - 2* bytes  
1789     * `NBD_INFO_BLOCK_SIZE` (3)
1791       Represents the server's advertised block size constraints; see the
1792       "Block size constraints" section for more details on what these
1793       values represent, and on constraints on their values.  The server
1794       MUST send this info if it is requested and it intends to enforce
1795       block size constraints other than the defaults. After
1796       sending this information in response to an `NBD_OPT_GO` in which
1797       the client specifically requested `NBD_INFO_BLOCK_SIZE`, the server
1798       can legitimately assume that any client that continues the session
1799       will support the block size constraints supplied (note that this
1800       assumption cannot be made solely on the basis of an `NBD_OPT_INFO`
1801       with an `NBD_INFO_BLOCK_SIZE` request, or an `NBD_OPT_GO` without
1802       an explicit `NBD_INFO_BLOCK_SIZE` request). The *length* MUST be 14,
1803       and the reply payload is interpreted as:
1805       - 16 bits, `NBD_INFO_BLOCK_SIZE`  
1806       - 32 bits, minimum block size  
1807       - 32 bits, preferred block size  
1808       - 32 bits, maximum payload size  
1810 * `NBD_REP_META_CONTEXT` (4)
1812     A description of a metadata context. Data:
1814     - 32 bits, NBD metadata context ID.
1815     - String, name of the metadata context. This is not required to be
1816       a human-readable string, but it MUST be valid UTF-8 data.
1818 There are a number of error reply types, all of which are denoted by
1819 having bit 31 set. All error replies MAY have some data set, in which
1820 case that data is an error message string suitable for display to the user.
1822 * `NBD_REP_ERR_UNSUP` (2^31 + 1)
1824     The option sent by the client is unknown by this server
1825     implementation (e.g., because the server is too old, or from another
1826     source).
1828 * `NBD_REP_ERR_POLICY` (2^31 + 2)
1830     The option sent by the client is known by this server and
1831     syntactically valid, but server-side policy forbids the server to
1832     allow the option (e.g., the client sent `NBD_OPT_LIST` but server
1833     configuration has that disabled)
1835 * `NBD_REP_ERR_INVALID` (2^31 + 3)
1837     The option sent by the client is known by this server, but was
1838     determined by the server to be syntactically or semantically
1839     invalid. For instance, the client sent an `NBD_OPT_LIST` with
1840     nonzero data length, or the client sent a second
1841     `NBD_OPT_STARTTLS` after TLS was already negotiated.
1843 * `NBD_REP_ERR_PLATFORM` (2^31 + 4)
1845     The option sent by the client is not supported on the platform on
1846     which the server is running, or requires compile-time options that
1847     were disabled, e.g., upon trying to use TLS.
1849 * `NBD_REP_ERR_TLS_REQD` (2^31 + 5)
1851     The server is unwilling to continue negotiation unless TLS is
1852     initiated first. In the case of `NBD_OPT_INFO` and `NBD_OPT_GO`
1853     this unwillingness MAY (depending on the TLS mode) be limited
1854     to the export in question. See the section on TLS above for
1855     further details.
1857 * `NBD_REP_ERR_UNKNOWN` (2^31 + 6)
1859     The requested export is not available.
1861 * `NBD_REP_ERR_SHUTDOWN` (2^31 + 7)
1863     The server is unwilling to continue negotiation as it is in the
1864     process of being shut down.
1866 * `NBD_REP_ERR_BLOCK_SIZE_REQD` (2^31 + 8)
1868     The server is unwilling to enter transmission phase for a given
1869     export unless the client first acknowledges (via
1870     `NBD_INFO_BLOCK_SIZE`) that it will obey non-default block sizing
1871     requirements.
1873 * `NBD_REP_ERR_TOO_BIG` (2^31 + 9)
1875     The request or the reply is too large to process.
1877 * `NBD_REP_ERR_EXT_HEADER_REQD` (2^31 + 10)
1879     The server is unwilling to proceed with the given request (such as
1880     `NBD_OPT_GO` or `NBD_OPT_SET_META_CONTEXT`) for a client that has
1881     not yet requested extended headers (via
1882     `NBD_OPT_EXTENDED_HEADERS`).  This error is also used to indicate
1883     that the server is unable to downgrade to structured replies (via
1884     `NBD_OPT_STRUCTURED_REPLY`) if extended headers have already been
1885     enabled.
1887 ### Transmission phase
1889 #### Flag fields
1891 ##### Command flags
1893 This field of 16 bits is sent by the client with every request and provides
1894 additional information to the server to execute the command. Refer to
1895 the "Request types" section below for more details about how a given flag
1896 affects a particular command.  Clients MUST NOT set a command flag bit
1897 that is not documented for the particular command; and whether a flag is
1898 valid may depend on negotiation during the handshake phase.
1900 - bit 0, `NBD_CMD_FLAG_FUA`; This flag is valid for all commands, provided
1901   `NBD_FLAG_SEND_FUA` has been negotiated, in which case the server MUST
1902   accept all commands with this bit set (even by ignoring the bit). The
1903   client SHOULD NOT set this bit unless the command has the potential of
1904   writing data (current commands are `NBD_CMD_WRITE`, `NBD_CMD_WRITE_ZEROES`
1905   and `NBD_CMD_TRIM`), however note that existing clients are known to set this
1906   bit on other commands. Subject to that, and provided `NBD_FLAG_SEND_FUA`
1907   is negotiated, the client MAY set this bit on all, no or some commands
1908   as it wishes (see the section on Ordering of messages and writes for
1909   details). If the server receives a command with `NBD_CMD_FLAG_FUA`
1910   set it MUST NOT send its reply to that command until all write
1911   operations (if any) associated with that command have been
1912   completed and persisted to non-volatile storage. If the command does
1913   not in fact write data (for instance on an `NBD_CMD_TRIM` in a situation
1914   where the command as a whole is ignored), the server MAY ignore this bit
1915   being set on such a command.
1916 - bit 1, `NBD_CMD_FLAG_NO_HOLE`; valid during `NBD_CMD_WRITE_ZEROES`.
1917   SHOULD be set to 1 if the client wants to ensure that the server does
1918   not create a hole. The client MAY send `NBD_CMD_FLAG_NO_HOLE` even
1919   if `NBD_FLAG_SEND_TRIM` was not set in the transmission flags field.
1920   The server MUST support the use of this flag if it advertises
1921   `NBD_FLAG_SEND_WRITE_ZEROES`.
1922 - bit 2, `NBD_CMD_FLAG_DF`; the "don't fragment" flag, valid during
1923   `NBD_CMD_READ`.  SHOULD be set to 1 if the client requires the
1924   server to send at most one content chunk in reply.  MUST NOT be set
1925   unless the transmission flags include `NBD_FLAG_SEND_DF`.  Use of
1926   this flag MAY trigger an `NBD_EOVERFLOW` error chunk, if the request
1927   length is too large.
1928 - bit 3, `NBD_CMD_FLAG_REQ_ONE`; valid during
1929   `NBD_CMD_BLOCK_STATUS`. If set, the client is interested in only one
1930   extent per metadata context. If this flag is present, the server
1931   MUST NOT send metadata on more than one extent in the reply. Client
1932   implementers should note that using this flag on multiple contiguous
1933   requests is likely to be inefficient.
1934 - bit 4, `NBD_CMD_FLAG_FAST_ZERO`; valid during
1935   `NBD_CMD_WRITE_ZEROES`. If set, but the server cannot perform the
1936   write zeroes any faster than it would for an equivalent
1937   `NBD_CMD_WRITE`, then the server MUST fail quickly with an error of
1938   `NBD_ENOTSUP`. The client MUST NOT set this unless the server advertised
1939   `NBD_FLAG_SEND_FAST_ZERO`.
1940 - bit 5, `NBD_CMD_FLAG_PAYLOAD_LEN`; only valid if extended headers
1941   were negotiated via `NBD_OPT_EXTENDED_HEADERS`.  If set, the
1942   *length* field of the header describes a (nonzero) payload length of
1943   data to be sent alongside the header; if the flag is clear, the
1944   header *length* is instead an effect length for the command's
1945   interaction with the export, and there is no payload after the
1946   header.  With extended headers, the flag MUST be set for
1947   `NBD_CMD_WRITE` (as the write command always sends a payload of the
1948   bytes to be written); for other commands, the flag will trigger an
1949   `NBD_EINVAL` error unless the command documents an optional payload
1950   form for the command and the server has implemented that form (an
1951   example being `NBD_CMD_BLOCK_STATUS` providing a payload form for
1952   restricting the response to a particular metacontext id, when the
1953   server advertises `NBD_FLAG_BLOCK_STATUS_PAYLOAD`).
1955 ##### Structured and extended header reply flags
1957 This field of 16 bits is sent by the server as part of every
1958 structured or extended header reply.
1960 - bit 0, `NBD_REPLY_FLAG_DONE`; the server MUST clear this bit if
1961   more structured reply chunks will be sent for the same client
1962   request, and MUST set this bit if this is the final reply.  This
1963   bit MUST always be set for the `NBD_REPLY_TYPE_NONE` chunk,
1964   although any other chunk type can also be used as the final
1965   chunk.
1967 The server MUST NOT set any other flags without first negotiating
1968 the extension with the client, unless the client can usefully
1969 react to the response without interpreting the flag (for instance
1970 if the flag is some form of hint).  Clients MUST ignore
1971 unrecognized flags.
1973 #### Structured reply types
1975 These values are used in the "type" field of a structured reply, and
1976 when extended headers are negotiated.  Some chunk types can
1977 additionally be categorized by role, such as *error chunks*, *content
1978 chunks*, or *status chunks*.  Each type determines how to interpret
1979 the "length" bytes of payload.  If the client receives an unknown or
1980 unexpected type, other than an *error chunk*, it MUST initiate a hard
1981 disconnect.  A server MUST NOT send a chunk where any variable-length
1982 portion of the chunk is larger than any advertised maximum payload
1983 size (however, the overall chunk may exceed the maximum payload by the
1984 small amount of fixed-length overhead inherent in the chunk type).
1986 * `NBD_REPLY_TYPE_NONE` (0)
1988   *length* MUST be 0 (and the payload field omitted).  This chunk
1989   type MUST always be used with the `NBD_REPLY_FLAG_DONE` bit set
1990   (that is, it may appear at most once in a structured reply, and
1991   is only useful as the final reply chunk).  If no earlier error
1992   chunks were sent, then this type implies that the overall client
1993   request is successful.  Valid as a reply to any request.
1995 * `NBD_REPLY_TYPE_OFFSET_DATA` (1)
1997   This chunk type is in the content chunk category.  *length* MUST be
1998   at least 9.  It represents the contents of *length - 8* bytes of the
1999   export, starting at the absolute *offset* from the start of the
2000   export.  The data MUST lie within the bounds of the original offset
2001   and length of the client's request, and MUST NOT overlap with the
2002   bounds of any earlier content chunk or error chunk in the same
2003   reply.  This chunk MAY be used more than once in a reply, unless the
2004   `NBD_CMD_FLAG_DF` flag was set.  Valid as a reply to `NBD_CMD_READ`.
2006   The payload is structured as:
2008   64 bits: offset (unsigned)  
2009   *length - 8* bytes: data  
2011 * `NBD_REPLY_TYPE_OFFSET_HOLE` (2)
2013   This chunk type is in the content chunk category.  *length* MUST be
2014   exactly 12.  It represents that the contents of *hole size* bytes,
2015   starting at the absolute *offset* from the start of the export, read
2016   as all zeroes.  The hole MUST lie within the bounds of the original
2017   offset and length of the client's request, and MUST NOT overlap with
2018   the bounds of any earlier content chunk or error chunk in the same
2019   reply.  This chunk MAY be used more than once in a reply, unless the
2020   `NBD_CMD_FLAG_DF` flag was set.  Valid as a reply to `NBD_CMD_READ`.
2022   The payload is structured as:
2024   64 bits: offset (unsigned)  
2025   32 bits: hole size (unsigned, MUST be nonzero)  
2027 * `NBD_REPLY_TYPE_OFFSET_HOLE_EXT` (3)
2029   This chunk type is in the content chunk category.  *length* MUST be
2030   exactly 16.  The semantics of this chunk mirror those of
2031   `NBD_REPLY_TYPE_OFFSET_HOLE`, other than the use of a larger *hole
2032   size* field.  This chunk type MUST NOT be used unless extended
2033   headers were negotiated with `NBD_OPT_EXTENDED_HEADERS`.
2035   The payload is structured as:
2037   64 bits: offset (unsigned)  
2038   64 bits: hole size (unsigned, MUST be nonzero)  
2040   Note that even though extended headers are in use, a server may
2041   enforce a maximum effect length on `NBD_CMD_READ` comparable to an
2042   advertised maximum payload length (rather than having to split a
2043   large effect length over multiple chunks); in this case, no valid
2044   `NBD_CMD_READ` will have an effect *length* large enough to require
2045   the use of this chunk type.  However, a client using extended
2046   headers MUST be prepared for the server to use either the compact or
2047   extended chunk type.
2049 * `NBD_REPLY_TYPE_BLOCK_STATUS` (5)
2051   This chunk type is in the status chunk category.  *length* MUST be
2052   4 + (a positive integer multiple of 8).  This reply represents a
2053   series of consecutive block descriptors where the sum of the length
2054   fields within the descriptors is subject to further constraints
2055   documented below.  A successful block status request MUST have
2056   exactly one status chunk per negotiated metadata context ID.
2058   The payload starts with:
2060   32 bits, metadata context ID  
2062   and is followed by a list of one or more descriptors, each with this
2063   layout:
2065   32 bits, length of the extent to which the status below
2066      applies (unsigned, MUST be nonzero)  
2067   32 bits, status flags  
2069   If the client used the `NBD_CMD_FLAG_REQ_ONE` flag in the request,
2070   then every reply chunk MUST contain exactly one descriptor, and that
2071   descriptor MUST NOT exceed the *length* of the original request.  If
2072   the client did not use the flag, and the server replies with N
2073   extents, then the sum of the *length* fields of the first N-1
2074   extents (if any) MUST be less than the requested length, while the
2075   *length* of the final extent MAY result in a sum larger than the
2076   original requested length, if the server has that information anyway
2077   as a side effect of reporting the status of the requested region.
2078   When multiple metadata contexts are negotiated, the reply chunks for
2079   the different contexts need not have the same number of extents or
2080   cumulative extent length.
2082   Servers SHOULD NOT send more than 2^20 extents in a single reply
2083   chunk; in other words, the size of
2084   `NBD_REPLY_TYPE_BLOCK_STATUS` should not be more than 4 + 8*2^20
2085   (8,388,612 bytes), even if this requires that the server truncate
2086   the response in relation to the *length* requested by the client.
2088   Even if the client did not use the `NBD_CMD_FLAG_REQ_ONE` flag in
2089   its request, the server MAY return fewer descriptors in the reply
2090   than would be required to fully specify the whole range of requested
2091   information to the client, if looking up the information would be
2092   too resource-intensive for the server, so long as at least one
2093   extent is returned.  Servers should however be aware that most
2094   client implementations will likely follow up with a request for
2095   extent information at the first offset not covered by a
2096   reduced-length reply.
2098   For an extension metadata context that documents that the status
2099   value may potentially occupy 64 bits, a server MUST NOT use this
2100   reply type unless the most-significant 32 bits of all *status*
2101   values included in this reply are all zeroes.  Note that if the
2102   client did not negotiate extended headers, then the server already
2103   guaranteed during the handshake phase that no metadata contexts
2104   utilizing a 64-bit status value were negotiated.
2106 * `NBD_REPLY_TYPE_BLOCK_STATUS_EXT` (6)
2108   This chunk type is in the status chunk category.  *length* MUST be
2109   8 + (a positive multiple of 16).  The semantics of this chunk mirror
2110   those of `NBD_REPLY_TYPE_BLOCK_STATUS`, other than the use of a
2111   larger *extent length* field and a 64-bit *status* field, and the
2112   addition of a *descriptor count* field that can be used for easier
2113   client processing.  This chunk type MUST NOT be used unless extended
2114   headers were negotiated with `NBD_OPT_EXTENDED_HEADERS`.
2116   If the *descriptor count* field contains 0, the number of subsequent
2117   descriptors is determined solely by the *length* field of the reply
2118   header.  However, the server MAY populate the *descriptor count*
2119   field with the number of descriptors that follow; when doing this,
2120   the server MUST ensure that the header *length* is equal to
2121   *descriptor count* * 16 + 8.
2123   The payload starts with:
2125   32 bits, metadata context ID  
2126   32 bits, descriptor count  
2128   and is followed by a list of one or more descriptors, each with this
2129   layout:
2131   64 bits, length of the extent to which the status below
2132      applies (unsigned, MUST be nonzero)  
2133   64 bits, status flags  
2135   Note that when extended headers are in use, the client MUST be
2136   prepared for the server to use either the compact or extended chunk
2137   type for metadata contexts, regardless of whether the client's
2138   hinted effect length was more or less than 32 bits; but the server
2139   MUST use exactly one of the two chunk types per negotiated
2140   metacontext ID.  However, the server MUST use the extended chunk
2141   type when responding to an extension metadata context that utilizes
2142   a 64-bit status code where the resulting *status* value is not
2143   representable in 32 bits.  For metadata contexts that only return a
2144   32-bit status (including all contexts in the "base:" namespace), the
2145   most-significant 32 bits of *status* MUST be all zeroes.
2147 All error chunk types have bit 15 set, and begin with the same
2148 *error*, *message length*, and optional *message* fields as
2149 `NBD_REPLY_TYPE_ERROR`.  If nonzero, *message length* indicates
2150 that an optional error string message appears next, suitable for
2151 display to a human user.  The header *length* then covers any
2152 remaining structured fields at the end.
2154 * `NBD_REPLY_TYPE_ERROR` (2^15 + 1)
2156   This chunk type is in the error chunk category.  *length* MUST
2157   be at least 6.  This chunk represents that an error occurred,
2158   and the client MAY NOT make any assumptions about partial
2159   success. This type SHOULD NOT be used more than once in a
2160   structured reply.  Valid as a reply to any request.  Note that
2161   *message length* MUST NOT exceed the 4096 bytes string length limit,
2162   and therefore there is no need for a counterpart extended-length
2163   error chunk type.
2165   The payload is structured as:
2167   32 bits: error (MUST be nonzero)  
2168   16 bits: message length (no more than header *length* - 6)  
2169   *message length* bytes: optional string suitable for
2170     direct display to a human being  
2172 * `NBD_REPLY_TYPE_ERROR_OFFSET` (2^15 + 2)
2174   This chunk type is in the error chunk category.  *length* MUST
2175   be at least 14.  This reply represents that an error occurred at
2176   a given offset, which MUST lie within the original offset and
2177   length of the request; the client can use this offset to
2178   determine if request had any partial success.  This chunk type
2179   MAY appear multiple times in a structured reply, although the
2180   same offset SHOULD NOT be repeated.  Likewise, if content chunks
2181   were sent earlier in the structured reply, the server SHOULD NOT
2182   send multiple distinct offsets that lie within the bounds of a
2183   single content chunk.  Valid as a reply to `NBD_CMD_READ`,
2184   `NBD_CMD_WRITE`, `NBD_CMD_TRIM`, `NBD_CMD_CACHE`,
2185   `NBD_CMD_WRITE_ZEROES`, and `NBD_CMD_BLOCK_STATUS`.
2187   The payload is structured as:
2189   32 bits: error (MUST be nonzero)  
2190   16 bits: message length (no more than header *length* - 14)  
2191   *message length* bytes: optional string suitable for
2192      direct display to a human being  
2193   64 bits: offset (unsigned)  
2195 If the client receives an unknown or unexpected type with bit 15
2196 set, it MUST consider the current reply as errored, but MAY
2197 continue transmission unless it detects that *message length* is
2198 too large to fit within the *length* specified by the header.  For
2199 all other messages with unknown or unexpected type or inconsistent
2200 contents, the client MUST initiate a hard disconnect.
2202 #### Request types
2204 The following request types exist:
2206 * `NBD_CMD_READ` (0)
2208     A read request. Length and offset define the data to be read. The
2209     server MUST reply with either a simple reply or a structured
2210     reply, according to whether structured replies
2211     (`NBD_OPT_STRUCTURED_REPLY`) or extended headers
2212     (`NBD_OPT_EXTENDED_HEADERS`) were negotiated.  The client SHOULD
2213     NOT request a read length of 0; the behavior of a server on such a
2214     request is unspecified although the server SHOULD NOT disconnect.
2216     *Simple replies*
2218     If neither structured replies nor extended headers were
2219     negotiated, then a read request MUST always be answered by a
2220     simple reply, as documented above (using magic 0x67446698
2221     `NBD_SIMPLE_REPLY_MAGIC`, and containing length bytes of data
2222     according to the client's request), which in turn means any client
2223     request with a length larger than the maximum payload size will
2224     fail.
2226     If an error occurs, the server SHOULD set the appropriate error code
2227     in the error field. The server MAY then initiate a hard disconnect.
2228     If it chooses not to, it MUST NOT send any payload for this request.
2230     If an error occurs while reading after the server has already sent
2231     out the reply header with an error field set to zero (i.e.,
2232     signalling no error), the server MUST immediately initiate a
2233     hard disconnect; it MUST NOT send any further data to the client.
2235     *Structured replies*
2237     If structured replies or extended headers are negotiated, then a
2238     read request MUST result in a reply with one or more structured
2239     chunks (each using `NBD_STRUCTURED_REPLY_MAGIC` or
2240     `NBD_EXTENDED_REPLY_MAGIC` according to what was negotiated),
2241     where the final chunk has the flag `NBD_REPLY_FLAG_DONE`, and with
2242     the following additional constraints.
2244     The server MAY split the reply into any number of content chunks
2245     (`NBD_REPLY_TYPE_OFFSET_DATA` and `NBD_REPLY_TYPE_OFFSET_HOLE` for
2246     structured replies, additionally `NBD_REPLY_TYPE_OFFSET_HOLE_EXT`
2247     for extended headers); each chunk MUST describe at least one byte,
2248     although to minimize overhead, the server SHOULD use chunks with
2249     lengths and offsets as an integer multiple of 512 bytes, where
2250     possible (the first and last chunk of an unaligned read being the
2251     most obvious places for an exception).  The server MUST NOT send
2252     content chunks that overlap with any earlier content or error
2253     chunk, and MUST NOT send chunks that describe data outside the
2254     offset and length of the request, but MAY send the content chunks
2255     in any order (the client MUST reassemble content chunks into the
2256     correct order), and MAY send additional content chunks even after
2257     reporting an error chunk.  A server MAY support read requests
2258     larger than the maximum payload size by splitting the response
2259     across multiple chunks (in particular, if extended headers are not
2260     in use, a request for more than 2^32 - 8 bytes containing data
2261     rather than holes MUST be split to avoid overflowing the 32-bit
2262     `NBD_REPLY_TYPE_OFFSET_DATA` length field); however, the server is
2263     also permitted to reject large read requests up front, so a client
2264     should be prepared to retry with smaller requests if a large
2265     request fails.
2267     When no error is detected, the server MUST send enough data chunks
2268     to cover the entire region described by the offset and length of
2269     the client's request.
2271     To minimize traffic, the server MAY use a content or error chunk
2272     as the final chunk by setting the `NBD_REPLY_FLAG_DONE` flag, but
2273     MUST NOT do so for a content chunk if it would still be possible
2274     to detect an error while transmitting the chunk.  The
2275     `NBD_REPLY_TYPE_NONE` chunk is always acceptable as the final
2276     chunk.
2278     If an error is detected, the server MUST still complete the
2279     transmission of any current chunk (it MUST use padding bytes which
2280     SHOULD be zero, for any remaining data portion of a chunk with
2281     type `NBD_REPLY_TYPE_OFFSET_DATA`), but MAY omit further content
2282     chunks.  The server MUST include an error chunk as one of the
2283     subsequent chunks, but MAY defer the error reporting behind other
2284     queued chunks.  An error chunk of type `NBD_REPLY_TYPE_ERROR`
2285     implies that the client MAY NOT make any assumptions about
2286     validity of data chunks (whether sent before or after the error
2287     chunk), and if used, SHOULD be the only error chunk in the reply.
2288     On the other hand, an error chunk of type
2289     `NBD_REPLY_TYPE_ERROR_OFFSET` gives fine-grained information about
2290     which earlier data chunk(s) encountered a failure; as such, a
2291     server MAY still usefully follow it with further non-overlapping
2292     content chunks or with error offsets for other content chunks.
2293     The server MAY send an error chunk with no corresponding content
2294     chunk, but MUST ensure that the content chunk is sent first if a
2295     content and error chunk cover the same offset.  Generally, a
2296     server SHOULD NOT mix errors with offsets with a generic error.
2297     As long as all errors are accompanied by offsets, the client MAY
2298     assume that any data chunks with no subsequent error offset are
2299     valid, that chunks with an overlapping error offset errors are
2300     valid up until the reported offset, and that portions of the read
2301     that do not have a corresponding content chunk are not valid.
2303     A client MAY initiate a hard disconnect if it detects that the server
2304     has sent invalid chunks (such as overlapping data, or not enough
2305     data before claiming success).
2307     In order to avoid the burden of reassembly, the client MAY set the
2308     `NBD_CMD_FLAG_DF` flag ("don't fragment").  If this flag is set,
2309     the server MUST send at most one content chunk, although it MAY
2310     still send multiple chunks (the remaining chunks would be error
2311     chunks or a final type of `NBD_REPLY_TYPE_NONE`).  If the area
2312     being read contains both data and a hole, the server MUST use
2313     `NBD_REPLY_TYPE_OFFSET_DATA` with the zeroes explicitly present.
2314     A server MAY reject a client's request with the error `NBD_EOVERFLOW`
2315     if the length is too large to send without fragmentation, in which
2316     case it MUST NOT send a content chunk; however, the server MUST
2317     support unfragmented reads in which the client's request length
2318     does not exceed 65,536 bytes.
2320 * `NBD_CMD_WRITE` (1)
2322     A write request. Length and offset define the location and amount of
2323     data to be written. The client MUST follow the request header with
2324     *length* number of bytes to be written to the device. The client
2325     SHOULD NOT request a write length of 0; the behavior of a server on
2326     such a request is unspecified although the server SHOULD NOT
2327     disconnect.
2329     The server MUST write the data to disk, and then send the reply
2330     message. The server MAY send the reply message before the data has
2331     reached permanent storage, unless `NBD_CMD_FLAG_FUA` is in use.
2333     If an error occurs, the server MUST set the appropriate error code
2334     in the error field.
2336 * `NBD_CMD_DISC` (2)
2338     A disconnect request. The server MUST handle all outstanding
2339     requests, shut down the TLS session (if one is running), and
2340     close the TCP session.  A client MUST NOT send
2341     anything to the server after sending an `NBD_CMD_DISC` command.
2343     The values of the length and offset fields in a disconnect request
2344     MUST be zero.
2346     There is no reply to an `NBD_CMD_DISC`.
2348 * `NBD_CMD_FLUSH` (3)
2350     A flush request. The server MUST NOT send a
2351     successful reply header for this request before all write requests
2352     for which a reply has already been sent to the client have reached
2353     permanent storage (using fsync() or similar).
2355     A client MUST NOT send a flush request unless `NBD_FLAG_SEND_FLUSH`
2356     was set in the transmission flags field.
2358     For a flush request, *length* and *offset* are reserved, and MUST be
2359     set to all-zero.
2361 * `NBD_CMD_TRIM` (4)
2363     A hint to the server that the data defined by length and offset is
2364     no longer needed. A server MAY discard *length* bytes starting at
2365     offset, but is not required to; and MAY round *offset* up and
2366     *length* down to meet internal alignment constraints so that only
2367     a portion of the client's request is actually discarded. The
2368     client SHOULD NOT request a trim length of 0; the behavior of a
2369     server on such a request is unspecified although the server SHOULD
2370     NOT disconnect.
2372     After issuing this command, a client MUST NOT make any assumptions
2373     about the contents of the export affected by this command, until
2374     overwriting it again with `NBD_CMD_WRITE` or `NBD_CMD_WRITE_ZEROES`.
2376     A client MUST NOT send a trim request unless `NBD_FLAG_SEND_TRIM`
2377     was set in the transmission flags field.
2379 * `NBD_CMD_CACHE` (5)
2381     A cache request.  The client is informing the server that it plans
2382     to access the area specified by *offset* and *length*.  The server
2383     MAY use this information to speed up further access to that area
2384     (for example, by performing the actions of `NBD_CMD_READ` but
2385     replying with just status instead of a payload, by using
2386     posix_fadvise(), or by retrieving remote data into a local cache
2387     so that future reads and unaligned writes to that region are
2388     faster).  However, it is unspecified what the server's actual
2389     caching mechanism is (if any), whether there is a limit on how
2390     much can be cached at once, and whether writes to a cached region
2391     have write-through or write-back semantics.  Thus, even when this
2392     command reports success, there is no guarantee of an actual
2393     performance gain.  A future version of this standard may add
2394     command flags to request particular caching behaviors, where a
2395     server would reply with an error if that behavior cannot be
2396     accomplished.
2398     If an error occurs, the server MUST set the appropriate error code
2399     in the error field. However failure on this operation does not
2400     imply that further read and write requests on this area will fail,
2401     and, other than any difference in performance, there MUST NOT be
2402     any difference in semantics compared to if the client had not used
2403     this command.  When no command flags are in use, the server MAY
2404     send a reply prior to the requested area being fully cached.
2406     Note that client implementations exist which attempt to send a
2407     cache request even when `NBD_FLAG_SEND_CACHE` was not set in the
2408     transmission flags field, however, these implementations do not
2409     use any command flags.  A server MAY advertise
2410     `NBD_FLAG_SEND_CACHE` even if the command has no effect or always
2411     fails with `NBD_EINVAL`; however, if it advertised the command, the
2412     server MUST reject any command flags it does not recognize.
2414 * `NBD_CMD_WRITE_ZEROES` (6)
2416     A write request with no payload. *Offset* and *length* define the
2417     location and amount of data to be zeroed. The client SHOULD NOT
2418     request a write length of 0; the behavior of a server on such a
2419     request is unspecified although the server SHOULD NOT disconnect.
2421     The server MUST zero out the data on disk, and then send the reply
2422     message. The server MAY send the reply message before the data has
2423     reached permanent storage, unless `NBD_CMD_FLAG_FUA` is in use.
2425     A client MUST NOT send a write zeroes request unless
2426     `NBD_FLAG_SEND_WRITE_ZEROES` was set in the transmission flags
2427     field. Additionally, a client MUST NOT send the
2428     `NBD_CMD_FLAG_FAST_ZERO` flag unless `NBD_FLAG_SEND_FAST_ZERO` was
2429     set in the transmission flags field.
2431     By default, the server MAY use trimming to zero out the area, even
2432     if it did not advertise `NBD_FLAG_SEND_TRIM`; but it MUST ensure
2433     that the data reads back as zero.  However, the client MAY set the
2434     command flag `NBD_CMD_FLAG_NO_HOLE` to inform the server that the
2435     area MUST be fully provisioned, ensuring that future writes to the
2436     same area will not cause fragmentation or cause failure due to
2437     insufficient space.
2439     If the server advertised `NBD_FLAG_SEND_FAST_ZERO` but
2440     `NBD_CMD_FLAG_FAST_ZERO` is not set, then the server MUST NOT fail
2441     with `NBD_ENOTSUP`, even if the operation is no faster than a
2442     corresponding `NBD_CMD_WRITE`. Conversely, if `NBD_CMD_FLAG_FAST_ZERO`
2443     is set, the server SHOULD NOT fail with `NBD_EOVERFLOW` regardless of
2444     the client length, MUST fail quickly with `NBD_ENOTSUP` unless the
2445     request can be serviced in less time than a corresponding
2446     `NBD_CMD_WRITE`, and SHOULD NOT alter the contents of the export when
2447     returning an `NBD_ENOTSUP` failure. The server's
2448     determination on whether to fail a fast request MAY depend on a
2449     number of factors, such as whether the request was suitably
2450     aligned, on whether the `NBD_CMD_FLAG_NO_HOLE` flag was present,
2451     or even on whether a previous `NBD_CMD_TRIM` had been performed on
2452     the region.  If the server did not advertise
2453     `NBD_FLAG_SEND_FAST_ZERO`, then it SHOULD NOT fail with
2454     `NBD_ENOTSUP`, regardless of the speed of servicing a request, and
2455     SHOULD fail with `NBD_EINVAL` if the `NBD_CMD_FLAG_FAST_ZERO` flag
2456     was set. A server MAY advertise `NBD_FLAG_SEND_FAST_ZERO` whether
2457     or not it will actually succeed on a fast zero request (a fast
2458     failure of `NBD_ENOTSUP` still counts as a fast response);
2459     similarly, a server SHOULD fail a fast zero request with
2460     `NBD_ENOTSUP` if the server cannot quickly determine in advance
2461     whether proceeding with the request would be fast, even if it
2462     turns out that the same request without the flag would be fast
2463     after all.
2465     One intended use of a fast zero request is optimizing the copying
2466     of a sparse image source into the export: a client can request
2467     fast zeroing of the entire export, and if it succeeds, follow that
2468     with write requests to just the data portions before a single
2469     flush of the entire image, for fewer transactions overall.  On the
2470     other hand, if the fast zero request fails, the fast failure lets
2471     the client know that it must manually write zeroes corresponding
2472     to the holes of the source image before a final flush, for more
2473     transactions but with no time lost to duplicated I/O to the data
2474     portions.  Knowing this usage pattern can help decide whether a
2475     server's implementation for writing zeroes counts as fast (for
2476     example, a successful fast zero request may start a background
2477     operation that would cause the next flush request to take longer,
2478     but that is okay as long as intermediate writes before that flush
2479     do not further lengthen the time spent on the overall sequence of
2480     operations).
2482     If an error occurs, the server MUST set the appropriate error code
2483     in the error field.
2485     The server SHOULD return `NBD_ENOSPC` if it receives a write zeroes request
2486     including one or more sectors beyond the size of the device. It SHOULD
2487     return `NBD_EPERM` if it receives a write zeroes request on a read-only export.
2489 * `NBD_CMD_BLOCK_STATUS` (7)
2491     A block status query request. The effect length is a hint to the
2492     server about the range of interest.  The client SHOULD NOT request
2493     a status length of 0; the behavior of a server on such a request
2494     is unspecified although the server SHOULD NOT disconnect.
2496     A client MUST NOT send `NBD_CMD_BLOCK_STATUS` unless within the
2497     negotiation phase it sent `NBD_OPT_SET_META_CONTEXT` at least
2498     once, and where the final time that was sent, it referred to the
2499     same export name used to enter transmission phase, and where the
2500     server returned at least one metadata context without an error.
2501     This in turn requires the client to first negotiate structured
2502     replies or extended headers. For a successful return, the server
2503     MUST use one reply chunk per selected context id (only
2504     `NBD_REPLY_TYPE_BLOCK_STATUS` for structured replies, and either
2505     `NBD_REPLY_TYPE_BLOCK_STATUS` or `NBD_REPLY_TYPE_BLOCK_STATUS_EXT`
2506     for extended headers).  The status field of each descriptor is
2507     determined by the flags field as defined by the metadata context.
2508     The server MAY send chunks in a different order than the context
2509     ids were assigned in reply to `NBD_OPT_SET_META_CONTEXT`.
2511     If extended headers were negotiated, a server MAY optionally
2512     advertise, via the transmission flag
2513     `NBD_FLAG_BLOCK_STATUS_PAYLOAD`, that it supports an alternative
2514     request form where the client sets `NBD_CMD_FLAG_PAYLOAD_LEN` in
2515     order to pass a payload that informs the server to limit its
2516     replies to the metacontext id(s) in the client's request payload,
2517     rather than giving an answer on all possible metacontext ids.  If
2518     the server does not support the payload form, or detects duplicate
2519     or unknown metacontext ids in the client's payload, the server
2520     MUST gracefully consume the client's payload before failing with
2521     `NBD_EINVAL`.  The payload form MUST occupy 8 + n*4 bytes, where n
2522     is the number of metacontext ids the client is interested in (as
2523     implied by the payload length), laid out as:
2525     64 bits, effect length  
2526     n * 32 bits, list of metacontext ids to use  
2528     The list of block status descriptors within a given status chunk
2529     represent consecutive portions of the export starting from
2530     specified *offset*.  If the client used the `NBD_CMD_FLAG_REQ_ONE`
2531     flag, each chunk contains exactly one descriptor where the
2532     *length* of the descriptor MUST NOT be greater than the *length*
2533     of the request; otherwise, a chunk MAY contain multiple
2534     descriptors, and the final descriptor MAY extend beyond the
2535     original requested size if the server can determine a larger
2536     length without additional effort.  On the other hand, the server
2537     MAY return less data than requested.  In particular, a server
2538     SHOULD NOT send more than 2^20 status descriptors in a single
2539     chunk.  However the server MUST return at least one status
2540     descriptor, and since each status descriptor has a non-zero
2541     length, a client can always make progress on a successful return.
2543     The server SHOULD use different *status* values between
2544     consecutive descriptors where feasible, although the client SHOULD
2545     be prepared to handle consecutive descriptors with the same
2546     *status* value.  The server SHOULD use descriptor lengths that are
2547     an integer multiple of 512 bytes where possible (the first and
2548     last descriptor of an unaligned query being the most obvious
2549     places for an exception), in part to avoid an amplification effect
2550     where a series of smaller descriptors can cause the server's reply
2551     to occupy more bytes than the *length* of the client's request.
2552     The server MUST use descriptor lengths that are an integer
2553     multiple of any advertised minimum block size. The status flags
2554     are intentionally defined so that a server MAY always safely
2555     report a status of 0 for any block, although the server SHOULD
2556     return additional status values when they can be easily detected.
2558     If an error occurs, the server SHOULD set the appropriate error
2559     code in the error field of an error chunk. However, if the error
2560     does not involve invalid usage (such as a request beyond the
2561     bounds of the export), a server MAY reply with a single block
2562     status descriptor with *length* matching the requested length,
2563     rather than reporting the error; in this case the context MAY
2564     mandate the status returned.
2566     A client MAY initiate a hard disconnect if it detects that the
2567     server has sent an invalid chunk. The server SHOULD return
2568     `NBD_EINVAL` if it receives a `NBD_CMD_BLOCK_STATUS` request including
2569     one or more sectors beyond the size of the device.
2571 * `NBD_CMD_RESIZE` (8)
2573     Defined by the experimental `RESIZE`
2574     [extension](https://github.com/NetworkBlockDevice/nbd/blob/extension-resize/doc/proto.md).
2576 * Other requests
2578     Some third-party implementations may require additional protocol
2579     messages which are not described in this document. In the interest of
2580     interoperability, authors of such implementations SHOULD contact the
2581     maintainer of this document, so that these messages can be listed here
2582     to avoid conflicting implementations.
2584 #### Error values
2586 The error values are used for the error field in the reply message.
2587 Originally, error messages were defined as the value of `errno` on the
2588 system running the server; however, although they happen to have similar
2589 values on most systems, these values are in fact not well-defined, and
2590 therefore not entirely portable.
2592 Therefore, the allowed values for the error field have been restricted
2593 to set of possibilities. To remain intelligible with older clients, the
2594 most common values of `errno` for that particular error has been chosen
2595 as the value for an error.
2597 The following error values are defined:
2599 * `NBD_EPERM` (1), Operation not permitted.
2600 * `NBD_EIO` (5), Input/output error.
2601 * `NBD_ENOMEM` (12), Cannot allocate memory.
2602 * `NBD_EINVAL` (22), Invalid argument.
2603 * `NBD_ENOSPC` (28), No space left on device.
2604 * `NBD_EOVERFLOW` (75), Value too large.
2605 * `NBD_ENOTSUP` (95), Operation not supported.
2606 * `NBD_ESHUTDOWN` (108), Server is in the process of being shut down.
2608 The server SHOULD return `NBD_ENOSPC` if it receives a write request
2609 including one or more sectors beyond the size of the device.  It also
2610 SHOULD map the `EDQUOT` and `EFBIG` errors to `NBD_ENOSPC`.  It SHOULD
2611 return `NBD_EINVAL` if it receives a read or trim request including one or
2612 more sectors beyond the size of the device, or if a read or write
2613 request is not aligned to advertised minimum block sizes. Finally, it
2614 SHOULD return `NBD_EPERM` if it receives a write or trim request on a
2615 read-only export.
2617 The server SHOULD NOT return `NBD_EOVERFLOW` except as documented in
2618 response to `NBD_CMD_READ` when `NBD_CMD_FLAG_DF` is supported, or when
2619 a command without payload requests a length larger than an advertised
2620 maximum payload length.
2622 The server SHOULD NOT return `NBD_ENOTSUP` except as documented in
2623 response to `NBD_CMD_WRITE_ZEROES` when `NBD_CMD_FLAG_FAST_ZERO` is
2624 supported.
2626 The server SHOULD return `NBD_EINVAL` if it receives an unknown command.
2628 The server SHOULD return `NBD_EINVAL` if it receives an unknown
2629 command flag. It also SHOULD return `NBD_EINVAL` if it receives a
2630 request with a flag not explicitly documented as applicable to the
2631 given request.
2633 Which error to return in any other case is not specified by the NBD
2634 protocol.
2636 The server SHOULD NOT return `NBD_ENOMEM` if at all possible.
2638 The client SHOULD treat an unexpected error value as if it had been
2639 `NBD_EINVAL`, rather than disconnecting from the server.
2641 ## Experimental extensions
2643 In addition to the normative elements of the specification set out
2644 herein, various experimental non-normative extensions have been
2645 proposed. These may not be implemented in any known server or client,
2646 and are subject to change at any point. A full implementation may
2647 require changes to the specifications, or cause the specifications to
2648 be withdrawn altogether.
2650 These experimental extensions are set out in git branches starting
2651 with names starting with the word 'extension'.
2653 Currently known are:
2655 * The `STRUCTURED_REPLY` [extension](https://github.com/NetworkBlockDevice/nbd/blob/extension-structured-reply/doc/proto.md).
2657 * The `BLOCK_STATUS` [extension](https://github.com/NetworkBlockDevice/nbd/blob/extension-blockstatus/doc/proto.md) (based on the `STRUCTURED_REPLY` extension).
2659 * The `RESIZE` [extension](https://github.com/NetworkBlockDevice/nbd/blob/extension-resize/doc/proto.md).
2661 Implementers of these extensions are strongly suggested to contact the
2662 [mailinglist](mailto:nbd@other.debian.org) in order to help
2663 fine-tune the specifications before committing to a particular
2664 implementation.
2666 Those proposing further extensions should also contact the
2667 [mailinglist](mailto:nbd@other.debian.org). It is
2668 possible to reserve command codes etc. within this document
2669 for such proposed extensions. Aside from that, extensions are
2670 written as branches which can be merged into master if and
2671 when those extensions are promoted to the normative version
2672 of the document in the master branch.
2674 ## Compatibility and interoperability
2676 Originally, the NBD protocol was a fairly simple protocol with few
2677 options. While the basic protocol is still reasonably simple, a growing
2678 number of extensions has been implemented that may make the protocol
2679 description seem overwhelming at first.
2681 In an effort to not overwhelm first-time implementers with various
2682 options and features that may or may not be important for their use
2683 case, while at the same time desiring maximum interoperability, this
2684 section tries to clarify what is optional and what is expected to be
2685 available in all implementations.
2687 All protocol options and messages not explicitly mentioned below should
2688 be considered optional features that MAY be negotiated between client
2689 and server, but are not required to be available.
2691 ### Baseline
2693 The following MUST be implemented by all implementations, and should be
2694 considered a baseline:
2696 - NOTLS mode
2697 - The fixed newstyle handshake
2698 - During the handshake:
2700     - the `NBD_OPT_INFO` and `NBD_OPT_GO` messages, with the
2701       `NBD_INFO_EXPORT` response.
2702     - Servers that receive messages which they do not implement MUST
2703       reply to them with `NBD_REP_ERR_UNSUP`, and MUST NOT fail to parse
2704       the next message received.
2705     - the `NBD_OPT_ABORT` message, and its response.
2706     - the `NBD_OPT_LIST` message and its response.
2708 - During the transmission phase:
2710     - Simple replies
2711     - the `NBD_CMD_READ` message (and its response)
2712     - the `NBD_CMD_WRITE` message (and its response), unless the
2713       implementation is a client that does not wish to write
2714     - the `NBD_CMD_DISC` message (and its resulting effects, although
2715       no response is involved)
2717 Clients that wish to use more messages MUST negotiate them during the
2718 handshake phase, first.
2720 ### Maximum interoperability
2722 Clients and servers that desire maximum interoperability SHOULD
2723 implement the following features:
2725 - TLS-encrypted communication, which may be required by some
2726   implementations or configurations;
2727 - Servers that implement block constraints through `NBD_INFO_BLOCK_SIZE`
2728   and desire maximum interoperability SHOULD NOT require them.
2729   Similarly, clients that desire maximum interoperability SHOULD
2730   implement querying for block size constraints. Since some clients
2731   default to a block size of 512 bytes, implementations desiring maximum
2732   interoperability MAY default to that size.
2733 - Clients or servers that desire interoperability with older
2734   implementations SHOULD implement the `NBD_OPT_EXPORT_NAME` message in
2735   addition to `NBD_OPT_INFO` and `NBD_OPT_GO`.
2736 - For data safety, implementing `NBD_CMD_FLUSH` and the
2737   `NBD_CMD_FLAG_FUA` flag to `NBD_CMD_WRITE` is strongly recommended.
2738   Clients that do not implement querying for block size constraints
2739   SHOULD abide by the rules laid out in the section "Block size
2740   constraints", above.
2741 - Servers that implement extended headers but desire interoperability
2742   with older client implementations SHOULD NOT use the
2743   `NBD_REP_ERR_EXT_HEADER_REQD` error response to `NBD_OPT_GO`, but
2744   should gracefully support compact headers.
2746 ### Future considerations
2748 The following may be moved to the "Maximum interoperability" or
2749 "Baseline" sections at some point in the future, but some significant
2750 implementations are not yet ready to support them:
2752 - Structured replies; the Linux kernel currently does not yet implement
2753   them.
2754 - Extended headers; these are similar to structured replies, but is
2755   new enough that many clients and servers do not yet implement them.
2757 ## About this file
2759 This file tries to document the NBD protocol as it is currently
2760 implemented in the Linux kernel and in the reference implementation. The
2761 purpose of this file is to allow people to understand the protocol
2762 without having to read the code. However, the description above does not
2763 come with any form of warranty; while every effort has been taken to
2764 avoid them, mistakes are possible.
2766 In contrast to the other files in this repository, this file is not
2767 licensed under the GPLv2. To the extent possible by applicable law, I
2768 hereby waive all copyright and related or neighboring rights to this
2769 file and release it into the public domain.
2771 The purpose of releasing this into the public domain is to allow
2772 competing implementations of the NBD protocol without those
2773 implementations being considered derivative implementations; but please
2774 note that changing this document, while allowed by its public domain
2775 status, does not make an incompatible implementation suddenly speak the
2776 NBD protocol.