block: BdrvChildRole.attach/detach() callbacks
[qemu/ar7.git] / io / channel-socket.c
blobf546c6830e2a2665fe6d75a4cf353cfd218fdcd9
1 /*
2 * QEMU I/O channels sockets driver
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "io/channel-socket.h"
24 #include "io/channel-watch.h"
25 #include "trace.h"
26 #include "qapi/clone-visitor.h"
28 #define SOCKET_MAX_FDS 16
30 SocketAddress *
31 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
32 Error **errp)
34 return socket_sockaddr_to_address(&ioc->localAddr,
35 ioc->localAddrLen,
36 errp);
39 SocketAddress *
40 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
41 Error **errp)
43 return socket_sockaddr_to_address(&ioc->remoteAddr,
44 ioc->remoteAddrLen,
45 errp);
48 QIOChannelSocket *
49 qio_channel_socket_new(void)
51 QIOChannelSocket *sioc;
52 QIOChannel *ioc;
54 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
55 sioc->fd = -1;
57 ioc = QIO_CHANNEL(sioc);
58 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
60 #ifdef WIN32
61 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
62 #endif
64 trace_qio_channel_socket_new(sioc);
66 return sioc;
70 static int
71 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
72 int fd,
73 Error **errp)
75 if (sioc->fd != -1) {
76 error_setg(errp, "Socket is already open");
77 return -1;
80 sioc->fd = fd;
81 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
82 sioc->localAddrLen = sizeof(sioc->localAddr);
85 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
86 &sioc->remoteAddrLen) < 0) {
87 if (errno == ENOTCONN) {
88 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
89 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
90 } else {
91 error_setg_errno(errp, errno,
92 "Unable to query remote socket address");
93 goto error;
97 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
98 &sioc->localAddrLen) < 0) {
99 error_setg_errno(errp, errno,
100 "Unable to query local socket address");
101 goto error;
104 #ifndef WIN32
105 if (sioc->localAddr.ss_family == AF_UNIX) {
106 QIOChannel *ioc = QIO_CHANNEL(sioc);
107 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS);
109 #endif /* WIN32 */
111 return 0;
113 error:
114 sioc->fd = -1; /* Let the caller close FD on failure */
115 return -1;
118 QIOChannelSocket *
119 qio_channel_socket_new_fd(int fd,
120 Error **errp)
122 QIOChannelSocket *ioc;
124 ioc = qio_channel_socket_new();
125 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
126 object_unref(OBJECT(ioc));
127 return NULL;
130 trace_qio_channel_socket_new_fd(ioc, fd);
132 return ioc;
136 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
137 SocketAddress *addr,
138 Error **errp)
140 int fd;
142 trace_qio_channel_socket_connect_sync(ioc, addr);
143 fd = socket_connect(addr, errp, NULL, NULL);
144 if (fd < 0) {
145 trace_qio_channel_socket_connect_fail(ioc);
146 return -1;
149 trace_qio_channel_socket_connect_complete(ioc, fd);
150 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
151 close(fd);
152 return -1;
155 return 0;
159 static void qio_channel_socket_connect_worker(QIOTask *task,
160 gpointer opaque)
162 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
163 SocketAddress *addr = opaque;
164 Error *err = NULL;
166 qio_channel_socket_connect_sync(ioc, addr, &err);
168 qio_task_set_error(task, err);
172 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
173 SocketAddress *addr,
174 QIOTaskFunc callback,
175 gpointer opaque,
176 GDestroyNotify destroy)
178 QIOTask *task = qio_task_new(
179 OBJECT(ioc), callback, opaque, destroy);
180 SocketAddress *addrCopy;
182 addrCopy = QAPI_CLONE(SocketAddress, addr);
184 /* socket_connect() does a non-blocking connect(), but it
185 * still blocks in DNS lookups, so we must use a thread */
186 trace_qio_channel_socket_connect_async(ioc, addr);
187 qio_task_run_in_thread(task,
188 qio_channel_socket_connect_worker,
189 addrCopy,
190 (GDestroyNotify)qapi_free_SocketAddress);
194 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
195 SocketAddress *addr,
196 Error **errp)
198 int fd;
200 trace_qio_channel_socket_listen_sync(ioc, addr);
201 fd = socket_listen(addr, errp);
202 if (fd < 0) {
203 trace_qio_channel_socket_listen_fail(ioc);
204 return -1;
207 trace_qio_channel_socket_listen_complete(ioc, fd);
208 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
209 close(fd);
210 return -1;
212 qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_LISTEN);
214 return 0;
218 static void qio_channel_socket_listen_worker(QIOTask *task,
219 gpointer opaque)
221 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
222 SocketAddress *addr = opaque;
223 Error *err = NULL;
225 qio_channel_socket_listen_sync(ioc, addr, &err);
227 qio_task_set_error(task, err);
231 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
232 SocketAddress *addr,
233 QIOTaskFunc callback,
234 gpointer opaque,
235 GDestroyNotify destroy)
237 QIOTask *task = qio_task_new(
238 OBJECT(ioc), callback, opaque, destroy);
239 SocketAddress *addrCopy;
241 addrCopy = QAPI_CLONE(SocketAddress, addr);
243 /* socket_listen() blocks in DNS lookups, so we must use a thread */
244 trace_qio_channel_socket_listen_async(ioc, addr);
245 qio_task_run_in_thread(task,
246 qio_channel_socket_listen_worker,
247 addrCopy,
248 (GDestroyNotify)qapi_free_SocketAddress);
252 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
253 SocketAddress *localAddr,
254 SocketAddress *remoteAddr,
255 Error **errp)
257 int fd;
259 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
260 fd = socket_dgram(remoteAddr, localAddr, errp);
261 if (fd < 0) {
262 trace_qio_channel_socket_dgram_fail(ioc);
263 return -1;
266 trace_qio_channel_socket_dgram_complete(ioc, fd);
267 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
268 close(fd);
269 return -1;
272 return 0;
276 struct QIOChannelSocketDGramWorkerData {
277 SocketAddress *localAddr;
278 SocketAddress *remoteAddr;
282 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
284 struct QIOChannelSocketDGramWorkerData *data = opaque;
285 qapi_free_SocketAddress(data->localAddr);
286 qapi_free_SocketAddress(data->remoteAddr);
287 g_free(data);
290 static void qio_channel_socket_dgram_worker(QIOTask *task,
291 gpointer opaque)
293 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
294 struct QIOChannelSocketDGramWorkerData *data = opaque;
295 Error *err = NULL;
297 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
298 qio_channel_socket_dgram_sync(ioc, data->localAddr,
299 data->remoteAddr, &err);
301 qio_task_set_error(task, err);
305 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
306 SocketAddress *localAddr,
307 SocketAddress *remoteAddr,
308 QIOTaskFunc callback,
309 gpointer opaque,
310 GDestroyNotify destroy)
312 QIOTask *task = qio_task_new(
313 OBJECT(ioc), callback, opaque, destroy);
314 struct QIOChannelSocketDGramWorkerData *data = g_new0(
315 struct QIOChannelSocketDGramWorkerData, 1);
317 data->localAddr = QAPI_CLONE(SocketAddress, localAddr);
318 data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr);
320 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
321 qio_task_run_in_thread(task,
322 qio_channel_socket_dgram_worker,
323 data,
324 qio_channel_socket_dgram_worker_free);
328 QIOChannelSocket *
329 qio_channel_socket_accept(QIOChannelSocket *ioc,
330 Error **errp)
332 QIOChannelSocket *cioc;
334 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
335 cioc->fd = -1;
336 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
337 cioc->localAddrLen = sizeof(ioc->localAddr);
339 #ifdef WIN32
340 QIO_CHANNEL(cioc)->event = CreateEvent(NULL, FALSE, FALSE, NULL);
341 #endif
344 retry:
345 trace_qio_channel_socket_accept(ioc);
346 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
347 &cioc->remoteAddrLen);
348 if (cioc->fd < 0) {
349 trace_qio_channel_socket_accept_fail(ioc);
350 if (errno == EINTR) {
351 goto retry;
353 goto error;
356 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
357 &cioc->localAddrLen) < 0) {
358 error_setg_errno(errp, errno,
359 "Unable to query local socket address");
360 goto error;
363 #ifndef WIN32
364 if (cioc->localAddr.ss_family == AF_UNIX) {
365 QIOChannel *ioc_local = QIO_CHANNEL(cioc);
366 qio_channel_set_feature(ioc_local, QIO_CHANNEL_FEATURE_FD_PASS);
368 #endif /* WIN32 */
370 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
371 return cioc;
373 error:
374 object_unref(OBJECT(cioc));
375 return NULL;
378 static void qio_channel_socket_init(Object *obj)
380 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
381 ioc->fd = -1;
384 static void qio_channel_socket_finalize(Object *obj)
386 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
388 if (ioc->fd != -1) {
389 QIOChannel *ioc_local = QIO_CHANNEL(ioc);
390 if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) {
391 Error *err = NULL;
393 socket_listen_cleanup(ioc->fd, &err);
394 if (err) {
395 error_report_err(err);
396 err = NULL;
399 #ifdef WIN32
400 WSAEventSelect(ioc->fd, NULL, 0);
401 #endif
402 closesocket(ioc->fd);
403 ioc->fd = -1;
408 #ifndef WIN32
409 static void qio_channel_socket_copy_fds(struct msghdr *msg,
410 int **fds, size_t *nfds)
412 struct cmsghdr *cmsg;
414 *nfds = 0;
415 *fds = NULL;
417 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
418 int fd_size, i;
419 int gotfds;
421 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
422 cmsg->cmsg_level != SOL_SOCKET ||
423 cmsg->cmsg_type != SCM_RIGHTS) {
424 continue;
427 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
429 if (!fd_size) {
430 continue;
433 gotfds = fd_size / sizeof(int);
434 *fds = g_renew(int, *fds, *nfds + gotfds);
435 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
437 for (i = 0; i < gotfds; i++) {
438 int fd = (*fds)[*nfds + i];
439 if (fd < 0) {
440 continue;
443 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
444 qemu_set_block(fd);
446 #ifndef MSG_CMSG_CLOEXEC
447 qemu_set_cloexec(fd);
448 #endif
450 *nfds += gotfds;
455 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
456 const struct iovec *iov,
457 size_t niov,
458 int **fds,
459 size_t *nfds,
460 Error **errp)
462 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
463 ssize_t ret;
464 struct msghdr msg = { NULL, };
465 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
466 int sflags = 0;
468 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
470 #ifdef MSG_CMSG_CLOEXEC
471 sflags |= MSG_CMSG_CLOEXEC;
472 #endif
474 msg.msg_iov = (struct iovec *)iov;
475 msg.msg_iovlen = niov;
476 if (fds && nfds) {
477 msg.msg_control = control;
478 msg.msg_controllen = sizeof(control);
481 retry:
482 ret = recvmsg(sioc->fd, &msg, sflags);
483 if (ret < 0) {
484 if (errno == EAGAIN) {
485 return QIO_CHANNEL_ERR_BLOCK;
487 if (errno == EINTR) {
488 goto retry;
491 error_setg_errno(errp, errno,
492 "Unable to read from socket");
493 return -1;
496 if (fds && nfds) {
497 qio_channel_socket_copy_fds(&msg, fds, nfds);
500 return ret;
503 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
504 const struct iovec *iov,
505 size_t niov,
506 int *fds,
507 size_t nfds,
508 Error **errp)
510 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
511 ssize_t ret;
512 struct msghdr msg = { NULL, };
513 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
514 size_t fdsize = sizeof(int) * nfds;
515 struct cmsghdr *cmsg;
517 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
519 msg.msg_iov = (struct iovec *)iov;
520 msg.msg_iovlen = niov;
522 if (nfds) {
523 if (nfds > SOCKET_MAX_FDS) {
524 error_setg_errno(errp, EINVAL,
525 "Only %d FDs can be sent, got %zu",
526 SOCKET_MAX_FDS, nfds);
527 return -1;
530 msg.msg_control = control;
531 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
533 cmsg = CMSG_FIRSTHDR(&msg);
534 cmsg->cmsg_len = CMSG_LEN(fdsize);
535 cmsg->cmsg_level = SOL_SOCKET;
536 cmsg->cmsg_type = SCM_RIGHTS;
537 memcpy(CMSG_DATA(cmsg), fds, fdsize);
540 retry:
541 ret = sendmsg(sioc->fd, &msg, 0);
542 if (ret <= 0) {
543 if (errno == EAGAIN) {
544 return QIO_CHANNEL_ERR_BLOCK;
546 if (errno == EINTR) {
547 goto retry;
549 error_setg_errno(errp, errno,
550 "Unable to write to socket");
551 return -1;
553 return ret;
555 #else /* WIN32 */
556 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
557 const struct iovec *iov,
558 size_t niov,
559 int **fds,
560 size_t *nfds,
561 Error **errp)
563 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
564 ssize_t done = 0;
565 ssize_t i;
567 for (i = 0; i < niov; i++) {
568 ssize_t ret;
569 retry:
570 ret = recv(sioc->fd,
571 iov[i].iov_base,
572 iov[i].iov_len,
574 if (ret < 0) {
575 if (errno == EAGAIN) {
576 if (done) {
577 return done;
578 } else {
579 return QIO_CHANNEL_ERR_BLOCK;
581 } else if (errno == EINTR) {
582 goto retry;
583 } else {
584 error_setg_errno(errp, errno,
585 "Unable to read from socket");
586 return -1;
589 done += ret;
590 if (ret < iov[i].iov_len) {
591 return done;
595 return done;
598 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
599 const struct iovec *iov,
600 size_t niov,
601 int *fds,
602 size_t nfds,
603 Error **errp)
605 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
606 ssize_t done = 0;
607 ssize_t i;
609 for (i = 0; i < niov; i++) {
610 ssize_t ret;
611 retry:
612 ret = send(sioc->fd,
613 iov[i].iov_base,
614 iov[i].iov_len,
616 if (ret < 0) {
617 if (errno == EAGAIN) {
618 if (done) {
619 return done;
620 } else {
621 return QIO_CHANNEL_ERR_BLOCK;
623 } else if (errno == EINTR) {
624 goto retry;
625 } else {
626 error_setg_errno(errp, errno,
627 "Unable to write to socket");
628 return -1;
631 done += ret;
632 if (ret < iov[i].iov_len) {
633 return done;
637 return done;
639 #endif /* WIN32 */
641 static int
642 qio_channel_socket_set_blocking(QIOChannel *ioc,
643 bool enabled,
644 Error **errp)
646 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
648 if (enabled) {
649 qemu_set_block(sioc->fd);
650 } else {
651 qemu_set_nonblock(sioc->fd);
653 return 0;
657 static void
658 qio_channel_socket_set_delay(QIOChannel *ioc,
659 bool enabled)
661 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
662 int v = enabled ? 0 : 1;
664 qemu_setsockopt(sioc->fd,
665 IPPROTO_TCP, TCP_NODELAY,
666 &v, sizeof(v));
670 static void
671 qio_channel_socket_set_cork(QIOChannel *ioc,
672 bool enabled)
674 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
675 int v = enabled ? 1 : 0;
677 socket_set_cork(sioc->fd, v);
681 static int
682 qio_channel_socket_close(QIOChannel *ioc,
683 Error **errp)
685 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
687 if (sioc->fd != -1) {
688 #ifdef WIN32
689 WSAEventSelect(sioc->fd, NULL, 0);
690 #endif
691 if (closesocket(sioc->fd) < 0) {
692 sioc->fd = -1;
693 error_setg_errno(errp, errno,
694 "Unable to close socket");
695 return -1;
697 sioc->fd = -1;
699 return 0;
702 static int
703 qio_channel_socket_shutdown(QIOChannel *ioc,
704 QIOChannelShutdown how,
705 Error **errp)
707 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
708 int sockhow;
710 switch (how) {
711 case QIO_CHANNEL_SHUTDOWN_READ:
712 sockhow = SHUT_RD;
713 break;
714 case QIO_CHANNEL_SHUTDOWN_WRITE:
715 sockhow = SHUT_WR;
716 break;
717 case QIO_CHANNEL_SHUTDOWN_BOTH:
718 default:
719 sockhow = SHUT_RDWR;
720 break;
723 if (shutdown(sioc->fd, sockhow) < 0) {
724 error_setg_errno(errp, errno,
725 "Unable to shutdown socket");
726 return -1;
728 return 0;
731 static void qio_channel_socket_set_aio_fd_handler(QIOChannel *ioc,
732 AioContext *ctx,
733 IOHandler *io_read,
734 IOHandler *io_write,
735 void *opaque)
737 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
738 aio_set_fd_handler(ctx, sioc->fd, false, io_read, io_write, NULL, opaque);
741 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
742 GIOCondition condition)
744 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
745 return qio_channel_create_socket_watch(ioc,
746 sioc->fd,
747 condition);
750 static void qio_channel_socket_class_init(ObjectClass *klass,
751 void *class_data G_GNUC_UNUSED)
753 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
755 ioc_klass->io_writev = qio_channel_socket_writev;
756 ioc_klass->io_readv = qio_channel_socket_readv;
757 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
758 ioc_klass->io_close = qio_channel_socket_close;
759 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
760 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
761 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
762 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
763 ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
766 static const TypeInfo qio_channel_socket_info = {
767 .parent = TYPE_QIO_CHANNEL,
768 .name = TYPE_QIO_CHANNEL_SOCKET,
769 .instance_size = sizeof(QIOChannelSocket),
770 .instance_init = qio_channel_socket_init,
771 .instance_finalize = qio_channel_socket_finalize,
772 .class_init = qio_channel_socket_class_init,
775 static void qio_channel_socket_register_types(void)
777 type_register_static(&qio_channel_socket_info);
780 type_init(qio_channel_socket_register_types);