From 069fe0bafd526a20c1630d32ff481e33acf0781c Mon Sep 17 00:00:00 2001
From: Ashish Mittal <ashish.mittal@veritas.com>
Date: Thu, 9 Jun 2016 19:52:12 -0700
Subject: [PATCH] Bug 1341866 Enable Veritas OpenFlame functionality on RedHat
 OSP8 platform

Request to upstream libvirt dependencies for qemu based network block driver from Veritas.
---
 docs/formatdomain.html.in                          | 12 +++--
 docs/schemas/domaincommon.rng                      |  1 +
 src/qemu/qemu_command.c                            | 51 ++++++++++++++++++++++
 src/qemu/qemu_domain.c                             |  1 +
 src/qemu/qemu_driver.c                             |  3 ++
 src/qemu/qemu_parse_command.c                      | 21 +++++++++
 src/util/virstoragefile.c                          |  4 +-
 src/util/virstoragefile.h                          |  1 +
 .../qemuxml2argv-disk-drive-network-oflame.args    | 24 ++++++++++
 .../qemuxml2argv-disk-drive-network-oflame.xml     | 35 +++++++++++++++
 tests/qemuxml2argvtest.c                           |  1 +
 11 files changed, 150 insertions(+), 4 deletions(-)
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.args
 create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.xml

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index e737e39..e3ca958 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -2124,9 +2124,9 @@
               <dd>
               The <code>protocol</code> attribute specifies the protocol to
               access to the requested image. Possible values are "nbd",
-              "iscsi", "rbd", "sheepdog" or "gluster".  If the
-              <code>protocol</code> attribute is "rbd", "sheepdog" or
-              "gluster", an additional attribute <code>name</code> is
+              "iscsi", "rbd", "sheepdog", "gluster" or "oflame".  If the
+              <code>protocol</code> attribute is "rbd", "sheepdog", "gluster"
+              or "oflame", an additional attribute <code>name</code> is
               mandatory to specify which volume/image will be used. For "nbd",
               the <code>name</code> attribute is optional. For "iscsi"
               (<span class="since">since 1.0.4</span>), the <code>name</code>
@@ -2236,6 +2236,12 @@
                 <td> only one </td>
                 <td> 24007 </td>
               </tr>
+              <tr>
+                <td> oflame </td>
+                <td> a server running Veritas hyperd daemon </td>
+                <td> one or more </td>
+                <td> 9999 </td>
+              </tr>
             </table>
             <p>
             gluster supports "tcp", "rdma", "unix" as valid values for the
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index d14c1c5..8731dea 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -1414,6 +1414,7 @@
             <value>ftp</value>
             <value>ftps</value>
             <value>tftp</value>
+            <value>oflame</value>
           </choice>
         </attribute>
         <optional>
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index b5d84e6..162f807 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -497,6 +497,7 @@ qemuNetworkDriveGetPort(int protocol,
             return 0;
 
         case VIR_STORAGE_NET_PROTOCOL_RBD:
+        case VIR_STORAGE_NET_PROTOCOL_OFLAME:
         case VIR_STORAGE_NET_PROTOCOL_LAST:
         case VIR_STORAGE_NET_PROTOCOL_NONE:
             /* not applicable */
@@ -894,6 +895,56 @@ qemuBuildNetworkDriveURI(virStorageSourcePtr src,
             ret = virBufferContentAndReset(&buf);
             break;
 
+        case VIR_STORAGE_NET_PROTOCOL_OFLAME:
+            if (VIR_ALLOC(uri) < 0)
+                goto cleanup;
+
+            if (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
+                if (VIR_STRDUP(uri->scheme,
+                               virStorageNetProtocolTypeToString(src->protocol)) < 0)
+                    goto cleanup;
+            } else {
+                if (virAsprintf(&uri->scheme, "%s+%s",
+                                virStorageNetProtocolTypeToString(src->protocol),
+                                virStorageNetHostTransportTypeToString(src->hosts->transport)) < 0)
+                    goto cleanup;
+            }
+
+            if (src->path) {
+                if (src->volume) {
+                    if (virAsprintf(&uri->path, "/%s%s",
+                                    src->volume, src->path) < 0)
+                        goto cleanup;
+                } else {
+                    if (virAsprintf(&uri->path, "%s%s",
+                                    src->path[0] == '/' ? "" : "/",
+                                    src->path) < 0)
+                        goto cleanup;
+                }
+            }
+
+            if (src->hosts->socket &&
+                virAsprintf(&uri->query, "socket=%s", src->hosts->socket) < 0)
+                goto cleanup;
+
+            if (qemuBuildGeneralSecinfoURI(uri, secinfo) < 0)
+                goto cleanup;
+
+            for (i = 0; i < src->nhosts; i++) {
+                if ((uri->port = qemuNetworkDriveGetPort(src->protocol, src->hosts[i].port)) < 0)
+                    goto cleanup;
+
+                if (VIR_STRDUP(uri->server, src->hosts[i].name) < 0)
+                    goto cleanup;
+
+            ret = virURIFormat(uri);
+            virBufferEscape(&buf, ',', ",", "%s", ret);
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                _("qemuBuildOFlameString builturi '%s'"), ret);
+            }
+
+            ret = virBufferContentAndReset(&buf);
+            break;
 
         case VIR_STORAGE_NET_PROTOCOL_LAST:
         case VIR_STORAGE_NET_PROTOCOL_NONE:
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 0a1c09a..4bfcdc6 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -883,6 +883,7 @@ qemuDomainSecretAESSetup(virConnectPtr conn,
     case VIR_STORAGE_NET_PROTOCOL_FTP:
     case VIR_STORAGE_NET_PROTOCOL_FTPS:
     case VIR_STORAGE_NET_PROTOCOL_TFTP:
+    case VIR_STORAGE_NET_PROTOCOL_OFLAME:
     case VIR_STORAGE_NET_PROTOCOL_LAST:
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("protocol '%s' cannot be used for encrypted secrets"),
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e70d3ce..294562f 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13461,6 +13461,7 @@ qemuDomainSnapshotPrepareDiskExternalBackingInactive(virDomainDiskDefPtr disk)
         case VIR_STORAGE_NET_PROTOCOL_FTP:
         case VIR_STORAGE_NET_PROTOCOL_FTPS:
         case VIR_STORAGE_NET_PROTOCOL_TFTP:
+        case VIR_STORAGE_NET_PROTOCOL_OFLAME:
         case VIR_STORAGE_NET_PROTOCOL_LAST:
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("external inactive snapshots are not supported on "
@@ -13523,6 +13524,7 @@ qemuDomainSnapshotPrepareDiskExternalOverlayActive(virDomainSnapshotDiskDefPtr d
         case VIR_STORAGE_NET_PROTOCOL_FTP:
         case VIR_STORAGE_NET_PROTOCOL_FTPS:
         case VIR_STORAGE_NET_PROTOCOL_TFTP:
+        case VIR_STORAGE_NET_PROTOCOL_OFLAME:
         case VIR_STORAGE_NET_PROTOCOL_LAST:
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("external active snapshots are not supported on "
@@ -13667,6 +13669,7 @@ qemuDomainSnapshotPrepareDiskInternal(virConnectPtr conn,
         case VIR_STORAGE_NET_PROTOCOL_FTP:
         case VIR_STORAGE_NET_PROTOCOL_FTPS:
         case VIR_STORAGE_NET_PROTOCOL_TFTP:
+        case VIR_STORAGE_NET_PROTOCOL_OFLAME:
         case VIR_STORAGE_NET_PROTOCOL_LAST:
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("internal inactive snapshots are not supported on "
diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c
index 4fafdf2..7f8ad81 100644
--- a/src/qemu/qemu_parse_command.c
+++ b/src/qemu/qemu_parse_command.c
@@ -263,6 +263,16 @@ qemuParseNBDString(virDomainDiskDefPtr disk)
     return -1;
 }
 
+static int
+qemuParseOflameString(virDomainDiskDefPtr def)
+{
+    virURIPtr uri = NULL;
+
+    if (!(uri = virURIParse(def->src->path)))
+        return -1;
+
+    return qemuParseDriveURIString(def, uri, "oflame");
+}
 
 /*
  * This method takes a string representing a QEMU command line ARGV set
@@ -738,6 +748,12 @@ qemuParseCommandLineDisk(virDomainXMLOptionPtr xmlopt,
                         if (VIR_STRDUP(def->src->path, vdi) < 0)
                             goto error;
                     }
+                } else if (STRPREFIX(def->src->path, "oflame:")) {
+                    def->src->type = VIR_STORAGE_TYPE_NETWORK;
+                    def->src->protocol = VIR_STORAGE_NET_PROTOCOL_OFLAME;
+
+                    if (qemuParseOflameString(def) < 0)
+                        goto error;
                 } else {
                     def->src->type = VIR_STORAGE_TYPE_FILE;
                 }
@@ -2012,6 +2028,11 @@ qemuParseCommandLine(virCapsPtr caps,
                         goto error;
 
                     break;
+                case VIR_STORAGE_NET_PROTOCOL_OFLAME:
+                    if (qemuParseOflameString(disk) < 0)
+                        goto error;
+
+                    break;
                 case VIR_STORAGE_NET_PROTOCOL_HTTP:
                 case VIR_STORAGE_NET_PROTOCOL_HTTPS:
                 case VIR_STORAGE_NET_PROTOCOL_FTP:
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index d2da9e7..3a0129e 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -83,7 +83,8 @@ VIR_ENUM_IMPL(virStorageNetProtocol, VIR_STORAGE_NET_PROTOCOL_LAST,
               "https",
               "ftp",
               "ftps",
-              "tftp")
+              "tftp",
+              "oflame")
 
 VIR_ENUM_IMPL(virStorageNetHostTransport, VIR_STORAGE_NET_HOST_TRANS_LAST,
               "tcp",
@@ -2516,6 +2517,7 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src,
     case VIR_STORAGE_NET_PROTOCOL_TFTP:
     case VIR_STORAGE_NET_PROTOCOL_ISCSI:
     case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
+    case VIR_STORAGE_NET_PROTOCOL_OFLAME:
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("malformed backing store path for protocol %s"),
                        protocol);
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index b88e715..b155ec0 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -130,6 +130,7 @@ typedef enum {
     VIR_STORAGE_NET_PROTOCOL_FTP,
     VIR_STORAGE_NET_PROTOCOL_FTPS,
     VIR_STORAGE_NET_PROTOCOL_TFTP,
+    VIR_STORAGE_NET_PROTOCOL_OFLAME,
 
     VIR_STORAGE_NET_PROTOCOL_LAST
 } virStorageNetProtocol;
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.args b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.args
new file mode 100644
index 0000000..5083cae
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.args
@@ -0,0 +1,24 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/home/test \
+USER=test \
+LOGNAME=test \
+QEMU_AUDIO_DRV=none \
+/usr/libexec/qemu-kvm \
+-name QEMUGuest1 \
+-S \
+-M pc \
+-cpu qemu32 \
+-m 214 \
+-smp 1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-nographic \
+-nodefaults \
+-monitor unix:/tmp/lib/domain--1-QEMUGuest1/monitor.sock,server,nowait \
+-no-acpi \
+-boot c \
+-usb \
+-drive file=oflame://192.168.0.1:9999/%7Beb90327c-8302-4725-9e1b-4e85ed4dc251%7D\
+oflame://172.172.17.56:9999/%7Beb90327c-8302-4725-9e1b-4e85ed4dc251%7D,\
+format=raw,if=none,id=drive-virtio-disk0,cache=none \
+-device virtio-blk-pci,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.xml b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.xml
new file mode 100644
index 0000000..f60820d
--- /dev/null
+++ b/tests/qemuxml2argvdata/qemuxml2argv-disk-drive-network-oflame.xml
@@ -0,0 +1,35 @@
+<domain type='qemu'>
+  <name>QEMUGuest1</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='i686' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/libexec/qemu-kvm</emulator>
+    <disk type='network' device='disk'>
+      <driver name='qemu' type='raw' cache='none'/>
+      <source protocol='oflame' name='{eb90327c-8302-4725-9e1b-4e85ed4dc251}'>
+        <host name='192.168.0.1' port='9999'/>
+        <host name='172.172.17.56' port='9999'/>
+      </source>
+      <backingStore/>
+      <target dev='vda' bus='virtio'/>
+      <serial>eb90327c-8302-4725-9e1b-4e85ed4dc251</serial>
+      <alias name='virtio-disk0'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
+    </disk>
+    <controller type='usb' index='0'/>
+    <controller type='pci' index='0' model='pci-root'/>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <memballoon model='none'/>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index f1f47ec..39d6157 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -799,6 +799,7 @@ mymain(void)
 # endif
     DO_TEST("disk-drive-network-rbd-ipv6", NONE);
     DO_TEST_FAILURE("disk-drive-network-rbd-no-colon", NONE);
+    DO_TEST("disk-drive-network-oflame", NONE);
     DO_TEST("disk-drive-no-boot",
             QEMU_CAPS_BOOTINDEX);
     DO_TEST_PARSE_ERROR("disk-device-lun-type-invalid",
-- 
2.5.4 (Apple Git-61)

