Repository navigation
Closed
Labels
area-System.Nethelp wanted[up-for-grabs] Good issue for external contributorsos-linuxLinux OS (any supported distro)
Milestone
Description
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.
Lines 404 to 446 in 3aeefbd
| 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?
Metadata
Metadata
Assignees
Labels
area-System.Nethelp wanted[up-for-grabs] Good issue for external contributorsos-linuxLinux OS (any supported distro)
Activity
wfurt commentedon Sep 13, 2023
The
Containsseems problematic. However, the file seems to be ordered by interface index and it would work IMHO ifeth10comes aftereth1. But we should probably fix it anyway.Clockwork-Muse commentedon Sep 13, 2023
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.
gfoidl commentedon Sep 13, 2023
Edit: please ignore, as I was not able to read the method to end (thank @MihaZupan for the hint)
Aside from that
runtime/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/StringParsingHelpers.Statistics.cs
Lines 411 to 416 in 3aeefbd
stackallocinside the loop may also be problematic. (isn't there an analyzer flagging this?)MihaZupan commentedon Sep 13, 2023
We'll return right after the
stackalloc, so it's fine in this case given it'll only run once.