blob: b11e266807df828fcf3aaf665bdb62ef80a3d945 [file] [log] [blame]
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -05001/*
Eric Blakeac132d02023-08-29 12:58:28 -05002 * QEMU Block driver for NBD
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -05003 *
4 * Copyright (c) 2021 Virtuozzo International GmbH.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu/osdep.h"
Denis V. Lunev8bb100c2022-05-30 12:39:29 +020026#include "trace.h"
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050027
28#include "block/nbd.h"
29
30#include "qapi/qapi-visit-sockets.h"
31#include "qapi/clone-visitor.h"
Markus Armbruster68ba85c2022-12-21 14:14:34 +010032#include "qemu/coroutine.h"
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050033
34struct NBDClientConnection {
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030035 /* Initialization constants, never change */
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050036 SocketAddress *saddr; /* address to connect to */
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030037 QCryptoTLSCreds *tlscreds;
Daniel P. Berrangé046f98d2022-03-04 19:36:00 +000038 char *tlshostname;
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030039 NBDExportInfo initial_info;
40 bool do_negotiation;
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +030041 bool do_retry;
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050042
43 QemuMutex mutex;
44
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030045 NBDExportInfo updated_info;
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +030046 /*
47 * @sioc represents a successful result. While thread is running, @sioc is
48 * used only by thread and not protected by mutex. When thread is not
49 * running, @sioc is stolen by nbd_co_establish_connection() under mutex.
50 */
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050051 QIOChannelSocket *sioc;
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030052 QIOChannel *ioc;
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +030053 /*
54 * @err represents previous attempt. It may be copied by
55 * nbd_co_establish_connection() when it reports failure.
56 */
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050057 Error *err;
58
59 /* All further fields are accessed only under mutex */
60 bool running; /* thread is running now */
61 bool detached; /* thread is detached and should cleanup the state */
62
63 /*
64 * wait_co: if non-NULL, which coroutine to wake in
65 * nbd_co_establish_connection() after yield()
66 */
67 Coroutine *wait_co;
68};
69
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +030070/*
71 * The function isn't protected by any mutex, only call it when the client
72 * connection attempt has not yet started.
73 */
74void nbd_client_connection_enable_retry(NBDClientConnection *conn)
75{
76 conn->do_retry = true;
77}
78
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030079NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
80 bool do_negotiation,
81 const char *export_name,
82 const char *x_dirty_bitmap,
Daniel P. Berrangé046f98d2022-03-04 19:36:00 +000083 QCryptoTLSCreds *tlscreds,
84 const char *tlshostname)
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050085{
86 NBDClientConnection *conn = g_new(NBDClientConnection, 1);
87
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030088 object_ref(OBJECT(tlscreds));
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -050089 *conn = (NBDClientConnection) {
90 .saddr = QAPI_CLONE(SocketAddress, saddr),
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030091 .tlscreds = tlscreds,
Daniel P. Berrangé046f98d2022-03-04 19:36:00 +000092 .tlshostname = g_strdup(tlshostname),
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030093 .do_negotiation = do_negotiation,
94
95 .initial_info.request_sizes = true,
Eric Blake56cf9d02023-09-25 14:22:39 -050096 .initial_info.mode = NBD_MODE_EXTENDED,
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +030097 .initial_info.base_allocation = true,
98 .initial_info.x_dirty_bitmap = g_strdup(x_dirty_bitmap),
99 .initial_info.name = g_strdup(export_name ?: "")
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500100 };
101
102 qemu_mutex_init(&conn->mutex);
103
104 return conn;
105}
106
107static void nbd_client_connection_do_free(NBDClientConnection *conn)
108{
109 if (conn->sioc) {
110 qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
111 object_unref(OBJECT(conn->sioc));
112 }
113 error_free(conn->err);
114 qapi_free_SocketAddress(conn->saddr);
Daniel P. Berrangé046f98d2022-03-04 19:36:00 +0000115 g_free(conn->tlshostname);
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300116 object_unref(OBJECT(conn->tlscreds));
117 g_free(conn->initial_info.x_dirty_bitmap);
118 g_free(conn->initial_info.name);
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500119 g_free(conn);
120}
121
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300122/*
123 * Connect to @addr and do NBD negotiation if @info is not null. If @tlscreds
124 * are given @outioc is returned. @outioc is provided only on success. The call
125 * may be cancelled from other thread by simply qio_channel_shutdown(sioc).
126 */
127static int nbd_connect(QIOChannelSocket *sioc, SocketAddress *addr,
128 NBDExportInfo *info, QCryptoTLSCreds *tlscreds,
Daniel P. Berrangé046f98d2022-03-04 19:36:00 +0000129 const char *tlshostname,
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300130 QIOChannel **outioc, Error **errp)
131{
132 int ret;
133
134 if (outioc) {
135 *outioc = NULL;
136 }
137
138 ret = qio_channel_socket_connect_sync(sioc, addr, errp);
139 if (ret < 0) {
140 return ret;
141 }
142
143 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
144
145 if (!info) {
146 return 0;
147 }
148
Stefan Hajnoczib84ca912023-08-30 18:47:59 -0400149 ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), tlscreds, tlshostname,
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300150 outioc, info, errp);
151 if (ret < 0) {
152 /*
153 * nbd_receive_negotiate() may setup tls ioc and return it even on
154 * failure path. In this case we should use it instead of original
155 * channel.
156 */
157 if (outioc && *outioc) {
Philippe Mathieu-Daudé7d5b0d62023-06-01 11:34:52 +0200158 qio_channel_close(*outioc, NULL);
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300159 object_unref(OBJECT(*outioc));
160 *outioc = NULL;
161 } else {
162 qio_channel_close(QIO_CHANNEL(sioc), NULL);
163 }
164
165 return ret;
166 }
167
168 return 0;
169}
170
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500171static void *connect_thread_func(void *opaque)
172{
173 NBDClientConnection *conn = opaque;
174 int ret;
175 bool do_free;
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300176 uint64_t timeout = 1;
177 uint64_t max_timeout = 16;
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500178
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300179 qemu_mutex_lock(&conn->mutex);
180 while (!conn->detached) {
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300181 Error *local_err = NULL;
182
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300183 assert(!conn->sioc);
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300184 conn->sioc = qio_channel_socket_new();
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500185
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300186 qemu_mutex_unlock(&conn->mutex);
187
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300188 conn->updated_info = conn->initial_info;
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300189
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300190 ret = nbd_connect(conn->sioc, conn->saddr,
191 conn->do_negotiation ? &conn->updated_info : NULL,
Daniel P. Berrangé046f98d2022-03-04 19:36:00 +0000192 conn->tlscreds, conn->tlshostname,
193 &conn->ioc, &local_err);
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300194
195 /*
196 * conn->updated_info will finally be returned to the user. Clear the
197 * pointers to our internally allocated strings, which are IN parameters
198 * of nbd_receive_negotiate() and therefore nbd_connect(). Caller
Michael Tokarev0a19d872023-07-14 14:33:49 +0300199 * shouldn't be interested in these fields.
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300200 */
201 conn->updated_info.x_dirty_bitmap = NULL;
202 conn->updated_info.name = NULL;
203
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300204 qemu_mutex_lock(&conn->mutex);
205
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300206 error_free(conn->err);
207 conn->err = NULL;
208 error_propagate(&conn->err, local_err);
209
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300210 if (ret < 0) {
211 object_unref(OBJECT(conn->sioc));
212 conn->sioc = NULL;
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300213 if (conn->do_retry && !conn->detached) {
Denis V. Lunev8bb100c2022-05-30 12:39:29 +0200214 trace_nbd_connect_thread_sleep(timeout);
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300215 qemu_mutex_unlock(&conn->mutex);
216
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300217 sleep(timeout);
218 if (timeout < max_timeout) {
219 timeout *= 2;
220 }
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300221
222 qemu_mutex_lock(&conn->mutex);
Vladimir Sementsov-Ogievskiye0e67cb2021-06-10 13:07:50 +0300223 continue;
224 }
225 }
226
227 break;
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500228 }
229
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300230 /* mutex is locked */
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500231
232 assert(conn->running);
233 conn->running = false;
234 if (conn->wait_co) {
235 aio_co_wake(conn->wait_co);
236 conn->wait_co = NULL;
237 }
238 do_free = conn->detached;
239
240 qemu_mutex_unlock(&conn->mutex);
241
242 if (do_free) {
243 nbd_client_connection_do_free(conn);
244 }
245
246 return NULL;
247}
248
249void nbd_client_connection_release(NBDClientConnection *conn)
250{
251 bool do_free = false;
252
253 if (!conn) {
254 return;
255 }
256
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300257 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
258 assert(!conn->detached);
259 if (conn->running) {
260 conn->detached = true;
261 } else {
262 do_free = true;
263 }
Vladimir Sementsov-Ogievskiyf58b2df2021-06-10 13:07:51 +0300264 if (conn->sioc) {
265 qio_channel_shutdown(QIO_CHANNEL(conn->sioc),
266 QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
267 }
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500268 }
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500269
270 if (do_free) {
271 nbd_client_connection_do_free(conn);
272 }
273}
274
275/*
276 * Get a new connection in context of @conn:
277 * if the thread is running, wait for completion
278 * if the thread already succeeded in the background, and user didn't get the
279 * result, just return it now
280 * otherwise the thread is not running, so start a thread and wait for
281 * completion
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300282 *
Vladimir Sementsov-Ogievskiy97cf8922021-06-10 13:07:59 +0300283 * If @blocking is false, don't wait for the thread, return immediately.
284 *
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300285 * If @info is not NULL, also do nbd-negotiation after successful connection.
286 * In this case info is used only as out parameter, and is fully initialized by
287 * nbd_co_establish_connection(). "IN" fields of info as well as related only to
288 * nbd_receive_export_list() would be zero (see description of NBDExportInfo in
289 * include/block/nbd.h).
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500290 */
Vladimir Sementsov-Ogievskiy43cb34d2021-06-10 13:07:56 +0300291QIOChannel *coroutine_fn
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300292nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
Vladimir Sementsov-Ogievskiy97cf8922021-06-10 13:07:59 +0300293 bool blocking, Error **errp)
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500294{
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500295 QemuThread thread;
296
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300297 if (conn->do_negotiation) {
298 assert(info);
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300299 }
300
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300301 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
302 /*
303 * Don't call nbd_co_establish_connection() in several coroutines in
304 * parallel. Only one call at once is supported.
305 */
306 assert(!conn->wait_co);
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500307
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300308 if (!conn->running) {
309 if (conn->sioc) {
310 /* Previous attempt finally succeeded in background */
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300311 if (conn->do_negotiation) {
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300312 memcpy(info, &conn->updated_info, sizeof(*info));
Vladimir Sementsov-Ogievskiy43cb34d2021-06-10 13:07:56 +0300313 if (conn->ioc) {
314 /* TLS channel now has own reference to parent */
315 object_unref(OBJECT(conn->sioc));
316 conn->sioc = NULL;
317
318 return g_steal_pointer(&conn->ioc);
319 }
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300320 }
Vladimir Sementsov-Ogievskiy43cb34d2021-06-10 13:07:56 +0300321
322 assert(!conn->ioc);
323
324 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300325 }
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500326
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300327 conn->running = true;
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300328 qemu_thread_create(&thread, "nbd-connect",
329 connect_thread_func, conn, QEMU_THREAD_DETACHED);
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500330 }
331
Vladimir Sementsov-Ogievskiy97cf8922021-06-10 13:07:59 +0300332 if (!blocking) {
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300333 if (conn->err) {
334 error_propagate(errp, error_copy(conn->err));
335 } else {
336 error_setg(errp, "No connection at the moment");
337 }
338
Vladimir Sementsov-Ogievskiy97cf8922021-06-10 13:07:59 +0300339 return NULL;
340 }
341
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300342 conn->wait_co = qemu_coroutine_self();
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500343 }
344
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500345 /*
346 * We are going to wait for connect-thread finish, but
347 * nbd_co_establish_connection_cancel() can interrupt.
348 */
349 qemu_coroutine_yield();
350
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300351 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
352 if (conn->running) {
353 /*
354 * The connection attempt was canceled and the coroutine resumed
355 * before the connection thread finished its job. Report the
356 * attempt as failed, but leave the connection thread running,
357 * to reuse it for the next connection attempt.
358 */
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300359 if (conn->err) {
360 error_propagate(errp, error_copy(conn->err));
361 } else {
Vladimir Sementsov-Ogievskiy9e144912021-09-06 22:06:50 +0300362 /*
363 * The only possible case here is cancelling by open_timer
364 * during nbd_open(). So, the error message is for that case.
365 * If we have more use cases, we can refactor
366 * nbd_co_establish_connection_cancel() to take an additional
367 * parameter cancel_reason, that would be passed than to the
368 * caller of cancelled nbd_co_establish_connection().
369 */
370 error_setg(errp, "Connection attempt cancelled by timeout");
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300371 }
372
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300373 return NULL;
374 } else {
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300375 /* Thread finished. There must be either error or sioc */
376 assert(!conn->err != !conn->sioc);
377
378 if (conn->err) {
379 error_propagate(errp, error_copy(conn->err));
Vladimir Sementsov-Ogievskiy43cb34d2021-06-10 13:07:56 +0300380 return NULL;
Vladimir Sementsov-Ogievskiy130d49b2021-06-10 13:07:49 +0300381 }
Vladimir Sementsov-Ogievskiy169b9a92021-09-06 22:06:49 +0300382
Vladimir Sementsov-Ogievskiy43cb34d2021-06-10 13:07:56 +0300383 if (conn->do_negotiation) {
384 memcpy(info, &conn->updated_info, sizeof(*info));
385 if (conn->ioc) {
386 /* TLS channel now has own reference to parent */
387 object_unref(OBJECT(conn->sioc));
388 conn->sioc = NULL;
389
390 return g_steal_pointer(&conn->ioc);
391 }
392 }
393
394 assert(!conn->ioc);
395
396 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300397 }
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500398 }
399
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300400 abort(); /* unreachable */
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500401}
402
403/*
404 * nbd_co_establish_connection_cancel
405 * Cancel nbd_co_establish_connection() asynchronously.
406 *
407 * Note that this function neither directly stops the thread nor closes the
408 * socket, but rather safely wakes nbd_co_establish_connection() which is
409 * sleeping in yield()
410 */
411void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
412{
Marc-André Lureau73ce9bb2024-03-28 14:20:37 +0400413 Coroutine *wait_co = NULL;
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500414
Vladimir Sementsov-Ogievskiye70da5f2021-06-10 13:07:48 +0300415 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
416 wait_co = g_steal_pointer(&conn->wait_co);
417 }
Vladimir Sementsov-Ogievskiy5276c872021-06-15 14:07:05 -0500418
419 if (wait_co) {
420 aio_co_wake(wait_co);
421 }
422}