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 "qapi/error.h"
22 #include "qapi/qapi-visit-sockets.h"
23 #include "qemu/module.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
,
204 trace_qio_channel_socket_listen_sync(ioc
, addr
, num
);
205 fd
= socket_listen(addr
, num
, errp
);
207 trace_qio_channel_socket_listen_fail(ioc
);
211 trace_qio_channel_socket_listen_complete(ioc
, fd
);
212 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
216 qio_channel_set_feature(QIO_CHANNEL(ioc
), QIO_CHANNEL_FEATURE_LISTEN
);
222 struct QIOChannelListenWorkerData
{
224 int num
; /* amount of expected connections */
227 static void qio_channel_listen_worker_free(gpointer opaque
)
229 struct QIOChannelListenWorkerData
*data
= opaque
;
231 qapi_free_SocketAddress(data
->addr
);
235 static void qio_channel_socket_listen_worker(QIOTask
*task
,
238 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
239 struct QIOChannelListenWorkerData
*data
= opaque
;
242 qio_channel_socket_listen_sync(ioc
, data
->addr
, data
->num
, &err
);
244 qio_task_set_error(task
, err
);
248 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
251 QIOTaskFunc callback
,
253 GDestroyNotify destroy
,
254 GMainContext
*context
)
256 QIOTask
*task
= qio_task_new(
257 OBJECT(ioc
), callback
, opaque
, destroy
);
258 struct QIOChannelListenWorkerData
*data
;
260 data
= g_new0(struct QIOChannelListenWorkerData
, 1);
261 data
->addr
= QAPI_CLONE(SocketAddress
, addr
);
264 /* socket_listen() blocks in DNS lookups, so we must use a thread */
265 trace_qio_channel_socket_listen_async(ioc
, addr
, num
);
266 qio_task_run_in_thread(task
,
267 qio_channel_socket_listen_worker
,
269 qio_channel_listen_worker_free
,
274 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
275 SocketAddress
*localAddr
,
276 SocketAddress
*remoteAddr
,
281 trace_qio_channel_socket_dgram_sync(ioc
, localAddr
, remoteAddr
);
282 fd
= socket_dgram(remoteAddr
, localAddr
, errp
);
284 trace_qio_channel_socket_dgram_fail(ioc
);
288 trace_qio_channel_socket_dgram_complete(ioc
, fd
);
289 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
298 struct QIOChannelSocketDGramWorkerData
{
299 SocketAddress
*localAddr
;
300 SocketAddress
*remoteAddr
;
304 static void qio_channel_socket_dgram_worker_free(gpointer opaque
)
306 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
307 qapi_free_SocketAddress(data
->localAddr
);
308 qapi_free_SocketAddress(data
->remoteAddr
);
312 static void qio_channel_socket_dgram_worker(QIOTask
*task
,
315 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
316 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
319 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
320 qio_channel_socket_dgram_sync(ioc
, data
->localAddr
,
321 data
->remoteAddr
, &err
);
323 qio_task_set_error(task
, err
);
327 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
328 SocketAddress
*localAddr
,
329 SocketAddress
*remoteAddr
,
330 QIOTaskFunc callback
,
332 GDestroyNotify destroy
,
333 GMainContext
*context
)
335 QIOTask
*task
= qio_task_new(
336 OBJECT(ioc
), callback
, opaque
, destroy
);
337 struct QIOChannelSocketDGramWorkerData
*data
= g_new0(
338 struct QIOChannelSocketDGramWorkerData
, 1);
340 data
->localAddr
= QAPI_CLONE(SocketAddress
, localAddr
);
341 data
->remoteAddr
= QAPI_CLONE(SocketAddress
, remoteAddr
);
343 trace_qio_channel_socket_dgram_async(ioc
, localAddr
, remoteAddr
);
344 qio_task_run_in_thread(task
,
345 qio_channel_socket_dgram_worker
,
347 qio_channel_socket_dgram_worker_free
,
353 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
356 QIOChannelSocket
*cioc
;
358 cioc
= qio_channel_socket_new();
359 cioc
->remoteAddrLen
= sizeof(ioc
->remoteAddr
);
360 cioc
->localAddrLen
= sizeof(ioc
->localAddr
);
363 trace_qio_channel_socket_accept(ioc
);
364 cioc
->fd
= qemu_accept(ioc
->fd
, (struct sockaddr
*)&cioc
->remoteAddr
,
365 &cioc
->remoteAddrLen
);
367 if (errno
== EINTR
) {
370 error_setg_errno(errp
, errno
, "Unable to accept connection");
371 trace_qio_channel_socket_accept_fail(ioc
);
375 if (getsockname(cioc
->fd
, (struct sockaddr
*)&cioc
->localAddr
,
376 &cioc
->localAddrLen
) < 0) {
377 error_setg_errno(errp
, errno
,
378 "Unable to query local socket address");
383 if (cioc
->localAddr
.ss_family
== AF_UNIX
) {
384 QIOChannel
*ioc_local
= QIO_CHANNEL(cioc
);
385 qio_channel_set_feature(ioc_local
, QIO_CHANNEL_FEATURE_FD_PASS
);
389 trace_qio_channel_socket_accept_complete(ioc
, cioc
, cioc
->fd
);
393 object_unref(OBJECT(cioc
));
397 static void qio_channel_socket_init(Object
*obj
)
399 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
403 static void qio_channel_socket_finalize(Object
*obj
)
405 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
408 QIOChannel
*ioc_local
= QIO_CHANNEL(ioc
);
409 if (qio_channel_has_feature(ioc_local
, QIO_CHANNEL_FEATURE_LISTEN
)) {
412 socket_listen_cleanup(ioc
->fd
, &err
);
414 error_report_err(err
);
419 WSAEventSelect(ioc
->fd
, NULL
, 0);
421 closesocket(ioc
->fd
);
428 static void qio_channel_socket_copy_fds(struct msghdr
*msg
,
429 int **fds
, size_t *nfds
)
431 struct cmsghdr
*cmsg
;
436 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
440 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
441 cmsg
->cmsg_level
!= SOL_SOCKET
||
442 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
446 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
452 gotfds
= fd_size
/ sizeof(int);
453 *fds
= g_renew(int, *fds
, *nfds
+ gotfds
);
454 memcpy(*fds
+ *nfds
, CMSG_DATA(cmsg
), fd_size
);
456 for (i
= 0; i
< gotfds
; i
++) {
457 int fd
= (*fds
)[*nfds
+ i
];
462 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
465 #ifndef MSG_CMSG_CLOEXEC
466 qemu_set_cloexec(fd
);
474 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
475 const struct iovec
*iov
,
481 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
483 struct msghdr msg
= { NULL
, };
484 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
487 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
489 msg
.msg_iov
= (struct iovec
*)iov
;
490 msg
.msg_iovlen
= niov
;
492 msg
.msg_control
= control
;
493 msg
.msg_controllen
= sizeof(control
);
494 #ifdef MSG_CMSG_CLOEXEC
495 sflags
|= MSG_CMSG_CLOEXEC
;
501 ret
= recvmsg(sioc
->fd
, &msg
, sflags
);
503 if (errno
== EAGAIN
) {
504 return QIO_CHANNEL_ERR_BLOCK
;
506 if (errno
== EINTR
) {
510 error_setg_errno(errp
, errno
,
511 "Unable to read from socket");
516 qio_channel_socket_copy_fds(&msg
, fds
, nfds
);
522 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
523 const struct iovec
*iov
,
529 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
531 struct msghdr msg
= { NULL
, };
532 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
533 size_t fdsize
= sizeof(int) * nfds
;
534 struct cmsghdr
*cmsg
;
536 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
538 msg
.msg_iov
= (struct iovec
*)iov
;
539 msg
.msg_iovlen
= niov
;
542 if (nfds
> SOCKET_MAX_FDS
) {
543 error_setg_errno(errp
, EINVAL
,
544 "Only %d FDs can be sent, got %zu",
545 SOCKET_MAX_FDS
, nfds
);
549 msg
.msg_control
= control
;
550 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfds
);
552 cmsg
= CMSG_FIRSTHDR(&msg
);
553 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
554 cmsg
->cmsg_level
= SOL_SOCKET
;
555 cmsg
->cmsg_type
= SCM_RIGHTS
;
556 memcpy(CMSG_DATA(cmsg
), fds
, fdsize
);
560 ret
= sendmsg(sioc
->fd
, &msg
, 0);
562 if (errno
== EAGAIN
) {
563 return QIO_CHANNEL_ERR_BLOCK
;
565 if (errno
== EINTR
) {
568 error_setg_errno(errp
, errno
,
569 "Unable to write to socket");
575 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
576 const struct iovec
*iov
,
582 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
586 for (i
= 0; i
< niov
; i
++) {
594 if (errno
== EAGAIN
) {
598 return QIO_CHANNEL_ERR_BLOCK
;
600 } else if (errno
== EINTR
) {
603 error_setg_errno(errp
, errno
,
604 "Unable to read from socket");
609 if (ret
< iov
[i
].iov_len
) {
617 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
618 const struct iovec
*iov
,
624 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
628 for (i
= 0; i
< niov
; i
++) {
636 if (errno
== EAGAIN
) {
640 return QIO_CHANNEL_ERR_BLOCK
;
642 } else if (errno
== EINTR
) {
645 error_setg_errno(errp
, errno
,
646 "Unable to write to socket");
651 if (ret
< iov
[i
].iov_len
) {
661 qio_channel_socket_set_blocking(QIOChannel
*ioc
,
665 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
668 qemu_set_block(sioc
->fd
);
670 qemu_set_nonblock(sioc
->fd
);
677 qio_channel_socket_set_delay(QIOChannel
*ioc
,
680 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
681 int v
= enabled
? 0 : 1;
684 IPPROTO_TCP
, TCP_NODELAY
,
690 qio_channel_socket_set_cork(QIOChannel
*ioc
,
693 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
694 int v
= enabled
? 1 : 0;
696 socket_set_cork(sioc
->fd
, v
);
701 qio_channel_socket_close(QIOChannel
*ioc
,
704 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
708 if (sioc
->fd
!= -1) {
710 WSAEventSelect(sioc
->fd
, NULL
, 0);
712 if (qio_channel_has_feature(ioc
, QIO_CHANNEL_FEATURE_LISTEN
)) {
713 socket_listen_cleanup(sioc
->fd
, errp
);
716 if (closesocket(sioc
->fd
) < 0) {
718 error_setg_errno(&err
, errno
, "Unable to close socket");
719 error_propagate(errp
, err
);
728 qio_channel_socket_shutdown(QIOChannel
*ioc
,
729 QIOChannelShutdown how
,
732 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
736 case QIO_CHANNEL_SHUTDOWN_READ
:
739 case QIO_CHANNEL_SHUTDOWN_WRITE
:
742 case QIO_CHANNEL_SHUTDOWN_BOTH
:
748 if (shutdown(sioc
->fd
, sockhow
) < 0) {
749 error_setg_errno(errp
, errno
,
750 "Unable to shutdown socket");
756 static void qio_channel_socket_set_aio_fd_handler(QIOChannel
*ioc
,
762 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
763 aio_set_fd_handler(ctx
, sioc
->fd
, false,
764 io_read
, io_write
, NULL
, NULL
, opaque
);
767 static GSource
*qio_channel_socket_create_watch(QIOChannel
*ioc
,
768 GIOCondition condition
)
770 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
771 return qio_channel_create_socket_watch(ioc
,
776 static void qio_channel_socket_class_init(ObjectClass
*klass
,
777 void *class_data G_GNUC_UNUSED
)
779 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
781 ioc_klass
->io_writev
= qio_channel_socket_writev
;
782 ioc_klass
->io_readv
= qio_channel_socket_readv
;
783 ioc_klass
->io_set_blocking
= qio_channel_socket_set_blocking
;
784 ioc_klass
->io_close
= qio_channel_socket_close
;
785 ioc_klass
->io_shutdown
= qio_channel_socket_shutdown
;
786 ioc_klass
->io_set_cork
= qio_channel_socket_set_cork
;
787 ioc_klass
->io_set_delay
= qio_channel_socket_set_delay
;
788 ioc_klass
->io_create_watch
= qio_channel_socket_create_watch
;
789 ioc_klass
->io_set_aio_fd_handler
= qio_channel_socket_set_aio_fd_handler
;
792 static const TypeInfo qio_channel_socket_info
= {
793 .parent
= TYPE_QIO_CHANNEL
,
794 .name
= TYPE_QIO_CHANNEL_SOCKET
,
795 .instance_size
= sizeof(QIOChannelSocket
),
796 .instance_init
= qio_channel_socket_init
,
797 .instance_finalize
= qio_channel_socket_finalize
,
798 .class_init
= qio_channel_socket_class_init
,
801 static void qio_channel_socket_register_types(void)
803 type_register_static(&qio_channel_socket_info
);
806 type_init(qio_channel_socket_register_types
);