1 | /* |
2 | * Copyright (C) 2016-2019 Red Hat, Inc. |
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 "qapi/qapi-types-block.h" |
24 | #include "io/channel-socket.h" |
25 | #include "crypto/tlscreds.h" |
26 | #include "qapi/error.h" |
27 | |
28 | /* Handshake phase structs - this struct is passed on the wire */ |
29 | |
30 | struct NBDOption { |
31 | uint64_t magic; /* NBD_OPTS_MAGIC */ |
32 | uint32_t option; /* NBD_OPT_* */ |
33 | uint32_t length; |
34 | } QEMU_PACKED; |
35 | typedef struct NBDOption NBDOption; |
36 | |
37 | struct NBDOptionReply { |
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; |
43 | typedef struct NBDOptionReply NBDOptionReply; |
44 | |
45 | typedef 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 | |
51 | /* Transmission phase structs |
52 | * |
53 | * Note: these are _NOT_ the same as the network representation of an NBD |
54 | * request and reply! |
55 | */ |
56 | struct NBDRequest { |
57 | uint64_t handle; |
58 | uint64_t from; |
59 | uint32_t len; |
60 | uint16_t flags; /* NBD_CMD_FLAG_* */ |
61 | uint16_t type; /* NBD_CMD_* */ |
62 | }; |
63 | typedef struct NBDRequest NBDRequest; |
64 | |
65 | typedef struct NBDSimpleReply { |
66 | uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */ |
67 | uint32_t error; |
68 | uint64_t handle; |
69 | } QEMU_PACKED NBDSimpleReply; |
70 | |
71 | /* Header of all structured replies */ |
72 | typedef 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 | |
80 | typedef 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 | |
94 | /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */ |
95 | typedef struct NBDStructuredReadData { |
96 | NBDStructuredReplyChunk h; /* h.length >= 9 */ |
97 | uint64_t offset; |
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 */ |
102 | typedef struct NBDStructuredReadHole { |
103 | NBDStructuredReplyChunk h; /* h.length == 12 */ |
104 | uint64_t offset; |
105 | uint32_t length; |
106 | } QEMU_PACKED NBDStructuredReadHole; |
107 | |
108 | /* Header of all NBD_REPLY_TYPE_ERROR* errors */ |
109 | typedef struct NBDStructuredError { |
110 | NBDStructuredReplyChunk h; /* h.length >= 6 */ |
111 | uint32_t error; |
112 | uint16_t message_length; |
113 | } QEMU_PACKED NBDStructuredError; |
114 | |
115 | /* Header of NBD_REPLY_TYPE_BLOCK_STATUS */ |
116 | typedef 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 */ |
123 | typedef struct NBDExtent { |
124 | uint32_t length; |
125 | uint32_t flags; /* NBD_STATE_* */ |
126 | } QEMU_PACKED NBDExtent; |
127 | |
128 | /* Transmission (export) flags: sent from server to client during handshake, |
129 | but describe what will happen during transmission */ |
130 | enum { |
131 | NBD_FLAG_HAS_FLAGS_BIT = 0, /* Flags are there */ |
132 | NBD_FLAG_READ_ONLY_BIT = 1, /* Device is read-only */ |
133 | NBD_FLAG_SEND_FLUSH_BIT = 2, /* Send FLUSH */ |
134 | NBD_FLAG_SEND_FUA_BIT = 3, /* Send FUA (Force Unit Access) */ |
135 | NBD_FLAG_ROTATIONAL_BIT = 4, /* Use elevator algorithm - |
136 | rotational media */ |
137 | NBD_FLAG_SEND_TRIM_BIT = 5, /* Send TRIM (discard) */ |
138 | NBD_FLAG_SEND_WRITE_ZEROES_BIT = 6, /* Send WRITE_ZEROES */ |
139 | NBD_FLAG_SEND_DF_BIT = 7, /* Send DF (Do not Fragment) */ |
140 | NBD_FLAG_CAN_MULTI_CONN_BIT = 8, /* Multi-client cache consistent */ |
141 | NBD_FLAG_SEND_RESIZE_BIT = 9, /* Send resize */ |
142 | NBD_FLAG_SEND_CACHE_BIT = 10, /* Send CACHE (prefetch) */ |
143 | }; |
144 | |
145 | #define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT) |
146 | #define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT) |
147 | #define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT) |
148 | #define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT) |
149 | #define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT) |
150 | #define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT) |
151 | #define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT) |
152 | #define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT) |
153 | #define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT) |
154 | #define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT) |
155 | #define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT) |
156 | |
157 | /* New-style handshake (global) flags, sent from server to client, and |
158 | control what will happen during handshake phase. */ |
159 | #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ |
160 | #define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */ |
161 | |
162 | /* New-style client flags, sent from client to server to control what happens |
163 | during handshake phase. */ |
164 | #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ |
165 | #define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */ |
166 | |
167 | /* Option requests. */ |
168 | #define NBD_OPT_EXPORT_NAME (1) |
169 | #define NBD_OPT_ABORT (2) |
170 | #define NBD_OPT_LIST (3) |
171 | /* #define NBD_OPT_PEEK_EXPORT (4) not in use */ |
172 | #define NBD_OPT_STARTTLS (5) |
173 | #define NBD_OPT_INFO (6) |
174 | #define NBD_OPT_GO (7) |
175 | #define NBD_OPT_STRUCTURED_REPLY (8) |
176 | #define NBD_OPT_LIST_META_CONTEXT (9) |
177 | #define NBD_OPT_SET_META_CONTEXT (10) |
178 | |
179 | /* Option reply types. */ |
180 | #define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value)) |
181 | |
182 | #define NBD_REP_ACK (1) /* Data sending finished. */ |
183 | #define NBD_REP_SERVER (2) /* Export description. */ |
184 | #define NBD_REP_INFO (3) /* NBD_OPT_INFO/GO. */ |
185 | #define NBD_REP_META_CONTEXT (4) /* NBD_OPT_{LIST,SET}_META_CONTEXT */ |
186 | |
187 | #define NBD_REP_ERR_UNSUP NBD_REP_ERR(1) /* Unknown option */ |
188 | #define NBD_REP_ERR_POLICY NBD_REP_ERR(2) /* Server denied */ |
189 | #define NBD_REP_ERR_INVALID NBD_REP_ERR(3) /* Invalid length */ |
190 | #define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4) /* Not compiled in */ |
191 | #define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5) /* TLS required */ |
192 | #define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */ |
193 | #define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting down */ |
194 | #define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZE */ |
195 | |
196 | /* Info types, used during NBD_REP_INFO */ |
197 | #define NBD_INFO_EXPORT 0 |
198 | #define NBD_INFO_NAME 1 |
199 | #define NBD_INFO_DESCRIPTION 2 |
200 | #define NBD_INFO_BLOCK_SIZE 3 |
201 | |
202 | /* Request flags, sent from client to server during transmission phase */ |
203 | #define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during write */ |
204 | #define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */ |
205 | #define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read */ |
206 | #define NBD_CMD_FLAG_REQ_ONE (1 << 3) /* only one extent in BLOCK_STATUS |
207 | * reply chunk */ |
208 | |
209 | /* Supported request types */ |
210 | enum { |
211 | NBD_CMD_READ = 0, |
212 | NBD_CMD_WRITE = 1, |
213 | NBD_CMD_DISC = 2, |
214 | NBD_CMD_FLUSH = 3, |
215 | NBD_CMD_TRIM = 4, |
216 | NBD_CMD_CACHE = 5, |
217 | NBD_CMD_WRITE_ZEROES = 6, |
218 | NBD_CMD_BLOCK_STATUS = 7, |
219 | }; |
220 | |
221 | #define NBD_DEFAULT_PORT 10809 |
222 | |
223 | /* Maximum size of a single READ/WRITE data buffer */ |
224 | #define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024) |
225 | |
226 | /* Maximum size of an export name. The NBD spec requires 256 and |
227 | * suggests that servers support up to 4096, but we stick to only the |
228 | * required size so that we can stack-allocate the names, and because |
229 | * going larger would require an audit of more code to make sure we |
230 | * aren't overflowing some other buffer. */ |
231 | #define NBD_MAX_NAME_SIZE 256 |
232 | |
233 | /* Two types of reply structures */ |
234 | #define NBD_SIMPLE_REPLY_MAGIC 0x67446698 |
235 | #define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef |
236 | |
237 | /* Structured reply flags */ |
238 | #define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last */ |
239 | |
240 | /* Structured reply types */ |
241 | #define NBD_REPLY_ERR(value) ((1 << 15) | (value)) |
242 | |
243 | #define NBD_REPLY_TYPE_NONE 0 |
244 | #define NBD_REPLY_TYPE_OFFSET_DATA 1 |
245 | #define NBD_REPLY_TYPE_OFFSET_HOLE 2 |
246 | #define NBD_REPLY_TYPE_BLOCK_STATUS 5 |
247 | #define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1) |
248 | #define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2) |
249 | |
250 | /* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */ |
251 | #define NBD_STATE_HOLE (1 << 0) |
252 | #define NBD_STATE_ZERO (1 << 1) |
253 | |
254 | /* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */ |
255 | #define NBD_STATE_DIRTY (1 << 0) |
256 | |
257 | static inline bool nbd_reply_type_is_error(int type) |
258 | { |
259 | return type & (1 << 15); |
260 | } |
261 | |
262 | /* NBD errors are based on errno numbers, so there is a 1:1 mapping, |
263 | * but only a limited set of errno values is specified in the protocol. |
264 | * Everything else is squashed to EINVAL. |
265 | */ |
266 | #define NBD_SUCCESS 0 |
267 | #define NBD_EPERM 1 |
268 | #define NBD_EIO 5 |
269 | #define NBD_ENOMEM 12 |
270 | #define NBD_EINVAL 22 |
271 | #define NBD_ENOSPC 28 |
272 | #define NBD_EOVERFLOW 75 |
273 | #define NBD_ESHUTDOWN 108 |
274 | |
275 | /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */ |
276 | struct NBDExportInfo { |
277 | /* Set by client before nbd_receive_negotiate() */ |
278 | bool request_sizes; |
279 | char *x_dirty_bitmap; |
280 | |
281 | /* Set by client before nbd_receive_negotiate(), or by server results |
282 | * during nbd_receive_export_list() */ |
283 | char *name; /* must be non-NULL */ |
284 | |
285 | /* In-out fields, set by client before nbd_receive_negotiate() and |
286 | * updated by server results during nbd_receive_negotiate() */ |
287 | bool structured_reply; |
288 | bool base_allocation; /* base:allocation context for NBD_CMD_BLOCK_STATUS */ |
289 | |
290 | /* Set by server results during nbd_receive_negotiate() and |
291 | * nbd_receive_export_list() */ |
292 | uint64_t size; |
293 | uint16_t flags; |
294 | uint32_t min_block; |
295 | uint32_t opt_block; |
296 | uint32_t max_block; |
297 | |
298 | uint32_t context_id; |
299 | |
300 | /* Set by server results during nbd_receive_export_list() */ |
301 | char *description; |
302 | int n_contexts; |
303 | char **contexts; |
304 | }; |
305 | typedef struct NBDExportInfo NBDExportInfo; |
306 | |
307 | int nbd_receive_negotiate(AioContext *aio_context, QIOChannel *ioc, |
308 | QCryptoTLSCreds *tlscreds, |
309 | const char *hostname, QIOChannel **outioc, |
310 | NBDExportInfo *info, Error **errp); |
311 | void nbd_free_export_list(NBDExportInfo *info, int count); |
312 | int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds, |
313 | const char *hostname, NBDExportInfo **info, |
314 | Error **errp); |
315 | int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info, |
316 | Error **errp); |
317 | int nbd_send_request(QIOChannel *ioc, NBDRequest *request); |
318 | int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc, |
319 | NBDReply *reply, Error **errp); |
320 | int nbd_client(int fd); |
321 | int nbd_disconnect(int fd); |
322 | int nbd_errno_to_system_errno(int err); |
323 | |
324 | typedef struct NBDExport NBDExport; |
325 | typedef struct NBDClient NBDClient; |
326 | |
327 | NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset, |
328 | uint64_t size, const char *name, const char *desc, |
329 | const char *bitmap, uint16_t nbdflags, |
330 | void (*close)(NBDExport *), bool writethrough, |
331 | BlockBackend *on_eject_blk, Error **errp); |
332 | void nbd_export_close(NBDExport *exp); |
333 | void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp); |
334 | void nbd_export_get(NBDExport *exp); |
335 | void nbd_export_put(NBDExport *exp); |
336 | |
337 | BlockBackend *nbd_export_get_blockdev(NBDExport *exp); |
338 | |
339 | NBDExport *nbd_export_find(const char *name); |
340 | void nbd_export_close_all(void); |
341 | |
342 | void nbd_client_new(QIOChannelSocket *sioc, |
343 | QCryptoTLSCreds *tlscreds, |
344 | const char *tlsauthz, |
345 | void (*close_fn)(NBDClient *, bool)); |
346 | void nbd_client_get(NBDClient *client); |
347 | void nbd_client_put(NBDClient *client); |
348 | |
349 | void nbd_server_start(SocketAddress *addr, const char *tls_creds, |
350 | const char *tls_authz, Error **errp); |
351 | |
352 | /* nbd_read |
353 | * Reads @size bytes from @ioc. Returns 0 on success. |
354 | */ |
355 | static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size, |
356 | const char *desc, Error **errp) |
357 | { |
358 | int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0; |
359 | |
360 | if (ret < 0) { |
361 | if (desc) { |
362 | error_prepend(errp, "Failed to read %s: " , desc); |
363 | } |
364 | return -1; |
365 | } |
366 | |
367 | return 0; |
368 | } |
369 | |
370 | #define DEF_NBD_READ_N(bits) \ |
371 | static inline int nbd_read##bits(QIOChannel *ioc, \ |
372 | uint##bits##_t *val, \ |
373 | const char *desc, Error **errp) \ |
374 | { \ |
375 | if (nbd_read(ioc, val, sizeof(*val), desc, errp) < 0) { \ |
376 | return -1; \ |
377 | } \ |
378 | *val = be##bits##_to_cpu(*val); \ |
379 | return 0; \ |
380 | } |
381 | |
382 | DEF_NBD_READ_N(16) /* Defines nbd_read16(). */ |
383 | DEF_NBD_READ_N(32) /* Defines nbd_read32(). */ |
384 | DEF_NBD_READ_N(64) /* Defines nbd_read64(). */ |
385 | |
386 | #undef DEF_NBD_READ_N |
387 | |
388 | static inline bool nbd_reply_is_simple(NBDReply *reply) |
389 | { |
390 | return reply->magic == NBD_SIMPLE_REPLY_MAGIC; |
391 | } |
392 | |
393 | static inline bool nbd_reply_is_structured(NBDReply *reply) |
394 | { |
395 | return reply->magic == NBD_STRUCTURED_REPLY_MAGIC; |
396 | } |
397 | |
398 | const char *nbd_reply_type_lookup(uint16_t type); |
399 | const char *nbd_opt_lookup(uint32_t opt); |
400 | const char *nbd_rep_lookup(uint32_t rep); |
401 | const char *nbd_info_lookup(uint16_t info); |
402 | const char *nbd_cmd_lookup(uint16_t info); |
403 | const char *nbd_err_lookup(int err); |
404 | |
405 | #endif |
406 | |