From nobody Sat Apr 27 09:50:12 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496058529949975.2594781670889; Mon, 29 May 2017 04:48:49 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5C25881236; Mon, 29 May 2017 11:48:46 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A954617AC0; Mon, 29 May 2017 11:48:45 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0B4B34BB7F; Mon, 29 May 2017 11:48:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v4T7wuAn027213 for ; Mon, 29 May 2017 03:58:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id 422A817580; Mon, 29 May 2017 07:58:56 +0000 (UTC) Received: from romwork.redhat.com (ovpn-120-26.rdu2.redhat.com [10.10.120.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E0BE1716F; Mon, 29 May 2017 07:58:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5C25881236 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 5C25881236 From: Vladik Romanovsky To: libvir-list@redhat.com Date: Mon, 29 May 2017 03:58:52 -0400 Message-Id: <20170529075852.27146-1-vromanso@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com X-Mailman-Approved-At: Mon, 29 May 2017 07:48:41 -0400 Cc: Vladik Romanovsky Subject: [libvirt] [PATCH go-xml] Add support for Node Device with basic testing. X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Mon, 29 May 2017 11:48:48 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- node_device.go | 277 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ node_device_test.go | 111 +++++++++++++++++++++ 2 files changed, 388 insertions(+) create mode 100644 node_device.go create mode 100644 node_device_test.go diff --git a/node_device.go b/node_device.go new file mode 100644 index 0000000..5375d32 --- /dev/null +++ b/node_device.go @@ -0,0 +1,277 @@ +/* + * This file is part of the libvirt-go-xml project + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + * + * Copyright (C) 2017 Red Hat, Inc. + * + */ + +package libvirtxml + +import ( + "encoding/xml" + "fmt" +) + +type NodeDevice struct { + Name string `xml:"name"` + Path string `xml:"path,omitempty"` + Parent string `xml:"parent,omitempty"` + Driver string `xml:"driver>name,omitempty"` + Capability *CapabilityType `xml:"capability"` +} + +type CapabilityType struct { + Type interface{} `xml:"type,attr"` +} + +type IDName struct { + ID string `xml:"id,attr"` + Name string `xml:",chardata"` +} + +type PciExpress struct { + Links []PciExpressLink `xml:"link"` +} + +type PciExpressLink struct { + Validity string `xml:"validity,attr,omitempty"` + Speed float64 `xml:"speed,attr,omitempty"` + Port int `xml:"port,attr,omitempty"` + Width int `xml:"width,attr,omitempty"` +} + +type IOMMUGroupType struct { + Number int `xml:"number,attr"` +} + +type NUMA struct { + Node int `xml:"node,attr"` +} + +type PciCapabilityType struct { + Domain int `xml:"domain,omitempty"` + Bus int `xml:"bus,omitempty"` + Slot int `xml:"slot,omitempty"` + Function int `xml:"function,omitempty"` + Product IDName `xml:"product,omitempty"` + Vendor IDName `xml:"vendor,omitempty"` + IommuGroup IOMMUGroupType `xml:"iommuGroup,omitempty"` + Numa NUMA `xml:"numa,omitempty"` + PciExpress PciExpress `xml:"pci-express,omitempty"` + Capabilities []PciCapability `xml:"capability,omitempty"` +} + +type PCIAddress struct { + Domain string `xml:"domain,attr"` + Bus string `xml:"bus,attr"` + Slot string `xml:"slot,attr"` + Function string `xml:"function,attr"` +} + +type PciCapability struct { + Type string `xml:"type,attr"` + Address []PCIAddress `xml:"address,omitempty"` + MaxCount int `xml:"maxCount,attr,omitempty"` +} + +type SystemHardware struct { + Vendor string `xml:"vendor"` + Version string `xml:"version"` + Serial string `xml:"serial"` + UUID string `xml:"uuid"` +} + +type SystemFirmware struct { + Vendor string `xml:"vendor"` + Version string `xml:"version"` + ReleaseData string `xml:"release_date"` +} + +type SystemCapabilityType struct { + Product string `xml:"product"` + Hardware SystemHardware `xml:"hardware"` + Firmware SystemFirmware `xml:"firmware"` +} + +type USBDeviceCapabilityType struct { + Bus int `xml:"bus"` + Device int `xml:"device"` + Product IDName `xml:"product,omitempty"` + Vendor IDName `xml:"vendor,omitempty"` +} + +type USBCapabilityType struct { + Number int `xml:"number"` + Class int `xml:"class"` + Subclass int `xml:"subclass"` + Protocol int `xml:"protocol"` + Description string `xml:"description,omitempty"` +} + +type NetOffloadFeatures struct { + Name string `xml:"number"` +} + +type NetLink struct { + State string `xml:"state,attr"` + Speed string `xml:"speed,attr,omitempty"` +} + +type NetCapability struct { + Type string `xml:"type,attr"` +} + +type NetCapabilityType struct { + Interface string `xml:"interface"` + Address string `xml:"address"` + Link NetLink `xml:"link"` + Features []NetOffloadFeatures `xml:"feature,omitempty"` + Capability NetCapability `xml:"capability"` +} + +type SCSIVportsOPS struct { + Vports int `xml:"vports,omitempty"` + MaxVports int `xml:"maxvports,,omitempty"` +} + +type SCSIFCHost struct { + WWNN string `xml:"wwnn,omitempty"` + WWPN string `xml:"wwpn,omitempty"` + FabricWWN string `xml:"fabric_wwn,omitempty"` +} + +type SCSIHostCapability struct { + VportsOPS SCSIVportsOPS `xml:"vports_ops"` + FCHost SCSIFCHost `xml:"fc_host"` +} + +type SCSIHostCapabilityType struct { + Host int `xml:"host"` + UniqueID int `xml:"unique_id"` + Capability SCSIHostCapability `xml:"capability"` +} + +type SCSICapabilityType struct { + Host int `xml:"host"` + Bus int `xml:"bus"` + Target int `xml:"target"` + Lun int `xml:"lun"` + Type string `xml:"type"` +} + +type StroageCap struct { + Type string `xml:"match,attr"` + MediaAvailable int `xml:"media_available,omitempty"` + MediaSize int `xml:"media_size,omitempty"` + MediaLable int `xml:"media_label,omitempty"` +} + +type StorageCapabilityType struct { + Block string `xml:"block"` + Bus string `xml:"bus"` + DriverType string `xml:"drive_type"` + Model string `xml:"model"` + Vendor string `xml:"vendor"` + Serial string `xml:"serial"` + Size int `xml:"size"` + Capatibility StroageCap `xml:"capability,omitempty"` +} + +type DRMCapabilityType struct { + Type string `xml:"type"` +} + +func (c *CapabilityType) UnmarshalXML(d *xml.Decoder, start xml.StartEleme= nt) error { + for _, attr :=3D range start.Attr { + fmt.Println(attr.Name.Local) + if attr.Name.Local =3D=3D "type" { + switch attr.Value { + case "pci": + var pciCaps PciCapabilityType + if err :=3D d.DecodeElement(&pciCaps, &start); err !=3D nil { + return err + } + c.Type =3D pciCaps + case "system": + var systemCaps SystemCapabilityType + if err :=3D d.DecodeElement(&systemCaps, &start); err !=3D nil { + return err + } + c.Type =3D systemCaps + case "usb_device": + var usbdevCaps USBDeviceCapabilityType + if err :=3D d.DecodeElement(&usbdevCaps, &start); err !=3D nil { + return err + } + c.Type =3D usbdevCaps + case "usb": + var usbCaps USBCapabilityType + if err :=3D d.DecodeElement(&usbCaps, &start); err !=3D nil { + return err + } + c.Type =3D usbCaps + case "net": + var netCaps NetCapabilityType + if err :=3D d.DecodeElement(&netCaps, &start); err !=3D nil { + return err + } + c.Type =3D netCaps + case "scsi_host": + var scsiHostCaps SCSIHostCapabilityType + if err :=3D d.DecodeElement(&scsiHostCaps, &start); err !=3D nil { + return err + } + c.Type =3D scsiHostCaps + case "scsi": + var scsiCaps SCSICapabilityType + if err :=3D d.DecodeElement(&scsiCaps, &start); err !=3D nil { + return err + } + c.Type =3D scsiCaps + case "storage": + var storageCaps StorageCapabilityType + if err :=3D d.DecodeElement(&storageCaps, &start); err !=3D nil { + return err + } + c.Type =3D storageCaps + case "drm": + var drmCaps DRMCapabilityType + if err :=3D d.DecodeElement(&drmCaps, &start); err !=3D nil { + return err + } + c.Type =3D drmCaps + } + } + } + return nil +} + +func (c *NodeDevice) Unmarshal(doc string) error { + return xml.Unmarshal([]byte(doc), c) +} + +func (c *NodeDevice) Marshal() (string, error) { + doc, err :=3D xml.MarshalIndent(c, "", " ") + if err !=3D nil { + return "", err + } + return string(doc), nil +} diff --git a/node_device_test.go b/node_device_test.go new file mode 100644 index 0000000..129956b --- /dev/null +++ b/node_device_test.go @@ -0,0 +1,111 @@ +/* + * This file is part of the libvirt-go-xml project + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + * + * Copyright (C) 2017 Red Hat, Inc. + * + */ + +package libvirtxml + +import ( + "reflect" + "strings" + "testing" +) + +var NodeDeviceTestData =3D []struct { + Object *NodeDevice + XML []string +}{ + { + Object: &NodeDevice{ + Name: "pci_0000_81_00_0", + Parent: "pci_0000_80_01_0", + Driver: "ixgbe", + Capability: &CapabilityType{ + Type: PciCapabilityType{ + Domain: 1, + Bus: 21, + Slot: 10, + Function: 50, + Product: IDName{ + ID: "0x1528", + Name: "Ethernet Controller 10-Gigabit X540-AT2", + }, + Vendor: IDName{ + ID: "0x8086", + Name: "Intel Corporation", + }, + IommuGroup: IOMMUGroupType{ + Number: 3, + }, + Numa: NUMA{ + Node: 1, + }, + Capabilities: []PciCapability{ + PciCapability{ + Type: "virt_functions", + MaxCount: 63, + }, + }, + }, + }, + }, + XML: []string{ + ``, + ` pci_0000_81_00_0`, + ` pci_0000_80_01_0`, + ` `, + ` ixgbe`, + ` `, + ` `, + ` 1`, + ` 21`, + ` 10`, + ` 50`, + ` Ethernet Controller 10-Gigabit X540-AT2`, + ` Intel Corporation`, + ` `, + ` `, + `
`, + ` `, + ` `, + ` `, + ``, + }, + }, +} + +func TestNodeDevice(t *testing.T) { + for _, test :=3D range NodeDeviceTestData { + xmlDoc :=3D strings.Join(test.XML, "\n") + nodeDevice :=3D NodeDevice{} + err :=3D nodeDevice.Unmarshal(xmlDoc) + if err !=3D nil { + t.Fatal(err) + } + + res :=3D reflect.DeepEqual(&nodeDevice, test.Object) + if !res { + t.Fatal("Bad NodeDevice object creation.") + } + } +} --=20 2.9.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list