📄 mii-diag.c
字号:
/* Mode: C; * mii-diag.c: Examine and set the MII registers of a network interfaces. Usage: mii-diag [-vw] interface. This program reads and writes the Media Independent Interface (MII) management registers on network transceivers. The registers control and report network link settings and errors. Examples are link speed, duplex, capabilities advertised to the link partner, status LED indications and link error counters. Notes: The compile-command is at the end of this source file. This program works with drivers that implement MII ioctl() calls. Written/copyright 1997-2003 by Donald Becker <becker@scyld.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. The author may be reached as becker@scyld.com, or C/O Scyld Computing Corporation 914 Bay Ridge Road, Suite 220 Annapolis MD 21403 References http://scyld.com/expert/mii-status.html http://scyld.com/expert/NWay.html http://www.national.com/pf/DP/DP83840.html*/static char version[] ="mii-diag.c:v2.09 9/06/2003 Donald Becker (becker@scyld.com)\n"" http://www.scyld.com/diag/index.html\n";static const char usage_msg[] ="Usage: %s [--help] [-aDfrRvVw] [-AF <speed+duplex>] [--watch] <interface>.\n";static const char long_usage_msg[] ="Usage: %s [-aDfrRvVw] [-AF <speed+duplex>] [--watch] <interface>. This program configures and monitors the transceiver management registers for network interfaces. It uses the Media Independent Interface (MII) standard with additional Linux-specific controls to communicate with the underlying device driver. The MII registers control and report network link settings and errors. Examples are link speed, duplex, capabilities advertised to the link partner, status LED indications and link error counters. The common usage is mii-diag eth0 The default interface is \"eth0\". Frequently used options are -A --advertise <speed|setting> -F --fixed-speed <speed> Speed is one of: 100baseT4, 100baseTx, 100baseTx-FD, 100baseTx-HD, 10baseT, 10baseT-FD, 10baseT-HD -s --status Return exit status 2 if there is no link beat. Less frequently used options are -a --all-interfaces Show the status all interfaces (Not recommended with options that change settings.) -D --debug -g --read-parameters Get driver-specific parameters. -G --set-parameters PARMS Set driver-specific parameters. Parameters are comma separated, missing elements retain existing value. -M --msg-level LEVEL Set the driver message bit map. -p --phy ADDR Set the PHY (MII address) to report. -r --restart Restart the link autonegotiation. -R --reset Reset the transceiver. -v --verbose Report each action taken. -V --version Emit version information. -w --watch Continuously monitor the transceiver and report changes. This command returns success (zero) if the interface information can be read. If the --status option is passed, a zero return means that the interface has link beat.";#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <getopt.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#ifdef use_linux_libc5#include <linux/if_arp.h>#include <linux/if_ether.h>#endiftypedef unsigned short u16;typedef unsigned int u32;#if defined(SIOCGPARAMS) && SIOCGPARAMS != SIOCDEVPRIVATE+3#error Changed definition for SIOCGPARAMS#else#define SIOCGPARAMS (SIOCDEVPRIVATE+3) /* Read operational parameters. */#define SIOCSPARAMS (SIOCDEVPRIVATE+4) /* Set operational parameters. */#endifconst char shortopts[] = "aA:C:DfF:gG:hmM:p:rRsvVw?";struct option longopts[] = { /* { name has_arg *flag val } */ {"all-interfaces", 0, 0, 'a'}, /* Show all interfaces. */ {"advertise", 1, 0, 'A'}, /* Change the capabilities advertised. */ {"BMCR", 1, 0, 'C'}, /* Set the control register. */ {"debug", 0, 0, 'D'}, /* Increase the debug level. */ {"force", 0, 0, 'f'}, /* Force the operation. */ {"fixed-speed", 1, 0, 'F'}, /* Fixed speed name. */ {"read-parameters", 0, 0, 'g'}, /* Show general settings values. */ {"set-parameters", 1, 0, 'G'}, /* Write general settings values. */ {"help", 0, 0, 'h'}, /* Print a long usage message. */ {"monitor", 0, 0, 'm'}, /* Monitor status register. */ {"msg-level", 1, 0, 'M'}, /* Set the driver message level. */ {"phy", 1, 0, 'p'}, /* Set the PHY (MII address) to report. */ {"restart", 0, 0, 'r'}, /* Restart the link negotiation */ {"reset", 0, 0, 'R'}, /* Reset the transceiver. */ {"status", 0, 0, 's'}, /* Non-zero exit status w/ no link beat. */ {"verbose", 0, 0, 'v'}, /* Report each action taken. */ {"version", 0, 0, 'V'}, /* Emit version information. */ {"watch", 0, 0, 'w'}, /* Constantly monitor the port. */ {"error", 0, 0, '?'}, /* Return the error message. */ { 0, 0, 0, 0 }};/* Usually in libmii.c, but trivial substitions are below. */extern int show_mii_details(long ioaddr, int phy_id);extern void monitor_mii(long ioaddr, int phy_id);/* Command-line flags. */unsigned int opt_a = 0, /* Show-all-interfaces flag. */ opt_f = 0, /* Force the operation. */ opt_g = 0, opt_G = 0, verbose = 0, /* Verbose flag. */ debug = 0, opt_version = 0, opt_restart = 0, opt_reset = 0, opt_status = 0, opt_watch = 0;static int msg_level = -1;static int set_BMCR = -1;static int nway_advertise = 0;static int fixed_speed = -1;static int override_phy = -1;char *opt_G_string = NULL;/* Internal values. */int new_ioctl_nums;int skfd = -1; /* AF_INET socket for ioctl() calls. */struct ifreq ifr;int do_one_xcvr(int skfd);int show_basic_mii(long ioaddr, int phy_id);int mdio_read(int skfd, int phy_id, int location);void mdio_write(int skfd, int phy_id, int location, int value);static int parse_advertise(const char *capabilities);static void monitor_status(long ioaddr, int phy_id);intmain(int argc, char **argv){ int c, errflag = 0; char **spp, *ifname; char *progname = rindex(argv[0], '/') ? rindex(argv[0], '/')+1 : argv[0]; while ((c = getopt_long(argc, argv, shortopts, longopts, 0)) != EOF) switch (c) { case 'a': opt_a++; break; case 'A': nway_advertise |= parse_advertise(optarg); if (nway_advertise == -1) errflag++; break; case 'C': set_BMCR = strtoul(optarg, NULL, 16); break; case 'D': debug++; break; case 'f': opt_f++; break; case 'F': fixed_speed = parse_advertise(optarg); if (fixed_speed == -1) errflag++; break; case 'g': opt_g++; break; case 'G': opt_G++; opt_G_string = strdup(optarg); break; case 'm': opt_watch++; opt_status++; break; case 'M': msg_level = strtoul(optarg, NULL, 0); break; case 'h': fprintf(stderr, long_usage_msg, progname); return 0; case 'p': override_phy = atoi(optarg); break; case 'r': opt_restart++; break; case 'R': opt_reset++; break; case 's': opt_status++; break; case 'v': verbose++; break; case 'V': opt_version++; break; case 'w': opt_watch++; break; case '?': errflag++; break; } if (errflag) { fprintf(stderr, usage_msg, progname); return 2; } if (verbose || opt_version) printf(version); /* Open a basic socket. */ if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) { perror("socket"); return 1; } if (debug) fprintf(stderr, "DEBUG: argc=%d, optind=%d and argv[optind] is %s.\n", argc, optind, argv[optind]); /* No remaining args means show all interfaces. */ if (optind == argc) { ifname = "eth0"; fprintf(stderr, "Using the default interface 'eth0'.\n"); } else { /* Copy the interface name. */ spp = argv + optind; ifname = *spp++; } if (ifname == NULL) { ifname = "eth0"; fprintf(stderr, "Using the default interface 'eth0'.\n"); } /* Verify that the interface supports the ioctl(), and if it is using the new or old SIOCGMIIPHY value (grrr...). */ { u16 *data = (u16 *)(&ifr.ifr_data); strncpy(ifr.ifr_name, ifname, IFNAMSIZ); data[0] = 0; if (ioctl(skfd, 0x8947, &ifr) >= 0) { new_ioctl_nums = 1; } else if (ioctl(skfd, SIOCDEVPRIVATE, &ifr) >= 0) { new_ioctl_nums = 0; } else { fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", ifname, strerror(errno)); (void) close(skfd); return 1; } if (verbose) printf(" Using the %s SIOCGMIIPHY value on PHY %d " "(BMCR 0x%4.4x).\n", new_ioctl_nums ? "new" : "old", data[0], data[3]); } do_one_xcvr(skfd); (void) close(skfd); return 0;}int do_one_xcvr(int skfd){ u16 *data = (u16 *)(&ifr.ifr_data); u32 *data32 = (u32 *)(&ifr.ifr_data); unsigned phy_id = data[0]; if (override_phy >= 0) { printf("Using the specified MII PHY index %d.\n", override_phy); phy_id = override_phy; } if (opt_g || opt_G || msg_level >= 0) { if (ioctl(skfd, SIOCGPARAMS, &ifr) < 0) { fprintf(stderr, "SIOCGPARAMS on %s failed: %s\n", ifr.ifr_name, strerror(errno)); return -1; } } if (opt_g) { int i; printf("Driver general parameter settings:"); for (i = 0; i*sizeof(u32) < sizeof(ifr.ifr_ifru); i++) { printf(" %d", data32[i]); } printf(".\n"); } if (opt_G) { /* Set up to four arbitrary driver parameters from the -G parameter. The format is comma separated integers, with a missing element retaining the previous value. */ char *str = opt_G_string; int i; for (i = 0; str && i < 4; i++) { char *endstr; u32 newval = strtol(str, &endstr, 0); if (debug) printf(" parse string '%s' value %d end '%s'.\n", str, newval, endstr); if (str == endstr) { if (endstr[0] == ',') /* No parameter */ str = endstr+1; else { fprintf(stderr, "Invalid driver parameter '%s'.\n", str); str = index(str, ','); } } else if (endstr[0] == ',') { data32[i] = newval; str = endstr + 1; } else if (endstr[0] == 0) { data32[i] = newval; break; } } printf("Setting new driver general parameters:"); for (i = 0; i*sizeof(u32) < sizeof(ifr.ifr_ifru); i++) { printf(" %d", data32[i]); } printf(".\n"); if (ioctl(skfd, SIOCSPARAMS, &ifr) < 0) { fprintf(stderr, "SIOCSPARAMS on %s failed: %s\n", ifr.ifr_name, strerror(errno));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -