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 if (QIO_CHANNEL(ioc)->features & QIO_CHANNEL_FEATURE_LISTEN) {
408 socket_listen_cleanup(ioc->fd, &err);
410 error_report_err(err);
415 WSAEventSelect(ioc->fd, NULL, 0);
417 closesocket(ioc->fd);
424 static void qio_channel_socket_copy_fds(struct msghdr *msg,
425 int **fds, size_t *nfds)
427 struct cmsghdr *cmsg;
432 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
436 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
437 cmsg->cmsg_level != SOL_SOCKET ||
438 cmsg->cmsg_type != SCM_RIGHTS) {
442 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
448 gotfds = fd_size / sizeof(int);
449 *fds = g_renew(int, *fds, *nfds + gotfds);
450 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
452 for (i = 0; i < gotfds; i++) {
453 int fd = (*fds)[*nfds + i];
458 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
461 #ifndef MSG_CMSG_CLOEXEC
462 qemu_set_cloexec(fd);
470 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
471 const struct iovec *iov,
477 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
479 struct msghdr msg = { NULL, };
480 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
483 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
485 #ifdef MSG_CMSG_CLOEXEC
486 sflags |= MSG_CMSG_CLOEXEC;
489 msg.msg_iov = (struct iovec *)iov;
490 msg.msg_iovlen = niov;
492 msg.msg_control = control;
493 msg.msg_controllen = sizeof(control);
497 ret = recvmsg(sioc->fd, &msg, sflags);
499 if (errno == EAGAIN) {
500 return QIO_CHANNEL_ERR_BLOCK;
502 if (errno == EINTR) {
506 error_setg_errno(errp, errno,
507 "Unable to read from socket");
512 qio_channel_socket_copy_fds(&msg, fds, nfds);
518 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
519 const struct iovec *iov,
525 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
527 struct msghdr msg = { NULL, };
528 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
529 size_t fdsize = sizeof(int) * nfds;
530 struct cmsghdr *cmsg;
532 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
534 msg.msg_iov = (struct iovec *)iov;
535 msg.msg_iovlen = niov;
538 if (nfds > SOCKET_MAX_FDS) {
539 error_setg_errno(errp, EINVAL,
540 "Only %d FDs can be sent, got %zu",
541 SOCKET_MAX_FDS, nfds);
545 msg.msg_control = control;
546 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
548 cmsg = CMSG_FIRSTHDR(&msg);
549 cmsg->cmsg_len = CMSG_LEN(fdsize);
550 cmsg->cmsg_level = SOL_SOCKET;
551 cmsg->cmsg_type = SCM_RIGHTS;
552 memcpy(CMSG_DATA(cmsg), fds, fdsize);
556 ret = sendmsg(sioc->fd, &msg, 0);
558 if (errno == EAGAIN) {
559 return QIO_CHANNEL_ERR_BLOCK;
561 if (errno == EINTR) {
564 error_setg_errno(errp, errno,
565 "Unable to write to socket");
571 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
572 const struct iovec *iov,
578 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
582 for (i = 0; i < niov; i++) {
590 if (errno == EAGAIN) {
594 return QIO_CHANNEL_ERR_BLOCK;
596 } else if (errno == EINTR) {
599 error_setg_errno(errp, errno,
600 "Unable to read from socket");
605 if (ret < iov[i].iov_len) {
613 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
614 const struct iovec *iov,
620 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
624 for (i = 0; i < niov; i++) {
632 if (errno == EAGAIN) {
636 return QIO_CHANNEL_ERR_BLOCK;
638 } else if (errno == EINTR) {
641 error_setg_errno(errp, errno,
642 "Unable to write to socket");
647 if (ret < iov[i].iov_len) {
657 qio_channel_socket_set_blocking(QIOChannel *ioc,
661 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
664 qemu_set_block(sioc->fd);
666 qemu_set_nonblock(sioc->fd);
668 WSAEventSelect(sioc->fd, ioc->event,
669 FD_READ | FD_ACCEPT | FD_CLOSE |
670 FD_CONNECT | FD_WRITE | FD_OOB);
678 qio_channel_socket_set_delay(QIOChannel *ioc,
681 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
682 int v = enabled ? 0 : 1;
684 qemu_setsockopt(sioc->fd,
685 IPPROTO_TCP, TCP_NODELAY,
691 qio_channel_socket_set_cork(QIOChannel *ioc,
694 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
695 int v = enabled ? 1 : 0;
697 socket_set_cork(sioc->fd, v);
702 qio_channel_socket_close(QIOChannel *ioc,
705 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
707 if (sioc->fd != -1) {
709 WSAEventSelect(sioc->fd, NULL, 0);
711 if (closesocket(sioc->fd) < 0) {
713 error_setg_errno(errp, errno,
714 "Unable to close socket");
723 qio_channel_socket_shutdown(QIOChannel *ioc,
724 QIOChannelShutdown how,
727 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
731 case QIO_CHANNEL_SHUTDOWN_READ:
734 case QIO_CHANNEL_SHUTDOWN_WRITE:
737 case QIO_CHANNEL_SHUTDOWN_BOTH:
743 if (shutdown(sioc->fd, sockhow) < 0) {
744 error_setg_errno(errp, errno,
745 "Unable to shutdown socket");
751 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
752 GIOCondition condition)
754 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
755 return qio_channel_create_socket_watch(ioc,
760 static void qio_channel_socket_class_init(ObjectClass *klass,
761 void *class_data G_GNUC_UNUSED)
763 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
765 ioc_klass->io_writev = qio_channel_socket_writev;
766 ioc_klass->io_readv = qio_channel_socket_readv;
767 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
768 ioc_klass->io_close = qio_channel_socket_close;
769 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
770 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
771 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
772 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
775 static const TypeInfo qio_channel_socket_info = {
776 .parent = TYPE_QIO_CHANNEL,
777 .name = TYPE_QIO_CHANNEL_SOCKET,
778 .instance_size = sizeof(QIOChannelSocket),
779 .instance_init = qio_channel_socket_init,
780 .instance_finalize = qio_channel_socket_finalize,
781 .class_init = qio_channel_socket_class_init,
784 static void qio_channel_socket_register_types(void)
786 type_register_static(&qio_channel_socket_info);
789 type_init(qio_channel_socket_register_types);