Skip to contentSkip to conversation

More wrappers for xenlight Go package

PATCH Superseded Nick Rosbrook 2 participants 14 messages Apr 12, 2020 – Apr 23, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

Nick Rosbrook (4):
golang/xenlight: add NameToDomid and DomidToName util functions golang/xenlight: add DeviceNicAdd/Remove wrappers golang/xenlight: add DevicePciAdd/Remove wrappers golang/xenlight: add DeviceUsbdevAdd/Remove wrappers

 tools/golang/xenlight/xenlight.go | 125 ++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)
Signature · 1 line
2.17.1
#1 lore raw

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

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 | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/golang/xenlight/xenlight.go +23 -0
index 3f1b0baa0c..8492bcec4e 100644
@@ -20,6 +20,7 @@ package xenlight
 #cgo LDFLAGS: -lxenlight -lyajl -lxentoollog
 #include <stdlib.h>
 #include <libxl.h>
+#include <libxl_utils.h>
 */
 import "C"
 
@@ -124,6 +125,28 @@ func (ctx *Context) Close() error {
 
 type Domid uint32
 
+// NameToDomid returns the Domid for a domain, given its name, if it exists.
+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 ^Domid(0), Error(ret)
+	}
+
+	return Domid(domid), nil
+}
+
+// DomidToName returns the name for a domain, given its domid.
+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
#2 lore raw

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

Quoted 30 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.

---
tools/golang/xenlight/xenlight.go | 23 +++++++++++++++++++++++

1 file changed, 23 insertions(+)
tools/golang/xenlight/xenlight.go +1 -0
index 3f1b0baa0c..8492bcec4e 100644
@@ -20,6 +20,7 @@ package xenlight
 #cgo LDFLAGS: -lxenlight -lyajl -lxentoollog
 #include <stdlib.h>
 #include <libxl.h>
+#include <libxl_utils.h>
 */

import "C"

tools/golang/xenlight/xenlight.go +0 -0
@@ -124,6 +125,28 @@ func (ctx *Context) Close() error {
 

type Domid uint32

tools/golang/xenlight/xenlight.go func (ctx *Context) Close() error { · line 136

Quoted 9 lines
tools/golang/xenlight/xenlight.go +9 -0
+// NameToDomid returns the Domid for a domain, given its name, if it exists.
+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 ^Domid(0), Error(ret)

libxl.h defines INVALID_DOMID — do we want to define an exported constant with the same name and use that here? (Although part of me wonders if DOMID_INVALID would be a better option.)

tools/golang/xenlight/xenlight.go func (ctx *Context) Close() error { · line 148

Quoted 12 lines
tools/golang/xenlight/xenlight.go +12 -0
+	}
+
+	return Domid(domid), nil
+}
+
+// DomidToName returns the name for a domain, given its domid.
+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)
+}

It looks to me like if the domid doesn’t exist, libxl_domid_to_name() will return NULL; and then DomidToName will return “”. Is that what we want?

If so, it should probably be documented.

One thing that might be worth pointing out is that both of these functions are actually racy: There’s no guarantee that by the time libxl_domid_to_name() returns that the domain with that name has died, and another domain with a different name has re-used the same domid. That’s partly why Xen has the whole “domain reaper” system, like for Unix processes (which so far isn’t implementable yet with the golang wrappers). I think when we make our “vm” library, we’re going to want to try to come up with something like an object that makes it easy to avoid this sort of race.

But that’s a discussion for another day. :-) Everything else looks good, thanks.

-George

#3 lore raw

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

Quoted 1 line

libxl.h defines INVALID_DOMID — do we want to define an exported constant with the same name and use that here? (Although part of me wonders if DOMID_INVALID would be a better option.)

Yeah, that makes sense. I'll add that.

Quoted 15 lines

tools/golang/xenlight/xenlight.go func (ctx *Context) Close() error { · line 148

Quoted 12 lines
tools/golang/xenlight/xenlight.go +12 -0
+     }
+
+     return Domid(domid), nil
+}
+
+// DomidToName returns the name for a domain, given its domid.
+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)
+}

It looks to me like if the domid doesn’t exist, libxl_domid_to_name() will return NULL; and then DomidToName will return “”. Is that what we want?

Quoted 1 line

If so, it should probably be documented.

I considered returning an error if C.GoString(cname) == "". But, with these functions (as well as the others in these series), I opted to keep the signatures aligned with their libxl counterparts since we're keeping the package API mostly one-to-one with libxl. I can add a second return value if you prefer, otherwise I'll just add a note in the comment.

Quoted 1 line

One thing that might be worth pointing out is that both of these functions are actually racy: There’s no guarantee that by the time libxl_domid_to_name() returns that the domain with that name has died, and another domain with a different name has re-used the same domid. That’s partly why Xen has the whole “domain reaper” system, like for Unix processes (which so far isn’t implementable yet with the golang wrappers). I think when we make our “vm” library, we’re going to want to try to come up with something like an object that makes it easy to avoid this sort of race.

That's good to know, thanks. I'll add that to the comments as well.

Quoted 1 line

But that’s a discussion for another day. :-) Everything else looks good, thanks.

Thanks!

-NR

#4 lore raw

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

Quoted 23 lines · Nick Rosbrook
Quoted 2 lines

libxl.h defines INVALID_DOMID — do we want to define an exported constant with the same name and use that here? (Although part of me wonders if DOMID_INVALID would be a better option.)

Yeah, that makes sense. I'll add that.

Quoted 15 lines

tools/golang/xenlight/xenlight.go func (ctx *Context) Close() error { · line 148

Quoted 12 lines
tools/golang/xenlight/xenlight.go +12 -0
+     }
+
+     return Domid(domid), nil
+}
+
+// DomidToName returns the name for a domain, given its domid.
+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)
+}

It looks to me like if the domid doesn’t exist, libxl_domid_to_name() will return NULL; and then DomidToName will return “”. Is that what we want?

Quoted 8 lines
Quoted 1 line

If so, it should probably be documented.

I considered returning an error if C.GoString(cname) == "". But, with these functions (as well as the others in these series), I opted to keep the signatures aligned with their libxl counterparts since we're keeping the package API mostly one-to-one with libxl. I can add a second return value if you prefer, otherwise I'll just add a note in the comment.

OK — adding a note in the comment is fine. I mainly wanted to make sure the question had actually been considered (although of course documenting that behavior is also important).

Thanks,
-George

#5 lore raw

PATCH 2/4 golang/xenlight: add DeviceNicAdd/Remove wrappers

In reply to Nick Rosbrook

Add DeviceNicAdd and DeviceNicRemove as wrappers for libxl_device_nic_add and libxl_device_nic_remove.

---
 tools/golang/xenlight/xenlight.go | 34 +++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/golang/xenlight/xenlight.go +34 -0
index 8492bcec4e..a56f913b81 100644
@@ -1068,3 +1068,37 @@ func (Ctx *Context) PrimaryConsoleGetTty(domid uint32) (path string, err error)
 	path = C.GoString(cpath)
 	return
 }
+
+// DeviceNicAdd adds a nic to a domain.
+func (Ctx *Context) DeviceNicAdd(domid Domid, nic *DeviceNic) error {
+	var cnic C.libxl_device_nic
+
+	if err := nic.toC(&cnic); err != nil {
+		return err
+	}
+	defer C.libxl_device_nic_dispose(&cnic)
+
+	ret := C.libxl_device_nic_add(Ctx.ctx, C.uint32_t(domid), &cnic, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
+
+// DeviceNicRemove removes a nic from a domain.
+func (Ctx *Context) DeviceNicRemove(domid Domid, nic *DeviceNic) error {
+	var cnic C.libxl_device_nic
+
+	if err := nic.toC(&cnic); err != nil {
+		return err
+	}
+	defer C.libxl_device_nic_dispose(&cnic)
+
+	ret := C.libxl_device_nic_remove(Ctx.ctx, C.uint32_t(domid), &cnic, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
Signature · 1 line
2.17.1
Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] golang/xenlight: add DeviceNicAdd/Remove wrappers

Quoted 29 lines · Nick Rosbrook

Add DeviceNicAdd and DeviceNicRemove as wrappers for libxl_device_nic_add and libxl_device_nic_remove.

---
tools/golang/xenlight/xenlight.go | 34 +++++++++++++++++++++++++++++++

1 file changed, 34 insertions(+)
tools/golang/xenlight/xenlight.go +9 -0
index 8492bcec4e..a56f913b81 100644
@@ -1068,3 +1068,37 @@ func (Ctx *Context) PrimaryConsoleGetTty(domid uint32) (path string, err error)
 	path = C.GoString(cpath)
 	return
 }
+
+// DeviceNicAdd adds a nic to a domain.
+func (Ctx *Context) DeviceNicAdd(domid Domid, nic *DeviceNic) error {
+	var cnic C.libxl_device_nic
+
+	if err := nic.toC(&cnic); err != nil {
+		return err
+	}
+	defer C.libxl_device_nic_dispose(&cnic)

tools/golang/xenlight/xenlight.go func (Ctx *Context) PrimaryConsoleGetTty(domid uint32) (path string, err error) · line 1089

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+
+	ret := C.libxl_device_nic_add(Ctx.ctx, C.uint32_t(domid), &cnic, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
+
+// DeviceNicRemove removes a nic from a domain.

I feel like I want to say here what it is you actually have to fill in to remove the nic; but after 10 minutes of poking around the code, I’m not actually sure myself. :-) (I think it *might* be just Devid and BackendDomid.)

So I’ll give this for now:

And if I find it before I finish reviewing the end of the series, we can check it in and look at improving the documentation later.

-George

#7 lore raw

Re: [PATCH 2/4] golang/xenlight: add DeviceNicAdd/Remove wrappers

Quoted 1 line

I feel like I want to say here what it is you actually have to fill in to remove the nic; but after 10 minutes of poking around the code, I’m not actually sure myself. :-) (I think it *might* be just Devid and BackendDomid.)

IIRC, you can use just the MAC or devid. I was using just the MAC when I tested it out.

Quoted 5 lines

So I’ll give this for now:

And if I find it before I finish reviewing the end of the series, we can check it in and look at improving the documentation later.

Sounds good, thanks!

-NR

#8 lore raw

PATCH 3/4 golang/xenlight: add DevicePciAdd/Remove wrappers

In reply to Nick Rosbrook

Add DevicePciAdd and DevicePciRemove as wrappers for libxl_device_pci_add and libxl_device_pci remove.

---
 tools/golang/xenlight/xenlight.go | 34 +++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/golang/xenlight/xenlight.go +34 -0
index a56f913b81..c94b046667 100644
@@ -1102,3 +1102,37 @@ func (Ctx *Context) DeviceNicRemove(domid Domid, nic *DeviceNic) error {
 
 	return nil
 }
+
+// DevicePciAdd is used to passthrough a PCI device to a domain.
+func (Ctx *Context) DevicePciAdd(domid Domid, pci *DevicePci) error {
+	var cpci C.libxl_device_pci
+
+	if err := pci.toC(&cpci); err != nil {
+		return err
+	}
+	defer C.libxl_device_pci_dispose(&cpci)
+
+	ret := C.libxl_device_pci_add(Ctx.ctx, C.uint32_t(domid), &cpci, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
+
+// DevicePciRemove removes a PCI device from a domain.
+func (Ctx *Context) DevicePciRemove(domid Domid, pci *DevicePci) error {
+	var cpci C.libxl_device_pci
+
+	if err := pci.toC(&cpci); err != nil {
+		return err
+	}
+	defer C.libxl_device_pci_dispose(&cpci)
+
+	ret := C.libxl_device_pci_remove(Ctx.ctx, C.uint32_t(domid), &cpci, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
Signature · 1 line
2.17.1
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] golang/xenlight: add DevicePciAdd/Remove wrappers

Quoted 8 lines · Nick Rosbrook

Add DevicePciAdd and DevicePciRemove as wrappers for libxl_device_pci_add and libxl_device_pci remove.

For 4.14, we should really look at adding functions to the IDL so that all this can be auto-generated.

#10 lore raw

Re: [PATCH 3/4] golang/xenlight: add DevicePciAdd/Remove wrappers

Quoted 11 lines · George Dunlap
Quoted 9 lines · Nick Rosbrook

Add DevicePciAdd and DevicePciRemove as wrappers for libxl_device_pci_add and libxl_device_pci remove.

For 4.14, we should really look at adding functions to the IDL so that all this can be auto-generated.

Agreed, there is obvious repetition here.

Quoted 1 line

Thanks!

-NR

#11 lore raw

PATCH 4/4 golang/xenlight: add DeviceUsbdevAdd/Remove wrappers

In reply to Nick Rosbrook

Add DeviceUsbdevAdd and DeviceUsbdevRemove as wrappers for libxl_device_usbdev_add and libxl_device_usbdev_remove.

---
 tools/golang/xenlight/xenlight.go | 34 +++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
tools/golang/xenlight/xenlight.go +34 -0
index c94b046667..2d45d2bd48 100644
@@ -1136,3 +1136,37 @@ func (Ctx *Context) DevicePciRemove(domid Domid, pci *DevicePci) error {
 
 	return nil
 }
+
+// DeviceUsbdevAdd adds a USB device to a domain.
+func (Ctx *Context) DeviceUsbdevAdd(domid Domid, usbdev *DeviceUsbdev) error {
+	var cusbdev C.libxl_device_usbdev
+
+	if err := usbdev.toC(&cusbdev); err != nil {
+		return err
+	}
+	defer C.libxl_device_usbdev_dispose(&cusbdev)
+
+	ret := C.libxl_device_usbdev_add(Ctx.ctx, C.uint32_t(domid), &cusbdev, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
+
+// DeviceUsbdevRemove removes a USB device from a domain.
+func (Ctx *Context) DeviceUsbdevRemove(domid Domid, usbdev *DeviceUsbdev) error {
+	var cusbdev C.libxl_device_usbdev
+
+	if err := usbdev.toC(&cusbdev); err != nil {
+		return err
+	}
+	defer C.libxl_device_usbdev_dispose(&cusbdev)
+
+	ret := C.libxl_device_usbdev_remove(Ctx.ctx, C.uint32_t(domid), &cusbdev, nil)
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
Signature · 1 line
2.17.1
#13 lore raw
In reply to Nick Rosbrook
Quoted 11 lines · Nick Rosbrook

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].

Quoted 7 lines

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

Nick Rosbrook (4):
golang/xenlight: add NameToDomid and DomidToName util functions golang/xenlight: add DeviceNicAdd/Remove wrappers golang/xenlight: add DevicePciAdd/Remove wrappers golang/xenlight: add DeviceUsbdevAdd/Remove wrappers

FYI I’ve pushed the last three to staging.

-George