1

I have searched this for a while, but there was no clear keyword to find same question.

Briefly showing my question:

ifconfig only shows enable NICs.

# ifconfig
br0       Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          ...

eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
          inet addr:XX.XX.XX.XX  Bcast:XX.XX.XX.XX  Mask:XX.XX.XX.XX
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          ...

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          ...

However ip addr shows all NICs.

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> ... UNKNOWN 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> ... state UP
3: eth1: <BROADCAST,MULTICAST> ... state DOWN
8: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> ... state UNKNOWN

Based on the screen output, I knew that ifconfig knows whether NICs are enabled or not via string UP. (eg. LOOPBACK,UP,LOWER_UP)

As far as I know, that string is not hardware-dependent. Thus neither /sys/class/net/NIC/operstate nor /sys/class/net/NIC/carrier is the answer.


My question is: Where is that string come from?

(I guess that is contained in some file of filesystem.)

  • You may need to look at ifconfig's source code to get a precise answer, but I'd bet a huge amount that a kernel driver's status is not a string. – Julie Pelletier Jun 15 '16 at 6:42
  • Thanks for your suggestion, I am reading the code of ipconfig and iproute2 now. It will take much time for me to understand how it works. – Kir Chou Jun 15 '16 at 6:51
2

man ifconfig says:

Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

If no arguments are given, ifconfig displays the status of the currently active interfaces.

If a single interface argument is given, it displays the status of the given interface only;

if a single -a argument is given, it displays the status of all interfaces, even those that are down.

Otherwise, it configures an interface.

man netdevice also may share some light on the question. It uses the ioctl() system call.

Another technique is to use strace.

It gives you a list of all the system calls made by any program you pass it, along with their arguments and return values. If your program just dumps some info and quits rather than running for an extended time it can be pretty straightforward to just do a man on all the system calls you see that look like they might provide the info you're searching for.

When I run

strace ifconfig

Some of the interesting calls are:

open("/proc/net/dev", O_RDONLY)         = 6

followed by a bunch of ioctls:

ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
ioctl(5, SIOCGIFHWADDR, {ifr_name="eth0", ifr_hwaddr=12:cd:4b:bb:7f:39}) = 0
ioctl(5, SIOCGIFMETRIC, {ifr_name="eth0", ifr_metric=0}) = 0
ioctl(5, SIOCGIFMTU, {ifr_name="eth0", ifr_mtu=9001}) = 0
  • Thanks, SIOCGIFFLAGS definitely helps me a lot. – Kir Chou Jun 15 '16 at 7:40
0

Thanks for malyy's help. Here is my test code.

check_ifup.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>

int main(int argc, char* argv[])
{
    if (argc < 2)
    {   
        printf("Usage: ./check_ifup interface_name\n");
        return 0;
    }   

    struct ifreq ifr;
    int sockfd;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
    bzero(&ifr, sizeof(ifr));
    strcpy(ifr.ifr_name, argv[1]);
    ioctl(sockfd, SIOCGIFFLAGS, &ifr);

    if (ifr.ifr_flags & IFF_UP)    
        printf("%s is up\n", argv[1]);
    else
        printf("%s is down or unknown\n", argv[1]);                                                            

    close(sockfd);
    return 0
}

Reference:

  1. Definition of interface flags
  2. ifconfig.c
  3. Some tutorial (Chinese)
  4. Difference between IFF_UP and IFF_RUNNING

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.