Skip to contentSkip to conversation

tools/xenstore: simplify xenstored main loop

PATCH Superseded Juergen Gross 2 participants 5 messages May 14, 2021 – May 17, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenstore: simplify xenstored main loop

The main loop of xenstored is rather complicated due to different handling of socket and ring-page interfaces. Unify that handling by introducing interface type specific functions can_read() and can_write().

Put the interface type specific functions in an own structure and let struct connection contain only a pointer to that new function vector.

---
 tools/xenstore/xenstored_core.c   | 117 ++++++++++++++----------------
 tools/xenstore/xenstored_core.h   |  19 ++---
 tools/xenstore/xenstored_domain.c |  11 ++-
 3 files changed, 73 insertions(+), 74 deletions(-)
tools/xenstore/xenstored_core.c +54 -63
index 4b7b71cfb3..b66d119a98 100644
@@ -226,8 +226,8 @@ static bool write_messages(struct connection *conn)
 				sockmsg_string(out->hdr.msg.type),
 				out->hdr.msg.len,
 				out->buffer, conn);
-		ret = conn->write(conn, out->hdr.raw + out->used,
-				  sizeof(out->hdr) - out->used);
+		ret = conn->funcs->write(conn, out->hdr.raw + out->used,
+					 sizeof(out->hdr) - out->used);
 		if (ret < 0)
 			return false;
 
@@ -243,8 +243,8 @@ static bool write_messages(struct connection *conn)
 			return true;
 	}
 
-	ret = conn->write(conn, out->buffer + out->used,
-			  out->hdr.msg.len - out->used);
+	ret = conn->funcs->write(conn, out->buffer + out->used,
+				 out->hdr.msg.len - out->used);
 	if (ret < 0)
 		return false;
 
@@ -1531,8 +1531,8 @@ static void handle_input(struct connection *conn)
 	/* Not finished header yet? */
 	if (in->inhdr) {
 		if (in->used != sizeof(in->hdr)) {
-			bytes = conn->read(conn, in->hdr.raw + in->used,
-					   sizeof(in->hdr) - in->used);
+			bytes = conn->funcs->read(conn, in->hdr.raw + in->used,
+						  sizeof(in->hdr) - in->used);
 			if (bytes < 0)
 				goto bad_client;
 			in->used += bytes;
@@ -1557,8 +1557,8 @@ static void handle_input(struct connection *conn)
 		in->inhdr = false;
 	}
 
-	bytes = conn->read(conn, in->buffer + in->used,
-			   in->hdr.msg.len - in->used);
+	bytes = conn->funcs->read(conn, in->buffer + in->used,
+				  in->hdr.msg.len - in->used);
 	if (bytes < 0)
 		goto bad_client;
 
@@ -1581,7 +1581,7 @@ static void handle_output(struct connection *conn)
 		ignore_connection(conn);
 }
 
-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
+struct connection *new_connection(struct interface_funcs *funcs)
 {
 	struct connection *new;
 
@@ -1591,8 +1591,7 @@ struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
 
 	new->fd = -1;
 	new->pollfd_idx = -1;
-	new->write = write;
-	new->read = read;
+	new->funcs = funcs;
 	new->is_ignored = false;
 	new->transaction_started = 0;
 	INIT_LIST_HEAD(&new->out_list);
@@ -1622,17 +1621,7 @@ static void accept_connection(int sock)
 {
 }
 
-int writefd(struct connection *conn, const void *data, unsigned int len)
-{
-	errno = EBADF;
-	return -1;
-}
-
-int readfd(struct connection *conn, void *data, unsigned int len)
-{
-	errno = EBADF;
-	return -1;
-}
+struct interface_funcs socket_funcs;
 #else
 int writefd(struct connection *conn, const void *data, unsigned int len)
 {
@@ -1672,6 +1661,29 @@ int readfd(struct connection *conn, void *data, unsigned int len)
 	return rc;
 }
 
+static bool socket_can_process(struct connection *conn, int mask)
+{
+	if (conn->pollfd_idx == -1)
+		return false;
+
+	if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
+		talloc_free(conn);
+		return false;
+	}
+
+	return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
+}
+
+static bool socket_can_write(struct connection *conn)
+{
+	return socket_can_process(conn, POLLOUT);
+}
+
+static bool socket_can_read(struct connection *conn)
+{
+	return socket_can_process(conn, POLLIN);
+}
+
 static void accept_connection(int sock)
 {
 	int fd;
@@ -1681,12 +1693,19 @@ static void accept_connection(int sock)
 	if (fd < 0)
 		return;
 
-	conn = new_connection(writefd, readfd);
+	conn = new_connection(&socket_funcs);
 	if (conn)
 		conn->fd = fd;
 	else
 		close(fd);
 }
+
+struct interface_funcs socket_funcs = {
+	.write = writefd,
+	.read = readfd,
+	.can_write = socket_can_write,
+	.can_read = socket_can_read,
+};
 #endif
 
 static int tdb_flags;
@@ -2304,47 +2323,19 @@ int main(int argc, char *argv[])
 			if (&next->list != &connections)
 				talloc_increase_ref_count(next);
 
-			if (conn->domain) {
-				if (domain_can_read(conn))
-					handle_input(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-				if (domain_can_write(conn) &&
-				    !list_empty(&conn->out_list))
-					handle_output(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-			} else {
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLIN) &&
-						 !conn->is_ignored)
-						handle_input(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLOUT) &&
-						 !conn->is_ignored)
-						handle_output(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
+			if (conn->funcs->can_read(conn))
+				handle_input(conn);
+			if (talloc_free(conn) == 0)
+				continue;
 
-				conn->pollfd_idx = -1;
-			}
+			talloc_increase_ref_count(conn);
+
+			if (conn->funcs->can_write(conn))
+				handle_output(conn);
+			if (talloc_free(conn) == 0)
+				continue;
+
+			conn->pollfd_idx = -1;
 		}
 
 		if (delayed_requests) {
tools/xenstore/xenstored_core.h +10 -9
index 6a6d0448e8..1467270476 100644
@@ -86,8 +86,13 @@ struct delayed_request {
 };
 
 struct connection;
-typedef int connwritefn_t(struct connection *, const void *, unsigned int);
-typedef int connreadfn_t(struct connection *, void *, unsigned int);
+
+struct interface_funcs {
+	int (*write)(struct connection *, const void *, unsigned int);
+	int (*read)(struct connection *, void *, unsigned int);
+	bool (*can_write)(struct connection *);
+	bool (*can_read)(struct connection *);
+};
 
 struct connection
 {
@@ -131,9 +136,8 @@ struct connection
 	/* My watches. */
 	struct list_head watches;
 
-	/* Methods for communicating over this connection: write can be NULL */
-	connwritefn_t *write;
-	connreadfn_t *read;
+	/* Methods for communicating over this connection. */
+	struct interface_funcs *funcs;
 
 	/* Support for live update: connection id. */
 	unsigned int conn_id;
@@ -196,7 +200,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
 struct node *read_node(struct connection *conn, const void *ctx,
 		       const char *name);
 
-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
+struct connection *new_connection(struct interface_funcs *funcs);
 struct connection *get_connection_by_id(unsigned int conn_id);
 void check_store(void);
 void corrupt(struct connection *conn, const char *fmt, ...);
@@ -254,9 +258,6 @@ void finish_daemonize(void);
 /* Open a pipe for signal handling */
 void init_pipe(int reopen_log_pipe[2]);
 
-int writefd(struct connection *conn, const void *data, unsigned int len);
-int readfd(struct connection *conn, void *data, unsigned int len);
-
 extern struct interface_funcs socket_funcs;
 extern xengnttab_handle **xgt_handle;
 
tools/xenstore/xenstored_domain.c +9 -2
index 0c17937c0f..6e0fa6e861 100644
@@ -172,6 +172,13 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
 	return len;
 }
 
+static struct interface_funcs domain_funcs = {
+	.write = writechn,
+	.read = readchn,
+	.can_write = domain_can_write,
+	.can_read = domain_can_read,
+};
+
 static void *map_interface(domid_t domid)
 {
 	return xengnttab_map_grant_ref(*xgt_handle, domid,
@@ -389,7 +396,7 @@ static int new_domain(struct domain *domain, int port, bool restore)
 
 	domain->introduced = true;
 
-	domain->conn = new_connection(writechn, readchn);
+	domain->conn = new_connection(&domain_funcs);
 	if (!domain->conn)  {
 		errno = ENOMEM;
 		return errno;
@@ -1288,7 +1295,7 @@ void read_state_connection(const void *ctx, const void *state)
 	struct domain *domain, *tdomain;
 
 	if (sc->conn_type == XS_STATE_CONN_TYPE_SOCKET) {
-		conn = new_connection(writefd, readfd);
+		conn = new_connection(&socket_funcs);
 		if (!conn)
 			barf("error restoring connection");
 		conn->fd = sc->spec.socket_fd;
Signature · 1 line
2.26.2
#1 lore raw

Hi,

Quoted 7 lines · Juergen Gross

The main loop of xenstored is rather complicated due to different handling of socket and ring-page interfaces. Unify that handling by introducing interface type specific functions can_read() and can_write().

Put the interface type specific functions in an own structure and let struct connection contain only a pointer to that new function vector.

I would split the renaming in a separate patch. This would be easier to review and remove some churn from this patch.

Quoted 57 lines
---
  tools/xenstore/xenstored_core.c   | 117 ++++++++++++++----------------
  tools/xenstore/xenstored_core.h   |  19 ++---
  tools/xenstore/xenstored_domain.c |  11 ++-
  3 files changed, 73 insertions(+), 74 deletions(-)
tools/xenstore/xenstored_core.c +8 -8
index 4b7b71cfb3..b66d119a98 100644
@@ -226,8 +226,8 @@ static bool write_messages(struct connection *conn)
  				sockmsg_string(out->hdr.msg.type),
  				out->hdr.msg.len,
  				out->buffer, conn);
-		ret = conn->write(conn, out->hdr.raw + out->used,
-				  sizeof(out->hdr) - out->used);
+		ret = conn->funcs->write(conn, out->hdr.raw + out->used,
+					 sizeof(out->hdr) - out->used);
  		if (ret < 0)
  			return false;
 
@@ -243,8 +243,8 @@ static bool write_messages(struct connection *conn)
  			return true;
  	}
 
-	ret = conn->write(conn, out->buffer + out->used,
-			  out->hdr.msg.len - out->used);
+	ret = conn->funcs->write(conn, out->buffer + out->used,
+				 out->hdr.msg.len - out->used);
  	if (ret < 0)
  		return false;
 
@@ -1531,8 +1531,8 @@ static void handle_input(struct connection *conn)
  	/* Not finished header yet? */
  	if (in->inhdr) {
  		if (in->used != sizeof(in->hdr)) {
-			bytes = conn->read(conn, in->hdr.raw + in->used,
-					   sizeof(in->hdr) - in->used);
+			bytes = conn->funcs->read(conn, in->hdr.raw + in->used,
+						  sizeof(in->hdr) - in->used);
  			if (bytes < 0)
  				goto bad_client;
  			in->used += bytes;
@@ -1557,8 +1557,8 @@ static void handle_input(struct connection *conn)
  		in->inhdr = false;
  	}
 
-	bytes = conn->read(conn, in->buffer + in->used,
-			   in->hdr.msg.len - in->used);
+	bytes = conn->funcs->read(conn, in->buffer + in->used,
+				  in->hdr.msg.len - in->used);
  	if (bytes < 0)
  		goto bad_client;
 

tools/xenstore/xenstored_core.c static void handle_output(struct connection *conn) · line 1584

Quoted 5 lines
tools/xenstore/xenstored_core.c +1 -1
@@ -1581,7 +1581,7 @@ static void handle_output(struct connection *conn)
  		ignore_connection(conn);
  }
 
-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
+struct connection *new_connection(struct interface_funcs *funcs)

I don't think the interface is meant to be changed after the connection is created. So this should be const.

Quoted 19 lines
tools/xenstore/xenstored_core.c +1 -4
  {
  	struct connection *new;
 
@@ -1591,8 +1591,7 @@ struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
 
  	new->fd = -1;
  	new->pollfd_idx = -1;
-	new->write = write;
-	new->read = read;
+	new->funcs = funcs;
  	new->is_ignored = false;
  	new->transaction_started = 0;
  	INIT_LIST_HEAD(&new->out_list);
@@ -1622,17 +1621,7 @@ static void accept_connection(int sock)
  {
  }
 
-int writefd(struct connection *conn, const void *data, unsigned int len)
-{

tools/xenstore/xenstored_core.c static void accept_connection(int sock) · line 1624

Quoted 10 lines
tools/xenstore/xenstored_core.c +1 -9
-	errno = EBADF;
-	return -1;
-}
-
-int readfd(struct connection *conn, void *data, unsigned int len)
-{
-	errno = EBADF;
-	return -1;
-}
+struct interface_funcs socket_funcs;

AFAICT, this is defined for mini-os because read_state_connection() may use it. The assumption here is XS_STATE_CONN_TYPE_SOCKET will never show up in the stream.

If there is any mistake in the stream, this could lead to dereference NULL and crash after. AFAICT, before, we would just ignore the connection.

I think it would be best if sockets_funcs() is not defined at all or we continue to ignore the connection. This can be probably done by implementing dummy callback for can_write/can_read.

Quoted 36 lines
tools/xenstore/xenstored_core.c +23 -0
  #else
  int writefd(struct connection *conn, const void *data, unsigned int len)
  {
@@ -1672,6 +1661,29 @@ int readfd(struct connection *conn, void *data, unsigned int len)
  	return rc;
  }
 
+static bool socket_can_process(struct connection *conn, int mask)
+{
+	if (conn->pollfd_idx == -1)
+		return false;
+
+	if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
+		talloc_free(conn);
+		return false;
+	}
+
+	return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
+}
+
+static bool socket_can_write(struct connection *conn)
+{
+	return socket_can_process(conn, POLLOUT);
+}
+
+static bool socket_can_read(struct connection *conn)
+{
+	return socket_can_process(conn, POLLIN);
+}
+
  static void accept_connection(int sock)
  {
  	int fd;
@@ -1681,12 +1693,19 @@ static void accept_connection(int sock)
  	if (fd < 0)
  		return;

tools/xenstore/xenstored_core.c static void accept_connection(int sock) · line 1703

Quoted 10 lines
tools/xenstore/xenstored_core.c +3 -1
 
-	conn = new_connection(writefd, readfd);
+	conn = new_connection(&socket_funcs);
  	if (conn)
  		conn->fd = fd;
  	else
  		close(fd);
  }
+
+struct interface_funcs socket_funcs = {

This should be const.

Quoted 110 lines
tools/xenstore/xenstored_core.c +17 -40
+	.write = writefd,
+	.read = readfd,
+	.can_write = socket_can_write,
+	.can_read = socket_can_read,
+};
  #endif
 
  static int tdb_flags;
@@ -2304,47 +2323,19 @@ int main(int argc, char *argv[])
  			if (&next->list != &connections)
  				talloc_increase_ref_count(next);
 
-			if (conn->domain) {
-				if (domain_can_read(conn))
-					handle_input(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-				if (domain_can_write(conn) &&
-				    !list_empty(&conn->out_list))
-					handle_output(conn);
-				if (talloc_free(conn) == 0)
-					continue;
-			} else {
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLIN) &&
-						 !conn->is_ignored)
-						handle_input(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
-
-				talloc_increase_ref_count(conn);
-
-				if (conn->pollfd_idx != -1) {
-					if (fds[conn->pollfd_idx].revents
-					    & ~(POLLIN|POLLOUT))
-						talloc_free(conn);
-					else if ((fds[conn->pollfd_idx].revents
-						  & POLLOUT) &&
-						 !conn->is_ignored)
-						handle_output(conn);
-				}
-				if (talloc_free(conn) == 0)
-					continue;
+			if (conn->funcs->can_read(conn))
+				handle_input(conn);
+			if (talloc_free(conn) == 0)
+				continue;
 
-				conn->pollfd_idx = -1;
-			}
+			talloc_increase_ref_count(conn);
+
+			if (conn->funcs->can_write(conn))
+				handle_output(conn);
+			if (talloc_free(conn) == 0)
+				continue;
+
+			conn->pollfd_idx = -1;
  		}
 
  		if (delayed_requests) {
tools/xenstore/xenstored_core.h +10 -6
index 6a6d0448e8..1467270476 100644
@@ -86,8 +86,13 @@ struct delayed_request {
  };
 
  struct connection;
-typedef int connwritefn_t(struct connection *, const void *, unsigned int);
-typedef int connreadfn_t(struct connection *, void *, unsigned int);
+
+struct interface_funcs {
+	int (*write)(struct connection *, const void *, unsigned int);
+	int (*read)(struct connection *, void *, unsigned int);
+	bool (*can_write)(struct connection *);
+	bool (*can_read)(struct connection *);
+};
 
  struct connection
  {
@@ -131,9 +136,8 @@ struct connection
  	/* My watches. */
  	struct list_head watches;
 
-	/* Methods for communicating over this connection: write can be NULL */
-	connwritefn_t *write;
-	connreadfn_t *read;
+	/* Methods for communicating over this connection. */
+	struct interface_funcs *funcs;
 
  	/* Support for live update: connection id. */
  	unsigned int conn_id;
@@ -196,7 +200,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
  struct node *read_node(struct connection *conn, const void *ctx,
  		       const char *name);
 
-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
+struct connection *new_connection(struct interface_funcs *funcs);
  struct connection *get_connection_by_id(unsigned int conn_id);
  void check_store(void);
  void corrupt(struct connection *conn, const char *fmt, ...);

tools/xenstore/xenstored_core.h void finish_daemonize(void); · line 261

Quoted 7 lines
tools/xenstore/xenstored_core.h +0 -3
@@ -254,9 +258,6 @@ void finish_daemonize(void);
  /* Open a pipe for signal handling */
  void init_pipe(int reopen_log_pipe[2]);
 
-int writefd(struct connection *conn, const void *data, unsigned int len);
-int readfd(struct connection *conn, void *data, unsigned int len);
-
  extern struct interface_funcs socket_funcs;

Hmmm... I guess this change splipped in the staging before hand?

Quoted 31 lines
tools/xenstore/xenstored_core.h +0 -0
  extern xengnttab_handle **xgt_handle;
 
tools/xenstore/xenstored_domain.c +8 -1
index 0c17937c0f..6e0fa6e861 100644
@@ -172,6 +172,13 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
  	return len;
  }
 
+static struct interface_funcs domain_funcs = {
+	.write = writechn,
+	.read = readchn,
+	.can_write = domain_can_write,
+	.can_read = domain_can_read,
+};
+
  static void *map_interface(domid_t domid)
  {
  	return xengnttab_map_grant_ref(*xgt_handle, domid,
@@ -389,7 +396,7 @@ static int new_domain(struct domain *domain, int port, bool restore)
 
  	domain->introduced = true;
 
-	domain->conn = new_connection(writechn, readchn);
+	domain->conn = new_connection(&domain_funcs);
  	if (!domain->conn)  {
  		errno = ENOMEM;
  		return errno;

tools/xenstore/xenstored_domain.c void read_state_connection(const void *ctx, const void *state) · line 1301

Quoted 8 lines
tools/xenstore/xenstored_domain.c +1 -1
@@ -1288,7 +1295,7 @@ void read_state_connection(const void *ctx, const void *state)
  	struct domain *domain, *tdomain;
 
  	if (sc->conn_type == XS_STATE_CONN_TYPE_SOCKET) {
-		conn = new_connection(writefd, readfd);
+		conn = new_connection(&socket_funcs);
  		if (!conn)
  			barf("error restoring connection");
  		conn->fd = sc->spec.socket_fd;

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 11 lines · Julien Grall

Hi,

Quoted 7 lines · Juergen Gross

The main loop of xenstored is rather complicated due to different handling of socket and ring-page interfaces. Unify that handling by introducing interface type specific functions can_read() and can_write().

Put the interface type specific functions in an own structure and let struct connection contain only a pointer to that new function vector.

I would split the renaming in a separate patch. This would be easier to review and remove some churn from this patch.

Okay, I'll split the patch.

Quoted 52 lines
Quoted 52 lines

---
  tools/xenstore/xenstored_core.c   | 117 ++++++++++++++---------------- tools/xenstore/xenstored_core.h   |  19 ++--- tools/xenstore/xenstored_domain.c |  11 ++-

  3 files changed, 73 insertions(+), 74 deletions(-)
tools/xenstore/xenstored_core.c +8 -8
index 4b7b71cfb3..b66d119a98 100644
@@ -226,8 +226,8 @@ static bool write_messages(struct connection *conn)
                   sockmsg_string(out->hdr.msg.type),
                   out->hdr.msg.len,
                   out->buffer, conn);
-        ret = conn->write(conn, out->hdr.raw + out->used,
-                  sizeof(out->hdr) - out->used);
+        ret = conn->funcs->write(conn, out->hdr.raw + out->used,
+                     sizeof(out->hdr) - out->used);
           if (ret < 0)
               return false;
@@ -243,8 +243,8 @@ static bool write_messages(struct connection *conn)
               return true;
       }
-    ret = conn->write(conn, out->buffer + out->used,
-              out->hdr.msg.len - out->used);
+    ret = conn->funcs->write(conn, out->buffer + out->used,
+                 out->hdr.msg.len - out->used);
       if (ret < 0)
           return false;
@@ -1531,8 +1531,8 @@ static void handle_input(struct connection *conn)
       /* Not finished header yet? */
       if (in->inhdr) {
           if (in->used != sizeof(in->hdr)) {
-            bytes = conn->read(conn, in->hdr.raw + in->used,
-                       sizeof(in->hdr) - in->used);
+            bytes = conn->funcs->read(conn, in->hdr.raw + in->used,
+                          sizeof(in->hdr) - in->used);
               if (bytes < 0)
                   goto bad_client;
               in->used += bytes;
@@ -1557,8 +1557,8 @@ static void handle_input(struct connection *conn)
           in->inhdr = false;
       }
-    bytes = conn->read(conn, in->buffer + in->used,
-               in->hdr.msg.len - in->used);
+    bytes = conn->funcs->read(conn, in->buffer + in->used,
+                  in->hdr.msg.len - in->used);
       if (bytes < 0)
           goto bad_client;
Quoted 8 lines

tools/xenstore/xenstored_core.c static void handle_output(struct connection *conn) · line 1584

Quoted 6 lines
tools/xenstore/xenstored_core.c +1 -1
@@ -1581,7 +1581,7 @@ static void handle_output(struct connection *conn)
           ignore_connection(conn);
   }
-struct connection *new_connection(connwritefn_t *write, connreadfn_t 
 *read)
+struct connection *new_connection(struct interface_funcs *funcs)

I don't think the interface is meant to be changed after the connection is created. So this should be const.

Yes.

Quoted 31 lines
Quoted 18 lines
tools/xenstore/xenstored_core.c +1 -4
   {
       struct connection *new;
@@ -1591,8 +1591,7 @@ struct connection *new_connection(connwritefn_t 
 *write, connreadfn_t *read)
       new->fd = -1;
       new->pollfd_idx = -1;
-    new->write = write;
-    new->read = read;
+    new->funcs = funcs;
       new->is_ignored = false;
       new->transaction_started = 0;
       INIT_LIST_HEAD(&new->out_list);
@@ -1622,17 +1621,7 @@ static void accept_connection(int sock)
   {
   }
-int writefd(struct connection *conn, const void *data, unsigned int len)
-{

tools/xenstore/xenstored_core.c static void accept_connection(int sock) · line 1623

Quoted 10 lines
tools/xenstore/xenstored_core.c +1 -9
-    errno = EBADF;
-    return -1;
-}
-
-int readfd(struct connection *conn, void *data, unsigned int len)
-{
-    errno = EBADF;
-    return -1;
-}
+struct interface_funcs socket_funcs;

AFAICT, this is defined for mini-os because read_state_connection() may use it. The assumption here is XS_STATE_CONN_TYPE_SOCKET will never show up in the stream.

Quoted 3 lines

If there is any mistake in the stream, this could lead to dereference NULL and crash after. AFAICT, before, we would just ignore the connection.

I think it would be best if sockets_funcs() is not defined at all or we continue to ignore the connection. This can be probably done by implementing dummy callback for can_write/can_read.

Hmm, yes. I can put the referencing part inside #ifdef NO_SOCKETS.

Quoted 40 lines
Quoted 40 lines
tools/xenstore/xenstored_core.c +24 -1
   #else
   int writefd(struct connection *conn, const void *data, unsigned int len)
   {
@@ -1672,6 +1661,29 @@ int readfd(struct connection *conn, void *data, 
 unsigned int len)
       return rc;
   }
+static bool socket_can_process(struct connection *conn, int mask)
+{
+    if (conn->pollfd_idx == -1)
+        return false;
+
+    if (fds[conn->pollfd_idx].revents & ~(POLLIN | POLLOUT)) {
+        talloc_free(conn);
+        return false;
+    }
+
+    return (fds[conn->pollfd_idx].revents & mask) && !conn->is_ignored;
+}
+
+static bool socket_can_write(struct connection *conn)
+{
+    return socket_can_process(conn, POLLOUT);
+}
+
+static bool socket_can_read(struct connection *conn)
+{
+    return socket_can_process(conn, POLLIN);
+}
+
   static void accept_connection(int sock)
   {
       int fd;
@@ -1681,12 +1693,19 @@ static void accept_connection(int sock)
       if (fd < 0)
           return;
-    conn = new_connection(writefd, readfd);
+    conn = new_connection(&socket_funcs);
       if (conn)
Quoted 8 lines

tools/xenstore/xenstored_core.c static void accept_connection(int sock) · line 1703

Quoted 6 lines
tools/xenstore/xenstored_core.c +0 -0
           conn->fd = fd;

      else
          close(fd);
  }

tools/xenstore/xenstored_core.c +2 -0
+
+struct interface_funcs socket_funcs = {

This should be const.

Yes.

Quoted 107 lines
Quoted 107 lines

+    .write = writefd,
+    .read = readfd,
+    .can_write = socket_can_write,
+    .can_read = socket_can_read,
+};
  #endif
  static int tdb_flags;

tools/xenstore/xenstored_core.c +12 -40
@@ -2304,47 +2323,19 @@ int main(int argc, char *argv[])
               if (&next->list != &connections)
                   talloc_increase_ref_count(next);
-            if (conn->domain) {
-                if (domain_can_read(conn))
-                    handle_input(conn);
-                if (talloc_free(conn) == 0)
-                    continue;
-
-                talloc_increase_ref_count(conn);
-                if (domain_can_write(conn) &&
-                    !list_empty(&conn->out_list))
-                    handle_output(conn);
-                if (talloc_free(conn) == 0)
-                    continue;
-            } else {
-                if (conn->pollfd_idx != -1) {
-                    if (fds[conn->pollfd_idx].revents
-                        & ~(POLLIN|POLLOUT))
-                        talloc_free(conn);
-                    else if ((fds[conn->pollfd_idx].revents
-                          & POLLIN) &&
-                         !conn->is_ignored)
-                        handle_input(conn);
-                }
-                if (talloc_free(conn) == 0)
-                    continue;
-
-                talloc_increase_ref_count(conn);
-
-                if (conn->pollfd_idx != -1) {
-                    if (fds[conn->pollfd_idx].revents
-                        & ~(POLLIN|POLLOUT))
-                        talloc_free(conn);
-                    else if ((fds[conn->pollfd_idx].revents
-                          & POLLOUT) &&
-                         !conn->is_ignored)
-                        handle_output(conn);
-                }
-                if (talloc_free(conn) == 0)
-                    continue;
+            if (conn->funcs->can_read(conn))
+                handle_input(conn);
+            if (talloc_free(conn) == 0)
+                continue;
-                conn->pollfd_idx = -1;
-            }
+            talloc_increase_ref_count(conn);
+
+            if (conn->funcs->can_write(conn))
+                handle_output(conn);
+            if (talloc_free(conn) == 0)
+                continue;
+
+            conn->pollfd_idx = -1;
           }
           if (delayed_requests) {
tools/xenstore/xenstored_core.h +7 -2
index 6a6d0448e8..1467270476 100644
@@ -86,8 +86,13 @@ struct delayed_request {
   };
   struct connection;
-typedef int connwritefn_t(struct connection *, const void *, unsigned 
 int);
-typedef int connreadfn_t(struct connection *, void *, unsigned int);
+
+struct interface_funcs {
+    int (*write)(struct connection *, const void *, unsigned int);
+    int (*read)(struct connection *, void *, unsigned int);
+    bool (*can_write)(struct connection *);
+    bool (*can_read)(struct connection *);
+};

  struct connection
  {

tools/xenstore/xenstored_core.h +0 -1
@@ -131,9 +136,8 @@ struct connection
       /* My watches. */
       struct list_head watches;
-    /* Methods for communicating over this connection: write can be 

NULL */
-    connwritefn_t *write;
-    connreadfn_t *read;
+    /* Methods for communicating over this connection. */ +    struct interface_funcs *funcs;
      /* Support for live update: connection id. */ unsigned int conn_id;

tools/xenstore/xenstored_core.h +1 -1
@@ -196,7 +200,7 @@ int write_node_raw(struct connection *conn, 
 TDB_DATA *key, struct node *node,
   struct node *read_node(struct connection *conn, const void *ctx,
                  const char *name);
-struct connection *new_connection(connwritefn_t *write, connreadfn_t 
 *read);
+struct connection *new_connection(struct interface_funcs *funcs);
   struct connection *get_connection_by_id(unsigned int conn_id);
   void check_store(void);

  void corrupt(struct connection *conn, const char *fmt, ...);

tools/xenstore/xenstored_core.h +0 -0
@@ -254,9 +258,6 @@ void finish_daemonize(void);
   /* Open a pipe for signal handling */
Quoted 8 lines

tools/xenstore/xenstored_core.h void finish_daemonize(void); · line 261

Quoted 6 lines
tools/xenstore/xenstored_core.h +0 -3
   void init_pipe(int reopen_log_pipe[2]);
-int writefd(struct connection *conn, const void *data, unsigned int 
 len);
-int readfd(struct connection *conn, void *data, unsigned int len);
-
   extern struct interface_funcs socket_funcs;

Hmmm... I guess this change splipped in the staging before hand?

No, I just forgot to make the functions static.

Juergen

Reviewed-by Acked-by Wei Liu
#3 lore raw

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 8 lines · Julien Grall
Quoted 8 lines

-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
+struct connection *new_connection(struct interface_funcs *funcs); struct connection *get_connection_by_id(unsigned int conn_id); void check_store(void);
  void corrupt(struct connection *conn, const char *fmt, ...);

tools/xenstore/xenstored_core.h +0 -0
@@ -254,9 +258,6 @@ void finish_daemonize(void);
   /* Open a pipe for signal handling */
Quoted 8 lines

tools/xenstore/xenstored_core.h void finish_daemonize(void); · line 261

Quoted 6 lines
tools/xenstore/xenstored_core.h +0 -3
   void init_pipe(int reopen_log_pipe[2]);
-int writefd(struct connection *conn, const void *data, unsigned int 
 len);
-int readfd(struct connection *conn, void *data, unsigned int len);
-
   extern struct interface_funcs socket_funcs;

Hmmm... I guess this change splipped in the staging before hand?

Quoted 1 line

No, I just forgot to make the functions static.

Hmmm... I am not sure how this is related to my question. What I meant it the line "extern struct interface_funcs ..." doesn't have a '+' in front.

If you look at the history, this was added by mistake in:

commit 2ea411bc2c0a5a4c7ab145270f1949630460e72b

Author: Juergen Gross <[email protected]>
Date:   Wed Jan 13 14:00:20 2021 +0100

tools/xenstore: add read connection state for live update

Add the needed functions for reading connection state for live update.

As the connection is identified by a unique connection id in the state records we need to add this to struct connection. Add a new function to return the connection based on a connection id.

Quoted 3 lines

Juergen

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 21 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 16 lines · Julien Grall
Quoted 7 lines

-struct connection *new_connection(connwritefn_t *write, connreadfn_t *read);
+struct connection *new_connection(struct interface_funcs *funcs); struct connection *get_connection_by_id(unsigned int conn_id); void check_store(void);
  void corrupt(struct connection *conn, const char *fmt, ...);

tools/xenstore/xenstored_core.h void finish_daemonize(void); · line 261

Quoted 7 lines
tools/xenstore/xenstored_core.h +0 -3
@@ -254,9 +258,6 @@ void finish_daemonize(void);
   /* Open a pipe for signal handling */
   void init_pipe(int reopen_log_pipe[2]);
-int writefd(struct connection *conn, const void *data, unsigned int 
 len);
-int readfd(struct connection *conn, void *data, unsigned int len);
-
   extern struct interface_funcs socket_funcs;

Hmmm... I guess this change splipped in the staging before hand?

Quoted 3 lines
Quoted 1 line

No, I just forgot to make the functions static.

Hmmm... I am not sure how this is related to my question. What I meant it the line "extern struct interface_funcs ..." doesn't have a '+' in front.

Oh, I misunderstood you.

Quoted 6 lines

If you look at the history, this was added by mistake in:

commit 2ea411bc2c0a5a4c7ab145270f1949630460e72b
Author: Juergen Gross <[email protected]>
Date:   Wed Jan 13 14:00:20 2021 +0100

Yes, this seems to be the case. I had this patch lying around for some time and moving the LU patches further up in the queue seems to have missed this line.

Juergen