Skip to contentSkip to conversation

golang/xenlight: Add libxl_utils support

PATCH Superseded Nicolas Belouin 3 participants 3 messages Jun 26, 2019 – Jun 27, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 golang/xenlight: Add libxl_utils support

The Go bindings for libxl miss functions from libxl_utils, lets start with the simple libxl_domid_to_name and its counterpart libxl_name_to_domid.

---
 tools/golang/xenlight/xenlight_utils.go | 61 +++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 tools/golang/xenlight/xenlight_utils.go
tools/golang/xenlight/xenlight_utils.go new file +61 -0
new file mode 100644
index 0000000000..ab7a585ec7
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 Nicolas Belouin, Gandi SAS
+ *
+ * This library 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 of the License.
+ *
+ * This library 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.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+package xenlight
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl -lxentoollog
+#include <stdlib.h>
+#include <libxl_utils.h>
+*/
+import "C"
+
+/*
+ * Other flags that may be needed at some point:
+ *  -lnl-route-3 -lnl-3
+ *
+ * To get back to static linking:
+ * #cgo LDFLAGS: -lxenlight -lyajl_s -lxengnttab -lxenstore -lxenguest -lxentoollog -lxenevtchn -lxenctrl -lxenforeignmemory -lxencall -lz -luuid -lutil
+ */
+
+import (
+	"unsafe"
+)
+
+//char* libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid);
+func (Ctx *Context) DomidToName(id Domid) (name string) {
+	cDomName := C.libxl_domid_to_name(Ctx.ctx, C.uint32_t(id))
+	name = C.GoString(cDomName)
+	return
+}
+
+//int libxl_name_to_domid(libxl_ct *ctx, const char *name, uint32_t *domid)
+func (Ctx *Context) NameToDomid(name string) (id Domid, err error) {
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	var cDomId C.uint32_t
+
+	ret := C.libxl_name_to_domid(Ctx.ctx, cname, &cDomId)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	id = Domid(cDomId)
+
+	return
+}
Signature · 7 lines
2.22.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

CC George

Quoted 80 lines · Nicolas Belouin

The Go bindings for libxl miss functions from libxl_utils, lets start with the simple libxl_domid_to_name and its counterpart libxl_name_to_domid.

---
 tools/golang/xenlight/xenlight_utils.go | 61 +++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 tools/golang/xenlight/xenlight_utils.go
tools/golang/xenlight/xenlight_utils.go new file +61 -0
new file mode 100644
index 0000000000..ab7a585ec7
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 Nicolas Belouin, Gandi SAS
+ *
+ * This library 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 of the License.
+ *
+ * This library 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.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+package xenlight
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl -lxentoollog
+#include <stdlib.h>
+#include <libxl_utils.h>
+*/
+import "C"
+
+/*
+ * Other flags that may be needed at some point:
+ *  -lnl-route-3 -lnl-3
+ *
+ * To get back to static linking:
+ * #cgo LDFLAGS: -lxenlight -lyajl_s -lxengnttab -lxenstore -lxenguest -lxentoollog -lxenevtchn -lxenctrl -lxenforeignmemory -lxencall -lz -luuid -lutil
+ */
+
+import (
+	"unsafe"
+)
+
+//char* libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid);
+func (Ctx *Context) DomidToName(id Domid) (name string) {
+	cDomName := C.libxl_domid_to_name(Ctx.ctx, C.uint32_t(id))
+	name = C.GoString(cDomName)
+	return
+}
+
+//int libxl_name_to_domid(libxl_ct *ctx, const char *name, uint32_t *domid)
+func (Ctx *Context) NameToDomid(name string) (id Domid, err error) {
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	var cDomId C.uint32_t
+
+	ret := C.libxl_name_to_domid(Ctx.ctx, cname, &cDomId)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	id = Domid(cDomId)
+
+	return
+}
Signature · 1 line
2.22.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Nicolas Belouin

Thanks for the patch! Looks like a good start; just a couple of comments:

Quoted 2 lines · Nicolas Belouin

The Go bindings for libxl miss functions from libxl_utils, lets start

let's

Quoted 5 lines

with the simple libxl_domid_to_name and its counterpart libxl_name_to_domid.

tools/golang/xenlight/xenlight_utils.go line 32

Quoted 9 lines
tools/golang/xenlight/xenlight_utils.go new file +9 -0
+import "C"
+
+/*
+ * Other flags that may be needed at some point:
+ *  -lnl-route-3 -lnl-3
+ *
+ * To get back to static linking:
+ * #cgo LDFLAGS: -lxenlight -lyajl_s -lxengnttab -lxenstore -lxenguest -lxentoollog -lxenevtchn -lxenctrl -lxenforeignmemory -lxencall -lz -luuid -lutil
+ */

I think we can drop this comment -- if anyone decides they want to go back to static linking, or to add those other flags, they can get them from xenlight.go.

tools/golang/xenlight/xenlight_utils.go line 40

Quoted 7 lines
tools/golang/xenlight/xenlight_utils.go new file +7 -0
+import (
+       "unsafe"
+)
+
+//char* libxl_domid_to_name(libxl_ctx *ctx, uint32_t domid);
+func (Ctx *Context) DomidToName(id Domid) (name string) {
+       cDomName := C.libxl_domid_to_name(Ctx.ctx, C.uint32_t(id))

libxl_domid_to_name() expects the caller to free() the string returned to it. Probably best if we defer C.free() here.

Everything else looks good, thanks!

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel