9119d9
From d0fd9e1092939dd2975d41fdbf1df017c71695e5 Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <d0fd9e1092939dd2975d41fdbf1df017c71695e5@dist-git>
9119d9
From: Peter Krempa <pkrempa@redhat.com>
9119d9
Date: Mon, 24 Nov 2014 17:51:14 +0100
9119d9
Subject: [PATCH] conf: Add channel state for virtio channels to the XML
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1146944
9119d9
9119d9
To track state of virtio channels this patch adds a new output-only
9119d9
attribute called 'state' to the <target> element of virtio channels.
9119d9
9119d9
This will be later populated with the guest state of the channel.
9119d9
9119d9
(cherry picked from commit 24c25a68c21adc197325a6b43e69367c0085d1b2)
9119d9
9119d9
Conflicts:
9119d9
	src/conf/domain_conf.c - context: brace cleanup not backported
9119d9
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 docs/formatdomain.html.in                          |  9 ++++-
9119d9
 docs/schemas/domaincommon.rng                      |  8 ++++
9119d9
 src/conf/domain_conf.c                             | 32 +++++++++++++++-
9119d9
 src/conf/domain_conf.h                             | 12 ++++++
9119d9
 .../qemuxml2argv-channel-virtio-state.args         | 17 +++++++++
9119d9
 .../qemuxml2argv-channel-virtio-state.xml          | 42 +++++++++++++++++++++
9119d9
 tests/qemuxml2argvtest.c                           |  2 +
9119d9
 .../qemuxml2xmlout-channel-virtio-state-active.xml | 43 ++++++++++++++++++++++
9119d9
 ...emuxml2xmlout-channel-virtio-state-inactive.xml | 42 +++++++++++++++++++++
9119d9
 tests/qemuxml2xmltest.c                            |  1 +
9119d9
 10 files changed, 204 insertions(+), 4 deletions(-)
9119d9
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.args
9119d9
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.xml
9119d9
 create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-active.xml
9119d9
 create mode 100644 tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-inactive.xml
9119d9
9119d9
diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
9119d9
index e044eab..f8d5b31 100644
9119d9
--- a/docs/formatdomain.html.in
9119d9
+++ b/docs/formatdomain.html.in
9119d9
@@ -4896,7 +4896,7 @@ qemu-kvm -net nic,model=? /dev/null
9119d9
     </channel>
9119d9
     <channel type='unix'>
9119d9
       <source mode='bind' path='/var/lib/libvirt/qemu/f16x86_64.agent'/>
9119d9
-      <target type='virtio' name='org.qemu.guest_agent.0'/>
9119d9
+      <target type='virtio' name='org.qemu.guest_agent.0' state='connected'/>
9119d9
     </channel>
9119d9
     <channel type='spicevmc'>
9119d9
       <target type='virtio' name='com.redhat.spice.0'/>
9119d9
@@ -4935,7 +4935,12 @@ qemu-kvm -net nic,model=? /dev/null
9119d9
         This is very useful in case of a qemu guest agent, where users don't
9119d9
         usually care about the source path since it's libvirt who talks to
9119d9
         the guest agent. In case users want to utilize this feature, they should
9119d9
-        leave <source> element out.
9119d9
+        leave <source> element out. Since
9119d9
+        1.2.11 the active XML for a virtio channel may contain an optional
9119d9
+        state attribute that reflects whether a process in the
9119d9
+        guest is active on the channel. This is an output-only attribute.
9119d9
+        Possible values for the state attribute are
9119d9
+        connected and disconnected.
9119d9
       
9119d9
       
spicevmc
9119d9
       
Paravirtualized SPICE channel. The domain must also have a
9119d9
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
9119d9
index 7cb37c7..4e917b1 100644
9119d9
--- a/docs/schemas/domaincommon.rng
9119d9
+++ b/docs/schemas/domaincommon.rng
9119d9
@@ -3384,6 +3384,14 @@
9119d9
       <optional>
9119d9
         <attribute name="name"/>
9119d9
       </optional>
9119d9
+      <optional>
9119d9
+        <attribute name="state">
9119d9
+          <choice>
9119d9
+            <value>connected</value>
9119d9
+            <value>disconnected</value>
9119d9
+          </choice>
9119d9
+        </attribute>
9119d9
+      </optional>
9119d9
     </element>
9119d9
   </define>
9119d9
   <define name="channel">
9119d9
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
9119d9
index baf16dc..ef6e9cf 100644
9119d9
--- a/src/conf/domain_conf.c
9119d9
+++ b/src/conf/domain_conf.c
9119d9
@@ -407,6 +407,11 @@ VIR_ENUM_IMPL(virDomainNetInterfaceLinkState, VIR_DOMAIN_NET_INTERFACE_LINK_STAT
9119d9
               "up",
9119d9
               "down")
9119d9
 
9119d9
+VIR_ENUM_IMPL(virDomainChrDeviceState, VIR_DOMAIN_CHR_DEVICE_STATE_LAST,
9119d9
+              "default",
9119d9
+              "connected",
9119d9
+              "disconnected");
9119d9
+
9119d9
 VIR_ENUM_IMPL(virDomainChrSerialTarget,
9119d9
               VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_LAST,
9119d9
               "isa-serial",
9119d9
@@ -7735,13 +7740,15 @@ virDomainChrTargetTypeFromString(virDomainChrDefPtr def,
9119d9
 
9119d9
 static int
9119d9
 virDomainChrDefParseTargetXML(virDomainChrDefPtr def,
9119d9
-                              xmlNodePtr cur)
9119d9
+                              xmlNodePtr cur,
9119d9
+                              unsigned int flags)
9119d9
 {
9119d9
     int ret = -1;
9119d9
     unsigned int port;
9119d9
     char *targetType = virXMLPropString(cur, "type");
9119d9
     char *addrStr = NULL;
9119d9
     char *portStr = NULL;
9119d9
+    char *stateStr = NULL;
9119d9
 
9119d9
     if ((def->targetType =
9119d9
          virDomainChrTargetTypeFromString(def, def->deviceType,
9119d9
@@ -7798,6 +7805,20 @@ virDomainChrDefParseTargetXML(virDomainChrDefPtr def,
9119d9
 
9119d9
         case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO:
9119d9
             def->target.name = virXMLPropString(cur, "name");
9119d9
+
9119d9
+            if (!(flags & VIR_DOMAIN_XML_INACTIVE) &&
9119d9
+                (stateStr = virXMLPropString(cur, "state"))) {
9119d9
+                int tmp;
9119d9
+
9119d9
+                if ((tmp = virDomainChrDeviceStateTypeFromString(stateStr)) <= 0) {
9119d9
+                    virReportError(VIR_ERR_XML_ERROR,
9119d9
+                                   _("invalid channel state value '%s'"),
9119d9
+                                   stateStr);
9119d9
+                    goto error;
9119d9
+                }
9119d9
+
9119d9
+                def->state = tmp;
9119d9
+            }
9119d9
             break;
9119d9
         }
9119d9
         break;
9119d9
@@ -7826,6 +7847,7 @@ virDomainChrDefParseTargetXML(virDomainChrDefPtr def,
9119d9
     VIR_FREE(targetType);
9119d9
     VIR_FREE(addrStr);
9119d9
     VIR_FREE(portStr);
9119d9
+    VIR_FREE(stateStr);
9119d9
 
9119d9
     return ret;
9119d9
 }
9119d9
@@ -8201,7 +8223,7 @@ virDomainChrDefParseXML(xmlXPathContextPtr ctxt,
9119d9
         if (cur->type == XML_ELEMENT_NODE) {
9119d9
             if (xmlStrEqual(cur->name, BAD_CAST "target")) {
9119d9
                 seenTarget = true;
9119d9
-                if (virDomainChrDefParseTargetXML(def, cur) < 0) {
9119d9
+                if (virDomainChrDefParseTargetXML(def, cur, flags) < 0) {
9119d9
                     goto error;
9119d9
                 }
9119d9
             }
9119d9
@@ -17309,6 +17331,12 @@ virDomainChrDefFormat(virBufferPtr buf,
9119d9
             if (def->target.name) {
9119d9
                 virBufferEscapeString(buf, " name='%s'", def->target.name);
9119d9
             }
9119d9
+
9119d9
+            if (def->state != VIR_DOMAIN_CHR_DEVICE_STATE_DEFAULT &&
9119d9
+                !(flags & VIR_DOMAIN_XML_INACTIVE)) {
9119d9
+                virBufferAsprintf(buf, " state='%s'",
9119d9
+                                  virDomainChrDeviceStateTypeToString(def->state));
9119d9
+            }
9119d9
             break;
9119d9
         }
9119d9
 
9119d9
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
9119d9
index 6768089..f9cccb4 100644
9119d9
--- a/src/conf/domain_conf.h
9119d9
+++ b/src/conf/domain_conf.h
9119d9
@@ -985,6 +985,16 @@ struct _virDomainNetDef {
9119d9
 # define VIR_NET_GENERATED_PREFIX "vnet"
9119d9
 
9119d9
 typedef enum {
9119d9
+    VIR_DOMAIN_CHR_DEVICE_STATE_DEFAULT = 0,
9119d9
+    VIR_DOMAIN_CHR_DEVICE_STATE_CONNECTED,
9119d9
+    VIR_DOMAIN_CHR_DEVICE_STATE_DISCONNECTED,
9119d9
+
9119d9
+    VIR_DOMAIN_CHR_DEVICE_STATE_LAST
9119d9
+} virDomainChrDeviceState;
9119d9
+
9119d9
+VIR_ENUM_DECL(virDomainChrDeviceState)
9119d9
+
9119d9
+typedef enum {
9119d9
     VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL = 0,
9119d9
     VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL,
9119d9
     VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE,
9119d9
@@ -1104,6 +1114,8 @@ struct _virDomainChrDef {
9119d9
         char *name; /* virtio */
9119d9
     } target;
9119d9
 
9119d9
+    virDomainChrDeviceState state;
9119d9
+
9119d9
     virDomainChrSourceDef source;
9119d9
 
9119d9
     virDomainDeviceInfo info;
9119d9
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.args b/tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.args
9119d9
new file mode 100644
9119d9
index 0000000..62bf14d
9119d9
--- /dev/null
9119d9
+++ b/tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.args
9119d9
@@ -0,0 +1,17 @@
9119d9
+LC_ALL=C PATH=/bin HOME=/home/test USER=test LOGNAME=test QEMU_AUDIO_DRV=none \
9119d9
+/usr/bin/qemu -S -M pc -m 214 -smp 1 -nographic -nodefconfig -nodefaults \
9119d9
+-chardev socket,id=charmonitor,path=/tmp/test-monitor,server,nowait \
9119d9
+-mon chardev=charmonitor,id=monitor,mode=readline \
9119d9
+-no-acpi -boot c \
9119d9
+-device virtio-serial-pci,id=virtio-serial1,bus=pci.0,addr=0xa \
9119d9
+-usb -hda /dev/HostVG/QEMUGuest1 \
9119d9
+-chardev pty,id=charchannel0 \
9119d9
+-device virtserialport,bus=virtio-serial1.0,nr=3,chardev=charchannel0,\
9119d9
+id=channel0,name=org.linux-kvm.port.foo \
9119d9
+-chardev pty,id=charchannel1 \
9119d9
+-device virtserialport,bus=virtio-serial1.0,nr=4,chardev=charchannel1,\
9119d9
+id=channel1,name=org.linux-kvm.port.foo1 \
9119d9
+-chardev pty,id=charchannel2 \
9119d9
+-device virtserialport,bus=virtio-serial1.0,nr=5,chardev=charchannel2,\
9119d9
+id=channel2,name=org.linux-kvm.port.foo2 \
9119d9
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3
9119d9
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.xml b/tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.xml
9119d9
new file mode 100644
9119d9
index 0000000..044b369
9119d9
--- /dev/null
9119d9
+++ b/tests/qemuxml2argvdata/qemuxml2argv-channel-virtio-state.xml
9119d9
@@ -0,0 +1,42 @@
9119d9
+<domain type='qemu' id='2'>
9119d9
+  <name>QEMUGuest1</name>
9119d9
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
9119d9
+  <memory unit='KiB'>219100</memory>
9119d9
+  <currentMemory unit='KiB'>219100</currentMemory>
9119d9
+  <vcpu placement='static' cpuset='1-4,8-20,525'>1</vcpu>
9119d9
+  <os>
9119d9
+    <type arch='i686' machine='pc'>hvm</type>
9119d9
+    <boot dev='hd'/>
9119d9
+  </os>
9119d9
+  <clock offset='utc'/>
9119d9
+  <on_poweroff>destroy</on_poweroff>
9119d9
+  <on_reboot>restart</on_reboot>
9119d9
+  <on_crash>destroy</on_crash>
9119d9
+  <devices>
9119d9
+    <emulator>/usr/bin/qemu</emulator>
9119d9
+    <disk type='block' device='disk'>
9119d9
+      <source dev='/dev/HostVG/QEMUGuest1'/>
9119d9
+      <target dev='hda' bus='ide'/>
9119d9
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
9119d9
+    </disk>
9119d9
+    <controller type='usb' index='0'/>
9119d9
+    <controller type='ide' index='0'/>
9119d9
+    <controller type='virtio-serial' index='1'>
9119d9
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
9119d9
+    </controller>
9119d9
+    <controller type='pci' index='0' model='pci-root'/>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo' state='connected'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='3'/>
9119d9
+    </channel>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo1' state='disconnected'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='4'/>
9119d9
+    </channel>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo2'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='5'/>
9119d9
+    </channel>
9119d9
+    <memballoon model='virtio'/>
9119d9
+  </devices>
9119d9
+</domain>
9119d9
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
9119d9
index 3efe3c9..97af90a 100644
9119d9
--- a/tests/qemuxml2argvtest.c
9119d9
+++ b/tests/qemuxml2argvtest.c
9119d9
@@ -1051,6 +1051,8 @@ mymain(void)
9119d9
             QEMU_CAPS_CHARDEV, QEMU_CAPS_DEVICE, QEMU_CAPS_NODEFCONFIG);
9119d9
     DO_TEST("channel-virtio",
9119d9
             QEMU_CAPS_DEVICE, QEMU_CAPS_CHARDEV, QEMU_CAPS_NODEFCONFIG);
9119d9
+    DO_TEST("channel-virtio-state",
9119d9
+            QEMU_CAPS_DEVICE, QEMU_CAPS_CHARDEV, QEMU_CAPS_NODEFCONFIG);
9119d9
     DO_TEST("channel-virtio-auto",
9119d9
             QEMU_CAPS_DEVICE, QEMU_CAPS_CHARDEV, QEMU_CAPS_NODEFCONFIG);
9119d9
     DO_TEST("console-virtio",
9119d9
diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-active.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-active.xml
9119d9
new file mode 100644
9119d9
index 0000000..4f050fc
9119d9
--- /dev/null
9119d9
+++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-active.xml
9119d9
@@ -0,0 +1,43 @@
9119d9
+<domain type='qemu' id='2'>
9119d9
+  <name>QEMUGuest1</name>
9119d9
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
9119d9
+  <memory unit='KiB'>219100</memory>
9119d9
+  <currentMemory unit='KiB'>219100</currentMemory>
9119d9
+  <vcpu placement='static' cpuset='1-4,8-20,525'>1</vcpu>
9119d9
+  <os>
9119d9
+    <type arch='i686' machine='pc'>hvm</type>
9119d9
+    <boot dev='hd'/>
9119d9
+  </os>
9119d9
+  <clock offset='utc'/>
9119d9
+  <on_poweroff>destroy</on_poweroff>
9119d9
+  <on_reboot>restart</on_reboot>
9119d9
+  <on_crash>destroy</on_crash>
9119d9
+  <devices>
9119d9
+    <emulator>/usr/bin/qemu</emulator>
9119d9
+    <disk type='block' device='disk'>
9119d9
+      <source dev='/dev/HostVG/QEMUGuest1'/>
9119d9
+      <backingStore/>
9119d9
+      <target dev='hda' bus='ide'/>
9119d9
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
9119d9
+    </disk>
9119d9
+    <controller type='usb' index='0'/>
9119d9
+    <controller type='ide' index='0'/>
9119d9
+    <controller type='virtio-serial' index='1'>
9119d9
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
9119d9
+    </controller>
9119d9
+    <controller type='pci' index='0' model='pci-root'/>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo' state='connected'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='3'/>
9119d9
+    </channel>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo1' state='disconnected'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='4'/>
9119d9
+    </channel>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo2'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='5'/>
9119d9
+    </channel>
9119d9
+    <memballoon model='virtio'/>
9119d9
+  </devices>
9119d9
+</domain>
9119d9
diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-inactive.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-inactive.xml
9119d9
new file mode 100644
9119d9
index 0000000..5027a1e
9119d9
--- /dev/null
9119d9
+++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-channel-virtio-state-inactive.xml
9119d9
@@ -0,0 +1,42 @@
9119d9
+<domain type='qemu'>
9119d9
+  <name>QEMUGuest1</name>
9119d9
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
9119d9
+  <memory unit='KiB'>219100</memory>
9119d9
+  <currentMemory unit='KiB'>219100</currentMemory>
9119d9
+  <vcpu placement='static' cpuset='1-4,8-20,525'>1</vcpu>
9119d9
+  <os>
9119d9
+    <type arch='i686' machine='pc'>hvm</type>
9119d9
+    <boot dev='hd'/>
9119d9
+  </os>
9119d9
+  <clock offset='utc'/>
9119d9
+  <on_poweroff>destroy</on_poweroff>
9119d9
+  <on_reboot>restart</on_reboot>
9119d9
+  <on_crash>destroy</on_crash>
9119d9
+  <devices>
9119d9
+    <emulator>/usr/bin/qemu</emulator>
9119d9
+    <disk type='block' device='disk'>
9119d9
+      <source dev='/dev/HostVG/QEMUGuest1'/>
9119d9
+      <target dev='hda' bus='ide'/>
9119d9
+      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
9119d9
+    </disk>
9119d9
+    <controller type='usb' index='0'/>
9119d9
+    <controller type='ide' index='0'/>
9119d9
+    <controller type='virtio-serial' index='1'>
9119d9
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
9119d9
+    </controller>
9119d9
+    <controller type='pci' index='0' model='pci-root'/>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='3'/>
9119d9
+    </channel>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo1'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='4'/>
9119d9
+    </channel>
9119d9
+    <channel type='pty'>
9119d9
+      <target type='virtio' name='org.linux-kvm.port.foo2'/>
9119d9
+      <address type='virtio-serial' controller='1' bus='0' port='5'/>
9119d9
+    </channel>
9119d9
+    <memballoon model='virtio'/>
9119d9
+  </devices>
9119d9
+</domain>
9119d9
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
9119d9
index f29e4ed..50f5907 100644
9119d9
--- a/tests/qemuxml2xmltest.c
9119d9
+++ b/tests/qemuxml2xmltest.c
9119d9
@@ -288,6 +288,7 @@ mymain(void)
9119d9
     DO_TEST("console-virtio-many");
9119d9
     DO_TEST("channel-guestfwd");
9119d9
     DO_TEST("channel-virtio");
9119d9
+    DO_TEST_DIFFERENT("channel-virtio-state");
9119d9
 
9119d9
     DO_TEST("hostdev-usb-address");
9119d9
     DO_TEST("hostdev-pci-address");
9119d9
-- 
9119d9
2.1.3
9119d9