Re: [dpdk-dev] [PATCH] vhost: allow for many vhost user ports

2016-12-06 Thread Jan Wickbom
> -Original Message-
> From: Yuanhan Liu [mailto:yuanhan@linux.intel.com]
> Sent: den 6 december 2016 07:57
> To: Jan Wickbom 
> Cc: huawei@intel.com; dev@dpdk.org; Patrik Andersson R
> 
> Subject: Re: [PATCH] vhost: allow for many vhost user ports
> 
> On Thu, Dec 01, 2016 at 04:26:50PM +0100, Jan Wickbom wrote:
> > 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.
> 
> Yeah, it's a known issue for a while. Thanks for making a patch to fix
> that!

Thanks for your comments! Please see below for some questions/opinions.

> 
> > 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
> >
> ...
> > +/**
> > + * Adjusts the highest index populated in the array of fds
> 
> This function only shrinks (but not extends) the fdset array, so why
> not naming it to something like "fdset_shrink".
> 
> > + * @return
> > + *   Index of highest position populated + 1.
> 
> I think it's clearer to say "The new size of fdset".
> 
> 
> > @@ -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);
> 
> Unncessary cast.

I'd like to keep the cast. The function returns int and it's nice to show we 
deliberately
don't care. No strong opinion though, but we should do the same everywhere. 
Please
see below.

> 
> > 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);

If we remove the cast above, we should remove this one as well.

> >
> > 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);
> 
> There is neither a NULL check, nor a free after the usage. Since it's
> a fixed size at compile time (MAX_FDS), you might want to define a
> static array for that.
> 
>   --yliu


[dpdk-dev] [PATCH v2] vhost: allow for many vhost user ports

2016-12-07 Thread Jan Wickbom
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 
---

v2:
* removed unnecessary casts
* static array replacing allocated memory

 lib/librte_vhost/fd_man.c | 142 +++---
 lib/librte_vhost/fd_man.h |   2 +-
 2 files changed, 85 insertions(+), 59 deletions(-)

diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c
index 2d3eeb7..3c743e3 100644
--- a/lib/librte_vhost/fd_man.c
+++ b/lib/librte_vhost/fd_man.c
@@ -35,16 +35,43 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
+#include 
 #include 
+#include 
 
 #include 
+#include 
 #include 
 
 #include "fd_man.h"
 
+#define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
+
+
+static struct pollfd rwfds[MAX_FDS];
+
+/**
+ * Adjusts the highest index populated in the array of fds
+ * @return
+ *   The new size of fdset.
+ */
+static int
+fdset_shrink(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 +83,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_shrink(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 +149,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 = NUL

Re: [dpdk-dev] [PATCH] vhost: allow for many vhost user ports

2016-12-07 Thread Jan Wickbom


> -Original Message-
> From: Yuanhan Liu [mailto:yuanhan@linux.intel.com]
> Sent: den 7 december 2016 11:13
> To: Jan Wickbom 
> Cc: dev@dpdk.org; Patrik Andersson R 
> Subject: Re: [PATCH] vhost: allow for many vhost user ports
> 
> On Thu, Dec 01, 2016 at 04:26:50PM +0100, Jan Wickbom wrote:
> >  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;
> 
> Another thing is we don't have to re-init this rwfds array again
> and again. Instead, we could
> 
> - set it up correctly when fdset_add is invoked: set the fd and
>   events.
> 
> - reset revents when it's been handled at fdset_event_dispatch().
> 
> - swap with the last one and shrink the array on fd delete
> 
> Could you make a follow up patch for that?

I don't see how that could easily be done. The loop index, i, is a direct 
reference between
an entry in the rwfds array and an entry in the pfdset array. It should stay 
like that while we are
hanging in the poll(). If  an entry in the pfdset array is removed while we are 
hanging in the poll()
and we then immediately replaces it with the last entry in the array we will 
end up in trouble if the
revent gets set for the "replaced" index. The direct reference is gone.
Or am I missing something?
/jaw
> 
> Thanks.
> 
>   --yliu


[dpdk-dev] [PATCH v3] vhost: allow for many vhost user ports

2016-12-12 Thread Jan Wickbom
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 
---

v3:
* removed unnecessary include
* removed fdset_fill, made it functionally part of poll loop

v2:
* removed unnecessary casts
* static array replacing allocated memory

 lib/librte_vhost/fd_man.c | 194 +-
 lib/librte_vhost/fd_man.h |   2 +-
 2 files changed, 105 insertions(+), 91 deletions(-)

diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c
index 2d3eeb7..c360d07 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)
+
+
+static struct pollfd rwfds[MAX_FDS];
+
+/**
+ * Adjusts the highest index populated in the array of fds
+ * @return
+ *   The new size of fdset.
+ */
+static void
+fdset_shrink(struct fdset *pfdset)
+{
+   int idx;
+
+   for (idx = pfdset->num - 1;
+idx >= 0 && pfdset->fd[idx].fd == -1;
+idx--)
+   ;
+
+   pfdset->num = idx + 1;
+}
+
 /**
  * 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,32 @@
 {
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.
- * @return
- *  the maximum fds filled in the read/write fd_set.
- */
-static int
-fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
-{
-   struct fdentry *pfdentry;
-   int i, maxfds = -1;
-   int num = MAX_FDS;
-
-   if (pfdset == NULL)
-   return -1;
-
-   for (i = 0; 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;
-   }
-   }
-   return maxfds;
 }
 
 void
@@ -132,6 +116,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 +138,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 +176,7 @@
pfdset->fd[i].fd = -1;
pfdset-&

Re: [dpdk-dev] [PATCH] vhost: allow for many vhost user ports

2016-12-12 Thread Jan Wickbom


> -Original Message-
> From: Yuanhan Liu [mailto:yuanhan@linux.intel.com]
> Sent: den 8 december 2016 06:51
> To: Jan Wickbom 
> Cc: dev@dpdk.org; Patrik Andersson R 
> Subject: Re: [PATCH] vhost: allow for many vhost user ports
> 
> On Wed, Dec 07, 2016 at 01:23:48PM +, Jan Wickbom wrote:
> > > On Thu, Dec 01, 2016 at 04:26:50PM +0100, Jan Wickbom wrote:
> > > >  static int
> > > > -fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
> > > > +fdset_fill(struct pollfd *rwfds, struct fdset *pfdset)
> ...
> > > > +   rwfds[i].fd = pfdentry->fd;
> > > > +   rwfds[i].events = pfdentry->rcb ? POLLIN : 0;
> > > > +   rwfds[i].events |= pfdentry->wcb ? POLLOUT :
> > > 0;
> > >
> > > Another thing is we don't have to re-init this rwfds array again
> > > and again. Instead, we could
> > >
> > > - set it up correctly when fdset_add is invoked: set the fd and
> > >   events.
> > >
> > > - reset revents when it's been handled at fdset_event_dispatch().
> > >
> > > - swap with the last one and shrink the array on fd delete
> > >
> > > Could you make a follow up patch for that?
> >
> > I don't see how that could easily be done. The loop index, i, is a direct
> reference between
> > an entry in the rwfds array and an entry in the pfdset array. It should stay
> like that while we are
> > hanging in the poll(). If  an entry in the pfdset array is removed while we
> are hanging in the poll()
> > and we then immediately replaces it with the last entry in the array we will
> end up in trouble if the
> > revent gets set for the "replaced" index. The direct reference is gone.
> > Or am I missing something?
> 
> Yes, we should not shrink the rwfds during the poll, but we could later, at
> the end of the while() loop.

Did something in v3, please have a look.

> 
> Talking about that, you should not invoke fdset_shrink() inside fdset_del(),
> since it could be in the poll stage.

Thanks! Forgot this! Will send a v4

> 
>   --yliu


Re: [dpdk-dev] [PATCH v3] vhost: allow for many vhost user ports

2016-12-13 Thread Jan Wickbom


> -Original Message-
> From: Yuanhan Liu [mailto:yuanhan@linux.intel.com]
> Sent: den 13 december 2016 10:15
> To: Jan Wickbom 
> Cc: dev@dpdk.org; Patrik Andersson R 
> Subject: Re: [PATCH v3] vhost: allow for many vhost user ports
> 
> On Mon, Dec 12, 2016 at 05:50:34PM +0100, Jan Wickbom wrote:
> > 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 
> ...
> > +static struct pollfd rwfds[MAX_FDS];
> 
> Though it's unlikely, but just assume we have multiple instance of
> fdset_event_dispatch(pfdset), a global rwfds will not work.
> 
> Thought twice, and it's better to put it into the fdset struct:
> 
> struct fdset {
> struct fdentry fd[MAX_FDS];
> struct pollfd rwfds[MAX_FDS];
> ...
> 

Done


> >  /**
> >   * 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,55 +217,71 @@
> >  void
> >  fdset_event_dispatch(struct fdset *pfdset)
> >  {
> > -   fd_set rfds, wfds;
> > -   int i, maxfds;
> > +   int i;
> > struct fdentry *pfdentry;
> > -   int num = MAX_FDS;
> > fd_cb rcb, wcb;
> > void *dat;
> > int fd;
> > int remove1, remove2;
> > -   int ret;
> >
> > if (pfdset == NULL)
> > return;
> >
> > -   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);
> > -
> > -   pthread_mutex_unlock(&pfdset->fd_mutex);
> > +   memset(rwfds, 0, sizeof(rwfds));
> >
> > +   while (1) {
> > /*
> > -* 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);
> > -   if (ret <= 0)
> > -   continue;
> > +   poll(rwfds, pfdset->num, 1000 /* millisecs */);
> >
> > -   for (i = 0; i < num; i++) {
> > -   remove1 = remove2 = 0;
> > +   for (i = 0; i < pfdset->num; ) {
> > pthread_mutex_lock(&pfdset-
> >fd_mutex);
> > +
> > pfdentry = &pfdset->fd[i];
> > fd = pfdentry->fd;
> > +
> > +   if (fd < 0) {
> > +   /* Removed during
> poll */
> > +
> > +
>   fdset_move_last(pfdset, i);
> > +
>   fdset_shrink(pfdset);
> > +
> > +
>   pthread_mutex_unlock(&pfdset->fd_mutex);
> > +
> > +   continue;
> > +   }
> > +
> > +   if (!rwfds[i].revents) {
> > +   /* No revents,
> maybe added during poll */
> > +
> > +   rwfds[i].fd = fd;
> > +   rwfds[i].events =
> pfdentry->rcb ? POLLIN : 0;
> > +   rwfds[i].events |=
> pfdentry->wcb ? POLLOUT : 0;
> > +
>   pthread_mutex_unlock(&pfdset->fd_mutex);
> > +
> > +   i++;
> > +   continue;
> 
> I think it's error-prone to manipulate the rwfds here. Besides, it
> registers an fd repeatedly.
> 
> The way I think of is:

[dpdk-dev] [PATCH v4] vhost: allow for many vhost user ports

2016-12-13 Thread Jan Wickbom
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 
---

v4:
* fdset_del can not shrink the array. Took care of that in sveral places
* Moved rwfds[] into struct fdset

v3:
* removed unnecessary include
* removed fdset_fill, made it functionally part of poll loop

v2:
* removed unnecessary casts
* static array replacing allocated memory

 lib/librte_vhost/fd_man.c | 226 ++
 lib/librte_vhost/fd_man.h |   4 +-
 2 files changed, 131 insertions(+), 99 deletions(-)

diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c
index 2d3eeb7..d7c775d 100644
--- a/lib/librte_vhost/fd_man.c
+++ b/lib/librte_vhost/fd_man.c
@@ -35,16 +35,38 @@
 #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
+ *   The new size of fdset.
+ */
+static int
+fdset_shrink(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 +78,28 @@
 {
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;
-}
-
-static int
-fdset_find_free_slot(struct fdset *pfdset)
-{
-   return fdset_find_fd(pfdset, -1);
+   return i == pfdset->num ? -1 : i;
 }
 
-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;
+   struct fdentry *pfdentry = &pfdset->fd[idx];
+   struct pollfd *pfd = &pfdset->rwfds[idx];
 
-   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.
- * @return
- *  the maximum fds filled in the read/write fd_set.
- */
-static int
-fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
-{
-   struct fdentry *pfdentry;
-   int i, maxfds = -1;
-   int num = MAX_FDS;
-
-   if (pfdset == NULL)
-   return -1;
-
-   for (i = 0; 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;
-   }
-   }
-   return maxfds;
+   pfd->fd = fd;
+   pfd->events = rcb ? POLLIN : 0;
+   pfd->events |= wcb ? POLLOUT : 0;
+   pfd->revents = 0;
 }
 
 void
@@ -132,11 +110,15 @@
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;
}
pfdset->num = 0;
+
+   memset(pfdset->rwfds, 0, sizeof(pfdset->rwfds));
 }
 
 /**
@@ -152,14 +134,14 @@
 
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) {
+   i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
+
+   if (i == -1) {
pthread_m

[dpdk-dev] [PATCH v5] vhost: allow for many vhost user ports

2016-12-14 Thread Jan Wickbom
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 
---

v5:
* pack of arrays moved after poll loop

v4:
* fdset_del can not shrink the array. Took care of that in sveral places
* Moved rwfds[] into struct fdset

v3:
* removed unnecessary include
* removed fdset_fill, made it functionally part of poll loop

v2:
* removed unnecessary casts
* static array replacing allocated memory

 lib/librte_vhost/fd_man.c | 234 ++
 lib/librte_vhost/fd_man.h |   2 +
 2 files changed, 135 insertions(+), 101 deletions(-)

diff --git a/lib/librte_vhost/fd_man.c b/lib/librte_vhost/fd_man.c
index 2d3eeb7..c7c2b35 100644
--- a/lib/librte_vhost/fd_man.c
+++ b/lib/librte_vhost/fd_man.c
@@ -35,93 +35,111 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
 
 #include "fd_man.h"
 
+#define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
+
 /**
- * Returns the index in the fdset for a given fd.
- * If fd is -1, it means to search for a free entry.
+ * Adjusts the highest index populated in the array of fds
  * @return
- *   index for the fd, or -1 if fd isn't in the fdset.
+ *   The new size of fdset.
  */
 static int
-fdset_find_fd(struct fdset *pfdset, int fd)
+fdset_shrink(struct fdset *pfdset)
 {
-   int i;
+   int idx;
 
-   if (pfdset == NULL)
-   return -1;
+   /* Remove all empty spots in the end */
 
-   for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
+   for (idx = pfdset->num - 1;
+idx >= 0 && pfdset->fd[idx].fd == -1;
+idx--)
;
 
-   return i ==  MAX_FDS ? -1 : i;
+   pfdset->num = idx + 1;
+
+   return pfdset->num;
 }
 
-static int
-fdset_find_free_slot(struct fdset *pfdset)
+/**
+ *  Moves the fd from last slot to specified slot, including
+ *  corresponding pollfd
+ */
+static void
+fdset_move_last(struct fdset *pfdset, int idx)
 {
-   return fdset_find_fd(pfdset, -1);
+   int last_idx = pfdset->num - 1;
+
+   if (idx < last_idx) {
+   pfdset->fd[idx] = pfdset->fd[last_idx];
+   pfdset->fd[last_idx].fd = -1;
+
+   pfdset->rwfds[idx] = pfdset->rwfds[last_idx];
+   pfdset->rwfds[last_idx].revents = 0;
+   }
 }
 
-static int
-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;
+/**
+ *  Packs the two arrays in the fdset structure
+ */
+static void
+fdset_pack(struct fdset *pfdset)
+{
+   int idx;
+   int num = pfdset->num;
 
-   pfdentry = &pfdset->fd[idx];
-   pfdentry->fd = fd;
-   pfdentry->rcb = rcb;
-   pfdentry->wcb = wcb;
-   pfdentry->dat = dat;
+   for (idx = 0; idx < num; idx++) {
+   if (pfdset->fd[idx].fd == -1) {
+   num = fdset_shrink(pfdset);
+   fdset_move_last(pfdset, idx);
+   }
+   }
 
-   return 0;
+   fdset_shrink(pfdset);
 }
 
 /**
- * Fill the read/write fd_set with the fds in the fdset.
+ * Returns the index in the fdset for a given fd.
+ * If fd is -1, it means to search for a free entry.
  * @return
- *  the maximum fds filled in the read/write fd_set.
+ *   index for the fd, or -1 if fd isn't in the fdset.
  */
 static int
-fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
+fdset_find_fd(struct fdset *pfdset, int fd)
 {
-   struct fdentry *pfdentry;
-   int i, maxfds = -1;
-   int num = MAX_FDS;
+   int i;
 
-   if (pfdset == NULL)
-   return -1;
+   for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++)
+   ;
 
-   for (i = 0; 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 = pf

[dpdk-dev] [PATCH] vhost: allow for many vhost user ports

2016-12-01 Thread Jan Wickbom
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 t