Skip to contentSkip to conversation

add function support to IDL

RFC PATCH Superseded Nick Rosbrook 2 participants 7 messages Jul 27, 2020 – Aug 17, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover add function support to IDL

At a Xen Summit design session for the golang bindings (see [1]), we agreed that it would be beneficial to expand the libxl IDL with function support. In addition to benefiting libxl itself, this would allow other language bindings to easily generate function wrappers.

These RFC patches outline a potential strategy for accomplishing this goal. The first patch adds the Function and CtxFunction classes to libxl/idl.py, introducing the idea of functions to the IDL. The second patch adds a DeviceFunction class and adds some sample definitions to libxl/libxl_types.idl for example purposes.

[1] https://lists.xenproject.org/archives/html/xen-devel/2020-07/msg00964.html

Nick Rosbrook (2):
libxl: add Function class to IDL
libxl: prototype libxl_device_nic_add/remove with IDL

 tools/golang/xenlight/gengotypes.py |  2 +-
 tools/libxl/gentypes.py             |  2 +-
 tools/libxl/idl.py                  | 54 ++++++++++++++++++++++++++++-
 tools/libxl/libxl_types.idl         |  6 ++++
 4 files changed, 61 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
#4 lore raw

RFC PATCH 2/2 libxl: prototype libxl_device_nic_add/remove with IDL

Add a DeviceFunction class and describe prototypes for libxl_device_nic_add/remove in libxl_types.idl.

--
This is mostly to serve as an example of how the first patch would be used for function support in the IDL.

---
 tools/libxl/idl.py          | 8 ++++++++
 tools/libxl/libxl_types.idl | 6 ++++++
 2 files changed, 14 insertions(+)
tools/libxl/idl.py +8 -0
index 1839871f86..15085af8c7 100644
@@ -386,6 +386,14 @@ class CtxFunction(Function):
 
         Function.__init__(self, name, params, return_type, return_is_status)
 
+class DeviceFunction(CtxFunction):
+    """ A function that modifies a device. """
+    def __init__(self, name=None, device_param=None):
+        params = [ ("domid", uint32), device_param ]
+
+        CtxFunction.__init__(self, name=name, params=params, return_type=integer,
+                             return_is_status=True, is_asyncop=True)
+
 def parse(f):
     print("Parsing %s" % f, file=sys.stderr)
 
tools/libxl/libxl_types.idl +6 -0
index 9d3f05f399..22ba93ee4b 100644
@@ -769,6 +769,12 @@ libxl_device_nic = Struct("device_nic", [
     ("colo_checkpoint_port", string)
     ])
 
+libxl_device_nic_add = DeviceFunction("device_nic_add",
+    ("nic", libxl_device_nic))
+
+libxl_device_nic_remove = DeviceFunction("device_nic_remove",
+    ("nic", libxl_device_nic))
+
 libxl_device_pci = Struct("device_pci", [
     ("func",      uint8),
     ("dev",       uint8),
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 1/2 libxl: add Function class to IDL

In reply to Nick Rosbrook

Add a Function and CtxFunction classes to idl.py to allow generator scripts to generate wrappers which are repetitive and straight forward when doing so by hand. Examples of such functions are the device_add/remove functions.

To start, a Function has attributes for namespace, name, parameters, return type, and an indication if the return value should be interpreted as a status code. The CtxFunction class extends this by indicating that a libxl_ctx is a required parmeter, and can optionally be an async function.

Also, add logic to idl.parse to return the list of functions found in an IDL file. For now, have users of idl.py -- i.e. libxl/gentypes.py and golang/xenlight/gengotypes.py -- ignore the list of functions returned.

---
 tools/golang/xenlight/gengotypes.py |  2 +-
 tools/libxl/gentypes.py             |  2 +-
 tools/libxl/idl.py                  | 46 ++++++++++++++++++++++++++++-
 3 files changed, 47 insertions(+), 3 deletions(-)
tools/golang/xenlight/gengotypes.py +1 -1
index 557fecd07b..bd3663c9ea 100644
@@ -718,7 +718,7 @@ def xenlight_golang_fmt_name(name, exported = True):
 if __name__ == '__main__':
     idlname = sys.argv[1]
 
-    (builtins, types) = idl.parse(idlname)
+    (builtins, types, _) = idl.parse(idlname)
 
     for b in builtins:
         name = b.typename
tools/libxl/gentypes.py +1 -1
index 9a45e45acc..ac7306f3f7 100644
@@ -592,7 +592,7 @@ if __name__ == '__main__':
 
     (_, idlname, header, header_private, header_json, impl) = sys.argv
 
-    (builtins,types) = idl.parse(idlname)
+    (builtins,types,_) = idl.parse(idlname)
 
     print("outputting libxl type definitions to %s" % header)
 
tools/libxl/idl.py +45 -1

65 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC PATCH 1/2] libxl: add Function class to IDL

Quoted 18 lines · Nick Rosbrook

Add a Function and CtxFunction classes to idl.py to allow generator scripts to generate wrappers which are repetitive and straight forward when doing so by hand. Examples of such functions are the device_add/remove functions.

To start, a Function has attributes for namespace, name, parameters, return type, and an indication if the return value should be interpreted as a status code. The CtxFunction class extends this by indicating that a libxl_ctx is a required parmeter, and can optionally be an async function.

Also, add logic to idl.parse to return the list of functions found in an IDL file. For now, have users of idl.py -- i.e. libxl/gentypes.py and golang/xenlight/gengotypes.py -- ignore the list of functions returned.

---

tools/libxl/idl.py class OrderedDict(dict): · line 359

Quoted 10 lines
tools/libxl/idl.py +10 -0
+class Function(object):
+    """
+    A general description of a function signature.
+
+    Attributes:
+      name (str): name of the function, excluding namespace.
+      params (list of (str,Type)): list of function parameters.
+      return_type (Type): the Type (if any), returned by the function.
+      return_is_status (bool): Indicates that the return value should be
+                               interpreted as an error/status code.

Can we get away without return_is_status? Couldn't we try to have return_type=libxl_error to indicate that return is a kind of status?

tools/libxl/idl.py class OrderedDict(dict): · line 380

Quoted 8 lines
tools/libxl/idl.py +8 -0
+    """
+class CtxFunction(Function):
+    """
+    A function that requires a libxl_ctx.
+
+    Attributes:
+      is_asyncop (bool): indicates that the function accepts a
+                         libxl_asyncop_how parameter.

While CtxFunction can be a function that takes libxl_ctx as first parameter, I don't think is_asyncop can be used. We can't know if ao_how will be last or not. For some function, ao_how is second to last. So, I guess ao_how might need to be listed in params

What do you think?

Thanks,

Signature · 1 line
Anthony PERARD
#5 lore raw

Re: [RFC PATCH 2/2] libxl: prototype libxl_device_nic_add/remove with IDL

In reply to Nick Rosbrook
Quoted 17 lines · Nick Rosbrook

Add a DeviceFunction class and describe prototypes for libxl_device_nic_add/remove in libxl_types.idl.

--
This is mostly to serve as an example of how the first patch would be used for function support in the IDL.

---
 tools/libxl/idl.py          | 8 ++++++++
 tools/libxl/libxl_types.idl | 6 ++++++
 2 files changed, 14 insertions(+)
tools/libxl/idl.py +0 -0
index 1839871f86..15085af8c7 100644

tools/libxl/idl.py class CtxFunction(Function): · line 390

Quoted 5 lines
tools/libxl/idl.py +2 -0
@@ -386,6 +386,14 @@ class CtxFunction(Function):
 
         Function.__init__(self, name, params, return_type, return_is_status)
 
+class DeviceFunction(CtxFunction):
+    """ A function that modifies a device. """

I guess that meant to be used by all function generated with the C macro LIBXL_DEFINE_DEVICE_ADD() and LIBXL_DEFINE_DEVICE_REMOVE(), isn't it?

I wonder if if we could get away with the type of device ("nic") and the type of the parameter (libxl_device_nic) and have DeviceFunction been a generator for both add and remove functions (and destroy).

Also there are functions like libxl_devid_to_device_nic() aren't those of type DeviceFunction as well ? But they don't takes any ao_how.

There is also libxl_device_nic_list{,_free}, but it is to handle a list of libxl_device_*, so it could be kind of related to DeviceFunction, but not quite. But maybe I'm going to far :-).

tools/libxl/libxl_types.idl libxl_device_nic = Struct("device_nic", [ · line 777

Quoted 7 lines
tools/libxl/libxl_types.idl +6 -0
+libxl_device_nic_add = DeviceFunction("device_nic_add",
+    ("nic", libxl_device_nic))
+
+libxl_device_nic_remove = DeviceFunction("device_nic_remove",
+    ("nic", libxl_device_nic))
+

Thanks,

Signature · 1 line
Anthony PERARD
#3 lore raw

Re: [RFC PATCH 1/2] libxl: add Function class to IDL

In reply to Anthony PERARD
Quoted 19 lines · Anthony PERARD
Quoted 18 lines · Nick Rosbrook

Add a Function and CtxFunction classes to idl.py to allow generator scripts to generate wrappers which are repetitive and straight forward when doing so by hand. Examples of such functions are the device_add/remove functions.

To start, a Function has attributes for namespace, name, parameters, return type, and an indication if the return value should be interpreted as a status code. The CtxFunction class extends this by indicating that a libxl_ctx is a required parmeter, and can optionally be an async function.

Also, add logic to idl.parse to return the list of functions found in an IDL file. For now, have users of idl.py -- i.e. libxl/gentypes.py and golang/xenlight/gengotypes.py -- ignore the list of functions returned.

---

Quoted 14 lines

tools/libxl/idl.py class OrderedDict(dict): · line 359

Quoted 10 lines
tools/libxl/idl.py +10 -0
+class Function(object):
+    """
+    A general description of a function signature.
+
+    Attributes:
+      name (str): name of the function, excluding namespace.
+      params (list of (str,Type)): list of function parameters.
+      return_type (Type): the Type (if any), returned by the function.
+      return_is_status (bool): Indicates that the return value should be
+                               interpreted as an error/status code.

Can we get away without return_is_status? Couldn't we try to have return_type=libxl_error to indicate that return is a kind of status?

Yes, I think that is much better.

Quoted 14 lines

tools/libxl/idl.py class OrderedDict(dict): · line 380

Quoted 8 lines
tools/libxl/idl.py +8 -0
+    """
+class CtxFunction(Function):
+    """
+    A function that requires a libxl_ctx.
+
+    Attributes:
+      is_asyncop (bool): indicates that the function accepts a
+                         libxl_asyncop_how parameter.

While CtxFunction can be a function that takes libxl_ctx as first parameter, I don't think is_asyncop can be used. We can't know if ao_how will be last or not. For some function, ao_how is second to last. So, I guess ao_how might need to be listed in params

Quoted 1 line

What do you think?

That's a good point. Do you think it would make sense to add Builtin definitions to libxl_types.idl for libxl_asyncop_how, libxl_asyncprogress_how, etc.? That way the generation scripts could work with those types more easily. But, I guess since those definitions aren't known until parse time we couldn't use them in the DeviceFunction class definition (but maybe that's not a big deal).

Thank you for the feedback.

-NR

#6 lore raw

Re: [RFC PATCH 2/2] libxl: prototype libxl_device_nic_add/remove with IDL

In reply to Anthony PERARD
Quoted 19 lines · Anthony PERARD
Quoted 18 lines · Nick Rosbrook

Add a DeviceFunction class and describe prototypes for libxl_device_nic_add/remove in libxl_types.idl.

--
This is mostly to serve as an example of how the first patch would be used for function support in the IDL.

---
 tools/libxl/idl.py          | 8 ++++++++
 tools/libxl/libxl_types.idl | 6 ++++++
 2 files changed, 14 insertions(+)
tools/libxl/idl.py +0 -0
index 1839871f86..15085af8c7 100644
@@ -386,6 +386,14 @@ class CtxFunction(Function):
 
Quoted 7 lines

tools/libxl/idl.py class CtxFunction(Function): · line 390

Quoted 4 lines
tools/libxl/idl.py +2 -0
         Function.__init__(self, name, params, return_type, return_is_status)
 
+class DeviceFunction(CtxFunction):
+    """ A function that modifies a device. """

I guess that meant to be used by all function generated with the C macro LIBXL_DEFINE_DEVICE_ADD() and LIBXL_DEFINE_DEVICE_REMOVE(), isn't it?

Yes, I think this could be used in place of those macros.

Quoted 4 lines

I wonder if if we could get away with the type of device ("nic") and the type of the parameter (libxl_device_nic) and have DeviceFunction been a generator for both add and remove functions (and destroy).

We could do that, but I think for clarity it might be valuable to explicitly define each of them. Actually, as I look at this patch again I wonder if it would be better to define Device{Add,Remove,Destroy} class definitions?

Quoted 6 lines

Also there are functions like libxl_devid_to_device_nic() aren't those of type DeviceFunction as well ? But they don't takes any ao_how.

There is also libxl_device_nic_list{,_free}, but it is to handle a list of libxl_device_*, so it could be kind of related to DeviceFunction, but not quite. But maybe I'm going to far :-).

I think this gives another good reason to define more specific Device* classes, rather than a broad DeviceFunction class. What do you think?

Thanks,

-NR