From patchwork Fri Jan 30 06:36:27 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huawei Xie X-Patchwork-Id: 2806 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 561355ABF; Fri, 30 Jan 2015 07:37:23 +0100 (CET) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by dpdk.org (Postfix) with ESMTP id 1C84E5A81 for ; Fri, 30 Jan 2015 07:37:14 +0100 (CET) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP; 29 Jan 2015 22:30:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.09,490,1418112000"; d="scan'208";a="678416640" Received: from shvmail01.sh.intel.com ([10.239.29.42]) by orsmga002.jf.intel.com with ESMTP; 29 Jan 2015 22:37:12 -0800 Received: from shecgisg003.sh.intel.com (shecgisg003.sh.intel.com [10.239.29.90]) by shvmail01.sh.intel.com with ESMTP id t0U6b9n6008154; Fri, 30 Jan 2015 14:37:09 +0800 Received: from shecgisg003.sh.intel.com (localhost [127.0.0.1]) by shecgisg003.sh.intel.com (8.13.6/8.13.6/SuSE Linux 0.8) with ESMTP id t0U6b8nG012128; Fri, 30 Jan 2015 14:37:10 +0800 Received: (from hxie5@localhost) by shecgisg003.sh.intel.com (8.13.6/8.13.6/Submit) id t0U6b7nE012124; Fri, 30 Jan 2015 14:37:07 +0800 From: Huawei Xie To: dev@dpdk.org Date: Fri, 30 Jan 2015 14:36:27 +0800 Message-Id: <1422599787-12009-13-git-send-email-huawei.xie@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1422599787-12009-1-git-send-email-huawei.xie@intel.com> References: <1422599787-12009-1-git-send-email-huawei.xie@intel.com> Subject: [dpdk-dev] [PATCH 12/12] lib/librte_vhost: support calling rte_vhost_driver_register after rte_vhost_driver_session_start X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" add mutext to protect fdset Signed-off-by: Huawei Xie --- lib/librte_vhost/vhost_user/fd_man.c | 39 +++++++++++++++++++++++----- lib/librte_vhost/vhost_user/fd_man.h | 2 ++ lib/librte_vhost/vhost_user/vhost-net-user.c | 19 +++++++++----- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/lib/librte_vhost/vhost_user/fd_man.c b/lib/librte_vhost/vhost_user/fd_man.c index 929fbc3..e86615d 100644 --- a/lib/librte_vhost/vhost_user/fd_man.c +++ b/lib/librte_vhost/vhost_user/fd_man.c @@ -145,6 +145,8 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat) if (pfdset == NULL || fd == -1) return -1; + pthread_mutex_lock(&pfdset->fd_mutex); + /* Find a free slot in the list. */ i = fdset_find_free_slot(pfdset); if (i == -1) @@ -153,6 +155,8 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat) fdset_add_fd(pfdset, i, fd, rcb, wcb, dat); pfdset->num++; + pthread_mutex_unlock(&pfdset->fd_mutex); + return 0; } @@ -164,12 +168,19 @@ fdset_del(struct fdset *pfdset, int fd) { int i; + if (pfdset == NULL || fd == -1) + return; + + pthread_mutex_lock(&pfdset->fd_mutex); + i = fdset_find_fd(pfdset, fd); if (i != -1 && fd != -1) { pfdset->fd[i].fd = -1; pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL; pfdset->num--; } + + pthread_mutex_unlock(&pfdset->fd_mutex); } /** @@ -183,6 +194,9 @@ fdset_event_dispatch(struct fdset *pfdset) int i, maxfds; struct fdentry *pfdentry; int num = MAX_FDS; + fd_cb rcb, wcb; + void *dat; + int fd; if (pfdset == NULL) return; @@ -190,18 +204,31 @@ fdset_event_dispatch(struct fdset *pfdset) while (1) { FD_ZERO(&rfds); FD_ZERO(&wfds); + pthread_mutex_lock(&pfdset->fd_mutex); + maxfds = fdset_fill(&rfds, &wfds, pfdset); - if (maxfds == -1) - return; + if (maxfds == -1) { + pthread_mutex_unlock(&pfdset->fd_mutex); + sleep(1); + continue; + } + + pthread_mutex_unlock(&pfdset->fd_mutex); select(maxfds + 1, &rfds, &wfds, NULL, NULL); for (i = 0; i < num; i++) { + pthread_mutex_lock(&pfdset->fd_mutex); pfdentry = &pfdset->fd[i]; - if (pfdentry->fd >= 0 && FD_ISSET(pfdentry->fd, &rfds) && pfdentry->rcb) - pfdentry->rcb(pfdentry->fd, pfdentry->dat); - if (pfdentry->fd >= 0 && FD_ISSET(pfdentry->fd, &wfds) && pfdentry->wcb) - pfdentry->wcb(pfdentry->fd, pfdentry->dat); + fd = pfdentry->fd; + rcb = pfdentry->rcb; + wcb = pfdentry->wcb; + dat = pfdentry->dat; + pthread_mutex_unlock(&pfdset->fd_mutex); + if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb) + rcb(fd, dat); + if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb) + wcb(fd, dat); } } } diff --git a/lib/librte_vhost/vhost_user/fd_man.h b/lib/librte_vhost/vhost_user/fd_man.h index 26b4619..4ebae57 100644 --- a/lib/librte_vhost/vhost_user/fd_man.h +++ b/lib/librte_vhost/vhost_user/fd_man.h @@ -34,6 +34,7 @@ #ifndef _FD_MAN_H_ #define _FD_MAN_H_ #include +#include #define MAX_FDS 1024 @@ -48,6 +49,7 @@ struct fdentry { struct fdset { struct fdentry fd[MAX_FDS]; + pthread_mutex_t fd_mutex; int num; /* current fd number of this fdset */ }; diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.c b/lib/librte_vhost/vhost_user/vhost-net-user.c index 44ef398..e6df8a8 100644 --- a/lib/librte_vhost/vhost_user/vhost-net-user.c +++ b/lib/librte_vhost/vhost_user/vhost-net-user.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -60,10 +61,18 @@ struct connfd_ctx { }; #define MAX_VHOST_SERVER 1024 -static struct { +struct _vhost_server { struct vhost_server *server[MAX_VHOST_SERVER]; - struct fdset fdset; /**< The fd list this vhost server manages. */ -} g_vhost_server; + struct fdset fdset; +}; + +static struct _vhost_server g_vhost_server = { + .fdset = { + .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL} }, + .fd_mutex = PTHREAD_MUTEX_INITIALIZER, + .num = 0 + }, +}; static int vserver_idx; @@ -423,10 +432,8 @@ rte_vhost_driver_register(const char *path) { struct vhost_server *vserver; - if (vserver_idx == 0) { - fdset_init(&g_vhost_server.fdset); + if (vserver_idx == 0) ops = get_virtio_net_callbacks(); - } if (vserver_idx == MAX_VHOST_SERVER) return -1;