summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/hud/hud_nic.c
blob: 36088a07ed42788ce00edbbf2ef612749bd9c5e9 (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**************************************************************************
 *
 * Copyright (C) 2016 Steven Toth <stoth@kernellabs.com>
 * Copyright (C) 2016 Zodiac Inflight Innovations
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#if HAVE_GALLIUM_EXTRA_HUD

/* Purpose: Reading network interface RX/TX throughput per second,
 * displaying on the HUD.
 */

#include "hud/hud_private.h"
#include "util/list.h"
#include "os/os_time.h"
#include "util/u_memory.h"
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/wireless.h>

#define LOCAL_DEBUG 0

struct nic_info
{
   struct list_head list;
   int mode;
   char name[64];
   uint64_t speedMbps;
   int is_wireless;

   char throughput_filename[128];
   uint64_t last_time;
   uint64_t last_nic_bytes;
};

/* TODO: We don't handle dynamic NIC arrival or removal.
 * Static globals specific to this HUD category.
 */
static int gnic_count = 0;
static struct list_head gnic_list;

static struct nic_info *
find_nic_by_name(const char *n, int mode)
{
   list_for_each_entry(struct nic_info, nic, &gnic_list, list) {
      if (nic->mode != mode)
         continue;

      if (strcasecmp(nic->name, n) == 0)
         return nic;
   }
   return 0;
}

static int
get_file_value(const char *fname, uint64_t *value)
{
   FILE *fh = fopen(fname, "r");
   if (!fh)
      return -1;
   if (fscanf(fh, "%" PRIu64 "", value) != 0) {
      /* Error */
   }
   fclose(fh);
   return 0;
}

static boolean
get_nic_bytes(const char *fn, uint64_t *bytes)
{
   if (get_file_value(fn, bytes) < 0)
      return FALSE;

   return TRUE;
}

static void
query_wifi_bitrate(const struct nic_info *nic, uint64_t *bitrate)
{
   int sockfd;
   struct iw_statistics stats;
   struct iwreq req;

   memset(&stats, 0, sizeof(stats));
   memset(&req, 0, sizeof(req));

   strcpy(req.ifr_name, nic->name);
   req.u.data.pointer = &stats;
   req.u.data.flags = 1;
   req.u.data.length = sizeof(struct iw_statistics);

   /* Any old socket will do, and a datagram socket is pretty cheap */
   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
      fprintf(stderr, "Unable to create socket for %s\n", nic->name);
      return;
   }

   if (ioctl(sockfd, SIOCGIWRATE, &req) == -1) {
      fprintf(stderr, "Error performing SIOCGIWSTATS on %s\n", nic->name);
      close(sockfd);
      return;
   }
   *bitrate = req.u.bitrate.value;

   close(sockfd);
}

static void
query_nic_rssi(const struct nic_info *nic, uint64_t *leveldBm)
{
   int sockfd;
   struct iw_statistics stats;
   struct iwreq req;

   memset(&stats, 0, sizeof(stats));
   memset(&req, 0, sizeof(req));

   strcpy(req.ifr_name, nic->name);
   req.u.data.pointer = &stats;
   req.u.data.flags = 1;
   req.u.data.length = sizeof(struct iw_statistics);

   if (nic->mode != NIC_RSSI_DBM)
      return;

   /* Any old socket will do, and a datagram socket is pretty cheap */
   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
      fprintf(stderr, "Unable to create socket for %s\n", nic->name);
      return;
   }

   /* Perform the ioctl */
   if (ioctl(sockfd, SIOCGIWSTATS, &req) == -1) {
      fprintf(stderr, "Error performing SIOCGIWSTATS on %s\n", nic->name);
      close(sockfd);
      return;
   }
   *leveldBm = ((char) stats.qual.level * -1);

   close(sockfd);

#if LOCAL_DEBUG
   printf("NIC signal level%s is %d%s.\n",
          (stats.qual.updated & IW_QUAL_DBM ? " (in dBm)" : ""),
          (char) stats.qual.level,
          (stats.qual.updated & IW_QUAL_LEVEL_UPDATED ? " (updated)" : ""));
#endif
}

static void
query_nic_load(struct hud_graph *gr)
{
   /* The framework calls us at a regular but indefined period,
    * not once per second, compensate the statistics accordingly.
    */

   struct nic_info *nic = gr->query_data;
   uint64_t now = os_time_get();

   if (nic->last_time) {
      if (nic->last_time + gr->pane->period <= now) {
         switch (nic->mode) {
         case NIC_DIRECTION_RX:
         case NIC_DIRECTION_TX:
            {
               uint64_t bytes;
               get_nic_bytes(nic->throughput_filename, &bytes);
               uint64_t nic_mbps =
                  ((bytes - nic->last_nic_bytes) / 1000000) * 8;

               float speedMbps = nic->speedMbps;
               float periodMs = gr->pane->period / 1000;
               float bits = nic_mbps;
               float period_factor = periodMs / 1000;
               float period_speed = speedMbps * period_factor;
               float pct = (bits / period_speed) * 100;

               /* Scaling bps with a narrow time period into a second,
                * potentially suffers from routing errors at higher
                * periods. Eg 104%. Compensate.
                */
               if (pct > 100)
                  pct = 100;
               hud_graph_add_value(gr, (uint64_t) pct);

               nic->last_nic_bytes = bytes;
            }
            break;
         case NIC_RSSI_DBM:
            {
               uint64_t leveldBm = 0;
               query_nic_rssi(nic, &leveldBm);
               hud_graph_add_value(gr, leveldBm);
            }
            break;
         }

         nic->last_time = now;
      }
   }
   else {
      /* initialize */
      switch (nic->mode) {
      case NIC_DIRECTION_RX:
      case NIC_DIRECTION_TX:
         get_nic_bytes(nic->throughput_filename, &nic->last_nic_bytes);
         break;
      case NIC_RSSI_DBM:
         break;
      }

      nic->last_time = now;
   }
}

static void
free_query_data(void *p)
{
   struct nic_info *nic = (struct nic_info *) p;
   list_del(&nic->list);
   FREE(nic);
}

/**
  * Create and initialize a new object for a specific network interface dev.
  * \param  pane  parent context.
  * \param  nic_name  logical block device name, EG. eth0.
  * \param  mode  query type (NIC_DIRECTION_RX/WR/RSSI) statistics.
  */
void
hud_nic_graph_install(struct hud_pane *pane, const char *nic_name,
                      unsigned int mode)
{
   struct hud_graph *gr;
   struct nic_info *nic;

   int num_nics = hud_get_num_nics(0);
   if (num_nics <= 0)
      return;

#if LOCAL_DEBUG
   printf("%s(%s, %s) - Creating HUD object\n", __func__, nic_name,
          mode == NIC_DIRECTION_RX ? "RX" :
          mode == NIC_DIRECTION_TX ? "TX" :
          mode == NIC_RSSI_DBM ? "RSSI" : "UNDEFINED");
#endif

   nic = find_nic_by_name(nic_name, mode);
   if (!nic)
      return;

   gr = CALLOC_STRUCT(hud_graph);
   if (!gr)
      return;

   nic->mode = mode;
   if (nic->mode == NIC_DIRECTION_RX) {
      snprintf(gr->name, sizeof(gr->name), "%s-rx-%lldMbps", nic->name,
         nic->speedMbps);
   }
   else if (nic->mode == NIC_DIRECTION_TX) {
      snprintf(gr->name, sizeof(gr->name), "%s-tx-%lldMbps", nic->name,
         nic->speedMbps);
   }
   else if (nic->mode == NIC_RSSI_DBM)
      snprintf(gr->name, sizeof(gr->name), "%s-rssi", nic->name);
   else
      return;

   gr->query_data = nic;
   gr->query_new_value = query_nic_load;

   /* Don't use free() as our callback as that messes up Gallium's
    * memory debugger.  Use simple free_query_data() wrapper.
    */
   gr->free_query_data = free_query_data;

   hud_pane_add_graph(pane, gr);
   hud_pane_set_max_value(pane, 100);
}

static int
is_wireless_nic(const char *dirbase)
{
   struct stat stat_buf;

   /* Check if its a wireless card */
   char fn[256];
   snprintf(fn, sizeof(fn), "%s/wireless", dirbase);
   if (stat(fn, &stat_buf) == 0)
      return 1;

   return 0;
}

static void
query_nic_bitrate(struct nic_info *nic, const char *dirbase)
{
   struct stat stat_buf;

   /* Check if its a wireless card */
   char fn[256];
   snprintf(fn, sizeof(fn), "%s/wireless", dirbase);
   if (stat(fn, &stat_buf) == 0) {
      /* we're a wireless nic */
      query_wifi_bitrate(nic, &nic->speedMbps);
      nic->speedMbps /= 1000000;
   }
   else {
      /* Must be a wired nic */
      snprintf(fn, sizeof(fn), "%s/speed", dirbase);
      get_file_value(fn, &nic->speedMbps);
   }
}

/**
  * Initialize internal object arrays and display NIC HUD help.
  * \param  displayhelp  true if the list of detected devices should be
                         displayed on the console.
  * \return  number of detected network interface devices.
  */
int
hud_get_num_nics(bool displayhelp)
{
   struct dirent *dp;
   struct stat stat_buf;
   struct nic_info *nic;
   char name[64];

   /* Return the number if network interfaces. */
   if (gnic_count)
      return gnic_count;

   /* Scan /sys/block, for every object type we support, create and
    * persist an object to represent its different statistics.
    */
   list_inithead(&gnic_list);
   DIR *dir = opendir("/sys/class/net/");
   if (!dir)
      return 0;

   while ((dp = readdir(dir)) != NULL) {

      /* Avoid 'lo' and '..' and '.' */
      if (strlen(dp->d_name) <= 2)
         continue;

      char basename[256];
      snprintf(basename, sizeof(basename), "/sys/class/net/%s", dp->d_name);
      snprintf(name, sizeof(name), "%s/statistics/rx_bytes", basename);
      if (stat(name, &stat_buf) < 0)
         continue;

      if (!S_ISREG(stat_buf.st_mode))
         continue;              /* Not a regular file */

      int is_wireless = is_wireless_nic(basename);

      /* Add the RX object */
      nic = CALLOC_STRUCT(nic_info);
      strcpy(nic->name, dp->d_name);
      snprintf(nic->throughput_filename, sizeof(nic->throughput_filename),
         "%s/statistics/rx_bytes", basename);
      nic->mode = NIC_DIRECTION_RX;
      nic->is_wireless = is_wireless;
      query_nic_bitrate(nic, basename);

      list_addtail(&nic->list, &gnic_list);
      gnic_count++;

      /* Add the TX object */
      nic = CALLOC_STRUCT(nic_info);
      strcpy(nic->name, dp->d_name);
      snprintf(nic->throughput_filename,
         sizeof(nic->throughput_filename),
         "/sys/class/net/%s/statistics/tx_bytes", dp->d_name);
      nic->mode = NIC_DIRECTION_TX;
      nic->is_wireless = is_wireless;

      query_nic_bitrate(nic, basename);

      list_addtail(&nic->list, &gnic_list);
      gnic_count++;

      if (nic->is_wireless) {
         /* RSSI Support */
         nic = CALLOC_STRUCT(nic_info);
         strcpy(nic->name, dp->d_name);
         snprintf(nic->throughput_filename,
            sizeof(nic->throughput_filename),
            "/sys/class/net/%s/statistics/tx_bytes", dp->d_name);
         nic->mode = NIC_RSSI_DBM;

         query_nic_bitrate(nic, basename);

         list_addtail(&nic->list, &gnic_list);
         gnic_count++;
      }

   }

   list_for_each_entry(struct nic_info, nic, &gnic_list, list) {
      char line[64];
      snprintf(line, sizeof(line), "    nic-%s-%s",
              nic->mode == NIC_DIRECTION_RX ? "rx" :
              nic->mode == NIC_DIRECTION_TX ? "tx" :
              nic->mode == NIC_RSSI_DBM ? "rssi" : "undefined", nic->name);

      puts(line);

   }

   return gnic_count;
}

#endif /* HAVE_GALLIUM_EXTRA_HUD */