[libvirt] [PATCH 1/3 RFC] Add function to get the network interface name of a pci device

Roopa Prabhu roprabhu at cisco.com
Tue Aug 2 12:33:11 UTC 2011




On 8/1/11 6:00 PM, "Stefan Berger" <stefanb at linux.vnet.ibm.com> wrote:

> On 08/01/2011 07:57 PM, Roopa Prabhu wrote:
>> From: Roopa Prabhu<roprabhu at cisco.com>
>> 
>> This function looks at sysfs "net" directory to get the network interface
>> name associated with the pci device
>> 
>> Signed-off-by: Roopa Prabhu<roprabhu at cisco.com>
>> Signed-off-by: Christian Benvenuti<benve at cisco.com>
>> Signed-off-by: David Wang<dwang2 at cisco.com>
>> ---
>>   src/util/pci.c |   42 ++++++++++++++++++++++++++++++++++++++++++
>>   src/util/pci.h |    2 ++
>>   2 files changed, 44 insertions(+), 0 deletions(-)
>> 
>> 
>> diff --git a/src/util/pci.c b/src/util/pci.c
>> index a79c164..d2deeef 100644
>> --- a/src/util/pci.c
>> +++ b/src/util/pci.c
>> @@ -1679,3 +1679,45 @@ int pciDeviceIsAssignable(pciDevice *dev,
>> 
>>       return 1;
>>   }
>> +
>> +/*
>> + * This function returns the network device name
>> + * of a pci device
>> + */
>> +int
>> +pciDeviceGetNetName(char *device_link_sysfs_path, char **netname)
>> +{
>> +        char *pcidev_sysfs_net_path = NULL;
>> +        int ret = -1;
>> +        DIR *dir = NULL;
>> +        struct dirent *entry = NULL;
>> +
>> +        if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path,
>> +            "net") == -1) {
>> +            virReportOOMError();
>> +            return -1;
>> +        }
>> +
>> +        dir = opendir(pcidev_sysfs_net_path);
>> +        if (dir == NULL)
>> +            goto out;
>> +
>> +        while ((entry = readdir(dir))) {
>> +               if (entry->d_name[0] == '.' ||
> The above check makes the following one obsolete. If all entries with
> first letter '.' can be skipped, then you could just keep the first one,
> otherwise I'd also use !strcmp(entry->d_name, ".").

Agree, yes I should use strcmp. Did not think of all cases here because the
net dir normally contains one entry. I will also revisit this check for
cases when there might be more net devices associated with a pci device.

>> +                   !strcmp(entry->d_name, ".."))
>> +                   continue;
>> +
>> +               /* Assume a single directory entry */
>> +               *netname = strdup(entry->d_name);
>> +               ret = 0;
>> +               break;
>> +        }
>> +
>> +        if (dir)
>> +            closedir(dir);
> Check for 'dir != NULL) is not necessary due to goto above.

Yes will fix. Thanks stefan.

>> +
>> +out:
>> +        VIR_FREE(pcidev_sysfs_net_path);
>> +
>> +        return ret;
>> +}
>> diff --git a/src/util/pci.h b/src/util/pci.h
>> index a351baf..fa25472 100644
>> --- a/src/util/pci.h
>> +++ b/src/util/pci.h
>> @@ -74,4 +74,6 @@ int pciDeviceIsAssignable(pciDevice *dev,
>>                             int strict_acs_check);
>>   int pciWaitForDeviceCleanup(pciDevice *dev, const char *matcher);
>> 
>> +int pciDeviceGetNetName(char *device_link_sysfs_path, char **netname);
>> +
>>   #endif /* __VIR_PCI_H__ */
>> 
>     Stefan
> 




More information about the libvir-list mailing list