3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "block/export.h"
24 #include "io/channel-socket.h"
25 #include "crypto/tlscreds.h"
26 #include "qapi/error.h"
27 #include "qemu/bswap.h"
29 typedef struct NBDExport NBDExport
;
30 typedef struct NBDClient NBDClient
;
31 typedef struct NBDClientConnection NBDClientConnection
;
32 typedef struct NBDMetaContexts NBDMetaContexts
;
34 extern const BlockExportDriver blk_exp_nbd
;
37 * NBD_DEFAULT_HANDSHAKE_MAX_SECS: Number of seconds in which client must
38 * succeed at NBD_OPT_GO before being forcefully dropped as too slow.
40 #define NBD_DEFAULT_HANDSHAKE_MAX_SECS 10
43 * NBD_DEFAULT_MAX_CONNECTIONS: Number of client sockets to allow at
44 * once; must be large enough to allow a MULTI_CONN-aware client like
45 * nbdcopy to create its typical number of 8-16 sockets.
47 #define NBD_DEFAULT_MAX_CONNECTIONS 100
49 /* Handshake phase structs - this struct is passed on the wire */
51 typedef struct NBDOption
{
52 uint64_t magic
; /* NBD_OPTS_MAGIC */
53 uint32_t option
; /* NBD_OPT_* */
55 } QEMU_PACKED NBDOption
;
57 typedef struct NBDOptionReply
{
58 uint64_t magic
; /* NBD_REP_MAGIC */
59 uint32_t option
; /* NBD_OPT_* */
60 uint32_t type
; /* NBD_REP_* */
62 } QEMU_PACKED NBDOptionReply
;
64 typedef struct NBDOptionReplyMetaContext
{
65 NBDOptionReply h
; /* h.type = NBD_REP_META_CONTEXT, h.length > 4 */
67 /* metadata context name follows */
68 } QEMU_PACKED NBDOptionReplyMetaContext
;
70 /* Track results of negotiation */
71 typedef enum NBDMode
{
72 /* Keep this list in a continuum of increasing features. */
73 NBD_MODE_OLDSTYLE
, /* server lacks newstyle negotiation */
74 NBD_MODE_EXPORT_NAME
, /* newstyle but only OPT_EXPORT_NAME safe */
75 NBD_MODE_SIMPLE
, /* newstyle but only simple replies */
76 NBD_MODE_STRUCTURED
, /* newstyle, structured replies enabled */
77 NBD_MODE_EXTENDED
, /* newstyle, extended headers enabled */
80 /* Transmission phase structs */
83 * Note: NBDRequest is _NOT_ the same as the network representation of an NBD
86 typedef struct NBDRequest
{
88 uint64_t from
; /* Offset touched by the command */
89 uint64_t len
; /* Effect length; 32 bit limit without extended headers */
90 uint16_t flags
; /* NBD_CMD_FLAG_* */
91 uint16_t type
; /* NBD_CMD_* */
92 NBDMode mode
; /* Determines which network representation to use */
93 NBDMetaContexts
*contexts
; /* Used by NBD_CMD_BLOCK_STATUS */
96 typedef struct NBDSimpleReply
{
97 uint32_t magic
; /* NBD_SIMPLE_REPLY_MAGIC */
100 } QEMU_PACKED NBDSimpleReply
;
102 /* Header of all structured replies */
103 typedef struct NBDStructuredReplyChunk
{
104 uint32_t magic
; /* NBD_STRUCTURED_REPLY_MAGIC */
105 uint16_t flags
; /* combination of NBD_REPLY_FLAG_* */
106 uint16_t type
; /* NBD_REPLY_TYPE_* */
107 uint64_t cookie
; /* request handle */
108 uint32_t length
; /* length of payload */
109 } QEMU_PACKED NBDStructuredReplyChunk
;
111 typedef struct NBDExtendedReplyChunk
{
112 uint32_t magic
; /* NBD_EXTENDED_REPLY_MAGIC */
113 uint16_t flags
; /* combination of NBD_REPLY_FLAG_* */
114 uint16_t type
; /* NBD_REPLY_TYPE_* */
115 uint64_t cookie
; /* request handle */
116 uint64_t offset
; /* request offset */
117 uint64_t length
; /* length of payload */
118 } QEMU_PACKED NBDExtendedReplyChunk
;
120 typedef union NBDReply
{
121 NBDSimpleReply simple
;
122 NBDStructuredReplyChunk structured
;
123 NBDExtendedReplyChunk extended
;
126 * @magic and @cookie fields have the same offset and size in all
127 * forms of replies, so let them be accessible without ".simple.",
128 * ".structured.", or ".extended." specifications.
135 QEMU_BUILD_BUG_ON(offsetof(NBDReply
, simple
.cookie
) !=
136 offsetof(NBDReply
, cookie
));
137 QEMU_BUILD_BUG_ON(offsetof(NBDReply
, structured
.cookie
) !=
138 offsetof(NBDReply
, cookie
));
139 QEMU_BUILD_BUG_ON(offsetof(NBDReply
, extended
.cookie
) !=
140 offsetof(NBDReply
, cookie
));
142 /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */
143 typedef struct NBDStructuredReadData
{
144 /* header's .length >= 9 */
146 /* At least one byte of data payload follows, calculated from h.length */
147 } QEMU_PACKED NBDStructuredReadData
;
149 /* Complete chunk for NBD_REPLY_TYPE_OFFSET_HOLE */
150 typedef struct NBDStructuredReadHole
{
151 /* header's length == 12 */
154 } QEMU_PACKED NBDStructuredReadHole
;
156 /* Header of all NBD_REPLY_TYPE_ERROR* errors */
157 typedef struct NBDStructuredError
{
158 /* header's length >= 6 */
160 uint16_t message_length
;
161 } QEMU_PACKED NBDStructuredError
;
163 /* Header of NBD_REPLY_TYPE_BLOCK_STATUS */
164 typedef struct NBDStructuredMeta
{
165 /* header's length >= 12 (at least one extent) */
167 /* NBDExtent32 extents[] follows, array length implied by header */
168 } QEMU_PACKED NBDStructuredMeta
;
170 /* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS */
171 typedef struct NBDExtent32
{
173 uint32_t flags
; /* NBD_STATE_* */
174 } QEMU_PACKED NBDExtent32
;
176 /* Header of NBD_REPLY_TYPE_BLOCK_STATUS_EXT */
177 typedef struct NBDExtendedMeta
{
178 /* header's length >= 24 (at least one extent) */
180 uint32_t count
; /* header length must be count * 16 + 8 */
181 /* NBDExtent64 extents[count] follows */
182 } QEMU_PACKED NBDExtendedMeta
;
184 /* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS_EXT */
185 typedef struct NBDExtent64
{
187 uint64_t flags
; /* NBD_STATE_* */
188 } QEMU_PACKED NBDExtent64
;
190 /* Client payload for limiting NBD_CMD_BLOCK_STATUS reply */
191 typedef struct NBDBlockStatusPayload
{
192 uint64_t effect_length
;
193 /* uint32_t ids[] follows, array length implied by header */
194 } QEMU_PACKED NBDBlockStatusPayload
;
196 /* Transmission (export) flags: sent from server to client during handshake,
197 but describe what will happen during transmission */
199 NBD_FLAG_HAS_FLAGS_BIT
= 0, /* Flags are there */
200 NBD_FLAG_READ_ONLY_BIT
= 1, /* Device is read-only */
201 NBD_FLAG_SEND_FLUSH_BIT
= 2, /* Send FLUSH */
202 NBD_FLAG_SEND_FUA_BIT
= 3, /* Send FUA (Force Unit Access) */
203 NBD_FLAG_ROTATIONAL_BIT
= 4, /* Use elevator algorithm -
205 NBD_FLAG_SEND_TRIM_BIT
= 5, /* Send TRIM (discard) */
206 NBD_FLAG_SEND_WRITE_ZEROES_BIT
= 6, /* Send WRITE_ZEROES */
207 NBD_FLAG_SEND_DF_BIT
= 7, /* Send DF (Do not Fragment) */
208 NBD_FLAG_CAN_MULTI_CONN_BIT
= 8, /* Multi-client cache consistent */
209 NBD_FLAG_SEND_RESIZE_BIT
= 9, /* Send resize */
210 NBD_FLAG_SEND_CACHE_BIT
= 10, /* Send CACHE (prefetch) */
211 NBD_FLAG_SEND_FAST_ZERO_BIT
= 11, /* FAST_ZERO flag for WRITE_ZEROES */
212 NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT
= 12, /* PAYLOAD flag for BLOCK_STATUS */
215 #define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT)
216 #define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT)
217 #define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT)
218 #define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT)
219 #define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT)
220 #define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT)
221 #define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT)
222 #define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT)
223 #define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT)
224 #define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT)
225 #define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT)
226 #define NBD_FLAG_SEND_FAST_ZERO (1 << NBD_FLAG_SEND_FAST_ZERO_BIT)
227 #define NBD_FLAG_BLOCK_STAT_PAYLOAD (1 << NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT)
229 /* New-style handshake (global) flags, sent from server to client, and
230 control what will happen during handshake phase. */
231 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
232 #define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
234 /* New-style client flags, sent from client to server to control what happens
235 during handshake phase. */
236 #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
237 #define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
239 /* Option requests. */
240 #define NBD_OPT_EXPORT_NAME (1)
241 #define NBD_OPT_ABORT (2)
242 #define NBD_OPT_LIST (3)
243 /* #define NBD_OPT_PEEK_EXPORT (4) not in use */
244 #define NBD_OPT_STARTTLS (5)
245 #define NBD_OPT_INFO (6)
246 #define NBD_OPT_GO (7)
247 #define NBD_OPT_STRUCTURED_REPLY (8)
248 #define NBD_OPT_LIST_META_CONTEXT (9)
249 #define NBD_OPT_SET_META_CONTEXT (10)
250 #define NBD_OPT_EXTENDED_HEADERS (11)
252 /* Option reply types. */
253 #define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value))
255 #define NBD_REP_ACK (1) /* Data sending finished. */
256 #define NBD_REP_SERVER (2) /* Export description. */
257 #define NBD_REP_INFO (3) /* NBD_OPT_INFO/GO. */
258 #define NBD_REP_META_CONTEXT (4) /* NBD_OPT_{LIST,SET}_META_CONTEXT */
260 #define NBD_REP_ERR_UNSUP NBD_REP_ERR(1) /* Unknown option */
261 #define NBD_REP_ERR_POLICY NBD_REP_ERR(2) /* Server denied */
262 #define NBD_REP_ERR_INVALID NBD_REP_ERR(3) /* Invalid length */
263 #define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4) /* Not compiled in */
264 #define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5) /* TLS required */
265 #define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */
266 #define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting down */
267 #define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZE */
268 #define NBD_REP_ERR_TOO_BIG NBD_REP_ERR(9) /* Payload size overflow */
269 #define NBD_REP_ERR_EXT_HEADER_REQD NBD_REP_ERR(10) /* Need extended headers */
271 /* Info types, used during NBD_REP_INFO */
272 #define NBD_INFO_EXPORT 0
273 #define NBD_INFO_NAME 1
274 #define NBD_INFO_DESCRIPTION 2
275 #define NBD_INFO_BLOCK_SIZE 3
277 /* Request flags, sent from client to server during transmission phase */
278 #define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during write */
279 #define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */
280 #define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read */
281 #define NBD_CMD_FLAG_REQ_ONE (1 << 3) \
282 /* only one extent in BLOCK_STATUS reply chunk */
283 #define NBD_CMD_FLAG_FAST_ZERO (1 << 4) /* fail if WRITE_ZEROES is not fast */
284 #define NBD_CMD_FLAG_PAYLOAD_LEN (1 << 5) \
285 /* length describes payload, not effect; only with ext header */
287 /* Supported request types */
295 NBD_CMD_WRITE_ZEROES
= 6,
296 NBD_CMD_BLOCK_STATUS
= 7,
299 #define NBD_DEFAULT_PORT 10809
301 /* Maximum size of a single READ/WRITE data buffer */
302 #define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)
305 * Maximum size of a protocol string (export name, metadata context name,
306 * etc.). Use malloc rather than stack allocation for storage of a
309 #define NBD_MAX_STRING_SIZE 4096
311 /* Two types of request structures, a given client will only use 1 */
312 #define NBD_REQUEST_MAGIC 0x25609513
313 #define NBD_EXTENDED_REQUEST_MAGIC 0x21e41c71
316 * Three types of reply structures, but what a client expects depends
317 * on NBD_OPT_STRUCTURED_REPLY and NBD_OPT_EXTENDED_HEADERS.
319 #define NBD_SIMPLE_REPLY_MAGIC 0x67446698
320 #define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef
321 #define NBD_EXTENDED_REPLY_MAGIC 0x6e8a278c
323 /* Chunk reply flags (for structured and extended replies) */
324 #define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last */
326 /* Chunk reply types */
327 #define NBD_REPLY_ERR(value) ((1 << 15) | (value))
329 #define NBD_REPLY_TYPE_NONE 0
330 #define NBD_REPLY_TYPE_OFFSET_DATA 1
331 #define NBD_REPLY_TYPE_OFFSET_HOLE 2
332 #define NBD_REPLY_TYPE_BLOCK_STATUS 5
333 #define NBD_REPLY_TYPE_BLOCK_STATUS_EXT 6
334 #define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)
335 #define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2)
337 /* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */
338 #define NBD_STATE_HOLE (1 << 0)
339 #define NBD_STATE_ZERO (1 << 1)
341 /* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */
342 #define NBD_STATE_DIRTY (1 << 0)
344 /* No flags needed for qemu:allocation-depth in NBD_REPLY_TYPE_BLOCK_STATUS */
346 static inline bool nbd_reply_type_is_error(int type
)
348 return type
& (1 << 15);
351 /* NBD errors are based on errno numbers, so there is a 1:1 mapping,
352 * but only a limited set of errno values is specified in the protocol.
353 * Everything else is squashed to EINVAL.
355 #define NBD_SUCCESS 0
358 #define NBD_ENOMEM 12
359 #define NBD_EINVAL 22
360 #define NBD_ENOSPC 28
361 #define NBD_EOVERFLOW 75
362 #define NBD_ENOTSUP 95
363 #define NBD_ESHUTDOWN 108
365 /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */
366 typedef struct NBDExportInfo
{
367 /* Set by client before nbd_receive_negotiate() */
369 char *x_dirty_bitmap
;
371 /* Set by client before nbd_receive_negotiate(), or by server results
372 * during nbd_receive_export_list() */
373 char *name
; /* must be non-NULL */
375 /* In-out fields, set by client before nbd_receive_negotiate() and
376 * updated by server results during nbd_receive_negotiate() */
377 NBDMode mode
; /* input maximum mode tolerated; output actual mode chosen */
378 bool base_allocation
; /* base:allocation context for NBD_CMD_BLOCK_STATUS */
380 /* Set by server results during nbd_receive_negotiate() and
381 * nbd_receive_export_list() */
390 /* Set by server results during nbd_receive_export_list() */
396 int nbd_receive_negotiate(QIOChannel
*ioc
, QCryptoTLSCreds
*tlscreds
,
397 const char *hostname
, QIOChannel
**outioc
,
398 NBDExportInfo
*info
, Error
**errp
);
399 void nbd_free_export_list(NBDExportInfo
*info
, int count
);
400 int nbd_receive_export_list(QIOChannel
*ioc
, QCryptoTLSCreds
*tlscreds
,
401 const char *hostname
, NBDExportInfo
**info
,
403 int nbd_init(int fd
, QIOChannelSocket
*sioc
, NBDExportInfo
*info
,
405 int nbd_send_request(QIOChannel
*ioc
, NBDRequest
*request
);
406 int coroutine_fn
nbd_receive_reply(BlockDriverState
*bs
, QIOChannel
*ioc
,
407 NBDReply
*reply
, NBDMode mode
,
409 int nbd_client(int fd
);
410 int nbd_disconnect(int fd
);
411 int nbd_errno_to_system_errno(int err
);
413 void nbd_export_set_on_eject_blk(BlockExport
*exp
, BlockBackend
*blk
);
415 AioContext
*nbd_export_aio_context(NBDExport
*exp
);
416 NBDExport
*nbd_export_find(const char *name
);
418 void nbd_client_new(QIOChannelSocket
*sioc
,
419 uint32_t handshake_max_secs
,
420 QCryptoTLSCreds
*tlscreds
,
421 const char *tlsauthz
,
422 void (*close_fn
)(NBDClient
*, bool),
424 void *nbd_client_owner(NBDClient
*client
);
425 void nbd_client_get(NBDClient
*client
);
426 void nbd_client_put(NBDClient
*client
);
428 void nbd_server_is_qemu_nbd(int max_connections
);
429 bool nbd_server_is_running(void);
430 int nbd_server_max_connections(void);
431 void nbd_server_start(SocketAddress
*addr
, const char *tls_creds
,
432 const char *tls_authz
, uint32_t max_connections
,
434 void nbd_server_start_options(NbdServerOptions
*arg
, Error
**errp
);
437 * Reads @size bytes from @ioc. Returns 0 on success.
439 static inline int nbd_read(QIOChannel
*ioc
, void *buffer
, size_t size
,
440 const char *desc
, Error
**errp
)
443 int ret
= qio_channel_read_all(ioc
, buffer
, size
, errp
) < 0 ? -EIO
: 0;
447 error_prepend(errp
, "Failed to read %s: ", desc
);
455 #define DEF_NBD_READ_N(bits) \
456 static inline int nbd_read##bits(QIOChannel *ioc, \
457 uint##bits##_t *val, \
458 const char *desc, Error **errp) \
460 int ret = nbd_read(ioc, val, sizeof(*val), desc, errp); \
464 *val = be##bits##_to_cpu(*val); \
468 DEF_NBD_READ_N(16) /* Defines nbd_read16(). */
469 DEF_NBD_READ_N(32) /* Defines nbd_read32(). */
470 DEF_NBD_READ_N(64) /* Defines nbd_read64(). */
472 #undef DEF_NBD_READ_N
474 static inline bool nbd_reply_is_simple(NBDReply
*reply
)
476 return reply
->magic
== NBD_SIMPLE_REPLY_MAGIC
;
479 static inline bool nbd_reply_is_structured(NBDReply
*reply
)
481 return reply
->magic
== NBD_STRUCTURED_REPLY_MAGIC
;
484 const char *nbd_reply_type_lookup(uint16_t type
);
485 const char *nbd_opt_lookup(uint32_t opt
);
486 const char *nbd_rep_lookup(uint32_t rep
);
487 const char *nbd_info_lookup(uint16_t info
);
488 const char *nbd_cmd_lookup(uint16_t info
);
489 const char *nbd_err_lookup(int err
);
490 const char *nbd_mode_lookup(NBDMode mode
);
492 /* nbd/client-connection.c */
493 void nbd_client_connection_enable_retry(NBDClientConnection
*conn
);
495 NBDClientConnection
*nbd_client_connection_new(const SocketAddress
*saddr
,
497 const char *export_name
,
498 const char *x_dirty_bitmap
,
499 QCryptoTLSCreds
*tlscreds
,
500 const char *tlshostname
);
501 void nbd_client_connection_release(NBDClientConnection
*conn
);
503 QIOChannel
*coroutine_fn
504 nbd_co_establish_connection(NBDClientConnection
*conn
, NBDExportInfo
*info
,
505 bool blocking
, Error
**errp
);
507 void nbd_co_establish_connection_cancel(NBDClientConnection
*conn
);