magnum: disable floppy DMA for now
[qemu.git] / io / channel-socket.c
blobbc117b111597bb26efe7f81c0a54229e941a7855
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 "io/channel-socket.h"
22 #include "io/channel-watch.h"
23 #include "trace.h"
25 #define SOCKET_MAX_FDS 16
27 SocketAddress *
28 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
29 Error **errp)
31 return socket_sockaddr_to_address(&ioc->localAddr,
32 ioc->localAddrLen,
33 errp);
36 SocketAddress *
37 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
38 Error **errp)
40 return socket_sockaddr_to_address(&ioc->remoteAddr,
41 ioc->remoteAddrLen,
42 errp);
45 QIOChannelSocket *
46 qio_channel_socket_new(void)
48 QIOChannelSocket *sioc;
49 QIOChannel *ioc;
51 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
52 sioc->fd = -1;
54 ioc = QIO_CHANNEL(sioc);
55 ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN);
57 trace_qio_channel_socket_new(sioc);
59 return sioc;
63 static int
64 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
65 int fd,
66 Error **errp)
68 if (sioc->fd != -1) {
69 error_setg(errp, "Socket is already open");
70 return -1;
73 sioc->fd = fd;
74 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
75 sioc->localAddrLen = sizeof(sioc->localAddr);
78 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
79 &sioc->remoteAddrLen) < 0) {
80 if (socket_error() == ENOTCONN) {
81 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
82 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
83 } else {
84 error_setg_errno(errp, socket_error(),
85 "Unable to query remote socket address");
86 goto error;
90 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
91 &sioc->localAddrLen) < 0) {
92 error_setg_errno(errp, socket_error(),
93 "Unable to query local socket address");
94 goto error;
97 #ifndef WIN32
98 if (sioc->localAddr.ss_family == AF_UNIX) {
99 QIOChannel *ioc = QIO_CHANNEL(sioc);
100 ioc->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
102 #endif /* WIN32 */
104 return 0;
106 error:
107 sioc->fd = -1; /* Let the caller close FD on failure */
108 return -1;
111 QIOChannelSocket *
112 qio_channel_socket_new_fd(int fd,
113 Error **errp)
115 QIOChannelSocket *ioc;
117 ioc = qio_channel_socket_new();
118 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
119 object_unref(OBJECT(ioc));
120 return NULL;
123 trace_qio_channel_socket_new_fd(ioc, fd);
125 return ioc;
129 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
130 SocketAddress *addr,
131 Error **errp)
133 int fd;
135 trace_qio_channel_socket_connect_sync(ioc, addr);
136 fd = socket_connect(addr, errp, NULL, NULL);
137 if (fd < 0) {
138 trace_qio_channel_socket_connect_fail(ioc);
139 return -1;
142 trace_qio_channel_socket_connect_complete(ioc, fd);
143 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
144 close(fd);
145 return -1;
148 return 0;
152 static int qio_channel_socket_connect_worker(QIOTask *task,
153 Error **errp,
154 gpointer opaque)
156 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
157 SocketAddress *addr = opaque;
158 int ret;
160 ret = qio_channel_socket_connect_sync(ioc,
161 addr,
162 errp);
164 object_unref(OBJECT(ioc));
165 return ret;
169 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
170 SocketAddress *addr,
171 QIOTaskFunc callback,
172 gpointer opaque,
173 GDestroyNotify destroy)
175 QIOTask *task = qio_task_new(
176 OBJECT(ioc), callback, opaque, destroy);
177 SocketAddress *addrCopy;
179 qapi_copy_SocketAddress(&addrCopy, addr);
181 /* socket_connect() does a non-blocking connect(), but it
182 * still blocks in DNS lookups, so we must use a thread */
183 trace_qio_channel_socket_connect_async(ioc, addr);
184 qio_task_run_in_thread(task,
185 qio_channel_socket_connect_worker,
186 addrCopy,
187 (GDestroyNotify)qapi_free_SocketAddress);
191 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
192 SocketAddress *addr,
193 Error **errp)
195 int fd;
197 trace_qio_channel_socket_listen_sync(ioc, addr);
198 fd = socket_listen(addr, errp);
199 if (fd < 0) {
200 trace_qio_channel_socket_listen_fail(ioc);
201 return -1;
204 trace_qio_channel_socket_listen_complete(ioc, fd);
205 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
206 close(fd);
207 return -1;
210 return 0;
214 static int qio_channel_socket_listen_worker(QIOTask *task,
215 Error **errp,
216 gpointer opaque)
218 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
219 SocketAddress *addr = opaque;
220 int ret;
222 ret = qio_channel_socket_listen_sync(ioc,
223 addr,
224 errp);
226 object_unref(OBJECT(ioc));
227 return ret;
231 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
232 SocketAddress *addr,
233 QIOTaskFunc callback,
234 gpointer opaque,
235 GDestroyNotify destroy)
237 QIOTask *task = qio_task_new(
238 OBJECT(ioc), callback, opaque, destroy);
239 SocketAddress *addrCopy;
241 qapi_copy_SocketAddress(&addrCopy, addr);
243 /* socket_listen() blocks in DNS lookups, so we must use a thread */
244 trace_qio_channel_socket_listen_async(ioc, addr);
245 qio_task_run_in_thread(task,
246 qio_channel_socket_listen_worker,
247 addrCopy,
248 (GDestroyNotify)qapi_free_SocketAddress);
252 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
253 SocketAddress *localAddr,
254 SocketAddress *remoteAddr,
255 Error **errp)
257 int fd;
259 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
260 fd = socket_dgram(localAddr, remoteAddr, errp);
261 if (fd < 0) {
262 trace_qio_channel_socket_dgram_fail(ioc);
263 return -1;
266 trace_qio_channel_socket_dgram_complete(ioc, fd);
267 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
268 close(fd);
269 return -1;
272 return 0;
276 struct QIOChannelSocketDGramWorkerData {
277 SocketAddress *localAddr;
278 SocketAddress *remoteAddr;
282 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
284 struct QIOChannelSocketDGramWorkerData *data = opaque;
285 qapi_free_SocketAddress(data->localAddr);
286 qapi_free_SocketAddress(data->remoteAddr);
287 g_free(data);
290 static int qio_channel_socket_dgram_worker(QIOTask *task,
291 Error **errp,
292 gpointer opaque)
294 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
295 struct QIOChannelSocketDGramWorkerData *data = opaque;
296 int ret;
298 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
299 ret = qio_channel_socket_dgram_sync(ioc,
300 data->localAddr,
301 data->remoteAddr,
302 errp);
304 object_unref(OBJECT(ioc));
305 return ret;
309 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
310 SocketAddress *localAddr,
311 SocketAddress *remoteAddr,
312 QIOTaskFunc callback,
313 gpointer opaque,
314 GDestroyNotify destroy)
316 QIOTask *task = qio_task_new(
317 OBJECT(ioc), callback, opaque, destroy);
318 struct QIOChannelSocketDGramWorkerData *data = g_new0(
319 struct QIOChannelSocketDGramWorkerData, 1);
321 qapi_copy_SocketAddress(&data->localAddr, localAddr);
322 qapi_copy_SocketAddress(&data->remoteAddr, remoteAddr);
324 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
325 qio_task_run_in_thread(task,
326 qio_channel_socket_dgram_worker,
327 data,
328 qio_channel_socket_dgram_worker_free);
332 QIOChannelSocket *
333 qio_channel_socket_accept(QIOChannelSocket *ioc,
334 Error **errp)
336 QIOChannelSocket *cioc;
338 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
339 cioc->fd = -1;
340 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
341 cioc->localAddrLen = sizeof(ioc->localAddr);
343 retry:
344 trace_qio_channel_socket_accept(ioc);
345 cioc->fd = accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
346 &cioc->remoteAddrLen);
347 if (cioc->fd < 0) {
348 trace_qio_channel_socket_accept_fail(ioc);
349 if (socket_error() == EINTR) {
350 goto retry;
352 goto error;
355 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
356 &cioc->localAddrLen) < 0) {
357 error_setg_errno(errp, socket_error(),
358 "Unable to query local socket address");
359 goto error;
362 #ifndef WIN32
363 if (cioc->localAddr.ss_family == AF_UNIX) {
364 QIO_CHANNEL(cioc)->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
366 #endif /* WIN32 */
368 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
369 return cioc;
371 error:
372 object_unref(OBJECT(cioc));
373 return NULL;
376 static void qio_channel_socket_init(Object *obj)
378 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
379 ioc->fd = -1;
382 static void qio_channel_socket_finalize(Object *obj)
384 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
385 if (ioc->fd != -1) {
386 close(ioc->fd);
387 ioc->fd = -1;
392 #ifndef WIN32
393 static void qio_channel_socket_copy_fds(struct msghdr *msg,
394 int **fds, size_t *nfds)
396 struct cmsghdr *cmsg;
398 *nfds = 0;
399 *fds = NULL;
401 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
402 int fd_size, i;
403 int gotfds;
405 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
406 cmsg->cmsg_level != SOL_SOCKET ||
407 cmsg->cmsg_type != SCM_RIGHTS) {
408 continue;
411 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
413 if (!fd_size) {
414 continue;
417 gotfds = fd_size / sizeof(int);
418 *fds = g_renew(int, *fds, *nfds + gotfds);
419 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
421 for (i = 0; i < gotfds; i++) {
422 int fd = (*fds)[*nfds + i];
423 if (fd < 0) {
424 continue;
427 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
428 qemu_set_block(fd);
430 #ifndef MSG_CMSG_CLOEXEC
431 qemu_set_cloexec(fd);
432 #endif
434 *nfds += gotfds;
439 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
440 const struct iovec *iov,
441 size_t niov,
442 int **fds,
443 size_t *nfds,
444 Error **errp)
446 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
447 ssize_t ret;
448 struct msghdr msg = { NULL, };
449 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
450 int sflags = 0;
452 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
454 #ifdef MSG_CMSG_CLOEXEC
455 sflags |= MSG_CMSG_CLOEXEC;
456 #endif
458 msg.msg_iov = (struct iovec *)iov;
459 msg.msg_iovlen = niov;
460 if (fds && nfds) {
461 msg.msg_control = control;
462 msg.msg_controllen = sizeof(control);
465 retry:
466 ret = recvmsg(sioc->fd, &msg, sflags);
467 if (ret < 0) {
468 if (socket_error() == EAGAIN ||
469 socket_error() == EWOULDBLOCK) {
470 return QIO_CHANNEL_ERR_BLOCK;
472 if (socket_error() == EINTR) {
473 goto retry;
476 error_setg_errno(errp, socket_error(),
477 "Unable to read from socket");
478 return -1;
481 if (fds && nfds) {
482 qio_channel_socket_copy_fds(&msg, fds, nfds);
485 return ret;
488 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
489 const struct iovec *iov,
490 size_t niov,
491 int *fds,
492 size_t nfds,
493 Error **errp)
495 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
496 ssize_t ret;
497 struct msghdr msg = { NULL, };
498 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
499 size_t fdsize = sizeof(int) * nfds;
500 struct cmsghdr *cmsg;
502 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
504 msg.msg_iov = (struct iovec *)iov;
505 msg.msg_iovlen = niov;
507 if (nfds) {
508 if (nfds > SOCKET_MAX_FDS) {
509 error_setg_errno(errp, EINVAL,
510 "Only %d FDs can be sent, got %zu",
511 SOCKET_MAX_FDS, nfds);
512 return -1;
515 msg.msg_control = control;
516 msg.msg_controllen = CMSG_SPACE(sizeof(int) * nfds);
518 cmsg = CMSG_FIRSTHDR(&msg);
519 cmsg->cmsg_len = CMSG_LEN(fdsize);
520 cmsg->cmsg_level = SOL_SOCKET;
521 cmsg->cmsg_type = SCM_RIGHTS;
522 memcpy(CMSG_DATA(cmsg), fds, fdsize);
525 retry:
526 ret = sendmsg(sioc->fd, &msg, 0);
527 if (ret <= 0) {
528 if (socket_error() == EAGAIN ||
529 socket_error() == EWOULDBLOCK) {
530 return QIO_CHANNEL_ERR_BLOCK;
532 if (socket_error() == EINTR) {
533 goto retry;
535 error_setg_errno(errp, socket_error(),
536 "Unable to write to socket");
537 return -1;
539 return ret;
541 #else /* WIN32 */
542 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
543 const struct iovec *iov,
544 size_t niov,
545 int **fds,
546 size_t *nfds,
547 Error **errp)
549 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
550 ssize_t done = 0;
551 ssize_t i;
553 for (i = 0; i < niov; i++) {
554 ssize_t ret;
555 retry:
556 ret = recv(sioc->fd,
557 iov[i].iov_base,
558 iov[i].iov_len,
560 if (ret < 0) {
561 if (socket_error() == EAGAIN) {
562 if (done) {
563 return done;
564 } else {
565 return QIO_CHANNEL_ERR_BLOCK;
567 } else if (socket_error() == EINTR) {
568 goto retry;
569 } else {
570 error_setg_errno(errp, socket_error(),
571 "Unable to write to socket");
572 return -1;
575 done += ret;
576 if (ret < iov[i].iov_len) {
577 return done;
581 return done;
584 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
585 const struct iovec *iov,
586 size_t niov,
587 int *fds,
588 size_t nfds,
589 Error **errp)
591 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
592 ssize_t done = 0;
593 ssize_t i;
595 for (i = 0; i < niov; i++) {
596 ssize_t ret;
597 retry:
598 ret = send(sioc->fd,
599 iov[i].iov_base,
600 iov[i].iov_len,
602 if (ret < 0) {
603 if (socket_error() == EAGAIN) {
604 if (done) {
605 return done;
606 } else {
607 return QIO_CHANNEL_ERR_BLOCK;
609 } else if (socket_error() == EINTR) {
610 goto retry;
611 } else {
612 error_setg_errno(errp, socket_error(),
613 "Unable to write to socket");
614 return -1;
617 done += ret;
618 if (ret < iov[i].iov_len) {
619 return done;
623 return done;
625 #endif /* WIN32 */
627 static int
628 qio_channel_socket_set_blocking(QIOChannel *ioc,
629 bool enabled,
630 Error **errp)
632 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
634 if (enabled) {
635 qemu_set_block(sioc->fd);
636 } else {
637 qemu_set_nonblock(sioc->fd);
639 return 0;
643 static void
644 qio_channel_socket_set_delay(QIOChannel *ioc,
645 bool enabled)
647 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
648 int v = enabled ? 0 : 1;
650 qemu_setsockopt(sioc->fd,
651 IPPROTO_TCP, TCP_NODELAY,
652 &v, sizeof(v));
656 static void
657 qio_channel_socket_set_cork(QIOChannel *ioc,
658 bool enabled)
660 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
661 int v = enabled ? 1 : 0;
663 socket_set_cork(sioc->fd, v);
667 static int
668 qio_channel_socket_close(QIOChannel *ioc,
669 Error **errp)
671 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
673 if (closesocket(sioc->fd) < 0) {
674 sioc->fd = -1;
675 error_setg_errno(errp, socket_error(),
676 "Unable to close socket");
677 return -1;
679 sioc->fd = -1;
680 return 0;
683 static int
684 qio_channel_socket_shutdown(QIOChannel *ioc,
685 QIOChannelShutdown how,
686 Error **errp)
688 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
689 int sockhow;
691 switch (how) {
692 case QIO_CHANNEL_SHUTDOWN_READ:
693 sockhow = SHUT_RD;
694 break;
695 case QIO_CHANNEL_SHUTDOWN_WRITE:
696 sockhow = SHUT_WR;
697 break;
698 case QIO_CHANNEL_SHUTDOWN_BOTH:
699 default:
700 sockhow = SHUT_RDWR;
701 break;
704 if (shutdown(sioc->fd, sockhow) < 0) {
705 error_setg_errno(errp, socket_error(),
706 "Unable to shutdown socket");
707 return -1;
709 return 0;
712 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
713 GIOCondition condition)
715 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
716 return qio_channel_create_fd_watch(ioc,
717 sioc->fd,
718 condition);
721 static void qio_channel_socket_class_init(ObjectClass *klass,
722 void *class_data G_GNUC_UNUSED)
724 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
726 ioc_klass->io_writev = qio_channel_socket_writev;
727 ioc_klass->io_readv = qio_channel_socket_readv;
728 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
729 ioc_klass->io_close = qio_channel_socket_close;
730 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
731 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
732 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
733 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
736 static const TypeInfo qio_channel_socket_info = {
737 .parent = TYPE_QIO_CHANNEL,
738 .name = TYPE_QIO_CHANNEL_SOCKET,
739 .instance_size = sizeof(QIOChannelSocket),
740 .instance_init = qio_channel_socket_init,
741 .instance_finalize = qio_channel_socket_finalize,
742 .class_init = qio_channel_socket_class_init,
745 static void qio_channel_socket_register_types(void)
747 type_register_static(&qio_channel_socket_info);
750 type_init(qio_channel_socket_register_types);