Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tc: drop logging #190

Merged
merged 1 commit into from
Dec 21, 2024
Merged
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
8 changes: 8 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ func endianSwapUint32(in uint32) uint32 {
((in & 0x00FF0000) >> 8) |
((in & 0xFF000000) >> 24)
}

// bytesToInt32 converts an array for four bytes in big endian format to an integer.
func bytesToInt32(b []byte) int32 {
if len(b) != 4 {
return 0
}
return int32(b[0])<<24 | int32(b[1])<<16 | int32(b[2])<<8 | int32(b[3])
}
18 changes: 18 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,21 @@ func TestEndianSwapUint32(t *testing.T) {
}
}
}

func TestBytesToInt32(t *testing.T) {
tests := []struct {
in []byte
out int32
}{
{in: []byte{0x1}, out: 0},
{in: []byte{0x0, 0x0, 0x0, 0x2a}, out: 42},
}

for _, test := range tests {
test := test
out := bytesToInt32(test.in)
if test.out != out {
t.Fatalf("%d != %d", test.out, out)
}
}
}
24 changes: 9 additions & 15 deletions tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tc
import (
"context"
"fmt"
"log"
"time"

"github.com/florianl/go-tc/internal/unix"
Expand All @@ -27,8 +26,6 @@ var _ tcConn = &netlink.Conn{}
// Tc represents a RTNETLINK wrapper
type Tc struct {
con tcConn

logger *log.Logger
}

var nativeEndian = native.Endian
Expand All @@ -47,12 +44,6 @@ func Open(config *Config) (*Tc, error) {
}
tc.con = con

if config.Logger == nil {
tc.logger = setDummyLogger()
} else {
tc.logger = config.Logger
}

return &tc, nil
}

Expand Down Expand Up @@ -107,9 +98,15 @@ func (tc *Tc) action(action int, flags netlink.HeaderFlags, msg interface{}, opt
}

for _, msg := range msgs {
if msg.Header.Type == netlink.Error {
// see https://www.infradead.org/~tgr/libnl/doc/core.html#core_errmsg
tc.logger.Printf("received netlink.Error in action()\n")
switch msg.Header.Type {
case netlink.Error:
errCode := bytesToInt32(msg.Data[:4])
// Check if the sucess message is embeded encoded as error code 0:
if errCode != 0 {
return fmt.Errorf("received error from netlink: %#v", msg)
}
case netlink.Overrun:
return fmt.Errorf("lost netlink data: %#v", msg)
}
}

Expand Down Expand Up @@ -294,7 +291,6 @@ func (tc *Tc) Monitor(ctx context.Context, deadline time.Duration, fn HookFunc)
return 0
}
}
tc.logger.Printf("Could not receive message: %v\n", err)
return 1
})
}
Expand Down Expand Up @@ -362,12 +358,10 @@ func (tc *Tc) monitor(ctx context.Context, deadline time.Duration,
for _, msg := range msgs {
var monitored Object
if err := unmarshalStruct(msg.Data[:20], &monitored.Msg); err != nil {
tc.logger.Printf("could not extract tc.Msg from %v\n", msg.Data[:20])
continue
}
if err := extractTcmsgAttributes(int(msg.Header.Type), msg.Data[20:],
&monitored.Attribute); err != nil {
tc.logger.Printf("could not extract attributes from %v\n", msg.Data[20:36])
continue
}
if fn(uint16(msg.Header.Type), monitored) != 0 {
Expand Down
13 changes: 0 additions & 13 deletions tc_gteq_1.16.go

This file was deleted.

12 changes: 0 additions & 12 deletions tc_linux_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package tc
import (
"context"
"fmt"
"log"
"net"
"os"
"testing"
Expand Down Expand Up @@ -161,17 +160,6 @@ func TestSocket(t *testing.T) {
t.Fatalf("failed to close test socket: %v", err)
}
})
t.Run("with logger", func(t *testing.T) {
tc, err := Open(&Config{
Logger: log.Default(),
})
if err != nil {
t.Fatalf("failed to open netlink socket: %v", err)
}
if err = tc.Close(); err != nil {
t.Fatalf("failed to close test socket: %v", err)
}
})
}

func TestMonitorWithErrorFunc(t *testing.T) {
Expand Down
17 changes: 0 additions & 17 deletions tc_lt_1.16.go

This file was deleted.

Loading