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
;
36 /* Handshake phase structs - this struct is passed on the wire */
38 typedef struct NBDOption
{
39 uint64_t magic
; /* NBD_OPTS_MAGIC */
40 uint32_t option
; /* NBD_OPT_* */
42 } QEMU_PACKED NBDOption
;
44 typedef struct NBDOptionReply
{
45 uint64_t magic
; /* NBD_REP_MAGIC */
46 uint32_t option
; /* NBD_OPT_* */
47 uint32_t type
; /* NBD_REP_* */
49 } QEMU_PACKED NBDOptionReply
;
51 typedef struct NBDOptionReplyMetaContext
{
52 NBDOptionReply h
; /* h.type = NBD_REP_META_CONTEXT, h.length > 4 */
54 /* metadata context name follows */
55 } QEMU_PACKED NBDOptionReplyMetaContext
;
57 /* Track results of negotiation */
58 typedef enum NBDMode
{
59 /* Keep this list in a continuum of increasing features. */
60 NBD_MODE_OLDSTYLE
, /* server lacks newstyle negotiation */
61 NBD_MODE_EXPORT_NAME
, /* newstyle but only OPT_EXPORT_NAME safe */
62 NBD_MODE_SIMPLE
, /* newstyle but only simple replies */
63 NBD_MODE_STRUCTURED
, /* newstyle, structured replies enabled */
64 NBD_MODE_EXTENDED
, /* newstyle, extended headers enabled */
67 /* Transmission phase structs */
70 * Note: NBDRequest is _NOT_ the same as the network representation of an NBD
73 typedef struct NBDRequest
{
75 uint64_t from
; /* Offset touched by the command */
76 uint64_t len
; /* Effect length; 32 bit limit without extended headers */
77 uint16_t flags
; /* NBD_CMD_FLAG_* */
78 uint16_t type
; /* NBD_CMD_* */
79 NBDMode mode
; /* Determines which network representation to use */
80 NBDMetaContexts
*contexts
; /* Used by NBD_CMD_BLOCK_STATUS */
83 typedef struct NBDSimpleReply
{
84 uint32_t magic
; /* NBD_SIMPLE_REPLY_MAGIC */
87 } QEMU_PACKED NBDSimpleReply
;
89 /* Header of all structured replies */
90 typedef struct NBDStructuredReplyChunk
{
91 uint32_t magic
; /* NBD_STRUCTURED_REPLY_MAGIC */
92 uint16_t flags
; /* combination of NBD_REPLY_FLAG_* */
93 uint16_t type
; /* NBD_REPLY_TYPE_* */
94 uint64_t cookie
; /* request handle */
95 uint32_t length
; /* length of payload */
96 } QEMU_PACKED NBDStructuredReplyChunk
;
98 typedef struct NBDExtendedReplyChunk
{
99 uint32_t magic
; /* NBD_EXTENDED_REPLY_MAGIC */
100 uint16_t flags
; /* combination of NBD_REPLY_FLAG_* */
101 uint16_t type
; /* NBD_REPLY_TYPE_* */
102 uint64_t cookie
; /* request handle */
103 uint64_t offset
; /* request offset */
104 uint64_t length
; /* length of payload */
105 } QEMU_PACKED NBDExtendedReplyChunk
;
107 typedef union NBDReply
{
108 NBDSimpleReply simple
;
109 NBDStructuredReplyChunk structured
;
110 NBDExtendedReplyChunk extended
;
113 * @magic and @cookie fields have the same offset and size in all
114 * forms of replies, so let them be accessible without ".simple.",
115 * ".structured.", or ".extended." specifications.
122 QEMU_BUILD_BUG_ON(offsetof(NBDReply
, simple
.cookie
) !=
123 offsetof(NBDReply
, cookie
));
124 QEMU_BUILD_BUG_ON(offsetof(NBDReply
, structured
.cookie
) !=
125 offsetof(NBDReply
, cookie
));
126 QEMU_BUILD_BUG_ON(offsetof(NBDReply
, extended
.cookie
) !=
127 offsetof(NBDReply
, cookie
));
129 /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */
130 typedef struct NBDStructuredReadData
{
131 /* header's .length >= 9 */
133 /* At least one byte of data payload follows, calculated from h.length */
134 } QEMU_PACKED NBDStructuredReadData
;
136 /* Complete chunk for NBD_REPLY_TYPE_OFFSET_HOLE */
137 typedef struct NBDStructuredReadHole
{
138 /* header's length == 12 */
141 } QEMU_PACKED NBDStructuredReadHole
;
143 /* Header of all NBD_REPLY_TYPE_ERROR* errors */
144 typedef struct NBDStructuredError
{
145 /* header's length >= 6 */
147 uint16_t message_length
;
148 } QEMU_PACKED NBDStructuredError
;
150 /* Header of NBD_REPLY_TYPE_BLOCK_STATUS */
151 typedef struct NBDStructuredMeta
{
152 /* header's length >= 12 (at least one extent) */
154 /* NBDExtent32 extents[] follows, array length implied by header */
155 } QEMU_PACKED NBDStructuredMeta
;
157 /* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS */
158 typedef struct NBDExtent32
{
160 uint32_t flags
; /* NBD_STATE_* */
161 } QEMU_PACKED NBDExtent32
;
163 /* Header of NBD_REPLY_TYPE_BLOCK_STATUS_EXT */
164 typedef struct NBDExtendedMeta
{
165 /* header's length >= 24 (at least one extent) */
167 uint32_t count
; /* header length must be count * 16 + 8 */
168 /* NBDExtent64 extents[count] follows */
169 } QEMU_PACKED NBDExtendedMeta
;
171 /* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS_EXT */
172 typedef struct NBDExtent64
{
174 uint64_t flags
; /* NBD_STATE_* */
175 } QEMU_PACKED NBDExtent64
;
177 /* Client payload for limiting NBD_CMD_BLOCK_STATUS reply */
178 typedef struct NBDBlockStatusPayload
{
179 uint64_t effect_length
;
180 /* uint32_t ids[] follows, array length implied by header */
181 } QEMU_PACKED NBDBlockStatusPayload
;
183 /* Transmission (export) flags: sent from server to client during handshake,
184 but describe what will happen during transmission */
186 NBD_FLAG_HAS_FLAGS_BIT
= 0, /* Flags are there */
187 NBD_FLAG_READ_ONLY_BIT
= 1, /* Device is read-only */
188 NBD_FLAG_SEND_FLUSH_BIT
= 2, /* Send FLUSH */
189 NBD_FLAG_SEND_FUA_BIT
= 3, /* Send FUA (Force Unit Access) */
190 NBD_FLAG_ROTATIONAL_BIT
= 4, /* Use elevator algorithm -
192 NBD_FLAG_SEND_TRIM_BIT
= 5, /* Send TRIM (discard) */
193 NBD_FLAG_SEND_WRITE_ZEROES_BIT
= 6, /* Send WRITE_ZEROES */
194 NBD_FLAG_SEND_DF_BIT
= 7, /* Send DF (Do not Fragment) */
195 NBD_FLAG_CAN_MULTI_CONN_BIT
= 8, /* Multi-client cache consistent */
196 NBD_FLAG_SEND_RESIZE_BIT
= 9, /* Send resize */
197 NBD_FLAG_SEND_CACHE_BIT
= 10, /* Send CACHE (prefetch) */
198 NBD_FLAG_SEND_FAST_ZERO_BIT
= 11, /* FAST_ZERO flag for WRITE_ZEROES */
199 NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT
= 12, /* PAYLOAD flag for BLOCK_STATUS */
202 #define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT)
203 #define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT)
204 #define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT)
205 #define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT)
206 #define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT)
207 #define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT)
208 #define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT)
209 #define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT)
210 #define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT)
211 #define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT)
212 #define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT)
213 #define NBD_FLAG_SEND_FAST_ZERO (1 << NBD_FLAG_SEND_FAST_ZERO_BIT)
214 #define NBD_FLAG_BLOCK_STAT_PAYLOAD (1 << NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT)
216 /* New-style handshake (global) flags, sent from server to client, and
217 control what will happen during handshake phase. */
218 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
219 #define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
221 /* New-style client flags, sent from client to server to control what happens
222 during handshake phase. */
223 #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
224 #define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
226 /* Option requests. */
227 #define NBD_OPT_EXPORT_NAME (1)
228 #define NBD_OPT_ABORT (2)
229 #define NBD_OPT_LIST (3)
230 /* #define NBD_OPT_PEEK_EXPORT (4) not in use */
231 #define NBD_OPT_STARTTLS (5)
232 #define NBD_OPT_INFO (6)
233 #define NBD_OPT_GO (7)
234 #define NBD_OPT_STRUCTURED_REPLY (8)
235 #define NBD_OPT_LIST_META_CONTEXT (9)
236 #define NBD_OPT_SET_META_CONTEXT (10)
237 #define NBD_OPT_EXTENDED_HEADERS (11)
239 /* Option reply types. */
240 #define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value))
242 #define NBD_REP_ACK (1) /* Data sending finished. */
243 #define NBD_REP_SERVER (2) /* Export description. */
244 #define NBD_REP_INFO (3) /* NBD_OPT_INFO/GO. */
245 #define NBD_REP_META_CONTEXT (4) /* NBD_OPT_{LIST,SET}_META_CONTEXT */
247 #define NBD_REP_ERR_UNSUP NBD_REP_ERR(1) /* Unknown option */
248 #define NBD_REP_ERR_POLICY NBD_REP_ERR(2) /* Server denied */
249 #define NBD_REP_ERR_INVALID NBD_REP_ERR(3) /* Invalid length */
250 #define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4) /* Not compiled in */
251 #define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5) /* TLS required */
252 #define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */
253 #define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting down */
254 #define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZE */
255 #define NBD_REP_ERR_TOO_BIG NBD_REP_ERR(9) /* Payload size overflow */
256 #define NBD_REP_ERR_EXT_HEADER_REQD NBD_REP_ERR(10) /* Need extended headers */
258 /* Info types, used during NBD_REP_INFO */
259 #define NBD_INFO_EXPORT 0
260 #define NBD_INFO_NAME 1
261 #define NBD_INFO_DESCRIPTION 2
262 #define NBD_INFO_BLOCK_SIZE 3
264 /* Request flags, sent from client to server during transmission phase */
265 #define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during write */
266 #define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */
267 #define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read */
268 #define NBD_CMD_FLAG_REQ_ONE (1 << 3) \
269 /* only one extent in BLOCK_STATUS reply chunk */
270 #define NBD_CMD_FLAG_FAST_ZERO (1 << 4) /* fail if WRITE_ZEROES is not fast */
271 #define NBD_CMD_FLAG_PAYLOAD_LEN (1 << 5) \
272 /* length describes payload, not effect; only with ext header */
274 /* Supported request types */
282 NBD_CMD_WRITE_ZEROES
= 6,
283 NBD_CMD_BLOCK_STATUS
= 7,
286 #define NBD_DEFAULT_PORT 10809
288 /* Maximum size of a single READ/WRITE data buffer */
289 #define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)
292 * Maximum size of a protocol string (export name, metadata context name,
293 * etc.). Use malloc rather than stack allocation for storage of a
296 #define NBD_MAX_STRING_SIZE 4096
298 /* Two types of request structures, a given client will only use 1 */
299 #define NBD_REQUEST_MAGIC 0x25609513
300 #define NBD_EXTENDED_REQUEST_MAGIC 0x21e41c71
303 * Three types of reply structures, but what a client expects depends
304 * on NBD_OPT_STRUCTURED_REPLY and NBD_OPT_EXTENDED_HEADERS.
306 #define NBD_SIMPLE_REPLY_MAGIC 0x67446698
307 #define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef
308 #define NBD_EXTENDED_REPLY_MAGIC 0x6e8a278c
310 /* Chunk reply flags (for structured and extended replies) */
311 #define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last */
313 /* Chunk reply types */
314 #define NBD_REPLY_ERR(value) ((1 << 15) | (value))
316 #define NBD_REPLY_TYPE_NONE 0
317 #define NBD_REPLY_TYPE_OFFSET_DATA 1
318 #define NBD_REPLY_TYPE_OFFSET_HOLE 2
319 #define NBD_REPLY_TYPE_BLOCK_STATUS 5
320 #define NBD_REPLY_TYPE_BLOCK_STATUS_EXT 6
321 #define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)
322 #define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2)
324 /* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */
325 #define NBD_STATE_HOLE (1 << 0)
326 #define NBD_STATE_ZERO (1 << 1)
328 /* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */
329 #define NBD_STATE_DIRTY (1 << 0)
331 /* No flags needed for qemu:allocation-depth in NBD_REPLY_TYPE_BLOCK_STATUS */
333 static inline bool nbd_reply_type_is_error(int type
)
335 return type
& (1 << 15);
338 /* NBD errors are based on errno numbers, so there is a 1:1 mapping,
339 * but only a limited set of errno values is specified in the protocol.
340 * Everything else is squashed to EINVAL.
342 #define NBD_SUCCESS 0
345 #define NBD_ENOMEM 12
346 #define NBD_EINVAL 22
347 #define NBD_ENOSPC 28
348 #define NBD_EOVERFLOW 75
349 #define NBD_ENOTSUP 95
350 #define NBD_ESHUTDOWN 108
352 /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */
353 typedef struct NBDExportInfo
{
354 /* Set by client before nbd_receive_negotiate() */
356 char *x_dirty_bitmap
;
358 /* Set by client before nbd_receive_negotiate(), or by server results
359 * during nbd_receive_export_list() */
360 char *name
; /* must be non-NULL */
362 /* In-out fields, set by client before nbd_receive_negotiate() and
363 * updated by server results during nbd_receive_negotiate() */
364 NBDMode mode
; /* input maximum mode tolerated; output actual mode chosen */
365 bool base_allocation
; /* base:allocation context for NBD_CMD_BLOCK_STATUS */
367 /* Set by server results during nbd_receive_negotiate() and
368 * nbd_receive_export_list() */
377 /* Set by server results during nbd_receive_export_list() */
383 int nbd_receive_negotiate(QIOChannel
*ioc
, QCryptoTLSCreds
*tlscreds
,
384 const char *hostname
, QIOChannel
**outioc
,
385 NBDExportInfo
*info
, Error
**errp
);
386 void nbd_free_export_list(NBDExportInfo
*info
, int count
);
387 int nbd_receive_export_list(QIOChannel
*ioc
, QCryptoTLSCreds
*tlscreds
,
388 const char *hostname
, NBDExportInfo
**info
,
390 int nbd_init(int fd
, QIOChannelSocket
*sioc
, NBDExportInfo
*info
,
392 int nbd_send_request(QIOChannel
*ioc
, NBDRequest
*request
);
393 int coroutine_fn
nbd_receive_reply(BlockDriverState
*bs
, QIOChannel
*ioc
,
394 NBDReply
*reply
, NBDMode mode
,
396 int nbd_client(int fd
);
397 int nbd_disconnect(int fd
);
398 int nbd_errno_to_system_errno(int err
);
400 void nbd_export_set_on_eject_blk(BlockExport
*exp
, BlockBackend
*blk
);
402 AioContext
*nbd_export_aio_context(NBDExport
*exp
);
403 NBDExport
*nbd_export_find(const char *name
);
405 void nbd_client_new(QIOChannelSocket
*sioc
,
406 QCryptoTLSCreds
*tlscreds
,
407 const char *tlsauthz
,
408 void (*close_fn
)(NBDClient
*, bool));
409 void nbd_client_get(NBDClient
*client
);
410 void nbd_client_put(NBDClient
*client
);
412 void nbd_server_is_qemu_nbd(int max_connections
);
413 bool nbd_server_is_running(void);
414 int nbd_server_max_connections(void);
415 void nbd_server_start(SocketAddress
*addr
, const char *tls_creds
,
416 const char *tls_authz
, uint32_t max_connections
,
418 void nbd_server_start_options(NbdServerOptions
*arg
, Error
**errp
);
421 * Reads @size bytes from @ioc. Returns 0 on success.
423 static inline int nbd_read(QIOChannel
*ioc
, void *buffer
, size_t size
,
424 const char *desc
, Error
**errp
)
427 int ret
= qio_channel_read_all(ioc
, buffer
, size
, errp
) < 0 ? -EIO
: 0;
431 error_prepend(errp
, "Failed to read %s: ", desc
);
439 #define DEF_NBD_READ_N(bits) \
440 static inline int nbd_read##bits(QIOChannel *ioc, \
441 uint##bits##_t *val, \
442 const char *desc, Error **errp) \
444 int ret = nbd_read(ioc, val, sizeof(*val), desc, errp); \
448 *val = be##bits##_to_cpu(*val); \
452 DEF_NBD_READ_N(16) /* Defines nbd_read16(). */
453 DEF_NBD_READ_N(32) /* Defines nbd_read32(). */
454 DEF_NBD_READ_N(64) /* Defines nbd_read64(). */
456 #undef DEF_NBD_READ_N
458 static inline bool nbd_reply_is_simple(NBDReply
*reply
)
460 return reply
->magic
== NBD_SIMPLE_REPLY_MAGIC
;
463 static inline bool nbd_reply_is_structured(NBDReply
*reply
)
465 return reply
->magic
== NBD_STRUCTURED_REPLY_MAGIC
;
468 const char *nbd_reply_type_lookup(uint16_t type
);
469 const char *nbd_opt_lookup(uint32_t opt
);
470 const char *nbd_rep_lookup(uint32_t rep
);
471 const char *nbd_info_lookup(uint16_t info
);
472 const char *nbd_cmd_lookup(uint16_t info
);
473 const char *nbd_err_lookup(int err
);
474 const char *nbd_mode_lookup(NBDMode mode
);
476 /* nbd/client-connection.c */
477 void nbd_client_connection_enable_retry(NBDClientConnection
*conn
);
479 NBDClientConnection
*nbd_client_connection_new(const SocketAddress
*saddr
,
481 const char *export_name
,
482 const char *x_dirty_bitmap
,
483 QCryptoTLSCreds
*tlscreds
,
484 const char *tlshostname
);
485 void nbd_client_connection_release(NBDClientConnection
*conn
);
487 QIOChannel
*coroutine_fn
488 nbd_co_establish_connection(NBDClientConnection
*conn
, NBDExportInfo
*info
,
489 bool blocking
, Error
**errp
);
491 void nbd_co_establish_connection_cancel(NBDClientConnection
*conn
);