Skip to content

Commit

Permalink
enhancement: Use pointer fields for FibreChannel* (#623)
Browse files Browse the repository at this point in the history
* enhancement: Use pointer fields for `FibreChannel*`

Allow fields to be `nil`-checked.

Fixes: #612
Signed-off-by: Pranshu Srivastava <[email protected]>

* fixup! enhancement: Use pointer fields for `FibreChannel*`

Signed-off-by: Pranshu Srivastava <[email protected]>

---------

Signed-off-by: Pranshu Srivastava <[email protected]>
  • Loading branch information
rexagod authored May 12, 2024
1 parent 5cca38b commit 68fb3df
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 99 deletions.
110 changes: 55 additions & 55 deletions sysfs/class_fibrechannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,36 @@ import (
const fibrechannelClassPath = "class/fc_host"

type FibreChannelCounters struct {
DumpedFrames uint64 // /sys/class/fc_host/<Name>/statistics/dumped_frames
ErrorFrames uint64 // /sys/class/fc_host/<Name>/statistics/error_frames
InvalidCRCCount uint64 // /sys/class/fc_host/<Name>/statistics/invalid_crc_count
RXFrames uint64 // /sys/class/fc_host/<Name>/statistics/rx_frames
RXWords uint64 // /sys/class/fc_host/<Name>/statistics/rx_words
TXFrames uint64 // /sys/class/fc_host/<Name>/statistics/tx_frames
TXWords uint64 // /sys/class/fc_host/<Name>/statistics/tx_words
SecondsSinceLastReset uint64 // /sys/class/fc_host/<Name>/statistics/seconds_since_last_reset
InvalidTXWordCount uint64 // /sys/class/fc_host/<Name>/statistics/invalid_tx_word_count
LinkFailureCount uint64 // /sys/class/fc_host/<Name>/statistics/link_failure_count
LossOfSyncCount uint64 // /sys/class/fc_host/<Name>/statistics/loss_of_sync_count
LossOfSignalCount uint64 // /sys/class/fc_host/<Name>/statistics/loss_of_signal_count
NosCount uint64 // /sys/class/fc_host/<Name>/statistics/nos_count
FCPPacketAborts uint64 // / sys/class/fc_host/<Name>/statistics/fcp_packet_aborts
DumpedFrames *uint64 // /sys/class/fc_host/<Name>/statistics/dumped_frames
ErrorFrames *uint64 // /sys/class/fc_host/<Name>/statistics/error_frames
InvalidCRCCount *uint64 // /sys/class/fc_host/<Name>/statistics/invalid_crc_count
RXFrames *uint64 // /sys/class/fc_host/<Name>/statistics/rx_frames
RXWords *uint64 // /sys/class/fc_host/<Name>/statistics/rx_words
TXFrames *uint64 // /sys/class/fc_host/<Name>/statistics/tx_frames
TXWords *uint64 // /sys/class/fc_host/<Name>/statistics/tx_words
SecondsSinceLastReset *uint64 // /sys/class/fc_host/<Name>/statistics/seconds_since_last_reset
InvalidTXWordCount *uint64 // /sys/class/fc_host/<Name>/statistics/invalid_tx_word_count
LinkFailureCount *uint64 // /sys/class/fc_host/<Name>/statistics/link_failure_count
LossOfSyncCount *uint64 // /sys/class/fc_host/<Name>/statistics/loss_of_sync_count
LossOfSignalCount *uint64 // /sys/class/fc_host/<Name>/statistics/loss_of_signal_count
NosCount *uint64 // /sys/class/fc_host/<Name>/statistics/nos_count
FCPPacketAborts *uint64 // /sys/class/fc_host/<Name>/statistics/fcp_packet_aborts
}

type FibreChannelHost struct {
Name string // /sys/class/fc_host/<Name>
Speed string // /sys/class/fc_host/<Name>/speed
PortState string // /sys/class/fc_host/<Name>/port_state
PortType string // /sys/class/fc_host/<Name>/port_type
SymbolicName string // /sys/class/fc_host/<Name>/symbolic_name
NodeName string // /sys/class/fc_host/<Name>/node_name
PortID string // /sys/class/fc_host/<Name>/port_id
PortName string // /sys/class/fc_host/<Name>/port_name
FabricName string // /sys/class/fc_host/<Name>/fabric_name
DevLossTMO string // /sys/class/fc_host/<Name>/dev_loss_tmo
SupportedClasses string // /sys/class/fc_host/<Name>/supported_classes
SupportedSpeeds string // /sys/class/fc_host/<Name>/supported_speeds
Counters FibreChannelCounters // /sys/class/fc_host/<Name>/statistics/*
Name *string // /sys/class/fc_host/<Name>
Speed *string // /sys/class/fc_host/<Name>/speed
PortState *string // /sys/class/fc_host/<Name>/port_state
PortType *string // /sys/class/fc_host/<Name>/port_type
SymbolicName *string // /sys/class/fc_host/<Name>/symbolic_name
NodeName *string // /sys/class/fc_host/<Name>/node_name
PortID *string // /sys/class/fc_host/<Name>/port_id
PortName *string // /sys/class/fc_host/<Name>/port_name
FabricName *string // /sys/class/fc_host/<Name>/fabric_name
DevLossTMO *string // /sys/class/fc_host/<Name>/dev_loss_tmo
SupportedClasses *string // /sys/class/fc_host/<Name>/supported_classes
SupportedSpeeds *string // /sys/class/fc_host/<Name>/supported_speeds
Counters *FibreChannelCounters // /sys/class/fc_host/<Name>/statistics/*
}

type FibreChannelClass map[string]FibreChannelHost
Expand All @@ -78,7 +78,7 @@ func (fs FS) FibreChannelClass() (FibreChannelClass, error) {
return nil, err
}

fcc[host.Name] = *host
fcc[*host.Name] = *host
}

return fcc, nil
Expand All @@ -87,7 +87,7 @@ func (fs FS) FibreChannelClass() (FibreChannelClass, error) {
// Parse a single FC host.
func (fs FS) parseFibreChannelHost(name string) (*FibreChannelHost, error) {
path := fs.sys.Path(fibrechannelClassPath, name)
host := FibreChannelHost{Name: name}
host := FibreChannelHost{Name: &name}

for _, f := range [...]string{"speed", "port_state", "port_type", "node_name", "port_id", "port_name", "fabric_name", "dev_loss_tmo", "symbolic_name", "supported_classes", "supported_speeds"} {
name := filepath.Join(path, f)
Expand All @@ -103,47 +103,47 @@ func (fs FS) parseFibreChannelHost(name string) (*FibreChannelHost, error) {

switch f {
case "speed":
host.Speed = value
host.Speed = &value
case "port_state":
host.PortState = value
host.PortState = &value
case "port_type":
host.PortType = value
host.PortType = &value
case "node_name":
if len(value) > 2 {
value = value[2:]
}
host.NodeName = value
host.NodeName = &value
case "port_id":
if len(value) > 2 {
value = value[2:]
}
host.PortID = value
host.PortID = &value
case "port_name":
if len(value) > 2 {
value = value[2:]
}
host.PortName = value
host.PortName = &value
case "fabric_name":
if len(value) > 2 {
value = value[2:]
}
host.FabricName = value
host.FabricName = &value
case "dev_loss_tmo":
host.DevLossTMO = value
host.DevLossTMO = &value
case "supported_classes":
host.SupportedClasses = value
host.SupportedClasses = &value
case "supported_speeds":
host.SupportedSpeeds = value
host.SupportedSpeeds = &value
case "symbolic_name":
host.SymbolicName = value
host.SymbolicName = &value
}
}

counters, err := parseFibreChannelStatistics(path)
if err != nil {
return nil, err
}
host.Counters = *counters
host.Counters = counters

return &host, nil
}
Expand Down Expand Up @@ -178,9 +178,9 @@ func parseFibreChannelStatistics(hostPath string) (*FibreChannelCounters, error)
// Below switch was automatically generated. Don't need everything in there yet, so the unwanted bits are commented out.
switch f.Name() {
case "dumped_frames":
counters.DumpedFrames = *vp.PUInt64()
counters.DumpedFrames = vp.PUInt64()
case "error_frames":
counters.ErrorFrames = *vp.PUInt64()
counters.ErrorFrames = vp.PUInt64()
/*
case "fc_no_free_exch":
counters.FcNoFreeExch = *vp.PUInt64()
Expand Down Expand Up @@ -208,41 +208,41 @@ func parseFibreChannelStatistics(hostPath string) (*FibreChannelCounters, error)
counters.FcpOutputRequests = *vp.PUInt64()
*/
case "fcp_packet_aborts":
counters.FCPPacketAborts = *vp.PUInt64()
counters.FCPPacketAborts = vp.PUInt64()
/*
case "fcp_packet_alloc_failures":
counters.FcpPacketAllocFailures = *vp.PUInt64()
*/
case "invalid_tx_word_count":
counters.InvalidTXWordCount = *vp.PUInt64()
counters.InvalidTXWordCount = vp.PUInt64()
case "invalid_crc_count":
counters.InvalidCRCCount = *vp.PUInt64()
counters.InvalidCRCCount = vp.PUInt64()
case "link_failure_count":
counters.LinkFailureCount = *vp.PUInt64()
counters.LinkFailureCount = vp.PUInt64()
/*
case "lip_count":
counters.LipCount = *vp.PUInt64()
*/
case "loss_of_signal_count":
counters.LossOfSignalCount = *vp.PUInt64()
counters.LossOfSignalCount = vp.PUInt64()
case "loss_of_sync_count":
counters.LossOfSyncCount = *vp.PUInt64()
counters.LossOfSyncCount = vp.PUInt64()
case "nos_count":
counters.NosCount = *vp.PUInt64()
counters.NosCount = vp.PUInt64()
/*
case "prim_seq_protocol_err_count":
counters.PrimSeqProtocolErrCount = *vp.PUInt64()
*/
case "rx_frames":
counters.RXFrames = *vp.PUInt64()
counters.RXFrames = vp.PUInt64()
case "rx_words":
counters.RXWords = *vp.PUInt64()
counters.RXWords = vp.PUInt64()
case "seconds_since_last_reset":
counters.SecondsSinceLastReset = *vp.PUInt64()
counters.SecondsSinceLastReset = vp.PUInt64()
case "tx_frames":
counters.TXFrames = *vp.PUInt64()
counters.TXFrames = vp.PUInt64()
case "tx_words":
counters.TXWords = *vp.PUInt64()
counters.TXWords = vp.PUInt64()
}

if err := vp.Err(); err != nil {
Expand Down
142 changes: 98 additions & 44 deletions sysfs/class_fibrechannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,109 @@ func TestFibreChannelClass(t *testing.T) {
t.Fatal(err)
}

// "host0" FibreChannelHost constants.
var (
name0 = "host0"
speed0 = "16 Gbit"
portState0 = "Online"
portType0 = "Point-To-Point (direct nport connection)"
portName0 = "1000e0071bce95f2"
symbolicName0 = "Emulex SN1100E2P FV12.4.270.3 DV12.4.0.0. HN:gotest. OS:Linux"
nodeName0 = "2000e0071bce95f2"
portID0 = "000002"
fabricName0 = "0"
devLossTMO0 = "30"
supportedClasses0 = "Class 3"
supportedSpeeds0 = "4 Gbit, 8 Gbit, 16 Gbit"

// "host0" FibreChannelHost.Counters constants.
dumpedFrames0 = ^uint64(0)
errorFrames0 = uint64(0)
invalidCRCCount0 = uint64(0x2)
rxFrames0 = uint64(0x3)
rxWords0 = uint64(0x4)
txFrames0 = uint64(0x5)
txWords0 = uint64(0x6)
secondsSinceLastReset0 = uint64(0x7)
invalidTXWordCount0 = uint64(0x8)
linkFailureCount0 = uint64(0x9)
lossOfSyncCount0 = uint64(0x10)
lossOfSignalCount0 = uint64(0x11)
nosCount0 = uint64(0x12)
fcpPacketAborts0 = uint64(0x13)
)

// "host1" FibreChannelHost constants.
var (
name1 = "host1"
portState1 = "Online"

// "host1" FibreChannelHost.Counters constants.
dumpedFrames1 = uint64(0)
errorFrames1 = ^uint64(0)
invalidCRCCount1 = uint64(0x20)
rxFrames1 = uint64(0x30)
rxWords1 = uint64(0x40)
txFrames1 = uint64(0x50)
txWords1 = uint64(0x60)
secondsSinceLastReset1 = uint64(0x70)
invalidTXWordCount1 = uint64(0x80)
linkFailureCount1 = uint64(0x90)
lossOfSyncCount1 = uint64(0x100)
lossOfSignalCount1 = uint64(0x110)
nosCount1 = uint64(0x120)
fcpPacketAborts1 = uint64(0x130)
)

want := FibreChannelClass{
"host0": FibreChannelHost{
Name: "host0",
Speed: "16 Gbit",
PortState: "Online",
PortType: "Point-To-Point (direct nport connection)",
PortName: "1000e0071bce95f2",
SymbolicName: "Emulex SN1100E2P FV12.4.270.3 DV12.4.0.0. HN:gotest. OS:Linux",
NodeName: "2000e0071bce95f2",
PortID: "000002",
FabricName: "0",
DevLossTMO: "30",
SupportedClasses: "Class 3",
SupportedSpeeds: "4 Gbit, 8 Gbit, 16 Gbit",
Counters: FibreChannelCounters{
DumpedFrames: ^uint64(0),
ErrorFrames: 0,
InvalidCRCCount: 0x2,
RXFrames: 0x3,
RXWords: 0x4,
TXFrames: 0x5,
TXWords: 0x6,
SecondsSinceLastReset: 0x7,
InvalidTXWordCount: 0x8,
LinkFailureCount: 0x9,
LossOfSyncCount: 0x10,
LossOfSignalCount: 0x11,
NosCount: 0x12,
FCPPacketAborts: 0x13,
Name: &name0,
Speed: &speed0,
PortState: &portState0,
PortType: &portType0,
PortName: &portName0,
SymbolicName: &symbolicName0,
NodeName: &nodeName0,
PortID: &portID0,
FabricName: &fabricName0,
DevLossTMO: &devLossTMO0,
SupportedClasses: &supportedClasses0,
SupportedSpeeds: &supportedSpeeds0,
Counters: &FibreChannelCounters{
DumpedFrames: &dumpedFrames0,
ErrorFrames: &errorFrames0,
InvalidCRCCount: &invalidCRCCount0,
RXFrames: &rxFrames0,
RXWords: &rxWords0,
TXFrames: &txFrames0,
TXWords: &txWords0,
SecondsSinceLastReset: &secondsSinceLastReset0,
InvalidTXWordCount: &invalidTXWordCount0,
LinkFailureCount: &linkFailureCount0,
LossOfSyncCount: &lossOfSyncCount0,
LossOfSignalCount: &lossOfSignalCount0,
NosCount: &nosCount0,
FCPPacketAborts: &fcpPacketAborts0,
},
},
"host1": FibreChannelHost{
Name: "host1",
PortState: "Online",
Counters: FibreChannelCounters{
DumpedFrames: 0,
ErrorFrames: ^uint64(0),
InvalidCRCCount: 0x20,
RXFrames: 0x30,
RXWords: 0x40,
TXFrames: 0x50,
TXWords: 0x60,
SecondsSinceLastReset: 0x70,
InvalidTXWordCount: 0x80,
LinkFailureCount: 0x90,
LossOfSyncCount: 0x100,
LossOfSignalCount: 0x110,
NosCount: 0x120,
FCPPacketAborts: 0x130,
Name: &name1,
PortState: &portState1,
Counters: &FibreChannelCounters{
DumpedFrames: &dumpedFrames1,
ErrorFrames: &errorFrames1,
InvalidCRCCount: &invalidCRCCount1,
RXFrames: &rxFrames1,
RXWords: &rxWords1,
TXFrames: &txFrames1,
TXWords: &txWords1,
SecondsSinceLastReset: &secondsSinceLastReset1,
InvalidTXWordCount: &invalidTXWordCount1,
LinkFailureCount: &linkFailureCount1,
LossOfSyncCount: &lossOfSyncCount1,
LossOfSignalCount: &lossOfSignalCount1,
NosCount: &nosCount1,
FCPPacketAborts: &fcpPacketAborts1,
},
},
}
Expand Down

0 comments on commit 68fb3df

Please sign in to comment.