forked from jbenc/plotnetcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethtool.c
106 lines (95 loc) · 2.62 KB
/
ethtool.c
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
/*
* This file is a part of plotnetcfg, a tool to visualize network config.
* Copyright (C) 2014 Red Hat, Inc. -- Jiri Benc <[email protected]>
*
* 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.
*/
#include "ethtool.h"
#include <errno.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
static int ethtool_ioctl(const char *ifname, void *data)
{
struct ifreq ifr;
int fd, err = 0;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
return errno;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
ifr.ifr_data = data;
if (ioctl(fd, SIOCETHTOOL, &ifr) < 0)
err = errno;
close(fd);
return err;
}
char *ethtool_driver(const char *ifname)
{
struct ethtool_drvinfo info;
memset(&info, 0, sizeof(info));
info.cmd = ETHTOOL_GDRVINFO;
if (ethtool_ioctl(ifname, &info))
return NULL;
return strdup(info.driver);
}
unsigned int ethtool_veth_peer(const char *ifname)
{
struct ethtool_drvinfo info;
struct ethtool_gstrings *strs;
struct ethtool_stats *stats;
unsigned int i, res = 0;
memset(&info, 0, sizeof(info));
info.cmd = ETHTOOL_GDRVINFO;
if (ethtool_ioctl(ifname, &info))
return 0;
if (!info.n_stats)
return 0;
strs = malloc(sizeof(struct ethtool_gstrings) + info.n_stats * ETH_GSTRING_LEN);
if (!strs)
return 0;
memset(strs, 0, sizeof(struct ethtool_gstrings));
strs->cmd = ETHTOOL_GSTRINGS;
strs->string_set = ETH_SS_STATS;
strs->len = info.n_stats;
if (ethtool_ioctl(ifname, strs))
goto fail_strs;
if (strs->len != info.n_stats)
goto fail_strs;
stats = malloc(sizeof(struct ethtool_stats) + info.n_stats * sizeof(__u64));
if (!stats)
goto fail_strs;
memset(stats, 0, sizeof(struct ethtool_stats));
stats->cmd = ETHTOOL_GSTATS;
stats->n_stats = info.n_stats;
if (ethtool_ioctl(ifname, stats))
goto fail_stats;
if (stats->n_stats != info.n_stats)
goto fail_stats;
for (i = 0; i < info.n_stats; i++) {
if (!strcmp((char *)strs->data + i * ETH_GSTRING_LEN, "peer_ifindex")) {
res = stats->data[i];
break;
}
}
fail_stats:
free(stats);
fail_strs:
free(strs);
return res;
}