Skip to contentSkip to conversation

More wrappers for xenlight Go package

PATCH In review Nick Rosbrook 2 participants 3 messages May 13, 2020

lore
Share & tools
lore
#1 lore raw

PATCH v3 1/1 golang/xenlight: add NameToDomid and DomidToName util functions

In reply to Nick Rosbrook

Many exported functions in xenlight require a domid as an argument. Make it easier for package users to use these functions by adding wrappers for the libxl utility functions libxl_name_to_domid and libxl_domid_to_name.

---
 tools/golang/xenlight/xenlight.go | 40 ++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
tools/golang/xenlight/xenlight.go +39 -1
index 6b4f492550..742e5e11f1 100644
@@ -21,13 +21,15 @@ package xenlight
 #cgo LDFLAGS: -lxenlight -lyajl -lxentoollog
 #include <stdlib.h>
 #include <libxl.h>
+#include <libxl_utils.h>
+
+#define INVALID_DOMID_TYPED ((uint32_t) INVALID_DOMID)
 
 static const libxl_childproc_hooks childproc_hooks = { .chldowner = libxl_sigchld_owner_mainloop };
 
 void xenlight_set_chldproc(libxl_ctx *ctx) {
 	libxl_childproc_setmode(ctx, &childproc_hooks, NULL);
 }
-
 */
 import "C"
 
@@ -75,6 +77,10 @@ var libxlErrors = map[Error]string{
 	ErrorFeatureRemoved:               "Feature removed",
 }
 
+const (
+	DomidInvalid Domid = Domid(C.INVALID_DOMID_TYPED)
+)
+
 func (e Error) Error() string {
 	if s, ok := libxlErrors[e]; ok {
 		return s
@@ -190,6 +196,38 @@ func (ctx *Context) Close() error {
 
 type Domid uint32
 
+// NameToDomid returns the Domid for a domain, given its name, if it exists.
+//
+// NameToDomid does not guarantee that the domid associated with name at
+// the time NameToDomid is called is the same as the domid associated with
+// name at the time NameToDomid returns.
+func (Ctx *Context) NameToDomid(name string) (Domid, error) {
+	var domid C.uint32_t
+
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	if ret := C.libxl_name_to_domid(Ctx.ctx, cname, &domid); ret != 0 {
+		return DomidInvalid, Error(ret)
+	}
+
+	return Domid(domid), nil
+}
+
+// DomidToName returns the name for a domain, given its domid. If there
+// is no domain with the given domid, DomidToName will return the empty
+// string.
+//
+// DomidToName does not guarantee that the name (if any) associated with domid
+// at the time DomidToName is called is the same as the name (if any) associated
+// with domid at the time DomidToName returns.
+func (Ctx *Context) DomidToName(domid Domid) string {
+	cname := C.libxl_domid_to_name(Ctx.ctx, C.uint32_t(domid))
+	defer C.free(unsafe.Pointer(cname))
+
+	return C.GoString(cname)
+}
+
 // Devid is a device ID.
 type Devid int
 
Signature · 1 line
2.17.1
#0 lore raw

v3 cover More wrappers for xenlight Go package

This series adds wrappers to the xenlight package for various libxl functions, which are now trivial to add with the generated types and marshaling helpers. In particular, these are functions that would allow redctl to begin making the transition to using the xenlight package. For reference, I have started an experimental branch where I am using these functions in redctl [1].

[1] https://gitlab.com/enr0n/redctl/-/blob/1bdf7b515654cc030e095f3a630a24530f930c00/internal/server/xenlight_xen_driver.go

Changes in v2:
- Define constant DomidInvalid in first patch for use in NameToDomid
- Add more detail to function comments

Changes in v3:
- Define INVALID_DOMID_TYPED in C preamble so that the Go const DomidInvalid is always in-sync with the libxl definition.

Nick Rosbrook (1):
golang/xenlight: add NameToDomid and DomidToName util functions

 tools/golang/xenlight/xenlight.go | 40 ++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
Reviewed-by on v3 1/1
#2 lore raw

Re: [PATCH v3 1/1] golang/xenlight: add NameToDomid and DomidToName util functions

In reply to Nick Rosbrook
Quoted 10 lines · Nick Rosbrook

Many exported functions in xenlight require a domid as an argument. Make it easier for package users to use these functions by adding wrappers for the libxl utility functions libxl_name_to_domid and libxl_domid_to_name.

Looks good, thanks!