ifconfig: how does it even?
I can already measure the latency of TCP sockets over the loopback interface. I want to compare this to TCP sockets connecting to one of the ‘real’ addresses the machine has, to see if it’s any different. I could see this being either the same as the loopback interface, or being slower. I’m way below the level I have any real knowledge of at this point, so there’s only one way to find out.
But rather than hardcode in the IP addresses, or take them on the command line,
I want the benchmark to find them itself. One thing I want out of these
benchmarks is for them to build on any Linux system, and run without needing
machine-specific arguments. When I find out what IP addresses my machine
has, I use ifconfig
. But how does ifconfig
do it? I was about to Google the
answer when I realised this would be a perfect time use strace
, and so I did!
I just straced ifconfig to find out how it finds out which interfaces exist! /cc @b0rk
— Kamal Marhubi (@kamalmarhubi) June 10, 2015
Here’s the quick summary of what happens:
- create sockets of both
AF_INET
andAF_INET6
families - read
/proc/net/dev
to get a complete list of interfaces; we’ll need this to get the addresses forAF_INET6
socket, as well as to get the list of interfaces that don’t have anAF_INET
address - use the
SIOCGIFCONF
ioctl
to get a list of addresses forAF_INET
- loop through the interface names that came from
/proc/net/dev
and for each one- read
/proc/net/if_inet6
to get the IPv6 address for the interface, if any - use a series of
ioctl
calls to get data about the interface
- read
To see in more detail, take a look at this gist for an annotated
strace
of ifconfig
and ifconfig -s
.