From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sesbmg22.ericsson.net (sesbmg22.ericsson.net [193.180.251.48]) by dpdk.org (Postfix) with ESMTP id D3F2F559C for ; Thu, 1 Dec 2016 16:27:15 +0100 (CET) X-AuditID: c1b4fb30-851fe70000000c18-72-584041511e56 Received: from ESESSHC022.ericsson.se (Unknown_Domain [153.88.183.84]) by (Symantec Mail Security) with SMTP id 0A.DB.03096.15140485; Thu, 1 Dec 2016 16:27:15 +0100 (CET) Received: from cloudvm.ki.sw.ericsson.se (153.88.183.153) by smtps.internal.ericsson.com (153.88.183.84) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 1 Dec 2016 16:27:13 +0100 From: Jan Wickbom To: , CC: , , Jan Wickbom Date: Thu, 1 Dec 2016 16:26:50 +0100 Message-ID: <1480606010-6132-1-git-send-email-jan.wickbom@ericsson.com> X-Mailer: git-send-email 1.9.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [153.88.183.153] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprJLMWRmVeSWpSXmKPExsUyM2J7iG6wo0OEwcJNfBbvPm1nsmifeZbJ 4vqEC6wOzB6/Fixl9Vi85yWTx7yTgQHMUVw2Kak5mWWpRfp2CVwZ91a8YC2Ya1tx8d8dxgbG w3pdjJwcEgImEr//LGPtYuTiEBJYxyhxtXMHG4Szm1Hi16dlbCBVbAI6EqvfdrCC2CIClhLP n05kArGZBeIlvt68A2YLC5hLNC95xAxiswioSLQencPSxcjBwSvgLtHREA+xTE7i5LHJYGN4 BQQlTs58wgIxRkLi4IsXYK1CAqoSzxc2MEHUK0lcn3edZQIj3ywkLbOQtCxgZFrFKFqcWpyU m25kpJdalJlcXJyfp5eXWrKJERhgB7f8NtjB+PK54yFGAQ5GJR7eD1YOEUKsiWXFlbmHGCU4 mJVEeL/bAIV4UxIrq1KL8uOLSnNSiw8xSnOwKInzmq28Hy4kkJ5YkpqdmlqQWgSTZeLglGpg XLb8ns2Vdh/nxNTD8zoKfts+NjBYOU/CeNN7Bb3bK+8tnlO397gL48N+0xfb9s4MiPnhmyw0 vfKn5f/Lpnp/+fTvT/oh/3Kq105lCc+/zWmvrt+KFD9skHPyoBOvfdHyH5LXvVVO9RnLtU8V j1biaz2g5PiiPN1+y5LmB7fMNmV1Ptvm3rm/TYmlOCPRUIu5qDgRAIwQJAssAgAA Subject: [dpdk-dev] [PATCH] vhost: allow for many vhost user ports 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: , X-List-Received-Date: Thu, 01 Dec 2016 15:27:16 -0000 Currently select() is used to monitor file descriptors for vhostuser ports. This limits the number of ports possible to create since the fd number is used as index in the fd_set and we have seen fds > 1023. This patch changes select() to poll(). This way we can keep an packed (pollfd) array for the fds, e.g. as many fds as the size of the array. Also see: http://dpdk.org/ml/archives/dev/2016-April/037024.html Signed-off-by: Jan Wickbom Reported-by: Patrik Andersson --- lib/librte_vhost/fd_man.c | 140 +++++++++++++++++++++++++++------------------- lib/librte_vhost/fd_man.h | 2 +- 2 files changed, 83 insertions(+), 59 deletions(-) diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c index 2d3eeb7..dc4aa3f 100644 --- a/lib/librte_vhost/fd_man.c +++ b/lib/librte_vhost/fd_man.c @@ -35,16 +35,40 @@ #include #include #include -#include #include #include +#include #include #include +#include #include #include "fd_man.h" +#define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL) + + +/** + * Adjusts the highest index populated in the array of fds + * @return + * Index of highest position populated + 1. + */ +static int +fdset_adjust_num(struct fdset *pfdset) +{ + int idx; + + for (idx = pfdset->num - 1; + idx >= 0 && pfdset->fd[idx].fd == -1; + idx--) + ; + + pfdset->num = idx + 1; + + return pfdset->num; +} + /** * Returns the index in the fdset for a given fd. * If fd is -1, it means to search for a free entry. @@ -56,72 +80,62 @@ { int i; - if (pfdset == NULL) - return -1; - - for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++) + for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++) ; - return i == MAX_FDS ? -1 : i; + return i == pfdset->num ? -1 : i; } static int fdset_find_free_slot(struct fdset *pfdset) { - return fdset_find_fd(pfdset, -1); + if (pfdset->num < MAX_FDS) + return pfdset->num; + else + return fdset_find_fd(pfdset, -1); } -static int -fdset_add_fd(struct fdset *pfdset, int idx, int fd, +static void +fdset_add_fd(struct fdset *pfdset, int idx, int fd, fd_cb rcb, fd_cb wcb, void *dat) { struct fdentry *pfdentry; - if (pfdset == NULL || idx >= MAX_FDS || fd >= FD_SETSIZE) - return -1; - pfdentry = &pfdset->fd[idx]; pfdentry->fd = fd; pfdentry->rcb = rcb; pfdentry->wcb = wcb; pfdentry->dat = dat; - - return 0; } /** - * Fill the read/write fd_set with the fds in the fdset. + * Compact the fdset and fill the read/write fds with the fds in the fdset. * @return - * the maximum fds filled in the read/write fd_set. + * the number of fds filled in the read/write fds. */ static int -fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset) +fdset_fill(struct pollfd *rwfds, struct fdset *pfdset) { struct fdentry *pfdentry; - int i, maxfds = -1; - int num = MAX_FDS; - - if (pfdset == NULL) - return -1; + int i; + int num; - for (i = 0; i < num; i++) { + for (i = 0, num = pfdset->num; i < num; i++) { pfdentry = &pfdset->fd[i]; - if (pfdentry->fd != -1) { - int added = 0; - if (pfdentry->rcb && rfset) { - FD_SET(pfdentry->fd, rfset); - added = 1; - } - if (pfdentry->wcb && wfset) { - FD_SET(pfdentry->fd, wfset); - added = 1; - } - if (added) - maxfds = pfdentry->fd < maxfds ? - maxfds : pfdentry->fd; + + if (pfdentry->fd < 0) { + /* Hole in the list. Move the last one here */ + + *pfdentry = pfdset->fd[num - 1]; + pfdset->fd[num - 1].fd = -1; + num = fdset_adjust_num(pfdset); } + rwfds[i].fd = pfdentry->fd; + rwfds[i].events = pfdentry->rcb ? POLLIN : 0; + rwfds[i].events |= pfdentry->wcb ? POLLOUT : 0; } - return maxfds; + + return i; } void @@ -132,6 +146,8 @@ if (pfdset == NULL) return; + pthread_mutex_init(&pfdset->fd_mutex, NULL); + for (i = 0; i < MAX_FDS; i++) { pfdset->fd[i].fd = -1; pfdset->fd[i].dat = NULL; @@ -152,14 +168,15 @@ pthread_mutex_lock(&pfdset->fd_mutex); - /* Find a free slot in the list. */ i = fdset_find_free_slot(pfdset); - if (i == -1 || fdset_add_fd(pfdset, i, fd, rcb, wcb, dat) < 0) { + if (i == -1) { pthread_mutex_unlock(&pfdset->fd_mutex); return -2; } - pfdset->num++; + fdset_add_fd(pfdset, i, fd, rcb, wcb, dat); + if (i == pfdset->num) + pfdset->num++; pthread_mutex_unlock(&pfdset->fd_mutex); @@ -189,7 +206,7 @@ pfdset->fd[i].fd = -1; pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL; pfdset->fd[i].dat = NULL; - pfdset->num--; + (void) fdset_adjust_num(pfdset); i = -1; } pthread_mutex_unlock(&pfdset->fd_mutex); @@ -211,12 +228,12 @@ pfdset->fd[index].fd = -1; pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL; - pfdset->fd[index].dat = NULL; - pfdset->num--; + (void) fdset_adjust_num(pfdset); pthread_mutex_unlock(&pfdset->fd_mutex); } + /** * This functions runs in infinite blocking loop until there is no fd in * pfdset. It calls corresponding r/w handler if there is event on the fd. @@ -229,44 +246,48 @@ void fdset_event_dispatch(struct fdset *pfdset) { - fd_set rfds, wfds; - int i, maxfds; + int i; struct fdentry *pfdentry; - int num = MAX_FDS; + int numfds; fd_cb rcb, wcb; void *dat; int fd; int remove1, remove2; int ret; + int handled; if (pfdset == NULL) return; + struct pollfd * const rwfds = + rte_malloc("struct pollfd", MAX_FDS * sizeof(*rwfds), 0); + while (1) { - struct timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - FD_ZERO(&rfds); - FD_ZERO(&wfds); pthread_mutex_lock(&pfdset->fd_mutex); - maxfds = fdset_fill(&rfds, &wfds, pfdset); + numfds = fdset_fill(rwfds, pfdset); pthread_mutex_unlock(&pfdset->fd_mutex); /* - * When select is blocked, other threads might unregister + * When poll is blocked, other threads might unregister * listenfds from and register new listenfds into fdset. - * When select returns, the entries for listenfds in the fdset + * When poll returns, the entries for listenfds in the fdset * might have been updated. It is ok if there is unwanted call * for new listenfds. */ - ret = select(maxfds + 1, &rfds, &wfds, NULL, &tv); + ret = poll(rwfds, numfds, 1000 /* millisecs */); + if (ret <= 0) continue; - for (i = 0; i < num; i++) { + for (i = handled = 0; i < numfds && handled < ret; i++) { + if (!rwfds[i].revents) + continue; + + handled++; remove1 = remove2 = 0; + pthread_mutex_lock(&pfdset->fd_mutex); pfdentry = &pfdset->fd[i]; fd = pfdentry->fd; @@ -275,9 +296,12 @@ dat = pfdentry->dat; pfdentry->busy = 1; pthread_mutex_unlock(&pfdset->fd_mutex); - if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb) + + if (fd >= 0 && rcb && + rwfds[i].revents & (POLLIN | FDPOLLERR)) rcb(fd, dat, &remove1); - if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb) + if (fd >= 0 && wcb && + rwfds[i].revents & (POLLOUT | FDPOLLERR)) wcb(fd, dat, &remove2); pfdentry->busy = 0; /* diff --git a/lib/librte_vhost/fd_man.h b/lib/librte_vhost/fd_man.h index bd66ed1..b5ba688 100644 --- a/lib/librte_vhost/fd_man.h +++ b/lib/librte_vhost/fd_man.h @@ -51,7 +51,7 @@ struct fdentry { struct fdset { struct fdentry fd[MAX_FDS]; pthread_mutex_t fd_mutex; - int num; /* current fd number of this fdset */ + int num; /* highest index occupied in fd array + 1 */ }; -- 1.9.1