From patchwork Fri Nov 15 19:44:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11247057 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C59ED13BD for ; Fri, 15 Nov 2019 19:48:04 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 95A6120732 for ; Fri, 15 Nov 2019 19:48:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZIxmCdVx" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 95A6120732 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iVhYL-0002WS-P3; Fri, 15 Nov 2019 19:46:45 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iVhYK-0002VC-Ir for xen-devel@lists.xenproject.org; Fri, 15 Nov 2019 19:46:44 +0000 X-Inumbo-ID: 782813be-07e0-11ea-9631-bc764e2007e4 Received: from mail-qv1-xf42.google.com (unknown [2607:f8b0:4864:20::f42]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 782813be-07e0-11ea-9631-bc764e2007e4; Fri, 15 Nov 2019 19:45:25 +0000 (UTC) Received: by mail-qv1-xf42.google.com with SMTP id cv8so4073028qvb.3 for ; Fri, 15 Nov 2019 11:45:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4CdgW+17WzqpQebBvw4AhmE1ljHoYYYW3hRrcGWwnfs=; b=ZIxmCdVxOchoQ9RMYE/HhoEwVGqeMO8UnT3bTMnhzgXMEo/eCrlqp14cdB/s0EWvdk eEq0cHiP5nKsktbhKQlDX3JwB7mbgXdd7G90eLB7E2FhXOJSUOzLyMJaa9SXNIX6ccdZ rKou8lAnxo9FcoTov8nXH/9Jl6Biyg3rUGdL73KO/N1l+nuRSTtcZU0vK+ISRLAZuNaO SXmGSsdufnAi0GLzp2ljY04br99Vvvv7gHvgt0NVnenFvTEDRvWrM9h5Z2rm4Z7tNh1R mMhq8930idl1OrD+x7vurrH6g+wvWw+uO0o4Ne8QXk/9W+4o/UxbAVVvaQOzXXxwAdGQ szFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4CdgW+17WzqpQebBvw4AhmE1ljHoYYYW3hRrcGWwnfs=; b=YRES5Eo6puheO0mJ/5/wr4vTDyNfWWu8yZKiYReS0RwhtZUUI5Po+hM29bi/VP525O 0srYAWoY6vAm1jPcmJVhzAZCUEMDa86jAF7kP7fBOD8tT3+znVyeI6069/m4UyGez8DD Ui/+1QFURj/i9EAW7w5m5d8mzVMxjIU3Z0GSaVKu4nFQP8YR/hdYLE17sRGOE1JfGFAT YN/oacWExIh22NtWETgp03AgEgr32tOjtoTQggIVdTEEFORcBimELS/aPsgd7lzXwbhQ vXaytUB7ILNs9MSopESYOKy2TiA6P5zWPuJzc/2U9AkekmSNjlIs1Tvi79ODIk3SBEKA yNzQ== X-Gm-Message-State: APjAAAVnp9IKwh7NoO7E954v4ZF4rJQfGwGELHmgHPlERiMxJmviSn+N zTC694hK4/s6DgonGYaTdhFpgxmr X-Google-Smtp-Source: APXvYqxWOlN2p7/kn+7ISk7bptjCSTzbtdQ0UOr1jnLa83QJL5fHNEH/j9HBvsQydtfjw3G7QEP4Tw== X-Received: by 2002:a0c:fa87:: with SMTP id o7mr14796111qvn.209.1573847124986; Fri, 15 Nov 2019 11:45:24 -0800 (PST) Received: from five.crux.rad.ainfosec.com (209-217-208-226.northland.net. [209.217.208.226]) by smtp.googlemail.com with ESMTPSA id y10sm4649679qkb.55.2019.11.15.11.45.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 11:45:23 -0800 (PST) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Fri, 15 Nov 2019 14:44:28 -0500 Message-Id: X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2 21/22] golang/xenlight: revise use of Context type X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Nick Rosbrook , Ian Jackson , kerriganb@ainfosec.com, George Dunlap , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Nick Rosbrook Remove the exported global context variable, 'Ctx.' Generally, it is better to not export global variables for use through a Go package. However, there are some exceptions that can be found in the standard library. Add a NewContext function instead, and remove the Open, IsOpen, and CheckOpen functions as a result. Also, comment-out an ineffectual assignment to 'err' inside the function Context.CpupoolInfo so that compilation does not fail. Signed-off-by: Nick Rosbrook --- tools/golang/xenlight/xenlight.go | 219 +++++------------------------- 1 file changed, 34 insertions(+), 185 deletions(-) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 210a418c02..4e12cebac1 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -74,6 +74,39 @@ func (e Error) Error() string { return fmt.Sprintf("libxl error: %d", -e) } +// Context represents a libxl_ctx. +type Context struct { + ctx *C.libxl_ctx + logger *C.xentoollog_logger_stdiostream +} + +// NewContext returns a new Context. +func NewContext() (*Context, error) { + var ctx Context + + ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0) + + ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0, (*C.xentoollog_logger)(unsafe.Pointer(ctx.logger))) + if ret != 0 { + return nil, Error(ret) + } + + return &ctx, nil +} + +// Close closes the Context. +func (ctx *Context) Close() error { + ret := C.libxl_ctx_free(ctx.ctx) + ctx.ctx = nil + C.xtl_logger_destroy((*C.xentoollog_logger)(unsafe.Pointer(ctx.logger))) + + if ret != 0 { + return Error(ret) + } + + return nil +} + /* * Types: Builtins */ @@ -304,11 +337,6 @@ func (cpl CpuidPolicyList) toC() (C.libxl_cpuid_policy_list, error) { return ccpl, nil } -type Context struct { - ctx *C.libxl_ctx - logger *C.xentoollog_logger_stdiostream -} - // Hwcap represents a libxl_hwcap. type Hwcap [8]uint32 @@ -463,11 +491,6 @@ func SchedulerFromString(name string) (s Scheduler, err error) { // libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx*, int *nb_pool_out); // void libxl_cpupoolinfo_list_free(libxl_cpupoolinfo *list, int nb_pool); func (Ctx *Context) ListCpupool() (list []Cpupoolinfo) { - err := Ctx.CheckOpen() - if err != nil { - return - } - var nbPool C.int c_cpupool_list := C.libxl_list_cpupool(Ctx.ctx, &nbPool) @@ -491,16 +514,11 @@ func (Ctx *Context) ListCpupool() (list []Cpupoolinfo) { // int libxl_cpupool_info(libxl_ctx *ctx, libxl_cpupoolinfo *info, uint32_t poolid); func (Ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo) { - err := Ctx.CheckOpen() - if err != nil { - return - } - var c_cpupool C.libxl_cpupoolinfo ret := C.libxl_cpupool_info(Ctx.ctx, &c_cpupool, C.uint32_t(Poolid)) if ret != 0 { - err = Error(-ret) + //err = Error(-ret) return } defer C.libxl_cpupoolinfo_dispose(&c_cpupool) @@ -517,11 +535,6 @@ func (Ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo) { // FIXME: uuid // FIXME: Setting poolid func (Ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitmap) (err error, Poolid uint32) { - err = Ctx.CheckOpen() - if err != nil { - return - } - poolid := C.uint32_t(C.LIBXL_CPUPOOL_POOLID_ANY) name := C.CString(Name) defer C.free(unsafe.Pointer(name)) @@ -550,11 +563,6 @@ func (Ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitma // int libxl_cpupool_destroy(libxl_ctx *ctx, uint32_t poolid); func (Ctx *Context) CpupoolDestroy(Poolid uint32) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_cpupool_destroy(Ctx.ctx, C.uint32_t(Poolid)) if ret != 0 { err = Error(-ret) @@ -566,11 +574,6 @@ func (Ctx *Context) CpupoolDestroy(Poolid uint32) (err error) { // int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu); func (Ctx *Context) CpupoolCpuadd(Poolid uint32, Cpu int) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_cpupool_cpuadd(Ctx.ctx, C.uint32_t(Poolid), C.int(Cpu)) if ret != 0 { err = Error(-ret) @@ -583,11 +586,6 @@ func (Ctx *Context) CpupoolCpuadd(Poolid uint32, Cpu int) (err error) { // int libxl_cpupool_cpuadd_cpumap(libxl_ctx *ctx, uint32_t poolid, // const libxl_bitmap *cpumap); func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - cbm, err := Cpumap.toC() if err != nil { return @@ -605,11 +603,6 @@ func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error // int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu); func (Ctx *Context) CpupoolCpuremove(Poolid uint32, Cpu int) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_cpupool_cpuremove(Ctx.ctx, C.uint32_t(Poolid), C.int(Cpu)) if ret != 0 { err = Error(-ret) @@ -622,11 +615,6 @@ func (Ctx *Context) CpupoolCpuremove(Poolid uint32, Cpu int) (err error) { // int libxl_cpupool_cpuremove_cpumap(libxl_ctx *ctx, uint32_t poolid, // const libxl_bitmap *cpumap); func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - cbm, err := Cpumap.toC() if err != nil { return @@ -644,11 +632,6 @@ func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err er // int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid); func (Ctx *Context) CpupoolRename(Name string, Poolid uint32) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - name := C.CString(Name) defer C.free(unsafe.Pointer(name)) @@ -663,11 +646,6 @@ func (Ctx *Context) CpupoolRename(Name string, Poolid uint32) (err error) { // int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus); func (Ctx *Context) CpupoolCpuaddNode(Poolid uint32, Node int) (Cpus int, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ccpus := C.int(0) ret := C.libxl_cpupool_cpuadd_node(Ctx.ctx, C.uint32_t(Poolid), C.int(Node), &ccpus) @@ -683,11 +661,6 @@ func (Ctx *Context) CpupoolCpuaddNode(Poolid uint32, Node int) (Cpus int, err er // int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus); func (Ctx *Context) CpupoolCpuremoveNode(Poolid uint32, Node int) (Cpus int, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ccpus := C.int(0) ret := C.libxl_cpupool_cpuremove_node(Ctx.ctx, C.uint32_t(Poolid), C.int(Node), &ccpus) @@ -703,11 +676,6 @@ func (Ctx *Context) CpupoolCpuremoveNode(Poolid uint32, Node int) (Cpus int, err // int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid); func (Ctx *Context) CpupoolMovedomain(Poolid uint32, Id Domid) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_cpupool_movedomain(Ctx.ctx, C.uint32_t(Poolid), C.uint32_t(Id)) if ret != 0 { err = Error(-ret) @@ -867,60 +835,8 @@ func (bm Bitmap) String() (s string) { return } -/* - * Context - */ -var Ctx Context - -func (Ctx *Context) IsOpen() bool { - return Ctx.ctx != nil -} - -func (Ctx *Context) Open() (err error) { - if Ctx.ctx != nil { - return - } - - Ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0) - if Ctx.logger == nil { - err = fmt.Errorf("Cannot open stdiostream") - return - } - - ret := C.libxl_ctx_alloc(&Ctx.ctx, C.LIBXL_VERSION, - 0, (*C.xentoollog_logger)(unsafe.Pointer(Ctx.logger))) - - if ret != 0 { - err = Error(-ret) - } - return -} - -func (Ctx *Context) Close() (err error) { - ret := C.libxl_ctx_free(Ctx.ctx) - Ctx.ctx = nil - - if ret != 0 { - err = Error(-ret) - } - C.xtl_logger_destroy((*C.xentoollog_logger)(unsafe.Pointer(Ctx.logger))) - return -} - -func (Ctx *Context) CheckOpen() (err error) { - if Ctx.ctx == nil { - err = fmt.Errorf("Context not opened") - } - return -} - //int libxl_get_max_cpus(libxl_ctx *ctx); func (Ctx *Context) GetMaxCpus() (maxCpus int, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_get_max_cpus(Ctx.ctx) if ret < 0 { err = Error(-ret) @@ -932,11 +848,6 @@ func (Ctx *Context) GetMaxCpus() (maxCpus int, err error) { //int libxl_get_online_cpus(libxl_ctx *ctx); func (Ctx *Context) GetOnlineCpus() (onCpus int, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_get_online_cpus(Ctx.ctx) if ret < 0 { err = Error(-ret) @@ -948,10 +859,6 @@ func (Ctx *Context) GetOnlineCpus() (onCpus int, err error) { //int libxl_get_max_nodes(libxl_ctx *ctx); func (Ctx *Context) GetMaxNodes() (maxNodes int, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } ret := C.libxl_get_max_nodes(Ctx.ctx) if ret < 0 { err = Error(-ret) @@ -963,10 +870,6 @@ func (Ctx *Context) GetMaxNodes() (maxNodes int, err error) { //int libxl_get_free_memory(libxl_ctx *ctx, uint64_t *memkb); func (Ctx *Context) GetFreeMemory() (memkb uint64, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } var cmem C.uint64_t ret := C.libxl_get_free_memory(Ctx.ctx, &cmem) @@ -982,10 +885,6 @@ func (Ctx *Context) GetFreeMemory() (memkb uint64, err error) { //int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo) func (Ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } var cphys C.libxl_physinfo C.libxl_physinfo_init(&cphys) defer C.libxl_physinfo_dispose(&cphys) @@ -1003,11 +902,6 @@ func (Ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) { //const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx); func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - var cinfo *C.libxl_version_info cinfo = C.libxl_get_version_info(Ctx.ctx) @@ -1018,11 +912,6 @@ func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) { } func (Ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - var cdi C.libxl_dominfo C.libxl_dominfo_init(&cdi) defer C.libxl_dominfo_dispose(&cdi) @@ -1040,11 +929,6 @@ func (Ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) { } func (Ctx *Context) DomainUnpause(Id Domid) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_domain_unpause(Ctx.ctx, C.uint32_t(Id), nil) if ret != 0 { @@ -1055,11 +939,6 @@ func (Ctx *Context) DomainUnpause(Id Domid) (err error) { //int libxl_domain_pause(libxl_ctx *ctx, uint32_t domain); func (Ctx *Context) DomainPause(id Domid) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_domain_pause(Ctx.ctx, C.uint32_t(id), nil) if ret != 0 { @@ -1070,11 +949,6 @@ func (Ctx *Context) DomainPause(id Domid) (err error) { //int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid); func (Ctx *Context) DomainShutdown(id Domid) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_domain_shutdown(Ctx.ctx, C.uint32_t(id), nil) if ret != 0 { @@ -1085,11 +959,6 @@ func (Ctx *Context) DomainShutdown(id Domid) (err error) { //int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid); func (Ctx *Context) DomainReboot(id Domid) (err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - ret := C.libxl_domain_reboot(Ctx.ctx, C.uint32_t(id), nil) if ret != 0 { @@ -1101,11 +970,6 @@ func (Ctx *Context) DomainReboot(id Domid) (err error) { //libxl_dominfo * libxl_list_domain(libxl_ctx*, int *nb_domain_out); //void libxl_dominfo_list_free(libxl_dominfo *list, int nb_domain); func (Ctx *Context) ListDomain() (glist []Dominfo) { - err := Ctx.CheckOpen() - if err != nil { - return - } - var nbDomain C.int clist := C.libxl_list_domain(Ctx.ctx, &nbDomain) defer C.libxl_dominfo_list_free(clist, nbDomain) @@ -1128,11 +992,6 @@ func (Ctx *Context) ListDomain() (glist []Dominfo) { // int *nb_vcpu, int *nr_cpus_out); //void libxl_vcpuinfo_list_free(libxl_vcpuinfo *, int nr_vcpus); func (Ctx *Context) ListVcpu(id Domid) (glist []Vcpuinfo) { - err := Ctx.CheckOpen() - if err != nil { - return - } - var nbVcpu C.int var nrCpu C.int @@ -1163,11 +1022,6 @@ func (ct ConsoleType) String() (str string) { //int libxl_console_get_tty(libxl_ctx *ctx, uint32_t domid, int cons_num, //libxl_console_type type, char **path); func (Ctx *Context) ConsoleGetTty(id Domid, consNum int, conType ConsoleType) (path string, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - var cpath *C.char ret := C.libxl_console_get_tty(Ctx.ctx, C.uint32_t(id), C.int(consNum), C.libxl_console_type(conType), &cpath) if ret != 0 { @@ -1183,11 +1037,6 @@ func (Ctx *Context) ConsoleGetTty(id Domid, consNum int, conType ConsoleType) (p //int libxl_primary_console_get_tty(libxl_ctx *ctx, uint32_t domid_vm, // char **path); func (Ctx *Context) PrimaryConsoleGetTty(domid uint32) (path string, err error) { - err = Ctx.CheckOpen() - if err != nil { - return - } - var cpath *C.char ret := C.libxl_primary_console_get_tty(Ctx.ctx, C.uint32_t(domid), &cpath) if ret != 0 {