Skip to contentSkip to conversation

vhost: allow for many vhost user ports

PATCH Superseded Jan Wickbom 3 participants 19 messages Dec 1, 2016 – Jan 12, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vhost: allow for many vhost user ports

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

---
 lib/librte_vhost/fd_man.c | 140 +++++++++++++++++++++++++++-------------------
 lib/librte_vhost/fd_man.h |   2 +-
 2 files changed, 83 insertions(+), 59 deletions(-)
lib/librte_vhost/fd_man.c +82 -58
index 2d3eeb7..dc4aa3f 100644
@@ -35,16 +35,40 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <poll.h>
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_malloc.h>
 #include <rte_log.h>
 
 #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;
 			/*
lib/librte_vhost/fd_man.h +1 -1
index bd66ed1..b5ba688 100644
@@ -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 */
 };
 
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 3 lines · 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.

Yeah, it's a known issue for a while. Thanks for making a patch to fix that!

Quoted 7 lines

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

...

Quoted 2 lines

+/**
+ * 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".

Quoted 2 lines

+ * @return
+ * Index of highest position populated + 1.

I think it's clearer to say "The new size of fdset".

lib/librte_vhost/fd_man.c line 209

Quoted 6 lines
lib/librte_vhost/fd_man.c +1 -1
@@ -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.

Quoted 30 lines
lib/librte_vhost/fd_man.c +4 -5
 			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;

lib/librte_vhost/fd_man.c line 263

Quoted 10 lines
lib/librte_vhost/fd_man.c +3 -0
 	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
#2 lore raw
Quoted 8 lines

-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: den 6 december 2016 07:57
To: Jan Wickbom <[email protected]>
Cc: [email protected]; [email protected]; Patrik Andersson R <[email protected]>
Subject: Re: [PATCH] vhost: allow for many vhost user ports

Quoted 7 lines
Quoted 3 lines · 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.

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.

Quoted 21 lines
Quoted 8 lines

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

...

Quoted 2 lines

+/**
+ * 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".

Quoted 2 lines

+ * @return
+ * Index of highest position populated + 1.

I think it's clearer to say "The new size of fdset".

Quoted 9 lines

lib/librte_vhost/fd_man.c line 210

Quoted 7 lines
lib/librte_vhost/fd_man.c +1 -1
@@ -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.

Quoted 11 lines

lib/librte_vhost/fd_man.c line 231

Quoted 11 lines
(quoted diff) +0 -0
 			i = -1;
 		}
lib/librte_vhost/fd_man.c +1 -2
 		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.

Quoted 37 lines
Quoted 20 lines
lib/librte_vhost/fd_man.c +3 -3
 	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;

lib/librte_vhost/fd_man.c line 263

Quoted 10 lines
lib/librte_vhost/fd_man.c +3 -0
 	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
#3 lore raw
Quoted 10 lines · Jan Wickbom
Quoted 9 lines

lib/librte_vhost/fd_man.c line 210

Quoted 7 lines
lib/librte_vhost/fd_man.c +1 -1
@@ -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.

Quoted 2 lines

I'd like to keep the cast. The function returns int and it's nice to show we deliberately don't care.

I know your point, but it's not needed.

Quoted 9 lines

No strong opinion though, but we should do the same everywhere. Please see below.

Quoted 6 lines
Quoted 6 lines
(quoted diff) +0 -0
 			i = -1;
 		}
(quoted diff) +0 -0
 		pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -211,12 +228,12 @@
 
Quoted 7 lines
Quoted 5 lines

lib/librte_vhost/fd_man.c line 231

Quoted 5 lines
lib/librte_vhost/fd_man.c +1 -2
 	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.

Yes.

	--yliu
#4 lore raw
In reply to Jan Wickbom
Quoted 30 lines · Jan Wickbom

static int

lib/librte_vhost/fd_man.c +5 -20
-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;
+

lib/librte_vhost/fd_man.c line 135

Quoted 10 lines
lib/librte_vhost/fd_man.c +9 -0
+		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?

Thanks.

	--yliu
#5 lore raw
Quoted 61 lines

-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: den 7 december 2016 11:13
To: Jan Wickbom <[email protected]>
Cc: [email protected]; Patrik Andersson R <[email protected]>
Subject: Re: [PATCH] vhost: allow for many vhost user ports

Quoted 30 lines · Jan Wickbom

static int

lib/librte_vhost/fd_man.c +4 -11
-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);

lib/librte_vhost/fd_man.c +0 -4
-				added = 1;
-			}
-			if (pfdentry->wcb && wfset) {
-				FD_SET(pfdentry-

fd, wfset);

lib/librte_vhost/fd_man.c +0 -4
-				added = 1;
-			}
-			if (added)
-				maxfds = pfdentry-
Quoted 2 lines

fd < maxfds ?
-

	maxfds : pfdentry->fd;

lib/librte_vhost/fd_man.c

Quoted 3 lines
lib/librte_vhost/fd_man.c +3 -0
+
+		if (pfdentry->fd < 0) {
+			/* Hole in the list. Move the last

one here */

lib/librte_vhost/fd_man.c

Quoted 4 lines
lib/librte_vhost/fd_man.c +4 -0
+
+			*pfdentry = pfdset->fd[num - 1];
+			pfdset->fd[num - 1].fd = -1;
+			num =

fdset_adjust_num(pfdset);

lib/librte_vhost/fd_man.c

Quoted 4 lines
lib/librte_vhost/fd_man.c +3 -0
 		}
+		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

Quoted 8 lines

- 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

Quoted 4 lines

Thanks.

	--yliu
#6 lore raw
Quoted 4 lines · Jan Wickbom
Quoted 4 lines · Jan Wickbom
Quoted 3 lines

static int

lib/librte_vhost/fd_man.c +1 -1
-fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
+fdset_fill(struct pollfd *rwfds, struct fdset *pfdset)

...

Quoted 15 lines
Quoted 15 lines

lib/librte_vhost/fd_man.c

Quoted 3 lines
lib/librte_vhost/fd_man.c +3 -0
+		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

Quoted 8 lines
Quoted 1 line

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.

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

	--yliu
#7 lore raw
Quoted 37 lines

-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: den 8 december 2016 06:51
To: Jan Wickbom <[email protected]>
Cc: [email protected]; Patrik Andersson R <[email protected]>
Subject: Re: [PATCH] vhost: allow for many vhost user ports

Quoted 4 lines · Jan Wickbom
Quoted 4 lines · Jan Wickbom
Quoted 3 lines

static int

lib/librte_vhost/fd_man.c +1 -1
-fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
+fdset_fill(struct pollfd *rwfds, struct fdset *pfdset)

...

Quoted 11 lines
Quoted 11 lines

lib/librte_vhost/fd_man.c

Quoted 3 lines
lib/librte_vhost/fd_man.c +3 -0
+		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.

Quoted 7 lines
Quoted 5 lines

- 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

Quoted 1 line

an entry in the rwfds array and an entry in the pfdset array. It should stay

like that while we are

Quoted 1 line

hanging in the poll(). If an entry in the pfdset array is removed while we

Quoted 8 lines

are hanging in the poll()

Quoted 1 line

and we then immediately replaces it with the last entry in the array we will

end up in trouble if the

Quoted 2 lines

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.

Quoted 3 lines

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

Quoted 2 lines
	--yliu
#8 lore raw

PATCH v2 1/1 vhost: allow for many vhost user ports

In reply to 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

---

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(-)
lib/librte_vhost/fd_man.c +84 -58
index 2d3eeb7..3c743e3 100644
@@ -35,16 +35,43 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <poll.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <rte_common.h>
+#include <rte_malloc.h>
 #include <rte_log.h>
 
 #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 = NULL;
@@ -152,14 +171,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 +209,7 @@
 			pfdset->fd[i].fd = -1;
 			pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
 			pfdset->fd[i].dat = NULL;
-			pfdset->num--;
+			fdset_shrink(pfdset);
 			i = -1;
 		}
 		pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -211,12 +231,12 @@
 
 	pfdset->fd[index].fd = -1;
 	pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
-	pfdset->fd[index].dat = NULL;
-	pfdset->num--;
+	fdset_shrink(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 +249,47 @@
 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;
 
+	memset(rwfds, 0, sizeof(rwfds));
+
 	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 +298,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;
 			/*
lib/librte_vhost/fd_man.h +1 -1
index bd66ed1..b5ba688 100644
@@ -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 */
 };
 
 
Signature · 1 line
1.9.1
#9 lore raw

PATCH v3 1/1 vhost: allow for many vhost user ports

In reply to 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

---

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(-)
lib/librte_vhost/fd_man.c +104 -90
index 2d3eeb7..c360d07 100644
@@ -35,16 +35,40 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <poll.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
 
 #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->fd[i].rcb = pfdset->fd[i].wcb = NULL;
 			pfdset->fd[i].dat = NULL;
-			pfdset->num--;
+			fdset_shrink(pfdset);
 			i = -1;
 		}
 		pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -198,25 +185,26 @@
 	return dat;
 }
 
+
 /**
- *  Unregister the fd at the specified slot from the fdset.
+ *  Moves the fd from last slot to specified slot, including
+ *  corresponding pollfd
  */
 static void
-fdset_del_slot(struct fdset *pfdset, int index)
+fdset_move_last(struct fdset *pfdset, int idx)
 {
-	if (pfdset == NULL || index < 0 || index >= MAX_FDS)
-		return;
-
-	pthread_mutex_lock(&pfdset->fd_mutex);
+	int last_idx = pfdset->num - 1;
 
-	pfdset->fd[index].fd = -1;
-	pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
-	pfdset->fd[index].dat = NULL;
-	pfdset->num--;
+	if (idx < last_idx) {
+		pfdset->fd[idx] = pfdset->fd[last_idx];
+		pfdset->fd[last_idx].fd = -1;
 
-	pthread_mutex_unlock(&pfdset->fd_mutex);
+		rwfds[idx] = rwfds[last_idx];
+		rwfds[last_idx].revents = 0;
+	}
 }
 
+
 /**
  * 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;
+			}
+
+			/* Valid fd, and at least one revent ... */
+
+			remove1 = remove2 = 0;
+
 			rcb = pfdentry->rcb;
 			wcb = pfdentry->wcb;
 			dat = pfdentry->dat;
 			pfdentry->busy = 1;
+
 			pthread_mutex_unlock(&pfdset->fd_mutex);
-			if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
+
+			if (rcb && rwfds[i].revents & (POLLIN | FDPOLLERR))
 				rcb(fd, dat, &remove1);
-			if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
+			if (wcb && rwfds[i].revents & (POLLOUT | FDPOLLERR))
 				wcb(fd, dat, &remove2);
 			pfdentry->busy = 0;
 			/*
@@ -292,8 +296,18 @@
 			 * listen fd in another thread, we couldn't call
 			 * fd_set_del.
 			 */
-			if (remove1 || remove2)
-				fdset_del_slot(pfdset, i);
+			if (remove1 || remove2) {
+				pthread_mutex_lock(&pfdset->fd_mutex);
+
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				continue;
+			}
+
+			i++;
 		}
 	}
 }
lib/librte_vhost/fd_man.h +1 -1
index bd66ed1..b5ba688 100644
@@ -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 */
 };
 
 
Signature · 1 line
1.9.1
#10 lore raw
Quoted 12 lines · 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

...

Quoted 1 line

+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];
            ...
Quoted 68 lines

/**
* 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.

lib/librte_vhost/fd_man.c +20 -22
@@ -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;
+			}
+

lib/librte_vhost/fd_man.c line 268

Quoted 10 lines
lib/librte_vhost/fd_man.c +10 -0
+			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:

- set rwfds[i] at fdset_add().

This also simply makes sure that pfdset->rwfds[i] and pfdset->fd[i] is correctly bond.

	fdset_add(fdset, fd, ...) {
		lock();

		i = fdset_find_free_slot(..);

		pfdset->fd[i]->fd  = fd;
		pfdset->fd[i]->rcb = rcb;
		pfdset->fd[i]->...;

		pfdset->rwfds[i]->fd = fd;
		pfdset->rwfds[i]->events  = ...;
		pfdset->rwfds[i]->revents = 0;
	}

- set pfdset->fd[i]->fd = -1 on fdset_del. Note we should not decrease 'num' here, as we may be at poll.

	fdset_del(fdset, fd) {
		lock();

		i = fdset_find_fd(pfdset, fd);
		pfdset->fd[i]->fd = -1;

		...
	}

- log down pfdset->num before poll, because 'num' may increase during poll.

I think it's optional, since even 'num' is increased during poll, it just leads to few more rwfds entries will be accessed. But it's not tracked by kernel, and revents is initiated with 0, that there is no issue.

- shrink the fdset and rwfds (together) for those removed entries, __outside__ the for loop after poll.

Works to you?

	--yliu
#11 lore raw
Quoted 28 lines

-----Original Message-----
From: Yuanhan Liu [mailto:[email protected]]
Sent: den 13 december 2016 10:15
To: Jan Wickbom <[email protected]>
Cc: [email protected]; Patrik Andersson R <[email protected]>
Subject: Re: [PATCH v3] vhost: allow for many vhost user ports

Quoted 12 lines · 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

...

Quoted 1 line

+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.

Quoted 7 lines

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

Quoted 141 lines
Quoted 27 lines

/**
* 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.

lib/librte_vhost/fd_man.c +1 -10
@@ -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);

lib/librte_vhost/fd_man.c line 258

Quoted 10 lines
lib/librte_vhost/fd_man.c +2 -6
-		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

Review line 234

Quoted 1 line
(quoted diff) +1 -0
+		 * When poll is blocked, other threads might

unregister

Review line 235

Quoted 1 line
(quoted diff) +0 -0
 		 * listenfds from and register new listenfds into

fdset.

Review line 260

Quoted 1 line
(quoted diff) +0 -1
-		 * When select returns, the entries for listenfds

in the fdset

Review line 236

Quoted 1 line
(quoted diff) +1 -0
+		 * When poll returns, the entries for listenfds in

the fdset

Review line 237

Quoted 1 line
(quoted diff) +0 -0
 		 * might have been updated. It is ok if there is

unwanted call

lib/librte_vhost/fd_man.c

Quoted 3 lines
lib/librte_vhost/fd_man.c +0 -1
 		 * for new listenfds.
 		 */
-		ret = select(maxfds + 1, &rfds, &wfds, NULL,

&tv);

Quoted 5 lines
lib/librte_vhost/fd_man.c +1 -3
-		if (ret <= 0)
-			continue;
+		poll(rwfds, pfdset->num, 1000 /* millisecs */);
 
-		for (i = 0; i < num; i++) {

lib/librte_vhost/fd_man.c

Quoted 10 lines
lib/librte_vhost/fd_man.c +5 -1
-			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 */

Review line 252

Quoted 2 lines
(quoted diff) +2 -0
+
+
	fdset_move_last(pfdset, i);

Review line 253

Quoted 1 line
(quoted diff) +1 -0
+
	fdset_shrink(pfdset);

Review line 255

Quoted 2 lines
(quoted diff) +2 -0
+
+
	pthread_mutex_unlock(&pfdset->fd_mutex);

lib/librte_vhost/fd_man.c

Quoted 6 lines
lib/librte_vhost/fd_man.c +6 -0
+
+				continue;
+			}
+
+			if (!rwfds[i].revents) {
+				/* No revents,

maybe added during poll */

lib/librte_vhost/fd_man.c

Quoted 3 lines
lib/librte_vhost/fd_man.c +3 -0
+
+				rwfds[i].fd = fd;
+				rwfds[i].events =

pfdentry->rcb ? POLLIN : 0;

Review line 265

Quoted 1 line
(quoted diff) +1 -0
+				rwfds[i].events |=

pfdentry->wcb ? POLLOUT : 0;

Review line 266

Quoted 1 line
(quoted diff) +1 -0
+
	pthread_mutex_unlock(&pfdset->fd_mutex);

lib/librte_vhost/fd_man.c line 268

Quoted 3 lines
lib/librte_vhost/fd_man.c +3 -0
+
+				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:

- set rwfds[i] at fdset_add().

This also simply makes sure that pfdset->rwfds[i] and pfdset->fd[i] is correctly bond.

	fdset_add(fdset, fd, ...) {
		lock();

		i = fdset_find_free_slot(..);

		pfdset->fd[i]->fd  = fd;
		pfdset->fd[i]->rcb = rcb;
		pfdset->fd[i]->...;

		pfdset->rwfds[i]->fd = fd;
		pfdset->rwfds[i]->events  = ...;
		pfdset->rwfds[i]->revents = 0;
	}

- set pfdset->fd[i]->fd = -1 on fdset_del. Note we should not decrease 'num' here, as we may be at poll.

	fdset_del(fdset, fd) {
		lock();

		i = fdset_find_fd(pfdset, fd);
		pfdset->fd[i]->fd = -1;

		...
	}

- log down pfdset->num before poll, because 'num' may increase during poll.

I think it's optional, since even 'num' is increased during poll, it just leads to few more rwfds entries will be accessed. But it's not tracked by kernel, and revents is initiated with 0, that there is no issue.

Quoted 5 lines

- shrink the fdset and rwfds (together) for those removed entries, __outside__
the for loop after poll.

Works to you?

I did quite of a rework of this yesterday before reading your mail. I think That should work, please have a look.
V4 coming in a minute

Quoted 2 lines
	--yliu
#12 lore raw

PATCH v4 1/1 vhost: allow for many vhost user ports

In reply to 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

---

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(-)
lib/librte_vhost/fd_man.c +128 -98
index 2d3eeb7..d7c775d 100644
@@ -35,16 +35,38 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
 
 #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_mutex_unlock(&pfdset->fd_mutex);
 		return -2;
 	}
 
-	pfdset->num++;
+	fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
 
 	pthread_mutex_unlock(&pfdset->fd_mutex);
 
@@ -189,7 +171,6 @@
 			pfdset->fd[i].fd = -1;
 			pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
 			pfdset->fd[i].dat = NULL;
-			pfdset->num--;
 			i = -1;
 		}
 		pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -198,25 +179,26 @@
 	return dat;
 }
 
+
 /**
- *  Unregister the fd at the specified slot from the fdset.
+ *  Moves the fd from last slot to specified slot, including
+ *  corresponding pollfd
  */
 static void
-fdset_del_slot(struct fdset *pfdset, int index)
+fdset_move_last(struct fdset *pfdset, int idx)
 {
-	if (pfdset == NULL || index < 0 || index >= MAX_FDS)
-		return;
-
-	pthread_mutex_lock(&pfdset->fd_mutex);
+	int last_idx = pfdset->num - 1;
 
-	pfdset->fd[index].fd = -1;
-	pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
-	pfdset->fd[index].dat = NULL;
-	pfdset->num--;
+	if (idx < last_idx) {
+		pfdset->fd[idx] = pfdset->fd[last_idx];
+		pfdset->fd[last_idx].fd = -1;
 
-	pthread_mutex_unlock(&pfdset->fd_mutex);
+		pfdset->rwfds[idx] = pfdset->rwfds[last_idx];
+		pfdset->rwfds[last_idx].revents = 0;
+	}
 }
 
+
 /**
  * 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 +211,75 @@
 void
 fdset_event_dispatch(struct fdset *pfdset)
 {
-	fd_set rfds, wfds;
-	int i, maxfds;
+	int i;
+	struct pollfd *pfd;
 	struct fdentry *pfdentry;
-	int num = MAX_FDS;
 	fd_cb rcb, wcb;
 	void *dat;
-	int fd;
+	int fd, numfds;
 	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);
-
 		/*
-		 * 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;
+		pthread_mutex_lock(&pfdset->fd_mutex);
 
-		for (i = 0; i < num; i++) {
-			remove1 = remove2 = 0;
+		numfds = pfdset->num;
+
+		pthread_mutex_unlock(&pfdset->fd_mutex);
+
+		poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+
+		for (i = 0; i < numfds; ) {
 			pthread_mutex_lock(&pfdset->fd_mutex);
+
 			pfdentry = &pfdset->fd[i];
 			fd = pfdentry->fd;
+			pfd = &pfdset->rwfds[i];
+
+			if (fd < 0) {
+				/* Removed during poll */
+				/* shrink first, last migth be deleted*/
+
+				fdset_shrink(pfdset);
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				continue;
+			}
+
+			if (!pfd->revents) {
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				i++;
+				continue;
+			}
+
+			/* Valid fd, and at least one revent ... */
+
+			remove1 = remove2 = 0;
+
 			rcb = pfdentry->rcb;
 			wcb = pfdentry->wcb;
 			dat = pfdentry->dat;
 			pfdentry->busy = 1;
+
 			pthread_mutex_unlock(&pfdset->fd_mutex);
-			if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
+
+			if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
 				rcb(fd, dat, &remove1);
-			if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
+			if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
 				wcb(fd, dat, &remove2);
 			pfdentry->busy = 0;
 			/*
@@ -292,8 +294,36 @@
 			 * listen fd in another thread, we couldn't call
 			 * fd_set_del.
 			 */
-			if (remove1 || remove2)
-				fdset_del_slot(pfdset, i);
+			if (remove1 || remove2) {
+				pthread_mutex_lock(&pfdset->fd_mutex);
+
+				/* shrink first, last migth be deleted*/
+				fdset_shrink(pfdset);
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				continue;
+			}
+
+			i++;
 		}
+
+		/* fdset_del do not shrink, pack eventual remainings of array */
+		pthread_mutex_lock(&pfdset->fd_mutex);
+
+		fdset_shrink(pfdset);
+
+		for ( ; i < pfdset->num; i++) {
+			pfdentry = &pfdset->fd[i];
+
+			if (pfdentry->fd < 0) {
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+			}
+		}
+
+		pthread_mutex_unlock(&pfdset->fd_mutex);
 	}
 }
lib/librte_vhost/fd_man.h +3 -1
index bd66ed1..03e7881 100644
@@ -35,6 +35,7 @@
 #define _FD_MAN_H_
 #include <stdint.h>
 #include <pthread.h>
+#include <poll.h>
 
 #define MAX_FDS 1024
 
@@ -49,9 +50,10 @@ struct fdentry {
 };
 
 struct fdset {
+	struct pollfd rwfds[MAX_FDS];
 	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 */
 };
 
 
Signature · 1 line
1.9.1
#13 lore raw
Quoted 9 lines · Jan Wickbom
lib/librte_vhost/fd_man.c +6 -0
+
+		poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+
+		for (i = 0; i < numfds; ) {
 			pthread_mutex_lock(&pfdset->fd_mutex);
+
 			pfdentry = &pfdset->fd[i];
 			fd = pfdentry->fd;
+			pfd = &pfdset->rwfds[i];

lib/librte_vhost/fd_man.c line 256

Quoted 10 lines
lib/librte_vhost/fd_man.c +10 -0
+
+			if (fd < 0) {
+				/* Removed during poll */
+				/* shrink first, last migth be deleted*/
+
+				fdset_shrink(pfdset);
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);

This patch looks better, but as said, I will not do shrink in the event processing loop: I would simply set a flag, something like "need_shrink" here, and do the shrink outside this for loop.

lib/librte_vhost/fd_man.c line 307

Quoted 11 lines
lib/librte_vhost/fd_man.c +11 -0
+			if (remove1 || remove2) {
+				pthread_mutex_lock(&pfdset->fd_mutex);
+
+				/* shrink first, last migth be deleted*/
+				fdset_shrink(pfdset);
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				continue;

Same here: just sets a flag.

Quoted 8 lines
lib/librte_vhost/fd_man.c +7 -0
+			}
+
+			i++;
 		}
+
+		/* fdset_del do not shrink, pack eventual remainings of array */
+		pthread_mutex_lock(&pfdset->fd_mutex);
+

lib/librte_vhost/fd_man.c line 325

Quoted 10 lines
lib/librte_vhost/fd_man.c +10 -0
+		fdset_shrink(pfdset);
+
+		for ( ; i < pfdset->num; i++) {
+			pfdentry = &pfdset->fd[i];
+
+			if (pfdentry->fd < 0) {
+				fdset_move_last(pfdset, i);
+				fdset_shrink(pfdset);
+			}
+		}

And yes, do the shrink here (when the shrink flag is set). But I would simply call fdset_shrink() here (without the for loop), and let the fdset_shrink() to handle the details: it could either be a swap with last __valid__ entry, or simply a memmov.

That said, if you prefer to choose fdset_move_last(), fine, invoke it inside fdset_shrink() then. Let fdset_shrink be able to remove an fd in the middle.

Quoted 17 lines
lib/librte_vhost/fd_man.c +2 -0
+
+		pthread_mutex_unlock(&pfdset->fd_mutex);
 	}
 }
lib/librte_vhost/fd_man.h +1 -0
index bd66ed1..03e7881 100644
@@ -35,6 +35,7 @@
 #define _FD_MAN_H_
 #include <stdint.h>
 #include <pthread.h>
+#include <poll.h>
 
 #define MAX_FDS 1024
 

lib/librte_vhost/fd_man.h struct fdentry { · line 56

Quoted 8 lines
lib/librte_vhost/fd_man.h +2 -1
@@ -49,9 +50,10 @@ struct fdentry {
 };
 
 struct fdset {
+	struct pollfd rwfds[MAX_FDS];
 	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 */

I don't see the comment change makes it more readable.

	--yliu
#14 lore raw

PATCH v5 1/1 vhost: allow for many vhost user ports

In reply to 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

---

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(-)
lib/librte_vhost/fd_man.c +133 -101
index 2d3eeb7..c7c2b35 100644
@@ -35,93 +35,111 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
 
 #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 = pfdentry->fd < maxfds ?
-					maxfds : pfdentry->fd;
-		}
-	}
-	return maxfds;
+	return i == pfdset->num ? -1 : i;
+}
+
+static void
+fdset_add_fd(struct fdset *pfdset, int idx, int fd,
+	fd_cb rcb, fd_cb wcb, void *dat)
+{
+	struct fdentry *pfdentry = &pfdset->fd[idx];
+	struct pollfd *pfd = &pfdset->rwfds[idx];
+
+	pfdentry->fd = fd;
+	pfdentry->rcb = rcb;
+	pfdentry->wcb = wcb;
+	pfdentry->dat = dat;
+
+	pfd->fd = fd;
+	pfd->events = rcb ? POLLIN : 0;
+	pfd->events |= wcb ? POLLOUT : 0;
+	pfd->revents = 0;
 }
 
 void
@@ -132,11 +150,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 +174,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_mutex_unlock(&pfdset->fd_mutex);
 		return -2;
 	}
 
-	pfdset->num++;
+	fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
 
 	pthread_mutex_unlock(&pfdset->fd_mutex);
 
@@ -189,7 +211,6 @@
 			pfdset->fd[i].fd = -1;
 			pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
 			pfdset->fd[i].dat = NULL;
-			pfdset->num--;
 			i = -1;
 		}
 		pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -198,24 +219,6 @@
 	return dat;
 }
 
-/**
- *  Unregister the fd at the specified slot from the fdset.
- */
-static void
-fdset_del_slot(struct fdset *pfdset, int index)
-{
-	if (pfdset == NULL || index < 0 || index >= MAX_FDS)
-		return;
-
-	pthread_mutex_lock(&pfdset->fd_mutex);
-
-	pfdset->fd[index].fd = -1;
-	pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
-	pfdset->fd[index].dat = NULL;
-	pfdset->num--;
-
-	pthread_mutex_unlock(&pfdset->fd_mutex);
-}
 
 /**
  * This functions runs in infinite blocking loop until there is no fd in
@@ -229,55 +232,74 @@
 void
 fdset_event_dispatch(struct fdset *pfdset)
 {
-	fd_set rfds, wfds;
-	int i, maxfds;
+	int i;
+	struct pollfd *pfd;
 	struct fdentry *pfdentry;
-	int num = MAX_FDS;
 	fd_cb rcb, wcb;
 	void *dat;
-	int fd;
+	int fd, numfds;
 	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);
+		int has_holes;
 
 		/*
-		 * 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;
+		pthread_mutex_lock(&pfdset->fd_mutex);
 
-		for (i = 0; i < num; i++) {
-			remove1 = remove2 = 0;
+		numfds = pfdset->num;
+
+		pthread_mutex_unlock(&pfdset->fd_mutex);
+
+		poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+
+		has_holes = 0;
+		for (i = 0; i < numfds; i++) {
 			pthread_mutex_lock(&pfdset->fd_mutex);
+
 			pfdentry = &pfdset->fd[i];
 			fd = pfdentry->fd;
+			pfd = &pfdset->rwfds[i];
+
+			if (fd < 0) {
+				/* Removed during poll, care later */
+
+				has_holes = 1;
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				continue;
+			}
+
+			if (!pfd->revents) {
+
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+
+				continue;
+			}
+
+			/* Valid fd, and at least one revent ... */
+
+			remove1 = remove2 = 0;
+
 			rcb = pfdentry->rcb;
 			wcb = pfdentry->wcb;
 			dat = pfdentry->dat;
 			pfdentry->busy = 1;
+
 			pthread_mutex_unlock(&pfdset->fd_mutex);
-			if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
+
+			if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
 				rcb(fd, dat, &remove1);
-			if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
+			if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
 				wcb(fd, dat, &remove2);
 			pfdentry->busy = 0;
 			/*
@@ -292,8 +314,18 @@
 			 * listen fd in another thread, we couldn't call
 			 * fd_set_del.
 			 */
-			if (remove1 || remove2)
-				fdset_del_slot(pfdset, i);
+			if (remove1 || remove2) {
+				pfdentry->fd = -1;
+				has_holes = 1;
+			}
+		}
+
+		if (has_holes) {
+			pthread_mutex_lock(&pfdset->fd_mutex);
+
+			fdset_pack(pfdset);
+
+			pthread_mutex_unlock(&pfdset->fd_mutex);
 		}
 	}
 }
lib/librte_vhost/fd_man.h +2 -0
index bd66ed1..d319cac 100644
@@ -35,6 +35,7 @@
 #define _FD_MAN_H_
 #include <stdint.h>
 #include <pthread.h>
+#include <poll.h>
 
 #define MAX_FDS 1024
 
@@ -49,6 +50,7 @@ struct fdentry {
 };
 
 struct fdset {
+	struct pollfd rwfds[MAX_FDS];
 	struct fdentry fd[MAX_FDS];
 	pthread_mutex_t fd_mutex;
 	int num;	/* current fd number of this fdset */
Signature · 1 line
1.9.1
#15 lore raw

PATCH v6 1/1 vhost: allow for many vhost user ports

From: Jan Wickbom <[email protected]>

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

---

For some reason, Jan won't be able to make v6, so I made it for him.

v6:
* simplify fdset_shrink a bit
* minor cleanups
* don't touch fdset_init, which has nothing to do with this patchset, yet this function will be removed in coming patch.

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 | 200 ++++++++++++++++++++++------------------------
 lib/librte_vhost/fd_man.h |   2 +
 2 files changed, 97 insertions(+), 105 deletions(-)
lib/librte_vhost/fd_man.c +95 -105
index 2d3eeb7..8a075da 100644
@@ -35,93 +35,91 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
-#include <sys/select.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <rte_common.h>
 #include <rte_log.h>
 
 #include "fd_man.h"
 
-/**
- * Returns the index in the fdset for a given fd.
- * If fd is -1, it means to search for a free entry.
- * @return
- *   index for the fd, or -1 if fd isn't in the fdset.
- */
+#define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
+
 static int
-fdset_find_fd(struct fdset *pfdset, int fd)
+get_last_valid_idx(struct fdset *pfdset, int last_valid_idx)
 {
 	int i;
 
-	if (pfdset == NULL)
-		return -1;
-
-	for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
+	for (i = last_valid_idx; i >= 0 && pfdset->fd[i].fd == -1; i--)
 		;
 
-	return i ==  MAX_FDS ? -1 : i;
+	return i;
 }
 
-static int
-fdset_find_free_slot(struct fdset *pfdset)
+static void
+fdset_move(struct fdset *pfdset, int dst, int src)
 {
-	return fdset_find_fd(pfdset, -1);
+	pfdset->fd[dst]    = pfdset->fd[src];
+	pfdset->rwfds[dst] = pfdset->rwfds[src];
 }
 
-static int
-fdset_add_fd(struct fdset  *pfdset, int idx, int fd,
-	fd_cb rcb, fd_cb wcb, void *dat)
+/*
+ * Find deleted fd entries and remove them
+ */
+static void
+fdset_shrink(struct fdset *pfdset)
 {
-	struct fdentry *pfdentry;
+	int i;
+	int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
 
-	if (pfdset == NULL || idx >= MAX_FDS || fd >= FD_SETSIZE)
-		return -1;
+	pthread_mutex_lock(&pfdset->fd_mutex);
 
-	pfdentry = &pfdset->fd[idx];
-	pfdentry->fd = fd;
-	pfdentry->rcb = rcb;
-	pfdentry->wcb = wcb;
-	pfdentry->dat = dat;
+	for (i = 0; i < last_valid_idx; i++) {
+		if (pfdset->fd[i].fd != -1)
+			continue;
 
-	return 0;
+		fdset_move(pfdset, i, last_valid_idx);
+		last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
+	}
+	pfdset->num = last_valid_idx + 1;
+
+	pthread_mutex_unlock(&pfdset->fd_mutex);
 }
 
 /**
- * Fill the read/write fd_set with the fds in the fdset.
+ * Returns the index in the fdset for a given fd.
  * @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 = pfdentry->fd < maxfds ?
-					maxfds : pfdentry->fd;
-		}
-	}
-	return maxfds;
+	return i == pfdset->num ? -1 : i;
+}
+
+static void
+fdset_add_fd(struct fdset *pfdset, int idx, int fd,
+	fd_cb rcb, fd_cb wcb, void *dat)
+{
+	struct fdentry *pfdentry = &pfdset->fd[idx];
+	struct pollfd *pfd = &pfdset->rwfds[idx];
+
+	pfdentry->fd  = fd;
+	pfdentry->rcb = rcb;
+	pfdentry->wcb = wcb;
+	pfdentry->dat = dat;
+
+	pfd->fd = fd;
+	pfd->events  = rcb ? POLLIN : 0;
+	pfd->events |= wcb ? POLLOUT : 0;
+	pfd->revents = 0;
 }
 
 void
@@ -151,16 +149,13 @@
 		return -1;
 
 	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_mutex_unlock(&pfdset->fd_mutex);
 		return -2;
 	}
 
-	pfdset->num++;
-
+	fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
 	pthread_mutex_unlock(&pfdset->fd_mutex);
 
 	return 0;
@@ -189,7 +184,6 @@
 			pfdset->fd[i].fd = -1;
 			pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
 			pfdset->fd[i].dat = NULL;
-			pfdset->num--;
 			i = -1;
 		}
 		pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -198,24 +192,6 @@
 	return dat;
 }
 
-/**
- *  Unregister the fd at the specified slot from the fdset.
- */
-static void
-fdset_del_slot(struct fdset *pfdset, int index)
-{
-	if (pfdset == NULL || index < 0 || index >= MAX_FDS)
-		return;
-
-	pthread_mutex_lock(&pfdset->fd_mutex);
-
-	pfdset->fd[index].fd = -1;
-	pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
-	pfdset->fd[index].dat = NULL;
-	pfdset->num--;
-
-	pthread_mutex_unlock(&pfdset->fd_mutex);
-}
 
 /**
  * This functions runs in infinite blocking loop until there is no fd in
@@ -229,55 +205,64 @@
 void
 fdset_event_dispatch(struct fdset *pfdset)
 {
-	fd_set rfds, wfds;
-	int i, maxfds;
+	int i;
+	struct pollfd *pfd;
 	struct fdentry *pfdentry;
-	int num = MAX_FDS;
 	fd_cb rcb, wcb;
 	void *dat;
-	int fd;
+	int fd, numfds;
 	int remove1, remove2;
-	int ret;
+	int need_shrink;
 
 	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);
 
 		/*
-		 * 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;
+		pthread_mutex_lock(&pfdset->fd_mutex);
+		numfds = pfdset->num;
+		pthread_mutex_unlock(&pfdset->fd_mutex);
 
-		for (i = 0; i < num; i++) {
-			remove1 = remove2 = 0;
+		poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+
+		need_shrink = 0;
+		for (i = 0; i < numfds; i++) {
 			pthread_mutex_lock(&pfdset->fd_mutex);
+
 			pfdentry = &pfdset->fd[i];
 			fd = pfdentry->fd;
+			pfd = &pfdset->rwfds[i];
+
+			if (fd < 0) {
+				need_shrink = 1;
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+				continue;
+			}
+
+			if (!pfd->revents) {
+				pthread_mutex_unlock(&pfdset->fd_mutex);
+				continue;
+			}
+
+			remove1 = remove2 = 0;
+
 			rcb = pfdentry->rcb;
 			wcb = pfdentry->wcb;
 			dat = pfdentry->dat;
 			pfdentry->busy = 1;
+
 			pthread_mutex_unlock(&pfdset->fd_mutex);
-			if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
+
+			if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
 				rcb(fd, dat, &remove1);
-			if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
+			if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
 				wcb(fd, dat, &remove2);
 			pfdentry->busy = 0;
 			/*
@@ -292,8 +277,13 @@
 			 * listen fd in another thread, we couldn't call
 			 * fd_set_del.
 			 */
-			if (remove1 || remove2)
-				fdset_del_slot(pfdset, i);
+			if (remove1 || remove2) {
+				pfdentry->fd = -1;
+				need_shrink = 1;
+			}
 		}
+
+		if (need_shrink)
+			fdset_shrink(pfdset);
 	}
 }
lib/librte_vhost/fd_man.h +2 -0
index bd66ed1..d319cac 100644
@@ -35,6 +35,7 @@
 #define _FD_MAN_H_
 #include <stdint.h>
 #include <pthread.h>
+#include <poll.h>
 
 #define MAX_FDS 1024
 
@@ -49,6 +50,7 @@ struct fdentry {
 };
 
 struct fdset {
+	struct pollfd rwfds[MAX_FDS];
 	struct fdentry fd[MAX_FDS];
 	pthread_mutex_t fd_mutex;
 	int num;	/* current fd number of this fdset */
Signature · 1 line
1.9.0
#16 lore raw
Quoted 9 lines · On Wed, 21 Dec 2016 17:45:13 +0800 Yuanhan Liu <[email protected]>…

From: Jan Wickbom <[email protected]>

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.

Quoted 6 lines

Also see:
http://dpdk.org/ml/archives/dev/2016-April/037024.html

Why not epoll()? It scales much better. The old System V poll is just as bad with 1023 fd's.

#17 lore raw
Quoted 15 lines · Stephen Hemminger
Quoted 12 lines · On Wed, 21 Dec 2016 17:45:13 +0800 Yuanhan Liu <[email protected]>…

From: Jan Wickbom <[email protected]>

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

Quoted 6 lines
Quoted 3 lines

Why not epoll()? It scales much better. The old System V poll is just as bad with 1023 fd's.

Indeed, there was a plan to use epoll() to fix this issue. It's been delayed long enough that Jan came up with a fix with poll recently.

I don't have strong preference to epoll() over poll, for following reasons:

- epoll() is Linux only, though I don't know other platforms (mainly *BSD) actually care vhost-user or not.

- epoll indeed scales much better, but I don't think the scale actually matters a lot here: vhost-user negotiation normally happens once on QEMU startup, and it's a short lived process. Both facts make it be non-performance critical.

- 1023 is also not a big problem so far: vhost lib hardcodes the max vhost devices we could support to be 1024 after all.

For above reasons and Jan already came up with a solution (that would work for most platforms), I think I'm fine with this poll() so far.

	--yliu
Applied
#18 lore raw
In reply to Yuanhan Liu
Quoted 9 lines · Yuanhan Liu

From: Jan Wickbom <[email protected]>

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.

Quoted 6 lines

Also see:
http://dpdk.org/ml/archives/dev/2016-April/037024.html

Applied to dpdk-next-virtio.

	--yliu