]>
Commit | Line | Data |
---|---|---|
75818250 | 1 | /* |
b626b51a | 2 | * Copyright (C) 2016 Red Hat, Inc. |
7a5ca864 FB |
3 | * Copyright (C) 2005 Anthony Liguori <[email protected]> |
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 | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
75818250 | 18 | */ |
7a5ca864 FB |
19 | |
20 | #ifndef NBD_H | |
21 | #define NBD_H | |
22 | ||
7a5ca864 | 23 | |
5a61cb60 | 24 | #include "qemu-common.h" |
f17c90be | 25 | #include "qemu/option.h" |
1c778ef7 | 26 | #include "io/channel-socket.h" |
f95910fe | 27 | #include "crypto/tlscreds.h" |
c12504ce | 28 | |
c8a3a1b6 EB |
29 | /* Handshake phase structs - this struct is passed on the wire */ |
30 | ||
31 | struct nbd_option { | |
32 | uint64_t magic; /* NBD_OPTS_MAGIC */ | |
33 | uint32_t option; /* NBD_OPT_* */ | |
34 | uint32_t length; | |
35 | } QEMU_PACKED; | |
36 | typedef struct nbd_option nbd_option; | |
37 | ||
38 | struct nbd_opt_reply { | |
39 | uint64_t magic; /* NBD_REP_MAGIC */ | |
40 | uint32_t option; /* NBD_OPT_* */ | |
41 | uint32_t type; /* NBD_REP_* */ | |
42 | uint32_t length; | |
43 | } QEMU_PACKED; | |
44 | typedef struct nbd_opt_reply nbd_opt_reply; | |
45 | ||
46 | /* Transmission phase structs | |
47 | * | |
48 | * Note: these are _NOT_ the same as the network representation of an NBD | |
56af2dda PB |
49 | * request and reply! |
50 | */ | |
ed2dd912 | 51 | struct NBDRequest { |
75818250 TS |
52 | uint64_t handle; |
53 | uint64_t from; | |
54 | uint32_t len; | |
c8a3a1b6 EB |
55 | uint16_t flags; /* NBD_CMD_FLAG_* */ |
56 | uint16_t type; /* NBD_CMD_* */ | |
56af2dda | 57 | }; |
ed2dd912 | 58 | typedef struct NBDRequest NBDRequest; |
75818250 | 59 | |
ed2dd912 | 60 | struct NBDReply { |
75818250 | 61 | uint64_t handle; |
56af2dda PB |
62 | uint32_t error; |
63 | }; | |
ed2dd912 | 64 | typedef struct NBDReply NBDReply; |
75818250 | 65 | |
b626b51a EB |
66 | /* Transmission (export) flags: sent from server to client during handshake, |
67 | but describe what will happen during transmission */ | |
b90fb4b8 PB |
68 | #define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */ |
69 | #define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */ | |
bbb74edd PB |
70 | #define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */ |
71 | #define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */ | |
72 | #define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */ | |
73 | #define NBD_FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */ | |
74 | ||
b626b51a EB |
75 | /* New-style handshake (global) flags, sent from server to client, and |
76 | control what will happen during handshake phase. */ | |
c203c59a EB |
77 | #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ |
78 | #define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */ | |
f5076b5a | 79 | |
b626b51a EB |
80 | /* New-style client flags, sent from client to server to control what happens |
81 | during handshake phase. */ | |
c203c59a EB |
82 | #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ |
83 | #define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */ | |
f5076b5a HB |
84 | |
85 | /* Reply types. */ | |
32d7d2e0 HB |
86 | #define NBD_REP_ACK (1) /* Data sending finished. */ |
87 | #define NBD_REP_SERVER (2) /* Export description. */ | |
ac97393d | 88 | #define NBD_REP_ERR_UNSUP ((UINT32_C(1) << 31) | 1) /* Unknown option. */ |
f95910fe | 89 | #define NBD_REP_ERR_POLICY ((UINT32_C(1) << 31) | 2) /* Server denied */ |
ac97393d | 90 | #define NBD_REP_ERR_INVALID ((UINT32_C(1) << 31) | 3) /* Invalid length. */ |
f95910fe DB |
91 | #define NBD_REP_ERR_TLS_REQD ((UINT32_C(1) << 31) | 5) /* TLS required */ |
92 | ||
b626b51a EB |
93 | /* Request flags, sent from client to server during transmission phase */ |
94 | #define NBD_CMD_FLAG_FUA (1 << 0) | |
f5076b5a | 95 | |
b626b51a | 96 | /* Supported request types */ |
75818250 TS |
97 | enum { |
98 | NBD_CMD_READ = 0, | |
99 | NBD_CMD_WRITE = 1, | |
bbb74edd PB |
100 | NBD_CMD_DISC = 2, |
101 | NBD_CMD_FLUSH = 3, | |
102 | NBD_CMD_TRIM = 4 | |
75818250 TS |
103 | }; |
104 | ||
1d45f8b5 LV |
105 | #define NBD_DEFAULT_PORT 10809 |
106 | ||
2d821488 SH |
107 | /* Maximum size of a single READ/WRITE data buffer */ |
108 | #define NBD_MAX_BUFFER_SIZE (32 * 1024 * 1024) | |
476b923c | 109 | |
943cec86 EB |
110 | /* Maximum size of an export name. The NBD spec requires 256 and |
111 | * suggests that servers support up to 4096, but we stick to only the | |
112 | * required size so that we can stack-allocate the names, and because | |
113 | * going larger would require an audit of more code to make sure we | |
114 | * aren't overflowing some other buffer. */ | |
115 | #define NBD_MAX_NAME_SIZE 256 | |
3777b09f | 116 | |
1c778ef7 DB |
117 | ssize_t nbd_wr_syncv(QIOChannel *ioc, |
118 | struct iovec *iov, | |
119 | size_t niov, | |
1c778ef7 DB |
120 | size_t length, |
121 | bool do_read); | |
7423f417 | 122 | int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, |
f95910fe DB |
123 | QCryptoTLSCreds *tlscreds, const char *hostname, |
124 | QIOChannel **outioc, | |
3f472659 | 125 | off_t *size, Error **errp); |
7423f417 | 126 | int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size); |
ed2dd912 EB |
127 | ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request); |
128 | ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply); | |
0a4eb864 | 129 | int nbd_client(int fd); |
7a5ca864 FB |
130 | int nbd_disconnect(int fd); |
131 | ||
af49bbbe | 132 | typedef struct NBDExport NBDExport; |
1743b515 | 133 | typedef struct NBDClient NBDClient; |
af49bbbe | 134 | |
cd7fca95 | 135 | NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size, |
7423f417 | 136 | uint16_t nbdflags, void (*close)(NBDExport *), |
cd7fca95 | 137 | bool writethrough, BlockBackend *on_eject_blk, |
98f44bbe | 138 | Error **errp); |
af49bbbe | 139 | void nbd_export_close(NBDExport *exp); |
2c8d9f06 PB |
140 | void nbd_export_get(NBDExport *exp); |
141 | void nbd_export_put(NBDExport *exp); | |
ce33967a | 142 | |
e140177d | 143 | BlockBackend *nbd_export_get_blockdev(NBDExport *exp); |
125afda8 | 144 | |
ee0a19ec PB |
145 | NBDExport *nbd_export_find(const char *name); |
146 | void nbd_export_set_name(NBDExport *exp, const char *name); | |
b1a75b33 | 147 | void nbd_export_set_description(NBDExport *exp, const char *description); |
ee0a19ec PB |
148 | void nbd_export_close_all(void); |
149 | ||
1c778ef7 DB |
150 | void nbd_client_new(NBDExport *exp, |
151 | QIOChannelSocket *sioc, | |
f95910fe DB |
152 | QCryptoTLSCreds *tlscreds, |
153 | const char *tlsaclname, | |
1c778ef7 | 154 | void (*close)(NBDClient *)); |
ce33967a PB |
155 | void nbd_client_get(NBDClient *client); |
156 | void nbd_client_put(NBDClient *client); | |
af49bbbe | 157 | |
7a5ca864 | 158 | #endif |