From patchwork Tue May 27 12:04:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Nikolaev X-Patchwork-Id: 352886 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id B4D6E140098 for ; Tue, 27 May 2014 22:05:10 +1000 (EST) Received: from localhost ([::1]:33815 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WpG7w-00059o-7N for incoming@patchwork.ozlabs.org; Tue, 27 May 2014 08:05:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43140) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WpG77-0003XZ-QF for qemu-devel@nongnu.org; Tue, 27 May 2014 08:04:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WpG70-00075t-G1 for qemu-devel@nongnu.org; Tue, 27 May 2014 08:04:17 -0400 Received: from mail-we0-f172.google.com ([74.125.82.172]:40592) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WpG70-00075m-6W for qemu-devel@nongnu.org; Tue, 27 May 2014 08:04:10 -0400 Received: by mail-we0-f172.google.com with SMTP id k48so9553397wev.17 for ; Tue, 27 May 2014 05:04:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-type :content-transfer-encoding; bh=diygGQyXbHFci4iplwfl0rMFAqXbwbV5XtgaT9PPzYE=; b=KCw8/wlL042KrepkdkMOlHyy/hG6PiyoCr9x7Z9CIvnTty+pPYUg8gGRqXUg3ovQu1 8xfeA0heYUPs1qyFnVQ2N91GZbZuxQGLaYlNCEFR51GUIr1zFSf8lieCw/li6zu3jZ0+ RGDS/a3MNU2JpvGbEFIskKvpr//gZpzziOqnvwsKtXPxRvhlLg6V0vaeKbnHDye7Spsq 7d9wNKyo0zTMLQ/T3zDP0srZYxE0TGPO3NcI109XIZmiHYeXPqs7PAU7Wk2T6ALLQifF froQmB0/31PMkuXL56hyleCsJnTA42JA7e+EDXOoaw24N33eLoKv+er6EdfXGTaW2bo4 aT0w== X-Gm-Message-State: ALoCoQlzkYOrAIO3WhKw6YWvGcRFlA4m/q4aOJPRPm14714h7zCcAJiE4KD0cuwUm+LpGBvPb5SG X-Received: by 10.180.76.6 with SMTP id g6mr261341wiw.34.1401192249460; Tue, 27 May 2014 05:04:09 -0700 (PDT) Received: from [0.0.14.236] ([82.146.27.14]) by mx.google.com with ESMTPSA id bn7sm34549329wjc.7.2014.05.27.05.04.08 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 May 2014 05:04:08 -0700 (PDT) From: Nikolay Nikolaev To: snabb-devel@googlegroups.com, qemu-devel@nongnu.org Date: Tue, 27 May 2014 15:04:02 +0300 Message-ID: <20140527120357.15172.29047.stgit@3820> In-Reply-To: <20140527120050.15172.94908.stgit@3820> References: <20140527120050.15172.94908.stgit@3820> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 74.125.82.172 Cc: a.motakis@virtualopensystems.com, luke@snabb.co, tech@virtualopensystems.com, n.nikolaev@virtualopensystems.com, mst@redhat.com Subject: [Qemu-devel] [PATCH v10 03/18] Add chardev API qemu_chr_fe_set_msgfds X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This will set an array of file descriptors to the internal structures. The next time a message is send the array will be send as ancillary data. This feature works on the UNIX domain socket backend only. Signed-off-by: Antonios Motakis Signed-off-by: Nikolay Nikolaev --- include/sysemu/char.h | 14 ++++++++ qemu-char.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/include/sysemu/char.h b/include/sysemu/char.h index 9981a6a..d99dcf6 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -62,6 +62,7 @@ struct CharDriverState { void (*chr_update_read_handler)(struct CharDriverState *s); int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg); int (*get_msgfd)(struct CharDriverState *s); + int (*set_msgfds)(struct CharDriverState *s, int *fds, int num); int (*chr_add_client)(struct CharDriverState *chr, int fd); IOEventHandler *chr_event; IOCanReadHandler *chr_can_read; @@ -229,6 +230,19 @@ int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg); int qemu_chr_fe_get_msgfd(CharDriverState *s); /** + * @qemu_chr_fe_set_msgfds: + * + * For backends capable of fd passing, set an array of fds to be passed with + * the next send operation. + * A subsequent call to this function before calling a write function will + * result in overwriting the fd array with the new value without being send. + * Upon writing the message the fd array is freed. + * + * Returns: -1 if fd passing isn't supported. + */ +int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num); + +/** * @qemu_chr_fe_claim: * * Claim a backend before using it, should be called before calling diff --git a/qemu-char.c b/qemu-char.c index d36a88f..53fae51 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -207,6 +207,11 @@ int qemu_chr_fe_get_msgfd(CharDriverState *s) return s->get_msgfd ? s->get_msgfd(s) : -1; } +int qemu_chr_fe_set_msgfds(CharDriverState *s, int *fds, int num) +{ + return s->set_msgfds ? s->set_msgfds(s, fds, num) : -1; +} + int qemu_chr_add_client(CharDriverState *s, int fd) { return s->chr_add_client ? s->chr_add_client(s, fd) : -1; @@ -2333,15 +2338,71 @@ typedef struct { int do_nodelay; int is_unix; int msgfd; + int *write_msgfds; + int write_msgfds_num; } TCPCharDriver; static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque); +#ifndef _WIN32 +static int unix_send_msgfds(CharDriverState *chr, const uint8_t *buf, int len) +{ + TCPCharDriver *s = chr->opaque; + struct msghdr msgh; + struct iovec iov; + int r; + + size_t fd_size = s->write_msgfds_num * sizeof(int); + char control[CMSG_SPACE(fd_size)]; + struct cmsghdr *cmsg; + + memset(&msgh, 0, sizeof(msgh)); + memset(control, 0, sizeof(control)); + + /* set the payload */ + iov.iov_base = (uint8_t *) buf; + iov.iov_len = len; + + msgh.msg_iov = &iov; + msgh.msg_iovlen = 1; + + msgh.msg_control = control; + msgh.msg_controllen = sizeof(control); + + cmsg = CMSG_FIRSTHDR(&msgh); + + cmsg->cmsg_len = CMSG_LEN(fd_size); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + memcpy(CMSG_DATA(cmsg), s->write_msgfds, fd_size); + + do { + r = sendmsg(s->fd, &msgh, 0); + } while (r < 0 && errno == EINTR); + + /* free the written msgfds, no matter what */ + if (s->write_msgfds_num) { + g_free(s->write_msgfds); + s->write_msgfds = 0; + s->write_msgfds_num = 0; + } + + return r; +} +#endif + static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) { TCPCharDriver *s = chr->opaque; if (s->connected) { - return io_channel_send(s->chan, buf, len); +#ifndef _WIN32 + if (s->is_unix && s->write_msgfds_num) { + return unix_send_msgfds(chr, buf, len); + } else +#endif + { + return io_channel_send(s->chan, buf, len); + } } else { /* XXX: indicate an error ? */ return len; @@ -2416,6 +2477,25 @@ static int tcp_get_msgfd(CharDriverState *chr) return fd; } +static int tcp_set_msgfds(CharDriverState *chr, int *fds, int num) +{ + TCPCharDriver *s = chr->opaque; + + /* clear old pending fd array */ + if (s->write_msgfds) { + g_free(s->write_msgfds); + } + + if (num) { + s->write_msgfds = g_malloc(num * sizeof(int)); + memcpy(s->write_msgfds, fds, num * sizeof(int)); + } + + s->write_msgfds_num = num; + + return 0; +} + #ifndef _WIN32 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) { @@ -2677,6 +2757,9 @@ static void tcp_chr_close(CharDriverState *chr) } closesocket(s->listen_fd); } + if (s->write_msgfds_num) { + g_free(s->write_msgfds); + } g_free(s); qemu_chr_be_event(chr, CHR_EVENT_CLOSED); } @@ -2706,6 +2789,8 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay, s->fd = -1; s->listen_fd = -1; s->msgfd = -1; + s->write_msgfds = 0; + s->write_msgfds_num = 0; chr->filename = g_malloc(256); switch (ss.ss_family) { @@ -2737,6 +2822,7 @@ static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay, chr->chr_sync_read = tcp_chr_sync_read; chr->chr_close = tcp_chr_close; chr->get_msgfd = tcp_get_msgfd; + chr->set_msgfds = tcp_set_msgfds; chr->chr_add_client = tcp_chr_add_client; chr->chr_add_watch = tcp_chr_add_watch; chr->chr_update_read_handler = tcp_chr_update_read_handler;