summaryrefslogtreecommitdiffstats
path: root/isys/ethtool.c
blob: 871f1d4fc1b45b66182ee3fcc7c7bb64242154e1 (plain)
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
/*
 * ethtool.c - setting of basic ethtool options
 *
 * Copyright (C) 2003  Red Hat, Inc.  All rights reserved.
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): Jeremy Katz <katzj@redhat.com>
 */

#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>

#include <linux/sockios.h>
#include "ethtool.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;
}

int identifyNIC(char *iface, int seconds) {
    int sock;
    struct ethtool_value edata;
    struct ifreq ifr;

    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("Unable to create socket");
        return -1;
    }

    memset(&ifr, 0, sizeof(ifr));
    memset(&edata, 0, sizeof(edata));

    strcpy(ifr.ifr_name, iface);
    edata.cmd = ETHTOOL_PHYS_ID;
    edata.data = seconds;
    ifr.ifr_data = (caddr_t) &edata;

    if (ioctl(sock, SIOCETHTOOL, &ifr) < 0) {
        perror("Unable to identify NIC");
    }

    return 0;
}