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"
27 #define SOCKET_MAX_FDS 16
30 qio_channel_socket_get_local_address(QIOChannelSocket
*ioc
,
33 return socket_sockaddr_to_address(&ioc
->localAddr
,
39 qio_channel_socket_get_remote_address(QIOChannelSocket
*ioc
,
42 return socket_sockaddr_to_address(&ioc
->remoteAddr
,
48 qio_channel_socket_new(void)
50 QIOChannelSocket
*sioc
;
53 sioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
56 ioc
= QIO_CHANNEL(sioc
);
57 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN
);
60 ioc
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
63 trace_qio_channel_socket_new(sioc
);
70 qio_channel_socket_set_fd(QIOChannelSocket
*sioc
,
75 socklen_t len
= sizeof(val
);
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 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_FD_PASS
);
112 if (getsockopt(fd
, SOL_SOCKET
, SO_ACCEPTCONN
, &val
, &len
) == 0 && val
) {
113 QIOChannel
*ioc
= QIO_CHANNEL(sioc
);
114 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_LISTEN
);
120 sioc
->fd
= -1; /* Let the caller close FD on failure */
125 qio_channel_socket_new_fd(int fd
,
128 QIOChannelSocket
*ioc
;
130 ioc
= qio_channel_socket_new();
131 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
132 object_unref(OBJECT(ioc
));
136 trace_qio_channel_socket_new_fd(ioc
, fd
);
142 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
148 trace_qio_channel_socket_connect_sync(ioc
, addr
);
149 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
151 trace_qio_channel_socket_connect_fail(ioc
);
155 trace_qio_channel_socket_connect_complete(ioc
, fd
);
156 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
165 static int qio_channel_socket_connect_worker(QIOTask
*task
,
169 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
170 SocketAddress
*addr
= opaque
;
173 ret
= qio_channel_socket_connect_sync(ioc
,
177 object_unref(OBJECT(ioc
));
182 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
184 QIOTaskFunc callback
,
186 GDestroyNotify destroy
)
188 QIOTask
*task
= qio_task_new(
189 OBJECT(ioc
), callback
, opaque
, destroy
);
190 SocketAddress
*addrCopy
;
192 qapi_copy_SocketAddress(&addrCopy
, addr
);
194 /* socket_connect() does a non-blocking connect(), but it
195 * still blocks in DNS lookups, so we must use a thread */
196 trace_qio_channel_socket_connect_async(ioc
, addr
);
197 qio_task_run_in_thread(task
,
198 qio_channel_socket_connect_worker
,
200 (GDestroyNotify
)qapi_free_SocketAddress
);
204 int qio_channel_socket_listen_sync(QIOChannelSocket
*ioc
,
210 trace_qio_channel_socket_listen_sync(ioc
, addr
);
211 fd
= socket_listen(addr
, errp
);
213 trace_qio_channel_socket_listen_fail(ioc
);
217 trace_qio_channel_socket_listen_complete(ioc
, fd
);
218 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
227 static int qio_channel_socket_listen_worker(QIOTask
*task
,
231 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
232 SocketAddress
*addr
= opaque
;
235 ret
= qio_channel_socket_listen_sync(ioc
,
239 object_unref(OBJECT(ioc
));
244 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
246 QIOTaskFunc callback
,
248 GDestroyNotify destroy
)
250 QIOTask
*task
= qio_task_new(
251 OBJECT(ioc
), callback
, opaque
, destroy
);
252 SocketAddress
*addrCopy
;
254 qapi_copy_SocketAddress(&addrCopy
, addr
);
256 /* socket_listen() blocks in DNS lookups, so we must use a thread */
257 trace_qio_channel_socket_listen_async(ioc
, addr
);
258 qio_task_run_in_thread(task
,
259 qio_channel_socket_listen_worker
,
261 (GDestroyNotify
)qapi_free_SocketAddress
);
265 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
266 SocketAddress
*localAddr
,
267 SocketAddress
*remoteAddr
,
272 trace_qio_channel_socket_dgram_sync(ioc
, localAddr
, remoteAddr
);
273 fd
= socket_dgram(remoteAddr
, localAddr
, errp
);
275 trace_qio_channel_socket_dgram_fail(ioc
);
279 trace_qio_channel_socket_dgram_complete(ioc
, fd
);
280 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
289 struct QIOChannelSocketDGramWorkerData
{
290 SocketAddress
*localAddr
;
291 SocketAddress
*remoteAddr
;
295 static void qio_channel_socket_dgram_worker_free(gpointer opaque
)
297 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
298 qapi_free_SocketAddress(data
->localAddr
);
299 qapi_free_SocketAddress(data
->remoteAddr
);
303 static int qio_channel_socket_dgram_worker(QIOTask
*task
,
307 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
308 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
311 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
312 ret
= qio_channel_socket_dgram_sync(ioc
,
317 object_unref(OBJECT(ioc
));
322 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
323 SocketAddress
*localAddr
,
324 SocketAddress
*remoteAddr
,
325 QIOTaskFunc callback
,
327 GDestroyNotify destroy
)
329 QIOTask
*task
= qio_task_new(
330 OBJECT(ioc
), callback
, opaque
, destroy
);
331 struct QIOChannelSocketDGramWorkerData
*data
= g_new0(
332 struct QIOChannelSocketDGramWorkerData
, 1);
334 qapi_copy_SocketAddress(&data
->localAddr
, localAddr
);
335 qapi_copy_SocketAddress(&data
->remoteAddr
, remoteAddr
);
337 trace_qio_channel_socket_dgram_async(ioc
, localAddr
, remoteAddr
);
338 qio_task_run_in_thread(task
,
339 qio_channel_socket_dgram_worker
,
341 qio_channel_socket_dgram_worker_free
);
346 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
349 QIOChannelSocket
*cioc
;
351 cioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
353 cioc
->remoteAddrLen
= sizeof(ioc
->remoteAddr
);
354 cioc
->localAddrLen
= sizeof(ioc
->localAddr
);
357 QIO_CHANNEL(cioc
)->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
362 trace_qio_channel_socket_accept(ioc
);
363 cioc
->fd
= qemu_accept(ioc
->fd
, (struct sockaddr
*)&cioc
->remoteAddr
,
364 &cioc
->remoteAddrLen
);
366 trace_qio_channel_socket_accept_fail(ioc
);
367 if (errno
== EINTR
) {
373 if (getsockname(cioc
->fd
, (struct sockaddr
*)&cioc
->localAddr
,
374 &cioc
->localAddrLen
) < 0) {
375 error_setg_errno(errp
, errno
,
376 "Unable to query local socket address");
381 if (cioc
->localAddr
.ss_family
== AF_UNIX
) {
382 QIO_CHANNEL(cioc
)->features
|= (1 << QIO_CHANNEL_FEATURE_FD_PASS
);
386 trace_qio_channel_socket_accept_complete(ioc
, cioc
, cioc
->fd
);
390 object_unref(OBJECT(cioc
));
394 static void qio_channel_socket_init(Object
*obj
)
396 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
400 static void qio_channel_socket_finalize(Object
*obj
)
402 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
405 WSAEventSelect(ioc
->fd
, NULL
, 0);
407 closesocket(ioc
->fd
);
414 static void qio_channel_socket_copy_fds(struct msghdr
*msg
,
415 int **fds
, size_t *nfds
)
417 struct cmsghdr
*cmsg
;
422 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
426 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
427 cmsg
->cmsg_level
!= SOL_SOCKET
||
428 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
432 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
438 gotfds
= fd_size
/ sizeof(int);
439 *fds
= g_renew(int, *fds
, *nfds
+ gotfds
);
440 memcpy(*fds
+ *nfds
, CMSG_DATA(cmsg
), fd_size
);
442 for (i
= 0; i
< gotfds
; i
++) {
443 int fd
= (*fds
)[*nfds
+ i
];
448 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
451 #ifndef MSG_CMSG_CLOEXEC
452 qemu_set_cloexec(fd
);
460 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
461 const struct iovec
*iov
,
467 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
469 struct msghdr msg
= { NULL
, };
470 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
473 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
475 #ifdef MSG_CMSG_CLOEXEC
476 sflags
|= MSG_CMSG_CLOEXEC
;
479 msg
.msg_iov
= (struct iovec
*)iov
;
480 msg
.msg_iovlen
= niov
;
482 msg
.msg_control
= control
;
483 msg
.msg_controllen
= sizeof(control
);
487 ret
= recvmsg(sioc
->fd
, &msg
, sflags
);
489 if (errno
== EAGAIN
) {
490 return QIO_CHANNEL_ERR_BLOCK
;
492 if (errno
== EINTR
) {
496 error_setg_errno(errp
, errno
,
497 "Unable to read from socket");
502 qio_channel_socket_copy_fds(&msg
, fds
, nfds
);
508 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
509 const struct iovec
*iov
,
515 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
517 struct msghdr msg
= { NULL
, };
518 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
519 size_t fdsize
= sizeof(int) * nfds
;
520 struct cmsghdr
*cmsg
;
522 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
524 msg
.msg_iov
= (struct iovec
*)iov
;
525 msg
.msg_iovlen
= niov
;
528 if (nfds
> SOCKET_MAX_FDS
) {
529 error_setg_errno(errp
, EINVAL
,
530 "Only %d FDs can be sent, got %zu",
531 SOCKET_MAX_FDS
, nfds
);
535 msg
.msg_control
= control
;
536 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfds
);
538 cmsg
= CMSG_FIRSTHDR(&msg
);
539 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
540 cmsg
->cmsg_level
= SOL_SOCKET
;
541 cmsg
->cmsg_type
= SCM_RIGHTS
;
542 memcpy(CMSG_DATA(cmsg
), fds
, fdsize
);
546 ret
= sendmsg(sioc
->fd
, &msg
, 0);
548 if (errno
== EAGAIN
) {
549 return QIO_CHANNEL_ERR_BLOCK
;
551 if (errno
== EINTR
) {
554 error_setg_errno(errp
, errno
,
555 "Unable to write to socket");
561 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
562 const struct iovec
*iov
,
568 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
572 for (i
= 0; i
< niov
; i
++) {
580 if (errno
== EAGAIN
) {
584 return QIO_CHANNEL_ERR_BLOCK
;
586 } else if (errno
== EINTR
) {
589 error_setg_errno(errp
, errno
,
590 "Unable to read from socket");
595 if (ret
< iov
[i
].iov_len
) {
603 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
604 const struct iovec
*iov
,
610 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
614 for (i
= 0; i
< niov
; i
++) {
622 if (errno
== EAGAIN
) {
626 return QIO_CHANNEL_ERR_BLOCK
;
628 } else if (errno
== EINTR
) {
631 error_setg_errno(errp
, errno
,
632 "Unable to write to socket");
637 if (ret
< iov
[i
].iov_len
) {
647 qio_channel_socket_set_blocking(QIOChannel
*ioc
,
651 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
654 qemu_set_block(sioc
->fd
);
656 qemu_set_nonblock(sioc
->fd
);
658 WSAEventSelect(sioc
->fd
, ioc
->event
,
659 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
660 FD_CONNECT
| FD_WRITE
| FD_OOB
);
668 qio_channel_socket_set_delay(QIOChannel
*ioc
,
671 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
672 int v
= enabled
? 0 : 1;
674 qemu_setsockopt(sioc
->fd
,
675 IPPROTO_TCP
, TCP_NODELAY
,
681 qio_channel_socket_set_cork(QIOChannel
*ioc
,
684 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
685 int v
= enabled
? 1 : 0;
687 socket_set_cork(sioc
->fd
, v
);
692 qio_channel_socket_close(QIOChannel
*ioc
,
695 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
697 if (sioc
->fd
!= -1) {
699 WSAEventSelect(sioc
->fd
, NULL
, 0);
701 if (closesocket(sioc
->fd
) < 0) {
703 error_setg_errno(errp
, errno
,
704 "Unable to close socket");
713 qio_channel_socket_shutdown(QIOChannel
*ioc
,
714 QIOChannelShutdown how
,
717 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
721 case QIO_CHANNEL_SHUTDOWN_READ
:
724 case QIO_CHANNEL_SHUTDOWN_WRITE
:
727 case QIO_CHANNEL_SHUTDOWN_BOTH
:
733 if (shutdown(sioc
->fd
, sockhow
) < 0) {
734 error_setg_errno(errp
, errno
,
735 "Unable to shutdown socket");
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
,
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
;
765 static const TypeInfo qio_channel_socket_info
= {
766 .parent
= TYPE_QIO_CHANNEL
,
767 .name
= TYPE_QIO_CHANNEL_SOCKET
,
768 .instance_size
= sizeof(QIOChannelSocket
),
769 .instance_init
= qio_channel_socket_init
,
770 .instance_finalize
= qio_channel_socket_finalize
,
771 .class_init
= qio_channel_socket_class_init
,
774 static void qio_channel_socket_register_types(void)
776 type_register_static(&qio_channel_socket_info
);
779 type_init(qio_channel_socket_register_types
);