]> git.proxmox.com Git - mirror_qemu.git/blob - nbd/client-connection.c
nbd: move connection code from block/nbd to nbd/client-connection
[mirror_qemu.git] / nbd / client-connection.c
1 /*
2 * QEMU Block driver for NBD
3 *
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"
26
27 #include "block/nbd.h"
28
29 #include "qapi/qapi-visit-sockets.h"
30 #include "qapi/clone-visitor.h"
31
32 struct NBDClientConnection {
33 /* Initialization constants */
34 SocketAddress *saddr; /* address to connect to */
35
36 QemuMutex mutex;
37
38 /*
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
43 * under the mutex.
44 */
45 QIOChannelSocket *sioc;
46 Error *err;
47
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 */
51
52 /*
53 * wait_co: if non-NULL, which coroutine to wake in
54 * nbd_co_establish_connection() after yield()
55 */
56 Coroutine *wait_co;
57 };
58
59 NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr)
60 {
61 NBDClientConnection *conn = g_new(NBDClientConnection, 1);
62
63 *conn = (NBDClientConnection) {
64 .saddr = QAPI_CLONE(SocketAddress, saddr),
65 };
66
67 qemu_mutex_init(&conn->mutex);
68
69 return conn;
70 }
71
72 static void nbd_client_connection_do_free(NBDClientConnection *conn)
73 {
74 if (conn->sioc) {
75 qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
76 object_unref(OBJECT(conn->sioc));
77 }
78 error_free(conn->err);
79 qapi_free_SocketAddress(conn->saddr);
80 g_free(conn);
81 }
82
83 static void *connect_thread_func(void *opaque)
84 {
85 NBDClientConnection *conn = opaque;
86 int ret;
87 bool do_free;
88
89 conn->sioc = qio_channel_socket_new();
90
91 error_free(conn->err);
92 conn->err = NULL;
93 ret = qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn->err);
94 if (ret < 0) {
95 object_unref(OBJECT(conn->sioc));
96 conn->sioc = NULL;
97 }
98
99 qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false);
100
101 qemu_mutex_lock(&conn->mutex);
102
103 assert(conn->running);
104 conn->running = false;
105 if (conn->wait_co) {
106 aio_co_wake(conn->wait_co);
107 conn->wait_co = NULL;
108 }
109 do_free = conn->detached;
110
111 qemu_mutex_unlock(&conn->mutex);
112
113 if (do_free) {
114 nbd_client_connection_do_free(conn);
115 }
116
117 return NULL;
118 }
119
120 void nbd_client_connection_release(NBDClientConnection *conn)
121 {
122 bool do_free = false;
123
124 if (!conn) {
125 return;
126 }
127
128 qemu_mutex_lock(&conn->mutex);
129 assert(!conn->detached);
130 if (conn->running) {
131 conn->detached = true;
132 } else {
133 do_free = true;
134 }
135 qemu_mutex_unlock(&conn->mutex);
136
137 if (do_free) {
138 nbd_client_connection_do_free(conn);
139 }
140 }
141
142 /*
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
148 * completion
149 */
150 QIOChannelSocket *coroutine_fn
151 nbd_co_establish_connection(NBDClientConnection *conn, Error **errp)
152 {
153 QIOChannelSocket *sioc = NULL;
154 QemuThread thread;
155
156 qemu_mutex_lock(&conn->mutex);
157
158 /*
159 * Don't call nbd_co_establish_connection() in several coroutines in
160 * parallel. Only one call at once is supported.
161 */
162 assert(!conn->wait_co);
163
164 if (!conn->running) {
165 if (conn->sioc) {
166 /* Previous attempt finally succeeded in background */
167 sioc = g_steal_pointer(&conn->sioc);
168 qemu_mutex_unlock(&conn->mutex);
169
170 return sioc;
171 }
172
173 conn->running = true;
174 error_free(conn->err);
175 conn->err = NULL;
176 qemu_thread_create(&thread, "nbd-connect",
177 connect_thread_func, conn, QEMU_THREAD_DETACHED);
178 }
179
180 conn->wait_co = qemu_coroutine_self();
181
182 qemu_mutex_unlock(&conn->mutex);
183
184 /*
185 * We are going to wait for connect-thread finish, but
186 * nbd_co_establish_connection_cancel() can interrupt.
187 */
188 qemu_coroutine_yield();
189
190 qemu_mutex_lock(&conn->mutex);
191
192 if (conn->running) {
193 /*
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.
198 */
199 error_setg(errp, "Connection attempt cancelled by other operation");
200 } else {
201 error_propagate(errp, conn->err);
202 conn->err = NULL;
203 sioc = g_steal_pointer(&conn->sioc);
204 }
205
206 qemu_mutex_unlock(&conn->mutex);
207
208 return sioc;
209 }
210
211 /*
212 * nbd_co_establish_connection_cancel
213 * Cancel nbd_co_establish_connection() asynchronously.
214 *
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()
218 */
219 void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
220 {
221 Coroutine *wait_co;
222
223 qemu_mutex_lock(&conn->mutex);
224
225 wait_co = g_steal_pointer(&conn->wait_co);
226
227 qemu_mutex_unlock(&conn->mutex);
228
229 if (wait_co) {
230 aio_co_wake(wait_co);
231 }
232 }