From owner-freebsd-net@freebsd.org Wed Feb 10 16:37:29 2016 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 08782AA4D91 for ; Wed, 10 Feb 2016 16:37:29 +0000 (UTC) (envelope-from rpokala@mac.com) Received: from mr11p00im-asmtp004.me.com (mr11p00im-asmtp004.me.com [17.110.69.135]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id EAE44CC5 for ; Wed, 10 Feb 2016 16:37:28 +0000 (UTC) (envelope-from rpokala@mac.com) Received: from [192.168.1.4] (c-24-6-178-251.hsd1.ca.comcast.net [24.6.178.251]) by mr11p00im-asmtp004.me.com (Oracle Communications Messaging Server 7.0.5.36.0 64bit (built Sep 8 2015)) with ESMTPSA id <0O2C005LQ8Q9MF00@mr11p00im-asmtp004.me.com> for freebsd-net@freebsd.org; Wed, 10 Feb 2016 15:37:22 +0000 (GMT) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-02-10_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1602100287 User-Agent: Microsoft-MacOutlook/0.0.0.160109 Date: Wed, 10 Feb 2016 07:37:20 -0800 Subject: Re: C program API to determine negotiated link speed of a network interface? From: Ravi Pokala Sender: "Pokala, Ravi" To: "freebsd-net@freebsd.org" Message-id: Thread-topic: C program API to determine negotiated link speed of a network interface? MIME-version: 1.0 Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 7bit X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Feb 2016 16:37:29 -0000 >Date: Tue, 9 Feb 2016 22:51:27 +0000 (UTC) >From: Pallav Bose >To: "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@)