Debian Codegraph @

tcpip/tcpip.go

raw symbols SCIP
1 // Copyright 2018 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 tcpip provides the interfaces and related types that users of the
16 // tcpip stack will use in order to create endpoints used to send and receive
17 // data over the network stack.
18 //
19 // The starting point is the creation and configuration of a stack. A stack can
20 // be created by calling the New() function of the tcpip/stack/stack package;
21 // configuring a stack involves creating NICs (via calls to Stack.CreateNIC()),
22 // adding network addresses (via calls to Stack.AddProtocolAddress()), and
23 // setting a route table (via a call to Stack.SetRouteTable()).
24 //
25 // Once a stack is configured, endpoints can be created by calling
26 // Stack.NewEndpoint(). Such endpoints can be used to send/receive data, connect
27 // to peers, listen for connections, accept connections, etc., depending on the
28 // transport protocol selected.
29 package tcpip
30
31 import (
32 "bytes"
33 "errors"
34 "fmt"
35 "io"
36 "math/bits"
37 "reflect"
38 "strconv"
39 "strings"
40 "time"
41
42 "inet.af/netstack/atomicbitops"
43 "inet.af/netstack/sync"
44 "inet.af/netstack/waiter"
45 )
46
47 // Using header.IPv4AddressSize would cause an import cycle.
48 const ipv4AddressSize = 4
49
50 // Errors related to Subnet
51 var (
52 errSubnetLengthMismatch = errors.New("subnet length of address and mask differ")
53 errSubnetAddressMasked = errors.New("subnet address has bits set outside the mask")
54 )
55
56 // ErrSaveRejection indicates a failed save due to unsupported networking state.
57 // This type of errors is only used for save logic.
58 type ErrSaveRejection struct {
59 Err error
60 }
61
62 // Error returns a sensible description of the save rejection error.
63 func (e *ErrSaveRejection) Error() string {
64 return "save rejected due to unsupported networking state: " + e.Err.Error()
65 }
66
67 // MonotonicTime is a monotonic clock reading.
68 //
69 // +stateify savable
70 type MonotonicTime struct {
71 nanoseconds int64
72 }
73
74 // Before reports whether the monotonic clock reading mt is before u.
75 func (mt MonotonicTime) Before(u MonotonicTime) bool {
76 return mt.nanoseconds < u.nanoseconds
77 }
78
79 // After reports whether the monotonic clock reading mt is after u.
80 func (mt MonotonicTime) After(u MonotonicTime) bool {
81 return mt.nanoseconds > u.nanoseconds
82 }
83
84 // Add returns the monotonic clock reading mt+d.
85 func (mt MonotonicTime) Add(d time.Duration) MonotonicTime {
86 return MonotonicTime{
87 nanoseconds: time.Unix(0, mt.nanoseconds).Add(d).Sub(time.Unix(0, 0)).Nanoseconds(),
88 }
89 }
90
91 // Sub returns the duration mt-u. If the result exceeds the maximum (or minimum)
92 // value that can be stored in a Duration, the maximum (or minimum) duration
93 // will be returned. To compute t-d for a duration d, use t.Add(-d).
94 func (mt MonotonicTime) Sub(u MonotonicTime) time.Duration {
95 return time.Unix(0, mt.nanoseconds).Sub(time.Unix(0, u.nanoseconds))
96 }
97
98 // A Clock provides the current time and schedules work for execution.
99 //
100 // Times returned by a Clock should always be used for application-visible
101 // time. Only monotonic times should be used for netstack internal timekeeping.
102 type Clock interface {
103 // Now returns the current local time.
104 Now() time.Time
105
106 // NowMonotonic returns the current monotonic clock reading.
107 NowMonotonic() MonotonicTime
108
109 // AfterFunc waits for the duration to elapse and then calls f in its own
110 // goroutine. It returns a Timer that can be used to cancel the call using
111 // its Stop method.
112 AfterFunc(d time.Duration, f func()) Timer
113 }
114
115 // Timer represents a single event. A Timer must be created with
116 // Clock.AfterFunc.
117 type Timer interface {
118 // Stop prevents the Timer from firing. It returns true if the call stops the
119 // timer, false if the timer has already expired or been stopped.
120 //
121 // If Stop returns false, then the timer has already expired and the function
122 // f of Clock.AfterFunc(d, f) has been started in its own goroutine; Stop
123 // does not wait for f to complete before returning. If the caller needs to
124 // know whether f is completed, it must coordinate with f explicitly.
125 Stop() bool
126
127 // Reset changes the timer to expire after duration d.
128 //
129 // Reset should be invoked only on stopped or expired timers. If the timer is
130 // known to have expired, Reset can be used directly. Otherwise, the caller
131 // must coordinate with the function f of Clock.AfterFunc(d, f).
132 Reset(d time.Duration)
133 }
134
135 // Address is a byte slice cast as a string that represents the address of a
136 // network node. Or, in the case of unix endpoints, it may represent a path.
137 type Address string
138
139 // WithPrefix returns the address with a prefix that represents a point subnet.
140 func (a Address) WithPrefix() AddressWithPrefix {
141 return AddressWithPrefix{
142 Address: a,
143 PrefixLen: len(a) * 8,
144 }
145 }
146
147 // Unspecified returns true if the address is unspecified.
148 func (a Address) Unspecified() bool {
149 for _, b := range a {
150 if b != 0 {
151 return false
152 }
153 }
154 return true
155 }
156
157 // MatchingPrefix returns the matching prefix length in bits.
158 //
159 // Panics if b and a have different lengths.
160 func (a Address) MatchingPrefix(b Address) uint8 {
161 const bitsInAByte = 8
162
163 if len(a) != len(b) {
164 panic(fmt.Sprintf("addresses %s and %s do not have the same length", a, b))
165 }
166
167 var prefix uint8
168 for i := range a {
169 aByte := a[i]
170 bByte := b[i]
171
172 if aByte == bByte {
173 prefix += bitsInAByte
174 continue
175 }
176
177 // Count the remaining matching bits in the byte from MSbit to LSBbit.
178 mask := uint8(1) << (bitsInAByte - 1)
179 for {
180 if aByte&mask == bByte&mask {
181 prefix++
182 mask >>= 1
183 continue
184 }
185
186 break
187 }
188
189 break
190 }
191
192 return prefix
193 }
194
195 // AddressMask is a bitmask for an address.
196 type AddressMask string
197
198 // String implements Stringer.
199 func (m AddressMask) String() string {
200 return Address(m).String()
201 }
202
203 // Prefix returns the number of bits before the first host bit.
204 func (m AddressMask) Prefix() int {
205 p := 0
206 for _, b := range []byte(m) {
207 p += bits.LeadingZeros8(^b)
208 }
209 return p
210 }
211
212 // Subnet is a subnet defined by its address and mask.
213 type Subnet struct {
214 address Address
215 mask AddressMask
216 }
217
218 // NewSubnet creates a new Subnet, checking that the address and mask are the same length.
219 func NewSubnet(a Address, m AddressMask) (Subnet, error) {
220 if len(a) != len(m) {
221 return Subnet{}, errSubnetLengthMismatch
222 }
223 for i := 0; i < len(a); i++ {
224 if a[i]&^m[i] != 0 {
225 return Subnet{}, errSubnetAddressMasked
226 }
227 }
228 return Subnet{a, m}, nil
229 }
230
231 // String implements Stringer.
232 func (s Subnet) String() string {
233 return fmt.Sprintf("%s/%d", s.ID(), s.Prefix())
234 }
235
236 // Contains returns true iff the address is of the same length and matches the
237 // subnet address and mask.
238 func (s *Subnet) Contains(a Address) bool {
239 if len(a) != len(s.address) {
240 return false
241 }
242 for i := 0; i < len(a); i++ {
243 if a[i]&s.mask[i] != s.address[i] {
244 return false
245 }
246 }
247 return true
248 }
249
250 // ID returns the subnet ID.
251 func (s *Subnet) ID() Address {
252 return s.address
253 }
254
255 // Bits returns the number of ones (network bits) and zeros (host bits) in the
256 // subnet mask.
257 func (s *Subnet) Bits() (ones int, zeros int) {
258 ones = s.mask.Prefix()
259 return ones, len(s.mask)*8 - ones
260 }
261
262 // Prefix returns the number of bits before the first host bit.
263 func (s *Subnet) Prefix() int {
264 return s.mask.Prefix()
265 }
266
267 // Mask returns the subnet mask.
268 func (s *Subnet) Mask() AddressMask {
269 return s.mask
270 }
271
272 // Broadcast returns the subnet's broadcast address.
273 func (s *Subnet) Broadcast() Address {
274 addr := []byte(s.address)
275 for i := range addr {
276 addr[i] |= ^s.mask[i]
277 }
278 return Address(addr)
279 }
280
281 // IsBroadcast returns true if the address is considered a broadcast address.
282 func (s *Subnet) IsBroadcast(address Address) bool {
283 // Only IPv4 supports the notion of a broadcast address.
284 if len(address) != ipv4AddressSize {
285 return false
286 }
287
288 // Normally, we would just compare address with the subnet's broadcast
289 // address but there is an exception where a simple comparison is not
290 // correct. This exception is for /31 and /32 IPv4 subnets where all
291 // addresses are considered valid host addresses.
292 //
293 // For /31 subnets, the case is easy. RFC 3021 Section 2.1 states that
294 // both addresses in a /31 subnet "MUST be interpreted as host addresses."
295 //
296 // For /32, the case is a bit more vague. RFC 3021 makes no mention of /32
297 // subnets. However, the same reasoning applies - if an exception is not
298 // made, then there do not exist any host addresses in a /32 subnet. RFC
299 // 4632 Section 3.1 also vaguely implies this interpretation by referring
300 // to addresses in /32 subnets as "host routes."
301 return s.Prefix() <= 30 && s.Broadcast() == address
302 }
303
304 // Equal returns true if this Subnet is equal to the given Subnet.
305 func (s Subnet) Equal(o Subnet) bool {
306 // If this changes, update Route.Equal accordingly.
307 return s == o
308 }
309
310 // NICID is a number that uniquely identifies a NIC.
311 type NICID int32
312
313 // ShutdownFlags represents flags that can be passed to the Shutdown() method
314 // of the Endpoint interface.
315 type ShutdownFlags int
316
317 // Values of the flags that can be passed to the Shutdown() method. They can
318 // be OR'ed together.
319 const (
320 ShutdownRead ShutdownFlags = 1 << iota
321 ShutdownWrite
322 )
323
324 // PacketType is used to indicate the destination of the packet.
325 type PacketType uint8
326
327 const (
328 // PacketHost indicates a packet addressed to the local host.
329 PacketHost PacketType = iota
330
331 // PacketOtherHost indicates an outgoing packet addressed to
332 // another host caught by a NIC in promiscuous mode.
333 PacketOtherHost
334
335 // PacketOutgoing for a packet originating from the local host
336 // that is looped back to a packet socket.
337 PacketOutgoing
338
339 // PacketBroadcast indicates a link layer broadcast packet.
340 PacketBroadcast
341
342 // PacketMulticast indicates a link layer multicast packet.
343 PacketMulticast
344 )
345
346 // FullAddress represents a full transport node address, as required by the
347 // Connect() and Bind() methods.
348 //
349 // +stateify savable
350 type FullAddress struct {
351 // NIC is the ID of the NIC this address refers to.
352 //
353 // This may not be used by all endpoint types.
354 NIC NICID
355
356 // Addr is the network or link layer address.
357 Addr Address
358
359 // Port is the transport port.
360 //
361 // This may not be used by all endpoint types.
362 Port uint16
363 }
364
365 // Payloader is an interface that provides data.
366 //
367 // This interface allows the endpoint to request the amount of data it needs
368 // based on internal buffers without exposing them.
369 type Payloader interface {
370 io.Reader
371
372 // Len returns the number of bytes of the unread portion of the
373 // Reader.
374 Len() int
375 }
376
377 var _ Payloader = (*bytes.Buffer)(nil)
378 var _ Payloader = (*bytes.Reader)(nil)
379
380 var _ io.Writer = (*SliceWriter)(nil)
381
382 // SliceWriter implements io.Writer for slices.
383 type SliceWriter []byte
384
385 // Write implements io.Writer.Write.
386 func (s *SliceWriter) Write(b []byte) (int, error) {
387 n := copy(*s, b)
388 *s = (*s)[n:]
389 var err error
390 if n != len(b) {
391 err = io.ErrShortWrite
392 }
393 return n, err
394 }
395
396 var _ io.Writer = (*LimitedWriter)(nil)
397
398 // A LimitedWriter writes to W but limits the amount of data copied to just N
399 // bytes. Each call to Write updates N to reflect the new amount remaining.
400 type LimitedWriter struct {
401 W io.Writer
402 N int64
403 }
404
405 func (l *LimitedWriter) Write(p []byte) (int, error) {
406 pLen := int64(len(p))
407 if pLen > l.N {
408 p = p[:l.N]
409 }
410 n, err := l.W.Write(p)
411 n64 := int64(n)
412 if err == nil && n64 != pLen {
413 err = io.ErrShortWrite
414 }
415 l.N -= n64
416 return n, err
417 }
418
419 // A ControlMessages contains socket control messages for IP sockets.
420 //
421 // +stateify savable
422 type ControlMessages struct {
423 // HasTimestamp indicates whether Timestamp is valid/set.
424 HasTimestamp bool
425
426 // Timestamp is the time that the last packet used to create the read data
427 // was received.
428 Timestamp time.Time `state:".(int64)"`
429
430 // HasInq indicates whether Inq is valid/set.
431 HasInq bool
432
433 // Inq is the number of bytes ready to be received.
434 Inq int32
435
436 // HasTOS indicates whether Tos is valid/set.
437 HasTOS bool
438
439 // TOS is the IPv4 type of service of the associated packet.
440 TOS uint8
441
442 // HasTClass indicates whether TClass is valid/set.
443 HasTClass bool
444
445 // TClass is the IPv6 traffic class of the associated packet.
446 TClass uint32
447
448 // HasIPPacketInfo indicates whether PacketInfo is set.
449 HasIPPacketInfo bool
450
451 // PacketInfo holds interface and address data on an incoming packet.
452 PacketInfo IPPacketInfo
453
454 // HasIPv6PacketInfo indicates whether IPv6PacketInfo is set.
455 HasIPv6PacketInfo bool
456
457 // IPv6PacketInfo holds interface and address data on an incoming packet.
458 IPv6PacketInfo IPv6PacketInfo
459
460 // HasOriginalDestinationAddress indicates whether OriginalDstAddress is
461 // set.
462 HasOriginalDstAddress bool
463
464 // OriginalDestinationAddress holds the original destination address
465 // and port of the incoming packet.
466 OriginalDstAddress FullAddress
467
468 // SockErr is the dequeued socket error on recvmsg(MSG_ERRQUEUE).
469 SockErr *SockError
470 }
471
472 // PacketOwner is used to get UID and GID of the packet.
473 type PacketOwner interface {
474 // KUID returns KUID of the packet.
475 KUID() uint32
476
477 // KGID returns KGID of the packet.
478 KGID() uint32
479 }
480
481 // ReadOptions contains options for Endpoint.Read.
482 type ReadOptions struct {
483 // Peek indicates whether this read is a peek.
484 Peek bool
485
486 // NeedRemoteAddr indicates whether to return the remote address, if
487 // supported.
488 NeedRemoteAddr bool
489
490 // NeedLinkPacketInfo indicates whether to return the link-layer information,
491 // if supported.
492 NeedLinkPacketInfo bool
493 }
494
495 // ReadResult represents result for a successful Endpoint.Read.
496 type ReadResult struct {
497 // Count is the number of bytes received and written to the buffer.
498 Count int
499
500 // Total is the number of bytes of the received packet. This can be used to
501 // determine whether the read is truncated.
502 Total int
503
504 // ControlMessages is the control messages received.
505 ControlMessages ControlMessages
506
507 // RemoteAddr is the remote address if ReadOptions.NeedAddr is true.
508 RemoteAddr FullAddress
509
510 // LinkPacketInfo is the link-layer information of the received packet if
511 // ReadOptions.NeedLinkPacketInfo is true.
512 LinkPacketInfo LinkPacketInfo
513 }
514
515 // Endpoint is the interface implemented by transport protocols (e.g., tcp, udp)
516 // that exposes functionality like read, write, connect, etc. to users of the
517 // networking stack.
518 type Endpoint interface {
519 // Close puts the endpoint in a closed state and frees all resources
520 // associated with it. Close initiates the teardown process, the
521 // Endpoint may not be fully closed when Close returns.
522 Close()
523
524 // Abort initiates an expedited endpoint teardown. As compared to
525 // Close, Abort prioritizes closing the Endpoint quickly over cleanly.
526 // Abort is best effort; implementing Abort with Close is acceptable.
527 Abort()
528
529 // Read reads data from the endpoint and optionally writes to dst.
530 //
531 // This method does not block if there is no data pending; in this case,
532 // ErrWouldBlock is returned.
533 //
534 // If non-zero number of bytes are successfully read and written to dst, err
535 // must be nil. Otherwise, if dst failed to write anything, ErrBadBuffer
536 // should be returned.
537 Read(io.Writer, ReadOptions) (ReadResult, Error)
538
539 // Write writes data to the endpoint's peer. This method does not block if
540 // the data cannot be written.
541 //
542 // Unlike io.Writer.Write, Endpoint.Write transfers ownership of any bytes
543 // successfully written to the Endpoint. That is, if a call to
544 // Write(SlicePayload{data}) returns (n, err), it may retain data[:n], and
545 // the caller should not use data[:n] after Write returns.
546 //
547 // Note that unlike io.Writer.Write, it is not an error for Write to
548 // perform a partial write (if n > 0, no error may be returned). Only
549 // stream (TCP) Endpoints may return partial writes, and even then only
550 // in the case where writing additional data would block. Other Endpoints
551 // will either write the entire message or return an error.
552 Write(Payloader, WriteOptions) (int64, Error)
553
554 // Connect connects the endpoint to its peer. Specifying a NIC is
555 // optional.
556 //
557 // There are three classes of return values:
558 // nil -- the attempt to connect succeeded.
559 // ErrConnectStarted/ErrAlreadyConnecting -- the connect attempt started
560 // but hasn't completed yet. In this case, the caller must call Connect
561 // or GetSockOpt(ErrorOption) when the endpoint becomes writable to
562 // get the actual result. The first call to Connect after the socket has
563 // connected returns nil. Calling connect again results in ErrAlreadyConnected.
564 // Anything else -- the attempt to connect failed.
565 //
566 // If address.Addr is empty, this means that Endpoint has to be
567 // disconnected if this is supported, otherwise
568 // ErrAddressFamilyNotSupported must be returned.
569 Connect(address FullAddress) Error
570
571 // Disconnect disconnects the endpoint from its peer.
572 Disconnect() Error
573
574 // Shutdown closes the read and/or write end of the endpoint connection
575 // to its peer.
576 Shutdown(flags ShutdownFlags) Error
577
578 // Listen puts the endpoint in "listen" mode, which allows it to accept
579 // new connections.
580 Listen(backlog int) Error
581
582 // Accept returns a new endpoint if a peer has established a connection
583 // to an endpoint previously set to listen mode. This method does not
584 // block if no new connections are available.
585 //
586 // The returned Queue is the wait queue for the newly created endpoint.
587 //
588 // If peerAddr is not nil then it is populated with the peer address of the
589 // returned endpoint.
590 Accept(peerAddr *FullAddress) (Endpoint, *waiter.Queue, Error)
591
592 // Bind binds the endpoint to a specific local address and port.
593 // Specifying a NIC is optional.
594 Bind(address FullAddress) Error
595
596 // GetLocalAddress returns the address to which the endpoint is bound.
597 GetLocalAddress() (FullAddress, Error)
598
599 // GetRemoteAddress returns the address to which the endpoint is
600 // connected.
601 GetRemoteAddress() (FullAddress, Error)
602
603 // Readiness returns the current readiness of the endpoint. For example,
604 // if waiter.EventIn is set, the endpoint is immediately readable.
605 Readiness(mask waiter.EventMask) waiter.EventMask
606
607 // SetSockOpt sets a socket option.
608 SetSockOpt(opt SettableSocketOption) Error
609
610 // SetSockOptInt sets a socket option, for simple cases where a value
611 // has the int type.
612 SetSockOptInt(opt SockOptInt, v int) Error
613
614 // GetSockOpt gets a socket option.
615 GetSockOpt(opt GettableSocketOption) Error
616
617 // GetSockOptInt gets a socket option for simple cases where a return
618 // value has the int type.
619 GetSockOptInt(SockOptInt) (int, Error)
620
621 // State returns a socket's lifecycle state. The returned value is
622 // protocol-specific and is primarily used for diagnostics.
623 State() uint32
624
625 // ModerateRecvBuf should be called everytime data is copied to the user
626 // space. This allows for dynamic tuning of recv buffer space for a
627 // given socket.
628 //
629 // NOTE: This method is a no-op for sockets other than TCP.
630 ModerateRecvBuf(copied int)
631
632 // Info returns a copy to the transport endpoint info.
633 Info() EndpointInfo
634
635 // Stats returns a reference to the endpoint stats.
636 Stats() EndpointStats
637
638 // SetOwner sets the task owner to the endpoint owner.
639 SetOwner(owner PacketOwner)
640
641 // LastError clears and returns the last error reported by the endpoint.
642 LastError() Error
643
644 // SocketOptions returns the structure which contains all the socket
645 // level options.
646 SocketOptions() *SocketOptions
647 }
648
649 // LinkPacketInfo holds Link layer information for a received packet.
650 //
651 // +stateify savable
652 type LinkPacketInfo struct {
653 // Protocol is the NetworkProtocolNumber for the packet.
654 Protocol NetworkProtocolNumber
655
656 // PktType is used to indicate the destination of the packet.
657 PktType PacketType
658 }
659
660 // EndpointInfo is the interface implemented by each endpoint info struct.
661 type EndpointInfo interface {
662 // IsEndpointInfo is an empty method to implement the tcpip.EndpointInfo
663 // marker interface.
664 IsEndpointInfo()
665 }
666
667 // EndpointStats is the interface implemented by each endpoint stats struct.
668 type EndpointStats interface {
669 // IsEndpointStats is an empty method to implement the tcpip.EndpointStats
670 // marker interface.
671 IsEndpointStats()
672 }
673
674 // WriteOptions contains options for Endpoint.Write.
675 type WriteOptions struct {
676 // If To is not nil, write to the given address instead of the endpoint's
677 // peer.
678 To *FullAddress
679
680 // More has the same semantics as Linux's MSG_MORE.
681 More bool
682
683 // EndOfRecord has the same semantics as Linux's MSG_EOR.
684 EndOfRecord bool
685
686 // Atomic means that all data fetched from Payloader must be written to the
687 // endpoint. If Atomic is false, then data fetched from the Payloader may be
688 // discarded if available endpoint buffer space is unsufficient.
689 Atomic bool
690 }
691
692 // SockOptInt represents socket options which values have the int type.
693 type SockOptInt int
694
695 const (
696 // KeepaliveCountOption is used by SetSockOptInt/GetSockOptInt to
697 // specify the number of un-ACKed TCP keepalives that will be sent
698 // before the connection is closed.
699 KeepaliveCountOption SockOptInt = iota
700
701 // IPv4TOSOption is used by SetSockOptInt/GetSockOptInt to specify TOS
702 // for all subsequent outgoing IPv4 packets from the endpoint.
703 IPv4TOSOption
704
705 // IPv6TrafficClassOption is used by SetSockOptInt/GetSockOptInt to
706 // specify TOS for all subsequent outgoing IPv6 packets from the
707 // endpoint.
708 IPv6TrafficClassOption
709
710 // MaxSegOption is used by SetSockOptInt/GetSockOptInt to set/get the
711 // current Maximum Segment Size(MSS) value as specified using the
712 // TCP_MAXSEG option.
713 MaxSegOption
714
715 // MTUDiscoverOption is used to set/get the path MTU discovery setting.
716 //
717 // NOTE: Setting this option to any other value than PMTUDiscoveryDont
718 // is not supported and will fail as such, and getting this option will
719 // always return PMTUDiscoveryDont.
720 MTUDiscoverOption
721
722 // MulticastTTLOption is used by SetSockOptInt/GetSockOptInt to control
723 // the default TTL value for multicast messages. The default is 1.
724 MulticastTTLOption
725
726 // ReceiveQueueSizeOption is used in GetSockOptInt to specify that the
727 // number of unread bytes in the input buffer should be returned.
728 ReceiveQueueSizeOption
729
730 // SendQueueSizeOption is used in GetSockOptInt to specify that the
731 // number of unread bytes in the output buffer should be returned.
732 SendQueueSizeOption
733
734 // TTLOption is used by SetSockOptInt/GetSockOptInt to control the
735 // default TTL/hop limit value for unicast messages. The default is
736 // protocol specific.
737 //
738 // A zero value indicates the default.
739 TTLOption
740
741 // TCPSynCountOption is used by SetSockOptInt/GetSockOptInt to specify
742 // the number of SYN retransmits that TCP should send before aborting
743 // the attempt to connect. It cannot exceed 255.
744 //
745 // NOTE: This option is currently only stubbed out and is no-op.
746 TCPSynCountOption
747
748 // TCPWindowClampOption is used by SetSockOptInt/GetSockOptInt to bound
749 // the size of the advertised window to this value.
750 //
751 // NOTE: This option is currently only stubed out and is a no-op
752 TCPWindowClampOption
753 )
754
755 const (
756 // PMTUDiscoveryWant is a setting of the MTUDiscoverOption to use
757 // per-route settings.
758 PMTUDiscoveryWant int = iota
759
760 // PMTUDiscoveryDont is a setting of the MTUDiscoverOption to disable
761 // path MTU discovery.
762 PMTUDiscoveryDont
763
764 // PMTUDiscoveryDo is a setting of the MTUDiscoverOption to always do
765 // path MTU discovery.
766 PMTUDiscoveryDo
767
768 // PMTUDiscoveryProbe is a setting of the MTUDiscoverOption to set DF
769 // but ignore path MTU.
770 PMTUDiscoveryProbe
771 )
772
773 // GettableNetworkProtocolOption is a marker interface for network protocol
774 // options that may be queried.
775 type GettableNetworkProtocolOption interface {
776 isGettableNetworkProtocolOption()
777 }
778
779 // SettableNetworkProtocolOption is a marker interface for network protocol
780 // options that may be set.
781 type SettableNetworkProtocolOption interface {
782 isSettableNetworkProtocolOption()
783 }
784
785 // DefaultTTLOption is used by stack.(*Stack).NetworkProtocolOption to specify
786 // a default TTL.
787 type DefaultTTLOption uint8
788
789 func (*DefaultTTLOption) isGettableNetworkProtocolOption() {}
790
791 func (*DefaultTTLOption) isSettableNetworkProtocolOption() {}
792
793 // GettableTransportProtocolOption is a marker interface for transport protocol
794 // options that may be queried.
795 type GettableTransportProtocolOption interface {
796 isGettableTransportProtocolOption()
797 }
798
799 // SettableTransportProtocolOption is a marker interface for transport protocol
800 // options that may be set.
801 type SettableTransportProtocolOption interface {
802 isSettableTransportProtocolOption()
803 }
804
805 // TCPSACKEnabled the SACK option for TCP.
806 //
807 // See: https://tools.ietf.org/html/rfc2018.
808 type TCPSACKEnabled bool
809
810 func (*TCPSACKEnabled) isGettableTransportProtocolOption() {}
811
812 func (*TCPSACKEnabled) isSettableTransportProtocolOption() {}
813
814 // TCPRecovery is the loss deteoction algorithm used by TCP.
815 type TCPRecovery int32
816
817 func (*TCPRecovery) isGettableTransportProtocolOption() {}
818
819 func (*TCPRecovery) isSettableTransportProtocolOption() {}
820
821 // TCPAlwaysUseSynCookies indicates unconditional usage of syncookies.
822 type TCPAlwaysUseSynCookies bool
823
824 func (*TCPAlwaysUseSynCookies) isGettableTransportProtocolOption() {}
825
826 func (*TCPAlwaysUseSynCookies) isSettableTransportProtocolOption() {}
827
828 const (
829 // TCPRACKLossDetection indicates RACK is used for loss detection and
830 // recovery.
831 TCPRACKLossDetection TCPRecovery = 1 << iota
832
833 // TCPRACKStaticReoWnd indicates the reordering window should not be
834 // adjusted when DSACK is received.
835 TCPRACKStaticReoWnd
836
837 // TCPRACKNoDupTh indicates RACK should not consider the classic three
838 // duplicate acknowledgements rule to mark the segments as lost. This
839 // is used when reordering is not detected.
840 TCPRACKNoDupTh
841 )
842
843 // TCPDelayEnabled enables/disables Nagle's algorithm in TCP.
844 type TCPDelayEnabled bool
845
846 func (*TCPDelayEnabled) isGettableTransportProtocolOption() {}
847
848 func (*TCPDelayEnabled) isSettableTransportProtocolOption() {}
849
850 // TCPSendBufferSizeRangeOption is the send buffer size range for TCP.
851 type TCPSendBufferSizeRangeOption struct {
852 Min int
853 Default int
854 Max int
855 }
856
857 func (*TCPSendBufferSizeRangeOption) isGettableTransportProtocolOption() {}
858
859 func (*TCPSendBufferSizeRangeOption) isSettableTransportProtocolOption() {}
860
861 // TCPReceiveBufferSizeRangeOption is the receive buffer size range for TCP.
862 type TCPReceiveBufferSizeRangeOption struct {
863 Min int
864 Default int
865 Max int
866 }
867
868 func (*TCPReceiveBufferSizeRangeOption) isGettableTransportProtocolOption() {}
869
870 func (*TCPReceiveBufferSizeRangeOption) isSettableTransportProtocolOption() {}
871
872 // TCPAvailableCongestionControlOption is the supported congestion control
873 // algorithms for TCP
874 type TCPAvailableCongestionControlOption string
875
876 func (*TCPAvailableCongestionControlOption) isGettableTransportProtocolOption() {}
877
878 func (*TCPAvailableCongestionControlOption) isSettableTransportProtocolOption() {}
879
880 // TCPModerateReceiveBufferOption enables/disables receive buffer moderation
881 // for TCP.
882 type TCPModerateReceiveBufferOption bool
883
884 func (*TCPModerateReceiveBufferOption) isGettableTransportProtocolOption() {}
885
886 func (*TCPModerateReceiveBufferOption) isSettableTransportProtocolOption() {}
887
888 // GettableSocketOption is a marker interface for socket options that may be
889 // queried.
890 type GettableSocketOption interface {
891 isGettableSocketOption()
892 }
893
894 // SettableSocketOption is a marker interface for socket options that may be
895 // configured.
896 type SettableSocketOption interface {
897 isSettableSocketOption()
898 }
899
900 // EndpointState represents the state of an endpoint.
901 type EndpointState uint8
902
903 // CongestionControlState indicates the current congestion control state for
904 // TCP sender.
905 type CongestionControlState int
906
907 const (
908 // Open indicates that the sender is receiving acks in order and
909 // no loss or dupACK's etc have been detected.
910 Open CongestionControlState = iota
911 // RTORecovery indicates that an RTO has occurred and the sender
912 // has entered an RTO based recovery phase.
913 RTORecovery
914 // FastRecovery indicates that the sender has entered FastRecovery
915 // based on receiving nDupAck's. This state is entered only when
916 // SACK is not in use.
917 FastRecovery
918 // SACKRecovery indicates that the sender has entered SACK based
919 // recovery.
920 SACKRecovery
921 // Disorder indicates the sender either received some SACK blocks
922 // or dupACK's.
923 Disorder
924 )
925
926 // TCPInfoOption is used by GetSockOpt to expose TCP statistics.
927 //
928 // TODO(b/64800844): Add and populate stat fields.
929 type TCPInfoOption struct {
930 // RTT is the smoothed round trip time.
931 RTT time.Duration
932
933 // RTTVar is the round trip time variation.
934 RTTVar time.Duration
935
936 // RTO is the retransmission timeout for the endpoint.
937 RTO time.Duration
938
939 // State is the current endpoint protocol state.
940 State EndpointState
941
942 // CcState is the congestion control state.
943 CcState CongestionControlState
944
945 // SndCwnd is the congestion window, in packets.
946 SndCwnd uint32
947
948 // SndSsthresh is the threshold between slow start and congestion
949 // avoidance.
950 SndSsthresh uint32
951
952 // ReorderSeen indicates if reordering is seen in the endpoint.
953 ReorderSeen bool
954 }
955
956 func (*TCPInfoOption) isGettableSocketOption() {}
957
958 // KeepaliveIdleOption is used by SetSockOpt/GetSockOpt to specify the time a
959 // connection must remain idle before the first TCP keepalive packet is sent.
960 // Once this time is reached, KeepaliveIntervalOption is used instead.
961 type KeepaliveIdleOption time.Duration
962
963 func (*KeepaliveIdleOption) isGettableSocketOption() {}
964
965 func (*KeepaliveIdleOption) isSettableSocketOption() {}
966
967 // KeepaliveIntervalOption is used by SetSockOpt/GetSockOpt to specify the
968 // interval between sending TCP keepalive packets.
969 type KeepaliveIntervalOption time.Duration
970
971 func (*KeepaliveIntervalOption) isGettableSocketOption() {}
972
973 func (*KeepaliveIntervalOption) isSettableSocketOption() {}
974
975 // TCPUserTimeoutOption is used by SetSockOpt/GetSockOpt to specify a user
976 // specified timeout for a given TCP connection.
977 // See: RFC5482 for details.
978 type TCPUserTimeoutOption time.Duration
979
980 func (*TCPUserTimeoutOption) isGettableSocketOption() {}
981
982 func (*TCPUserTimeoutOption) isSettableSocketOption() {}
983
984 // CongestionControlOption is used by SetSockOpt/GetSockOpt to set/get
985 // the current congestion control algorithm.
986 type CongestionControlOption string
987
988 func (*CongestionControlOption) isGettableSocketOption() {}
989
990 func (*CongestionControlOption) isSettableSocketOption() {}
991
992 func (*CongestionControlOption) isGettableTransportProtocolOption() {}
993
994 func (*CongestionControlOption) isSettableTransportProtocolOption() {}
995
996 // TCPLingerTimeoutOption is used by SetSockOpt/GetSockOpt to set/get the
997 // maximum duration for which a socket lingers in the TCP_FIN_WAIT_2 state
998 // before being marked closed.
999 type TCPLingerTimeoutOption time.Duration
1000
1001 func (*TCPLingerTimeoutOption) isGettableSocketOption() {}
1002
1003 func (*TCPLingerTimeoutOption) isSettableSocketOption() {}
1004
1005 func (*TCPLingerTimeoutOption) isGettableTransportProtocolOption() {}
1006
1007 func (*TCPLingerTimeoutOption) isSettableTransportProtocolOption() {}
1008
1009 // TCPTimeWaitTimeoutOption is used by SetSockOpt/GetSockOpt to set/get the
1010 // maximum duration for which a socket lingers in the TIME_WAIT state
1011 // before being marked closed.
1012 type TCPTimeWaitTimeoutOption time.Duration
1013
1014 func (*TCPTimeWaitTimeoutOption) isGettableSocketOption() {}
1015
1016 func (*TCPTimeWaitTimeoutOption) isSettableSocketOption() {}
1017
1018 func (*TCPTimeWaitTimeoutOption) isGettableTransportProtocolOption() {}
1019
1020 func (*TCPTimeWaitTimeoutOption) isSettableTransportProtocolOption() {}
1021
1022 // TCPDeferAcceptOption is used by SetSockOpt/GetSockOpt to allow a
1023 // accept to return a completed connection only when there is data to be
1024 // read. This usually means the listening socket will drop the final ACK
1025 // for a handshake till the specified timeout until a segment with data arrives.
1026 type TCPDeferAcceptOption time.Duration
1027
1028 func (*TCPDeferAcceptOption) isGettableSocketOption() {}
1029
1030 func (*TCPDeferAcceptOption) isSettableSocketOption() {}
1031
1032 // TCPMinRTOOption is use by SetSockOpt/GetSockOpt to allow overriding
1033 // default MinRTO used by the Stack.
1034 type TCPMinRTOOption time.Duration
1035
1036 func (*TCPMinRTOOption) isGettableSocketOption() {}
1037
1038 func (*TCPMinRTOOption) isSettableSocketOption() {}
1039
1040 func (*TCPMinRTOOption) isGettableTransportProtocolOption() {}
1041
1042 func (*TCPMinRTOOption) isSettableTransportProtocolOption() {}
1043
1044 // TCPMaxRTOOption is use by SetSockOpt/GetSockOpt to allow overriding
1045 // default MaxRTO used by the Stack.
1046 type TCPMaxRTOOption time.Duration
1047
1048 func (*TCPMaxRTOOption) isGettableSocketOption() {}
1049
1050 func (*TCPMaxRTOOption) isSettableSocketOption() {}
1051
1052 func (*TCPMaxRTOOption) isGettableTransportProtocolOption() {}
1053
1054 func (*TCPMaxRTOOption) isSettableTransportProtocolOption() {}
1055
1056 // TCPMaxRetriesOption is used by SetSockOpt/GetSockOpt to set/get the
1057 // maximum number of retransmits after which we time out the connection.
1058 type TCPMaxRetriesOption uint64
1059
1060 func (*TCPMaxRetriesOption) isGettableSocketOption() {}
1061
1062 func (*TCPMaxRetriesOption) isSettableSocketOption() {}
1063
1064 func (*TCPMaxRetriesOption) isGettableTransportProtocolOption() {}
1065
1066 func (*TCPMaxRetriesOption) isSettableTransportProtocolOption() {}
1067
1068 // TCPSynRetriesOption is used by SetSockOpt/GetSockOpt to specify stack-wide
1069 // default for number of times SYN is retransmitted before aborting a connect.
1070 type TCPSynRetriesOption uint8
1071
1072 func (*TCPSynRetriesOption) isGettableSocketOption() {}
1073
1074 func (*TCPSynRetriesOption) isSettableSocketOption() {}
1075
1076 func (*TCPSynRetriesOption) isGettableTransportProtocolOption() {}
1077
1078 func (*TCPSynRetriesOption) isSettableTransportProtocolOption() {}
1079
1080 // MulticastInterfaceOption is used by SetSockOpt/GetSockOpt to specify a
1081 // default interface for multicast.
1082 type MulticastInterfaceOption struct {
1083 NIC NICID
1084 InterfaceAddr Address
1085 }
1086
1087 func (*MulticastInterfaceOption) isGettableSocketOption() {}
1088
1089 func (*MulticastInterfaceOption) isSettableSocketOption() {}
1090
1091 // MembershipOption is used to identify a multicast membership on an interface.
1092 type MembershipOption struct {
1093 NIC NICID
1094 InterfaceAddr Address
1095 MulticastAddr Address
1096 }
1097
1098 // AddMembershipOption identifies a multicast group to join on some interface.
1099 type AddMembershipOption MembershipOption
1100
1101 func (*AddMembershipOption) isSettableSocketOption() {}
1102
1103 // RemoveMembershipOption identifies a multicast group to leave on some
1104 // interface.
1105 type RemoveMembershipOption MembershipOption
1106
1107 func (*RemoveMembershipOption) isSettableSocketOption() {}
1108
1109 // SocketDetachFilterOption is used by SetSockOpt to detach a previously attached
1110 // classic BPF filter on a given endpoint.
1111 type SocketDetachFilterOption int
1112
1113 func (*SocketDetachFilterOption) isSettableSocketOption() {}
1114
1115 // OriginalDestinationOption is used to get the original destination address
1116 // and port of a redirected packet.
1117 type OriginalDestinationOption FullAddress
1118
1119 func (*OriginalDestinationOption) isGettableSocketOption() {}
1120
1121 // TCPTimeWaitReuseOption is used stack.(*Stack).TransportProtocolOption to
1122 // specify if the stack can reuse the port bound by an endpoint in TIME-WAIT for
1123 // new connections when it is safe from protocol viewpoint.
1124 type TCPTimeWaitReuseOption uint8
1125
1126 func (*TCPTimeWaitReuseOption) isGettableSocketOption() {}
1127
1128 func (*TCPTimeWaitReuseOption) isSettableSocketOption() {}
1129
1130 func (*TCPTimeWaitReuseOption) isGettableTransportProtocolOption() {}
1131
1132 func (*TCPTimeWaitReuseOption) isSettableTransportProtocolOption() {}
1133
1134 const (
1135 // TCPTimeWaitReuseDisabled indicates reuse of port bound by endponts in TIME-WAIT cannot
1136 // be reused for new connections.
1137 TCPTimeWaitReuseDisabled TCPTimeWaitReuseOption = iota
1138
1139 // TCPTimeWaitReuseGlobal indicates reuse of port bound by endponts in TIME-WAIT can
1140 // be reused for new connections irrespective of the src/dest addresses.
1141 TCPTimeWaitReuseGlobal
1142
1143 // TCPTimeWaitReuseLoopbackOnly indicates reuse of port bound by endpoint in TIME-WAIT can
1144 // only be reused if the connection was a connection over loopback. i.e src/dest adddresses
1145 // are loopback addresses.
1146 TCPTimeWaitReuseLoopbackOnly
1147 )
1148
1149 // LingerOption is used by SetSockOpt/GetSockOpt to set/get the
1150 // duration for which a socket lingers before returning from Close.
1151 //
1152 // +marshal
1153 // +stateify savable
1154 type LingerOption struct {
1155 Enabled bool
1156 Timeout time.Duration
1157 }
1158
1159 // IPPacketInfo is the message structure for IP_PKTINFO.
1160 //
1161 // +stateify savable
1162 type IPPacketInfo struct {
1163 // NIC is the ID of the NIC to be used.
1164 NIC NICID
1165
1166 // LocalAddr is the local address.
1167 LocalAddr Address
1168
1169 // DestinationAddr is the destination address found in the IP header.
1170 DestinationAddr Address
1171 }
1172
1173 // IPv6PacketInfo is the message structure for IPV6_PKTINFO.
1174 //
1175 // +stateify savable
1176 type IPv6PacketInfo struct {
1177 Addr Address
1178 NIC NICID
1179 }
1180
1181 // SendBufferSizeOption is used by stack.(Stack*).Option/SetOption to
1182 // get/set the default, min and max send buffer sizes.
1183 type SendBufferSizeOption struct {
1184 // Min is the minimum size for send buffer.
1185 Min int
1186
1187 // Default is the default size for send buffer.
1188 Default int
1189
1190 // Max is the maximum size for send buffer.
1191 Max int
1192 }
1193
1194 // ReceiveBufferSizeOption is used by stack.(Stack*).Option/SetOption to
1195 // get/set the default, min and max receive buffer sizes.
1196 type ReceiveBufferSizeOption struct {
1197 // Min is the minimum size for send buffer.
1198 Min int
1199
1200 // Default is the default size for send buffer.
1201 Default int
1202
1203 // Max is the maximum size for send buffer.
1204 Max int
1205 }
1206
1207 // GetSendBufferLimits is used to get the send buffer size limits.
1208 type GetSendBufferLimits func(StackHandler) SendBufferSizeOption
1209
1210 // GetStackSendBufferLimits is used to get default, min and max send buffer size.
1211 func GetStackSendBufferLimits(so StackHandler) SendBufferSizeOption {
1212 var ss SendBufferSizeOption
1213 if err := so.Option(&ss); err != nil {
1214 panic(fmt.Sprintf("s.Option(%#v) = %s", ss, err))
1215 }
1216 return ss
1217 }
1218
1219 // GetReceiveBufferLimits is used to get the send buffer size limits.
1220 type GetReceiveBufferLimits func(StackHandler) ReceiveBufferSizeOption
1221
1222 // GetStackReceiveBufferLimits is used to get default, min and max send buffer size.
1223 func GetStackReceiveBufferLimits(so StackHandler) ReceiveBufferSizeOption {
1224 var ss ReceiveBufferSizeOption
1225 if err := so.Option(&ss); err != nil {
1226 panic(fmt.Sprintf("s.Option(%#v) = %s", ss, err))
1227 }
1228 return ss
1229 }
1230
1231 // Route is a row in the routing table. It specifies through which NIC (and
1232 // gateway) sets of packets should be routed. A row is considered viable if the
1233 // masked target address matches the destination address in the row.
1234 type Route struct {
1235 // Destination must contain the target address for this row to be viable.
1236 Destination Subnet
1237
1238 // Gateway is the gateway to be used if this row is viable.
1239 Gateway Address
1240
1241 // NIC is the id of the nic to be used if this row is viable.
1242 NIC NICID
1243 }
1244
1245 // String implements the fmt.Stringer interface.
1246 func (r Route) String() string {
1247 var out strings.Builder
1248 _, _ = fmt.Fprintf(&out, "%s", r.Destination)
1249 if len(r.Gateway) > 0 {
1250 _, _ = fmt.Fprintf(&out, " via %s", r.Gateway)
1251 }
1252 _, _ = fmt.Fprintf(&out, " nic %d", r.NIC)
1253 return out.String()
1254 }
1255
1256 // Equal returns true if the given Route is equal to this Route.
1257 func (r Route) Equal(to Route) bool {
1258 // NOTE: This relies on the fact that r.Destination == to.Destination
1259 return r == to
1260 }
1261
1262 // TransportProtocolNumber is the number of a transport protocol.
1263 type TransportProtocolNumber uint32
1264
1265 // NetworkProtocolNumber is the EtherType of a network protocol in an Ethernet
1266 // frame.
1267 //
1268 // See: https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml
1269 type NetworkProtocolNumber uint32
1270
1271 // A StatCounter keeps track of a statistic.
1272 //
1273 // +stateify savable
1274 type StatCounter struct {
1275 count atomicbitops.AlignedAtomicUint64
1276 }
1277
1278 // Increment adds one to the counter.
1279 func (s *StatCounter) Increment() {
1280 s.IncrementBy(1)
1281 }
1282
1283 // Decrement minuses one to the counter.
1284 func (s *StatCounter) Decrement() {
1285 s.IncrementBy(^uint64(0))
1286 }
1287
1288 // Value returns the current value of the counter.
1289 func (s *StatCounter) Value(...string) uint64 {
1290 return s.count.Load()
1291 }
1292
1293 // IncrementBy increments the counter by v.
1294 func (s *StatCounter) IncrementBy(v uint64) {
1295 s.count.Add(v)
1296 }
1297
1298 func (s *StatCounter) String() string {
1299 return strconv.FormatUint(s.Value(), 10)
1300 }
1301
1302 // A MultiCounterStat keeps track of two counters at once.
1303 type MultiCounterStat struct {
1304 a *StatCounter
1305 b *StatCounter
1306 }
1307
1308 // Init sets both internal counters to point to a and b.
1309 func (m *MultiCounterStat) Init(a, b *StatCounter) {
1310 m.a = a
1311 m.b = b
1312 }
1313
1314 // Increment adds one to the counters.
1315 func (m *MultiCounterStat) Increment() {
1316 m.a.Increment()
1317 m.b.Increment()
1318 }
1319
1320 // IncrementBy increments the counters by v.
1321 func (m *MultiCounterStat) IncrementBy(v uint64) {
1322 m.a.IncrementBy(v)
1323 m.b.IncrementBy(v)
1324 }
1325
1326 // ICMPv4PacketStats enumerates counts for all ICMPv4 packet types.
1327 type ICMPv4PacketStats struct {
1328 // LINT.IfChange(ICMPv4PacketStats)
1329
1330 // EchoRequest is the number of ICMPv4 echo packets counted.
1331 EchoRequest *StatCounter
1332
1333 // EchoReply is the number of ICMPv4 echo reply packets counted.
1334 EchoReply *StatCounter
1335
1336 // DstUnreachable is the number of ICMPv4 destination unreachable packets
1337 // counted.
1338 DstUnreachable *StatCounter
1339
1340 // SrcQuench is the number of ICMPv4 source quench packets counted.
1341 SrcQuench *StatCounter
1342
1343 // Redirect is the number of ICMPv4 redirect packets counted.
1344 Redirect *StatCounter
1345
1346 // TimeExceeded is the number of ICMPv4 time exceeded packets counted.
1347 TimeExceeded *StatCounter
1348
1349 // ParamProblem is the number of ICMPv4 parameter problem packets counted.
1350 ParamProblem *StatCounter
1351
1352 // Timestamp is the number of ICMPv4 timestamp packets counted.
1353 Timestamp *StatCounter
1354
1355 // TimestampReply is the number of ICMPv4 timestamp reply packets counted.
1356 TimestampReply *StatCounter
1357
1358 // InfoRequest is the number of ICMPv4 information request packets counted.
1359 InfoRequest *StatCounter
1360
1361 // InfoReply is the number of ICMPv4 information reply packets counted.
1362 InfoReply *StatCounter
1363
1364 // LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4PacketStats)
1365 }
1366
1367 // ICMPv4SentPacketStats collects outbound ICMPv4-specific stats.
1368 type ICMPv4SentPacketStats struct {
1369 // LINT.IfChange(ICMPv4SentPacketStats)
1370
1371 ICMPv4PacketStats
1372
1373 // Dropped is the number of ICMPv4 packets dropped due to link layer errors.
1374 Dropped *StatCounter
1375
1376 // RateLimited is the number of ICMPv4 packets dropped due to rate limit being
1377 // exceeded.
1378 RateLimited *StatCounter
1379
1380 // LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4SentPacketStats)
1381 }
1382
1383 // ICMPv4ReceivedPacketStats collects inbound ICMPv4-specific stats.
1384 type ICMPv4ReceivedPacketStats struct {
1385 // LINT.IfChange(ICMPv4ReceivedPacketStats)
1386
1387 ICMPv4PacketStats
1388
1389 // Invalid is the number of invalid ICMPv4 packets received.
1390 Invalid *StatCounter
1391
1392 // LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4ReceivedPacketStats)
1393 }
1394
1395 // ICMPv4Stats collects ICMPv4-specific stats.
1396 type ICMPv4Stats struct {
1397 // LINT.IfChange(ICMPv4Stats)
1398
1399 // PacketsSent contains statistics about sent packets.
1400 PacketsSent ICMPv4SentPacketStats
1401
1402 // PacketsReceived contains statistics about received packets.
1403 PacketsReceived ICMPv4ReceivedPacketStats
1404
1405 // LINT.ThenChange(network/ipv4/stats.go:multiCounterICMPv4Stats)
1406 }
1407
1408 // ICMPv6PacketStats enumerates counts for all ICMPv6 packet types.
1409 type ICMPv6PacketStats struct {
1410 // LINT.IfChange(ICMPv6PacketStats)
1411
1412 // EchoRequest is the number of ICMPv6 echo request packets counted.
1413 EchoRequest *StatCounter
1414
1415 // EchoReply is the number of ICMPv6 echo reply packets counted.
1416 EchoReply *StatCounter
1417
1418 // DstUnreachable is the number of ICMPv6 destination unreachable packets
1419 // counted.
1420 DstUnreachable *StatCounter
1421
1422 // PacketTooBig is the number of ICMPv6 packet too big packets counted.
1423 PacketTooBig *StatCounter
1424
1425 // TimeExceeded is the number of ICMPv6 time exceeded packets counted.
1426 TimeExceeded *StatCounter
1427
1428 // ParamProblem is the number of ICMPv6 parameter problem packets counted.
1429 ParamProblem *StatCounter
1430
1431 // RouterSolicit is the number of ICMPv6 router solicit packets counted.
1432 RouterSolicit *StatCounter
1433
1434 // RouterAdvert is the number of ICMPv6 router advert packets counted.
1435 RouterAdvert *StatCounter
1436
1437 // NeighborSolicit is the number of ICMPv6 neighbor solicit packets counted.
1438 NeighborSolicit *StatCounter
1439
1440 // NeighborAdvert is the number of ICMPv6 neighbor advert packets counted.
1441 NeighborAdvert *StatCounter
1442
1443 // RedirectMsg is the number of ICMPv6 redirect message packets counted.
1444 RedirectMsg *StatCounter
1445
1446 // MulticastListenerQuery is the number of Multicast Listener Query messages
1447 // counted.
1448 MulticastListenerQuery *StatCounter
1449
1450 // MulticastListenerReport is the number of Multicast Listener Report messages
1451 // counted.
1452 MulticastListenerReport *StatCounter
1453
1454 // MulticastListenerDone is the number of Multicast Listener Done messages
1455 // counted.
1456 MulticastListenerDone *StatCounter
1457
1458 // LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6PacketStats)
1459 }
1460
1461 // ICMPv6SentPacketStats collects outbound ICMPv6-specific stats.
1462 type ICMPv6SentPacketStats struct {
1463 // LINT.IfChange(ICMPv6SentPacketStats)
1464
1465 ICMPv6PacketStats
1466
1467 // Dropped is the number of ICMPv6 packets dropped due to link layer errors.
1468 Dropped *StatCounter
1469
1470 // RateLimited is the number of ICMPv6 packets dropped due to rate limit being
1471 // exceeded.
1472 RateLimited *StatCounter
1473
1474 // LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6SentPacketStats)
1475 }
1476
1477 // ICMPv6ReceivedPacketStats collects inbound ICMPv6-specific stats.
1478 type ICMPv6ReceivedPacketStats struct {
1479 // LINT.IfChange(ICMPv6ReceivedPacketStats)
1480
1481 ICMPv6PacketStats
1482
1483 // Unrecognized is the number of ICMPv6 packets received that the transport
1484 // layer does not know how to parse.
1485 Unrecognized *StatCounter
1486
1487 // Invalid is the number of invalid ICMPv6 packets received.
1488 Invalid *StatCounter
1489
1490 // RouterOnlyPacketsDroppedByHost is the number of ICMPv6 packets dropped due
1491 // to being router-specific packets.
1492 RouterOnlyPacketsDroppedByHost *StatCounter
1493
1494 // LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6ReceivedPacketStats)
1495 }
1496
1497 // ICMPv6Stats collects ICMPv6-specific stats.
1498 type ICMPv6Stats struct {
1499 // LINT.IfChange(ICMPv6Stats)
1500
1501 // PacketsSent contains statistics about sent packets.
1502 PacketsSent ICMPv6SentPacketStats
1503
1504 // PacketsReceived contains statistics about received packets.
1505 PacketsReceived ICMPv6ReceivedPacketStats
1506
1507 // LINT.ThenChange(network/ipv6/stats.go:multiCounterICMPv6Stats)
1508 }
1509
1510 // ICMPStats collects ICMP-specific stats (both v4 and v6).
1511 type ICMPStats struct {
1512 // V4 contains the ICMPv4-specifics stats.
1513 V4 ICMPv4Stats
1514
1515 // V6 contains the ICMPv4-specifics stats.
1516 V6 ICMPv6Stats
1517 }
1518
1519 // IGMPPacketStats enumerates counts for all IGMP packet types.
1520 type IGMPPacketStats struct {
1521 // LINT.IfChange(IGMPPacketStats)
1522
1523 // MembershipQuery is the number of Membership Query messages counted.
1524 MembershipQuery *StatCounter
1525
1526 // V1MembershipReport is the number of Version 1 Membership Report messages
1527 // counted.
1528 V1MembershipReport *StatCounter
1529
1530 // V2MembershipReport is the number of Version 2 Membership Report messages
1531 // counted.
1532 V2MembershipReport *StatCounter
1533
1534 // LeaveGroup is the number of Leave Group messages counted.
1535 LeaveGroup *StatCounter
1536
1537 // LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPPacketStats)
1538 }
1539
1540 // IGMPSentPacketStats collects outbound IGMP-specific stats.
1541 type IGMPSentPacketStats struct {
1542 // LINT.IfChange(IGMPSentPacketStats)
1543
1544 IGMPPacketStats
1545
1546 // Dropped is the number of IGMP packets dropped.
1547 Dropped *StatCounter
1548
1549 // LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPSentPacketStats)
1550 }
1551
1552 // IGMPReceivedPacketStats collects inbound IGMP-specific stats.
1553 type IGMPReceivedPacketStats struct {
1554 // LINT.IfChange(IGMPReceivedPacketStats)
1555
1556 IGMPPacketStats
1557
1558 // Invalid is the number of invalid IGMP packets received.
1559 Invalid *StatCounter
1560
1561 // ChecksumErrors is the number of IGMP packets dropped due to bad checksums.
1562 ChecksumErrors *StatCounter
1563
1564 // Unrecognized is the number of unrecognized messages counted, these are
1565 // silently ignored for forward-compatibilty.
1566 Unrecognized *StatCounter
1567
1568 // LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPReceivedPacketStats)
1569 }
1570
1571 // IGMPStats collects IGMP-specific stats.
1572 type IGMPStats struct {
1573 // LINT.IfChange(IGMPStats)
1574
1575 // PacketsSent contains statistics about sent packets.
1576 PacketsSent IGMPSentPacketStats
1577
1578 // PacketsReceived contains statistics about received packets.
1579 PacketsReceived IGMPReceivedPacketStats
1580
1581 // LINT.ThenChange(network/ipv4/stats.go:multiCounterIGMPStats)
1582 }
1583
1584 // IPForwardingStats collects stats related to IP forwarding (both v4 and v6).
1585 type IPForwardingStats struct {
1586 // LINT.IfChange(IPForwardingStats)
1587
1588 // Unrouteable is the number of IP packets received which were dropped
1589 // because a route to their destination could not be constructed.
1590 Unrouteable *StatCounter
1591
1592 // ExhaustedTTL is the number of IP packets received which were dropped
1593 // because their TTL was exhausted.
1594 ExhaustedTTL *StatCounter
1595
1596 // LinkLocalSource is the number of IP packets which were dropped
1597 // because they contained a link-local source address.
1598 LinkLocalSource *StatCounter
1599
1600 // LinkLocalDestination is the number of IP packets which were dropped
1601 // because they contained a link-local destination address.
1602 LinkLocalDestination *StatCounter
1603
1604 // PacketTooBig is the number of IP packets which were dropped because they
1605 // were too big for the outgoing MTU.
1606 PacketTooBig *StatCounter
1607
1608 // HostUnreachable is the number of IP packets received which could not be
1609 // successfully forwarded due to an unresolvable next hop.
1610 HostUnreachable *StatCounter
1611
1612 // ExtensionHeaderProblem is the number of IP packets which were dropped
1613 // because of a problem encountered when processing an IPv6 extension
1614 // header.
1615 ExtensionHeaderProblem *StatCounter
1616
1617 // Errors is the number of IP packets received which could not be
1618 // successfully forwarded.
1619 Errors *StatCounter
1620
1621 // LINT.ThenChange(network/internal/ip/stats.go:multiCounterIPForwardingStats)
1622 }
1623
1624 // IPStats collects IP-specific stats (both v4 and v6).
1625 type IPStats struct {
1626 // LINT.IfChange(IPStats)
1627
1628 // PacketsReceived is the number of IP packets received from the link layer.
1629 PacketsReceived *StatCounter
1630
1631 // ValidPacketsReceived is the number of valid IP packets that reached the IP
1632 // layer.
1633 ValidPacketsReceived *StatCounter
1634
1635 // DisabledPacketsReceived is the number of IP packets received from the link
1636 // layer when the IP layer is disabled.
1637 DisabledPacketsReceived *StatCounter
1638
1639 // InvalidDestinationAddressesReceived is the number of IP packets received
1640 // with an unknown or invalid destination address.
1641 InvalidDestinationAddressesReceived *StatCounter
1642
1643 // InvalidSourceAddressesReceived is the number of IP packets received with a
1644 // source address that should never have been received on the wire.
1645 InvalidSourceAddressesReceived *StatCounter
1646
1647 // PacketsDelivered is the number of incoming IP packets that are successfully
1648 // delivered to the transport layer.
1649 PacketsDelivered *StatCounter
1650
1651 // PacketsSent is the number of IP packets sent via WritePacket.
1652 PacketsSent *StatCounter
1653
1654 // OutgoingPacketErrors is the number of IP packets which failed to write to a
1655 // link-layer endpoint.
1656 OutgoingPacketErrors *StatCounter
1657
1658 // MalformedPacketsReceived is the number of IP Packets that were dropped due
1659 // to the IP packet header failing validation checks.
1660 MalformedPacketsReceived *StatCounter
1661
1662 // MalformedFragmentsReceived is the number of IP Fragments that were dropped
1663 // due to the fragment failing validation checks.
1664 MalformedFragmentsReceived *StatCounter
1665
1666 // IPTablesPreroutingDropped is the number of IP packets dropped in the
1667 // Prerouting chain.
1668 IPTablesPreroutingDropped *StatCounter
1669
1670 // IPTablesInputDropped is the number of IP packets dropped in the Input
1671 // chain.
1672 IPTablesInputDropped *StatCounter
1673
1674 // IPTablesForwardDropped is the number of IP packets dropped in the Forward
1675 // chain.
1676 IPTablesForwardDropped *StatCounter
1677
1678 // IPTablesOutputDropped is the number of IP packets dropped in the Output
1679 // chain.
1680 IPTablesOutputDropped *StatCounter
1681
1682 // IPTablesPostroutingDropped is the number of IP packets dropped in the
1683 // Postrouting chain.
1684 IPTablesPostroutingDropped *StatCounter
1685
1686 // TODO(https://gvisor.dev/issues/5529): Move the IPv4-only option stats out
1687 // of IPStats.
1688 // OptionTimestampReceived is the number of Timestamp options seen.
1689 OptionTimestampReceived *StatCounter
1690
1691 // OptionRecordRouteReceived is the number of Record Route options seen.
1692 OptionRecordRouteReceived *StatCounter
1693
1694 // OptionRouterAlertReceived is the number of Router Alert options seen.
1695 OptionRouterAlertReceived *StatCounter
1696
1697 // OptionUnknownReceived is the number of unknown IP options seen.
1698 OptionUnknownReceived *StatCounter
1699
1700 // Forwarding collects stats related to IP forwarding.
1701 Forwarding IPForwardingStats
1702
1703 // LINT.ThenChange(network/internal/ip/stats.go:MultiCounterIPStats)
1704 }
1705
1706 // ARPStats collects ARP-specific stats.
1707 type ARPStats struct {
1708 // LINT.IfChange(ARPStats)
1709
1710 // PacketsReceived is the number of ARP packets received from the link layer.
1711 PacketsReceived *StatCounter
1712
1713 // DisabledPacketsReceived is the number of ARP packets received from the link
1714 // layer when the ARP layer is disabled.
1715 DisabledPacketsReceived *StatCounter
1716
1717 // MalformedPacketsReceived is the number of ARP packets that were dropped due
1718 // to being malformed.
1719 MalformedPacketsReceived *StatCounter
1720
1721 // RequestsReceived is the number of ARP requests received.
1722 RequestsReceived *StatCounter
1723
1724 // RequestsReceivedUnknownTargetAddress is the number of ARP requests that
1725 // were targeted to an interface different from the one it was received on.
1726 RequestsReceivedUnknownTargetAddress *StatCounter
1727
1728 // OutgoingRequestInterfaceHasNoLocalAddressErrors is the number of failures
1729 // to send an ARP request because the interface has no network address
1730 // assigned to it.
1731 OutgoingRequestInterfaceHasNoLocalAddressErrors *StatCounter
1732
1733 // OutgoingRequestBadLocalAddressErrors is the number of failures to send an
1734 // ARP request with a bad local address.
1735 OutgoingRequestBadLocalAddressErrors *StatCounter
1736
1737 // OutgoingRequestsDropped is the number of ARP requests which failed to write
1738 // to a link-layer endpoint.
1739 OutgoingRequestsDropped *StatCounter
1740
1741 // OutgoingRequestSent is the number of ARP requests successfully written to a
1742 // link-layer endpoint.
1743 OutgoingRequestsSent *StatCounter
1744
1745 // RepliesReceived is the number of ARP replies received.
1746 RepliesReceived *StatCounter
1747
1748 // OutgoingRepliesDropped is the number of ARP replies which failed to write
1749 // to a link-layer endpoint.
1750 OutgoingRepliesDropped *StatCounter
1751
1752 // OutgoingRepliesSent is the number of ARP replies successfully written to a
1753 // link-layer endpoint.
1754 OutgoingRepliesSent *StatCounter
1755
1756 // LINT.ThenChange(network/arp/stats.go:multiCounterARPStats)
1757 }
1758
1759 // TCPStats collects TCP-specific stats.
1760 type TCPStats struct {
1761 // ActiveConnectionOpenings is the number of connections opened
1762 // successfully via Connect.
1763 ActiveConnectionOpenings *StatCounter
1764
1765 // PassiveConnectionOpenings is the number of connections opened
1766 // successfully via Listen.
1767 PassiveConnectionOpenings *StatCounter
1768
1769 // CurrentEstablished is the number of TCP connections for which the
1770 // current state is ESTABLISHED.
1771 CurrentEstablished *StatCounter
1772
1773 // CurrentConnected is the number of TCP connections that
1774 // are in connected state.
1775 CurrentConnected *StatCounter
1776
1777 // EstablishedResets is the number of times TCP connections have made
1778 // a direct transition to the CLOSED state from either the
1779 // ESTABLISHED state or the CLOSE-WAIT state.
1780 EstablishedResets *StatCounter
1781
1782 // EstablishedClosed is the number of times established TCP connections
1783 // made a transition to CLOSED state.
1784 EstablishedClosed *StatCounter
1785
1786 // EstablishedTimedout is the number of times an established connection
1787 // was reset because of keep-alive time out.
1788 EstablishedTimedout *StatCounter
1789
1790 // ListenOverflowSynDrop is the number of times the listen queue overflowed
1791 // and a SYN was dropped.
1792 ListenOverflowSynDrop *StatCounter
1793
1794 // ListenOverflowAckDrop is the number of times the final ACK
1795 // in the handshake was dropped due to overflow.
1796 ListenOverflowAckDrop *StatCounter
1797
1798 // ListenOverflowCookieSent is the number of times a SYN cookie was sent.
1799 ListenOverflowSynCookieSent *StatCounter
1800
1801 // ListenOverflowSynCookieRcvd is the number of times a valid SYN
1802 // cookie was received.
1803 ListenOverflowSynCookieRcvd *StatCounter
1804
1805 // ListenOverflowInvalidSynCookieRcvd is the number of times an invalid SYN cookie
1806 // was received.
1807 ListenOverflowInvalidSynCookieRcvd *StatCounter
1808
1809 // FailedConnectionAttempts is the number of calls to Connect or Listen
1810 // (active and passive openings, respectively) that end in an error.
1811 FailedConnectionAttempts *StatCounter
1812
1813 // ValidSegmentsReceived is the number of TCP segments received that
1814 // the transport layer successfully parsed.
1815 ValidSegmentsReceived *StatCounter
1816
1817 // InvalidSegmentsReceived is the number of TCP segments received that
1818 // the transport layer could not parse.
1819 InvalidSegmentsReceived *StatCounter
1820
1821 // SegmentsSent is the number of TCP segments sent.
1822 SegmentsSent *StatCounter
1823
1824 // SegmentSendErrors is the number of TCP segments failed to be sent.
1825 SegmentSendErrors *StatCounter
1826
1827 // ResetsSent is the number of TCP resets sent.
1828 ResetsSent *StatCounter
1829
1830 // ResetsReceived is the number of TCP resets received.
1831 ResetsReceived *StatCounter
1832
1833 // Retransmits is the number of TCP segments retransmitted.
1834 Retransmits *StatCounter
1835
1836 // FastRecovery is the number of times Fast Recovery was used to
1837 // recover from packet loss.
1838 FastRecovery *StatCounter
1839
1840 // SACKRecovery is the number of times SACK Recovery was used to
1841 // recover from packet loss.
1842 SACKRecovery *StatCounter
1843
1844 // TLPRecovery is the number of times recovery was accomplished by the tail
1845 // loss probe.
1846 TLPRecovery *StatCounter
1847
1848 // SlowStartRetransmits is the number of segments retransmitted in slow
1849 // start.
1850 SlowStartRetransmits *StatCounter
1851
1852 // FastRetransmit is the number of segments retransmitted in fast
1853 // recovery.
1854 FastRetransmit *StatCounter
1855
1856 // Timeouts is the number of times the RTO expired.
1857 Timeouts *StatCounter
1858
1859 // ChecksumErrors is the number of segments dropped due to bad checksums.
1860 ChecksumErrors *StatCounter
1861
1862 // FailedPortReservations is the number of times TCP failed to reserve
1863 // a port.
1864 FailedPortReservations *StatCounter
1865
1866 // SegmentsAckedWithDSACK is the number of segments acknowledged with
1867 // DSACK.
1868 SegmentsAckedWithDSACK *StatCounter
1869
1870 // SpuriousRecovery is the number of times the connection entered loss
1871 // recovery spuriously.
1872 SpuriousRecovery *StatCounter
1873 }
1874
1875 // UDPStats collects UDP-specific stats.
1876 type UDPStats struct {
1877 // PacketsReceived is the number of UDP datagrams received via
1878 // HandlePacket.
1879 PacketsReceived *StatCounter
1880
1881 // UnknownPortErrors is the number of incoming UDP datagrams dropped
1882 // because they did not have a known destination port.
1883 UnknownPortErrors *StatCounter
1884
1885 // ReceiveBufferErrors is the number of incoming UDP datagrams dropped
1886 // due to the receiving buffer being in an invalid state.
1887 ReceiveBufferErrors *StatCounter
1888
1889 // MalformedPacketsReceived is the number of incoming UDP datagrams
1890 // dropped due to the UDP header being in a malformed state.
1891 MalformedPacketsReceived *StatCounter
1892
1893 // PacketsSent is the number of UDP datagrams sent via sendUDP.
1894 PacketsSent *StatCounter
1895
1896 // PacketSendErrors is the number of datagrams failed to be sent.
1897 PacketSendErrors *StatCounter
1898
1899 // ChecksumErrors is the number of datagrams dropped due to bad checksums.
1900 ChecksumErrors *StatCounter
1901 }
1902
1903 // NICNeighborStats holds metrics for the neighbor table.
1904 type NICNeighborStats struct {
1905 // LINT.IfChange(NICNeighborStats)
1906
1907 // UnreachableEntryLookups counts the number of lookups performed on an
1908 // entry in Unreachable state.
1909 UnreachableEntryLookups *StatCounter
1910
1911 // LINT.ThenChange(stack/nic_stats.go:multiCounterNICNeighborStats)
1912 }
1913
1914 // NICPacketStats holds basic packet statistics.
1915 type NICPacketStats struct {
1916 // LINT.IfChange(NICPacketStats)
1917
1918 // Packets is the number of packets counted.
1919 Packets *StatCounter
1920
1921 // Bytes is the number of bytes counted.
1922 Bytes *StatCounter
1923
1924 // LINT.ThenChange(stack/nic_stats.go:multiCounterNICPacketStats)
1925 }
1926
1927 // IntegralStatCounterMap holds a map associating integral keys with
1928 // StatCounters.
1929 type IntegralStatCounterMap struct {
1930 mu sync.RWMutex
1931 // +checklocks:mu
1932 counterMap map[uint64]*StatCounter
1933 }
1934
1935 // Keys returns all keys present in the map.
1936 func (m *IntegralStatCounterMap) Keys() []uint64 {
1937 m.mu.RLock()
1938 defer m.mu.RUnlock()
1939 var keys []uint64
1940 for k := range m.counterMap {
1941 keys = append(keys, k)
1942 }
1943 return keys
1944 }
1945
1946 // Get returns the counter mapped by the provided key.
1947 func (m *IntegralStatCounterMap) Get(key uint64) (*StatCounter, bool) {
1948 m.mu.RLock()
1949 defer m.mu.RUnlock()
1950 counter, ok := m.counterMap[key]
1951 return counter, ok
1952 }
1953
1954 // Init initializes the map.
1955 func (m *IntegralStatCounterMap) Init() {
1956 m.mu.Lock()
1957 defer m.mu.Unlock()
1958 m.counterMap = make(map[uint64]*StatCounter)
1959 }
1960
1961 // Increment increments the counter associated with the provided key.
1962 func (m *IntegralStatCounterMap) Increment(key uint64) {
1963 m.mu.RLock()
1964 counter, ok := m.counterMap[key]
1965 m.mu.RUnlock()
1966
1967 if !ok {
1968 m.mu.Lock()
1969 counter, ok = m.counterMap[key]
1970 if !ok {
1971 counter = new(StatCounter)
1972 m.counterMap[key] = counter
1973 }
1974 m.mu.Unlock()
1975 }
1976 counter.Increment()
1977 }
1978
1979 // A MultiIntegralStatCounterMap keeps track of two integral counter maps at
1980 // once.
1981 type MultiIntegralStatCounterMap struct {
1982 a *IntegralStatCounterMap
1983 b *IntegralStatCounterMap
1984 }
1985
1986 // Init sets the internal integral counter maps to point to a and b.
1987 func (m *MultiIntegralStatCounterMap) Init(a, b *IntegralStatCounterMap) {
1988 m.a = a
1989 m.b = b
1990 }
1991
1992 // Increment increments the counter in each map corresponding to the
1993 // provided key.
1994 func (m *MultiIntegralStatCounterMap) Increment(key uint64) {
1995 m.a.Increment(key)
1996 m.b.Increment(key)
1997 }
1998
1999 // NICStats holds NIC statistics.
2000 type NICStats struct {
2001 // LINT.IfChange(NICStats)
2002
2003 // UnknownL3ProtocolRcvdPacketCounts records the number of packets recieved
2004 // for each unknown or unsupported netowrk protocol number.
2005 UnknownL3ProtocolRcvdPacketCounts *IntegralStatCounterMap
2006
2007 // UnknownL4ProtocolRcvdPacketCounts records the number of packets recieved
2008 // for each unknown or unsupported transport protocol number.
2009 UnknownL4ProtocolRcvdPacketCounts *IntegralStatCounterMap
2010
2011 // MalformedL4RcvdPackets is the number of packets received by a NIC that
2012 // could not be delivered to a transport endpoint because the L4 header could
2013 // not be parsed.
2014 MalformedL4RcvdPackets *StatCounter
2015
2016 // Tx contains statistics about transmitted packets.
2017 Tx NICPacketStats
2018
2019 // Rx contains statistics about received packets.
2020 Rx NICPacketStats
2021
2022 // DisabledRx contains statistics about received packets on disabled NICs.
2023 DisabledRx NICPacketStats
2024
2025 // Neighbor contains statistics about neighbor entries.
2026 Neighbor NICNeighborStats
2027
2028 // LINT.ThenChange(stack/nic_stats.go:multiCounterNICStats)
2029 }
2030
2031 // FillIn returns a copy of s with nil fields initialized to new StatCounters.
2032 func (s NICStats) FillIn() NICStats {
2033 InitStatCounters(reflect.ValueOf(&s).Elem())
2034 return s
2035 }
2036
2037 // Stats holds statistics about the networking stack.
2038 type Stats struct {
2039 // TODO(https://gvisor.dev/issues/5986): Make the DroppedPackets stat less
2040 // ambiguous.
2041
2042 // DroppedPackets is the number of packets dropped at the transport layer.
2043 DroppedPackets *StatCounter
2044
2045 // NICs is an aggregation of every NIC's statistics. These should not be
2046 // incremented using this field, but using the relevant NIC multicounters.
2047 NICs NICStats
2048
2049 // ICMP is an aggregation of every NetworkEndpoint's ICMP statistics (both v4
2050 // and v6). These should not be incremented using this field, but using the
2051 // relevant NetworkEndpoint ICMP multicounters.
2052 ICMP ICMPStats
2053
2054 // IGMP is an aggregation of every NetworkEndpoint's IGMP statistics. These
2055 // should not be incremented using this field, but using the relevant
2056 // NetworkEndpoint IGMP multicounters.
2057 IGMP IGMPStats
2058
2059 // IP is an aggregation of every NetworkEndpoint's IP statistics. These should
2060 // not be incremented using this field, but using the relevant NetworkEndpoint
2061 // IP multicounters.
2062 IP IPStats
2063
2064 // ARP is an aggregation of every NetworkEndpoint's ARP statistics. These
2065 // should not be incremented using this field, but using the relevant
2066 // NetworkEndpoint ARP multicounters.
2067 ARP ARPStats
2068
2069 // TCP holds TCP-specific stats.
2070 TCP TCPStats
2071
2072 // UDP holds UDP-specific stats.
2073 UDP UDPStats
2074 }
2075
2076 // ReceiveErrors collects packet receive errors within transport endpoint.
2077 //
2078 // +stateify savable
2079 type ReceiveErrors struct {
2080 // ReceiveBufferOverflow is the number of received packets dropped
2081 // due to the receive buffer being full.
2082 ReceiveBufferOverflow StatCounter
2083
2084 // MalformedPacketsReceived is the number of incoming packets
2085 // dropped due to the packet header being in a malformed state.
2086 MalformedPacketsReceived StatCounter
2087
2088 // ClosedReceiver is the number of received packets dropped because
2089 // of receiving endpoint state being closed.
2090 ClosedReceiver StatCounter
2091
2092 // ChecksumErrors is the number of packets dropped due to bad checksums.
2093 ChecksumErrors StatCounter
2094 }
2095
2096 // SendErrors collects packet send errors within the transport layer for an
2097 // endpoint.
2098 //
2099 // +stateify savable
2100 type SendErrors struct {
2101 // SendToNetworkFailed is the number of packets failed to be written to
2102 // the network endpoint.
2103 SendToNetworkFailed StatCounter
2104
2105 // NoRoute is the number of times we failed to resolve IP route.
2106 NoRoute StatCounter
2107 }
2108
2109 // ReadErrors collects segment read errors from an endpoint read call.
2110 //
2111 // +stateify savable
2112 type ReadErrors struct {
2113 // ReadClosed is the number of received packet drops because the endpoint
2114 // was shutdown for read.
2115 ReadClosed StatCounter
2116
2117 // InvalidEndpointState is the number of times we found the endpoint state
2118 // to be unexpected.
2119 InvalidEndpointState StatCounter
2120
2121 // NotConnected is the number of times we tried to read but found that the
2122 // endpoint was not connected.
2123 NotConnected StatCounter
2124 }
2125
2126 // WriteErrors collects packet write errors from an endpoint write call.
2127 //
2128 // +stateify savable
2129 type WriteErrors struct {
2130 // WriteClosed is the number of packet drops because the endpoint
2131 // was shutdown for write.
2132 WriteClosed StatCounter
2133
2134 // InvalidEndpointState is the number of times we found the endpoint state
2135 // to be unexpected.
2136 InvalidEndpointState StatCounter
2137
2138 // InvalidArgs is the number of times invalid input arguments were
2139 // provided for endpoint Write call.
2140 InvalidArgs StatCounter
2141 }
2142
2143 // TransportEndpointStats collects statistics about the endpoint.
2144 //
2145 // +stateify savable
2146 type TransportEndpointStats struct {
2147 // PacketsReceived is the number of successful packet receives.
2148 PacketsReceived StatCounter
2149
2150 // PacketsSent is the number of successful packet sends.
2151 PacketsSent StatCounter
2152
2153 // ReceiveErrors collects packet receive errors within transport layer.
2154 ReceiveErrors ReceiveErrors
2155
2156 // ReadErrors collects packet read errors from an endpoint read call.
2157 ReadErrors ReadErrors
2158
2159 // SendErrors collects packet send errors within the transport layer.
2160 SendErrors SendErrors
2161
2162 // WriteErrors collects packet write errors from an endpoint write call.
2163 WriteErrors WriteErrors
2164 }
2165
2166 // IsEndpointStats is an empty method to implement the tcpip.EndpointStats
2167 // marker interface.
2168 func (*TransportEndpointStats) IsEndpointStats() {}
2169
2170 // InitStatCounters initializes v's fields with nil StatCounter fields to new
2171 // StatCounters.
2172 func InitStatCounters(v reflect.Value) {
2173 for i := 0; i < v.NumField(); i++ {
2174 v := v.Field(i)
2175 if s, ok := v.Addr().Interface().(**StatCounter); ok {
2176 if *s == nil {
2177 *s = new(StatCounter)
2178 }
2179 } else if s, ok := v.Addr().Interface().(**IntegralStatCounterMap); ok {
2180 if *s == nil {
2181 *s = new(IntegralStatCounterMap)
2182 (*s).Init()
2183 }
2184 } else {
2185 InitStatCounters(v)
2186 }
2187 }
2188 }
2189
2190 // FillIn returns a copy of s with nil fields initialized to new StatCounters.
2191 func (s Stats) FillIn() Stats {
2192 InitStatCounters(reflect.ValueOf(&s).Elem())
2193 return s
2194 }
2195
2196 // Clone returns a copy of the TransportEndpointStats by atomically reading
2197 // each field.
2198 func (src *TransportEndpointStats) Clone() TransportEndpointStats {
2199 var dst TransportEndpointStats
2200 clone(reflect.ValueOf(&dst).Elem(), reflect.ValueOf(src).Elem())
2201 return dst
2202 }
2203
2204 func clone(dst reflect.Value, src reflect.Value) {
2205 for i := 0; i < dst.NumField(); i++ {
2206 d := dst.Field(i)
2207 s := src.Field(i)
2208 if c, ok := s.Addr().Interface().(*StatCounter); ok {
2209 d.Addr().Interface().(*StatCounter).IncrementBy(c.Value())
2210 } else {
2211 clone(d, s)
2212 }
2213 }
2214 }
2215
2216 // String implements the fmt.Stringer interface.
2217 func (a Address) String() string {
2218 switch len(a) {
2219 case 4:
2220 return fmt.Sprintf("%d.%d.%d.%d", int(a[0]), int(a[1]), int(a[2]), int(a[3]))
2221 case 16:
2222 // Find the longest subsequence of hexadecimal zeros.
2223 start, end := -1, -1
2224 for i := 0; i < len(a); i += 2 {
2225 j := i
2226 for j < len(a) && a[j] == 0 && a[j+1] == 0 {
2227 j += 2
2228 }
2229 if j > i+2 && j-i > end-start {
2230 start, end = i, j
2231 }
2232 }
2233
2234 var b strings.Builder
2235 for i := 0; i < len(a); i += 2 {
2236 if i == start {
2237 b.WriteString("::")
2238 i = end
2239 if end >= len(a) {
2240 break
2241 }
2242 } else if i > 0 {
2243 b.WriteByte(':')
2244 }
2245 v := uint16(a[i+0])<<8 | uint16(a[i+1])
2246 if v == 0 {
2247 b.WriteByte('0')
2248 } else {
2249 const digits = "0123456789abcdef"
2250 for i := uint(3); i < 4; i-- {
2251 if v := v >> (i * 4); v != 0 {
2252 b.WriteByte(digits[v&0xf])
2253 }
2254 }
2255 }
2256 }
2257 return b.String()
2258 default:
2259 return fmt.Sprintf("%x", []byte(a))
2260 }
2261 }
2262
2263 // To4 converts the IPv4 address to a 4-byte representation.
2264 // If the address is not an IPv4 address, To4 returns "".
2265 func (a Address) To4() Address {
2266 const (
2267 ipv4len = 4
2268 ipv6len = 16
2269 )
2270 if len(a) == ipv4len {
2271 return a
2272 }
2273 if len(a) == ipv6len &&
2274 isZeros(a[0:10]) &&
2275 a[10] == 0xff &&
2276 a[11] == 0xff {
2277 return a[12:16]
2278 }
2279 return ""
2280 }
2281
2282 // isZeros reports whether a is all zeros.
2283 func isZeros(a Address) bool {
2284 for i := 0; i < len(a); i++ {
2285 if a[i] != 0 {
2286 return false
2287 }
2288 }
2289 return true
2290 }
2291
2292 // LinkAddress is a byte slice cast as a string that represents a link address.
2293 // It is typically a 6-byte MAC address.
2294 type LinkAddress string
2295
2296 // String implements the fmt.Stringer interface.
2297 func (a LinkAddress) String() string {
2298 switch len(a) {
2299 case 6:
2300 return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", a[0], a[1], a[2], a[3], a[4], a[5])
2301 default:
2302 return fmt.Sprintf("%x", []byte(a))
2303 }
2304 }
2305
2306 // ParseMACAddress parses an IEEE 802 address.
2307 //
2308 // It must be in the format aa:bb:cc:dd:ee:ff or aa-bb-cc-dd-ee-ff.
2309 func ParseMACAddress(s string) (LinkAddress, error) {
2310 parts := strings.FieldsFunc(s, func(c rune) bool {
2311 return c == ':' || c == '-'
2312 })
2313 if len(parts) != 6 {
2314 return "", fmt.Errorf("inconsistent parts: %s", s)
2315 }
2316 addr := make([]byte, 0, len(parts))
2317 for _, part := range parts {
2318 u, err := strconv.ParseUint(part, 16, 8)
2319 if err != nil {
2320 return "", fmt.Errorf("invalid hex digits: %s", s)
2321 }
2322 addr = append(addr, byte(u))
2323 }
2324 return LinkAddress(addr), nil
2325 }
2326
2327 // AddressWithPrefix is an address with its subnet prefix length.
2328 type AddressWithPrefix struct {
2329 // Address is a network address.
2330 Address Address
2331
2332 // PrefixLen is the subnet prefix length.
2333 PrefixLen int
2334 }
2335
2336 // String implements the fmt.Stringer interface.
2337 func (a AddressWithPrefix) String() string {
2338 return fmt.Sprintf("%s/%d", a.Address, a.PrefixLen)
2339 }
2340
2341 // Subnet converts the address and prefix into a Subnet value and returns it.
2342 func (a AddressWithPrefix) Subnet() Subnet {
2343 addrLen := len(a.Address)
2344 if a.PrefixLen <= 0 {
2345 return Subnet{
2346 address: Address(strings.Repeat("\x00", addrLen)),
2347 mask: AddressMask(strings.Repeat("\x00", addrLen)),
2348 }
2349 }
2350 if a.PrefixLen >= addrLen*8 {
2351 return Subnet{
2352 address: a.Address,
2353 mask: AddressMask(strings.Repeat("\xff", addrLen)),
2354 }
2355 }
2356
2357 sa := make([]byte, addrLen)
2358 sm := make([]byte, addrLen)
2359 n := uint(a.PrefixLen)
2360 for i := 0; i < addrLen; i++ {
2361 if n >= 8 {
2362 sa[i] = a.Address[i]
2363 sm[i] = 0xff
2364 n -= 8
2365 continue
2366 }
2367 sm[i] = ^byte(0xff >> n)
2368 sa[i] = a.Address[i] & sm[i]
2369 n = 0
2370 }
2371
2372 // For extra caution, call NewSubnet rather than directly creating the Subnet
2373 // value. If that fails it indicates a serious bug in this code, so panic is
2374 // in order.
2375 s, err := NewSubnet(Address(sa), AddressMask(sm))
2376 if err != nil {
2377 panic("invalid subnet: " + err.Error())
2378 }
2379 return s
2380 }
2381
2382 // ProtocolAddress is an address and the network protocol it is associated
2383 // with.
2384 type ProtocolAddress struct {
2385 // Protocol is the protocol of the address.
2386 Protocol NetworkProtocolNumber
2387
2388 // AddressWithPrefix is a network address with its subnet prefix length.
2389 AddressWithPrefix AddressWithPrefix
2390 }
2391
2392 var (
2393 // danglingEndpointsMu protects access to danglingEndpoints.
2394 danglingEndpointsMu sync.Mutex
2395
2396 // danglingEndpoints tracks all dangling endpoints no longer owned by the app.
2397 danglingEndpoints = make(map[Endpoint]struct{})
2398 )
2399
2400 // GetDanglingEndpoints returns all dangling endpoints.
2401 func GetDanglingEndpoints() []Endpoint {
2402 danglingEndpointsMu.Lock()
2403 es := make([]Endpoint, 0, len(danglingEndpoints))
2404 for e := range danglingEndpoints {
2405 es = append(es, e)
2406 }
2407 danglingEndpointsMu.Unlock()
2408 return es
2409 }
2410
2411 // AddDanglingEndpoint adds a dangling endpoint.
2412 func AddDanglingEndpoint(e Endpoint) {
2413 danglingEndpointsMu.Lock()
2414 danglingEndpoints[e] = struct{}{}
2415 danglingEndpointsMu.Unlock()
2416 }
2417
2418 // DeleteDanglingEndpoint removes a dangling endpoint.
2419 func DeleteDanglingEndpoint(e Endpoint) {
2420 danglingEndpointsMu.Lock()
2421 delete(danglingEndpoints, e)
2422 danglingEndpointsMu.Unlock()
2423 }
2424
2425 // AsyncLoading is the global barrier for asynchronous endpoint loading
2426 // activities.
2427 var AsyncLoading sync.WaitGroup