From nobody Thu Nov 28 08:28:51 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=1676026046; cv=none; d=zohomail.com; s=zohoarc; b=VV3w1JQ9iFmjtr/GQJYY7KJmNsBIs3Cc1W9eNtaAaBbPVJLazWnXkglEQ8sBD0tPmidV+43HMBITF97b1cKQ/M+3YPTnJM0JQeBh3DhpReMbJfALwnrF+23N5JHuSXtezy4YsaLp1JU41Wbi36vwQa1wKFIedLtwJB8h8KKmEA8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676026046; 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=LfzPuWES19Z6zkc6ddDA2+HHqNpphAyol7vuoCqTTog=; b=Fea+wdw5m1XcobAv7JYPlos90Tt0bywNMZYT21XAltoFd01opCR+hYGygjYe/kPt19+AyeL0B9qp8Gb5VGkb6XVzvwveZGXu89KA9kPPs5YhPaWVszXJT8Us2MXs2cnfaElzUdc5jBECHiZKVLf2rr+P8BeJwyIQjK/+L7qnDHU= 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) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1676026046364884.6940855398032; Fri, 10 Feb 2023 02:47:26 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.493262.763148 (Exim 4.92) (envelope-from ) id 1pQQvi-0007KO-Rk; Fri, 10 Feb 2023 10:46:58 +0000 Received: by outflank-mailman (output) from mailman id 493262.763148; Fri, 10 Feb 2023 10:46:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQQvi-0007KD-O1; Fri, 10 Feb 2023 10:46:58 +0000 Received: by outflank-mailman (input) for mailman id 493262; Fri, 10 Feb 2023 10:46:57 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQQvg-000617-Oy for xen-devel@lists.xenproject.org; Fri, 10 Feb 2023 10:46:56 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3b473f65-a930-11ed-93b5-47a8fe42b414; Fri, 10 Feb 2023 11:46:53 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 875945CA5F; Fri, 10 Feb 2023 10:46:53 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5A2101325E; Fri, 10 Feb 2023 10:46:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id zWCwFJ0g5mPzUwAAMHmgww (envelope-from ); Fri, 10 Feb 2023 10:46:53 +0000 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: 3b473f65-a930-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1676026013; 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=LfzPuWES19Z6zkc6ddDA2+HHqNpphAyol7vuoCqTTog=; b=GIBT8TTNBayaUmMcymygthXHVyC152KeQDlcror7XNkR6fy91STK/zBi9jv06fw1TW0mVD NUZW4SFP7o63G6Moegu++0snEw5DKiTUwXXw20BJSTj6s7qyanIAE70qP7qj3jwvwBaMN3 FGk5rL7CQO1tvOLGFOlkAW7jPaBt9GY= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, wl@xen.org, Juergen Gross Subject: [PATCH v2 4/7] Mini-OS: add 9pfs frontend Date: Fri, 10 Feb 2023 11:46:25 +0100 Message-Id: <20230210104628.14374-5-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230210104628.14374-1-jgross@suse.com> References: <20230210104628.14374-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1676026047928100001 Content-Type: text/plain; charset="utf-8" Add a frontend for the 9pfs PV device. For now add only the code needed to connect to the backend and the related disconnect functionality. The 9pfs protocol support will be added later. Due to its nature (ability to access files) the whole code is guarded by "#ifdef HAVE_LIBC". Signed-off-by: Juergen Gross Reviewed-by: Samuel Thibault --- V2: - add better error handling to version parsing (Samuel Thibault) --- 9pfront.c | 286 ++++++++++++++++++++++++++++++++++ Config.mk | 1 + Makefile | 1 + arch/x86/testbuild/all-no | 1 + arch/x86/testbuild/all-yes | 1 + arch/x86/testbuild/newxen-yes | 1 + include/9pfront.h | 7 + 7 files changed, 298 insertions(+) create mode 100644 9pfront.c create mode 100644 include/9pfront.h diff --git a/9pfront.c b/9pfront.c new file mode 100644 index 00000000..89ecb3a1 --- /dev/null +++ b/9pfront.c @@ -0,0 +1,286 @@ +/* + * Minimal 9pfs PV frontend for Mini-OS. + * Copyright (c) 2023 Juergen Gross, SUSE Software Solution GmbH + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_LIBC +struct dev_9pfs { + int id; + char nodename[20]; + unsigned int dom; + char *backend; + + char *tag; + const char *mnt; + + struct xen_9pfs_data_intf *intf; + struct xen_9pfs_data data; + RING_IDX prod_pvt_out; + RING_IDX cons_pvt_in; + + grant_ref_t ring_ref; + evtchn_port_t evtchn; + unsigned int ring_order; + xenbus_event_queue events; +}; + +#define DEFAULT_9PFS_RING_ORDER 4 + +static unsigned int ftype_9pfs; + +static void intr_9pfs(evtchn_port_t port, struct pt_regs *regs, void *data) +{ +} + +static int open_9pfs(struct mount_point *mnt, const char *pathname, int fl= ags, + mode_t mode) +{ + errno =3D ENOSYS; + + return -1; +} + +static void free_9pfront(struct dev_9pfs *dev) +{ + unsigned int i; + + if ( dev->data.in && dev->intf ) + { + for ( i =3D 0; i < (1 << dev->ring_order); i++ ) + gnttab_end_access(dev->intf->ref[i]); + free_pages(dev->data.in, dev->ring_order); + } + unbind_evtchn(dev->evtchn); + gnttab_end_access(dev->ring_ref); + free_page(dev->intf); + free(dev->backend); + free(dev->tag); + free(dev); +} + +void *init_9pfront(unsigned int id, const char *mnt) +{ + struct dev_9pfs *dev; + char *msg; + char *reason =3D ""; + xenbus_transaction_t xbt; + int retry =3D 1; + char bepath[64] =3D { 0 }; + XenbusState state; + unsigned int i; + void *addr; + char *version; + char *v; + + printk("9pfsfront add %u, for mount at %s\n", id, mnt); + dev =3D malloc(sizeof(*dev)); + memset(dev, 0, sizeof(*dev)); + snprintf(dev->nodename, sizeof(dev->nodename), "device/9pfs/%u", id); + dev->id =3D id; + + msg =3D xenbus_read_unsigned(XBT_NIL, dev->nodename, "backend-id", &de= v->dom); + if ( msg ) + goto err; + msg =3D xenbus_read_string(XBT_NIL, dev->nodename, "backend", &dev->ba= ckend); + if ( msg ) + goto err; + msg =3D xenbus_read_string(XBT_NIL, dev->nodename, "tag", &dev->tag); + if ( msg ) + goto err; + + snprintf(bepath, sizeof(bepath), "%s/state", dev->backend); + free(xenbus_watch_path_token(XBT_NIL, bepath, bepath, &dev->events)); + state =3D xenbus_read_integer(bepath); + while ( msg =3D=3D NULL && state < XenbusStateInitWait ) + msg =3D xenbus_wait_for_state_change(bepath, &state, &dev->events); + if ( msg || state !=3D XenbusStateInitWait ) + { + reason =3D "illegal backend state"; + goto err; + } + + msg =3D xenbus_read_unsigned(XBT_NIL, dev->backend, "max-ring-page-ord= er", + &dev->ring_order); + if ( msg ) + goto err; + if ( dev->ring_order > DEFAULT_9PFS_RING_ORDER ) + dev->ring_order =3D DEFAULT_9PFS_RING_ORDER; + + msg =3D xenbus_read_string(XBT_NIL, dev->backend, "versions", &version= ); + if ( msg ) + goto err; + for ( v =3D version; *v; v++ ) + { + if ( strtoul(v, &v, 10) =3D=3D 1 && (*v =3D=3D ',' || *v =3D=3D 0)= ) + { + v =3D NULL; + break; + } + if ( *v !=3D ',' && *v !=3D 0 ) + { + reason =3D "backend published illegal version string"; + free(version); + goto err; + } + } + free(version); + if ( v ) + { + reason =3D "backend doesn't support version 1"; + goto err; + } + + dev->ring_ref =3D gnttab_alloc_and_grant((void **)&dev->intf); + memset(dev->intf, 0, PAGE_SIZE); + if ( evtchn_alloc_unbound(dev->dom, intr_9pfs, dev, &dev->evtchn) ) + { + reason =3D "no event channel"; + goto err; + } + dev->intf->ring_order =3D dev->ring_order; + dev->data.in =3D (void *)alloc_pages(dev->ring_order); + dev->data.out =3D dev->data.in + XEN_FLEX_RING_SIZE(dev->ring_order); + for ( i =3D 0; i < (1 << dev->ring_order); i++ ) + { + addr =3D dev->data.in + i * PAGE_SIZE; + dev->intf->ref[i] =3D gnttab_grant_access(dev->dom, virt_to_mfn(ad= dr), 0); + } + + while ( retry ) + { + msg =3D xenbus_transaction_start(&xbt); + if ( msg ) + { + free(msg); + msg =3D NULL; + reason =3D "starting transaction"; + goto err; + } + + msg =3D xenbus_printf(xbt, dev->nodename, "version", "%u", 1); + if ( msg ) + goto err_tr; + msg =3D xenbus_printf(xbt, dev->nodename, "num-rings", "%u", 1); + if ( msg ) + goto err_tr; + msg =3D xenbus_printf(xbt, dev->nodename, "ring-ref0", "%u", + dev->ring_ref); + if ( msg ) + goto err_tr; + msg =3D xenbus_printf(xbt, dev->nodename, "event-channel-0", "%u", + dev->evtchn); + if ( msg ) + goto err_tr; + msg =3D xenbus_printf(xbt, dev->nodename, "state", "%u", + XenbusStateInitialised); + if ( msg ) + goto err_tr; + + free(xenbus_transaction_end(xbt, 0, &retry)); + } + + state =3D xenbus_read_integer(bepath); + while ( msg =3D=3D NULL && state < XenbusStateConnected ) + msg =3D xenbus_wait_for_state_change(bepath, &state, &dev->events); + if ( msg || state !=3D XenbusStateConnected ) + { + reason =3D "illegal backend state"; + goto err; + } + + msg =3D xenbus_printf(XBT_NIL, dev->nodename, "state", "%u", + XenbusStateConnected); + if ( msg ) + goto err; + + unmask_evtchn(dev->evtchn); + + dev->mnt =3D mnt; + if ( mount(dev->mnt, dev, open_9pfs) ) + { + reason =3D "mount failed"; + goto err; + } + + return dev; + + err_tr: + free(xenbus_transaction_end(xbt, 1, &retry)); + + err: + if ( bepath[0] ) + free(xenbus_unwatch_path_token(XBT_NIL, bepath, bepath)); + if ( msg ) + printk("9pfsfront add %u failed, error %s accessing Xenstore\n", + id, msg); + else + printk("9pfsfront add %u failed, %s\n", id, reason); + free_9pfront(dev); + free(msg); + return NULL; +} + +void shutdown_9pfront(void *dev) +{ + struct dev_9pfs *dev9p =3D dev; + char bepath[64]; + XenbusState state; + char *msg; + char *reason =3D ""; + + umount(dev9p->mnt); + snprintf(bepath, sizeof(bepath), "%s/state", dev9p->backend); + + msg =3D xenbus_printf(XBT_NIL, dev9p->nodename, "state", "%u", + XenbusStateClosing); + if ( msg ) + goto err; + + state =3D xenbus_read_integer(bepath); + while ( msg =3D=3D NULL && state < XenbusStateClosing) + msg =3D xenbus_wait_for_state_change(bepath, &state, &dev9p->event= s); + if ( msg || state !=3D XenbusStateClosing ) + { + reason =3D "illegal backend state"; + goto err; + } + + msg =3D xenbus_printf(XBT_NIL, dev9p->nodename, "state", "%u", + XenbusStateClosed); + if ( msg ) + goto err; + + free_9pfront(dev9p); + + return; + + err: + if ( msg ) + printk("9pfsfront shutdown %u failed, error %s accessing Xenstore\= n", + dev9p->id, msg); + else + printk("9pfsfront shutdown %u failed, %s\n", dev9p->id, reason); + free(msg); +} + +static const struct file_ops ops_9pfs =3D { + .name =3D "9pfs", +}; + +__attribute__((constructor)) +static void initialize_9pfs(void) +{ + ftype_9pfs =3D alloc_file_type(&ops_9pfs); +} + +#endif /* HAVE_LIBC */ diff --git a/Config.mk b/Config.mk index 1a24b30e..677e93df 100644 --- a/Config.mk +++ b/Config.mk @@ -187,6 +187,7 @@ CONFIG-n +=3D CONFIG_QEMU_XS_ARGS CONFIG-n +=3D CONFIG_TEST CONFIG-n +=3D CONFIG_PCIFRONT CONFIG-n +=3D CONFIG_TPMFRONT +CONFIG-n +=3D CONFIG_9PFRONT CONFIG-n +=3D CONFIG_TPM_TIS CONFIG-n +=3D CONFIG_TPMBACK CONFIG-n +=3D CONFIG_BALLOON diff --git a/Makefile b/Makefile index 747c7c01..7ee181a2 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,7 @@ SUBDIRS :=3D lib src-$(CONFIG_BLKFRONT) +=3D blkfront.c src-$(CONFIG_CONSFRONT) +=3D consfront.c src-$(CONFIG_TPMFRONT) +=3D tpmfront.c +src-$(CONFIG_9PFRONT) +=3D 9pfront.c src-$(CONFIG_TPM_TIS) +=3D tpm_tis.c src-$(CONFIG_TPMBACK) +=3D tpmback.c src-y +=3D console.c diff --git a/arch/x86/testbuild/all-no b/arch/x86/testbuild/all-no index f79a1012..5b3e99ed 100644 --- a/arch/x86/testbuild/all-no +++ b/arch/x86/testbuild/all-no @@ -12,6 +12,7 @@ CONFIG_NETFRONT =3D n CONFIG_FBFRONT =3D n CONFIG_KBDFRONT =3D n CONFIG_CONSFRONT =3D n +CONFIG_9PFRONT =3D n CONFIG_XENBUS =3D n CONFIG_LIBXS =3D n CONFIG_LWIP =3D n diff --git a/arch/x86/testbuild/all-yes b/arch/x86/testbuild/all-yes index faa3af32..8ae489a4 100644 --- a/arch/x86/testbuild/all-yes +++ b/arch/x86/testbuild/all-yes @@ -12,6 +12,7 @@ CONFIG_NETFRONT =3D y CONFIG_FBFRONT =3D y CONFIG_KBDFRONT =3D y CONFIG_CONSFRONT =3D y +CONFIG_9PFRONT =3D y CONFIG_XENBUS =3D y CONFIG_LIBXS =3D y CONFIG_BALLOON =3D y diff --git a/arch/x86/testbuild/newxen-yes b/arch/x86/testbuild/newxen-yes index dc83e670..ee27a328 100644 --- a/arch/x86/testbuild/newxen-yes +++ b/arch/x86/testbuild/newxen-yes @@ -12,6 +12,7 @@ CONFIG_NETFRONT =3D y CONFIG_FBFRONT =3D y CONFIG_KBDFRONT =3D y CONFIG_CONSFRONT =3D y +CONFIG_9PFRONT =3D y CONFIG_XENBUS =3D y CONFIG_LIBXS =3D y CONFIG_BALLOON =3D y diff --git a/include/9pfront.h b/include/9pfront.h new file mode 100644 index 00000000..722ec564 --- /dev/null +++ b/include/9pfront.h @@ -0,0 +1,7 @@ +#ifndef __9PFRONT_H__ +#define __9PFRONT_H__ + +void *init_9pfront(unsigned int id, const char *mnt); +void shutdown_9pfront(void *dev); + +#endif /* __9PFRONT_H__ */ --=20 2.35.3