hw: aspeed_scu: Introduce clkin_25Mhz attribute
[qemu.git] / io / channel-socket.c
blob9f5ddf68b68785d1733e75254e99bb2010614bf5
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.1 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 "qapi/error.h"
22 #include "qapi/qapi-visit-sockets.h"
23 #include "qemu/module.h"
24 #include "io/channel-socket.h"
25 #include "io/channel-watch.h"
26 #include "trace.h"
27 #include "qapi/clone-visitor.h"
29 #define SOCKET_MAX_FDS 16
31 SocketAddress *
32 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
33 Error **errp)
35 return socket_sockaddr_to_address(&ioc->localAddr,
36 ioc->localAddrLen,
37 errp);
40 SocketAddress *
41 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
42 Error **errp)
44 return socket_sockaddr_to_address(&ioc->remoteAddr,
45 ioc->remoteAddrLen,
46 errp);
49 QIOChannelSocket *
50 qio_channel_socket_new(void)
52 QIOChannelSocket *sioc;
53 QIOChannel *ioc;
55 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
56 sioc->fd = -1;
58 ioc = QIO_CHANNEL(sioc);
59 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
61 #ifdef WIN32
62 ioc->event = CreateEvent(NULL, FALSE, FALSE, NULL);
63 #endif
65 trace_qio_channel_socket_new(sioc);
67 return sioc;
71 static int
72 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
73 int fd,
74 Error **errp)
76 if (sioc->fd != -1) {
77 error_setg(errp, "Socket is already open");
78 return -1;
81 sioc->fd = fd;
82 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
83 sioc->localAddrLen = sizeof(sioc->localAddr);
86 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
87 &sioc->remoteAddrLen) < 0) {
88 if (errno == ENOTCONN) {
89 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
90 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
91 } else {
92 error_setg_errno(errp, errno,
93 "Unable to query remote socket address");
94 goto error;
98 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
99 &sioc->localAddrLen) < 0) {
100 error_setg_errno(errp, errno,
101 "Unable to query local socket address");
102 goto error;
105 #ifndef WIN32
106 if (sioc->localAddr.ss_family == AF_UNIX) {
107 QIOChannel *ioc = QIO_CHANNEL(sioc);
108 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS);
110 #endif /* WIN32 */
112 return 0;
114 error:
115 sioc->fd = -1; /* Let the caller close FD on failure */
116 return -1;
119 QIOChannelSocket *
120 qio_channel_socket_new_fd(int fd,
121 Error **errp)
123 QIOChannelSocket *ioc;
125 ioc = qio_channel_socket_new();
126 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
127 object_unref(OBJECT(ioc));
128 return NULL;
131 trace_qio_channel_socket_new_fd(ioc, fd);
133 return ioc;
137 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
138 SocketAddress *addr,
139 Error **errp)
141 int fd;
143 trace_qio_channel_socket_connect_sync(ioc, addr);
144 fd = socket_connect(addr, errp);
145 if (fd < 0) {
146 trace_qio_channel_socket_connect_fail(ioc);
147 return -1;
150 trace_qio_channel_socket_connect_complete(ioc, fd);
151 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
152 close(fd);
153 return -1;
156 return 0;
160 static void qio_channel_socket_connect_worker(QIOTask *task,
161 gpointer opaque)
163 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
164 SocketAddress *addr = opaque;
165 Error *err = NULL;
167 qio_channel_socket_connect_sync(ioc, addr, &err);
169 qio_task_set_error(task, err);
173 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
174 SocketAddress *addr,
175 QIOTaskFunc callback,
176 gpointer opaque,
177 GDestroyNotify destroy,
178 GMainContext *context)
180 QIOTask *task = qio_task_new(
181 OBJECT(ioc), callback, opaque, destroy);
182 SocketAddress *addrCopy;
184 addrCopy = QAPI_CLONE(SocketAddress, addr);
186 /* socket_connect() does a non-blocking connect(), but it
187 * still blocks in DNS lookups, so we must use a thread */
188 trace_qio_channel_socket_connect_async(ioc, addr);
189 qio_task_run_in_thread(task,
190 qio_channel_socket_connect_worker,
191 addrCopy,
192 (GDestroyNotify)qapi_free_SocketAddress,
193 context);
197 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
198 SocketAddress *addr,
199 int num,
200 Error **errp)
202 int fd;
204 trace_qio_channel_socket_listen_sync(ioc, addr, num);
205 fd = socket_listen(addr, num, 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 struct QIOChannelListenWorkerData {
223 SocketAddress *addr;
224 int num; /* amount of expected connections */
227 static void qio_channel_listen_worker_free(gpointer opaque)
229 struct QIOChannelListenWorkerData *data = opaque;
231 qapi_free_SocketAddress(data->addr);
232 g_free(data);
235 static void qio_channel_socket_listen_worker(QIOTask *task,
236 gpointer opaque)
238 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
239 struct QIOChannelListenWorkerData *data = opaque;
240 Error *err = NULL;
242 qio_channel_socket_listen_sync(ioc, data->addr, data->num, &err);
244 qio_task_set_error(task, err);
248 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
249 SocketAddress *addr,
250 int num,
251 QIOTaskFunc callback,
252 gpointer opaque,
253 GDestroyNotify destroy,
254 GMainContext *context)
256 QIOTask *task = qio_task_new(
257 OBJECT(ioc), callback, opaque, destroy);
258 struct QIOChannelListenWorkerData *data;
260 data = g_new0(struct QIOChannelListenWorkerData, 1);
261 data->addr = QAPI_CLONE(SocketAddress, addr);
262 data->num = num;
264 /* socket_listen() blocks in DNS lookups, so we must use a thread */
265 trace_qio_channel_socket_listen_async(ioc, addr, num);
266 qio_task_run_in_thread(task,
267 qio_channel_socket_listen_worker,
268 data,
269 qio_channel_listen_worker_free,
270 context);
274 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
275 SocketAddress *localAddr,
276 SocketAddress *remoteAddr,
277 Error **errp)
279 int fd;
281 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
282 fd = socket_dgram(remoteAddr, localAddr, errp);
283 if (fd < 0) {
284 trace_qio_channel_socket_dgram_fail(ioc);
285 return -1;
288 trace_qio_channel_socket_dgram_complete(ioc, fd);
289 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
290 close(fd);
291 return -1;
294 return 0;
298 struct QIOChannelSocketDGramWorkerData {
299 SocketAddress *localAddr;
300 SocketAddress *remoteAddr;
304 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
306 struct QIOChannelSocketDGramWorkerData *data = opaque;
307 qapi_free_SocketAddress(data->localAddr);
308 qapi_free_SocketAddress(data->remoteAddr);
309 g_free(data);
312 static void qio_channel_socket_dgram_worker(QIOTask *task,
313 gpointer opaque)
315 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
316 struct QIOChannelSocketDGramWorkerData *data = opaque;
317 Error *err = NULL;
319 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
320 qio_channel_socket_dgram_sync(ioc, data->localAddr,
321 data->remoteAddr, &err);
323 qio_task_set_error(task, err);
327 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
328 SocketAddress *localAddr,
329 SocketAddress *remoteAddr,
330 QIOTaskFunc callback,
331 gpointer opaque,
332 GDestroyNotify destroy,
333 GMainContext *context)
335 QIOTask *task = qio_task_new(
336 OBJECT(ioc), callback, opaque, destroy);
337 struct QIOChannelSocketDGramWorkerData *data = g_new0(
338 struct QIOChannelSocketDGramWorkerData, 1);
340 data->localAddr = QAPI_CLONE(SocketAddress, localAddr);
341 data->remoteAddr = QAPI_CLONE(SocketAddress, remoteAddr);
343 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
344 qio_task_run_in_thread(task,
345 qio_channel_socket_dgram_worker,
346 data,
347 qio_channel_socket_dgram_worker_free,
348 context);
352 QIOChannelSocket *
353 qio_channel_socket_accept(QIOChannelSocket *ioc,
354 Error **errp)
356 QIOChannelSocket *cioc;
358 cioc = qio_channel_socket_new();
359 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
360 cioc->localAddrLen = sizeof(ioc->localAddr);
362 retry:
363 trace_qio_channel_socket_accept(ioc);
364 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
365 &cioc->remoteAddrLen);
366 if (cioc->fd < 0) {
367 if (errno == EINTR) {
368 goto retry;
370 error_setg_errno(errp, errno, "Unable to accept connection");
371 trace_qio_channel_socket_accept_fail(ioc);
372 goto error;
375 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
376 &cioc->localAddrLen) < 0) {
377 error_setg_errno(errp, errno,
378 "Unable to query local socket address");
379 goto error;
382 #ifndef WIN32
383 if (cioc->localAddr.ss_family == AF_UNIX) {
384 QIOChannel *ioc_local = QIO_CHANNEL(cioc);
385 qio_channel_set_feature(ioc_local, QIO_CHANNEL_FEATURE_FD_PASS);
387 #endif /* WIN32 */
389 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
390 return cioc;
392 error:
393 object_unref(OBJECT(cioc));
394 return NULL;
397 static void qio_channel_socket_init(Object *obj)
399 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
400 ioc->fd = -1;
403 static void qio_channel_socket_finalize(Object *obj)
405 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
407 if (ioc->fd != -1) {
408 QIOChannel *ioc_local = QIO_CHANNEL(ioc);
409 if (qio_channel_has_feature(ioc_local, QIO_CHANNEL_FEATURE_LISTEN)) {
410 Error *err = NULL;
412 socket_listen_cleanup(ioc->fd, &err);
413 if (err) {
414 error_report_err(err);
415 err = NULL;
418 #ifdef WIN32
419 WSAEventSelect(ioc->fd, NULL, 0);
420 #endif
421 closesocket(ioc->fd);
422 ioc->fd = -1;
427 #ifndef WIN32
428 static void qio_channel_socket_copy_fds(struct msghdr *msg,
429 int **fds, size_t *nfds)
431 struct cmsghdr *cmsg;
433 *nfds = 0;
434 *fds = NULL;
436 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
437 int fd_size, i;
438 int gotfds;
440 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
441 cmsg->cmsg_level != SOL_SOCKET ||
442 cmsg->cmsg_type != SCM_RIGHTS) {
443 continue;
446 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
448 if (!fd_size) {
449 continue;
452 gotfds = fd_size / sizeof(int);
453 *fds = g_renew(int, *fds, *nfds + gotfds);
454 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
456 for (i = 0; i < gotfds; i++) {
457 int fd = (*fds)[*nfds + i];
458 if (fd < 0) {
459 continue;
462 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
463 qemu_set_block(fd);
465 #ifndef MSG_CMSG_CLOEXEC
466 qemu_set_cloexec(fd);
467 #endif
469 *nfds += gotfds;
474 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
475 const struct iovec *iov,
476 size_t niov,
477 int **fds,
478 size_t *nfds,
479 Error **errp)
481 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
482 ssize_t ret;
483 struct msghdr msg = { NULL, };
484 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
485 int sflags = 0;
487 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
489 msg.msg_iov = (struct iovec *)iov;
490 msg.msg_iovlen = niov;
491 if (fds && nfds) {
492 msg.msg_control = control;
493 msg.msg_controllen = sizeof(control);
494 #ifdef MSG_CMSG_CLOEXEC
495 sflags |= MSG_CMSG_CLOEXEC;
496 #endif
500 retry:
501 ret = recvmsg(sioc->fd, &msg, sflags);
502 if (ret < 0) {
503 if (errno == EAGAIN) {
504 return QIO_CHANNEL_ERR_BLOCK;
506 if (errno == EINTR) {
507 goto retry;
510 error_setg_errno(errp, errno,
511 "Unable to read from socket");
512 return -1;
515 if (fds && nfds) {
516 qio_channel_socket_copy_fds(&msg, fds, nfds);
519 return ret;
522 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
523 const struct iovec *iov,
524 size_t niov,
525 int *fds,
526 size_t nfds,
527 Error **errp)
529 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
530 ssize_t ret;
531 struct msghdr msg = { NULL, };
532 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
533 size_t fdsize = sizeof(int) * nfds;
534 struct cmsghdr *cmsg;
536 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
538 msg.msg_iov = (struct iovec *)iov;
539 msg.msg_iovlen = niov;
541 if (nfds) {
542 if (nfds > SOCKET_MAX_FDS) {
543 error_setg_errno(errp, EINVAL,
544 "Only %d FDs can be sent, got %zu",
545 SOCKET_MAX_FDS, nfds);
546 return -1;
549 msg.msg_control = control;
550 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
552 cmsg = CMSG_FIRSTHDR(&msg);
553 cmsg->cmsg_len = CMSG_LEN(fdsize);
554 cmsg->cmsg_level = SOL_SOCKET;
555 cmsg->cmsg_type = SCM_RIGHTS;
556 memcpy(CMSG_DATA(cmsg), fds, fdsize);
559 retry:
560 ret = sendmsg(sioc->fd, &msg, 0);
561 if (ret <= 0) {
562 if (errno == EAGAIN) {
563 return QIO_CHANNEL_ERR_BLOCK;
565 if (errno == EINTR) {
566 goto retry;
568 error_setg_errno(errp, errno,
569 "Unable to write to socket");
570 return -1;
572 return ret;
574 #else /* WIN32 */
575 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
576 const struct iovec *iov,
577 size_t niov,
578 int **fds,
579 size_t *nfds,
580 Error **errp)
582 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
583 ssize_t done = 0;
584 ssize_t i;
586 for (i = 0; i < niov; i++) {
587 ssize_t ret;
588 retry:
589 ret = recv(sioc->fd,
590 iov[i].iov_base,
591 iov[i].iov_len,
593 if (ret < 0) {
594 if (errno == EAGAIN) {
595 if (done) {
596 return done;
597 } else {
598 return QIO_CHANNEL_ERR_BLOCK;
600 } else if (errno == EINTR) {
601 goto retry;
602 } else {
603 error_setg_errno(errp, errno,
604 "Unable to read from socket");
605 return -1;
608 done += ret;
609 if (ret < iov[i].iov_len) {
610 return done;
614 return done;
617 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
618 const struct iovec *iov,
619 size_t niov,
620 int *fds,
621 size_t nfds,
622 Error **errp)
624 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
625 ssize_t done = 0;
626 ssize_t i;
628 for (i = 0; i < niov; i++) {
629 ssize_t ret;
630 retry:
631 ret = send(sioc->fd,
632 iov[i].iov_base,
633 iov[i].iov_len,
635 if (ret < 0) {
636 if (errno == EAGAIN) {
637 if (done) {
638 return done;
639 } else {
640 return QIO_CHANNEL_ERR_BLOCK;
642 } else if (errno == EINTR) {
643 goto retry;
644 } else {
645 error_setg_errno(errp, errno,
646 "Unable to write to socket");
647 return -1;
650 done += ret;
651 if (ret < iov[i].iov_len) {
652 return done;
656 return done;
658 #endif /* WIN32 */
660 static int
661 qio_channel_socket_set_blocking(QIOChannel *ioc,
662 bool enabled,
663 Error **errp)
665 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
667 if (enabled) {
668 qemu_set_block(sioc->fd);
669 } else {
670 qemu_set_nonblock(sioc->fd);
672 return 0;
676 static void
677 qio_channel_socket_set_delay(QIOChannel *ioc,
678 bool enabled)
680 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
681 int v = enabled ? 0 : 1;
683 setsockopt(sioc->fd,
684 IPPROTO_TCP, TCP_NODELAY,
685 &v, sizeof(v));
689 static void
690 qio_channel_socket_set_cork(QIOChannel *ioc,
691 bool enabled)
693 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
694 int v = enabled ? 1 : 0;
696 socket_set_cork(sioc->fd, v);
700 static int
701 qio_channel_socket_close(QIOChannel *ioc,
702 Error **errp)
704 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
705 int rc = 0;
706 Error *err = NULL;
708 if (sioc->fd != -1) {
709 #ifdef WIN32
710 WSAEventSelect(sioc->fd, NULL, 0);
711 #endif
712 if (qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_LISTEN)) {
713 socket_listen_cleanup(sioc->fd, errp);
716 if (closesocket(sioc->fd) < 0) {
717 sioc->fd = -1;
718 error_setg_errno(&err, errno, "Unable to close socket");
719 error_propagate(errp, err);
720 return -1;
722 sioc->fd = -1;
724 return rc;
727 static int
728 qio_channel_socket_shutdown(QIOChannel *ioc,
729 QIOChannelShutdown how,
730 Error **errp)
732 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
733 int sockhow;
735 switch (how) {
736 case QIO_CHANNEL_SHUTDOWN_READ:
737 sockhow = SHUT_RD;
738 break;
739 case QIO_CHANNEL_SHUTDOWN_WRITE:
740 sockhow = SHUT_WR;
741 break;
742 case QIO_CHANNEL_SHUTDOWN_BOTH:
743 default:
744 sockhow = SHUT_RDWR;
745 break;
748 if (shutdown(sioc->fd, sockhow) < 0) {
749 error_setg_errno(errp, errno,
750 "Unable to shutdown socket");
751 return -1;
753 return 0;
756 static void qio_channel_socket_set_aio_fd_handler(QIOChannel *ioc,
757 AioContext *ctx,
758 IOHandler *io_read,
759 IOHandler *io_write,
760 void *opaque)
762 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
763 aio_set_fd_handler(ctx, sioc->fd, false,
764 io_read, io_write, NULL, NULL, opaque);
767 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
768 GIOCondition condition)
770 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
771 return qio_channel_create_socket_watch(ioc,
772 sioc->fd,
773 condition);
776 static void qio_channel_socket_class_init(ObjectClass *klass,
777 void *class_data G_GNUC_UNUSED)
779 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
781 ioc_klass->io_writev = qio_channel_socket_writev;
782 ioc_klass->io_readv = qio_channel_socket_readv;
783 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
784 ioc_klass->io_close = qio_channel_socket_close;
785 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
786 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
787 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
788 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
789 ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
792 static const TypeInfo qio_channel_socket_info = {
793 .parent = TYPE_QIO_CHANNEL,
794 .name = TYPE_QIO_CHANNEL_SOCKET,
795 .instance_size = sizeof(QIOChannelSocket),
796 .instance_init = qio_channel_socket_init,
797 .instance_finalize = qio_channel_socket_finalize,
798 .class_init = qio_channel_socket_class_init,
801 static void qio_channel_socket_register_types(void)
803 type_register_static(&qio_channel_socket_info);
806 type_init(qio_channel_socket_register_types);