From owner-svn-src-head@FreeBSD.ORG Tue Jun 5 21:35:48 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 90B621065675; Tue, 5 Jun 2012 21:35:48 +0000 (UTC) (envelope-from emax@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 71A5D8FC1F; Tue, 5 Jun 2012 21:35:48 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q55LZma4045852; Tue, 5 Jun 2012 21:35:48 GMT (envelope-from emax@svn.freebsd.org) Received: (from emax@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q55LZmvX045850; Tue, 5 Jun 2012 21:35:48 GMT (envelope-from emax@svn.freebsd.org) Message-Id: <201206052135.q55LZmvX045850@svn.freebsd.org> From: Maksim Yevmenkin Date: Tue, 5 Jun 2012 21:35:48 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r236644 - head/tools/tools/ifpifa X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jun 2012 21:35:48 -0000 Author: emax Date: Tue Jun 5 21:35:47 2012 New Revision: 236644 URL: http://svn.freebsd.org/changeset/base/236644 Log: Add a very simple debug tool that would dump list of interfaces, addresses on each interface, and, associated refcounter. I found it handy to check for address refcounter leaks. Added: head/tools/tools/ifpifa/ head/tools/tools/ifpifa/Makefile (contents, props changed) head/tools/tools/ifpifa/ifpifa.c (contents, props changed) Added: head/tools/tools/ifpifa/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/tools/ifpifa/Makefile Tue Jun 5 21:35:47 2012 (r236644) @@ -0,0 +1,10 @@ +# $FreeBSD$ + +PROG= ifpifa +NO_MAN= +WARNS?=6 +BINDIR?=/usr/local/bin +DPADD=${LIBKVM} +LDADD=-lkvm + +.include Added: head/tools/tools/ifpifa/ifpifa.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/tools/ifpifa/ifpifa.c Tue Jun 5 21:35:47 2012 (r236644) @@ -0,0 +1,190 @@ +/*- + * Copyright (c) 2012 maksim yevmenkin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* gcc -Wall -ggdb ifpifa.c -lkvm -o ifpifa */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +__FBSDID("$FreeBSD$"); + +static struct nlist nl[] = { +#define N_IFNET 0 + { .n_name = "_ifnet", }, + { .n_name = NULL, }, +}; + +static int +kread(kvm_t *kd, u_long addr, char *buffer, int size) +{ + if (kd == NULL || buffer == NULL) + return (-1); + + if (kvm_read(kd, addr, buffer, size) != size) { + warnx("kvm_read: %s", kvm_geterr(kd)); + return (-1); + } + + return (0); +} + +int +main(void) +{ + kvm_t *kd; + char errbuf[_POSIX2_LINE_MAX]; + u_long ifnetaddr, ifnetaddr_next; + u_long ifaddraddr, ifaddraddr_next; + struct ifnet ifnet; + struct ifnethead ifnethead; + union { + struct ifaddr ifa; + struct in_ifaddr in; + struct in6_ifaddr in6; + } ifaddr; + union { + struct sockaddr *sa; + struct sockaddr_dl *sal; + struct sockaddr_in *sa4; + struct sockaddr_in6 *sa6; + } sa; + char addr[INET6_ADDRSTRLEN]; + + kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); + if (kd == NULL) { + warnx("kvm_openfiles: %s", errbuf); + exit(0); + } + + if (kvm_nlist(kd, nl) < 0) { + warnx("kvm_nlist: %s", kvm_geterr(kd)); + goto out; + } + + if (nl[N_IFNET].n_type == 0) { + warnx("kvm_nlist: no namelist"); + goto out; + } + + if (kread(kd, nl[N_IFNET].n_value, + (char *) &ifnethead, sizeof(ifnethead)) != 0) + goto out; + + for (ifnetaddr = (u_long) TAILQ_FIRST(&ifnethead); + ifnetaddr != 0; + ifnetaddr = ifnetaddr_next) { + if (kread(kd, ifnetaddr, (char *) &ifnet, sizeof(ifnet)) != 0) + goto out; + ifnetaddr_next = (u_long) TAILQ_NEXT(&ifnet, if_link); + + printf("%s\n", ifnet.if_xname); + + for (ifaddraddr = (u_long) TAILQ_FIRST(&ifnet.if_addrhead); + ifaddraddr != 0; + ifaddraddr = ifaddraddr_next) { + if (kread(kd, ifaddraddr, + (char *) &ifaddr, sizeof(ifaddr)) != 0) + goto out; + + ifaddraddr_next = (u_long) + TAILQ_NEXT(&ifaddr.ifa, ifa_link); + + sa.sa = (struct sockaddr *)( + (unsigned char *) ifaddr.ifa.ifa_addr - + (unsigned char *) ifaddraddr + + (unsigned char *) &ifaddr); + + switch (sa.sa->sa_family) { + case AF_LINK: + switch (sa.sal->sdl_type) { + case IFT_ETHER: + case IFT_FDDI: + ether_ntoa_r((struct ether_addr * ) + LLADDR(sa.sal), addr); + break; + + case IFT_LOOP: + strcpy(addr, "loopback"); + break; + + default: + snprintf(addr, sizeof(addr), + "", + sa.sal->sdl_type); + break; + } + break; + + case AF_INET: + inet_ntop(AF_INET, &sa.sa4->sin_addr, + addr, sizeof(addr)); + break; + + case AF_INET6: + inet_ntop(AF_INET6, &sa.sa6->sin6_addr, + addr, sizeof(addr)); + break; + + default: + snprintf(addr, sizeof(addr), "family=%d", + sa.sa->sa_family); + break; + } + + printf("\t%s ifa_refcnt=%u\n", + addr, ifaddr.ifa.ifa_refcnt); + } + } +out: + kvm_close(kd); + + return (0); +} +