blob: bd7e20013669cd6b179e99aaba84fed0f25d4172 [file] [log] [blame]
Fam Zheng798bfe02016-01-14 16:41:02 +08001/*
Eric Blake22efd812023-06-08 08:56:34 -05002 * Copyright Red Hat
Fam Zheng798bfe02016-01-14 16:41:02 +08003 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 *
5 * Network Block Device Client Side
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
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010021#include "qapi/error.h"
Markus Armbrusterdc5e9ac2019-08-12 07:23:49 +020022#include "qemu/queue.h"
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +030023#include "trace.h"
Fam Zheng798bfe02016-01-14 16:41:02 +080024#include "nbd-internal.h"
Eric Blake7c6f5dd2019-01-17 13:36:57 -060025#include "qemu/cutils.h"
Fam Zheng798bfe02016-01-14 16:41:02 +080026
Fam Zheng798bfe02016-01-14 16:41:02 +080027/* Definitions for opaque data types */
28
29static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
30
31/* That's all folks */
32
33/* Basic flow for negotiation
34
35 Server Client
36 Negotiate
37
38 or
39
40 Server Client
41 Negotiate #1
42 Option
43 Negotiate #2
44
45 ----
46
47 followed by
48
49 Server Client
50 Request
51 Response
52 Request
53 Response
54 ...
55 ...
56 Request (type == 2)
57
58*/
59
Eric Blakec8a3a1b2016-10-14 13:33:10 -050060/* Send an option request.
61 *
62 * The request is for option @opt, with @data containing @len bytes of
63 * additional payload for the request (@len may be -1 to treat @data as
64 * a C string; and @data may be NULL if @len is 0).
65 * Return 0 if successful, -1 with errp set if it is impossible to
66 * continue. */
67static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
68 uint32_t len, const char *data,
69 Error **errp)
70{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +020071 ERRP_GUARD();
Vladimir Sementsov-Ogievskiy420a4e92017-11-22 13:19:57 +030072 NBDOption req;
Eric Blakec8a3a1b2016-10-14 13:33:10 -050073 QEMU_BUILD_BUG_ON(sizeof(req) != 16);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +000074
Eric Blakec8a3a1b2016-10-14 13:33:10 -050075 if (len == -1) {
76 req.length = len = strlen(data);
77 }
Eric Blake3736cc52017-07-07 15:30:43 -050078 trace_nbd_send_option_request(opt, nbd_opt_lookup(opt), len);
Eric Blakec8a3a1b2016-10-14 13:33:10 -050079
80 stq_be_p(&req.magic, NBD_OPTS_MAGIC);
81 stl_be_p(&req.option, opt);
82 stl_be_p(&req.length, len);
83
Vladimir Sementsov-Ogievskiyd1fdf252017-06-02 18:01:39 +030084 if (nbd_write(ioc, &req, sizeof(req), errp) < 0) {
Eric Blakecb6b1a32017-11-13 09:24:24 -060085 error_prepend(errp, "Failed to send option request header: ");
Eric Blakec8a3a1b2016-10-14 13:33:10 -050086 return -1;
87 }
88
Vladimir Sementsov-Ogievskiyd1fdf252017-06-02 18:01:39 +030089 if (len && nbd_write(ioc, (char *) data, len, errp) < 0) {
Eric Blakecb6b1a32017-11-13 09:24:24 -060090 error_prepend(errp, "Failed to send option request data: ");
Eric Blakec8a3a1b2016-10-14 13:33:10 -050091 return -1;
92 }
93
94 return 0;
95}
96
Eric Blake2cdbf412016-10-14 13:33:11 -050097/* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
98 * not going to attempt further negotiation. */
99static void nbd_send_opt_abort(QIOChannel *ioc)
100{
101 /* Technically, a compliant server is supposed to reply to us; but
102 * older servers disconnected instead. At any rate, we're allowed
103 * to disconnect without waiting for the server reply, so we don't
104 * even care if the request makes it to the server, let alone
105 * waiting around for whether the server replies. */
106 nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
107}
108
109
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500110/* Receive the header of an option reply, which should match the given
111 * opt. Read through the length field, but NOT the length bytes of
112 * payload. Return 0 if successful, -1 with errp set if it is
113 * impossible to continue. */
114static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
Vladimir Sementsov-Ogievskiy420a4e92017-11-22 13:19:57 +0300115 NBDOptionReply *reply, Error **errp)
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500116{
117 QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300118 if (nbd_read(ioc, reply, sizeof(*reply), "option reply", errp) < 0) {
Eric Blake2cdbf412016-10-14 13:33:11 -0500119 nbd_send_opt_abort(ioc);
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500120 return -1;
121 }
Peter Maydell80c7c2b2018-09-27 17:42:00 +0100122 reply->magic = be64_to_cpu(reply->magic);
123 reply->option = be32_to_cpu(reply->option);
124 reply->type = be32_to_cpu(reply->type);
125 reply->length = be32_to_cpu(reply->length);
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500126
Eric Blake3736cc52017-07-07 15:30:43 -0500127 trace_nbd_receive_option_reply(reply->option, nbd_opt_lookup(reply->option),
128 reply->type, nbd_rep_lookup(reply->type),
129 reply->length);
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500130
131 if (reply->magic != NBD_REP_MAGIC) {
132 error_setg(errp, "Unexpected option reply magic");
Eric Blake2cdbf412016-10-14 13:33:11 -0500133 nbd_send_opt_abort(ioc);
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500134 return -1;
135 }
136 if (reply->option != opt) {
Eric Blake6c5c0352018-12-15 07:53:07 -0600137 error_setg(errp, "Unexpected option type %u (%s), expected %u (%s)",
138 reply->option, nbd_opt_lookup(reply->option),
139 opt, nbd_opt_lookup(opt));
Eric Blake2cdbf412016-10-14 13:33:11 -0500140 nbd_send_opt_abort(ioc);
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500141 return -1;
142 }
143 return 0;
144}
145
Eric Blake5de47732019-08-24 12:28:13 -0500146/*
147 * If reply represents success, return 1 without further action. If
148 * reply represents an error, consume the optional payload of the
149 * packet on ioc. Then return 0 for unsupported (so the client can
150 * fall back to other approaches), where @strict determines if only
151 * ERR_UNSUP or all errors fit that category, or -1 with errp set for
152 * other errors.
Alex Bligh6ff58162016-04-06 10:59:22 -0600153 */
Vladimir Sementsov-Ogievskiy420a4e92017-11-22 13:19:57 +0300154static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply,
Eric Blake5de47732019-08-24 12:28:13 -0500155 bool strict, Error **errp)
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000156{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +0200157 ERRP_GUARD();
Eric Blake5de47732019-08-24 12:28:13 -0500158 g_autofree char *msg = NULL;
Alex Bligh6ff58162016-04-06 10:59:22 -0600159
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500160 if (!(reply->type & (1 << 31))) {
Alex Bligh6ff58162016-04-06 10:59:22 -0600161 return 1;
162 }
163
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500164 if (reply->length) {
165 if (reply->length > NBD_MAX_BUFFER_SIZE) {
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300166 error_setg(errp, "server error %" PRIu32
Eric Blake3736cc52017-07-07 15:30:43 -0500167 " (%s) message is too long",
168 reply->type, nbd_rep_lookup(reply->type));
Eric Blake5de47732019-08-24 12:28:13 -0500169 goto err;
Alex Bligh6ff58162016-04-06 10:59:22 -0600170 }
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500171 msg = g_malloc(reply->length + 1);
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300172 if (nbd_read(ioc, msg, reply->length, NULL, errp) < 0) {
173 error_prepend(errp, "Failed to read option error %" PRIu32
Eric Blakecb6b1a32017-11-13 09:24:24 -0600174 " (%s) message: ",
Eric Blake3736cc52017-07-07 15:30:43 -0500175 reply->type, nbd_rep_lookup(reply->type));
Eric Blake5de47732019-08-24 12:28:13 -0500176 goto err;
Alex Bligh6ff58162016-04-06 10:59:22 -0600177 }
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500178 msg[reply->length] = '\0';
Eric Blakebee21ef2018-12-18 16:57:13 -0600179 trace_nbd_server_error_msg(reply->type,
180 nbd_reply_type_lookup(reply->type), msg);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000181 }
182
Eric Blake5de47732019-08-24 12:28:13 -0500183 if (reply->type == NBD_REP_ERR_UNSUP || !strict) {
184 trace_nbd_reply_err_ignored(reply->option,
185 nbd_opt_lookup(reply->option),
186 reply->type, nbd_rep_lookup(reply->type));
187 return 0;
188 }
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000189
Eric Blake5de47732019-08-24 12:28:13 -0500190 switch (reply->type) {
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000191 case NBD_REP_ERR_POLICY:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300192 error_setg(errp, "Denied by server for option %" PRIu32 " (%s)",
Eric Blake3736cc52017-07-07 15:30:43 -0500193 reply->option, nbd_opt_lookup(reply->option));
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000194 break;
195
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000196 case NBD_REP_ERR_INVALID:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300197 error_setg(errp, "Invalid parameters for option %" PRIu32 " (%s)",
Eric Blake3736cc52017-07-07 15:30:43 -0500198 reply->option, nbd_opt_lookup(reply->option));
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000199 break;
200
Eric Blakeb6f5d3b2016-10-14 13:33:16 -0500201 case NBD_REP_ERR_PLATFORM:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300202 error_setg(errp, "Server lacks support for option %" PRIu32 " (%s)",
Eric Blake3736cc52017-07-07 15:30:43 -0500203 reply->option, nbd_opt_lookup(reply->option));
Eric Blakeb6f5d3b2016-10-14 13:33:16 -0500204 break;
205
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000206 case NBD_REP_ERR_TLS_REQD:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300207 error_setg(errp, "TLS negotiation required before option %" PRIu32
Eric Blake3736cc52017-07-07 15:30:43 -0500208 " (%s)", reply->option, nbd_opt_lookup(reply->option));
Eric Blake1b5c15c2019-09-07 12:20:55 -0500209 error_append_hint(errp, "Did you forget a valid tls-creds?\n");
Eric Blake3736cc52017-07-07 15:30:43 -0500210 break;
211
212 case NBD_REP_ERR_UNKNOWN:
Eric Blake9a76bd72017-07-17 09:23:10 -0500213 error_setg(errp, "Requested export not available");
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000214 break;
215
Eric Blakeb6f5d3b2016-10-14 13:33:16 -0500216 case NBD_REP_ERR_SHUTDOWN:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300217 error_setg(errp, "Server shutting down before option %" PRIu32 " (%s)",
Eric Blake3736cc52017-07-07 15:30:43 -0500218 reply->option, nbd_opt_lookup(reply->option));
219 break;
220
221 case NBD_REP_ERR_BLOCK_SIZE_REQD:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300222 error_setg(errp, "Server requires INFO_BLOCK_SIZE for option %" PRIu32
Eric Blake3736cc52017-07-07 15:30:43 -0500223 " (%s)", reply->option, nbd_opt_lookup(reply->option));
Eric Blakeb6f5d3b2016-10-14 13:33:16 -0500224 break;
225
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000226 default:
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300227 error_setg(errp, "Unknown error code when asking for option %" PRIu32
Eric Blake3736cc52017-07-07 15:30:43 -0500228 " (%s)", reply->option, nbd_opt_lookup(reply->option));
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000229 break;
230 }
231
Alex Bligh6ff58162016-04-06 10:59:22 -0600232 if (msg) {
Eric Blake9a76bd72017-07-17 09:23:10 -0500233 error_append_hint(errp, "server reported: %s\n", msg);
Alex Bligh6ff58162016-04-06 10:59:22 -0600234 }
235
Eric Blake5de47732019-08-24 12:28:13 -0500236 err:
237 nbd_send_opt_abort(ioc);
238 return -1;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000239}
240
Eric Blake091d0bf2019-01-17 13:36:45 -0600241/* nbd_receive_list:
242 * Process another portion of the NBD_OPT_LIST reply, populating any
243 * name received into *@name. If @description is non-NULL, and the
244 * server provided a description, that is also populated. The caller
245 * must eventually call g_free() on success.
246 * Returns 1 if name and description were set and iteration must continue,
247 * 0 if iteration is complete (including if OPT_LIST unsupported),
248 * -1 with @errp set if an unrecoverable error occurred.
249 */
250static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,
Eric Blake75368aa2016-10-14 13:33:13 -0500251 Error **errp)
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000252{
Vladimir Sementsov-Ogievskiy420a4e92017-11-22 13:19:57 +0300253 NBDOptionReply reply;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000254 uint32_t len;
255 uint32_t namelen;
Eric Blakedf18c042019-08-24 12:28:12 -0500256 g_autofree char *local_name = NULL;
257 g_autofree char *local_desc = NULL;
Alex Bligh6ff58162016-04-06 10:59:22 -0600258 int error;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000259
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500260 if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000261 return -1;
262 }
Eric Blake5de47732019-08-24 12:28:13 -0500263 error = nbd_handle_reply_err(ioc, &reply, true, errp);
Alex Bligh6ff58162016-04-06 10:59:22 -0600264 if (error <= 0) {
265 return error;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000266 }
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500267 len = reply.length;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000268
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500269 if (reply.type == NBD_REP_ACK) {
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000270 if (len != 0) {
271 error_setg(errp, "length too long for option end");
Eric Blake2cdbf412016-10-14 13:33:11 -0500272 nbd_send_opt_abort(ioc);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000273 return -1;
274 }
Eric Blake75368aa2016-10-14 13:33:13 -0500275 return 0;
276 } else if (reply.type != NBD_REP_SERVER) {
Eric Blake6c5c0352018-12-15 07:53:07 -0600277 error_setg(errp, "Unexpected reply type %u (%s), expected %u (%s)",
278 reply.type, nbd_rep_lookup(reply.type),
279 NBD_REP_SERVER, nbd_rep_lookup(NBD_REP_SERVER));
Eric Blake2cdbf412016-10-14 13:33:11 -0500280 nbd_send_opt_abort(ioc);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000281 return -1;
282 }
Eric Blake75368aa2016-10-14 13:33:13 -0500283
284 if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
285 error_setg(errp, "incorrect option length %" PRIu32, len);
286 nbd_send_opt_abort(ioc);
287 return -1;
288 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300289 if (nbd_read32(ioc, &namelen, "option name length", errp) < 0) {
Eric Blake75368aa2016-10-14 13:33:13 -0500290 nbd_send_opt_abort(ioc);
291 return -1;
292 }
Eric Blake75368aa2016-10-14 13:33:13 -0500293 len -= sizeof(namelen);
Eric Blake93676c82019-11-13 20:46:34 -0600294 if (len < namelen || namelen > NBD_MAX_STRING_SIZE) {
295 error_setg(errp, "incorrect name length in server's list response");
Eric Blake75368aa2016-10-14 13:33:13 -0500296 nbd_send_opt_abort(ioc);
297 return -1;
298 }
Eric Blake75368aa2016-10-14 13:33:13 -0500299
Eric Blake091d0bf2019-01-17 13:36:45 -0600300 local_name = g_malloc(namelen + 1);
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300301 if (nbd_read(ioc, local_name, namelen, "export name", errp) < 0) {
Eric Blake75368aa2016-10-14 13:33:13 -0500302 nbd_send_opt_abort(ioc);
Eric Blakedf18c042019-08-24 12:28:12 -0500303 return -1;
Eric Blake75368aa2016-10-14 13:33:13 -0500304 }
Eric Blake091d0bf2019-01-17 13:36:45 -0600305 local_name[namelen] = '\0';
Eric Blake75368aa2016-10-14 13:33:13 -0500306 len -= namelen;
Eric Blake091d0bf2019-01-17 13:36:45 -0600307 if (len) {
Eric Blake93676c82019-11-13 20:46:34 -0600308 if (len > NBD_MAX_STRING_SIZE) {
309 error_setg(errp, "incorrect description length in server's "
310 "list response");
311 nbd_send_opt_abort(ioc);
312 return -1;
313 }
Eric Blake091d0bf2019-01-17 13:36:45 -0600314 local_desc = g_malloc(len + 1);
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300315 if (nbd_read(ioc, local_desc, len, "export description", errp) < 0) {
Eric Blake091d0bf2019-01-17 13:36:45 -0600316 nbd_send_opt_abort(ioc);
Eric Blakedf18c042019-08-24 12:28:12 -0500317 return -1;
Eric Blake091d0bf2019-01-17 13:36:45 -0600318 }
319 local_desc[len] = '\0';
Eric Blake75368aa2016-10-14 13:33:13 -0500320 }
Eric Blake091d0bf2019-01-17 13:36:45 -0600321
322 trace_nbd_receive_list(local_name, local_desc ?: "");
Eric Blakedf18c042019-08-24 12:28:12 -0500323 *name = g_steal_pointer(&local_name);
Eric Blake091d0bf2019-01-17 13:36:45 -0600324 if (description) {
Eric Blakedf18c042019-08-24 12:28:12 -0500325 *description = g_steal_pointer(&local_desc);
Eric Blake75368aa2016-10-14 13:33:13 -0500326 }
Eric Blakedf18c042019-08-24 12:28:12 -0500327 return 1;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000328}
329
330
Eric Blake138796d2019-01-17 13:36:53 -0600331/*
332 * nbd_opt_info_or_go:
333 * Send option for NBD_OPT_INFO or NBD_OPT_GO and parse the reply.
334 * Returns -1 if the option proves the export @info->name cannot be
335 * used, 0 if the option is unsupported (fall back to NBD_OPT_LIST and
Eric Blake8ecaeae2017-07-07 15:30:47 -0500336 * NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to
Eric Blake138796d2019-01-17 13:36:53 -0600337 * go (with the rest of @info populated).
338 */
339static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt,
340 NBDExportInfo *info, Error **errp)
Eric Blake8ecaeae2017-07-07 15:30:47 -0500341{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +0200342 ERRP_GUARD();
Vladimir Sementsov-Ogievskiy420a4e92017-11-22 13:19:57 +0300343 NBDOptionReply reply;
Eric Blake6dc16672019-01-17 13:36:46 -0600344 uint32_t len = strlen(info->name);
Eric Blake8ecaeae2017-07-07 15:30:47 -0500345 uint16_t type;
346 int error;
347 char *buf;
348
349 /* The protocol requires that the server send NBD_INFO_EXPORT with
350 * a non-zero flags (at least NBD_FLAG_HAS_FLAGS must be set); so
351 * flags still 0 is a witness of a broken server. */
352 info->flags = 0;
353
Eric Blake138796d2019-01-17 13:36:53 -0600354 assert(opt == NBD_OPT_GO || opt == NBD_OPT_INFO);
355 trace_nbd_opt_info_go_start(nbd_opt_lookup(opt), info->name);
Eric Blake081dd1f2017-07-07 15:30:49 -0500356 buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1);
Eric Blake8ecaeae2017-07-07 15:30:47 -0500357 stl_be_p(buf, len);
Eric Blake6dc16672019-01-17 13:36:46 -0600358 memcpy(buf + 4, info->name, len);
Eric Blake081dd1f2017-07-07 15:30:49 -0500359 /* At most one request, everything else up to server */
360 stw_be_p(buf + 4 + len, info->request_sizes);
361 if (info->request_sizes) {
362 stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE);
363 }
Eric Blake138796d2019-01-17 13:36:53 -0600364 error = nbd_send_option_request(ioc, opt,
Philippe Mathieu-Daudé158b9aa2017-07-26 23:42:09 -0300365 4 + len + 2 + 2 * info->request_sizes,
366 buf, errp);
367 g_free(buf);
368 if (error < 0) {
Eric Blake8ecaeae2017-07-07 15:30:47 -0500369 return -1;
370 }
371
372 while (1) {
Eric Blake138796d2019-01-17 13:36:53 -0600373 if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
Eric Blake8ecaeae2017-07-07 15:30:47 -0500374 return -1;
375 }
Eric Blake5de47732019-08-24 12:28:13 -0500376 error = nbd_handle_reply_err(ioc, &reply, true, errp);
Eric Blake8ecaeae2017-07-07 15:30:47 -0500377 if (error <= 0) {
378 return error;
379 }
380 len = reply.length;
381
382 if (reply.type == NBD_REP_ACK) {
Eric Blake138796d2019-01-17 13:36:53 -0600383 /*
384 * Server is done sending info, and moved into transmission
385 * phase for NBD_OPT_GO, but make sure it sent flags
386 */
Eric Blake8ecaeae2017-07-07 15:30:47 -0500387 if (len) {
388 error_setg(errp, "server sent invalid NBD_REP_ACK");
Eric Blake8ecaeae2017-07-07 15:30:47 -0500389 return -1;
390 }
391 if (!info->flags) {
392 error_setg(errp, "broken server omitted NBD_INFO_EXPORT");
Eric Blake8ecaeae2017-07-07 15:30:47 -0500393 return -1;
394 }
Eric Blake138796d2019-01-17 13:36:53 -0600395 trace_nbd_opt_info_go_success(nbd_opt_lookup(opt));
Eric Blake8ecaeae2017-07-07 15:30:47 -0500396 return 1;
397 }
398 if (reply.type != NBD_REP_INFO) {
Eric Blake6c5c0352018-12-15 07:53:07 -0600399 error_setg(errp, "unexpected reply type %u (%s), expected %u (%s)",
400 reply.type, nbd_rep_lookup(reply.type),
401 NBD_REP_INFO, nbd_rep_lookup(NBD_REP_INFO));
Eric Blake8ecaeae2017-07-07 15:30:47 -0500402 nbd_send_opt_abort(ioc);
403 return -1;
404 }
405 if (len < sizeof(type)) {
406 error_setg(errp, "NBD_REP_INFO length %" PRIu32 " is too short",
407 len);
408 nbd_send_opt_abort(ioc);
409 return -1;
410 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300411 if (nbd_read16(ioc, &type, "info type", errp) < 0) {
Eric Blake8ecaeae2017-07-07 15:30:47 -0500412 nbd_send_opt_abort(ioc);
413 return -1;
414 }
415 len -= sizeof(type);
Eric Blake8ecaeae2017-07-07 15:30:47 -0500416 switch (type) {
417 case NBD_INFO_EXPORT:
418 if (len != sizeof(info->size) + sizeof(info->flags)) {
419 error_setg(errp, "remaining export info len %" PRIu32
420 " is unexpected size", len);
421 nbd_send_opt_abort(ioc);
422 return -1;
423 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300424 if (nbd_read64(ioc, &info->size, "info size", errp) < 0) {
Eric Blake8ecaeae2017-07-07 15:30:47 -0500425 nbd_send_opt_abort(ioc);
426 return -1;
427 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300428 if (nbd_read16(ioc, &info->flags, "info flags", errp) < 0) {
Eric Blake8ecaeae2017-07-07 15:30:47 -0500429 nbd_send_opt_abort(ioc);
430 return -1;
431 }
Eric Blake3add3ab2019-03-30 10:57:04 -0500432 if (info->min_block &&
433 !QEMU_IS_ALIGNED(info->size, info->min_block)) {
Eric Blakee53f88d2019-04-04 09:52:26 -0500434 error_setg(errp, "export size %" PRIu64 " is not multiple of "
Eric Blake3add3ab2019-03-30 10:57:04 -0500435 "minimum block size %" PRIu32, info->size,
436 info->min_block);
437 nbd_send_opt_abort(ioc);
438 return -1;
439 }
Eric Blake8ecaeae2017-07-07 15:30:47 -0500440 trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
441 break;
442
Eric Blake081dd1f2017-07-07 15:30:49 -0500443 case NBD_INFO_BLOCK_SIZE:
444 if (len != sizeof(info->min_block) * 3) {
445 error_setg(errp, "remaining export info len %" PRIu32
446 " is unexpected size", len);
447 nbd_send_opt_abort(ioc);
448 return -1;
449 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300450 if (nbd_read32(ioc, &info->min_block, "info minimum block size",
451 errp) < 0) {
Eric Blake081dd1f2017-07-07 15:30:49 -0500452 nbd_send_opt_abort(ioc);
453 return -1;
454 }
Eric Blake081dd1f2017-07-07 15:30:49 -0500455 if (!is_power_of_2(info->min_block)) {
Eric Blakee475d102018-05-01 10:46:53 -0500456 error_setg(errp, "server minimum block size %" PRIu32
457 " is not a power of two", info->min_block);
Eric Blake081dd1f2017-07-07 15:30:49 -0500458 nbd_send_opt_abort(ioc);
459 return -1;
460 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300461 if (nbd_read32(ioc, &info->opt_block, "info preferred block size",
462 errp) < 0)
463 {
Eric Blake081dd1f2017-07-07 15:30:49 -0500464 nbd_send_opt_abort(ioc);
465 return -1;
466 }
Eric Blake081dd1f2017-07-07 15:30:49 -0500467 if (!is_power_of_2(info->opt_block) ||
468 info->opt_block < info->min_block) {
Eric Blakee475d102018-05-01 10:46:53 -0500469 error_setg(errp, "server preferred block size %" PRIu32
470 " is not valid", info->opt_block);
Eric Blake081dd1f2017-07-07 15:30:49 -0500471 nbd_send_opt_abort(ioc);
472 return -1;
473 }
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300474 if (nbd_read32(ioc, &info->max_block, "info maximum block size",
475 errp) < 0)
476 {
Eric Blake081dd1f2017-07-07 15:30:49 -0500477 nbd_send_opt_abort(ioc);
478 return -1;
479 }
Eric Blakee475d102018-05-01 10:46:53 -0500480 if (info->max_block < info->min_block) {
481 error_setg(errp, "server maximum block size %" PRIu32
482 " is not valid", info->max_block);
483 nbd_send_opt_abort(ioc);
484 return -1;
485 }
Eric Blake138796d2019-01-17 13:36:53 -0600486 trace_nbd_opt_info_block_size(info->min_block, info->opt_block,
487 info->max_block);
Eric Blake081dd1f2017-07-07 15:30:49 -0500488 break;
489
Eric Blake8ecaeae2017-07-07 15:30:47 -0500490 default:
Eric Blake93676c82019-11-13 20:46:34 -0600491 /*
492 * Not worth the bother to check if NBD_INFO_NAME or
493 * NBD_INFO_DESCRIPTION exceed NBD_MAX_STRING_SIZE.
494 */
Eric Blake138796d2019-01-17 13:36:53 -0600495 trace_nbd_opt_info_unknown(type, nbd_info_lookup(type));
Eric Blake8ecaeae2017-07-07 15:30:47 -0500496 if (nbd_drop(ioc, len, errp) < 0) {
Eric Blakecb6b1a32017-11-13 09:24:24 -0600497 error_prepend(errp, "Failed to read info payload: ");
Eric Blake8ecaeae2017-07-07 15:30:47 -0500498 nbd_send_opt_abort(ioc);
499 return -1;
500 }
501 break;
502 }
503 }
504}
505
Eric Blake75368aa2016-10-14 13:33:13 -0500506/* Return -1 on failure, 0 if wantname is an available export. */
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000507static int nbd_receive_query_exports(QIOChannel *ioc,
508 const char *wantname,
509 Error **errp)
510{
Eric Blake091d0bf2019-01-17 13:36:45 -0600511 bool list_empty = true;
512 bool found_export = false;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000513
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300514 trace_nbd_receive_query_exports_start(wantname);
Eric Blakec8a3a1b2016-10-14 13:33:10 -0500515 if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000516 return -1;
517 }
518
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000519 while (1) {
Eric Blake091d0bf2019-01-17 13:36:45 -0600520 char *name;
521 int ret = nbd_receive_list(ioc, &name, NULL, errp);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000522
523 if (ret < 0) {
Eric Blake75368aa2016-10-14 13:33:13 -0500524 /* Server gave unexpected reply */
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000525 return -1;
Eric Blake75368aa2016-10-14 13:33:13 -0500526 } else if (ret == 0) {
527 /* Done iterating. */
Eric Blake091d0bf2019-01-17 13:36:45 -0600528 if (list_empty) {
529 /*
530 * We don't have enough context to tell a server that
531 * sent an empty list apart from a server that does
532 * not support the list command; but as this function
533 * is just used to trigger a nicer error message
534 * before trying NBD_OPT_EXPORT_NAME, assume the
535 * export is available.
536 */
537 return 0;
538 } else if (!found_export) {
Eric Blake75368aa2016-10-14 13:33:13 -0500539 error_setg(errp, "No export with name '%s' available",
540 wantname);
541 nbd_send_opt_abort(ioc);
542 return -1;
543 }
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300544 trace_nbd_receive_query_exports_success(wantname);
Eric Blake75368aa2016-10-14 13:33:13 -0500545 return 0;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000546 }
Eric Blake091d0bf2019-01-17 13:36:45 -0600547 list_empty = false;
548 if (!strcmp(name, wantname)) {
549 found_export = true;
550 }
551 g_free(name);
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000552 }
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000553}
554
Eric Blake5de47732019-08-24 12:28:13 -0500555/*
556 * nbd_request_simple_option: Send an option request, and parse the reply.
557 * @strict controls whether ERR_UNSUP or all errors produce 0 status.
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200558 * return 1 for successful negotiation,
559 * 0 if operation is unsupported,
560 * -1 with errp set for any other error
561 */
Eric Blake5de47732019-08-24 12:28:13 -0500562static int nbd_request_simple_option(QIOChannel *ioc, int opt, bool strict,
563 Error **errp)
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200564{
Vladimir Sementsov-Ogievskiy420a4e92017-11-22 13:19:57 +0300565 NBDOptionReply reply;
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200566 int error;
567
568 if (nbd_send_option_request(ioc, opt, 0, NULL, errp) < 0) {
569 return -1;
570 }
571
572 if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
573 return -1;
574 }
Eric Blake5de47732019-08-24 12:28:13 -0500575 error = nbd_handle_reply_err(ioc, &reply, strict, errp);
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200576 if (error <= 0) {
577 return error;
578 }
579
580 if (reply.type != NBD_REP_ACK) {
581 error_setg(errp, "Server answered option %d (%s) with unexpected "
Vladimir Sementsov-Ogievskiy28fb4942018-02-15 16:51:43 +0300582 "reply %" PRIu32 " (%s)", opt, nbd_opt_lookup(opt),
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200583 reply.type, nbd_rep_lookup(reply.type));
584 nbd_send_opt_abort(ioc);
585 return -1;
586 }
587
588 if (reply.length != 0) {
589 error_setg(errp, "Option %d ('%s') response length is %" PRIu32
590 " (it should be zero)", opt, nbd_opt_lookup(opt),
591 reply.length);
592 nbd_send_opt_abort(ioc);
593 return -1;
594 }
595
596 return 1;
597}
598
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000599static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
600 QCryptoTLSCreds *tlscreds,
601 const char *hostname, Error **errp)
602{
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200603 int ret;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000604 QIOChannelTLS *tioc;
605 struct NBDTLSHandshakeData data = { 0 };
606
Eric Blake5de47732019-08-24 12:28:13 -0500607 ret = nbd_request_simple_option(ioc, NBD_OPT_STARTTLS, true, errp);
Vladimir Sementsov-Ogievskiyd7952992017-10-27 12:40:34 +0200608 if (ret <= 0) {
609 if (ret == 0) {
610 error_setg(errp, "Server don't support STARTTLS option");
611 nbd_send_opt_abort(ioc);
612 }
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000613 return NULL;
614 }
615
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300616 trace_nbd_receive_starttls_new_client();
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000617 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
618 if (!tioc) {
619 return NULL;
620 }
Daniel P. Berrange0d73f722016-09-30 11:57:14 +0100621 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000622 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300623 trace_nbd_receive_starttls_tls_handshake();
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000624 qio_channel_tls_handshake(tioc,
625 nbd_tls_handshake,
626 &data,
Peter Xu1939ccd2018-03-05 14:43:24 +0800627 NULL,
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000628 NULL);
629
630 if (!data.complete) {
631 g_main_loop_run(data.loop);
632 }
633 g_main_loop_unref(data.loop);
634 if (data.error) {
635 error_propagate(errp, data.error);
636 object_unref(OBJECT(tioc));
637 return NULL;
638 }
639
640 return QIO_CHANNEL(tioc);
641}
642
Eric Blake757b3ab2019-01-17 13:36:48 -0600643/*
644 * nbd_send_meta_query:
645 * Send 0 or 1 set/list meta context queries.
646 * Return 0 on success, -1 with errp set for any error
647 */
648static int nbd_send_meta_query(QIOChannel *ioc, uint32_t opt,
649 const char *export, const char *query,
650 Error **errp)
651{
652 int ret;
Eric Blakef47b6ea2023-06-08 08:56:30 -0500653 uint32_t export_len;
Eric Blake757b3ab2019-01-17 13:36:48 -0600654 uint32_t queries = !!query;
655 uint32_t query_len = 0;
656 uint32_t data_len;
657 char *data;
658 char *p;
659
Eric Blakef47b6ea2023-06-08 08:56:30 -0500660 assert(strnlen(export, NBD_MAX_STRING_SIZE + 1) <= NBD_MAX_STRING_SIZE);
661 export_len = strlen(export);
Eric Blake757b3ab2019-01-17 13:36:48 -0600662 data_len = sizeof(export_len) + export_len + sizeof(queries);
663 if (query) {
Eric Blakef47b6ea2023-06-08 08:56:30 -0500664 assert(strnlen(query, NBD_MAX_STRING_SIZE + 1) <= NBD_MAX_STRING_SIZE);
Eric Blake757b3ab2019-01-17 13:36:48 -0600665 query_len = strlen(query);
666 data_len += sizeof(query_len) + query_len;
667 } else {
668 assert(opt == NBD_OPT_LIST_META_CONTEXT);
669 }
670 p = data = g_malloc(data_len);
671
672 trace_nbd_opt_meta_request(nbd_opt_lookup(opt), query ?: "(all)", export);
673 stl_be_p(p, export_len);
674 memcpy(p += sizeof(export_len), export, export_len);
675 stl_be_p(p += export_len, queries);
676 if (query) {
677 stl_be_p(p += sizeof(queries), query_len);
678 memcpy(p += sizeof(query_len), query, query_len);
679 }
680
681 ret = nbd_send_option_request(ioc, opt, data_len, data, errp);
682 g_free(data);
683 return ret;
684}
685
Eric Blake0182c1a2019-01-17 13:36:49 -0600686/*
687 * nbd_receive_one_meta_context:
688 * Called in a loop to receive and trace one set/list meta context reply.
689 * Pass non-NULL @name or @id to collect results back to the caller, which
690 * must eventually call g_free().
691 * return 1 if name is set and iteration must continue,
692 * 0 if iteration is complete (including if option is unsupported),
693 * -1 with errp set for any error
694 */
695static int nbd_receive_one_meta_context(QIOChannel *ioc,
696 uint32_t opt,
697 char **name,
698 uint32_t *id,
699 Error **errp)
700{
701 int ret;
702 NBDOptionReply reply;
703 char *local_name = NULL;
704 uint32_t local_id;
705
706 if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
707 return -1;
708 }
709
Eric Blake5de47732019-08-24 12:28:13 -0500710 ret = nbd_handle_reply_err(ioc, &reply, false, errp);
Eric Blake0182c1a2019-01-17 13:36:49 -0600711 if (ret <= 0) {
712 return ret;
713 }
714
715 if (reply.type == NBD_REP_ACK) {
716 if (reply.length != 0) {
717 error_setg(errp, "Unexpected length to ACK response");
718 nbd_send_opt_abort(ioc);
719 return -1;
720 }
721 return 0;
722 } else if (reply.type != NBD_REP_META_CONTEXT) {
723 error_setg(errp, "Unexpected reply type %u (%s), expected %u (%s)",
724 reply.type, nbd_rep_lookup(reply.type),
725 NBD_REP_META_CONTEXT, nbd_rep_lookup(NBD_REP_META_CONTEXT));
726 nbd_send_opt_abort(ioc);
727 return -1;
728 }
729
730 if (reply.length <= sizeof(local_id) ||
731 reply.length > NBD_MAX_BUFFER_SIZE) {
732 error_setg(errp, "Failed to negotiate meta context, server "
733 "answered with unexpected length %" PRIu32,
734 reply.length);
735 nbd_send_opt_abort(ioc);
736 return -1;
737 }
738
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300739 if (nbd_read32(ioc, &local_id, "context id", errp) < 0) {
Eric Blake0182c1a2019-01-17 13:36:49 -0600740 return -1;
741 }
Eric Blake0182c1a2019-01-17 13:36:49 -0600742
743 reply.length -= sizeof(local_id);
744 local_name = g_malloc(reply.length + 1);
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300745 if (nbd_read(ioc, local_name, reply.length, "context name", errp) < 0) {
Eric Blake0182c1a2019-01-17 13:36:49 -0600746 g_free(local_name);
747 return -1;
748 }
749 local_name[reply.length] = '\0';
750 trace_nbd_opt_meta_reply(nbd_opt_lookup(opt), local_name, local_id);
751
752 if (name) {
753 *name = local_name;
754 } else {
755 g_free(local_name);
756 }
757 if (id) {
758 *id = local_id;
759 }
760 return 1;
761}
762
763/*
764 * nbd_negotiate_simple_meta_context:
Eric Blake2df94eb2019-01-17 13:36:47 -0600765 * Request the server to set the meta context for export @info->name
766 * using @info->x_dirty_bitmap with a fallback to "base:allocation",
767 * setting @info->context_id to the resulting id. Fail if the server
768 * responds with more than one context or with a context different
769 * than the query.
770 * return 1 for successful negotiation,
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300771 * 0 if operation is unsupported,
772 * -1 with errp set for any other error
773 */
774static int nbd_negotiate_simple_meta_context(QIOChannel *ioc,
Eric Blake2df94eb2019-01-17 13:36:47 -0600775 NBDExportInfo *info,
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300776 Error **errp)
777{
Eric Blake2df94eb2019-01-17 13:36:47 -0600778 /*
779 * TODO: Removing the x_dirty_bitmap hack will mean refactoring
780 * this function to request and store ids for multiple contexts
781 * (both base:allocation and a dirty bitmap), at which point this
782 * function should lose the term _simple.
783 */
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300784 int ret;
Eric Blake2df94eb2019-01-17 13:36:47 -0600785 const char *context = info->x_dirty_bitmap ?: "base:allocation";
Vladimir Sementsov-Ogievskiy89aa0d82018-04-27 17:20:01 +0300786 bool received = false;
Eric Blake0182c1a2019-01-17 13:36:49 -0600787 char *name = NULL;
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300788
Eric Blake757b3ab2019-01-17 13:36:48 -0600789 if (nbd_send_meta_query(ioc, NBD_OPT_SET_META_CONTEXT,
790 info->name, context, errp) < 0) {
791 return -1;
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300792 }
793
Eric Blake0182c1a2019-01-17 13:36:49 -0600794 ret = nbd_receive_one_meta_context(ioc, NBD_OPT_SET_META_CONTEXT,
795 &name, &info->context_id, errp);
796 if (ret < 0) {
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300797 return -1;
798 }
Eric Blake0182c1a2019-01-17 13:36:49 -0600799 if (ret == 1) {
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300800 if (strcmp(context, name)) {
801 error_setg(errp, "Failed to negotiate meta context '%s', server "
802 "answered with different context '%s'", context,
803 name);
804 g_free(name);
Eric Blake260e34d2018-03-29 18:18:37 -0500805 nbd_send_opt_abort(ioc);
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300806 return -1;
807 }
808 g_free(name);
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300809 received = true;
810
Eric Blake0182c1a2019-01-17 13:36:49 -0600811 ret = nbd_receive_one_meta_context(ioc, NBD_OPT_SET_META_CONTEXT,
812 NULL, NULL, errp);
813 if (ret < 0) {
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300814 return -1;
815 }
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300816 }
Eric Blake0182c1a2019-01-17 13:36:49 -0600817 if (ret != 0) {
818 error_setg(errp, "Server answered with more than one context");
Eric Blake260e34d2018-03-29 18:18:37 -0500819 nbd_send_opt_abort(ioc);
820 return -1;
821 }
Eric Blake2df94eb2019-01-17 13:36:47 -0600822 return received;
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +0300823}
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000824
Eric Blake10b89982019-01-17 13:36:51 -0600825/*
Eric Blake0b576b62019-01-17 13:36:55 -0600826 * nbd_list_meta_contexts:
827 * Request the server to list all meta contexts for export @info->name.
828 * return 0 if list is complete (even if empty),
829 * -1 with errp set for any error
830 */
831static int nbd_list_meta_contexts(QIOChannel *ioc,
832 NBDExportInfo *info,
833 Error **errp)
834{
835 int ret;
Eric Blake7c6f5dd2019-01-17 13:36:57 -0600836 int seen_any = false;
837 int seen_qemu = false;
Eric Blake0b576b62019-01-17 13:36:55 -0600838
839 if (nbd_send_meta_query(ioc, NBD_OPT_LIST_META_CONTEXT,
840 info->name, NULL, errp) < 0) {
841 return -1;
842 }
843
844 while (1) {
845 char *context;
846
847 ret = nbd_receive_one_meta_context(ioc, NBD_OPT_LIST_META_CONTEXT,
848 &context, NULL, errp);
Eric Blake7c6f5dd2019-01-17 13:36:57 -0600849 if (ret == 0 && seen_any && !seen_qemu) {
850 /*
851 * Work around qemu 3.0 bug: the server forgot to send
852 * "qemu:" replies to 0 queries. If we saw at least one
853 * reply (probably base:allocation), but none of them were
854 * qemu:, then run a more specific query to make sure.
855 */
856 seen_qemu = true;
857 if (nbd_send_meta_query(ioc, NBD_OPT_LIST_META_CONTEXT,
858 info->name, "qemu:", errp) < 0) {
859 return -1;
860 }
861 continue;
862 }
Eric Blake0b576b62019-01-17 13:36:55 -0600863 if (ret <= 0) {
864 return ret;
865 }
Eric Blake7c6f5dd2019-01-17 13:36:57 -0600866 seen_any = true;
867 seen_qemu |= strstart(context, "qemu:", NULL);
Eric Blake0b576b62019-01-17 13:36:55 -0600868 info->contexts = g_renew(char *, info->contexts, ++info->n_contexts);
869 info->contexts[info->n_contexts - 1] = context;
870 }
871}
872
873/*
Eric Blake10b89982019-01-17 13:36:51 -0600874 * nbd_start_negotiate:
875 * Start the handshake to the server. After a positive return, the server
876 * is ready to accept additional NBD_OPT requests.
877 * Returns: negative errno: failure talking to server
Eric Blakebfe04d02023-06-08 08:56:37 -0500878 * non-negative: enum NBDMode describing server abilities
Eric Blake10b89982019-01-17 13:36:51 -0600879 */
Stefan Hajnoczi078c8ad2023-08-30 18:48:00 -0400880static int nbd_start_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
Eric Blake10b89982019-01-17 13:36:51 -0600881 const char *hostname, QIOChannel **outioc,
882 bool structured_reply, bool *zeroes,
883 Error **errp)
Fam Zheng798bfe02016-01-14 16:41:02 +0800884{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +0200885 ERRP_GUARD();
Eric Blake004a89f2017-07-07 15:30:41 -0500886 uint64_t magic;
Fam Zheng798bfe02016-01-14 16:41:02 +0800887
Eric Blake10b89982019-01-17 13:36:51 -0600888 trace_nbd_start_negotiate(tlscreds, hostname ? hostname : "<null>");
Fam Zheng798bfe02016-01-14 16:41:02 +0800889
Eric Blaked21a2d32019-01-17 13:36:54 -0600890 if (zeroes) {
891 *zeroes = true;
892 }
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000893 if (outioc) {
894 *outioc = NULL;
895 }
896 if (tlscreds && !outioc) {
897 error_setg(errp, "Output I/O channel required for TLS");
Eric Blake2b8d0952019-01-17 13:36:50 -0600898 return -EINVAL;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000899 }
900
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300901 if (nbd_read64(ioc, &magic, "initial magic", errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -0600902 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +0800903 }
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300904 trace_nbd_receive_negotiate_magic(magic);
Fam Zheng798bfe02016-01-14 16:41:02 +0800905
Eric Blakeef2e35f2018-12-15 07:53:10 -0600906 if (magic != NBD_INIT_MAGIC) {
907 error_setg(errp, "Bad initial magic received: 0x%" PRIx64, magic);
Eric Blake2b8d0952019-01-17 13:36:50 -0600908 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +0800909 }
910
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300911 if (nbd_read64(ioc, &magic, "server magic", errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -0600912 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +0800913 }
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300914 trace_nbd_receive_negotiate_magic(magic);
Fam Zheng798bfe02016-01-14 16:41:02 +0800915
Daniel P. Berrangef72d7052016-02-10 18:41:05 +0000916 if (magic == NBD_OPTS_MAGIC) {
Daniel P. Berrangee2a9d9a2016-02-10 18:41:07 +0000917 uint32_t clientflags = 0;
Daniel P. Berrangee2a9d9a2016-02-10 18:41:07 +0000918 uint16_t globalflags;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000919 bool fixedNewStyle = false;
Fam Zheng798bfe02016-01-14 16:41:02 +0800920
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300921 if (nbd_read16(ioc, &globalflags, "server flags", errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -0600922 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +0800923 }
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +0300924 trace_nbd_receive_negotiate_server_flags(globalflags);
Daniel P. Berrangee2a9d9a2016-02-10 18:41:07 +0000925 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000926 fixedNewStyle = true;
Daniel P. Berrangee2a9d9a2016-02-10 18:41:07 +0000927 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
928 }
Eric Blakec203c592016-10-14 13:33:14 -0500929 if (globalflags & NBD_FLAG_NO_ZEROES) {
Eric Blaked21a2d32019-01-17 13:36:54 -0600930 if (zeroes) {
931 *zeroes = false;
932 }
Eric Blakec203c592016-10-14 13:33:14 -0500933 clientflags |= NBD_FLAG_C_NO_ZEROES;
934 }
Daniel P. Berrangee2a9d9a2016-02-10 18:41:07 +0000935 /* client requested flags */
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000936 clientflags = cpu_to_be32(clientflags);
Vladimir Sementsov-Ogievskiyd1fdf252017-06-02 18:01:39 +0300937 if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) {
Eric Blakecb6b1a32017-11-13 09:24:24 -0600938 error_prepend(errp, "Failed to send clientflags field: ");
Eric Blake2b8d0952019-01-17 13:36:50 -0600939 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +0800940 }
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000941 if (tlscreds) {
942 if (fixedNewStyle) {
943 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
944 if (!*outioc) {
Eric Blake2b8d0952019-01-17 13:36:50 -0600945 return -EINVAL;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000946 }
947 ioc = *outioc;
948 } else {
949 error_setg(errp, "Server does not support STARTTLS");
Eric Blake2b8d0952019-01-17 13:36:50 -0600950 return -EINVAL;
Daniel P. Berrangef95910f2016-02-10 18:41:11 +0000951 }
952 }
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +0000953 if (fixedNewStyle) {
Eric Blake10b89982019-01-17 13:36:51 -0600954 int result = 0;
Eric Blake8ecaeae2017-07-07 15:30:47 -0500955
Vladimir Sementsov-Ogievskiyf140e302017-10-27 12:40:37 +0200956 if (structured_reply) {
957 result = nbd_request_simple_option(ioc,
958 NBD_OPT_STRUCTURED_REPLY,
Eric Blake5de47732019-08-24 12:28:13 -0500959 false, errp);
Vladimir Sementsov-Ogievskiyf140e302017-10-27 12:40:37 +0200960 if (result < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -0600961 return -EINVAL;
Vladimir Sementsov-Ogievskiyf140e302017-10-27 12:40:37 +0200962 }
Vladimir Sementsov-Ogievskiyf140e302017-10-27 12:40:37 +0200963 }
Eric Blakebfe04d02023-06-08 08:56:37 -0500964 return result ? NBD_MODE_STRUCTURED : NBD_MODE_SIMPLE;
Eric Blake10b89982019-01-17 13:36:51 -0600965 } else {
Eric Blakebfe04d02023-06-08 08:56:37 -0500966 return NBD_MODE_EXPORT_NAME;
Eric Blake10b89982019-01-17 13:36:51 -0600967 }
968 } else if (magic == NBD_CLIENT_MAGIC) {
969 if (tlscreds) {
970 error_setg(errp, "Server does not support STARTTLS");
971 return -EINVAL;
972 }
Eric Blakebfe04d02023-06-08 08:56:37 -0500973 return NBD_MODE_OLDSTYLE;
Eric Blake10b89982019-01-17 13:36:51 -0600974 } else {
975 error_setg(errp, "Bad server magic received: 0x%" PRIx64, magic);
976 return -EINVAL;
977 }
978}
Vladimir Sementsov-Ogievskiyf140e302017-10-27 12:40:37 +0200979
Eric Blake10b89982019-01-17 13:36:51 -0600980/*
Eric Blakeb3c9d332019-01-17 13:36:52 -0600981 * nbd_negotiate_finish_oldstyle:
982 * Populate @info with the size and export flags from an oldstyle server,
983 * but does not consume 124 bytes of reserved zero padding.
984 * Returns 0 on success, -1 with @errp set on failure
985 */
986static int nbd_negotiate_finish_oldstyle(QIOChannel *ioc, NBDExportInfo *info,
987 Error **errp)
988{
989 uint32_t oldflags;
990
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300991 if (nbd_read64(ioc, &info->size, "export length", errp) < 0) {
Eric Blakeb3c9d332019-01-17 13:36:52 -0600992 return -EINVAL;
993 }
Eric Blakeb3c9d332019-01-17 13:36:52 -0600994
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +0300995 if (nbd_read32(ioc, &oldflags, "export flags", errp) < 0) {
Eric Blakeb3c9d332019-01-17 13:36:52 -0600996 return -EINVAL;
997 }
Eric Blakeb3c9d332019-01-17 13:36:52 -0600998 if (oldflags & ~0xffff) {
999 error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
1000 return -EINVAL;
1001 }
1002 info->flags = oldflags;
1003 return 0;
1004}
1005
1006/*
Eric Blake10b89982019-01-17 13:36:51 -06001007 * nbd_receive_negotiate:
1008 * Connect to server, complete negotiation, and move into transmission phase.
1009 * Returns: negative errno: failure talking to server
1010 * 0: server is connected
1011 */
Stefan Hajnoczib84ca912023-08-30 18:47:59 -04001012int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
Eric Blake10b89982019-01-17 13:36:51 -06001013 const char *hostname, QIOChannel **outioc,
1014 NBDExportInfo *info, Error **errp)
1015{
Vladimir Sementsov-Ogievskiy795d9462020-07-07 18:50:36 +02001016 ERRP_GUARD();
Eric Blake10b89982019-01-17 13:36:51 -06001017 int result;
1018 bool zeroes;
1019 bool base_allocation = info->base_allocation;
Vladimir Sementsov-Ogievskiy78a33ab2018-03-12 18:21:23 +03001020
Eric Blake93676c82019-11-13 20:46:34 -06001021 assert(info->name && strlen(info->name) <= NBD_MAX_STRING_SIZE);
Eric Blake10b89982019-01-17 13:36:51 -06001022 trace_nbd_receive_negotiate_name(info->name);
1023
Stefan Hajnoczi078c8ad2023-08-30 18:48:00 -04001024 result = nbd_start_negotiate(ioc, tlscreds, hostname, outioc,
Eric Blake10b89982019-01-17 13:36:51 -06001025 info->structured_reply, &zeroes, errp);
Eric Blakebfe04d02023-06-08 08:56:37 -05001026 if (result < 0) {
1027 return result;
1028 }
Eric Blake10b89982019-01-17 13:36:51 -06001029
1030 info->structured_reply = false;
1031 info->base_allocation = false;
1032 if (tlscreds && *outioc) {
1033 ioc = *outioc;
1034 }
1035
Eric Blakebfe04d02023-06-08 08:56:37 -05001036 switch ((NBDMode)result) {
1037 case NBD_MODE_STRUCTURED:
Eric Blake10b89982019-01-17 13:36:51 -06001038 info->structured_reply = true;
1039 if (base_allocation) {
1040 result = nbd_negotiate_simple_meta_context(ioc, info, errp);
Eric Blake8ecaeae2017-07-07 15:30:47 -05001041 if (result < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -06001042 return -EINVAL;
Eric Blake8ecaeae2017-07-07 15:30:47 -05001043 }
Eric Blake10b89982019-01-17 13:36:51 -06001044 info->base_allocation = result == 1;
Daniel P. Berrange9344e5f2016-02-10 18:41:09 +00001045 }
Eric Blake10b89982019-01-17 13:36:51 -06001046 /* fall through */
Eric Blakebfe04d02023-06-08 08:56:37 -05001047 case NBD_MODE_SIMPLE:
Eric Blake10b89982019-01-17 13:36:51 -06001048 /* Try NBD_OPT_GO first - if it works, we are done (it
1049 * also gives us a good message if the server requires
1050 * TLS). If it is not available, fall back to
1051 * NBD_OPT_LIST for nicer error messages about a missing
1052 * export, then use NBD_OPT_EXPORT_NAME. */
Eric Blake138796d2019-01-17 13:36:53 -06001053 result = nbd_opt_info_or_go(ioc, NBD_OPT_GO, info, errp);
Eric Blake10b89982019-01-17 13:36:51 -06001054 if (result < 0) {
1055 return -EINVAL;
1056 }
1057 if (result > 0) {
1058 return 0;
1059 }
1060 /* Check our desired export is present in the
1061 * server export list. Since NBD_OPT_EXPORT_NAME
1062 * cannot return an error message, running this
1063 * query gives us better error reporting if the
1064 * export name is not available.
1065 */
1066 if (nbd_receive_query_exports(ioc, info->name, errp) < 0) {
1067 return -EINVAL;
1068 }
1069 /* fall through */
Eric Blakebfe04d02023-06-08 08:56:37 -05001070 case NBD_MODE_EXPORT_NAME:
Eric Blakec8a3a1b2016-10-14 13:33:10 -05001071 /* write the export name request */
Eric Blake6dc16672019-01-17 13:36:46 -06001072 if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, info->name,
Eric Blakec8a3a1b2016-10-14 13:33:10 -05001073 errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -06001074 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +08001075 }
Fam Zheng798bfe02016-01-14 16:41:02 +08001076
Eric Blakec8a3a1b2016-10-14 13:33:10 -05001077 /* Read the response */
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001078 if (nbd_read64(ioc, &info->size, "export length", errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -06001079 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +08001080 }
Fam Zheng798bfe02016-01-14 16:41:02 +08001081
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001082 if (nbd_read16(ioc, &info->flags, "export flags", errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -06001083 return -EINVAL;
Daniel P. Berrangef72d7052016-02-10 18:41:05 +00001084 }
Eric Blake10b89982019-01-17 13:36:51 -06001085 break;
Eric Blakebfe04d02023-06-08 08:56:37 -05001086 case NBD_MODE_OLDSTYLE:
Eric Blake6dc16672019-01-17 13:36:46 -06001087 if (*info->name) {
1088 error_setg(errp, "Server does not support non-empty export names");
Eric Blake2b8d0952019-01-17 13:36:50 -06001089 return -EINVAL;
Daniel P. Berrangef72d7052016-02-10 18:41:05 +00001090 }
Eric Blakeb3c9d332019-01-17 13:36:52 -06001091 if (nbd_negotiate_finish_oldstyle(ioc, info, errp) < 0) {
Eric Blake2b8d0952019-01-17 13:36:50 -06001092 return -EINVAL;
Daniel P. Berrangef72d7052016-02-10 18:41:05 +00001093 }
Eric Blake10b89982019-01-17 13:36:51 -06001094 break;
1095 default:
Eric Blakebfe04d02023-06-08 08:56:37 -05001096 g_assert_not_reached();
Fam Zheng798bfe02016-01-14 16:41:02 +08001097 }
Daniel P. Berrangef72d7052016-02-10 18:41:05 +00001098
Eric Blake004a89f2017-07-07 15:30:41 -05001099 trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
Vladimir Sementsov-Ogievskiyd1fdf252017-06-02 18:01:39 +03001100 if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
Eric Blakecb6b1a32017-11-13 09:24:24 -06001101 error_prepend(errp, "Failed to read reserved block: ");
Eric Blake2b8d0952019-01-17 13:36:50 -06001102 return -EINVAL;
Fam Zheng798bfe02016-01-14 16:41:02 +08001103 }
Eric Blake2b8d0952019-01-17 13:36:50 -06001104 return 0;
Fam Zheng798bfe02016-01-14 16:41:02 +08001105}
1106
Eric Blaked21a2d32019-01-17 13:36:54 -06001107/* Clean up result of nbd_receive_export_list */
1108void nbd_free_export_list(NBDExportInfo *info, int count)
1109{
Eric Blake0b576b62019-01-17 13:36:55 -06001110 int i, j;
Eric Blaked21a2d32019-01-17 13:36:54 -06001111
1112 if (!info) {
1113 return;
1114 }
1115
1116 for (i = 0; i < count; i++) {
1117 g_free(info[i].name);
1118 g_free(info[i].description);
Eric Blake0b576b62019-01-17 13:36:55 -06001119 for (j = 0; j < info[i].n_contexts; j++) {
1120 g_free(info[i].contexts[j]);
1121 }
1122 g_free(info[i].contexts);
Eric Blaked21a2d32019-01-17 13:36:54 -06001123 }
1124 g_free(info);
1125}
1126
1127/*
1128 * nbd_receive_export_list:
1129 * Query details about a server's exports, then disconnect without
1130 * going into transmission phase. Return a count of the exports listed
1131 * in @info by the server, or -1 on error. Caller must free @info using
1132 * nbd_free_export_list().
1133 */
1134int nbd_receive_export_list(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
1135 const char *hostname, NBDExportInfo **info,
1136 Error **errp)
1137{
1138 int result;
1139 int count = 0;
1140 int i;
1141 int rc;
1142 int ret = -1;
1143 NBDExportInfo *array = NULL;
1144 QIOChannel *sioc = NULL;
1145
1146 *info = NULL;
Stefan Hajnoczi078c8ad2023-08-30 18:48:00 -04001147 result = nbd_start_negotiate(ioc, tlscreds, hostname, &sioc, true,
Vladimir Sementsov-Ogievskiya8e2bb62019-06-18 14:43:21 +03001148 NULL, errp);
Eric Blaked21a2d32019-01-17 13:36:54 -06001149 if (tlscreds && sioc) {
1150 ioc = sioc;
1151 }
Eric Blakebfe04d02023-06-08 08:56:37 -05001152 if (result < 0) {
1153 goto out;
1154 }
Eric Blaked21a2d32019-01-17 13:36:54 -06001155
Eric Blakebfe04d02023-06-08 08:56:37 -05001156 switch ((NBDMode)result) {
1157 case NBD_MODE_SIMPLE:
1158 case NBD_MODE_STRUCTURED:
Eric Blaked21a2d32019-01-17 13:36:54 -06001159 /* newstyle - use NBD_OPT_LIST to populate array, then try
1160 * NBD_OPT_INFO on each array member. If structured replies
1161 * are enabled, also try NBD_OPT_LIST_META_CONTEXT. */
1162 if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
1163 goto out;
1164 }
1165 while (1) {
1166 char *name;
1167 char *desc;
1168
1169 rc = nbd_receive_list(ioc, &name, &desc, errp);
1170 if (rc < 0) {
1171 goto out;
1172 } else if (rc == 0) {
1173 break;
1174 }
1175 array = g_renew(NBDExportInfo, array, ++count);
1176 memset(&array[count - 1], 0, sizeof(*array));
1177 array[count - 1].name = name;
1178 array[count - 1].description = desc;
Eric Blakebfe04d02023-06-08 08:56:37 -05001179 array[count - 1].structured_reply = result == NBD_MODE_STRUCTURED;
Eric Blaked21a2d32019-01-17 13:36:54 -06001180 }
1181
1182 for (i = 0; i < count; i++) {
1183 array[i].request_sizes = true;
1184 rc = nbd_opt_info_or_go(ioc, NBD_OPT_INFO, &array[i], errp);
1185 if (rc < 0) {
1186 goto out;
1187 } else if (rc == 0) {
1188 /*
1189 * Pointless to try rest of loop. If OPT_INFO doesn't work,
1190 * it's unlikely that meta contexts work either
1191 */
1192 break;
1193 }
1194
Eric Blakebfe04d02023-06-08 08:56:37 -05001195 if (result == NBD_MODE_STRUCTURED &&
Eric Blake0b576b62019-01-17 13:36:55 -06001196 nbd_list_meta_contexts(ioc, &array[i], errp) < 0) {
1197 goto out;
1198 }
Eric Blaked21a2d32019-01-17 13:36:54 -06001199 }
1200
1201 /* Send NBD_OPT_ABORT as a courtesy before hanging up */
1202 nbd_send_opt_abort(ioc);
1203 break;
Eric Blakebfe04d02023-06-08 08:56:37 -05001204 case NBD_MODE_EXPORT_NAME:
Eric Blaked21a2d32019-01-17 13:36:54 -06001205 error_setg(errp, "Server does not support export lists");
1206 /* We can't even send NBD_OPT_ABORT, so merely hang up */
1207 goto out;
Eric Blakebfe04d02023-06-08 08:56:37 -05001208 case NBD_MODE_OLDSTYLE:
1209 /* Lone export name is implied, but we can parse length and flags */
Eric Blaked21a2d32019-01-17 13:36:54 -06001210 array = g_new0(NBDExportInfo, 1);
1211 array->name = g_strdup("");
1212 count = 1;
1213
1214 if (nbd_negotiate_finish_oldstyle(ioc, array, errp) < 0) {
1215 goto out;
1216 }
1217
1218 /* Send NBD_CMD_DISC as a courtesy to the server, but ignore all
1219 * errors now that we have the information we wanted. */
1220 if (nbd_drop(ioc, 124, NULL) == 0) {
1221 NBDRequest request = { .type = NBD_CMD_DISC };
1222
1223 nbd_send_request(ioc, &request);
1224 }
1225 break;
1226 default:
Eric Blakebfe04d02023-06-08 08:56:37 -05001227 g_assert_not_reached();
Eric Blaked21a2d32019-01-17 13:36:54 -06001228 }
1229
1230 *info = array;
1231 array = NULL;
1232 ret = count;
1233
1234 out:
1235 qio_channel_shutdown(ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
1236 qio_channel_close(ioc, NULL);
1237 object_unref(OBJECT(sioc));
1238 nbd_free_export_list(array, count);
1239 return ret;
1240}
1241
Fam Zheng798bfe02016-01-14 16:41:02 +08001242#ifdef __linux__
Eric Blake004a89f2017-07-07 15:30:41 -05001243int nbd_init(int fd, QIOChannelSocket *sioc, NBDExportInfo *info,
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +03001244 Error **errp)
Fam Zheng798bfe02016-01-14 16:41:02 +08001245{
Eric Blake081dd1f2017-07-07 15:30:49 -05001246 unsigned long sector_size = MAX(BDRV_SECTOR_SIZE, info->min_block);
1247 unsigned long sectors = info->size / sector_size;
1248
1249 /* FIXME: Once the kernel module is patched to honor block sizes,
1250 * and to advertise that fact to user space, we should update the
1251 * hand-off to the kernel to use any block sizes we learned. */
1252 assert(!info->request_sizes);
1253 if (info->size / sector_size != sectors) {
Eric Blake004a89f2017-07-07 15:30:41 -05001254 error_setg(errp, "Export size %" PRIu64 " too large for 32-bit kernel",
1255 info->size);
Eric Blakef57e2412016-05-11 16:39:40 -06001256 return -E2BIG;
1257 }
1258
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001259 trace_nbd_init_set_socket();
Fam Zheng798bfe02016-01-14 16:41:02 +08001260
Eric Blakef57e2412016-05-11 16:39:40 -06001261 if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
Fam Zheng798bfe02016-01-14 16:41:02 +08001262 int serrno = errno;
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +03001263 error_setg(errp, "Failed to set NBD socket");
Fam Zheng798bfe02016-01-14 16:41:02 +08001264 return -serrno;
1265 }
1266
Eric Blake081dd1f2017-07-07 15:30:49 -05001267 trace_nbd_init_set_block_size(sector_size);
Fam Zheng798bfe02016-01-14 16:41:02 +08001268
Eric Blake081dd1f2017-07-07 15:30:49 -05001269 if (ioctl(fd, NBD_SET_BLKSIZE, sector_size) < 0) {
Fam Zheng798bfe02016-01-14 16:41:02 +08001270 int serrno = errno;
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +03001271 error_setg(errp, "Failed setting NBD block size");
Fam Zheng798bfe02016-01-14 16:41:02 +08001272 return -serrno;
1273 }
1274
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001275 trace_nbd_init_set_size(sectors);
Eric Blake081dd1f2017-07-07 15:30:49 -05001276 if (info->size % sector_size) {
1277 trace_nbd_init_trailing_bytes(info->size % sector_size);
Eric Blakef57e2412016-05-11 16:39:40 -06001278 }
Fam Zheng798bfe02016-01-14 16:41:02 +08001279
Eric Blakef57e2412016-05-11 16:39:40 -06001280 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
Fam Zheng798bfe02016-01-14 16:41:02 +08001281 int serrno = errno;
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +03001282 error_setg(errp, "Failed setting size (in blocks)");
Fam Zheng798bfe02016-01-14 16:41:02 +08001283 return -serrno;
1284 }
1285
Eric Blake004a89f2017-07-07 15:30:41 -05001286 if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) info->flags) < 0) {
Fam Zheng798bfe02016-01-14 16:41:02 +08001287 if (errno == ENOTTY) {
Eric Blake004a89f2017-07-07 15:30:41 -05001288 int read_only = (info->flags & NBD_FLAG_READ_ONLY) != 0;
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001289 trace_nbd_init_set_readonly();
Fam Zheng798bfe02016-01-14 16:41:02 +08001290
1291 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
1292 int serrno = errno;
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +03001293 error_setg(errp, "Failed setting read-only attribute");
Fam Zheng798bfe02016-01-14 16:41:02 +08001294 return -serrno;
1295 }
1296 } else {
1297 int serrno = errno;
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +03001298 error_setg(errp, "Failed setting flags");
Fam Zheng798bfe02016-01-14 16:41:02 +08001299 return -serrno;
1300 }
1301 }
1302
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001303 trace_nbd_init_finish();
Fam Zheng798bfe02016-01-14 16:41:02 +08001304
1305 return 0;
1306}
1307
1308int nbd_client(int fd)
1309{
1310 int ret;
1311 int serrno;
1312
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001313 trace_nbd_client_loop();
Fam Zheng798bfe02016-01-14 16:41:02 +08001314
1315 ret = ioctl(fd, NBD_DO_IT);
1316 if (ret < 0 && errno == EPIPE) {
1317 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
1318 * the socket via NBD_DISCONNECT. We do not want to return 1 in
1319 * that case.
1320 */
1321 ret = 0;
1322 }
1323 serrno = errno;
1324
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001325 trace_nbd_client_loop_ret(ret, strerror(serrno));
Fam Zheng798bfe02016-01-14 16:41:02 +08001326
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001327 trace_nbd_client_clear_queue();
Fam Zheng798bfe02016-01-14 16:41:02 +08001328 ioctl(fd, NBD_CLEAR_QUE);
1329
Vladimir Sementsov-Ogievskiy95884632017-07-07 18:29:18 +03001330 trace_nbd_client_clear_socket();
Fam Zheng798bfe02016-01-14 16:41:02 +08001331 ioctl(fd, NBD_CLEAR_SOCK);
1332
1333 errno = serrno;
1334 return ret;
1335}
Eric Blake98494e32016-05-11 16:39:39 -06001336
1337int nbd_disconnect(int fd)
1338{
1339 ioctl(fd, NBD_CLEAR_QUE);
1340 ioctl(fd, NBD_DISCONNECT);
1341 ioctl(fd, NBD_CLEAR_SOCK);
1342 return 0;
1343}
1344
Eric Blake3c1fa352018-12-15 07:53:08 -06001345#endif /* __linux__ */
Fam Zheng798bfe02016-01-14 16:41:02 +08001346
Vladimir Sementsov-Ogievskiy490dc5e2017-08-04 18:14:27 +03001347int nbd_send_request(QIOChannel *ioc, NBDRequest *request)
Fam Zheng798bfe02016-01-14 16:41:02 +08001348{
1349 uint8_t buf[NBD_REQUEST_SIZE];
Fam Zheng798bfe02016-01-14 16:41:02 +08001350
Eric Blake22efd812023-06-08 08:56:34 -05001351 trace_nbd_send_request(request->from, request->len, request->cookie,
Eric Blake48000eb2017-07-17 14:26:34 -05001352 request->flags, request->type,
1353 nbd_cmd_lookup(request->type));
Eric Blake7548fe32016-04-05 21:35:04 -06001354
Peter Maydellf6be6722016-06-10 17:15:42 +01001355 stl_be_p(buf, NBD_REQUEST_MAGIC);
Eric Blakeb626b512016-10-14 13:33:04 -05001356 stw_be_p(buf + 4, request->flags);
1357 stw_be_p(buf + 6, request->type);
Eric Blake22efd812023-06-08 08:56:34 -05001358 stq_be_p(buf + 8, request->cookie);
Peter Maydellf6be6722016-06-10 17:15:42 +01001359 stq_be_p(buf + 16, request->from);
1360 stl_be_p(buf + 24, request->len);
Fam Zheng798bfe02016-01-14 16:41:02 +08001361
Vladimir Sementsov-Ogievskiyd1fdf252017-06-02 18:01:39 +03001362 return nbd_write(ioc, buf, sizeof(buf), NULL);
Fam Zheng798bfe02016-01-14 16:41:02 +08001363}
1364
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001365/* nbd_receive_simple_reply
1366 * Read simple reply except magic field (which should be already read).
1367 * Payload is not read (payload is possible for CMD_READ, but here we even
1368 * don't know whether it take place or not).
1369 */
1370static int nbd_receive_simple_reply(QIOChannel *ioc, NBDSimpleReply *reply,
1371 Error **errp)
1372{
1373 int ret;
1374
1375 assert(reply->magic == NBD_SIMPLE_REPLY_MAGIC);
1376
1377 ret = nbd_read(ioc, (uint8_t *)reply + sizeof(reply->magic),
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001378 sizeof(*reply) - sizeof(reply->magic), "reply", errp);
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001379 if (ret < 0) {
1380 return ret;
1381 }
1382
Peter Maydell80c7c2b2018-09-27 17:42:00 +01001383 reply->error = be32_to_cpu(reply->error);
Eric Blake22efd812023-06-08 08:56:34 -05001384 reply->cookie = be64_to_cpu(reply->cookie);
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001385
1386 return 0;
1387}
1388
1389/* nbd_receive_structured_reply_chunk
1390 * Read structured reply chunk except magic field (which should be already
1391 * read).
1392 * Payload is not read.
1393 */
1394static int nbd_receive_structured_reply_chunk(QIOChannel *ioc,
1395 NBDStructuredReplyChunk *chunk,
1396 Error **errp)
1397{
1398 int ret;
1399
1400 assert(chunk->magic == NBD_STRUCTURED_REPLY_MAGIC);
1401
1402 ret = nbd_read(ioc, (uint8_t *)chunk + sizeof(chunk->magic),
Vladimir Sementsov-Ogievskiye6798f02019-01-28 19:58:30 +03001403 sizeof(*chunk) - sizeof(chunk->magic), "structured chunk",
1404 errp);
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001405 if (ret < 0) {
1406 return ret;
1407 }
1408
Peter Maydell80c7c2b2018-09-27 17:42:00 +01001409 chunk->flags = be16_to_cpu(chunk->flags);
1410 chunk->type = be16_to_cpu(chunk->type);
Eric Blake22efd812023-06-08 08:56:34 -05001411 chunk->cookie = be64_to_cpu(chunk->cookie);
Peter Maydell80c7c2b2018-09-27 17:42:00 +01001412 chunk->length = be32_to_cpu(chunk->length);
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001413
Eric Blake70fa99f2023-06-08 08:56:36 -05001414 /*
1415 * Because we use BLOCK_STATUS with REQ_ONE, and cap READ requests
1416 * at 32M, no valid server should send us payload larger than
1417 * this. Even if we stopped using REQ_ONE, sane servers will cap
1418 * the number of extents they return for block status.
1419 */
1420 if (chunk->length > NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData)) {
1421 error_setg(errp, "server chunk %" PRIu32 " (%s) payload is too long",
1422 chunk->type, nbd_rep_lookup(chunk->type));
1423 return -EINVAL;
1424 }
1425
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001426 return 0;
1427}
1428
Kevin Wolfa7b78fc2019-02-18 14:38:15 +01001429/* nbd_read_eof
1430 * Tries to read @size bytes from @ioc.
1431 * Returns 1 on success
1432 * 0 on eof, when no data was read (errp is not set)
1433 * negative errno on failure (errp is set)
1434 */
1435static inline int coroutine_fn
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001436nbd_read_eof(BlockDriverState *bs, QIOChannel *ioc, void *buffer, size_t size,
1437 Error **errp)
Kevin Wolfa7b78fc2019-02-18 14:38:15 +01001438{
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001439 bool partial = false;
Kevin Wolfa7b78fc2019-02-18 14:38:15 +01001440
1441 assert(size);
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001442 while (size > 0) {
1443 struct iovec iov = { .iov_base = buffer, .iov_len = size };
1444 ssize_t len;
1445
1446 len = qio_channel_readv(ioc, &iov, 1, errp);
1447 if (len == QIO_CHANNEL_ERR_BLOCK) {
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001448 qio_channel_yield(ioc, G_IO_IN);
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001449 continue;
1450 } else if (len < 0) {
1451 return -EIO;
1452 } else if (len == 0) {
1453 if (partial) {
1454 error_setg(errp,
1455 "Unexpected end-of-file before all bytes were read");
1456 return -EIO;
1457 } else {
1458 return 0;
1459 }
1460 }
1461
1462 partial = true;
1463 size -= len;
1464 buffer = (uint8_t*) buffer + len;
Kevin Wolfa7b78fc2019-02-18 14:38:15 +01001465 }
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001466 return 1;
Kevin Wolfa7b78fc2019-02-18 14:38:15 +01001467}
1468
Vladimir Sementsov-Ogievskiyba845642017-08-04 18:14:26 +03001469/* nbd_receive_reply
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001470 *
1471 * Decreases bs->in_flight while waiting for a new reply. This yield is where
1472 * we wait indefinitely and the coroutine must be able to be safely reentered
1473 * for nbd_client_attach_aio_context().
1474 *
Vladimir Sementsov-Ogievskiyba845642017-08-04 18:14:26 +03001475 * Returns 1 on success
1476 * 0 on eof, when no data was read (errp is not set)
1477 * negative errno on failure (errp is set)
1478 */
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001479int coroutine_fn nbd_receive_reply(BlockDriverState *bs, QIOChannel *ioc,
1480 NBDReply *reply, Error **errp)
Fam Zheng798bfe02016-01-14 16:41:02 +08001481{
Vladimir Sementsov-Ogievskiyba845642017-08-04 18:14:26 +03001482 int ret;
Eric Blake079d3262017-11-08 15:56:59 -06001483 const char *type;
Fam Zheng798bfe02016-01-14 16:41:02 +08001484
Kevin Wolfd3bd5b92019-02-18 14:56:01 +01001485 ret = nbd_read_eof(bs, ioc, &reply->magic, sizeof(reply->magic), errp);
Paolo Bonziniff829112017-02-13 14:52:24 +01001486 if (ret <= 0) {
Fam Zheng798bfe02016-01-14 16:41:02 +08001487 return ret;
1488 }
1489
Peter Maydell80c7c2b2018-09-27 17:42:00 +01001490 reply->magic = be32_to_cpu(reply->magic);
Fam Zheng798bfe02016-01-14 16:41:02 +08001491
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001492 switch (reply->magic) {
1493 case NBD_SIMPLE_REPLY_MAGIC:
1494 ret = nbd_receive_simple_reply(ioc, &reply->simple, errp);
1495 if (ret < 0) {
1496 break;
1497 }
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001498 trace_nbd_receive_simple_reply(reply->simple.error,
1499 nbd_err_lookup(reply->simple.error),
Eric Blake22efd812023-06-08 08:56:34 -05001500 reply->cookie);
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001501 break;
1502 case NBD_STRUCTURED_REPLY_MAGIC:
1503 ret = nbd_receive_structured_reply_chunk(ioc, &reply->structured, errp);
1504 if (ret < 0) {
1505 break;
1506 }
Eric Blake079d3262017-11-08 15:56:59 -06001507 type = nbd_reply_type_lookup(reply->structured.type);
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001508 trace_nbd_receive_structured_reply_chunk(reply->structured.flags,
Eric Blake079d3262017-11-08 15:56:59 -06001509 reply->structured.type, type,
Eric Blake22efd812023-06-08 08:56:34 -05001510 reply->structured.cookie,
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001511 reply->structured.length);
1512 break;
1513 default:
1514 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", reply->magic);
Eric Blakeb6f5d3b2016-10-14 13:33:16 -05001515 return -EINVAL;
1516 }
Vladimir Sementsov-Ogievskiyd2febed2017-10-27 12:40:35 +02001517 if (ret < 0) {
1518 return ret;
Fam Zheng798bfe02016-01-14 16:41:02 +08001519 }
Vladimir Sementsov-Ogievskiyba845642017-08-04 18:14:26 +03001520
1521 return 1;
Fam Zheng798bfe02016-01-14 16:41:02 +08001522}
1523