migration: fix typos in qapi-schema from latest migration additions
[qemu/ar7.git] / io / channel-socket.c
blobca8bc20b17a2d8a0172ed13c4153c0c6ebb63bde
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"
27 #define SOCKET_MAX_FDS 16
29 SocketAddress *
30 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
31 Error **errp)
33 return socket_sockaddr_to_address(&ioc->localAddr,
34 ioc->localAddrLen,
35 errp);
38 SocketAddress *
39 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
40 Error **errp)
42 return socket_sockaddr_to_address(&ioc->remoteAddr,
43 ioc->remoteAddrLen,
44 errp);
47 QIOChannelSocket *
48 qio_channel_socket_new(void)
50 QIOChannelSocket *sioc;
51 QIOChannel *ioc;
53 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
54 sioc->fd = -1;
56 ioc = QIO_CHANNEL(sioc);
57 ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN);
59 #ifdef WIN32
60 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
61 #endif
63 trace_qio_channel_socket_new(sioc);
65 return sioc;
69 static int
70 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
71 int fd,
72 Error **errp)
74 if (sioc->fd != -1) {
75 error_setg(errp, "Socket is already open");
76 return -1;
79 sioc->fd = fd;
80 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
81 sioc->localAddrLen = sizeof(sioc->localAddr);
84 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
85 &sioc->remoteAddrLen) < 0) {
86 if (errno == ENOTCONN) {
87 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
88 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
89 } else {
90 error_setg_errno(errp, errno,
91 "Unable to query remote socket address");
92 goto error;
96 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
97 &sioc->localAddrLen) < 0) {
98 error_setg_errno(errp, errno,
99 "Unable to query local socket address");
100 goto error;
103 #ifndef WIN32
104 if (sioc->localAddr.ss_family == AF_UNIX) {
105 QIOChannel *ioc = QIO_CHANNEL(sioc);
106 ioc->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
108 #endif /* WIN32 */
110 return 0;
112 error:
113 sioc->fd = -1; /* Let the caller close FD on failure */
114 return -1;
117 QIOChannelSocket *
118 qio_channel_socket_new_fd(int fd,
119 Error **errp)
121 QIOChannelSocket *ioc;
123 ioc = qio_channel_socket_new();
124 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
125 object_unref(OBJECT(ioc));
126 return NULL;
129 trace_qio_channel_socket_new_fd(ioc, fd);
131 return ioc;
135 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
136 SocketAddress *addr,
137 Error **errp)
139 int fd;
141 trace_qio_channel_socket_connect_sync(ioc, addr);
142 fd = socket_connect(addr, errp, NULL, NULL);
143 if (fd < 0) {
144 trace_qio_channel_socket_connect_fail(ioc);
145 return -1;
148 trace_qio_channel_socket_connect_complete(ioc, fd);
149 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
150 close(fd);
151 return -1;
154 return 0;
158 static int qio_channel_socket_connect_worker(QIOTask *task,
159 Error **errp,
160 gpointer opaque)
162 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
163 SocketAddress *addr = opaque;
164 int ret;
166 ret = qio_channel_socket_connect_sync(ioc,
167 addr,
168 errp);
170 object_unref(OBJECT(ioc));
171 return ret;
175 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
176 SocketAddress *addr,
177 QIOTaskFunc callback,
178 gpointer opaque,
179 GDestroyNotify destroy)
181 QIOTask *task = qio_task_new(
182 OBJECT(ioc), callback, opaque, destroy);
183 SocketAddress *addrCopy;
185 qapi_copy_SocketAddress(&addrCopy, 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,
192 addrCopy,
193 (GDestroyNotify)qapi_free_SocketAddress);
197 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
198 SocketAddress *addr,
199 Error **errp)
201 int fd;
203 trace_qio_channel_socket_listen_sync(ioc, addr);
204 fd = socket_listen(addr, errp);
205 if (fd < 0) {
206 trace_qio_channel_socket_listen_fail(ioc);
207 return -1;
210 trace_qio_channel_socket_listen_complete(ioc, fd);
211 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
212 close(fd);
213 return -1;
216 return 0;
220 static int qio_channel_socket_listen_worker(QIOTask *task,
221 Error **errp,
222 gpointer opaque)
224 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
225 SocketAddress *addr = opaque;
226 int ret;
228 ret = qio_channel_socket_listen_sync(ioc,
229 addr,
230 errp);
232 object_unref(OBJECT(ioc));
233 return ret;
237 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
238 SocketAddress *addr,
239 QIOTaskFunc callback,
240 gpointer opaque,
241 GDestroyNotify destroy)
243 QIOTask *task = qio_task_new(
244 OBJECT(ioc), callback, opaque, destroy);
245 SocketAddress *addrCopy;
247 qapi_copy_SocketAddress(&addrCopy, addr);
249 /* socket_listen() blocks in DNS lookups, so we must use a thread */
250 trace_qio_channel_socket_listen_async(ioc, addr);
251 qio_task_run_in_thread(task,
252 qio_channel_socket_listen_worker,
253 addrCopy,
254 (GDestroyNotify)qapi_free_SocketAddress);
258 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
259 SocketAddress *localAddr,
260 SocketAddress *remoteAddr,
261 Error **errp)
263 int fd;
265 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
266 fd = socket_dgram(remoteAddr, localAddr, errp);
267 if (fd < 0) {
268 trace_qio_channel_socket_dgram_fail(ioc);
269 return -1;
272 trace_qio_channel_socket_dgram_complete(ioc, fd);
273 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
274 close(fd);
275 return -1;
278 return 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);
293 g_free(data);
296 static int qio_channel_socket_dgram_worker(QIOTask *task,
297 Error **errp,
298 gpointer opaque)
300 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
301 struct QIOChannelSocketDGramWorkerData *data = opaque;
302 int ret;
304 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
305 ret = qio_channel_socket_dgram_sync(ioc,
306 data->localAddr,
307 data->remoteAddr,
308 errp);
310 object_unref(OBJECT(ioc));
311 return ret;
315 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
316 SocketAddress *localAddr,
317 SocketAddress *remoteAddr,
318 QIOTaskFunc callback,
319 gpointer opaque,
320 GDestroyNotify destroy)
322 QIOTask *task = qio_task_new(
323 OBJECT(ioc), callback, opaque, destroy);
324 struct QIOChannelSocketDGramWorkerData *data = g_new0(
325 struct QIOChannelSocketDGramWorkerData, 1);
327 qapi_copy_SocketAddress(&data->localAddr, localAddr);
328 qapi_copy_SocketAddress(&data->remoteAddr, remoteAddr);
330 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
331 qio_task_run_in_thread(task,
332 qio_channel_socket_dgram_worker,
333 data,
334 qio_channel_socket_dgram_worker_free);
338 QIOChannelSocket *
339 qio_channel_socket_accept(QIOChannelSocket *ioc,
340 Error **errp)
342 QIOChannelSocket *cioc;
344 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
345 cioc->fd = -1;
346 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
347 cioc->localAddrLen = sizeof(ioc->localAddr);
349 #ifdef WIN32
350 QIO_CHANNEL(cioc)->event = CreateEvent(NULL, FALSE, FALSE, NULL);
351 #endif
354 retry:
355 trace_qio_channel_socket_accept(ioc);
356 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
357 &cioc->remoteAddrLen);
358 if (cioc->fd < 0) {
359 trace_qio_channel_socket_accept_fail(ioc);
360 if (errno == EINTR) {
361 goto retry;
363 goto error;
366 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
367 &cioc->localAddrLen) < 0) {
368 error_setg_errno(errp, errno,
369 "Unable to query local socket address");
370 goto error;
373 #ifndef WIN32
374 if (cioc->localAddr.ss_family == AF_UNIX) {
375 QIO_CHANNEL(cioc)->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
377 #endif /* WIN32 */
379 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
380 return cioc;
382 error:
383 object_unref(OBJECT(cioc));
384 return NULL;
387 static void qio_channel_socket_init(Object *obj)
389 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
390 ioc->fd = -1;
393 static void qio_channel_socket_finalize(Object *obj)
395 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
396 if (ioc->fd != -1) {
397 #ifdef WIN32
398 WSAEventSelect(ioc->fd, NULL, 0);
399 #endif
400 closesocket(ioc->fd);
401 ioc->fd = -1;
406 #ifndef WIN32
407 static void qio_channel_socket_copy_fds(struct msghdr *msg,
408 int **fds, size_t *nfds)
410 struct cmsghdr *cmsg;
412 *nfds = 0;
413 *fds = NULL;
415 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
416 int fd_size, i;
417 int gotfds;
419 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
420 cmsg->cmsg_level != SOL_SOCKET ||
421 cmsg->cmsg_type != SCM_RIGHTS) {
422 continue;
425 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
427 if (!fd_size) {
428 continue;
431 gotfds = fd_size / sizeof(int);
432 *fds = g_renew(int, *fds, *nfds + gotfds);
433 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
435 for (i = 0; i < gotfds; i++) {
436 int fd = (*fds)[*nfds + i];
437 if (fd < 0) {
438 continue;
441 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
442 qemu_set_block(fd);
444 #ifndef MSG_CMSG_CLOEXEC
445 qemu_set_cloexec(fd);
446 #endif
448 *nfds += gotfds;
453 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
454 const struct iovec *iov,
455 size_t niov,
456 int **fds,
457 size_t *nfds,
458 Error **errp)
460 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
461 ssize_t ret;
462 struct msghdr msg = { NULL, };
463 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
464 int sflags = 0;
466 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
468 #ifdef MSG_CMSG_CLOEXEC
469 sflags |= MSG_CMSG_CLOEXEC;
470 #endif
472 msg.msg_iov = (struct iovec *)iov;
473 msg.msg_iovlen = niov;
474 if (fds && nfds) {
475 msg.msg_control = control;
476 msg.msg_controllen = sizeof(control);
479 retry:
480 ret = recvmsg(sioc->fd, &msg, sflags);
481 if (ret < 0) {
482 if (errno == EAGAIN) {
483 return QIO_CHANNEL_ERR_BLOCK;
485 if (errno == EINTR) {
486 goto retry;
489 error_setg_errno(errp, errno,
490 "Unable to read from socket");
491 return -1;
494 if (fds && nfds) {
495 qio_channel_socket_copy_fds(&msg, fds, nfds);
498 return ret;
501 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
502 const struct iovec *iov,
503 size_t niov,
504 int *fds,
505 size_t nfds,
506 Error **errp)
508 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
509 ssize_t ret;
510 struct msghdr msg = { NULL, };
511 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
512 size_t fdsize = sizeof(int) * nfds;
513 struct cmsghdr *cmsg;
515 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
517 msg.msg_iov = (struct iovec *)iov;
518 msg.msg_iovlen = niov;
520 if (nfds) {
521 if (nfds > SOCKET_MAX_FDS) {
522 error_setg_errno(errp, EINVAL,
523 "Only %d FDs can be sent, got %zu",
524 SOCKET_MAX_FDS, nfds);
525 return -1;
528 msg.msg_control = control;
529 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
531 cmsg = CMSG_FIRSTHDR(&msg);
532 cmsg->cmsg_len = CMSG_LEN(fdsize);
533 cmsg->cmsg_level = SOL_SOCKET;
534 cmsg->cmsg_type = SCM_RIGHTS;
535 memcpy(CMSG_DATA(cmsg), fds, fdsize);
538 retry:
539 ret = sendmsg(sioc->fd, &msg, 0);
540 if (ret <= 0) {
541 if (errno == EAGAIN) {
542 return QIO_CHANNEL_ERR_BLOCK;
544 if (errno == EINTR) {
545 goto retry;
547 error_setg_errno(errp, errno,
548 "Unable to write to socket");
549 return -1;
551 return ret;
553 #else /* WIN32 */
554 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
555 const struct iovec *iov,
556 size_t niov,
557 int **fds,
558 size_t *nfds,
559 Error **errp)
561 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
562 ssize_t done = 0;
563 ssize_t i;
565 for (i = 0; i < niov; i++) {
566 ssize_t ret;
567 retry:
568 ret = recv(sioc->fd,
569 iov[i].iov_base,
570 iov[i].iov_len,
572 if (ret < 0) {
573 if (errno == EAGAIN) {
574 if (done) {
575 return done;
576 } else {
577 return QIO_CHANNEL_ERR_BLOCK;
579 } else if (errno == EINTR) {
580 goto retry;
581 } else {
582 error_setg_errno(errp, errno,
583 "Unable to read from socket");
584 return -1;
587 done += ret;
588 if (ret < iov[i].iov_len) {
589 return done;
593 return done;
596 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
597 const struct iovec *iov,
598 size_t niov,
599 int *fds,
600 size_t nfds,
601 Error **errp)
603 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
604 ssize_t done = 0;
605 ssize_t i;
607 for (i = 0; i < niov; i++) {
608 ssize_t ret;
609 retry:
610 ret = send(sioc->fd,
611 iov[i].iov_base,
612 iov[i].iov_len,
614 if (ret < 0) {
615 if (errno == EAGAIN) {
616 if (done) {
617 return done;
618 } else {
619 return QIO_CHANNEL_ERR_BLOCK;
621 } else if (errno == EINTR) {
622 goto retry;
623 } else {
624 error_setg_errno(errp, errno,
625 "Unable to write to socket");
626 return -1;
629 done += ret;
630 if (ret < iov[i].iov_len) {
631 return done;
635 return done;
637 #endif /* WIN32 */
639 static int
640 qio_channel_socket_set_blocking(QIOChannel *ioc,
641 bool enabled,
642 Error **errp)
644 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
646 if (enabled) {
647 qemu_set_block(sioc->fd);
648 } else {
649 qemu_set_nonblock(sioc->fd);
650 #ifdef WIN32
651 WSAEventSelect(sioc->fd, ioc->event,
652 FD_READ | FD_ACCEPT | FD_CLOSE |
653 FD_CONNECT | FD_WRITE | FD_OOB);
654 #endif
656 return 0;
660 static void
661 qio_channel_socket_set_delay(QIOChannel *ioc,
662 bool enabled)
664 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
665 int v = enabled ? 0 : 1;
667 qemu_setsockopt(sioc->fd,
668 IPPROTO_TCP, TCP_NODELAY,
669 &v, sizeof(v));
673 static void
674 qio_channel_socket_set_cork(QIOChannel *ioc,
675 bool enabled)
677 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
678 int v = enabled ? 1 : 0;
680 socket_set_cork(sioc->fd, v);
684 static int
685 qio_channel_socket_close(QIOChannel *ioc,
686 Error **errp)
688 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
690 if (sioc->fd != -1) {
691 #ifdef WIN32
692 WSAEventSelect(sioc->fd, NULL, 0);
693 #endif
694 if (closesocket(sioc->fd) < 0) {
695 sioc->fd = -1;
696 error_setg_errno(errp, errno,
697 "Unable to close socket");
698 return -1;
700 sioc->fd = -1;
702 return 0;
705 static int
706 qio_channel_socket_shutdown(QIOChannel *ioc,
707 QIOChannelShutdown how,
708 Error **errp)
710 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
711 int sockhow;
713 switch (how) {
714 case QIO_CHANNEL_SHUTDOWN_READ:
715 sockhow = SHUT_RD;
716 break;
717 case QIO_CHANNEL_SHUTDOWN_WRITE:
718 sockhow = SHUT_WR;
719 break;
720 case QIO_CHANNEL_SHUTDOWN_BOTH:
721 default:
722 sockhow = SHUT_RDWR;
723 break;
726 if (shutdown(sioc->fd, sockhow) < 0) {
727 error_setg_errno(errp, errno,
728 "Unable to shutdown socket");
729 return -1;
731 return 0;
734 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
735 GIOCondition condition)
737 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
738 return qio_channel_create_socket_watch(ioc,
739 sioc->fd,
740 condition);
743 static void qio_channel_socket_class_init(ObjectClass *klass,
744 void *class_data G_GNUC_UNUSED)
746 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
748 ioc_klass->io_writev = qio_channel_socket_writev;
749 ioc_klass->io_readv = qio_channel_socket_readv;
750 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
751 ioc_klass->io_close = qio_channel_socket_close;
752 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
753 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
754 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
755 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
758 static const TypeInfo qio_channel_socket_info = {
759 .parent = TYPE_QIO_CHANNEL,
760 .name = TYPE_QIO_CHANNEL_SOCKET,
761 .instance_size = sizeof(QIOChannelSocket),
762 .instance_init = qio_channel_socket_init,
763 .instance_finalize = qio_channel_socket_finalize,
764 .class_init = qio_channel_socket_class_init,
767 static void qio_channel_socket_register_types(void)
769 type_register_static(&qio_channel_socket_info);
772 type_init(qio_channel_socket_register_types);