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 "io/channel-socket.h"
23 #include "io/channel-watch.h"
26 #define SOCKET_MAX_FDS 16
29 qio_channel_socket_get_local_address(QIOChannelSocket
*ioc
,
32 return socket_sockaddr_to_address(&ioc
->localAddr
,
38 qio_channel_socket_get_remote_address(QIOChannelSocket
*ioc
,
41 return socket_sockaddr_to_address(&ioc
->remoteAddr
,
47 qio_channel_socket_new(void)
49 QIOChannelSocket
*sioc
;
52 sioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
55 ioc
= QIO_CHANNEL(sioc
);
56 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN
);
59 ioc
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
62 trace_qio_channel_socket_new(sioc
);
69 qio_channel_socket_set_fd(QIOChannelSocket
*sioc
,
74 error_setg(errp
, "Socket is already open");
79 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
80 sioc
->localAddrLen
= sizeof(sioc
->localAddr
);
83 if (getpeername(fd
, (struct sockaddr
*)&sioc
->remoteAddr
,
84 &sioc
->remoteAddrLen
) < 0) {
85 if (errno
== ENOTCONN
) {
86 memset(&sioc
->remoteAddr
, 0, sizeof(sioc
->remoteAddr
));
87 sioc
->remoteAddrLen
= sizeof(sioc
->remoteAddr
);
89 error_setg_errno(errp
, errno
,
90 "Unable to query remote socket address");
95 if (getsockname(fd
, (struct sockaddr
*)&sioc
->localAddr
,
96 &sioc
->localAddrLen
) < 0) {
97 error_setg_errno(errp
, errno
,
98 "Unable to query local socket address");
103 if (sioc
->localAddr
.ss_family
== AF_UNIX
) {
104 QIOChannel
*ioc
= QIO_CHANNEL(sioc
);
105 ioc
->features
|= (1 << QIO_CHANNEL_FEATURE_FD_PASS
);
112 sioc
->fd
= -1; /* Let the caller close FD on failure */
117 qio_channel_socket_new_fd(int fd
,
120 QIOChannelSocket
*ioc
;
122 ioc
= qio_channel_socket_new();
123 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
124 object_unref(OBJECT(ioc
));
128 trace_qio_channel_socket_new_fd(ioc
, fd
);
134 int qio_channel_socket_connect_sync(QIOChannelSocket
*ioc
,
140 trace_qio_channel_socket_connect_sync(ioc
, addr
);
141 fd
= socket_connect(addr
, errp
, NULL
, NULL
);
143 trace_qio_channel_socket_connect_fail(ioc
);
147 trace_qio_channel_socket_connect_complete(ioc
, fd
);
148 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
157 static int qio_channel_socket_connect_worker(QIOTask
*task
,
161 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
162 SocketAddress
*addr
= opaque
;
165 ret
= qio_channel_socket_connect_sync(ioc
,
169 object_unref(OBJECT(ioc
));
174 void qio_channel_socket_connect_async(QIOChannelSocket
*ioc
,
176 QIOTaskFunc callback
,
178 GDestroyNotify destroy
)
180 QIOTask
*task
= qio_task_new(
181 OBJECT(ioc
), callback
, opaque
, destroy
);
182 SocketAddress
*addrCopy
;
184 qapi_copy_SocketAddress(&addrCopy
, 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
);
196 int qio_channel_socket_listen_sync(QIOChannelSocket
*ioc
,
202 trace_qio_channel_socket_listen_sync(ioc
, addr
);
203 fd
= socket_listen(addr
, errp
);
205 trace_qio_channel_socket_listen_fail(ioc
);
209 trace_qio_channel_socket_listen_complete(ioc
, fd
);
210 if (qio_channel_socket_set_fd(ioc
, fd
, errp
) < 0) {
219 static int qio_channel_socket_listen_worker(QIOTask
*task
,
223 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
224 SocketAddress
*addr
= opaque
;
227 ret
= qio_channel_socket_listen_sync(ioc
,
231 object_unref(OBJECT(ioc
));
236 void qio_channel_socket_listen_async(QIOChannelSocket
*ioc
,
238 QIOTaskFunc callback
,
240 GDestroyNotify destroy
)
242 QIOTask
*task
= qio_task_new(
243 OBJECT(ioc
), callback
, opaque
, destroy
);
244 SocketAddress
*addrCopy
;
246 qapi_copy_SocketAddress(&addrCopy
, addr
);
248 /* socket_listen() blocks in DNS lookups, so we must use a thread */
249 trace_qio_channel_socket_listen_async(ioc
, addr
);
250 qio_task_run_in_thread(task
,
251 qio_channel_socket_listen_worker
,
253 (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 int qio_channel_socket_dgram_worker(QIOTask
*task
,
299 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
300 struct QIOChannelSocketDGramWorkerData
*data
= opaque
;
303 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
304 ret
= qio_channel_socket_dgram_sync(ioc
,
309 object_unref(OBJECT(ioc
));
314 void qio_channel_socket_dgram_async(QIOChannelSocket
*ioc
,
315 SocketAddress
*localAddr
,
316 SocketAddress
*remoteAddr
,
317 QIOTaskFunc callback
,
319 GDestroyNotify destroy
)
321 QIOTask
*task
= qio_task_new(
322 OBJECT(ioc
), callback
, opaque
, destroy
);
323 struct QIOChannelSocketDGramWorkerData
*data
= g_new0(
324 struct QIOChannelSocketDGramWorkerData
, 1);
326 qapi_copy_SocketAddress(&data
->localAddr
, localAddr
);
327 qapi_copy_SocketAddress(&data
->remoteAddr
, remoteAddr
);
329 trace_qio_channel_socket_dgram_async(ioc
, localAddr
, remoteAddr
);
330 qio_task_run_in_thread(task
,
331 qio_channel_socket_dgram_worker
,
333 qio_channel_socket_dgram_worker_free
);
338 qio_channel_socket_accept(QIOChannelSocket
*ioc
,
341 QIOChannelSocket
*cioc
;
343 cioc
= QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET
));
345 cioc
->remoteAddrLen
= sizeof(ioc
->remoteAddr
);
346 cioc
->localAddrLen
= sizeof(ioc
->localAddr
);
349 QIO_CHANNEL(cioc
)->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
354 trace_qio_channel_socket_accept(ioc
);
355 cioc
->fd
= qemu_accept(ioc
->fd
, (struct sockaddr
*)&cioc
->remoteAddr
,
356 &cioc
->remoteAddrLen
);
358 trace_qio_channel_socket_accept_fail(ioc
);
359 if (errno
== EINTR
) {
365 if (getsockname(cioc
->fd
, (struct sockaddr
*)&cioc
->localAddr
,
366 &cioc
->localAddrLen
) < 0) {
367 error_setg_errno(errp
, errno
,
368 "Unable to query local socket address");
373 if (cioc
->localAddr
.ss_family
== AF_UNIX
) {
374 QIO_CHANNEL(cioc
)->features
|= (1 << QIO_CHANNEL_FEATURE_FD_PASS
);
378 trace_qio_channel_socket_accept_complete(ioc
, cioc
, cioc
->fd
);
382 object_unref(OBJECT(cioc
));
386 static void qio_channel_socket_init(Object
*obj
)
388 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
392 static void qio_channel_socket_finalize(Object
*obj
)
394 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(obj
);
397 WSAEventSelect(ioc
->fd
, NULL
, 0);
399 closesocket(ioc
->fd
);
406 static void qio_channel_socket_copy_fds(struct msghdr
*msg
,
407 int **fds
, size_t *nfds
)
409 struct cmsghdr
*cmsg
;
414 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
418 if (cmsg
->cmsg_len
< CMSG_LEN(sizeof(int)) ||
419 cmsg
->cmsg_level
!= SOL_SOCKET
||
420 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
424 fd_size
= cmsg
->cmsg_len
- CMSG_LEN(0);
430 gotfds
= fd_size
/ sizeof(int);
431 *fds
= g_renew(int, *fds
, *nfds
+ gotfds
);
432 memcpy(*fds
+ *nfds
, CMSG_DATA(cmsg
), fd_size
);
434 for (i
= 0; i
< gotfds
; i
++) {
435 int fd
= (*fds
)[*nfds
+ i
];
440 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
443 #ifndef MSG_CMSG_CLOEXEC
444 qemu_set_cloexec(fd
);
452 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
453 const struct iovec
*iov
,
459 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
461 struct msghdr msg
= { NULL
, };
462 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
465 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
467 #ifdef MSG_CMSG_CLOEXEC
468 sflags
|= MSG_CMSG_CLOEXEC
;
471 msg
.msg_iov
= (struct iovec
*)iov
;
472 msg
.msg_iovlen
= niov
;
474 msg
.msg_control
= control
;
475 msg
.msg_controllen
= sizeof(control
);
479 ret
= recvmsg(sioc
->fd
, &msg
, sflags
);
481 if (errno
== EAGAIN
) {
482 return QIO_CHANNEL_ERR_BLOCK
;
484 if (errno
== EINTR
) {
488 error_setg_errno(errp
, errno
,
489 "Unable to read from socket");
494 qio_channel_socket_copy_fds(&msg
, fds
, nfds
);
500 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
501 const struct iovec
*iov
,
507 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
509 struct msghdr msg
= { NULL
, };
510 char control
[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
)];
511 size_t fdsize
= sizeof(int) * nfds
;
512 struct cmsghdr
*cmsg
;
514 memset(control
, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS
));
516 msg
.msg_iov
= (struct iovec
*)iov
;
517 msg
.msg_iovlen
= niov
;
520 if (nfds
> SOCKET_MAX_FDS
) {
521 error_setg_errno(errp
, EINVAL
,
522 "Only %d FDs can be sent, got %zu",
523 SOCKET_MAX_FDS
, nfds
);
527 msg
.msg_control
= control
;
528 msg
.msg_controllen
= CMSG_SPACE(sizeof(int) * nfds
);
530 cmsg
= CMSG_FIRSTHDR(&msg
);
531 cmsg
->cmsg_len
= CMSG_LEN(fdsize
);
532 cmsg
->cmsg_level
= SOL_SOCKET
;
533 cmsg
->cmsg_type
= SCM_RIGHTS
;
534 memcpy(CMSG_DATA(cmsg
), fds
, fdsize
);
538 ret
= sendmsg(sioc
->fd
, &msg
, 0);
540 if (errno
== EAGAIN
) {
541 return QIO_CHANNEL_ERR_BLOCK
;
543 if (errno
== EINTR
) {
546 error_setg_errno(errp
, errno
,
547 "Unable to write to socket");
553 static ssize_t
qio_channel_socket_readv(QIOChannel
*ioc
,
554 const struct iovec
*iov
,
560 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
564 for (i
= 0; i
< niov
; i
++) {
572 if (errno
== EAGAIN
) {
576 return QIO_CHANNEL_ERR_BLOCK
;
578 } else if (errno
== EINTR
) {
581 error_setg_errno(errp
, errno
,
582 "Unable to read from socket");
587 if (ret
< iov
[i
].iov_len
) {
595 static ssize_t
qio_channel_socket_writev(QIOChannel
*ioc
,
596 const struct iovec
*iov
,
602 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
606 for (i
= 0; i
< niov
; i
++) {
614 if (errno
== EAGAIN
) {
618 return QIO_CHANNEL_ERR_BLOCK
;
620 } else if (errno
== EINTR
) {
623 error_setg_errno(errp
, errno
,
624 "Unable to write to socket");
629 if (ret
< iov
[i
].iov_len
) {
639 qio_channel_socket_set_blocking(QIOChannel
*ioc
,
643 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
646 qemu_set_block(sioc
->fd
);
648 qemu_set_nonblock(sioc
->fd
);
650 WSAEventSelect(sioc
->fd
, ioc
->event
,
651 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
652 FD_CONNECT
| FD_WRITE
| FD_OOB
);
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
);
689 if (sioc
->fd
!= -1) {
691 WSAEventSelect(sioc
->fd
, NULL
, 0);
693 if (closesocket(sioc
->fd
) < 0) {
695 error_setg_errno(errp
, errno
,
696 "Unable to close socket");
705 qio_channel_socket_shutdown(QIOChannel
*ioc
,
706 QIOChannelShutdown how
,
709 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
713 case QIO_CHANNEL_SHUTDOWN_READ
:
716 case QIO_CHANNEL_SHUTDOWN_WRITE
:
719 case QIO_CHANNEL_SHUTDOWN_BOTH
:
725 if (shutdown(sioc
->fd
, sockhow
) < 0) {
726 error_setg_errno(errp
, errno
,
727 "Unable to shutdown socket");
733 static GSource
*qio_channel_socket_create_watch(QIOChannel
*ioc
,
734 GIOCondition condition
)
736 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(ioc
);
737 return qio_channel_create_socket_watch(ioc
,
742 static void qio_channel_socket_class_init(ObjectClass
*klass
,
743 void *class_data G_GNUC_UNUSED
)
745 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
747 ioc_klass
->io_writev
= qio_channel_socket_writev
;
748 ioc_klass
->io_readv
= qio_channel_socket_readv
;
749 ioc_klass
->io_set_blocking
= qio_channel_socket_set_blocking
;
750 ioc_klass
->io_close
= qio_channel_socket_close
;
751 ioc_klass
->io_shutdown
= qio_channel_socket_shutdown
;
752 ioc_klass
->io_set_cork
= qio_channel_socket_set_cork
;
753 ioc_klass
->io_set_delay
= qio_channel_socket_set_delay
;
754 ioc_klass
->io_create_watch
= qio_channel_socket_create_watch
;
757 static const TypeInfo qio_channel_socket_info
= {
758 .parent
= TYPE_QIO_CHANNEL
,
759 .name
= TYPE_QIO_CHANNEL_SOCKET
,
760 .instance_size
= sizeof(QIOChannelSocket
),
761 .instance_init
= qio_channel_socket_init
,
762 .instance_finalize
= qio_channel_socket_finalize
,
763 .class_init
= qio_channel_socket_class_init
,
766 static void qio_channel_socket_register_types(void)
768 type_register_static(&qio_channel_socket_info
);
771 type_init(qio_channel_socket_register_types
);