diff --git a/src/lib/hardware/README.md b/src/lib/hardware/README.md index 4e39388b24..537b0b7c35 100644 --- a/src/lib/hardware/README.md +++ b/src/lib/hardware/README.md @@ -23,6 +23,19 @@ An array of supported hardware devices. Must be populated by calling `pci.scan_devices`. Each entry is a table as returned by `pci.device_info`. +— Function **pci.canonical** *pciaddress* + +Returns the canonical representation of a PCI address. The canonical +representation is preferred internally in Snabb Switch and for +presenting to users. It shortens addresses with leading zeros like +this: `0000:01:00.0` becomes `01:00.0`. + +— Function **pci.qualified** *pciaddress* + +Returns the fully qualified representation of a PCI address. Fully +qualified addresses have the form `0000:01:00.0` and so this function +undoes any abbreviation in the canonical representation. + — Function **pci.scan_devices** Scans for available PCI devices and populates the `pci.devices` table. diff --git a/src/lib/hardware/README.md.src b/src/lib/hardware/README.md.src index 4e39388b24..537b0b7c35 100644 --- a/src/lib/hardware/README.md.src +++ b/src/lib/hardware/README.md.src @@ -23,6 +23,19 @@ An array of supported hardware devices. Must be populated by calling `pci.scan_devices`. Each entry is a table as returned by `pci.device_info`. +— Function **pci.canonical** *pciaddress* + +Returns the canonical representation of a PCI address. The canonical +representation is preferred internally in Snabb Switch and for +presenting to users. It shortens addresses with leading zeros like +this: `0000:01:00.0` becomes `01:00.0`. + +— Function **pci.qualified** *pciaddress* + +Returns the fully qualified representation of a PCI address. Fully +qualified addresses have the form `0000:01:00.0` and so this function +undoes any abbreviation in the canonical representation. + — Function **pci.scan_devices** Scans for available PCI devices and populates the `pci.devices` table. diff --git a/src/lib/hardware/pci.lua b/src/lib/hardware/pci.lua index 2b9100bf28..00d0fd1d35 100644 --- a/src/lib/hardware/pci.lua +++ b/src/lib/hardware/pci.lua @@ -34,7 +34,7 @@ end function device_info (pciaddress) local info = {} local p = path(pciaddress) - info.pciaddress = pciaddress + info.pciaddress = canonical(pciaddress) info.vendor = lib.firstline(p.."/vendor") info.device = lib.firstline(p.."/device") info.model = which_model(info.vendor, info.device) @@ -50,7 +50,7 @@ function device_info (pciaddress) end --- Return the path to the sysfs directory for `pcidev`. -function path(pcidev) return "/sys/bus/pci/devices/"..pcidev end +function path(pcidev) return "/sys/bus/pci/devices/"..qualified(pcidev) end model = { ["82599_SFP"] = 'Intel 82599 SFP', @@ -97,10 +97,10 @@ end --- The corresponding network interface (e.g. `eth0`) will disappear. function unbind_device_from_linux (pciaddress) root_check() - local p = path(pciaddress).."/driver/unbind" - if lib.can_write(p) then - lib.writefile(path(pciaddress).."/driver/unbind", pciaddress) - end + local p = path(pciaddress).."/driver/unbind" + if lib.can_write(p) then + lib.writefile(path(pciaddress).."/driver/unbind", qualified(pciaddress)) + end end -- Memory map PCI device configuration space. @@ -142,6 +142,20 @@ function root_check () lib.root_check("error: must run as root to access PCI devices") end +-- Return the canonical (abbreviated) representation of the PCI address. +-- +-- example: canonical("0000:01:00.0") -> "01:00.0" +function canonical (address) + return address:gsub("^0000:", "") +end + +-- Return the fully-qualified representation of a PCI address. +-- +-- example: qualified("01:00.0") -> "0000:01:00.0" +function qualified (address) + return address:gsub("^%d%d:%d%d[.]%d+$", "0000:%1") +end + --- ### Selftest --- --- PCI selftest scans for available devices and performs our driver's @@ -149,6 +163,10 @@ end function selftest () print("selftest: pci") + assert(qualified("0000:01:00.0") == "0000:01:00.0", "qualified 1") + assert(qualified( "01:00.0") == "0000:01:00.0", "qualified 2") + assert(canonical("0000:01:00.0") == "01:00.0", "canonical 1") + assert(canonical( "01:00.0") == "01:00.0", "canonical 2") scan_devices() print_device_summary() end diff --git a/src/program/packetblaster/packetblaster.lua b/src/program/packetblaster/packetblaster.lua index b53f67ac4f..37af858960 100644 --- a/src/program/packetblaster/packetblaster.lua +++ b/src/program/packetblaster/packetblaster.lua @@ -67,7 +67,7 @@ function is_device_suitable (pcidev, patterns) return true end for _, pattern in ipairs(patterns) do - if pcidev.pciaddress:gmatch(pattern)() then + if pci.qualified(pcidev.pciaddress):gmatch(pattern)() then return true end end