diff options
author | Ian Gudger <igudger@google.com> | 2018-08-23 08:54:09 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-08-23 08:55:23 -0700 |
commit | abe7764928bb18fe417c53c8ea8aa9fb970114b7 (patch) | |
tree | 4ea44a38b3193ac29a4151daff471d8744f9071e /pkg/tcpip/stack | |
parent | a78df1d874f376c0924d5a8f91e9e2b5458cca0f (diff) |
Encapsulate netstack metrics
PiperOrigin-RevId: 209943212
Change-Id: I96dcbc7c2ab2426e510b94a564436505256c5c79
Diffstat (limited to 'pkg/tcpip/stack')
-rw-r--r-- | pkg/tcpip/stack/nic.go | 14 | ||||
-rw-r--r-- | pkg/tcpip/stack/stack.go | 26 |
2 files changed, 14 insertions, 26 deletions
diff --git a/pkg/tcpip/stack/nic.go b/pkg/tcpip/stack/nic.go index 592006a32..284732874 100644 --- a/pkg/tcpip/stack/nic.go +++ b/pkg/tcpip/stack/nic.go @@ -282,12 +282,12 @@ func (n *NIC) RemoveAddress(addr tcpip.Address) *tcpip.Error { func (n *NIC) DeliverNetworkPacket(linkEP LinkEndpoint, remoteLinkAddr tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv *buffer.VectorisedView) { netProto, ok := n.stack.networkProtocols[protocol] if !ok { - atomic.AddUint64(&n.stack.stats.UnknownProtocolRcvdPackets, 1) + n.stack.stats.UnknownProtocolRcvdPackets.Increment() return } if len(vv.First()) < netProto.MinimumPacketSize() { - atomic.AddUint64(&n.stack.stats.MalformedRcvdPackets, 1) + n.stack.stats.MalformedRcvdPackets.Increment() return } @@ -330,7 +330,7 @@ func (n *NIC) DeliverNetworkPacket(linkEP LinkEndpoint, remoteLinkAddr tcpip.Lin } if ref == nil { - atomic.AddUint64(&n.stack.stats.UnknownNetworkEndpointRcvdPackets, 1) + n.stack.stats.UnknownNetworkEndpointRcvdPackets.Increment() return } @@ -345,19 +345,19 @@ func (n *NIC) DeliverNetworkPacket(linkEP LinkEndpoint, remoteLinkAddr tcpip.Lin func (n *NIC) DeliverTransportPacket(r *Route, protocol tcpip.TransportProtocolNumber, vv *buffer.VectorisedView) { state, ok := n.stack.transportProtocols[protocol] if !ok { - atomic.AddUint64(&n.stack.stats.UnknownProtocolRcvdPackets, 1) + n.stack.stats.UnknownProtocolRcvdPackets.Increment() return } transProto := state.proto if len(vv.First()) < transProto.MinimumPacketSize() { - atomic.AddUint64(&n.stack.stats.MalformedRcvdPackets, 1) + n.stack.stats.MalformedRcvdPackets.Increment() return } srcPort, dstPort, err := transProto.ParsePorts(vv.First()) if err != nil { - atomic.AddUint64(&n.stack.stats.MalformedRcvdPackets, 1) + n.stack.stats.MalformedRcvdPackets.Increment() return } @@ -379,7 +379,7 @@ func (n *NIC) DeliverTransportPacket(r *Route, protocol tcpip.TransportProtocolN // We could not find an appropriate destination for this packet, so // deliver it to the global handler. if !transProto.HandleUnknownDestinationPacket(r, id, vv) { - atomic.AddUint64(&n.stack.stats.MalformedRcvdPackets, 1) + n.stack.stats.MalformedRcvdPackets.Increment() } } diff --git a/pkg/tcpip/stack/stack.go b/pkg/tcpip/stack/stack.go index e09c7efda..cc5427cf9 100644 --- a/pkg/tcpip/stack/stack.go +++ b/pkg/tcpip/stack/stack.go @@ -26,7 +26,6 @@ package stack import ( "sync" - "sync/atomic" "time" "gvisor.googlesource.com/gvisor/pkg/sleep" @@ -308,6 +307,9 @@ type Options struct { // // If no Clock is specified, the clock source will be time.Now. Clock tcpip.Clock + + // Stats are optional statistic counters. + Stats tcpip.Stats } // New allocates a new networking stack with only the requested networking and @@ -331,6 +333,7 @@ func New(network []string, transport []string, opts Options) *Stack { linkAddrCache: newLinkAddrCache(ageLimit, resolutionTimeout, resolutionAttempts), PortManager: ports.NewPortManager(), clock: clock, + stats: opts.Stats.FillIn(), } // Add specified network protocols. @@ -437,27 +440,12 @@ func (s *Stack) NowNanoseconds() int64 { return s.clock.NowNanoseconds() } -// Stats returns a snapshot of the current stats. -// -// NOTE: The underlying stats are updated using atomic instructions as a result -// the snapshot returned does not represent the value of all the stats at any -// single given point of time. -// TODO: Make stats available in sentry for debugging/diag. -func (s *Stack) Stats() tcpip.Stats { - return tcpip.Stats{ - UnknownProtocolRcvdPackets: atomic.LoadUint64(&s.stats.UnknownProtocolRcvdPackets), - UnknownNetworkEndpointRcvdPackets: atomic.LoadUint64(&s.stats.UnknownNetworkEndpointRcvdPackets), - MalformedRcvdPackets: atomic.LoadUint64(&s.stats.MalformedRcvdPackets), - DroppedPackets: atomic.LoadUint64(&s.stats.DroppedPackets), - } -} - -// MutableStats returns a mutable copy of the current stats. +// Stats returns a mutable copy of the current stats. // // This is not generally exported via the public interface, but is available // internally. -func (s *Stack) MutableStats() *tcpip.Stats { - return &s.stats +func (s *Stack) Stats() tcpip.Stats { + return s.stats } // SetRouteTable assigns the route table to be used by this stack. It |