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