Skip to content

Network Traffic Statistics on Linux #91982

Closed

Description

@skyoxZ

Recently I'm working on getting network traffic (bytes received/sent) on Linux. I learned that it can be done by reading /proc/net/dev. But later I found this has already been implemented in NetworkInterface and it just does the same thing: parsing /proc/net/dev.

internal static IPInterfaceStatisticsTable ParseInterfaceStatisticsTableFromFile(string filePath, string name)
{
using (StreamReader sr = OpenStreamReader(filePath))
{
sr.ReadLine();
sr.ReadLine();
int index = 0;
while (!sr.EndOfStream)
{
string line = sr.ReadLine()!;
if (line.Contains(name))
{
Span<Range> pieces = stackalloc Range[18]; // [0] skipped, [1]-[16] used, +1 to ensure any additional segment goes into [17]
ReadOnlySpan<char> lineSpan = line;
pieces = pieces.Slice(0, lineSpan.SplitAny(pieces, " :", StringSplitOptions.RemoveEmptyEntries));
return new IPInterfaceStatisticsTable()
{
BytesReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[1]]),
PacketsReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[2]]),
ErrorsReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[3]]),
IncomingPacketsDropped = ParseUInt64AndClampToInt64(lineSpan[pieces[4]]),
FifoBufferErrorsReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[5]]),
PacketFramingErrorsReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[6]]),
CompressedPacketsReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[7]]),
MulticastFramesReceived = ParseUInt64AndClampToInt64(lineSpan[pieces[8]]),
BytesTransmitted = ParseUInt64AndClampToInt64(lineSpan[pieces[9]]),
PacketsTransmitted = ParseUInt64AndClampToInt64(lineSpan[pieces[10]]),
ErrorsTransmitted = ParseUInt64AndClampToInt64(lineSpan[pieces[11]]),
OutgoingPacketsDropped = ParseUInt64AndClampToInt64(lineSpan[pieces[12]]),
FifoBufferErrorsTransmitted = ParseUInt64AndClampToInt64(lineSpan[pieces[13]]),
CollisionsDetected = ParseUInt64AndClampToInt64(lineSpan[pieces[14]]),
CarrierLosses = ParseUInt64AndClampToInt64(lineSpan[pieces[15]]),
CompressedPacketsTransmitted = ParseUInt64AndClampToInt64(lineSpan[pieces[16]]),
};
}
index += 1;
}
throw ExceptionHelper.CreateForParseFailure();
}
}

I notice that it uses line.Contains(name) to locate the specific adapter. I'm curious if it's safe to do so. For example, is it possible that both eth1 and eth10 exist?

Activity

  1. ghost added
    untriaged
    on Sep 13, 2023
  2. wfurt commented on Sep 13, 2023

    @wfurt
    Member

    The Contains seems problematic. However, the file seems to be ordered by interface index and it would work IMHO if eth10 comes after eth1. But we should probably fix it anyway.

  3. added
    os-linux
    and removed
    untriaged
    on Sep 13, 2023
  4. added this to the 9.0.0 milestone on Sep 13, 2023
  5. added
    help wanted
    on Sep 13, 2023
  6. Clockwork-Muse commented on Sep 13, 2023

    @Clockwork-Muse
    Contributor

    Various parts of Linux allow you to rename various devices, including network interfaces. I've had to do that for a device deployment.

    I don't know if this file would be safe from or subject to the naming restrictions, but "interface index" might not be safe in those cases.

  7. ghost added
    in-pr
    on Sep 13, 2023
  8. ghost removed
    in-pr
    on Sep 13, 2023
  9. gfoidl commented on Sep 13, 2023

    @gfoidl
    Member

    Edit: please ignore, as I was not able to read the method to end (thank @MihaZupan for the hint)

    Aside from that

    while (!sr.EndOfStream)
    {
    string line = sr.ReadLine()!;
    if (line.Contains(name))
    {
    Span<Range> pieces = stackalloc Range[18]; // [0] skipped, [1]-[16] used, +1 to ensure any additional segment goes into [17]
    the stackalloc inside the loop may also be problematic. (isn't there an analyzer flagging this?)

  10. MihaZupan commented on Sep 13, 2023

    @MihaZupan
    Member

    We'll return right after the stackalloc, so it's fine in this case given it'll only run once.

  11. ghost added
    in-pr
    on Sep 17, 2023
  12. ghost removed
    in-pr
    on Oct 2, 2023
  13. ghost locked as resolved and limited conversation to collaborators on Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Nethelp wantedos-linux

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions