1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30struct NBDOption {
31 uint64_t magic;
32 uint32_t option;
33 uint32_t length;
34} QEMU_PACKED;
35typedef struct NBDOption NBDOption;
36
37struct NBDOptionReply {
38 uint64_t magic;
39 uint32_t option;
40 uint32_t type;
41 uint32_t length;
42} QEMU_PACKED;
43typedef struct NBDOptionReply NBDOptionReply;
44
45typedef struct NBDOptionReplyMetaContext {
46 NBDOptionReply h;
47 uint32_t context_id;
48
49} QEMU_PACKED NBDOptionReplyMetaContext;
50
51
52
53
54
55
56struct NBDRequest {
57 uint64_t handle;
58 uint64_t from;
59 uint32_t len;
60 uint16_t flags;
61 uint16_t type;
62};
63typedef struct NBDRequest NBDRequest;
64
65typedef struct NBDSimpleReply {
66 uint32_t magic;
67 uint32_t error;
68 uint64_t handle;
69} QEMU_PACKED NBDSimpleReply;
70
71
72typedef struct NBDStructuredReplyChunk {
73 uint32_t magic;
74 uint16_t flags;
75 uint16_t type;
76 uint64_t handle;
77 uint32_t length;
78} QEMU_PACKED NBDStructuredReplyChunk;
79
80typedef union NBDReply {
81 NBDSimpleReply simple;
82 NBDStructuredReplyChunk structured;
83 struct {
84
85
86
87
88 uint32_t magic;
89 uint32_t _skip;
90 uint64_t handle;
91 } QEMU_PACKED;
92} NBDReply;
93
94
95typedef struct NBDStructuredReadData {
96 NBDStructuredReplyChunk h;
97 uint64_t offset;
98
99} QEMU_PACKED NBDStructuredReadData;
100
101
102typedef struct NBDStructuredReadHole {
103 NBDStructuredReplyChunk h;
104 uint64_t offset;
105 uint32_t length;
106} QEMU_PACKED NBDStructuredReadHole;
107
108
109typedef struct NBDStructuredError {
110 NBDStructuredReplyChunk h;
111 uint32_t error;
112 uint16_t message_length;
113} QEMU_PACKED NBDStructuredError;
114
115
116typedef struct NBDStructuredMeta {
117 NBDStructuredReplyChunk h;
118 uint32_t context_id;
119
120} QEMU_PACKED NBDStructuredMeta;
121
122
123typedef struct NBDExtent {
124 uint32_t length;
125 uint32_t flags;
126} QEMU_PACKED NBDExtent;
127
128
129
130#define NBD_FLAG_HAS_FLAGS (1 << 0)
131#define NBD_FLAG_READ_ONLY (1 << 1)
132#define NBD_FLAG_SEND_FLUSH (1 << 2)
133#define NBD_FLAG_SEND_FUA (1 << 3)
134#define NBD_FLAG_ROTATIONAL (1 << 4)
135
136#define NBD_FLAG_SEND_TRIM (1 << 5)
137#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6)
138#define NBD_FLAG_SEND_DF (1 << 7)
139#define NBD_FLAG_CAN_MULTI_CONN (1 << 8)
140#define NBD_FLAG_SEND_RESIZE (1 << 9)
141#define NBD_FLAG_SEND_CACHE (1 << 10)
142
143
144
145#define NBD_FLAG_FIXED_NEWSTYLE (1 << 0)
146#define NBD_FLAG_NO_ZEROES (1 << 1)
147
148
149
150#define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0)
151#define NBD_FLAG_C_NO_ZEROES (1 << 1)
152
153
154#define NBD_OPT_EXPORT_NAME (1)
155#define NBD_OPT_ABORT (2)
156#define NBD_OPT_LIST (3)
157
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)
162#define NBD_OPT_LIST_META_CONTEXT (9)
163#define NBD_OPT_SET_META_CONTEXT (10)
164
165
166#define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value))
167
168#define NBD_REP_ACK (1)
169#define NBD_REP_SERVER (2)
170#define NBD_REP_INFO (3)
171#define NBD_REP_META_CONTEXT (4)
172
173#define NBD_REP_ERR_UNSUP NBD_REP_ERR(1)
174#define NBD_REP_ERR_POLICY NBD_REP_ERR(2)
175#define NBD_REP_ERR_INVALID NBD_REP_ERR(3)
176#define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4)
177#define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5)
178#define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6)
179#define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7)
180#define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8)
181
182
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
187
188
189#define NBD_CMD_FLAG_FUA (1 << 0)
190#define NBD_CMD_FLAG_NO_HOLE (1 << 1)
191#define NBD_CMD_FLAG_DF (1 << 2)
192#define NBD_CMD_FLAG_REQ_ONE (1 << 3)
193
194
195
196enum {
197 NBD_CMD_READ = 0,
198 NBD_CMD_WRITE = 1,
199 NBD_CMD_DISC = 2,
200 NBD_CMD_FLUSH = 3,
201 NBD_CMD_TRIM = 4,
202 NBD_CMD_CACHE = 5,
203 NBD_CMD_WRITE_ZEROES = 6,
204 NBD_CMD_BLOCK_STATUS = 7,
205};
206
207#define NBD_DEFAULT_PORT 10809
208
209
210#define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024)
211
212
213
214
215
216
217#define NBD_MAX_NAME_SIZE 256
218
219
220#define NBD_SIMPLE_REPLY_MAGIC 0x67446698
221#define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef
222
223
224#define NBD_REPLY_FLAG_DONE (1 << 0)
225
226
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
232#define NBD_REPLY_TYPE_BLOCK_STATUS 5
233#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)
234#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2)
235
236
237#define NBD_STATE_HOLE (1 << 0)
238#define NBD_STATE_ZERO (1 << 1)
239
240
241#define NBD_STATE_DIRTY (1 << 0)
242
243static inline bool nbd_reply_type_is_error(int type)
244{
245 return type & (1 << 15);
246}
247
248
249
250
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
258#define NBD_EOVERFLOW 75
259#define NBD_ESHUTDOWN 108
260
261
262struct NBDExportInfo {
263
264 bool request_sizes;
265 char *x_dirty_bitmap;
266
267
268
269 char *name;
270
271
272
273 bool structured_reply;
274 bool base_allocation;
275
276
277
278 uint64_t size;
279 uint16_t flags;
280 uint32_t min_block;
281 uint32_t opt_block;
282 uint32_t max_block;
283
284 uint32_t context_id;
285
286
287 char *description;
288 int n_contexts;
289 char **contexts;
290};
291typedef struct NBDExportInfo NBDExportInfo;
292
293int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
294 const char *hostname, QIOChannel **outioc,
295 NBDExportInfo *info, Error **errp);
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);
300int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
301 Error **errp);
302int nbd_send_request(QIOChannel *ioc, NBDRequest *request);
303int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc,
304 NBDReply *reply, Error **errp);
305int nbd_client(int fd);
306int nbd_disconnect(int fd);
307int nbd_errno_to_system_errno(int err);
308
309typedef struct NBDExport NBDExport;
310typedef struct NBDClient NBDClient;
311
312NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
313 uint64_t size, const char *name, const char *desc,
314 const char *bitmap, uint16_t nbdflags,
315 void (*close)(NBDExport *), bool writethrough,
316 BlockBackend *on_eject_blk, Error **errp);
317void nbd_export_close(NBDExport *exp);
318void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp);
319void nbd_export_get(NBDExport *exp);
320void nbd_export_put(NBDExport *exp);
321
322BlockBackend *nbd_export_get_blockdev(NBDExport *exp);
323
324NBDExport *nbd_export_find(const char *name);
325void nbd_export_close_all(void);
326
327void nbd_client_new(QIOChannelSocket *sioc,
328 QCryptoTLSCreds *tlscreds,
329 const char *tlsauthz,
330 void (*close_fn)(NBDClient *, bool));
331void nbd_client_get(NBDClient *client);
332void nbd_client_put(NBDClient *client);
333
334void nbd_server_start(SocketAddress *addr, const char *tls_creds,
335 const char *tls_authz, Error **errp);
336
337
338
339
340static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
341 const char *desc, Error **errp)
342{
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; \
365}
366
367DEF_NBD_READ_N(16)
368DEF_NBD_READ_N(32)
369DEF_NBD_READ_N(64)
370
371#undef DEF_NBD_READ_N
372
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
383const char *nbd_reply_type_lookup(uint16_t type);
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);
389
390#endif
391