1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| /* 指定网速时,需要关闭自动协商吗?需要吗?不需要吗? 千兆有半双工吗?需要吗? -->测试发现,设置百兆、千兆时,同时开启自动协商,则会断网再连接一次。 如果不开自动协商,则不会断网,从千兆切换到百兆时会无效,故默认自动协商 */ #include <string.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/types.h> #include <net/if.h>
#include <linux/ethtool.h> #include <linux/sockios.h>
int ethtool_mygset(const char* devname, int* speed, int* duplex, int* autoneg, int* link) { struct ifreq ifr; int fd = 0; int err = -1;
struct ethtool_cmd ecmd; struct ethtool_value edata;
if (devname == NULL) return -2;
memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, devname);
fd = socket(AF_INET, SOCK_DGRAM, 0); printf("socket fd: %d\n", fd); if (fd < 0) { perror("ethtool_gset Cannot get control socket"); return -1; }
ecmd.cmd = ETHTOOL_GSET; ifr.ifr_data = (caddr_t)&ecmd; err = ioctl(fd, SIOCETHTOOL, &ifr);
if (err < 0) { perror("Cannot get device settings"); return -1; } printf("PHY xx - %d/%s ", ecmd.speed, (ecmd.duplex == DUPLEX_FULL) ? "Full" : "Half"); printf(" Auto-negotiation: %s ", (ecmd.autoneg == AUTONEG_DISABLE) ? "off" : "on");
switch (ecmd.speed) { case SPEED_10: case SPEED_100: case SPEED_1000: case SPEED_2500: case SPEED_10000: *speed = ecmd.speed; break; default: fprintf(stdout, "Unknown! (%i)\n", ecmd.speed); break; }; switch (ecmd.duplex) { case DUPLEX_HALF: case DUPLEX_FULL: *duplex = ecmd.duplex; break; default: fprintf(stdout, "Unknown! (%i)\n", ecmd.duplex); break; }; *autoneg = ecmd.autoneg;
edata.cmd = ETHTOOL_GLINK; ifr.ifr_data = (caddr_t)&edata; err = ioctl(fd, SIOCETHTOOL, &ifr); if (err == 0) { *link = edata.data ? 1: 0;
printf(" %s\n", edata.data ? "Up" : "Down"); } else if (errno != EOPNOTSUPP) { perror("Cannot get link status"); }
close(fd);
return 0; }
int ethtool_mysset(const char* devname, int speed, int duplex, int autoneg) { int speed_wanted = -1; int duplex_wanted = -1; int autoneg_wanted = AUTONEG_ENABLE; int advertising_wanted = -1; struct ethtool_cmd ecmd; struct ifreq ifr; int fd = 0; int err = -1;
// pass args if (devname == NULL) { printf("devname emtpy...\n"); return -2; } speed_wanted = speed; duplex_wanted = duplex; autoneg_wanted = autoneg;
strcpy(ifr.ifr_name, devname);
fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { perror("ethtool_sset Cannot get control socket"); return -1; }
ecmd.cmd = ETHTOOL_GSET; ifr.ifr_data = (caddr_t)&ecmd; err = ioctl(fd, SIOCETHTOOL, &ifr); if (err < 0) { perror("Cannot get current device settings"); return -1; }
if (speed_wanted != -1) ecmd.speed = speed_wanted; if (duplex_wanted != -1) ecmd.duplex = duplex_wanted; if (autoneg_wanted != -1) ecmd.autoneg = autoneg_wanted;
if ((autoneg_wanted == AUTONEG_ENABLE) && (advertising_wanted < 0)) { if (speed_wanted == SPEED_10 && duplex_wanted == DUPLEX_HALF) advertising_wanted = ADVERTISED_10baseT_Half; else if (speed_wanted == SPEED_10 && duplex_wanted == DUPLEX_FULL) advertising_wanted = ADVERTISED_10baseT_Full; else if (speed_wanted == SPEED_100 && duplex_wanted == DUPLEX_HALF) advertising_wanted = ADVERTISED_100baseT_Half; else if (speed_wanted == SPEED_100 && duplex_wanted == DUPLEX_FULL) advertising_wanted = ADVERTISED_100baseT_Full; else if (speed_wanted == SPEED_1000 && duplex_wanted == DUPLEX_HALF) advertising_wanted = ADVERTISED_1000baseT_Half; else if (speed_wanted == SPEED_1000 && duplex_wanted == DUPLEX_FULL) advertising_wanted = ADVERTISED_1000baseT_Full; else if (speed_wanted == SPEED_2500 && duplex_wanted == DUPLEX_FULL) advertising_wanted = ADVERTISED_2500baseX_Full; else if (speed_wanted == SPEED_10000 && duplex_wanted == DUPLEX_FULL) advertising_wanted = ADVERTISED_10000baseT_Full; else advertising_wanted = 0; }
if (advertising_wanted != -1) { if (advertising_wanted == 0) ecmd.advertising = ecmd.supported & (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full | ADVERTISED_2500baseX_Full | ADVERTISED_10000baseT_Full); else ecmd.advertising = advertising_wanted; }
ecmd.cmd = ETHTOOL_SSET; ifr.ifr_data = (caddr_t)&ecmd; err = ioctl(fd, SIOCETHTOOL, &ifr); if (err < 0) perror("Cannot set new settings");
if (err < 0) { if (speed_wanted != -1) fprintf(stderr, " not setting speed\n"); if (duplex_wanted != -1) fprintf(stderr, " not setting duplex\n"); if (autoneg_wanted != -1) fprintf(stderr, " not setting autoneg\n"); }
close(fd);
return 0; }
|