summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/stack/stack_test.go
diff options
context:
space:
mode:
authorBert Muthalaly <stijlist@google.com>2019-03-19 08:29:37 -0700
committerShentubot <shentubot@google.com>2019-03-19 08:30:43 -0700
commit928809fa7d3b682c7e2e06c9f9b1f3fb5d75a0d6 (patch)
tree33e1e1c21be68ffaeeb83cfb86f29af512515058 /pkg/tcpip/stack/stack_test.go
parent8a499ae65f361fb01c2e4be03122f69910a8ba4a (diff)
Add layer 2 stats (tx, rx) X (packets, bytes) to netstack
PiperOrigin-RevId: 239194420 Change-Id: Ie193e8ac2b7a6db21195ac85824a335930483971
Diffstat (limited to 'pkg/tcpip/stack/stack_test.go')
-rw-r--r--pkg/tcpip/stack/stack_test.go58
1 files changed, 52 insertions, 6 deletions
diff --git a/pkg/tcpip/stack/stack_test.go b/pkg/tcpip/stack/stack_test.go
index b366de21d..da8269999 100644
--- a/pkg/tcpip/stack/stack_test.go
+++ b/pkg/tcpip/stack/stack_test.go
@@ -273,7 +273,7 @@ func TestNetworkReceive(t *testing.T) {
}
}
-func sendTo(t *testing.T, s *stack.Stack, addr tcpip.Address) {
+func sendTo(t *testing.T, s *stack.Stack, addr tcpip.Address, payload buffer.View) {
r, err := s.FindRoute(0, "", addr, fakeNetNumber, false /* multicastLoop */)
if err != nil {
t.Fatalf("FindRoute failed: %v", err)
@@ -281,9 +281,8 @@ func sendTo(t *testing.T, s *stack.Stack, addr tcpip.Address) {
defer r.Release()
hdr := buffer.NewPrependable(int(r.MaxHeaderLength()))
- if err := r.WritePacket(hdr, buffer.VectorisedView{}, fakeTransNumber, 123); err != nil {
+ if err := r.WritePacket(hdr, payload.ToVectorisedView(), fakeTransNumber, 123); err != nil {
t.Errorf("WritePacket failed: %v", err)
- return
}
}
@@ -304,7 +303,7 @@ func TestNetworkSend(t *testing.T) {
}
// Make sure that the link-layer endpoint received the outbound packet.
- sendTo(t, s, "\x03")
+ sendTo(t, s, "\x03", nil)
if c := linkEP.Drain(); c != 1 {
t.Errorf("packetCount = %d, want %d", c, 1)
}
@@ -351,14 +350,14 @@ func TestNetworkSendMultiRoute(t *testing.T) {
})
// Send a packet to an odd destination.
- sendTo(t, s, "\x05")
+ sendTo(t, s, "\x05", nil)
if c := linkEP1.Drain(); c != 1 {
t.Errorf("packetCount = %d, want %d", c, 1)
}
// Send a packet to an even destination.
- sendTo(t, s, "\x06")
+ sendTo(t, s, "\x06", nil)
if c := linkEP2.Drain(); c != 1 {
t.Errorf("packetCount = %d, want %d", c, 1)
@@ -1055,6 +1054,44 @@ func TestGetMainNICAddressAddRemove(t *testing.T) {
}
}
+func TestNICStats(t *testing.T) {
+ s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
+ id1, linkEP1 := channel.New(10, defaultMTU, "")
+ if err := s.CreateNIC(1, id1); err != nil {
+ t.Fatalf("CreateNIC failed: %v", err)
+ }
+ if err := s.AddAddress(1, fakeNetNumber, "\x01"); err != nil {
+ t.Fatalf("AddAddress failed: %v", err)
+ }
+ // Route all packets for address \x01 to NIC 1.
+ s.SetRouteTable([]tcpip.Route{
+ {"\x01", "\xff", "\x00", 1},
+ })
+
+ // Send a packet to address 1.
+ buf := buffer.NewView(30)
+ linkEP1.Inject(fakeNetNumber, buf.ToVectorisedView())
+ if got, want := s.NICInfo()[1].Stats.Rx.Packets.Value(), uint64(1); got != want {
+ t.Errorf("got Rx.Packets.Value() = %d, want = %d", got, want)
+ }
+
+ if got, want := s.NICInfo()[1].Stats.Rx.Bytes.Value(), uint64(len(buf)); got != want {
+ t.Errorf("got Rx.Bytes.Value() = %d, want = %d", got, want)
+ }
+
+ payload := buffer.NewView(10)
+ // Write a packet out via the address for NIC 1
+ sendTo(t, s, "\x01", payload)
+ want := uint64(linkEP1.Drain())
+ if got := s.NICInfo()[1].Stats.Tx.Packets.Value(); got != want {
+ t.Errorf("got Tx.Packets.Value() = %d, linkEP1.Drain() = %d", got, want)
+ }
+
+ if got, want := s.NICInfo()[1].Stats.Tx.Bytes.Value(), uint64(len(payload)); got != want {
+ t.Errorf("got Tx.Bytes.Value() = %d, want = %d", got, want)
+ }
+}
+
func TestNICForwarding(t *testing.T) {
// Create a stack with the fake network protocol, two NICs, each with
// an address.
@@ -1092,6 +1129,15 @@ func TestNICForwarding(t *testing.T) {
default:
t.Fatal("Packet not forwarded")
}
+
+ // Test that forwarding increments Tx stats correctly.
+ if got, want := s.NICInfo()[2].Stats.Tx.Packets.Value(), uint64(1); got != want {
+ t.Errorf("got Tx.Packets.Value() = %d, want = %d", got, want)
+ }
+
+ if got, want := s.NICInfo()[2].Stats.Tx.Bytes.Value(), uint64(len(buf)); got != want {
+ t.Errorf("got Tx.Bytes.Value() = %d, want = %d", got, want)
+ }
}
func init() {