]> git.sur5r.net Git - i3/i3status/blob - src/print_eth_info.c
fix ethernet speed display for 100 Gbit/s cards
[i3/i3status] / src / print_eth_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/ioctl.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <net/if.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <yajl/yajl_gen.h>
14 #include <yajl/yajl_version.h>
15
16 #include "i3status.h"
17
18 #if defined(__linux__)
19 #include <linux/ethtool.h>
20 #include <linux/sockios.h>
21 #define PART_ETHSPEED "E: %s (%d Mbit/s)"
22 #endif
23
24 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
25 #include <net/if_media.h>
26
27 #define PART_ETHSPEED "E: %s (%s)"
28 #endif
29
30 #if defined(__OpenBSD__) || defined(__NetBSD__)
31 #include <errno.h>
32 #include <net/if_media.h>
33 #endif
34
35 static int print_eth_speed(char *outwalk, const char *interface) {
36 #if defined(__linux__)
37     int ethspeed = 0;
38     struct ifreq ifr;
39     struct ethtool_cmd ecmd;
40
41     ecmd.cmd = ETHTOOL_GSET;
42     (void)memset(&ifr, 0, sizeof(ifr));
43     ifr.ifr_data = (caddr_t)&ecmd;
44     (void)strcpy(ifr.ifr_name, interface);
45     if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0) {
46         ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ethtool_cmd_speed(&ecmd));
47         return sprintf(outwalk, "%d Mbit/s", ethspeed);
48     } else
49         return sprintf(outwalk, "?");
50 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
51     const char *ethspeed;
52     struct ifmediareq ifm;
53     (void)memset(&ifm, 0, sizeof(ifm));
54     (void)strncpy(ifm.ifm_name, interface, sizeof(ifm.ifm_name));
55     int ret;
56 #ifdef SIOCGIFXMEDIA
57     ret = ioctl(general_socket, SIOCGIFXMEDIA, (caddr_t)&ifm);
58     if (ret < 0)
59 #endif
60         ret = ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifm);
61     if (ret < 0)
62         return sprintf(outwalk, "?");
63
64     /* Get the description of the media type, partially taken from
65      * FreeBSD's ifconfig */
66     const struct ifmedia_description *desc;
67     static struct ifmedia_description ifm_subtype_descriptions[] =
68         IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
69
70     if (IFM_TYPE(ifm.ifm_active) != IFM_ETHER)
71         return sprintf(outwalk, "?");
72     if (ifm.ifm_status & IFM_AVALID && !(ifm.ifm_status & IFM_ACTIVE))
73         return sprintf(outwalk, "no carrier");
74     for (desc = ifm_subtype_descriptions;
75          desc->ifmt_string != NULL;
76          desc++) {
77         if (desc->ifmt_word == IFM_SUBTYPE(ifm.ifm_active))
78             break;
79     }
80     ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
81     return sprintf(outwalk, "%s", ethspeed);
82 #elif defined(__OpenBSD__) || defined(__NetBSD__)
83     const char *ethspeed;
84     struct ifmediareq ifmr;
85
86     (void)memset(&ifmr, 0, sizeof(ifmr));
87     (void)strlcpy(ifmr.ifm_name, interface, sizeof(ifmr.ifm_name));
88
89     if (ioctl(general_socket, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
90         if (errno != E2BIG)
91             return sprintf(outwalk, "?");
92     }
93
94     struct ifmedia_description *desc;
95     struct ifmedia_description ifm_subtype_descriptions[] =
96         IFM_SUBTYPE_DESCRIPTIONS;
97
98     for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; desc++) {
99         /*
100                  * Skip these non-informative values and go right ahead to the
101                  * actual speeds.
102                  */
103         if (BEGINS_WITH(desc->ifmt_string, "autoselect") ||
104             BEGINS_WITH(desc->ifmt_string, "auto"))
105             continue;
106
107         if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) &&
108             IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmr.ifm_active))
109             break;
110     }
111     ethspeed = (desc->ifmt_string != NULL ? desc->ifmt_string : "?");
112     return sprintf(outwalk, "%s", ethspeed);
113
114 #else
115     return sprintf(outwalk, "?");
116 #endif
117 }
118
119 /*
120  * Combines ethernet IP addresses and speed (if requested) for displaying
121  *
122  * Table summarizing what is the decision to prefer IPv4 or IPv6
123  * based their values.
124  *
125  * | ipv4_address | ipv6_address | Chosen IP | Color             |
126  * |--------------|--------------|-----------|-------------------|
127  * | NULL         | NULL         | None      | bad (red)         |
128  * | NULL         | no IP        | IPv6      | degraded (orange) |
129  * | NULL         | ::1/128      | IPv6      | ok (green)        |
130  * | no IP        | NULL         | IPv4      | degraded          |
131  * | no IP        | no IP        | IPv4      | degraded          |
132  * | no IP        | ::1/128      | IPv6      | ok                |
133  * | 127.0.0.1    | NULL         | IPv4      | ok                |
134  * | 127.0.0.1    | no IP        | IPv4      | ok                |
135  * | 127.0.0.1    | ::1/128      | IPv4      | ok                |
136  */
137 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
138     const char *format = format_down;  // default format
139
140     const char *walk;
141     char *outwalk = buffer;
142
143     INSTANCE(interface);
144
145     char *ipv4_address = sstrdup(get_ip_addr(interface, AF_INET));
146     char *ipv6_address = sstrdup(get_ip_addr(interface, AF_INET6));
147
148     /*
149      * Removing '%' and following characters from IPv6 since the interface identifier is redundant,
150      * as the output already includes the interface name.
151     */
152     if (ipv6_address != NULL) {
153         char *prct_ptr = strstr(ipv6_address, "%");
154         if (prct_ptr != NULL) {
155             *prct_ptr = '\0';
156         }
157     }
158
159     bool prefer_ipv4 = true;
160     if (ipv4_address == NULL) {
161         if (ipv6_address == NULL) {
162             START_COLOR("color_bad");
163             goto out;
164         } else {
165             prefer_ipv4 = false;
166         }
167     } else if (BEGINS_WITH(ipv4_address, "no IP") && ipv6_address != NULL && !BEGINS_WITH(ipv6_address, "no IP")) {
168         prefer_ipv4 = false;
169     }
170
171     format = format_up;
172
173     const char *ip_address = (prefer_ipv4) ? ipv4_address : ipv6_address;
174     if (BEGINS_WITH(ip_address, "no IP")) {
175         START_COLOR("color_degraded");
176     } else {
177         START_COLOR("color_good");
178     }
179
180 out:
181     for (walk = format; *walk != '\0'; walk++) {
182         if (*walk != '%') {
183             *(outwalk++) = *walk;
184
185         } else if (BEGINS_WITH(walk + 1, "ip")) {
186             outwalk += sprintf(outwalk, "%s", ip_address);
187             walk += strlen("ip");
188
189         } else if (BEGINS_WITH(walk + 1, "speed")) {
190             outwalk += print_eth_speed(outwalk, interface);
191             walk += strlen("speed");
192
193         } else if (BEGINS_WITH(walk + 1, "interface")) {
194             outwalk += sprintf(outwalk, "%s", interface);
195             walk += strlen("interface");
196
197         } else {
198             *(outwalk++) = '%';
199         }
200     }
201     END_COLOR;
202     free(ipv4_address);
203     free(ipv6_address);
204     OUTPUT_FULL_TEXT(buffer);
205 }