Skip to content

Commit 12abe5a

Browse files
authored
Fix: fallback to lspci for NIC model name when not found in udevadm (#670)
* fix: add fallback to lspci when udevadm doesn't include NIC model name Signed-off-by: Harper, Jason M <jason.m.harper@intel.com> * don't include virtual interfaces in summary Signed-off-by: Harper, Jason M <jason.m.harper@intel.com> * improve check for virtual interface Signed-off-by: Harper, Jason M <jason.m.harper@intel.com> * more efficient extraction of model Signed-off-by: Harper, Jason M <jason.m.harper@intel.com> --------- Signed-off-by: Harper, Jason M <jason.m.harper@intel.com>
1 parent 084aa5c commit 12abe5a

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

internal/extract/nic.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,27 @@ func NICIrqMappingsFromOutput(outputs map[string]script.ScriptOutput) [][]string
229229
// NICSummaryFromOutput returns a summary of installed NICs.
230230
func NICSummaryFromOutput(outputs map[string]script.ScriptOutput) string {
231231
nics := ParseNicInfo(outputs[script.NicInfoScriptName].Stdout)
232-
if len(nics) == 0 {
233-
return "N/A"
234-
}
235232
modelCount := make(map[string]int)
236233
for _, nic := range nics {
234+
// don't include virtual functions or bridge devices in the summary as they are not physical NICs, and we want the summary to reflect physical hardware
235+
if nic.IsVirtual || strings.Contains(nic.Name, "(virtual)") || nic.Driver == "bridge" {
236+
continue
237+
}
238+
if strings.TrimSpace(nic.Model) == "" {
239+
vendor := strings.TrimSpace(nic.Vendor)
240+
if vendor == "" {
241+
nic.Model = "Unknown Vendor and Model"
242+
} else {
243+
nic.Model = vendor + " Unknown Model"
244+
}
245+
}
237246
modelCount[nic.Model]++
238247
}
248+
if len(modelCount) == 0 {
249+
return "N/A"
250+
}
239251
var summary []string
240252
for model, count := range modelCount {
241-
if model == "" {
242-
model = "Unknown NIC"
243-
}
244253
summary = append(summary, fmt.Sprintf("%dx %s", count, model))
245254
}
246255
return strings.Join(summary, ", ")

internal/script/scripts.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,34 @@ rdmsr 0x2FFE
845845
udevadm_out=$(udevadm info --query=all --path=/sys/class/net/"$ifc")
846846
echo "Vendor ID: $(echo "$udevadm_out" | grep ID_VENDOR_ID= | cut -d'=' -f2)"
847847
echo "Model ID: $(echo "$udevadm_out" | grep ID_MODEL_ID= | cut -d'=' -f2)"
848-
echo "Vendor: $(echo "$udevadm_out" | grep ID_VENDOR_FROM_DATABASE= | cut -d'=' -f2)"
849-
echo "Model: $(echo "$udevadm_out" | grep ID_MODEL_FROM_DATABASE= | cut -d'=' -f2)"
848+
vendor=$(echo "$udevadm_out" | grep ID_VENDOR_FROM_DATABASE= | cut -d'=' -f2)
849+
echo "Vendor: $vendor"
850+
model=$(echo "$udevadm_out" | grep ID_MODEL_FROM_DATABASE= | cut -d'=' -f2)
851+
# fall back to lspci if model is not available from udevadm, and trim vendor prefix if present (e.g. "Intel Ethernet Controller" -> "Ethernet Controller")
852+
if [ -z "$model" ]; then
853+
id_path=$(echo "$udevadm_out" | grep ID_PATH= | cut -d'=' -f2)
854+
bdf=${id_path##*-}
855+
# ID_PATH may include the domain (0000:49:00.0); lspci typically prints 49:00.0.
856+
if [[ "$bdf" == *:*:* ]]; then
857+
bdf=${bdf#*:}
858+
fi
859+
if [ -n "$bdf" ] && command -v lspci >/dev/null 2>&1; then
860+
model=$(lspci -s "$bdf" -i pci.ids.gz | awk 'NR == 1 { pos = index($0, ": "); if (pos > 0) print substr($0, pos + 2); exit }')
861+
if [ -n "$model" ] && [ -n "$vendor" ]; then
862+
model=$(awk -v vendor="$vendor" -v model="$model" 'BEGIN {
863+
v = tolower(vendor)
864+
m = tolower(model)
865+
out = model
866+
if (v != "" && substr(m, 1, length(v)) == v) {
867+
out = substr(model, length(v) + 1)
868+
sub(/^[[:space:]]+/, "", out)
869+
}
870+
print out
871+
}')
872+
fi
873+
fi
874+
fi
875+
echo "Model: $model"
850876
echo "MTU: $(cat /sys/class/net/"$ifc"/mtu 2>/dev/null)"
851877
echo "$ethtool_out"
852878
echo "$ethtool_i_out"
@@ -883,7 +909,7 @@ rdmsr 0x2FFE
883909
echo "----------------------------------------"
884910
done
885911
`,
886-
Depends: []string{"ethtool"},
912+
Depends: []string{"ethtool", "lspci", "pci.ids.gz"},
887913
Superuser: true,
888914
},
889915
IRQBalanceScriptName: {

0 commit comments

Comments
 (0)