Skip to contentSkip to conversation

remove QEMUOptionParameter

RFC PATCH Dong Xu Wang 1 participant 1 message Sep 21, 2012 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Missed cc qemu-devel, added CC, sorry.

-------- 原始消息 --------
主题: Re: [Qemu-devel] [PATCH RFC] remove QEMUOptionParameter 日期: Fri, 21 Sep 2012 10:43:12 +0800
发件人: Dong Xu Wang <[email protected]> 收件人: Markus Armbruster <[email protected]>

Quoted 7 lines · Markus Armbruster
Quoted 1 line · Dong Xu Wang

Markus, I am working with v2 and have some questions based your comments.

Your replies are very hard to read, because whatever you use to send them wraps quoted lines. Please fix that.

Sorry for that, will notice next time.

Quoted 59 lines
Quoted 6 lines · Markus Armbruster
Quoted 5 lines

Some overlap with what I'm working on, but since you have code to show, and I don't, I'll review yours as if mine didn't exist.

Dong Xu Wang <[email protected]> writes:

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
qemu-option.c +0 -0
index 27891e7..b83ca52 100644

[...]

Quoted 47 lines
Quoted 46 lines
Quoted 34 lines

static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)

qemu-option.c +23 -5
@@ -832,14 +547,36 @@ void qemu_opts_del(QemuOpts *opts)
 
  int qemu_opts_print(QemuOpts *opts, void *dummy)
  {
-    QemuOpt *opt;
+    QemuOpt *opt = NULL;
+    QemuOptDesc *desc = opts->list->desc;
 
-    fprintf(stderr, "%s: %s:", opts->list->name,
-            opts->id ? opts->id : "<noid>");
-    QTAILQ_FOREACH(opt, &opts->head, next) {
-        fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
+    while (desc && desc->name) {
+        opt = qemu_opt_find(opts, desc->name);
+        switch (desc->type) {
+        case QEMU_OPT_STRING:
+            if (opt != NULL) {
+                printf("%s='%s' ", opt->name, opt->str);
+            }
+            break;
+        case QEMU_OPT_BOOL:
+            printf("%s=%s ", desc->name, (opt && opt->str) ? "on" : "off");
+            break;
+        case QEMU_OPT_NUMBER:
+        case QEMU_OPT_SIZE:
+            if (strcmp(desc->name, "cluster_size")) {
+                printf("%s=%" PRId64 " ", desc->name,
+                    (opt && opt->value.uint) ? opt->value.uint : 0);
+            } else {
+                printf("%s=%" PRId64 " ", desc->name,
+ (opt && opt->value.uint) ? opt->value.uint : desc->def_value);
+            }
+            break;

qemu-option.c void qemu_opts_del(QemuOpts *opts) · line 582

Quoted 10 lines
qemu-option.c +5 -1
+        default:
+            printf("%s=(unknown type) ", desc->name);
+            break;
+        }
+        desc++;
      }
-    fprintf(stderr, "\n");
      return 0;
  }
 

Why do you need to change this function?

Quoted 7 lines
Quoted 3 lines

I just want to have the same output as before, and I noticed that qemu_opts_print function
has no user. Is it Okay to change it?

Can you give examples where unchanged qemu_opts_print() prints something else than your code?

After changing is:
[wdongxu]$ qemu-img create -f qcow2 t.qcow2 8G Formatting 't.qcow2', fmt=qcow2 size=8589934592 encryption=off cluster_size=65536 lazy_refcounts=off

If use qemu_otps_print directly:
[wdongxu]$ qemu-img create -f qcow2 t.qcow2 8G
qcow2-create-opts: <noid>: size="(null)"
Formatting 't.qcow2', fmt=qcow2

Without this patch, block.c uses print_option_parameters, output is as the same as my code.

Quoted 123 lines
Quoted 61 lines
Quoted 1 line

qemu-option.c int qemu_opts_foreach(QemuOptsList *list,

Quoted 1 line
qemu-option.c +0 -0
@@ -1110,3 +847,140 @@ int qemu_opts_foreach(QemuOptsList *list,

qemu_opts_loopfunc func, void *opaque,

Quoted 26 lines
Quoted 13 lines
qemu-option.c +10 -0
      loc_pop(&loc);
      return rc;
  }
+
+static size_t count_opts_list(QemuOptsList *list)
+{
+    size_t i = 0;
+
+    while (list && list->desc[i].name) {
+        i++;
+    }
+
+    return i;

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 869

Quoted 10 lines
qemu-option.c +10 -0
+}
+
+static bool opts_desc_find(QemuOptsList *list, const char *name)
+{
+    const QemuOptDesc *desc = list->desc;
+    int i;
+
+    for (i = 0; desc[i].name != NULL; i++) {
+        if (strcmp(desc[i].name, name) == 0) {
+            return true;;

Extra ;

Quoted 8 lines

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 873

Quoted 4 lines
qemu-option.c +4 -0
+        }
+    }
+    return false;
+}

Duplicates the loop searching list->desc[] yet again. What about factoring out all the copies into a function? Separate cleanup patch preceding this one.

Okay.

Quoted 13 lines
Quoted 13 lines
qemu-option.c +12 -0
+
+/* merge two QemuOptsLists to one and return it. */
+QemuOptsList *append_opts_list(QemuOptsList *first,
+    QemuOptsList *second)
+{
+    size_t num_first_options, num_second_options;
+    QemuOptsList *dest = NULL;
+    int i = 0;
+    int index = 0;
+
+    num_first_options = count_opts_list(first);
+    num_second_options = count_opts_list(second);
Quoted 8 lines

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 891

Quoted 6 lines
qemu-option.c +6 -0
+    if (num_first_options + num_second_options == 0) {
+        return NULL;
+    }
+
+    dest = g_malloc0(sizeof(QemuOptsList)
+        + (num_first_options + num_second_options) * sizeof(QemuOptDesc));

Allocate space for both first and second.

Quoted 2 lines

I just make sure we have enough to hold struct QemuOptsList and QemuOptDesc members.

Let me correct myself: allocate space for one new QemuOptsList plus enough space for the QemuOptDesc in first and second.

Quoted 22 lines
Quoted 7 lines

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 897

Quoted 6 lines
qemu-option.c +6 -0
+
+    if (first) {
+        memcpy(dest, first, sizeof(QemuOptsList));
+    } else if (second) {
+        memcpy(dest, second, sizeof(QemuOptsList));
+    }
Quoted 6 lines

Copy either first or second.

If both are non-null, the space for second remains blank.

Why copy at all? The loops below copy again, don't they?

struct QemuOptsList has a member "QemuOptDesc desc[]", what I want to do is:

1) Copy QemuOptsList, excluding "QemuOptDesc desc[]", because it is "flexible array member",
does not take any space in QemuOptsList. Then dest will have the same name and implied_opt_name
as first QemuoptsList's(if first is null, will be the same as second's).

Quoted 4 lines

2) Copy desc member, I have to allocate space for the "flexible array member", g_strdup name and help, if it already exists in desc, then skip g_strdup.

Am I right?

Your code confused me. Let me try again.

dest->desc is set below. All the other members of dest get copied from first if it's non-null, else from second if it's non-null, else remain blank.

Issues with copying from first or second:

* Copying dest->head from is evil. It makes dest look like it was a member of tail queue dest->head.tqh_first, but that's not true.

I figure you need to QTAILQ_EMPTY(&dest->head).

* Copying dest->name isn't quite kosher, either, but probably harmless.

* Copying dest->implied_opt_name isn't optimal. What if first->implied_opt_name is null, but second->implied_opt_name is non-null? Then dest->implied_opt_name = second->implied_opt_name would be nicer.

I guess your particular use doesn't care, because all your implied_opt_name are null.

* dest->merge_lists is fishy, too. It governs how multiple qemu_opts_create(dest, ...) with the same ID behave. What should happen if first->merge_lists != second->merge_lists?

I guess all your merge_list are false.

Quoted 9 lines

* When both first and second are null, *dest remains blank. Why is that okay?

Perhaps a less problematic operation than "merge two QemuOptsLists" would be "set QemuOptsList C's desc to the merge of A's and B's desc." That way, the caller has to set up all the problematic members of the new QemuOptsList. Then use it to build a special function to create options for a pair of BlockDrivers.

Okay, you are right, I did not think about it so carefully. I will follow your advice, using new name and initializing other struct members in a new function.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
qemu-option.c +5 -0
+
+    while (first && (first->desc[i].name)) {
+        if (!opts_desc_find(dest, first->desc[i].name)) {
+            dest->desc[index].name = strdup(first->desc[i].name);
+            dest->desc[index].help = strdup(first->desc[i].help);
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

g_strdup()

Why do you need to copy name and help?

No answer?

I want dest->desc has the same members as first plus second, so I copy name and help, and desc->name will be used in opts_desc_find function(see below).

Quoted 31 lines
Quoted 24 lines
Quoted 5 lines

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 905

Quoted 3 lines
qemu-option.c +3 -0
+            dest->desc[index].type = first->desc[i].type;
+            dest->desc[index].def_value = first->desc[i].def_value;
+            dest->desc[++index].name = NULL;

I'd terminate dest->desc[] after the loop, not in every iteration.

Okay.

Quoted 16 lines
Quoted 15 lines
qemu-option.c +14 -0
+        }
+        i++;
+    }
+    i = 0;
+    while (second && (second->desc[i].name)) {
+        if (!opts_desc_find(dest, second->desc[i].name)) {
+            dest->desc[index].name = strdup(first->desc[i].name);
+            dest->desc[index].help = strdup(first->desc[i].help);
+            dest->desc[index].type = second->desc[i].type;
+            dest->desc[index].def_value = second->desc[i].def_value;
+            dest->desc[++i].name = NULL;
+        }
+        i++;
+    }
Quoted 6 lines
Quoted 3 lines

Please avoid duplicating the loop.

What if first and second both contain the same parameter name?

Then the second will be skipped, I used opts_desc_find to determine whether it already exists or not.

Quoted 6 lines

Ah, missed that, thanks.

First argument's options take precedence over second's, same as in the function this replaces (append_option_parameters()). Okay.

Document it in the function comment?

Okay, will add in v2.

Quoted 72 lines
Quoted 72 lines
Quoted 16 lines
Quoted 16 lines
qemu-option.c +15 -0
+    return dest;
+}
+
+void free_opts_list(QemuOptsList *list)
+{
+    int i = 0;
+
+    while (list && list->desc[i].name) {
+        g_free((char *)list->desc[i].name);
+        g_free((char *)list->desc[i].help);
+        i++;
+    }
+
+    g_free(list);
+}
Quoted 8 lines

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 942

Quoted 8 lines
qemu-option.c +8 -0
+
+void print_opts_list(QemuOptsList *list)
+{
+    int i = 0;
+    printf("Supported options:\n");
+    while (list && list->desc[i].name) {
+        printf("%-16s %s\n", list->desc[i].name,
+ list->desc[i].help ? list->desc[i].help : "No description

available");

Quoted 46 lines
Quoted 34 lines
qemu-option.c +34 -0
+        i++;
+    }
+}
+
+QemuOpts *parse_opts_list(const char *param,
+    QemuOptsList *list, QemuOpts *dest)
+{
+    char name[256];
+    char value[256];
+    char *param_delim, *value_delim;
+    char next_delim;
+
+    if (list == NULL) {
+        return NULL;
+    }
+    while (*param) {
+        param_delim = strchr(param, ',');
+        value_delim = strchr(param, '=');
+
+        if (value_delim && (value_delim < param_delim || !param_delim)) {
+            next_delim = '=';
+        } else {
+            next_delim = ',';
+            value_delim = NULL;
+        }
+
+        param = get_opt_name(name, sizeof(name), param, next_delim);
+        if (value_delim) {
+            param = get_opt_value(value, sizeof(value), param + 1);
+        }
+        if (*param != '\0') {
+            param++;
+        }
+

qemu-option.c int qemu_opts_foreach(QemuOptsList *list, · line 986

Quoted 10 lines
qemu-option.c +10 -0
+        if (qemu_opt_set(dest, name, value_delim ? value : NULL)) {
+            goto fail;
+        }
+    }
+
+    return dest;
+
+fail:
+    return NULL;
+}

Can you explain why the existing QemuOpts parsers won't do?

Quoted 5 lines
Quoted 2 lines

I think exsiting parsers is for QEMUOptionParameter, I have not found them for QemuOpts?

Check out opts_do_parse() and its callers.

Okay.

Quoted 68 lines
Quoted 68 lines
Quoted 19 lines
Quoted 19 lines
qemu-option.h +0 -11
index ca72986..75718fe 100644
@@ -31,24 +31,6 @@
  #include "error.h"
  #include "qdict.h"
 
-enum QEMUOptionParType {
-    OPT_FLAG,
-    OPT_NUMBER,
-    OPT_SIZE,
-    OPT_STRING,
-};
-
-typedef struct QEMUOptionParameter {
-    const char *name;
-    enum QEMUOptionParType type;
-    union {
Quoted 8 lines

qemu-option.h line 34

Quoted 8 lines
qemu-option.h +0 -7
-        uint64_t n;
-        char* s;
-    } value;
-    const char *help;
-} QEMUOptionParameter;
-
-
 const char *get_opt_name(char *buf, int buf_size, const char *p,

char delim);

Quoted 9 lines

qemu-option.h int get_param_value(char *buf, int buf_size, · line 63

Quoted 9 lines
qemu-option.h +0 -3
  const char *get_opt_value(char *buf, int buf_size, const char *p);
  int get_next_param_value(char *buf, int buf_size,
@@ -58,27 +40,6 @@ int get_param_value(char *buf, int buf_size,
  int check_params(char *buf, int buf_size,
                   const char * const *params, const char *str);
 
-
-/*
- * The following functions take a parameter list as input. This is

a pointer to

Quoted 1 line

qemu-option.h int get_param_value(char *buf, int buf_size, · line 64

Quoted 1 line
qemu-option.h +0 -1
- * the first element of a QEMUOptionParameter array which is

terminated by an

Quoted 27 lines
Quoted 21 lines
qemu-option.h +0 -17
- * entry with entry->name == NULL.
- */
-
-QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
-    const char *name);
-int set_option_parameter(QEMUOptionParameter *list, const char *name,
-    const char *value);
-int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
-    uint64_t value);
-QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
-    QEMUOptionParameter *list);
-QEMUOptionParameter *parse_option_parameters(const char *param,
-    QEMUOptionParameter *list, QEMUOptionParameter *dest);
-void free_option_parameters(QEMUOptionParameter *list);
-void print_option_parameters(QEMUOptionParameter *list);
-void print_option_help(QEMUOptionParameter *list);
-
  /* ------------------------------------------------------------------ */
 
  typedef struct QemuOpt QemuOpt;

qemu-option.h typedef struct QemuOptDesc { · line 60

Quoted 4 lines
qemu-option.h +1 -0
@@ -96,6 +57,7 @@ typedef struct QemuOptDesc {
      const char *name;
      enum QemuOptType type;
      const char *help;
+    uint64_t def_value;

The only user I can see is qemu_opts_print(), which can't be right.

Quoted 8 lines
Quoted 4 lines

I want to pass default value, such as QCOW2_DEFAULT_CLUSTER_SIZE, it will be used
while qemu-img create, or qcow2_create can not get correct default cluster size. Is it Okay?

Maybe, but I can't see where def_value gets used, by qemu-img create or anything else. Please point me to the code that uses it.

Yep, it can be removed, I used such code to pass default value:
cluster_size = qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, DEFAULT_CLUSTER_SIZE);

Quoted 19 lines
Quoted 19 lines
Quoted 4 lines

qemu-option.h typedef int (*qemu_opts_loopfunc)(QemuOpts

Quoted 4 lines
qemu-option.h +0 -0
  } QemuOptDesc;
 
  struct QemuOptsList {
@@ -152,5 +114,10 @@ typedef int (*qemu_opts_loopfunc)(QemuOpts

*opts, void *opaque);

Quoted 2 lines

qemu-option.h typedef int (*qemu_opts_loopfunc)(QemuOpts · line 115

Quoted 2 lines
qemu-option.h +0 -0
  int qemu_opts_print(QemuOpts *opts, void *dummy);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,

void *opaque,

Quoted 10 lines
Quoted 9 lines
qemu-option.h +6 -1
                        int abort_on_failure);
-
+QemuOptsList *append_opts_list(QemuOptsList *dest,
+    QemuOptsList *list);
+void free_opts_list(QemuOptsList *list);
+void print_opts_list(QemuOptsList *list);
+QemuOpts *parse_opts_list(const char *param,
+    QemuOptsList *list, QemuOpts *dest);
  #endif
Quoted 7 lines
Quoted 1 line

Thank you Markus for you so detail comments, :)

You're welcome.

Quoted 2 lines

Luiz, I think I need to use your 1-3 patchs in your series.
http://lists.nongnu.org/archive/html/qemu-devel/2012-07/msg02716.html

Thank you Markus.