]>
git.proxmox.com Git - mirror_qemu.git/blob - nbd/client-connection.c
2 * QEMU Block driver for NBD
4 * Copyright (c) 2021 Virtuozzo International GmbH.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
27 #include "block/nbd.h"
29 #include "qapi/qapi-visit-sockets.h"
30 #include "qapi/clone-visitor.h"
32 struct NBDClientConnection
{
33 /* Initialization constants */
34 SocketAddress
*saddr
; /* address to connect to */
39 * @sioc and @err represent a connection attempt. While running
40 * is true, they are only used by the connection thread, and mutex
41 * locking is not needed. Once the thread finishes,
42 * nbd_co_establish_connection then steals these pointers while
45 QIOChannelSocket
*sioc
;
48 /* All further fields are accessed only under mutex */
49 bool running
; /* thread is running now */
50 bool detached
; /* thread is detached and should cleanup the state */
53 * wait_co: if non-NULL, which coroutine to wake in
54 * nbd_co_establish_connection() after yield()
59 NBDClientConnection
*nbd_client_connection_new(const SocketAddress
*saddr
)
61 NBDClientConnection
*conn
= g_new(NBDClientConnection
, 1);
63 *conn
= (NBDClientConnection
) {
64 .saddr
= QAPI_CLONE(SocketAddress
, saddr
),
67 qemu_mutex_init(&conn
->mutex
);
72 static void nbd_client_connection_do_free(NBDClientConnection
*conn
)
75 qio_channel_close(QIO_CHANNEL(conn
->sioc
), NULL
);
76 object_unref(OBJECT(conn
->sioc
));
78 error_free(conn
->err
);
79 qapi_free_SocketAddress(conn
->saddr
);
83 static void *connect_thread_func(void *opaque
)
85 NBDClientConnection
*conn
= opaque
;
89 conn
->sioc
= qio_channel_socket_new();
91 error_free(conn
->err
);
93 ret
= qio_channel_socket_connect_sync(conn
->sioc
, conn
->saddr
, &conn
->err
);
95 object_unref(OBJECT(conn
->sioc
));
99 qio_channel_set_delay(QIO_CHANNEL(conn
->sioc
), false);
101 qemu_mutex_lock(&conn
->mutex
);
103 assert(conn
->running
);
104 conn
->running
= false;
106 aio_co_wake(conn
->wait_co
);
107 conn
->wait_co
= NULL
;
109 do_free
= conn
->detached
;
111 qemu_mutex_unlock(&conn
->mutex
);
114 nbd_client_connection_do_free(conn
);
120 void nbd_client_connection_release(NBDClientConnection
*conn
)
122 bool do_free
= false;
128 qemu_mutex_lock(&conn
->mutex
);
129 assert(!conn
->detached
);
131 conn
->detached
= true;
135 qemu_mutex_unlock(&conn
->mutex
);
138 nbd_client_connection_do_free(conn
);
143 * Get a new connection in context of @conn:
144 * if the thread is running, wait for completion
145 * if the thread already succeeded in the background, and user didn't get the
146 * result, just return it now
147 * otherwise the thread is not running, so start a thread and wait for
150 QIOChannelSocket
*coroutine_fn
151 nbd_co_establish_connection(NBDClientConnection
*conn
, Error
**errp
)
153 QIOChannelSocket
*sioc
= NULL
;
156 qemu_mutex_lock(&conn
->mutex
);
159 * Don't call nbd_co_establish_connection() in several coroutines in
160 * parallel. Only one call at once is supported.
162 assert(!conn
->wait_co
);
164 if (!conn
->running
) {
166 /* Previous attempt finally succeeded in background */
167 sioc
= g_steal_pointer(&conn
->sioc
);
168 qemu_mutex_unlock(&conn
->mutex
);
173 conn
->running
= true;
174 error_free(conn
->err
);
176 qemu_thread_create(&thread
, "nbd-connect",
177 connect_thread_func
, conn
, QEMU_THREAD_DETACHED
);
180 conn
->wait_co
= qemu_coroutine_self();
182 qemu_mutex_unlock(&conn
->mutex
);
185 * We are going to wait for connect-thread finish, but
186 * nbd_co_establish_connection_cancel() can interrupt.
188 qemu_coroutine_yield();
190 qemu_mutex_lock(&conn
->mutex
);
194 * The connection attempt was canceled and the coroutine resumed
195 * before the connection thread finished its job. Report the
196 * attempt as failed, but leave the connection thread running,
197 * to reuse it for the next connection attempt.
199 error_setg(errp
, "Connection attempt cancelled by other operation");
201 error_propagate(errp
, conn
->err
);
203 sioc
= g_steal_pointer(&conn
->sioc
);
206 qemu_mutex_unlock(&conn
->mutex
);
212 * nbd_co_establish_connection_cancel
213 * Cancel nbd_co_establish_connection() asynchronously.
215 * Note that this function neither directly stops the thread nor closes the
216 * socket, but rather safely wakes nbd_co_establish_connection() which is
217 * sleeping in yield()
219 void nbd_co_establish_connection_cancel(NBDClientConnection
*conn
)
223 qemu_mutex_lock(&conn
->mutex
);
225 wait_co
= g_steal_pointer(&conn
->wait_co
);
227 qemu_mutex_unlock(&conn
->mutex
);
230 aio_co_wake(wait_co
);