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

Ravi Pokala rpokala at mac.com
Wed Feb 10 16:37:29 UTC 2016


>Date: Tue, 9 Feb 2016 22:51:27 +0000 (UTC)
>From: Pallav Bose <pallav_bose at yahoo.com>
>To: "freebsd-net at freebsd.org" <freebsd-net at freebsd.org>
>Subject: C program API to determine negotiated link speed of a network
>	interface?
>Message-ID:
>	<249322925.1631277.1455058288002.JavaMail.yahoo at mail.yahoo.com>
>Content-Type: text/plain; charset=UTF-8
>
>Hi,
>I'm writing a C program to list all available interfaces and their link speed. ...
>
>...
>
>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?
>
>...
>
>Thanks,Pallav

Hi Pallav,

Take a look at sbin/ifconfig/ifmedia.c::media_status(). Basically, you call ioctl(SIOCGIFMEDIA) once to get the number of media types for the NIC, allocate local memory to hold the list, then call ioctl(SIOCGIFMEDIA) again to copy the list into that local buffer. Then print_media_word() parses it and emits (amongst other things) the speed.

Alas, print_media_word () and several of the helper functions it calls are (a) not part of a library, so you have to re-implement them yourself in your own sources and (b) are copy/pasted between ifconfig and etherswitch. That screams for refactoring, and if/when that happens, it should have a public interface.

Hope that helps,

Ravi (rpokala@)



More information about the freebsd-net mailing list