Skip to content

Commit

Permalink
tools/xenstore: move per connection read and write func hooks into a …
Browse files Browse the repository at this point in the history
…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).

Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Julien Grall <jgrall@amazon.com>
  • Loading branch information
jgross1 authored and Julien Grall committed May 18, 2021
1 parent 3ac8835 commit 8b9890e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
44 changes: 18 additions & 26 deletions tools/xenstore/xenstored_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -1532,8 +1532,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;
Expand All @@ -1558,8 +1558,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;

Expand All @@ -1582,7 +1582,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;

Expand All @@ -1592,8 +1592,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);
Expand Down Expand Up @@ -1622,20 +1621,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;

Expand All @@ -1651,7 +1638,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;

Expand All @@ -1673,6 +1660,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;
Expand All @@ -1682,7 +1674,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
Expand Down
21 changes: 11 additions & 10 deletions tools/xenstore/xenstored_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, ...);
Expand Down Expand Up @@ -256,10 +258,9 @@ 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;
#ifndef NO_SOCKETS
extern const struct interface_funcs socket_funcs;
#endif
extern xengnttab_handle **xgt_handle;

int remember_string(struct hashtable *hash, const char *str);
Expand Down
13 changes: 11 additions & 2 deletions tools/xenstore/xenstored_domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8b9890e

Please sign in to comment.