Skip to contentSkip to conversation

libxl: ocaml: improve the bindings

PATCH In review Rob Hoes 6 participants 70 messages Oct 21, 2013 – Nov 15, 2013

lore
Share & tools
lore

Rob Hoes

author
#0 lore raw

The following series of patches fill in most of the gaps in the OCaml bindings to libxl, to make them useful for clients such as xapi/xenopsd (from XenServer). There are a number of bugfixes to the existing bindings as well. I have an experimental version of xenopsd that successfully uses the new bindings.

This is version 3 of this patch series to fix the OCaml binding to libxl. I believe I have addressed all the points raised by Ian Jackson's review of version 2. See the individual patches for detailed changes with respect to v2.

For convenience, the patches in this series may be pulled using:

git pull git://github.com/robhoes/xen.git hydrogen-upstream-v3-rebased

---
Resend note: rebased to current master, to take into account the changed interface to libxl_domain_create_restore.

author
#1 lore raw

PATCH 1/28 libxl: idl: allow KeyedUnion members to be empty

This is useful when the key enum has an "invalid" option and avoids the need to declare a dummy struct. Use this for domain_build_info resulting in the generated API changing like so:

    --- tools/libxl/_libxl_BACKUP_types.h
    +++ tools/libxl/_libxl_types.h
    @@ -377,8 +377,6 @@ typedef struct libxl_domain_build_info {
                 const char * features;
                 libxl_defbool e820_host;
             } pv;
    -        struct {
    -        } invalid;
         } u;
     } libxl_domain_build_info;
     void libxl_domain_build_info_dispose(libxl_domain_build_info *p);

+ a related change to the JSON generation.

---
 tools/libxl/gentest.py      |    3 ++-
 tools/libxl/gentypes.py     |   11 ++++++++---
 tools/libxl/libxl_types.idl |    2 +-
 3 files changed, 11 insertions(+), 5 deletions(-)
tools/libxl/gentest.py +2 -1
index 84b4fd7..6fab493 100644
@@ -46,7 +46,8 @@ def gen_rand_init(ty, v, indent = "    ", parent = None):
         for f in ty.fields:
             (nparent,fexpr) = ty.member(v, f, parent is None)
             s += "case %s:\n" % f.enumname
-            s += gen_rand_init(f.type, fexpr, indent + "    ", nparent)
+            if f.type is not None:
+                s += gen_rand_init(f.type, fexpr, indent + "    ", nparent)
             s += "    break;\n"
         s += "}\n"
     elif isinstance(ty, idl.Struct) \
tools/libxl/gentypes.py +8 -3
index 30f29ba..be06257 100644
@@ -45,6 +45,8 @@ def libxl_C_type_define(ty, indent = ""):
             s += "typedef %s %s {\n" % (ty.kind, ty.typename)
 
         for f in ty.fields:
+            if isinstance(ty, idl.KeyedUnion) and f.type is None: continue
+            
             x = libxl_C_instance_of(f.type, f.name)
             if f.const:
                 x = "const " + x
@@ -67,7 +69,8 @@ def libxl_C_type_dispose(ty, v, indent = "    ", parent = None):
         for f in ty.fields:
             (nparent,fexpr) = ty.member(v, f, parent is None)
             s += "case %s:\n" % f.enumname
-            s += libxl_C_type_dispose(f.type, fexpr, indent + "    ", nparent)
+            if f.type is not None:
+                s += libxl_C_type_dispose(f.type, fexpr, indent + "    ", nparent)
             s += "    break;\n"
         s += "}\n"
     elif isinstance(ty, idl.Array):
@@ -115,7 +118,8 @@ def _libxl_C_type_init(ty, v, indent = "    ", parent = None, subinit=False):
             for f in ty.fields:
                 (nparent,fexpr) = ty.member(v, f, parent is None)
                 s += "case %s:\n" % f.enumname
-                s += _libxl_C_type_init(f.type, fexpr, "    ", nparent)
+                if f.type is not None:
+                    s += _libxl_C_type_init(f.type, fexpr, "    ", nparent)
                 s += "    break;\n"
             s += "}\n"
         else:
@@ -214,7 +218,8 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
         for f in ty.fields:
             (nparent,fexpr) = ty.member(v, f, parent is None)
             s += "case %s:\n" % f.enumname
-            s += libxl_C_type_gen_json(f.type, fexpr, indent + "    ", nparent)
+            if f.type is not None:
+                s += libxl_C_type_gen_json(f.type, fexpr, indent + "    ", nparent)
             s += "    break;\n"
         s += "}\n"
     elif isinstance(ty, idl.Struct) and (parent is None or ty.json_fn is None):
tools/libxl/libxl_types.idl +1 -1
index d2cea8a..5c43d6f 100644
@@ -362,7 +362,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                       # Use host's E820 for PCI passthrough.
                                       ("e820_host", libxl_defbool),
                                       ])),
-                 ("invalid", Struct(None, [])),
+                 ("invalid", None),
                  ], keyvar_init_val = "LIBXL_DOMAIN_TYPE_INVALID")),
     ], dir=DIR_IN
 )
Signature · 1 line
1.7.10.4
author
#2 lore raw

PATCH 2/28 libxl: ocaml: support for Arrays in bindings generator.

In reply to Rob Hoes

No change in generated code because no arrays are currently generated.

---
 tools/ocaml/libs/xl/genwrap.py |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/ocaml/libs/xl/genwrap.py +9 -2
index 5757218..1b68b6b 100644
@@ -143,7 +143,14 @@ def c_val(ty, c, o, indent="", parent = None):
             raise NotImplementedError("No c_val fn for Builtin %s (%s)" % (ty.typename, type(ty)))
         s += "%s;" % (fn % { "o": o, "c": c })
     elif isinstance (ty,idl.Array):
-        raise("Cannot handle Array type\n")
+        s += "{\n"
+        s += "\tint i;\n"
+        s += "\t%s = Wosize_val(%s);\n" % (parent + ty.lenvar.name, o)
+        s += "\t%s = (%s) calloc(%s, sizeof(*%s));\n" % (c, ty.typename, parent + ty.lenvar.name, c)
+        s += "\tfor(i=0; i<%s; i++) {\n" % (parent + ty.lenvar.name)
+        s += c_val(ty.elem_type, c+"[i]", "Field(%s, i)" % o, indent="\t\t", parent=parent) + "\n"
+        s += "\t}\n"
+        s += "}\n"
     elif isinstance(ty,idl.Enumeration) and (parent is None):
         n = 0
         s += "switch(Int_val(%s)) {\n" % o
@@ -207,7 +214,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
         s += "\t    value array_elem;\n"
         s += "\t    %s = caml_alloc(%s,0);\n" % (o, parent + ty.lenvar.name)
         s += "\t    for(i=0; i<%s; i++) {\n" % (parent + ty.lenvar.name)
-        s += "\t        %s\n" % ocaml_Val(ty.elem_type, "array_elem", c + "[i]", "")
+        s += "\t        %s\n" % ocaml_Val(ty.elem_type, "array_elem", c + "[i]", "", parent=parent)
         s += "\t        Store_field(%s, i, array_elem);\n" % o
         s += "\t    }\n"
         s += "\t}"
Signature · 1 line
1.7.10.4
author
#3 lore raw

PATCH 3/28 libxl: ocaml: avoid reserved words in type and field names.

In reply to Rob Hoes

Do this by adding a "xl_" prefix to all names.

---
New in v3:
* Using common prefixes rather than changing names on a case by case basis.

---
 tools/ocaml/libs/xl/genwrap.py |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +4 -1
index 1b68b6b..a14fcfe 100644
@@ -70,8 +70,11 @@ def ocaml_type_of(ty):
     else:
         return ty.rawname
 
+def munge_name(name):
+    return "xl_" + name
+
 def ocaml_instance_of(type, name):
-    return "%s : %s" % (name, ocaml_type_of(type))
+    return "%s : %s" % (munge_name(name), ocaml_type_of(type))
 
 def gen_ocaml_ml(ty, interface, indent=""):
 
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 1 line · Rob Hoes

Do this by adding a "xl_" prefix to all names.

Does this not result in pretty fugly looking ocaml code with lots of spurious "xl_" everywhere?

Quoted 28 lines

---
New in v3:
* Using common prefixes rather than changing names on a case by case basis.

---
 tools/ocaml/libs/xl/genwrap.py |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +4 -1
index 1b68b6b..a14fcfe 100644
@@ -70,8 +70,11 @@ def ocaml_type_of(ty):
     else:
         return ty.rawname
 
+def munge_name(name):
+    return "xl_" + name
+
 def ocaml_instance_of(type, name):
-    return "%s : %s" % (name, ocaml_type_of(type))
+    return "%s : %s" % (munge_name(name), ocaml_type_of(type))
 
 def gen_ocaml_ml(ty, interface, indent=""):
 
author
#5 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 5 lines · Ian Campbell
Quoted 1 line · Rob Hoes

Do this by adding a "xl_" prefix to all names.

Does this not result in pretty fugly looking ocaml code with lots of spurious "xl_" everywhere?

Yes… I'm not too happy about that, but I think it is the only easy enough way of making this transformation "injective", as IanJ suggested. The alternative would be to change the munge function on a case-by-case basis, e.g. whenever someone adds a name which happens to be an OCaml keyword to the libxl IDL.

Quoted 30 lines
Quoted 28 lines

---
New in v3:
* Using common prefixes rather than changing names on a case by case basis.
---
tools/ocaml/libs/xl/genwrap.py | 5 ++++-

1 file changed, 4 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +3 -0
index 1b68b6b..a14fcfe 100644
@@ -70,8 +70,11 @@ def ocaml_type_of(ty):
    else:
        return ty.rawname
 
+def munge_name(name):
+    return "xl_" + name
+

def ocaml_instance_of(type, name):

tools/ocaml/libs/xl/genwrap.py +1 -1
-    return "%s : %s" % (name, ocaml_type_of(type))
+    return "%s : %s" % (munge_name(name), ocaml_type_of(type))

def gen_ocaml_ml(ty, interface, indent=""):

#6 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 4 lines · Rob Hoes
Quoted 1 line · Ian Campbell

On Mon, 2013-10-21 at 14:32 +0100, Rob Hoes wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Do this by adding a "xl_" prefix to all names.

Does this not result in pretty fugly looking ocaml code with lots of spurious "xl_" everywhere?

Yes… I'm not too happy about that, but I think it is the only easy enough way of making this transformation "injective", as IanJ suggested.

If the result of making the transformation injective is that the ocaml code looks like libxl.xl_domain_build.xl_max_memkb then I think the cure has been worse than the disease.

I would suggest that if libxl has a structure which has both type and ty fields then we have a bug in our API due to the use of confusingly similar field names which are not easily discriminated i.e. they should have been foo_type and bar_type (but perhaps due to API stability they would have to be type and bar_type in practice).

The approach used here could be made slightly more palatable by only applying it to names of the form "([prefix])*[keyword]", by applying a new prefix. i.e. type -> xl_type -> xl_xl_type.

Quoted 3 lines

The alternative would be to change the munge function on a
case-by-case basis, e.g. whenever someone adds a name which happens to be an OCaml keyword to the libxl IDL.

There aren't all that many ocaml keywords, I suppose? I expect many of them are unlikely field names. If we don't want to enumerate them up front (which I would accept, it's a bit tedious) I'd be perfectly happy to fault things in as the libxl IDL gains new inconveniently named fields, it'll break the build so we should notice pretty quickly!

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#7 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 10 lines · Rob Hoes
Quoted 9 lines
Quoted 5 lines · Ian Campbell
Quoted 1 line · Rob Hoes

Do this by adding a "xl_" prefix to all names.

Does this not result in pretty fugly looking ocaml code with lots of spurious "xl_" everywhere?

Quoted 7 lines
Quoted 3 lines

Yes… I'm not too happy about that, but I think it is the only easy enough way of making this transformation "injective", as IanJ suggested.

If the result of making the transformation injective is that the ocaml code looks like libxl.xl_domain_build.xl_max_memkb then I think the cure has been worse than the disease.

It's not quite that bad, because it is only needed for lower-case stuff and therefore excludes module names. Your example would be "Xenlight.Domain_build_info.xl_max_memkb". But yes, it does make things look a bit odd.

Quoted 12 lines

I would suggest that if libxl has a structure which has both type and ty fields then we have a bug in our API due to the use of confusingly similar field names which are not easily discriminated i.e. they should have been foo_type and bar_type (but perhaps due to API stability they would have to be type and bar_type in practice).

The approach used here could be made slightly more palatable by only applying it to names of the form "([prefix])*[keyword]", by applying a new prefix. i.e. type -> xl_type -> xl_xl_type.

Quoted 2 lines

The alternative would be to change the munge function on a
case-by-case basis, e.g. whenever someone adds a name which happens

Quoted 8 lines

to

Quoted 1 line

be an OCaml keyword to the libxl IDL.

There aren't all that many ocaml keywords, I suppose? I expect many of them are unlikely field names. If we don't want to enumerate them up front (which I would accept, it's a bit tedious) I'd be perfectly happy to fault things in as the libxl IDL gains new inconveniently named fields, it'll break the build so we should notice pretty quickly!

I'd be very happy with that, if it is acceptable to you and IanJ!

Cheers,
Rob

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Rob Hoes writes ("RE: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names."):

Quoted 8 lines
Quoted 6 lines

There aren't all that many ocaml keywords, I suppose? I expect many of them are unlikely field names. If we don't want to enumerate them up front (which I would accept, it's a bit tedious) I'd be perfectly happy to fault things in as the libxl IDL gains new inconveniently named fields, it'll break the build so we should notice pretty quickly!

I'd be very happy with that, if it is acceptable to you and IanJ!

That would be fine by me. I would marginally prefer to simply dump a list of the ocaml keywords into the ocaml idl generator.

(I definitely agree with Ian that the transformation should be such that most words are unchanged. I think s/^(xl_)*(KEYWORD)$/xl_$&/ should do it.)

Ian.

#9 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 8 lines · Ian Jackson

Rob Hoes writes ("RE: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names."):

Quoted 7 lines
Quoted 6 lines

There aren't all that many ocaml keywords, I suppose? I expect many of them are unlikely field names. If we don't want to enumerate them up front (which I would accept, it's a bit tedious) I'd be perfectly happy to fault things in as the libxl IDL gains new inconveniently named fields, it'll break the build so we should notice pretty quickly!

Quoted 4 lines
Quoted 1 line

I'd be very happy with that, if it is acceptable to you and IanJ!

That would be fine by me. I would marginally prefer to simply dump a list of the ocaml keywords into the ocaml idl generator.

http://caml.inria.fr/pub/docs/manual-ocaml-4.01/manual044.html seems to have a list, I suppose it is reasonably static across ocaml updates? It's not too insane to include it all now. Using most of them in our ABI would be a bug in our ABI IMHO and/or are already C reserved words (e.g. "else", "false" etc).

Ian.

#10 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Ian Campbell writes ("Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names."):

Quoted 8 lines · Ian Jackson
Quoted 2 lines

That would be fine by me. I would marginally prefer to simply dump a list of the ocaml keywords into the ocaml idl generator.

http://caml.inria.fr/pub/docs/manual-ocaml-4.01/manual044.html seems to have a list, I suppose it is reasonably static across ocaml updates? It's not too insane to include it all now. Using most of them in our ABI would be a bug in our ABI IMHO

Right.

Quoted 1 line

and/or are already C reserved words (e.g. "else", "false" etc).

Irrelevant now, but I would be tempted to say that if we would really want to pick a C reserved word for an IDL item we should do a similar workaround for C. Ie I don't think C should be special. But C's set of reserved words is small enough that it's not very likely.

And even more irrelevant: this happened to me in a previous life. We had an IDL-based code generator. One of the fields in one of our structs was called "export". A few years later, the C++ people decided to introduce a new keyword, "export". We were quite cross...

Ian.

#11 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 10 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names."):

Quoted 8 lines · Ian Jackson
Quoted 2 lines

That would be fine by me. I would marginally prefer to simply dump a list of the ocaml keywords into the ocaml idl generator.

http://caml.inria.fr/pub/docs/manual-ocaml-4.01/manual044.html seems to have a list, I suppose it is reasonably static across ocaml updates? It's not too insane to include it all now. Using most of them in our ABI would be a bug in our ABI IMHO

Quoted 7 lines

Right.

Quoted 1 line

and/or are already C reserved words (e.g. "else", "false" etc).

Irrelevant now, but I would be tempted to say that if we would really want to pick a C reserved word for an IDL item we should do a similar workaround for C. Ie I don't think C should be special.

Sounds reasonable.

Quoted 2 lines

But C's set
of reserved words is small enough that it's not very likely.

Yes.

Quoted 4 lines

And even more irrelevant: this happened to me in a previous life. We had an IDL-based code generator. One of the fields in one of our structs was called "export". A few years later, the C++ people decided to introduce a new keyword, "export". We were quite cross...

I can imagine ;-)

author
#12 lore raw

Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid reserved words in type and field names.

Quoted 15 lines · Ian Jackson
Quoted 1 line

Ian Campbell writes ("Re: [PATCH v3-RESEND 03/28] libxl: ocaml: avoid

reserved words in type and field names."):

Quoted 5 lines
Quoted 5 lines · Ian Jackson
Quoted 2 lines

That would be fine by me. I would marginally prefer to simply dump a list of the ocaml keywords into the ocaml idl generator.

http://caml.inria.fr/pub/docs/manual-ocaml-4.01/manual044.html

seems

Quoted 6 lines
Quoted 3 lines

to have a list, I suppose it is reasonably static across ocaml updates? It's not too insane to include it all now. Using most of them in our ABI would be a bug in our ABI IMHO

Right.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

and/or are already C reserved words (e.g. "else", "false" etc).

Irrelevant now, but I would be tempted to say that if we would really want to pick a C reserved word for an IDL item we should do a similar workaround for C. Ie I don't think C should be special.

Sounds reasonable.

Ok, so I'll change the patch to include the list of OCaml keywords, and have the munge function add an "xl_" prefix to those and only those names that are in the list.

Thanks,
Rob

Quoted 12 lines
Quoted 2 lines

But C's set
of reserved words is small enough that it's not very likely.

Yes.

Quoted 4 lines

And even more irrelevant: this happened to me in a previous life. We had an IDL-based code generator. One of the fields in one of our structs was called "export". A few years later, the C++ people decided to introduce a new keyword, "export". We were quite cross...

I can imagine ;-)

author
#13 lore raw

PATCH 4/28 libxl: ocaml: support for KeyedUnion in the bindings generator.

In reply to Rob Hoes

A KeyedUnion consists of two fields in the containing struct. First an enum field ("e") used as a descriminator and second a union ("u") containing potentially anonymous structs associated with each enum value.

We map the anonymous structs to structs named after the descriminator field ("e") and the specific enum values. We then declare an ocaml variant type name e__union mapping each enum value to its associated struct.

So given IDL:

foo = Enumeration("foo", [

    (0, "BAR"),
    (1, "BAZ"),
])
s = Struct("s", [
    ("u", KeyedUnion(none, foo, "blargle", [
        ("bar", Struct(...xxx...)),
        ("baz", Struct(...yyy...)),
    ])),

])

We generate C:

enum foo { BAR, BAZ };

struct s {
    enum foo blargle;
    union {
        struct { ...xxx... } bar;
        struct { ...yyy... } baz;
    } u;
}

and map this to ocaml

type foo = BAR | BAZ;

module S = struct

    type blargle_bar = ...xxx...;

    type blargle_baz = ...yyy...;

    type blargle__union = Bar of blargle_bar | Baz of blargle_baz;

    type t =
    {
        blargle : blargle__union;
    }

end

These type names are OK because they are already within the namespace associated with the struct "s".

If the struct associated with bar is empty then we don't bother with blargle_bar of "of blargle_bar".

No actually change in the generated code since we don't generate any KeyedUnions yet.

The actual implementation was inspired by
http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php#ref_constvrnt

---
 tools/libxl/idl.py             |    3 +
 tools/ocaml/libs/xl/genwrap.py |  162 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 147 insertions(+), 18 deletions(-)
tools/libxl/idl.py +3 -0
index 7d95e3f..f4908dd 100644
@@ -216,6 +216,9 @@ class Struct(Aggregate):
         kwargs.setdefault('passby', PASS_BY_REFERENCE)
         Aggregate.__init__(self, "struct", name, fields, **kwargs)
 
+    def has_fields(self):
+        return len(self.fields) != 0
+
 class Union(Aggregate):
     def __init__(self, name, fields, **kwargs):
         # Generally speaking some intelligence is required to free a
tools/ocaml/libs/xl/genwrap.py +144 -18

210 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#14 lore raw

PATCH 5/28 libxl: ocaml: add some more builtin types.

In reply to Rob Hoes

* bitmaps
* string_list
* key_value_list
* cpuid_policy_list (left "empty" for now)

None of these are used yet, so no change to the generated code.

Bitmap_val requires a ctx, so leave it as an abort for now.

---
 tools/ocaml/libs/xl/genwrap.py       |    6 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |  127 ++++++++++++++++++++++++++++++----
 2 files changed, 119 insertions(+), 14 deletions(-)
tools/ocaml/libs/xl/genwrap.py +5 -1
index ad14cad..d5688c8 100644
@@ -13,9 +13,13 @@ builtins = {
     "libxl_devid":          ("devid",                  "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
     "libxl_defbool":        ("bool option",            "%(c)s = Defbool_val(%(o)s)",        "Val_defbool(%(c)s)" ),
     "libxl_uuid":           ("int array",              "Uuid_val(gc, lg, &%(c)s, %(o)s)",   "Val_uuid(&%(c)s)"),
-    "libxl_key_value_list": ("(string * string) list", None,                                None),
+    "libxl_bitmap":         ("bool array",             "Bitmap_val(gc, lg, &%(c)s, %(o)s)",   "Val_bitmap(&%(c)s)"),    
+    "libxl_key_value_list": ("(string * string) list", "libxl_key_value_list_val(gc, lg, &%(c)s, %(o)s)", "Val_key_value_list(&%(c)s)"),
+    "libxl_string_list":    ("string list",            "libxl_string_list_val(gc, lg, &%(c)s, %(o)s)", "Val_string_list(&%(c)s)"),
     "libxl_mac":            ("int array",              "Mac_val(gc, lg, &%(c)s, %(o)s)",    "Val_mac(&%(c)s)"),
     "libxl_hwcap":          ("int32 array",            None,                                "Val_hwcap(&%(c)s)"),
+    # The following needs to be sorted out later
+    "libxl_cpuid_policy_list": ("unit",                "%(c)s = 0",                         "Val_unit"),
     }
 
 DEVICE_FUNCTIONS = [ ("add",            ["t", "domid", "unit"]),
tools/ocaml/libs/xl/xenlight_stubs.c +114 -13

161 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#15 lore raw

PATCH 6/28 libxc: ocaml: add simple binding for xentoollog (output only).

In reply to Rob Hoes

These bindings allow ocaml code to receive log message via xentoollog but do not support injecting messages into xentoollog from ocaml. Receiving log messages from libx{c,l} and forwarding them to ocaml is the use case which is needed by the following patches.

Add a simple noddy test case (tools/ocaml/test).

---
New in v3:
* Auto-generate the log-level converters from xentoollog.h.
* Move the stdio logger from the library to the test suite.
* Use a counter instead of a random number when registering a callback.

---
 .gitignore                                     |    1 +
 .hgignore                                      |    1 +
 tools/ocaml/Makefile                           |    2 +-
 tools/ocaml/Makefile.rules                     |    2 +-
 tools/ocaml/libs/Makefile                      |    1 +
 tools/ocaml/libs/xentoollog/META.in            |    4 +
 tools/ocaml/libs/xentoollog/Makefile           |   61 ++++++++
 tools/ocaml/libs/xentoollog/caml_xentoollog.h  |   24 +++
 tools/ocaml/libs/xentoollog/genlevels.py       |  127 +++++++++++++++
 tools/ocaml/libs/xentoollog/xentoollog.ml.in   |   48 ++++++
 tools/ocaml/libs/xentoollog/xentoollog.mli.in  |   43 ++++++
 tools/ocaml/libs/xentoollog/xentoollog_stubs.c |  196 ++++++++++++++++++++++++
 tools/ocaml/test/Makefile                      |   28 ++++
 tools/ocaml/test/xtl.ml                        |   40 +++++
 14 files changed, 576 insertions(+), 2 deletions(-)
 create mode 100644 tools/ocaml/libs/xentoollog/META.in
 create mode 100644 tools/ocaml/libs/xentoollog/Makefile
 create mode 100644 tools/ocaml/libs/xentoollog/caml_xentoollog.h
 create mode 100755 tools/ocaml/libs/xentoollog/genlevels.py
 create mode 100644 tools/ocaml/libs/xentoollog/xentoollog.ml.in
 create mode 100644 tools/ocaml/libs/xentoollog/xentoollog.mli.in
 create mode 100644 tools/ocaml/libs/xentoollog/xentoollog_stubs.c
 create mode 100644 tools/ocaml/test/Makefile
 create mode 100644 tools/ocaml/test/xtl.ml
.gitignore +1 -0
index 3253675..f51c345 100644
@@ -384,6 +384,7 @@ tools/ocaml/libs/xl/_libxl_types.mli.in
 tools/ocaml/libs/xl/xenlight.ml
 tools/ocaml/libs/xl/xenlight.mli
 tools/ocaml/xenstored/oxenstored
+tools/ocaml/test/xtl
 
 tools/debugger/kdd/kdd
 tools/firmware/etherboot/ipxe.tar.gz
.hgignore +1 -0
index 05cb0de..bb1b67d 100644
@@ -308,6 +308,7 @@
 ^tools/ocaml/libs/xl/xenlight\.ml$
 ^tools/ocaml/libs/xl/xenlight\.mli$
 ^tools/ocaml/xenstored/oxenstored$
+^tools/ocaml/test/xtl$
 ^tools/autom4te\.cache$
 ^tools/config\.h$
 ^tools/config\.log$
tools/ocaml/Makefile +1 -1
index 6b22bbe..8e4ca36 100644
@@ -1,7 +1,7 @@
 XEN_ROOT = $(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
-SUBDIRS_PROGRAMS = xenstored
+SUBDIRS_PROGRAMS = xenstored test
 
 SUBDIRS = libs $(SUBDIRS_PROGRAMS)
 
tools/ocaml/Makefile.rules +1 -1
index 5e6d81e..0745e83 100644
@@ -24,7 +24,7 @@ ALL_OCAML_OBJS ?= $(OBJS)
 %.cmi: %.mli
 	$(call quiet-command, $(OCAMLC) $(OCAMLCFLAGS) -c -o $@ $<,MLI,$@)
 
-%.cmx: %.ml
+%.cmx %.o: %.ml
 	$(call quiet-command, $(OCAMLOPT) $(OCAMLOPTFLAGS) -c -o $@ $<,MLOPT,$@)
 
 %.ml: %.mll
tools/ocaml/libs/Makefile +1 -0
index bca0fa2..3afdc89 100644
@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 SUBDIRS= \
 	mmap \
+	xentoollog \
 	xc eventchn \
 	xb xs xl
 
tools/ocaml/libs/xentoollog/META.in new file +4 -0
new file mode 100644
index 0000000..7b06683
@@ -0,0 +1,4 @@
+version = "@VERSION@"
+description = "Xen Tools Logger Interface"
+archive(byte) = "xentoollog.cma"
+archive(native) = "xentoollog.cmxa"
tools/ocaml/libs/xentoollog/Makefile new file +61 -0

61 lines not on this page · raw message

tools/ocaml/libs/xentoollog/caml_xentoollog.h new file +24 -0
new file mode 100644
index 0000000..0eb7618
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2013      Citrix Ltd.
+ * Author Ian Campbell <[email protected]>
+ * Author Rob Hoes <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+struct caml_xtl {
+	xentoollog_logger vtable;
+	char *vmessage_cb;
+	char *progress_cb;
+};
+
+#define Xtl_val(x)(*((struct caml_xtl **) Data_custom_val(x)))
+
tools/ocaml/libs/xentoollog/genlevels.py new file +127 -0

127 lines not on this page · raw message

tools/ocaml/libs/xentoollog/xentoollog.ml.in new file +48 -0

48 lines not on this page · raw message

tools/ocaml/libs/xentoollog/xentoollog.mli.in new file +43 -0

43 lines not on this page · raw message

tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +196 -0

196 lines not on this page · raw message

tools/ocaml/test/Makefile new file +28 -0
new file mode 100644
index 0000000..980054c
@@ -0,0 +1,28 @@
+XEN_ROOT = $(CURDIR)/../../..
+OCAML_TOPLEVEL = $(CURDIR)/..
+include $(OCAML_TOPLEVEL)/common.make
+
+OCAMLINCLUDE += \
+	-I $(OCAML_TOPLEVEL)/libs/xentoollog
+
+OBJS = xtl
+
+PROGRAMS = xtl
+
+xtl_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-cclib -lxenctrl
+
+xtl_OBJS = xtl
+
+OCAML_PROGRAM = xtl
+
+all: $(PROGRAMS)
+
+bins: $(PROGRAMS)
+
+install: all
+	$(INSTALL_DIR) $(DESTDIR)$(BINDIR)
+	$(INSTALL_PROG) $(PROGRAMS) $(DESTDIR)$(BINDIR)
+
+include $(OCAML_TOPLEVEL)/Makefile.rules
tools/ocaml/test/xtl.ml new file +40 -0
new file mode 100644
index 0000000..db30aae
@@ -0,0 +1,40 @@
+open Arg
+open Printf
+open Xentoollog
+
+let stdio_vmessage min_level level errno ctx msg =
+	let level_str = level_to_string level
+	and errno_str = match errno with None -> "" | Some s -> sprintf ": errno=%d" s
+	and ctx_str = match ctx with None -> "" | Some s -> sprintf ": %s" s in
+	if compare min_level level <= 0 then begin
+		printf "%s%s%s: %s\n" level_str ctx_str errno_str msg;
+		flush stdout;
+	end
+
+let stdio_progress ctx what percent dne total =
+	let nl = if dne = total then "\n" else "" in
+	printf "\rProgress %s %d%% (%Ld/%Ld)%s" what percent dne total nl;
+	flush stdout
+
+let create_stdio_logger ?(level=Info) () =
+	let cbs = {
+		vmessage = stdio_vmessage level;
+		progress = stdio_progress; } in
+	create "Xentoollog.stdio_logger" cbs
+
+let do_test level = 
+  let lgr = create_stdio_logger ~level:level () in
+  begin
+    test lgr;
+  end
+
+let () =
+  let debug_level = ref Info in
+  let speclist = [
+    ("-v", Arg.Unit (fun () -> debug_level := Debug), "Verbose");
+    ("-q", Arg.Unit (fun () -> debug_level := Critical), "Quiet");
+  ] in
+  let usage_msg = "usage: xtl [OPTIONS]" in
+  Arg.parse speclist (fun s -> ()) usage_msg;
+
+  do_test !debug_level
Signature · 1 line
1.7.10.4
#16 lore raw

Re: [PATCH v3-RESEND 06/28] libxc: ocaml: add simple binding for xentoollog (output only).

Quoted 7 lines · Rob Hoes

[...]

tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +5 -0
+static void stub_xtl_ocaml_vmessage(struct xentoollog_logger *logger,
+	xentoollog_level level,
+	int errnoval,
+	const char *context,
+	const char *format,

tools/ocaml/libs/xentoollog/xentoollog_stubs.c line 91

Quoted 10 lines
tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +10 -0
+	va_list al)
+{
+	CAMLparam0();
+	CAMLlocalN(args, 4);
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	value *func = caml_named_value(xtl->vmessage_cb) ;
+	char *msg;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();

Coverity is rightly pointing out that an array cannot be NULL. Would you mind fixing as part of the resend of the remainder of this series?

[...]

tools/ocaml/libs/xentoollog/xentoollog_stubs.c line 120

Quoted 12 lines
tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +12 -0
+static void stub_xtl_ocaml_progress(struct xentoollog_logger *logger,
+	const char *context,
+	const char *doing_what /* no \r,\n */,
+	int percent, unsigned long done, unsigned long total)
+{
+	CAMLparam0();
+	CAMLlocalN(args, 5);
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	value *func = caml_named_value(xtl->progress_cb) ;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();

Another one.

Ian.

author
#17 lore raw

Re: [PATCH v3-RESEND 06/28] libxc: ocaml: add simple binding for xentoollog (output only).

Quoted 13 lines · IanC
Quoted 12 lines · Rob Hoes

[...]

tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +10 -0
+static void stub_xtl_ocaml_vmessage(struct xentoollog_logger *logger,
+	xentoollog_level level,
+	int errnoval,
+	const char *context,
+	const char *format,
+	va_list al)
+{
+	CAMLparam0();
+	CAMLlocalN(args, 4);
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
Quoted 8 lines

tools/ocaml/libs/xentoollog/xentoollog_stubs.c line 91

Quoted 5 lines
tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +5 -0
+	value *func = caml_named_value(xtl->vmessage_cb) ;
+	char *msg;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();

Coverity is rightly pointing out that an array cannot be NULL. Would you mind fixing as part of the resend of the remainder of this series?

Sure, I'll fix that.

Andrew Cooper showed me another issue found by Coverity. We have a fix, which I'll submit as well.

Rob

Quoted 16 lines

[...]

tools/ocaml/libs/xentoollog/xentoollog_stubs.c line 120

Quoted 11 lines
tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +11 -0
+static void stub_xtl_ocaml_progress(struct xentoollog_logger *logger,
+	const char *context,
+	const char *doing_what /* no \r,\n */,
+	int percent, unsigned long done, unsigned long total) {
+	CAMLparam0();
+	CAMLlocalN(args, 5);
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	value *func = caml_named_value(xtl->progress_cb) ;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();

Another one.

Ian.

#18 lore raw

Re: [PATCH v3-RESEND 06/28] libxc: ocaml: add simple binding for xentoollog (output only).

Quoted 20 lines · Rob Hoes
Quoted 11 lines · IanC
Quoted 10 lines · Rob Hoes

[...]

tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +9 -0
+static void stub_xtl_ocaml_vmessage(struct xentoollog_logger *logger,
+	xentoollog_level level,
+	int errnoval,
+	const char *context,
+	const char *format,
+	va_list al)
+{
+	CAMLparam0();
+	CAMLlocalN(args, 4);
Quoted 8 lines

tools/ocaml/libs/xentoollog/xentoollog_stubs.c line 91

Quoted 6 lines
tools/ocaml/libs/xentoollog/xentoollog_stubs.c new file +6 -0
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	value *func = caml_named_value(xtl->vmessage_cb) ;
+	char *msg;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();

Coverity is rightly pointing out that an array cannot be NULL. Would you mind fixing as part of the resend of the remainder of this series?

Quoted 5 lines

Sure, I'll fix that.

Andrew Cooper showed me another issue found by Coverity. We have a fix, which I'll submit as well.

Rob

Following our bit of debugging in this area, I notice that the current series leaves some intermediate files around.

tools/ocaml/libs/xentoollog/_xtl_levels.inc
tools/ocaml/libs/xentoollog/_xtl_levels.ml.in
tools/ocaml/libs/xentoollog/_xtl_levels.mli.in
tools/ocaml/libs/xentoollog/xentoollog.ml
tools/ocaml/libs/xentoollog/xentoollog.mli

When fixing this stuff up, do you mind also modifying .gitignore/.hgignore for all the intermediate files creates?

~Andrew

author
#19 lore raw

PATCH 7/28 libxl: ocaml: allocate a long lived libxl context.

In reply to Rob Hoes

Rather than allocating a new context for every libxl call begin to switch to a model where a context is allocated by the caller and may then be used for multiple calls down into the library.

---
New in v3:
* Removed unneeded bits of the patch (which were anyway removed in a later patch).

---
 tools/ocaml/libs/xl/META.in          |    1 +
 tools/ocaml/libs/xl/Makefile         |    3 +++
 tools/ocaml/libs/xl/xenlight.ml.in   |    4 ++++
 tools/ocaml/libs/xl/xenlight.mli.in  |    4 ++++
 tools/ocaml/libs/xl/xenlight_stubs.c |   37 ++++++++++++++++++++++++++++++++++
 5 files changed, 49 insertions(+)
tools/ocaml/libs/xl/META.in +1 -0
index fe2c60b..3f0c552 100644
@@ -1,4 +1,5 @@
 version = "@VERSION@"
 description = "Xen Toolstack Library"
+requires = "xentoollog"
 archive(byte) = "xenlight.cma"
 archive(native) = "xenlight.cmxa"
tools/ocaml/libs/xl/Makefile +3 -0
index c9e5274..6917a20 100644
@@ -5,11 +5,14 @@ include $(TOPLEVEL)/common.make
 # ignore unused generated functions
 CFLAGS += -Wno-unused
 CFLAGS += $(CFLAGS_libxenlight)
+CFLAGS += -I ../xentoollog
 
 OBJS = xenlight
 INTF = xenlight.cmi
 LIBS = xenlight.cma xenlight.cmxa
 
+OCAMLINCLUDE += -I ../xentoollog
+
 LIBS_xenlight = $(LDLIBS_libxenlight)
 
 xenlight_OBJS = $(OBJS)
tools/ocaml/libs/xl/xenlight.ml.in +4 -0
index dcc1a38..3d663d8 100644
@@ -20,6 +20,10 @@ type devid = int
 
 (* @@LIBXL_TYPES@@ *)
 
+type ctx
+
+external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
+
 external send_trigger : domid -> trigger -> int -> unit = "stub_xl_send_trigger"
 external send_sysrq : domid -> char -> unit = "stub_xl_send_sysrq"
 external send_debug_keys : domid -> string -> unit = "stub_xl_send_debug_keys"
tools/ocaml/libs/xl/xenlight.mli.in +4 -0
index 3fd0165..96d859c 100644
@@ -20,6 +20,10 @@ type devid = int
 
 (* @@LIBXL_TYPES@@ *)
 
+type ctx
+
+external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
+
 external send_trigger : domid -> trigger -> int -> unit = "stub_xl_send_trigger"
 external send_sysrq : domid -> char -> unit = "stub_xl_send_sysrq"
 external send_debug_keys : domid -> string -> unit = "stub_xl_send_debug_keys"
tools/ocaml/libs/xl/xenlight_stubs.c +37 -0
index a7bf6ba..c26226f 100644
@@ -21,6 +21,7 @@
 #include <caml/signals.h>
 #include <caml/fail.h>
 #include <caml/callback.h>
+#include <caml/custom.h>
 
 #include <sys/mman.h>
 #include <stdint.h>
@@ -29,6 +30,11 @@
 #include <libxl.h>
 #include <libxl_utils.h>
 
+#include "caml_xentoollog.h"
+
+#define Ctx_val(x)(*((libxl_ctx **) Data_custom_val(x)))
+#define CTX ((libxl_ctx *) Ctx_val(ctx))
+
 struct caml_logger {
 	struct xentoollog_logger logger;
 	int log_offset;
@@ -97,6 +103,37 @@ static void failwith_xl(char *fname, struct caml_logger *lg)
 	caml_raise_with_string(*caml_named_value("xl.error"), s);
 }
 
+void ctx_finalize(value ctx)
+{
+	libxl_ctx_free(CTX);
+}
+
+static struct custom_operations libxl_ctx_custom_operations = {
+	"libxl_ctx_custom_operations",
+	ctx_finalize /* custom_finalize_default */,
+	custom_compare_default,
+	custom_hash_default,
+	custom_serialize_default,
+	custom_deserialize_default
+};
+
+CAMLprim value stub_libxl_ctx_alloc(value logger)
+{
+	CAMLparam1(logger);
+	CAMLlocal1(handle);
+	libxl_ctx *ctx;
+	int ret;
+
+	ret = libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, (xentoollog_logger *) Xtl_val(logger));
+	if (ret != 0) \
+		failwith_xl("cannot init context", NULL);
+
+	handle = caml_alloc_custom(&libxl_ctx_custom_operations, sizeof(ctx), 0, 1);
+	Ctx_val(handle) = ctx;
+
+	CAMLreturn(handle);
+}
+
 static void * gc_calloc(caml_gc *gc, size_t nmemb, size_t size)
 {
 	void *ptr;
Signature · 1 line
1.7.10.4
author
#20 lore raw

PATCH 8/28 libxl: ocaml: switch all functions over to take a context.

In reply to Rob Hoes

Since the context has a logger we can get rid of the logger built into these bindings and use the xentoollog bindings instead.

The gc is of limited use when most things are freed with libxl_FOO_dispose, so get rid of that too.

---
 tools/ocaml/libs/xl/genwrap.py       |   44 ++--
 tools/ocaml/libs/xl/xenlight.ml.in   |   11 +-
 tools/ocaml/libs/xl/xenlight.mli.in  |    9 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |  474 +++++++++-------------------------
 4 files changed, 153 insertions(+), 385 deletions(-)
tools/ocaml/libs/xl/genwrap.py +22 -22
index d5688c8..2a90681 100644
@@ -8,23 +8,23 @@ import idl
 builtins = {
     "bool":                 ("bool",                   "%(c)s = Bool_val(%(o)s)",           "Val_bool(%(c)s)" ),
     "int":                  ("int",                    "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
-    "char *":               ("string",                 "%(c)s = dup_String_val(gc, %(o)s)", "caml_copy_string(%(c)s)"),
+    "char *":               ("string",                 "%(c)s = dup_String_val(%(o)s)", "caml_copy_string(%(c)s)"),
     "libxl_domid":          ("domid",                  "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
     "libxl_devid":          ("devid",                  "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
     "libxl_defbool":        ("bool option",            "%(c)s = Defbool_val(%(o)s)",        "Val_defbool(%(c)s)" ),
-    "libxl_uuid":           ("int array",              "Uuid_val(gc, lg, &%(c)s, %(o)s)",   "Val_uuid(&%(c)s)"),
-    "libxl_bitmap":         ("bool array",             "Bitmap_val(gc, lg, &%(c)s, %(o)s)",   "Val_bitmap(&%(c)s)"),    
-    "libxl_key_value_list": ("(string * string) list", "libxl_key_value_list_val(gc, lg, &%(c)s, %(o)s)", "Val_key_value_list(&%(c)s)"),
-    "libxl_string_list":    ("string list",            "libxl_string_list_val(gc, lg, &%(c)s, %(o)s)", "Val_string_list(&%(c)s)"),
-    "libxl_mac":            ("int array",              "Mac_val(gc, lg, &%(c)s, %(o)s)",    "Val_mac(&%(c)s)"),
+    "libxl_uuid":           ("int array",              "Uuid_val(&%(c)s, %(o)s)",   "Val_uuid(&%(c)s)"),
+    "libxl_bitmap":         ("bool array",             "Bitmap_val(ctx, &%(c)s, %(o)s)",   "Val_bitmap(&%(c)s)"),    
+    "libxl_key_value_list": ("(string * string) list", "libxl_key_value_list_val(&%(c)s, %(o)s)", "Val_key_value_list(&%(c)s)"),
+    "libxl_string_list":    ("string list",            "libxl_string_list_val(&%(c)s, %(o)s)", "Val_string_list(&%(c)s)"),
+    "libxl_mac":            ("int array",              "Mac_val(&%(c)s, %(o)s)",    "Val_mac(&%(c)s)"),
     "libxl_hwcap":          ("int32 array",            None,                                "Val_hwcap(&%(c)s)"),
     # The following needs to be sorted out later
     "libxl_cpuid_policy_list": ("unit",                "%(c)s = 0",                         "Val_unit"),
     }
 
-DEVICE_FUNCTIONS = [ ("add",            ["t", "domid", "unit"]),
-                     ("remove",         ["t", "domid", "unit"]),
-                     ("destroy",        ["t", "domid", "unit"]),
+DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "unit"]),
+                     ("remove",         ["ctx", "t", "domid", "unit"]),
+                     ("destroy",        ["ctx", "t", "domid", "unit"]),
                    ]
 
 functions = { # ( name , [type1,type2,....] )
@@ -33,13 +33,13 @@ functions = { # ( name , [type1,type2,....] )
     "device_disk":    DEVICE_FUNCTIONS,
     "device_nic":     DEVICE_FUNCTIONS,
     "device_pci":     DEVICE_FUNCTIONS,
-    "physinfo":       [ ("get",            ["unit", "t"]),
+    "physinfo":       [ ("get",            ["ctx", "t"]),
                       ],
-    "cputopology":    [ ("get",            ["unit", "t array"]),
+    "cputopology":    [ ("get",            ["ctx", "t array"]),
                       ],
     "domain_sched_params":
-                      [ ("get",            ["domid", "t"]),
-                        ("set",            ["domid", "t", "unit"]),
+                      [ ("get",            ["ctx", "domid", "t"]),
+                        ("set",            ["ctx", "domid", "t", "unit"]),
                       ],
 }
 def stub_fn_name(ty, name):
@@ -226,7 +226,7 @@ def c_val(ty, c, o, indent="", parent = None):
         for e in ty.values:
             s += "    case %d: *%s = %s; break;\n" % (n, c, e.name)
             n += 1
-        s += "    default: failwith_xl(\"cannot convert value to %s\", lg); break;\n" % ty.typename
+        s += "    default: failwith_xl(\"cannot convert value to %s\"); break;\n" % ty.typename
         s += "}"
     elif isinstance(ty, idl.KeyedUnion):
         s += "{\n"
@@ -239,7 +239,7 @@ def c_val(ty, c, o, indent="", parent = None):
                                                     parent + ty.keyvar.name,
                                                     f.enumname)
                 n += 1
-        s += "\t\t    default: failwith_xl(\"variant handling bug %s%s (long)\", lg); break;\n" % (parent, ty.keyvar.name)        
+        s += "\t\t    default: failwith_xl(\"variant handling bug %s%s (long)\"); break;\n" % (parent, ty.keyvar.name)        
         s += "\t\t}\n"
         s += "\t} else {\n"
         s += "\t\t/* Is block... */\n"
@@ -255,7 +255,7 @@ def c_val(ty, c, o, indent="", parent = None):
                 s += "%s" % c_val(f.type, fexpr, "Field(%s, 0)" % o, indent=indent+"\t\t        ")
                 s += "break;\n"
                 n += 1
-        s += "\t\t    default: failwith_xl(\"variant handling bug %s%s (block)\", lg); break;\n" % (parent, ty.keyvar.name)
+        s += "\t\t    default: failwith_xl(\"variant handling bug %s%s (block)\"); break;\n" % (parent, ty.keyvar.name)
         s += "\t\t}\n"
         s += "\t}\n"
         s += "}"
@@ -268,14 +268,14 @@ def c_val(ty, c, o, indent="", parent = None):
             s += "%s\n" % c_val(f.type, fexpr, "Field(%s, %d)" % (o,n), parent=nparent)
             n = n + 1
     else:
-        s += "%s_val(gc, lg, %s, %s);" % (ty.rawname, ty.pass_arg(c, parent is None, passby=idl.PASS_BY_REFERENCE), o)
+        s += "%s_val(ctx, %s, %s);" % (ty.rawname, ty.pass_arg(c, parent is None, passby=idl.PASS_BY_REFERENCE), o)
     
     return s.replace("\n", "\n%s" % indent)
 
 def gen_c_val(ty, indent=""):
     s = "/* Convert caml value to %s */\n" % ty.rawname
     
-    s += "static int %s_val (caml_gc *gc, struct caml_logger *lg, %s, value v)\n" % (ty.rawname, ty.make_arg("c_val", passby=idl.PASS_BY_REFERENCE))
+    s += "static int %s_val (libxl_ctx *ctx, %s, value v)\n" % (ty.rawname, ty.make_arg("c_val", passby=idl.PASS_BY_REFERENCE))
     s += "{\n"
     s += "\tCAMLparam1(v);\n"
     s += "\n"
@@ -324,7 +324,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
         for e in ty.values:
             s += "    case %s: %s = Int_val(%d); break;\n" % (e.name, o, n)
             n += 1
-        s += "    default: failwith_xl(\"cannot convert value from %s\", lg); break;\n" % ty.typename
+        s += "    default: failwith_xl(\"cannot convert value from %s\"); break;\n" % ty.typename
         s += "}"
     elif isinstance(ty, idl.KeyedUnion):
         n = 0
@@ -353,7 +353,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
                 m += 1
                 #s += "\t        %s = caml_alloc(%d,%d);\n" % (o,len(f.type.fields),n)
             s += "\t        break;\n"
-        s += "\t    default: failwith_xl(\"cannot convert value from %s\", lg); break;\n" % ty.typename
+        s += "\t    default: failwith_xl(\"cannot convert value from %s\"); break;\n" % ty.typename
         s += "\t}"
     elif isinstance(ty,idl.Aggregate) and (parent is None or ty.rawname is None):
         s += "{\n"
@@ -378,14 +378,14 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
             n = n + 1
         s += "}"
     else:
-        s += "%s = Val_%s(gc, lg, %s);" % (o, ty.rawname, ty.pass_arg(c, parent is None))
+        s += "%s = Val_%s(%s);" % (o, ty.rawname, ty.pass_arg(c, parent is None))
     
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
 def gen_Val_ocaml(ty, indent=""):
     s = "/* Convert %s to a caml value */\n" % ty.rawname
 
-    s += "static value Val_%s (caml_gc *gc, struct caml_logger *lg, %s)\n" % (ty.rawname, ty.make_arg(ty.rawname+"_c"))
+    s += "static value Val_%s (%s)\n" % (ty.rawname, ty.make_arg(ty.rawname+"_c"))
     s += "{\n"
     s += "\tCAMLparam0();\n"
     s += "\tCAMLlocal1(%s_ocaml);\n" % ty.rawname
tools/ocaml/libs/xl/xenlight.ml.in +5 -6
index 3d663d8..dffba72 100644
@@ -15,17 +15,16 @@
 
 exception Error of string
 
+type ctx
 type domid = int
 type devid = int
 
 (* @@LIBXL_TYPES@@ *)
 
-type ctx
-
 external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 
-external send_trigger : domid -> trigger -> int -> unit = "stub_xl_send_trigger"
-external send_sysrq : domid -> char -> unit = "stub_xl_send_sysrq"
-external send_debug_keys : domid -> string -> unit = "stub_xl_send_debug_keys"
+external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
+external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
+external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 
-let _ = Callback.register_exception "xl.error" (Error "register_callback")
+let _ = Callback.register_exception "Xenlight.Error" (Error(""))
tools/ocaml/libs/xl/xenlight.mli.in +4 -5
index 96d859c..e2686bb 100644
@@ -15,15 +15,14 @@
 
 exception Error of string
 
+type ctx
 type domid = int
 type devid = int
 
 (* @@LIBXL_TYPES@@ *)
 
-type ctx
-
 external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 
-external send_trigger : domid -> trigger -> int -> unit = "stub_xl_send_trigger"
-external send_sysrq : domid -> char -> unit = "stub_xl_send_sysrq"
-external send_debug_keys : domid -> string -> unit = "stub_xl_send_debug_keys"
+external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
+external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
+external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
tools/ocaml/libs/xl/xenlight_stubs.c +122 -352

630 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#21 lore raw

PATCH 9/28 libxl: make the libxl error type an IDL enum

In reply to Rob Hoes

This makes it easier to use in language bindings.

---
New in v3:
* New patch to simplify the (following) exception handling patch.

---
 tools/libxl/libxl.h         |   18 ------------------
 tools/libxl/libxl_device.c  |    6 +++---
 tools/libxl/libxl_types.idl |   17 +++++++++++++++++
 3 files changed, 20 insertions(+), 21 deletions(-)
tools/libxl/libxl.h +0 -18
index 1c6675d..9379694 100644
@@ -459,24 +459,6 @@ typedef struct libxl__ctx libxl_ctx;
 
 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx);
 
-enum {
-    ERROR_NONSPECIFIC = -1,
-    ERROR_VERSION = -2,
-    ERROR_FAIL = -3,
-    ERROR_NI = -4,
-    ERROR_NOMEM = -5,
-    ERROR_INVAL = -6,
-    ERROR_BADFAIL = -7,
-    ERROR_GUEST_TIMEDOUT = -8,
-    ERROR_TIMEDOUT = -9,
-    ERROR_NOPARAVIRT = -10,
-    ERROR_NOT_READY = -11,
-    ERROR_OSEVENT_REG_FAIL = -12,
-    ERROR_BUFFERFULL = -13,
-    ERROR_UNKNOWN_CHILD = -14,
-};
-
-
 /*
  * Some libxl operations can take a long time.  These functions take a
  * parameter to control their concurrency:
tools/libxl/libxl_device.c +3 -3
index 16a92a4..11c53cf 100644
@@ -497,7 +497,7 @@ static void multidev_one_callback(libxl__egc *egc, libxl__ao_device *aodev)
 {
     STATE_AO_GC(aodev->ao);
     libxl__multidev *multidev = aodev->multidev;
-    int i, error = 0;
+    int i, err = 0;
 
     aodev->active = 0;
 
@@ -506,10 +506,10 @@ static void multidev_one_callback(libxl__egc *egc, libxl__ao_device *aodev)
             return;
 
         if (multidev->array[i]->rc)
-            error = multidev->array[i]->rc;
+            err = multidev->array[i]->rc;
     }
 
-    multidev->callback(egc, multidev, error);
+    multidev->callback(egc, multidev, err);
     return;
 }
 
tools/libxl/libxl_types.idl +17 -0
index 5c43d6f..778a416 100644
@@ -28,6 +28,23 @@ MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT")
 # Constants / Enumerations
 #
 
+libxl_error = Enumeration("error", [
+    (-1, "NONSPECIFIC"),
+    (-2, "VERSION"),
+    (-3, "FAIL"),
+    (-4, "NI"),
+    (-5, "NOMEM"),
+    (-6, "INVAL"),
+    (-7, "BADFAIL"),
+    (-8, "GUEST_TIMEDOUT"),
+    (-9, "TIMEDOUT"),
+    (-10, "NOPARAVIRT"),
+    (-11, "NOT_READY"),
+    (-12, "OSEVENT_REG_FAIL"),
+    (-13, "BUFFERFULL"),
+    (-14, "UNKNOWN_CHILD"),
+    ], namespace = "")
+
 libxl_domain_type = Enumeration("domain_type", [
     (-1, "INVALID"),
     (1, "HVM"),
Signature · 1 line
1.7.10.4
#22 lore raw

Re: [PATCH v3-RESEND 09/28] libxl: make the libxl error type an IDL enum

tools/libxl/libxl_device.c static void multidev_one_callback(libxl__egc *egc, libxl__ao_device *aodev) · line 500

Quoted 10 lines · Rob Hoes
tools/libxl/libxl_device.c +1 -1
index 16a92a4..11c53cf 100644
@@ -497,7 +497,7 @@ static void multidev_one_callback(libxl__egc *egc, libxl__ao_device *aodev)
 {
     STATE_AO_GC(aodev->ao);
     libxl__multidev *multidev = aodev->multidev;
-    int i, error = 0;
+    int i, err = 0;

Why the spurious s/error/err/ ?

Quoted 9 lines
tools/libxl/libxl_types.idl +1 -0
index 5c43d6f..778a416 100644
@@ -28,6 +28,23 @@ MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT")
 # Constants / Enumerations
 #
 
+libxl_error = Enumeration("error", [

[...]

Quoted 1 line

+ ], namespace = "")

Ah, because you've defined "enum error" as an unnamespaced type. Irrespective of the clash you've found I think this is a no go since an application might reasonably be using "error" (also, err is a standard function too, see err.h and/or err(3)).

Assuming the idl doesn't support anonymous enums (I don't recall writing anything to do that ;-)) and you quite reasonably don't want to add such support I think enum libxl_error as the name is fine.

I wonder if we should begin transitioning over to using this as the return type. Probably mostly involves horrible pain...

Quoted 4 lines

+
libxl_domain_type = Enumeration("domain_type", [

     (-1, "INVALID"),
     (1, "HVM"),
author
#23 lore raw

Re: [PATCH v3-RESEND 09/28] libxl: make the libxl error type an IDL enum

Quoted 22 lines · Rob Hoes

tools/libxl/libxl_device.c static void multidev_one_callback(libxl__egc *egc, · line 500

Quoted 10 lines
tools/libxl/libxl_device.c +1 -1
index 16a92a4..11c53cf 100644
@@ -497,7 +497,7 @@ static void multidev_one_callback(libxl__egc *egc,
 libxl__ao_device *aodev)  {
     STATE_AO_GC(aodev->ao);
     libxl__multidev *multidev = aodev->multidev;
-    int i, error = 0;
+    int i, err = 0;

Why the spurious s/error/err/ ?

tools/libxl/libxl_types.idl MemKB = UInt(64, init_val =

Quoted 5 lines
tools/libxl/libxl_types.idl +0 -0
index 5c43d6f..778a416 100644
@@ -28,6 +28,23 @@ MemKB = UInt(64, init_val =

"LIBXL_MEMKB_DEFAULT")

Quoted 2 lines
tools/libxl/libxl_types.idl +0 -0
 # Constants / Enumerations  #
 
Quoted 8 lines
Quoted 1 line
tools/libxl/libxl_types.idl +1 -0
+libxl_error = Enumeration("error", [

[...]

Quoted 1 line

+ ], namespace = "")

Ah, because you've defined "enum error" as an unnamespaced type. Irrespective of the clash you've found I think this is a no go since an application might reasonably be using "error" (also, err is a standard function too, see err.h and/or err(3)).

Yes, I wanted to make sure that the enum value names did not change. With the default namespace, you'd get values such as "LIBXL_ERROR_FAIL" rather than "ERROR_FAIL".

Quoted 3 lines

Assuming the idl doesn't support anonymous enums (I don't recall writing anything to do that ;-)) and you quite reasonably don't want to add such support I think enum libxl_error as the name is fine.

I think the IDL constructs enums as follows, for each value: namespace + "_" + enum name + "_" + value name. So naming the enum "libxl_error" again gives values such as "LIBXL_ERROR_FAIL", and we're back to square one :)

Perhaps we need another option in the IDL for this?

Quoted 8 lines

I wonder if we should begin transitioning over to using this as the return type. Probably mostly involves horrible pain...

Quoted 4 lines

+
libxl_domain_type = Enumeration("domain_type", [

     (-1, "INVALID"),
     (1, "HVM"),
#24 lore raw

Re: [PATCH v3-RESEND 09/28] libxl: make the libxl error type an IDL enum

Quoted 26 lines · Rob Hoes
Quoted 26 lines · Rob Hoes

tools/libxl/libxl_device.c static void multidev_one_callback(libxl__egc *egc, · line 500

Quoted 10 lines
tools/libxl/libxl_device.c +1 -1
index 16a92a4..11c53cf 100644
@@ -497,7 +497,7 @@ static void multidev_one_callback(libxl__egc *egc,
 libxl__ao_device *aodev)  {
     STATE_AO_GC(aodev->ao);
     libxl__multidev *multidev = aodev->multidev;
-    int i, error = 0;
+    int i, err = 0;

Why the spurious s/error/err/ ?

tools/libxl/libxl_types.idl MemKB = UInt(64, init_val =

Quoted 5 lines
tools/libxl/libxl_types.idl +0 -0
index 5c43d6f..778a416 100644
@@ -28,6 +28,23 @@ MemKB = UInt(64, init_val =

"LIBXL_MEMKB_DEFAULT")

Quoted 3 lines
tools/libxl/libxl_types.idl +1 -0
 # Constants / Enumerations  #
 
+libxl_error = Enumeration("error", [

[...]

Quoted 1 line

+ ], namespace = "")

Quoted 8 lines
Quoted 4 lines

Ah, because you've defined "enum error" as an unnamespaced type. Irrespective of the clash you've found I think this is a no go since an application might reasonably be using "error" (also, err is a standard function too, see err.h and/or err(3)).

Yes, I wanted to make sure that the enum value names did not change. With the default namespace, you'd get values such as "LIBXL_ERROR_FAIL" rather than "ERROR_FAIL".

OH, right, that makes sense.

Quoted 11 lines
Quoted 4 lines

Assuming the idl doesn't support anonymous enums (I don't recall writing anything to do that ;-)) and you quite reasonably don't want to add such support I think enum libxl_error as the name is fine.

I think the IDL constructs enums as follows, for each value: namespace
+ "_" + enum name + "_" + value name. So naming the enum "libxl_error" again gives values such as "LIBXL_ERROR_FAIL", and we're back to square one :)

Perhaps we need another option in the IDL for this?

Yes, I think so. Something like a value_namespace option to the containing enum would do the trick I think? Something like this (untested, needs a corresponding idl.txt update) would allow you to pass value_namespace = ""

tools/libxl/idl.py +3 -1
index 7d95e3f..0425384 100644
@@ -136,7 +136,7 @@ class EnumerationValue(object):
 
         self.valuename = str.upper(name)
         self.rawname = str.upper(enum.rawname) + "_" + self.valuename
-        self.name = str.upper(enum.namespace) + self.rawname
+        self.name = str.upper(enum.value_namespace) + self.rawname
         self.value = value
 
 class Enumeration(Type):
@@ -144,6 +144,8 @@ class Enumeration(Type):
         kwargs.setdefault('dispose_fn', None)
         Type.__init__(self, typename, **kwargs)
 
+        self.value_namespace = kwargs.setdefault('value_namespace', self.namespace)
+
         self.values = []
         for v in values:
             # (value, name)

Ian.

author
#25 lore raw

PATCH 10/28 libxl: ocaml: generate string_of_* functions for enums

In reply to Rob Hoes

---
New in v3:
* New patch, which allowed us to remove the hand-written string_of_error function.

---
 tools/ocaml/libs/xl/genwrap.py |    9 +++++++++
 1 file changed, 9 insertions(+)
tools/ocaml/libs/xl/genwrap.py +9 -0
index 2a90681..d8d675c 100644
@@ -152,6 +152,15 @@ def gen_ocaml_ml(ty, interface, indent=""):
         s += "type %s = \n" % ty.rawname
         for v in ty.values:
             s += "\t | %s\n" % v.rawname
+
+        if interface:
+            s += "\nval string_of_%s : %s -> string\n" % (ty.rawname, ty.rawname)
+        else:
+            s += "\nlet string_of_%s = function\n" % ty.rawname
+            n = len(ty.rawname) + 1
+            for v in ty.values:
+                s += '\t| %s -> "%s"\n' % (v.rawname, v.rawname[n:])
+
     elif isinstance(ty, idl.Aggregate):
         s += ""
         
Signature · 1 line
1.7.10.4
#26 lore raw

Re: [PATCH v3-RESEND 10/28] libxl: ocaml: generate string_of_* functions for enums

tools/ocaml/libs/xl/genwrap.py def gen_ocaml_ml(ty, interface, indent=""): · line 162

Quoted 8 lines · Rob Hoes
tools/ocaml/libs/xl/genwrap.py +7 -0
+        if interface:
+            s += "\nval string_of_%s : %s -> string\n" % (ty.rawname, ty.rawname)
+        else:
+            s += "\nlet string_of_%s = function\n" % ty.rawname
+            n = len(ty.rawname) + 1
+            for v in ty.values:
+                s += '\t| %s -> "%s"\n' % (v.rawname, v.rawname[n:])

Is n here trying to strip the libxl_ prefix? Oh no, its the enum name prefix.

v.valuename is unhelpfully upper-cased (this might be an error in id.py TBH). But I think v.valuename.lower() (or whatever case munging ocaml requires) would be fine here.

Or you could trivially add v.basename or something similar to tools/libxl/idl.{txt,py} I think.

Ian.

author
#27 lore raw

Re: [PATCH v3-RESEND 10/28] libxl: ocaml: generate string_of_* functions for enums

Quoted 11 lines · Rob Hoes

Review

Quoted 3 lines
(quoted diff) +2 -0
+        if interface:
+            s += "\nval string_of_%s : %s -> string\n" % (ty.rawname,

ty.rawname)

tools/ocaml/libs/xl/genwrap.py def gen_ocaml_ml(ty, interface, indent=""): · line 162

Quoted 5 lines
tools/ocaml/libs/xl/genwrap.py +5 -0
+        else:
+            s += "\nlet string_of_%s = function\n" % ty.rawname
+            n = len(ty.rawname) + 1
+            for v in ty.values:
+                s += '\t| %s -> "%s"\n' % (v.rawname, v.rawname[n:])
Quoted 5 lines

Is n here trying to strip the libxl_ prefix? Oh no, its the enum name prefix.

v.valuename is unhelpfully upper-cased (this might be an error in id.py TBH). But I think v.valuename.lower() (or whatever case munging ocaml requires) would be fine here.

Ah, just v.valuename is fine. I missed that one (I should read idl.txt again ;)).

Cheers,
Rob

Quoted 4 lines

Or you could trivially add v.basename or something similar to tools/libxl/idl.{txt,py} I think.

Ian.

author
#28 lore raw

PATCH 11/28 libxl: ocaml: propagate the libxl return error code in exceptions

In reply to Rob Hoes

---
New in v3:
* Now using the auto-generated error type.

---
 tools/ocaml/libs/xl/genwrap.py       |   10 +++----
 tools/ocaml/libs/xl/xenlight.ml.in   |   10 +++++--
 tools/ocaml/libs/xl/xenlight.mli.in  |    9 ++++--
 tools/ocaml/libs/xl/xenlight_stubs.c |   50 ++++++++++++++++++++++++----------
 4 files changed, 55 insertions(+), 24 deletions(-)
tools/ocaml/libs/xl/genwrap.py +5 -5
index d8d675c..611d892 100644
@@ -235,7 +235,7 @@ def c_val(ty, c, o, indent="", parent = None):
         for e in ty.values:
             s += "    case %d: *%s = %s; break;\n" % (n, c, e.name)
             n += 1
-        s += "    default: failwith_xl(\"cannot convert value to %s\"); break;\n" % ty.typename
+        s += "    default: failwith_xl(ERROR_FAIL, \"cannot convert value to %s\"); break;\n" % ty.typename
         s += "}"
     elif isinstance(ty, idl.KeyedUnion):
         s += "{\n"
@@ -248,7 +248,7 @@ def c_val(ty, c, o, indent="", parent = None):
                                                     parent + ty.keyvar.name,
                                                     f.enumname)
                 n += 1
-        s += "\t\t    default: failwith_xl(\"variant handling bug %s%s (long)\"); break;\n" % (parent, ty.keyvar.name)        
+        s += "\t\t    default: failwith_xl(ERROR_FAIL, \"variant handling bug %s%s (long)\"); break;\n" % (parent, ty.keyvar.name)        
         s += "\t\t}\n"
         s += "\t} else {\n"
         s += "\t\t/* Is block... */\n"
@@ -264,7 +264,7 @@ def c_val(ty, c, o, indent="", parent = None):
                 s += "%s" % c_val(f.type, fexpr, "Field(%s, 0)" % o, indent=indent+"\t\t        ")
                 s += "break;\n"
                 n += 1
-        s += "\t\t    default: failwith_xl(\"variant handling bug %s%s (block)\"); break;\n" % (parent, ty.keyvar.name)
+        s += "\t\t    default: failwith_xl(ERROR_FAIL, \"variant handling bug %s%s (block)\"); break;\n" % (parent, ty.keyvar.name)
         s += "\t\t}\n"
         s += "\t}\n"
         s += "}"
@@ -333,7 +333,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
         for e in ty.values:
             s += "    case %s: %s = Int_val(%d); break;\n" % (e.name, o, n)
             n += 1
-        s += "    default: failwith_xl(\"cannot convert value from %s\"); break;\n" % ty.typename
+        s += "    default: failwith_xl(ERROR_FAIL, \"cannot convert value from %s\"); break;\n" % ty.typename
         s += "}"
     elif isinstance(ty, idl.KeyedUnion):
         n = 0
@@ -362,7 +362,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
                 m += 1
                 #s += "\t        %s = caml_alloc(%d,%d);\n" % (o,len(f.type.fields),n)
             s += "\t        break;\n"
-        s += "\t    default: failwith_xl(\"cannot convert value from %s\"); break;\n" % ty.typename
+        s += "\t    default: failwith_xl(ERROR_FAIL, \"cannot convert value from %s\"); break;\n" % ty.typename
         s += "\t}"
     elif isinstance(ty,idl.Aggregate) and (parent is None or ty.rawname is None):
         s += "{\n"
tools/ocaml/libs/xl/xenlight.ml.in +7 -3
index dffba72..a281425 100644
@@ -13,18 +13,22 @@
  * GNU Lesser General Public License for more details.
  *)
 
-exception Error of string
-
 type ctx
 type domid = int
 type devid = int
 
 (* @@LIBXL_TYPES@@ *)
 
+exception Error of (error * string)
+
 external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 
+external test_raise_exception: unit -> unit = "stub_raise_exception"
+
 external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
 external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
 external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 
-let _ = Callback.register_exception "Xenlight.Error" (Error(""))
+let register_exceptions () =
+	Callback.register_exception "Xenlight.Error" (Error(ERROR_FAIL, ""))
+
tools/ocaml/libs/xl/xenlight.mli.in +7 -2
index e2686bb..d663196 100644
@@ -13,16 +13,21 @@
  * GNU Lesser General Public License for more details.
  *)
 
-exception Error of string
-
 type ctx
 type domid = int
 type devid = int
 
 (* @@LIBXL_TYPES@@ *)
 
+exception Error of (error * string)
+
+val register_exceptions: unit -> unit
+
 external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 
+external test_raise_exception: unit -> unit = "stub_raise_exception"
+
 external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
 external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
 external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
+
tools/ocaml/libs/xl/xenlight_stubs.c +36 -14

118 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#29 lore raw

PATCH 12/28 libxl: ocaml: make Val_defbool GC-proof

In reply to Rob Hoes

The Val_* functions potentially allocate new values on the OCaml heap, and may trigger an iteration of the OCaml GC. Therefore, it is important to assign values produced by Val_* immediately to variables declared with CAMLlocal macros, which register the values with the GC.

This leads to slightly weird looking C code, but avoids hard to find segfaults.

---
 tools/ocaml/libs/xl/xenlight_stubs.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tools/ocaml/libs/xl/xenlight_stubs.c +7 -5
index a6f6e6f..d15a625 100644
@@ -308,15 +308,17 @@ static int Uuid_val(libxl_uuid *c_val, value v)
 static value Val_defbool(libxl_defbool c_val)
 {
 	CAMLparam0();
-	CAMLlocal1(v);
+	CAMLlocal2(v1, v2);
+	bool b;
 
 	if (libxl_defbool_is_default(c_val))
-		v = Val_none;
+		v2 = Val_none;
 	else {
-		bool b = libxl_defbool_val(c_val);
-		v = Val_some(b ? Val_bool(true) : Val_bool(false));
+		b = libxl_defbool_val(c_val);
+		v1 = b ? Val_bool(true) : Val_bool(false);
+		v2 = Val_some(v1);
 	}
-	CAMLreturn(v);
+	CAMLreturn(v2);
 }
 
 static libxl_defbool Defbool_val(value v)
Signature · 1 line
1.7.10.4
Acked-by on v1 12/28
#30 lore raw

Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof

Quoted 4 lines · Rob Hoes

The Val_* functions potentially allocate new values on the OCaml heap, and may trigger an iteration of the OCaml GC. Therefore, it is important to assign values produced by Val_* immediately to variables declared with CAMLlocal macros, which register the values with the GC.

Bonkers stuff ;-)

Quoted 3 lines

This leads to slightly weird looking C code, but avoids hard to find segfaults.

#31 lore raw

Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof

Ian Campbell writes ("Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof"):

Quoted 7 lines · Rob Hoes
Quoted 4 lines

The Val_* functions potentially allocate new values on the OCaml heap, and may trigger an iteration of the OCaml GC. Therefore, it is important to assign values produced by Val_* immediately to variables declared with CAMLlocal macros, which register the values with the GC.

Bonkers stuff ;-)

Even given this explanation, I still don't understand why the fix is correct and complete.

What difference do you think it makes whether things are "assigned to variables" or not ?

Bonus if your explanation refers to sequence points.

Ian.

#32 lore raw

Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof

Ian Jackson writes ("Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof"):

Quoted 4 lines

What difference do you think it makes whether things are "assigned to variables" or not ?

Bonus if your explanation refers to sequence points.

Perhaps these special CAMLlocal macros make the things volatile or something, I guess.

Perhaps you could refer me to the Ocaml docs ?

Ian.

#33 lore raw

Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof

Quoted 8 lines · Ian Jackson

Ian Jackson writes ("Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof"):

Quoted 4 lines

What difference do you think it makes whether things are "assigned to variables" or not ?

Bonus if your explanation refers to sequence points.

Perhaps these special CAMLlocal macros make the things volatile or something, I guess.

They seem to end up being structs.

Quoted 1 line

Perhaps you could refer me to the Ocaml docs ?

Hah!

I usually scrobble around under /usr/include/caml/*.h

I also referenced
http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php in the past (and there's a link in the series somewhere) but sadly it appears to be 404 now -- Rob can you find a replacement?

(CCing Dave, who seems to understand some off this stuff)

#34 lore raw

Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof

Hi,

Quoted 7 lines · Ian Campbell
Quoted 6 lines · Ian Jackson

Ian Jackson writes ("Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof"):

Quoted 3 lines

What difference do you think it makes whether things are "assigned to variables" or not ?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Bonus if your explanation refers to sequence points.

Perhaps these special CAMLlocal macros make the things volatile or something, I guess.

They seem to end up being structs.

IIRC the CAML{param,local} macros put the values into structs which can be reached from a GC root (the previous interface used macros called something like "Begin_roots" "End_roots"). Any value you've allocated and you want to keep had better be reachable when the GC comes calling -- so I tend to obsessively use the macros everywhere. You can optimise them away but if you call a function which ends up allocating, one time in a hundred the minor heap will be full, it'll do a sweep and deallocate anything unreachable. Having been bitten too many times, I'd rather sacrifice a little performance for safety there :)

It is quite hard to write good ocaml c stubs, because you're caught between the two worlds. To appease the GC you have to write some fairly unidiomatic C. FYI there's an exciting new way of building C stubs through a library called "ctypes" being written by Jeremy Yallop from OCamlLabs-- we should check it out for version 2 of these bindings...

Cheers,
Dave

#35 lore raw

Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof

In reply to Ian Jackson

Ian Jackson writes ("Re: [PATCH v3-RESEND 12/28] libxl: ocaml: make Val_defbool GC-proof"):

Quoted 1 line

Bonus if your explanation refers to sequence points.

Andrew Cooper has helped explain, and Ian Campbell pointed me at caml/memory.h.

AIUI now: Val_bool and Val_some allocate. So they may enter the GC and therefore at that point you're not allowed to have any ocaml-allocated values (such as the results from Val_bool) which aren't in the GC root set. Assigning the return value to the CAMLlocal variable puts it in the GC root set.

So if that's right, I approve of the patch.

Ian.

(I still think the docs are pretty poor.)

author
#36 lore raw

PATCH 13/28 libxl: ocaml: add domain_build/create_info/config and events to the bindings.

In reply to Rob Hoes

We now have enough infrastructure in place to do this trivially.

---
 tools/libxl/libxl_types.idl    |    2 +-
 tools/ocaml/libs/xl/genwrap.py |    4 ----
 2 files changed, 1 insertion(+), 5 deletions(-)
tools/libxl/libxl_types.idl +1 -1
index 778a416..ee3efba 100644
@@ -464,7 +464,7 @@ libxl_domain_config = Struct("domain_config", [
     ("on_reboot", libxl_action_on_shutdown),
     ("on_watchdog", libxl_action_on_shutdown),
     ("on_crash", libxl_action_on_shutdown),
-    ])
+    ], dir=DIR_IN)
 
 libxl_diskinfo = Struct("diskinfo", [
     ("backend", string),
tools/ocaml/libs/xl/genwrap.py +0 -4
index 611d892..8154458 100644
@@ -432,11 +432,7 @@ if __name__ == '__main__':
     # Do not generate these yet.
     blacklist = [
         "cpupoolinfo",
-        "domain_create_info",
-        "domain_build_info",
-        "domain_config",
         "vcpuinfo",
-        "event",
         ]
 
     for t in blacklist:
Signature · 1 line
1.7.10.4
author
#37 lore raw

PATCH 14/28 libxl: ocaml: add META to list of generated files in Makefile

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/Makefile |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/ocaml/libs/xl/Makefile +1 -1
index 6917a20..0408cc2 100644
@@ -22,7 +22,7 @@ OCAML_LIBRARY = xenlight
 
 GENERATED_FILES += xenlight.ml xenlight.ml.tmp xenlight.mli xenlight.mli.tmp
 GENERATED_FILES += _libxl_types.ml.in _libxl_types.mli.in
-GENERATED_FILES += _libxl_types.inc
+GENERATED_FILES += _libxl_types.inc META
 
 all: $(INTF) $(LIBS)
 
Signature · 1 line
1.7.10.4
#38 lore raw

Re: [PATCH v3-RESEND 14/28] libxl: ocaml: add META to list of generated files in Makefile

Quoted 3 lines · Rob Hoes

Pretty certain this one safely stands alone, applied, thanks.

If you want to highlight any others which are acked and which have no dependencies (or whose dependencies are recursively so) please do. Or feel free to not bother and they'll get swept along with the eventual commit.

Ian.

author
#39 lore raw

PATCH 15/28 libxl: ocaml: fix the handling of enums in the bindings generator

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +1 -1
index 8154458..acd2728 100644
@@ -331,7 +331,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
         n = 0
         s += "switch(%s) {\n" % c
         for e in ty.values:
-            s += "    case %s: %s = Int_val(%d); break;\n" % (e.name, o, n)
+            s += "    case %s: %s = Val_int(%d); break;\n" % (e.name, o, n)
             n += 1
         s += "    default: failwith_xl(ERROR_FAIL, \"cannot convert value from %s\"); break;\n" % ty.typename
         s += "}"
Signature · 1 line
1.7.10.4
author
#40 lore raw

PATCH 16/28 libxl: ocaml: use the "string option" type for IDL strings

In reply to Rob Hoes

The libxl IDL is based on C type "char *", and therefore "strings" can by NULL, or be an actual string. In ocaml, it is common to encode such things as option types.

---
 tools/ocaml/libs/xl/genwrap.py       |    2 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |   21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +1 -1
index acd2728..5cbb748 100644
@@ -8,7 +8,7 @@ import idl
 builtins = {
     "bool":                 ("bool",                   "%(c)s = Bool_val(%(o)s)",           "Val_bool(%(c)s)" ),
     "int":                  ("int",                    "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
-    "char *":               ("string",                 "%(c)s = dup_String_val(%(o)s)", "caml_copy_string(%(c)s)"),
+    "char *":               ("string option",          "%(c)s = String_option_val(%(o)s)",  "Val_string_option(%(c)s)"),
     "libxl_domid":          ("domid",                  "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
     "libxl_devid":          ("devid",                  "%(c)s = Int_val(%(o)s)",            "Val_int(%(c)s)"  ),
     "libxl_defbool":        ("bool option",            "%(c)s = Defbool_val(%(o)s)",        "Val_defbool(%(c)s)" ),
tools/ocaml/libs/xl/xenlight_stubs.c +21 -0
index d15a625..152600b 100644
@@ -347,6 +347,27 @@ static value Val_hwcap(libxl_hwcap *c_val)
 	CAMLreturn(hwcap);
 }
 
+static value Val_string_option(const char *c_val)
+{
+	CAMLparam0();
+	CAMLlocal2(tmp1, tmp2);
+	if (c_val) {
+		tmp1 = caml_copy_string(c_val);
+		tmp2 = Val_some(tmp1);
+		CAMLreturn(tmp2);
+	}
+	else
+		CAMLreturn(Val_none);
+}
+
+static char *String_option_val(value v)
+{
+	char *s = NULL;
+	if (v != Val_none)
+		s = dup_String_val(Some_val(v));
+	return s;
+}
+
 #include "_libxl_types.inc"
 
 #define _STRINGIFY(x) #x
Signature · 1 line
1.7.10.4
Acked-by on v1 16/28
#41 lore raw

Re: [PATCH v3-RESEND 16/28] libxl: ocaml: use the "string option" type for IDL strings

Quoted 5 lines · Rob Hoes

The libxl IDL is based on C type "char *", and therefore "strings" can by NULL, or be an actual string. In ocaml, it is common to encode such things as option types.

author
#43 lore raw

PATCH 17/28 libxl: ocaml: add dominfo_list and dominfo_get

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |    3 +++
 tools/ocaml/libs/xl/xenlight_stubs.c |   41 ++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
tools/ocaml/libs/xl/genwrap.py +3 -0
index 5cbb748..daeffdf 100644
@@ -33,6 +33,9 @@ functions = { # ( name , [type1,type2,....] )
     "device_disk":    DEVICE_FUNCTIONS,
     "device_nic":     DEVICE_FUNCTIONS,
     "device_pci":     DEVICE_FUNCTIONS,
+    "dominfo":        [ ("list",           ["ctx", "t list"]),
+                        ("get",            ["ctx", "domid", "t"]),
+                      ],
     "physinfo":       [ ("get",            ["ctx", "t"]),
                       ],
     "cputopology":    [ ("get",            ["ctx", "t array"]),
tools/ocaml/libs/xl/xenlight_stubs.c +41 -0

47 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#44 lore raw

PATCH 18/28 libxl: ocaml: implement some simple tests

In reply to Rob Hoes
---
 .gitignore                          |    3 ++-
 .hgignore                           |    2 ++
 tools/ocaml/test/Makefile           |   30 ++++++++++++++++++++++++++----
 tools/ocaml/test/list_domains.ml    |   28 ++++++++++++++++++++++++++++
 tools/ocaml/test/raise_exception.ml |   11 +++++++++++
 tools/ocaml/test/send_debug_keys.ml |   15 +++++++++++++++
 6 files changed, 84 insertions(+), 5 deletions(-)
 create mode 100644 tools/ocaml/test/list_domains.ml
 create mode 100644 tools/ocaml/test/raise_exception.ml
 create mode 100644 tools/ocaml/test/send_debug_keys.ml
.gitignore +2 -1
index f51c345..88a8c75 100644
@@ -385,7 +385,8 @@ tools/ocaml/libs/xl/xenlight.ml
 tools/ocaml/libs/xl/xenlight.mli
 tools/ocaml/xenstored/oxenstored
 tools/ocaml/test/xtl
-
+tools/ocaml/test/send_debug_keys
+tools/ocaml/test/list_domains
 tools/debugger/kdd/kdd
 tools/firmware/etherboot/ipxe.tar.gz
 tools/firmware/etherboot/ipxe/
.hgignore +2 -0
index bb1b67d..ee5c084 100644
@@ -309,6 +309,8 @@
 ^tools/ocaml/libs/xl/xenlight\.mli$
 ^tools/ocaml/xenstored/oxenstored$
 ^tools/ocaml/test/xtl$
+^tools/ocaml/test/send_debug_keys$
+^tools/ocaml/test/list_domains$
 ^tools/autom4te\.cache$
 ^tools/config\.h$
 ^tools/config\.log$
tools/ocaml/test/Makefile +26 -4
index 980054c..8387d43 100644
@@ -2,12 +2,16 @@ XEN_ROOT = $(CURDIR)/../../..
 OCAML_TOPLEVEL = $(CURDIR)/..
 include $(OCAML_TOPLEVEL)/common.make
 
+CFLAGS += $(CFLAGS_libxenlight)
+LIBS_xenlight = $(LDLIBS_libxenlight)
+
 OCAMLINCLUDE += \
-	-I $(OCAML_TOPLEVEL)/libs/xentoollog
+	-I $(OCAML_TOPLEVEL)/libs/xentoollog \
+	-I $(OCAML_TOPLEVEL)/libs/xl
 
-OBJS = xtl
+OBJS = xtl send_debug_keys list_domains raise_exception
 
-PROGRAMS = xtl
+PROGRAMS = xtl send_debug_keys list_domains raise_exception
 
 xtl_LIBS =  \
 	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
@@ -15,7 +19,25 @@ xtl_LIBS =  \
 
 xtl_OBJS = xtl
 
-OCAML_PROGRAM = xtl
+send_debug_keys_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xl $(OCAML_TOPLEVEL)/libs/xl/xenlight.cmxa
+
+send_debug_keys_OBJS = xtl send_debug_keys
+
+list_domains_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xl $(OCAML_TOPLEVEL)/libs/xl/xenlight.cmxa
+
+list_domains_OBJS = xtl list_domains
+
+raise_exception_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xl $(OCAML_TOPLEVEL)/libs/xl/xenlight.cmxa
+
+raise_exception_OBJS = raise_exception
+
+OCAML_PROGRAM = xtl send_debug_keys list_domains raise_exception
 
 all: $(PROGRAMS)
 
tools/ocaml/test/list_domains.ml new file +28 -0
new file mode 100644
index 0000000..15598e0
@@ -0,0 +1,28 @@
+open Arg
+open Printf
+open Xenlight
+
+let bool_as_char b c = if b then c else '-'
+
+let print_dominfo dominfo =
+  let id = dominfo.Xenlight.Dominfo.xl_domid
+  and running = bool_as_char dominfo.Xenlight.Dominfo.xl_running 'r'
+  and blocked = bool_as_char dominfo.Xenlight.Dominfo.xl_blocked 'b'
+  and paused = bool_as_char dominfo.Xenlight.Dominfo.xl_paused 'p'
+  and shutdown = bool_as_char dominfo.Xenlight.Dominfo.xl_shutdown 's'
+  and dying = bool_as_char dominfo.Xenlight.Dominfo.xl_dying 'd'
+  and memory = dominfo.Xenlight.Dominfo.xl_current_memkb
+  in
+  printf "Dom %d: %c%c%c%c%c %LdKB\n" id running blocked paused shutdown dying memory
+
+let _ =
+  let logger = Xtl.create_stdio_logger (*~level:Xentoollog.Debug*) () in
+  let ctx = Xenlight.ctx_alloc logger in
+  try
+    let domains = Xenlight.Dominfo.list ctx in
+    List.iter (fun d -> print_dominfo d) domains
+  with Xenlight.Error(err, fn) -> begin
+    printf "Caught Exception: %s: %s\n" (Xenlight.string_of_error err) fn;
+  end
+
+
tools/ocaml/test/raise_exception.ml new file +11 -0
new file mode 100644
index 0000000..d4371f5
@@ -0,0 +1,11 @@
+open Printf
+open Xentoollog
+open Xenlight
+
+let _ = 
+  try
+    Xenlight.test_raise_exception ()
+  with Xenlight.Error(err, fn) -> begin
+    printf "Caught Exception: %s: %s\n" (Xenlight.string_of_error err) fn;
+  end
+
tools/ocaml/test/send_debug_keys.ml new file +15 -0
new file mode 100644
index 0000000..b9cd61e
@@ -0,0 +1,15 @@
+open Arg
+open Printf
+open Xenlight
+
+let send_keys ctx s = 
+  printf "Sending debug key %s\n" s;
+  Xenlight.send_debug_keys ctx s;
+  ()
+  
+let _ = 
+  let logger = Xtl.create_stdio_logger () in
+  let ctx = Xenlight.ctx_alloc logger in
+  Arg.parse [
+  ] (fun s -> send_keys ctx s) "usage: send_debug_keys <keys>"
+
Signature · 1 line
1.7.10.4
author
#45 lore raw

PATCH 19/28 libxl: ocaml: event management

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/xenlight.ml.in   |   66 +++++++
 tools/ocaml/libs/xl/xenlight.mli.in  |   47 +++++
 tools/ocaml/libs/xl/xenlight_stubs.c |  325 ++++++++++++++++++++++++++++++++++
 3 files changed, 438 insertions(+)
tools/ocaml/libs/xl/xenlight.ml.in +66 -0
index a281425..9eba5d7 100644
@@ -25,10 +25,76 @@ external ctx_alloc: Xentoollog.handle -> ctx = "stub_libxl_ctx_alloc"
 
 external test_raise_exception: unit -> unit = "stub_raise_exception"
 
+type event =
+	| POLLIN (* There is data to read *)
+	| POLLPRI (* There is urgent data to read *)
+	| POLLOUT (* Writing now will not block *)
+	| POLLERR (* Error condition (revents only) *)
+	| POLLHUP (* Device has been disconnected (revents only) *)
+	| POLLNVAL (* Invalid request: fd not open (revents only). *)
+
 external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
 external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
 external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 
+module type EVENT_USERS =
+	sig
+		type osevent_user
+		type event_user
+		type async_user
+	end
+
+module Async = functor (S: EVENT_USERS) -> struct
+	type for_libxl
+	type event_hooks
+	type osevent_hooks
+
+	module OseventSet = Set.Make(struct type t = S.osevent_user;; let compare = Pervasives.compare end)
+	module EventSet = Set.Make(struct type t = S.event_user;; let compare = Pervasives.compare end)
+	module AsyncSet = Set.Make(struct type t = S.async_user;; let compare = Pervasives.compare end)
+
+	let osevent_users = ref OseventSet.empty
+	let event_users = ref EventSet.empty
+	let async_users = ref AsyncSet.empty
+	let async_callback_ref = ref None
+
+	external osevent_register_hooks' : ctx -> S.osevent_user -> osevent_hooks = "stub_libxl_osevent_register_hooks"
+	external osevent_occurred_fd : ctx -> for_libxl -> Unix.file_descr -> event list -> event list -> unit = "stub_libxl_osevent_occurred_fd"
+	external osevent_occurred_timeout : ctx -> for_libxl -> unit = "stub_libxl_osevent_occurred_timeout"
+
+	let osevent_register_hooks ctx ~user ~fd_register ~fd_modify ~fd_deregister ~timeout_register ~timeout_modify =
+		Callback.register "libxl_fd_register" fd_register;
+		Callback.register "libxl_fd_modify" fd_modify;
+		Callback.register "libxl_fd_deregister" fd_deregister;
+		Callback.register "libxl_timeout_register" timeout_register;
+		Callback.register "libxl_timeout_modify" timeout_modify;
+		osevent_users := OseventSet.add user !osevent_users;
+		osevent_register_hooks' ctx user
+
+	let async f user =
+		async_users := AsyncSet.add user !async_users;
+		f ?async:(Some user) ()
+
+	let async_callback' result user =
+		async_users := AsyncSet.remove user !async_users;
+		match !async_callback_ref with
+		| None -> ()
+		| Some f -> f ~result ~user
+
+	let async_register_callback ~async_callback =
+		async_callback_ref := Some async_callback;
+		Callback.register "libxl_async_callback" async_callback'
+
+	external evenable_domain_death : ctx -> domid -> int -> unit = "stub_libxl_evenable_domain_death"
+	external event_register_callbacks' : ctx -> S.event_user -> event_hooks = "stub_libxl_event_register_callbacks"
+
+	let event_register_callbacks ctx ~user ~event_occurs_callback ~event_disaster_callback =
+		Callback.register "libxl_event_occurs_callback" event_occurs_callback;
+		Callback.register "libxl_event_disaster_callback" event_disaster_callback;
+		event_users := EventSet.add user !event_users;
+		event_register_callbacks' ctx user
+end
+
 let register_exceptions () =
 	Callback.register_exception "Xenlight.Error" (Error(ERROR_FAIL, ""))
 
tools/ocaml/libs/xl/xenlight.mli.in +47 -0

54 lines not on this page · raw message

tools/ocaml/libs/xl/xenlight_stubs.c +325 -0

343 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#46 lore raw

PATCH 20/28 libxl: ocaml: allow device operations to be called asynchronously

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |    6 +++---
 tools/ocaml/libs/xl/xenlight_stubs.c |   14 +++++++++++---
 2 files changed, 14 insertions(+), 6 deletions(-)
tools/ocaml/libs/xl/genwrap.py +3 -3
index daeffdf..721a336 100644
@@ -22,9 +22,9 @@ builtins = {
     "libxl_cpuid_policy_list": ("unit",                "%(c)s = 0",                         "Val_unit"),
     }
 
-DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "unit"]),
-                     ("remove",         ["ctx", "t", "domid", "unit"]),
-                     ("destroy",        ["ctx", "t", "domid", "unit"]),
+DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                     ("remove",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                     ("destroy",        ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
                    ]
 
 functions = { # ( name , [type1,type2,....] )
tools/ocaml/libs/xl/xenlight_stubs.c +11 -3
index 5b38e7c..7e56db9 100644
@@ -396,15 +396,23 @@ void async_callback(libxl_ctx *ctx, int rc, void *for_callback)
 #define STRINGIFY(x) _STRINGIFY(x)
 
 #define _DEVICE_ADDREMOVE(type,op)					\
-value stub_xl_device_##type##_##op(value ctx, value info, value domid)	\
+value stub_xl_device_##type##_##op(value ctx, value info, value domid,	\
+	value async, value unit)					\
 {									\
-	CAMLparam3(ctx, info, domid);					\
+	CAMLparam5(ctx, info, domid, async, unit);			\
 	libxl_device_##type c_info;					\
 	int ret, marker_var;						\
+	libxl_asyncop_how ao_how;					\
 									\
 	device_##type##_val(CTX, &c_info, info);			\
 									\
-	ret = libxl_device_##type##_##op(CTX, Int_val(domid), &c_info, 0); \
+	if (async != Val_none) {					\
+		ao_how.callback = async_callback;			\
+		ao_how.u.for_callback = (void *) Some_val(async);	\
+	}								\
+									\
+	ret = libxl_device_##type##_##op(CTX, Int_val(domid), &c_info,	\
+		async != Val_none ? &ao_how : NULL);			\
 									\
 	libxl_device_##type##_dispose(&c_info);				\
 									\
Signature · 1 line
1.7.10.4
#47 lore raw

Re: [PATCH v3-RESEND 20/28] libxl: ocaml: allow device operations to be called asynchronously

Quoted 11 lines · Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |    6 +++---
 tools/ocaml/libs/xl/xenlight_stubs.c |   14 +++++++++++---
 2 files changed, 14 insertions(+), 6 deletions(-)
tools/ocaml/libs/xl/genwrap.py +0 -0
index daeffdf..721a336 100644

tools/ocaml/libs/xl/genwrap.py builtins = { · line 27

Quoted 9 lines
tools/ocaml/libs/xl/genwrap.py +3 -3
@@ -22,9 +22,9 @@ builtins = {
     "libxl_cpuid_policy_list": ("unit",                "%(c)s = 0",                         "Val_unit"),
     }
 
-DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "unit"]),
-                     ("remove",         ["ctx", "t", "domid", "unit"]),
-                     ("destroy",        ["ctx", "t", "domid", "unit"]),
+DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                     ("remove",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                     ("destroy",        ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),

I probably don't speak enough ocaml to make heads or tails of it but can you include the resulting ocaml type in the changelog? Is it really

        ctx-> t -> ?async -> () -> ()

?

I guess I don't fully grok "-> ()" if it can chain like that ;-)

Quoted 8 lines
tools/ocaml/libs/xl/genwrap.py +0 -0
                    ]
 
 functions = { # ( name , [type1,type2,....] )
tools/ocaml/libs/xl/xenlight_stubs.c +0 -0
index 5b38e7c..7e56db9 100644

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback) · line 402

Quoted 9 lines
tools/ocaml/libs/xl/xenlight_stubs.c +3 -2
@@ -396,15 +396,23 @@ void async_callback(libxl_ctx *ctx, int rc, void *for_callback)
 #define STRINGIFY(x) _STRINGIFY(x)
 
 #define _DEVICE_ADDREMOVE(type,op)					\
-value stub_xl_device_##type##_##op(value ctx, value info, value domid)	\
+value stub_xl_device_##type##_##op(value ctx, value info, value domid,	\
+	value async, value unit)					\
 {									\
-	CAMLparam3(ctx, info, domid);					\
+	CAMLparam5(ctx, info, domid, async, unit);			\

Only one unit here?

Quoted 17 lines
tools/ocaml/libs/xl/xenlight_stubs.c +8 -1
 	libxl_device_##type c_info;					\
 	int ret, marker_var;						\
+	libxl_asyncop_how ao_how;					\
 									\
 	device_##type##_val(CTX, &c_info, info);			\
 									\
-	ret = libxl_device_##type##_##op(CTX, Int_val(domid), &c_info, 0); \
+	if (async != Val_none) {					\
+		ao_how.callback = async_callback;			\
+		ao_how.u.for_callback = (void *) Some_val(async);	\
+	}								\
+									\
+	ret = libxl_device_##type##_##op(CTX, Int_val(domid), &c_info,	\
+		async != Val_none ? &ao_how : NULL);			\
 									\
 	libxl_device_##type##_dispose(&c_info);				\
 									\
author
#48 lore raw

Re: [PATCH v3-RESEND 20/28] libxl: ocaml: allow device operations to be called asynchronously

Quoted 24 lines · Rob Hoes

tools/ocaml/libs/xl/genwrap.py builtins = { · line 22

Quoted 12 lines
---
 tools/ocaml/libs/xl/genwrap.py       |    6 +++---
 tools/ocaml/libs/xl/xenlight_stubs.c |   14 +++++++++++---
 2 files changed, 14 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/tools/ocaml/libs/xl/genwrap.py index daeffdf..721a336 100644

tools/ocaml/libs/xl/genwrap.py +0 -0
@@ -22,9 +22,9 @@ builtins = {
     "libxl_cpuid_policy_list": ("unit",                "%(c)s = 0",

"Val_unit"),

tools/ocaml/libs/xl/genwrap.py builtins = {

Quoted 6 lines
tools/ocaml/libs/xl/genwrap.py +1 -3
     }
 
-DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "unit"]),
-                     ("remove",         ["ctx", "t", "domid", "unit"]),
-                     ("destroy",        ["ctx", "t", "domid", "unit"]),
+DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "?async:'a",

"unit", "unit"]),

tools/ocaml/libs/xl/genwrap.py builtins = { · line 27

Quoted 2 lines
tools/ocaml/libs/xl/genwrap.py +2 -0
+                     ("remove",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                     ("destroy",        ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
Quoted 6 lines

I probably don't speak enough ocaml to make heads or tails of it but can you include the resulting ocaml type in the changelog? Is it really

        ctx-> t -> ?async -> () -> ()

?

I guess I don't fully grok "-> ()" if it can chain like that ;-)

The type of the function is "ctx-> t -> domid -> ?async -> () -> ()". The last () is the return type. The one before that is the type of the last argument of the function, which means you always have to call the function with () at the end. This used to mark "the end" of the function. It is not allowed in OCaml for functions to have optional arguments at the end.

Synchronous example:
remove ctx my_disk 666 ()

Asynchronous example:
remove ctx my_disk 666 ~async:my_user_id ()

The manual says: "A function taking some optional arguments must also take at least one non-optional argument. The criterion for deciding whether an optional argument has been omitted is the non-labeled application of an argument appearing after this optional argument in the function type." [http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual006.html]

So another option would be to move the ?async argument further to the left, and skip the extra (), e.g. "ctx -> ?async -> t -> domid -> ()". But I wanted to keep the async stuff at the end (like in the libxl function), and adding a () is quite common in OCaml.

Rob

Quoted 46 lines

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void · line 398

Quoted 13 lines
tools/ocaml/libs/xl/genwrap.py +0 -0
                    ]
 
 functions = { # ( name , [type1,type2,....] ) diff --git

a/tools/ocaml/libs/xl/xenlight_stubs.c
b/tools/ocaml/libs/xl/xenlight_stubs.c
index 5b38e7c..7e56db9 100644

tools/ocaml/libs/xl/xenlight_stubs.c +0 -0
@@ -396,15 +396,23 @@ void async_callback(libxl_ctx *ctx, int rc, void
 *for_callback)  #define STRINGIFY(x) _STRINGIFY(x)
 
 #define _DEVICE_ADDREMOVE(type,op)
	\

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void · line 399

Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +0 -1
-value stub_xl_device_##type##_##op(value ctx, value info, value domid)
	\

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void · line 399

Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
+value stub_xl_device_##type##_##op(value ctx, value info, value domid,
	\

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback)

Quoted 3 lines
tools/ocaml/libs/xl/xenlight_stubs.c +1 -1
+	value async, value unit)					\
 {									\
-	CAMLparam3(ctx, info, domid);
	\

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback) · line 402

Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
+	CAMLparam5(ctx, info, domid, async, unit);			\

Only one unit here?

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback)

Quoted 13 lines
tools/ocaml/libs/xl/xenlight_stubs.c +7 -1
 	libxl_device_##type c_info;					\
 	int ret, marker_var;						\
+	libxl_asyncop_how ao_how;					\
 									\
 	device_##type##_val(CTX, &c_info, info);			\
 									\
-	ret = libxl_device_##type##_##op(CTX, Int_val(domid), &c_info, 0); \
+	if (async != Val_none) {					\
+		ao_how.callback = async_callback;			\
+		ao_how.u.for_callback = (void *) Some_val(async);	\
+	}								\
+									\
+	ret = libxl_device_##type##_##op(CTX, Int_val(domid), &c_info,
	\

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback)

Quoted 3 lines
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
+		async != Val_none ? &ao_how : NULL);			\
 									\
 	libxl_device_##type##_dispose(&c_info);
	\
Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +0 -0
 									\
Acked-by
#49 lore raw

Re: [PATCH v3-RESEND 20/28] libxl: ocaml: allow device operations to be called asynchronously

Quoted 29 lines · Rob Hoes
Quoted 29 lines · Rob Hoes

tools/ocaml/libs/xl/genwrap.py builtins = { · line 22

Quoted 12 lines
---
 tools/ocaml/libs/xl/genwrap.py       |    6 +++---
 tools/ocaml/libs/xl/xenlight_stubs.c |   14 +++++++++++---
 2 files changed, 14 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/tools/ocaml/libs/xl/genwrap.py index daeffdf..721a336 100644

tools/ocaml/libs/xl/genwrap.py +0 -0
@@ -22,9 +22,9 @@ builtins = {
     "libxl_cpuid_policy_list": ("unit",                "%(c)s = 0",

"Val_unit"),

tools/ocaml/libs/xl/genwrap.py builtins = {

Quoted 6 lines
tools/ocaml/libs/xl/genwrap.py +1 -3
     }
 
-DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "unit"]),
-                     ("remove",         ["ctx", "t", "domid", "unit"]),
-                     ("destroy",        ["ctx", "t", "domid", "unit"]),
+DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "?async:'a",

"unit", "unit"]),

tools/ocaml/libs/xl/genwrap.py builtins = { · line 27

Quoted 2 lines
tools/ocaml/libs/xl/genwrap.py +2 -0
+                     ("remove",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                     ("destroy",        ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),

I probably don't speak enough ocaml to make heads or tails of it but can you include the resulting ocaml type in the changelog? Is it really

        ctx-> t -> ?async -> () -> ()

?

Quoted 8 lines
Quoted 1 line

I guess I don't fully grok "-> ()" if it can chain like that ;-)

The type of the function is "ctx-> t -> domid -> ?async -> () -> ()". The last () is the return type. The one before that is the type of the last argument of the function, which means you always have to call the function with () at the end. This used to mark "the end" of the function. It is not allowed in OCaml for functions to have optional arguments at the end.

Ah, yes, someone taught me that once before!

[...]

Quoted 4 lines

So another option would be to move the ?async argument further to the left, and skip the extra (), e.g. "ctx -> ?async -> t -> domid -> ()". But I wanted to keep the async stuff at the end (like in the libxl function), and adding a () is quite common in OCaml.

Right, assuming you are happy that the ordering you have is logical from an ocaml pov (which may differ from libxl/C) it sounds fine to me.

Thanks for (re)educating me!

Ian.

author
#50 lore raw

PATCH 21/28 libxl: ocaml: add NIC helper functions

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |    5 ++++-
 tools/ocaml/libs/xl/xenlight_stubs.c |   36 ++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +4 -1
index 721a336..c037515 100644
@@ -31,7 +31,10 @@ functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
     "device_disk":    DEVICE_FUNCTIONS,
-    "device_nic":     DEVICE_FUNCTIONS,
+    "device_nic":     DEVICE_FUNCTIONS +
+                      [ ("list",           ["ctx", "domid", "t list"]),
+                        ("of_devid",       ["ctx", "domid", "int", "t"]),
+                      ],
     "device_pci":     DEVICE_FUNCTIONS,
     "dominfo":        [ ("list",           ["ctx", "t list"]),
                         ("get",            ["ctx", "domid", "t"]),
tools/ocaml/libs/xl/xenlight_stubs.c +36 -0
index 7e56db9..b921229 100644
@@ -433,6 +433,42 @@ DEVICE_ADDREMOVE(vfb)
 DEVICE_ADDREMOVE(vkb)
 DEVICE_ADDREMOVE(pci)
 
+value stub_xl_device_nic_of_devid(value ctx, value domid, value devid)
+{
+	CAMLparam3(ctx, domid, devid);
+	libxl_device_nic nic;
+	libxl_devid_to_device_nic(CTX, Int_val(domid), Int_val(devid), &nic);
+	CAMLreturn(Val_device_nic(&nic));
+}
+
+value stub_xl_device_nic_list(value ctx, value domid)
+{
+	CAMLparam2(ctx, domid);
+	CAMLlocal2(list, temp);
+	libxl_device_nic *c_list;
+	int i, nb;
+	uint32_t c_domid;
+
+	c_domid = Int_val(domid);
+
+	c_list = libxl_device_nic_list(CTX, c_domid, &nb);
+	if (!c_list)
+		failwith_xl(ERROR_FAIL, "nic_list");
+
+	list = temp = Val_emptylist;
+	for (i = 0; i < nb; i++) {
+		list = caml_alloc_small(2, Tag_cons);
+		Field(list, 0) = Val_int(0);
+		Field(list, 1) = temp;
+		temp = list;
+		Store_field(list, 0, Val_device_nic(&c_list[i]));
+		libxl_device_nic_dispose(&c_list[i]);
+	}
+	free(c_list);
+
+	CAMLreturn(list);
+}
+
 value stub_xl_physinfo_get(value ctx)
 {
 	CAMLparam1(ctx);
Signature · 1 line
1.7.10.4
#51 lore raw

Re: [PATCH v3-RESEND 21/28] libxl: ocaml: add NIC helper functions

Quoted 27 lines · Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |    5 ++++-
 tools/ocaml/libs/xl/xenlight_stubs.c |   36 ++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +4 -1
index 721a336..c037515 100644
@@ -31,7 +31,10 @@ functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
     "device_disk":    DEVICE_FUNCTIONS,
-    "device_nic":     DEVICE_FUNCTIONS,
+    "device_nic":     DEVICE_FUNCTIONS +
+                      [ ("list",           ["ctx", "domid", "t list"]),
+                        ("of_devid",       ["ctx", "domid", "int", "t"]),
+                      ],
     "device_pci":     DEVICE_FUNCTIONS,
     "dominfo":        [ ("list",           ["ctx", "t list"]),
                         ("get",            ["ctx", "domid", "t"]),
tools/ocaml/libs/xl/xenlight_stubs.c +0 -0
index 7e56db9..b921229 100644

tools/ocaml/libs/xl/xenlight_stubs.c DEVICE_ADDREMOVE(vfb) · line 441

Quoted 9 lines
tools/ocaml/libs/xl/xenlight_stubs.c +6 -0
@@ -433,6 +433,42 @@ DEVICE_ADDREMOVE(vfb)
 DEVICE_ADDREMOVE(vkb)
 DEVICE_ADDREMOVE(pci)
 
+value stub_xl_device_nic_of_devid(value ctx, value domid, value devid)
+{
+	CAMLparam3(ctx, domid, devid);
+	libxl_device_nic nic;
+	libxl_devid_to_device_nic(CTX, Int_val(domid), Int_val(devid), &nic);
+	CAMLreturn(Val_device_nic(&nic));

You need to libxl_device_nic_dispose(&nic) I think.

Quoted 33 lines
tools/ocaml/libs/xl/xenlight_stubs.c +30 -0
+}
+
+value stub_xl_device_nic_list(value ctx, value domid)
+{
+	CAMLparam2(ctx, domid);
+	CAMLlocal2(list, temp);
+	libxl_device_nic *c_list;
+	int i, nb;
+	uint32_t c_domid;
+
+	c_domid = Int_val(domid);
+
+	c_list = libxl_device_nic_list(CTX, c_domid, &nb);
+	if (!c_list)
+		failwith_xl(ERROR_FAIL, "nic_list");
+
+	list = temp = Val_emptylist;
+	for (i = 0; i < nb; i++) {
+		list = caml_alloc_small(2, Tag_cons);
+		Field(list, 0) = Val_int(0);
+		Field(list, 1) = temp;
+		temp = list;
+		Store_field(list, 0, Val_device_nic(&c_list[i]));
+		libxl_device_nic_dispose(&c_list[i]);
+	}
+	free(c_list);
+
+	CAMLreturn(list);
+}
+
 value stub_xl_physinfo_get(value ctx)
 {
 	CAMLparam1(ctx);
author
#52 lore raw

Re: [PATCH v3-RESEND 21/28] libxl: ocaml: add NIC helper functions

Quoted 39 lines · Rob Hoes
Quoted 4 lines
---
 tools/ocaml/libs/xl/genwrap.py       |    5 ++++-
 tools/ocaml/libs/xl/xenlight_stubs.c |   36

++++++++++++++++++++++++++++++++++

Quoted 24 lines
 2 files changed, 40 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/tools/ocaml/libs/xl/genwrap.py index 721a336..c037515 100644

tools/ocaml/libs/xl/genwrap.py +4 -1
@@ -31,7 +31,10 @@ functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
     "device_disk":    DEVICE_FUNCTIONS,
-    "device_nic":     DEVICE_FUNCTIONS,
+    "device_nic":     DEVICE_FUNCTIONS +
+                      [ ("list",           ["ctx", "domid", "t list"]),
+                        ("of_devid",       ["ctx", "domid", "int", "t"]),
+                      ],
     "device_pci":     DEVICE_FUNCTIONS,
     "dominfo":        [ ("list",           ["ctx", "t list"]),
                         ("get",            ["ctx", "domid", "t"]),
tools/ocaml/libs/xl/xenlight_stubs.c +0 -0
index 7e56db9..b921229 100644

tools/ocaml/libs/xl/xenlight_stubs.c DEVICE_ADDREMOVE(vfb) · line 440

Quoted 8 lines
tools/ocaml/libs/xl/xenlight_stubs.c +5 -0
@@ -433,6 +433,42 @@ DEVICE_ADDREMOVE(vfb)
 DEVICE_ADDREMOVE(vkb)
 DEVICE_ADDREMOVE(pci)
 
+value stub_xl_device_nic_of_devid(value ctx, value domid, value
+devid) {
+	CAMLparam3(ctx, domid, devid);
+	libxl_device_nic nic;
+	libxl_devid_to_device_nic(CTX, Int_val(domid), Int_val(devid),

&nic);

Quoted 3 lines

tools/ocaml/libs/xl/xenlight_stubs.c DEVICE_ADDREMOVE(vfb) · line 441

Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
+	CAMLreturn(Val_device_nic(&nic));

You need to libxl_device_nic_dispose(&nic) I think.

Ok, will add that (and for the following disk-related patch as well).

Rob

Quoted 32 lines
Quoted 31 lines
tools/ocaml/libs/xl/xenlight_stubs.c +29 -0
+}
+
+value stub_xl_device_nic_list(value ctx, value domid) {
+	CAMLparam2(ctx, domid);
+	CAMLlocal2(list, temp);
+	libxl_device_nic *c_list;
+	int i, nb;
+	uint32_t c_domid;
+
+	c_domid = Int_val(domid);
+
+	c_list = libxl_device_nic_list(CTX, c_domid, &nb);
+	if (!c_list)
+		failwith_xl(ERROR_FAIL, "nic_list");
+
+	list = temp = Val_emptylist;
+	for (i = 0; i < nb; i++) {
+		list = caml_alloc_small(2, Tag_cons);
+		Field(list, 0) = Val_int(0);
+		Field(list, 1) = temp;
+		temp = list;
+		Store_field(list, 0, Val_device_nic(&c_list[i]));
+		libxl_device_nic_dispose(&c_list[i]);
+	}
+	free(c_list);
+
+	CAMLreturn(list);
+}
+
 value stub_xl_physinfo_get(value ctx)  {
 	CAMLparam1(ctx);
author
#53 lore raw

PATCH 22/28 libxl: ocaml: add PCI device helper functions

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |    7 ++-
 tools/ocaml/libs/xl/xenlight_stubs.c |   90 ++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 1 deletion(-)
tools/ocaml/libs/xl/genwrap.py +6 -1
index c037515..819df3a 100644
@@ -35,7 +35,12 @@ functions = { # ( name , [type1,type2,....] )
                       [ ("list",           ["ctx", "domid", "t list"]),
                         ("of_devid",       ["ctx", "domid", "int", "t"]),
                       ],
-    "device_pci":     DEVICE_FUNCTIONS,
+    "device_pci":     DEVICE_FUNCTIONS +
+                      [ ("list",              ["ctx", "domid", "t list"]),
+                        ("assignable_add",    ["ctx", "t", "bool", "unit"]),
+                        ("assignable_remove", ["ctx", "t", "bool", "unit"]),
+                        ("assignable_list",   ["ctx", "t list"]),
+                      ],
     "dominfo":        [ ("list",           ["ctx", "t list"]),
                         ("get",            ["ctx", "domid", "t"]),
                       ],
tools/ocaml/libs/xl/xenlight_stubs.c +90 -0

96 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#55 lore raw

PATCH 23/28 libxl: ocaml: add disk and cdrom helper functions

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py       |   17 +++++++-----
 tools/ocaml/libs/xl/xenlight_stubs.c |   47 ++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 12 deletions(-)
tools/ocaml/libs/xl/genwrap.py +10 -7
index 819df3a..192bec2 100644
@@ -26,18 +26,21 @@ DEVICE_FUNCTIONS = [ ("add",            ["ctx", "t", "domid", "?async:'a", "unit
                      ("remove",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
                      ("destroy",        ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
                    ]
+DEVICE_LIST =      [ ("list",           ["ctx", "domid", "t list"]),
+                   ]
 
 functions = { # ( name , [type1,type2,....] )
     "device_vfb":     DEVICE_FUNCTIONS,
     "device_vkb":     DEVICE_FUNCTIONS,
-    "device_disk":    DEVICE_FUNCTIONS,
-    "device_nic":     DEVICE_FUNCTIONS +
-                      [ ("list",           ["ctx", "domid", "t list"]),
-                        ("of_devid",       ["ctx", "domid", "int", "t"]),
+    "device_disk":    DEVICE_FUNCTIONS + DEVICE_LIST +
+                      [ ("insert",         ["ctx", "t", "domid", "?async:'a", "unit", "unit"]),
+                        ("of_vdev",        ["ctx", "domid", "string", "t"]),
+                      ],
+    "device_nic":     DEVICE_FUNCTIONS + DEVICE_LIST +
+                      [ ("of_devid",       ["ctx", "domid", "int", "t"]),
                       ],
-    "device_pci":     DEVICE_FUNCTIONS +
-                      [ ("list",              ["ctx", "domid", "t list"]),
-                        ("assignable_add",    ["ctx", "t", "bool", "unit"]),
+    "device_pci":     DEVICE_FUNCTIONS + DEVICE_LIST +
+                      [ ("assignable_add",    ["ctx", "t", "bool", "unit"]),
                         ("assignable_remove", ["ctx", "t", "bool", "unit"]),
                         ("assignable_list",   ["ctx", "t list"]),
                       ],
tools/ocaml/libs/xl/xenlight_stubs.c +42 -5

77 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#56 lore raw

Re: [PATCH v3-RESEND 23/28] libxl: ocaml: add disk and cdrom helper functions

tools/ocaml/libs/xl/xenlight_stubs.c value stub_xl_device_nic_list(value ctx, value domid) · line 506

Quoted 6 lines · Rob Hoes
tools/ocaml/libs/xl/xenlight_stubs.c +6 -0
+value stub_xl_device_disk_of_vdev(value ctx, value domid, value vdev)
+{
+	CAMLparam3(ctx, domid, vdev);
+	libxl_device_disk disk;
+	libxl_vdev_to_device_disk(CTX, Int_val(domid), String_val(vdev), &disk);
+	CAMLreturn(Val_device_disk(&disk));

Needs to dispose of the C disk.

Ian.

author
#57 lore raw

PATCH 24/28 libxl: ocaml: add VM lifecycle operations

In reply to Rob Hoes

Also, reorganise toplevel OCaml functions into modules of Xenlight.

---
 tools/ocaml/libs/xl/xenlight.ml.in   |   22 +++-
 tools/ocaml/libs/xl/xenlight.mli.in  |   22 +++-
 tools/ocaml/libs/xl/xenlight_stubs.c |  208 ++++++++++++++++++++++++++++++++++
 tools/ocaml/test/send_debug_keys.ml  |    2 +-
 4 files changed, 247 insertions(+), 7 deletions(-)
tools/ocaml/libs/xl/xenlight.ml.in +19 -3
index 9eba5d7..8388c5f 100644
@@ -33,9 +33,25 @@ type event =
 	| POLLHUP (* Device has been disconnected (revents only) *)
 	| POLLNVAL (* Invalid request: fd not open (revents only). *)
 
-external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
-external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
-external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
+module Domain = struct
+	external create_new : ctx -> Domain_config.t -> ?async:'a -> unit -> domid = "stub_libxl_domain_create_new"
+	external create_restore : ctx -> Domain_config.t -> (Unix.file_descr * Domain_restore_params.t) ->
+		?async:'a -> unit -> domid = "stub_libxl_domain_create_restore"
+	external shutdown : ctx -> domid -> unit = "stub_libxl_domain_shutdown"
+	external wait_shutdown : ctx -> domid -> unit = "stub_libxl_domain_wait_shutdown"
+	external reboot : ctx -> domid -> unit = "stub_libxl_domain_reboot"
+	external destroy : ctx -> domid -> ?async:'a -> unit -> unit = "stub_libxl_domain_destroy"
+	external suspend : ctx -> domid -> Unix.file_descr -> ?async:'a -> unit -> unit = "stub_libxl_domain_suspend"
+	external pause : ctx -> domid -> unit = "stub_libxl_domain_pause"
+	external unpause : ctx -> domid -> unit = "stub_libxl_domain_unpause"
+
+	external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
+	external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
+end
+
+module Host = struct
+	external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
+end
 
 module type EVENT_USERS =
 	sig
tools/ocaml/libs/xl/xenlight.mli.in +19 -3
index 28e0eb2..31faf26 100644
@@ -35,9 +35,25 @@ type event =
 	| POLLHUP (* Device has been disconnected (revents only) *)
 	| POLLNVAL (* Invalid request: fd not open (revents only). *)
 
-external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
-external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
-external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
+module Domain : sig
+	external create_new : ctx -> Domain_config.t -> ?async:'a -> unit -> domid = "stub_libxl_domain_create_new"
+	external create_restore : ctx -> Domain_config.t -> (Unix.file_descr * Domain_restore_params.t) ->
+		?async:'a -> unit -> domid = "stub_libxl_domain_create_restore"
+	external shutdown : ctx -> domid -> unit = "stub_libxl_domain_shutdown"
+	external wait_shutdown : ctx -> domid -> unit = "stub_libxl_domain_wait_shutdown"
+	external reboot : ctx -> domid -> unit = "stub_libxl_domain_reboot"
+	external destroy : ctx -> domid -> ?async:'a -> unit -> unit = "stub_libxl_domain_destroy"
+	external suspend : ctx -> domid -> Unix.file_descr -> ?async:'a -> unit -> unit = "stub_libxl_domain_suspend"
+	external pause : ctx -> domid -> unit = "stub_libxl_domain_pause"
+	external unpause : ctx -> domid -> unit = "stub_libxl_domain_unpause"
+
+	external send_trigger : ctx -> domid -> trigger -> int -> unit = "stub_xl_send_trigger"
+	external send_sysrq : ctx -> domid -> char -> unit = "stub_xl_send_sysrq"
+end
+
+module Host : sig
+	external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
+end
 
 module type EVENT_USERS =
 	sig
tools/ocaml/libs/xl/xenlight_stubs.c +208 -0

214 lines not on this page · raw message

tools/ocaml/test/send_debug_keys.ml +1 -1
index b9cd61e..2cca322 100644
@@ -4,7 +4,7 @@ open Xenlight
 
 let send_keys ctx s = 
   printf "Sending debug key %s\n" s;
-  Xenlight.send_debug_keys ctx s;
+  Xenlight.Host.send_debug_keys ctx s;
   ()
   
 let _ = 
Signature · 1 line
1.7.10.4
#58 lore raw

Re: [PATCH v3-RESEND 24/28] libxl: ocaml: add VM lifecycle operations

Quoted 1 line · Rob Hoes

+static int domain_wait_event(libxl_ctx *ctx, int domid, libxl_event **event_r)

Do you not have the infrastructure in place to bind this to ocaml and do the next level of the call chain in ocaml instead of C?

Quoted 24 lines
tools/ocaml/libs/xl/xenlight_stubs.c +23 -0
+{
+	int ret;
+	for (;;) {
+		ret = libxl_event_wait(ctx, event_r, LIBXL_EVENTMASK_ALL, 0,0);
+		if (ret) {
+			return ret;
+		}
+		if ((*event_r)->domid != domid) {
+			libxl_event_free(CTX, *event_r);
+			continue;
+		}
+		return ret;
+	}
+}
+
+value stub_libxl_domain_create_new(value ctx, value domain_config, value async, value unit)
+{
[...]
+	libxl_asyncop_how ao_how;
+
+	if (async != Val_none) {
+		ao_how.callback = async_callback;
+		ao_how.u.for_callback = (void *) Some_val(async);
+	}

[...]

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback) · line 432

Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
+		async != Val_none ? &ao_how : NULL, NULL);

Lots of this patten. What about

#define OCAML_ASYNC_HOW \
	libxl_asyncop_how _ao_how; \
	libxl_asyncop_how *ao_how = NULL; \
	if (async != Val_none) \
		_ao_how.callback = ...
		_ao_how.u.for_callback = ...
		ao_how = &_ao_how

Then an unconditional ao_how at the point of use?

This relies on this file being compiled to allow mixed code and variable definitions. We do that for libxl itself, not sure about here. Could be enabled.

Or at least the initialisation of ao_how could be made a function. Perhaps

	static value Val_oahow(value aohow struct aohow *c_aohow)
	{
		if (aohow == Val_none) return NULL;

		c_aohow->etc 
		return c_aohow
	}

then

	libxl_aohow aohow;

	libxl_do_foo(....
		Val_aohow(async, &aohow), ...);

Review

Quoted 2 lines
(quoted diff) +2 -0
+value stub_libxl_domain_wait_shutdown(value ctx, value domid)
+{

This is the thing I meant could you do in ocaml above...

Quoted 17 lines
tools/ocaml/libs/xl/xenlight_stubs.c +17 -0
+	CAMLparam2(ctx, domid);
+	int ret;
+	libxl_event *event;
+	libxl_evgen_domain_death *deathw;
+	ret = libxl_evenable_domain_death(CTX, Int_val(domid), 0, &deathw);
+	if (ret)
+		failwith_xl(ret, "domain_wait_shutdown");
+
+	for (;;) {
+		ret = domain_wait_event(CTX, Int_val(domid), &event);
+		if (ret) {
+			libxl_evdisable_domain_death(CTX, deathw);
+			failwith_xl(ret, "domain_wait_shutdown");
+		}
+
+		switch (event->type) {
+		case LIBXL_EVENT_TYPE_DOMAIN_DEATH:

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback) · line 511

Quoted 10 lines
tools/ocaml/libs/xl/xenlight_stubs.c +10 -0
+			goto done;
+		case LIBXL_EVENT_TYPE_DOMAIN_SHUTDOWN:
+			goto done;
+		default:
+			break;
+		}
+		libxl_event_free(CTX, event);
+	}
+done:
+	libxl_event_free(CTX, event);

I don't know, but this might be clearer with

	the_right-type type = event->type;
	lbixl_event_free(...)

before the switch, which then becomes switch(type)?

Ian.

author
#59 lore raw

Re: [PATCH v3-RESEND 24/28] libxl: ocaml: add VM lifecycle operations

Quoted 7 lines · Rob Hoes

Review

Quoted 3 lines
(quoted diff) +2 -0
+static int domain_wait_event(libxl_ctx *ctx, int domid, libxl_event
+**event_r)

Do you not have the infrastructure in place to bind this to ocaml and do the next level of the call chain in ocaml instead of C?

It turns out that we are not actually using this anymore in our code, nor the wait_shutdown function introduced in this patch. We already have the lower-level event functionality implemented. I'll remove these from the patch.

Quoted 53 lines

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback)

Quoted 4 lines
tools/ocaml/libs/xl/xenlight_stubs.c +4 -0
+{
+	int ret;
+	for (;;) {
+		ret = libxl_event_wait(ctx, event_r, LIBXL_EVENTMASK_ALL,

0,0);

Quoted 20 lines
tools/ocaml/libs/xl/xenlight_stubs.c +19 -0
+		if (ret) {
+			return ret;
+		}
+		if ((*event_r)->domid != domid) {
+			libxl_event_free(CTX, *event_r);
+			continue;
+		}
+		return ret;
+	}
+}
+
+value stub_libxl_domain_create_new(value ctx, value domain_config,
+value async, value unit) {
[...]
+	libxl_asyncop_how ao_how;
+
+	if (async != Val_none) {
+		ao_how.callback = async_callback;
+		ao_how.u.for_callback = (void *) Some_val(async);
+	}

[...]

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback) · line 432

Quoted 1 line
tools/ocaml/libs/xl/xenlight_stubs.c +1 -0
+		async != Val_none ? &ao_how : NULL, NULL);

Lots of this patten. What about

#define OCAML_ASYNC_HOW \
	libxl_asyncop_how _ao_how; \
	libxl_asyncop_how *ao_how = NULL; \
	if (async != Val_none) \
		_ao_how.callback = ...
		_ao_how.u.for_callback = ...
		ao_how = &_ao_how

Then an unconditional ao_how at the point of use?

This relies on this file being compiled to allow mixed code and variable definitions. We do that for libxl itself, not sure about here. Could be enabled.

Or at least the initialisation of ao_how could be made a function. Perhaps

	static value Val_oahow(value aohow struct aohow *c_aohow)
	{
		if (aohow == Val_none) return NULL;

		c_aohow->etc
		return c_aohow
	}
Quoted 5 lines

then

	libxl_aohow aohow;

	libxl_do_foo(....
		Val_aohow(async, &aohow), ...);

Yes, we should really separate this out indeed. I'll do that now.

Rob

Quoted 39 lines
Quoted 1 line

+value stub_libxl_domain_wait_shutdown(value ctx, value domid) {

This is the thing I meant could you do in ocaml above...

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback)

Quoted 5 lines
tools/ocaml/libs/xl/xenlight_stubs.c +5 -0
+	CAMLparam2(ctx, domid);
+	int ret;
+	libxl_event *event;
+	libxl_evgen_domain_death *deathw;
+	ret = libxl_evenable_domain_death(CTX, Int_val(domid), 0,

&deathw);

Quoted 12 lines
tools/ocaml/libs/xl/xenlight_stubs.c +12 -0
+	if (ret)
+		failwith_xl(ret, "domain_wait_shutdown");
+
+	for (;;) {
+		ret = domain_wait_event(CTX, Int_val(domid), &event);
+		if (ret) {
+			libxl_evdisable_domain_death(CTX, deathw);
+			failwith_xl(ret, "domain_wait_shutdown");
+		}
+
+		switch (event->type) {
+		case LIBXL_EVENT_TYPE_DOMAIN_DEATH:

tools/ocaml/libs/xl/xenlight_stubs.c void async_callback(libxl_ctx *ctx, int rc, void *for_callback) · line 511

Quoted 10 lines
tools/ocaml/libs/xl/xenlight_stubs.c +10 -0
+			goto done;
+		case LIBXL_EVENT_TYPE_DOMAIN_SHUTDOWN:
+			goto done;
+		default:
+			break;
+		}
+		libxl_event_free(CTX, event);
+	}
+done:
+	libxl_event_free(CTX, event);

I don't know, but this might be clearer with

	the_right-type type = event->type;
	lbixl_event_free(...)

before the switch, which then becomes switch(type)?

Ian.

author
#60 lore raw

PATCH 25/28 libxl: ocaml: in send_debug_keys, clean up before raising exception

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/xenlight_stubs.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/ocaml/libs/xl/xenlight_stubs.c +2 -2
index 84e1ad1..0c45743 100644
@@ -967,11 +967,11 @@ value stub_xl_send_debug_keys(value ctx, value keys)
 	c_keys = dup_String_val(keys);
 
 	ret = libxl_send_debug_keys(CTX, c_keys);
+	free(c_keys);
+
 	if (ret != 0)
 		failwith_xl(ret, "send_debug_keys");
 
-	free(c_keys);
-
 	CAMLreturn(Val_unit);
 }
 
Signature · 1 line
1.7.10.4
Acked-by on v1 25/28
#61 lore raw

Re: [PATCH v3-RESEND 25/28] libxl: ocaml: in send_debug_keys, clean up before raising exception

Quoted 1 line · Rob Hoes

I must confess I wasn't paying attention to this sort of issue in the previous patches I looked at. I presume you have cast your eye over them as part of making this fix?

author
#62 lore raw

Re: [PATCH v3-RESEND 25/28] libxl: ocaml: in send_debug_keys, clean up before raising exception

Quoted 8 lines · Rob Hoes
Quoted 1 line

I must confess I wasn't paying attention to this sort of issue in the previous patches I looked at. I presume you have cast your eye over them as part of making this fix?

I did indeed! This was one of the issues that came up in the first version of the series.

Cheers,
Rob

author
#63 lore raw

PATCH 26/28 libxl: ocaml: provide defaults for libxl types

In reply to Rob Hoes

Libxl functions such as libxl_domain_create_new take large structs of configuration parameters. Often, we would like to use the default values for many of these parameters.

The struct and keyed-union types in libxl have init functions, which fill in the defaults for a given type. This commit provides an OCaml interface to obtain records of defaults by calling the relevant init function.

These default records can be used as a base to construct your own records, and to selectively override parameters where needed.

For example, a Domain_create_info record can now be created as follows:

  Xenlight.Domain_create_info.({ default ctx () with
    ty = Xenlight.DOMAIN_TYPE_PV;
    name = Some vm_name;
    uuid = vm_uuid;
  })

For types with KeyedUnion fields, such as Domain_build_info, a record with defaults is obtained by specifying the type key:

Xenlight.Domain_build_info.default ctx ~ty:Xenlight.DOMAIN_TYPE_HVM ()

---
 tools/ocaml/libs/xl/genwrap.py |   61 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 7 deletions(-)
tools/ocaml/libs/xl/genwrap.py +54 -7
index 192bec2..64e509b 100644
@@ -112,6 +112,7 @@ def gen_struct(ty):
 
 def gen_ocaml_keyedunions(ty, interface, indent, parent = None):
     s = ""
+    union_type = ""
     
     if ty.rawname is not None:
         # Non-anonymous types need no special handling
@@ -151,9 +152,11 @@ def gen_ocaml_keyedunions(ty, interface, indent, parent = None):
         s += " | ".join(u) + "\n"
         ty.union_name = name
 
+        union_type = "?%s:%s" % (munge_name(nparent), ty.keyvar.type.rawname)
+
     if s == "":
-        return None
-    return s.replace("\n", "\n%s" % indent)
+        return None, None
+    return s.replace("\n", "\n%s" % indent), union_type
 
 def gen_ocaml_ml(ty, interface, indent=""):
 
@@ -190,17 +193,27 @@ def gen_ocaml_ml(ty, interface, indent=""):
                 s += "module %s = struct\n" % module_name
                 
         # Handle KeyedUnions...
+        union_types = []
         for f in ty.fields:
-            ku = gen_ocaml_keyedunions(f.type, interface, "\t")
+            ku, union_type = gen_ocaml_keyedunions(f.type, interface, "\t")
             if ku is not None:
                 s += ku
                 s += "\n"
+            if union_type is not None:
+                union_types.append(union_type)
 
         s += "\ttype t =\n"
         s += "\t{\n"
         s += gen_struct(ty)
         s += "\t}\n"
-        
+
+        if ty.init_fn is not None:
+            union_args = "".join([u + " -> " for u in union_types])
+            if interface:
+                s += "\tval default : ctx -> %sunit -> t\n" % union_args
+            else:
+                s += "\texternal default : ctx -> %sunit -> t = \"stub_libxl_%s_init\"\n" % (union_args, ty.rawname)
+
         if functions.has_key(ty.rawname):
             for name,args in functions[ty.rawname]:
                 s += "\texternal %s : " % name
@@ -428,6 +441,38 @@ def gen_c_stub_prototype(ty, fns):
         s += ");\n"
     return s
 
+def gen_c_default(ty):
+    s = "/* Get the defaults for %s */\n" % ty.rawname
+    # Handle KeyedUnions...
+    union_types = []
+    for f in ty.fields:
+        if isinstance(f.type, idl.KeyedUnion):
+            union_types.append(f.type.keyvar)
+
+    s += "value stub_libxl_%s_init(value ctx, %svalue unit)\n" % (ty.rawname,
+        "".join(["value " + u.name + ", " for u in union_types]))
+    s += "{\n"
+    s += "\tCAMLparam%d(ctx, %sunit);\n" % (len(union_types) + 2, "".join([u.name + ", " for u in union_types]))
+    s += "\tCAMLlocal1(val);\n"
+    s += "\tlibxl_%s c_val;\n" % ty.rawname
+    s += "\tlibxl_%s_init(&c_val);\n" % ty.rawname
+    for u in union_types:
+        s += "\tif (%s != Val_none) {\n" % u.name
+        s += "\t\t%s c = 0;\n" % u.type.typename
+        s += "\t\t%s_val(CTX, &c, Some_val(%s));\n" % (u.type.rawname, u.name)
+        s += "\t\tlibxl_%s_init_%s(&c_val, c);\n" % (ty.rawname, u.name)
+        s += "\t}\n"
+    s += "\tval = Val_%s(&c_val);\n" % ty.rawname
+    if ty.dispose_fn:
+        s += "\tlibxl_%s_dispose(&c_val);\n" % ty.rawname
+    s += "\tCAMLreturn(val);\n"
+    s += "}\n"
+    return s
+
+def gen_c_defaults(ty):
+    s = gen_c_default(ty)
+    return s
+
 def autogen_header(open_comment, close_comment):
     s = open_comment + " AUTO-GENERATED FILE DO NOT EDIT " + close_comment + "\n"
     s += open_comment + " autogenerated by \n"
@@ -480,12 +525,14 @@ if __name__ == '__main__':
         if ty.marshal_in():
             cinc.write(gen_c_val(ty))
             cinc.write("\n")
-        if ty.marshal_out():
-            cinc.write(gen_Val_ocaml(ty))
-            cinc.write("\n")
+        cinc.write(gen_Val_ocaml(ty))
+        cinc.write("\n")
         if functions.has_key(ty.rawname):
             cinc.write(gen_c_stub_prototype(ty, functions[ty.rawname]))
             cinc.write("\n")
+        if ty.init_fn is not None:
+            cinc.write(gen_c_defaults(ty))
+            cinc.write("\n")
         #sys.stdout.write("\n")
     
     ml.write("(* END OF AUTO-GENERATED CODE *)\n")
Signature · 1 line
1.7.10.4
Acked-by on v1 26/28
#64 lore raw

Re: [PATCH v3-RESEND 26/28] libxl: ocaml: provide defaults for libxl types

Quoted 20 lines · Rob Hoes

Libxl functions such as libxl_domain_create_new take large structs of configuration parameters. Often, we would like to use the default values for many of these parameters.

The struct and keyed-union types in libxl have init functions, which fill in the defaults for a given type. This commit provides an OCaml interface to obtain records of defaults by calling the relevant init function.

These default records can be used as a base to construct your own records, and to selectively override parameters where needed.

For example, a Domain_create_info record can now be created as follows:

  Xenlight.Domain_create_info.({ default ctx () with
    ty = Xenlight.DOMAIN_TYPE_PV;
    name = Some vm_name;
    uuid = vm_uuid;
  })
Quoted 6 lines

For types with KeyedUnion fields, such as Domain_build_info, a record with defaults is obtained by specifying the type key:

Xenlight.Domain_build_info.default ctx ~ty:Xenlight.DOMAIN_TYPE_HVM ()

I have a feeling some of this could be done better, but that's my fault for laying such crappy groundwork, sorry!

author
#65 lore raw

PATCH 27/28 libxl: ocaml: use CAMLlocal1 macro rather than value-type in auto-generated C-code

In reply to Rob Hoes
---
 tools/ocaml/libs/xl/genwrap.py |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/ocaml/libs/xl/genwrap.py +2 -2
index 64e509b..3d5e386 100644
@@ -347,7 +347,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
     elif isinstance(ty, idl.Array):
         s += "{\n"
         s += "\t    int i;\n"
-        s += "\t    value array_elem;\n"
+        s += "\t    CAMLlocal1(array_elem);\n"
         s += "\t    %s = caml_alloc(%s,0);\n" % (o, parent + ty.lenvar.name)
         s += "\t    for(i=0; i<%s; i++) {\n" % (parent + ty.lenvar.name)
         s += "\t        %s\n" % ocaml_Val(ty.elem_type, "array_elem", c + "[i]", "", parent=parent)
@@ -397,7 +397,7 @@ def ocaml_Val(ty, o, c, indent="", parent = None):
             fn = "anon_field"
         else:
             fn = "%s_field" % ty.rawname
-        s += "\tvalue %s;\n" % fn
+        s += "\tCAMLlocal1(%s);\n" % fn
         s += "\n"
         s += "\t%s = caml_alloc_tuple(%d);\n" % (o, len(ty.fields))
         
Signature · 1 line
1.7.10.4
author
#67 lore raw

PATCH 28/28 libxl: ocaml: add console reader functions

In reply to Rob Hoes

---
New in v3:
* Replacing the "add xen_console_read" patch, with the more light-weight line-by-line reader functions.

---
 tools/ocaml/libs/xl/xenlight.ml.in   |   10 ++++-
 tools/ocaml/libs/xl/xenlight.mli.in  |    7 ++++
 tools/ocaml/libs/xl/xenlight_stubs.c |   68 ++++++++++++++++++++++++++++++++++
 tools/ocaml/test/Makefile            |   12 ++++--
 tools/ocaml/test/dmesg.ml            |   18 +++++++++
 5 files changed, 111 insertions(+), 4 deletions(-)
 create mode 100644 tools/ocaml/test/dmesg.ml
tools/ocaml/libs/xl/xenlight.ml.in +9 -1
index 8388c5f..6c95f14 100644
@@ -50,6 +50,13 @@ module Domain = struct
 end
 
 module Host = struct
+	type console_reader
+	exception End_of_file
+
+	external xen_console_read_start : ctx -> int -> console_reader  = "stub_libxl_xen_console_read_start"
+	external xen_console_read_line : ctx -> console_reader -> string = "stub_libxl_xen_console_read_line"
+	external xen_console_read_finish : ctx -> console_reader -> unit = "stub_libxl_xen_console_read_finish"
+
 	external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 end
 
@@ -112,5 +119,6 @@ module Async = functor (S: EVENT_USERS) -> struct
 end
 
 let register_exceptions () =
-	Callback.register_exception "Xenlight.Error" (Error(ERROR_FAIL, ""))
+	Callback.register_exception "Xenlight.Error" (Error(ERROR_FAIL, ""));
+	Callback.register_exception "Xenlight.Host.End_of_file" (Host.End_of_file)
 
tools/ocaml/libs/xl/xenlight.mli.in +7 -0
index 31faf26..e489d19 100644
@@ -52,6 +52,13 @@ module Domain : sig
 end
 
 module Host : sig
+	type console_reader
+	exception End_of_file
+
+	external xen_console_read_start : ctx -> int -> console_reader  = "stub_libxl_xen_console_read_start"
+	external xen_console_read_line : ctx -> console_reader -> string = "stub_libxl_xen_console_read_line"
+	external xen_console_read_finish : ctx -> console_reader -> unit = "stub_libxl_xen_console_read_finish"
+
 	external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 end
 
tools/ocaml/libs/xl/xenlight_stubs.c +68 -0

74 lines not on this page · raw message

tools/ocaml/test/Makefile +9 -3
index 8387d43..e6ba865 100644
@@ -9,9 +9,9 @@ OCAMLINCLUDE += \
 	-I $(OCAML_TOPLEVEL)/libs/xentoollog \
 	-I $(OCAML_TOPLEVEL)/libs/xl
 
-OBJS = xtl send_debug_keys list_domains raise_exception
+OBJS = xtl send_debug_keys list_domains raise_exception dmesg
 
-PROGRAMS = xtl send_debug_keys list_domains raise_exception
+PROGRAMS = xtl send_debug_keys list_domains raise_exception dmesg
 
 xtl_LIBS =  \
 	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
@@ -37,7 +37,13 @@ raise_exception_LIBS =  \
 
 raise_exception_OBJS = raise_exception
 
-OCAML_PROGRAM = xtl send_debug_keys list_domains raise_exception
+dmesg_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xl $(OCAML_TOPLEVEL)/libs/xl/xenlight.cmxa
+
+dmesg_OBJS = xtl dmesg
+
+OCAML_PROGRAM = xtl send_debug_keys list_domains raise_exception dmesg
 
 all: $(PROGRAMS)
 
tools/ocaml/test/dmesg.ml new file +18 -0
new file mode 100644
index 0000000..864fac4
@@ -0,0 +1,18 @@
+open Printf
+
+let _ =
+	Xenlight.register_exceptions ();
+	let logger = Xtl.create_stdio_logger ~level:Xentoollog.Debug () in
+	let ctx = Xenlight.ctx_alloc logger in
+
+	let open Xenlight.Host in
+	let reader = xen_console_read_start ctx 0 in
+	(try
+		while true do
+			let line = xen_console_read_line ctx reader in
+			print_string line
+		done
+	with End_of_file -> ());
+	let _ = xen_console_read_finish ctx reader in
+	()
+
Signature · 1 line
1.7.10.4
Acked-by on v1 28/28
#68 lore raw

Re: [PATCH v3-RESEND 28/28] libxl: ocaml: add console reader functions

Quoted 1 line · Rob Hoes

In so far as I have any clue at all what the right way to do these sorts of complex type ocaml bindings:

Quoted 192 lines

---
New in v3:
* Replacing the "add xen_console_read" patch, with the more light-weight line-by-line reader functions.

---
 tools/ocaml/libs/xl/xenlight.ml.in   |   10 ++++-
 tools/ocaml/libs/xl/xenlight.mli.in  |    7 ++++
 tools/ocaml/libs/xl/xenlight_stubs.c |   68 ++++++++++++++++++++++++++++++++++
 tools/ocaml/test/Makefile            |   12 ++++--
 tools/ocaml/test/dmesg.ml            |   18 +++++++++
 5 files changed, 111 insertions(+), 4 deletions(-)
 create mode 100644 tools/ocaml/test/dmesg.ml
tools/ocaml/libs/xl/xenlight.ml.in +9 -1
index 8388c5f..6c95f14 100644
@@ -50,6 +50,13 @@ module Domain = struct
 end
 
 module Host = struct
+	type console_reader
+	exception End_of_file
+
+	external xen_console_read_start : ctx -> int -> console_reader  = "stub_libxl_xen_console_read_start"
+	external xen_console_read_line : ctx -> console_reader -> string = "stub_libxl_xen_console_read_line"
+	external xen_console_read_finish : ctx -> console_reader -> unit = "stub_libxl_xen_console_read_finish"
+
 	external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 end
 
@@ -112,5 +119,6 @@ module Async = functor (S: EVENT_USERS) -> struct
 end
 
 let register_exceptions () =
-	Callback.register_exception "Xenlight.Error" (Error(ERROR_FAIL, ""))
+	Callback.register_exception "Xenlight.Error" (Error(ERROR_FAIL, ""));
+	Callback.register_exception "Xenlight.Host.End_of_file" (Host.End_of_file)
 
tools/ocaml/libs/xl/xenlight.mli.in +7 -0
index 31faf26..e489d19 100644
@@ -52,6 +52,13 @@ module Domain : sig
 end
 
 module Host : sig
+	type console_reader
+	exception End_of_file
+
+	external xen_console_read_start : ctx -> int -> console_reader  = "stub_libxl_xen_console_read_start"
+	external xen_console_read_line : ctx -> console_reader -> string = "stub_libxl_xen_console_read_line"
+	external xen_console_read_finish : ctx -> console_reader -> unit = "stub_libxl_xen_console_read_finish"
+
 	external send_debug_keys : ctx -> string -> unit = "stub_xl_send_debug_keys"
 end
 
tools/ocaml/libs/xl/xenlight_stubs.c +68 -0
index 0c45743..cab4ed4 100644
@@ -975,6 +975,74 @@ value stub_xl_send_debug_keys(value ctx, value keys)
 	CAMLreturn(Val_unit);
 }
 
+static struct custom_operations libxl_console_reader_custom_operations = {
+	"libxl_console_reader_custom_operations",
+	custom_finalize_default,
+	custom_compare_default,
+	custom_hash_default,
+	custom_serialize_default,
+	custom_deserialize_default
+};
+
+#define Console_reader_val(x)(*((libxl_xen_console_reader **) Data_custom_val(x)))
+
+value stub_libxl_xen_console_read_start(value ctx, value clear)
+{
+	CAMLparam2(ctx, clear);
+	CAMLlocal1(handle);
+	libxl_xen_console_reader *cr;
+
+	cr = libxl_xen_console_read_start(CTX, Int_val(clear));
+
+	handle = caml_alloc_custom(&libxl_console_reader_custom_operations, sizeof(cr), 0, 1);
+	Console_reader_val(handle) = cr;
+
+	CAMLreturn(handle);
+}
+
+static void raise_eof(void)
+{
+	static value *exc = NULL;
+
+	/* First time around, lookup by name */
+	if (!exc)
+		exc = caml_named_value("Xenlight.Host.End_of_file");
+
+	if (!exc)
+		caml_invalid_argument("Exception Xenlight.Host.End_of_file not initialized, please link xenlight.cma");
+
+	caml_raise_constant(*exc);
+}
+
+value stub_libxl_xen_console_read_line(value ctx, value reader)
+{
+	CAMLparam2(ctx, reader);
+	CAMLlocal1(line);
+	int ret;
+	char *c_line;
+	libxl_xen_console_reader *cr = (libxl_xen_console_reader *) Console_reader_val(reader);
+
+	ret = libxl_xen_console_read_line(CTX, cr, &c_line);
+
+	if (ret < 0)
+		failwith_xl(ret, "xen_console_read_line");
+	if (ret == 0)
+		raise_eof();
+
+	line = caml_copy_string(c_line);
+
+	CAMLreturn(line);
+}
+
+value stub_libxl_xen_console_read_finish(value ctx, value reader)
+{
+	CAMLparam2(ctx, reader);
+	libxl_xen_console_reader *cr = (libxl_xen_console_reader *) Console_reader_val(reader);
+
+	libxl_xen_console_read_finish(CTX, cr);
+
+	CAMLreturn(Val_unit);
+}
 
 /* Event handling */
 
tools/ocaml/test/Makefile +9 -3
index 8387d43..e6ba865 100644
@@ -9,9 +9,9 @@ OCAMLINCLUDE += \
 	-I $(OCAML_TOPLEVEL)/libs/xentoollog \
 	-I $(OCAML_TOPLEVEL)/libs/xl
 
-OBJS = xtl send_debug_keys list_domains raise_exception
+OBJS = xtl send_debug_keys list_domains raise_exception dmesg
 
-PROGRAMS = xtl send_debug_keys list_domains raise_exception
+PROGRAMS = xtl send_debug_keys list_domains raise_exception dmesg
 
 xtl_LIBS =  \
 	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
@@ -37,7 +37,13 @@ raise_exception_LIBS =  \
 
 raise_exception_OBJS = raise_exception
 
-OCAML_PROGRAM = xtl send_debug_keys list_domains raise_exception
+dmesg_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xl $(OCAML_TOPLEVEL)/libs/xl/xenlight.cmxa
+
+dmesg_OBJS = xtl dmesg
+
+OCAML_PROGRAM = xtl send_debug_keys list_domains raise_exception dmesg
 
 all: $(PROGRAMS)
 
tools/ocaml/test/dmesg.ml new file +18 -0
new file mode 100644
index 0000000..864fac4
@@ -0,0 +1,18 @@
+open Printf
+
+let _ =
+	Xenlight.register_exceptions ();
+	let logger = Xtl.create_stdio_logger ~level:Xentoollog.Debug () in
+	let ctx = Xenlight.ctx_alloc logger in
+
+	let open Xenlight.Host in
+	let reader = xen_console_read_start ctx 0 in
+	(try
+		while true do
+			let line = xen_console_read_line ctx reader in
+			print_string line
+		done
+	with End_of_file -> ());
+	let _ = xen_console_read_finish ctx reader in
+	()
+
Acked-by
#69 lore raw
In reply to Rob Hoes

Hi Rob,

Quoted 9 lines · Rob Hoes

The following series of patches fill in most of the gaps in the OCaml bindings to libxl, to make them useful for clients such as xapi/xenopsd (from XenServer). There are a number of bugfixes to the existing bindings as well. I have an experimental version of xenopsd that successfully uses the new bindings.

This is version 3 of this patch series to fix the OCaml binding to libxl. I believe I have addressed all the points raised by Ian Jackson's review of version 2. See the individual patches for detailed changes with respect to v2.

I'm happy with all the tools/ocaml patches:

Cheers,
Dave