From patchwork Mon Oct 7 15:13:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11177817 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 BA17C139A for ; Mon, 7 Oct 2019 15:16:28 +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 8A18A2054F for ; Mon, 7 Oct 2019 15:16:28 +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="Xwj2x5B+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8A18A2054F 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 1iHUj5-0007tP-VQ; Mon, 07 Oct 2019 15:15:07 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iHUj4-0007sJ-Q9 for xen-devel@lists.xenproject.org; Mon, 07 Oct 2019 15:15:06 +0000 X-Inumbo-ID: 0c9fa8d0-e915-11e9-9bee-bc764e2007e4 Received: from mail-qk1-x741.google.com (unknown [2607:f8b0:4864:20::741]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 0c9fa8d0-e915-11e9-9bee-bc764e2007e4; Mon, 07 Oct 2019 15:13:42 +0000 (UTC) Received: by mail-qk1-x741.google.com with SMTP id h126so12881434qke.10 for ; Mon, 07 Oct 2019 08:13:42 -0700 (PDT) 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=3NK2iVPX52TZ2Khs2H1T1nbwaCHGNOhE5c2CNQ/BWI0=; b=Xwj2x5B+naqLB2RKBf+gCGb9mlc+WInY34tbhmmatjTJO0zhWn+DAncF0vqzRpHpad Qn8JCG5coasPVk0AQS1lll2hV5loeRI/7L+W0erfYJfdu8Ku8fSJ4pA1uENl+oQpckzA 2BxTJkO4P6pyPJAXyEdhZ78rfLCUqAwBVbAEagac+WrO1t30iAvSLwHwg6oZoBNp11Le dLH58YNy4oP8fKOmxmk7FsrpkDROapXCuLqU8oiNYsgshqQ7BN5HvT+rB0LSBuS75wbO aX3jLDoheY+SjbsDdxNC/6uWigtEtp5QE6FLTTBDuxrAwPnLB+sYSRpDuDoxWKtmecuu tBcg== 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=3NK2iVPX52TZ2Khs2H1T1nbwaCHGNOhE5c2CNQ/BWI0=; b=qh48to/TG+f/AHtw4qxFlFJoJE7WgCsbdKVMQbc76TxNXmGBrAHl757GNuNOP9g6rt bBN6YHOZjl56dYCXoyBIeTwCs3Ki2OaCxJq3ArXLarKAPN58CKwdU+qSBHkIHHxIX2tk gAOc4WdkD0noLwy9cIXT+ZvGjLn2nmNH4TE7MnBKvkcX4p4KZIbJkbK+TdQW7m7VY3Qx B9PjxZNOXD56p1adhkcWkwcJgxLt0Irql+Ci9FhRi0Fg3y2+mc06m0T6H7GojblNn6gA 2Nj4hsY1fCiMHdhzLKeFlpwFQJhXdsaUq75fykOTHVW/lImL5TECkRtnqiHM9/eaeLaU 9r2Q== X-Gm-Message-State: APjAAAX2EoWRtExdOI4bPC7pbJUnp2oMeXIDuffnKmmjxBKonjq61T63 KH2a+3s6EIpqxpHQ7pFmxXCnEkYx X-Google-Smtp-Source: APXvYqyAJE9UBDD+dY61D1pjaJBDh5WNZMksJLdv9TnnA5YgGTfUSeV03794OafqA2Yg31RxkQt/cQ== X-Received: by 2002:a37:4e48:: with SMTP id c69mr23557837qkb.182.1570461221941; Mon, 07 Oct 2019 08:13:41 -0700 (PDT) Received: from five.crux.rad.ainfosec.com (209-217-208-226.northland.net. [209.217.208.226]) by smtp.googlemail.com with ESMTPSA id c185sm8354313qkg.74.2019.10.07.08.13.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 08:13:41 -0700 (PDT) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Mon, 7 Oct 2019 11:13:09 -0400 Message-Id: <8e98996ecacbd56d7802df346a0f500475bf79cc.1570456846.git.rosbrookn@ainfosec.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 22/24] 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 --- Cc: George Dunlap Cc: Ian Jackson Cc: Wei Liu 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 a8f933c75f..e540b5413d 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 */ @@ -299,11 +332,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 @@ -480,11 +508,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) @@ -508,16 +531,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) @@ -534,11 +552,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)) @@ -567,11 +580,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) @@ -583,11 +591,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) @@ -600,11 +603,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 @@ -622,11 +620,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) @@ -639,11 +632,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 @@ -661,11 +649,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)) @@ -680,11 +663,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) @@ -700,11 +678,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) @@ -720,11 +693,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) @@ -884,60 +852,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) @@ -949,11 +865,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) @@ -965,10 +876,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) @@ -980,10 +887,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) @@ -999,10 +902,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) @@ -1020,11 +919,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) @@ -1035,11 +929,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) @@ -1057,11 +946,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 { @@ -1072,11 +956,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 { @@ -1087,11 +966,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)) if ret != 0 { @@ -1102,11 +976,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)) if ret != 0 { @@ -1118,11 +987,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) @@ -1145,11 +1009,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 @@ -1180,11 +1039,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 { @@ -1200,11 +1054,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 {