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 error_setg(errp
, "Socket is already open");
80 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
81 sioc
->localAddrLen
= sizeof(sioc
->localAddr
);
84 if (getpeername(fd
, (struct sockaddr
*)&sioc
->remoteAddr
,
85 &sioc
->remoteAddrLen
) < 0) {
86 if (errno
== ENOTCONN
) {
87 memset(&sioc
->remoteAddr
, 0, sizeof(sioc
->remoteAddr
));
88 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
90 error_setg_errno(errp
, errno
,
91 "Unable to query remote socket address");
96 if (getsockname(fd
, (struct sockaddr
*)&sioc
->localAddr
,
97 &sioc
->localAddrLen
) < 0) {
98 error_setg_errno(errp
, errno
,
99 "Unable to query local socket address");
104 if (sioc
->localAddr
.ss_family
== AF_UNIX
) {
105 QIOChannel
*ioc
= QIO_CHANNEL(sioc
);
106 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_FD_PASS
);
113 sioc
->fd
= -1; /* Let the caller close FD on failure */
118 qio_channel_socket_new_fd(int fd
,
121 QIOChannelSocket
*ioc
;
123 ioc
= qio_channel_socket_new();
124 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
125 object_unref(OBJECT(ioc
));
129 trace_qio_channel_socket_new_fd(ioc
, fd
);
135 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
141 trace_qio_channel_socket_connect_sync(ioc
, addr
);
142 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
144 trace_qio_channel_socket_connect_fail(ioc
);
148 trace_qio_channel_socket_connect_complete(ioc
, fd
);
149 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
158 static int qio_channel_socket_connect_worker(QIOTask
*task
,
162 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
163 SocketAddress
*addr
= opaque
;
166 ret
= qio_channel_socket_connect_sync(ioc
,
170 object_unref(OBJECT(ioc
));
175 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
177 QIOTaskFunc callback
,
179 GDestroyNotify destroy
)
181 QIOTask
*task
= qio_task_new(
182 OBJECT(ioc
), callback
, opaque
, destroy
);
183 SocketAddress
*addrCopy
;
185 qapi_copy_SocketAddress(&addrCopy
, 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
);
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) {
220 static int qio_channel_socket_listen_worker(QIOTask
*task
,
224 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
225 SocketAddress
*addr
= opaque
;
228 ret
= qio_channel_socket_listen_sync(ioc
,
232 object_unref(OBJECT(ioc
));
237 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
239 QIOTaskFunc callback
,
241 GDestroyNotify destroy
)
243 QIOTask
*task
= qio_task_new(
244 OBJECT(ioc
), callback
, opaque
, destroy
);
245 SocketAddress
*addrCopy
;
247 qapi_copy_SocketAddress(&addrCopy
, addr
);
249 /* socket_listen() blocks in DNS lookups, so we must use a thread */
250 trace_qio_channel_socket_listen_async(ioc
, addr
);
251 qio_task_run_in_thread(task
,
252 qio_channel_socket_listen_worker
,
254 (GDestroyNotify
)qapi_free_SocketAddress
);
258 int qio_channel_socket_dgram_sync(QIOChannelSocket
*ioc
,
259 SocketAddress
*localAddr
,
260 SocketAddress
*remoteAddr
,
265 trace_qio_channel_socket_dgram_sync(ioc
, localAddr
, remoteAddr
);
266 fd
= socket_dgram(remoteAddr
, localAddr
, errp
);
268 trace_qio_channel_socket_dgram_fail(ioc
);
272 trace_qio_channel_socket_dgram_complete(ioc
, fd
);
273 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
282 struct QIOChannelSocketDGramWorkerData
{
283 SocketAddress
*localAddr
;
284 SocketAddress
*remoteAddr
;
288 static void qio_channel_socket_dgram_worker_free(gpointer opaque
)
290 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
291 qapi_free_SocketAddress(data
->localAddr
);
292 qapi_free_SocketAddress(data
->remoteAddr
);
296 static int qio_channel_socket_dgram_worker(QIOTask
*task
,
300 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
301 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
304 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
305 ret
= qio_channel_socket_dgram_sync(ioc
,
310 object_unref(OBJECT(ioc
));
315 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
316 SocketAddress
*localAddr
,
317 SocketAddress
*remoteAddr
,
318 QIOTaskFunc callback
,
320 GDestroyNotify destroy
)
322 QIOTask
*task
= qio_task_new(
323 OBJECT(ioc
), callback
, opaque
, destroy
);
324 struct QIOChannelSocketDGramWorkerData
*data
= g_new0(
325 struct QIOChannelSocketDGramWorkerData
, 1);
327 qapi_copy_SocketAddress(&data
->localAddr
, localAddr
);
328 qapi_copy_SocketAddress(&data
->remoteAddr
, remoteAddr
);
330 trace_qio_channel_socket_dgram_async(ioc
, localAddr
, remoteAddr
);
331 qio_task_run_in_thread(task
,
332 qio_channel_socket_dgram_worker
,
334 qio_channel_socket_dgram_worker_free
);
339 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
342 QIOChannelSocket
*cioc
;
344 cioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
346 cioc
->remoteAddrLen
= sizeof(ioc
->remoteAddr
);
347 cioc
->localAddrLen
= sizeof(ioc
->localAddr
);
350 QIO_CHANNEL(cioc
)->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
355 trace_qio_channel_socket_accept(ioc
);
356 cioc
->fd
= qemu_accept(ioc
->fd
, (struct sockaddr
*)&cioc
->remoteAddr
,
357 &cioc
->remoteAddrLen
);
359 trace_qio_channel_socket_accept_fail(ioc
);
360 if (errno
== EINTR
) {
366 if (getsockname(cioc
->fd
, (struct sockaddr
*)&cioc
->localAddr
,
367 &cioc
->localAddrLen
) < 0) {
368 error_setg_errno(errp
, errno
,
369 "Unable to query local socket address");
374 if (cioc
->localAddr
.ss_family
== AF_UNIX
) {
375 QIO_CHANNEL(cioc
)->features
|= (1 << QIO_CHANNEL_FEATURE_FD_PASS
);
379 trace_qio_channel_socket_accept_complete(ioc
, cioc
, cioc
->fd
);
383 object_unref(OBJECT(cioc
));
387 static void qio_channel_socket_init(Object
*obj
)
389 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
393 static void qio_channel_socket_finalize(Object
*obj
)
395 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
398 WSAEventSelect(ioc
->fd
, NULL
, 0);
400 closesocket(ioc
->fd
);
407 static void qio_channel_socket_copy_fds(struct msghdr
*msg
,
408 int **fds
, size_t *nfds
)
410 struct cmsghdr
*cmsg
;
415 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
419 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
420 cmsg
->cmsg_level
!= SOL_SOCKET
||
421 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
425 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
431 gotfds
= fd_size
/ sizeof(int);
432 *fds
= g_renew(int, *fds
, *nfds
+ gotfds
);
433 memcpy(*fds
+ *nfds
, CMSG_DATA(cmsg
), fd_size
);
435 for (i
= 0; i
< gotfds
; i
++) {
436 int fd
= (*fds
)[*nfds
+ i
];
441 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
444 #ifndef MSG_CMSG_CLOEXEC
445 qemu_set_cloexec(fd
);
453 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
454 const struct iovec
*iov
,
460 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
462 struct msghdr msg
= { NULL
, };
463 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
466 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
468 #ifdef MSG_CMSG_CLOEXEC
469 sflags
|= MSG_CMSG_CLOEXEC
;
472 msg
.msg_iov
= (struct iovec
*)iov
;
473 msg
.msg_iovlen
= niov
;
475 msg
.msg_control
= control
;
476 msg
.msg_controllen
= sizeof(control
);
480 ret
= recvmsg(sioc
->fd
, &msg
, sflags
);
482 if (errno
== EAGAIN
) {
483 return QIO_CHANNEL_ERR_BLOCK
;
485 if (errno
== EINTR
) {
489 error_setg_errno(errp
, errno
,
490 "Unable to read from socket");
495 qio_channel_socket_copy_fds(&msg
, fds
, nfds
);
501 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
502 const struct iovec
*iov
,
508 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
510 struct msghdr msg
= { NULL
, };
511 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
512 size_t fdsize
= sizeof(int) * nfds
;
513 struct cmsghdr
*cmsg
;
515 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
517 msg
.msg_iov
= (struct iovec
*)iov
;
518 msg
.msg_iovlen
= niov
;
521 if (nfds
> SOCKET_MAX_FDS
) {
522 error_setg_errno(errp
, EINVAL
,
523 "Only %d FDs can be sent, got %zu",
524 SOCKET_MAX_FDS
, nfds
);
528 msg
.msg_control
= control
;
529 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfds
);
531 cmsg
= CMSG_FIRSTHDR(&msg
);
532 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
533 cmsg
->cmsg_level
= SOL_SOCKET
;
534 cmsg
->cmsg_type
= SCM_RIGHTS
;
535 memcpy(CMSG_DATA(cmsg
), fds
, fdsize
);
539 ret
= sendmsg(sioc
->fd
, &msg
, 0);
541 if (errno
== EAGAIN
) {
542 return QIO_CHANNEL_ERR_BLOCK
;
544 if (errno
== EINTR
) {
547 error_setg_errno(errp
, errno
,
548 "Unable to write to socket");
554 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
555 const struct iovec
*iov
,
561 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
565 for (i
= 0; i
< niov
; i
++) {
573 if (errno
== EAGAIN
) {
577 return QIO_CHANNEL_ERR_BLOCK
;
579 } else if (errno
== EINTR
) {
582 error_setg_errno(errp
, errno
,
583 "Unable to read from socket");
588 if (ret
< iov
[i
].iov_len
) {
596 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
597 const struct iovec
*iov
,
603 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
607 for (i
= 0; i
< niov
; i
++) {
615 if (errno
== EAGAIN
) {
619 return QIO_CHANNEL_ERR_BLOCK
;
621 } else if (errno
== EINTR
) {
624 error_setg_errno(errp
, errno
,
625 "Unable to write to socket");
630 if (ret
< iov
[i
].iov_len
) {
640 qio_channel_socket_set_blocking(QIOChannel
*ioc
,
644 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
647 qemu_set_block(sioc
->fd
);
649 qemu_set_nonblock(sioc
->fd
);
651 WSAEventSelect(sioc
->fd
, ioc
->event
,
652 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
653 FD_CONNECT
| FD_WRITE
| FD_OOB
);
661 qio_channel_socket_set_delay(QIOChannel
*ioc
,
664 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
665 int v
= enabled
? 0 : 1;
667 qemu_setsockopt(sioc
->fd
,
668 IPPROTO_TCP
, TCP_NODELAY
,
674 qio_channel_socket_set_cork(QIOChannel
*ioc
,
677 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
678 int v
= enabled
? 1 : 0;
680 socket_set_cork(sioc
->fd
, v
);
685 qio_channel_socket_close(QIOChannel
*ioc
,
688 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
690 if (sioc
->fd
!= -1) {
692 WSAEventSelect(sioc
->fd
, NULL
, 0);
694 if (closesocket(sioc
->fd
) < 0) {
696 error_setg_errno(errp
, errno
,
697 "Unable to close socket");
706 qio_channel_socket_shutdown(QIOChannel
*ioc
,
707 QIOChannelShutdown how
,
710 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
714 case QIO_CHANNEL_SHUTDOWN_READ
:
717 case QIO_CHANNEL_SHUTDOWN_WRITE
:
720 case QIO_CHANNEL_SHUTDOWN_BOTH
:
726 if (shutdown(sioc
->fd
, sockhow
) < 0) {
727 error_setg_errno(errp
, errno
,
728 "Unable to shutdown socket");
734 static GSource
*qio_channel_socket_create_watch(QIOChannel
*ioc
,
735 GIOCondition condition
)
737 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
738 return qio_channel_create_socket_watch(ioc
,
743 static void qio_channel_socket_class_init(ObjectClass
*klass
,
744 void *class_data G_GNUC_UNUSED
)
746 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
748 ioc_klass
->io_writev
= qio_channel_socket_writev
;
749 ioc_klass
->io_readv
= qio_channel_socket_readv
;
750 ioc_klass
->io_set_blocking
= qio_channel_socket_set_blocking
;
751 ioc_klass
->io_close
= qio_channel_socket_close
;
752 ioc_klass
->io_shutdown
= qio_channel_socket_shutdown
;
753 ioc_klass
->io_set_cork
= qio_channel_socket_set_cork
;
754 ioc_klass
->io_set_delay
= qio_channel_socket_set_delay
;
755 ioc_klass
->io_create_watch
= qio_channel_socket_create_watch
;
758 static const TypeInfo qio_channel_socket_info
= {
759 .parent
= TYPE_QIO_CHANNEL
,
760 .name
= TYPE_QIO_CHANNEL_SOCKET
,
761 .instance_size
= sizeof(QIOChannelSocket
),
762 .instance_init
= qio_channel_socket_init
,
763 .instance_finalize
= qio_channel_socket_finalize
,
764 .class_init
= qio_channel_socket_class_init
,
767 static void qio_channel_socket_register_types(void)
769 type_register_static(&qio_channel_socket_info
);
772 type_init(qio_channel_socket_register_types
);