[libvirt] [PATCH v3] virsh: lookup interface by name or mac other than one by one

Change virMacAddrParse() to accept NULL addr for mac address testing purpose. --- src/util/virmacaddr.c | 3 ++- tools/virsh-interface.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c index c4ca0a8..8e41844 100644 --- a/src/util/virmacaddr.c +++ b/src/util/virmacaddr.c @@ -166,7 +166,8 @@ virMacAddrParse(const char* str, virMacAddrPtr addr) (0xFF < result)) break; - addr->addr[i] = (unsigned char) result; + if (addr) + addr->addr[i] = (unsigned char) result; if ((i == 5) && (*end_ptr == '\0')) return 0; diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c index f75c572..74b4ed7 100644 --- a/tools/virsh-interface.c +++ b/tools/virsh-interface.c @@ -35,6 +35,7 @@ #include "virbuffer.h" #include "viralloc.h" #include "virfile.h" +#include "virmacaddr.h" #include "virutil.h" #include "virxml.h" #include "virstring.h" @@ -46,6 +47,7 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, { virInterfacePtr iface = NULL; const char *n = NULL; + bool is_mac = false; virCheckFlags(VSH_BYNAME | VSH_BYMAC, NULL); if (!optname) @@ -62,14 +64,17 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, if (name) *name = n; + if (virMacAddrParse(n, NULL) == 0) + is_mac = true; + /* try it by NAME */ - if (flags & VSH_BYNAME) { + if (!is_mac && (flags & VSH_BYNAME)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface NAME\n", cmd->def->name, optname); iface = virInterfaceLookupByName(ctl->conn, n); - } + /* try it by MAC */ - if (!iface && (flags & VSH_BYMAC)) { + } else if (is_mac && (flags & VSH_BYMAC)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface MAC\n", cmd->def->name, optname); iface = virInterfaceLookupByMACString(ctl->conn, n); -- 1.8.1.4

On Wed, May 15, 2013 at 09:50:48PM +0800, Guannan Ren wrote:
Change virMacAddrParse() to accept NULL addr for mac address testing purpose. --- src/util/virmacaddr.c | 3 ++- tools/virsh-interface.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c index c4ca0a8..8e41844 100644 --- a/src/util/virmacaddr.c +++ b/src/util/virmacaddr.c @@ -166,7 +166,8 @@ virMacAddrParse(const char* str, virMacAddrPtr addr) (0xFF < result)) break;
- addr->addr[i] = (unsigned char) result; + if (addr) + addr->addr[i] = (unsigned char) result;
if ((i == 5) && (*end_ptr == '\0')) return 0;
NACK to this change.
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c index f75c572..74b4ed7 100644 --- a/tools/virsh-interface.c +++ b/tools/virsh-interface.c @@ -35,6 +35,7 @@ #include "virbuffer.h" #include "viralloc.h" #include "virfile.h" +#include "virmacaddr.h" #include "virutil.h" #include "virxml.h" #include "virstring.h" @@ -46,6 +47,7 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, { virInterfacePtr iface = NULL; const char *n = NULL; + bool is_mac = false; virCheckFlags(VSH_BYNAME | VSH_BYMAC, NULL);
if (!optname) @@ -62,14 +64,17 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, if (name) *name = n;
+ if (virMacAddrParse(n, NULL) == 0) + is_mac = true;
Just add a valid parameter here. Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

Use virMacAddrParse() to distinguish interface name from interface mac address. --- tools/virsh-interface.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c index f75c572..1c2e40b 100644 --- a/tools/virsh-interface.c +++ b/tools/virsh-interface.c @@ -35,6 +35,7 @@ #include "virbuffer.h" #include "viralloc.h" #include "virfile.h" +#include "virmacaddr.h" #include "virutil.h" #include "virxml.h" #include "virstring.h" @@ -46,6 +47,8 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, { virInterfacePtr iface = NULL; const char *n = NULL; + bool is_mac = false; + virMacAddr dummy; virCheckFlags(VSH_BYNAME | VSH_BYMAC, NULL); if (!optname) @@ -62,14 +65,17 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, if (name) *name = n; + if (virMacAddrParse(n, &dummy) == 0) + is_mac = true; + /* try it by NAME */ - if (flags & VSH_BYNAME) { + if (!is_mac && (flags & VSH_BYNAME)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface NAME\n", cmd->def->name, optname); iface = virInterfaceLookupByName(ctl->conn, n); - } + /* try it by MAC */ - if (!iface && (flags & VSH_BYMAC)) { + } else if (is_mac && (flags & VSH_BYMAC)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface MAC\n", cmd->def->name, optname); iface = virInterfaceLookupByMACString(ctl->conn, n); -- 1.8.1.4

On 05/15/2013 08:22 AM, Guannan Ren wrote:
Use virMacAddrParse() to distinguish interface name from interface mac address. --- tools/virsh-interface.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
ACK. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 05/16/2013 12:52 AM, Eric Blake wrote:
On 05/15/2013 08:22 AM, Guannan Ren wrote:
Use virMacAddrParse() to distinguish interface name from interface mac address. --- tools/virsh-interface.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) ACK.
Thanks, pushed now.
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Guannan Ren