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 "io/channel-socket.h"
23 #include "io/channel-watch.h"
26 #define SOCKET_MAX_FDS 16
29 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
32 return socket_sockaddr_to_address(&ioc->localAddr,
38 qio_channel_socket_get_remote_address(QIOChannelSocket *ioc,
41 return socket_sockaddr_to_address(&ioc->remoteAddr,
47 qio_channel_socket_new(void)
49 QIOChannelSocket *sioc;
52 sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
55 ioc = QIO_CHANNEL(sioc);
56 ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN);
58 trace_qio_channel_socket_new(sioc);
65 qio_channel_socket_set_fd(QIOChannelSocket *sioc,
70 error_setg(errp, "Socket is already open");
75 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
76 sioc->localAddrLen = sizeof(sioc->localAddr);
79 if (getpeername(fd, (struct sockaddr *)&sioc->remoteAddr,
80 &sioc->remoteAddrLen) < 0) {
81 if (socket_error() == ENOTCONN) {
82 memset(&sioc->remoteAddr, 0, sizeof(sioc->remoteAddr));
83 sioc->remoteAddrLen = sizeof(sioc->remoteAddr);
85 error_setg_errno(errp, socket_error(),
86 "Unable to query remote socket address");
91 if (getsockname(fd, (struct sockaddr *)&sioc->localAddr,
92 &sioc->localAddrLen) < 0) {
93 error_setg_errno(errp, socket_error(),
94 "Unable to query local socket address");
99 if (sioc->localAddr.ss_family == AF_UNIX) {
100 QIOChannel *ioc = QIO_CHANNEL(sioc);
101 ioc->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
108 sioc->fd = -1; /* Let the caller close FD on failure */
113 qio_channel_socket_new_fd(int fd,
116 QIOChannelSocket *ioc;
118 ioc = qio_channel_socket_new();
119 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
120 object_unref(OBJECT(ioc));
124 trace_qio_channel_socket_new_fd(ioc, fd);
130 int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
136 trace_qio_channel_socket_connect_sync(ioc, addr);
137 fd = socket_connect(addr, errp, NULL, NULL);
139 trace_qio_channel_socket_connect_fail(ioc);
143 trace_qio_channel_socket_connect_complete(ioc, fd);
144 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
153 static int qio_channel_socket_connect_worker(QIOTask *task,
157 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
158 SocketAddress *addr = opaque;
161 ret = qio_channel_socket_connect_sync(ioc,
165 object_unref(OBJECT(ioc));
170 void qio_channel_socket_connect_async(QIOChannelSocket *ioc,
172 QIOTaskFunc callback,
174 GDestroyNotify destroy)
176 QIOTask *task = qio_task_new(
177 OBJECT(ioc), callback, opaque, destroy);
178 SocketAddress *addrCopy;
180 qapi_copy_SocketAddress(&addrCopy, addr);
182 /* socket_connect() does a non-blocking connect(), but it
183 * still blocks in DNS lookups, so we must use a thread */
184 trace_qio_channel_socket_connect_async(ioc, addr);
185 qio_task_run_in_thread(task,
186 qio_channel_socket_connect_worker,
188 (GDestroyNotify)qapi_free_SocketAddress);
192 int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
198 trace_qio_channel_socket_listen_sync(ioc, addr);
199 fd = socket_listen(addr, errp);
201 trace_qio_channel_socket_listen_fail(ioc);
205 trace_qio_channel_socket_listen_complete(ioc, fd);
206 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
215 static int qio_channel_socket_listen_worker(QIOTask *task,
219 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
220 SocketAddress *addr = opaque;
223 ret = qio_channel_socket_listen_sync(ioc,
227 object_unref(OBJECT(ioc));
232 void qio_channel_socket_listen_async(QIOChannelSocket *ioc,
234 QIOTaskFunc callback,
236 GDestroyNotify destroy)
238 QIOTask *task = qio_task_new(
239 OBJECT(ioc), callback, opaque, destroy);
240 SocketAddress *addrCopy;
242 qapi_copy_SocketAddress(&addrCopy, addr);
244 /* socket_listen() blocks in DNS lookups, so we must use a thread */
245 trace_qio_channel_socket_listen_async(ioc, addr);
246 qio_task_run_in_thread(task,
247 qio_channel_socket_listen_worker,
249 (GDestroyNotify)qapi_free_SocketAddress);
253 int qio_channel_socket_dgram_sync(QIOChannelSocket *ioc,
254 SocketAddress *localAddr,
255 SocketAddress *remoteAddr,
260 trace_qio_channel_socket_dgram_sync(ioc, localAddr, remoteAddr);
261 fd = socket_dgram(remoteAddr, localAddr, errp);
263 trace_qio_channel_socket_dgram_fail(ioc);
267 trace_qio_channel_socket_dgram_complete(ioc, fd);
268 if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
277 struct QIOChannelSocketDGramWorkerData {
278 SocketAddress *localAddr;
279 SocketAddress *remoteAddr;
283 static void qio_channel_socket_dgram_worker_free(gpointer opaque)
285 struct QIOChannelSocketDGramWorkerData *data = opaque;
286 qapi_free_SocketAddress(data->localAddr);
287 qapi_free_SocketAddress(data->remoteAddr);
291 static int qio_channel_socket_dgram_worker(QIOTask *task,
295 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
296 struct QIOChannelSocketDGramWorkerData *data = opaque;
299 /* socket_dgram() blocks in DNS lookups, so we must use a thread */
300 ret = qio_channel_socket_dgram_sync(ioc,
305 object_unref(OBJECT(ioc));
310 void qio_channel_socket_dgram_async(QIOChannelSocket *ioc,
311 SocketAddress *localAddr,
312 SocketAddress *remoteAddr,
313 QIOTaskFunc callback,
315 GDestroyNotify destroy)
317 QIOTask *task = qio_task_new(
318 OBJECT(ioc), callback, opaque, destroy);
319 struct QIOChannelSocketDGramWorkerData *data = g_new0(
320 struct QIOChannelSocketDGramWorkerData, 1);
322 qapi_copy_SocketAddress(&data->localAddr, localAddr);
323 qapi_copy_SocketAddress(&data->remoteAddr, remoteAddr);
325 trace_qio_channel_socket_dgram_async(ioc, localAddr, remoteAddr);
326 qio_task_run_in_thread(task,
327 qio_channel_socket_dgram_worker,
329 qio_channel_socket_dgram_worker_free);
334 qio_channel_socket_accept(QIOChannelSocket *ioc,
337 QIOChannelSocket *cioc;
339 cioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
341 cioc->remoteAddrLen = sizeof(ioc->remoteAddr);
342 cioc->localAddrLen = sizeof(ioc->localAddr);
345 trace_qio_channel_socket_accept(ioc);
346 cioc->fd = qemu_accept(ioc->fd, (struct sockaddr *)&cioc->remoteAddr,
347 &cioc->remoteAddrLen);
349 trace_qio_channel_socket_accept_fail(ioc);
350 if (socket_error() == EINTR) {
356 if (getsockname(cioc->fd, (struct sockaddr *)&cioc->localAddr,
357 &cioc->localAddrLen) < 0) {
358 error_setg_errno(errp, socket_error(),
359 "Unable to query local socket address");
364 if (cioc->localAddr.ss_family == AF_UNIX) {
365 QIO_CHANNEL(cioc)->features |= (1 << QIO_CHANNEL_FEATURE_FD_PASS);
369 trace_qio_channel_socket_accept_complete(ioc, cioc, cioc->fd);
373 object_unref(OBJECT(cioc));
377 static void qio_channel_socket_init(Object *obj)
379 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
383 static void qio_channel_socket_finalize(Object *obj)
385 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
394 static void qio_channel_socket_copy_fds(struct msghdr *msg,
395 int **fds, size_t *nfds)
397 struct cmsghdr *cmsg;
402 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
406 if (cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
407 cmsg->cmsg_level != SOL_SOCKET ||
408 cmsg->cmsg_type != SCM_RIGHTS) {
412 fd_size = cmsg->cmsg_len - CMSG_LEN(0);
418 gotfds = fd_size / sizeof(int);
419 *fds = g_renew(int, *fds, *nfds + gotfds);
420 memcpy(*fds + *nfds, CMSG_DATA(cmsg), fd_size);
422 for (i = 0; i < gotfds; i++) {
423 int fd = (*fds)[*nfds + i];
428 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
431 #ifndef MSG_CMSG_CLOEXEC
432 qemu_set_cloexec(fd);
440 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
441 const struct iovec *iov,
447 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
449 struct msghdr msg = { NULL, };
450 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
453 memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
455 #ifdef MSG_CMSG_CLOEXEC
456 sflags |= MSG_CMSG_CLOEXEC;
459 msg.msg_iov = (struct iovec *)iov;
460 msg.msg_iovlen = niov;
462 msg.msg_control = control;
463 msg.msg_controllen = sizeof(control);
467 ret = recvmsg(sioc->fd, &msg, sflags);
469 if (socket_error() == EAGAIN) {
470 return QIO_CHANNEL_ERR_BLOCK;
472 if (socket_error() == EINTR) {
476 error_setg_errno(errp, socket_error(),
477 "Unable to read from socket");
482 qio_channel_socket_copy_fds(&msg, fds, nfds);
488 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
489 const struct iovec *iov,
495 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
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;
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);
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);
526 ret = sendmsg(sioc->fd, &msg, 0);
528 if (socket_error() == EAGAIN) {
529 return QIO_CHANNEL_ERR_BLOCK;
531 if (socket_error() == EINTR) {
534 error_setg_errno(errp, socket_error(),
535 "Unable to write to socket");
541 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
542 const struct iovec *iov,
548 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
552 for (i = 0; i < niov; i++) {
560 if (socket_error() == EAGAIN) {
564 return QIO_CHANNEL_ERR_BLOCK;
566 } else if (socket_error() == EINTR) {
569 error_setg_errno(errp, socket_error(),
570 "Unable to read from socket");
575 if (ret < iov[i].iov_len) {
583 static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
584 const struct iovec *iov,
590 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
594 for (i = 0; i < niov; i++) {
602 if (socket_error() == EAGAIN) {
606 return QIO_CHANNEL_ERR_BLOCK;
608 } else if (socket_error() == EINTR) {
611 error_setg_errno(errp, socket_error(),
612 "Unable to write to socket");
617 if (ret < iov[i].iov_len) {
627 qio_channel_socket_set_blocking(QIOChannel *ioc,
631 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
634 qemu_set_block(sioc->fd);
636 qemu_set_nonblock(sioc->fd);
643 qio_channel_socket_set_delay(QIOChannel *ioc,
646 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
647 int v = enabled ? 0 : 1;
649 qemu_setsockopt(sioc->fd,
650 IPPROTO_TCP, TCP_NODELAY,
656 qio_channel_socket_set_cork(QIOChannel *ioc,
659 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
660 int v = enabled ? 1 : 0;
662 socket_set_cork(sioc->fd, v);
667 qio_channel_socket_close(QIOChannel *ioc,
670 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
672 if (closesocket(sioc->fd) < 0) {
674 error_setg_errno(errp, socket_error(),
675 "Unable to close socket");
683 qio_channel_socket_shutdown(QIOChannel *ioc,
684 QIOChannelShutdown how,
687 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
691 case QIO_CHANNEL_SHUTDOWN_READ:
694 case QIO_CHANNEL_SHUTDOWN_WRITE:
697 case QIO_CHANNEL_SHUTDOWN_BOTH:
703 if (shutdown(sioc->fd, sockhow) < 0) {
704 error_setg_errno(errp, socket_error(),
705 "Unable to shutdown socket");
711 static GSource *qio_channel_socket_create_watch(QIOChannel *ioc,
712 GIOCondition condition)
714 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
715 return qio_channel_create_socket_watch(ioc,
720 static void qio_channel_socket_class_init(ObjectClass *klass,
721 void *class_data G_GNUC_UNUSED)
723 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
725 ioc_klass->io_writev = qio_channel_socket_writev;
726 ioc_klass->io_readv = qio_channel_socket_readv;
727 ioc_klass->io_set_blocking = qio_channel_socket_set_blocking;
728 ioc_klass->io_close = qio_channel_socket_close;
729 ioc_klass->io_shutdown = qio_channel_socket_shutdown;
730 ioc_klass->io_set_cork = qio_channel_socket_set_cork;
731 ioc_klass->io_set_delay = qio_channel_socket_set_delay;
732 ioc_klass->io_create_watch = qio_channel_socket_create_watch;
735 static const TypeInfo qio_channel_socket_info = {
736 .parent = TYPE_QIO_CHANNEL,
737 .name = TYPE_QIO_CHANNEL_SOCKET,
738 .instance_size = sizeof(QIOChannelSocket),
739 .instance_init = qio_channel_socket_init,
740 .instance_finalize = qio_channel_socket_finalize,
741 .class_init = qio_channel_socket_class_init,
744 static void qio_channel_socket_register_types(void)
746 type_register_static(&qio_channel_socket_info);
749 type_init(qio_channel_socket_register_types);