1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "trace.h"
27
28#include "block/nbd.h"
29
30#include "qapi/qapi-visit-sockets.h"
31#include "qapi/clone-visitor.h"
32
33struct NBDClientConnection {
34
35 SocketAddress *saddr;
36 QCryptoTLSCreds *tlscreds;
37 char *tlshostname;
38 NBDExportInfo initial_info;
39 bool do_negotiation;
40 bool do_retry;
41
42 QemuMutex mutex;
43
44 NBDExportInfo updated_info;
45
46
47
48
49
50 QIOChannelSocket *sioc;
51 QIOChannel *ioc;
52
53
54
55
56 Error *err;
57
58
59 bool running;
60 bool detached;
61
62
63
64
65
66 Coroutine *wait_co;
67};
68
69
70
71
72
73void nbd_client_connection_enable_retry(NBDClientConnection *conn)
74{
75 conn->do_retry = true;
76}
77
78NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
79 bool do_negotiation,
80 const char *export_name,
81 const char *x_dirty_bitmap,
82 QCryptoTLSCreds *tlscreds,
83 const char *tlshostname)
84{
85 NBDClientConnection *conn = g_new(NBDClientConnection, 1);
86
87 object_ref(OBJECT(tlscreds));
88 *conn = (NBDClientConnection) {
89 .saddr = QAPI_CLONE(SocketAddress, saddr),
90 .tlscreds = tlscreds,
91 .tlshostname = g_strdup(tlshostname),
92 .do_negotiation = do_negotiation,
93
94 .initial_info.request_sizes = true,
95 .initial_info.structured_reply = true,
96 .initial_info.base_allocation = true,
97 .initial_info.x_dirty_bitmap = g_strdup(x_dirty_bitmap),
98 .initial_info.name = g_strdup(export_name ?: "")
99 };
100
101 qemu_mutex_init(&conn->mutex);
102
103 return conn;
104}
105
106static void nbd_client_connection_do_free(NBDClientConnection *conn)
107{
108 if (conn->sioc) {
109 qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
110 object_unref(OBJECT(conn->sioc));
111 }
112 error_free(conn->err);
113 qapi_free_SocketAddress(conn->saddr);
114 g_free(conn->tlshostname);
115 object_unref(OBJECT(conn->tlscreds));
116 g_free(conn->initial_info.x_dirty_bitmap);
117 g_free(conn->initial_info.name);
118 g_free(conn);
119}
120
121
122
123
124
125
126static int nbd_connect(QIOChannelSocket *sioc, SocketAddress *addr,
127 NBDExportInfo *info, QCryptoTLSCreds *tlscreds,
128 const char *tlshostname,
129 QIOChannel **outioc, Error **errp)
130{
131 int ret;
132
133 if (outioc) {
134 *outioc = NULL;
135 }
136
137 ret = qio_channel_socket_connect_sync(sioc, addr, errp);
138 if (ret < 0) {
139 return ret;
140 }
141
142 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
143
144 if (!info) {
145 return 0;
146 }
147
148 ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), tlscreds,
149 tlshostname,
150 outioc, info, errp);
151 if (ret < 0) {
152
153
154
155
156
157 if (outioc && *outioc) {
158 qio_channel_close(QIO_CHANNEL(*outioc), NULL);
159 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
171static void *connect_thread_func(void *opaque)
172{
173 NBDClientConnection *conn = opaque;
174 int ret;
175 bool do_free;
176 uint64_t timeout = 1;
177 uint64_t max_timeout = 16;
178
179 qemu_mutex_lock(&conn->mutex);
180 while (!conn->detached) {
181 Error *local_err = NULL;
182
183 assert(!conn->sioc);
184 conn->sioc = qio_channel_socket_new();
185
186 qemu_mutex_unlock(&conn->mutex);
187
188 conn->updated_info = conn->initial_info;
189
190 ret = nbd_connect(conn->sioc, conn->saddr,
191 conn->do_negotiation ? &conn->updated_info : NULL,
192 conn->tlscreds, conn->tlshostname,
193 &conn->ioc, &local_err);
194
195
196
197
198
199
200
201 conn->updated_info.x_dirty_bitmap = NULL;
202 conn->updated_info.name = NULL;
203
204 qemu_mutex_lock(&conn->mutex);
205
206 error_free(conn->err);
207 conn->err = NULL;
208 error_propagate(&conn->err, local_err);
209
210 if (ret < 0) {
211 object_unref(OBJECT(conn->sioc));
212 conn->sioc = NULL;
213 if (conn->do_retry && !conn->detached) {
214 trace_nbd_connect_thread_sleep(timeout);
215 qemu_mutex_unlock(&conn->mutex);
216
217 sleep(timeout);
218 if (timeout < max_timeout) {
219 timeout *= 2;
220 }
221
222 qemu_mutex_lock(&conn->mutex);
223 continue;
224 }
225 }
226
227 break;
228 }
229
230
231
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
257 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 }
264 if (conn->sioc) {
265 qio_channel_shutdown(QIO_CHANNEL(conn->sioc),
266 QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
267 }
268 }
269
270 if (do_free) {
271 nbd_client_connection_do_free(conn);
272 }
273}
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291QIOChannel *coroutine_fn
292nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
293 bool blocking, Error **errp)
294{
295 QemuThread thread;
296
297 if (conn->do_negotiation) {
298 assert(info);
299 }
300
301 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
302
303
304
305
306 assert(!conn->wait_co);
307
308 if (!conn->running) {
309 if (conn->sioc) {
310
311 if (conn->do_negotiation) {
312 memcpy(info, &conn->updated_info, sizeof(*info));
313 if (conn->ioc) {
314
315 object_unref(OBJECT(conn->sioc));
316 conn->sioc = NULL;
317
318 return g_steal_pointer(&conn->ioc);
319 }
320 }
321
322 assert(!conn->ioc);
323
324 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
325 }
326
327 conn->running = true;
328 qemu_thread_create(&thread, "nbd-connect",
329 connect_thread_func, conn, QEMU_THREAD_DETACHED);
330 }
331
332 if (!blocking) {
333 if (conn->err) {
334 error_propagate(errp, error_copy(conn->err));
335 } else {
336 error_setg(errp, "No connection at the moment");
337 }
338
339 return NULL;
340 }
341
342 conn->wait_co = qemu_coroutine_self();
343 }
344
345
346
347
348
349 qemu_coroutine_yield();
350
351 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
352 if (conn->running) {
353
354
355
356
357
358
359 if (conn->err) {
360 error_propagate(errp, error_copy(conn->err));
361 } else {
362
363
364
365
366
367
368
369
370 error_setg(errp, "Connection attempt cancelled by timeout");
371 }
372
373 return NULL;
374 } else {
375
376 assert(!conn->err != !conn->sioc);
377
378 if (conn->err) {
379 error_propagate(errp, error_copy(conn->err));
380 return NULL;
381 }
382
383 if (conn->do_negotiation) {
384 memcpy(info, &conn->updated_info, sizeof(*info));
385 if (conn->ioc) {
386
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));
397 }
398 }
399
400 abort();
401}
402
403
404
405
406
407
408
409
410
411void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
412{
413 Coroutine *wait_co;
414
415 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
416 wait_co = g_steal_pointer(&conn->wait_co);
417 }
418
419 if (wait_co) {
420 aio_co_wake(wait_co);
421 }
422}
423