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 qio_channel_set_feature(ioc, 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 error_setg(errp, "Socket is already open");
81 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
82 sioc->localAddrLen = sizeof(sioc->localAddr);
85 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
86 &sioc->remoteAddrLen) < 0) {
87 if (errno == ENOTCONN) {
88 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
89 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
91 error_setg_errno(errp, errno,
92 "Unable to query remote socket address");
97 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
98 &sioc->localAddrLen) < 0) {
99 error_setg_errno(errp, errno,
100 "Unable to query local socket address");
105 if (sioc->localAddr.ss_family == AF_UNIX) {
106 QIOChannel *ioc = QIO_CHANNEL(sioc);
107 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS);
114 sioc->fd = -1; /* Let the caller close FD on failure */
119 qio_channel_socket_new_fd(int fd,
122 QIOChannelSocket *ioc;
124 ioc = qio_channel_socket_new();
125 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
126 object_unref(OBJECT(ioc));
130 trace_qio_channel_socket_new_fd(ioc, fd);
136 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
142 trace_qio_channel_socket_connect_sync(ioc, addr);
143 fd = socket_connect(addr, errp, NULL, NULL);
145 trace_qio_channel_socket_connect_fail(ioc);
149 trace_qio_channel_socket_connect_complete(ioc, fd);
150 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
159 static int qio_channel_socket_connect_worker(QIOTask *task,
163 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
164 SocketAddress *addr = opaque;
167 ret = qio_channel_socket_connect_sync(ioc,
171 object_unref(OBJECT(ioc));
176 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
178 QIOTaskFunc callback,
180 GDestroyNotify destroy)
182 QIOTask *task = qio_task_new(
183 OBJECT(ioc), callback, opaque, destroy);
184 SocketAddress *addrCopy;
186 addrCopy = QAPI_CLONE(SocketAddress, addr);
188 /* socket_connect() does a non-blocking connect(), but it
189 * still blocks in DNS lookups, so we must use a thread */
190 trace_qio_channel_socket_connect_async(ioc, addr);
191 qio_task_run_in_thread(task,
192 qio_channel_socket_connect_worker,
194 (GDestroyNotify)qapi_free_SocketAddress);
198 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
204 trace_qio_channel_socket_listen_sync(ioc, addr);
205 fd = socket_listen(addr, errp);
207 trace_qio_channel_socket_listen_fail(ioc);
211 trace_qio_channel_socket_listen_complete(ioc, fd);
212 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
216 qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_LISTEN);
222 static int qio_channel_socket_listen_worker(QIOTask *task,
226 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
227 SocketAddress *addr = opaque;
230 ret = qio_channel_socket_listen_sync(ioc,
234 object_unref(OBJECT(ioc));
239 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
241 QIOTaskFunc callback,
243 GDestroyNotify destroy)
245 QIOTask *task = qio_task_new(
246 OBJECT(ioc), callback, opaque, destroy);
247 SocketAddress *addrCopy;
249 addrCopy = QAPI_CLONE(SocketAddress, addr);
251 /* socket_listen() blocks in DNS lookups, so we must use a thread */
252 trace_qio_channel_socket_listen_async(ioc, addr);
253 qio_task_run_in_thread(task,
254 qio_channel_socket_listen_worker,
256 (GDestroyNotify)qapi_free_SocketAddress);
260 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
261 SocketAddress *localAddr,
262 SocketAddress *remoteAddr,
267 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
268 fd = socket_dgram(remoteAddr, localAddr, errp);
270 trace_qio_channel_socket_dgram_fail(ioc);
274 trace_qio_channel_socket_dgram_complete(ioc, fd);
275 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
284 struct QIOChannelSocketDGramWorkerData {
285 SocketAddress *localAddr;
286 SocketAddress *remoteAddr;
290 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
292 struct QIOChannelSocketDGramWorkerData *data = opaque;
293 qapi_free_SocketAddress(data->localAddr);
294 qapi_free_SocketAddress(data->remoteAddr);
298 static int qio_channel_socket_dgram_worker(QIOTask *task,
302 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
303 struct QIOChannelSocketDGramWorkerData *data = opaque;
306 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
307 ret = qio_channel_socket_dgram_sync(ioc,
312 object_unref(OBJECT(ioc));
317 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
318 SocketAddress *localAddr,
319 SocketAddress *remoteAddr,
320 QIOTaskFunc callback,
322 GDestroyNotify destroy)
324 QIOTask *task = qio_task_new(
325 OBJECT(ioc), callback, opaque, destroy);
326 struct QIOChannelSocketDGramWorkerData *data = g_new0(
327 struct QIOChannelSocketDGramWorkerData, 1);
329 data->localAddr = QAPI_CLONE(SocketAddress, localAddr);
330 data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr);
332 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
333 qio_task_run_in_thread(task,
334 qio_channel_socket_dgram_worker,
336 qio_channel_socket_dgram_worker_free);
341 qio_channel_socket_accept(QIOChannelSocket *ioc,
344 QIOChannelSocket *cioc;
346 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
348 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
349 cioc->localAddrLen = sizeof(ioc->localAddr);
352 QIO_CHANNEL(cioc)->event = CreateEvent(NULL, FALSE, FALSE, NULL);
357 trace_qio_channel_socket_accept(ioc);
358 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
359 &cioc->remoteAddrLen);
361 trace_qio_channel_socket_accept_fail(ioc);
362 if (errno == EINTR) {
368 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
369 &cioc->localAddrLen) < 0) {
370 error_setg_errno(errp, errno,
371 "Unable to query local socket address");
376 if (cioc->localAddr.ss_family == AF_UNIX) {
377 QIOChannel *ioc_local = QIO_CHANNEL(cioc);
378 qio_channel_set_feature(ioc_local, QIO_CHANNEL_FEATURE_FD_PASS);
382 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
386 object_unref(OBJECT(cioc));
390 static void qio_channel_socket_init(Object *obj)
392 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
396 static void qio_channel_socket_finalize(Object *obj)
398 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
401 QIOChannel *ioc_local = QIO_CHANNEL(ioc);
402 if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) {
405 socket_listen_cleanup(ioc->fd, &err);
407 error_report_err(err);
412 WSAEventSelect(ioc->fd, NULL, 0);
414 closesocket(ioc->fd);
421 static void qio_channel_socket_copy_fds(struct msghdr *msg,
422 int **fds, size_t *nfds)
424 struct cmsghdr *cmsg;
429 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
433 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
434 cmsg->cmsg_level != SOL_SOCKET ||
435 cmsg->cmsg_type != SCM_RIGHTS) {
439 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
445 gotfds = fd_size / sizeof(int);
446 *fds = g_renew(int, *fds, *nfds + gotfds);
447 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
449 for (i = 0; i < gotfds; i++) {
450 int fd = (*fds)[*nfds + i];
455 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
458 #ifndef MSG_CMSG_CLOEXEC
459 qemu_set_cloexec(fd);
467 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
468 const struct iovec *iov,
474 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
476 struct msghdr msg = { NULL, };
477 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
480 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
482 #ifdef MSG_CMSG_CLOEXEC
483 sflags |= MSG_CMSG_CLOEXEC;
486 msg.msg_iov = (struct iovec *)iov;
487 msg.msg_iovlen = niov;
489 msg.msg_control = control;
490 msg.msg_controllen = sizeof(control);
494 ret = recvmsg(sioc->fd, &msg, sflags);
496 if (errno == EAGAIN) {
497 return QIO_CHANNEL_ERR_BLOCK;
499 if (errno == EINTR) {
503 error_setg_errno(errp, errno,
504 "Unable to read from socket");
509 qio_channel_socket_copy_fds(&msg, fds, nfds);
515 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
516 const struct iovec *iov,
522 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
524 struct msghdr msg = { NULL, };
525 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
526 size_t fdsize = sizeof(int) * nfds;
527 struct cmsghdr *cmsg;
529 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
531 msg.msg_iov = (struct iovec *)iov;
532 msg.msg_iovlen = niov;
535 if (nfds > SOCKET_MAX_FDS) {
536 error_setg_errno(errp, EINVAL,
537 "Only %d FDs can be sent, got %zu",
538 SOCKET_MAX_FDS, nfds);
542 msg.msg_control = control;
543 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
545 cmsg = CMSG_FIRSTHDR(&msg);
546 cmsg->cmsg_len = CMSG_LEN(fdsize);
547 cmsg->cmsg_level = SOL_SOCKET;
548 cmsg->cmsg_type = SCM_RIGHTS;
549 memcpy(CMSG_DATA(cmsg), fds, fdsize);
553 ret = sendmsg(sioc->fd, &msg, 0);
555 if (errno == EAGAIN) {
556 return QIO_CHANNEL_ERR_BLOCK;
558 if (errno == EINTR) {
561 error_setg_errno(errp, errno,
562 "Unable to write to socket");
568 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
569 const struct iovec *iov,
575 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
579 for (i = 0; i < niov; i++) {
587 if (errno == EAGAIN) {
591 return QIO_CHANNEL_ERR_BLOCK;
593 } else if (errno == EINTR) {
596 error_setg_errno(errp, errno,
597 "Unable to read from socket");
602 if (ret < iov[i].iov_len) {
610 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
611 const struct iovec *iov,
617 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
621 for (i = 0; i < niov; i++) {
629 if (errno == EAGAIN) {
633 return QIO_CHANNEL_ERR_BLOCK;
635 } else if (errno == EINTR) {
638 error_setg_errno(errp, errno,
639 "Unable to write to socket");
644 if (ret < iov[i].iov_len) {
654 qio_channel_socket_set_blocking(QIOChannel *ioc,
658 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
661 qemu_set_block(sioc->fd);
663 qemu_set_nonblock(sioc->fd);
665 WSAEventSelect(sioc->fd, ioc->event,
666 FD_READ | FD_ACCEPT | FD_CLOSE |
667 FD_CONNECT | FD_WRITE | FD_OOB);
675 qio_channel_socket_set_delay(QIOChannel *ioc,
678 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
679 int v = enabled ? 0 : 1;
681 qemu_setsockopt(sioc->fd,
682 IPPROTO_TCP, TCP_NODELAY,
688 qio_channel_socket_set_cork(QIOChannel *ioc,
691 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
692 int v = enabled ? 1 : 0;
694 socket_set_cork(sioc->fd, v);
699 qio_channel_socket_close(QIOChannel *ioc,
702 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
704 if (sioc->fd != -1) {
706 WSAEventSelect(sioc->fd, NULL, 0);
708 if (closesocket(sioc->fd) < 0) {
710 error_setg_errno(errp, errno,
711 "Unable to close socket");
720 qio_channel_socket_shutdown(QIOChannel *ioc,
721 QIOChannelShutdown how,
724 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
728 case QIO_CHANNEL_SHUTDOWN_READ:
731 case QIO_CHANNEL_SHUTDOWN_WRITE:
734 case QIO_CHANNEL_SHUTDOWN_BOTH:
740 if (shutdown(sioc->fd, sockhow) < 0) {
741 error_setg_errno(errp, errno,
742 "Unable to shutdown socket");
748 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
749 GIOCondition condition)
751 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
752 return qio_channel_create_socket_watch(ioc,
757 static void qio_channel_socket_class_init(ObjectClass *klass,
758 void *class_data G_GNUC_UNUSED)
760 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
762 ioc_klass->io_writev = qio_channel_socket_writev;
763 ioc_klass->io_readv = qio_channel_socket_readv;
764 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
765 ioc_klass->io_close = qio_channel_socket_close;
766 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
767 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
768 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
769 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
772 static const TypeInfo qio_channel_socket_info = {
773 .parent = TYPE_QIO_CHANNEL,
774 .name = TYPE_QIO_CHANNEL_SOCKET,
775 .instance_size = sizeof(QIOChannelSocket),
776 .instance_init = qio_channel_socket_init,
777 .instance_finalize = qio_channel_socket_finalize,
778 .class_init = qio_channel_socket_class_init,
781 static void qio_channel_socket_register_types(void)
783 type_register_static(&qio_channel_socket_info);
786 type_init(qio_channel_socket_register_types);