From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 87354A04B6 for ; Tue, 12 Nov 2019 16:19:15 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 6AB282BDB; Tue, 12 Nov 2019 16:19:15 +0100 (CET) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by dpdk.org (Postfix) with ESMTP id ADB1D2BAA for ; Tue, 12 Nov 2019 16:19:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573571952; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tEKjZZ3WFM1Z5hy4Oo9vMmQNfKGV44eyW0NF/sHMWbE=; b=TbkdB+ZgeN+q4PL86tLA7lj7F4xgz1aX9dBYurWhacUby9h7++avUu7zNGyDLCpzzb/c+T WVIvfEC7aUUAeWpgPhSREM2ijdRNchxEeNoCn+spRwrt+z92nWn+JxpyxT82JwD6QMosw4 VdG0ff3eW0o1qjV1Nfr4ZGMNH5R1LkA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-20-7cOSCngNNPKFnRUqj73LzQ-1; Tue, 12 Nov 2019 10:19:08 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 08ED610C76FD; Tue, 12 Nov 2019 15:19:08 +0000 (UTC) Received: from localhost.localdomain (ovpn-112-39.ams2.redhat.com [10.36.112.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 96C8A63BA9; Tue, 12 Nov 2019 15:19:06 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, stable@dpdk.org Cc: Maxime Coquelin , "Dr . David Alan Gilbert" Date: Tue, 12 Nov 2019 16:18:50 +0100 Message-Id: <20191112151852.27341-2-maxime.coquelin@redhat.com> In-Reply-To: <20191112151852.27341-1-maxime.coquelin@redhat.com> References: <20191112151852.27341-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-MC-Unique: 7cOSCngNNPKFnRUqj73LzQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Subject: [dpdk-stable] [v16.11 PATCH v2 2/4] vhost: add number of fds to vhost-user messages X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Sender: "stable" As soon as some ancillary data (fds) are received, it is copied without checking its length. This patch adds the number of fds received to the message, which is set in read_vhost_message(). This is preliminary work to support sending fds to Qemu. Signed-off-by: Dr. David Alan Gilbert Signed-off-by: Maxime Coquelin (cherry picked from commit c00bb88d35fe975ede0ea35bdf4f765a2cece7e8) Signed-off-by: Maxime Coquelin --- lib/librte_vhost/socket.c | 22 +++++++++++++++++----- lib/librte_vhost/vhost_user.c | 2 +- lib/librte_vhost/vhost_user.h | 4 +++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c index 805b2e5b23..4a19280fb3 100644 --- a/lib/librte_vhost/socket.c +++ b/lib/librte_vhost/socket.c @@ -101,17 +101,23 @@ static struct vhost_user vhost_user =3D { =09.mutex =3D PTHREAD_MUTEX_INITIALIZER, }; =20 -/* return bytes# of read on success or negative val on failure. */ +/* + * return bytes# of read on success or negative val on failure. Update fdn= um + * with number of fds read. + */ int -read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num) +read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds, +=09=09int *fd_num) { =09struct iovec iov; =09struct msghdr msgh; -=09size_t fdsize =3D fd_num * sizeof(int); -=09char control[CMSG_SPACE(fdsize)]; +=09char control[CMSG_SPACE(max_fds * sizeof(int))]; =09struct cmsghdr *cmsg; +=09int got_fds =3D 0; =09int ret; =20 +=09*fd_num =3D 0; + =09memset(&msgh, 0, sizeof(msgh)); =09iov.iov_base =3D buf; =09iov.iov_len =3D buflen; @@ -136,11 +142,17 @@ read_fd_message(int sockfd, char *buf, int buflen, in= t *fds, int fd_num) =09=09cmsg =3D CMSG_NXTHDR(&msgh, cmsg)) { =09=09if ((cmsg->cmsg_level =3D=3D SOL_SOCKET) && =09=09=09(cmsg->cmsg_type =3D=3D SCM_RIGHTS)) { -=09=09=09memcpy(fds, CMSG_DATA(cmsg), fdsize); +=09=09=09got_fds =3D (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); +=09=09=09*fd_num =3D got_fds; +=09=09=09memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int)); =09=09=09break; =09=09} =09} =20 +=09/* Clear out unused file descriptors */ +=09while (got_fds < max_fds) +=09=09fds[got_fds++] =3D -1; + =09return ret; } =20 diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c index 8a01c295e7..b8f6a9fba5 100644 --- a/lib/librte_vhost/vhost_user.c +++ b/lib/librte_vhost/vhost_user.c @@ -963,7 +963,7 @@ read_vhost_message(int sockfd, struct VhostUserMsg *msg= ) =09int ret; =20 =09ret =3D read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE, -=09=09msg->fds, VHOST_MEMORY_MAX_NREGIONS); +=09=09msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num); =09if (ret <=3D 0) =09=09return ret; =20 diff --git a/lib/librte_vhost/vhost_user.h b/lib/librte_vhost/vhost_user.h index ba78d32684..ea9d304f82 100644 --- a/lib/librte_vhost/vhost_user.h +++ b/lib/librte_vhost/vhost_user.h @@ -110,6 +110,7 @@ typedef struct VhostUserMsg { =09=09VhostUserLog log; =09} payload; =09int fds[VHOST_MEMORY_MAX_NREGIONS]; +=09int fd_num; } __attribute((packed)) VhostUserMsg; =20 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64) @@ -122,7 +123,8 @@ typedef struct VhostUserMsg { int vhost_user_msg_handler(int vid, int fd); =20 /* socket.c */ -int read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_nu= m); +int read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_f= ds, +=09=09int *fd_num); int send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_nu= m); =20 #endif --=20 2.21.0