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#include "qemu/coroutine.h"
33
34struct NBDClientConnection {
35
36 SocketAddress *saddr;
37 QCryptoTLSCreds *tlscreds;
38 char *tlshostname;
39 NBDExportInfo initial_info;
40 bool do_negotiation;
41 bool do_retry;
42
43 QemuMutex mutex;
44
45 NBDExportInfo updated_info;
46
47
48
49
50
51 QIOChannelSocket *sioc;
52 QIOChannel *ioc;
53
54
55
56
57 Error *err;
58
59
60 bool running;
61 bool detached;
62
63
64
65
66
67 Coroutine *wait_co;
68};
69
70
71
72
73
74void nbd_client_connection_enable_retry(NBDClientConnection *conn)
75{
76 conn->do_retry = true;
77}
78
79NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
80 bool do_negotiation,
81 const char *export_name,
82 const char *x_dirty_bitmap,
83 QCryptoTLSCreds *tlscreds,
84 const char *tlshostname)
85{
86 NBDClientConnection *conn = g_new(NBDClientConnection, 1);
87
88 object_ref(OBJECT(tlscreds));
89 *conn = (NBDClientConnection) {
90 .saddr = QAPI_CLONE(SocketAddress, saddr),
91 .tlscreds = tlscreds,
92 .tlshostname = g_strdup(tlshostname),
93 .do_negotiation = do_negotiation,
94
95 .initial_info.request_sizes = true,
96 .initial_info.structured_reply = true,
97 .initial_info.base_allocation = true,
98 .initial_info.x_dirty_bitmap = g_strdup(x_dirty_bitmap),
99 .initial_info.name = g_strdup(export_name ?: "")
100 };
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);
115 g_free(conn->tlshostname);
116 object_unref(OBJECT(conn->tlscreds));
117 g_free(conn->initial_info.x_dirty_bitmap);
118 g_free(conn->initial_info.name);
119 g_free(conn);
120}
121
122
123
124
125
126
127static int nbd_connect(QIOChannelSocket *sioc, SocketAddress *addr,
128 NBDExportInfo *info, QCryptoTLSCreds *tlscreds,
129 const char *tlshostname,
130 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
149 ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), tlscreds,
150 tlshostname,
151 outioc, info, errp);
152 if (ret < 0) {
153
154
155
156
157
158 if (outioc && *outioc) {
159 qio_channel_close(*outioc, NULL);
160 object_unref(OBJECT(*outioc));
161 *outioc = NULL;
162 } else {
163 qio_channel_close(QIO_CHANNEL(sioc), NULL);
164 }
165
166 return ret;
167 }
168
169 return 0;
170}
171
172static void *connect_thread_func(void *opaque)
173{
174 NBDClientConnection *conn = opaque;
175 int ret;
176 bool do_free;
177 uint64_t timeout = 1;
178 uint64_t max_timeout = 16;
179
180 qemu_mutex_lock(&conn->mutex);
181 while (!conn->detached) {
182 Error *local_err = NULL;
183
184 assert(!conn->sioc);
185 conn->sioc = qio_channel_socket_new();
186
187 qemu_mutex_unlock(&conn->mutex);
188
189 conn->updated_info = conn->initial_info;
190
191 ret = nbd_connect(conn->sioc, conn->saddr,
192 conn->do_negotiation ? &conn->updated_info : NULL,
193 conn->tlscreds, conn->tlshostname,
194 &conn->ioc, &local_err);
195
196
197
198
199
200
201
202 conn->updated_info.x_dirty_bitmap = NULL;
203 conn->updated_info.name = NULL;
204
205 qemu_mutex_lock(&conn->mutex);
206
207 error_free(conn->err);
208 conn->err = NULL;
209 error_propagate(&conn->err, local_err);
210
211 if (ret < 0) {
212 object_unref(OBJECT(conn->sioc));
213 conn->sioc = NULL;
214 if (conn->do_retry && !conn->detached) {
215 trace_nbd_connect_thread_sleep(timeout);
216 qemu_mutex_unlock(&conn->mutex);
217
218 sleep(timeout);
219 if (timeout < max_timeout) {
220 timeout *= 2;
221 }
222
223 qemu_mutex_lock(&conn->mutex);
224 continue;
225 }
226 }
227
228 break;
229 }
230
231
232
233 assert(conn->running);
234 conn->running = false;
235 if (conn->wait_co) {
236 aio_co_wake(conn->wait_co);
237 conn->wait_co = NULL;
238 }
239 do_free = conn->detached;
240
241 qemu_mutex_unlock(&conn->mutex);
242
243 if (do_free) {
244 nbd_client_connection_do_free(conn);
245 }
246
247 return NULL;
248}
249
250void nbd_client_connection_release(NBDClientConnection *conn)
251{
252 bool do_free = false;
253
254 if (!conn) {
255 return;
256 }
257
258 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
259 assert(!conn->detached);
260 if (conn->running) {
261 conn->detached = true;
262 } else {
263 do_free = true;
264 }
265 if (conn->sioc) {
266 qio_channel_shutdown(QIO_CHANNEL(conn->sioc),
267 QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
268 }
269 }
270
271 if (do_free) {
272 nbd_client_connection_do_free(conn);
273 }
274}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292QIOChannel *coroutine_fn
293nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
294 bool blocking, Error **errp)
295{
296 QemuThread thread;
297
298 if (conn->do_negotiation) {
299 assert(info);
300 }
301
302 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
303
304
305
306
307 assert(!conn->wait_co);
308
309 if (!conn->running) {
310 if (conn->sioc) {
311
312 if (conn->do_negotiation) {
313 memcpy(info, &conn->updated_info, sizeof(*info));
314 if (conn->ioc) {
315
316 object_unref(OBJECT(conn->sioc));
317 conn->sioc = NULL;
318
319 return g_steal_pointer(&conn->ioc);
320 }
321 }
322
323 assert(!conn->ioc);
324
325 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
326 }
327
328 conn->running = true;
329 qemu_thread_create(&thread, "nbd-connect",
330 connect_thread_func, conn, QEMU_THREAD_DETACHED);
331 }
332
333 if (!blocking) {
334 if (conn->err) {
335 error_propagate(errp, error_copy(conn->err));
336 } else {
337 error_setg(errp, "No connection at the moment");
338 }
339
340 return NULL;
341 }
342
343 conn->wait_co = qemu_coroutine_self();
344 }
345
346
347
348
349
350 qemu_coroutine_yield();
351
352 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
353 if (conn->running) {
354
355
356
357
358
359
360 if (conn->err) {
361 error_propagate(errp, error_copy(conn->err));
362 } else {
363
364
365
366
367
368
369
370
371 error_setg(errp, "Connection attempt cancelled by timeout");
372 }
373
374 return NULL;
375 } else {
376
377 assert(!conn->err != !conn->sioc);
378
379 if (conn->err) {
380 error_propagate(errp, error_copy(conn->err));
381 return NULL;
382 }
383
384 if (conn->do_negotiation) {
385 memcpy(info, &conn->updated_info, sizeof(*info));
386 if (conn->ioc) {
387
388 object_unref(OBJECT(conn->sioc));
389 conn->sioc = NULL;
390
391 return g_steal_pointer(&conn->ioc);
392 }
393 }
394
395 assert(!conn->ioc);
396
397 return QIO_CHANNEL(g_steal_pointer(&conn->sioc));
398 }
399 }
400
401 abort();
402}
403
404
405
406
407
408
409
410
411
412void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
413{
414 Coroutine *wait_co;
415
416 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
417 wait_co = g_steal_pointer(&conn->wait_co);
418 }
419
420 if (wait_co) {
421 aio_co_wake(wait_co);
422 }
423}
424