github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/stack/nic_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 stack 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/SagerNet/gvisor/pkg/tcpip" 22 "github.com/SagerNet/gvisor/pkg/tcpip/buffer" 23 "github.com/SagerNet/gvisor/pkg/tcpip/header" 24 "github.com/SagerNet/gvisor/pkg/tcpip/testutil" 25 ) 26 27 var _ AddressableEndpoint = (*testIPv6Endpoint)(nil) 28 var _ NetworkEndpoint = (*testIPv6Endpoint)(nil) 29 var _ NDPEndpoint = (*testIPv6Endpoint)(nil) 30 31 // An IPv6 NetworkEndpoint that throws away outgoing packets. 32 // 33 // We use this instead of ipv6.endpoint because the ipv6 package depends on 34 // the stack package which this test lives in, causing a cyclic dependency. 35 type testIPv6Endpoint struct { 36 AddressableEndpointState 37 38 nic NetworkInterface 39 protocol *testIPv6Protocol 40 41 invalidatedRtr tcpip.Address 42 } 43 44 func (*testIPv6Endpoint) Enable() tcpip.Error { 45 return nil 46 } 47 48 func (*testIPv6Endpoint) Enabled() bool { 49 return true 50 } 51 52 func (*testIPv6Endpoint) Disable() {} 53 54 // DefaultTTL implements NetworkEndpoint.DefaultTTL. 55 func (*testIPv6Endpoint) DefaultTTL() uint8 { 56 return 0 57 } 58 59 // MTU implements NetworkEndpoint.MTU. 60 func (e *testIPv6Endpoint) MTU() uint32 { 61 return e.nic.MTU() - header.IPv6MinimumSize 62 } 63 64 // MaxHeaderLength implements NetworkEndpoint.MaxHeaderLength. 65 func (e *testIPv6Endpoint) MaxHeaderLength() uint16 { 66 return e.nic.MaxHeaderLength() + header.IPv6MinimumSize 67 } 68 69 // WritePacket implements NetworkEndpoint.WritePacket. 70 func (*testIPv6Endpoint) WritePacket(*Route, NetworkHeaderParams, *PacketBuffer) tcpip.Error { 71 return nil 72 } 73 74 // WritePackets implements NetworkEndpoint.WritePackets. 75 func (*testIPv6Endpoint) WritePackets(*Route, PacketBufferList, NetworkHeaderParams) (int, tcpip.Error) { 76 // Our tests don't use this so we don't support it. 77 return 0, &tcpip.ErrNotSupported{} 78 } 79 80 // WriteHeaderIncludedPacket implements 81 // NetworkEndpoint.WriteHeaderIncludedPacket. 82 func (*testIPv6Endpoint) WriteHeaderIncludedPacket(*Route, *PacketBuffer) tcpip.Error { 83 // Our tests don't use this so we don't support it. 84 return &tcpip.ErrNotSupported{} 85 } 86 87 // HandlePacket implements NetworkEndpoint.HandlePacket. 88 func (*testIPv6Endpoint) HandlePacket(*PacketBuffer) {} 89 90 // Close implements NetworkEndpoint.Close. 91 func (e *testIPv6Endpoint) Close() { 92 e.AddressableEndpointState.Cleanup() 93 } 94 95 // NetworkProtocolNumber implements NetworkEndpoint.NetworkProtocolNumber. 96 func (*testIPv6Endpoint) NetworkProtocolNumber() tcpip.NetworkProtocolNumber { 97 return header.IPv6ProtocolNumber 98 } 99 100 func (e *testIPv6Endpoint) InvalidateDefaultRouter(rtr tcpip.Address) { 101 e.invalidatedRtr = rtr 102 } 103 104 // Stats implements NetworkEndpoint. 105 func (*testIPv6Endpoint) Stats() NetworkEndpointStats { 106 return &testIPv6EndpointStats{} 107 } 108 109 var _ NetworkEndpointStats = (*testIPv6EndpointStats)(nil) 110 111 type testIPv6EndpointStats struct{} 112 113 // IsNetworkEndpointStats implements stack.NetworkEndpointStats. 114 func (*testIPv6EndpointStats) IsNetworkEndpointStats() {} 115 116 // We use this instead of ipv6.protocol because the ipv6 package depends on 117 // the stack package which this test lives in, causing a cyclic dependency. 118 type testIPv6Protocol struct{} 119 120 // Number implements NetworkProtocol.Number. 121 func (*testIPv6Protocol) Number() tcpip.NetworkProtocolNumber { 122 return header.IPv6ProtocolNumber 123 } 124 125 // MinimumPacketSize implements NetworkProtocol.MinimumPacketSize. 126 func (*testIPv6Protocol) MinimumPacketSize() int { 127 return header.IPv6MinimumSize 128 } 129 130 // DefaultPrefixLen implements NetworkProtocol.DefaultPrefixLen. 131 func (*testIPv6Protocol) DefaultPrefixLen() int { 132 return header.IPv6AddressSize * 8 133 } 134 135 // ParseAddresses implements NetworkProtocol.ParseAddresses. 136 func (*testIPv6Protocol) ParseAddresses(v buffer.View) (src, dst tcpip.Address) { 137 h := header.IPv6(v) 138 return h.SourceAddress(), h.DestinationAddress() 139 } 140 141 // NewEndpoint implements NetworkProtocol.NewEndpoint. 142 func (p *testIPv6Protocol) NewEndpoint(nic NetworkInterface, _ TransportDispatcher) NetworkEndpoint { 143 e := &testIPv6Endpoint{ 144 nic: nic, 145 protocol: p, 146 } 147 e.AddressableEndpointState.Init(e) 148 return e 149 } 150 151 // SetOption implements NetworkProtocol.SetOption. 152 func (*testIPv6Protocol) SetOption(tcpip.SettableNetworkProtocolOption) tcpip.Error { 153 return nil 154 } 155 156 // Option implements NetworkProtocol.Option. 157 func (*testIPv6Protocol) Option(tcpip.GettableNetworkProtocolOption) tcpip.Error { 158 return nil 159 } 160 161 // Close implements NetworkProtocol.Close. 162 func (*testIPv6Protocol) Close() {} 163 164 // Wait implements NetworkProtocol.Wait. 165 func (*testIPv6Protocol) Wait() {} 166 167 // Parse implements NetworkProtocol.Parse. 168 func (*testIPv6Protocol) Parse(*PacketBuffer) (tcpip.TransportProtocolNumber, bool, bool) { 169 return 0, false, false 170 } 171 172 func TestDisabledRxStatsWhenNICDisabled(t *testing.T) { 173 // When the NIC is disabled, the only field that matters is the stats field. 174 // This test is limited to stats counter checks. 175 nic := nic{ 176 stats: makeNICStats(tcpip.NICStats{}.FillIn()), 177 } 178 179 if got := nic.stats.local.DisabledRx.Packets.Value(); got != 0 { 180 t.Errorf("got DisabledRx.Packets = %d, want = 0", got) 181 } 182 if got := nic.stats.local.DisabledRx.Bytes.Value(); got != 0 { 183 t.Errorf("got DisabledRx.Bytes = %d, want = 0", got) 184 } 185 if got := nic.stats.local.Rx.Packets.Value(); got != 0 { 186 t.Errorf("got Rx.Packets = %d, want = 0", got) 187 } 188 if got := nic.stats.local.Rx.Bytes.Value(); got != 0 { 189 t.Errorf("got Rx.Bytes = %d, want = 0", got) 190 } 191 192 if t.Failed() { 193 t.FailNow() 194 } 195 196 nic.DeliverNetworkPacket("", "", 0, NewPacketBuffer(PacketBufferOptions{ 197 Data: buffer.View([]byte{1, 2, 3, 4}).ToVectorisedView(), 198 })) 199 200 if got := nic.stats.local.DisabledRx.Packets.Value(); got != 1 { 201 t.Errorf("got DisabledRx.Packets = %d, want = 1", got) 202 } 203 if got := nic.stats.local.DisabledRx.Bytes.Value(); got != 4 { 204 t.Errorf("got DisabledRx.Bytes = %d, want = 4", got) 205 } 206 if got := nic.stats.local.Rx.Packets.Value(); got != 0 { 207 t.Errorf("got Rx.Packets = %d, want = 0", got) 208 } 209 if got := nic.stats.local.Rx.Bytes.Value(); got != 0 { 210 t.Errorf("got Rx.Bytes = %d, want = 0", got) 211 } 212 } 213 214 func TestMultiCounterStatsInitialization(t *testing.T) { 215 global := tcpip.NICStats{}.FillIn() 216 nic := nic{ 217 stats: makeNICStats(global), 218 } 219 multi := nic.stats.multiCounterNICStats 220 local := nic.stats.local 221 if err := testutil.ValidateMultiCounterStats(reflect.ValueOf(&multi).Elem(), []reflect.Value{reflect.ValueOf(&local).Elem(), reflect.ValueOf(&global).Elem()}); err != nil { 222 t.Error(err) 223 } 224 }