All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>, Wei Liu <wl@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v3 07/33] tools/xenlogd: add 9pfs version request support
Date: Thu,  4 Jan 2024 10:00:29 +0100	[thread overview]
Message-ID: <20240104090055.27323-8-jgross@suse.com> (raw)
In-Reply-To: <20240104090055.27323-1-jgross@suse.com>

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 <jgross@suse.com>
---
V3:
- use unaligned helper macros (Jason Andryuk)
---
 tools/xen-9pfsd/io.c | 201 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 201 insertions(+)

diff --git a/tools/xen-9pfsd/io.c b/tools/xen-9pfsd/io.c
index 476562298b..245faeb9a6 100644
--- a/tools/xen-9pfsd/io.c
+++ b/tools/xen-9pfsd/io.c
@@ -22,8 +22,12 @@
 #include "xen-9pfsd.h"
 
 /* P9 protocol commands (response is either cmd+1 or P9_CMD_ERROR). */
+#define P9_CMD_VERSION    100
 #define P9_CMD_ERROR      107
 
+#define P9_MIN_MSIZE      2048
+#define P9_VERSION        "9P2000.u"
+
 struct p9_qid {
     uint8_t type;
 #define QID_TYPE_DIR      0x80
@@ -276,6 +280,169 @@ static unsigned int add_string(struct ring *ring, const char *str,
     return ret;
 }
 
+static bool chk_data(struct ring *ring, void *data, unsigned int len)
+{
+    struct p9_header *hdr = ring->buffer;
+
+    if ( data + len <= ring->buffer + hdr->size )
+        return true;
+
+    errno = E2BIG;
+
+    return false;
+}
+
+static bool fill_data_elem(void **par, void **array, unsigned int *array_sz,
+                           unsigned int elem_sz, void *data)
+{
+    if ( *array_sz && !*array )
+    {
+        *array = calloc(*array_sz, elem_sz);
+        if ( !*array )
+            return false;
+        *par = *array;
+    }
+
+    memcpy(*par, data, elem_sz);
+
+    if ( *array_sz )
+    {
+        *par += elem_sz;
+        *array_sz -= 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 elements)
+ *    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 the 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 + <length> bytes)
+ *    2 parameters are consumed, first an unsigned int for the length, then 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 + <length> characters)
+ *    The 0-terminated string is stored in device->str + off, off is stored 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 = ring->buffer;
+    void *data = hdr + 1;
+    void *par;
+    unsigned int pars = 0;
+    const char *f;
+    va_list ap;
+    unsigned int len;
+    unsigned int str_off;
+    unsigned int array_sz = 0;
+    void **array = NULL;
+
+    va_start(ap, fmt);
+
+    for ( f = fmt; *f; f++ )
+    {
+        if ( !array_sz )
+            par = 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 = get_unaligned((uint16_t *)data);
+            data += sizeof(uint16_t);
+            *(unsigned int *)par = array_sz;
+            array = va_arg(ap, void **);
+            *array = 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 += sizeof(uint8_t);
+            break;
+
+        case 'D':
+            if ( array_sz )
+                fmt_err(fmt);
+            if ( !chk_data(ring, data, sizeof(uint32_t)) )
+                return pars;
+            len = get_unaligned((uint32_t *)data);
+            data += sizeof(uint32_t);
+            *(unsigned int *)par = len;
+            par = va_arg(ap, void *);
+            if ( !chk_data(ring, data, len) )
+                return pars;
+            memcpy(par, data, len);
+            data += 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 += sizeof(uint64_t);
+            break;
+
+        case 'S':
+            if ( !chk_data(ring, data, sizeof(uint16_t)) )
+                return pars;
+            len = get_unaligned((uint16_t *)data);
+            data += sizeof(uint16_t);
+            if ( !chk_data(ring, data, len) )
+                return pars;
+            str_off = add_string(ring, data, len);
+            if ( str_off == ~0 )
+                return pars;
+            if ( !fill_data_elem(&par, array, &array_sz, sizeof(unsigned int),
+                                 &str_off) )
+                return pars;
+            data += 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 += 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;
@@ -287,6 +454,36 @@ static void p9_error(struct ring *ring, uint16_t tag, uint32_t err)
                 &err);
 }
 
+static void p9_version(struct ring *ring, struct p9_header *hdr)
+{
+    uint32_t max_size;
+    unsigned int off;
+    char *version;
+    int ret;
+
+    ret = fill_data(ring, "US", &max_size, &off);
+    if ( ret != 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 = max_size;
+
+    version = ring->str + off;
+    if ( strcmp(version, P9_VERSION) )
+        version = "unknown";
+
+    fill_buffer(ring, hdr->cmd + 1, hdr->tag, "US", &ring->max_size, version);
+}
+
 void *io_thread(void *arg)
 {
     struct ring *ring = arg;
@@ -342,6 +539,10 @@ void *io_thread(void *arg)
 
             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);
-- 
2.35.3



  parent reply	other threads:[~2024-01-04  9:01 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-04  9:00 [PATCH v3 00/33] tools: enable xenstore-stubdom to use 9pfs Juergen Gross
2024-01-04  9:00 ` [PATCH v3 01/33] tools: add access macros for unaligned data Juergen Gross
2024-01-05 15:21   ` Andrew Cooper
2024-01-04  9:00 ` [PATCH v3 02/33] xen/public: add some more 9pfs xenstore paths Juergen Gross
2024-01-04  9:00 ` [PATCH v3 03/33] tools: add a new xen logging daemon Juergen Gross
2024-01-05 15:48   ` Andrew Cooper
2024-01-04  9:00 ` [PATCH v3 04/33] tools/xenlogd: connect to frontend Juergen Gross
2024-01-05 15:06   ` Jason Andryuk
2024-01-04  9:00 ` [PATCH v3 05/33] tools/xenlogd: add transport layer Juergen Gross
2024-01-04  9:00 ` [PATCH v3 06/33] tools/xenlogd: add 9pfs response generation support Juergen Gross
2024-01-05 15:08   ` Jason Andryuk
2024-01-04  9:00 ` Juergen Gross [this message]
2024-01-05 15:09   ` [PATCH v3 07/33] tools/xenlogd: add 9pfs version request support Jason Andryuk
2024-01-04  9:00 ` [PATCH v3 08/33] tools/xenlogd: add 9pfs attach " Juergen Gross
2024-01-09 18:48   ` Jason Andryuk
2024-01-31 12:09     ` Jürgen Groß
2024-01-04  9:00 ` [PATCH v3 09/33] tools/xenlogd: add 9pfs walk " Juergen Gross
2024-01-09 19:19   ` Jason Andryuk
2024-01-31 12:10     ` Jürgen Groß
2024-01-04  9:00 ` [PATCH v3 10/33] tools/xenlogd: add 9pfs open " Juergen Gross
2024-01-09 19:37   ` Jason Andryuk
2024-01-04  9:00 ` [PATCH v3 11/33] tools/xenlogd: add 9pfs clunk " Juergen Gross
2024-01-09 19:41   ` Jason Andryuk
2024-01-04  9:00 ` [PATCH v3 12/33] tools/xenlogd: add 9pfs create " Juergen Gross
2024-01-09 19:45   ` Jason Andryuk
2024-01-04  9:00 ` [PATCH v3 13/33] tools/xenlogd: add 9pfs stat " Juergen Gross
2024-01-09 19:48   ` Jason Andryuk
2024-01-04  9:00 ` [PATCH v3 14/33] tools/xenlogd: add 9pfs write " Juergen Gross
2024-01-04  9:00 ` [PATCH v3 15/33] tools/xenlogd: add 9pfs read " Juergen Gross
2024-01-04  9:00 ` [PATCH v3 16/33] tools/libs/light: add backend type for 9pfs PV devices Juergen Gross
2024-01-05 11:00   ` George Dunlap
2024-01-12 16:55   ` Anthony PERARD
2024-01-31 15:18     ` Jürgen Groß
2024-01-31 16:55       ` Anthony PERARD
2024-01-15 15:38   ` Anthony PERARD
2024-01-31 15:19     ` Jürgen Groß
2024-01-04  9:00 ` [PATCH v3 17/33] tools/xl: support new 9pfs backend xen-9pfsd Juergen Gross
2024-01-09 20:01   ` Jason Andryuk
2024-01-15 15:14   ` Anthony PERARD
2024-01-31 15:20     ` Jürgen Groß
2024-02-02 15:28       ` Juergen Gross
2024-02-02 15:43         ` Juergen Gross
2024-01-04  9:00 ` [PATCH v3 18/33] tools/helpers: allocate xenstore event channel for xenstore stubdom Juergen Gross
2024-01-04  9:00 ` [PATCH v3 19/33] tools/xenstored: rename xenbus_evtchn() Juergen Gross
2024-01-04  9:00 ` [PATCH v3 20/33] stubdom: extend xenstore stubdom configs Juergen Gross
2024-01-04  9:00 ` [PATCH v3 21/33] tools: add 9pfs device to xenstore-stubdom Juergen Gross
2024-01-15 15:31   ` Anthony PERARD
2024-02-02 16:34     ` Juergen Gross
2024-01-04  9:00 ` [PATCH v3 22/33] tools/xenstored: add early_init() function Juergen Gross
2024-01-10 20:23   ` Jason Andryuk
2024-01-25 18:47   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 23/33] tools/xenstored: move systemd handling to posix.c Juergen Gross
2024-01-10 20:26   ` Jason Andryuk
2024-01-25 18:48   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 24/33] tools/xenstored: move all log-pipe handling into posix.c Juergen Gross
2024-01-10 20:30   ` Jason Andryuk
2024-01-25 18:54   ` Julien Grall
2024-02-05  8:06     ` Juergen Gross
2024-01-04  9:00 ` [PATCH v3 25/33] tools/xenstored: move all socket " Juergen Gross
2024-01-10 20:47   ` Jason Andryuk
2024-01-26 16:22   ` Julien Grall
2024-02-05  8:07     ` Juergen Gross
2024-02-05  9:35       ` Julien Grall
2024-02-05  9:21     ` Juergen Gross
2024-02-05  9:36       ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 26/33] tools/xenstored: get own domid in stubdom case Juergen Gross
2024-01-10 20:48   ` Jason Andryuk
2024-01-26 16:23   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 27/33] tools/xenstored: rework ring page (un)map functions Juergen Gross
2024-01-04  9:00 ` [PATCH v3 28/33] tools/xenstored: split domain_init() Juergen Gross
2024-01-10 20:50   ` Jason Andryuk
2024-01-26 16:25   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 29/33] tools/xenstored: map stubdom interface Juergen Gross
2024-01-10 20:52   ` Jason Andryuk
2024-01-26 16:29   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 30/33] tools/xenstored: mount 9pfs device in stubdom Juergen Gross
2024-01-26 17:25   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 31/33] tools/xenstored: add helpers for filename handling Juergen Gross
2024-01-26 17:27   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 32/33] tools/xenstored: support complete log capabilities in stubdom Juergen Gross
2024-01-26 17:29   ` Julien Grall
2024-01-04  9:00 ` [PATCH v3 33/33] tools/xenstored: have a single do_control_memreport() Juergen Gross
2024-01-05 15:20 ` [PATCH v3 00/33] tools: enable xenstore-stubdom to use 9pfs Andrew Cooper
2024-01-15 15:42   ` Anthony PERARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240104090055.27323-8-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=anthony.perard@citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.