]> Git Repo - qemu.git/blame - include/block/nbd.h
qcow2: Creating images with external data file
[qemu.git] / include / block / nbd.h
CommitLineData
75818250 1/*
d21a2d34 2 * Copyright (C) 2016-2019 Red Hat, Inc.
7a5ca864
FB
3 * Copyright (C) 2005 Anthony Liguori <[email protected]>
4 *
5 * Network Block Device
6 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
75818250 18 */
7a5ca864
FB
19
20#ifndef NBD_H
21#define NBD_H
22
9af23989 23#include "qapi/qapi-types-block.h"
1c778ef7 24#include "io/channel-socket.h"
f95910fe 25#include "crypto/tlscreds.h"
e6798f06 26#include "qapi/error.h"
c12504ce 27
c8a3a1b6
EB
28/* Handshake phase structs - this struct is passed on the wire */
29
420a4e95 30struct NBDOption {
c8a3a1b6
EB
31 uint64_t magic; /* NBD_OPTS_MAGIC */
32 uint32_t option; /* NBD_OPT_* */
33 uint32_t length;
34} QEMU_PACKED;
420a4e95 35typedef struct NBDOption NBDOption;
c8a3a1b6 36
420a4e95 37struct NBDOptionReply {
c8a3a1b6
EB
38 uint64_t magic; /* NBD_REP_MAGIC */
39 uint32_t option; /* NBD_OPT_* */
40 uint32_t type; /* NBD_REP_* */
41 uint32_t length;
42} QEMU_PACKED;
420a4e95 43typedef struct NBDOptionReply NBDOptionReply;
c8a3a1b6 44
25c14678
VSO
45typedef struct NBDOptionReplyMetaContext {
46 NBDOptionReply h; /* h.type = NBD_REP_META_CONTEXT, h.length > 4 */
47 uint32_t context_id;
48 /* meta context name follows */
49} QEMU_PACKED NBDOptionReplyMetaContext;
50
c8a3a1b6
EB
51/* Transmission phase structs
52 *
53 * Note: these are _NOT_ the same as the network representation of an NBD
56af2dda
PB
54 * request and reply!
55 */
ed2dd912 56struct NBDRequest {
75818250
TS
57 uint64_t handle;
58 uint64_t from;
59 uint32_t len;
c8a3a1b6
EB
60 uint16_t flags; /* NBD_CMD_FLAG_* */
61 uint16_t type; /* NBD_CMD_* */
56af2dda 62};
ed2dd912 63typedef struct NBDRequest NBDRequest;
75818250 64
caad5384
VSO
65typedef struct NBDSimpleReply {
66 uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */
67 uint32_t error;
68 uint64_t handle;
69} QEMU_PACKED NBDSimpleReply;
70
bae245d1
EB
71/* Header of all structured replies */
72typedef struct NBDStructuredReplyChunk {
73 uint32_t magic; /* NBD_STRUCTURED_REPLY_MAGIC */
74 uint16_t flags; /* combination of NBD_REPLY_FLAG_* */
75 uint16_t type; /* NBD_REPLY_TYPE_* */
76 uint64_t handle; /* request handle */
77 uint32_t length; /* length of payload */
78} QEMU_PACKED NBDStructuredReplyChunk;
79
d2febedb
VSO
80typedef union NBDReply {
81 NBDSimpleReply simple;
82 NBDStructuredReplyChunk structured;
83 struct {
84 /* @magic and @handle fields have the same offset and size both in
85 * simple reply and structured reply chunk, so let them be accessible
86 * without ".simple." or ".structured." specification
87 */
88 uint32_t magic;
89 uint32_t _skip;
90 uint64_t handle;
91 } QEMU_PACKED;
92} NBDReply;
93
efdc0c10
EB
94/* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */
95typedef struct NBDStructuredReadData {
96 NBDStructuredReplyChunk h; /* h.length >= 9 */
bae245d1 97 uint64_t offset;
efdc0c10
EB
98 /* At least one byte of data payload follows, calculated from h.length */
99} QEMU_PACKED NBDStructuredReadData;
100
101/* Complete chunk for NBD_REPLY_TYPE_OFFSET_HOLE */
102typedef struct NBDStructuredReadHole {
103 NBDStructuredReplyChunk h; /* h.length == 12 */
104 uint64_t offset;
105 uint32_t length;
106} QEMU_PACKED NBDStructuredReadHole;
bae245d1
EB
107
108/* Header of all NBD_REPLY_TYPE_ERROR* errors */
109typedef struct NBDStructuredError {
efdc0c10 110 NBDStructuredReplyChunk h; /* h.length >= 6 */
bae245d1
EB
111 uint32_t error;
112 uint16_t message_length;
113} QEMU_PACKED NBDStructuredError;
114
25c14678
VSO
115/* Header of NBD_REPLY_TYPE_BLOCK_STATUS */
116typedef struct NBDStructuredMeta {
117 NBDStructuredReplyChunk h; /* h.length >= 12 (at least one extent) */
118 uint32_t context_id;
119 /* extents follows */
120} QEMU_PACKED NBDStructuredMeta;
121
122/* Extent chunk for NBD_REPLY_TYPE_BLOCK_STATUS */
123typedef struct NBDExtent {
124 uint32_t length;
125 uint32_t flags; /* NBD_STATE_* */
126} QEMU_PACKED NBDExtent;
127
b626b51a
EB
128/* Transmission (export) flags: sent from server to client during handshake,
129 but describe what will happen during transmission */
92652b12
VSO
130#define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
131#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
132#define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
133#define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */
134#define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm -
135 rotational media */
136#define NBD_FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */
137#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6) /* Send WRITE_ZEROES */
bae245d1 138#define NBD_FLAG_SEND_DF (1 << 7) /* Send DF (Do not Fragment) */
df91328a
DL
139#define NBD_FLAG_CAN_MULTI_CONN (1 << 8) /* Multi-client cache consistent */
140#define NBD_FLAG_SEND_RESIZE (1 << 9) /* Send resize */
141#define NBD_FLAG_SEND_CACHE (1 << 10) /* Send CACHE (prefetch) */
bbb74edd 142
b626b51a
EB
143/* New-style handshake (global) flags, sent from server to client, and
144 control what will happen during handshake phase. */
c203c59a
EB
145#define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
146#define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
f5076b5a 147
b626b51a
EB
148/* New-style client flags, sent from client to server to control what happens
149 during handshake phase. */
c203c59a
EB
150#define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
151#define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
f5076b5a 152
3736cc5b 153/* Option requests. */
6bc86957
VSO
154#define NBD_OPT_EXPORT_NAME (1)
155#define NBD_OPT_ABORT (2)
156#define NBD_OPT_LIST (3)
157/* #define NBD_OPT_PEEK_EXPORT (4) not in use */
158#define NBD_OPT_STARTTLS (5)
159#define NBD_OPT_INFO (6)
160#define NBD_OPT_GO (7)
161#define NBD_OPT_STRUCTURED_REPLY (8)
25c14678
VSO
162#define NBD_OPT_LIST_META_CONTEXT (9)
163#define NBD_OPT_SET_META_CONTEXT (10)
3736cc5b
EB
164
165/* Option reply types. */
b6f5d3b5
EB
166#define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value))
167
6bc86957
VSO
168#define NBD_REP_ACK (1) /* Data sending finished. */
169#define NBD_REP_SERVER (2) /* Export description. */
170#define NBD_REP_INFO (3) /* NBD_OPT_INFO/GO. */
25c14678 171#define NBD_REP_META_CONTEXT (4) /* NBD_OPT_{LIST,SET}_META_CONTEXT */
3736cc5b
EB
172
173#define NBD_REP_ERR_UNSUP NBD_REP_ERR(1) /* Unknown option */
174#define NBD_REP_ERR_POLICY NBD_REP_ERR(2) /* Server denied */
175#define NBD_REP_ERR_INVALID NBD_REP_ERR(3) /* Invalid length */
176#define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4) /* Not compiled in */
177#define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5) /* TLS required */
178#define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */
179#define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting down */
180#define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZE */
181
182/* Info types, used during NBD_REP_INFO */
183#define NBD_INFO_EXPORT 0
184#define NBD_INFO_NAME 1
185#define NBD_INFO_DESCRIPTION 2
186#define NBD_INFO_BLOCK_SIZE 3
f95910fe 187
b626b51a 188/* Request flags, sent from client to server during transmission phase */
1f4d6d18
EB
189#define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during write */
190#define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */
bae245d1 191#define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read */
25c14678
VSO
192#define NBD_CMD_FLAG_REQ_ONE (1 << 3) /* only one extent in BLOCK_STATUS
193 * reply chunk */
f5076b5a 194
b626b51a 195/* Supported request types */
75818250
TS
196enum {
197 NBD_CMD_READ = 0,
198 NBD_CMD_WRITE = 1,
bbb74edd
PB
199 NBD_CMD_DISC = 2,
200 NBD_CMD_FLUSH = 3,
1f4d6d18 201 NBD_CMD_TRIM = 4,
bc37b06a 202 NBD_CMD_CACHE = 5,
1f4d6d18 203 NBD_CMD_WRITE_ZEROES = 6,
25c14678 204 NBD_CMD_BLOCK_STATUS = 7,
75818250
TS
205};
206
1d45f8b5
LV
207#define NBD_DEFAULT_PORT 10809
208
2d821488
SH
209/* Maximum size of a single READ/WRITE data buffer */
210#define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)
476b923c 211
943cec86
EB
212/* Maximum size of an export name. The NBD spec requires 256 and
213 * suggests that servers support up to 4096, but we stick to only the
214 * required size so that we can stack-allocate the names, and because
215 * going larger would require an audit of more code to make sure we
216 * aren't overflowing some other buffer. */
217#define NBD_MAX_NAME_SIZE 256
3777b09f 218
bae245d1
EB
219/* Two types of reply structures */
220#define NBD_SIMPLE_REPLY_MAGIC 0x67446698
221#define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef
222
223/* Structured reply flags */
224#define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last */
225
226/* Structured reply types */
227#define NBD_REPLY_ERR(value) ((1 << 15) | (value))
228
229#define NBD_REPLY_TYPE_NONE 0
230#define NBD_REPLY_TYPE_OFFSET_DATA 1
231#define NBD_REPLY_TYPE_OFFSET_HOLE 2
25c14678 232#define NBD_REPLY_TYPE_BLOCK_STATUS 5
bae245d1
EB
233#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)
234#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2)
235
3d068aff 236/* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */
25c14678
VSO
237#define NBD_STATE_HOLE (1 << 0)
238#define NBD_STATE_ZERO (1 << 1)
239
3d068aff
VSO
240/* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */
241#define NBD_STATE_DIRTY (1 << 0)
242
f140e300
VSO
243static inline bool nbd_reply_type_is_error(int type)
244{
245 return type & (1 << 15);
246}
247
dd689440
EB
248/* NBD errors are based on errno numbers, so there is a 1:1 mapping,
249 * but only a limited set of errno values is specified in the protocol.
250 * Everything else is squashed to EINVAL.
251 */
252#define NBD_SUCCESS 0
253#define NBD_EPERM 1
254#define NBD_EIO 5
255#define NBD_ENOMEM 12
256#define NBD_EINVAL 22
257#define NBD_ENOSPC 28
bae245d1 258#define NBD_EOVERFLOW 75
dd689440
EB
259#define NBD_ESHUTDOWN 108
260
004a89fc
EB
261/* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */
262struct NBDExportInfo {
081dd1fe
EB
263 /* Set by client before nbd_receive_negotiate() */
264 bool request_sizes;
216ee365 265 char *x_dirty_bitmap;
d21a2d34
EB
266
267 /* Set by client before nbd_receive_negotiate(), or by server results
268 * during nbd_receive_export_list() */
6dc1667d 269 char *name; /* must be non-NULL */
f140e300
VSO
270
271 /* In-out fields, set by client before nbd_receive_negotiate() and
272 * updated by server results during nbd_receive_negotiate() */
273 bool structured_reply;
78a33ab5 274 bool base_allocation; /* base:allocation context for NBD_CMD_BLOCK_STATUS */
f140e300 275
d21a2d34
EB
276 /* Set by server results during nbd_receive_negotiate() and
277 * nbd_receive_export_list() */
004a89fc
EB
278 uint64_t size;
279 uint16_t flags;
081dd1fe
EB
280 uint32_t min_block;
281 uint32_t opt_block;
282 uint32_t max_block;
78a33ab5 283
2df94eb5 284 uint32_t context_id;
d21a2d34
EB
285
286 /* Set by server results during nbd_receive_export_list() */
287 char *description;
0b576b6b
EB
288 int n_contexts;
289 char **contexts;
004a89fc
EB
290};
291typedef struct NBDExportInfo NBDExportInfo;
292
6dc1667d
EB
293int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
294 const char *hostname, QIOChannel **outioc,
295 NBDExportInfo *info, Error **errp);
d21a2d34
EB
296void nbd_free_export_list(NBDExportInfo *info, int count);
297int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
298 const char *hostname, NBDExportInfo **info,
299 Error **errp);
004a89fc 300int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
be41c100 301 Error **errp);
490dc5ed 302int nbd_send_request(QIOChannel *ioc, NBDRequest *request);
d3bd5b90
KW
303int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc,
304 NBDReply *reply, Error **errp);
0a4eb864 305int nbd_client(int fd);
7a5ca864 306int nbd_disconnect(int fd);
dd689440 307int nbd_errno_to_system_errno(int err);
7a5ca864 308
af49bbbe 309typedef struct NBDExport NBDExport;
1743b515 310typedef struct NBDClient NBDClient;
af49bbbe 311
9d26dfcb
EB
312NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
313 uint64_t size, const char *name, const char *desc,
678ba275
EB
314 const char *bitmap, uint16_t nbdflags,
315 void (*close)(NBDExport *), bool writethrough,
316 BlockBackend *on_eject_blk, Error **errp);
af49bbbe 317void nbd_export_close(NBDExport *exp);
a3b0dc75 318void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp);
2c8d9f06
PB
319void nbd_export_get(NBDExport *exp);
320void nbd_export_put(NBDExport *exp);
ce33967a 321
e140177d 322BlockBackend *nbd_export_get_blockdev(NBDExport *exp);
125afda8 323
ee0a19ec 324NBDExport *nbd_export_find(const char *name);
ee0a19ec
PB
325void nbd_export_close_all(void);
326
7f7dfe2a 327void nbd_client_new(QIOChannelSocket *sioc,
f95910fe
DB
328 QCryptoTLSCreds *tlscreds,
329 const char *tlsaclname,
0c9390d9 330 void (*close_fn)(NBDClient *, bool));
ce33967a
PB
331void nbd_client_get(NBDClient *client);
332void nbd_client_put(NBDClient *client);
af49bbbe 333
bd269ebc
MA
334void nbd_server_start(SocketAddress *addr, const char *tls_creds,
335 Error **errp);
336
56dc682b
EB
337/* nbd_read
338 * Reads @size bytes from @ioc. Returns 0 on success.
339 */
340static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
e6798f06 341 const char *desc, Error **errp)
56dc682b 342{
e6798f06
VSO
343 int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
344
345 if (ret < 0) {
346 if (desc) {
347 error_prepend(errp, "Failed to read %s: ", desc);
348 }
349 return -1;
350 }
351
352 return 0;
353}
354
355#define DEF_NBD_READ_N(bits) \
356static inline int nbd_read##bits(QIOChannel *ioc, \
357 uint##bits##_t *val, \
358 const char *desc, Error **errp) \
359{ \
360 if (nbd_read(ioc, val, sizeof(*val), desc, errp) < 0) { \
361 return -1; \
362 } \
363 *val = be##bits##_to_cpu(*val); \
364 return 0; \
56dc682b
EB
365}
366
e6798f06
VSO
367DEF_NBD_READ_N(16) /* Defines nbd_read16(). */
368DEF_NBD_READ_N(32) /* Defines nbd_read32(). */
369DEF_NBD_READ_N(64) /* Defines nbd_read64(). */
370
371#undef DEF_NBD_READ_N
372
d2febedb
VSO
373static inline bool nbd_reply_is_simple(NBDReply *reply)
374{
375 return reply->magic == NBD_SIMPLE_REPLY_MAGIC;
376}
377
378static inline bool nbd_reply_is_structured(NBDReply *reply)
379{
380 return reply->magic == NBD_STRUCTURED_REPLY_MAGIC;
381}
382
f140e300 383const char *nbd_reply_type_lookup(uint16_t type);
757a0d05
VSO
384const char *nbd_opt_lookup(uint32_t opt);
385const char *nbd_rep_lookup(uint32_t rep);
386const char *nbd_info_lookup(uint16_t info);
387const char *nbd_cmd_lookup(uint16_t info);
388const char *nbd_err_lookup(int err);
f140e300 389
7a5ca864 390#endif
This page took 0.765606 seconds and 4 git commands to generate.