Skip to content
Closed
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
2 changes: 1 addition & 1 deletion calnex/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var exportCmd = &cobra.Command{
Use: "export",
Short: "export calnex measurement data",
Run: func(_ *cobra.Command, _ []string) {
var chs []api.Channel
chs := make([]api.Channel, 0, len(channels))
for _, channel := range channels {
chs = append(chs, channel)
}
Expand Down
2 changes: 1 addition & 1 deletion calnex/verify/checks/rb.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (p *RB) Run(target string, insecureTLS bool) error {
// RBState 7 is "Hold-over (Weak GNSS)"
return nil
}
return fmt.Errorf("Rubidium clock state is: %s", rb.RBStateName)
return fmt.Errorf("rubidium clock state is: %s", rb.RBStateName)
}

// Remediate the check
Expand Down
4 changes: 2 additions & 2 deletions cmd/ntpcheck/checker/chrony.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (n *ChronyCheck) Run() (*NTPCheckResult, error) {
log.Debugf("Status: %v", packet.GetStatus())
tracking, ok := packet.(*chrony.ReplyTracking)
if !ok {
return nil, fmt.Errorf("Got wrong 'tracking' response %+v", packet)
return nil, fmt.Errorf("got wrong 'tracking' response %+v", packet)
}
result.Correction = tracking.CurrentCorrection
result.LIDesc = control.LeapDesc[uint8(tracking.LeapStatus)]
Expand All @@ -106,7 +106,7 @@ func (n *ChronyCheck) Run() (*NTPCheckResult, error) {
}
sources, ok := packet.(*chrony.ReplySources)
if !ok {
return nil, fmt.Errorf("Got wrong 'sources' response %+v", packet)
return nil, fmt.Errorf("got wrong 'sources' response %+v", packet)
}
log.Debugf("Got %d sources", sources.NSources)

Expand Down
2 changes: 1 addition & 1 deletion cmd/ntpcheck/checker/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func peersAverages(peers []*Peer) (*averages, error) {
}

func medianOffset(peers []*Peer) float64 {
offsets := []float64{}
offsets := make([]float64, 0, len(peers))
for _, p := range peers {
offsets = append(offsets, p.Offset)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/pshark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (m *MultiMessageType) Set(messageType string) error {

// String returns joined list of message types
func (m *MultiMessageType) String() string {
s := []string{}
s := make([]string, 0, len(*m))
for _, v := range []ptp.MessageType(*m) {
s = append(s, v.String())
}
Expand All @@ -61,7 +61,7 @@ func (m *MultiMessageType) String() string {

// GetDefaults returns default message type filter
func (m *MultiMessageType) GetDefaults() []ptp.MessageType {
res := []ptp.MessageType{}
res := make([]ptp.MessageType, 0, len(ptp.MessageTypeToString))
for v := range ptp.MessageTypeToString {
res = append(res, v)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ptpcheck/checker/ptp4l_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (c *fakeConn) Write(p []byte) (n int, err error) {
}

func prepareTestClient(t *testing.T, packets ...ptp.Packet) *ptp.MgmtClient {
outputs := []*bytes.Buffer{}
outputs := make([]*bytes.Buffer, 0, len(packets))
for _, packet := range packets {
buf := &bytes.Buffer{}
b, err := ptp.Bytes(packet)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ptpcheck/cmd/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func checkPathDelay(r *checker.PTPCheckResult) (status, string) {
}

func portServiceStatsDiagnosers(r *checker.PTPCheckResult) []diagnoser {
result := []diagnoser{}
// counters are reset on ptp4l restart
var maxPacketsLoss uint64 = 100

Expand Down Expand Up @@ -216,6 +215,7 @@ func portServiceStatsDiagnosers(r *checker.PTPCheckResult) []diagnoser {
explanation: "We expect FollowUp packets to arrive in correct order",
},
}
result := make([]diagnoser, 0, len(checks))
for _, check := range checks {
var f diagnoser
check := check // capture loop variable
Expand Down
5 changes: 1 addition & 4 deletions cmd/ptpcheck/cmd/pdelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,7 @@ func collectMulticastResponses(conn client.UDPConnWithTS, expectedSeq uint16, t1
deadline := time.Now().Add(timeout)

// Set socket read timeout so ReadPacketWithRXTimestampBuf doesn't block forever
tv := unix.Timeval{
Sec: int64(timeout.Seconds()),
Usec: (timeout % time.Second).Microseconds(),
}
tv := unix.NsecToTimeval(timeout.Nanoseconds())
if err := unix.SetsockoptTimeval(conn.ConnFd(), unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv); err != nil {
log.Warningf("[pdelay] failed to set socket timeout: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ptpcheck/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestObserveOffset(t *testing.T) {

// repeatNumber creates a slice with the given number repeated repetitionCount times
func repeatNumber(repetitionCount int, number float64) []float64 {
slice := []float64{}
slice := make([]float64, 0, repetitionCount)
for range repetitionCount {
slice = append(slice, number)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/ziffy/node/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,8 @@ func colNumber(header []string, colName string) (int, error) {
}

func computePrintData(sw []SwitchPrintInfo) [][]string {
ret := [][]string{
{"uniq", "width", "hop", "ip_address", "sample_sp", "intf", "hostname", "flows", "TC", "avg_CF(ns)", "max_CF(ns)", "min_CF(ns)"},
}
ret := make([][]string, 0, 1+len(sw))
ret = append(ret, []string{"uniq", "width", "hop", "ip_address", "sample_sp", "intf", "hostname", "flows", "TC", "avg_CF(ns)", "max_CF(ns)", "min_CF(ns)"})

// unique counts number of devices discovered
unique := 1
Expand Down
6 changes: 3 additions & 3 deletions cmd/ziffy/node/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ func (r *Receiver) handlePacket(rawPacket gopacket.Packet) {
log.Tracef("unable to parse PTP: %v", err)
return
}
if ptpSync.Header.ControlField != ZiffyHexa {
if ptpSync.ControlField != ZiffyHexa {
log.Trace("no ziffy packet")
return
}

log.Debugf("Type=%v, CF=%v, sPort=%v, sPort2=%v, sIP=%v", ptpSync.Header.MessageType().String(),
ptpSync.Header.CorrectionField, ptpSync.Header.SourcePortIdentity.PortNumber, srcPort, srcIP)
log.Debugf("Type=%v, CF=%v, sPort=%v, sPort2=%v, sIP=%v", ptpSync.MessageType().String(),
ptpSync.CorrectionField, ptpSync.SourcePortIdentity.PortNumber, srcPort, srcIP)

if err := r.sendResponse(srcIP, rawPacket); err != nil {
log.Tracef("unable to send response: %v", err)
Expand Down
28 changes: 14 additions & 14 deletions cmd/ziffy/node/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (s *Sender) traceRoute(destinationIP string, sendingPort int, routeID int)
route := &PathInfo{switches: nil, rackSwHostname: s.rackSwHostname}
ptpUDPAddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(destinationIP, fmt.Sprint(s.Config.DestinationPort)))
if err != nil {
return route, fmt.Errorf("traceRoute unable to resolve UDPAddr: %w", err)
return nil, fmt.Errorf("traceRoute unable to resolve UDPAddr: %w", err)
}
ptpAddr := timestamp.IPToSockaddr(ptpUDPAddr.IP, s.Config.DestinationPort)
domain := unix.AF_INET6
Expand All @@ -204,16 +204,16 @@ func (s *Sender) traceRoute(destinationIP string, sendingPort int, routeID int)
}
connFd, err := unix.Socket(domain, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
if err != nil {
return route, fmt.Errorf("traceRoute unable to create connection: %w", err)
return nil, fmt.Errorf("traceRoute unable to create connection: %w", err)
}
defer unix.Close(connFd)
// set SO_REUSEPORT so we can trace network path from same source port that ptp4u uses
if err = unix.SetsockoptInt(connFd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
return route, fmt.Errorf("setting SO_REUSEPORT on sender socket: %w", err)
return nil, fmt.Errorf("setting SO_REUSEPORT on sender socket: %w", err)
}
localAddr := timestamp.IPToSockaddr(net.IPv6zero, sendingPort)
if err := unix.Bind(connFd, localAddr); err != nil {
return route, fmt.Errorf("traceRoute unable to bind %v connection: %w", localAddr, err)
return nil, fmt.Errorf("traceRoute unable to bind %v connection: %w", localAddr, err)
}

destReached := false
Expand All @@ -224,11 +224,11 @@ func (s *Sender) traceRoute(destinationIP string, sendingPort int, routeID int)
for hop := s.Config.HopMin; hop <= hopMax && (!destReached || s.Config.ContReached); hop++ {
for i := 0; i < s.Config.PacketsPerHop; i++ {
if err := unix.SetsockoptInt(connFd, unix.IPPROTO_IPV6, unix.IPV6_UNICAST_HOPS, hop); err != nil {
return route, err
return nil, err
}
// First 2 bits from Traffic Class are unused, so we shift the value 2 bits
if err := unix.SetsockoptInt(connFd, unix.IPPROTO_IPV6, unix.IPV6_TCLASS, s.Config.DSCP<<2); err != nil {
return route, err
return nil, err
}
var p ptp.Packet
switch s.Config.MessageType {
Expand All @@ -237,11 +237,11 @@ func (s *Sender) traceRoute(destinationIP string, sendingPort int, routeID int)
case ptp.MessageSignaling:
p = formSignalingPacket(hop, routeID)
default:
return route, fmt.Errorf("unsupported packet type %v", s.Config.MessageType)
return nil, fmt.Errorf("unsupported packet type %v", s.Config.MessageType)
}

if err := s.sendEventMsg(p, connFd, ptpAddr); err != nil {
return route, err
return nil, err
}

select {
Expand Down Expand Up @@ -333,13 +333,13 @@ func (s *Sender) handleIcmpPacket(rawPacket []byte, l int, rAddr net.Addr) {
)
switch v := ptpPacket.(type) {
case *ptp.SyncDelayReq:
corrField = v.Header.CorrectionField
sequenceID = v.Header.SequenceID
portNum = v.Header.SourcePortIdentity.PortNumber
corrField = v.CorrectionField
sequenceID = v.SequenceID
portNum = v.SourcePortIdentity.PortNumber
case *ptp.Signaling:
corrField = v.Header.CorrectionField
sequenceID = v.Header.SequenceID
portNum = v.Header.SourcePortIdentity.PortNumber
corrField = v.CorrectionField
sequenceID = v.SequenceID
portNum = v.SourcePortIdentity.PortNumber
default:
log.Errorf("Received unexpected packet %T, ignoring", v)
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/ziffy/node/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestClearPaths(t *testing.T) {
}

func TestSortSwitchesByHop(t *testing.T) {
var switches []SwitchTrafficInfo
switches := make([]SwitchTrafficInfo, 0, 5)
switches = append(switches, SwitchTrafficInfo{hop: 3, routeIdx: 1})
switches = append(switches, SwitchTrafficInfo{hop: 1, routeIdx: 2})
switches = append(switches, SwitchTrafficInfo{hop: 10, routeIdx: 5})
Expand Down
8 changes: 4 additions & 4 deletions ntp/chrony/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ func TestDecodeSourceDataWithIPAddrID(t *testing.T) {
require.True(t, ok, "expected *ReplySourceData")

// Verify the IPAddr has Family = IPAddrID
require.Equal(t, IPAddrID, reply.SourceData.IPAddr.Family)
require.Equal(t, IPAddrID, reply.IPAddr.Family)

// Verify the ID value is correctly stored in the IP field
require.Equal(t, uint8(0x00), reply.SourceData.IPAddr.IP[0])
require.Equal(t, uint8(0x00), reply.SourceData.IPAddr.IP[1])
require.Equal(t, uint8(0x00), reply.IPAddr.IP[0])
require.Equal(t, uint8(0x00), reply.IPAddr.IP[1])
require.Equal(t, uint8(0x00), reply.SourceData.IPAddr.IP[2])
require.Equal(t, uint8(0x09), reply.SourceData.IPAddr.IP[3])

Expand Down Expand Up @@ -298,7 +298,7 @@ func TestDecodeServerStats(t *testing.T) {
}
require.Equal(t, want, packet)

require.Equal(t, want.ReplyHead.Reply, packet.GetType())
require.Equal(t, want.Reply, packet.GetType())
}

func TestDecodeServerStats2(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ntp/control/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestNTPControlMsg_GetAssociations(t *testing.T) {
PeerEventCounter: 0,
PeerEventCode: 3,
}
assocData := []uint8{}
assocData := make([]uint8, 0, 8)
assocData = append(assocData, uint16to2x8(1)...)
assocData = append(assocData, uint16to2x8(peerStatusWord1.Word())...)
assocData = append(assocData, uint16to2x8(2)...)
Expand Down
10 changes: 5 additions & 5 deletions phc/pps_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ func TestNewPiServo(t *testing.T) {
servo, err := NewPiServo(time.Duration(1), time.Duration(1), time.Duration(0), mockFrequencyGetter, 0.0)

require.NoError(t, err)
require.Equal(t, int64(1), servo.Servo.FirstStepThreshold)
require.Equal(t, true, servo.Servo.FirstUpdate)
require.Equal(t, int64(1), servo.FirstStepThreshold)
require.Equal(t, true, servo.FirstUpdate)
require.Equal(t, -1.0, servo.MeanFreq())
require.Equal(t, "INIT", servo.GetState().String())
require.Equal(t, 3.0, servo.GetMaxFreq())
Expand Down Expand Up @@ -387,8 +387,8 @@ func TestNewPiServoMaxFreqError(t *testing.T) {
servo, err := NewPiServo(time.Duration(1), time.Duration(1), time.Duration(0), mockFrequencyGetter, 0.0)

require.NoError(t, err)
require.Equal(t, int64(1), servo.Servo.FirstStepThreshold)
require.Equal(t, true, servo.Servo.FirstUpdate)
require.Equal(t, int64(1), servo.FirstStepThreshold)
require.Equal(t, true, servo.FirstUpdate)
require.Equal(t, -1.0, servo.MeanFreq())
require.Equal(t, "INIT", servo.GetState().String())
require.Equal(t, 500000.0, servo.GetMaxFreq())
Expand Down Expand Up @@ -472,7 +472,7 @@ func TestPPSSink_getPPSEventTimestamp(t *testing.T) {
err := binary.Write(&intBuffer, hostendian.Order, &event)
require.NoError(t, err)
copy(buf, intBuffer.Bytes())
fmt.Print(buf)
fmt.Print(string(buf))
})

// Act
Expand Down
2 changes: 1 addition & 1 deletion ptp/linearizability/linearizability_ptp.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func (lt *PTPTester) RunTest(ctx context.Context) TestResult {
func (lt *PTPTester) processTimestamp(sequenceID uint16, rxTimestamp time.Time) error {
sendTS, found := lt.sendTS[sequenceID]
if !found {
expected := []uint16{}
expected := make([]uint16, 0, len(lt.sendTS))
for e := range lt.sendTS {
expected = append(expected, e)
}
Expand Down
8 changes: 4 additions & 4 deletions ptp/protocol/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,20 @@ func (p *ManagementMsgErrorStatus) UnmarshalBinary(rawBytes []byte) error {
if err := binary.Read(reader, be, &p.ManagementMsgHead); err != nil {
return fmt.Errorf("reading ManagementMsgErrorStatus ManagementMsgHead: %w", err)
}
if err := binary.Read(reader, be, &p.ManagementErrorStatusTLV.TLVHead); err != nil {
if err := binary.Read(reader, be, &p.TLVHead); err != nil {
return fmt.Errorf("reading ManagementMsgErrorStatus TLVHead: %w", err)
}
if err := binary.Read(reader, be, &p.ManagementErrorStatusTLV.ManagementErrorID); err != nil {
if err := binary.Read(reader, be, &p.ManagementErrorID); err != nil {
return fmt.Errorf("reading ManagementMsgErrorStatus ManagementErrorID: %w", err)
}
if err := binary.Read(reader, be, &p.ManagementErrorStatusTLV.ManagementID); err != nil {
if err := binary.Read(reader, be, &p.ManagementID); err != nil {
return fmt.Errorf("reading ManagementMsgErrorStatus ManagementID: %w", err)
}
if err := binary.Read(reader, be, &p.ManagementErrorStatusTLV.Reserved); err != nil {
return fmt.Errorf("reading ManagementMsgErrorStatus Reserved: %w", err)
}
// packet can have trailing bytes, let's make sure we don't try to read past given length
toRead := int(p.ManagementMsgHead.Header.MessageLength)
toRead := int(p.MessageLength)
toRead -= binary.Size(p.ManagementMsgHead)
toRead -= binary.Size(p.ManagementErrorStatusTLV.TLVHead)
toRead -= binary.Size(p.ManagementErrorStatusTLV.ManagementErrorID)
Expand Down
7 changes: 4 additions & 3 deletions ptp/protocol/ptp4l.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ func (e *UnicastMasterEntry) UnmarshalBinary(b []byte) error {
e.ClockQuality.ClockClass = ClockClass(b[10])
e.ClockQuality.ClockAccuracy = ClockAccuracy(b[11])
e.ClockQuality.OffsetScaledLogVariance = binary.BigEndian.Uint16(b[12:])
if b[14] == 0 {
switch b[14] {
case 0:
e.Selected = false
} else if b[14] == 1 {
case 1:
e.Selected = true
} else {
default:
return fmt.Errorf("unexpected 'selected' value %d", b[14])
}
e.PortState = UnicastMasterState(b[15])
Expand Down
4 changes: 2 additions & 2 deletions ptp/protocol/ptp4l_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestParseTimeStatusNP(t *testing.T) {
func TestTimeStatusNPRequest(t *testing.T) {
req := TimeStatusNPRequest()
// it's normally generated from PID, set to know value
req.ManagementMsgHead.Header.SourcePortIdentity.PortNumber = 12345
req.Header.SourcePortIdentity.PortNumber = 12345

raw, err := Bytes(req)
want := []byte{
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestParsePortStatsNP(t *testing.T) {
func TestPortStatsNPRequest(t *testing.T) {
req := PortStatsNPRequest()
// it's normally generated from PID, set to know value
req.ManagementMsgHead.Header.SourcePortIdentity.PortNumber = 12345
req.Header.SourcePortIdentity.PortNumber = 12345

raw, err := Bytes(req)
want := []byte{
Expand Down
8 changes: 2 additions & 6 deletions ptp/protocol/tlvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ func writeTLVs(tlvs []TLV, b []byte) (int, error) {
func readTLVs(tlvs []TLV, maxLength int, b []byte) ([]TLV, error) {
pos := 0
var tlvType TLVType
for {
// packet can have trailing bytes, let's make sure we don't try to read past given length
if pos+tlvHeadSize > maxLength {
break
}
for pos+tlvHeadSize <= maxLength {
tlvType = TLVType(binary.BigEndian.Uint16(b[pos:]))

switch tlvType {
Expand Down Expand Up @@ -353,7 +349,7 @@ func (t *PathTraceTLV) UnmarshalBinary(b []byte) error {
return err
}
t.PathSequence = []ClockIdentity{}
for i := 0; i*8 <= int(t.TLVHead.LengthField); i++ {
for i := 0; i*8 <= int(t.LengthField); i++ {
pos := tlvHeadSize + i*8
if pos+8 >= len(b) {
break
Expand Down
2 changes: 1 addition & 1 deletion ptp/ptp4u/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func ReadPidFile(path string) (int, error) {
return 0, err
}

return strconv.Atoi(strings.Replace(string(content), "\n", "", -1))
return strconv.Atoi(strings.ReplaceAll(string(content), "\n", ""))
}

// ifaceIPs gets all IPs on the specified interface
Expand Down
Loading
Loading