From nobody Thu Nov 28 08:52:22 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=1676277900; cv=none; d=zohomail.com; s=zohoarc; b=aBxyVb6tRDG4e1lx4W2R2v+Os6hnQAvU+XPgrN9PiF0KdpXaHWQdYXJ8HPdiJCdsuWey1ILEYOBDhiBOBg/tDX7qUs1FtcvJeo9cI9XubgtHtecA50bJR/TL+NmsUmDe+11fBFbpA5Upcvu7J3q3snP3YsUSREJpeIF6CMk6N3Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676277900; 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=4NFSUNX/XcwJ+J4tdrvx1QT/0r6zmT/B4WFWAW17s1Y=; b=KJERmItqnyJudkODHwB0imijB8dSgEULRRMVlcmpsEINPU5DR6rB1O7lzchCefrKcGte+tyrY/itbbuM1NN2KcLs1cmKOK8i8ZwpuejAiX34o0XqRKWbzn2EEmElOI5NYlaxDfMh91NwUMfHkUXSJruMCf7k2RgijgRdz3l1Yj0= 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 1676277900085123.32886897678623; Mon, 13 Feb 2023 00:45:00 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.494222.764256 (Exim 4.92) (envelope-from ) id 1pRUS4-0006rw-4y; Mon, 13 Feb 2023 08:44:44 +0000 Received: by outflank-mailman (output) from mailman id 494222.764256; Mon, 13 Feb 2023 08:44:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pRUS4-0006r9-14; Mon, 13 Feb 2023 08:44:44 +0000 Received: by outflank-mailman (input) for mailman id 494222; Mon, 13 Feb 2023 08:44:43 +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 1pRUS2-0006An-PP for xen-devel@lists.xenproject.org; Mon, 13 Feb 2023 08:44:42 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a66207d9-ab7a-11ed-93b5-47a8fe42b414; Mon, 13 Feb 2023 09:44:38 +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-out1.suse.de (Postfix) with ESMTPS id 574D037870; Mon, 13 Feb 2023 08:44:38 +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 27CEC1391B; Mon, 13 Feb 2023 08:44:38 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id NVlaCHb46WNKRAAAMHmgww (envelope-from ); Mon, 13 Feb 2023 08:44:38 +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: a66207d9-ab7a-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1676277878; 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=4NFSUNX/XcwJ+J4tdrvx1QT/0r6zmT/B4WFWAW17s1Y=; b=ustIl96+ogoy1DaxbvOYXnW7OajUyM3EpHALnPxdTl4PQWnCUph2nuuVepesGXpkt4r8NL iGr8qspU1Bgi5Hyqp2A6plTEteeirlzrHsxeFcST/dlYnekUQQjDJeKaBRVpOpxk1wU+R1 w3byhGuWbnR4KhqqjZNbmOlSHXa9Aps= 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 v3 4/7] Mini-OS: add 9pfs frontend Date: Mon, 13 Feb 2023 09:44:09 +0100 Message-Id: <20230213084412.21065-5-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230213084412.21065-1-jgross@suse.com> References: <20230213084412.21065-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1676277901134100002 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