cutils: factor out pci_host_device_address_parse()
PATCH v2 1/1 cutils: factor out pci_host_device_address_parse()
This will allow the function to be used outside of QOM properties.
Add tests and replace strtoul usage for qemu_strtoul while touching it.
---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h | 12 ++++++++++
hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)
include/hw/pci/pci.h +12 -0
@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +#endif
hw/core/qdev-properties.c +3 -58
82 lines not on this page · raw message
tests/test-cutils.c +46 -0
63 lines not on this page · raw message
util/cutils.c +59 -0
68 lines not on this page · raw message
Signature · 1 line
2.11.0.295.gd7dffce1c.dirty
Quoted 2 lines · Marc-André Lureau
This will allow the function to be used outside of QOM properties.
Hi Mark-Andre,
I am not sure cutil.h is the right header for the function, because is too PCI related for a utility library.
Also, maybe we can post the patch together with the code that uses the exposed function.
And thanks for the unit-tests, much appreciated!
Marcel
Quoted 268 lines
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.Signed-off-by Marc-André Lureau <[email protected]>---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +12
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +#endif hw/core/qdev-properties.c +3
-58@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,visit_type_str (v, name, &p, errp); } -/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp) {@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,Property *prop = opaque;PCIHostDeviceAddress *addr =qdev_get_prop_ptr (dev, prop);Error *local_err =NULL ; -char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize (dev, name, errp);@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,return ; } - p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo qdev_prop_pci_host_devaddr = {tests/test-cutils.c +46
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + } +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +59
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr) +{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +}
Hi
----- Original Message -----
Quoted 9 lines · Marc-André Lureau
Quoted 2 lines
This will allow the function to be used outside of QOM properties.
Hi Mark-Andre,
I am not sure
cutil.his the right header for the function, because is too PCI
related for a utility library.
It's in the pci.h header, implementation and test is put in {test-,}cutils.c
I can try to put it in pci/pci.c, but it will be harder to link to for testing (note: I haven't tried). I could also put it in a seperate pci-parse.c for example.
Quoted 2 lines
Also, maybe we can post the patch together with the code that uses the exposed function.
I dropped the patch using it for now, but I thought it would still be nice to have the test, doc and strtoul change.
Quoted 2 lines
And thanks for the unit-tests, much appreciated!
thanks
Quoted 280 lines
Marcel
Quoted 276 lines
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.Signed-off-by Marc-André Lureau <[email protected]>---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +12
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +#endif
hw/core/qdev-properties.c +0
-4@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,visit_type_str (v, name, &p, errp); } -/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,void *opaque, Error **errp)
{hw/core/qdev-properties.c +1
-5@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, Property *prop = opaque;PCIHostDeviceAddress *addr =qdev_get_prop_ptr (dev, prop);Error *local_err =NULL ; -char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize(dev, name, errp);hw/core/qdev-properties.c +2
-49@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, return; } - p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo
qdev_prop_pci_host_devaddr= {tests/test-cutils.c +46
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + } +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +3
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse(const char *str, PCIHostDeviceAddress*addr)
util/cutils.c +56
-0+{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +}
Quoted 7 lines · Marc-André Lureau
Hi
----- Original Message -----
Quoted 4 lines · Marc-André Lureau
Quoted 2 lines
This will allow the function to be used outside of QOM properties.
Quoted 8 lines
Quoted 5 lines
Hi Mark-Andre,
I am not sure
cutil.his the right header for the function, because is too PCI
related for a utility library.It's in the
pci.hheader, implementation and test is put in {test-,}cutils.c
sorry about the miss-match, I war referring to the implementation.
Quoted 1 line
I can try to put it in
pci/pci.c, but it will be harder to link to for testing (note: I haven't tried). I could also put it in a seperatepci-parse.cfor example.
no need for a new file, but hw/pci/pci_host.c should be a good place.
regarding the tests, an new pci test file would be welcomed.
Quoted 7 lines
Quoted 4 lines
Also, maybe we can post the patch together with the code that uses the exposed function.
I dropped the patch using it for now, but I thought it would still be nice to have the test, doc and strtoul change.
I agree if Michael has nothing against this.
Thanks,
Marcel
Quoted 285 lines
Quoted 2 lines
And thanks for the unit-tests, much appreciated!
thanks
Quoted 280 lines
Marcel
Quoted 276 lines
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.Signed-off-by Marc-André Lureau <[email protected]>---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +12
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +#endif
hw/core/qdev-properties.c +0
-4@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,visit_type_str (v, name, &p, errp); } -/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,void *opaque, Error **errp)
{hw/core/qdev-properties.c +1
-5@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, Property *prop = opaque;PCIHostDeviceAddress *addr =qdev_get_prop_ptr (dev, prop);Error *local_err =NULL ; -char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize(dev, name, errp);hw/core/qdev-properties.c +2
-49@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, return; } - p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo
qdev_prop_pci_host_devaddr= {tests/test-cutils.c +46
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + } +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +3
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse(const char *str, PCIHostDeviceAddress*addr)
util/cutils.c +56
-0+{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +}
Hi
----- Original Message -----
Quoted 18 lines · Marc-André Lureau
Quoted 7 lines
Hi
----- Original Message -----
Quoted 4 lines · Marc-André Lureau
Quoted 2 lines
This will allow the function to be used outside of QOM properties.
Quoted 9 lines
Quoted 5 lines
Hi Mark-Andre,
I am not sure
cutil.his the right header for the function, because is too PCI
related for a utility library.It's in the
pci.hheader, implementation and test is put in {test-,}cutils.c
Quoted 7 lines
sorry about the miss-match, I war referring to the implementation.
Quoted 3 lines
I can try to put it in
pci/pci.c, but it will be harder to link to for testing (note: I haven't tried). I could also put it in a seperatepci-parse.cfor example.no need for a new file, but
hw/pci/pci_host.cshould be a good place.
It pulls in pci/pci.c for pci_find_device() and friends, plus type registration etc.
I'll move it to a standalone pci/pci-util.c
Quoted 307 lines
regarding the tests, an new pci test file would be welcomed.
Quoted 8 lines
Quoted 4 lines
Also, maybe we can post the patch together with the code that uses the exposed function.
I dropped the patch using it for now, but I thought it would still be nice to have the test, doc and strtoul change.
I agree if Michael has nothing against this.
Thanks,
MarcelQuoted 288 lines
Quoted 2 lines
And thanks for the unit-tests, much appreciated!
thanks
Quoted 283 lines
Marcel
Quoted 279 lines
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.Signed-off-by Marc-André Lureau <[email protected]>---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +12
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +#endif
hw/core/qdev-properties.c +0
-0@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj,Visitor
*v, const char *name,visit_type_str(v, name, &p, errp);}
hw/core/qdev-properties.c +0
-4-/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp) {@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj,Visitor
*v, const char *name,Property *prop = opaque; PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop); Error *local_err = NULL;hw/core/qdev-properties.c +1
-5-char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize (dev, name, errp);@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj,Visitor
*v, const char *name,return; }hw/core/qdev-properties.c +2
-49- p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo qdev_prop_pci_host_devaddr = {tests/test-cutils.c +46
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + } +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +3
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse(const char *str, PCIHostDeviceAddress*addr)
util/cutils.c +56
-0+{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +}
Quoted 5 lines · Marc-André Lureau
This will allow the function to be used outside of QOM properties.
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.Signed-off-by Marc-André Lureau <[email protected]>
Sorry, I don't want to start maintaining it. So pls do not move it to hw/pci. Find out who wants to maintain this and add it there :)
Quoted 21 lines
---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +3
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); +/** +* pci_host_device_address_parse: +* @str: string to parse
include/hw/pci/pci.h extern const VMStateDescription vmstate_pci_device; · line 830
Quoted 10 lines
include/hw/pci/pci.h +9
-0+* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +#endif
Used to be a static function, now it's an interface, I'm not sure it's a win.
Quoted 138 lines
hw/core/qdev-properties.c +3
-58@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,visit_type_str (v, name, &p, errp); } -/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp) {@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,Property *prop = opaque;PCIHostDeviceAddress *addr =qdev_get_prop_ptr (dev, prop);Error *local_err =NULL ; -char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize (dev, name, errp);@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,return ; } - p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo qdev_prop_pci_host_devaddr = {tests/test-cutils.c +34
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" };
tests/test-cutils.c static void test_qemu_strtosz_suffix_unit(void) · line 1483
Quoted 10 lines
tests/test-cutils.c +7
-0+ +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + } +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );
Pls find a way to test is through a property.
Quoted 87 lines
tests/test-cutils.c +5
-0@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +59
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr) +{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +}Signature · 1 line
2.11.0.295.gd7dffce1c.dirty
Hi
----- Original Message -----
Quoted 9 lines · Marc-André Lureau
Quoted 5 lines
This will allow the function to be used outside of QOM properties.
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.Signed-off-by Marc-André Lureau <[email protected]>Sorry, I don't want to start maintaining it. So pls do not move it to hw/pci. Find out who wants to maintain this and add it there :)
Well, if you are PCI maintainer, I guess it falls into you anyway ;)
Quoted 21 lines
Quoted 21 lines
---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +0
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector);
Quoted 16 lines
Quoted 13 lines
include/hw/pci/pci.h +12
-0+/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +Quoted 1 line
#endif
Used to be a static function, now it's an interface, I'm not sure it's a win.
It's an internal interface, with a single user and a test. Not a big deal.
Quoted 148 lines
Quoted 148 lines
hw/core/qdev-properties.c +0
-4@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,visit_type_str (v, name, &p, errp); } -/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,void *opaque, Error **errp)
{hw/core/qdev-properties.c +1
-5@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, Property *prop = opaque;PCIHostDeviceAddress *addr =qdev_get_prop_ptr (dev, prop);Error *local_err =NULL ; -char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize(dev, name, errp);hw/core/qdev-properties.c +2
-49@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, return; } - p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo
qdev_prop_pci_host_devaddr= {tests/test-cutils.c +39
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + }
Quoted 8 lines
tests/test-cutils.c static void test_qemu_strtosz_suffix_unit(void) · line 1483
Quoted 5 lines
tests/test-cutils.c +2
-0+} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );Pls find a way to test is through a property.
Probably doable, though it's less of a unit test then. I'll look into doing it.
Quoted 90 lines
Quoted 89 lines
tests/test-cutils.c +5
-0@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +3
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse(const char *str, PCIHostDeviceAddress*addr)
util/cutils.c +56
-1+{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +} --2.11.0.295.gd7dffce1c.dirty
Quoted 8 lines · Marc-André Lureau
Hi
----- Original Message -----
Quoted 5 lines · Marc-André Lureau
Quoted 4 lines
This will allow the function to be used outside of QOM properties.
Add tests and replace strtoul usage for
qemu_strtoulwhile touching it.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Marc-André Lureau <[email protected]>Sorry, I don't want to start maintaining it. So pls do not move it to hw/pci. Find out who wants to maintain this and add it there :)
Well, if you are PCI maintainer, I guess it falls into you anyway ;)
I don't get to maintain all pci devices. This parsing code is part of QMP/HMP interfaces with pass-through devices. Not part of pci emulation which is what I maintain.
Quoted 289 lines
Quoted 21 lines
Quoted 21 lines
---
v2:
- fixed incompatible type warning/error
include/hw/pci/pci.h| 12 ++++++++++hw/core/qdev-properties.c | 61 +++-------------------------------------------- tests/test-cutils.c | 46 +++++++++++++++++++++++++++++++++++ util/cutils.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 58 deletions(-)include/hw/pci/pci.h +0
-0@@ -815,4 +815,16 @@ extern const VMStateDescription vmstate_pci_device;MSIMessage pci_get_msi_message (PCIDevice *dev,int vector);Quoted 16 lines
Quoted 13 lines
include/hw/pci/pci.h +12
-0+/** +* pci_host_device_address_parse: +* @str: string to parse +* @addr: destination structure or NULL +* +* Parse [<domain>:]<bus>:<slot>.<func> +* if <domain> is not supplied, it's assumed to be 0. +* +* Returns: -1 on failure, 0 on success. +*/ +int pci_host_device_address_parse (const char *str,PCIHostDeviceAddress *addr); +Quoted 1 line
#endif
Used to be a static function, now it's an interface, I'm not sure it's a win.
It's an internal interface, with a single user and a test. Not a big deal.
Quoted 148 lines
Quoted 148 lines
hw/core/qdev-properties.c +0
-4@@ -722,10 +722,6 @@ static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,visit_type_str (v, name, &p, errp); } -/* -* Parse [<domain>:]<bus>:<slot>.<func> -* if <domain> is not supplied, it's assumed to be 0. -*/ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,void *opaque, Error **errp)
{hw/core/qdev-properties.c +1
-5@@ -733,11 +729,7 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, Property *prop = opaque;PCIHostDeviceAddress *addr =qdev_get_prop_ptr (dev, prop);Error *local_err =NULL ; -char *str, *p; -char *e; -unsigned long val; -unsigned long dom =0 , bus =0 ; -unsigned int slot =0 , func =0 ; +char *str;if (dev->realized) {qdev_prop_set_after_realize(dev, name, errp);hw/core/qdev-properties.c +2
-49@@ -750,58 +742,11 @@ static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, return; } - p = str; - val =strtoul (p, &e,16 ); -if (e == p || *e !=':' ) { - goto inval; - } - bus = val; - - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } -if (*e ==':' ) { - dom = bus; - bus = val; - p = e +1 ; - val =strtoul (p, &e,16 ); -if (e == p) { - goto inval; - } - } - slot = val; - -if (*e !='.' ) { - goto inval; +if (!pci_host_device_address_parse (str, addr)) { +error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); } - p = e +1 ; - val =strtoul (p, &e,10 ); -if (e == p) { - goto inval; - } - func = val; - -if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { - goto inval; - } - -if (*e) { - goto inval; - } - - addr->domain = dom; - addr->bus = bus; - addr->slot = slot; - addr->function = func;g_free (str); -return ; - -inval: -error_set_from_qdev_prop_error (errp,EINVAL , dev, prop, str); -g_free (str); }PropertyInfo
qdev_prop_pci_host_devaddr= {tests/test-cutils.c +39
-0@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "hw/pci/pci.h" static void test_parse_uint_null (void ) {@@ -1437,6 +1438,46 @@ static void test_qemu_strtosz_suffix_unit(void) g_assert_cmpint(res, ==, 12345000); } +static void test_pci_parse_valid (void ) +{ +PCIHostDeviceAddress addr = {0xff ,0xff ,0xff ,0xff }; +int i, res; +const struct { +const char *str; +PCIHostDeviceAddress addr; + } tests[] = { + {"1:2.3" , {0 ,1 ,2 ,3 } }, + {"4:1:2.3" , {4 ,1 ,2 ,3 } }, + {"a:a:a.7" , {10 ,10 ,10 ,7 } }, + }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i].str, &addr); + g_assert_cmpint(res, ==, 0); + + g_assert_cmpint(addr.domain, ==, tests[i].addr.domain); + g_assert_cmpint(addr.bus, ==, tests[i].addr.bus); + g_assert_cmpint(addr.slot, ==, tests[i].addr.slot); + g_assert_cmpint(addr.function, ==, tests[i].addr.function); + } +} + +static void test_pci_parse_invalid (void ) +{ +int i, res; +const char *tests[] = { +"" ,"foo" ,"1:2.3 foo" , +"a:a:a.a" , +"1" ,"1:" ,"1:2" ,"1:2:" ,"1:2:3" ,":1:2:3." , +"-1:1:2.3" ,"-1:2.3" ,"1:-1.3" ,"1:2.-3" , +"0x10000:1:1.1" ,"0x100:1.1" ,"1:0x20.1" ,"1:1.8" }; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + res =pci_host_device_address_parse (tests[i],NULL ); + g_assert_cmpint(res, ==, -1); + }Quoted 8 lines
tests/test-cutils.c static void test_qemu_strtosz_suffix_unit(void) · line 1483
Quoted 5 lines
tests/test-cutils.c +2
-0+} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );Pls find a way to test is through a property.
Probably doable, though it's less of a unit test then. I'll look into doing it.
Quoted 90 lines
Quoted 89 lines
tests/test-cutils.c +5
-0@@ -1598,5 +1639,10 @@ int main(int argc, char **argv)g_test_add_func ("/cutils/strtosz/suffix-unit" , test_qemu_strtosz_suffix_unit); +g_test_add_func ("/cutils/pci-parse/valid" , + test_pci_parse_valid); +g_test_add_func ("/cutils/pci-parse/invalid" , + test_pci_parse_invalid); +return g_test_run (); }util/cutils.c +3
-0@@ -29,6 +29,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "net/net.h" +#include "hw/pci/pci.h" #include "qemu/cutils.h" void strpadcpy (char *buf,int buf_size,const char *str,char pad)@@ -594,3 +595,61 @@ const char *qemu_ether_ntoa(const MACAddr *mac)return ret; } + +int pci_host_device_address_parse(const char *str, PCIHostDeviceAddress*addr)
util/cutils.c +56
-1+{ +const char *p = str; +const char *e; +unsigned long val; +unsigned int dom =0 , bus =0 ; +unsigned int slot =0 , func =0 ; + +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p || *e !=':' ) { +return -1 ; + } + bus = val; + + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } +if (*e ==':' ) { + dom = bus; + bus = val; + p = e +1 ; +if (qemu_strtoul (p, &e,16 , &val) <0 + || e == p) { +return -1 ; + } + } + slot = val; + +if (*e !='.' ) { +return -1 ; + } + p = e +1 ; +if (qemu_strtoul (p, &e,10 , &val) <0 + || e == p) { +return -1 ; + } + func = val; + +if (dom >0xffff || bus >0xff || slot >0x1f || func >7 ) { +return -1 ; + } + +if (*e) { +return -1 ; + } + +if (addr) { + addr->domain = dom; + addr->bus = bus; + addr->slot = slot; + addr->function = func; + } + +return 0 ; +} --2.11.0.295.gd7dffce1c.dirty