Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.pci: Support short/canonical ("01:00.0") PCI addresses #669

Merged
merged 3 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/lib/hardware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/lib/hardware/README.md.src
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 24 additions & 6 deletions src/lib/hardware/pci.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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',
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -142,13 +142,31 @@ 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
--- self-test on each of them.

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
Expand Down
2 changes: 1 addition & 1 deletion src/program/packetblaster/packetblaster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down