From nobody Sun May 5 03:17:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1621318792; cv=none; d=zohomail.com; s=zohoarc; b=VceVaSyhJnmMLsREaCucmPvigNdC7L0hospwfYylHNpHb60tStoG7J8NPhHw12OTCA7n6jiioBh8mYKQIFjA/N+uHnhrSrJqF+lahduETLT7rits6hJgK8QpKPb6Ixl9j3yw0fFPjp7OGE7JTvH2+6z1oEz7PqapG32LxLur5O8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621318792; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=G6YyW3jOF1EJwsgj7luS33EIS2oEXyrGcjmcRKpanwo=; b=W9RpqDbXl8FgMCSkX2PRhJyWzl6Fjx4vB7FVVEFS1rtQyp4pqasSxAOrsmhL2YpCZXs8hv7Q7q9b5lKCV9QFkXyc/JU3fHdfMK323l6+7aZ9Iw29R+qBnQLvJrDi2A+3BORUkM91BeUpOjMdQqng7Sgv/12YZ+om//0SHLjvcHQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1621318792428901.6699335887366; Mon, 17 May 2021 23:19:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.128708.241579 (Exim 4.92) (envelope-from ) id 1lit4f-0004uQ-Ct; Tue, 18 May 2021 06:19:25 +0000 Received: by outflank-mailman (output) from mailman id 128708.241579; Tue, 18 May 2021 06:19:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lit4f-0004tB-7f; Tue, 18 May 2021 06:19:25 +0000 Received: by outflank-mailman (input) for mailman id 128708; Tue, 18 May 2021 06:19:23 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lit4d-0004r6-Rz for xen-devel@lists.xenproject.org; Tue, 18 May 2021 06:19:23 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 343f3315-56cf-42ad-8ffc-dfa3b0f2d8d8; Tue, 18 May 2021 06:19:22 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 00EACAF06; Tue, 18 May 2021 06:19:21 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 343f3315-56cf-42ad-8ffc-dfa3b0f2d8d8 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1621318761; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G6YyW3jOF1EJwsgj7luS33EIS2oEXyrGcjmcRKpanwo=; b=E62mX5q/nY0dWfHcPR42VkWIvyXDLZ2Pg14cHH20i38zYBTY/l2bpWkAbniZbPcUr7D1Ls fYDWGKkrWOm9vhrltbZmpX/HBETa1CKd/MQoiyp6IxRAIzik85Mi2i2fetwkUCyy7FD8pu kgw0o4/iSMReo3OJpiji3F5VjD1xX2I= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Ian Jackson , Wei Liu , Julien Grall Subject: [PATCH v3 1/2] tools/xenstore: move per connection read and write func hooks into a struct Date: Tue, 18 May 2021 08:19:06 +0200 Message-Id: <20210518061907.16374-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210518061907.16374-1-jgross@suse.com> References: <20210518061907.16374-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Julien Grall --- V2: - split off from V1 patch (Julien Grall) - use const qualifier (Julien Grall) - drop socket specific case for Mini-OS (Julien Grall) V3: - guard extern declaration with #ifndef NO_SOCKETS (Julien Grall) --- tools/xenstore/xenstored_core.c | 44 +++++++++++++------------------ tools/xenstore/xenstored_core.h | 21 ++++++++------- tools/xenstore/xenstored_domain.c | 13 +++++++-- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 4b7b71cfb3..856f518075 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -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 =3D conn->write(conn, out->hdr.raw + out->used, - sizeof(out->hdr) - out->used); + ret =3D conn->funcs->write(conn, out->hdr.raw + out->used, + sizeof(out->hdr) - out->used); if (ret < 0) return false; =20 @@ -243,8 +243,8 @@ static bool write_messages(struct connection *conn) return true; } =20 - ret =3D conn->write(conn, out->buffer + out->used, - out->hdr.msg.len - out->used); + ret =3D conn->funcs->write(conn, out->buffer + out->used, + out->hdr.msg.len - out->used); if (ret < 0) return false; =20 @@ -1531,8 +1531,8 @@ static void handle_input(struct connection *conn) /* Not finished header yet? */ if (in->inhdr) { if (in->used !=3D sizeof(in->hdr)) { - bytes =3D conn->read(conn, in->hdr.raw + in->used, - sizeof(in->hdr) - in->used); + bytes =3D conn->funcs->read(conn, in->hdr.raw + in->used, + sizeof(in->hdr) - in->used); if (bytes < 0) goto bad_client; in->used +=3D bytes; @@ -1557,8 +1557,8 @@ static void handle_input(struct connection *conn) in->inhdr =3D false; } =20 - bytes =3D conn->read(conn, in->buffer + in->used, - in->hdr.msg.len - in->used); + bytes =3D conn->funcs->read(conn, in->buffer + in->used, + in->hdr.msg.len - in->used); if (bytes < 0) goto bad_client; =20 @@ -1581,7 +1581,7 @@ static void handle_output(struct connection *conn) ignore_connection(conn); } =20 -struct connection *new_connection(connwritefn_t *write, connreadfn_t *read) +struct connection *new_connection(const struct interface_funcs *funcs) { struct connection *new; =20 @@ -1591,8 +1591,7 @@ struct connection *new_connection(connwritefn_t *writ= e, connreadfn_t *read) =20 new->fd =3D -1; new->pollfd_idx =3D -1; - new->write =3D write; - new->read =3D read; + new->funcs =3D funcs; new->is_ignored =3D false; new->transaction_started =3D 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 =3D EBADF; - return -1; -} - -int readfd(struct connection *conn, void *data, unsigned int len) -{ - errno =3D 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; =20 @@ -1650,7 +1637,7 @@ int writefd(struct connection *conn, const void *data= , unsigned int len) return rc; } =20 -int readfd(struct connection *conn, void *data, unsigned int len) +static int readfd(struct connection *conn, void *data, unsigned int len) { int rc; =20 @@ -1672,6 +1659,11 @@ int readfd(struct connection *conn, void *data, unsi= gned int len) return rc; } =20 +const struct interface_funcs socket_funcs =3D { + .write =3D writefd, + .read =3D readfd, +}; + static void accept_connection(int sock) { int fd; @@ -1681,7 +1673,7 @@ static void accept_connection(int sock) if (fd < 0) return; =20 - conn =3D new_connection(writefd, readfd); + conn =3D new_connection(&socket_funcs); if (conn) conn->fd =3D fd; else diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 6a6d0448e8..6c4845c196 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -86,8 +86,11 @@ struct delayed_request { }; =20 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); +}; =20 struct connection { @@ -131,9 +134,8 @@ struct connection /* My watches. */ struct list_head watches; =20 - /* 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; =20 /* Support for live update: connection id. */ unsigned int conn_id; @@ -196,7 +198,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *k= ey, struct node *node, struct node *read_node(struct connection *conn, const void *ctx, const char *name); =20 -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,9 @@ void finish_daemonize(void); /* Open a pipe for signal handling */ void init_pipe(int reopen_log_pipe[2]); =20 -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; =20 int remember_string(struct hashtable *hash, const char *str); diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index 0c17937c0f..f3cd56050e 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -172,6 +172,11 @@ static int readchn(struct connection *conn, void *data= , unsigned int len) return len; } =20 +static const struct interface_funcs domain_funcs =3D { + .write =3D writechn, + .read =3D 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) =20 domain->introduced =3D true; =20 - domain->conn =3D new_connection(writechn, readchn); + domain->conn =3D new_connection(&domain_funcs); if (!domain->conn) { errno =3D ENOMEM; return errno; @@ -1288,10 +1293,14 @@ void read_state_connection(const void *ctx, const v= oid *state) struct domain *domain, *tdomain; =20 if (sc->conn_type =3D=3D XS_STATE_CONN_TYPE_SOCKET) { - conn =3D new_connection(writefd, readfd); +#ifdef NO_SOCKETS + barf("socket based connection without sockets"); +#else + conn =3D new_connection(&socket_funcs); if (!conn) barf("error restoring connection"); conn->fd =3D sc->spec.socket_fd; +#endif } else { domain =3D introduce_domain(ctx, sc->spec.ring.domid, sc->spec.ring.evtchn, true); --=20 2.26.2 From nobody Sun May 5 03:17:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1621318789; cv=none; d=zohomail.com; s=zohoarc; b=IVe/g16mFV4no7RqV896ZaLB64SztQPBaTd2n+D42L/UVPZYdgx9uT9X1Ukfv/XFdV4GpFxotwYVEA1IYpIr3tF6n8L68JAbUwizn1S2KBTVN+o30zJuoQLZymqoQiccutHWlRPROGZSXLYm+dRVhNNH2K1qPRRqdxRbXZRkkqA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621318789; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=989znmd4h1mcR15D9WJ4lxb1rPSUzv44K4HKKcR8q7E=; b=ChzhMWQ3aYmEa4mZNBwPAGO4QcQuCyk5s1053OtWzURfUTSHQy5HgOuGhDw6fc7pmh6sgF07+kPs1AzgLs2xn8A4dN8boutbg2DTWSXkFc7wBuAZFipu/Pj9wzwP5O/6axclN0U374c+7KDJ1pWw1/2D7+E+xU6NM7b3uylQv3k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1621318789942365.40745753052556; Mon, 17 May 2021 23:19:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.128709.241597 (Exim 4.92) (envelope-from ) id 1lit4j-0005Pe-Ou; Tue, 18 May 2021 06:19:29 +0000 Received: by outflank-mailman (output) from mailman id 128709.241597; Tue, 18 May 2021 06:19:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lit4j-0005PX-LR; Tue, 18 May 2021 06:19:29 +0000 Received: by outflank-mailman (input) for mailman id 128709; Tue, 18 May 2021 06:19:28 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lit4i-0004r5-Av for xen-devel@lists.xenproject.org; Tue, 18 May 2021 06:19:28 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id b9eb95af-3541-49c4-a5d0-f0780bced95c; Tue, 18 May 2021 06:19:21 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 257A4AF09; Tue, 18 May 2021 06:19:21 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b9eb95af-3541-49c4-a5d0-f0780bced95c X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1621318761; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=989znmd4h1mcR15D9WJ4lxb1rPSUzv44K4HKKcR8q7E=; b=K8D2WDHhpm027ir+2xZwrN16a1x6CaO7Ak4rl1QNL70OwzTqMlWNqEk1opWnNsDYhr3xAq ZtZ+XeRyCjhPYWpQPoBUhtfRsgrbi0dJ9nGsC/88UCRhQr6RXvxHKVW1CoU8hGFeTI3J+i OIz1J3uqccvF2psrQILsefoKpj8HIV0= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Ian Jackson , Wei Liu Subject: [PATCH v3 2/2] tools/xenstore: simplify xenstored main loop Date: Tue, 18 May 2021 08:19:07 +0200 Message-Id: <20210518061907.16374-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210518061907.16374-1-jgross@suse.com> References: <20210518061907.16374-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" 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(). 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. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - split off function vector introduction (Julien Grall) V3: - expand commit message (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(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 856f518075..883a1a582a 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -1659,9 +1659,34 @@ static int readfd(struct connection *conn, void *dat= a, unsigned int len) return rc; } =20 +static bool socket_can_process(struct connection *conn, int mask) +{ + if (conn->pollfd_idx =3D=3D -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 =3D { .write =3D writefd, .read =3D readfd, + .can_write =3D socket_can_write, + .can_read =3D socket_can_read, }; =20 static void accept_connection(int sock) @@ -2296,47 +2321,19 @@ int main(int argc, char *argv[]) if (&next->list !=3D &connections) talloc_increase_ref_count(next); =20 - if (conn->domain) { - if (domain_can_read(conn)) - handle_input(conn); - if (talloc_free(conn) =3D=3D 0) - continue; - - talloc_increase_ref_count(conn); - if (domain_can_write(conn) && - !list_empty(&conn->out_list)) - handle_output(conn); - if (talloc_free(conn) =3D=3D 0) - continue; - } else { - if (conn->pollfd_idx !=3D -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) =3D=3D 0) - continue; - - talloc_increase_ref_count(conn); - - if (conn->pollfd_idx !=3D -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) =3D=3D 0) - continue; + if (conn->funcs->can_read(conn)) + handle_input(conn); + if (talloc_free(conn) =3D=3D 0) + continue; =20 - conn->pollfd_idx =3D -1; - } + talloc_increase_ref_count(conn); + + if (conn->funcs->can_write(conn)) + handle_output(conn); + if (talloc_free(conn) =3D=3D 0) + continue; + + conn->pollfd_idx =3D -1; } =20 if (delayed_requests) { diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 6c4845c196..bb36111ecc 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -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 *); }; =20 struct connection diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index f3cd56050e..708bf68af0 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -175,6 +175,8 @@ static int readchn(struct connection *conn, void *data,= unsigned int len) static const struct interface_funcs domain_funcs =3D { .write =3D writechn, .read =3D readchn, + .can_write =3D domain_can_write, + .can_read =3D domain_can_read, }; =20 static void *map_interface(domid_t domid) --=20 2.26.2