Skip to content

Commit

Permalink
enhancement: Use pointer fields for FibreChannel*
Browse files Browse the repository at this point in the history
Allow fields to be `nil`-checked.

Fixes: #612
Signed-off-by: Pranshu Srivastava <[email protected]>
  • Loading branch information
rexagod committed Mar 23, 2024
1 parent f7c2619 commit 7857a4f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 99 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/google/go-cmp v0.6.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.18.0
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0gQBEuevE/AaBsHY=
k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
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
89 changes: 45 additions & 44 deletions sysfs/class_fibrechannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/utils/ptr"
)

func TestFibreChannelClass(t *testing.T) {
Expand All @@ -35,53 +36,53 @@ func TestFibreChannelClass(t *testing.T) {

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: ptr.To("host0"),
Speed: ptr.To("16 Gbit"),
PortState: ptr.To("Online"),
PortType: ptr.To("Point-To-Point (direct nport connection)"),
PortName: ptr.To("1000e0071bce95f2"),
SymbolicName: ptr.To("Emulex SN1100E2P FV12.4.270.3 DV12.4.0.0. HN:gotest. OS:Linux"),
NodeName: ptr.To("2000e0071bce95f2"),
PortID: ptr.To("000002"),
FabricName: ptr.To("0"),
DevLossTMO: ptr.To("30"),
SupportedClasses: ptr.To("Class 3"),
SupportedSpeeds: ptr.To("4 Gbit, 8 Gbit, 16 Gbit"),
Counters: &FibreChannelCounters{
DumpedFrames: ptr.To(^uint64(0)),
ErrorFrames: ptr.To(uint64(0)),
InvalidCRCCount: ptr.To(uint64(0x2)),
RXFrames: ptr.To(uint64(0x3)),
RXWords: ptr.To(uint64(0x4)),
TXFrames: ptr.To(uint64(0x5)),
TXWords: ptr.To(uint64(0x6)),
SecondsSinceLastReset: ptr.To(uint64(0x7)),
InvalidTXWordCount: ptr.To(uint64(0x8)),
LinkFailureCount: ptr.To(uint64(0x9)),
LossOfSyncCount: ptr.To(uint64(0x10)),
LossOfSignalCount: ptr.To(uint64(0x11)),
NosCount: ptr.To(uint64(0x12)),
FCPPacketAborts: ptr.To(uint64(0x13)),
},
},
"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: ptr.To("host1"),
PortState: ptr.To("Online"),
Counters: &FibreChannelCounters{
DumpedFrames: ptr.To(uint64(0)),
ErrorFrames: ptr.To(^uint64(0)),
InvalidCRCCount: ptr.To(uint64(0x20)),
RXFrames: ptr.To(uint64(0x30)),
RXWords: ptr.To(uint64(0x40)),
TXFrames: ptr.To(uint64(0x50)),
TXWords: ptr.To(uint64(0x60)),
SecondsSinceLastReset: ptr.To(uint64(0x70)),
InvalidTXWordCount: ptr.To(uint64(0x80)),
LinkFailureCount: ptr.To(uint64(0x90)),
LossOfSyncCount: ptr.To(uint64(0x100)),
LossOfSignalCount: ptr.To(uint64(0x110)),
NosCount: ptr.To(uint64(0x120)),
FCPPacketAborts: ptr.To(uint64(0x130)),
},
},
}
Expand Down

0 comments on commit 7857a4f

Please sign in to comment.