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.1 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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qemu/module.h"
25 #include "io/channel-socket.h"
26 #include "io/channel-watch.h"
28 #include "qapi/clone-visitor.h"
30 #define SOCKET_MAX_FDS 16
33 qio_channel_socket_get_local_address(QIOChannelSocket
*ioc
,
36 return socket_sockaddr_to_address(&ioc
->localAddr
,
42 qio_channel_socket_get_remote_address(QIOChannelSocket
*ioc
,
45 return socket_sockaddr_to_address(&ioc
->remoteAddr
,
51 qio_channel_socket_new(void)
53 QIOChannelSocket
*sioc
;
56 sioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
59 ioc
= QIO_CHANNEL(sioc
);
60 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
63 ioc
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
66 trace_qio_channel_socket_new(sioc
);
73 qio_channel_socket_set_fd(QIOChannelSocket
*sioc
,
78 error_setg(errp
, "Socket is already open");
83 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
84 sioc
->localAddrLen
= sizeof(sioc
->localAddr
);
87 if (getpeername(fd
, (struct sockaddr
*)&sioc
->remoteAddr
,
88 &sioc
->remoteAddrLen
) < 0) {
89 if (errno
== ENOTCONN
) {
90 memset(&sioc
->remoteAddr
, 0, sizeof(sioc
->remoteAddr
));
91 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
93 error_setg_errno(errp
, errno
,
94 "Unable to query remote socket address");
99 if (getsockname(fd
, (struct sockaddr
*)&sioc
->localAddr
,
100 &sioc
->localAddrLen
) < 0) {
101 error_setg_errno(errp
, errno
,
102 "Unable to query local socket address");
107 if (sioc
->localAddr
.ss_family
== AF_UNIX
) {
108 QIOChannel
*ioc
= QIO_CHANNEL(sioc
);
109 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_FD_PASS
);
116 sioc
->fd
= -1; /* Let the caller close FD on failure */
121 qio_channel_socket_new_fd(int fd
,
124 QIOChannelSocket
*ioc
;
126 ioc
= qio_channel_socket_new();
127 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
128 object_unref(OBJECT(ioc
));
132 trace_qio_channel_socket_new_fd(ioc
, fd
);
138 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
144 trace_qio_channel_socket_connect_sync(ioc
, addr
);
145 fd
= socket_connect(addr
, errp
);
147 trace_qio_channel_socket_connect_fail(ioc
);
151 trace_qio_channel_socket_connect_complete(ioc
, fd
);
152 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
161 static void qio_channel_socket_connect_worker(QIOTask
*task
,
164 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
165 SocketAddress
*addr
= opaque
;
168 qio_channel_socket_connect_sync(ioc
, addr
, &err
);
170 qio_task_set_error(task
, err
);
174 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
176 QIOTaskFunc callback
,
178 GDestroyNotify destroy
,
179 GMainContext
*context
)
181 QIOTask
*task
= qio_task_new(
182 OBJECT(ioc
), callback
, opaque
, destroy
);
183 SocketAddress
*addrCopy
;
185 addrCopy
= QAPI_CLONE(SocketAddress
, addr
);
187 /* socket_connect() does a non-blocking connect(), but it
188 * still blocks in DNS lookups, so we must use a thread */
189 trace_qio_channel_socket_connect_async(ioc
, addr
);
190 qio_task_run_in_thread(task
,
191 qio_channel_socket_connect_worker
,
193 (GDestroyNotify
)qapi_free_SocketAddress
,
198 int qio_channel_socket_listen_sync(QIOChannelSocket
*ioc
,
205 trace_qio_channel_socket_listen_sync(ioc
, addr
, num
);
206 fd
= socket_listen(addr
, num
, errp
);
208 trace_qio_channel_socket_listen_fail(ioc
);
212 trace_qio_channel_socket_listen_complete(ioc
, fd
);
213 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
217 qio_channel_set_feature(QIO_CHANNEL(ioc
), QIO_CHANNEL_FEATURE_LISTEN
);
223 struct QIOChannelListenWorkerData
{
225 int num
; /* amount of expected connections */
228 static void qio_channel_listen_worker_free(gpointer opaque
)
230 struct QIOChannelListenWorkerData
*data
= opaque
;
232 qapi_free_SocketAddress(data
->addr
);
236 static void qio_channel_socket_listen_worker(QIOTask
*task
,
239 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
240 struct QIOChannelListenWorkerData
*data
= opaque
;
243 qio_channel_socket_listen_sync(ioc
, data
->addr
, data
->num
, &err
);
245 qio_task_set_error(task
, err
);
249 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
252 QIOTaskFunc callback
,
254 GDestroyNotify destroy
,
255 GMainContext
*context
)
257 QIOTask
*task
= qio_task_new(
258 OBJECT(ioc
), callback
, opaque
, destroy
);
259 struct QIOChannelListenWorkerData
*data
;
261 data
= g_new0(struct QIOChannelListenWorkerData
, 1);
262 data
->addr
= QAPI_CLONE(SocketAddress
, addr
);
265 /* socket_listen() blocks in DNS lookups, so we must use a thread */
266 trace_qio_channel_socket_listen_async(ioc
, addr
, num
);
267 qio_task_run_in_thread(task
,
268 qio_channel_socket_listen_worker
,
270 qio_channel_listen_worker_free
,
275 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
276 SocketAddress
*localAddr
,
277 SocketAddress
*remoteAddr
,
282 trace_qio_channel_socket_dgram_sync(ioc
, localAddr
, remoteAddr
);
283 fd
= socket_dgram(remoteAddr
, localAddr
, errp
);
285 trace_qio_channel_socket_dgram_fail(ioc
);
289 trace_qio_channel_socket_dgram_complete(ioc
, fd
);
290 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
299 struct QIOChannelSocketDGramWorkerData
{
300 SocketAddress
*localAddr
;
301 SocketAddress
*remoteAddr
;
305 static void qio_channel_socket_dgram_worker_free(gpointer opaque
)
307 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
308 qapi_free_SocketAddress(data
->localAddr
);
309 qapi_free_SocketAddress(data
->remoteAddr
);
313 static void qio_channel_socket_dgram_worker(QIOTask
*task
,
316 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
317 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
320 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
321 qio_channel_socket_dgram_sync(ioc
, data
->localAddr
,
322 data
->remoteAddr
, &err
);
324 qio_task_set_error(task
, err
);
328 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
329 SocketAddress
*localAddr
,
330 SocketAddress
*remoteAddr
,
331 QIOTaskFunc callback
,
333 GDestroyNotify destroy
,
334 GMainContext
*context
)
336 QIOTask
*task
= qio_task_new(
337 OBJECT(ioc
), callback
, opaque
, destroy
);
338 struct QIOChannelSocketDGramWorkerData
*data
= g_new0(
339 struct QIOChannelSocketDGramWorkerData
, 1);
341 data
->localAddr
= QAPI_CLONE(SocketAddress
, localAddr
);
342 data
->remoteAddr
= QAPI_CLONE(SocketAddress
, remoteAddr
);
344 trace_qio_channel_socket_dgram_async(ioc
, localAddr
, remoteAddr
);
345 qio_task_run_in_thread(task
,
346 qio_channel_socket_dgram_worker
,
348 qio_channel_socket_dgram_worker_free
,
354 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
357 QIOChannelSocket
*cioc
;
359 cioc
= qio_channel_socket_new();
360 cioc
->remoteAddrLen
= sizeof(ioc
->remoteAddr
);
361 cioc
->localAddrLen
= sizeof(ioc
->localAddr
);
364 trace_qio_channel_socket_accept(ioc
);
365 cioc
->fd
= qemu_accept(ioc
->fd
, (struct sockaddr
*)&cioc
->remoteAddr
,
366 &cioc
->remoteAddrLen
);
368 if (errno
== EINTR
) {
371 error_setg_errno(errp
, errno
, "Unable to accept connection");
372 trace_qio_channel_socket_accept_fail(ioc
);
376 if (getsockname(cioc
->fd
, (struct sockaddr
*)&cioc
->localAddr
,
377 &cioc
->localAddrLen
) < 0) {
378 error_setg_errno(errp
, errno
,
379 "Unable to query local socket address");
384 if (cioc
->localAddr
.ss_family
== AF_UNIX
) {
385 QIOChannel
*ioc_local
= QIO_CHANNEL(cioc
);
386 qio_channel_set_feature(ioc_local
, QIO_CHANNEL_FEATURE_FD_PASS
);
390 trace_qio_channel_socket_accept_complete(ioc
, cioc
, cioc
->fd
);
394 object_unref(OBJECT(cioc
));
398 static void qio_channel_socket_init(Object
*obj
)
400 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
404 static void qio_channel_socket_finalize(Object
*obj
)
406 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
409 QIOChannel
*ioc_local
= QIO_CHANNEL(ioc
);
410 if (qio_channel_has_feature(ioc_local
, QIO_CHANNEL_FEATURE_LISTEN
)) {
413 socket_listen_cleanup(ioc
->fd
, &err
);
415 error_report_err(err
);
420 WSAEventSelect(ioc
->fd
, NULL
, 0);
422 closesocket(ioc
->fd
);
429 static void qio_channel_socket_copy_fds(struct msghdr
*msg
,
430 int **fds
, size_t *nfds
)
432 struct cmsghdr
*cmsg
;
437 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
441 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
442 cmsg
->cmsg_level
!= SOL_SOCKET
||
443 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
447 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
453 gotfds
= fd_size
/ sizeof(int);
454 *fds
= g_renew(int, *fds
, *nfds
+ gotfds
);
455 memcpy(*fds
+ *nfds
, CMSG_DATA(cmsg
), fd_size
);
457 for (i
= 0; i
< gotfds
; i
++) {
458 int fd
= (*fds
)[*nfds
+ i
];
463 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
466 #ifndef MSG_CMSG_CLOEXEC
467 qemu_set_cloexec(fd
);
475 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
476 const struct iovec
*iov
,
482 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
484 struct msghdr msg
= { NULL
, };
485 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
488 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
490 msg
.msg_iov
= (struct iovec
*)iov
;
491 msg
.msg_iovlen
= niov
;
493 msg
.msg_control
= control
;
494 msg
.msg_controllen
= sizeof(control
);
495 #ifdef MSG_CMSG_CLOEXEC
496 sflags
|= MSG_CMSG_CLOEXEC
;
502 ret
= recvmsg(sioc
->fd
, &msg
, sflags
);
504 if (errno
== EAGAIN
) {
505 return QIO_CHANNEL_ERR_BLOCK
;
507 if (errno
== EINTR
) {
511 error_setg_errno(errp
, errno
,
512 "Unable to read from socket");
517 qio_channel_socket_copy_fds(&msg
, fds
, nfds
);
523 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
524 const struct iovec
*iov
,
530 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
532 struct msghdr msg
= { NULL
, };
533 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
534 size_t fdsize
= sizeof(int) * nfds
;
535 struct cmsghdr
*cmsg
;
537 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
539 msg
.msg_iov
= (struct iovec
*)iov
;
540 msg
.msg_iovlen
= niov
;
543 if (nfds
> SOCKET_MAX_FDS
) {
544 error_setg_errno(errp
, EINVAL
,
545 "Only %d FDs can be sent, got %zu",
546 SOCKET_MAX_FDS
, nfds
);
550 msg
.msg_control
= control
;
551 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfds
);
553 cmsg
= CMSG_FIRSTHDR(&msg
);
554 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
555 cmsg
->cmsg_level
= SOL_SOCKET
;
556 cmsg
->cmsg_type
= SCM_RIGHTS
;
557 memcpy(CMSG_DATA(cmsg
), fds
, fdsize
);
561 ret
= sendmsg(sioc
->fd
, &msg
, 0);
563 if (errno
== EAGAIN
) {
564 return QIO_CHANNEL_ERR_BLOCK
;
566 if (errno
== EINTR
) {
569 error_setg_errno(errp
, errno
,
570 "Unable to write to socket");
576 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
577 const struct iovec
*iov
,
583 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
587 for (i
= 0; i
< niov
; i
++) {
595 if (errno
== EAGAIN
) {
599 return QIO_CHANNEL_ERR_BLOCK
;
601 } else if (errno
== EINTR
) {
604 error_setg_errno(errp
, errno
,
605 "Unable to read from socket");
610 if (ret
< iov
[i
].iov_len
) {
618 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
619 const struct iovec
*iov
,
625 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
629 for (i
= 0; i
< niov
; i
++) {
637 if (errno
== EAGAIN
) {
641 return QIO_CHANNEL_ERR_BLOCK
;
643 } else if (errno
== EINTR
) {
646 error_setg_errno(errp
, errno
,
647 "Unable to write to socket");
652 if (ret
< iov
[i
].iov_len
) {
662 qio_channel_socket_set_blocking(QIOChannel
*ioc
,
666 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
669 qemu_set_block(sioc
->fd
);
671 qemu_set_nonblock(sioc
->fd
);
678 qio_channel_socket_set_delay(QIOChannel
*ioc
,
681 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
682 int v
= enabled
? 0 : 1;
684 qemu_setsockopt(sioc
->fd
,
685 IPPROTO_TCP
, TCP_NODELAY
,
691 qio_channel_socket_set_cork(QIOChannel
*ioc
,
694 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
695 int v
= enabled
? 1 : 0;
697 socket_set_cork(sioc
->fd
, v
);
702 qio_channel_socket_close(QIOChannel
*ioc
,
705 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
709 if (sioc
->fd
!= -1) {
711 WSAEventSelect(sioc
->fd
, NULL
, 0);
713 if (qio_channel_has_feature(ioc
, QIO_CHANNEL_FEATURE_LISTEN
)) {
714 socket_listen_cleanup(sioc
->fd
, errp
);
717 if (closesocket(sioc
->fd
) < 0) {
719 error_setg_errno(&err
, errno
, "Unable to close socket");
720 error_propagate(errp
, err
);
729 qio_channel_socket_shutdown(QIOChannel
*ioc
,
730 QIOChannelShutdown how
,
733 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
737 case QIO_CHANNEL_SHUTDOWN_READ
:
740 case QIO_CHANNEL_SHUTDOWN_WRITE
:
743 case QIO_CHANNEL_SHUTDOWN_BOTH
:
749 if (shutdown(sioc
->fd
, sockhow
) < 0) {
750 error_setg_errno(errp
, errno
,
751 "Unable to shutdown socket");
757 static void qio_channel_socket_set_aio_fd_handler(QIOChannel
*ioc
,
763 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
764 aio_set_fd_handler(ctx
, sioc
->fd
, false,
765 io_read
, io_write
, NULL
, NULL
, opaque
);
768 static GSource
*qio_channel_socket_create_watch(QIOChannel
*ioc
,
769 GIOCondition condition
)
771 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
772 return qio_channel_create_socket_watch(ioc
,
777 static void qio_channel_socket_class_init(ObjectClass
*klass
,
778 void *class_data G_GNUC_UNUSED
)
780 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
782 ioc_klass
->io_writev
= qio_channel_socket_writev
;
783 ioc_klass
->io_readv
= qio_channel_socket_readv
;
784 ioc_klass
->io_set_blocking
= qio_channel_socket_set_blocking
;
785 ioc_klass
->io_close
= qio_channel_socket_close
;
786 ioc_klass
->io_shutdown
= qio_channel_socket_shutdown
;
787 ioc_klass
->io_set_cork
= qio_channel_socket_set_cork
;
788 ioc_klass
->io_set_delay
= qio_channel_socket_set_delay
;
789 ioc_klass
->io_create_watch
= qio_channel_socket_create_watch
;
790 ioc_klass
->io_set_aio_fd_handler
= qio_channel_socket_set_aio_fd_handler
;
793 static const TypeInfo qio_channel_socket_info
= {
794 .parent
= TYPE_QIO_CHANNEL
,
795 .name
= TYPE_QIO_CHANNEL_SOCKET
,
796 .instance_size
= sizeof(QIOChannelSocket
),
797 .instance_init
= qio_channel_socket_init
,
798 .instance_finalize
= qio_channel_socket_finalize
,
799 .class_init
= qio_channel_socket_class_init
,
802 static void qio_channel_socket_register_types(void)
804 type_register_static(&qio_channel_socket_info
);
807 type_init(qio_channel_socket_register_types
);