From patchwork Mon Oct 18 20:11:04 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: stephen hemminger X-Patchwork-Id: 68237 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 11ADDB70E4 for ; Tue, 19 Oct 2010 07:11:41 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933142Ab0JRULI (ORCPT ); Mon, 18 Oct 2010 16:11:08 -0400 Received: from mail.vyatta.com ([76.74.103.46]:44647 "EHLO mail.vyatta.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757716Ab0JRULH (ORCPT ); Mon, 18 Oct 2010 16:11:07 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.vyatta.com (Postfix) with ESMTP id 1E2811828E97; Mon, 18 Oct 2010 13:11:06 -0700 (PDT) X-Virus-Scanned: amavisd-new at tahiti.vyatta.com Received: from mail.vyatta.com ([127.0.0.1]) by localhost (mail.vyatta.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id iA1u1yHdF2Ue; Mon, 18 Oct 2010 13:11:05 -0700 (PDT) Received: from nehalam (pool-74-107-135-205.ptldor.fios.verizon.net [74.107.135.205]) by mail.vyatta.com (Postfix) with ESMTPSA id 219DC1828980; Mon, 18 Oct 2010 13:11:05 -0700 (PDT) Date: Mon, 18 Oct 2010 13:11:04 -0700 From: Stephen Hemminger To: Ben Hutchings , Jeff Garzik Cc: netdev@vger.kernel.org Subject: [PATCH] ethtool: add get permanent address option (v2) Message-ID: <20101018131104.02681d60@nehalam> In-Reply-To: <1287431541.2252.556.camel@achroite.uk.solarflarecom.com> References: <20101018114139.71073a40@nehalam> <1287431541.2252.556.camel@achroite.uk.solarflarecom.com> Organization: Vyatta X-Mailer: Claws Mail 3.7.6 (GTK+ 2.22.0; x86_64-pc-linux-gnu) Mime-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add command level support for showing permanent address. The ioctl has been around for a long time but there was no option to display it. Note: MAX_ADDR_LEN is defined in netdevice.h but including netdevice.h leads to multiple definition errors with if.h. Signed-off-by: Stephen Hemminger --- Fix perror(), memory leak and indenting (v2) ethtool.8 | 6 ++++++ ethtool.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletions(-) diff --git a/ethtool.8 b/ethtool.8 index 3ca403c..84d5cc0 100644 --- a/ethtool.8 +++ b/ethtool.8 @@ -176,6 +176,9 @@ ethtool \- Display or change ethernet card settings .I ethX .RI [ N ] +.B ethtool \-P|\-\-show-permaddr +.I ethX + .B ethtool \-r|\-\-negotiate .I ethX @@ -388,6 +391,9 @@ blinking one or more LEDs on the specific ethernet port. .B N Length of time to perform phys-id, in seconds. .TP +.B \-P \-\-show-permaddr +Queries the specified ethernet device for permanent hardware address. +.TP .B \-r \-\-negotiate Restarts auto-negotiation on the specified ethernet device, if auto-negotiation is enabled. diff --git a/ethtool.c b/ethtool.c index 6b2b7c8..1326f54 100644 --- a/ethtool.c +++ b/ethtool.c @@ -51,6 +51,9 @@ #ifndef SIOCETHTOOL #define SIOCETHTOOL 0x8946 #endif +#ifndef MAX_ADDR_LEN +#define MAX_ADDR_LEN 32 +#endif #ifndef ARRAY_SIZE #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #endif @@ -107,6 +110,8 @@ static int do_srxfhindir(int fd, struct ifreq *ifr); static int do_srxntuple(int fd, struct ifreq *ifr); static int do_grxntuple(int fd, struct ifreq *ifr); static int do_flash(int fd, struct ifreq *ifr); +static int do_permaddr(int fd, struct ifreq *ifr); + static int send_ioctl(int fd, struct ifreq *ifr); static enum { @@ -136,6 +141,7 @@ static enum { MODE_SNTUPLE, MODE_GNTUPLE, MODE_FLASHDEV, + MODE_PERMADDR, } mode = MODE_GSET; static struct option { @@ -247,6 +253,8 @@ static struct option { "action \n" }, { "-u", "--show-ntuple", MODE_GNTUPLE, "Get Rx ntuple filters and actions\n" }, + { "-P", "--show-permaddr", MODE_PERMADDR, + "Show permanent hardware address" }, { "-h", "--help", MODE_HELP, "Show this help" }, {} }; @@ -750,7 +758,8 @@ static void parse_cmdline(int argc, char **argp) (mode == MODE_SNTUPLE) || (mode == MODE_GNTUPLE) || (mode == MODE_PHYS_ID) || - (mode == MODE_FLASHDEV)) { + (mode == MODE_FLASHDEV) | + (mode == MODE_PERMADDR)) { devname = argp[i]; break; } @@ -1868,6 +1877,8 @@ static int doit(void) return do_grxntuple(fd, &ifr); } else if (mode == MODE_FLASHDEV) { return do_flash(fd, &ifr); + } else if (mode == MODE_PERMADDR) { + return do_permaddr(fd, &ifr); } return 69; @@ -2950,6 +2961,31 @@ static int do_flash(int fd, struct ifreq *ifr) return err; } +static int do_permaddr(int fd, struct ifreq *ifr) +{ + int i, err; + struct ethtool_perm_addr *epaddr; + + epaddr = malloc(sizeof(struct ethtool_perm_addr) + MAX_ADDR_LEN); + epaddr->cmd = ETHTOOL_GPERMADDR; + epaddr->size = MAX_ADDR_LEN; + ifr->ifr_data = (caddr_t)epaddr; + + err = send_ioctl(fd, ifr); + if (err < 0) + perror("Cannot read permanent address"); + else { + printf("Permanent address:"); + for (i = 0; i < epaddr->size; i++) + printf("%c%02x", (i == 0) ? ' ' : ':', + epaddr->data[i]); + printf("\n"); + } + free(epaddr); + + return err; +} + static int do_srxntuple(int fd, struct ifreq *ifr) { int err;