Skip to content

Commit

Permalink
Use netip.Addr instead of net.IP
Browse files Browse the repository at this point in the history
Using the net/netip package instead of the net package can help reduce
the memory footprint of the library and help reduce the number of
heap allocations.

This is a breaking change for consumers for the libray as exported types
are updated to use fields of type netip.Addr instead of net.IP.

Fixes ti-mo#35

Signed-off-by: Antonin Bas <abas@vmware.com>
  • Loading branch information
antoninbas committed Oct 10, 2023
1 parent 8a9bdf7 commit 31ad178
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 158 deletions.
16 changes: 8 additions & 8 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package conntrack_test
import (
"fmt"
"log"
"net"
"net/netip"
"testing"

"github.com/mdlayher/netlink"
Expand Down Expand Up @@ -34,8 +34,8 @@ func ExampleConn_createUpdateFlow() {
// Set up a new Flow object using a given set of attributes.
f := conntrack.NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
netip.MustParseAddr("2a00:1450:400e:804::200e"),
netip.MustParseAddr("2a00:1450:400e:804::200f"),
1234, 80, 120, 0,
)

Expand Down Expand Up @@ -72,12 +72,12 @@ func ExampleConn_dumpFilter() {
}

f1 := conntrack.NewFlow(
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"),
1234, 80, 120, 0x00ff, // Set a connection mark
)

f2 := conntrack.NewFlow(
17, 0, net.ParseIP("2a00:1450:400e:804::200e"), net.ParseIP("2a00:1450:400e:804::200f"),
17, 0, netip.MustParseAddr("2a00:1450:400e:804::200e"), netip.MustParseAddr("2a00:1450:400e:804::200f"),
1234, 80, 120, 0xff00, // Set a connection mark
)

Expand Down Expand Up @@ -116,12 +116,12 @@ func ExampleConn_flushFilter() {
}

f1 := conntrack.NewFlow(
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"),
1234, 80, 120, 0x00ff, // Set a connection mark
)

f2 := conntrack.NewFlow(
17, 0, net.ParseIP("2a00:1450:400e:804::200e"), net.ParseIP("2a00:1450:400e:804::200f"),
17, 0, netip.MustParseAddr("2a00:1450:400e:804::200e"), netip.MustParseAddr("2a00:1450:400e:804::200f"),
1234, 80, 120, 0xff00, // Set a connection mark
)

Expand Down Expand Up @@ -155,7 +155,7 @@ func ExampleConn_delete() {
}

f := conntrack.NewFlow(
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"),
1234, 80, 120, 0,
)

Expand Down
4 changes: 2 additions & 2 deletions event_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package conntrack

import (
"net"
"net/netip"
"testing"

"github.com/mdlayher/netlink"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestConnListen(t *testing.T) {

var warn bool

ip := net.ParseIP("::f00")
ip := netip.MustParseAddr("::f00")
for _, proto := range []uint8{unix.IPPROTO_TCP, unix.IPPROTO_UDP, unix.IPPROTO_DCCP, unix.IPPROTO_SCTP} {
// Create the Flow.
f := NewFlow(
Expand Down
12 changes: 6 additions & 6 deletions expect_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package conntrack

import (
"net"
"net/netip"
"testing"

"golang.org/x/sys/unix"
Expand All @@ -27,7 +27,7 @@ func TestConnCreateExpect(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)

f := NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 42000, 21, 120, 0)
f := NewFlow(6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 42000, 21, 120, 0)

err = c.Create(f)
require.NoError(t, err, "unexpected error creating flow", f)
Expand All @@ -37,8 +37,8 @@ func TestConnCreateExpect(t *testing.T) {
TupleMaster: f.TupleOrig,
Tuple: Tuple{
IP: IPTuple{
SourceAddress: net.IPv4(1, 2, 3, 4),
DestinationAddress: net.IPv4(5, 6, 7, 8),
SourceAddress: netip.MustParseAddr("1.2.3.4"),
DestinationAddress: netip.MustParseAddr("5.6.7.8"),
},
Proto: ProtoTuple{
Protocol: 6,
Expand All @@ -48,8 +48,8 @@ func TestConnCreateExpect(t *testing.T) {
},
Mask: Tuple{
IP: IPTuple{
SourceAddress: net.IPv4(255, 255, 255, 255),
DestinationAddress: net.IPv4(255, 255, 255, 255),
SourceAddress: netip.MustParseAddr("255.255.255.255"),
DestinationAddress: netip.MustParseAddr("255.255.255.255"),
},
Proto: ProtoTuple{
Protocol: 6,
Expand Down
27 changes: 13 additions & 14 deletions expect_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package conntrack

import (
"net"
"net/netip"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ti-mo/netfilter"
Expand Down Expand Up @@ -183,8 +182,8 @@ var corpusExpect = []struct {
exp: Expect{
TupleMaster: Tuple{
IP: IPTuple{
SourceAddress: []byte{127, 0, 0, 1},
DestinationAddress: []byte{127, 0, 0, 2},
SourceAddress: netip.MustParseAddr("127.0.0.1"),
DestinationAddress: netip.MustParseAddr("127.0.0.2"),
},
Proto: ProtoTuple{
Protocol: 6,
Expand All @@ -194,8 +193,8 @@ var corpusExpect = []struct {
},
Tuple: Tuple{
IP: IPTuple{
SourceAddress: []byte{127, 0, 0, 1},
DestinationAddress: []byte{127, 0, 0, 2},
SourceAddress: netip.MustParseAddr("127.0.0.1"),
DestinationAddress: netip.MustParseAddr("127.0.0.2"),
},
Proto: ProtoTuple{
Protocol: 6,
Expand All @@ -204,8 +203,8 @@ var corpusExpect = []struct {
},
Mask: Tuple{
IP: IPTuple{
SourceAddress: []byte{255, 255, 255, 255},
DestinationAddress: []byte{255, 255, 255, 255},
SourceAddress: netip.MustParseAddr("255.255.255.255"),
DestinationAddress: netip.MustParseAddr("255.255.255.255"),
},
Proto: ProtoTuple{
Protocol: 6,
Expand Down Expand Up @@ -265,7 +264,7 @@ func TestExpectUnmarshal(t *testing.T) {
var ex Expect
assert.NoError(t, ex.unmarshal(mustDecodeAttributes(tt.attrs)))

if diff := cmp.Diff(tt.exp, ex); diff != "" {
if diff := Diff(tt.exp, ex); diff != "" {
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
}
})
Expand Down Expand Up @@ -355,7 +354,7 @@ func TestExpectMarshal(t *testing.T) {
},
}

if diff := cmp.Diff(want, exm); diff != "" {
if diff := Diff(want, exm); diff != "" {
t.Fatalf("unexpected Expect marshal (-want +got):\n%s", diff)
}

Expand Down Expand Up @@ -425,7 +424,7 @@ func TestExpectNATUnmarshal(t *testing.T) {

require.NoError(t, err)

if diff := cmp.Diff(tt.enat, enat); diff != "" {
if diff := Diff(tt.enat, enat); diff != "" {
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
}
})
Expand All @@ -439,8 +438,8 @@ func TestExpectNATMarshal(t *testing.T) {
Direction: true,
Tuple: Tuple{
IP: IPTuple{
SourceAddress: net.ParseIP("baa:baa::b"),
DestinationAddress: net.ParseIP("ef00:3f00::ba13"),
SourceAddress: netip.MustParseAddr("baa:baa::b"),
DestinationAddress: netip.MustParseAddr("ef00:3f00::ba13"),
},
Proto: ProtoTuple{
Protocol: 13,
Expand All @@ -458,7 +457,7 @@ func TestExpectNATMarshal(t *testing.T) {

// Only verify first attribute (direction); Tuple marshal has its own tests
want := netfilter.Attribute{Type: uint16(ctaExpectNATDir), Data: []byte{0, 0, 0, 1}}
if diff := cmp.Diff(want, enm.Children[0]); diff != "" {
if diff := Diff(want, enm.Children[0]); diff != "" {
t.Fatalf("unexpected ExpectNAT marshal (-want +got):\n%s", diff)
}
}
Expand Down
4 changes: 1 addition & 3 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

"github.com/ti-mo/netfilter"

"github.com/google/go-cmp/cmp"
)

func TestFilterMarshal(t *testing.T) {
Expand All @@ -22,7 +20,7 @@ func TestFilterMarshal(t *testing.T) {
},
}

if diff := cmp.Diff(fm, f.marshal()); diff != "" {
if diff := Diff(fm, f.marshal()); diff != "" {
t.Fatalf("unexpected Filter marshal (-want +got):\n%s", diff)
}
}
4 changes: 2 additions & 2 deletions flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package conntrack

import (
"fmt"
"net"
"net/netip"

"github.com/mdlayher/netlink"
"github.com/ti-mo/netfilter"
Expand Down Expand Up @@ -44,7 +44,7 @@ type Flow struct {
// source and destination addresses. srcPort and dstPort are the source and
// destination ports. timeout is the non-zero time-to-live of a connection in
// seconds.
func NewFlow(proto uint8, status StatusFlag, srcAddr, destAddr net.IP,
func NewFlow(proto uint8, status StatusFlag, srcAddr, destAddr netip.Addr,
srcPort, destPort uint16, timeout, mark uint32) Flow {

var f Flow
Expand Down
56 changes: 28 additions & 28 deletions flow_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package conntrack

import (
"net"
"net/netip"
"testing"

"golang.org/x/sys/unix"
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestConnCreateFlows(t *testing.T) {

// Create IPv4 flows
for i := 1; i <= numFlows; i++ {
f = NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 1234, uint16(i), 120, 0)
f = NewFlow(6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 1234, uint16(i), 120, 0)

err = c.Create(f)
require.NoError(t, err, "creating IPv4 flow", i)
Expand All @@ -45,8 +45,8 @@ func TestConnCreateFlows(t *testing.T) {
for i := 1; i <= numFlows; i++ {
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
netip.MustParseAddr("2a00:1450:400e:804::200e"),
netip.MustParseAddr("2a00:1450:400e:804::200f"),
1234, uint16(i), 120, 0,
))
require.NoError(t, err, "creating IPv6 flow", i)
Expand Down Expand Up @@ -81,17 +81,17 @@ func TestConnFlush(t *testing.T) {
// Create IPv4 flow
err = c.Create(NewFlow(
6, 0,
net.IPv4(1, 2, 3, 4),
net.IPv4(5, 6, 7, 8),
netip.MustParseAddr("1.2.3.4"),
netip.MustParseAddr("5.6.7.8"),
1234, 80, 120, 0,
))
require.NoError(t, err, "creating IPv4 flow")

// Create IPv6 flow
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
netip.MustParseAddr("2a00:1450:400e:804::200e"),
netip.MustParseAddr("2a00:1450:400e:804::200f"),
1234, 80, 120, 0,
))
require.NoError(t, err, "creating IPv6 flow")
Expand Down Expand Up @@ -130,17 +130,17 @@ func TestConnFlushFilter(t *testing.T) {
// Create IPv4 flow
err = c.Create(NewFlow(
6, 0,
net.IPv4(1, 2, 3, 4),
net.IPv4(5, 6, 7, 8),
netip.MustParseAddr("1.2.3.4"),
netip.MustParseAddr("5.6.7.8"),
1234, 80, 120, 0,
))
require.NoError(t, err, "creating IPv4 flow")

// Create IPv6 flow with mark
err = c.Create(NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
netip.MustParseAddr("2a00:1450:400e:804::200e"),
netip.MustParseAddr("2a00:1450:400e:804::200f"),
1234, 80, 120, 0xff00,
))
require.NoError(t, err, "creating IPv6 flow")
Expand Down Expand Up @@ -174,8 +174,8 @@ func TestConnCreateDeleteFlows(t *testing.T) {
for i := 1; i <= numFlows; i++ {
f = NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::223e"),
net.ParseIP("2a00:1450:400e:804::223f"),
netip.MustParseAddr("2a00:1450:400e:804::223e"),
netip.MustParseAddr("2a00:1450:400e:804::223f"),
1234, uint16(i), 120, 0,
)

Expand All @@ -199,8 +199,8 @@ func TestConnCreateUpdateFlow(t *testing.T) {

f := NewFlow(
17, 0,
net.ParseIP("1.2.3.4"),
net.ParseIP("5.6.7.8"),
netip.MustParseAddr("1.2.3.4"),
netip.MustParseAddr("5.6.7.8"),
1234, 5678, 120, 0,
)

Expand Down Expand Up @@ -262,8 +262,8 @@ func TestConnUpdateError(t *testing.T) {

f := NewFlow(
17, 0,
net.ParseIP("1.2.3.4"),
net.ParseIP("5.6.7.8"),
netip.MustParseAddr("1.2.3.4"),
netip.MustParseAddr("5.6.7.8"),
1234, 5678, 120, 0,
)

Expand All @@ -280,10 +280,10 @@ func TestConnCreateGetFlow(t *testing.T) {
require.NoError(t, err)

flows := map[string]Flow{
"v4m1": NewFlow(17, 0, net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8"), 1234, 5678, 120, 0),
"v4m2": NewFlow(17, 0, net.ParseIP("10.0.0.1"), net.ParseIP("10.0.0.2"), 24000, 80, 120, 0),
"v6m1": NewFlow(17, 0, net.ParseIP("2a12:1234:200f:600::200a"), net.ParseIP("2a12:1234:200f:600::200b"), 6554, 53, 120, 0),
"v6m2": NewFlow(17, 0, net.ParseIP("900d:f00d:24::7"), net.ParseIP("baad:beef:b00::b00"), 1323, 22, 120, 0),
"v4m1": NewFlow(17, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 1234, 5678, 120, 0),
"v4m2": NewFlow(17, 0, netip.MustParseAddr("10.0.0.1"), netip.MustParseAddr("10.0.0.2"), 24000, 80, 120, 0),
"v6m1": NewFlow(17, 0, netip.MustParseAddr("2a12:1234:200f:600::200a"), netip.MustParseAddr("2a12:1234:200f:600::200b"), 6554, 53, 120, 0),
"v6m2": NewFlow(17, 0, netip.MustParseAddr("900d:f00d:24::7"), netip.MustParseAddr("baad:beef:b00::b00"), 1323, 22, 120, 0),
}

for n, f := range flows {
Expand All @@ -306,7 +306,7 @@ func TestDumpZero(t *testing.T) {
c, _, err := makeNSConn()
require.NoError(t, err)

f := NewFlow(17, 0, net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8"), 1234, 5678, 120, 0xff000000)
f := NewFlow(17, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 1234, 5678, 120, 0xff000000)

f.CountersOrig.Bytes = 1337
f.CountersReply.Bytes = 9001
Expand All @@ -332,10 +332,10 @@ func TestConnDumpFilter(t *testing.T) {
require.NoError(t, err)

flows := map[string]Flow{
"v4m1": NewFlow(17, 0, net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8"), 1234, 5678, 120, 0xff000000),
"v4m2": NewFlow(17, 0, net.ParseIP("10.0.0.1"), net.ParseIP("10.0.0.2"), 24000, 80, 120, 0x00ff0000),
"v6m1": NewFlow(17, 0, net.ParseIP("2a12:1234:200f:600::200a"), net.ParseIP("2a12:1234:200f:600::200b"), 6554, 53, 120, 0x0000ff00),
"v6m2": NewFlow(17, 0, net.ParseIP("900d:f00d:24::7"), net.ParseIP("baad:beef:b00::b00"), 1323, 22, 120, 0x000000ff),
"v4m1": NewFlow(17, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 1234, 5678, 120, 0xff000000),
"v4m2": NewFlow(17, 0, netip.MustParseAddr("10.0.0.1"), netip.MustParseAddr("10.0.0.2"), 24000, 80, 120, 0x00ff0000),
"v6m1": NewFlow(17, 0, netip.MustParseAddr("2a12:1234:200f:600::200a"), netip.MustParseAddr("2a12:1234:200f:600::200b"), 6554, 53, 120, 0x0000ff00),
"v6m2": NewFlow(17, 0, netip.MustParseAddr("900d:f00d:24::7"), netip.MustParseAddr("baad:beef:b00::b00"), 1323, 22, 120, 0x000000ff),
}

// Expect empty result from empty table dump
Expand Down Expand Up @@ -372,7 +372,7 @@ func BenchmarkCreateDeleteFlow(b *testing.B) {
b.Fatal(err)
}

f := NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 1234, 80, 120, 0)
f := NewFlow(6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 1234, 80, 120, 0)

for n := 0; n < b.N; n++ {
err = c.Create(f)
Expand Down
Loading

0 comments on commit 31ad178

Please sign in to comment.