From nobody Fri Oct 18 05:20:27 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; 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=fail(p=quarantine dis=quarantine) header.from=suse.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1707411395761388.1573473718464; Thu, 8 Feb 2024 08:56:35 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.678358.1055580 (Exim 4.92) (envelope-from ) id 1rY7hJ-0007vA-2C; Thu, 08 Feb 2024 16:56:25 +0000 Received: by outflank-mailman (output) from mailman id 678358.1055580; Thu, 08 Feb 2024 16:56: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 1rY7hI-0007v1-Uu; Thu, 08 Feb 2024 16:56:24 +0000 Received: by outflank-mailman (input) for mailman id 678358; Thu, 08 Feb 2024 16:56:23 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rY7hH-0005bD-Rl for xen-devel@lists.xenproject.org; Thu, 08 Feb 2024 16:56:23 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id fd331859-c6a2-11ee-8a4a-1f161083a0e0; Thu, 08 Feb 2024 17:56:23 +0100 (CET) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id E25D921F60; Thu, 8 Feb 2024 16:56:22 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B27D81326D; Thu, 8 Feb 2024 16:56:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 82saKrYHxWXVEwAAD6G6ig (envelope-from ); Thu, 08 Feb 2024 16:56:22 +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: fd331859-c6a2-11ee-8a4a-1f161083a0e0 From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Anthony PERARD , Jason Andryuk Subject: [PATCH v5 06/22] tools/9pfsd: add 9pfs version request support Date: Thu, 8 Feb 2024 17:55:30 +0100 Message-Id: <20240208165546.5715-7-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240208165546.5715-1-jgross@suse.com> References: <20240208165546.5715-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Level: Authentication-Results: smtp-out1.suse.de; none X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Score: -4.00 X-Rspamd-Queue-Id: E25D921F60 X-Spam-Flag: NO X-ZM-MESSAGEID: 1707411396950100003 Content-Type: text/plain; charset="utf-8" Add the version request of the 9pfs protocol. For the version use the "9P2000.u" variant, as it is supported by Mini-OS and Linux. For the request parsing add all format items needed even in future in order to avoid code churn for those additions later. Signed-off-by: Juergen Gross Acked-by: Anthony PERARD Reviewed-by: Jason Andryuk --- V3: - use unaligned helper macros (Jason Andryuk) --- tools/9pfsd/io.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) diff --git a/tools/9pfsd/io.c b/tools/9pfsd/io.c index 4a44c70c4d..839dd1112c 100644 --- a/tools/9pfsd/io.c +++ b/tools/9pfsd/io.c @@ -22,8 +22,12 @@ #include "xen-9pfsd.h" =20 /* P9 protocol commands (response is either cmd+1 or P9_CMD_ERROR). */ +#define P9_CMD_VERSION 100 #define P9_CMD_ERROR 107 =20 +#define P9_MIN_MSIZE 2048 +#define P9_VERSION "9P2000.u" + struct p9_qid { uint8_t type; #define QID_TYPE_DIR 0x80 @@ -294,6 +298,169 @@ static unsigned int add_string(struct ring *ring, con= st char *str, return ret; } =20 +static bool chk_data(struct ring *ring, void *data, unsigned int len) +{ + struct p9_header *hdr =3D ring->buffer; + + if ( data + len <=3D ring->buffer + hdr->size ) + return true; + + errno =3D E2BIG; + + return false; +} + +static bool fill_data_elem(void **par, void **array, unsigned int *array_s= z, + unsigned int elem_sz, void *data) +{ + if ( *array_sz && !*array ) + { + *array =3D calloc(*array_sz, elem_sz); + if ( !*array ) + return false; + *par =3D *array; + } + + memcpy(*par, data, elem_sz); + + if ( *array_sz ) + { + *par +=3D elem_sz; + *array_sz -=3D 1; + } + + return true; +} + +/* + * Fill variables with request data. + * fmt is a sequence of format characters. Supported characters are: + * a: an array (2 bytes number of elements + the following format as eleme= nts) + * The number of elements is stored in the first unsigned int parameter= , the + * next parameter is a pointer to an array of elements as denoted by th= e next + * format character. The array is allocated dynamically. + * b: 1 byte unsigned integer + * The value is stored in the next parameter with type uint8_t. + * D: Data blob (4 byte length + bytes) + * 2 parameters are consumed, first an unsigned int for the length, the= n a + * pointer to the first uint8_t value. + * No array support. + * L: 8 byte unsigned integer + * The value is stored in the next parameter with type uint64_t. + * S: String (2 byte length + characters) + * The 0-terminated string is stored in device->str + off, off is store= d in + * the next parameter with type unsigned int. + * U: 4 byte unsigned integer + * The value is stored in the next parameter with type uint32_t. + * + * Return value: number of filled variables, errno will be set in case of + * error. + */ +static int fill_data(struct ring *ring, const char *fmt, ...) +{ + struct p9_header *hdr =3D ring->buffer; + void *data =3D hdr + 1; + void *par; + unsigned int pars =3D 0; + const char *f; + va_list ap; + unsigned int len; + unsigned int str_off; + unsigned int array_sz =3D 0; + void **array =3D NULL; + + va_start(ap, fmt); + + for ( f =3D fmt; *f; f++ ) + { + if ( !array_sz ) + par =3D va_arg(ap, void *); + + switch ( *f ) + { + case 'a': + f++; + if ( !*f || array_sz ) + fmt_err(fmt); + if ( !chk_data(ring, data, sizeof(uint16_t)) ) + return pars; + array_sz =3D get_unaligned((uint16_t *)data); + data +=3D sizeof(uint16_t); + *(unsigned int *)par =3D array_sz; + array =3D va_arg(ap, void **); + *array =3D NULL; + break; + + case 'b': + if ( !chk_data(ring, data, sizeof(uint8_t)) ) + return pars; + if ( !fill_data_elem(&par, array, &array_sz, sizeof(uint8_t), + data) ) + return pars; + data +=3D sizeof(uint8_t); + break; + + case 'D': + if ( array_sz ) + fmt_err(fmt); + if ( !chk_data(ring, data, sizeof(uint32_t)) ) + return pars; + len =3D get_unaligned((uint32_t *)data); + data +=3D sizeof(uint32_t); + *(unsigned int *)par =3D len; + par =3D va_arg(ap, void *); + if ( !chk_data(ring, data, len) ) + return pars; + memcpy(par, data, len); + data +=3D len; + break; + + case 'L': + if ( !chk_data(ring, data, sizeof(uint64_t)) ) + return pars; + if ( !fill_data_elem(&par, array, &array_sz, sizeof(uint64_t), + data) ) + return pars; + data +=3D sizeof(uint64_t); + break; + + case 'S': + if ( !chk_data(ring, data, sizeof(uint16_t)) ) + return pars; + len =3D get_unaligned((uint16_t *)data); + data +=3D sizeof(uint16_t); + if ( !chk_data(ring, data, len) ) + return pars; + str_off =3D add_string(ring, data, len); + if ( str_off =3D=3D ~0 ) + return pars; + if ( !fill_data_elem(&par, array, &array_sz, sizeof(unsigned i= nt), + &str_off) ) + return pars; + data +=3D len; + break; + + case 'U': + if ( !chk_data(ring, data, sizeof(uint32_t)) ) + return pars; + if ( !fill_data_elem(&par, array, &array_sz, sizeof(uint32_t), + data) ) + return pars; + data +=3D sizeof(uint32_t); + break; + + default: + fmt_err(fmt); + } + + if ( array_sz ) + f--; + pars++; + } + + return pars; +} + static void p9_error(struct ring *ring, uint16_t tag, uint32_t err) { unsigned int erroff; @@ -305,6 +472,36 @@ static void p9_error(struct ring *ring, uint16_t tag, = uint32_t err) &err); } =20 +static void p9_version(struct ring *ring, struct p9_header *hdr) +{ + uint32_t max_size; + unsigned int off; + char *version; + int ret; + + ret =3D fill_data(ring, "US", &max_size, &off); + if ( ret !=3D 2 ) + { + p9_error(ring, hdr->tag, errno); + return; + } + + if ( max_size < P9_MIN_MSIZE ) + { + p9_error(ring, hdr->tag, EMSGSIZE); + return; + } + + if ( max_size < ring->max_size ) + ring->max_size =3D max_size; + + version =3D ring->str + off; + if ( strcmp(version, P9_VERSION) ) + version =3D "unknown"; + + fill_buffer(ring, hdr->cmd + 1, hdr->tag, "US", &ring->max_size, versi= on); +} + void *io_thread(void *arg) { struct ring *ring =3D arg; @@ -360,6 +557,10 @@ void *io_thread(void *arg) =20 switch ( hdr.cmd ) { + case P9_CMD_VERSION: + p9_version(ring, &hdr); + break; + default: syslog(LOG_DEBUG, "%u.%u sent unhandled command %u\n", ring->device->domid, ring->device->devid, hdr.cmd); --=20 2.35.3