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"
26 #include "qapi/clone-visitor.h"
28 #define SOCKET_MAX_FDS 16
31 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
34 return socket_sockaddr_to_address(&ioc->localAddr,
40 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
43 return socket_sockaddr_to_address(&ioc->remoteAddr,
49 qio_channel_socket_new(void)
51 QIOChannelSocket *sioc;
54 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
57 ioc = QIO_CHANNEL(sioc);
58 ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN);
61 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
64 trace_qio_channel_socket_new(sioc);
71 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
76 socklen_t len = sizeof(val);
79 error_setg(errp, "Socket is already open");
84 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
85 sioc->localAddrLen = sizeof(sioc->localAddr);
88 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
89 &sioc->remoteAddrLen) < 0) {
90 if (errno == ENOTCONN) {
91 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
92 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
94 error_setg_errno(errp, errno,
95 "Unable to query remote socket address");
100 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
101 &sioc->localAddrLen) < 0) {
102 error_setg_errno(errp, errno,
103 "Unable to query local socket address");
108 if (sioc->localAddr.ss_family == AF_UNIX) {
109 QIOChannel *ioc = QIO_CHANNEL(sioc);
110 ioc->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
113 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == 0 && val) {
114 QIOChannel *ioc = QIO_CHANNEL(sioc);
115 ioc->features |= (1 << QIO_CHANNEL_FEATURE_LISTEN);
121 sioc->fd = -1; /* Let the caller close FD on failure */
126 qio_channel_socket_new_fd(int fd,
129 QIOChannelSocket *ioc;
131 ioc = qio_channel_socket_new();
132 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
133 object_unref(OBJECT(ioc));
137 trace_qio_channel_socket_new_fd(ioc, fd);
143 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
149 trace_qio_channel_socket_connect_sync(ioc, addr);
150 fd = socket_connect(addr, errp, NULL, NULL);
152 trace_qio_channel_socket_connect_fail(ioc);
156 trace_qio_channel_socket_connect_complete(ioc, fd);
157 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
166 static int qio_channel_socket_connect_worker(QIOTask *task,
170 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
171 SocketAddress *addr = opaque;
174 ret = qio_channel_socket_connect_sync(ioc,
178 object_unref(OBJECT(ioc));
183 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
185 QIOTaskFunc callback,
187 GDestroyNotify destroy)
189 QIOTask *task = qio_task_new(
190 OBJECT(ioc), callback, opaque, destroy);
191 SocketAddress *addrCopy;
193 addrCopy = QAPI_CLONE(SocketAddress, addr);
195 /* socket_connect() does a non-blocking connect(), but it
196 * still blocks in DNS lookups, so we must use a thread */
197 trace_qio_channel_socket_connect_async(ioc, addr);
198 qio_task_run_in_thread(task,
199 qio_channel_socket_connect_worker,
201 (GDestroyNotify)qapi_free_SocketAddress);
205 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
211 trace_qio_channel_socket_listen_sync(ioc, addr);
212 fd = socket_listen(addr, errp);
214 trace_qio_channel_socket_listen_fail(ioc);
218 trace_qio_channel_socket_listen_complete(ioc, fd);
219 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
228 static int qio_channel_socket_listen_worker(QIOTask *task,
232 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
233 SocketAddress *addr = opaque;
236 ret = qio_channel_socket_listen_sync(ioc,
240 object_unref(OBJECT(ioc));
245 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
247 QIOTaskFunc callback,
249 GDestroyNotify destroy)
251 QIOTask *task = qio_task_new(
252 OBJECT(ioc), callback, opaque, destroy);
253 SocketAddress *addrCopy;
255 addrCopy = QAPI_CLONE(SocketAddress, addr);
257 /* socket_listen() blocks in DNS lookups, so we must use a thread */
258 trace_qio_channel_socket_listen_async(ioc, addr);
259 qio_task_run_in_thread(task,
260 qio_channel_socket_listen_worker,
262 (GDestroyNotify)qapi_free_SocketAddress);
266 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
267 SocketAddress *localAddr,
268 SocketAddress *remoteAddr,
273 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
274 fd = socket_dgram(remoteAddr, localAddr, errp);
276 trace_qio_channel_socket_dgram_fail(ioc);
280 trace_qio_channel_socket_dgram_complete(ioc, fd);
281 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
290 struct QIOChannelSocketDGramWorkerData {
291 SocketAddress *localAddr;
292 SocketAddress *remoteAddr;
296 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
298 struct QIOChannelSocketDGramWorkerData *data = opaque;
299 qapi_free_SocketAddress(data->localAddr);
300 qapi_free_SocketAddress(data->remoteAddr);
304 static int qio_channel_socket_dgram_worker(QIOTask *task,
308 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
309 struct QIOChannelSocketDGramWorkerData *data = opaque;
312 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
313 ret = qio_channel_socket_dgram_sync(ioc,
318 object_unref(OBJECT(ioc));
323 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
324 SocketAddress *localAddr,
325 SocketAddress *remoteAddr,
326 QIOTaskFunc callback,
328 GDestroyNotify destroy)
330 QIOTask *task = qio_task_new(
331 OBJECT(ioc), callback, opaque, destroy);
332 struct QIOChannelSocketDGramWorkerData *data = g_new0(
333 struct QIOChannelSocketDGramWorkerData, 1);
335 data->localAddr = QAPI_CLONE(SocketAddress, localAddr);
336 data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr);
338 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
339 qio_task_run_in_thread(task,
340 qio_channel_socket_dgram_worker,
342 qio_channel_socket_dgram_worker_free);
347 qio_channel_socket_accept(QIOChannelSocket *ioc,
350 QIOChannelSocket *cioc;
352 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
354 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
355 cioc->localAddrLen = sizeof(ioc->localAddr);
358 QIO_CHANNEL(cioc)->event = CreateEvent(NULL, FALSE, FALSE, NULL);
363 trace_qio_channel_socket_accept(ioc);
364 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
365 &cioc->remoteAddrLen);
367 trace_qio_channel_socket_accept_fail(ioc);
368 if (errno == EINTR) {
374 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
375 &cioc->localAddrLen) < 0) {
376 error_setg_errno(errp, errno,
377 "Unable to query local socket address");
382 if (cioc->localAddr.ss_family == AF_UNIX) {
383 QIO_CHANNEL(cioc)->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
387 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
391 object_unref(OBJECT(cioc));
395 static void qio_channel_socket_init(Object *obj)
397 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
401 static void qio_channel_socket_finalize(Object *obj)
403 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
406 if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
409 socket_listen_cleanup(ioc->fd, &err);
411 error_report_err(err);
416 WSAEventSelect(ioc->fd, NULL, 0);
418 closesocket(ioc->fd);
425 static void qio_channel_socket_copy_fds(struct msghdr *msg,
426 int **fds, size_t *nfds)
428 struct cmsghdr *cmsg;
433 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
437 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
438 cmsg->cmsg_level != SOL_SOCKET ||
439 cmsg->cmsg_type != SCM_RIGHTS) {
443 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
449 gotfds = fd_size / sizeof(int);
450 *fds = g_renew(int, *fds, *nfds + gotfds);
451 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
453 for (i = 0; i < gotfds; i++) {
454 int fd = (*fds)[*nfds + i];
459 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
462 #ifndef MSG_CMSG_CLOEXEC
463 qemu_set_cloexec(fd);
471 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
472 const struct iovec *iov,
478 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
480 struct msghdr msg = { NULL, };
481 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
484 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
486 #ifdef MSG_CMSG_CLOEXEC
487 sflags |= MSG_CMSG_CLOEXEC;
490 msg.msg_iov = (struct iovec *)iov;
491 msg.msg_iovlen = niov;
493 msg.msg_control = control;
494 msg.msg_controllen = sizeof(control);
498 ret = recvmsg(sioc->fd, &msg, sflags);
500 if (errno == EAGAIN) {
501 return QIO_CHANNEL_ERR_BLOCK;
503 if (errno == EINTR) {
507 error_setg_errno(errp, errno,
508 "Unable to read from socket");
513 qio_channel_socket_copy_fds(&msg, fds, nfds);
519 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
520 const struct iovec *iov,
526 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
528 struct msghdr msg = { NULL, };
529 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
530 size_t fdsize = sizeof(int) * nfds;
531 struct cmsghdr *cmsg;
533 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
535 msg.msg_iov = (struct iovec *)iov;
536 msg.msg_iovlen = niov;
539 if (nfds > SOCKET_MAX_FDS) {
540 error_setg_errno(errp, EINVAL,
541 "Only %d FDs can be sent, got %zu",
542 SOCKET_MAX_FDS, nfds);
546 msg.msg_control = control;
547 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
549 cmsg = CMSG_FIRSTHDR(&msg);
550 cmsg->cmsg_len = CMSG_LEN(fdsize);
551 cmsg->cmsg_level = SOL_SOCKET;
552 cmsg->cmsg_type = SCM_RIGHTS;
553 memcpy(CMSG_DATA(cmsg), fds, fdsize);
557 ret = sendmsg(sioc->fd, &msg, 0);
559 if (errno == EAGAIN) {
560 return QIO_CHANNEL_ERR_BLOCK;
562 if (errno == EINTR) {
565 error_setg_errno(errp, errno,
566 "Unable to write to socket");
572 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
573 const struct iovec *iov,
579 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
583 for (i = 0; i < niov; i++) {
591 if (errno == EAGAIN) {
595 return QIO_CHANNEL_ERR_BLOCK;
597 } else if (errno == EINTR) {
600 error_setg_errno(errp, errno,
601 "Unable to read from socket");
606 if (ret < iov[i].iov_len) {
614 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
615 const struct iovec *iov,
621 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
625 for (i = 0; i < niov; i++) {
633 if (errno == EAGAIN) {
637 return QIO_CHANNEL_ERR_BLOCK;
639 } else if (errno == EINTR) {
642 error_setg_errno(errp, errno,
643 "Unable to write to socket");
648 if (ret < iov[i].iov_len) {
658 qio_channel_socket_set_blocking(QIOChannel *ioc,
662 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
665 qemu_set_block(sioc->fd);
667 qemu_set_nonblock(sioc->fd);
669 WSAEventSelect(sioc->fd, ioc->event,
670 FD_READ | FD_ACCEPT | FD_CLOSE |
671 FD_CONNECT | FD_WRITE | FD_OOB);
679 qio_channel_socket_set_delay(QIOChannel *ioc,
682 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
683 int v = enabled ? 0 : 1;
685 qemu_setsockopt(sioc->fd,
686 IPPROTO_TCP, TCP_NODELAY,
692 qio_channel_socket_set_cork(QIOChannel *ioc,
695 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
696 int v = enabled ? 1 : 0;
698 socket_set_cork(sioc->fd, v);
703 qio_channel_socket_close(QIOChannel *ioc,
706 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
708 if (sioc->fd != -1) {
710 WSAEventSelect(sioc->fd, NULL, 0);
712 if (closesocket(sioc->fd) < 0) {
714 error_setg_errno(errp, errno,
715 "Unable to close socket");
724 qio_channel_socket_shutdown(QIOChannel *ioc,
725 QIOChannelShutdown how,
728 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
732 case QIO_CHANNEL_SHUTDOWN_READ:
735 case QIO_CHANNEL_SHUTDOWN_WRITE:
738 case QIO_CHANNEL_SHUTDOWN_BOTH:
744 if (shutdown(sioc->fd, sockhow) < 0) {
745 error_setg_errno(errp, errno,
746 "Unable to shutdown socket");
752 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
753 GIOCondition condition)
755 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
756 return qio_channel_create_socket_watch(ioc,
761 static void qio_channel_socket_class_init(ObjectClass *klass,
762 void *class_data G_GNUC_UNUSED)
764 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
766 ioc_klass->io_writev = qio_channel_socket_writev;
767 ioc_klass->io_readv = qio_channel_socket_readv;
768 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
769 ioc_klass->io_close = qio_channel_socket_close;
770 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
771 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
772 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
773 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
776 static const TypeInfo qio_channel_socket_info = {
777 .parent = TYPE_QIO_CHANNEL,
778 .name = TYPE_QIO_CHANNEL_SOCKET,
779 .instance_size = sizeof(QIOChannelSocket),
780 .instance_init = qio_channel_socket_init,
781 .instance_finalize = qio_channel_socket_finalize,
782 .class_init = qio_channel_socket_class_init,
785 static void qio_channel_socket_register_types(void)
787 type_register_static(&qio_channel_socket_info);
790 type_init(qio_channel_socket_register_types);