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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qemu/module.h"
25 #include "io/channel-socket.h"
26 #include "io/channel-watch.h"
28 #include "qapi/clone-visitor.h"
30 #define SOCKET_MAX_FDS 16
33 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
36 return socket_sockaddr_to_address(&ioc->localAddr,
42 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
45 return socket_sockaddr_to_address(&ioc->remoteAddr,
51 qio_channel_socket_new(void)
53 QIOChannelSocket *sioc;
56 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
59 ioc = QIO_CHANNEL(sioc);
60 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
63 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
66 trace_qio_channel_socket_new(sioc);
73 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
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 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS);
116 sioc->fd = -1; /* Let the caller close FD on failure */
121 qio_channel_socket_new_fd(int fd,
124 QIOChannelSocket *ioc;
126 ioc = qio_channel_socket_new();
127 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
128 object_unref(OBJECT(ioc));
132 trace_qio_channel_socket_new_fd(ioc, fd);
138 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
144 trace_qio_channel_socket_connect_sync(ioc, addr);
145 fd = socket_connect(addr, errp);
147 trace_qio_channel_socket_connect_fail(ioc);
151 trace_qio_channel_socket_connect_complete(ioc, fd);
152 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
161 static void qio_channel_socket_connect_worker(QIOTask *task,
164 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
165 SocketAddress *addr = opaque;
168 qio_channel_socket_connect_sync(ioc, addr, &err);
170 qio_task_set_error(task, err);
174 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
176 QIOTaskFunc callback,
178 GDestroyNotify destroy,
179 GMainContext *context)
181 QIOTask *task = qio_task_new(
182 OBJECT(ioc), callback, opaque, destroy);
183 SocketAddress *addrCopy;
185 addrCopy = QAPI_CLONE(SocketAddress, addr);
187 /* socket_connect() does a non-blocking connect(), but it
188 * still blocks in DNS lookups, so we must use a thread */
189 trace_qio_channel_socket_connect_async(ioc, addr);
190 qio_task_run_in_thread(task,
191 qio_channel_socket_connect_worker,
193 (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 void qio_channel_socket_listen_worker(QIOTask *task,
225 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
226 SocketAddress *addr = opaque;
229 qio_channel_socket_listen_sync(ioc, addr, &err);
231 qio_task_set_error(task, err);
235 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
237 QIOTaskFunc callback,
239 GDestroyNotify destroy,
240 GMainContext *context)
242 QIOTask *task = qio_task_new(
243 OBJECT(ioc), callback, opaque, destroy);
244 SocketAddress *addrCopy;
246 addrCopy = QAPI_CLONE(SocketAddress, 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,
258 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
259 SocketAddress *localAddr,
260 SocketAddress *remoteAddr,
265 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
266 fd = socket_dgram(remoteAddr, localAddr, errp);
268 trace_qio_channel_socket_dgram_fail(ioc);
272 trace_qio_channel_socket_dgram_complete(ioc, fd);
273 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
282 struct QIOChannelSocketDGramWorkerData {
283 SocketAddress *localAddr;
284 SocketAddress *remoteAddr;
288 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
290 struct QIOChannelSocketDGramWorkerData *data = opaque;
291 qapi_free_SocketAddress(data->localAddr);
292 qapi_free_SocketAddress(data->remoteAddr);
296 static void 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 qio_channel_socket_dgram_sync(ioc, data->localAddr,
305 data->remoteAddr, &err);
307 qio_task_set_error(task, err);
311 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
312 SocketAddress *localAddr,
313 SocketAddress *remoteAddr,
314 QIOTaskFunc callback,
316 GDestroyNotify destroy,
317 GMainContext *context)
319 QIOTask *task = qio_task_new(
320 OBJECT(ioc), callback, opaque, destroy);
321 struct QIOChannelSocketDGramWorkerData *data = g_new0(
322 struct QIOChannelSocketDGramWorkerData, 1);
324 data->localAddr = QAPI_CLONE(SocketAddress, localAddr);
325 data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr);
327 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
328 qio_task_run_in_thread(task,
329 qio_channel_socket_dgram_worker,
331 qio_channel_socket_dgram_worker_free,
337 qio_channel_socket_accept(QIOChannelSocket *ioc,
340 QIOChannelSocket *cioc;
342 cioc = qio_channel_socket_new();
343 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
344 cioc->localAddrLen = sizeof(ioc->localAddr);
347 trace_qio_channel_socket_accept(ioc);
348 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
349 &cioc->remoteAddrLen);
351 if (errno == EINTR) {
354 error_setg_errno(errp, errno, "Unable to accept connection");
355 trace_qio_channel_socket_accept_fail(ioc);
359 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
360 &cioc->localAddrLen) < 0) {
361 error_setg_errno(errp, errno,
362 "Unable to query local socket address");
367 if (cioc->localAddr.ss_family == AF_UNIX) {
368 QIOChannel *ioc_local = QIO_CHANNEL(cioc);
369 qio_channel_set_feature(ioc_local, QIO_CHANNEL_FEATURE_FD_PASS);
373 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
377 object_unref(OBJECT(cioc));
381 static void qio_channel_socket_init(Object *obj)
383 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
387 static void qio_channel_socket_finalize(Object *obj)
389 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
392 QIOChannel *ioc_local = QIO_CHANNEL(ioc);
393 if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) {
396 socket_listen_cleanup(ioc->fd, &err);
398 error_report_err(err);
403 WSAEventSelect(ioc->fd, NULL, 0);
405 closesocket(ioc->fd);
412 static void qio_channel_socket_copy_fds(struct msghdr *msg,
413 int **fds, size_t *nfds)
415 struct cmsghdr *cmsg;
420 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
424 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
425 cmsg->cmsg_level != SOL_SOCKET ||
426 cmsg->cmsg_type != SCM_RIGHTS) {
430 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
436 gotfds = fd_size / sizeof(int);
437 *fds = g_renew(int, *fds, *nfds + gotfds);
438 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
440 for (i = 0; i < gotfds; i++) {
441 int fd = (*fds)[*nfds + i];
446 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
449 #ifndef MSG_CMSG_CLOEXEC
450 qemu_set_cloexec(fd);
458 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
459 const struct iovec *iov,
465 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
467 struct msghdr msg = { NULL, };
468 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
471 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
473 #ifdef MSG_CMSG_CLOEXEC
474 sflags |= MSG_CMSG_CLOEXEC;
477 msg.msg_iov = (struct iovec *)iov;
478 msg.msg_iovlen = niov;
480 msg.msg_control = control;
481 msg.msg_controllen = sizeof(control);
485 ret = recvmsg(sioc->fd, &msg, sflags);
487 if (errno == EAGAIN) {
488 return QIO_CHANNEL_ERR_BLOCK;
490 if (errno == EINTR) {
494 error_setg_errno(errp, errno,
495 "Unable to read from socket");
500 qio_channel_socket_copy_fds(&msg, fds, nfds);
506 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
507 const struct iovec *iov,
513 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
515 struct msghdr msg = { NULL, };
516 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
517 size_t fdsize = sizeof(int) * nfds;
518 struct cmsghdr *cmsg;
520 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
522 msg.msg_iov = (struct iovec *)iov;
523 msg.msg_iovlen = niov;
526 if (nfds > SOCKET_MAX_FDS) {
527 error_setg_errno(errp, EINVAL,
528 "Only %d FDs can be sent, got %zu",
529 SOCKET_MAX_FDS, nfds);
533 msg.msg_control = control;
534 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
536 cmsg = CMSG_FIRSTHDR(&msg);
537 cmsg->cmsg_len = CMSG_LEN(fdsize);
538 cmsg->cmsg_level = SOL_SOCKET;
539 cmsg->cmsg_type = SCM_RIGHTS;
540 memcpy(CMSG_DATA(cmsg), fds, fdsize);
544 ret = sendmsg(sioc->fd, &msg, 0);
546 if (errno == EAGAIN) {
547 return QIO_CHANNEL_ERR_BLOCK;
549 if (errno == EINTR) {
552 error_setg_errno(errp, errno,
553 "Unable to write to socket");
559 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
560 const struct iovec *iov,
566 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
570 for (i = 0; i < niov; i++) {
578 if (errno == EAGAIN) {
582 return QIO_CHANNEL_ERR_BLOCK;
584 } else if (errno == EINTR) {
587 error_setg_errno(errp, errno,
588 "Unable to read from socket");
593 if (ret < iov[i].iov_len) {
601 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
602 const struct iovec *iov,
608 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
612 for (i = 0; i < niov; i++) {
620 if (errno == EAGAIN) {
624 return QIO_CHANNEL_ERR_BLOCK;
626 } else if (errno == EINTR) {
629 error_setg_errno(errp, errno,
630 "Unable to write to socket");
635 if (ret < iov[i].iov_len) {
645 qio_channel_socket_set_blocking(QIOChannel *ioc,
649 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
652 qemu_set_block(sioc->fd);
654 qemu_set_nonblock(sioc->fd);
661 qio_channel_socket_set_delay(QIOChannel *ioc,
664 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
665 int v = enabled ? 0 : 1;
667 qemu_setsockopt(sioc->fd,
668 IPPROTO_TCP, TCP_NODELAY,
674 qio_channel_socket_set_cork(QIOChannel *ioc,
677 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
678 int v = enabled ? 1 : 0;
680 socket_set_cork(sioc->fd, v);
685 qio_channel_socket_close(QIOChannel *ioc,
688 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
691 if (sioc->fd != -1) {
693 WSAEventSelect(sioc->fd, NULL, 0);
695 if (qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_LISTEN)) {
696 socket_listen_cleanup(sioc->fd, errp);
699 if (closesocket(sioc->fd) < 0) {
701 error_setg_errno(errp, errno,
702 "Unable to close socket");
711 qio_channel_socket_shutdown(QIOChannel *ioc,
712 QIOChannelShutdown how,
715 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
719 case QIO_CHANNEL_SHUTDOWN_READ:
722 case QIO_CHANNEL_SHUTDOWN_WRITE:
725 case QIO_CHANNEL_SHUTDOWN_BOTH:
731 if (shutdown(sioc->fd, sockhow) < 0) {
732 error_setg_errno(errp, errno,
733 "Unable to shutdown socket");
739 static void qio_channel_socket_set_aio_fd_handler(QIOChannel *ioc,
745 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
746 aio_set_fd_handler(ctx, sioc->fd, false, io_read, io_write, NULL, opaque);
749 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
750 GIOCondition condition)
752 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
753 return qio_channel_create_socket_watch(ioc,
758 static void qio_channel_socket_class_init(ObjectClass *klass,
759 void *class_data G_GNUC_UNUSED)
761 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
763 ioc_klass->io_writev = qio_channel_socket_writev;
764 ioc_klass->io_readv = qio_channel_socket_readv;
765 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
766 ioc_klass->io_close = qio_channel_socket_close;
767 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
768 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
769 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
770 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
771 ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
774 static const TypeInfo qio_channel_socket_info = {
775 .parent = TYPE_QIO_CHANNEL,
776 .name = TYPE_QIO_CHANNEL_SOCKET,
777 .instance_size = sizeof(QIOChannelSocket),
778 .instance_init = qio_channel_socket_init,
779 .instance_finalize = qio_channel_socket_finalize,
780 .class_init = qio_channel_socket_class_init,
783 static void qio_channel_socket_register_types(void)
785 type_register_static(&qio_channel_socket_info);
788 type_init(qio_channel_socket_register_types);