All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs
@ 2015-09-01 22:30 Jeff Cody
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Jeff Cody @ 2015-09-01 22:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, qemu-block, jsnow, armbru, programmingkidx

Changes from RFC v1:

    Patch 1: Several typos / grammatical errors (thanks Eric, John)
             Make id_subsys_str[] const pointer to const strings (thanks Eric)
             Moved id_subsys_str[] out from  id_generate() (thanks John)
             Assert on null string for given id (thanks Eric)
             Zero-pad the 2-digit random # (thanks John)

    Patch 2: None

Born from the conversation on qemu-devel, this generation scheme uses the
format ultimately proposed by Kevin, after list discussion.

It attempts to keep the ID strings as small as possible, while fulfilling:
    
    1.) Guarantee no collisions with a user-specified ID
    2.) Identify the sub-system the ID belongs to
    3.) Guarantee of uniqueness
    4.) Spoiling predictibility, to avoid creating an assumption
        of object ordering and parsing (i.e., we don't want users to think
        they can guess the next ID based on prior behavior).

See patch 1 for the generation scheme details.

Jeff Cody (2):
  util - add automated ID generation utility
  block: auto-generated node-names

 block.c               | 25 ++++++++++++++++---------
 include/qemu-common.h |  8 ++++++++
 util/id.c             | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 9 deletions(-)

-- 
1.9.3

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-01 22:30 [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Jeff Cody
@ 2015-09-01 22:30 ` Jeff Cody
  2015-09-01 22:55   ` Eric Blake
                     ` (3 more replies)
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 2/2] block: auto-generated node-names Jeff Cody
                   ` (2 subsequent siblings)
  3 siblings, 4 replies; 13+ messages in thread
From: Jeff Cody @ 2015-09-01 22:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, qemu-block, jsnow, armbru, programmingkidx

Multiple sub-systems in QEMU may find it useful to generate IDs
for objects that a user may reference via QMP or HMP.  This patch
presents a standardized way to do it, so that automatic ID generation
follows the same rules.

This patch enforces the following rules when generating an ID:

1.) Guarantee no collisions with a user-specified ID
2.) Identify the sub-system the ID belongs to
3.) Guarantee of uniqueness
4.) Spoiling predictability, to avoid creating an assumption
    of object ordering and parsing (i.e., we don't want users to think
    they can guess the next ID based on prior behavior).

The scheme for this is as follows (no spaces):

                # subsys D RR
Reserved char --|    |   | |
Subsystem String ----|   | |
Unique number (64-bit) --| |
Two-digit random number ---|

For example, a generated node-name for the block sub-system may look
like this:

    #block076

The caller of id_generate() is responsible for freeing the generated
node name string with g_free().

Signed-off-by: Jeff Cody <jcody@redhat.com>
---
 include/qemu-common.h |  8 ++++++++
 util/id.c             | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/qemu-common.h b/include/qemu-common.h
index bbaffd1..f6b0105 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -237,6 +237,14 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
 #define STR_OR_NULL(str) ((str) ? (str) : "null")
 
 /* id.c */
+
+typedef enum IdSubSystems {
+    ID_QDEV,
+    ID_BLOCK,
+    ID_MAX      /* last element, used as array size */
+} IdSubSystems;
+
+char *id_generate(IdSubSystems);
 bool id_wellformed(const char *id);
 
 /* path.c */
diff --git a/util/id.c b/util/id.c
index 09b22fb..9457f2d 100644
--- a/util/id.c
+++ b/util/id.c
@@ -26,3 +26,39 @@ bool id_wellformed(const char *id)
     }
     return true;
 }
+
+#define ID_SPECIAL_CHAR '#'
+
+static const char * const id_subsys_str[] = {
+    [ID_QDEV]  = "qdev",
+    [ID_BLOCK] = "block",
+};
+
+/* Generates an ID of the form:
+ *
+ * "#block146",
+ *
+ *  where:
+ *      - "#" is always the reserved character '#'
+ *      - "block" refers to the subsystem identifed via IdSubSystems
+ *        and id_subsys_str[]
+ *      - "1" is a unique number (up to a uint64_t) for the subsystem
+ *      - "46" is a zero-padded two digit pseudo-random number
+ *
+ * The caller is responsible for freeing the returned string with g_free()
+ */
+char *id_generate(IdSubSystems id)
+{
+    static uint64_t id_counters[ID_MAX];
+    uint32_t rnd;
+
+    assert(id < ID_MAX);
+    assert(id_subsys_str[id]);
+
+    rnd = g_random_int_range(0, 99);
+
+    return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR,
+                                                        id_subsys_str[id],
+                                                        id_counters[id]++,
+                                                        rnd);
+}
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [Qemu-devel] [PATCH v2 2/2] block: auto-generated node-names
  2015-09-01 22:30 [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Jeff Cody
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
@ 2015-09-01 22:30 ` Jeff Cody
  2015-09-18 13:03   ` Markus Armbruster
  2015-09-03 15:02 ` [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Kevin Wolf
  2015-09-18 13:06 ` [Qemu-devel] " Markus Armbruster
  3 siblings, 1 reply; 13+ messages in thread
From: Jeff Cody @ 2015-09-01 22:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, qemu-block, jsnow, armbru, programmingkidx

If a node-name is not specified, automatically generate the node-name.

Generated node-names will use the "block" sub-system identifier.

Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Signed-off-by: Jeff Cody <jcody@redhat.com>
---
 block.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/block.c b/block.c
index d088ee0..892e127 100644
--- a/block.c
+++ b/block.c
@@ -771,32 +771,39 @@ static void bdrv_assign_node_name(BlockDriverState *bs,
                                   const char *node_name,
                                   Error **errp)
 {
+    char *gen_node_name = NULL;
+
     if (!node_name) {
-        return;
-    }
-
-    /* Check for empty string or invalid characters */
-    if (!id_wellformed(node_name)) {
-        error_setg(errp, "Invalid node name");
-        return;
+        gen_node_name = id_generate(ID_BLOCK);
+        node_name = gen_node_name;
+    } else {
+        /* Check for empty string or invalid characters, but not if it is
+         * generated (generated names use characters not available to the user)
+         * */
+        if (!id_wellformed(node_name)) {
+            error_setg(errp, "Invalid node name");
+            return;
+        }
     }
 
     /* takes care of avoiding namespaces collisions */
     if (blk_by_name(node_name)) {
         error_setg(errp, "node-name=%s is conflicting with a device id",
                    node_name);
-        return;
+        goto out;
     }
 
     /* takes care of avoiding duplicates node names */
     if (bdrv_find_node(node_name)) {
         error_setg(errp, "Duplicate node name");
-        return;
+        goto out;
     }
 
     /* copy node name into the bs and insert it into the graph list */
     pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
     QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
+out:
+    g_free(gen_node_name);
 }
 
 static QemuOptsList bdrv_runtime_opts = {
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
@ 2015-09-01 22:55   ` Eric Blake
  2015-09-02  0:08     ` Jeff Cody
  2015-09-02  0:13   ` John Snow
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 13+ messages in thread
From: Eric Blake @ 2015-09-01 22:55 UTC (permalink / raw)
  To: Jeff Cody, qemu-devel; +Cc: kwolf, qemu-block, armbru, programmingkidx, jsnow

[-- Attachment #1: Type: text/plain, Size: 2016 bytes --]

On 09/01/2015 04:30 PM, Jeff Cody wrote:
> Multiple sub-systems in QEMU may find it useful to generate IDs
> for objects that a user may reference via QMP or HMP.  This patch
> presents a standardized way to do it, so that automatic ID generation
> follows the same rules.
> 
> This patch enforces the following rules when generating an ID:
> 
> 1.) Guarantee no collisions with a user-specified ID
> 2.) Identify the sub-system the ID belongs to
> 3.) Guarantee of uniqueness
> 4.) Spoiling predictability, to avoid creating an assumption
>     of object ordering and parsing (i.e., we don't want users to think
>     they can guess the next ID based on prior behavior).
> 
> The scheme for this is as follows (no spaces):
> 
>                 # subsys D RR
> Reserved char --|    |   | |
> Subsystem String ----|   | |
> Unique number (64-bit) --| |
> Two-digit random number ---|
> 
> For example, a generated node-name for the block sub-system may look
> like this:
> 
>     #block076
> 
> The caller of id_generate() is responsible for freeing the generated
> node name string with g_free().
> 
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  include/qemu-common.h |  8 ++++++++
>  util/id.c             | 36 ++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)

Reviewed-by: Eric Blake <eblake@redhat.com>

> +
> +static const char * const id_subsys_str[] = {
> +    [ID_QDEV]  = "qdev",
> +    [ID_BLOCK] = "block",
> +};

We're inconsistent on whether there should be a space in '*const'
(compare commit a91e211 with b86f461, for example).  But since qemu.git
has both spellings in various contexts, it's probably not worth changing
here.

> +
> +    rnd = g_random_int_range(0, 99);

Thankfully, g_random_* is a PRNG that is not consuming system entropy
(auto-generated names should not be starving /dev/random).

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-01 22:55   ` Eric Blake
@ 2015-09-02  0:08     ` Jeff Cody
  0 siblings, 0 replies; 13+ messages in thread
From: Jeff Cody @ 2015-09-02  0:08 UTC (permalink / raw)
  To: Eric Blake; +Cc: kwolf, qemu-block, qemu-devel, armbru, programmingkidx, jsnow

On Tue, Sep 01, 2015 at 04:55:16PM -0600, Eric Blake wrote:
> On 09/01/2015 04:30 PM, Jeff Cody wrote:
> > Multiple sub-systems in QEMU may find it useful to generate IDs
> > for objects that a user may reference via QMP or HMP.  This patch
> > presents a standardized way to do it, so that automatic ID generation
> > follows the same rules.
> > 
> > This patch enforces the following rules when generating an ID:
> > 
> > 1.) Guarantee no collisions with a user-specified ID
> > 2.) Identify the sub-system the ID belongs to
> > 3.) Guarantee of uniqueness
> > 4.) Spoiling predictability, to avoid creating an assumption
> >     of object ordering and parsing (i.e., we don't want users to think
> >     they can guess the next ID based on prior behavior).
> > 
> > The scheme for this is as follows (no spaces):
> > 
> >                 # subsys D RR
> > Reserved char --|    |   | |
> > Subsystem String ----|   | |
> > Unique number (64-bit) --| |
> > Two-digit random number ---|
> > 
> > For example, a generated node-name for the block sub-system may look
> > like this:
> > 
> >     #block076
> > 
> > The caller of id_generate() is responsible for freeing the generated
> > node name string with g_free().
> > 
> > Signed-off-by: Jeff Cody <jcody@redhat.com>
> > ---
> >  include/qemu-common.h |  8 ++++++++
> >  util/id.c             | 36 ++++++++++++++++++++++++++++++++++++
> >  2 files changed, 44 insertions(+)
> 
> Reviewed-by: Eric Blake <eblake@redhat.com>
> 
> > +
> > +static const char * const id_subsys_str[] = {
> > +    [ID_QDEV]  = "qdev",
> > +    [ID_BLOCK] = "block",
> > +};
> 
> We're inconsistent on whether there should be a space in '*const'
> (compare commit a91e211 with b86f461, for example).  But since qemu.git
> has both spellings in various contexts, it's probably not worth changing
> here.
> 

If this ends up needing another rev, I'll change it for you :)

> > +
> > +    rnd = g_random_int_range(0, 99);
> 
> Thankfully, g_random_* is a PRNG that is not consuming system entropy
> (auto-generated names should not be starving /dev/random).
>

Yup.


Thanks,

Jeff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
  2015-09-01 22:55   ` Eric Blake
@ 2015-09-02  0:13   ` John Snow
  2015-09-02  6:35   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
  2015-09-18 12:58   ` [Qemu-devel] " Markus Armbruster
  3 siblings, 0 replies; 13+ messages in thread
From: John Snow @ 2015-09-02  0:13 UTC (permalink / raw)
  To: Jeff Cody, qemu-devel; +Cc: kwolf, programmingkidx, armbru, qemu-block



On 09/01/2015 06:30 PM, Jeff Cody wrote:
> Multiple sub-systems in QEMU may find it useful to generate IDs
> for objects that a user may reference via QMP or HMP.  This patch
> presents a standardized way to do it, so that automatic ID generation
> follows the same rules.
> 
> This patch enforces the following rules when generating an ID:
> 
> 1.) Guarantee no collisions with a user-specified ID
> 2.) Identify the sub-system the ID belongs to
> 3.) Guarantee of uniqueness
> 4.) Spoiling predictability, to avoid creating an assumption
>     of object ordering and parsing (i.e., we don't want users to think
>     they can guess the next ID based on prior behavior).
> 
> The scheme for this is as follows (no spaces):
> 
>                 # subsys D RR
> Reserved char --|    |   | |
> Subsystem String ----|   | |
> Unique number (64-bit) --| |
> Two-digit random number ---|
> 
> For example, a generated node-name for the block sub-system may look
> like this:
> 
>     #block076
> 
> The caller of id_generate() is responsible for freeing the generated
> node name string with g_free().
> 
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  include/qemu-common.h |  8 ++++++++
>  util/id.c             | 36 ++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
> 
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index bbaffd1..f6b0105 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -237,6 +237,14 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
>  #define STR_OR_NULL(str) ((str) ? (str) : "null")
>  
>  /* id.c */
> +
> +typedef enum IdSubSystems {
> +    ID_QDEV,
> +    ID_BLOCK,
> +    ID_MAX      /* last element, used as array size */
> +} IdSubSystems;
> +
> +char *id_generate(IdSubSystems);
>  bool id_wellformed(const char *id);
>  
>  /* path.c */
> diff --git a/util/id.c b/util/id.c
> index 09b22fb..9457f2d 100644
> --- a/util/id.c
> +++ b/util/id.c
> @@ -26,3 +26,39 @@ bool id_wellformed(const char *id)
>      }
>      return true;
>  }
> +
> +#define ID_SPECIAL_CHAR '#'
> +
> +static const char * const id_subsys_str[] = {
> +    [ID_QDEV]  = "qdev",
> +    [ID_BLOCK] = "block",
> +};
> +
> +/* Generates an ID of the form:
> + *
> + * "#block146",
> + *
> + *  where:
> + *      - "#" is always the reserved character '#'
> + *      - "block" refers to the subsystem identifed via IdSubSystems
> + *        and id_subsys_str[]
> + *      - "1" is a unique number (up to a uint64_t) for the subsystem
> + *      - "46" is a zero-padded two digit pseudo-random number
> + *
> + * The caller is responsible for freeing the returned string with g_free()
> + */
> +char *id_generate(IdSubSystems id)
> +{
> +    static uint64_t id_counters[ID_MAX];
> +    uint32_t rnd;
> +
> +    assert(id < ID_MAX);
> +    assert(id_subsys_str[id]);
> +
> +    rnd = g_random_int_range(0, 99);
> +
> +    return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR,
> +                                                        id_subsys_str[id],
> +                                                        id_counters[id]++,
> +                                                        rnd);
> +}
> 

Seems good to me.

Reviewed-by: John Snow <jsnow@redhat.com>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [Qemu-block] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
  2015-09-01 22:55   ` Eric Blake
  2015-09-02  0:13   ` John Snow
@ 2015-09-02  6:35   ` Alberto Garcia
  2015-09-03 14:47     ` Jeff Cody
  2015-09-18 12:58   ` [Qemu-devel] " Markus Armbruster
  3 siblings, 1 reply; 13+ messages in thread
From: Alberto Garcia @ 2015-09-02  6:35 UTC (permalink / raw)
  To: Jeff Cody, qemu-devel; +Cc: kwolf, programmingkidx, armbru, qemu-block

On Wed 02 Sep 2015 12:30:15 AM CEST, Jeff Cody <jcody@redhat.com> wrote:

> Multiple sub-systems in QEMU may find it useful to generate IDs
> for objects that a user may reference via QMP or HMP.  This patch
> presents a standardized way to do it, so that automatic ID generation
> follows the same rules.

> +
> +typedef enum IdSubSystems {
> +    ID_QDEV,
> +    ID_BLOCK,
> +    ID_MAX      /* last element, used as array size */
> +} IdSubSystems;
> +
> +char *id_generate(IdSubSystems);

Not that it matters much, but it seems that everywhere else in the QEMU
source code the rule is to name the parameters in function prototypes.

Otherwise, the patch looks good!

Reviewed-by: Alberto Garcia <berto@igalia.com>

Berto

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [Qemu-block] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-02  6:35   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
@ 2015-09-03 14:47     ` Jeff Cody
  0 siblings, 0 replies; 13+ messages in thread
From: Jeff Cody @ 2015-09-03 14:47 UTC (permalink / raw)
  To: Alberto Garcia; +Cc: kwolf, qemu-block, qemu-devel, armbru, programmingkidx

On Wed, Sep 02, 2015 at 08:35:37AM +0200, Alberto Garcia wrote:
> On Wed 02 Sep 2015 12:30:15 AM CEST, Jeff Cody <jcody@redhat.com> wrote:
> 
> > Multiple sub-systems in QEMU may find it useful to generate IDs
> > for objects that a user may reference via QMP or HMP.  This patch
> > presents a standardized way to do it, so that automatic ID generation
> > follows the same rules.
> 
> > +
> > +typedef enum IdSubSystems {
> > +    ID_QDEV,
> > +    ID_BLOCK,
> > +    ID_MAX      /* last element, used as array size */
> > +} IdSubSystems;
> > +
> > +char *id_generate(IdSubSystems);
> 
> Not that it matters much, but it seems that everywhere else in the QEMU
> source code the rule is to name the parameters in function prototypes.
>

Indeed... another syntactic tweak to make if a v3 is needed (or a
maintainer insists!)

> Otherwise, the patch looks good!
> 
> Reviewed-by: Alberto Garcia <berto@igalia.com>
> 
> Berto

Thanks!

-Jeff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs
  2015-09-01 22:30 [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Jeff Cody
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 2/2] block: auto-generated node-names Jeff Cody
@ 2015-09-03 15:02 ` Kevin Wolf
  2015-09-03 15:54   ` [Qemu-devel] [Qemu-block] " Kevin Wolf
  2015-09-18 13:06 ` [Qemu-devel] " Markus Armbruster
  3 siblings, 1 reply; 13+ messages in thread
From: Kevin Wolf @ 2015-09-03 15:02 UTC (permalink / raw)
  To: Jeff Cody; +Cc: qemu-block, jsnow, qemu-devel, armbru, programmingkidx

Am 02.09.2015 um 00:30 hat Jeff Cody geschrieben:
> Changes from RFC v1:
> 
>     Patch 1: Several typos / grammatical errors (thanks Eric, John)
>              Make id_subsys_str[] const pointer to const strings (thanks Eric)
>              Moved id_subsys_str[] out from  id_generate() (thanks John)
>              Assert on null string for given id (thanks Eric)
>              Zero-pad the 2-digit random # (thanks John)
> 
>     Patch 2: None
> 
> Born from the conversation on qemu-devel, this generation scheme uses the
> format ultimately proposed by Kevin, after list discussion.
> 
> It attempts to keep the ID strings as small as possible, while fulfilling:
>     
>     1.) Guarantee no collisions with a user-specified ID
>     2.) Identify the sub-system the ID belongs to
>     3.) Guarantee of uniqueness
>     4.) Spoiling predictibility, to avoid creating an assumption
>         of object ordering and parsing (i.e., we don't want users to think
>         they can guess the next ID based on prior behavior).
> 
> See patch 1 for the generation scheme details.

Thanks, applied to the block branch.

While applying, I fixed up the id_generate() declaration in the header
file to include an identifier for the parameter.

Kevin

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [Qemu-block] [PATCH v2 0/2] Auto-generated IDs
  2015-09-03 15:02 ` [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Kevin Wolf
@ 2015-09-03 15:54   ` Kevin Wolf
  0 siblings, 0 replies; 13+ messages in thread
From: Kevin Wolf @ 2015-09-03 15:54 UTC (permalink / raw)
  To: Jeff Cody; +Cc: programmingkidx, qemu-devel, qemu-block, armbru

Am 03.09.2015 um 17:02 hat Kevin Wolf geschrieben:
> Am 02.09.2015 um 00:30 hat Jeff Cody geschrieben:
> > Changes from RFC v1:
> > 
> >     Patch 1: Several typos / grammatical errors (thanks Eric, John)
> >              Make id_subsys_str[] const pointer to const strings (thanks Eric)
> >              Moved id_subsys_str[] out from  id_generate() (thanks John)
> >              Assert on null string for given id (thanks Eric)
> >              Zero-pad the 2-digit random # (thanks John)
> > 
> >     Patch 2: None
> > 
> > Born from the conversation on qemu-devel, this generation scheme uses the
> > format ultimately proposed by Kevin, after list discussion.
> > 
> > It attempts to keep the ID strings as small as possible, while fulfilling:
> >     
> >     1.) Guarantee no collisions with a user-specified ID
> >     2.) Identify the sub-system the ID belongs to
> >     3.) Guarantee of uniqueness
> >     4.) Spoiling predictibility, to avoid creating an assumption
> >         of object ordering and parsing (i.e., we don't want users to think
> >         they can guess the next ID based on prior behavior).
> > 
> > See patch 1 for the generation scheme details.
> 
> Thanks, applied to the block branch.
> 
> While applying, I fixed up the id_generate() declaration in the header
> file to include an identifier for the parameter.

I'm afraid we'll need a v3 that fixes the qemu-iotests output for 051
and 067. It will also require a new filter function because of the
random part in the ID.

Kevin

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
                     ` (2 preceding siblings ...)
  2015-09-02  6:35   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
@ 2015-09-18 12:58   ` Markus Armbruster
  3 siblings, 0 replies; 13+ messages in thread
From: Markus Armbruster @ 2015-09-18 12:58 UTC (permalink / raw)
  To: Jeff Cody; +Cc: kwolf, programmingkidx, jsnow, qemu-devel, qemu-block

Jeff Cody <jcody@redhat.com> writes:

> Multiple sub-systems in QEMU may find it useful to generate IDs
> for objects that a user may reference via QMP or HMP.  This patch
> presents a standardized way to do it, so that automatic ID generation
> follows the same rules.
>
> This patch enforces the following rules when generating an ID:
>
> 1.) Guarantee no collisions with a user-specified ID
> 2.) Identify the sub-system the ID belongs to
> 3.) Guarantee of uniqueness
> 4.) Spoiling predictability, to avoid creating an assumption
>     of object ordering and parsing (i.e., we don't want users to think
>     they can guess the next ID based on prior behavior).
>
> The scheme for this is as follows (no spaces):
>
>                 # subsys D RR
> Reserved char --|    |   | |
> Subsystem String ----|   | |
> Unique number (64-bit) --| |
> Two-digit random number ---|
>
> For example, a generated node-name for the block sub-system may look
> like this:
>
>     #block076
>
> The caller of id_generate() is responsible for freeing the generated
> node name string with g_free().
>
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  include/qemu-common.h |  8 ++++++++
>  util/id.c             | 36 ++++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
>
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index bbaffd1..f6b0105 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -237,6 +237,14 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end,
>  #define STR_OR_NULL(str) ((str) ? (str) : "null")
>  
>  /* id.c */
> +
> +typedef enum IdSubSystems {
> +    ID_QDEV,
> +    ID_BLOCK,
> +    ID_MAX      /* last element, used as array size */
> +} IdSubSystems;
> +
> +char *id_generate(IdSubSystems);
>  bool id_wellformed(const char *id);
>  
>  /* path.c */
> diff --git a/util/id.c b/util/id.c
> index 09b22fb..9457f2d 100644
> --- a/util/id.c
> +++ b/util/id.c
> @@ -26,3 +26,39 @@ bool id_wellformed(const char *id)
>      }
>      return true;
>  }
> +
> +#define ID_SPECIAL_CHAR '#'
> +
> +static const char * const id_subsys_str[] = {

Like Eric, I'd prefer *const.

> +    [ID_QDEV]  = "qdev",
> +    [ID_BLOCK] = "block",
> +};
> +
> +/* Generates an ID of the form:

Style nit: wing you comments on both ends, please.

> + *
> + * "#block146",
> + *
> + *  where:
> + *      - "#" is always the reserved character '#'
> + *      - "block" refers to the subsystem identifed via IdSubSystems
> + *        and id_subsys_str[]
> + *      - "1" is a unique number (up to a uint64_t) for the subsystem
> + *      - "46" is a zero-padded two digit pseudo-random number

Recommend to note that the value does not satisfy id_wellformed().

I'd specify a bit more losely:

/*
 * Generates an ID of the form PREFIX SUBSYSTEM NUMBER
 * where
 * - PREFIX is the reserved character '#'
 * - SUBSYSTEM identifies the subsystem creating the ID
 * - NUMBER is a decimal number unique within SUBSYSTEM.
 * Example: "#block146"
 *
 * Note that these IDs do not satisfy id_wellformed().

> + *
> + * The caller is responsible for freeing the returned string with g_free()
> + */
> +char *id_generate(IdSubSystems id)
> +{
> +    static uint64_t id_counters[ID_MAX];
> +    uint32_t rnd;
> +
> +    assert(id < ID_MAX);
> +    assert(id_subsys_str[id]);
> +
> +    rnd = g_random_int_range(0, 99);

99 is off by one: "Returns a random gint32 equally distributed over the
range [begin ..end -1]."

> +
> +    return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR,
> +                                                        id_subsys_str[id],
> +                                                        id_counters[id]++,
> +                                                        rnd);
> +}

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 2/2] block: auto-generated node-names
  2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 2/2] block: auto-generated node-names Jeff Cody
@ 2015-09-18 13:03   ` Markus Armbruster
  0 siblings, 0 replies; 13+ messages in thread
From: Markus Armbruster @ 2015-09-18 13:03 UTC (permalink / raw)
  To: Jeff Cody; +Cc: kwolf, programmingkidx, jsnow, qemu-devel, qemu-block

Jeff Cody <jcody@redhat.com> writes:

> If a node-name is not specified, automatically generate the node-name.
>
> Generated node-names will use the "block" sub-system identifier.
>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> Reviewed-by: John Snow <jsnow@redhat.com>
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  block.c | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/block.c b/block.c
> index d088ee0..892e127 100644
> --- a/block.c
> +++ b/block.c
> @@ -771,32 +771,39 @@ static void bdrv_assign_node_name(BlockDriverState *bs,
>                                    const char *node_name,
>                                    Error **errp)
>  {
> +    char *gen_node_name = NULL;
> +
>      if (!node_name) {
> -        return;
> -    }
> -
> -    /* Check for empty string or invalid characters */
> -    if (!id_wellformed(node_name)) {
> -        error_setg(errp, "Invalid node name");
> -        return;
> +        gen_node_name = id_generate(ID_BLOCK);
> +        node_name = gen_node_name;

I'd do

        node_name = gen_node_name = id_generate(ID_BLOCK);

but that's clearly a matter of taste.

> +    } else {
> +        /* Check for empty string or invalid characters, but not if it is
> +         * generated (generated names use characters not available to the user)
> +         * */

Untidy comment.  Make it:

        /*
         * Check for empty string or invalid characters, but not if it
         * is generated (generated names use characters not available to
         * the user)
         */

> +        if (!id_wellformed(node_name)) {
> +            error_setg(errp, "Invalid node name");
> +            return;
> +        }
>      }

I'd probably do an else if here.  Additional benefit: smaller diff.

>  
>      /* takes care of avoiding namespaces collisions */
>      if (blk_by_name(node_name)) {
>          error_setg(errp, "node-name=%s is conflicting with a device id",
>                     node_name);
> -        return;
> +        goto out;
>      }
>  
>      /* takes care of avoiding duplicates node names */
>      if (bdrv_find_node(node_name)) {
>          error_setg(errp, "Duplicate node name");
> -        return;
> +        goto out;
>      }
>  
>      /* copy node name into the bs and insert it into the graph list */
>      pstrcpy(bs->node_name, sizeof(bs->node_name), node_name);
>      QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list);
> +out:
> +    g_free(gen_node_name);
>  }
>  
>  static QemuOptsList bdrv_runtime_opts = {

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs
  2015-09-01 22:30 [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Jeff Cody
                   ` (2 preceding siblings ...)
  2015-09-03 15:02 ` [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Kevin Wolf
@ 2015-09-18 13:06 ` Markus Armbruster
  3 siblings, 0 replies; 13+ messages in thread
From: Markus Armbruster @ 2015-09-18 13:06 UTC (permalink / raw)
  To: Jeff Cody; +Cc: kwolf, programmingkidx, jsnow, qemu-devel, qemu-block

Jeff Cody <jcody@redhat.com> writes:

> Changes from RFC v1:
>
>     Patch 1: Several typos / grammatical errors (thanks Eric, John)
>              Make id_subsys_str[] const pointer to const strings (thanks Eric)
>              Moved id_subsys_str[] out from  id_generate() (thanks John)
>              Assert on null string for given id (thanks Eric)
>              Zero-pad the 2-digit random # (thanks John)
>
>     Patch 2: None
>
> Born from the conversation on qemu-devel, this generation scheme uses the
> format ultimately proposed by Kevin, after list discussion.
>
> It attempts to keep the ID strings as small as possible, while fulfilling:
>     
>     1.) Guarantee no collisions with a user-specified ID
>     2.) Identify the sub-system the ID belongs to
>     3.) Guarantee of uniqueness
>     4.) Spoiling predictibility, to avoid creating an assumption
>         of object ordering and parsing (i.e., we don't want users to think
>         they can guess the next ID based on prior behavior).
>
> See patch 1 for the generation scheme details.

Since PATCH 1 defines an ID for the qdev subsystem, I expected a patch
to put it to use.  As long as there is none, PATCH 1 shouldn't define
ID_QDEV.

Looks good apart from a harmless off-by-one and a few style nits.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-09-18 13:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-01 22:30 [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Jeff Cody
2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 1/2] util - add automated ID generation utility Jeff Cody
2015-09-01 22:55   ` Eric Blake
2015-09-02  0:08     ` Jeff Cody
2015-09-02  0:13   ` John Snow
2015-09-02  6:35   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2015-09-03 14:47     ` Jeff Cody
2015-09-18 12:58   ` [Qemu-devel] " Markus Armbruster
2015-09-01 22:30 ` [Qemu-devel] [PATCH v2 2/2] block: auto-generated node-names Jeff Cody
2015-09-18 13:03   ` Markus Armbruster
2015-09-03 15:02 ` [Qemu-devel] [PATCH v2 0/2] Auto-generated IDs Kevin Wolf
2015-09-03 15:54   ` [Qemu-devel] [Qemu-block] " Kevin Wolf
2015-09-18 13:06 ` [Qemu-devel] " Markus Armbruster

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.