tools/xenstore: simplify xenstored main loop
v2 cover tools/xenstore: simplify xenstored main loop
Small series to make the main loop of xenstored more readable.
Changes in V2:
- split into two patches
- use const
- NO_SOCKETS handling
Juergen Gross (2):
tools/xenstore: move per connection read and write func hooks into a
struct
tools/xenstore: simplify xenstored main loop
tools/xenstore/xenstored_core.c | 121 ++++++++++++++---------------- tools/xenstore/xenstored_core.h | 21 +++--- tools/xenstore/xenstored_domain.c | 15 +++- 3 files changed, 79 insertions(+), 78 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/2 tools/xenstore: move per connection read and write func hooks into a struct
Put the interface type specific functions into an own structure and let struct connection contain only a pointer to that new function vector.
Don't even define the socket based functions in case of NO_SOCKETS (Mini-OS).
---
V2:
- split off from V1 patch (Julien Grall)
- use const qualifier (Julien Grall)
- drop socket specific case for Mini-OS (Julien Grall)
--- tools/xenstore/xenstored_core.c | 44 +++++++++++++------------------ tools/xenstore/xenstored_core.h | 19 +++++++------ tools/xenstore/xenstored_domain.c | 13 +++++++-- 3 files changed, 38 insertions(+), 38 deletions(-)
tools/xenstore/xenstored_core.c +18 -26
@@ -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 (const 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);
@@ -1621,20 +1620,8 @@ struct connection *get_connection_by_id(unsigned int conn_id)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 ; -}#else -int writefd (struct connection *conn,const void *data,unsigned int len) +static int writefd (struct connection *conn,const void *data,unsigned int len) {int rc;
@@ -1650,7 +1637,7 @@ int writefd(struct connection *conn, const void *data, unsigned int len)return rc; } -int readfd (struct connection *conn,void *data,unsigned int len) +static int readfd (struct connection *conn,void *data,unsigned int len) {int rc;
@@ -1672,6 +1659,11 @@ int readfd(struct connection *conn, void *data, unsigned int len)return rc; } +const struct interface_funcs socket_funcs = { + .write = writefd, + .read = readfd, +}; +static void accept_connection (int sock) {int fd;
@@ -1681,7 +1673,7 @@ 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
tools/xenstore/xenstored_core.h +9 -10
@@ -86,8 +86,11 @@ 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 ); +};struct connection {
@@ -131,9 +134,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. */ +const struct interface_funcs *funcs;/* Support for live update: connection id. */ unsigned int conn_id;
@@ -196,7 +198,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 (const 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,10 +256,7 @@ 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 const struct interface_funcs socket_funcs;extern xengnttab_handle **xgt_handle;int remember_string (struct hashtable *hash,const char *str);
tools/xenstore/xenstored_domain.c +11 -2
@@ -172,6 +172,11 @@ static int readchn(struct connection *conn, void *data, unsigned int len)return len; } +static const struct interface_funcs domain_funcs = { + .write = writechn, + .read = readchn, +}; +static void *map_interface (domid_t domid) { return xengnttab_map_grant_ref(*xgt_handle, domid,
@@ -389,7 +394,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,10 +1293,14 @@ 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); +#ifdef NO_SOCKETS +barf ("socket based connection without sockets" ); +#else + conn =new_connection (&socket_funcs);if (!conn)barf ("error restoring connection" ); conn->fd = sc->spec.socket_fd; +#endif }else { domain =introduce_domain (ctx, sc->spec.ring.domid, sc->spec.ring.evtchn, true);
Signature · 1 line
2.26.2
Re: [PATCH v2 1/2] tools/xenstore: move per connection read and write func hooks into a struct
Hi Juergen,
Quoted 6 lines · Juergen Gross
tools/xenstore/xenstored_core.h +1
-1-struct connection *new_connection (connwritefn_t *write,connreadfn_t *read); +struct connection *new_connection (const 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 259
Quoted 8 lines
tools/xenstore/xenstored_core.h +1
-4@@ -254,10 +256,7 @@ 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 const struct interface_funcs socket_funcs;
Shouldn't this be protected with #ifdef NO_SOCKETS?
The rest looks good to me:
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 1/2] tools/xenstore: move per connection read and write func hooks into a struct
Quoted 19 lines · Julien Grall
Hi Juergen,
Quoted 7 lines · Juergen Gross
-struct connection *new_connection(connwritefn_t *write,
connreadfn_t*read);
+struct connection *new_connection(const structinterface_funcs*funcs); struct connection *get_connection_by_id(unsigned intconn_id); voidcheck_store(void);
voidcorrupt(struct connection *conn, const char *fmt, ...);tools/xenstore/xenstored_core.h void finish_daemonize(void); · line 259
Quoted 8 lines
tools/xenstore/xenstored_core.h +1
-4@@ -254,10 +256,7 @@ 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 const struct interface_funcs socket_funcs;Shouldn't this be protected with #ifdef
NO_SOCKETS?
Yes, I can add it.
Quoted 4 lines
The rest looks good to me:
Reviewed-by Julien Grall <[email protected]>
Thanks,
Juergen
PATCH v2 2/2 tools/xenstore: simplify xenstored main loop
In reply to Juergen GrossThe 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().
---
V2:
- split off function vector introduction (Julien Grall)
--- tools/xenstore/xenstored_core.c | 77 +++++++++++++++---------------- tools/xenstore/xenstored_core.h | 2 + tools/xenstore/xenstored_domain.c | 2 + 3 files changed, 41 insertions(+), 40 deletions(-)
tools/xenstore/xenstored_core.c +37 -40
@@ -1659,9 +1659,34 @@ static 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 ); +} +const struct interface_funcs socket_funcs = { .write = writefd, .read = readfd, + .can_write = socket_can_write, + .can_read = socket_can_read, };static void accept_connection (int sock)
@@ -2296,47 +2321,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 +2 -0
@@ -90,6 +90,8 @@ struct connection;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_domain.c +2 -0
@@ -175,6 +175,8 @@ static int readchn(struct connection *conn, void *data, unsigned int len)static const 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)
Signature · 1 line
2.26.2
Hi Juergen,
Quoted 57 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()andcan_write().Signed-off-by Juergen Gross <[email protected]>---
V2:
- split off function vector introduction (Julien Grall)--- tools/xenstore/xenstored_core.c | 77 +++++++++++++++---------------- tools/xenstore/xenstored_core.h | 2 + tools/xenstore/xenstored_domain.c | 2 + 3 files changed, 41 insertions(+), 40 deletions(-)tools/xenstore/xenstored_core.c +25
-0@@ -1659,9 +1659,34 @@ static 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 ); +} +const struct interface_funcs socket_funcs = { .write = writefd, .read = readfd, + .can_write = socket_can_write, + .can_read = socket_can_read, };static void accept_connection (int sock)@@ -2296,47 +2321,19 @@ int main(int argc, char *argv[])if (&next->list != &connections)talloc_increase_ref_count (next);
tools/xenstore/xenstored_core.c int main(int argc, char *argv[]) · line 2307
Quoted 10 lines
tools/xenstore/xenstored_core.c +0
-9-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))
AFAICT, the check "!list_empty(&conn->out_list)" can be safely removed because write_messages() will check if the list is empty (list_top() returns NULL in this case). Is that correct?
Cheers,
Signature · 1 line
Julien Grall
Quoted 62 lines · Julien Grall
Hi Juergen,
Quoted 59 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()andcan_write().Signed-off-by Juergen Gross <[email protected]>---
V2:
- split off function vector introduction (Julien Grall)
---
tools/xenstore/xenstored_core.c | 77 +++++++++++++++---------------- tools/xenstore/xenstored_core.h | 2 +tools/xenstore/xenstored_domain.c| 2 +3 files changed, 41 insertions(+), 40 deletions(-)tools/xenstore/xenstored_core.c +25
-3@@ -1659,9 +1659,34 @@ static 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); +} + const struct interface_funcs socket_funcs = { .write = writefd, .read = readfd, + .can_write = socket_can_write, + .can_read = socket_can_read, }; static void accept_connection(int sock)@@ -2296,47 +2321,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);
Quoted 8 lines
tools/xenstore/xenstored_core.c int main(int argc, char *argv[]) · line 2307
Quoted 6 lines
tools/xenstore/xenstored_core.c +0
-6- if (talloc_free(conn) == 0) - continue; - - talloc_increase_ref_count(conn); - if (domain_can_write(conn) && - !list_empty(&conn->out_list))AFAICT, the check "!list_empty(&conn->out_list)" can be safely removed because
write_messages()will check if the list is empty (list_top()returns NULL in this case). Is that correct?
Yes.
Juergen
Hi Juergen,
Quoted 72 lines · Juergen Gross
Quoted 62 lines · Julien Grall
Hi Juergen,
Quoted 59 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()andcan_write().Signed-off-by Juergen Gross <[email protected]>---
V2:
- split off function vector introduction (Julien Grall)
---
tools/xenstore/xenstored_core.c | 77 +++++++++++++++---------------- tools/xenstore/xenstored_core.h | 2 +tools/xenstore/xenstored_domain.c| 2 +3 files changed, 41 insertions(+), 40 deletions(-)tools/xenstore/xenstored_core.c +25
-3@@ -1659,9 +1659,34 @@ static 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); +} + const struct interface_funcs socket_funcs = { .write = writefd, .read = readfd, + .can_write = socket_can_write, + .can_read = socket_can_read, }; static void accept_connection(int sock)@@ -2296,47 +2321,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);Quoted 8 lines
tools/xenstore/xenstored_core.c int main(int argc, char *argv[]) · line 2307
Quoted 6 lines
tools/xenstore/xenstored_core.c +0
-6- if (talloc_free(conn) == 0) - continue; - - talloc_increase_ref_count(conn); - if (domain_can_write(conn) && - !list_empty(&conn->out_list))AFAICT, the check "!list_empty(&conn->out_list)" can be safely removed because
write_messages()will check if the list is empty (list_top()returns NULL in this case). Is that correct?
Quoted 1 line
Yes.
Thanks, how about adding in the commit message:
"Take the opportunity to remove the empty list check before calling write_messages() because the function is already able to cope with an empty list."
I can update the commit message while committing it.
Cheers,
Signature · 1 line
Julien Grall