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
|
/*
* ethtool.c - setting of basic ethtool options
*
* Copyright 2003 Red Hat, Inc.
*
* Jeremy Katz <katzj@redhat.com>
*
* This software may be freely redistributed under the terms of the GNU
* general public license.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#ifdef DIET
typedef void * caddr_t;
#endif
#include <linux/sockios.h>
#include "net.h"
#include "ethtool-copy.h"
static int set_intf_up(struct ifreq ifr, int sock) {
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
return (-1);
}
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
fprintf(stderr, "failed to bring up interface %s: %s", ifr.ifr_name,
strerror(errno));
return -1;
}
return (0);
}
int setEthtoolSettings(char * dev, ethtool_speed speed,
ethtool_duplex duplex) {
int sock, err;
struct ethtool_cmd ecmd;
struct ifreq ifr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Unable to create socket");
return -1;
}
/* Setup our control structures. */
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, dev);
if (set_intf_up(ifr, sock) == -1) {
fprintf(stderr, "unable to bring up interface %s: %s", dev,
strerror(errno));
return -1;
}
ecmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = (caddr_t)&ecmd;
err = ioctl(sock, SIOCETHTOOL, &ifr);
if (err < 0) {
perror("Unable to get settings via ethtool. Not setting");
return -1;
}
if (speed != ETHTOOL_SPEED_UNSPEC)
ecmd.speed = speed;
if (duplex != ETHTOOL_DUPLEX_UNSPEC)
ecmd.duplex = duplex;
if ((duplex != ETHTOOL_DUPLEX_UNSPEC) || (speed != ETHTOOL_SPEED_UNSPEC))
ecmd.autoneg = AUTONEG_DISABLE;
ecmd.cmd = ETHTOOL_SSET;
ifr.ifr_data = (caddr_t)&ecmd;
err = ioctl(sock, SIOCETHTOOL, &ifr);
if (err < 0) {
// perror("Unable to set settings via ethtool. Not setting");
return -1;
}
return 0;
}
|