C program API to determine negotiated link speed of a network interface?

Pallav Bose pallav_bose at yahoo.com
Tue Feb 9 22:57:29 UTC 2016


Hi,
I'm writing a C program to list all available interfaces and their link speed. I can use getifaddrs(3) to obtain a list of network interfaces in a struct ifaddrs, but none of the fields in this struct gives me information about the negotiated link speed. From the man page of getifaddrs(3):
The ifaddrs structure contains at least the following entries:

     struct    ifaddrs    *ifa_next;      /* Pointer to next struct */
     char                 *ifa_name;      /* Interface name */
     u_int                ifa_flags;      /* Interface flags    */
     struct    sockaddr  *ifa_addr;       /* Interface address */
     struct    sockaddr  *ifa_netmask;    /* Interface netmask */
     struct    sockaddr  *ifa_broadaddr;  /* Interface broadcast address */
     struct    sockaddr  *ifa_dstaddr;    /* P2P interface destination */
     void                *ifa_data;       /* Address specific data */

Running truss on ifconfig(8) tells me that the ioctl SIOCGIFMEDIA can be used, but it is not clear to me how. Is there an API in C which does this already?
# truss ifconfig em0....<snipped>....
ioctl(3,SIOCGIFMAC,0xffffe210)                   ERR#22 'Invalid argument'
ioctl(3,SIOCGIFMEDIA,0xffffe1f0)                 = 0 (0x0)
ioctl(3,SIOCGIFMEDIA,0xffffe1f0)                 = 0 (0x0)
        media: Ethernet autoselect (1000baseT <full-duplex>)
write(1,"\tmedia: Ethernet autoselect (10"...,54) = 54 (0x36)
        status: active
write(1,"\tstatus: active\n",16)                 = 16 (0x10)

Thanks,Pallav


More information about the freebsd-net mailing list