Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 10 Feb 2016 07:37:20 -0800
From:      Ravi Pokala <rpokala@mac.com>
To:        "freebsd-net@freebsd.org" <freebsd-net@freebsd.org>
Subject:   Re: C program API to determine negotiated link speed of a network interface?
Message-ID:  <C0E0F530-712E-4840-AFFD-CA21F2D35236@panasas.com>

next in thread | raw e-mail | index | archive | help
>Date: Tue, 9 Feb 2016 22:51:27 +0000 (UTC)
>From: Pallav Bose <pallav_bose@yahoo.com>
>To: "freebsd-net@freebsd.org" <freebsd-net@freebsd.org>
>Subject: C program API to determine negotiated link speed of a network
>	interface?
>Message-ID:
>	<249322925.1631277.1455058288002.JavaMail.yahoo@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@)




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?C0E0F530-712E-4840-AFFD-CA21F2D35236>