Increment the counters when sending Echo requests

Updates #5597

PiperOrigin-RevId: 361252003
This commit is contained in:
Arthur Sfez
2021-03-05 16:51:45 -08:00
committed by gVisor bot
parent 2db8f74859
commit fb733cdb8f
+26 -2
View File
@@ -26,6 +26,8 @@ import (
"gvisor.dev/gvisor/pkg/waiter"
)
// TODO(https://gvisor.dev/issues/5623): Unit test this package.
// +stateify savable
type icmpPacket struct {
icmpPacketEntry
@@ -414,6 +416,11 @@ func send4(r *stack.Route, ident uint16, data buffer.View, ttl uint8, owner tcpi
return &tcpip.ErrInvalidEndpointState{}
}
// Because this icmp endpoint is implemented in the transport layer, we can
// only increment the 'stack-wide' stats but we can't increment the
// 'per-NetworkEndpoint' stats.
sentStat := r.Stats().ICMP.V4.PacketsSent.EchoRequest
icmpv4.SetChecksum(0)
icmpv4.SetChecksum(^header.Checksum(icmpv4, header.Checksum(data, 0)))
@@ -422,7 +429,14 @@ func send4(r *stack.Route, ident uint16, data buffer.View, ttl uint8, owner tcpi
if ttl == 0 {
ttl = r.DefaultTTL()
}
return r.WritePacket(nil /* gso */, stack.NetworkHeaderParams{Protocol: header.ICMPv4ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS}, pkt)
if err := r.WritePacket(nil /* gso */, stack.NetworkHeaderParams{Protocol: header.ICMPv4ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS}, pkt); err != nil {
r.Stats().ICMP.V4.PacketsSent.Dropped.Increment()
return err
}
sentStat.Increment()
return nil
}
func send6(r *stack.Route, ident uint16, data buffer.View, ttl uint8) tcpip.Error {
@@ -444,6 +458,10 @@ func send6(r *stack.Route, ident uint16, data buffer.View, ttl uint8) tcpip.Erro
if icmpv6.Type() != header.ICMPv6EchoRequest || icmpv6.Code() != 0 {
return &tcpip.ErrInvalidEndpointState{}
}
// Because this icmp endpoint is implemented in the transport layer, we can
// only increment the 'stack-wide' stats but we can't increment the
// 'per-NetworkEndpoint' stats.
sentStat := r.Stats().ICMP.V6.PacketsSent.EchoRequest
pkt.Data().AppendView(data)
dataRange := pkt.Data().AsRange()
@@ -458,7 +476,13 @@ func send6(r *stack.Route, ident uint16, data buffer.View, ttl uint8) tcpip.Erro
if ttl == 0 {
ttl = r.DefaultTTL()
}
return r.WritePacket(nil /* gso */, stack.NetworkHeaderParams{Protocol: header.ICMPv6ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS}, pkt)
if err := r.WritePacket(nil /* gso */, stack.NetworkHeaderParams{Protocol: header.ICMPv6ProtocolNumber, TTL: ttl, TOS: stack.DefaultTOS}, pkt); err != nil {
r.Stats().ICMP.V6.PacketsSent.Dropped.Increment()
}
sentStat.Increment()
return nil
}
// checkV4MappedLocked determines the effective network protocol and converts