From 68fb3dfee2a525aa93bd9d52a616515ab9d2bbbe Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sun, 12 May 2024 22:00:26 +0530 Subject: [PATCH] enhancement: Use pointer fields for `FibreChannel*` (#623) * enhancement: Use pointer fields for `FibreChannel*` Allow fields to be `nil`-checked. Fixes: #612 Signed-off-by: Pranshu Srivastava * fixup! enhancement: Use pointer fields for `FibreChannel*` Signed-off-by: Pranshu Srivastava --------- Signed-off-by: Pranshu Srivastava --- sysfs/class_fibrechannel.go | 110 ++++++++++++------------ sysfs/class_fibrechannel_test.go | 142 +++++++++++++++++++++---------- 2 files changed, 153 insertions(+), 99 deletions(-) diff --git a/sysfs/class_fibrechannel.go b/sysfs/class_fibrechannel.go index ded07a88..4b583f71 100644 --- a/sysfs/class_fibrechannel.go +++ b/sysfs/class_fibrechannel.go @@ -28,36 +28,36 @@ import ( const fibrechannelClassPath = "class/fc_host" type FibreChannelCounters struct { - DumpedFrames uint64 // /sys/class/fc_host//statistics/dumped_frames - ErrorFrames uint64 // /sys/class/fc_host//statistics/error_frames - InvalidCRCCount uint64 // /sys/class/fc_host//statistics/invalid_crc_count - RXFrames uint64 // /sys/class/fc_host//statistics/rx_frames - RXWords uint64 // /sys/class/fc_host//statistics/rx_words - TXFrames uint64 // /sys/class/fc_host//statistics/tx_frames - TXWords uint64 // /sys/class/fc_host//statistics/tx_words - SecondsSinceLastReset uint64 // /sys/class/fc_host//statistics/seconds_since_last_reset - InvalidTXWordCount uint64 // /sys/class/fc_host//statistics/invalid_tx_word_count - LinkFailureCount uint64 // /sys/class/fc_host//statistics/link_failure_count - LossOfSyncCount uint64 // /sys/class/fc_host//statistics/loss_of_sync_count - LossOfSignalCount uint64 // /sys/class/fc_host//statistics/loss_of_signal_count - NosCount uint64 // /sys/class/fc_host//statistics/nos_count - FCPPacketAborts uint64 // / sys/class/fc_host//statistics/fcp_packet_aborts + DumpedFrames *uint64 // /sys/class/fc_host//statistics/dumped_frames + ErrorFrames *uint64 // /sys/class/fc_host//statistics/error_frames + InvalidCRCCount *uint64 // /sys/class/fc_host//statistics/invalid_crc_count + RXFrames *uint64 // /sys/class/fc_host//statistics/rx_frames + RXWords *uint64 // /sys/class/fc_host//statistics/rx_words + TXFrames *uint64 // /sys/class/fc_host//statistics/tx_frames + TXWords *uint64 // /sys/class/fc_host//statistics/tx_words + SecondsSinceLastReset *uint64 // /sys/class/fc_host//statistics/seconds_since_last_reset + InvalidTXWordCount *uint64 // /sys/class/fc_host//statistics/invalid_tx_word_count + LinkFailureCount *uint64 // /sys/class/fc_host//statistics/link_failure_count + LossOfSyncCount *uint64 // /sys/class/fc_host//statistics/loss_of_sync_count + LossOfSignalCount *uint64 // /sys/class/fc_host//statistics/loss_of_signal_count + NosCount *uint64 // /sys/class/fc_host//statistics/nos_count + FCPPacketAborts *uint64 // /sys/class/fc_host//statistics/fcp_packet_aborts } type FibreChannelHost struct { - Name string // /sys/class/fc_host/ - Speed string // /sys/class/fc_host//speed - PortState string // /sys/class/fc_host//port_state - PortType string // /sys/class/fc_host//port_type - SymbolicName string // /sys/class/fc_host//symbolic_name - NodeName string // /sys/class/fc_host//node_name - PortID string // /sys/class/fc_host//port_id - PortName string // /sys/class/fc_host//port_name - FabricName string // /sys/class/fc_host//fabric_name - DevLossTMO string // /sys/class/fc_host//dev_loss_tmo - SupportedClasses string // /sys/class/fc_host//supported_classes - SupportedSpeeds string // /sys/class/fc_host//supported_speeds - Counters FibreChannelCounters // /sys/class/fc_host//statistics/* + Name *string // /sys/class/fc_host/ + Speed *string // /sys/class/fc_host//speed + PortState *string // /sys/class/fc_host//port_state + PortType *string // /sys/class/fc_host//port_type + SymbolicName *string // /sys/class/fc_host//symbolic_name + NodeName *string // /sys/class/fc_host//node_name + PortID *string // /sys/class/fc_host//port_id + PortName *string // /sys/class/fc_host//port_name + FabricName *string // /sys/class/fc_host//fabric_name + DevLossTMO *string // /sys/class/fc_host//dev_loss_tmo + SupportedClasses *string // /sys/class/fc_host//supported_classes + SupportedSpeeds *string // /sys/class/fc_host//supported_speeds + Counters *FibreChannelCounters // /sys/class/fc_host//statistics/* } type FibreChannelClass map[string]FibreChannelHost @@ -78,7 +78,7 @@ func (fs FS) FibreChannelClass() (FibreChannelClass, error) { return nil, err } - fcc[host.Name] = *host + fcc[*host.Name] = *host } return fcc, nil @@ -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) @@ -103,39 +103,39 @@ 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 } } @@ -143,7 +143,7 @@ func (fs FS) parseFibreChannelHost(name string) (*FibreChannelHost, error) { if err != nil { return nil, err } - host.Counters = *counters + host.Counters = counters return &host, nil } @@ -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() @@ -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 { diff --git a/sysfs/class_fibrechannel_test.go b/sysfs/class_fibrechannel_test.go index 55f728c2..5f1869ec 100644 --- a/sysfs/class_fibrechannel_test.go +++ b/sysfs/class_fibrechannel_test.go @@ -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, }, }, }