From nobody Mon Feb 9 15:10:30 2026 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=1611307435; cv=none; d=zohomail.com; s=zohoarc; b=eK+aL//PJRUcQe9edHHQyvyF1ieRTAiUw+4tsDUIohD7LghPIOytxS0YxuH25GblkEuVSifeAOW5yhqID5LaaWSrMUJPE4MDWb+qIcxpBLOfGPCnfpBAPAKwrSi03Y+a2EzTtSG0i5AkNBPyFRYNGaa3fPVsoKeW75zM/fDN9/w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611307435; 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=pcX+rWOqAhDGj65NpKjUEipsQKULXpFhZPvdYr1P+4c=; b=annOqq7GhgZieV5HDFRrcO3clB48MvEfctXpIkSNPk4E+pt5ZV16R6HgazKaM4wTPn5b8PTzuu2CGcJcpcyO8L0WlsRcrjoauOkhAYMS2iCTwP4KSb+agIndFMSyqAEaC0rBoU6ruf7AvvrQE+9hjesQmpTarqYy9bucuCoQrnQ= 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 1611307435207639.7876534146303; Fri, 22 Jan 2021 01:23:55 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.72608.130797 (Exim 4.92) (envelope-from ) id 1l2sfL-0003HF-BG; Fri, 22 Jan 2021 09:23:39 +0000 Received: by outflank-mailman (output) from mailman id 72608.130797; Fri, 22 Jan 2021 09:23:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l2sfL-0003H7-6h; Fri, 22 Jan 2021 09:23:39 +0000 Received: by outflank-mailman (input) for mailman id 72608; Fri, 22 Jan 2021 09:23:37 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l2sfJ-00039U-MY for xen-devel@lists.xenproject.org; Fri, 22 Jan 2021 09:23:37 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 2f7368dd-0fd8-4b41-a6a3-c1320f9c17ce; Fri, 22 Jan 2021 09:23:26 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 25050B7DD; Fri, 22 Jan 2021 09:23:26 +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: 2f7368dd-0fd8-4b41-a6a3-c1320f9c17ce 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=1611307406; 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=pcX+rWOqAhDGj65NpKjUEipsQKULXpFhZPvdYr1P+4c=; b=Ph8za9kR5TvLYIt/1l2GN9zKWTMy7V9Tw0E0idEywpAcKsXMPxA+AH7RVDG2e/BG2tpATI guKyJBEvcDtAmFG1+c4D0RFsXTfuIBBFXJu+qF1V9iFBRgKNIGVt87zwL8hNuvDlPYBj9L 97upBt5ZUFK3D10R2sHJqx3mLfUlSgs= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Ian Jackson , Wei Liu Subject: [PATCH RFC 1/4] tools/xenstore: simplify xenstored main loop Date: Fri, 22 Jan 2021 10:23:21 +0100 Message-Id: <20210122092324.7178-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210122092324.7178-1-jgross@suse.com> References: <20210122092324.7178-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(). Put the interface type specific functions in an own structure and let struct connection contain only a pointer to that new function vector. Signed-off-by: Juergen Gross --- tools/xenstore/xenstored_core.c | 118 +++++++++++++++++------------- tools/xenstore/xenstored_core.h | 16 ++-- tools/xenstore/xenstored_domain.c | 11 ++- 3 files changed, 85 insertions(+), 60 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 65ecdd0568..c89f5202fe 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 @@ -1520,8 +1520,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; @@ -1546,8 +1546,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 @@ -1570,7 +1570,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(struct interface_funcs *funcs) { struct connection *new; =20 @@ -1580,8 +1580,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); @@ -1622,6 +1621,16 @@ int readfd(struct connection *conn, void *data, unsi= gned int len) errno =3D EBADF; return -1; } + +static bool socket_can_write(struct connection *conn) +{ + return false; +} + +static bool socket_can_read(struct connection *conn) +{ + return false; +} #else int writefd(struct connection *conn, const void *data, unsigned int len) { @@ -1661,6 +1670,32 @@ int readfd(struct connection *conn, void *data, unsi= gned int len) return rc; } =20 +static bool socket_can_write(struct connection *conn) +{ + 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 & POLLOUT; +} + +static bool socket_can_read(struct connection *conn) +{ + 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 & POLLIN; +} + static void accept_connection(int sock) { int fd; @@ -1670,7 +1705,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 @@ -1678,6 +1713,13 @@ static void accept_connection(int sock) } #endif =20 +struct interface_funcs socket_funcs =3D { + .write =3D writefd, + .read =3D readfd, + .can_write =3D socket_can_write, + .can_read =3D socket_can_read, +}; + static int tdb_flags; =20 /* We create initial nodes manually. */ @@ -2280,47 +2322,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 589699e833..84b364d82c 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -87,8 +87,13 @@ 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); + bool (*can_write)(struct connection *); + bool (*can_read)(struct connection *); +}; =20 struct connection { @@ -132,9 +137,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. */ + struct interface_funcs *funcs; =20 /* Support for live update: connection id. */ unsigned int conn_id; @@ -197,7 +201,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(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, ...); diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index 3d4d0649a2..04f2a974e4 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -172,6 +172,13 @@ static int readchn(struct connection *conn, void *data= , unsigned int len) return len; } =20 +static struct interface_funcs domain_funcs =3D { + .write =3D writechn, + .read =3D readchn, + .can_write =3D domain_can_write, + .can_read =3D 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) =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,7 +1295,7 @@ void read_state_connection(const void *ctx, const voi= d *state) struct domain *domain, *tdomain; =20 if (sc->conn_type =3D=3D XS_STATE_CONN_TYPE_SOCKET) { - conn =3D new_connection(writefd, readfd); + conn =3D new_connection(&socket_funcs); if (!conn) barf("error restoring connection"); conn->fd =3D sc->spec.socket_fd; --=20 2.26.2