Merge remote-tracking branch 'remotes/gkurz/tags/for-upstream' into staging
[qemu.git] / io / channel-socket.c
blobd7e03f62662d71f8821605ecbb725f7017e98f59
1 /*
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"
25 #include "trace.h"
26 #include "qapi/clone-visitor.h"
28 #define SOCKET_MAX_FDS 16
30 SocketAddress *
31 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
32 Error **errp)
34 return socket_sockaddr_to_address(&ioc->localAddr,
35 ioc->localAddrLen,
36 errp);
39 SocketAddress *
40 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
41 Error **errp)
43 return socket_sockaddr_to_address(&ioc->remoteAddr,
44 ioc->remoteAddrLen,
45 errp);
48 QIOChannelSocket *
49 qio_channel_socket_new(void)
51 QIOChannelSocket *sioc;
52 QIOChannel *ioc;
54 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
55 sioc->fd = -1;
57 ioc = QIO_CHANNEL(sioc);
58 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
60 #ifdef WIN32
61 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
62 #endif
64 trace_qio_channel_socket_new(sioc);
66 return sioc;
70 static int
71 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
72 int fd,
73 Error **errp)
75 if (sioc->fd != -1) {
76 error_setg(errp, "Socket is already open");
77 return -1;
80 sioc->fd = fd;
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);
90 } else {
91 error_setg_errno(errp, errno,
92 "Unable to query remote socket address");
93 goto error;
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");
101 goto error;
104 #ifndef WIN32
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);
109 #endif /* WIN32 */
111 return 0;
113 error:
114 sioc->fd = -1; /* Let the caller close FD on failure */
115 return -1;
118 QIOChannelSocket *
119 qio_channel_socket_new_fd(int fd,
120 Error **errp)
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));
127 return NULL;
130 trace_qio_channel_socket_new_fd(ioc, fd);
132 return ioc;
136 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
137 SocketAddress *addr,
138 Error **errp)
140 int fd;
142 trace_qio_channel_socket_connect_sync(ioc, addr);
143 fd = socket_connect(addr, errp, NULL, NULL);
144 if (fd < 0) {
145 trace_qio_channel_socket_connect_fail(ioc);
146 return -1;
149 trace_qio_channel_socket_connect_complete(ioc, fd);
150 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
151 close(fd);
152 return -1;
155 return 0;
159 static int qio_channel_socket_connect_worker(QIOTask *task,
160 Error **errp,
161 gpointer opaque)
163 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
164 SocketAddress *addr = opaque;
165 int ret;
167 ret = qio_channel_socket_connect_sync(ioc,
168 addr,
169 errp);
171 object_unref(OBJECT(ioc));
172 return ret;
176 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
177 SocketAddress *addr,
178 QIOTaskFunc callback,
179 gpointer opaque,
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,
193 addrCopy,
194 (GDestroyNotify)qapi_free_SocketAddress);
198 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
199 SocketAddress *addr,
200 Error **errp)
202 int fd;
204 trace_qio_channel_socket_listen_sync(ioc, addr);
205 fd = socket_listen(addr, errp);
206 if (fd < 0) {
207 trace_qio_channel_socket_listen_fail(ioc);
208 return -1;
211 trace_qio_channel_socket_listen_complete(ioc, fd);
212 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
213 close(fd);
214 return -1;
216 qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_LISTEN);
218 return 0;
222 static int qio_channel_socket_listen_worker(QIOTask *task,
223 Error **errp,
224 gpointer opaque)
226 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
227 SocketAddress *addr = opaque;
228 int ret;
230 ret = qio_channel_socket_listen_sync(ioc,
231 addr,
232 errp);
234 object_unref(OBJECT(ioc));
235 return ret;
239 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
240 SocketAddress *addr,
241 QIOTaskFunc callback,
242 gpointer opaque,
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,
255 addrCopy,
256 (GDestroyNotify)qapi_free_SocketAddress);
260 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
261 SocketAddress *localAddr,
262 SocketAddress *remoteAddr,
263 Error **errp)
265 int fd;
267 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
268 fd = socket_dgram(remoteAddr, localAddr, errp);
269 if (fd < 0) {
270 trace_qio_channel_socket_dgram_fail(ioc);
271 return -1;
274 trace_qio_channel_socket_dgram_complete(ioc, fd);
275 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
276 close(fd);
277 return -1;
280 return 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);
295 g_free(data);
298 static int qio_channel_socket_dgram_worker(QIOTask *task,
299 Error **errp,
300 gpointer opaque)
302 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
303 struct QIOChannelSocketDGramWorkerData *data = opaque;
304 int ret;
306 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
307 ret = qio_channel_socket_dgram_sync(ioc,
308 data->localAddr,
309 data->remoteAddr,
310 errp);
312 object_unref(OBJECT(ioc));
313 return ret;
317 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
318 SocketAddress *localAddr,
319 SocketAddress *remoteAddr,
320 QIOTaskFunc callback,
321 gpointer opaque,
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,
335 data,
336 qio_channel_socket_dgram_worker_free);
340 QIOChannelSocket *
341 qio_channel_socket_accept(QIOChannelSocket *ioc,
342 Error **errp)
344 QIOChannelSocket *cioc;
346 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
347 cioc->fd = -1;
348 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
349 cioc->localAddrLen = sizeof(ioc->localAddr);
351 #ifdef WIN32
352 QIO_CHANNEL(cioc)->event = CreateEvent(NULL, FALSE, FALSE, NULL);
353 #endif
356 retry:
357 trace_qio_channel_socket_accept(ioc);
358 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
359 &cioc->remoteAddrLen);
360 if (cioc->fd < 0) {
361 trace_qio_channel_socket_accept_fail(ioc);
362 if (errno == EINTR) {
363 goto retry;
365 goto error;
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");
372 goto error;
375 #ifndef WIN32
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);
380 #endif /* WIN32 */
382 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
383 return cioc;
385 error:
386 object_unref(OBJECT(cioc));
387 return NULL;
390 static void qio_channel_socket_init(Object *obj)
392 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
393 ioc->fd = -1;
396 static void qio_channel_socket_finalize(Object *obj)
398 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
400 if (ioc->fd != -1) {
401 QIOChannel *ioc_local = QIO_CHANNEL(ioc);
402 if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) {
403 Error *err = NULL;
405 socket_listen_cleanup(ioc->fd, &err);
406 if (err) {
407 error_report_err(err);
408 err = NULL;
411 #ifdef WIN32
412 WSAEventSelect(ioc->fd, NULL, 0);
413 #endif
414 closesocket(ioc->fd);
415 ioc->fd = -1;
420 #ifndef WIN32
421 static void qio_channel_socket_copy_fds(struct msghdr *msg,
422 int **fds, size_t *nfds)
424 struct cmsghdr *cmsg;
426 *nfds = 0;
427 *fds = NULL;
429 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
430 int fd_size, i;
431 int gotfds;
433 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
434 cmsg->cmsg_level != SOL_SOCKET ||
435 cmsg->cmsg_type != SCM_RIGHTS) {
436 continue;
439 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
441 if (!fd_size) {
442 continue;
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];
451 if (fd < 0) {
452 continue;
455 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
456 qemu_set_block(fd);
458 #ifndef MSG_CMSG_CLOEXEC
459 qemu_set_cloexec(fd);
460 #endif
462 *nfds += gotfds;
467 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
468 const struct iovec *iov,
469 size_t niov,
470 int **fds,
471 size_t *nfds,
472 Error **errp)
474 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
475 ssize_t ret;
476 struct msghdr msg = { NULL, };
477 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
478 int sflags = 0;
480 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
482 #ifdef MSG_CMSG_CLOEXEC
483 sflags |= MSG_CMSG_CLOEXEC;
484 #endif
486 msg.msg_iov = (struct iovec *)iov;
487 msg.msg_iovlen = niov;
488 if (fds && nfds) {
489 msg.msg_control = control;
490 msg.msg_controllen = sizeof(control);
493 retry:
494 ret = recvmsg(sioc->fd, &msg, sflags);
495 if (ret < 0) {
496 if (errno == EAGAIN) {
497 return QIO_CHANNEL_ERR_BLOCK;
499 if (errno == EINTR) {
500 goto retry;
503 error_setg_errno(errp, errno,
504 "Unable to read from socket");
505 return -1;
508 if (fds && nfds) {
509 qio_channel_socket_copy_fds(&msg, fds, nfds);
512 return ret;
515 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
516 const struct iovec *iov,
517 size_t niov,
518 int *fds,
519 size_t nfds,
520 Error **errp)
522 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
523 ssize_t ret;
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;
534 if (nfds) {
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);
539 return -1;
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);
552 retry:
553 ret = sendmsg(sioc->fd, &msg, 0);
554 if (ret <= 0) {
555 if (errno == EAGAIN) {
556 return QIO_CHANNEL_ERR_BLOCK;
558 if (errno == EINTR) {
559 goto retry;
561 error_setg_errno(errp, errno,
562 "Unable to write to socket");
563 return -1;
565 return ret;
567 #else /* WIN32 */
568 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
569 const struct iovec *iov,
570 size_t niov,
571 int **fds,
572 size_t *nfds,
573 Error **errp)
575 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
576 ssize_t done = 0;
577 ssize_t i;
579 for (i = 0; i < niov; i++) {
580 ssize_t ret;
581 retry:
582 ret = recv(sioc->fd,
583 iov[i].iov_base,
584 iov[i].iov_len,
586 if (ret < 0) {
587 if (errno == EAGAIN) {
588 if (done) {
589 return done;
590 } else {
591 return QIO_CHANNEL_ERR_BLOCK;
593 } else if (errno == EINTR) {
594 goto retry;
595 } else {
596 error_setg_errno(errp, errno,
597 "Unable to read from socket");
598 return -1;
601 done += ret;
602 if (ret < iov[i].iov_len) {
603 return done;
607 return done;
610 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
611 const struct iovec *iov,
612 size_t niov,
613 int *fds,
614 size_t nfds,
615 Error **errp)
617 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
618 ssize_t done = 0;
619 ssize_t i;
621 for (i = 0; i < niov; i++) {
622 ssize_t ret;
623 retry:
624 ret = send(sioc->fd,
625 iov[i].iov_base,
626 iov[i].iov_len,
628 if (ret < 0) {
629 if (errno == EAGAIN) {
630 if (done) {
631 return done;
632 } else {
633 return QIO_CHANNEL_ERR_BLOCK;
635 } else if (errno == EINTR) {
636 goto retry;
637 } else {
638 error_setg_errno(errp, errno,
639 "Unable to write to socket");
640 return -1;
643 done += ret;
644 if (ret < iov[i].iov_len) {
645 return done;
649 return done;
651 #endif /* WIN32 */
653 static int
654 qio_channel_socket_set_blocking(QIOChannel *ioc,
655 bool enabled,
656 Error **errp)
658 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
660 if (enabled) {
661 qemu_set_block(sioc->fd);
662 } else {
663 qemu_set_nonblock(sioc->fd);
664 #ifdef WIN32
665 WSAEventSelect(sioc->fd, ioc->event,
666 FD_READ | FD_ACCEPT | FD_CLOSE |
667 FD_CONNECT | FD_WRITE | FD_OOB);
668 #endif
670 return 0;
674 static void
675 qio_channel_socket_set_delay(QIOChannel *ioc,
676 bool enabled)
678 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
679 int v = enabled ? 0 : 1;
681 qemu_setsockopt(sioc->fd,
682 IPPROTO_TCP, TCP_NODELAY,
683 &v, sizeof(v));
687 static void
688 qio_channel_socket_set_cork(QIOChannel *ioc,
689 bool enabled)
691 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
692 int v = enabled ? 1 : 0;
694 socket_set_cork(sioc->fd, v);
698 static int
699 qio_channel_socket_close(QIOChannel *ioc,
700 Error **errp)
702 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
704 if (sioc->fd != -1) {
705 #ifdef WIN32
706 WSAEventSelect(sioc->fd, NULL, 0);
707 #endif
708 if (closesocket(sioc->fd) < 0) {
709 sioc->fd = -1;
710 error_setg_errno(errp, errno,
711 "Unable to close socket");
712 return -1;
714 sioc->fd = -1;
716 return 0;
719 static int
720 qio_channel_socket_shutdown(QIOChannel *ioc,
721 QIOChannelShutdown how,
722 Error **errp)
724 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
725 int sockhow;
727 switch (how) {
728 case QIO_CHANNEL_SHUTDOWN_READ:
729 sockhow = SHUT_RD;
730 break;
731 case QIO_CHANNEL_SHUTDOWN_WRITE:
732 sockhow = SHUT_WR;
733 break;
734 case QIO_CHANNEL_SHUTDOWN_BOTH:
735 default:
736 sockhow = SHUT_RDWR;
737 break;
740 if (shutdown(sioc->fd, sockhow) < 0) {
741 error_setg_errno(errp, errno,
742 "Unable to shutdown socket");
743 return -1;
745 return 0;
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,
753 sioc->fd,
754 condition);
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);