From ea9f00f7289a3bef975442165d410ddf299626a3 Mon Sep 17 00:00:00 2001 From: Guduri Prathyusha Date: Wed, 22 Mar 2017 19:41:26 +0530 Subject: [PATCH] usertools: refactor NIC and crypto binding details get_nic_details() and get_crypto_details() shares a lot of common code. Created a new unified get_device_details() function get the device details. Signed-off-by: Guduri Prathyusha Signed-off-by: Jerin Jacob --- usertools/dpdk-devbind.py | 103 +++++++++++--------------------------- 1 file changed, 30 insertions(+), 73 deletions(-) diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py index 1b9c651553..0444b546ff 100755 --- a/usertools/dpdk-devbind.py +++ b/usertools/dpdk-devbind.py @@ -234,23 +234,24 @@ def get_pci_device_details(dev_id): return device +def clear_data(): + '''This function clears any old data''' + devices = {} -def get_nic_details(): +def get_device_details(devices_type): '''This function populates the "devices" dictionary. The keys used are the pci addresses (domain:bus:slot.func). The values are themselves dictionaries - one for each NIC.''' global devices global dpdk_drivers - # clear any old data - devices = {} # first loop through and read details for all devices # request machine readable format, with numeric IDs dev = {} dev_lines = check_output(["lspci", "-Dvmmn"]).splitlines() for dev_line in dev_lines: if len(dev_line) == 0: - if dev["Class"][0:2] == NETWORK_BASE_CLASS: + if dev["Class"][0:2] == devices_type: # convert device and vendor ids to numbers, then add to global dev["Vendor"] = int(dev["Vendor"], 16) dev["Device"] = int(dev["Device"], 16) @@ -260,81 +261,35 @@ def get_nic_details(): name, value = dev_line.decode().split("\t", 1) dev[name.rstrip(":")] = value - # check what is the interface if any for an ssh connection if - # any to this host, so we can mark it later. - ssh_if = [] - route = check_output(["ip", "-o", "route"]) - # filter out all lines for 169.254 routes - route = "\n".join(filter(lambda ln: not ln.startswith("169.254"), + if devices_type == NETWORK_BASE_CLASS: + # check what is the interface if any for an ssh connection if + # any to this host, so we can mark it later. + ssh_if = [] + route = check_output(["ip", "-o", "route"]) + # filter out all lines for 169.254 routes + route = "\n".join(filter(lambda ln: not ln.startswith("169.254"), route.decode().splitlines())) - rt_info = route.split() - for i in range(len(rt_info) - 1): - if rt_info[i] == "dev": - ssh_if.append(rt_info[i+1]) - - # based on the basic info, get extended text details - for d in devices.keys(): - # get additional info and add it to existing data - devices[d] = devices[d].copy() - devices[d].update(get_pci_device_details(d).items()) - - for _if in ssh_if: - if _if in devices[d]["Interface"].split(","): - devices[d]["Ssh_if"] = True - devices[d]["Active"] = "*Active*" - break - - # add igb_uio to list of supporting modules if needed - if "Module_str" in devices[d]: - for driver in dpdk_drivers: - if driver not in devices[d]["Module_str"]: - devices[d]["Module_str"] = \ - devices[d]["Module_str"] + ",%s" % driver - else: - devices[d]["Module_str"] = ",".join(dpdk_drivers) - - # make sure the driver and module strings do not have any duplicates - if has_driver(d): - modules = devices[d]["Module_str"].split(",") - if devices[d]["Driver_str"] in modules: - modules.remove(devices[d]["Driver_str"]) - devices[d]["Module_str"] = ",".join(modules) - - -def get_crypto_details(): - '''This function populates the "devices" dictionary. The keys used are - the pci addresses (domain:bus:slot.func). The values are themselves - dictionaries - one for each NIC.''' - global devices - global dpdk_drivers - - # clear any old data - # devices = {} - # first loop through and read details for all devices - # request machine readable format, with numeric IDs - dev = {} - dev_lines = check_output(["lspci", "-Dvmmn"]).splitlines() - for dev_line in dev_lines: - if len(dev_line) == 0: - if dev["Class"][0:2] == CRYPTO_BASE_CLASS: - # convert device and vendor ids to numbers, then add to global - dev["Vendor"] = int(dev["Vendor"], 16) - dev["Device"] = int(dev["Device"], 16) - # use dict to make copy of dev - devices[dev["Slot"]] = dict(dev) - else: - name, value = dev_line.decode().split("\t", 1) - dev[name.rstrip(":")] = value + rt_info = route.split() + for i in range(len(rt_info) - 1): + if rt_info[i] == "dev": + ssh_if.append(rt_info[i+1]) # based on the basic info, get extended text details for d in devices.keys(): - if devices[d]["Class"][0:2] != CRYPTO_BASE_CLASS: + if devices[d]["Class"][0:2] != devices_type: continue # get additional info and add it to existing data devices[d] = devices[d].copy() devices[d].update(get_pci_device_details(d).items()) + if devices_type == NETWORK_BASE_CLASS: + for _if in ssh_if: + if _if in devices[d]["Interface"].split(","): + devices[d]["Ssh_if"] = True + devices[d]["Active"] = "*Active*" + break + # add igb_uio to list of supporting modules if needed if "Module_str" in devices[d]: for driver in dpdk_drivers: @@ -638,8 +593,9 @@ def do_arg_actions(): bind_all(args, b_flag, force_flag) if status_flag: if b_flag is not None: - get_nic_details() # refresh if we have changed anything - get_crypto_details() # refresh if we have changed anything + clear_data() + get_device_details(NETWORK_BASE_CLASS) # refresh if we have changed anything + get_device_details(CRYPTO_BASE_CLASS) # refresh if we have changed anything show_status() @@ -647,8 +603,9 @@ def main(): '''program main function''' parse_args() check_modules() - get_nic_details() - get_crypto_details() + clear_data() + get_device_details(NETWORK_BASE_CLASS) + get_device_details(CRYPTO_BASE_CLASS) do_arg_actions() if __name__ == "__main__": -- 2.20.1