[PATCH v2.1 1/3] interface: introduce downscript element for interface

2020-05-25 Thread Chen Hanxiao
https://gitlab.com/libvirt/libvirt/-/issues/13

Add support for downscript:







Signed-off-by: Chen Hanxiao 
---
 docs/formatdomain.html.in | 6 +-
 docs/schemas/domaincommon.rng | 8 
 src/conf/domain_conf.c| 9 +
 src/conf/domain_conf.h| 1 +
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 23eb029234..4e2320d537 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -5879,7 +5879,10 @@
 
   After creating/opening the tap device, an optional shell script
   (given in the path attribute of
-  the script element) will be run; this can
+  the script element) will be run;
+  Also, after detaching/closing the tap device, an optional shell
+  script (given in the path attribute of
+  the downscript element) will be run; this can
   be used to do whatever extra host network integration is
   required.
 
@@ -5889,6 +5892,7 @@
 devices
   interface type='ethernet'
 script path='/etc/qemu-ifup-mynet'/
+downscript path='/etc/qemu-ifdown-mynet'/
   /interface
   ...
   interface type='ethernet'
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 9d60b090f3..6727cd743b 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -3191,6 +3191,14 @@
   
 
   
+  
+
+  
+
+  
+  
+
+  
   
 
   
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c201fc901d..1406cf079e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -2520,6 +2520,7 @@ virDomainNetDefClear(virDomainNetDefPtr def)
 VIR_FREE(def->teaming.persistent);
 VIR_FREE(def->virtPortProfile);
 VIR_FREE(def->script);
+VIR_FREE(def->downscript);
 VIR_FREE(def->domain_name);
 VIR_FREE(def->ifname);
 VIR_FREE(def->ifname_guest);
@@ -11977,6 +11978,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 g_autofree char *ifname_guest = NULL;
 g_autofree char *ifname_guest_actual = NULL;
 g_autofree char *script = NULL;
+g_autofree char *downscript = NULL;
 g_autofree char *address = NULL;
 g_autofree char *port = NULL;
 g_autofree char *localaddr = NULL;
@@ -12149,6 +12151,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 } else if (!script &&
virXMLNodeNameEqual(cur, "script")) {
 script = virXMLPropString(cur, "path");
+} else if (!downscript &&
+   virXMLNodeNameEqual(cur, "downscript")) {
+downscript = virXMLPropString(cur, "path");
 } else if (!domain_name &&
virXMLNodeNameEqual(cur, "backenddomain")) {
 domain_name = virXMLPropString(cur, "name");
@@ -12482,6 +12487,8 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 
 if (script != NULL)
 def->script = g_steal_pointer();
+if (downscript != NULL)
+def->downscript = g_steal_pointer();
 if (domain_name != NULL)
 def->domain_name = g_steal_pointer(_name);
 if (ifname != NULL)
@@ -26567,6 +26574,8 @@ virDomainNetDefFormat(virBufferPtr buf,
 
 virBufferEscapeString(buf, "\n",
   def->script);
+virBufferEscapeString(buf, "\n",
+  def->downscript);
 virBufferEscapeString(buf, "\n", 
def->domain_name);
 
 if (def->ifname &&
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index ddc75d8de2..e152c599ca 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1055,6 +1055,7 @@ struct _virDomainNetDef {
 unsigned long sndbuf;
 } tune;
 char *script;
+char *downscript;
 char *domain_name; /* backend domain name */
 char *ifname; /* interface name on the host () */
 int managed_tap; /* enum virTristateBool - ABSENT == YES */
-- 
2.23.0



[PATCH v2.1 3/3] news: add description about downscript

2020-05-25 Thread Chen Hanxiao
Signed-off-by: Chen Hanxiao 
---
 docs/news.xml | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index 4cef804aac..67fb85377d 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -52,6 +52,16 @@
   
   
 
+  
+
+  qemu: support network interface downscript
+
+
+  QEMU has the ability to run a script when a NIC is brought up
+  and down. Libvirt only enables use of the up script.
+  Now add support for postscript when NIC is down/detached.
+
+  
   
 
   qemu: support disabling hotplug/unplug of PCIe devices
-- 
2.23.0



[PATCH v2.1 2/3] downscript: add support for booting and hotplug interface

2020-05-25 Thread Chen Hanxiao
Support downscript for booting vm,
and hotunplug interface device.

Signed-off-by: Chen Hanxiao 
---
 src/qemu/qemu_extdevice.c   |  4 ++
 src/qemu/qemu_hotplug.c |  3 ++
 tests/qemuxml2argvdata/downscript.xml   | 60 +
 tests/qemuxml2xmloutdata/downscript.xml | 60 +
 tests/qemuxml2xmltest.c |  1 +
 5 files changed, 128 insertions(+)
 create mode 100644 tests/qemuxml2argvdata/downscript.xml
 create mode 100644 tests/qemuxml2xmloutdata/downscript.xml

diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c
index 2096272761..4962521de4 100644
--- a/src/qemu/qemu_extdevice.c
+++ b/src/qemu/qemu_extdevice.c
@@ -213,6 +213,7 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
virDomainObjPtr vm)
 {
 virDomainDefPtr def = vm->def;
+virDomainNetType actualType;
 size_t i;
 
 if (qemuExtDevicesInitPaths(driver, def) < 0)
@@ -230,10 +231,13 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
 
 for (i = 0; i < def->nnets; i++) {
 virDomainNetDefPtr net = def->nets[i];
+actualType = virDomainNetGetActualType(net);
 qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
 
 if (slirp)
 qemuSlirpStop(slirp, vm, driver, net);
+if (actualType == VIR_DOMAIN_NET_TYPE_ETHERNET && net->downscript)
+virNetDevRunEthernetScript(net->ifname, net->downscript);
 }
 
 for (i = 0; i < def->nfss; i++) {
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 5608566d69..f94f518f4d 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4663,6 +4663,9 @@ qemuDomainRemoveNetDevice(virQEMUDriverPtr driver,
 virDomainNetReleaseActualDevice(conn, vm->def, net);
 else
 VIR_WARN("Unable to release network device '%s'", 
NULLSTR(net->ifname));
+} else if (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET) {
+if (net->script)
+   virNetDevRunEthernetScript(net->ifname, net->downscript);
 }
 virDomainNetDefFree(net);
 return 0;
diff --git a/tests/qemuxml2argvdata/downscript.xml 
b/tests/qemuxml2argvdata/downscript.xml
new file mode 100644
index 00..4d0fb1beab
--- /dev/null
+++ b/tests/qemuxml2argvdata/downscript.xml
@@ -0,0 +1,60 @@
+
+  QEMUGuest1
+  c7a5fdbd-edaf-9455-926a-d65c16db1809
+  219136
+  219136
+  1
+  
+hvm
+
+  
+  
+qemu64
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+  
+
+
+  
+
+
+  
+
+
+
+  
+
+
+  
+  
+  
+
+
+  
+  
+  
+  
+
+
+  
+  
+  
+  
+  
+
+
+
+
+  
+
+  
+
diff --git a/tests/qemuxml2xmloutdata/downscript.xml 
b/tests/qemuxml2xmloutdata/downscript.xml
new file mode 100644
index 00..4d0fb1beab
--- /dev/null
+++ b/tests/qemuxml2xmloutdata/downscript.xml
@@ -0,0 +1,60 @@
+
+  QEMUGuest1
+  c7a5fdbd-edaf-9455-926a-d65c16db1809
+  219136
+  219136
+  1
+  
+hvm
+
+  
+  
+qemu64
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+  
+
+
+  
+
+
+  
+
+
+
+  
+
+
+  
+  
+  
+
+
+  
+  
+  
+  
+
+
+  
+  
+  
+  
+  
+
+
+
+
+  
+
+  
+
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
index 033f81013e..dcc7b29ded 100644
--- a/tests/qemuxml2xmltest.c
+++ b/tests/qemuxml2xmltest.c
@@ -1478,6 +1478,7 @@ mymain(void)
 DO_TEST_CAPS_ARCH_LATEST("x86_64-default-cpu-tcg-q35-4.2", "x86_64");
 
 DO_TEST_CAPS_LATEST("virtio-9p-multidevs");
+DO_TEST("downscript", NONE);
 
 if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
 virFileDeleteTree(fakerootdir);
-- 
2.23.0



[PATCH v2.1 0/3] Support network interface downscript

2020-05-25 Thread Chen Hanxiao
QEMU has the ability to run a script when a NIC is brought up and down. 
Libvirt only enables use of the up script at this time.
This series add support for postscript when NIC is down/detached.

Chen Hanxiao (3):
  interface: introduce downscript
  downscript: add support for booting and hotplug interface
  news: add description about downscript

 docs/formatdomain.html.in   |  6 ++-
 docs/news.xml   | 10 +
 docs/schemas/domaincommon.rng   |  8 
 src/conf/domain_conf.c  |  9 
 src/conf/domain_conf.h  |  1 +
 src/qemu/qemu_extdevice.c   |  4 ++
 src/qemu/qemu_hotplug.c |  3 ++
 tests/qemuxml2argvdata/downscript.xml   | 60 +
 tests/qemuxml2xmloutdata/downscript.xml | 60 +
 tests/qemuxml2xmltest.c |  1 +
 10 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 tests/qemuxml2argvdata/downscript.xml
 create mode 100644 tests/qemuxml2xmloutdata/downscript.xml

-- 
2.23.0



[PATCH v2 0/3] Support network interface downscript

2020-05-25 Thread Chen Hanxiao
QEMU has the ability to run a script when a NIC is brought up and down. 
Libvirt only enables use of the up script at this time.
This series add support for postscript when NIC is down/detached.


Chen Hanxiao (3):
  interface: introduce downscript
  downscript: add support for booting and hotplug interface
  downscript: docs: add docs and news

 docs/formatdomain.html.in   |  6 ++-
 docs/news.xml   | 10 +
 docs/schemas/domaincommon.rng   |  8 
 src/conf/domain_conf.c  |  9 
 src/conf/domain_conf.h  |  1 +
 src/qemu/qemu_extdevice.c   |  4 ++
 src/qemu/qemu_hotplug.c |  3 ++
 tests/qemuxml2argvdata/downscript.xml   | 60 +
 tests/qemuxml2xmloutdata/downscript.xml | 60 +
 tests/qemuxml2xmltest.c |  1 +
 10 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 tests/qemuxml2argvdata/downscript.xml
 create mode 100644 tests/qemuxml2xmloutdata/downscript.xml

-- 
2.23.0



[PATCH v2 3/3] downscript: docs: add docs and news

2020-05-25 Thread Chen Hanxiao
Signed-off-by: Chen Hanxiao 
---
 docs/formatdomain.html.in |  6 +-
 docs/news.xml | 10 ++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 23eb029234..4e2320d537 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -5879,7 +5879,10 @@
 
   After creating/opening the tap device, an optional shell script
   (given in the path attribute of
-  the script element) will be run; this can
+  the script element) will be run;
+  Also, after detaching/closing the tap device, an optional shell
+  script (given in the path attribute of
+  the downscript element) will be run; this can
   be used to do whatever extra host network integration is
   required.
 
@@ -5889,6 +5892,7 @@
 devices
   interface type='ethernet'
 script path='/etc/qemu-ifup-mynet'/
+downscript path='/etc/qemu-ifdown-mynet'/
   /interface
   ...
   interface type='ethernet'
diff --git a/docs/news.xml b/docs/news.xml
index 4cef804aac..67fb85377d 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -52,6 +52,16 @@
   
   
 
+  
+
+  qemu: support network interface downscript
+
+
+  QEMU has the ability to run a script when a NIC is brought up
+  and down. Libvirt only enables use of the up script.
+  Now add support for postscript when NIC is down/detached.
+
+  
   
 
   qemu: support disabling hotplug/unplug of PCIe devices
-- 
2.23.0



[PATCH v2 2/3] downscript: add support for booting and hotplug interface

2020-05-25 Thread Chen Hanxiao
Support downscript for booting vm,
and hotunplug interface device.

Signed-off-by: Chen Hanxiao 
---
 src/qemu/qemu_extdevice.c   |  4 ++
 src/qemu/qemu_hotplug.c |  3 ++
 tests/qemuxml2argvdata/downscript.xml   | 60 +
 tests/qemuxml2xmloutdata/downscript.xml | 60 +
 tests/qemuxml2xmltest.c |  1 +
 5 files changed, 128 insertions(+)
 create mode 100644 tests/qemuxml2argvdata/downscript.xml
 create mode 100644 tests/qemuxml2xmloutdata/downscript.xml

diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c
index 2096272761..4962521de4 100644
--- a/src/qemu/qemu_extdevice.c
+++ b/src/qemu/qemu_extdevice.c
@@ -213,6 +213,7 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
virDomainObjPtr vm)
 {
 virDomainDefPtr def = vm->def;
+virDomainNetType actualType;
 size_t i;
 
 if (qemuExtDevicesInitPaths(driver, def) < 0)
@@ -230,10 +231,13 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
 
 for (i = 0; i < def->nnets; i++) {
 virDomainNetDefPtr net = def->nets[i];
+actualType = virDomainNetGetActualType(net);
 qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
 
 if (slirp)
 qemuSlirpStop(slirp, vm, driver, net);
+if (actualType == VIR_DOMAIN_NET_TYPE_ETHERNET && net->downscript)
+virNetDevRunEthernetScript(net->ifname, net->downscript);
 }
 
 for (i = 0; i < def->nfss; i++) {
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 5608566d69..f94f518f4d 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4663,6 +4663,9 @@ qemuDomainRemoveNetDevice(virQEMUDriverPtr driver,
 virDomainNetReleaseActualDevice(conn, vm->def, net);
 else
 VIR_WARN("Unable to release network device '%s'", 
NULLSTR(net->ifname));
+} else if (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET) {
+if (net->script)
+   virNetDevRunEthernetScript(net->ifname, net->downscript);
 }
 virDomainNetDefFree(net);
 return 0;
diff --git a/tests/qemuxml2argvdata/downscript.xml 
b/tests/qemuxml2argvdata/downscript.xml
new file mode 100644
index 00..4d0fb1beab
--- /dev/null
+++ b/tests/qemuxml2argvdata/downscript.xml
@@ -0,0 +1,60 @@
+
+  QEMUGuest1
+  c7a5fdbd-edaf-9455-926a-d65c16db1809
+  219136
+  219136
+  1
+  
+hvm
+
+  
+  
+qemu64
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+  
+
+
+  
+
+
+  
+
+
+
+  
+
+
+  
+  
+  
+
+
+  
+  
+  
+  
+
+
+  
+  
+  
+  
+  
+
+
+
+
+  
+
+  
+
diff --git a/tests/qemuxml2xmloutdata/downscript.xml 
b/tests/qemuxml2xmloutdata/downscript.xml
new file mode 100644
index 00..4d0fb1beab
--- /dev/null
+++ b/tests/qemuxml2xmloutdata/downscript.xml
@@ -0,0 +1,60 @@
+
+  QEMUGuest1
+  c7a5fdbd-edaf-9455-926a-d65c16db1809
+  219136
+  219136
+  1
+  
+hvm
+
+  
+  
+qemu64
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+  
+
+
+  
+
+
+  
+
+
+
+  
+
+
+  
+  
+  
+
+
+  
+  
+  
+  
+
+
+  
+  
+  
+  
+  
+
+
+
+
+  
+
+  
+
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
index 033f81013e..dcc7b29ded 100644
--- a/tests/qemuxml2xmltest.c
+++ b/tests/qemuxml2xmltest.c
@@ -1478,6 +1478,7 @@ mymain(void)
 DO_TEST_CAPS_ARCH_LATEST("x86_64-default-cpu-tcg-q35-4.2", "x86_64");
 
 DO_TEST_CAPS_LATEST("virtio-9p-multidevs");
+DO_TEST("downscript", NONE);
 
 if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
 virFileDeleteTree(fakerootdir);
-- 
2.23.0



[PATCH v2 1/3] interface: introduce downscript

2020-05-25 Thread Chen Hanxiao
https://gitlab.com/libvirt/libvirt/-/issues/13

Add support for downscript:







Signed-off-by: Chen Hanxiao 
---
 docs/schemas/domaincommon.rng | 8 
 src/conf/domain_conf.c| 9 +
 src/conf/domain_conf.h| 1 +
 3 files changed, 18 insertions(+)

diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 9d60b090f3..6727cd743b 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -3191,6 +3191,14 @@
   
 
   
+  
+
+  
+
+  
+  
+
+  
   
 
   
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c201fc901d..1406cf079e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -2520,6 +2520,7 @@ virDomainNetDefClear(virDomainNetDefPtr def)
 VIR_FREE(def->teaming.persistent);
 VIR_FREE(def->virtPortProfile);
 VIR_FREE(def->script);
+VIR_FREE(def->downscript);
 VIR_FREE(def->domain_name);
 VIR_FREE(def->ifname);
 VIR_FREE(def->ifname_guest);
@@ -11977,6 +11978,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 g_autofree char *ifname_guest = NULL;
 g_autofree char *ifname_guest_actual = NULL;
 g_autofree char *script = NULL;
+g_autofree char *downscript = NULL;
 g_autofree char *address = NULL;
 g_autofree char *port = NULL;
 g_autofree char *localaddr = NULL;
@@ -12149,6 +12151,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 } else if (!script &&
virXMLNodeNameEqual(cur, "script")) {
 script = virXMLPropString(cur, "path");
+} else if (!downscript &&
+   virXMLNodeNameEqual(cur, "downscript")) {
+downscript = virXMLPropString(cur, "path");
 } else if (!domain_name &&
virXMLNodeNameEqual(cur, "backenddomain")) {
 domain_name = virXMLPropString(cur, "name");
@@ -12482,6 +12487,8 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 
 if (script != NULL)
 def->script = g_steal_pointer();
+if (downscript != NULL)
+def->downscript = g_steal_pointer();
 if (domain_name != NULL)
 def->domain_name = g_steal_pointer(_name);
 if (ifname != NULL)
@@ -26567,6 +26574,8 @@ virDomainNetDefFormat(virBufferPtr buf,
 
 virBufferEscapeString(buf, "\n",
   def->script);
+virBufferEscapeString(buf, "\n",
+  def->downscript);
 virBufferEscapeString(buf, "\n", 
def->domain_name);
 
 if (def->ifname &&
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index ddc75d8de2..e152c599ca 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1055,6 +1055,7 @@ struct _virDomainNetDef {
 unsigned long sndbuf;
 } tune;
 char *script;
+char *downscript;
 char *domain_name; /* backend domain name */
 char *ifname; /* interface name on the host () */
 int managed_tap; /* enum virTristateBool - ABSENT == YES */
-- 
2.23.0



Re:Re: [PATCH 0/3] Support network interface downscript

2020-05-22 Thread Chen Hanxiao



At 2020-05-22 23:09:21, "Michal Privoznik"  wrote:
>On 5/21/20 3:59 PM, Chen Hanxiao wrote:
>> QEMU has the ability to run a script when a NIC is brought up and down.
>> Libvirt only enables use of the up script at this time.
>> This series add support for postscript when NIC is down/detached.
>> 
>> Chen Hanxiao (3):
>>downscript: Support network interface downscript
>>downscript: add test case
>>doc: downscript: updating the documentation
>> 
>>   docs/formatdomain.html.in   |  6 ++-
>>   docs/schemas/domaincommon.rng   |  8 
>>   src/conf/domain_conf.c  |  9 
>>   src/conf/domain_conf.h  |  1 +
>>   src/qemu/qemu_extdevice.c   |  4 ++
>>   src/qemu/qemu_hotplug.c |  6 +++
>>   tests/qemuxml2argvdata/downscript.xml   | 60 +
>>   tests/qemuxml2xmloutdata/downscript.xml | 60 +
>>   tests/qemuxml2xmltest.c |  1 +
>>   9 files changed, 154 insertions(+), 1 deletion(-)
>>   create mode 100644 tests/qemuxml2argvdata/downscript.xml
>>   create mode 100644 tests/qemuxml2xmloutdata/downscript.xml
>> 
>
>I don't mean to be picky, especially with new contributors (well, you 
>have one contribution already, exactly one month ago). Anyway, we 
>usually structure patches differently. In the first patch we add XML 
>parsing, formatting, RNG change, documentation and xml2xml test case. In 
>the second the feature is implemented in the driver and xml2argv test 
>case is introduced. In the third patch the news.xml is adjusted.
>
>The reason is that this way it is easier for downstream maintainers to 
>backport patches.
>
>Mind posting a v2 which follows that? Apart from a small nit in 1/3 the 
>rest looks good.
>

I've contributed to libvirt for years(from another email), but did not send
patched for years either :).


I'll post a v2 to address all the comments aroud the weekends.


Regards,
- Hanxiao

[PATCH 2/3] downscript: add test case

2020-05-21 Thread Chen Hanxiao
add a testcase for new element.

Signed-off-by: Chen Hanxiao 
---
 tests/qemuxml2argvdata/downscript.xml   | 60 +
 tests/qemuxml2xmloutdata/downscript.xml | 60 +
 tests/qemuxml2xmltest.c |  1 +
 3 files changed, 121 insertions(+)
 create mode 100644 tests/qemuxml2argvdata/downscript.xml
 create mode 100644 tests/qemuxml2xmloutdata/downscript.xml

diff --git a/tests/qemuxml2argvdata/downscript.xml 
b/tests/qemuxml2argvdata/downscript.xml
new file mode 100644
index 00..70c5f1d376
--- /dev/null
+++ b/tests/qemuxml2argvdata/downscript.xml
@@ -0,0 +1,60 @@
+
+  QEMUGuest1
+  c7a5fdbd-edaf-9455-926a-d65c16db1809
+  219136
+  219136
+  1
+  
+hvm
+
+  
+  
+qemu64
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+  
+
+
+  
+
+
+  
+
+
+
+  
+
+
+  
+  
+  
+
+
+  
+  
+  
+  
+
+
+  
+  
+  
+  
+  
+
+
+
+
+  
+
+  
+
diff --git a/tests/qemuxml2xmloutdata/downscript.xml 
b/tests/qemuxml2xmloutdata/downscript.xml
new file mode 100644
index 00..70c5f1d376
--- /dev/null
+++ b/tests/qemuxml2xmloutdata/downscript.xml
@@ -0,0 +1,60 @@
+
+  QEMUGuest1
+  c7a5fdbd-edaf-9455-926a-d65c16db1809
+  219136
+  219136
+  1
+  
+hvm
+
+  
+  
+qemu64
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+  
+
+
+  
+
+
+  
+
+
+
+  
+
+
+  
+  
+  
+
+
+  
+  
+  
+  
+
+
+  
+  
+  
+  
+  
+
+
+
+
+  
+
+  
+
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
index 033f81013e..dcc7b29ded 100644
--- a/tests/qemuxml2xmltest.c
+++ b/tests/qemuxml2xmltest.c
@@ -1478,6 +1478,7 @@ mymain(void)
 DO_TEST_CAPS_ARCH_LATEST("x86_64-default-cpu-tcg-q35-4.2", "x86_64");
 
 DO_TEST_CAPS_LATEST("virtio-9p-multidevs");
+DO_TEST("downscript", NONE);
 
 if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
 virFileDeleteTree(fakerootdir);
-- 
2.23.0



[PATCH 1/3] downscript: Support network interface downscript

2020-05-21 Thread Chen Hanxiao
https://gitlab.com/libvirt/libvirt/-/issues/13

Add support for downscript:







Signed-off-by: Chen Hanxiao 
---
 docs/schemas/domaincommon.rng | 8 
 src/conf/domain_conf.c| 9 +
 src/conf/domain_conf.h| 1 +
 src/qemu/qemu_extdevice.c | 4 
 src/qemu/qemu_hotplug.c   | 6 ++
 5 files changed, 28 insertions(+)

diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 9d60b090f3..6727cd743b 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -3191,6 +3191,14 @@
   
 
   
+  
+
+  
+
+  
+  
+
+  
   
 
   
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c201fc901d..1406cf079e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -2520,6 +2520,7 @@ virDomainNetDefClear(virDomainNetDefPtr def)
 VIR_FREE(def->teaming.persistent);
 VIR_FREE(def->virtPortProfile);
 VIR_FREE(def->script);
+VIR_FREE(def->downscript);
 VIR_FREE(def->domain_name);
 VIR_FREE(def->ifname);
 VIR_FREE(def->ifname_guest);
@@ -11977,6 +11978,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 g_autofree char *ifname_guest = NULL;
 g_autofree char *ifname_guest_actual = NULL;
 g_autofree char *script = NULL;
+g_autofree char *downscript = NULL;
 g_autofree char *address = NULL;
 g_autofree char *port = NULL;
 g_autofree char *localaddr = NULL;
@@ -12149,6 +12151,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 } else if (!script &&
virXMLNodeNameEqual(cur, "script")) {
 script = virXMLPropString(cur, "path");
+} else if (!downscript &&
+   virXMLNodeNameEqual(cur, "downscript")) {
+downscript = virXMLPropString(cur, "path");
 } else if (!domain_name &&
virXMLNodeNameEqual(cur, "backenddomain")) {
 domain_name = virXMLPropString(cur, "name");
@@ -12482,6 +12487,8 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 
 if (script != NULL)
 def->script = g_steal_pointer();
+if (downscript != NULL)
+def->downscript = g_steal_pointer();
 if (domain_name != NULL)
 def->domain_name = g_steal_pointer(_name);
 if (ifname != NULL)
@@ -26567,6 +26574,8 @@ virDomainNetDefFormat(virBufferPtr buf,
 
 virBufferEscapeString(buf, "\n",
   def->script);
+virBufferEscapeString(buf, "\n",
+  def->downscript);
 virBufferEscapeString(buf, "\n", 
def->domain_name);
 
 if (def->ifname &&
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index ddc75d8de2..e152c599ca 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1055,6 +1055,7 @@ struct _virDomainNetDef {
 unsigned long sndbuf;
 } tune;
 char *script;
+char *downscript;
 char *domain_name; /* backend domain name */
 char *ifname; /* interface name on the host () */
 int managed_tap; /* enum virTristateBool - ABSENT == YES */
diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c
index 2096272761..4962521de4 100644
--- a/src/qemu/qemu_extdevice.c
+++ b/src/qemu/qemu_extdevice.c
@@ -213,6 +213,7 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
virDomainObjPtr vm)
 {
 virDomainDefPtr def = vm->def;
+virDomainNetType actualType;
 size_t i;
 
 if (qemuExtDevicesInitPaths(driver, def) < 0)
@@ -230,10 +231,13 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
 
 for (i = 0; i < def->nnets; i++) {
 virDomainNetDefPtr net = def->nets[i];
+actualType = virDomainNetGetActualType(net);
 qemuSlirpPtr slirp = QEMU_DOMAIN_NETWORK_PRIVATE(net)->slirp;
 
 if (slirp)
 qemuSlirpStop(slirp, vm, driver, net);
+if (actualType == VIR_DOMAIN_NET_TYPE_ETHERNET && net->downscript)
+virNetDevRunEthernetScript(net->ifname, net->downscript);
 }
 
 for (i = 0; i < def->nfss; i++) {
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 5608566d69..97c6508a55 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -5942,6 +5942,12 @@ qemuDomainDetachDeviceLive(virDomainObjPtr vm,
 ret = qemuDomainRemoveDevice(driver, vm, );
 }
 
+if ((virDomainDeviceType)match->type == VIR_DOMAIN_DEVICE_NET) {
+virDomainNetDefPtr net = detach.data.net;
+if (net->script)
+virNetDevRunEthernetScript(net->ifname, net->downscript);
+}
+
  cleanup:
 if (!async)
 qemuDomainResetDeviceRemoval(vm);
-- 
2.23.0



[PATCH 0/3] Support network interface downscript

2020-05-21 Thread Chen Hanxiao
QEMU has the ability to run a script when a NIC is brought up and down. 
Libvirt only enables use of the up script at this time.
This series add support for postscript when NIC is down/detached.

Chen Hanxiao (3):
  downscript: Support network interface downscript
  downscript: add test case
  doc: downscript: updating the documentation

 docs/formatdomain.html.in   |  6 ++-
 docs/schemas/domaincommon.rng   |  8 
 src/conf/domain_conf.c  |  9 
 src/conf/domain_conf.h  |  1 +
 src/qemu/qemu_extdevice.c   |  4 ++
 src/qemu/qemu_hotplug.c |  6 +++
 tests/qemuxml2argvdata/downscript.xml   | 60 +
 tests/qemuxml2xmloutdata/downscript.xml | 60 +
 tests/qemuxml2xmltest.c |  1 +
 9 files changed, 154 insertions(+), 1 deletion(-)
 create mode 100644 tests/qemuxml2argvdata/downscript.xml
 create mode 100644 tests/qemuxml2xmloutdata/downscript.xml

-- 
2.23.0



[PATCH 3/3] doc: downscript: updating the documentation

2020-05-21 Thread Chen Hanxiao
We can give a postscript for some netdev.

Signed-off-by: Chen Hanxiao 
---
 docs/formatdomain.html.in | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 23eb029234..4e2320d537 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -5879,7 +5879,10 @@
 
   After creating/opening the tap device, an optional shell script
   (given in the path attribute of
-  the script element) will be run; this can
+  the script element) will be run;
+  Also, after detaching/closing the tap device, an optional shell
+  script (given in the path attribute of
+  the downscript element) will be run; this can
   be used to do whatever extra host network integration is
   required.
 
@@ -5889,6 +5892,7 @@
 devices
   interface type='ethernet'
 script path='/etc/qemu-ifup-mynet'/
+downscript path='/etc/qemu-ifdown-mynet'/
   /interface
   ...
   interface type='ethernet'
-- 
2.23.0



[PATCH] RFC: Support network interface downscript

2020-05-14 Thread Chen Hanxiao
https://gitlab.com/libvirt/libvirt/-/issues/13

Add support for downscript:







But -net ...,script=/etc/qemu-ifup has some limitation:
https://github.com/qemu/qemu/blob/035b448b84f3557206abc44d786c5d3db2638f7d/net/tap.c#L818
Maybe virNetDevRunEthernetScript is another choice.

Signed-off-by: Chen Hanxiao 
---
 src/conf/domain_conf.c| 7 +++
 src/conf/domain_conf.h| 1 +
 src/qemu/qemu_extdevice.c | 2 ++
 3 files changed, 10 insertions(+)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c201fc9..5cb39a3 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -2520,6 +2520,7 @@ virDomainNetDefClear(virDomainNetDefPtr def)
 VIR_FREE(def->teaming.persistent);
 VIR_FREE(def->virtPortProfile);
 VIR_FREE(def->script);
+VIR_FREE(def->downscript);
 VIR_FREE(def->domain_name);
 VIR_FREE(def->ifname);
 VIR_FREE(def->ifname_guest);
@@ -11977,6 +11978,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 g_autofree char *ifname_guest = NULL;
 g_autofree char *ifname_guest_actual = NULL;
 g_autofree char *script = NULL;
+g_autofree char *downscript = NULL;
 g_autofree char *address = NULL;
 g_autofree char *port = NULL;
 g_autofree char *localaddr = NULL;
@@ -12149,6 +12151,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 } else if (!script &&
virXMLNodeNameEqual(cur, "script")) {
 script = virXMLPropString(cur, "path");
+} else if (!downscript &&
+   virXMLNodeNameEqual(cur, "downscript")) {
+downscript = virXMLPropString(cur, "path");
 } else if (!domain_name &&
virXMLNodeNameEqual(cur, "backenddomain")) {
 domain_name = virXMLPropString(cur, "name");
@@ -12482,6 +12487,8 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 
 if (script != NULL)
 def->script = g_steal_pointer();
+if (downscript != NULL)
+def->downscript = g_steal_pointer();
 if (domain_name != NULL)
 def->domain_name = g_steal_pointer(_name);
 if (ifname != NULL)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index ddc75d8..e152c59 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1055,6 +1055,7 @@ struct _virDomainNetDef {
 unsigned long sndbuf;
 } tune;
 char *script;
+char *downscript;
 char *domain_name; /* backend domain name */
 char *ifname; /* interface name on the host () */
 int managed_tap; /* enum virTristateBool - ABSENT == YES */
diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c
index 2096272..f8e440a 100644
--- a/src/qemu/qemu_extdevice.c
+++ b/src/qemu/qemu_extdevice.c
@@ -234,6 +234,8 @@ qemuExtDevicesStop(virQEMUDriverPtr driver,
 
 if (slirp)
 qemuSlirpStop(slirp, vm, driver, net);
+if (net->downscript)
+virNetDevRunEthernetScript(net->ifname, net->downscript);
 }
 
 for (i = 0; i < def->nfss; i++) {
-- 
1.8.3.1



[PATCH] docs: drvqemu: trivial fix for qemu commands passthrough

2020-04-22 Thread Chen Hanxiao
element  should be the child of 

Signed-off-by: Chen Hanxiao 
---
 docs/drvqemu.html.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/drvqemu.html.in b/docs/drvqemu.html.in
index 5f412ba376..afc4ddf56d 100644
--- a/docs/drvqemu.html.in
+++ b/docs/drvqemu.html.in
@@ -578,7 +578,7 @@ mount -t cgroup none /dev/cgroup -o devices
   typically, the namespace is given the name
   of qemu.  With the namespace in place, it is then
   possible to add an element qemu:commandline
-  under driver, with the following sub-elements
+  under domain, with the following sub-elements
   repeated as often as needed:
 
   
-- 
2.23.0




Re: [libvirt] [PATCH v3] qemu: Remove network type limitation for qemuARPGetInterfaces

2018-09-25 Thread Chen Hanxiao


At 2018-09-25 17:37:24, "Lin Ma"  wrote:
>Let's ignore the checking of interface type when we call the function
>qemuARPGetInterfaces to get IP from host's arp table.
>
>Based on suggestion from Laine.
>https://www.redhat.com/archives/libvir-list/2018-September/msg00684.html
>
>Signed-off-by: Lin Ma 
>---
> src/qemu/qemu_driver.c | 3 ---
> 1 file changed, 3 deletions(-)
>
>diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
>index 10d6bca186..3110e74e0e 100644
>--- a/src/qemu/qemu_driver.c
>+++ b/src/qemu/qemu_driver.c
>@@ -20945,9 +20945,6 @@ qemuARPGetInterfaces(virDomainObjPtr vm,
> goto cleanup;
> 
> for (i = 0; i < vm->def->nnets; i++) {
>-if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
>-continue;
>-
> virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
> for (j = 0; j < table->n; j++) {
> virArpTableEntry entry = table->t[j];
>-- 
>2.19.0

Reviewed-by: Chen Hanxiao 

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 1/2] qemu: Remove network type limitation for qemuARPGetInterfaces

2018-09-11 Thread Chen Hanxiao


At 2018-09-11 13:51:43, "Lin Ma"  wrote:   
>On 09/10/2018 02:55 PM, Chen Hanxiao   wrote:  
>
>At 2018-09-07 18:44:53, "Lin Ma"  wrote:
>>When we call qemuARPGetInterfaces to get IP from host's arp table, The
>>iface's type has nothing to do with it, We should allow all of type.
>>
>>Signed-off-by: Lin Ma 
>
>Hi,
>   with my limited tests, 
>   we can't get useful info by arp query from other kind of interface types.
>  
>
>
>
>IMO, As long as the host interface and the guest interface
>  communicated
>
>through osi layer3 or above, Host kernel will maintaince
>  the corresponding
>
>  ip <-> mac entry in arp able.
>
>

A lot of network backend, such as vrouter, dpdk, the package from guest os
did not come back to the host's protocol stack.
We can't see them in host's arp table.

>
>What you mentioned above remind me one thing: I forgot to
>  mention that
>
>if the switch doesn't support hairpin mode, then the
>  virtual interfaces
>
>which backend is mactap can't communicate with host,
>  that means the host
>
>arp table won't include the ip <-> mac information
>  of such virtual interfaces.
>
>In this case, the qemuARPGetInterfaces
>  can't get IP.
>
>
>
>So I'd like to change the code to only ignore
>  VIR_DOMAIN_NET_TYPE_DIRECT

Maybe adding VIR_DOMAIN_NET_TYPE_ETHERNET and VIR_DOMAIN_NET_TYPE_VHOSTUSER?

Regards,
- Chen

>
>in patch V2, For anyelse net types, the
>  code will iterate the arp table for
>
>loop to try to search if there is a
>  matched entry.
>
>
>
>what do you think?
>
>
>
>Thanks,
>
>Lin
  


--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 1/2] qemu: Remove network type limitation for qemuARPGetInterfaces

2018-09-10 Thread Chen Hanxiao

At 2018-09-07 18:44:53, "Lin Ma"  wrote:
>When we call qemuARPGetInterfaces to get IP from host's arp table, The
>iface's type has nothing to do with it, We should allow all of type.
>
>Signed-off-by: Lin Ma 

Hi,
   with my limited tests, 
   we can't get useful info by arp query from other kind of interface types.


Regards,
- Chen--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 2/2] virarptable: Return a virArpTablePtr when the nlmsghdr for loop is over

2018-09-10 Thread Chen Hanxiao


At 2018-09-07 18:44:54, "Lin Ma"  wrote:
>commit b00c9c39 removed the label end_of_netlink_messages and 'return
>table' statement, It causes the function virArpTableGet doesn't return
>a proper virArpTable pointer.
>
>How to reproduce:
> # virsh domiflist sles12sp3
>Interface  Type   Source Model   MAC
>---
>vnet0  networkdefaultvirtio  52:54:00:cd:02:e6
>
> # virsh domifaddr sles12sp3 --source arp
>error: Failed to query for interfaces addresses
>error: An error occurred, but the cause is unknown
>
>It seems that the "if (nh->nlmsg_type == NLMSG_DONE)" statement won't be
>meted. So this patch adds 'return table' when the iterations of nlmsghdr
>for loop is over.
>
>Signed-off-by: Lin Ma 
>---

It seemed that the cleanup series delete our return value.

Reviewed-by: Chen Hanxiao 

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] util:Fix with process number and pid file do not match

2018-07-03 Thread Chen Hanxiao

At 2018-07-03 15:36:19, "Michal Prívozník"  wrote:
>On 07/02/2018 01:08 PM, dubo163 wrote:
>> From: dubobo 
>> 
>> the libvirtd pid file is not match the os process pid number
>> which is smaller than before.
>> 
>> this would be exist if the libvirtd process coredump or the os
>> process was killed which the next pid number is smaller.
>> 
>> you can be also edit the pid file to write the longer number than
>> before,then restart the libvirtd service.
>> 
>> Signed-off-by: dubobo 
>
>I'm sorry, but this has to be your legal name, which I believe dubobo is
>not. Also as I was pointed out earlier, the name of the author of the
>patch has to be legal name.

Guess that a space needed between family name and given name.
Such as "du bobo" , "Du Bobo" or "Bobo Du"

As a Chinese, the author's name is  a common given name.
One of my friend had the name with that same pronounciation : )

Regards,
- Chen

>
>> ---
>>  src/util/virpidfile.c | 6 ++
>>  1 file changed, 6 insertions(+)
>> 
>> diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
>> index 58ab29f..8b0ff99 100644
>> --- a/src/util/virpidfile.c
>> +++ b/src/util/virpidfile.c
>> @@ -445,6 +445,12 @@ int virPidFileAcquirePath(const char *path,
>>  }
>>  
>>  snprintf(pidstr, sizeof(pidstr), "%lld", (long long) pid);
>> +if (ftruncate(fd, 0) < 0) {
>> +VIR_FORCE_CLOSE(fd);
>> +return -1;
>
>So if ftruncate() fails, caller sees -1 but no error message. This is
>not nice because users then have no idea what went wrong. All they see
>is a failed attempt to start libvirtd. We need virReportSystemError() here.
>
>> +}
>> +
>> +lseek(fd, 0, SEEK_SET);
>
>This is pretty useless. Since open() nothing was written to/read from
>the pidfile. So we don't really need to seek in it.
>
>Michal
>
>--
>libvir-list mailing list
>libvir-list@redhat.com
>https://www.redhat.com/mailman/listinfo/libvir-list

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH] set-lifecycle-action: add description of type and action

2018-06-26 Thread Chen Hanxiao


At 2018-06-21 19:28:55, "Chen Hanxiao"  wrote:
>From: Chen Hanxiao 
>
>In [1],  are described as "on_poweroff",
>   "on_reboot", "on_crash".
>   but we accept "poweroff", "reboot" and "crash".
>This patch adds docs about them.
>
>[1]: https://libvirt.org/formatdomain.html#elementsEvents
>
>Signed-off-by: Chen Hanxiao 
>---

ping

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] set-lifecycle-action: add description of type and action

2018-06-21 Thread Chen Hanxiao
From: Chen Hanxiao 

In [1],  are described as "on_poweroff",
   "on_reboot", "on_crash".
   but we accept "poweroff", "reboot" and "crash".
This patch adds docs about them.

[1]: https://libvirt.org/formatdomain.html#elementsEvents

Signed-off-by: Chen Hanxiao 
---
 tools/virsh.pod | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/virsh.pod b/tools/virsh.pod
index 7cb8c8a6e4..1b479f318d 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2414,8 +2414,12 @@ the B command.
 =item B I I I
 [[I<--config>] [I<--live>] | [I<--current>]]
 
-Set the lifecycle I for specified lifecycle I. For the list of
-lifecycle types and actions and possible combinations see the documentation at
+Set the lifecycle I for specified lifecycle I.
+The valid I are "poweroff", "reboot" and "crash", each of them
+allow valid I are "destroy", "restart", "rename-restart", "preserve".
+For I "crash", additional actions "coredump-destroy"
+and "coredump-restart" are supported. For the list of lifecycle types
+and actions and possible combinations see the documentation at
 L<https://libvirt.org/formatdomain.html#elementsEvents>.
 
 =item B I I I [I<--encrypted>]
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 2/4] cmdDomblkinfo: add --all to show all block devices info

2018-06-19 Thread Chen Hanxiao
From: Chen Hanxiao 

This patch introduces --all to show all block devices info
of guests like:

virsh # domblkinfo w08 --all
Target CapacityAllocation  Physical
---
hda42949672960 9878110208  9878110208
vda10737418240 10736439296 10737418240

Target CapacityAllocation  Physical
---
hda40.000 GiB  9.200 GiB   9.200 GiB
vda10.000 GiB  9.999 GiB   10.000 GiB

Signed-off-by: Chen Hanxiao 
---
v3:
  check error code on network disk
v2:
  add support --human to --all
v1.1:
  fix self-test

 tools/virsh-domain-monitor.c | 128 +--
 tools/virsh.pod  |   5 +-
 2 files changed, 112 insertions(+), 21 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index daa86e8310..43e39f79c1 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -388,8 +388,7 @@ static const vshCmdInfo info_domblkinfo[] = {
 static const vshCmdOptDef opts_domblkinfo[] = {
 VIRSH_COMMON_OPT_DOMAIN_FULL(0),
 {.name = "device",
- .type = VSH_OT_DATA,
- .flags = VSH_OFLAG_REQ,
+ .type = VSH_OT_STRING,
  .completer = virshDomainDiskTargetCompleter,
  .help = N_("block device")
 },
@@ -397,30 +396,67 @@ static const vshCmdOptDef opts_domblkinfo[] = {
  .type = VSH_OT_BOOL,
  .help = N_("Human readable output")
 },
+{.name = "all",
+ .type = VSH_OT_BOOL,
+ .help = N_("display all block devices info")
+},
 {.name = NULL}
 };
 
 static void
 cmdDomblkinfoPrint(vshControl *ctl,
const virDomainBlockInfo *info,
-   bool human)
+   const char *device,
+   bool human, bool title)
 {
+char *cap = NULL, *alloc = NULL, *phy = NULL;
+
+if (title) {
+vshPrintExtra(ctl, "%-10s %-15s %-15s %-15s\n", _("Target"),
+  _("Capacity"), _("Allocation"), _("Physical"));
+vshPrintExtra(ctl, "-"
+  "\n");
+return;
+}
+
 if (!human) {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
+if (device) {
+vshPrint(ctl, "%-10s %-15llu %-15llu %-15llu\n", device,
+ info->capacity, info->allocation, info->physical);
+} else {
+vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
+vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
+vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
+}
 } else {
-double val;
-const char *unit;
-
-val = vshPrettyCapacity(info->capacity, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
-val = vshPrettyCapacity(info->allocation, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
-val = vshPrettyCapacity(info->physical, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+double val_cap, val_alloc, val_phy;
+const char *unit_cap, *unit_alloc, *unit_phy;
+
+val_cap = vshPrettyCapacity(info->capacity, _cap);
+val_alloc = vshPrettyCapacity(info->allocation, _alloc);
+val_phy = vshPrettyCapacity(info->physical, _phy);
+if (device) {
+if (virAsprintf(, "%.3lf %s", val_cap, unit_cap) < 0 ||
+virAsprintf(, "%.3lf %s", val_alloc, unit_alloc) < 0 ||
+virAsprintf(, "%.3lf %s", val_phy, unit_phy) < 0)
+goto cleanup;
+
+vshPrint(ctl, "%-10s %-15s %-15s %-15s\n",
+ device, cap, alloc, phy);
+} else {
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"),
+ val_cap, unit_cap);
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"),
+ val_alloc, unit_alloc);
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"),
+ val_phy, unit_phy);
+}
 }
 
+ cleanup:
+VIR_FREE(cap);
+VIR_FREE(alloc);
+VIR_FREE(phy);
 }
 
 static bool
@@ -430,25 +466,77 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 virDo

[libvirt] [PATCH v3 3/4] cmdDomblkinfoPrint: support printing "-" for invalid virDomainBlockInfo

2018-06-19 Thread Chen Hanxiao
From: Chen Hanxiao 

For inactive domain, we'll set virDomainBlockInfo to 0
if specific error code got.
Print "-" to show the value should be ignored in this scenario.

Signed-off-by: Chen Hanxiao 
---
 tools/virsh-domain-monitor.c | 73 
 1 file changed, 49 insertions(+), 24 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 43e39f79c1..3acf5450b3 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -410,6 +410,15 @@ cmdDomblkinfoPrint(vshControl *ctl,
bool human, bool title)
 {
 char *cap = NULL, *alloc = NULL, *phy = NULL;
+bool invalid = false;
+
+struct blockInfoText {
+char *capacity;
+char *allocation;
+char *physical;
+};
+
+struct blockInfoText *blkInfoText = NULL;
 
 if (title) {
 vshPrintExtra(ctl, "%-10s %-15s %-15s %-15s\n", _("Target"),
@@ -419,15 +428,23 @@ cmdDomblkinfoPrint(vshControl *ctl,
 return;
 }
 
-if (!human) {
-if (device) {
-vshPrint(ctl, "%-10s %-15llu %-15llu %-15llu\n", device,
- info->capacity, info->allocation, info->physical);
-} else {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
-}
+invalid = info->capacity == 0 &&
+  info->allocation == 0 &&
+  info->physical == 0;
+blkInfoText = vshCalloc(ctl, 1, sizeof(*blkInfoText));
+
+if (invalid) {
+blkInfoText->capacity = vshStrdup(ctl, "-");
+blkInfoText->allocation = vshStrdup(ctl, "-");
+blkInfoText->physical = vshStrdup(ctl, "-");
+} else if (!human) {
+if (virAsprintf(>capacity, "%llu",
+info->capacity) < 0 ||
+virAsprintf(>allocation, "%llu",
+info->allocation) < 0 ||
+virAsprintf(>physical, "%llu",
+info->physical) < 0)
+goto cleanup;
 } else {
 double val_cap, val_alloc, val_phy;
 const char *unit_cap, *unit_alloc, *unit_phy;
@@ -435,28 +452,36 @@ cmdDomblkinfoPrint(vshControl *ctl,
 val_cap = vshPrettyCapacity(info->capacity, _cap);
 val_alloc = vshPrettyCapacity(info->allocation, _alloc);
 val_phy = vshPrettyCapacity(info->physical, _phy);
-if (device) {
-if (virAsprintf(, "%.3lf %s", val_cap, unit_cap) < 0 ||
-virAsprintf(, "%.3lf %s", val_alloc, unit_alloc) < 0 ||
-virAsprintf(, "%.3lf %s", val_phy, unit_phy) < 0)
-goto cleanup;
 
-vshPrint(ctl, "%-10s %-15s %-15s %-15s\n",
- device, cap, alloc, phy);
-} else {
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"),
- val_cap, unit_cap);
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"),
- val_alloc, unit_alloc);
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"),
- val_phy, unit_phy);
-}
+if (virAsprintf(>capacity, "%.3lf %s",
+val_cap, unit_cap) < 0 ||
+virAsprintf(>allocation, "%.3lf %s",
+val_alloc, unit_alloc) < 0 ||
+virAsprintf(>physical, "%.3lf %s",
+val_phy, unit_phy) < 0)
+goto cleanup;
+}
+
+if (device) {
+vshPrint(ctl, "%-10s %-15s %-15s %-15s\n", device,
+ blkInfoText->capacity, blkInfoText->allocation,
+ blkInfoText->physical);
+} else {
+vshPrint(ctl, "%-15s %s\n", _("Capacity:"), blkInfoText->capacity);
+vshPrint(ctl, "%-15s %s\n", _("Allocation:"), blkInfoText->allocation);
+vshPrint(ctl, "%-15s %s\n", _("Physical:"), blkInfoText->physical);
 }
 
  cleanup:
 VIR_FREE(cap);
 VIR_FREE(alloc);
 VIR_FREE(phy);
+if (blkInfoText) {
+VIR_FREE(blkInfoText->capacity);
+VIR_FREE(blkInfoText->allocation);
+VIR_FREE(blkInfoText->physical);
+}
+VIR_FREE(blkInfoText);
 }
 
 static bool
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 0/4] cmdDomblkinfo: introduce --all to show all block devices info

2018-06-19 Thread Chen Hanxiao
This series introduce --all to cmdDomblkinfo to show
all block devices info in one cmd.
Likes a combination of domblklist and domblkinfo.

v3:
  check error code on network disk
v2:
  add support --human for --all
v1.1
  fix a self test

Chen Hanxiao (4):
  cmdDomblkinfo: introduce helper cmdDomblkinfoPrint
  cmdDomblkinfo: add --all to show all block devices info
  cmdDomblkinfoPrint: support printing "-" for invalid
virDomainBlockInfo
  news: add cmdDomblkinfo --all option

 docs/news.xml|  10 +++
 tools/virsh-domain-monitor.c | 160 ++-
 tools/virsh.pod  |   5 +-
 3 files changed, 155 insertions(+), 20 deletions(-)

-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 4/4] news: add cmdDomblkinfo --all option

2018-06-19 Thread Chen Hanxiao
From: Chen Hanxiao 

Update news for cmdDomblkinfo --all option.

Signed-off-by: Chen Hanxiao 
---
v3:
  update descriptions

 docs/news.xml | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index 08e5dcbda3..9bf7442047 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -204,6 +204,16 @@
   secret-event, pool-event and nodedev-event)
 
   
+  
+
+  virsh: Add --all to domblkinfo command
+
+
+  Alter the domblkinfo command to add the option
+  --all in order to display the size details of each domain
+  block device from one command in a output table.
+
+  
 
 
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 1/4] cmdDomblkinfo: introduce helper cmdDomblkinfoPrint

2018-06-19 Thread Chen Hanxiao
From: Chen Hanxiao 

Introduce helper cmdDomblkinfoPrint for printing.

Reviewed-by: John Ferlan 
Signed-off-by: Chen Hanxiao 
---
 tools/virsh-domain-monitor.c | 39 ++--
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 8cbb3db37c..daa86e8310 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -400,6 +400,29 @@ static const vshCmdOptDef opts_domblkinfo[] = {
 {.name = NULL}
 };
 
+static void
+cmdDomblkinfoPrint(vshControl *ctl,
+   const virDomainBlockInfo *info,
+   bool human)
+{
+if (!human) {
+vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
+vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
+vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
+} else {
+double val;
+const char *unit;
+
+val = vshPrettyCapacity(info->capacity, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
+val = vshPrettyCapacity(info->allocation, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
+val = vshPrettyCapacity(info->physical, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+}
+
+}
+
 static bool
 cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 {
@@ -420,21 +443,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 
 human = vshCommandOptBool(cmd, "human");
 
-if (!human) {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
-} else {
-double val;
-const char *unit;
-
-val = vshPrettyCapacity(info.capacity, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
-val = vshPrettyCapacity(info.allocation, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
-val = vshPrettyCapacity(info.physical, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
-}
+cmdDomblkinfoPrint(ctl, , human);
 
 ret = true;
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v2 2/3] cmdDomblkinfo: add --all to show all block devices info

2018-06-15 Thread Chen Hanxiao



At 2018-06-15 05:41:48, "John Ferlan"  wrote:
>
>
>On 06/11/2018 06:52 AM, Chen Hanxiao wrote:
>> From: Chen Hanxiao 
>> 
[...]
>> 
>
>Do you have networked disks in your domain configs? For a non running
>guest, t; otherwise, you would have noted:
>
># virsh domblkinfo $dom --all
>Target CapacityAllocation  Physical
>-
>vda10737418240 2086727680  10737418240
>error: internal error: missing storage backend for network files using
>iscsi protocol
>

Yes, I tested this cases.
This issue already existed for the original domblkinfo, so I didn't change this.
Maybe we should fix it in another patch.

>> diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
>> index daa86e8310..22c0b740c6 100644
...
>> +if (device) {
>> +if (virAsprintf(, "%.3lf %s", val_cap, unit_cap) < 0)
>> +goto cleanup;
>> +
>> +if (virAsprintf(, "%.3lf %s", val_alloc, unit_alloc) < 0)
>> +goto cleanup;
>> +
>> +if (virAsprintf(, "%.3lf %s", val_phy, unit_phy) < 0)
>> +goto cleanup;
>
>it would be fine I think to do:
>
>if (virAsprintf(, "%.3lf %s", val_cap, unit_cap) < 0 ||
>virAsprintf(, "%.3lf %s", val_alloc, unit_alloc) < 0 ||
>virAsprintf(, "%.3lf %s", val_phy, unit_phy) < 0)
>goto cleanup;
>
>But that's not required.
>

Looks much better, will be changed in the next series.

>> +
>> +vshPrint(ctl, "%-10s %-15s %-15s %-15s\n",
[...]

>> +ctxt->node = disks[i];
>> +target = virXPathString("string(./target/@dev)", ctxt);
>> +
>> +if (virDomainGetBlockInfo(dom, target, , 0) < 0)
>> +goto cleanup;
>
>If the domain is not running, then it's possible to return an error for
>a networked disk (e.g. )... This is
>because qemuDomainGetBlockInfo calls qemuStorageLimitsRefresh which
>calls qemuDomainStorageOpenStat and for non local storage the
>virStorageFileInitAs will eventually fail in virStorageFileBackendForType.
>
>A couple of options come to mind...
>
>... let the failure occur as is, so be it...
>
>... check the last error message for code == VIR_ERR_INTERNAL_ERROR and
>domain == VIR_FROM_STORAGE and we have a source protocol from an
>inactive domain, then assume it's a we cannot get there from here.
>
>... Other options?
>
>If we fail virDomainGetBlockInfo we could still display values as long
>as there's an appropriately placed  memset(, 0, sizeof(info)). That
>way we display only the name and 0's for everything else. Not optimal,
>but easily described in the man page that an inactive guest, using
>network protocol for storage may not be able to get the size values and
>virsh will display as 0's instead... or get more creative and display
>"-" for each size column.

I prefer this solutions.
Also, I think domblkinfo DOM DEVICE should follow this if it's a network disk.

>
>
>> +
>> +cmdDomblkinfoPrint(ctl, , target, human, false);
>> +
>> +VIR_FREE(target);
>> +}
>> +} else {
>> +if (virDomainGetBlockInfo(dom, device, , 0) < 0)
>> +goto cleanup;
>> +
>> +cmdDomblkinfoPrint(ctl, , NULL, human, false);
>> +}
>>  
>>  ret = true;
>>  
>>   cleanup:
>>  virshDomainFree(dom);
>> +VIR_FREE(target);
>> +VIR_FREE(disks);
>> +xmlXPathFreeContext(ctxt);
>> +xmlFreeDoc(xmldoc);
>>  return ret;
>>  }
>
>Taking the handle the error path option and a bit of poetic license,
>here's some diffs...

Will do in v3.
>
> char *target = NULL;
>+char *protocol = NULL;
>...
> if (all) {
>+bool active = virDomainIsActive(dom) == 1;
>+int rc;
>+
>...
> for (i = 0; i < ndisks; i++) {
> ctxt->node = disks[i];
>+protocol = virXPathString("string(./source/@protocol)", ctxt);
> target = virXPathString("string(./target/@dev)", ctxt);
>...
>-if (virDomainGetBlockInfo(dom, target, , 0) < 0)
>-goto cleanup;
>+rc = virDomainGetBlockInfo(dom, target, , 0);
>+
>+if (rc < 0) {
>+if (protocol && !active &&
>+virGetLastErrorCode() == VIR_ERR_INTERNAL_ERROR &&
>+ 

[libvirt] [PATCH] libvirt: fix a typo

2018-06-13 Thread Chen Hanxiao
From: Chen Hanxiao 

s/httsp/https

Signed-off-by: Chen Hanxiao 
---
 src/libvirt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index ffb002f4e1..52f4dd2808 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -1098,7 +1098,7 @@ virConnectOpenInternal(const char *name,
  * if not already running. This can be prevented by setting the
  * environment variable LIBVIRT_AUTOSTART=0
  *
- * URIs are documented at httsp://libvirt.org/uri.html
+ * URIs are documented at https://libvirt.org/uri.html
  *
  * virConnectClose should be used to release the resources after the connection
  * is no longer needed.
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 2/3] cmdDomblkinfo: add --all to show all block devices info

2018-06-11 Thread Chen Hanxiao
From: Chen Hanxiao 

This patch introduces --all to show all block devices info
of guests like:

virsh # domblkinfo --all
Target CapacityAllocation  Physical
---
hda42949672960 9878110208  9878110208
vda10737418240 10736439296 10737418240

# domblkinfo --all --human
Target CapacityAllocation  Physical
---
hda40.000 GiB  9.200 GiB   9.200 GiB
vda10.000 GiB  9.999 GiB   10.000 GiB

Signed-off-by: Chen Hanxiao 
---
v2:
  add support --human to --all
v1.1:
  fix self-test

 tools/virsh-domain-monitor.c | 118 +--
 tools/virsh.pod  |   5 +-
 2 files changed, 102 insertions(+), 21 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index daa86e8310..22c0b740c6 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -388,8 +388,7 @@ static const vshCmdInfo info_domblkinfo[] = {
 static const vshCmdOptDef opts_domblkinfo[] = {
 VIRSH_COMMON_OPT_DOMAIN_FULL(0),
 {.name = "device",
- .type = VSH_OT_DATA,
- .flags = VSH_OFLAG_REQ,
+ .type = VSH_OT_STRING,
  .completer = virshDomainDiskTargetCompleter,
  .help = N_("block device")
 },
@@ -397,30 +396,71 @@ static const vshCmdOptDef opts_domblkinfo[] = {
  .type = VSH_OT_BOOL,
  .help = N_("Human readable output")
 },
+{.name = "all",
+ .type = VSH_OT_BOOL,
+ .help = N_("display all block devices info")
+},
 {.name = NULL}
 };
 
 static void
 cmdDomblkinfoPrint(vshControl *ctl,
const virDomainBlockInfo *info,
-   bool human)
+   const char *device,
+   bool human, bool title)
 {
+char *cap = NULL, *alloc = NULL, *phy = NULL;
+
+if (title) {
+vshPrintExtra(ctl, "%-10s %-15s %-15s %-15s\n", _("Target"),
+  _("Capacity"), _("Allocation"), _("Physical"));
+vshPrintExtra(ctl, "-"
+  "\n");
+return;
+}
+
 if (!human) {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
+if (device) {
+vshPrint(ctl, "%-10s %-15llu %-15llu %-15llu\n", device,
+ info->capacity, info->allocation, info->physical);
+} else {
+vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
+vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
+vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
+}
 } else {
-double val;
-const char *unit;
-
-val = vshPrettyCapacity(info->capacity, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
-val = vshPrettyCapacity(info->allocation, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
-val = vshPrettyCapacity(info->physical, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+double val_cap, val_alloc, val_phy;
+const char *unit_cap, *unit_alloc, *unit_phy;
+
+val_cap = vshPrettyCapacity(info->capacity, _cap);
+val_alloc = vshPrettyCapacity(info->allocation, _alloc);
+val_phy = vshPrettyCapacity(info->physical, _phy);
+if (device) {
+if (virAsprintf(, "%.3lf %s", val_cap, unit_cap) < 0)
+goto cleanup;
+
+if (virAsprintf(, "%.3lf %s", val_alloc, unit_alloc) < 0)
+goto cleanup;
+
+if (virAsprintf(, "%.3lf %s", val_phy, unit_phy) < 0)
+goto cleanup;
+
+vshPrint(ctl, "%-10s %-15s %-15s %-15s\n",
+ device, cap, alloc, phy);
+} else {
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"),
+ val_cap, unit_cap);
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"),
+ val_alloc, unit_alloc);
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"),
+ val_phy, unit_phy);
+}
 }
 
+ cleanup:
+VIR_FREE(cap);
+VIR_FREE(alloc);
+VIR_FREE(phy);
 }
 
 static bool
@@ -430,25 +470,63 @@ cmdDomblk

[libvirt] [PATCH v2 1/3] cmdDomblkinfo: introduce helper cmdDomblkinfoPrint

2018-06-11 Thread Chen Hanxiao
From: Chen Hanxiao 

Introduce helper cmdDomblkinfoPrint for printing.

Signed-off-by: Chen Hanxiao 
---
 tools/virsh-domain-monitor.c | 39 ++--
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 8cbb3db37c..daa86e8310 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -400,6 +400,29 @@ static const vshCmdOptDef opts_domblkinfo[] = {
 {.name = NULL}
 };
 
+static void
+cmdDomblkinfoPrint(vshControl *ctl,
+   const virDomainBlockInfo *info,
+   bool human)
+{
+if (!human) {
+vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info->capacity);
+vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info->allocation);
+vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info->physical);
+} else {
+double val;
+const char *unit;
+
+val = vshPrettyCapacity(info->capacity, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
+val = vshPrettyCapacity(info->allocation, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
+val = vshPrettyCapacity(info->physical, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+}
+
+}
+
 static bool
 cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 {
@@ -420,21 +443,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 
 human = vshCommandOptBool(cmd, "human");
 
-if (!human) {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
-} else {
-double val;
-const char *unit;
-
-val = vshPrettyCapacity(info.capacity, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
-val = vshPrettyCapacity(info.allocation, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
-val = vshPrettyCapacity(info.physical, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
-}
+cmdDomblkinfoPrint(ctl, , human);
 
 ret = true;
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 0/3] cmdDomblkinfo: introduce --all to show all block devices info

2018-06-11 Thread Chen Hanxiao
This series introduce --all to cmdDomblkinfo to show
all block devices info in one cmd.
Likes a combination of domblklist and domblkinfo.

v2:
  add support --human for --all
v1.1
  fix a self test

Chen Hanxiao (3):
  cmdDomblkinfo: introduce helper cmdDomblkinfoPrint
  cmdDomblkinfo: add --all to show all block devices info
  news: add cmdDomblkinfo --all option

 docs/news.xml|   8 +++
 tools/virsh-domain-monitor.c | 125 +--
 tools/virsh.pod  |   5 +-
 3 files changed, 118 insertions(+), 20 deletions(-)

-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 3/3] news: add cmdDomblkinfo --all option

2018-06-11 Thread Chen Hanxiao
From: Chen Hanxiao 

Update news for cmdDomblkinfo --all option.

Signed-off-by: Chen Hanxiao 
---
 docs/news.xml | 8 
 1 file changed, 8 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index e0cf381f33..c07db04809 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -193,6 +193,14 @@
   secret-event, pool-event and nodedev-event)
 
   
+  
+
+  cmdDomblkinfo: introduce --all to show all block devices info
+
+
+  Add option --all to show all block deviecs info in one command.
+
+  
 
 
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] virstring: fix a typo

2018-06-11 Thread Chen Hanxiao
From: Chen Hanxiao 

s/glibc's_asprintf/glibc's asprintf

Signed-off-by: Chen Hanxiao 
---
 src/util/virstring.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/util/virstring.h b/src/util/virstring.h
index fa2ec1df4d..607ae66e99 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -248,7 +248,7 @@ size_t virStringListLength(const char * const *strings);
  * @strp: variable to hold result (char **)
  * @fmt: printf format
  *
- * Like glibc's_asprintf but makes sure *strp == NULL on failure, in which case
+ * Like glibc's asprintf but makes sure *strp == NULL on failure, in which case
  * the OOM error is reported too.
  *
  * Returns -1 on failure (with OOM error reported), number of bytes printed
@@ -264,7 +264,7 @@ size_t virStringListLength(const char * const *strings);
  * @strp: variable to hold result (char **)
  * @fmt: printf format
  *
- * Like glibc's_asprintf but makes sure *strp == NULL on failure.
+ * Like glibc's asprintf but makes sure *strp == NULL on failure.
  *
  * Returns -1 on failure, number of bytes printed on success.
  */
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v1.1 1/2] cmdDomblkinfo: introduce --all to show all block devices info

2018-06-11 Thread Chen Hanxiao



At 2018-06-09 04:49:08, "John Ferlan"  wrote:
>
>
>On 06/07/2018 12:19 AM, Chen Hanxiao wrote:
>> From: Chen Hanxiao 
>> 
>> This patch introduces --all to show all block devices info
>> of guests like:
>> 
>> virsh # domblkinfo w08 --all
>> Target CapacityAllocation  Physical
>> ---
>> hda42949672960 9878110208  9878110208
>> vda10737418240 10736439296 10737418240
>> 
>
>You don't handle the --pretty at all.

Will do in v2.

>
>> Signed-off-by: Chen Hanxiao 
>> ---
...

>> +vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
>
>Maybe you should create/insert a patch which "first just" moves the
>printing to a separate method such as :
>
>static void
>cmdDomblkinfoPrint(vshControl *ctl,
>   const virDomainBlockInfo *info,
>   bool human)
>
>Passing
>
>cmdDomblkinfoPrint(ctl, , human);
>
>Then when adding the "all" functionality, the printing does get a bit
>trickier, but it's not impossible to figure out.  Look at the volume
>list details code for some ideas...   The real "problem" lies in the
>length of the data and trying to figure an optimal sizes to create the
>formatting string so that everything looks good.  Consider small sizes
>and larger sizes in the output.  When printing pretty - you won't have
>something like "1000.000 MiB" because that'd be "1.000 GiB".  At the
>very least you'd have:
>
> 1.000 GiB 1.000 GiB 1.000 GiB
>
>and at the very most you'd have
>
> 999.000 MiB 999.000 MiB 999.000 MiB
>
>right?  So make everything line up from that knowledge.
>
>

We can use virAsprintf to combine val and unit together.
As the longest len(999.000 MiB) = 12, so %-15s will be enough.

Thanks for your comments.
V2 will come soon.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1.1 1/2] cmdDomblkinfo: introduce --all to show all block devices info

2018-06-06 Thread Chen Hanxiao
From: Chen Hanxiao 

This patch introduces --all to show all block devices info
of guests like:

virsh # domblkinfo w08 --all
Target CapacityAllocation  Physical
---
hda42949672960 9878110208  9878110208
vda10737418240 10736439296 10737418240

Signed-off-by: Chen Hanxiao 
---
v1.1:
  fix self-test

 tools/virsh-domain-monitor.c | 84 +++-
 tools/virsh.pod  |  5 ++-
 2 files changed, 68 insertions(+), 21 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 39cb2ce7f0..fe31838e30 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -389,8 +389,7 @@ static const vshCmdInfo info_domblkinfo[] = {
 static const vshCmdOptDef opts_domblkinfo[] = {
 VIRSH_COMMON_OPT_DOMAIN_FULL(0),
 {.name = "device",
- .type = VSH_OT_DATA,
- .flags = VSH_OFLAG_REQ,
+ .type = VSH_OT_STRING,
  .completer = virshDomainDiskTargetCompleter,
  .help = N_("block device")
 },
@@ -398,6 +397,10 @@ static const vshCmdOptDef opts_domblkinfo[] = {
  .type = VSH_OT_BOOL,
  .help = N_("Human readable output")
 },
+{.name = "all",
+ .type = VSH_OT_BOOL,
+ .help = N_("display all block devices info")
+},
 {.name = NULL}
 };
 
@@ -408,38 +411,79 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 virDomainPtr dom;
 bool ret = false;
 bool human = false;
+bool all = false;
 const char *device = NULL;
+xmlDocPtr xmldoc = NULL;
+xmlXPathContextPtr ctxt = NULL;
+int ndisks;
+size_t i;
+xmlNodePtr *disks = NULL;
+char *target = NULL;
 
 if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
 return false;
 
-if (vshCommandOptStringReq(ctl, cmd, "device", ) < 0)
+all = vshCommandOptBool(cmd, "all");
+if (!all && vshCommandOptStringQuiet(ctl, cmd, "device", ) <= 0) {
+vshError(ctl, "command 'domblkinfo' requires  option");
 goto cleanup;
+}
 
-if (virDomainGetBlockInfo(dom, device, , 0) < 0)
-goto cleanup;
+if (all) {
+if (virshDomainGetXML(ctl, cmd, 0, , ) < 0)
+goto cleanup;
 
-human = vshCommandOptBool(cmd, "human");
+ndisks = virXPathNodeSet("./devices/disk", ctxt, );
+if (ndisks < 0)
+goto cleanup;
 
-if (!human) {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
-} else {
-double val;
-const char *unit;
-
-val = vshPrettyCapacity(info.capacity, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
-val = vshPrettyCapacity(info.allocation, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
-val = vshPrettyCapacity(info.physical, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+vshPrintExtra(ctl, "%-10s %-15s %-15s %-15s\n", _("Target"),
+  _("Capacity"), _("Allocation"), _("Physical"));
+vshPrintExtra(ctl, "-"
+  "--\n");
+
+for (i = 0; i < ndisks; i++) {
+ctxt->node = disks[i];
+target = virXPathString("string(./target/@dev)", ctxt);
+
+if (virDomainGetBlockInfo(dom, target, , 0) < 0)
+goto cleanup;
+
+vshPrint(ctl, "%-10s %-15llu %-15llu %-15llu\n", target,
+ info.capacity, info.allocation, info.physical);
+
+VIR_FREE(target);
+}
+} else if (device) {
+if (virDomainGetBlockInfo(dom, device, , 0) < 0)
+goto cleanup;
+
+human = vshCommandOptBool(cmd, "human");
+
+if (!human) {
+vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
+vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
+vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
+} else {
+double val;
+const char *unit;
+
+val = vshPrettyCapacity(info.capacity, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
+val = vshPrettyCapacity(info.allocation, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
+ 

[libvirt] [PATCH v1.1 0/2] cmdDomblkinfo: introduce --all to show all

2018-06-06 Thread Chen Hanxiao
This series introduce --all to cmdDomblkinfo to show
all block devices info in one cmd.
Likes a combination of domblklist and domblkinfo.

v1.1:
  fix a self test


Chen Hanxiao (2):
  cmdDomblkinfo: introduce --all to show all block devices info
  news: add cmdDomblkinfo --all option

 docs/news.xml|  8 
 tools/virsh-domain-monitor.c | 84 +++-
 tools/virsh.pod  |  5 ++-
 3 files changed, 76 insertions(+), 21 deletions(-)

-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v1.1 2/2] news: add cmdDomblkinfo --all option

2018-06-06 Thread Chen Hanxiao
From: Chen Hanxiao 

Update news for cmdDomblkinfo --all option.

Signed-off-by: Chen Hanxiao 
---
 docs/news.xml | 8 
 1 file changed, 8 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index c45850f625..daee3aa6b7 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -123,6 +123,14 @@
   secret-event, pool-event and nodedev-event)
 
   
+  
+
+  cmdDomblkinfo: introduce --all to show all block devices info
+
+
+  Add option --all to show all block deviecs info in one command.
+
+  
 
 
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 1/2] cmdDomblkinfo: introduce --all to show all block devices info

2018-06-06 Thread Chen Hanxiao
From: Chen Hanxiao 

This patch introduces --all to show all block devices info
of guests like:

virsh # domblkinfo w08 --all
Target CapacityAllocation  Physical
---
hda42949672960 9878110208  9878110208
vda10737418240 10736439296 10737418240

Signed-off-by: Chen Hanxiao 
---
 tools/virsh-domain-monitor.c | 82 +++-
 tools/virsh.pod  |  5 ++-
 2 files changed, 67 insertions(+), 20 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 39cb2ce7f0..6a2dd72b45 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -390,7 +390,6 @@ static const vshCmdOptDef opts_domblkinfo[] = {
 VIRSH_COMMON_OPT_DOMAIN_FULL(0),
 {.name = "device",
  .type = VSH_OT_DATA,
- .flags = VSH_OFLAG_REQ,
  .completer = virshDomainDiskTargetCompleter,
  .help = N_("block device")
 },
@@ -398,6 +397,10 @@ static const vshCmdOptDef opts_domblkinfo[] = {
  .type = VSH_OT_BOOL,
  .help = N_("Human readable output")
 },
+{.name = "all",
+ .type = VSH_OT_BOOL,
+ .help = N_("display all block devices info")
+},
 {.name = NULL}
 };
 
@@ -408,38 +411,79 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
 virDomainPtr dom;
 bool ret = false;
 bool human = false;
+bool all = false;
 const char *device = NULL;
+xmlDocPtr xmldoc = NULL;
+xmlXPathContextPtr ctxt = NULL;
+int ndisks;
+size_t i;
+xmlNodePtr *disks = NULL;
+char *target = NULL;
 
 if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
 return false;
 
-if (vshCommandOptStringReq(ctl, cmd, "device", ) < 0)
+all = vshCommandOptBool(cmd, "all");
+if (!all && vshCommandOptStringQuiet(ctl, cmd, "device", ) <= 0) {
+vshError(ctl, "command 'domblkinfo' requires  option");
 goto cleanup;
+}
 
-if (virDomainGetBlockInfo(dom, device, , 0) < 0)
-goto cleanup;
+if (all) {
+if (virshDomainGetXML(ctl, cmd, 0, , ) < 0)
+goto cleanup;
 
-human = vshCommandOptBool(cmd, "human");
+ndisks = virXPathNodeSet("./devices/disk", ctxt, );
+if (ndisks < 0)
+goto cleanup;
 
-if (!human) {
-vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
-vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
-vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
-} else {
-double val;
-const char *unit;
-
-val = vshPrettyCapacity(info.capacity, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
-val = vshPrettyCapacity(info.allocation, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
-val = vshPrettyCapacity(info.physical, );
-vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+vshPrintExtra(ctl, "%-10s %-15s %-15s %-15s\n", _("Target"),
+  _("Capacity"), _("Allocation"), _("Physical"));
+vshPrintExtra(ctl, "-"
+  "--\n");
+
+for (i = 0; i < ndisks; i++) {
+ctxt->node = disks[i];
+target = virXPathString("string(./target/@dev)", ctxt);
+
+if (virDomainGetBlockInfo(dom, target, , 0) < 0)
+goto cleanup;
+
+vshPrint(ctl, "%-10s %-15llu %-15llu %-15llu\n", target,
+ info.capacity, info.allocation, info.physical);
+
+VIR_FREE(target);
+}
+} else if (device) {
+if (virDomainGetBlockInfo(dom, device, , 0) < 0)
+goto cleanup;
+
+human = vshCommandOptBool(cmd, "human");
+
+if (!human) {
+vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
+vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
+vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
+} else {
+double val;
+const char *unit;
+
+val = vshPrettyCapacity(info.capacity, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
+val = vshPrettyCapacity(info.allocation, );
+vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
+val = vshPrettyCapacity(info.physical, );
+vshPrint(ctl, "%-15s %-.

[libvirt] [PATCH 2/2] news: add cmdDomblkinfo --all option

2018-06-06 Thread Chen Hanxiao
From: Chen Hanxiao 

Update news for cmdDomblkinfo --all option.

Signed-off-by: Chen Hanxiao 
---
 docs/news.xml | 8 
 1 file changed, 8 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index c45850f625..daee3aa6b7 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -123,6 +123,14 @@
   secret-event, pool-event and nodedev-event)
 
   
+  
+
+  cmdDomblkinfo: introduce --all to show all block devices info
+
+
+  Add option --all to show all block deviecs info in one command.
+
+  
 
 
 
-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 0/2] cmdDomblkinfo: introduce --all to show all

2018-06-06 Thread Chen Hanxiao
This series introduce --all to cmdDomblkinfo to show
all block devices info in one cmd.
Likes a combination of domblklist and domblkinfo.

Chen Hanxiao (2):
  cmdDomblkinfo: introduce --all to show all block devices info
  news: add cmdDomblkinfo --all option

 docs/news.xml|  8 
 tools/virsh-domain-monitor.c | 82 +++-
 tools/virsh.pod  |  5 ++-
 3 files changed, 75 insertions(+), 20 deletions(-)

-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] virsh: add missing help info of --source to domifaddr

2018-03-27 Thread Chen Hanxiao
At 2018-03-16 10:03:15, "Chen Hanxiao" <chen_han_x...@126.com> wrote:
>From: Chen Hanxiao <chenhanx...@gmail.com>
>
>commit b4b5c82ce forgot to add this.
>
>Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>---
> tools/virsh-domain-monitor.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
>index 68da11ed5..0f768d394 100644
>--- a/tools/virsh-domain-monitor.c
>+++ b/tools/virsh-domain-monitor.c
>@@ -2160,7 +2160,7 @@ static const vshCmdOptDef opts_domifaddr[] = {
> {.name = "source",
>  .type = VSH_OT_STRING,
>  .flags = VSH_OFLAG_NONE,
>- .help = N_("address source: 'lease' or 'agent'")},
>+ .help = N_("address source: 'lease', 'agent' or 'arp'")},
> {.name = NULL}
> };
> 

ping

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 1/2] virarptable: fix some leaks and format issue

2018-03-17 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

fix some leaks and format issue
Also support virArpTableFree to get NULL.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 src/util/virarptable.c | 13 +++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/util/virarptable.c b/src/util/virarptable.c
index 92839fdca..c0e90dc4b 100644
--- a/src/util/virarptable.c
+++ b/src/util/virarptable.c
@@ -47,6 +47,7 @@ VIR_LOG_INIT("util.arptable");
 # define NDA_RTA(r) \
 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg
 
+
 static int
 parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
 {
@@ -64,7 +65,9 @@ parse_rtattr(struct rtattr *tb[], int max, struct rtattr 
*rta, int len)
 return 0;
 }
 
-virArpTablePtr virArpTableGet(void)
+
+virArpTablePtr
+virArpTableGet(void)
 {
 int num = 0;
 int msglen;
@@ -156,6 +159,7 @@ virArpTablePtr virArpTableGet(void)
 return table;
 
  cleanup:
+virArpTableFree(table);
 VIR_FREE(ipstr);
 VIR_FREE(nlData);
 return NULL;
@@ -163,7 +167,8 @@ virArpTablePtr virArpTableGet(void)
 
 #else
 
-virArpTablePtr virArpTableGet(void)
+virArpTablePtr
+virArpTableGet(void)
 {
 virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("get arp table not implemented on this platform"));
@@ -176,6 +181,10 @@ void
 virArpTableFree(virArpTablePtr table)
 {
 size_t i;
+
+if (!table)
+return;
+
 for (i = 0; i < table->n; i++) {
 VIR_FREE(table->t[i].ipaddr);
 VIR_FREE(table->t[i].mac);
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 2/2] qemu: fix a mem leak

2018-03-17 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

fix a mem leak

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 src/qemu/qemu_driver.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 5c55c69af..14b72a8f6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -20682,6 +20682,8 @@ qemuARPGetInterfaces(virDomainObjPtr vm,
 
 if (VIR_APPEND_ELEMENT(ifaces_ret, ifaces_count, iface) < 0)
 goto cleanup;
+
+virDomainInterfaceFree(iface);
 }
 }
 }
@@ -20691,6 +20693,7 @@ qemuARPGetInterfaces(virDomainObjPtr vm,
 
  cleanup:
 virArpTableFree(table);
+virDomainInterfaceFree(iface);
 
 if (ifaces_ret) {
 for (i = 0; i < ifaces_count; i++)
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH rebase v4 2/5] util: introduce helper to parse message from RTM_GETNEIGH query

2018-03-17 Thread Chen Hanxiao


At 2018-03-16 18:53:58, "John Ferlan" <jfer...@redhat.com> wrote:
>
>
>On 03/08/2018 02:11 AM, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> introduce helper to parse RTM_GETNEIGH query message and
>> store it in struct virArpTable.
>> 
>> Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>> ---
>> v4-rebase:
>>   fit split Makefile.am
>>   fit new virMacAddr fields
>> 
>> v4:
>>   use netlink query instead of parsing /proc/net/arp
>> 
>> v3:
>>   s/virGetArpTable/virArpTableGet
>>   alloc virArpTable in virArpTableGet
>>   return ENOSUPP on none-Linux platform
>>   move helpers to virarptable.[ch]
>> 
>>  po/POTFILES.in   |   1 +
>>  src/Makefile.am  |   1 +
>>  src/libvirt_private.syms |   5 ++
>>  src/util/Makefile.inc.am |   2 +
>>  src/util/virarptable.c   | 181 
>> +++
>>  src/util/virarptable.h   |  48 +
>>  6 files changed, 238 insertions(+)
>>  create mode 100644 src/util/virarptable.c
>>  create mode 100644 src/util/virarptable.h
>> 
>
>Couple of Coverity issues
>

Thanks, will be fixed soon.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 0/2] virarptable: fix some leaks and format

2018-03-17 Thread Chen Hanxiao
Address John's comment on v4

Chen Hanxiao (2):
  virarptable: fix some leaks and format issue
  qemu: fix some leaks

 src/qemu/qemu_driver.c |  3 +++
 src/util/virarptable.c | 13 +++--
 2 files changed, 14 insertions(+), 2 deletions(-)

-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] virsh: add missing help info of --source to domifaddr

2018-03-15 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

commit b4b5c82ce forgot to add this.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 tools/virsh-domain-monitor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 68da11ed5..0f768d394 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2160,7 +2160,7 @@ static const vshCmdOptDef opts_domifaddr[] = {
 {.name = "source",
  .type = VSH_OT_STRING,
  .flags = VSH_OFLAG_NONE,
- .help = N_("address source: 'lease' or 'agent'")},
+ .help = N_("address source: 'lease', 'agent' or 'arp'")},
 {.name = NULL}
 };
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 2/2] virarptable: Include rtnetlink.h only on Linux

2018-03-15 Thread Chen Hanxiao


At 2018-03-15 20:49:30, "Michal Privoznik" <mpriv...@redhat.com> wrote:
>And at the same time, do that from .c rather than .h file.
>
>Signed-off-by: Michal Privoznik <mpriv...@redhat.com>
>---
> src/util/virarptable.c | 3 +++
> src/util/virarptable.h | 1 -
> 2 files changed, 3 insertions(+), 1 deletion(-)
>
>diff --git a/src/util/virarptable.c b/src/util/virarptable.c
>index 2730ef91af..8d9ab5fdc8 100644
>--- a/src/util/virarptable.c
>+++ b/src/util/virarptable.c
>@@ -26,6 +26,9 @@
> #include 
> #include 
> #include 
>+#ifdef __linux__
>+# include 
>+#endif
> 
> #include "viralloc.h"
> #include "virarptable.h"
>diff --git a/src/util/virarptable.h b/src/util/virarptable.h
>index 404d8eb867..6829198128 100644
>--- a/src/util/virarptable.h
>+++ b/src/util/virarptable.h
>@@ -25,7 +25,6 @@
> # define __VIR_ARPTABLE_H__
> 
> # include "internal.h"
>-# include 
> 
> typedef struct _virArpTableEntry virArpTableEntry;
> typedef virArpTableEntry *virArpTableEntryPtr;
>-- 

Reviewed-by: Chen Hanxiao<chenhanx...@gmail.com>

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH rebase v4 0/5] qemu: use arp table of host to get the

2018-03-15 Thread Chen Hanxiao


At 2018-03-15 18:23:03, "Michal Privoznik" <mpriv...@redhat.com> wrote:
>On 03/08/2018 08:11 AM, Chen Hanxiao wrote:
>> introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
>> of VM from the output of /proc/net/arp
>> 
>> Chen Hanxiao (5):
>>   util: introduce virNetlinkGetNeighbor to get neighbor table entry
>>   util: introduce helper to parse message from RTM_GETNEIGH query
>>   qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
>>   virsh: add --source arp to domifaddr
>>   news: qemu: use arp table of host to get the IP address of guests
>> 
>>  docs/news.xml|   9 ++
>>  include/libvirt/libvirt-domain.h |   1 +
>>  po/POTFILES.in   |   1 +
>>  src/Makefile.am  |   1 +
>>  src/libvirt-domain.c |   7 ++
>>  src/libvirt_private.syms |   6 ++
>>  src/qemu/qemu_driver.c   |  72 
>>  src/util/Makefile.inc.am |   2 +
>>  src/util/virarptable.c   | 181 
>> +++
>>  src/util/virarptable.h   |  48 +++
>>  src/util/virnetlink.c|  82 ++
>>  src/util/virnetlink.h|   2 +
>>  tools/virsh-domain-monitor.c |   2 +
>>  tools/virsh.pod  |   7 +-
>>  14 files changed, 418 insertions(+), 3 deletions(-)
>>  create mode 100644 src/util/virarptable.c
>>  create mode 100644 src/util/virarptable.h
>> 
>
>
>I've fixed both small issues I raised, ACKed and pushed.
>

Thanks.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH rebase v4 0/5] qemu: use arp table of host to get the

2018-03-15 Thread Chen Hanxiao



At 2018-03-08 15:11:54, "Chen Hanxiao" <chen_han_x...@126.com> wrote:
>introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
>of VM from the output of /proc/net/arp
>
>Chen Hanxiao (5):
>  util: introduce virNetlinkGetNeighbor to get neighbor table entry
>  util: introduce helper to parse message from RTM_GETNEIGH query
>  qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
>  virsh: add --source arp to domifaddr
>  news: qemu: use arp table of host to get the IP address of guests
>
> docs/news.xml|   9 ++
> include/libvirt/libvirt-domain.h |   1 +
> po/POTFILES.in   |   1 +
> src/Makefile.am  |   1 +
> src/libvirt-domain.c |   7 ++
> src/libvirt_private.syms |   6 ++
> src/qemu/qemu_driver.c   |  72 
> src/util/Makefile.inc.am |   2 +
> src/util/virarptable.c   | 181 +++
> src/util/virarptable.h   |  48 +++
> src/util/virnetlink.c|  82 ++
> src/util/virnetlink.h|   2 +
> tools/virsh-domain-monitor.c |   2 +
> tools/virsh.pod  |   7 +-
> 14 files changed, 418 insertions(+), 3 deletions(-)
> create mode 100644 src/util/virarptable.c
> create mode 100644 src/util/virarptable.h
>
>-- 

ping

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH rebase v4 1/5] util: introduce virNetlinkGetNeighbor to get neighbor table entry

2018-03-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

use RTM_GETNEIGH to query arp table entry by netlink socket

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4:
  use netlink to get arp table entry

 src/libvirt_private.syms |  1 +
 src/util/virnetlink.c| 82 
 src/util/virnetlink.h|  2 ++
 3 files changed, 85 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3766e20d3..11b9f4937 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2386,6 +2386,7 @@ virNetlinkEventServiceStart;
 virNetlinkEventServiceStop;
 virNetlinkEventServiceStopAll;
 virNetlinkGetErrorCode;
+virNetlinkGetNeighbor;
 virNetlinkShutdown;
 virNetlinkStartup;
 
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index e61bbb96d..f0a92db23 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -590,6 +590,88 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 goto cleanup;
 }
 
+/**
+ * virNetlinkGetNeighbor:
+ *
+ * @nlData:  Gets a pointer to the raw data from netlink.
+ MUST BE FREED BY CALLER!
+ * @src_pid: pid used for nl_pid of the local end of the netlink message
+ *   (0 == "use getpid()")
+ * @dst_pid: pid of destination nl_pid if the kernel
+ *   is not the target of the netlink message but it is to be
+ *   sent to another process (0 if sending to the kernel)
+ *
+ * Get neighbor table entry from netlink.
+ *
+ * Returns 0 on success, -1 on fatal error.
+ */
+int
+virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
+{
+int rc = -1;
+struct nlmsghdr *resp = NULL;
+struct nlmsgerr *err;
+struct ndmsg ndinfo = {
+.ndm_family = AF_UNSPEC,
+};
+unsigned int recvbuflen;
+struct nl_msg *nl_msg;
+
+nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
+if (!nl_msg) {
+virReportOOMError();
+return -1;
+}
+
+if (nlmsg_append(nl_msg, , sizeof(ndinfo), NLMSG_ALIGNTO) < 0)
+goto buffer_too_small;
+
+
+if (virNetlinkCommand(nl_msg, , ,
+  src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
+goto cleanup;
+
+if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
+goto malformed_resp;
+
+switch (resp->nlmsg_type) {
+case NLMSG_ERROR:
+err = (struct nlmsgerr *)NLMSG_DATA(resp);
+if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
+goto malformed_resp;
+
+if (err->error) {
+virReportSystemError(-err->error,
+ "%s", _("error dumping"));
+goto cleanup;
+}
+break;
+
+case RTM_NEWNEIGH:
+break;
+
+default:
+goto malformed_resp;
+}
+rc = recvbuflen;
+
+ cleanup:
+nlmsg_free(nl_msg);
+if (rc < 0)
+   VIR_FREE(resp);
+*nlData = resp;
+return rc;
+
+ malformed_resp:
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("malformed netlink response message"));
+goto cleanup;
+
+ buffer_too_small:
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("allocated netlink buffer is too small"));
+goto cleanup;
+}
 
 int
 virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen)
diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h
index 088b01343..2a9de0a57 100644
--- a/src/util/virnetlink.h
+++ b/src/util/virnetlink.h
@@ -71,6 +71,8 @@ int virNetlinkDumpLink(const char *ifname, int ifindex,
void **nlData, struct nlattr **tb,
uint32_t src_pid, uint32_t dst_pid)
 ATTRIBUTE_RETURN_CHECK;
+int
+virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid);
 
 typedef void (*virNetlinkEventHandleCallback)(struct nlmsghdr *,
   unsigned int length,
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH rebase v4 5/5] news: qemu: use arp table of host to get the IP address of guests

2018-03-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4:
  rebase on 4.2

v3:
  more verbose description

 docs/news.xml | 9 +
 1 file changed, 9 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index a51ca973e..6d729d508 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -46,6 +46,15 @@
   information, log the relevant data to the domain log file.
 
   
+  
+
+  qemu: use arp table of host to get the IP address of guests
+
+
+  Find IP address of a VM by arp table on hosts.
+  If someone customizing IP address inside VM, it will be helpful.
+
+  
 
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH rebase v4 0/5] qemu: use arp table of host to get the

2018-03-07 Thread Chen Hanxiao
introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp

Chen Hanxiao (5):
  util: introduce virNetlinkGetNeighbor to get neighbor table entry
  util: introduce helper to parse message from RTM_GETNEIGH query
  qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
  virsh: add --source arp to domifaddr
  news: qemu: use arp table of host to get the IP address of guests

 docs/news.xml|   9 ++
 include/libvirt/libvirt-domain.h |   1 +
 po/POTFILES.in   |   1 +
 src/Makefile.am  |   1 +
 src/libvirt-domain.c |   7 ++
 src/libvirt_private.syms |   6 ++
 src/qemu/qemu_driver.c   |  72 
 src/util/Makefile.inc.am |   2 +
 src/util/virarptable.c   | 181 +++
 src/util/virarptable.h   |  48 +++
 src/util/virnetlink.c|  82 ++
 src/util/virnetlink.h|   2 +
 tools/virsh-domain-monitor.c |   2 +
 tools/virsh.pod  |   7 +-
 14 files changed, 418 insertions(+), 3 deletions(-)
 create mode 100644 src/util/virarptable.c
 create mode 100644 src/util/virarptable.h

-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH rebase v4 3/5] qemu: introduce qemuARPGetInterfaces to get IP from host's arp table

2018-03-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the message of netlink RTM_GETNEIGH

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4:
  remove dummy entry
  use VIR_APPEND_ELEMENT

v3:
  add docs in virDomainInterfaceAddresses
  remove error label
  show network interface which did not match the arp table

 include/libvirt/libvirt-domain.h |  1 +
 src/libvirt-domain.c |  7 
 src/qemu/qemu_driver.c   | 72 
 3 files changed, 80 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4048acf38..38e2d9a3e 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr;
 typedef enum {
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
+VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */
 
 # ifdef VIR_ENUM_SENTINELS
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index eaec0979a..1ae83610d 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -11721,6 +11721,13 @@ virDomainFSInfoFree(virDomainFSInfoPtr info)
  * To match such interface with the one from @dom XML use MAC address or IP
  * range.
  *
+ * If @source is VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, the /proc/net/arp
+ * will be check to obtain the interface addresses.
+ * As the arp cache did not refresh in time, the returned ip address
+ * may be unreachable.
+ * As the route config of the guest, the returned mac address
+ * may be duplicated.
+ *
  * @ifaces->name and @ifaces->hwaddr are never NULL.
  *
  * The caller *must* free @ifaces when no longer needed. Usual use case
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 9e715e7a0..7d77e1643 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -70,6 +70,7 @@
 #include "virnetdevopenvswitch.h"
 #include "capabilities.h"
 #include "viralloc.h"
+#include "virarptable.h"
 #include "viruuid.h"
 #include "domain_conf.h"
 #include "domain_audit.h"
@@ -157,6 +158,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
  virDomainObjPtr vm,
  virDomainInterfacePtr **ifaces);
 
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+virDomainInterfacePtr **ifaces);
+
 static virQEMUDriverPtr qemu_driver;
 
 
@@ -20516,6 +20520,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom,
 
 break;
 
+case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP:
+ret = qemuARPGetInterfaces(vm, ifaces);
+break;
+
 default:
 virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("Unknown IP address data source %d"),
@@ -20625,6 +20633,70 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 }
 
 
+static int
+qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces)
+{
+size_t i, j;
+size_t ifaces_count = 0;
+int ret = -1;
+char macaddr[VIR_MAC_STRING_BUFLEN];
+virDomainInterfacePtr *ifaces_ret = NULL;
+virDomainInterfacePtr iface = NULL;
+virArpTablePtr table;
+
+table = virArpTableGet();
+if (!table)
+goto cleanup;
+
+for (i = 0; i < vm->def->nnets; i++) {
+if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+continue;
+
+virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+virArpTableEntry entry;
+for (j = 0; j < table->n; j++) {
+entry = table->t[j];
+if (STREQ(entry.mac, macaddr)) {
+if (VIR_ALLOC(iface) < 0)
+goto cleanup;
+
+iface->naddrs = 1;
+if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+goto cleanup;
+
+if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->addrs->addr, entry.ipaddr) < 0)
+goto cleanup;
+
+if (VIR_APPEND_ELEMENT(ifaces_ret, ifaces_count, iface) < 0)
+goto cleanup;
+
+}
+}
+}
+
+VIR_STEAL_PTR(*ifaces, ifaces_ret);
+ret = ifaces_count;
+
+ cleanup:
+virArpTableFree(table);
+
+if (ifaces_ret) {
+for (i = 0; i < ifaces_count; i++)
+virDomainInterfaceFree(ifaces_ret[i]);
+}
+VIR_FREE(ifaces_ret);
+
+return ret;
+}
+
+
 stati

[libvirt] [PATCH rebase v4 4/5] virsh: add --source arp to domifaddr

2018-03-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

We can use:
  domifaddr f26-cloud --source arp
to get the address.

Acked-by: Michal Privoznik <mpriv...@redhat.com>
Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 tools/virsh-domain-monitor.c | 2 ++
 tools/virsh.pod  | 7 ---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 32a42707e..68da11ed5 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2190,6 +2190,8 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE;
 } else if (STREQ(sourcestr, "agent")) {
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT;
+} else if (STREQ(sourcestr, "arp")) {
+source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP;
 } else {
 vshError(ctl, _("Unknown data source '%s'"), sourcestr);
 goto cleanup;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 8f0e8d74b..515f18fdc 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -759,7 +759,7 @@ B (fields appear in the following 
order):
 
 
 =item B I [I] [I<--full>]
-  [I<--source lease|agent>]
+  [I<--source lease|agent|arp>]
 
 Get a list of interfaces of a running domain along with their IP and MAC
 addresses, or limited output just for one interface if I is
@@ -774,8 +774,9 @@ only the interface name and MAC address is displayed for 
the first name and
 MAC address with "-" for the others using the same name and MAC address.
 
 The I<--source> argument specifies what data source to use for the
-addresses, currently one of 'lease' to read DHCP leases, or 'agent' to query
-the guest OS via an agent. If unspecified, 'lease' is the default.
+addresses, currently 'lease' to read DHCP leases, 'agent' to query
+the guest OS via an agent, or 'arp' to get IP from host's arp tables.
+If unspecified, 'lease' is the default.
 
 =item B I I
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH rebase v4 2/5] util: introduce helper to parse message from RTM_GETNEIGH query

2018-03-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce helper to parse RTM_GETNEIGH query message and
store it in struct virArpTable.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4-rebase:
  fit split Makefile.am
  fit new virMacAddr fields

v4:
  use netlink query instead of parsing /proc/net/arp

v3:
  s/virGetArpTable/virArpTableGet
  alloc virArpTable in virArpTableGet
  return ENOSUPP on none-Linux platform
  move helpers to virarptable.[ch]

 po/POTFILES.in   |   1 +
 src/Makefile.am  |   1 +
 src/libvirt_private.syms |   5 ++
 src/util/Makefile.inc.am |   2 +
 src/util/virarptable.c   | 181 +++
 src/util/virarptable.h   |  48 +
 6 files changed, 238 insertions(+)
 create mode 100644 src/util/virarptable.c
 create mode 100644 src/util/virarptable.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index cfdd4ebdd..71c61dec9 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -192,6 +192,7 @@ src/uml/uml_conf.c
 src/uml/uml_driver.c
 src/util/iohelper.c
 src/util/viralloc.c
+src/util/virarptable.c
 src/util/viraudit.c
 src/util/virauth.c
 src/util/virauthconfig.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 8b1e4c8a4..82c5d5cde 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -672,6 +672,7 @@ noinst_LTLIBRARIES += libvirt-setuid-rpc-client.la
 libvirt_setuid_rpc_client_la_SOURCES = \
util/viralloc.c \
util/virarch.c \
+   util/virarptable.c \
util/viratomic.c \
util/viratomic.h \
util/virbitmap.c \
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 11b9f4937..05b0c5b0e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1365,6 +1365,11 @@ virArchGetWordSize;
 virArchToString;
 
 
+# util/virarptable.h
+virArpTableFree;
+virArpTableGet;
+
+
 # util/viraudit.h
 virAuditClose;
 virAuditEncode;
diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am
index a91b30dca..02d9c42cc 100644
--- a/src/util/Makefile.inc.am
+++ b/src/util/Makefile.inc.am
@@ -5,6 +5,8 @@ UTIL_SOURCES = \
util/viralloc.h \
util/virarch.c \
util/virarch.h \
+   util/virarptable.c \
+   util/virarptable.h \
util/viratomic.c \
util/viratomic.h \
util/viraudit.c \
diff --git a/src/util/virarptable.c b/src/util/virarptable.c
new file mode 100644
index 0..cb56338eb
--- /dev/null
+++ b/src/util/virarptable.c
@@ -0,0 +1,181 @@
+/*
+ * virarptable.c Linux ARP table handling
+ *
+ * Copyright (C) 2018 Chen Hanxiao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Chen Hanxiao <chenhanx...@gmail.com>
+ */
+
+#include 
+
+#include 
+#include 
+#include 
+
+#include "viralloc.h"
+#include "virarptable.h"
+#include "virfile.h"
+#include "virlog.h"
+#include "virnetlink.h"
+#include "virsocketaddr.h"
+#include "virstring.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.arptable");
+
+#ifdef __linux__
+
+# define NDA_RTA(r) \
+((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg
+
+static int
+parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
+{
+memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
+while (RTA_OK(rta, len)) {
+if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
+tb[rta->rta_type] = rta;
+rta = RTA_NEXT(rta, len);
+}
+
+if (len)
+VIR_WARN("malformed netlink message: Deficit %d, rta_len=%d",
+ len, rta->rta_len);
+return 0;
+}
+
+virArpTablePtr virArpTableGet(void)
+{
+int num = 0;
+int msglen;
+void *nlData = NULL;
+virArpTablePtr table = NULL;
+char *ipstr = NULL;
+struct nlmsghdr* nh;
+struct rtattr * tb[NDA_MAX+1];
+
+msglen = virNetlinkGetNeighbor(, 0, 0);
+if (msglen < 0)
+return NULL;
+
+if (VIR_ALLOC(table) < 0)
+return NULL;
+
+nh = (struct nlmsghdr*)nlData;
+
+while (NLMSG_OK(nh, msglen)) {
+struct ndmsg *r = NLMSG_DATA(nh);
+int len = nh->nlmsg_len;
+void *addr;
+
+  if ((len -= NLMSG_LENGTH(sizeof(*nh))) < 0) {
+   

Re: [libvirt] [PATCH v4 0/5] qemu: use arp table of host to get the

2018-02-21 Thread Chen Hanxiao

At 2018-02-08 15:46:59, "Chen Hanxiao" <chen_han_x...@126.com> wrote:
>introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
>of VM from the output of /proc/net/arp
>
>Chen Hanxiao (5):
>  util: introduce virNetlinkGetNeighbor to get neighbor table entry
>  util: introduce helper to parse message from RTM_GETNEIGH query
>  qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
>  virsh: add --source arp to domifaddr
>  news: qemu: use arp table of host to get the IP address of guests
>
> docs/news.xml|   9 ++
> include/libvirt/libvirt-domain.h |   1 +
> po/POTFILES.in   |   1 +
> src/Makefile.am  |   1 +
> src/libvirt-domain.c |   7 ++
> src/libvirt_private.syms |   6 ++
> src/qemu/qemu_driver.c   |  72 
> src/util/virarptable.c   | 182 +++
> src/util/virarptable.h   |  47 ++
> src/util/virnetlink.c|  82 ++
> src/util/virnetlink.h|   2 +
> tools/virsh-domain-monitor.c |   2 +
> tools/virsh.pod  |   7 +-
> 13 files changed, 416 insertions(+), 3 deletions(-)
> create mode 100644 src/util/virarptable.c
> create mode 100644 src/util/virarptable.h
>

ping

Regards,

- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] qemu: don't leak in qemuGetDHCPInterfaces when failing to alloc

2018-02-10 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

We forgot to free alloced mem when failed to
dup ifname or macaddr.

Also use VIR_STEAL_PTR to simplify codes.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 src/qemu/qemu_agent.c  | 3 +--
 src/qemu/qemu_driver.c | 7 +++
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 5d125c413..0f36054a6 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -2190,8 +2190,7 @@ qemuAgentGetInterfaces(qemuAgentPtr mon,
 iface->naddrs = addrs_count;
 }
 
-*ifaces = ifaces_ret;
-ifaces_ret = NULL;
+VIR_STEAL_PTR(*ifaces, ifaces_ret);
 ret = ifaces_count;
 
  cleanup:
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 978ecd4e0..60cdd237a 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -20569,10 +20569,10 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 goto error;
 
 if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
-goto cleanup;
+goto error;
 
 if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
-goto cleanup;
+goto error;
 }
 
 for (j = 0; j < n_leases; j++) {
@@ -20592,8 +20592,7 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 VIR_FREE(leases);
 }
 
-*ifaces = ifaces_ret;
-ifaces_ret = NULL;
+VIR_STEAL_PTR(*ifaces, ifaces_ret);
 rv = ifaces_count;
 
  cleanup:
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] qemu: Alter condition to avoid possible NULL deref

2018-02-09 Thread Chen Hanxiao

At 2018-02-09 23:33:38, "John Ferlan" <jfer...@redhat.com> wrote:
>Commit 'f0f2a5ec2' neglected to adjust the if condition to split
>out the possibility that the @watchdog is NULL when altering the
>message to add detail about the model.
>
>Just split out the condition and use previous/original message, but
>with the new message code.
>
>Found by Coverity
>
>Signed-off-by: John Ferlan <jfer...@redhat.com>
>---
> src/qemu/qemu_hotplug.c | 9 +++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
>diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
>index c7bf25eee..3291ce613 100644
>--- a/src/qemu/qemu_hotplug.c
>+++ b/src/qemu/qemu_hotplug.c
>@@ -5159,11 +5159,16 @@ qemuDomainDetachWatchdog(virQEMUDriverPtr driver,
> virDomainWatchdogDefPtr watchdog = vm->def->watchdog;
> qemuDomainObjPrivatePtr priv = vm->privateData;
> 
>+if (!watchdog) {
>+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
>+   _("watchdog device not present in domain 
>configuration"));
>+return -1;
>+}
>+
> /* While domains can have up to one watchdog, the one supplied by the user
>  * doesn't necessarily match the one domain has. Refuse to detach in such
>  * case. */
>-if (!(watchdog &&
>-  watchdog->model == dev->model &&
>+if (!(watchdog->model == dev->model &&
>   watchdog->action == dev->action &&
>   virDomainDeviceInfoAddressIsEqual(>info, >info))) {
> virReportError(VIR_ERR_DEVICE_MISSING,
>-- 

Reviewed-by: Chen Hanxiao <chenhanx...@gmail.com>

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v4 5/5] news: qemu: use arp table of host to get the IP address of guests

2018-02-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  more verbose description

 docs/news.xml | 9 +
 1 file changed, 9 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index 5a2943a58..5d6a467d3 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -72,6 +72,15 @@
   completion data.
 
   
+  
+
+  qemu: use arp table of host to get the IP address of guests
+
+
+  Find IP address of a VM by arp table on hosts.
+  If someone customizing IP address inside VM, it will be helpful.
+
+  
 
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v4 2/5] util: introduce helper to parse message from RTM_GETNEIGH query

2018-02-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce helper to parse RTM_GETNEIGH query message and
store it in struct virArpTable.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4:
  use netlink query instead of parsing /proc/net/arp

v3:
  s/virGetArpTable/virArpTableGet
  alloc virArpTable in virArpTableGet
  return ENOSUPP on none-Linux platform
  move helpers to virarptable.[ch]

 po/POTFILES.in   |   1 +
 src/Makefile.am  |   1 +
 src/libvirt_private.syms |   5 ++
 src/util/virarptable.c   | 182 +++
 src/util/virarptable.h   |  47 
 5 files changed, 236 insertions(+)
 create mode 100644 src/util/virarptable.c
 create mode 100644 src/util/virarptable.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index cbf2accba..0f2ba7490 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -193,6 +193,7 @@ src/uml/uml_conf.c
 src/uml/uml_driver.c
 src/util/iohelper.c
 src/util/viralloc.c
+src/util/virarptable.c
 src/util/viraudit.c
 src/util/virauth.c
 src/util/virauthconfig.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 79adc9ba5..dd79b58e0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -98,6 +98,7 @@ augeastest_DATA =
 UTIL_SOURCES = \
util/viralloc.c util/viralloc.h \
util/virarch.h util/virarch.c \
+   util/virarptable.h util/virarptable.c \
util/viratomic.h util/viratomic.c \
util/viraudit.c util/viraudit.h \
util/virauth.c util/virauth.h \
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d90eb3dde..1167220f2 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1346,6 +1346,11 @@ virArchGetWordSize;
 virArchToString;
 
 
+# util/virarptable.h
+virArpTableFree;
+virArpTableGet;
+
+
 # util/viraudit.h
 virAuditClose;
 virAuditEncode;
diff --git a/src/util/virarptable.c b/src/util/virarptable.c
new file mode 100644
index 0..cbf085403
--- /dev/null
+++ b/src/util/virarptable.c
@@ -0,0 +1,182 @@
+/*
+ * virarptable.c Linux ARP table handling
+ *
+ * Copyright (C) 2018 Chen Hanxiao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Chen Hanxiao <chenhanx...@gmail.com>
+ */
+
+#include 
+
+#include 
+#include 
+#include 
+
+#include "viralloc.h"
+#include "virarptable.h"
+#include "virfile.h"
+#include "virlog.h"
+#include "virnetlink.h"
+#include "virsocketaddr.h"
+#include "virstring.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.arptable");
+
+#ifdef __linux__
+
+# define NDA_RTA(r) \
+((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg
+
+static int
+parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
+{
+memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
+while (RTA_OK(rta, len)) {
+if ((rta->rta_type <= max) && (!tb[rta->rta_type]))
+tb[rta->rta_type] = rta;
+rta = RTA_NEXT(rta, len);
+}
+
+if (len)
+VIR_WARN("malformed netlink message: Deficit %d, rta_len=%d",
+ len, rta->rta_len);
+return 0;
+}
+
+virArpTablePtr virArpTableGet(void)
+{
+int num = 0;
+int msglen;
+void *nlData = NULL;
+virArpTablePtr table = NULL;
+char *ipstr = NULL;
+struct nlmsghdr* nh;
+struct rtattr * tb[NDA_MAX+1];
+
+msglen = virNetlinkGetNeighbor(, 0, 0);
+if (msglen < 0)
+return NULL;
+
+if (VIR_ALLOC(table) < 0)
+return NULL;
+
+nh = (struct nlmsghdr*)nlData;
+
+while (NLMSG_OK(nh, msglen)) {
+struct ndmsg *r = NLMSG_DATA(nh);
+int len = nh->nlmsg_len;
+void *addr;
+
+  if ((len -= NLMSG_LENGTH(sizeof(*nh))) < 0) {
+  virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("wrong nlmsg len"));
+  goto cleanup;
+  }
+
+  if (r->ndm_family && (r->ndm_family != AF_INET))
+  goto next_nlmsg;
+
+  /* catch stale and reachalbe arp entry only */
+  if (r->ndm_state &&
+  (!(r->ndm_state == NUD_STALE || r->ndm_state == NUD_REACHABLE))) {
+  nh = NLMSG_NEXT(nh, msg

[libvirt] [PATCH v4 1/5] util: introduce virNetlinkGetNeighbor to get neighbor table entry

2018-02-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

use RTM_GETNEIGH to query arp table entry by netlink socket

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4:
  use netlink to get arp table entry

 src/libvirt_private.syms |  1 +
 src/util/virnetlink.c| 82 
 src/util/virnetlink.h|  2 ++
 3 files changed, 85 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 0bce0bbfb..d90eb3dde 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2367,6 +2367,7 @@ virNetlinkEventServiceStart;
 virNetlinkEventServiceStop;
 virNetlinkEventServiceStopAll;
 virNetlinkGetErrorCode;
+virNetlinkGetNeighbor;
 virNetlinkShutdown;
 virNetlinkStartup;
 
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index d732fe8cf..0f9b0f5d1 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -590,6 +590,88 @@ virNetlinkDelLink(const char *ifname, 
virNetlinkDelLinkFallback fallback)
 goto cleanup;
 }
 
+/**
+ * virNetlinkGetNeighbor:
+ *
+ * @nlData:  Gets a pointer to the raw data from netlink.
+ MUST BE FREED BY CALLER!
+ * @src_pid: pid used for nl_pid of the local end of the netlink message
+ *   (0 == "use getpid()")
+ * @dst_pid: pid of destination nl_pid if the kernel
+ *   is not the target of the netlink message but it is to be
+ *   sent to another process (0 if sending to the kernel)
+ *
+ * Get neighbor table entry from netlink.
+ *
+ * Returns 0 on success, -1 on fatal error.
+ */
+int
+virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
+{
+int rc = -1;
+struct nlmsghdr *resp = NULL;
+struct nlmsgerr *err;
+struct ndmsg ndinfo = {
+.ndm_family = AF_UNSPEC,
+};
+unsigned int recvbuflen;
+struct nl_msg *nl_msg;
+
+nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
+if (!nl_msg) {
+virReportOOMError();
+return -1;
+}
+
+if (nlmsg_append(nl_msg, , sizeof(ndinfo), NLMSG_ALIGNTO) < 0)
+goto buffer_too_small;
+
+
+if (virNetlinkCommand(nl_msg, , ,
+  src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
+goto cleanup;
+
+if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
+goto malformed_resp;
+
+switch (resp->nlmsg_type) {
+case NLMSG_ERROR:
+err = (struct nlmsgerr *)NLMSG_DATA(resp);
+if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
+goto malformed_resp;
+
+if (err->error) {
+virReportSystemError(-err->error,
+ "%s", _("error dumping"));
+goto cleanup;
+}
+break;
+
+case RTM_NEWNEIGH:
+break;
+
+default:
+goto malformed_resp;
+}
+rc = recvbuflen;
+
+ cleanup:
+nlmsg_free(nl_msg);
+if (rc < 0)
+   VIR_FREE(resp);
+*nlData = resp;
+return rc;
+
+ malformed_resp:
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("malformed netlink response message"));
+goto cleanup;
+
+ buffer_too_small:
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("allocated netlink buffer is too small"));
+goto cleanup;
+}
 
 int
 virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen)
diff --git a/src/util/virnetlink.h b/src/util/virnetlink.h
index 088b01343..2a9de0a57 100644
--- a/src/util/virnetlink.h
+++ b/src/util/virnetlink.h
@@ -71,6 +71,8 @@ int virNetlinkDumpLink(const char *ifname, int ifindex,
void **nlData, struct nlattr **tb,
uint32_t src_pid, uint32_t dst_pid)
 ATTRIBUTE_RETURN_CHECK;
+int
+virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid);
 
 typedef void (*virNetlinkEventHandleCallback)(struct nlmsghdr *,
   unsigned int length,
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v4 3/5] qemu: introduce qemuARPGetInterfaces to get IP from host's arp table

2018-02-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the message of netlink RTM_GETNEIGH

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4:
  remove dummy entry
  use VIR_APPEND_ELEMENT
v3:
  add docs in virDomainInterfaceAddresses
  remove error label
  show network interface which did not match the arp table

 include/libvirt/libvirt-domain.h |  1 +
 src/libvirt-domain.c |  7 
 src/qemu/qemu_driver.c   | 72 
 3 files changed, 80 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4048acf38..38e2d9a3e 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr;
 typedef enum {
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
+VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */
 
 # ifdef VIR_ENUM_SENTINELS
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index eaec0979a..1ae83610d 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -11721,6 +11721,13 @@ virDomainFSInfoFree(virDomainFSInfoPtr info)
  * To match such interface with the one from @dom XML use MAC address or IP
  * range.
  *
+ * If @source is VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, the /proc/net/arp
+ * will be check to obtain the interface addresses.
+ * As the arp cache did not refresh in time, the returned ip address
+ * may be unreachable.
+ * As the route config of the guest, the returned mac address
+ * may be duplicated.
+ *
  * @ifaces->name and @ifaces->hwaddr are never NULL.
  *
  * The caller *must* free @ifaces when no longer needed. Usual use case
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index bff49e7be..0cec39df6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -70,6 +70,7 @@
 #include "virnetdevopenvswitch.h"
 #include "capabilities.h"
 #include "viralloc.h"
+#include "virarptable.h"
 #include "viruuid.h"
 #include "domain_conf.h"
 #include "domain_audit.h"
@@ -160,6 +161,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
  virDomainObjPtr vm,
  virDomainInterfacePtr **ifaces);
 
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+virDomainInterfacePtr **ifaces);
+
 static virQEMUDriverPtr qemu_driver;
 
 
@@ -20516,6 +20520,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom,
 
 break;
 
+case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP:
+ret = qemuARPGetInterfaces(vm, ifaces);
+break;
+
 default:
 virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("Unknown IP address data source %d"),
@@ -20626,6 +20634,70 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 }
 
 
+static int
+qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces)
+{
+size_t i, j;
+size_t ifaces_count = 0;
+int ret = -1;
+char macaddr[VIR_MAC_STRING_BUFLEN];
+virDomainInterfacePtr *ifaces_ret = NULL;
+virDomainInterfacePtr iface = NULL;
+virArpTablePtr table;
+
+table = virArpTableGet();
+if (!table)
+goto cleanup;
+
+for (i = 0; i < vm->def->nnets; i++) {
+if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+continue;
+
+virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+virArpTableEntry entry;
+for (j = 0; j < table->n; j++) {
+entry = table->t[j];
+if (STREQ(entry.mac, macaddr)) {
+if (VIR_ALLOC(iface) < 0)
+goto cleanup;
+
+iface->naddrs = 1;
+if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+goto cleanup;
+
+if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->addrs->addr, entry.ipaddr) < 0)
+goto cleanup;
+
+if (VIR_APPEND_ELEMENT(ifaces_ret, ifaces_count, iface) < 0)
+goto cleanup;
+
+}
+}
+}
+
+VIR_STEAL_PTR(*ifaces, ifaces_ret);
+ret = ifaces_count;
+
+ cleanup:
+virArpTableFree(table);
+
+if (ifaces_ret) {
+for (i = 0; i < ifaces_count; i++)
+virDomainInterfaceFree(ifaces_ret[i]);
+}
+VIR_FREE(ifaces_ret);
+
+return ret;
+}
+
+
 stati

[libvirt] [PATCH v4 4/5] virsh: add --source arp to domifaddr

2018-02-07 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

We can use:
  domifaddr f26-cloud --source arp
to get the address.

Acked-by: Michal Privoznik <mpriv...@redhat.com>
Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 tools/virsh-domain-monitor.c | 2 ++
 tools/virsh.pod  | 7 ---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 32a42707e..68da11ed5 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2190,6 +2190,8 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE;
 } else if (STREQ(sourcestr, "agent")) {
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT;
+} else if (STREQ(sourcestr, "arp")) {
+source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP;
 } else {
 vshError(ctl, _("Unknown data source '%s'"), sourcestr);
 goto cleanup;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 69cc42338..1dfe2a9b0 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -759,7 +759,7 @@ B (fields appear in the following 
order):
 
 
 =item B I [I] [I<--full>]
-  [I<--source lease|agent>]
+  [I<--source lease|agent|arp>]
 
 Get a list of interfaces of a running domain along with their IP and MAC
 addresses, or limited output just for one interface if I is
@@ -774,8 +774,9 @@ only the interface name and MAC address is displayed for 
the first name and
 MAC address with "-" for the others using the same name and MAC address.
 
 The I<--source> argument specifies what data source to use for the
-addresses, currently one of 'lease' to read DHCP leases, or 'agent' to query
-the guest OS via an agent. If unspecified, 'lease' is the default.
+addresses, currently 'lease' to read DHCP leases, 'agent' to query
+the guest OS via an agent, or 'arp' to get IP from host's arp tables.
+If unspecified, 'lease' is the default.
 
 =item B I I
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v4 0/5] qemu: use arp table of host to get the

2018-02-07 Thread Chen Hanxiao
introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp

Chen Hanxiao (5):
  util: introduce virNetlinkGetNeighbor to get neighbor table entry
  util: introduce helper to parse message from RTM_GETNEIGH query
  qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
  virsh: add --source arp to domifaddr
  news: qemu: use arp table of host to get the IP address of guests

 docs/news.xml|   9 ++
 include/libvirt/libvirt-domain.h |   1 +
 po/POTFILES.in   |   1 +
 src/Makefile.am  |   1 +
 src/libvirt-domain.c |   7 ++
 src/libvirt_private.syms |   6 ++
 src/qemu/qemu_driver.c   |  72 
 src/util/virarptable.c   | 182 +++
 src/util/virarptable.h   |  47 ++
 src/util/virnetlink.c|  82 ++
 src/util/virnetlink.h|   2 +
 tools/virsh-domain-monitor.c |   2 +
 tools/virsh.pod  |   7 +-
 13 files changed, 416 insertions(+), 3 deletions(-)
 create mode 100644 src/util/virarptable.c
 create mode 100644 src/util/virarptable.h

-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] util: netlink: fix the mismatch parameter description of functions

2018-02-06 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Some of netlink functions don't have the right
@parameters description according to the declaration of function.

This patch fix them.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 src/util/virnetlink.c | 35 ++-
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index d732fe8cf..6a6508fa4 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -278,14 +278,14 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t 
src_pid,
 
 /**
  * virNetlinkCommand:
- * @nlmsg: pointer to netlink message
- * @respbuf: pointer to pointer where response buffer will be allocated
+ * @nl_msg:  pointer to netlink message
+ * @resp:   pointer to pointer where response buffer will be allocated
  * @respbuflen: pointer to integer holding the size of the response buffer
- *  on return of the function.
- * @src_pid: the pid of the process to send a message
- * @dst_pid: the pid of the process to talk to, i.e., pid = 0 for kernel
- * @protocol: netlink protocol
- * @groups: the group identifier
+ *  on return of the function.
+ * @src_pid:the pid of the process to send a message
+ * @dst_pid:the pid of the process to talk to, i.e., pid = 0 for kernel
+ * @protocol:   netlink protocol
+ * @groups: the group identifier
  *
  * Send the given message to the netlink layer and receive response.
  * Returns 0 on success, -1 on error. In case of error, no response
@@ -387,9 +387,9 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
  *
  * @ifname:  The name of the interface; only use if ifindex <= 0
  * @ifindex: The interface index; may be <= 0 if ifname is given
- * @data:Gets a pointer to the raw data from netlink.
+ * @nlData:  Gets a pointer to the raw data from netlink.
  MUST BE FREED BY CALLER!
- * @nlattr:  Pointer to a pointer of netlink attributes that will contain
+ * @tb:  Pointer to a pointer of netlink attributes that will contain
  *   the results
  * @src_pid: pid used for nl_pid of the local end of the netlink message
  *   (0 == "use getpid()")
@@ -505,7 +505,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
 /**
  * virNetlinkDelLink:
  *
- * @ifname: Name of the link
+ * @ifname:   Name of the link
  * @fallback: pointer to an alternate function that will
  *be called to perform the delete if RTM_DELLINK fails
  *with EOPNOTSUPP (any other error will simply be treated
@@ -648,7 +648,7 @@ virNetlinkEventServerUnlock(virNetlinkEventSrvPrivatePtr 
driver)
 /**
  * virNetlinkEventRemoveClientPrimitive:
  *
- * @i: index of the client to remove from the table
+ * @i:index of the client to remove from the table
  * @protocol: netlink protocol
  *
  * This static function does the low level removal of a client from
@@ -837,7 +837,8 @@ int virNetlinkEventServiceLocalPid(unsigned int protocol)
  * This registers a netlink socket with the event interface.
  *
  * @protocol: netlink protocol
- * @groups: broadcast groups to join in
+ * @groups:   broadcast groups to join in
+ *
  * Returns -1 if the monitor cannot be registered, 0 upon success
  */
 int
@@ -925,9 +926,9 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned 
int groups)
  *
  * @handleCB: callback to invoke when an event occurs
  * @removeCB: callback to invoke when removing a client
- * @opaque: user data to pass to callback
- * @macaddr: macaddr to store with the data. Used to identify callers.
- *   May be null.
+ * @opaque:   user data to pass to callback
+ * @macaddr:  macaddr to store with the data. Used to identify callers.
+ *May be null.
  * @protocol: netlink protocol
  *
  * register a callback for handling of netlink messages. The
@@ -1003,8 +1004,8 @@ virNetlinkEventAddClient(virNetlinkEventHandleCallback 
handleCB,
 /**
  * virNetlinkEventRemoveClient:
  *
- * @watch: watch whose handle to remove
- * @macaddr: macaddr whose handle to remove
+ * @watch:watch whose handle to remove
+ * @macaddr:  macaddr whose handle to remove
  * @protocol: netlink protocol
  *
  * Unregister a callback from a netlink monitor.
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 1/4] util: introduce helper to parse /proc/net/arp

2018-02-06 Thread Chen Hanxiao


At 2018-01-29 20:01:30, "Peter Krempa" <pkre...@redhat.com> wrote:
>On Mon, Jan 29, 2018 at 16:35:33 +0800, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> introduce helper to parse /proc/net/arp and
>> store it in struct virArpTable.
>> 
>> Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>> ---
>> v3:
[...]
>
>
>without limiting the size here is a great idea. That is a buffer
>overflow right here.
>
>Also parsing /proc/net/arp is not enough, it will not list IPv6
>neighbors.
>
>Additionally I'd stay away from parsing this file completely. Not even
>the obsolete 'arp' utility is using this but rather uses AF_NETLINK
>socket to discover it.
>
>NACK on this approach.

I'll use RTM_GETNEIGH to get these.
Thanks for the advice.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH go] go: Add ERR_DEVICE_MISSING constants

2018-01-31 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 error.go   | 3 +++
 error_compat.h | 6 ++
 2 files changed, 9 insertions(+)

diff --git a/error.go b/error.go
index fb218f7..9de277c 100644
--- a/error.go
+++ b/error.go
@@ -353,6 +353,9 @@ const (
 
// error in libssh transport driver
ERR_LIBSSH = ErrorNumber(C.VIR_ERR_LIBSSH)
+
+   // libvirt fail to find the desired device
+   ERR_DEVICE_MISSING = ErrorNumber(C.VIR_ERR_DEVICE_MISSING)
 )
 
 type ErrorDomain int
diff --git a/error_compat.h b/error_compat.h
index f26dc09..64ed991 100644
--- a/error_compat.h
+++ b/error_compat.h
@@ -147,4 +147,10 @@
 #define VIR_FROM_RESCTRL 67
 #endif
 
+/* 4.1.0 */
+
+#ifndef VIR_ERR_DEVICE_MISSING
+#define VIR_ERR_DEVICE_MISSING 99
+#endif
+
 #endif /* LIBVIRT_GO_ERROR_COMPAT_H__ */
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 4/5] qemu: Use VIR_ERR_DEVICE_MISSING for various DetachDeviceConfig messages

2018-01-31 Thread Chen Hanxiao


At 2018-01-31 23:34:55, "John Ferlan" <jfer...@redhat.com> wrote:
>
>Let's alter the commit message to be more similar to patch 3/5, e.g.:
>
>qemu: Use VIR_ERR_DEVICE_MISSING for various coldplug messages
>
>On 01/22/2018 11:24 PM, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> Modify OPERATION_FAILED error codes to use DEVICE_MISSING instead.
>
>Changing this to:
>
>Use the DEVICE_MISSING error code when helpers fail to find
>the requested device. This makes it easier for consumers to
>key off the error code rather than the error message.
>
>
>...
>
>again no need to post an update, I can make the adjustment...
>
>> 
>> Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>> ---
>>  src/qemu/qemu_driver.c | 20 ++--
>>  1 file changed, 10 insertions(+), 10 deletions(-)
>> 
>> diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
>> index a203c9297..10eebd61c 100644
>> --- a/src/qemu/qemu_driver.c
>> +++ b/src/qemu/qemu_driver.c
>> @@ -8172,7 +8172,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
>>  case VIR_DOMAIN_DEVICE_HOSTDEV: {
>
>Any specific reason you didn't alter the VIR_DOMAIN_DEVICE_DISK error
>code as well?  I can do that for you before pushing.
>

Ahh, my fault.
Thanks for your help.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH perl] perl: Add ERR_DEVICE_MISSING constants

2018-01-31 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 Changes   | 2 +-
 Virt.xs   | 1 +
 lib/Sys/Virt/Error.pm | 4 
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/Changes b/Changes
index eda3ec5..22d5752 100644
--- a/Changes
+++ b/Changes
@@ -2,7 +2,7 @@ Revision history for perl module Sys::Virt
 
 4.1.0 2018-00-00
 
- - XXX
+ - Add VIR_ERR_DEVICE_MISSING constants
 
 4.0.0 2018-01-23
 
diff --git a/Virt.xs b/Virt.xs
index 7d2f1a7..f19880f 100644
--- a/Virt.xs
+++ b/Virt.xs
@@ -9488,4 +9488,5 @@ BOOT:
   REGISTER_CONSTANT(VIR_ERR_NO_CLIENT, ERR_NO_CLIENT);
   REGISTER_CONSTANT(VIR_ERR_AGENT_UNSYNCED, ERR_AGENT_UNSYNCED);
   REGISTER_CONSTANT(VIR_ERR_LIBSSH, ERR_LIBSSH);
+  REGISTER_CONSTANT(VIR_ERR_DEVICE_MISSING, ERR_DEVICE_MISSING);
 }
diff --git a/lib/Sys/Virt/Error.pm b/lib/Sys/Virt/Error.pm
index b97985d..78f33f3 100644
--- a/lib/Sys/Virt/Error.pm
+++ b/lib/Sys/Virt/Error.pm
@@ -812,6 +812,10 @@ Libvirt guest agent replies with wrong id to guest-sync 
command
 
 The libSSH operation failed
 
+=item Sys::Virt::Error::ERR_DEVICE_MISSING
+
+Libvirt fail to find the desired device.
+
 =back
 
 =head1 AUTHORS
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 3/5] qemu: Use VIR_ERR_DEVICE_MISSING for various hotplug messages

2018-01-31 Thread Chen Hanxiao


At 2018-01-31 23:33:35, "John Ferlan" <jfer...@redhat.com> wrote:
>
>
>On 01/22/2018 11:24 PM, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> Modify OPERATION_FAILED error codes to use DEVICE_MISSING instead.
>
>I'll modify this one to:
>
>Modify OPERATION_FAILED and INTERNAL_ERROR error codes to use
>DEVICE_MISSING instead for failures associated with the inability to
>find the device. This makes it easier for consumers to key off the error
>code rather than the error message.
>
>No need to post another series - just let me know if you're fine with
>the adjusted text...
>

Thanks for this adjustment.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 2/5] qemu: Introduce VIR_ERR_DEVICE_MISSING

2018-01-31 Thread Chen Hanxiao


At 2018-01-31 23:32:35, "John Ferlan" <jfer...@redhat.com> wrote:
>
>
>On 01/22/2018 11:24 PM, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> Add new error code to be able to allow consumers (such as Nova) to be
>> able to key of a specific error code rather than needing to search the
>> error message."
>> 
>> Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>> ---
>> v3:
>>   commit message updated
>> 
>>  include/libvirt/virterror.h | 1 +
>>  src/util/virerror.c | 6 ++
>>  2 files changed, 7 insertions(+)
>> 
>
>BTW: This introduces issues for libvirt-perl and I'm assuming libvirt-go
>- can you generate patches for those and post them?
>

Sure, patches will come soon.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 4/4] news: qemu: use arp table of host to get the IP address of guests

2018-01-29 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  more verbose description

 docs/news.xml | 9 +
 1 file changed, 9 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index 2268fdf79..706546e6d 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -56,6 +56,15 @@
   interfaces, NWFilters, and so on).
 
   
+  
+
+  qemu: use arp table of host to get the IP address of guests
+
+
+  Find IP address of a VM by arp table on hosts.
+  If someone customizing IP address inside VM, it will be helpful.
+
+  
 
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 0/4] qemu: use arp table of host to get the

2018-01-29 Thread Chen Hanxiao
introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp

Chen Hanxiao (4):
  util: introduce helper to parse /proc/net/arp
  qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
  virsh: add --source arp to domifaddr
  news: qemu: use arp table of host to get the IP address of guests

 docs/news.xml|   9 
 include/libvirt/libvirt-domain.h |   1 +
 po/POTFILES.in   |   1 +
 src/Makefile.am  |   1 +
 src/libvirt-domain.c |   7 +++
 src/libvirt_private.syms |   5 ++
 src/qemu/qemu_driver.c   |  96 +
 src/util/virarptable.c   | 114 +++
 src/util/virarptable.h   |  48 +
 tools/virsh-domain-monitor.c |   2 +
 tools/virsh.pod  |   7 +--
 11 files changed, 288 insertions(+), 3 deletions(-)
 create mode 100644 src/util/virarptable.c
 create mode 100644 src/util/virarptable.h

-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 3/4] virsh: add --source arp to domifaddr

2018-01-29 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

We can use:
  domifaddr f26-cloud --source arp
to get the address.

Acked-by: Michal Privoznik <mpriv...@redhat.com>
Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 tools/virsh-domain-monitor.c | 2 ++
 tools/virsh.pod  | 7 ---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 32a42707e..68da11ed5 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2190,6 +2190,8 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE;
 } else if (STREQ(sourcestr, "agent")) {
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT;
+} else if (STREQ(sourcestr, "arp")) {
+source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP;
 } else {
 vshError(ctl, _("Unknown data source '%s'"), sourcestr);
 goto cleanup;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 69cc42338..1dfe2a9b0 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -759,7 +759,7 @@ B (fields appear in the following 
order):
 
 
 =item B I [I] [I<--full>]
-  [I<--source lease|agent>]
+  [I<--source lease|agent|arp>]
 
 Get a list of interfaces of a running domain along with their IP and MAC
 addresses, or limited output just for one interface if I is
@@ -774,8 +774,9 @@ only the interface name and MAC address is displayed for 
the first name and
 MAC address with "-" for the others using the same name and MAC address.
 
 The I<--source> argument specifies what data source to use for the
-addresses, currently one of 'lease' to read DHCP leases, or 'agent' to query
-the guest OS via an agent. If unspecified, 'lease' is the default.
+addresses, currently 'lease' to read DHCP leases, 'agent' to query
+the guest OS via an agent, or 'arp' to get IP from host's arp tables.
+If unspecified, 'lease' is the default.
 
 =item B I I
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 2/4] qemu: introduce qemuARPGetInterfaces to get IP from host's arp table

2018-01-29 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  add docs in virDomainInterfaceAddresses
  remove error label
  show network interface which did not match the arp table

 include/libvirt/libvirt-domain.h |  1 +
 src/libvirt-domain.c |  7 +++
 src/qemu/qemu_driver.c   | 96 
 3 files changed, 104 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4048acf38..38e2d9a3e 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr;
 typedef enum {
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
+VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */
 
 # ifdef VIR_ENUM_SENTINELS
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index eaec0979a..1ae83610d 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -11721,6 +11721,13 @@ virDomainFSInfoFree(virDomainFSInfoPtr info)
  * To match such interface with the one from @dom XML use MAC address or IP
  * range.
  *
+ * If @source is VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, the /proc/net/arp
+ * will be check to obtain the interface addresses.
+ * As the arp cache did not refresh in time, the returned ip address
+ * may be unreachable.
+ * As the route config of the guest, the returned mac address
+ * may be duplicated.
+ *
  * @ifaces->name and @ifaces->hwaddr are never NULL.
  *
  * The caller *must* free @ifaces when no longer needed. Usual use case
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a203c9297..d0cb88053 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -70,6 +70,7 @@
 #include "virnetdevopenvswitch.h"
 #include "capabilities.h"
 #include "viralloc.h"
+#include "virarptable.h"
 #include "viruuid.h"
 #include "domain_conf.h"
 #include "domain_audit.h"
@@ -160,6 +161,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
  virDomainObjPtr vm,
  virDomainInterfacePtr **ifaces);
 
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+virDomainInterfacePtr **ifaces);
+
 static virQEMUDriverPtr qemu_driver;
 
 
@@ -20384,6 +20388,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom,
 
 break;
 
+case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP:
+ret = qemuARPGetInterfaces(vm, ifaces);
+break;
+
 default:
 virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("Unknown IP address data source %d"),
@@ -20494,6 +20502,94 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 }
 
 
+static int
+qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces)
+{
+size_t i, j;
+size_t ifaces_count = 0;
+int ret = -1;
+char macaddr[VIR_MAC_STRING_BUFLEN];
+virDomainInterfacePtr *ifaces_ret = NULL;
+virDomainInterfacePtr iface = NULL;
+virArpTablePtr table;
+int got_one = 0;
+
+table = virArpTableGet();
+if (!table)
+goto cleanup;
+
+for (i = 0; i < vm->def->nnets; i++) {
+got_one = 0;
+if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+continue;
+
+virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+virArpTableEntry entry;
+for (j = 0; j < table->n; j++) {
+entry = table->t[j];
+if (STREQ(entry.mac, macaddr)) {
+got_one = 1;
+if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
+goto cleanup;
+if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
+goto cleanup;
+
+iface = ifaces_ret[ifaces_count - 1];
+iface->naddrs = 1;
+if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->addrs->addr, entry.ipaddr) < 0)
+goto cleanup;
+}
+}
+
+if (got_one == 0) {
+if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
+goto cleanup;
+if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
+   

[libvirt] [PATCH v3 1/4] util: introduce helper to parse /proc/net/arp

2018-01-29 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce helper to parse /proc/net/arp and
store it in struct virArpTable.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  s/virGetArpTable/virArpTableGet
  alloc virArpTable in virArpTableGet
  return ENOSUPP on none-Linux platform
  move helpers to virarptable.[ch]

 po/POTFILES.in   |   1 +
 src/Makefile.am  |   1 +
 src/libvirt_private.syms |   5 +++
 src/util/virarptable.c   | 114 +++
 src/util/virarptable.h   |  48 
 5 files changed, 169 insertions(+)
 create mode 100644 src/util/virarptable.c
 create mode 100644 src/util/virarptable.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8382ee633..91a4c5730 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -193,6 +193,7 @@ src/uml/uml_conf.c
 src/uml/uml_driver.c
 src/util/iohelper.c
 src/util/viralloc.c
+src/util/virarptable.c
 src/util/viraudit.c
 src/util/virauth.c
 src/util/virauthconfig.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 289b03747..923eea6fe 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -98,6 +98,7 @@ augeastest_DATA =
 UTIL_SOURCES = \
util/viralloc.c util/viralloc.h \
util/virarch.h util/virarch.c \
+   util/virarptable.h util/virarptable.c \
util/viratomic.h util/viratomic.c \
util/viraudit.c util/viraudit.h \
util/virauth.c util/virauth.h \
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f2a2c8650..08229797a 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1332,6 +1332,11 @@ virArchGetWordSize;
 virArchToString;
 
 
+# util/virarptable.h
+virArpTableFree;
+virArpTableGet;
+
+
 # util/viraudit.h
 virAuditClose;
 virAuditEncode;
diff --git a/src/util/virarptable.c b/src/util/virarptable.c
new file mode 100644
index 0..48a38352f
--- /dev/null
+++ b/src/util/virarptable.c
@@ -0,0 +1,114 @@
+/*
+ * virarptable.c Linux ARP table handling
+ *
+ * Copyright (C) 2018 Chen Hanxiao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Chen Hanxiao <chenhanx...@gmail.com>
+ */
+
+#include 
+
+#include 
+#include 
+
+#include "viralloc.h"
+#include "virarptable.h"
+#include "virfile.h"
+#include "virstring.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+#ifdef __linux__
+
+virArpTablePtr virArpTableGet(void)
+{
+FILE *fp = NULL;
+char line[1024];
+int num = 0;
+virArpTablePtr table = NULL;
+
+if (VIR_ALLOC(table) < 0)
+return NULL;
+
+if (!(fp = fopen("/proc/net/arp", "r")))
+goto cleanup;
+
+while (fgets(line, sizeof(line), fp)) {
+char ip[32], mac[32], dev_name[32], hwtype[32],
+ flags[32], mask[32], nouse[32];
+
+if (STRPREFIX(line, "IP address"))
+continue;
+
+if (VIR_REALLOC_N(table->t, num + 1) < 0)
+goto cleanup;
+
+table->n = num + 1;
+
+/* /proc/net/arp looks like:
+ * 172.16.17.254  0x1 0x2  e4:68:a3:8d:ed:d3  *   enp3s0
+ */
+sscanf(line, "%[0-9.]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ 
\t\n]",
+   ip, nouse,
+   hwtype, nouse,
+   flags, nouse,
+   mac, nouse,
+   mask, nouse,
+   dev_name);
+
+if (VIR_STRDUP(table->t[num].ipaddr, ip) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(table->t[num].mac, mac) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(table->t[num].dev_name, dev_name) < 0)
+goto cleanup;
+
+num++;
+}
+
+return table;
+
+ cleanup:
+VIR_FORCE_FCLOSE(fp);
+return NULL;
+}
+
+#else
+
+virArpTablePtr virArpTableGet(void)
+{
+virReportError(VIR_ERR_NO_SUPPORT, "%s",
+   _("get arp table not implemented on this platform"));
+return NULL;
+}
+
+#endif /* __linux__ */
+
+void
+virArpTableFree(virArpTablePtr table)
+{
+size_t i;
+for (i = 0; i < table->n; i++) {
+VIR_FREE(table->t[i].ipaddr);
+VIR_FREE(table->t[i].mac);
+VIR_FREE(table->t[i].dev_name);
+}
+VIR_FREE(t

[libvirt] [PATCH v2 0/4] qemu: use arp table of host to get the

2018-01-24 Thread Chen Hanxiao
introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp

Chen Hanxiao (4):
  util: introduce helper to parse /proc/net/arp
  qemu: introduce qemuARPGetInterfaces to get IP from host's arp table
  virsh: add --source arp to domifaddr
  news: qemu: use arp table of host to get the IP address of guests

 docs/news.xml|  5 +++
 include/libvirt/libvirt-domain.h |  1 +
 src/libvirt_private.syms |  2 ++
 src/qemu/qemu_driver.c   | 75 
 src/util/virmacaddr.c| 67 +++
 src/util/virmacaddr.h| 18 ++
 tools/virsh-domain-monitor.c |  2 ++
 tools/virsh.pod  |  7 ++--
 8 files changed, 174 insertions(+), 3 deletions(-)

-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 1/4] util: introduce helper to parse /proc/net/arp

2018-01-24 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce helper to parse /proc/net/arp and
store it in struct virArpTable.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virmacaddr.c| 67 
 src/util/virmacaddr.h| 18 +
 3 files changed, 87 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bc8cc1fba..26385a2e9 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2133,6 +2133,8 @@ virLogVMessage;
 
 
 # util/virmacaddr.h
+virArpTableFree;
+virGetArpTable;
 virMacAddrCmp;
 virMacAddrCmpRaw;
 virMacAddrCompare;
diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c
index 409fdc34d..540bdbbaa 100644
--- a/src/util/virmacaddr.c
+++ b/src/util/virmacaddr.c
@@ -27,10 +27,15 @@
 #include 
 
 #include "c-ctype.h"
+#include "viralloc.h"
+#include "virfile.h"
 #include "virmacaddr.h"
 #include "virrandom.h"
+#include "virstring.h"
 #include "virutil.h"
 
+#define VIR_FROM_THIS VIR_FROM_NONE
+
 static const unsigned char virMacAddrBroadcastAddrRaw[VIR_MAC_BUFLEN] =
 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
@@ -257,3 +262,65 @@ virMacAddrIsBroadcastRaw(const unsigned char 
s[VIR_MAC_BUFLEN])
 {
 return memcmp(virMacAddrBroadcastAddrRaw, s, sizeof(*s)) == 0;
 }
+
+int
+virGetArpTable(virArpTablePtr *table)
+{
+#define PROC_NET_ARP"/proc/net/arp"
+FILE *fp = NULL;
+char line[1024];
+int num = 0;
+int ret = -1;
+
+if (!(fp = fopen(PROC_NET_ARP, "r")))
+goto cleanup;
+
+while (fgets(line, sizeof(line), fp)) {
+char ip[32], mac[32], dev_name[32], hwtype[32],
+ flags[32], mask[32], nouse[32];
+
+if (STRPREFIX(line, "IP address"))
+continue;
+
+num++;
+if (VIR_REALLOC_N((*table)->t, num) < 0)
+goto cleanup;
+(*table)->n = num;
+/* /proc/net/arp looks like:
+ * 172.16.17.254  0x1 0x2  e4:68:a3:8d:ed:d3  *   enp3s0
+ */
+sscanf(line, "%[0-9.]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ ]%[ ]%[^ 
\t\n]",
+   ip, nouse,
+   hwtype, nouse,
+   flags, nouse,
+   mac, nouse,
+   mask, nouse,
+   dev_name);
+
+
+if (VIR_STRDUP((*table)->t[num - 1].ipaddr, ip) < 0)
+goto cleanup;
+if (VIR_STRDUP((*table)->t[num - 1].mac, mac) < 0)
+goto cleanup;
+if (VIR_STRDUP((*table)->t[num - 1].dev_name, dev_name) < 0)
+goto cleanup;
+}
+
+ret = 0;
+
+ cleanup:
+VIR_FORCE_FCLOSE(fp);
+return ret;
+}
+
+void
+virArpTableFree(virArpTablePtr table)
+{
+size_t i;
+for (i = 0; i < table->n; i++) {
+VIR_FREE(table->t[i].ipaddr);
+VIR_FREE(table->t[i].mac);
+VIR_FREE(table->t[i].dev_name);
+}
+VIR_FREE(table);
+}
diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
index ef4285d63..eb18092d1 100644
--- a/src/util/virmacaddr.h
+++ b/src/util/virmacaddr.h
@@ -40,6 +40,22 @@ struct _virMacAddr {
false otherwise. */
 };
 
+typedef struct _virArpTableEntry virArpTableEntry;
+typedef virArpTableEntry *virArpTableEntryPtr;
+typedef struct _virArpTable virArpTable;
+typedef virArpTable *virArpTablePtr;
+
+struct _virArpTableEntry{
+char *ipaddr;
+char *mac;
+char *dev_name;
+};
+
+struct _virArpTable {
+int n;
+virArpTableEntryPtr t;
+};
+
 int virMacAddrCompare(const char *mac1, const char *mac2);
 int virMacAddrCmp(const virMacAddr *mac1, const virMacAddr *mac2);
 int virMacAddrCmpRaw(const virMacAddr *mac1,
@@ -59,5 +75,7 @@ int virMacAddrParseHex(const char* str,
 bool virMacAddrIsUnicast(const virMacAddr *addr);
 bool virMacAddrIsMulticast(const virMacAddr *addr);
 bool virMacAddrIsBroadcastRaw(const unsigned char s[VIR_MAC_BUFLEN]);
+int virGetArpTable(virArpTablePtr *table);
+void virArpTableFree(virArpTablePtr table);
 
 #endif /* __VIR_MACADDR_H__ */
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 3/4] virsh: add --source arp to domifaddr

2018-01-24 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

We can use:
  domifaddr f26-cloud --source arp
to get the address.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 tools/virsh-domain-monitor.c | 2 ++
 tools/virsh.pod  | 7 ---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 32a42707e..68da11ed5 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -2190,6 +2190,8 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE;
 } else if (STREQ(sourcestr, "agent")) {
 source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT;
+} else if (STREQ(sourcestr, "arp")) {
+source = VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP;
 } else {
 vshError(ctl, _("Unknown data source '%s'"), sourcestr);
 goto cleanup;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 69cc42338..1dfe2a9b0 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -759,7 +759,7 @@ B (fields appear in the following 
order):
 
 
 =item B I [I] [I<--full>]
-  [I<--source lease|agent>]
+  [I<--source lease|agent|arp>]
 
 Get a list of interfaces of a running domain along with their IP and MAC
 addresses, or limited output just for one interface if I is
@@ -774,8 +774,9 @@ only the interface name and MAC address is displayed for 
the first name and
 MAC address with "-" for the others using the same name and MAC address.
 
 The I<--source> argument specifies what data source to use for the
-addresses, currently one of 'lease' to read DHCP leases, or 'agent' to query
-the guest OS via an agent. If unspecified, 'lease' is the default.
+addresses, currently 'lease' to read DHCP leases, 'agent' to query
+the guest OS via an agent, or 'arp' to get IP from host's arp tables.
+If unspecified, 'lease' is the default.
 
 =item B I I
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 2/4] qemu: introduce qemuARPGetInterfaces to get IP from host's arp table

2018-01-24 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of /proc/net/arp

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 include/libvirt/libvirt-domain.h |  1 +
 src/qemu/qemu_driver.c   | 75 
 2 files changed, 76 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4048acf38..38e2d9a3e 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr;
 typedef enum {
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
+VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */
 
 # ifdef VIR_ENUM_SENTINELS
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a203c9297..e31a74261 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -160,6 +160,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
  virDomainObjPtr vm,
  virDomainInterfacePtr **ifaces);
 
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+virDomainInterfacePtr **ifaces);
+
 static virQEMUDriverPtr qemu_driver;
 
 
@@ -20384,6 +20387,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom,
 
 break;
 
+case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP:
+ret = qemuARPGetInterfaces(vm, ifaces);
+break;
+
 default:
 virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("Unknown IP address data source %d"),
@@ -20494,6 +20501,74 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 }
 
 
+static int
+qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces)
+{
+size_t i, j;
+size_t ifaces_count = 0;
+int ret = -1;
+char macaddr[VIR_MAC_STRING_BUFLEN];
+virDomainInterfacePtr *ifaces_ret = NULL;
+virDomainInterfacePtr iface = NULL;
+
+virArpTablePtr table;
+if (VIR_ALLOC(table) < 0)
+goto error;
+if (virGetArpTable() < 0)
+goto cleanup;
+
+for (i = 0; i < vm->def->nnets; i++) {
+if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+continue;
+
+virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+virArpTableEntry entry;
+for (j = 0; j < table->n; j++) {
+entry = table->t[j];
+if (STREQ(entry.mac, macaddr)) {
+if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
+goto error;
+
+if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
+goto error;
+
+iface = ifaces_ret[ifaces_count - 1];
+iface->naddrs = 1;
+if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+goto error;
+
+if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->addrs->addr, entry.ipaddr) < 0)
+goto cleanup;
+}
+}
+}
+
+*ifaces = ifaces_ret;
+ifaces_ret = NULL;
+ret = ifaces_count;
+
+ cleanup:
+virArpTableFree(table);
+return ret;
+
+ error:
+if (ifaces_ret) {
+for (i = 0; i < ifaces_count; i++)
+virDomainInterfaceFree(ifaces_ret[i]);
+}
+VIR_FREE(ifaces_ret);
+
+goto cleanup;
+}
+
+
 static int
 qemuDomainSetUserPassword(virDomainPtr dom,
   const char *user,
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 4/4] news: qemu: use arp table of host to get the IP address of guests

2018-01-24 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 docs/news.xml | 5 +
 1 file changed, 5 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index b9e04c632..105917f4d 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -47,6 +47,11 @@
   interfaces, NWFilters, and so on).
 
   
+  
+
+  qemu: use arp table of host to get the IP address of guests
+
+  
 
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] qemu: get VM's ip address from the output of arp command

2018-01-24 Thread Chen Hanxiao



At 2018-01-24 02:01:27, "Martin Kletzander" <mklet...@redhat.com> wrote:
>On Tue, Jan 23, 2018 at 03:40:02PM +0800, Chen Hanxiao wrote:
>>From: Chen Hanxiao <chenhanx...@gmail.com>
>>
>>Introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
>>of VM from the output of `arp -an` command.
>>
>
>I'm not sure about the implications of this as sometimes we can't use that, but
>I guess that's fine.
>
>>We can use:
>>  domifaddr f26-cloud --source arp
>>to get the address.
>>
>>Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>>---
>> include/libvirt/libvirt-domain.h |   1 +
>> src/qemu/qemu_driver.c   | 102 
>> +++
>> tools/virsh-domain-monitor.c |   2 +
>> 3 files changed, 105 insertions(+)
>>
>
>I see no documentation changes here.
>
>>diff --git a/include/libvirt/libvirt-domain.h 
>>b/include/libvirt/libvirt-domain.h
[...]
>>+
>
>I think this should be determined like other programs in
>`m4/virt-external-programs.m4`.
>
>But rather than this, is this supported under non-Linux?  I don't know how arp
>works elsewhere, but for Linux we could just parse /proc/net/arp ourselves.

Sure, will be done in v2.

Regards,
- Chen


--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] qemu: get VM's ip address from the output of arp command

2018-01-23 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Introduce VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP to get ip address
of VM from the output of `arp -an` command.

We can use:
  domifaddr f26-cloud --source arp
to get the address.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 include/libvirt/libvirt-domain.h |   1 +
 src/qemu/qemu_driver.c   | 102 +++
 tools/virsh-domain-monitor.c |   2 +
 3 files changed, 105 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 4048acf38..38e2d9a3e 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -4665,6 +4665,7 @@ typedef virMemoryParameter *virMemoryParameterPtr;
 typedef enum {
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE = 0, /* Parse DHCP lease file */
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT = 1, /* Query qemu guest agent */
+VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP = 2, /* Query ARP tables */
 
 # ifdef VIR_ENUM_SENTINELS
 VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LAST
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a203c9297..5aaf69442 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -160,6 +160,9 @@ static int qemuGetDHCPInterfaces(virDomainPtr dom,
  virDomainObjPtr vm,
  virDomainInterfacePtr **ifaces);
 
+static int qemuARPGetInterfaces(virDomainObjPtr vm,
+virDomainInterfacePtr **ifaces);
+
 static virQEMUDriverPtr qemu_driver;
 
 
@@ -20384,6 +20387,10 @@ qemuDomainInterfaceAddresses(virDomainPtr dom,
 
 break;
 
+case VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP:
+ret = qemuARPGetInterfaces(vm, ifaces);
+break;
+
 default:
 virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("Unknown IP address data source %d"),
@@ -20494,6 +20501,101 @@ qemuGetDHCPInterfaces(virDomainPtr dom,
 }
 
 
+static int
+qemuARPGetInterfaces(virDomainObjPtr vm,
+ virDomainInterfacePtr **ifaces)
+{
+size_t i, j;
+size_t ifaces_count = 0;
+int ret = -1;
+char macaddr[VIR_MAC_STRING_BUFLEN];
+virDomainInterfacePtr *ifaces_ret = NULL;
+virDomainInterfacePtr iface = NULL;
+virCommandPtr cmd = NULL;
+char *outbuf;
+char **lines = NULL;
+char **matches = NULL;
+#define ARP_CMD "/usr/sbin/arp"
+
+if (!(cmd = virCommandNewArgList(ARP_CMD,
+ "-an",
+ NULL)))
+goto cleanup;
+
+virCommandSetOutputBuffer(cmd, );
+
+if (virCommandRun(cmd, NULL) < 0)
+goto cleanup;
+
+lines = virStringSplit(outbuf, "\n", 0);
+if (lines == NULL)
+goto cleanup;
+
+for (i = 0; i < vm->def->nnets; i++) {
+if (vm->def->nets[i]->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+continue;
+
+virMacAddrFormat(&(vm->def->nets[i]->mac), macaddr);
+
+if (strstr(outbuf, macaddr)) {
+for (j = 0; lines[j]; j++) {
+const char *line = lines[j];
+if (line == NULL)
+break;
+if (strstr(line, macaddr) &&
+virStringSearch(line, "\\b([0-9]{1,3}\\.){3}[0-9]{1,3}\\b",
+1, ) == 1) {
+
+if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
+goto error;
+
+if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
+goto error;
+
+iface = ifaces_ret[ifaces_count - 1];
+iface->naddrs = 1;
+if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
+goto error;
+
+if (VIR_STRDUP(iface->name, vm->def->nets[i]->ifname) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->hwaddr, macaddr) < 0)
+goto cleanup;
+
+if (VIR_STRDUP(iface->addrs->addr, matches[0]) < 0)
+goto cleanup;
+}
+}
+} else {
+VIR_DEBUG("Got nothing");
+continue;
+}
+}
+
+*ifaces = ifaces_ret;
+ifaces_ret = NULL;
+ret = ifaces_count;
+
+ cleanup:
+virCommandFree(cmd);
+VIR_FREE(outbuf);
+virStringListFree(lines);
+virStringListFree(matches);
+cmd = NULL;
+return ret;
+
+ error:
+if (ifaces_ret) {
+for (i = 0; i < ifaces_count; i++)
+virDomainInterfaceFree(ifaces_ret[i]);
+}
+VIR_FREE(ifaces_ret);
+
+goto cleanup;
+}
+
+
 static int
 qemuDomainSetUserPassword(virDomainPtr dom,
   const char *user,
diff --git a/tools/vir

[libvirt] [PATCH v3 1/5] qemu: Add some more details for hotplug errors when device not found

2018-01-22 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

More proper/detail error messages updated.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  subject changed.
  some description of logs changed.

 src/libvirt_private.syms |  2 ++
 src/qemu/qemu_hotplug.c  | 35 +++
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bc8cc1fba..70a91e5fd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -203,6 +203,7 @@ virDomainChrConsoleTargetTypeToString;
 virDomainChrDefForeach;
 virDomainChrDefFree;
 virDomainChrDefNew;
+virDomainChrDeviceTypeToString;
 virDomainChrEquals;
 virDomainChrFind;
 virDomainChrGetDomainPtrs;
@@ -427,6 +428,7 @@ virDomainMemoryDefFree;
 virDomainMemoryFindByDef;
 virDomainMemoryFindInactiveByDef;
 virDomainMemoryInsert;
+virDomainMemoryModelTypeToString;
 virDomainMemoryRemove;
 virDomainMemorySourceTypeFromString;
 virDomainMemorySourceTypeToString;
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 6b245bd6a..1f67ab74a 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -3514,8 +3514,9 @@ qemuDomainChangeGraphics(virQEMUDriverPtr driver,
 int ret = -1;
 
 if (!olddev) {
-virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-   _("cannot find existing graphics device to modify"));
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _("cannot find existing graphics device to modify of "
+ "type '%s'"), type);
 goto cleanup;
 }
 
@@ -5101,8 +5102,10 @@ qemuDomainDetachShmemDevice(virQEMUDriverPtr driver,
 qemuDomainObjPrivatePtr priv = vm->privateData;
 
 if ((idx = virDomainShmemDefFind(vm->def, dev)) < 0) {
-virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-   _("device not present in domain configuration"));
+virReportError(VIR_ERR_OPERATION_INVALID,
+   _("model '%s' shmem device not present "
+ "in domain configuration"),
+   virDomainShmemModelTypeToString(dev->model));
 return -1;
 }
 
@@ -5158,8 +5161,10 @@ qemuDomainDetachWatchdog(virQEMUDriverPtr driver,
   watchdog->model == dev->model &&
   watchdog->action == dev->action &&
   virDomainDeviceInfoAddressIsEqual(>info, >info))) {
-virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-   _("watchdog device not present in domain 
configuration"));
+virReportError(VIR_ERR_OPERATION_INVALID,
+   _("model '%s' watchdog device not present "
+ "in domain configuration"),
+   virDomainWatchdogModelTypeToString(watchdog->model));
 return -1;
 }
 
@@ -5429,8 +5434,10 @@ int qemuDomainDetachChrDevice(virQEMUDriverPtr driver,
 char *devstr = NULL;
 
 if (!(tmpChr = virDomainChrFind(vmdef, chr))) {
-virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-   _("device not present in domain configuration"));
+virReportError(VIR_ERR_OPERATION_INVALID,
+   _("chr type '%s' device not present "
+ "in domain configuration"),
+   virDomainChrDeviceTypeToString(chr->deviceType));
 goto cleanup;
 }
 
@@ -5476,8 +5483,10 @@ qemuDomainDetachRNGDevice(virQEMUDriverPtr driver,
 int ret = -1;
 
 if ((idx = virDomainRNGFind(vm->def, rng)) < 0) {
-virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-   _("device not present in domain configuration"));
+virReportError(VIR_ERR_OPERATION_INVALID,
+   _("model '%s' RNG device not present "
+ "in domain configuration"),
+   virDomainRNGBackendTypeToString(rng->model));
 return -1;
 }
 
@@ -5519,8 +5528,10 @@ qemuDomainDetachMemoryDevice(virQEMUDriverPtr driver,
 qemuDomainMemoryDeviceAlignSize(vm->def, memdef);
 
 if ((idx = virDomainMemoryFindByDef(vm->def, memdef)) < 0) {
-virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-   _("device not present in domain configuration"));
+virReportError(VIR_ERR_OPERATION_INVALID,
+   _("model '%s' memory device not present "
+ "in the domain configuration"),
+   virDomainMemoryModelTypeToString(memdef->model));
 return -1;
 }
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 4/5] qemu: Use VIR_ERR_DEVICE_MISSING for various DetachDeviceConfig messages

2018-01-22 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Modify OPERATION_FAILED error codes to use DEVICE_MISSING instead.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 src/qemu/qemu_driver.c | 20 ++--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a203c9297..10eebd61c 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -8172,7 +8172,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 case VIR_DOMAIN_DEVICE_HOSTDEV: {
 hostdev = dev->data.hostdev;
 if ((idx = virDomainHostdevFind(vmdef, hostdev, _hostdev)) < 0) {
-virReportError(VIR_ERR_INVALID_ARG, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("device not present in domain configuration"));
 return -1;
 }
@@ -8184,7 +8184,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 case VIR_DOMAIN_DEVICE_LEASE:
 lease = dev->data.lease;
 if (!(det_lease = virDomainLeaseRemove(vmdef, lease))) {
-virReportError(VIR_ERR_INVALID_ARG,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("Lease %s in lockspace %s does not exist"),
lease->key, NULLSTR(lease->lockspace));
 return -1;
@@ -8196,7 +8196,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 cont = dev->data.controller;
 if ((idx = virDomainControllerFind(vmdef, cont->type,
cont->idx)) < 0) {
-virReportError(VIR_ERR_INVALID_ARG, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("device not present in domain configuration"));
 return -1;
 }
@@ -8218,7 +8218,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 fs = dev->data.fs;
 idx = virDomainFSIndexByName(vmdef, fs->dst);
 if (idx < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("no matching filesystem device was found"));
 return -1;
 }
@@ -8229,7 +8229,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 
 case VIR_DOMAIN_DEVICE_RNG:
 if ((idx = virDomainRNGFind(vmdef, dev->data.rng)) < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("no matching RNG device was found"));
 return -1;
 }
@@ -8240,7 +8240,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 case VIR_DOMAIN_DEVICE_MEMORY:
 if ((idx = virDomainMemoryFindInactiveByDef(vmdef,
 dev->data.memory)) < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("matching memory device was not found"));
 return -1;
 }
@@ -8252,7 +8252,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 case VIR_DOMAIN_DEVICE_REDIRDEV:
 if ((idx = virDomainRedirdevDefFind(vmdef,
 dev->data.redirdev)) < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("no matching redirdev was not found"));
 return -1;
 }
@@ -8262,7 +8262,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 
 case VIR_DOMAIN_DEVICE_SHMEM:
 if ((idx = virDomainShmemDefFind(vmdef, dev->data.shmem)) < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("matching shmem device was not found"));
 return -1;
 }
@@ -8273,7 +8273,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 
 case VIR_DOMAIN_DEVICE_WATCHDOG:
 if (!vmdef->watchdog) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("domain has no watchdog"));
 return -1;
 }
@@ -8283,7 +8283,7 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
 
 case VIR_DOMAIN_DEVICE_INPUT:
 if ((idx = virDomainInputDefFind(vmdef, dev->data.input)) < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("matching input device not found"));
 return -1;
 }
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 2/5] qemu: Introduce VIR_ERR_DEVICE_MISSING

2018-01-22 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Add new error code to be able to allow consumers (such as Nova) to be
able to key of a specific error code rather than needing to search the
error message."

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  commit message updated

 include/libvirt/virterror.h | 1 +
 src/util/virerror.c | 6 ++
 2 files changed, 7 insertions(+)

diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
index 91ba29784..3e7c7a02c 100644
--- a/include/libvirt/virterror.h
+++ b/include/libvirt/virterror.h
@@ -320,6 +320,7 @@ typedef enum {
 VIR_ERR_AGENT_UNSYNCED = 97,/* guest agent replies with wrong id
to guest-sync command (DEPRECATED)*/
 VIR_ERR_LIBSSH = 98,/* error in libssh transport driver */
+VIR_ERR_DEVICE_MISSING = 99,/* fail to find the desired device */
 } virErrorNumber;
 
 /**
diff --git a/src/util/virerror.c b/src/util/virerror.c
index 562c3bc61..c000b0043 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -1453,6 +1453,12 @@ virErrorMsg(virErrorNumber error, const char *info)
 else
 errmsg = _("libssh transport error: %s");
 break;
+case VIR_ERR_DEVICE_MISSING:
+if (info == NULL)
+errmsg = _("device not found");
+else
+errmsg = _("device not found: %s");
+break;
 }
 return errmsg;
 }
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 0/5] Introduce VIR_ERR_DEVICE_MISSING

2018-01-22 Thread Chen Hanxiao
Use Introduce VIR_ERR_DEVICE_MISSING for hotplug and detach device
error message.

Chen Hanxiao (5):
  qemu: Add some more details for hotplug errors when device not found
  qemu: Introduce VIR_ERR_DEVICE_MISSING
  qemu: Use VIR_ERR_DEVICE_MISSING for various hotplug messages
  qemu: Use VIR_ERR_DEVICE_MISSING for various DetachDeviceConfig
messages
  news: Add VIR_ERR_DEVICE_MISSING change as improvements

 docs/news.xml   |  5 +
 include/libvirt/virterror.h |  1 +
 src/conf/domain_conf.c  |  8 
 src/libvirt_private.syms|  2 ++
 src/qemu/qemu_driver.c  | 20 +--
 src/qemu/qemu_hotplug.c | 47 -
 src/util/virerror.c |  6 ++
 7 files changed, 57 insertions(+), 32 deletions(-)

-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 3/5] qemu: Use VIR_ERR_DEVICE_MISSING for various hotplug messages

2018-01-22 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Modify OPERATION_FAILED error codes to use DEVICE_MISSING instead.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v3:
  modify virDomainNetFindIdx to use VIR_ERR_DEVICE_MISSING

 src/conf/domain_conf.c  |  8 
 src/qemu/qemu_hotplug.c | 24 
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a1c25060f..b81aeed59 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -16228,7 +16228,7 @@ virDomainNetFindIdx(virDomainDefPtr def, 
virDomainNetDefPtr net)
 
 if (matchidx < 0) {
 if (MACAddrSpecified && PCIAddrSpecified) {
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("no device matching MAC address %s found on "
  "%.4x:%.2x:%.2x.%.1x"),
virMacAddrFormat(>mac, mac),
@@ -16237,18 +16237,18 @@ virDomainNetFindIdx(virDomainDefPtr def, 
virDomainNetDefPtr net)
net->info.addr.pci.slot,
net->info.addr.pci.function);
 } else if (PCIAddrSpecified) {
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("no device found on %.4x:%.2x:%.2x.%.1x"),
net->info.addr.pci.domain,
net->info.addr.pci.bus,
net->info.addr.pci.slot,
net->info.addr.pci.function);
 } else if (MACAddrSpecified) {
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("no device matching MAC address %s found"),
virMacAddrFormat(>mac, mac));
 } else {
-virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+virReportError(VIR_ERR_DEVICE_MISSING, "%s",
_("no matching device found"));
 }
 }
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 1f67ab74a..12198ec8f 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -3514,7 +3514,7 @@ qemuDomainChangeGraphics(virQEMUDriverPtr driver,
 int ret = -1;
 
 if (!olddev) {
-virReportError(VIR_ERR_INTERNAL_ERROR,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("cannot find existing graphics device to modify of "
  "type '%s'"), type);
 goto cleanup;
@@ -4809,7 +4809,7 @@ int qemuDomainDetachControllerDevice(virQEMUDriverPtr 
driver,
 if ((idx = virDomainControllerFind(vm->def,
dev->data.controller->type,
dev->data.controller->idx)) < 0) {
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("controller %s:%d not found"),

virDomainControllerTypeToString(dev->data.controller->type),
dev->data.controller->idx);
@@ -5038,18 +5038,18 @@ int qemuDomainDetachHostDevice(virQEMUDriverPtr driver,
 if (idx < 0) {
 switch (subsys->type) {
 case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("host pci device %.4x:%.2x:%.2x.%.1x not found"),
pcisrc->addr.domain, pcisrc->addr.bus,
pcisrc->addr.slot, pcisrc->addr.function);
 break;
 case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
 if (usbsrc->bus && usbsrc->device) {
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("host usb device %03d.%03d not found"),
usbsrc->bus, usbsrc->device);
 } else {
-virReportError(VIR_ERR_OPERATION_FAILED,
+virReportError(VIR_ERR_DEVICE_MISSING,
_("host usb device vendor=0x%.4x product=0x%.4x 
not found"),
usbsrc->vendor, usbsrc->product);
 }
@@ -5058,13 +5058,13 @@ int qemuDomainDetachHostDevice(virQEMUDriverPtr driver,
 if (scsisrc->protocol ==
 VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
 virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = 
>u.iscsi;
-virReportError(VIR_ERR_OPERATION_FAILED,
+ 

[libvirt] [PATCH v3 5/5] news: Add VIR_ERR_DEVICE_MISSING change as improvements

2018-01-22 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 docs/news.xml | 5 +
 1 file changed, 5 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index b4d980624..5798f42d8 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -37,6 +37,11 @@
 
 
 
+  
+
+  qemu: Use VIR_ERR_DEVICE_MISSING for various hotplug/detach messages.
+
+  
 
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3] libvirtd: clarify the TLS conf default value setting

2018-01-21 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Provide more details related to the requirement that setting one
of the values requires setting all of them.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>

---
v3:
  description updated follow John's comments
v2:
  fix a typo

 daemon/libvirtd.conf | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/daemon/libvirtd.conf b/daemon/libvirtd.conf
index 8e0c0d96d..91b3f47de 100644
--- a/daemon/libvirtd.conf
+++ b/daemon/libvirtd.conf
@@ -182,6 +182,20 @@
 # TLS x509 certificate configuration
 #
 
+# Use of TLS requires that x509 certificates be issued. The default locations
+# for the certificate files is as follows:
+#
+#   /etc/pki/CA/cacert.pem - The CA master certificate
+#   /etc/pki/libvirt/servercert.pem- The server certificate signed with
+#the cacert.pem
+#   /etc/pki/libvirt/private/serverkey.pem - The server private key
+#
+# It is possible to override the default locations by altering the 'key_file',
+# 'cert_file', and 'ca_file' values and uncommenting them below.
+#
+# NB, overriding the default of one location requires uncommenting and
+# possibly additionally overriding the other settings.
+#
 
 # Override the default server key file path
 #
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2] libvirtd: clarify the TLS conf default vaule setting

2018-01-12 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

As the description of daemon/libvirtd.conf, setting
key_file, cert_file or key_file will override the default value.
But if we set any one of them, we need to set all the rest of them.

This patch clarify that description.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v2:
 fix a typo

 daemon/libvirtd.conf | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/daemon/libvirtd.conf b/daemon/libvirtd.conf
index 8e0c0d96d..7040ff26b 100644
--- a/daemon/libvirtd.conf
+++ b/daemon/libvirtd.conf
@@ -183,6 +183,9 @@
 #
 
 
+# NB, if the default value of 'key_file', 'cert_file' or
+# 'ca_file' would be changed,
+# all of them should be changed together.
 # Override the default server key file path
 #
 #key_file = "/etc/pki/libvirt/private/serverkey.pem"
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] libvirtd: clarify the TLS conf default vaule setting

2018-01-12 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

As the description of daemon/libvirtd.conf, setting
key_file, cert_file or key_file will override the default value.
But if we set any one of them, we need to set all the rest of them.

This patch clarify that description.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 daemon/libvirtd.conf | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/daemon/libvirtd.conf b/daemon/libvirtd.conf
index 8e0c0d96d..7040ff26b 100644
--- a/daemon/libvirtd.conf
+++ b/daemon/libvirtd.conf
@@ -183,6 +183,9 @@
 #
 
 
+# NB, if the default value of 'key_file', 'cert_file" or
+# 'ca_file' would be changed,
+# all of them should be changed together.
 # Override the default server key file path
 #
 #key_file = "/etc/pki/libvirt/private/serverkey.pem"
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [resend][PATCH] deamon: use default value if ca_file, cert_file or key_file not set

2018-01-12 Thread Chen Hanxiao


At 2018-01-12 17:44:38, "Jiri Denemark" <jdene...@redhat.com> wrote:
>On Fri, Jan 12, 2018 at 17:09:10 +0800, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> As the description of daemon/libvirtd.conf, setting
>> key_file, cert_file or key_file will override the default value.
>> But if we set any one of them, we need to set all the rest of them.
>
>I think this is a reasonable behavior. If a default value is not usable
>for one of them, the other will likely need to be changed too.
>
>Although ca_file could be separated. In other words, I can imagine
>someone wants to change ca_file but keep default values for
>cert_file/key_file or keep default ca_file and override
>cert_file/key_file. Overriding cert_file or key_file only without also
>changing the other one doesn't make a lot of sense.
>
>Anyway, the patch is incorrect...
>

Thanks for the review.

I'll post another patch for the description of the conf.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [resend][PATCH] deamon: use default value if ca_file, cert_file or key_file not set

2018-01-12 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

As the description of daemon/libvirtd.conf, setting
key_file, cert_file or key_file will override the default value.
But if we set any one of them, we need to set all the rest of them.

This patch set default value to them as daemon/libvirtd.conf
described.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
 daemon/libvirtd.c | 27 ++-
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 6d3b83355..93983f63b 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -493,19 +493,28 @@ daemonSetupNetworking(virNetServerPtr srv,
 config->cert_file ||
 config->key_file) {
 if (!config->ca_file) {
-virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-   _("No CA certificate path set to match 
server key/cert"));
-goto cleanup;
+VIR_WARN("Using default path for ca_file");
+if (VIR_STRDUP(config->ca_file, LIBVIRT_CACERT) < 0) {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+   _("No CA certificate path set to match 
server key/cert"));
+goto cleanup;
+}
 }
 if (!config->cert_file) {
-virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-   _("No server certificate path set to match 
server key"));
-goto cleanup;
+VIR_WARN("Using default path for cert_file");
+if (VIR_STRDUP(config->cert_file, LIBVIRT_SERVERCERT) < 0) 
{
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+   _("No server certificate path set to 
match server key"));
+goto cleanup;
+}
 }
 if (!config->key_file) {
-virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-   _("No server key path set to match server 
cert"));
-goto cleanup;
+VIR_WARN("Using default path for key_file");
+if (VIR_STRDUP(config->key_file, LIBVIRT_SERVERKEY) < 0) {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+   _("No server key path set to match 
server cert"));
+goto cleanup;
+}
 }
 VIR_DEBUG("Using CA='%s' cert='%s' key='%s'",
   config->ca_file, config->cert_file, 
config->key_file);
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v2] domain_conf: skip boot order check of CD-ROM or floppy device when change-media

2018-01-12 Thread Chen Hanxiao

At 2018-01-11 21:36:29, "Ján Tomko" <jto...@redhat.com> wrote:
>On Thu, Jan 11, 2018 at 06:16:37PM +0800, Chen Hanxiao wrote:
>>From: Chen Hanxiao <chenhanx...@gmail.com>
>>
>>If we insert or eject a CD-ROM/floppy device by:
>> 'virsh change-media VM --eject/--insert some.iso --live',
>>and the original CD-ROM device was configed with a boot order,
>>we may get:
>>  unsupported configuration: boot order 2 is already used by another device
>>
>>We just updated 'source file' section rather than hotplug a new device.
>>This check should be skipped in this case.
>>
>
>Attempting to change the boot index on update won't work and should be
>forbidden, as stated in the review for v1:
>https://www.redhat.com/archives/libvir-list/2018-January/msg00178.html
>

My case is not try to change boot index, but to change-media:

1) boot a VM with a CD-ROM, a centos7.1 ISO inside
2) change iso from centos7.3 to centos7.2 by:
#  change-media c72 hda   /media/b/ISO/CentOS-7-x86_64-DVD-1611.iso  --live
Successfully updated media.

This works and we can see cd-rom changed inside guest.

But if we had 

  
  
  
 +
  


  
  
  
  
 +
  


Then change media will fail:
 #  change-media c72 hda   /media/b/ISO/CentOS-7-x86_64-DVD-1511.iso  --live
error: Failed to complete action update on media
error: unsupported configuration: boot order 2 is already used by another device

This is a common case when install OS with multiple DVDs,
or change DVD media when guest is active.

>>Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>>---
>>v2:
[...]
>>+/* Skip check for insert or eject CD-ROM device */
>>+if (disk_device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
>>+disk_device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
>
>Even though cdrom hotplug is not supported by libvirt, assuming that
>we're dealing with an update just because of the device type is wrong:
>https://www.redhat.com/archives/libvir-list/2018-January/msg00180.html
>
>virDomainDefCompatibleDevice should be aware of the operation (attach
>vs. update) and behave accordingly (forbid duplicit bootindexes for
>attach and a bootindex change for update)
>

As we can successfully 'virsh change-media' without  of CD-ROM 
device,
should we forbid this case for a live domain?

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2] domain_conf: skip boot order check of CD-ROM or floppy device when change-media

2018-01-11 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

If we insert or eject a CD-ROM/floppy device by:
 'virsh change-media VM --eject/--insert some.iso --live',
and the original CD-ROM device was configed with a boot order,
we may get:
  unsupported configuration: boot order 2 is already used by another device

We just updated 'source file' section rather than hotplug a new device.
This check should be skipped in this case.

Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v2:
  commit message updated
  remove ATTRIBUTE_UNUSED from @device

 src/conf/domain_conf.c | 19 ++-
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a1c25060f..e006cea0a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -26881,17 +26881,26 @@ virDomainDeviceIsUSB(virDomainDeviceDefPtr dev)
 
 static int
 virDomainDeviceInfoCheckBootIndex(virDomainDefPtr def ATTRIBUTE_UNUSED,
-  virDomainDeviceDefPtr device 
ATTRIBUTE_UNUSED,
+  virDomainDeviceDefPtr device,
   virDomainDeviceInfoPtr info,
   void *opaque)
 {
 virDomainDeviceInfoPtr newinfo = opaque;
+virDomainDiskDefPtr disk = device->data.disk;
+int disk_device = disk->device;
 
 if (info->bootIndex == newinfo->bootIndex) {
-virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-   _("boot order %u is already used by another device"),
-   newinfo->bootIndex);
-return -1;
+/* Skip check for insert or eject CD-ROM device */
+if (disk_device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
+disk_device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
+VIR_DEBUG("Skip boot index check for floppy or CDROM");
+return 0;
+} else {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+   _("boot order %u is already used by another 
device"),
+   newinfo->bootIndex);
+return -1;
+}
 }
 return 0;
 }
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] domain_conf: skip boot order check of CD-ROM or floppy device

2018-01-05 Thread Chen Hanxiao


At 2018-01-05 23:01:50, "John Ferlan" <jfer...@redhat.com> wrote:
>
>
>On 12/25/2017 06:21 AM, Chen Hanxiao wrote:
>> From: Chen Hanxiao <chenhanx...@gmail.com>
>> 
>> If we insert or eject a CD-ROM/floppy device with a boot order,
>> we may get:
>>   unsupported configuration: boot order 2 is already used by another device
>> 
>> This check should be skipped in this case.
>> 
>> Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
>> ---
>>  src/conf/domain_conf.c | 16 
>>  1 file changed, 12 insertions(+), 4 deletions(-)
>> 
>
>Is there a specific reason for your assertion?  Given what scenario?
1) create a VM by virt-manger with a virtio disk and a cd-rom
2) set boot order of them
3) Try to eject a CD-rom device, such as installing an OS with multiple DVDs
i. e: virsh change-media ...
4) got:
  unsupported configuration: boot order 2 is already used by another device.

>
>virDomainDefCompatibleDevice is called from Attach and Update qemu/lxc
>code currently.
>
>I dunno, but if someone is trying to attach or update a floppy/cdrom and
>wanted to use a boot index in use by something else, then I would think
>inhibiting that is a good idea...

We updated the source file of floppy/cdrom, which is not a hotplug operation,
so the boot order check should be skipped.

Regards,
- Chen

>
>> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
>> index 9a62bc472..885ab88d2 100644
>> --- a/src/conf/domain_conf.c
>> +++ b/src/conf/domain_conf.c
>> @@ -26880,11 +26880,19 @@ virDomainDeviceInfoCheckBootIndex(virDomainDefPtr 
>> def ATTRIBUTE_UNUSED,
>>  {
>>  virDomainDeviceInfoPtr newinfo = opaque;
>>  
>> +int disk_device = device->data.disk->device;
>
>Typically the deref would be something like virDomainDiskDefPtr disk =
>device->data.disk; and then "disk->device"
>
>BTW: @device would then no longer be ATTRIBUTE_UNUSED.
>
>
>John
>
>>  if (info->bootIndex == newinfo->bootIndex) {
>> -virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
>> -   _("boot order %u is already used by another device"),
>> -   newinfo->bootIndex);
>> -return -1;
>> +/* Skip check for insert or eject CD-ROM device */
>> +if (disk_device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
>> +disk_device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
>> +VIR_DEBUG("Skip boot index check for floppy or CDROM");
>> +return 0;
>> +} else {
>> +virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
>> +   _("boot order %u is already used by another 
>> device"),
>> +   newinfo->bootIndex);
>> +return -1;
>> +}
>>  }
>>  return 0;
>>  }
>> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 1/2] qemu: Add support for hot unplugging redirdev device which can use the detach-device --live

2018-01-05 Thread Chen Hanxiao

在 2018-01-05 21:16:20,"John Ferlan" <jfer...@redhat.com> 写道:
>
>
>On 01/05/2018 05:40 AM, Ján Tomko wrote:
>> On Fri, Dec 22, 2017 at 04:04:03PM +0800, Chen Hanxiao wrote:
>>> From: Chen Hanxiao <chenhanx...@gmail.com>
>>>
[...]
>>> +    if ((ret = qemuDomainWaitForDeviceRemoval(vm)) == 1)
>>> +    ret = qemuDomainRemoveRedirdevDevice(driver, vm,
>>> tmpRedirdevDef);
>> 
>> If qemuDomainWaitForDeviceRemoval returns 0 (QEMU supports
>> DEVICE_DEL_EVENT and did not unplug the device in 5 seconds),
>> then libvirtd should remove the device asynchronnously, when the device
>> deletion event arrives.
>> 
>> qemuDomainRemoveRedirdevDevice needs to be also called from
>> qemuDomainRemoveDevice for that to happen.
>> 
>> Jan
>> 
>
>oh yeah - right, adding :
>
>+case VIR_DOMAIN_DEVICE_REDIRDEV:
>+ret = qemuDomainRemoveRedirdevDevice(driver, vm,
>dev->data.redirdev);
>+break;
>+
>
>to qemuDomainRemoveDevice
>

Thanks, will be updated soon.

Regards,
- Chen

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v4.1 1/2] qemu: Add support for hot unplugging redirdev device which can use the detach-device --live

2018-01-05 Thread Chen Hanxiao
From: Chen Hanxiao <chenhanx...@gmail.com>

Commit id '162efa1a' added support hotplug a redirdev, but
did not add the hot unplug. This patch will add that support
to allow usage of the detach-device --live on the device.

Reviewed-by: John Ferlan <jfer...@redhat.com>
Signed-off-by: Chen Hanxiao <chenhanx...@gmail.com>
---
v4.1:
  call qemuDomainRemoveRedirdevDevice in qemuDomainRemoveDevice
v4:
  commit message changed and fit code style.
v3:
  use helper qemuDomainDelChardevTLSObjects
  address John's comments.
v2:
  rebase on master

 src/qemu/qemu_driver.c  |  4 ++-
 src/qemu/qemu_hotplug.c | 96 -
 src/qemu/qemu_hotplug.h |  4 +++
 3 files changed, 102 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 4eb8521c4..a203c9297 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -7799,6 +7799,9 @@ qemuDomainDetachDeviceLive(virDomainObjPtr vm,
 case VIR_DOMAIN_DEVICE_INPUT:
 ret = qemuDomainDetachInputDevice(vm, dev->data.input);
 break;
+case VIR_DOMAIN_DEVICE_REDIRDEV:
+ret = qemuDomainDetachRedirdevDevice(driver, vm, dev->data.redirdev);
+break;
 
 case VIR_DOMAIN_DEVICE_FS:
 case VIR_DOMAIN_DEVICE_SOUND:
@@ -7808,7 +7811,6 @@ qemuDomainDetachDeviceLive(virDomainObjPtr vm,
 case VIR_DOMAIN_DEVICE_SMARTCARD:
 case VIR_DOMAIN_DEVICE_MEMBALLOON:
 case VIR_DOMAIN_DEVICE_NVRAM:
-case VIR_DOMAIN_DEVICE_REDIRDEV:
 case VIR_DOMAIN_DEVICE_NONE:
 case VIR_DOMAIN_DEVICE_TPM:
 case VIR_DOMAIN_DEVICE_PANIC:
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 385be80f2..6e07b4cd3 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4399,6 +4399,53 @@ qemuDomainRemoveInputDevice(virDomainObjPtr vm,
 }
 
 
+static int
+qemuDomainRemoveRedirdevDevice(virQEMUDriverPtr driver,
+   virDomainObjPtr vm,
+   virDomainRedirdevDefPtr dev)
+{
+qemuDomainObjPrivatePtr priv = vm->privateData;
+virObjectEventPtr event;
+char *charAlias = NULL;
+ssize_t idx;
+int ret = -1;
+
+VIR_DEBUG("Removing redirdev device %s from domain %p %s",
+  dev->info.alias, vm, vm->def->name);
+
+if (!(charAlias = qemuAliasChardevFromDevAlias(dev->info.alias)))
+goto cleanup;
+
+qemuDomainObjEnterMonitor(driver, vm);
+/* DeviceDel from Detach may remove chardev,
+ * so we cannot rely on return status to delete TLS chardevs.
+ */
+ignore_value(qemuMonitorDetachCharDev(priv->mon, charAlias));
+
+if (qemuDomainObjExitMonitor(driver, vm) < 0)
+goto cleanup;
+
+if (qemuDomainDelChardevTLSObjects(driver, vm, dev->source, charAlias) < 0)
+goto cleanup;
+
+virDomainAuditRedirdev(vm, dev, "detach", true);
+
+event = virDomainEventDeviceRemovedNewFromObj(vm, dev->info.alias);
+qemuDomainEventQueue(driver, event);
+
+if ((idx = virDomainRedirdevDefFind(vm->def, dev)) >= 0)
+virDomainRedirdevDefRemove(vm->def, idx);
+qemuDomainReleaseDeviceAddress(vm, >info, NULL);
+virDomainRedirdevDefFree(dev);
+
+ret = 0;
+
+ cleanup:
+VIR_FREE(charAlias);
+return ret;
+}
+
+
 int
 qemuDomainRemoveDevice(virQEMUDriverPtr driver,
virDomainObjPtr vm,
@@ -4438,6 +4485,11 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
 ret = qemuDomainRemoveInputDevice(vm, dev->data.input);
 break;
 
+case VIR_DOMAIN_DEVICE_REDIRDEV:
+ret = qemuDomainRemoveRedirdevDevice(driver, vm, dev->data.redirdev);
+break;
+
+
 case VIR_DOMAIN_DEVICE_NONE:
 case VIR_DOMAIN_DEVICE_LEASE:
 case VIR_DOMAIN_DEVICE_FS:
@@ -4446,7 +4498,6 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
 case VIR_DOMAIN_DEVICE_WATCHDOG:
 case VIR_DOMAIN_DEVICE_GRAPHICS:
 case VIR_DOMAIN_DEVICE_HUB:
-case VIR_DOMAIN_DEVICE_REDIRDEV:
 case VIR_DOMAIN_DEVICE_SMARTCARD:
 case VIR_DOMAIN_DEVICE_MEMBALLOON:
 case VIR_DOMAIN_DEVICE_NVRAM:
@@ -5139,6 +5190,49 @@ qemuDomainDetachWatchdog(virQEMUDriverPtr driver,
 }
 
 
+int
+qemuDomainDetachRedirdevDevice(virQEMUDriverPtr driver,
+   virDomainObjPtr vm,
+   virDomainRedirdevDefPtr dev)
+{
+int ret = -1;
+qemuDomainObjPrivatePtr priv = vm->privateData;
+virDomainRedirdevDefPtr tmpRedirdevDef;
+ssize_t idx;
+
+if ((idx = virDomainRedirdevDefFind(vm->def, dev)) < 0) {
+virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+   _("no matching redirdev was not found"));
+return -1;
+}
+
+tmpRedirdevDef = vm->def->redirdevs[idx];
+
+if (!tmpRedirdevDef->info.alias) {
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",

  1   2   3   4   5   6   >