| 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 | typedef struct NBDMetaContexts NBDMetaContexts; |
| 33 | |
| 34 | extern const BlockExportDriver blk_exp_nbd; |
| 35 | |
| 36 | /* |
| 37 | * NBD_DEFAULT_HANDSHAKE_MAX_SECS: Number of seconds in which client must |
| 38 | * succeed at NBD_OPT_GO before being forcefully dropped as too slow. |
| 39 | */ |
| 40 | #define NBD_DEFAULT_HANDSHAKE_MAX_SECS 10 |
| 41 | |
| 42 | /* |
| 43 | * NBD_DEFAULT_MAX_CONNECTIONS: Number of client sockets to allow at |
| 44 | * once; must be large enough to allow a MULTI_CONN-aware client like |
| 45 | * nbdcopy to create its typical number of 8-16 sockets. |
| 46 | */ |
| 47 | #define NBD_DEFAULT_MAX_CONNECTIONS 100 |
| 48 | |
| 49 | /* Handshake phase structs - this struct is passed on the wire */ |
| 50 | |
| 51 | typedef struct NBDOption { |
| 52 | uint64_t magic; /* NBD_OPTS_MAGIC */ |
| 53 | uint32_t option; /* NBD_OPT_* */ |
| 54 | uint32_t length; |
| 55 | } QEMU_PACKED NBDOption; |
| 56 | |
| 57 | typedef struct NBDOptionReply { |
| 58 | uint64_t magic; /* NBD_REP_MAGIC */ |
| 59 | uint32_t option; /* NBD_OPT_* */ |
| 60 | uint32_t type; /* NBD_REP_* */ |
| 61 | uint32_t length; |
| 62 | } QEMU_PACKED NBDOptionReply; |
| 63 | |
| 64 | typedef struct NBDOptionReplyMetaContext { |
| 65 | NBDOptionReply h; /* h.type = NBD_REP_META_CONTEXT, h.length > 4 */ |
| 66 | uint32_t context_id; |
| 67 | /* metadata context name follows */ |
| 68 | } QEMU_PACKED NBDOptionReplyMetaContext; |
| 69 | |
| 70 | /* Track results of negotiation */ |
| 71 | typedef enum NBDMode { |
| 72 | /* Keep this list in a continuum of increasing features. */ |
| 73 | NBD_MODE_OLDSTYLE, /* server lacks newstyle negotiation */ |
| 74 | NBD_MODE_EXPORT_NAME, /* newstyle but only OPT_EXPORT_NAME safe */ |
| 75 | NBD_MODE_SIMPLE, /* newstyle but only simple replies */ |
| 76 | NBD_MODE_STRUCTURED, /* newstyle, structured replies enabled */ |
| 77 | NBD_MODE_EXTENDED, /* newstyle, extended headers enabled */ |
| 78 | } NBDMode; |
| 79 | |
| 80 | /* Transmission phase structs */ |
| 81 | |
| 82 | /* |
| 83 | * Note: NBDRequest is _NOT_ the same as the network representation of an NBD |
| 84 | * request! |
| 85 | */ |
| 86 | typedef struct NBDRequest { |
| 87 | uint64_t cookie; |
| 88 | uint64_t from; /* Offset touched by the command */ |
| 89 | uint64_t len; /* Effect length; 32 bit limit without extended headers */ |
| 90 | uint16_t flags; /* NBD_CMD_FLAG_* */ |
| 91 | uint16_t type; /* NBD_CMD_* */ |
| 92 | NBDMode mode; /* Determines which network representation to use */ |
| 93 | NBDMetaContexts *contexts; /* Used by NBD_CMD_BLOCK_STATUS */ |
| 94 | } NBDRequest; |
| 95 | |
| 96 | typedef struct NBDSimpleReply { |
| 97 | uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */ |
| 98 | uint32_t error; |
| 99 | uint64_t cookie; |
| 100 | } QEMU_PACKED NBDSimpleReply; |
| 101 | |
| 102 | /* Header of all structured replies */ |
| 103 | typedef struct NBDStructuredReplyChunk { |
| 104 | uint32_t magic; /* NBD_STRUCTURED_REPLY_MAGIC */ |
| 105 | uint16_t flags; /* combination of NBD_REPLY_FLAG_* */ |
| 106 | uint16_t type; /* NBD_REPLY_TYPE_* */ |
| 107 | uint64_t cookie; /* request handle */ |
| 108 | uint32_t length; /* length of payload */ |
| 109 | } QEMU_PACKED NBDStructuredReplyChunk; |
| 110 | |
| 111 | typedef struct NBDExtendedReplyChunk { |
| 112 | uint32_t magic; /* NBD_EXTENDED_REPLY_MAGIC */ |
| 113 | uint16_t flags; /* combination of NBD_REPLY_FLAG_* */ |
| 114 | uint16_t type; /* NBD_REPLY_TYPE_* */ |
| 115 | uint64_t cookie; /* request handle */ |
| 116 | uint64_t offset; /* request offset */ |
| 117 | uint64_t length; /* length of payload */ |
| 118 | } QEMU_PACKED NBDExtendedReplyChunk; |
| 119 | |
| 120 | typedef union NBDReply { |
| 121 | NBDSimpleReply simple; |
| 122 | NBDStructuredReplyChunk structured; |
| 123 | NBDExtendedReplyChunk extended; |
| 124 | struct { |
| 125 | /* |
| 126 | * @magic and @cookie fields have the same offset and size in all |
| 127 | * forms of replies, so let them be accessible without ".simple.", |
| 128 | * ".structured.", or ".extended." specifications. |
| 129 | */ |
| 130 | uint32_t magic; |
| 131 | uint32_t _skip; |
| 132 | uint64_t cookie; |
| 133 | }; |
| 134 | } NBDReply; |
| 135 | QEMU_BUILD_BUG_ON(offsetof(NBDReply, simple.cookie) != |
| 136 | offsetof(NBDReply, cookie)); |
| 137 | QEMU_BUILD_BUG_ON(offsetof(NBDReply, structured.cookie) != |
| 138 | offsetof(NBDReply, cookie)); |
| 139 | QEMU_BUILD_BUG_ON(offsetof(NBDReply, extended.cookie) != |
| 140 | offsetof(NBDReply, cookie)); |
| 141 | |
| 142 | /* Header of chunk for NBD_REPLY_TYPE_OFFSET_DATA */ |
| 143 | typedef struct NBDStructuredReadData { |
| 144 | /* header's .length >= 9 */ |
| 145 | uint64_t offset; |
| 146 | /* At least one byte of data payload follows, calculated from h.length */ |
| 147 | } QEMU_PACKED NBDStructuredReadData; |
| 148 | |
| 149 | /* Complete chunk for NBD_REPLY_TYPE_OFFSET_HOLE */ |
| 150 | typedef struct NBDStructuredReadHole { |
| 151 | /* header's length == 12 */ |
| 152 | uint64_t offset; |
| 153 | uint32_t length; |
| 154 | } QEMU_PACKED NBDStructuredReadHole; |
| 155 | |
| 156 | /* Header of all NBD_REPLY_TYPE_ERROR* errors */ |
| 157 | typedef struct NBDStructuredError { |
| 158 | /* header's length >= 6 */ |
| 159 | uint32_t error; |
| 160 | uint16_t message_length; |
| 161 | } QEMU_PACKED NBDStructuredError; |
| 162 | |
| 163 | /* Header of NBD_REPLY_TYPE_BLOCK_STATUS */ |
| 164 | typedef struct NBDStructuredMeta { |
| 165 | /* header's length >= 12 (at least one extent) */ |
| 166 | uint32_t context_id; |
| 167 | /* NBDExtent32 extents[] follows, array length implied by header */ |
| 168 | } QEMU_PACKED NBDStructuredMeta; |
| 169 | |
| 170 | /* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS */ |
| 171 | typedef struct NBDExtent32 { |
| 172 | uint32_t length; |
| 173 | uint32_t flags; /* NBD_STATE_* */ |
| 174 | } QEMU_PACKED NBDExtent32; |
| 175 | |
| 176 | /* Header of NBD_REPLY_TYPE_BLOCK_STATUS_EXT */ |
| 177 | typedef struct NBDExtendedMeta { |
| 178 | /* header's length >= 24 (at least one extent) */ |
| 179 | uint32_t context_id; |
| 180 | uint32_t count; /* header length must be count * 16 + 8 */ |
| 181 | /* NBDExtent64 extents[count] follows */ |
| 182 | } QEMU_PACKED NBDExtendedMeta; |
| 183 | |
| 184 | /* Extent array element for NBD_REPLY_TYPE_BLOCK_STATUS_EXT */ |
| 185 | typedef struct NBDExtent64 { |
| 186 | uint64_t length; |
| 187 | uint64_t flags; /* NBD_STATE_* */ |
| 188 | } QEMU_PACKED NBDExtent64; |
| 189 | |
| 190 | /* Client payload for limiting NBD_CMD_BLOCK_STATUS reply */ |
| 191 | typedef struct NBDBlockStatusPayload { |
| 192 | uint64_t effect_length; |
| 193 | /* uint32_t ids[] follows, array length implied by header */ |
| 194 | } QEMU_PACKED NBDBlockStatusPayload; |
| 195 | |
| 196 | /* Transmission (export) flags: sent from server to client during handshake, |
| 197 | but describe what will happen during transmission */ |
| 198 | enum { |
| 199 | NBD_FLAG_HAS_FLAGS_BIT = 0, /* Flags are there */ |
| 200 | NBD_FLAG_READ_ONLY_BIT = 1, /* Device is read-only */ |
| 201 | NBD_FLAG_SEND_FLUSH_BIT = 2, /* Send FLUSH */ |
| 202 | NBD_FLAG_SEND_FUA_BIT = 3, /* Send FUA (Force Unit Access) */ |
| 203 | NBD_FLAG_ROTATIONAL_BIT = 4, /* Use elevator algorithm - |
| 204 | rotational media */ |
| 205 | NBD_FLAG_SEND_TRIM_BIT = 5, /* Send TRIM (discard) */ |
| 206 | NBD_FLAG_SEND_WRITE_ZEROES_BIT = 6, /* Send WRITE_ZEROES */ |
| 207 | NBD_FLAG_SEND_DF_BIT = 7, /* Send DF (Do not Fragment) */ |
| 208 | NBD_FLAG_CAN_MULTI_CONN_BIT = 8, /* Multi-client cache consistent */ |
| 209 | NBD_FLAG_SEND_RESIZE_BIT = 9, /* Send resize */ |
| 210 | NBD_FLAG_SEND_CACHE_BIT = 10, /* Send CACHE (prefetch) */ |
| 211 | NBD_FLAG_SEND_FAST_ZERO_BIT = 11, /* FAST_ZERO flag for WRITE_ZEROES */ |
| 212 | NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT = 12, /* PAYLOAD flag for BLOCK_STATUS */ |
| 213 | }; |
| 214 | |
| 215 | #define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT) |
| 216 | #define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT) |
| 217 | #define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT) |
| 218 | #define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT) |
| 219 | #define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT) |
| 220 | #define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT) |
| 221 | #define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT) |
| 222 | #define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT) |
| 223 | #define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT) |
| 224 | #define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT) |
| 225 | #define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT) |
| 226 | #define NBD_FLAG_SEND_FAST_ZERO (1 << NBD_FLAG_SEND_FAST_ZERO_BIT) |
| 227 | #define NBD_FLAG_BLOCK_STAT_PAYLOAD (1 << NBD_FLAG_BLOCK_STAT_PAYLOAD_BIT) |
| 228 | |
| 229 | /* New-style handshake (global) flags, sent from server to client, and |
| 230 | control what will happen during handshake phase. */ |
| 231 | #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ |
| 232 | #define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */ |
| 233 | |
| 234 | /* New-style client flags, sent from client to server to control what happens |
| 235 | during handshake phase. */ |
| 236 | #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ |
| 237 | #define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */ |
| 238 | |
| 239 | /* Option requests. */ |
| 240 | #define NBD_OPT_EXPORT_NAME (1) |
| 241 | #define NBD_OPT_ABORT (2) |
| 242 | #define NBD_OPT_LIST (3) |
| 243 | /* #define NBD_OPT_PEEK_EXPORT (4) not in use */ |
| 244 | #define NBD_OPT_STARTTLS (5) |
| 245 | #define NBD_OPT_INFO (6) |
| 246 | #define NBD_OPT_GO (7) |
| 247 | #define NBD_OPT_STRUCTURED_REPLY (8) |
| 248 | #define NBD_OPT_LIST_META_CONTEXT (9) |
| 249 | #define NBD_OPT_SET_META_CONTEXT (10) |
| 250 | #define NBD_OPT_EXTENDED_HEADERS (11) |
| 251 | |
| 252 | /* Option reply types. */ |
| 253 | #define NBD_REP_ERR(value) ((UINT32_C(1) << 31) | (value)) |
| 254 | |
| 255 | #define NBD_REP_ACK (1) /* Data sending finished. */ |
| 256 | #define NBD_REP_SERVER (2) /* Export description. */ |
| 257 | #define NBD_REP_INFO (3) /* NBD_OPT_INFO/GO. */ |
| 258 | #define NBD_REP_META_CONTEXT (4) /* NBD_OPT_{LIST,SET}_META_CONTEXT */ |
| 259 | |
| 260 | #define NBD_REP_ERR_UNSUP NBD_REP_ERR(1) /* Unknown option */ |
| 261 | #define NBD_REP_ERR_POLICY NBD_REP_ERR(2) /* Server denied */ |
| 262 | #define NBD_REP_ERR_INVALID NBD_REP_ERR(3) /* Invalid length */ |
| 263 | #define NBD_REP_ERR_PLATFORM NBD_REP_ERR(4) /* Not compiled in */ |
| 264 | #define NBD_REP_ERR_TLS_REQD NBD_REP_ERR(5) /* TLS required */ |
| 265 | #define NBD_REP_ERR_UNKNOWN NBD_REP_ERR(6) /* Export unknown */ |
| 266 | #define NBD_REP_ERR_SHUTDOWN NBD_REP_ERR(7) /* Server shutting down */ |
| 267 | #define NBD_REP_ERR_BLOCK_SIZE_REQD NBD_REP_ERR(8) /* Need INFO_BLOCK_SIZE */ |
| 268 | #define NBD_REP_ERR_TOO_BIG NBD_REP_ERR(9) /* Payload size overflow */ |
| 269 | #define NBD_REP_ERR_EXT_HEADER_REQD NBD_REP_ERR(10) /* Need extended headers */ |
| 270 | |
| 271 | /* Info types, used during NBD_REP_INFO */ |
| 272 | #define NBD_INFO_EXPORT 0 |
| 273 | #define NBD_INFO_NAME 1 |
| 274 | #define NBD_INFO_DESCRIPTION 2 |
| 275 | #define NBD_INFO_BLOCK_SIZE 3 |
| 276 | |
| 277 | /* Request flags, sent from client to server during transmission phase */ |
| 278 | #define NBD_CMD_FLAG_FUA (1 << 0) /* 'force unit access' during write */ |
| 279 | #define NBD_CMD_FLAG_NO_HOLE (1 << 1) /* don't punch hole on zero run */ |
| 280 | #define NBD_CMD_FLAG_DF (1 << 2) /* don't fragment structured read */ |
| 281 | #define NBD_CMD_FLAG_REQ_ONE (1 << 3) \ |
| 282 | /* only one extent in BLOCK_STATUS reply chunk */ |
| 283 | #define NBD_CMD_FLAG_FAST_ZERO (1 << 4) /* fail if WRITE_ZEROES is not fast */ |
| 284 | #define NBD_CMD_FLAG_PAYLOAD_LEN (1 << 5) \ |
| 285 | /* length describes payload, not effect; only with ext header */ |
| 286 | |
| 287 | /* Supported request types */ |
| 288 | enum { |
| 289 | NBD_CMD_READ = 0, |
| 290 | NBD_CMD_WRITE = 1, |
| 291 | NBD_CMD_DISC = 2, |
| 292 | NBD_CMD_FLUSH = 3, |
| 293 | NBD_CMD_TRIM = 4, |
| 294 | NBD_CMD_CACHE = 5, |
| 295 | NBD_CMD_WRITE_ZEROES = 6, |
| 296 | NBD_CMD_BLOCK_STATUS = 7, |
| 297 | }; |
| 298 | |
| 299 | #define NBD_DEFAULT_PORT 10809 |
| 300 | |
| 301 | /* Maximum size of a single READ/WRITE data buffer */ |
| 302 | #define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024) |
| 303 | |
| 304 | /* |
| 305 | * Maximum size of a protocol string (export name, metadata context name, |
| 306 | * etc.). Use malloc rather than stack allocation for storage of a |
| 307 | * string. |
| 308 | */ |
| 309 | #define NBD_MAX_STRING_SIZE 4096 |
| 310 | |
| 311 | /* Two types of request structures, a given client will only use 1 */ |
| 312 | #define NBD_REQUEST_MAGIC 0x25609513 |
| 313 | #define NBD_EXTENDED_REQUEST_MAGIC 0x21e41c71 |
| 314 | |
| 315 | /* |
| 316 | * Three types of reply structures, but what a client expects depends |
| 317 | * on NBD_OPT_STRUCTURED_REPLY and NBD_OPT_EXTENDED_HEADERS. |
| 318 | */ |
| 319 | #define NBD_SIMPLE_REPLY_MAGIC 0x67446698 |
| 320 | #define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef |
| 321 | #define NBD_EXTENDED_REPLY_MAGIC 0x6e8a278c |
| 322 | |
| 323 | /* Chunk reply flags (for structured and extended replies) */ |
| 324 | #define NBD_REPLY_FLAG_DONE (1 << 0) /* This reply-chunk is last */ |
| 325 | |
| 326 | /* Chunk reply types */ |
| 327 | #define NBD_REPLY_ERR(value) ((1 << 15) | (value)) |
| 328 | |
| 329 | #define NBD_REPLY_TYPE_NONE 0 |
| 330 | #define NBD_REPLY_TYPE_OFFSET_DATA 1 |
| 331 | #define NBD_REPLY_TYPE_OFFSET_HOLE 2 |
| 332 | #define NBD_REPLY_TYPE_BLOCK_STATUS 5 |
| 333 | #define NBD_REPLY_TYPE_BLOCK_STATUS_EXT 6 |
| 334 | #define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1) |
| 335 | #define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2) |
| 336 | |
| 337 | /* Extent flags for base:allocation in NBD_REPLY_TYPE_BLOCK_STATUS */ |
| 338 | #define NBD_STATE_HOLE (1 << 0) |
| 339 | #define NBD_STATE_ZERO (1 << 1) |
| 340 | |
| 341 | /* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */ |
| 342 | #define NBD_STATE_DIRTY (1 << 0) |
| 343 | |
| 344 | /* No flags needed for qemu:allocation-depth in NBD_REPLY_TYPE_BLOCK_STATUS */ |
| 345 | |
| 346 | static inline bool nbd_reply_type_is_error(int type) |
| 347 | { |
| 348 | return type & (1 << 15); |
| 349 | } |
| 350 | |
| 351 | /* NBD errors are based on errno numbers, so there is a 1:1 mapping, |
| 352 | * but only a limited set of errno values is specified in the protocol. |
| 353 | * Everything else is squashed to EINVAL. |
| 354 | */ |
| 355 | #define NBD_SUCCESS 0 |
| 356 | #define NBD_EPERM 1 |
| 357 | #define NBD_EIO 5 |
| 358 | #define NBD_ENOMEM 12 |
| 359 | #define NBD_EINVAL 22 |
| 360 | #define NBD_ENOSPC 28 |
| 361 | #define NBD_EOVERFLOW 75 |
| 362 | #define NBD_ENOTSUP 95 |
| 363 | #define NBD_ESHUTDOWN 108 |
| 364 | |
| 365 | /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */ |
| 366 | typedef struct NBDExportInfo { |
| 367 | /* Set by client before nbd_receive_negotiate() */ |
| 368 | bool request_sizes; |
| 369 | char *x_dirty_bitmap; |
| 370 | |
| 371 | /* Set by client before nbd_receive_negotiate(), or by server results |
| 372 | * during nbd_receive_export_list() */ |
| 373 | char *name; /* must be non-NULL */ |
| 374 | |
| 375 | /* In-out fields, set by client before nbd_receive_negotiate() and |
| 376 | * updated by server results during nbd_receive_negotiate() */ |
| 377 | NBDMode mode; /* input maximum mode tolerated; output actual mode chosen */ |
| 378 | bool base_allocation; /* base:allocation context for NBD_CMD_BLOCK_STATUS */ |
| 379 | |
| 380 | /* Set by server results during nbd_receive_negotiate() and |
| 381 | * nbd_receive_export_list() */ |
| 382 | uint64_t size; |
| 383 | uint16_t flags; |
| 384 | uint32_t min_block; |
| 385 | uint32_t opt_block; |
| 386 | uint32_t max_block; |
| 387 | |
| 388 | uint32_t context_id; |
| 389 | |
| 390 | /* Set by server results during nbd_receive_export_list() */ |
| 391 | char *description; |
| 392 | int n_contexts; |
| 393 | char **contexts; |
| 394 | } NBDExportInfo; |
| 395 | |
| 396 | int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds, |
| 397 | const char *hostname, QIOChannel **outioc, |
| 398 | NBDExportInfo *info, Error **errp); |
| 399 | void nbd_free_export_list(NBDExportInfo *info, int count); |
| 400 | int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds, |
| 401 | const char *hostname, NBDExportInfo **info, |
| 402 | Error **errp); |
| 403 | int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info, |
| 404 | Error **errp); |
| 405 | int nbd_send_request(QIOChannel *ioc, NBDRequest *request); |
| 406 | int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc, |
| 407 | NBDReply *reply, NBDMode mode, |
| 408 | Error **errp); |
| 409 | int nbd_client(int fd); |
| 410 | int nbd_disconnect(int fd); |
| 411 | int nbd_errno_to_system_errno(int err); |
| 412 | |
| 413 | void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk); |
| 414 | |
| 415 | AioContext *nbd_export_aio_context(NBDExport *exp); |
| 416 | NBDExport *nbd_export_find(const char *name); |
| 417 | |
| 418 | void nbd_client_new(QIOChannelSocket *sioc, |
| 419 | uint32_t handshake_max_secs, |
| 420 | QCryptoTLSCreds *tlscreds, |
| 421 | const char *tlsauthz, |
| 422 | void (*close_fn)(NBDClient *, bool), |
| 423 | void *owner); |
| 424 | void *nbd_client_owner(NBDClient *client); |
| 425 | void nbd_client_get(NBDClient *client); |
| 426 | void nbd_client_put(NBDClient *client); |
| 427 | |
| 428 | void nbd_server_is_qemu_nbd(int max_connections); |
| 429 | bool nbd_server_is_running(void); |
| 430 | int nbd_server_max_connections(void); |
| 431 | void nbd_server_start(SocketAddress *addr, uint32_t handshake_max_secs, |
| 432 | const char *tls_creds, const char *tls_authz, |
| 433 | uint32_t max_connections, Error **errp); |
| 434 | void nbd_server_start_options(NbdServerOptions *arg, Error **errp); |
| 435 | |
| 436 | /* nbd_read |
| 437 | * Reads @size bytes from @ioc. Returns 0 on success. |
| 438 | */ |
| 439 | static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size, |
| 440 | const char *desc, Error **errp) |
| 441 | { |
| 442 | ERRP_GUARD(); |
| 443 | int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0; |
| 444 | |
| 445 | if (ret < 0) { |
| 446 | if (desc) { |
| 447 | error_prepend(errp, "Failed to read %s: ", desc); |
| 448 | } |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | #define DEF_NBD_READ_N(bits) \ |
| 456 | static inline int nbd_read##bits(QIOChannel *ioc, \ |
| 457 | uint##bits##_t *val, \ |
| 458 | const char *desc, Error **errp) \ |
| 459 | { \ |
| 460 | int ret = nbd_read(ioc, val, sizeof(*val), desc, errp); \ |
| 461 | if (ret < 0) { \ |
| 462 | return ret; \ |
| 463 | } \ |
| 464 | *val = be##bits##_to_cpu(*val); \ |
| 465 | return 0; \ |
| 466 | } |
| 467 | |
| 468 | DEF_NBD_READ_N(16) /* Defines nbd_read16(). */ |
| 469 | DEF_NBD_READ_N(32) /* Defines nbd_read32(). */ |
| 470 | DEF_NBD_READ_N(64) /* Defines nbd_read64(). */ |
| 471 | |
| 472 | #undef DEF_NBD_READ_N |
| 473 | |
| 474 | static inline bool nbd_reply_is_simple(NBDReply *reply) |
| 475 | { |
| 476 | return reply->magic == NBD_SIMPLE_REPLY_MAGIC; |
| 477 | } |
| 478 | |
| 479 | static inline bool nbd_reply_is_structured(NBDReply *reply) |
| 480 | { |
| 481 | return reply->magic == NBD_STRUCTURED_REPLY_MAGIC; |
| 482 | } |
| 483 | |
| 484 | const char *nbd_reply_type_lookup(uint16_t type); |
| 485 | const char *nbd_opt_lookup(uint32_t opt); |
| 486 | const char *nbd_rep_lookup(uint32_t rep); |
| 487 | const char *nbd_info_lookup(uint16_t info); |
| 488 | const char *nbd_cmd_lookup(uint16_t info); |
| 489 | const char *nbd_err_lookup(int err); |
| 490 | const char *nbd_mode_lookup(NBDMode mode); |
| 491 | |
| 492 | /* nbd/client-connection.c */ |
| 493 | void nbd_client_connection_enable_retry(NBDClientConnection *conn); |
| 494 | |
| 495 | NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr, |
| 496 | bool do_negotiation, |
| 497 | const char *export_name, |
| 498 | const char *x_dirty_bitmap, |
| 499 | QCryptoTLSCreds *tlscreds, |
| 500 | const char *tlshostname); |
| 501 | void nbd_client_connection_release(NBDClientConnection *conn); |
| 502 | |
| 503 | QIOChannel *coroutine_fn |
| 504 | nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info, |
| 505 | bool blocking, Error **errp); |
| 506 | |
| 507 | void nbd_co_establish_connection_cancel(NBDClientConnection *conn); |
| 508 | |
| 509 | #endif |