diff mbox series

[RFC,v2,6/7] libxl: implement device add/remove/destroy functions generation

Message ID 5986715fe1d677533b67c06e9561cd716716d46a.1614734296.git.rosbrookn@ainfosec.com (mailing list archive)
State New
Headers show
Series add function support to IDL | expand

Commit Message

Nick Rosbrook March 3, 2021, 1:46 a.m. UTC
Use the newly added function support in the IDL to generate the function
definitions for the device add, remove, and destroy functions. The
content of the generated functions is taken from the device fuction
macro framework in libxl_internal.h.

For now, the definitions are not actually written out to a .c file, but
are invoked to ensure there is no build regression introduced. A later
commit will replace the existing macros with this generated code.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
---
 tools/libs/light/gentypes.py | 91 +++++++++++++++++++++++++++++++++++-
 1 file changed, 90 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/libs/light/gentypes.py b/tools/libs/light/gentypes.py
index f9957b79a2..9f1856399a 100644
--- a/tools/libs/light/gentypes.py
+++ b/tools/libs/light/gentypes.py
@@ -584,6 +584,85 @@  def libxl_C_enum_from_string(ty, str, e, indent = "    "):
         s = indent + s
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
+def libxl_func_define_device_add(func):
+    s = ''
+
+    return_type = func.return_type.typename
+    if isinstance(func.return_type, idl.Enumeration):
+        return_type = idl.integer.typename
+
+    params = ', '.join([ ty.make_arg(name) for (name,ty) in func.params ])
+
+    s += '{0} {1}({2})\n'.format(return_type, func.name, params)
+    s += '{\n'
+    s += '\tAO_CREATE(ctx, domid, ao_how);\n'
+    s += '\tlibxl__ao_device *aodev;\n\n'
+    s += '\tGCNEW(aodev);\n'
+    s += '\tlibxl__prepare_ao_device(ao, aodev);\n'
+    s += '\taodev->action = LIBXL__DEVICE_ACTION_ADD;\n'
+    s += '\taodev->callback = device_addrm_aocomplete;\n'
+    s += '\taodev->update_json = true;\n'
+    s += '\tlibxl__{0}(egc, domid, type, aodev);\n\n'.format(func.rawname)
+    s += '\treturn AO_INPROGRESS;\n'
+    s += '}\n'
+
+    return s
+
+def libxl_func_define_device_remove_ext(func, action=None):
+    s = ''
+
+    flag = None
+    if action == 'remove':
+        flag = 'LIBXL__FORCE_AUTO'
+    elif action == 'destroy':
+        flag = 'LIBXL__FORCE_ON'
+    else:
+        raise Exception('Unsupported action %s' % action)
+
+    # This is used to formulate the function name libxl__device_from_<devtype>
+    devtype = func.device_param[1].rawname.replace('device_','')
+
+    remtype = 'generic'
+    if func.custom_remove is not None:
+        remtype = func.custom_remove
+
+    return_type = func.return_type.typename
+    if isinstance(func.return_type, idl.Enumeration):
+        return_type = idl.integer.typename
+
+    params = ', '.join([ ty.make_arg(name) for (name,ty) in func.params ])
+
+    s += '{0} {1}({2})\n'.format(return_type, func.name, params)
+    s += '{\n'
+    s += '\tAO_CREATE(ctx, domid, ao_how);\n'
+    s += '\tlibxl__device *device;\n'
+    s += '\tlibxl__ao_device *aodev;\n'
+    s += '\tint rc;\n'
+    s += '\n'
+    s += '\tGCNEW(device);\n'
+    s += '\trc = libxl__device_from_{0}(gc, domid, type, device);\n'.format(devtype)
+    s += '\tif (rc != 0) goto out;\n'
+    s += '\n'
+    s += '\tGCNEW(aodev);\n'
+    s += '\tlibxl__prepare_ao_device(ao, aodev);\n'
+    s += '\taodev->action = LIBXL__DEVICE_ACTION_REMOVE;\n'
+    s += '\taodev->dev = device;\n'
+    s += '\taodev->callback = device_addrm_aocomplete;\n'
+    s += '\taodev->force.flag = {0};\n'.format(flag)
+    s += '\tlibxl__initiate_device_{0}_remove(egc, aodev);\n'.format(remtype)
+    s += '\n'
+    s += 'out:\n'
+    s += '\tif (rc) return AO_CREATE_FAIL(rc);\n'
+    s += '\treturn AO_INPROGRESS;\n'
+    s += '}\n'
+
+    return s
+
+def libxl_func_define_device_remove(func):
+    return libxl_func_define_device_remove_ext(func, action='remove')
+
+def libxl_func_define_device_destroy(func):
+    return libxl_func_define_device_remove_ext(func, action='destroy')
 
 if __name__ == '__main__':
     if len(sys.argv) != 6:
@@ -592,7 +671,7 @@  if __name__ == '__main__':
 
     (_, idlname, header, header_private, header_json, impl) = sys.argv
 
-    (builtins,types,_) = idl.parse(idlname)
+    (builtins,types,funcs) = idl.parse(idlname)
 
     print("outputting libxl type definitions to %s" % header)
 
@@ -794,4 +873,14 @@  if __name__ == '__main__':
         f.write("}\n")
         f.write("\n")
 
+    for func in funcs:
+        if type(func) is idl.DeviceAddFunction:
+            _ = libxl_func_define_device_add(func)
+        elif type(func) is idl.DeviceRemoveFunction:
+            _ = libxl_func_define_device_remove(func)
+        elif type(func) is idl.DeviceDestroyFunction:
+            _ = libxl_func_define_device_destroy(func)
+        else:
+            raise Exception("Unexpected Function class %s" % type(func))
+
     f.close()