[libvirt] [PATCH go-xml] Add support for device RNG

Thomas Hipp thipp at suse.de
Tue Jul 4 08:16:07 UTC 2017


Add support for device RNG (random number generator), and add test code.

Signed-off-by: Thomas Hipp <thipp at suse.de>
---
 domain.go      | 34 +++++++++++++++++++++++
 domain_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)

diff --git a/domain.go b/domain.go
index f9e3a80..b4f56bf 100644
--- a/domain.go
+++ b/domain.go
@@ -184,6 +184,8 @@ type DomainInterfaceSource struct {
 	Path    string `xml:"path,attr,omitempty"`
 	Mode    string `xml:"mode,attr,omitempty"`
 	Port    uint   `xml:"port,attr,omitempty"`
+	Service string `xml:"service,attr,omitempty"`
+	Host    string `xml:"host,attr,omitempty"`
 }
 
 type DomainInterfaceTarget struct {
@@ -384,6 +386,25 @@ type DomainSound struct {
 	Address *DomainAddress    `xml:"address"`
 }
 
+type DomainRNGRate struct {
+	Bytes  uint `xml:"bytes,attr"`
+	Period uint `xml:"period,attr,omitempty"`
+}
+
+type DomainRNGBackend struct {
+	Backend string                  `xml:",chardata"`
+	Model   string                  `xml:"model,attr"`
+	Type    string                  `xml:"type,attr,omitempty"`
+	Sources []DomainInterfaceSource `xml:"source"`
+}
+
+type DomainRNG struct {
+	XMLName xml.Name          `xml:"rng"`
+	Model   string            `xml:"model,attr"`
+	Rate    *DomainRNGRate    `xml:"rate"`
+	Backend *DomainRNGBackend `xml:"backend"`
+}
+
 type DomainDeviceList struct {
 	Emulator    string             `xml:"emulator,omitempty"`
 	Controllers []DomainController `xml:"controller"`
@@ -398,6 +419,7 @@ type DomainDeviceList struct {
 	Channels    []DomainChannel    `xml:"channel"`
 	MemBalloon  *DomainMemBalloon  `xml:"memballoon"`
 	Sounds      []DomainSound      `xml:"sound"`
+	RNGs        []DomainRNG        `xml:"rng"`
 }
 
 type DomainMemory struct {
@@ -734,3 +756,15 @@ func (d *DomainSound) Marshal() (string, error) {
 	}
 	return string(doc), nil
 }
+
+func (d *DomainRNG) Unmarshal(doc string) error {
+	return xml.Unmarshal([]byte(doc), d)
+}
+
+func (d *DomainRNG) Marshal() (string, error) {
+	doc, err := xml.MarshalIndent(d, "", "  ")
+	if err != nil {
+		return "", err
+	}
+	return string(doc), nil
+}
diff --git a/domain_test.go b/domain_test.go
index 0ce908b..aac23fe 100644
--- a/domain_test.go
+++ b/domain_test.go
@@ -333,6 +333,30 @@ var domainTestData = []struct {
 						},
 					},
 				},
+				RNGs: []DomainRNG{
+					DomainRNG{
+						Model: "virtio",
+						Rate: &DomainRNGRate{
+							Period: 2000,
+							Bytes:  1234,
+						},
+						Backend: &DomainRNGBackend{
+							Model: "egd",
+							Type:  "udp",
+							Sources: []DomainInterfaceSource{
+								DomainInterfaceSource{
+									Mode:    "bind",
+									Service: "1234",
+								},
+								DomainInterfaceSource{
+									Mode:    "connect",
+									Host:    "1.2.3.4",
+									Service: "1234",
+								},
+							},
+						},
+					},
+				},
 			},
 		},
 		Expected: []string{
@@ -368,6 +392,13 @@ var domainTestData = []struct {
 			`      <codec type="duplex"></codec>`,
 			`      <address type="pci" domain="0" bus="0" slot="8" function="0"></address>`,
 			`    </sound>`,
+			`    <rng model="virtio">`,
+			`      <rate bytes="1234" period="2000"></rate>`,
+			`      <backend model="egd" type="udp">`,
+			`        <source mode="bind" service="1234"></source>`,
+			`        <source mode="connect" service="1234" host="1.2.3.4"></source>`,
+			`      </backend>`,
+			`    </rng>`,
 			`  </devices>`,
 			`</domain>`,
 		},
@@ -1334,6 +1365,60 @@ var domainTestData = []struct {
 			`</sound>`,
 		},
 	},
+	{
+		Object: &DomainRNG{
+			Model: "virtio",
+			Rate: &DomainRNGRate{
+				Period: 2000,
+				Bytes:  1234,
+			},
+			Backend: &DomainRNGBackend{
+				Backend: "/dev/random",
+				Model:   "random",
+			},
+		},
+
+		Expected: []string{
+			`<rng model="virtio">`,
+			`  <rate bytes="1234" period="2000"></rate>`,
+			`  <backend model="random">/dev/random</backend>`,
+			`</rng>`,
+		},
+	},
+	{
+		Object: &DomainRNG{
+			Model: "virtio",
+			Rate: &DomainRNGRate{
+				Period: 2000,
+				Bytes:  1234,
+			},
+			Backend: &DomainRNGBackend{
+				Model: "egd",
+				Type:  "udp",
+				Sources: []DomainInterfaceSource{
+					DomainInterfaceSource{
+						Mode:    "bind",
+						Service: "1234",
+					},
+					DomainInterfaceSource{
+						Mode:    "connect",
+						Host:    "1.2.3.4",
+						Service: "1234",
+					},
+				},
+			},
+		},
+
+		Expected: []string{
+			`<rng model="virtio">`,
+			`  <rate bytes="1234" period="2000"></rate>`,
+			`  <backend model="egd" type="udp">`,
+			`    <source mode="bind" service="1234"></source>`,
+			`    <source mode="connect" service="1234" host="1.2.3.4"></source>`,
+			`  </backend>`,
+			`</rng>`,
+		},
+	},
 }
 
 func TestDomain(t *testing.T) {
-- 
2.13.2




More information about the libvir-list mailing list