[netcf-devel] [PATCH] Detect and report live interface type.

David Lutterkort lutter at redhat.com
Tue Oct 13 20:49:40 UTC 2009


On Tue, 2009-10-13 at 13:27 -0400, Laine Stump wrote:
> Poke around in sysfs and procfs to determine the type of an interface,
> and set the "type" attribute in the xml appropriately. If no clue is
> found, default to "ethernet", which is the current behavior elsewhere
> in netcf as well.

Cool .. some small nits:

> diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
> index 8e36fab..ead5b9f 100644
> --- a/src/drv_initscripts.c
> +++ b/src/drv_initscripts.c
> @@ -775,6 +775,9 @@ char *drv_xml_state(struct netcf_if *nif) {
>      prop = xmlNewProp(root, BAD_CAST "name", BAD_CAST nif->name);
>      ERR_NOMEM(prop == NULL, ncf);
>  
> +    prop = xmlSetProp(root, BAD_CAST "type", BAD_CAST if_type(ncf, nif->name));
> +    ERR_NOMEM(prop == NULL, ncf);

This swallows errors in if_type; I'd prefer

        const char *type = if_type(ncf, nif->name);
        ERR_BAIL(ncf);
        prop = xmlSetProp(root, BAD_CAST "type", BAD_CAST type);
        ERR_NOMEM(prop == NULL, ncf);

> diff --git a/src/dutil.c b/src/dutil.c
> index 817ec6d..9c8e575 100644
> --- a/src/dutil.c
> +++ b/src/dutil.c
> @@ -499,6 +499,39 @@ int if_ipv4_prefix(struct netcf *ncf, const char *intf) {
>      return prefix;
>  }
>  
> +const char *if_type(struct netcf *ncf, const char *intf) {
> +    char *path;
> +    struct stat stats;
> +    const char *ret = NULL;
> +
> +    xasprintf(&path, "/proc/net/vlan/%s", intf);
> +    ERR_NOMEM(path == NULL, ncf);
> +    if ((stat (path, &stats) == 0) && S_ISREG (stats.st_mode)) {
> +        ret = "vlan";
> +    }
> +    FREE(path);
> +
> +    if (ret == NULL) {
> +        xasprintf(&path, "/sys/class/net/%s/bridge", intf);
> +        ERR_NOMEM(path == NULL, ncf);
> +        if (stat (path, &stats) == 0 && S_ISDIR (stats.st_mode))
> +            return "bridge";
> +        FREE(path);
> +    }
> +    if (ret == NULL) {
> +        xasprintf(&path, "/sys/class/net/%s/bonding", intf);
> +        ERR_NOMEM(path == NULL, ncf);
> +        if (stat (path, &stats) == 0 && S_ISDIR (stats.st_mode))
> +            return "bond";
> +        FREE(path);
> +    }
> +
> +error:        
> +    if (ret == NULL)
> +        ret = "ethernet";
> +    return ret;
> +}

I'd prefer if the error return was NULL, i.e. the error label should go
two lines down.

David




More information about the netcf-devel mailing list