]> git.proxmox.com Git - mirror_qemu.git/blob - include/block/nbd.h
nbd/server: Prepare for alternate-size headers
[mirror_qemu.git] / include / block / nbd.h
1 /*
2 * Copyright Red Hat
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef NBD_H
21 #define NBD_H
22
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"
28
29 typedef struct NBDExport NBDExport;
30 typedef struct NBDClient NBDClient;
31 typedef struct NBDClientConnection NBDClientConnection;
32
33 extern const BlockExportDriver blk_exp_nbd;
34
35 /* Handshake phase structs - this struct is passed on the wire */
36
37 typedef struct NBDOption {
38 uint64_t magic; /* NBD_OPTS_MAGIC */
39 uint32_t option; /* NBD_OPT_* */
40 uint32_t length;
41 } QEMU_PACKED NBDOption;
42
43 typedef struct NBDOptionReply {
44 uint64_t magic; /* NBD_REP_MAGIC */
45 uint32_t option; /* NBD_OPT_* */
46 uint32_t type; /* NBD_REP_* */
47 uint32_t length;
48 } QEMU_PACKED NBDOptionReply;
49
50 typedef struct NBDOptionReplyMetaContext {
51 NBDOptionReply h; /* h.type = NBD_REP_META_CONTEXT, h.length > 4 */
52 uint32_t context_id;
53 /* metadata context name follows */
54 } QEMU_PACKED NBDOptionReplyMetaContext;
55
56 /* Transmission phase structs
57 *
58 * Note: these are _NOT_ the same as the network representation of an NBD
59 * request and reply!
60 */
61 typedef struct NBDRequest {
62 uint64_t handle;
63 uint64_t from;
64 uint32_t len;
65 uint16_t flags; /* NBD_CMD_FLAG_* */
66 uint16_t type; /* NBD_CMD_* */
67 } NBDRequest;
68
69 typedef struct NBDSimpleReply {
70 uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */
71 uint32_t error;
72 uint64_t handle;
73 } QEMU_PACKED NBDSimpleReply;
74
75 /* Header of all structured replies */
76 typedef struct NBDStructuredReplyChunk {
77 uint32_t magic; /* NBD_STRUCTURED_REPLY_MAGIC */
78 uint16_t flags; /* combination of NBD_REPLY_FLAG_* */
79 uint16_t type; /* NBD_REPLY_TYPE_* */
80 uint64_t handle; /* request handle */
81 uint32_t length; /* length of payload */
82 } QEMU_PACKED NBDStructuredReplyChunk;
83
84 typedef union NBDReply {
85 NBDSimpleReply simple;
86 NBDStructuredReplyChunk structured;
87 struct {
88 /* @magic and @handle fields have the same offset and size both in
89 * simple reply and structured reply chunk, so let them be accessible
90 * without ".simple." or ".structured." specification
91 */
92 uint32_t magic;
93 uint32_t _skip;
94 uint64_t handle;
95 } QEMU_PACKED;
96 } NBDReply;
97
98 /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */
99 typedef struct NBDStructuredReadData {
100 /* header's .length >= 9 */
101 uint64_t offset;
102 /* At least one byte of data payload follows, calculated from h.length */
103 } QEMU_PACKED NBDStructuredReadData;
104
105 /* Complete chunk for NBD_REPLY_TYPE_OFFSET_HOLE */
106 typedef struct NBDStructuredReadHole {
107 /* header's length == 12 */
108 uint64_t offset;
109 uint32_t length;
110 } QEMU_PACKED NBDStructuredReadHole;
111
112 /* Header of all NBD_REPLY_TYPE_ERROR* errors */
113 typedef struct NBDStructuredError {
114 /* header's length >= 6 */
115 uint32_t error;
116 uint16_t message_length;
117 } QEMU_PACKED NBDStructuredError;
118
119 /* Header of NBD_REPLY_TYPE_BLOCK_STATUS */
120 typedef struct NBDStructuredMeta {
121 /* header's length >= 12 (at least one extent) */
122 uint32_t context_id;
123 /* extents follows */
124 } QEMU_PACKED NBDStructuredMeta;
125
126 /* Extent chunk for NBD_REPLY_TYPE_BLOCK_STATUS */
127 typedef struct NBDExtent {
128 uint32_t length;
129 uint32_t flags; /* NBD_STATE_* */
130 } QEMU_PACKED NBDExtent;
131
132 /* Transmission (export) flags: sent from server to client during handshake,
133 but describe what will happen during transmission */
134 enum {
135 NBD_FLAG_HAS_FLAGS_BIT = 0, /* Flags are there */
136 NBD_FLAG_READ_ONLY_BIT = 1, /* Device is read-only */
137 NBD_FLAG_SEND_FLUSH_BIT = 2, /* Send FLUSH */
138 NBD_FLAG_SEND_FUA_BIT = 3, /* Send FUA (Force Unit Access) */
139 NBD_FLAG_ROTATIONAL_BIT = 4, /* Use elevator algorithm -
140 rotational media */
141 NBD_FLAG_SEND_TRIM_BIT = 5, /* Send TRIM (discard) */
142 NBD_FLAG_SEND_WRITE_ZEROES_BIT = 6, /* Send WRITE_ZEROES */
143 NBD_FLAG_SEND_DF_BIT = 7, /* Send DF (Do not Fragment) */
144 NBD_FLAG_CAN_MULTI_CONN_BIT = 8, /* Multi-client cache consistent */
145 NBD_FLAG_SEND_RESIZE_BIT = 9, /* Send resize */
146 NBD_FLAG_SEND_CACHE_BIT = 10, /* Send CACHE (prefetch) */
147 NBD_FLAG_SEND_FAST_ZERO_BIT = 11, /* FAST_ZERO flag for WRITE_ZEROES */
148 };
149
150 #define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT)
151 #define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT)
152 #define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT)
153 #define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT)
154 #define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT)
155 #define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT)
156 #define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT)
157 #define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT)
158 #define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT)
159 #define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT)
160 #define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT)
161 #define NBD_FLAG_SEND_FAST_ZERO (1 << NBD_FLAG_SEND_FAST_ZERO_BIT)
162
163 /* New-style handshake (global) flags, sent from server to client, and
164 control what will happen during handshake phase. */
165 #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
166 #define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
167
168 /* New-style client flags, sent from client to server to control what happens
169 during handshake phase. */
170 #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
171 #define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
172
173 /* Option requests. */
174 #define NBD_OPT_EXPORT_NAME (1)
175 #define NBD_OPT_ABORT (2)
176 #define NBD_OPT_LIST (3)
177 /* #define NBD_OPT_PEEK_EXPORT (4) not in use */
178 #define NBD_OPT_STARTTLS (5)
179 #define NBD_OPT_INFO (6)
180 #define NBD_OPT_GO (7)
181 #define NBD_OPT_STRUCTURED_REPLY (8)
182 #define NBD_OPT_LIST_META_CONTEXT (9)
183 #define NBD_OPT_SET_META_CONTEXT (10)
184
185 /* Option reply types. */
186 #define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value))
187
188 #define NBD_REP_ACK (1) /* Data sending finished. */
189 #define NBD_REP_SERVER (2) /* Export description. */
190 #define NBD_REP_INFO (3) /* NBD_OPT_INFO/GO. */
191 #define NBD_REP_META_CONTEXT (4) /* NBD_OPT_{LIST,SET}_META_CONTEXT */
192
193 #define NBD_REP_ERR_UNSUP NBD_REP_ERR(1) /* Unknown option */
194 #define NBD_REP_ERR_POLICY NBD_REP_ERR(2) /* Server denied */
195 #define NBD_REP_ERR_INVALID NBD_REP_ERR(3) /* Invalid length */
196 #define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4) /* Not compiled in */
197 #define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5) /* TLS required */
198 #define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */
199 #define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting down */
200 #define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZE */
201
202 /* Info types, used during NBD_REP_INFO */
203 #define NBD_INFO_EXPORT 0
204 #define NBD_INFO_NAME 1
205 #define NBD_INFO_DESCRIPTION 2
206 #define NBD_INFO_BLOCK_SIZE 3
207
208 /* Request flags, sent from client to server during transmission phase */
209 #define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during write */
210 #define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */
211 #define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read */
212 #define NBD_CMD_FLAG_REQ_ONE (1 << 3) /* only one extent in BLOCK_STATUS
213 * reply chunk */
214 #define NBD_CMD_FLAG_FAST_ZERO (1 << 4) /* fail if WRITE_ZEROES is not fast */
215
216 /* Supported request types */
217 enum {
218 NBD_CMD_READ = 0,
219 NBD_CMD_WRITE = 1,
220 NBD_CMD_DISC = 2,
221 NBD_CMD_FLUSH = 3,
222 NBD_CMD_TRIM = 4,
223 NBD_CMD_CACHE = 5,
224 NBD_CMD_WRITE_ZEROES = 6,
225 NBD_CMD_BLOCK_STATUS = 7,
226 };
227
228 #define NBD_DEFAULT_PORT 10809
229
230 /* Maximum size of a single READ/WRITE data buffer */
231 #define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)
232
233 /*
234 * Maximum size of a protocol string (export name, metadata context name,
235 * etc.). Use malloc rather than stack allocation for storage of a
236 * string.
237 */
238 #define NBD_MAX_STRING_SIZE 4096
239
240 /* Two types of reply structures */
241 #define NBD_SIMPLE_REPLY_MAGIC 0x67446698
242 #define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef
243
244 /* Structured reply flags */
245 #define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last */
246
247 /* Structured reply types */
248 #define NBD_REPLY_ERR(value) ((1 << 15) | (value))
249
250 #define NBD_REPLY_TYPE_NONE 0
251 #define NBD_REPLY_TYPE_OFFSET_DATA 1
252 #define NBD_REPLY_TYPE_OFFSET_HOLE 2
253 #define NBD_REPLY_TYPE_BLOCK_STATUS 5
254 #define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)
255 #define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2)
256
257 /* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */
258 #define NBD_STATE_HOLE (1 << 0)
259 #define NBD_STATE_ZERO (1 << 1)
260
261 /* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */
262 #define NBD_STATE_DIRTY (1 << 0)
263
264 /* No flags needed for qemu:allocation-depth in NBD_REPLY_TYPE_BLOCK_STATUS */
265
266 static inline bool nbd_reply_type_is_error(int type)
267 {
268 return type & (1 << 15);
269 }
270
271 /* NBD errors are based on errno numbers, so there is a 1:1 mapping,
272 * but only a limited set of errno values is specified in the protocol.
273 * Everything else is squashed to EINVAL.
274 */
275 #define NBD_SUCCESS 0
276 #define NBD_EPERM 1
277 #define NBD_EIO 5
278 #define NBD_ENOMEM 12
279 #define NBD_EINVAL 22
280 #define NBD_ENOSPC 28
281 #define NBD_EOVERFLOW 75
282 #define NBD_ENOTSUP 95
283 #define NBD_ESHUTDOWN 108
284
285 /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */
286 typedef struct NBDExportInfo {
287 /* Set by client before nbd_receive_negotiate() */
288 bool request_sizes;
289 char *x_dirty_bitmap;
290
291 /* Set by client before nbd_receive_negotiate(), or by server results
292 * during nbd_receive_export_list() */
293 char *name; /* must be non-NULL */
294
295 /* In-out fields, set by client before nbd_receive_negotiate() and
296 * updated by server results during nbd_receive_negotiate() */
297 bool structured_reply;
298 bool base_allocation; /* base:allocation context for NBD_CMD_BLOCK_STATUS */
299
300 /* Set by server results during nbd_receive_negotiate() and
301 * nbd_receive_export_list() */
302 uint64_t size;
303 uint16_t flags;
304 uint32_t min_block;
305 uint32_t opt_block;
306 uint32_t max_block;
307
308 uint32_t context_id;
309
310 /* Set by server results during nbd_receive_export_list() */
311 char *description;
312 int n_contexts;
313 char **contexts;
314 } NBDExportInfo;
315
316 int nbd_receive_negotiate(AioContext *aio_context, QIOChannel *ioc,
317 QCryptoTLSCreds *tlscreds,
318 const char *hostname, QIOChannel **outioc,
319 NBDExportInfo *info, Error **errp);
320 void nbd_free_export_list(NBDExportInfo *info, int count);
321 int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
322 const char *hostname, NBDExportInfo **info,
323 Error **errp);
324 int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
325 Error **errp);
326 int nbd_send_request(QIOChannel *ioc, NBDRequest *request);
327 int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc,
328 NBDReply *reply, Error **errp);
329 int nbd_client(int fd);
330 int nbd_disconnect(int fd);
331 int nbd_errno_to_system_errno(int err);
332
333 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk);
334
335 AioContext *nbd_export_aio_context(NBDExport *exp);
336 NBDExport *nbd_export_find(const char *name);
337
338 void nbd_client_new(QIOChannelSocket *sioc,
339 QCryptoTLSCreds *tlscreds,
340 const char *tlsauthz,
341 void (*close_fn)(NBDClient *, bool));
342 void nbd_client_get(NBDClient *client);
343 void nbd_client_put(NBDClient *client);
344
345 void nbd_server_is_qemu_nbd(int max_connections);
346 bool nbd_server_is_running(void);
347 int nbd_server_max_connections(void);
348 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
349 const char *tls_authz, uint32_t max_connections,
350 Error **errp);
351 void nbd_server_start_options(NbdServerOptions *arg, Error **errp);
352
353 /* nbd_read
354 * Reads @size bytes from @ioc. Returns 0 on success.
355 */
356 static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
357 const char *desc, Error **errp)
358 {
359 ERRP_GUARD();
360 int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
361
362 if (ret < 0) {
363 if (desc) {
364 error_prepend(errp, "Failed to read %s: ", desc);
365 }
366 return ret;
367 }
368
369 return 0;
370 }
371
372 #define DEF_NBD_READ_N(bits) \
373 static inline int nbd_read##bits(QIOChannel *ioc, \
374 uint##bits##_t *val, \
375 const char *desc, Error **errp) \
376 { \
377 int ret = nbd_read(ioc, val, sizeof(*val), desc, errp); \
378 if (ret < 0) { \
379 return ret; \
380 } \
381 *val = be##bits##_to_cpu(*val); \
382 return 0; \
383 }
384
385 DEF_NBD_READ_N(16) /* Defines nbd_read16(). */
386 DEF_NBD_READ_N(32) /* Defines nbd_read32(). */
387 DEF_NBD_READ_N(64) /* Defines nbd_read64(). */
388
389 #undef DEF_NBD_READ_N
390
391 static inline bool nbd_reply_is_simple(NBDReply *reply)
392 {
393 return reply->magic == NBD_SIMPLE_REPLY_MAGIC;
394 }
395
396 static inline bool nbd_reply_is_structured(NBDReply *reply)
397 {
398 return reply->magic == NBD_STRUCTURED_REPLY_MAGIC;
399 }
400
401 const char *nbd_reply_type_lookup(uint16_t type);
402 const char *nbd_opt_lookup(uint32_t opt);
403 const char *nbd_rep_lookup(uint32_t rep);
404 const char *nbd_info_lookup(uint16_t info);
405 const char *nbd_cmd_lookup(uint16_t info);
406 const char *nbd_err_lookup(int err);
407
408 /* nbd/client-connection.c */
409 void nbd_client_connection_enable_retry(NBDClientConnection *conn);
410
411 NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
412 bool do_negotiation,
413 const char *export_name,
414 const char *x_dirty_bitmap,
415 QCryptoTLSCreds *tlscreds,
416 const char *tlshostname);
417 void nbd_client_connection_release(NBDClientConnection *conn);
418
419 QIOChannel *coroutine_fn
420 nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
421 bool blocking, Error **errp);
422
423 void nbd_co_establish_connection_cancel(NBDClientConnection *conn);
424
425 #endif