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 "qapi/qapi-visit-sockets.h"
24 #include "io/channel-socket.h"
25 #include "io/channel-watch.h"
27 #include "qapi/clone-visitor.h"
29 #define SOCKET_MAX_FDS 16
32 qio_channel_socket_get_local_address(QIOChannelSocket
*ioc
,
35 return socket_sockaddr_to_address(&ioc
->localAddr
,
41 qio_channel_socket_get_remote_address(QIOChannelSocket
*ioc
,
44 return socket_sockaddr_to_address(&ioc
->remoteAddr
,
50 qio_channel_socket_new(void)
52 QIOChannelSocket
*sioc
;
55 sioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
58 ioc
= QIO_CHANNEL(sioc
);
59 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
62 ioc
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
65 trace_qio_channel_socket_new(sioc
);
72 qio_channel_socket_set_fd(QIOChannelSocket
*sioc
,
77 error_setg(errp
, "Socket is already open");
82 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
83 sioc
->localAddrLen
= sizeof(sioc
->localAddr
);
86 if (getpeername(fd
, (struct sockaddr
*)&sioc
->remoteAddr
,
87 &sioc
->remoteAddrLen
) < 0) {
88 if (errno
== ENOTCONN
) {
89 memset(&sioc
->remoteAddr
, 0, sizeof(sioc
->remoteAddr
));
90 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
92 error_setg_errno(errp
, errno
,
93 "Unable to query remote socket address");
98 if (getsockname(fd
, (struct sockaddr
*)&sioc
->localAddr
,
99 &sioc
->localAddrLen
) < 0) {
100 error_setg_errno(errp
, errno
,
101 "Unable to query local socket address");
106 if (sioc
->localAddr
.ss_family
== AF_UNIX
) {
107 QIOChannel
*ioc
= QIO_CHANNEL(sioc
);
108 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_FD_PASS
);
115 sioc
->fd
= -1; /* Let the caller close FD on failure */
120 qio_channel_socket_new_fd(int fd
,
123 QIOChannelSocket
*ioc
;
125 ioc
= qio_channel_socket_new();
126 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
127 object_unref(OBJECT(ioc
));
131 trace_qio_channel_socket_new_fd(ioc
, fd
);
137 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
143 trace_qio_channel_socket_connect_sync(ioc
, addr
);
144 fd
= socket_connect(addr
, errp
);
146 trace_qio_channel_socket_connect_fail(ioc
);
150 trace_qio_channel_socket_connect_complete(ioc
, fd
);
151 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
160 static void qio_channel_socket_connect_worker(QIOTask
*task
,
163 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
164 SocketAddress
*addr
= opaque
;
167 qio_channel_socket_connect_sync(ioc
, addr
, &err
);
169 qio_task_set_error(task
, err
);
173 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
175 QIOTaskFunc callback
,
177 GDestroyNotify destroy
,
178 GMainContext
*context
)
180 QIOTask
*task
= qio_task_new(
181 OBJECT(ioc
), callback
, opaque
, destroy
);
182 SocketAddress
*addrCopy
;
184 addrCopy
= QAPI_CLONE(SocketAddress
, addr
);
186 /* socket_connect() does a non-blocking connect(), but it
187 * still blocks in DNS lookups, so we must use a thread */
188 trace_qio_channel_socket_connect_async(ioc
, addr
);
189 qio_task_run_in_thread(task
,
190 qio_channel_socket_connect_worker
,
192 (GDestroyNotify
)qapi_free_SocketAddress
,
197 int qio_channel_socket_listen_sync(QIOChannelSocket
*ioc
,
203 trace_qio_channel_socket_listen_sync(ioc
, addr
);
204 fd
= socket_listen(addr
, errp
);
206 trace_qio_channel_socket_listen_fail(ioc
);
210 trace_qio_channel_socket_listen_complete(ioc
, fd
);
211 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
215 qio_channel_set_feature(QIO_CHANNEL(ioc
), QIO_CHANNEL_FEATURE_LISTEN
);
221 static void qio_channel_socket_listen_worker(QIOTask
*task
,
224 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
225 SocketAddress
*addr
= opaque
;
228 qio_channel_socket_listen_sync(ioc
, addr
, &err
);
230 qio_task_set_error(task
, err
);
234 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
236 QIOTaskFunc callback
,
238 GDestroyNotify destroy
,
239 GMainContext
*context
)
241 QIOTask
*task
= qio_task_new(
242 OBJECT(ioc
), callback
, opaque
, destroy
);
243 SocketAddress
*addrCopy
;
245 addrCopy
= QAPI_CLONE(SocketAddress
, addr
);
247 /* socket_listen() blocks in DNS lookups, so we must use a thread */
248 trace_qio_channel_socket_listen_async(ioc
, addr
);
249 qio_task_run_in_thread(task
,
250 qio_channel_socket_listen_worker
,
252 (GDestroyNotify
)qapi_free_SocketAddress
,
257 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
258 SocketAddress
*localAddr
,
259 SocketAddress
*remoteAddr
,
264 trace_qio_channel_socket_dgram_sync(ioc
, localAddr
, remoteAddr
);
265 fd
= socket_dgram(remoteAddr
, localAddr
, errp
);
267 trace_qio_channel_socket_dgram_fail(ioc
);
271 trace_qio_channel_socket_dgram_complete(ioc
, fd
);
272 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
281 struct QIOChannelSocketDGramWorkerData
{
282 SocketAddress
*localAddr
;
283 SocketAddress
*remoteAddr
;
287 static void qio_channel_socket_dgram_worker_free(gpointer opaque
)
289 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
290 qapi_free_SocketAddress(data
->localAddr
);
291 qapi_free_SocketAddress(data
->remoteAddr
);
295 static void qio_channel_socket_dgram_worker(QIOTask
*task
,
298 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
299 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
302 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
303 qio_channel_socket_dgram_sync(ioc
, data
->localAddr
,
304 data
->remoteAddr
, &err
);
306 qio_task_set_error(task
, err
);
310 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
311 SocketAddress
*localAddr
,
312 SocketAddress
*remoteAddr
,
313 QIOTaskFunc callback
,
315 GDestroyNotify destroy
,
316 GMainContext
*context
)
318 QIOTask
*task
= qio_task_new(
319 OBJECT(ioc
), callback
, opaque
, destroy
);
320 struct QIOChannelSocketDGramWorkerData
*data
= g_new0(
321 struct QIOChannelSocketDGramWorkerData
, 1);
323 data
->localAddr
= QAPI_CLONE(SocketAddress
, localAddr
);
324 data
->remoteAddr
= QAPI_CLONE(SocketAddress
, remoteAddr
);
326 trace_qio_channel_socket_dgram_async(ioc
, localAddr
, remoteAddr
);
327 qio_task_run_in_thread(task
,
328 qio_channel_socket_dgram_worker
,
330 qio_channel_socket_dgram_worker_free
,
336 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
339 QIOChannelSocket
*cioc
;
341 cioc
= qio_channel_socket_new();
342 cioc
->remoteAddrLen
= sizeof(ioc
->remoteAddr
);
343 cioc
->localAddrLen
= sizeof(ioc
->localAddr
);
346 trace_qio_channel_socket_accept(ioc
);
347 cioc
->fd
= qemu_accept(ioc
->fd
, (struct sockaddr
*)&cioc
->remoteAddr
,
348 &cioc
->remoteAddrLen
);
350 if (errno
== EINTR
) {
353 error_setg_errno(errp
, errno
, "Unable to accept connection");
354 trace_qio_channel_socket_accept_fail(ioc
);
358 if (getsockname(cioc
->fd
, (struct sockaddr
*)&cioc
->localAddr
,
359 &cioc
->localAddrLen
) < 0) {
360 error_setg_errno(errp
, errno
,
361 "Unable to query local socket address");
366 if (cioc
->localAddr
.ss_family
== AF_UNIX
) {
367 QIOChannel
*ioc_local
= QIO_CHANNEL(cioc
);
368 qio_channel_set_feature(ioc_local
, QIO_CHANNEL_FEATURE_FD_PASS
);
372 trace_qio_channel_socket_accept_complete(ioc
, cioc
, cioc
->fd
);
376 object_unref(OBJECT(cioc
));
380 static void qio_channel_socket_init(Object
*obj
)
382 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
386 static void qio_channel_socket_finalize(Object
*obj
)
388 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
391 QIOChannel
*ioc_local
= QIO_CHANNEL(ioc
);
392 if (qio_channel_has_feature(ioc_local
, QIO_CHANNEL_FEATURE_LISTEN
)) {
395 socket_listen_cleanup(ioc
->fd
, &err
);
397 error_report_err(err
);
402 WSAEventSelect(ioc
->fd
, NULL
, 0);
404 closesocket(ioc
->fd
);
411 static void qio_channel_socket_copy_fds(struct msghdr
*msg
,
412 int **fds
, size_t *nfds
)
414 struct cmsghdr
*cmsg
;
419 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
423 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
424 cmsg
->cmsg_level
!= SOL_SOCKET
||
425 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
429 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
435 gotfds
= fd_size
/ sizeof(int);
436 *fds
= g_renew(int, *fds
, *nfds
+ gotfds
);
437 memcpy(*fds
+ *nfds
, CMSG_DATA(cmsg
), fd_size
);
439 for (i
= 0; i
< gotfds
; i
++) {
440 int fd
= (*fds
)[*nfds
+ i
];
445 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
448 #ifndef MSG_CMSG_CLOEXEC
449 qemu_set_cloexec(fd
);
457 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
458 const struct iovec
*iov
,
464 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
466 struct msghdr msg
= { NULL
, };
467 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
470 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
472 #ifdef MSG_CMSG_CLOEXEC
473 sflags
|= MSG_CMSG_CLOEXEC
;
476 msg
.msg_iov
= (struct iovec
*)iov
;
477 msg
.msg_iovlen
= niov
;
479 msg
.msg_control
= control
;
480 msg
.msg_controllen
= sizeof(control
);
484 ret
= recvmsg(sioc
->fd
, &msg
, sflags
);
486 if (errno
== EAGAIN
) {
487 return QIO_CHANNEL_ERR_BLOCK
;
489 if (errno
== EINTR
) {
493 error_setg_errno(errp
, errno
,
494 "Unable to read from socket");
499 qio_channel_socket_copy_fds(&msg
, fds
, nfds
);
505 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
506 const struct iovec
*iov
,
512 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
514 struct msghdr msg
= { NULL
, };
515 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
516 size_t fdsize
= sizeof(int) * nfds
;
517 struct cmsghdr
*cmsg
;
519 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
521 msg
.msg_iov
= (struct iovec
*)iov
;
522 msg
.msg_iovlen
= niov
;
525 if (nfds
> SOCKET_MAX_FDS
) {
526 error_setg_errno(errp
, EINVAL
,
527 "Only %d FDs can be sent, got %zu",
528 SOCKET_MAX_FDS
, nfds
);
532 msg
.msg_control
= control
;
533 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfds
);
535 cmsg
= CMSG_FIRSTHDR(&msg
);
536 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
537 cmsg
->cmsg_level
= SOL_SOCKET
;
538 cmsg
->cmsg_type
= SCM_RIGHTS
;
539 memcpy(CMSG_DATA(cmsg
), fds
, fdsize
);
543 ret
= sendmsg(sioc
->fd
, &msg
, 0);
545 if (errno
== EAGAIN
) {
546 return QIO_CHANNEL_ERR_BLOCK
;
548 if (errno
== EINTR
) {
551 error_setg_errno(errp
, errno
,
552 "Unable to write to socket");
558 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
559 const struct iovec
*iov
,
565 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
569 for (i
= 0; i
< niov
; i
++) {
577 if (errno
== EAGAIN
) {
581 return QIO_CHANNEL_ERR_BLOCK
;
583 } else if (errno
== EINTR
) {
586 error_setg_errno(errp
, errno
,
587 "Unable to read from socket");
592 if (ret
< iov
[i
].iov_len
) {
600 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
601 const struct iovec
*iov
,
607 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
611 for (i
= 0; i
< niov
; i
++) {
619 if (errno
== EAGAIN
) {
623 return QIO_CHANNEL_ERR_BLOCK
;
625 } else if (errno
== EINTR
) {
628 error_setg_errno(errp
, errno
,
629 "Unable to write to socket");
634 if (ret
< iov
[i
].iov_len
) {
644 qio_channel_socket_set_blocking(QIOChannel
*ioc
,
648 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
651 qemu_set_block(sioc
->fd
);
653 qemu_set_nonblock(sioc
->fd
);
660 qio_channel_socket_set_delay(QIOChannel
*ioc
,
663 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
664 int v
= enabled
? 0 : 1;
666 qemu_setsockopt(sioc
->fd
,
667 IPPROTO_TCP
, TCP_NODELAY
,
673 qio_channel_socket_set_cork(QIOChannel
*ioc
,
676 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
677 int v
= enabled
? 1 : 0;
679 socket_set_cork(sioc
->fd
, v
);
684 qio_channel_socket_close(QIOChannel
*ioc
,
687 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
690 if (sioc
->fd
!= -1) {
691 SocketAddress
*addr
= socket_local_address(sioc
->fd
, errp
);
693 WSAEventSelect(sioc
->fd
, NULL
, 0);
695 if (closesocket(sioc
->fd
) < 0) {
697 error_setg_errno(errp
, errno
,
698 "Unable to close socket");
703 if (addr
&& addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
704 && addr
->u
.q_unix
.path
) {
705 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
706 error_setg_errno(errp
, errno
,
707 "Failed to unlink socket %s",
708 addr
->u
.q_unix
.path
);
714 qapi_free_SocketAddress(addr
);
721 qio_channel_socket_shutdown(QIOChannel
*ioc
,
722 QIOChannelShutdown how
,
725 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
729 case QIO_CHANNEL_SHUTDOWN_READ
:
732 case QIO_CHANNEL_SHUTDOWN_WRITE
:
735 case QIO_CHANNEL_SHUTDOWN_BOTH
:
741 if (shutdown(sioc
->fd
, sockhow
) < 0) {
742 error_setg_errno(errp
, errno
,
743 "Unable to shutdown socket");
749 static void qio_channel_socket_set_aio_fd_handler(QIOChannel
*ioc
,
755 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
756 aio_set_fd_handler(ctx
, sioc
->fd
, false, io_read
, io_write
, NULL
, opaque
);
759 static GSource
*qio_channel_socket_create_watch(QIOChannel
*ioc
,
760 GIOCondition condition
)
762 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
763 return qio_channel_create_socket_watch(ioc
,
768 static void qio_channel_socket_class_init(ObjectClass
*klass
,
769 void *class_data G_GNUC_UNUSED
)
771 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
773 ioc_klass
->io_writev
= qio_channel_socket_writev
;
774 ioc_klass
->io_readv
= qio_channel_socket_readv
;
775 ioc_klass
->io_set_blocking
= qio_channel_socket_set_blocking
;
776 ioc_klass
->io_close
= qio_channel_socket_close
;
777 ioc_klass
->io_shutdown
= qio_channel_socket_shutdown
;
778 ioc_klass
->io_set_cork
= qio_channel_socket_set_cork
;
779 ioc_klass
->io_set_delay
= qio_channel_socket_set_delay
;
780 ioc_klass
->io_create_watch
= qio_channel_socket_create_watch
;
781 ioc_klass
->io_set_aio_fd_handler
= qio_channel_socket_set_aio_fd_handler
;
784 static const TypeInfo qio_channel_socket_info
= {
785 .parent
= TYPE_QIO_CHANNEL
,
786 .name
= TYPE_QIO_CHANNEL_SOCKET
,
787 .instance_size
= sizeof(QIOChannelSocket
),
788 .instance_init
= qio_channel_socket_init
,
789 .instance_finalize
= qio_channel_socket_finalize
,
790 .class_init
= qio_channel_socket_class_init
,
793 static void qio_channel_socket_register_types(void)
795 type_register_static(&qio_channel_socket_info
);
798 type_init(qio_channel_socket_register_types
);