libxl: a couple of MAC address patches
PATCH 1/1 libxl: a couple of MAC address patches
These two patches should have been part of my "libxl: rationalise libxl_device_* APIs" series but I missed them out. I will include them in future repostings but for now here they are.
PATCH 1/1 libxl: add and use parse_mac helper function
# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1317807819 -3600
# Node ID b85fcaaf73a9cfb5e5f448015c56630aa8daba3d # Parent 6a40e32738e7026769c2932fa53b8aefd34e6a77
libxl: add and use parse_mac helper function
rather than open coding a bunch it a bunch of times.
tools/libxl/libxl_internal.c +17 -0
@@ -277,6 +277,23 @@ int libxl__file_reference_unmap(libxl_fireturn ERROR_FAIL ; } +_hidden int libxl__parse_mac(const char *s, libxl_mac mac) +{ +const char *tok; +char *endptr; +int i; + +for (i =0 , tok = s; *tok && (i <6 ); ++i, tok +=3 ) { + mac[i] =strtol (tok, &endptr,16 ); +if (endptr != (tok +2 ) || (*endptr !='\0' && *endptr !=':' ) ) +return ERROR_INVAL ; + } +if ( i !=6 ) +return ERROR_INVAL ; + +return 0 ; +} +int libxl__fd_set_cloexec (int fd) {int flags =0 ;
tools/libxl/libxl_internal.h +2 -0
@@ -426,6 +426,8 @@ _hidden int libxl__fd_set_cloexec(int fd _hidden int libxl__e820_alloc(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config); +_hidden int libxl__parse_mac(const char *s, libxl_mac mac); +#define STRINGIFY (x) #x#define TOSTRING (x) STRINGIFY(x)
tools/libxl/libxl_utils.c +11 -15
@@ -452,22 +452,19 @@ int libxl_mac_to_device_nic(libxl_ctx *c const char *mac, libxl_device_nic *nic) { libxl_nicinfo *nics; -unsigned int nb, i; +unsigned int nb, rc, i;int found; -uint8_t mac_n[6 ]; +libxl_mac mac_n;uint8_t *a, *b; -const char *tok; -char *endptr; + + rc =libxl__parse_mac (mac, mac_n); +if (rc) +return rc; nics =libxl_list_nics (ctx, domid, &nb);if (!nics)return ERROR_FAIL ; -for (i =0 , tok = mac; *tok && (i <6 ); ++i, tok +=3 ) { - mac_n[i] =strtol (tok, &endptr,16 ); -if (endptr != (tok +2 )) -return ERROR_INVAL ; - }memset (nic,0 ,sizeof (libxl_device_nic)); found =0 ;for (i =0 ; i < nb; ++i) {
@@ -494,9 +491,8 @@ int libxl_devid_to_device_nic(libxl_ctx const char *devid, libxl_device_nic *nic) { libxl__gc gc = LIBXL_INIT_GC(ctx); -char *tok, *val; +char *val;char *dompath, *nic_path_fe, *nic_path_be; -unsigned int i;int rc =ERROR_FAIL ;memset (nic,0 ,sizeof (libxl_device_nic));
@@ -515,10 +511,10 @@ int libxl_devid_to_device_nic(libxl_ctx nic->devid =strtoul (devid,NULL ,10 ); val =libxl__xs_read (&gc,XBT_NULL ,libxl__sprintf (&gc,"%s/mac" , nic_path_fe)); -for (i =0 , tok =strtok (val,":" ); tok && (i <6 ); - ++i, tok =strtok (NULL ,":" )) { - nic->mac[i] =strtoul (tok,NULL ,16 ); - } + rc =libxl__parse_mac (val, nic->mac); +if (rc) + goto out; + nic->script =xs_read (ctx->xsh,XBT_NULL ,libxl__sprintf (&gc,"%s/script" , nic_path_be),NULL ); rc =0 ; out:
Re: [PATCH 1 of 2] libxl: add and use parse_mac helper function
Ian Campbell writes ("[Xen-devel] [PATCH 1 of 2] libxl: add and use parse_mac helper function"):
Quoted 1 line
libxl: add and use
parse_machelper function
Acked and applied both of these, thanks.
Ian.
PATCH 1/1 libxl: fix libxl_mac_to_device_nic
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1317807819 -3600
# Node ID 88486538d739b5fb0aab51526e77315a696c6092 # Parent b85fcaaf73a9cfb5e5f448015c56630aa8daba3d
libxl: fix libxl_mac_to_device_nic
I think I broke this back in 22041:4c9ef5ec9146, using i as both the inner and outer loop iterator.
I've added libxl__compare_macs which helps keep things clean.
tools/libxl/libxl_internal.c +12 -0
@@ -294,6 +294,18 @@ _hidden int libxl__parse_mac(const charreturn 0 ; } +_hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b) +{ +int i; + +for (i =0 ; i<6 ; i++) { +if ((*a)[i] != (*b)[i]) +return (*a)[i] - (*b)[i]; + } + +return 0 ; +} +int libxl__fd_set_cloexec (int fd) {int flags =0 ;
tools/libxl/libxl_internal.h +3 -0
@@ -426,7 +426,10 @@ _hidden int libxl__fd_set_cloexec(int fd _hidden int libxl__e820_alloc(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config); +/* parse the string @s as a sequence of 6 colon separated bytes in to @mac */ _hidden int libxl__parse_mac(const char *s, libxl_mac mac); +/* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */ +_hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b);#define STRINGIFY (x) #x#define TOSTRING (x) STRINGIFY(x)
tools/libxl/libxl_utils.c +5 -9
@@ -453,9 +453,7 @@ int libxl_mac_to_device_nic(libxl_ctx *c {libxl_nicinfo *nics;unsigned int nb, rc, i; -int found;libxl_mac mac_n; -uint8_t *a, *b; rc =libxl__parse_mac (mac, mac_n);if (rc)
@@ -466,17 +464,15 @@ int libxl_mac_to_device_nic(libxl_ctx *creturn ERROR_FAIL ;memset (nic,0 ,sizeof (libxl_device_nic)); - found =0 ; + + rc =ERROR_INVAL ;for (i =0 ; i < nb; ++i) { -for (i =0 , a = nics[i].mac, b = mac_n; - (b < mac_n +6 ) && (*a == *b); ++a, ++b) - ; -if ((b >= mac_n +6 ) && (*a == *b)) { +if (!libxl__compare_macs (&mac_n, &nics[i].mac)) { nic->backend_domid = nics[i].backend_id; nic->devid = nics[i].devid;memcpy (nic->mac, nics[i].mac,sizeof (nic->mac)); nic->script =strdup (nics[i].script); - found =1 ; + rc =0 ;break ; } }
@@ -484,7 +480,7 @@ int libxl_mac_to_device_nic(libxl_ctx *cfor (i=0 ; i<nb; i++)libxl_nicinfo_destroy (&nics[i]);free (nics); -return found; +return rc; } int libxl_devid_to_device_nic(libxl_ctx *ctx, uint32_t domid,