gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/network/ipv4/stats_test.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ipv4 16 17 import ( 18 "reflect" 19 "testing" 20 21 "gvisor.dev/gvisor/pkg/tcpip" 22 "gvisor.dev/gvisor/pkg/tcpip/stack" 23 "gvisor.dev/gvisor/pkg/tcpip/testutil" 24 ) 25 26 var _ stack.NetworkInterface = (*testInterface)(nil) 27 28 type testInterface struct { 29 stack.NetworkInterface 30 nicID tcpip.NICID 31 } 32 33 func (t *testInterface) ID() tcpip.NICID { 34 return t.nicID 35 } 36 37 // +checklocks:proto.mu 38 func knownNICIDs(proto *protocol) []tcpip.NICID { 39 var nicIDs []tcpip.NICID 40 41 for k := range proto.eps { 42 nicIDs = append(nicIDs, k) 43 } 44 45 return nicIDs 46 } 47 48 type statsTestContext struct { 49 s *stack.Stack 50 } 51 52 func newStatsTestContext() statsTestContext { 53 s := stack.New(stack.Options{ 54 NetworkProtocols: []stack.NetworkProtocolFactory{NewProtocol}, 55 }) 56 return statsTestContext{s: s} 57 } 58 59 func (ctx statsTestContext) cleanup() { 60 ctx.s.Close() 61 ctx.s.Wait() 62 } 63 64 func TestClearEndpointFromProtocolOnClose(t *testing.T) { 65 ctx := newStatsTestContext() 66 defer ctx.cleanup() 67 s := ctx.s 68 69 proto := s.NetworkProtocolInstance(ProtocolNumber).(*protocol) 70 nic := testInterface{nicID: 1} 71 ep := proto.NewEndpoint(&nic, nil).(*endpoint) 72 var nicIDs []tcpip.NICID 73 74 proto.mu.Lock() 75 foundEP, hasEndpointBeforeClose := proto.eps[nic.ID()] 76 nicIDs = knownNICIDs(proto) 77 proto.mu.Unlock() 78 79 if !hasEndpointBeforeClose { 80 t.Fatalf("expected to find the nic id %d in the protocol's endpoint map (%v)", nic.ID(), nicIDs) 81 } 82 if foundEP != ep { 83 t.Fatalf("found an incorrect endpoint mapped to nic id %d", nic.ID()) 84 } 85 86 ep.Close() 87 88 proto.mu.Lock() 89 _, hasEP := proto.eps[nic.ID()] 90 nicIDs = knownNICIDs(proto) 91 proto.mu.Unlock() 92 if hasEP { 93 t.Fatalf("unexpectedly found an endpoint mapped to the nic id %d in the protocol's known nic ids (%v)", nic.ID(), nicIDs) 94 } 95 } 96 97 func TestMultiCounterStatsInitialization(t *testing.T) { 98 ctx := newStatsTestContext() 99 defer ctx.cleanup() 100 s := ctx.s 101 102 proto := s.NetworkProtocolInstance(ProtocolNumber).(*protocol) 103 var nic testInterface 104 ep := proto.NewEndpoint(&nic, nil).(*endpoint) 105 // At this point, the Stack's stats and the NetworkEndpoint's stats are 106 // expected to be bound by a MultiCounterStat. 107 refStack := s.Stats() 108 refEP := ep.stats.localStats 109 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&ep.stats.ip).Elem(), []reflect.Value{reflect.ValueOf(&refEP.IP).Elem(), reflect.ValueOf(&refStack.IP).Elem()}, testutil.ValidateMultiCounterStatsOptions{ 110 ExpectMultiCounterStat: true, 111 ExpectMultiIntegralStatCounterMap: false, 112 }); err != nil { 113 t.Error(err) 114 } 115 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&ep.stats.icmp).Elem(), []reflect.Value{reflect.ValueOf(&refEP.ICMP).Elem(), reflect.ValueOf(&refStack.ICMP.V4).Elem()}, testutil.ValidateMultiCounterStatsOptions{ 116 ExpectMultiCounterStat: true, 117 ExpectMultiIntegralStatCounterMap: false, 118 }); err != nil { 119 t.Error(err) 120 } 121 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&ep.stats.igmp).Elem(), []reflect.Value{reflect.ValueOf(&refEP.IGMP).Elem(), reflect.ValueOf(&refStack.IGMP).Elem()}, testutil.ValidateMultiCounterStatsOptions{ 122 ExpectMultiCounterStat: true, 123 ExpectMultiIntegralStatCounterMap: false, 124 }); err != nil { 125 t.Error(err) 126 } 127 }