summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/Linux/netdev.c
blob: c2ef2d16f47013209ad25a9bd98ffd2c65fec5b2 (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
    KSysGuard, the KDE System Guard

    Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include "Command.h"
#include "ksysguardd.h"

#include "netdev.h"

#define MON_SIZE	128

#define CALC( a, b, c, d, e, f ) \
{ \
  if (f){ \
    NetDevs[ i ].a = a - NetDevs[ i ].Old##a; \
    NetDevs[ i ].Old##a = a; \
  } \
  else{ \
    NetDevs[ i ].a = a; \
  } \
}

#define REGISTERSENSOR( a, b, c, d, e, f ) \
{ \
  snprintf( mon, MON_SIZE, "network/interfaces/%s/%s", tag, b ); \
  registerMonitor( mon, "integer", printNetDev##a, printNetDev##a##Info, NetDevSM ); \
}

#define UNREGISTERSENSOR( a, b, c, d, e, f ) \
{ \
  snprintf( mon, MON_SIZE, "network/interfaces/%s/%s", NetDevs[ i ].name, b ); \
  removeMonitor( mon ); \
}

#define DEFMEMBERS( a, b, c, d, e, f ) \
signed long long Old##a; \
signed long long a; \
signed long a##Scale;

#define DEFVARS( a, b, c, d, e, f ) \
signed long long a;


/*The sixth variable is 1 if the quantity variation must be provided, 0 if the absolute value must be provided */
#define FORALL( a ) \
  a( recBytes, "receiver/data", "Received Data", "kBytes/s", 1024, 1) \
  a( recPacks, "receiver/packets", "Received Packets", "1/s", 1, 1 ) \
  a( recErrs, "receiver/errors", "Receiver Errors", "1/s", 1, 1 ) \
  a( recDrop, "receiver/drops", "Receiver Drops", "1/s", 1, 1 ) \
  a( recFifo, "receiver/fifo", "Receiver FIFO Overruns", "1/s", 1, 1 ) \
  a( recFrame, "receiver/frame", "Receiver Frame Errors", "1/s", 1, 1 ) \
  a( recCompressed, "receiver/compressed", "Received Compressed Packets", "1/s", 1, 1 ) \
  a( recMulticast, "receiver/multicast", "Received Multicast Packets", "1/s", 1, 1 ) \
  a( sentBytes, "transmitter/data", "Sent Data", "kBytes/s", 1024, 1 ) \
  a( sentPacks, "transmitter/packets", "Sent Packets", "1/s", 1, 1 ) \
  a( sentErrs, "transmitter/errors", "Transmitter Errors", "1/s", 1, 1 ) \
  a( sentDrop, "transmitter/drops", "Transmitter Drops", "1/s", 1, 1 ) \
  a( sentFifo, "transmitter/fifo", "Transmitter FIFO overruns", "1/s", 1, 1 ) \
  a( sentColls, "transmitter/collisions", "Transmitter Collisions", "1/s", 1, 1 ) \
  a( sentCarrier, "transmitter/carrier", "Transmitter Carrier losses", "1/s", 1, 1 ) \
  a( sentCompressed, "transmitter/compressed", "Transmitter Compressed Packets", "1/s", 1, 1 )

#define FORALLWIFI( a ) \
 a( linkQuality, "wifi/quality", "Link Quality", "", 1, 0) \
 a( signalLevel, "wifi/signal", "Signal Level", "dBm", 1, 0) \
 a( noiseLevel, "wifi/noise", "Noise Level", "dBm", 1, 0) \
 a( nwid, "wifi/nwid", "Rx Invalid Nwid Packets", "1/s", 1, 1) \
 a( RxCrypt, "wifi/crypt", "Rx Invalid Crypt Packets", "1/s", 1, 1) \
 a( frag, "wifi/frag", "Rx Invalid Frag Packets", "1/s", 1, 1) \
 a( retry, "wifi/retry", "Tx Excessive Retries Packets", "1/s", 1, 1) \
 a( misc, "wifi/misc", "Invalid Misc Packets", "1/s", 1, 1) \
 a( beacon, "wifi/beacon", "Missed Beacon", "1/s", 1, 1)

#define SETZERO( a, b, c, d, e, f ) \
a = 0;

#define SETMEMBERZERO( a, b, c, d, e, f ) \
NetDevs[ i ].a = 0; \
NetDevs[ i ].a##Scale = e;

#define DECLAREFUNC( a, b, c, d, e, f ) \
void printNetDev##a( const char* cmd ); \
void printNetDev##a##Info( const char* cmd );

typedef struct
{
  FORALL( DEFMEMBERS )
  FORALLWIFI( DEFMEMBERS )
  char name[ 32 ];
  int wifi;
} NetDevInfo;

/* We have observed deviations of up to 5% in the accuracy of the timer
 * interrupts. So we try to measure the interrupt interval and use this
 * value to calculate timing dependant values. */
static float timeInterval = 0;
static struct timeval lastSampling;
static struct timeval currSampling;
static struct SensorModul* NetDevSM;

#define NETDEVBUFSIZE 4096
static char NetDevBuf[ NETDEVBUFSIZE ];
static char NetDevWifiBuf[ NETDEVBUFSIZE ];
static int NetDevCnt = 0;
static int Dirty = 0;
static int NetDevOk = 0;
static long OldHash = 0;

#define MAXNETDEVS 64
static NetDevInfo NetDevs[ MAXNETDEVS ];

void processNetDev( void );

FORALL( DECLAREFUNC )
FORALLWIFI( DECLAREFUNC )

static int processNetDev_( void )
{
  int i,j;
  char format[ 32 ];
  char devFormat[ 16 ];
  char buf[ 1024 ];
  char tag[ 64 ];
  char* netDevBufP = NetDevBuf;
  char* netDevWifiBufP = NetDevWifiBuf;

  sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 );
  sprintf( devFormat, "%%%ds", (int)sizeof( tag ) - 1 );

  /* skip 2 first lines */
  for ( i = 0; i < 2; i++ ) {
    sscanf( netDevBufP, format, buf );
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevBufP += strlen( buf ) + 1;  /* move netDevBufP to next line */
  }

  for ( i = 0; sscanf( netDevBufP, format, buf ) == 1; ++i ) {
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevBufP += strlen( buf ) + 1;  /* move netDevBufP to next line */

    if ( sscanf( buf, devFormat, tag ) ) {
      char* pos = strchr( tag, ':' );
      if ( pos ) {
        FORALL( DEFVARS );
        *pos = '\0';
        FORALL( SETZERO );
        sscanf( buf + 7, "%lli %lli %lli %lli %lli %lli %lli %lli " 
                "%lli %lli %lli %lli %lli %lli %lli %lli",
                &recBytes, &recPacks, &recErrs, &recDrop, &recFifo,
                &recFrame, &recCompressed, &recMulticast,
                &sentBytes, &sentPacks, &sentErrs, &sentDrop,
                &sentFifo, &sentColls, &sentCarrier, &sentCompressed );

        if ( i >= NetDevCnt || strcmp( NetDevs[ i ].name, tag ) != 0 ) {
          /* The network device configuration has changed. We
           * need to reconfigure the netdev module. */
          return -1;
        } else {
          FORALL( CALC );
        }
      }
    }
  }

  if ( i != NetDevCnt )
    return -1;

  /*Update the values for the wifi interfaces*/
  if ( *netDevWifiBufP=='\0')  /*there is no /proc/net/wireless file*/
    return 0;

  /* skip 2 first lines */
  for ( i = 0; i < 2; i++ ) {
    sscanf( netDevWifiBufP, format, buf );
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevWifiBufP += strlen( buf ) + 1;  /* move netDevWifiBufP to next line */
  }

  for ( j = 0; sscanf( netDevWifiBufP, format, buf ) == 1; ++j ) {
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevWifiBufP += strlen( buf ) + 1;  /* move netDevWifiBufP to next line */

    if ( sscanf( buf, devFormat, tag ) ) {
      char* pos = strchr( tag, ':' );
      if ( pos ) {
        FORALLWIFI( DEFVARS );
        *pos = '\0';

       for (i = 0 ; i < NetDevCnt ; ++i){ /*find the corresponding interface*/
           if ( strcmp(tag,NetDevs[ i ].name)==0){
               break;
           }
        }
       sscanf( buf + 12, " %lli. %lli. %lli. %lli %lli %lli %lli %lli %lli",
                &linkQuality, &signalLevel, &noiseLevel, &nwid,
                &RxCrypt, &frag,  &retry, &misc, &beacon );
       signalLevel -= 256; /*the units are dBm*/
       noiseLevel -= 256;
        FORALLWIFI( CALC );
      }
    }
  }



  /* save exact time inverval between this and the last read of
   * /proc/net/dev */
  timeInterval = currSampling.tv_sec - lastSampling.tv_sec +
                 ( currSampling.tv_usec - lastSampling.tv_usec ) / 1000000.0;
  lastSampling = currSampling;
  Dirty = 0;

  return 0;
}

void processNetDev( void )
{
  int i;

  if ( NetDevCnt == 0 )
		return;

  for ( i = 0; i < 5 && processNetDev_() < 0; ++i )
    checkNetDev();

  /* If 5 reconfiguration attemts failed, something is very wrong and
   * we close the netdev module for further use. */
  if ( i == 5 )
    exitNetDev();
}

/*
================================ public part =================================
*/

void initNetDev( struct SensorModul* sm )
{
  int i,j;
  char format[ 32 ];
  char devFormat[ 16 ];
  char buf[ 1024 ];
  char tag[ 64 ];
  char* netDevBufP = NetDevBuf;
  char* netDevWifiBufP = NetDevWifiBuf;

  NetDevSM = sm;

  if ( updateNetDev() < 0 )
    return;

  sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 );
  sprintf( devFormat, "%%%ds", (int)sizeof( tag ) - 1 );

  /* skip 2 first lines */
  for ( i = 0; i < 2; i++ ) {
    sscanf( netDevBufP, format, buf );
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevBufP += strlen( buf ) + 1;  /* move netDevBufP to next line */
  }

  for ( i = 0; sscanf( netDevBufP, format, buf ) == 1; ++i ) {
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevBufP += strlen( buf ) + 1;  /* move netDevBufP to next line */

    if ( sscanf( buf, devFormat, tag ) ) {
      char* pos = strchr( tag, ':' );
      if ( pos ) {
        char mon[ MON_SIZE ];
        *pos = '\0';
        strlcpy( NetDevs[ i ].name, tag, sizeof( NetDevs[ i ].name ) );
        FORALL( REGISTERSENSOR );
        sscanf( pos + 1, "%lli %lli %lli %lli %lli %lli %lli %lli" 
                "%lli %lli %lli %lli %lli %lli %lli %lli",
                &NetDevs[ i ].recBytes, &NetDevs[ i ].recPacks,
                &NetDevs[ i ].recErrs, &NetDevs[ i ].recDrop,
                &NetDevs[ i ].recFifo, &NetDevs[ i ].recFrame,
                &NetDevs[ i ].recCompressed, &NetDevs[ i ].recMulticast,
                &NetDevs[ i ].sentBytes, &NetDevs[ i ].sentPacks,
                &NetDevs[ i ].sentErrs, &NetDevs[ i ].sentDrop,
                &NetDevs[ i ].sentFifo, &NetDevs[ i ].sentColls,
                &NetDevs[ i ].sentCarrier, &NetDevs[ i ].sentCompressed );
        NetDevCnt++;
			}
      FORALL( SETMEMBERZERO );
    }

  }

  /* detect the wifi interfaces*/
   /* skip 2 first lines */
  for ( i = 0; i < 2; i++ ) {
    sscanf( netDevWifiBufP, format, buf );
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevWifiBufP += strlen( buf ) + 1;  /* move netDevWifiBufP to next line */
  }

  for ( j = 0; sscanf( netDevWifiBufP, format, buf ) == 1; ++j ) {
    buf[ sizeof( buf ) - 1 ] = '\0';
    netDevWifiBufP += strlen( buf ) + 1;  /* move netDevWifiBufP to next line */

    if ( sscanf( buf, devFormat, tag ) ) {
      char * pos = strchr( tag, ':' );
      if ( pos ) {
        char mon[ MON_SIZE ];
        *pos = '\0';
       /*find and tag the corresponding NetDev as wifi enabled.
        At the end of the loop,  i is the index of the device. 
        This variable i is used in some macro */
       for (i = 0 ; i < NetDevCnt ; ++i){ 
           if ( strcmp(tag,NetDevs[ i ].name)==0){
               NetDevs[ i ].wifi = 1;
               break;
           }
        }
        FORALLWIFI( REGISTERSENSOR );
      }
      FORALLWIFI( SETMEMBERZERO );  /* the variable i must point to the corrrect NetDevs[i]*/
    }
  }

  /* Call processNetDev to elimitate initial peek values. */
  processNetDev();
}

void exitNetDev( void )
{
  int i;

  for ( i = 0; i < NetDevCnt; ++i ) {
    char mon[ MON_SIZE ];
    FORALL( UNREGISTERSENSOR );
    if (NetDevs[ i ].wifi)
       FORALLWIFI( UNREGISTERSENSOR );
  }
  NetDevCnt = 0;
}

int updateNetDev( void )
{
  /* We read the information about the network interfaces from
     /proc/net/dev. The file should look like this:

  Inter-|   Receive                                                 |  Transmit
  face  | bytes    packets errs drop fifo frame compressed multicast| bytes    packets errs drop fifo colls carrier compressed
      lo:275135772 1437448    0    0    0     0          0         0 275135772 1437448    0    0    0     0       0          0
    eth0:123648812  655251    0    0    0     0          0         0 246847871  889636    0    0    0     0       0          0       Inter-|   Receive                                                |  Transmit
  face  | bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
      lo:275135772 1437448    0    0    0     0          0         0 275135772 1437448    0    0    0     0       0          0
    eth0:123648812  655251    0    0    0     0          0         0 246847871  889636    0    0    0     0       0          0
	*/

  size_t n;
  int fd;
  long hash;
  char* p;

  if ( NetDevOk < 0 )
    return 0;

  if ( ( fd = open( "/proc/net/dev", O_RDONLY ) ) < 0 ) {
    /* /proc/net/dev may not exist on some machines. */
    NetDevOk = -1;
    return 0;
  }

  if ( ( n = read( fd, NetDevBuf, NETDEVBUFSIZE - 1 ) ) == NETDEVBUFSIZE - 1 ) {
    log_error( "Internal buffer too small to read \'/proc/net/dev\'" );
    NetDevOk = -1;

    close( fd );
    return -1;
  }

  gettimeofday( &currSampling, 0 );
  close( fd );
  NetDevOk = 1;
  NetDevBuf[ n ] = '\0';

  /* Calculate hash over the first 7 characters of each line starting
   * after the first newline. */
  for ( p = NetDevBuf, hash = 0; *p; ++p )
    if ( *p == '\n' )
      for ( ++p; *p && *p != ':' && *p != '|'; ++p )
        hash = ( ( hash << 6 ) + *p ) % 390389;

  if ( OldHash != 0 && OldHash != hash ) {
    print_error( "RECONFIGURE\n" );
    CheckSetupFlag = 1;
  }
  OldHash = hash;

  Dirty = 1;

  /* We read the informations about the wifi from /proc/net/wireless and store it into NetDevWifiBuf */
  if ( ( fd = open( "/proc/net/wireless", O_RDONLY ) ) < 0 ) {
    /* /proc/net/wireless may not exist on some machines. */
    NetDevWifiBuf[0]='\0';
  }

  if ( ( n = read( fd, NetDevWifiBuf, NETDEVBUFSIZE - 1 ) ) == NETDEVBUFSIZE - 1 ) {
    log_error( "Internal buffer too small to read \'/proc/net/wireless\'" );
    close( fd );
    return -1;
  }
  close( fd );
  NetDevWifiBuf[ n ] = '\0';

  return 0;
}

void checkNetDev( void )
{
	/* Values for other network devices are lost, but it is still better
	 * than not detecting any new devices. TODO: Fix after 2.1 is out. */
  exitNetDev();
  initNetDev( NetDevSM );
}

#define PRINTFUNC( a, b, c, d, e, f ) \
void printNetDev##a( const char* cmd ) \
{ \
  int i; \
  char* beg; \
  char* end; \
  char dev[ 64 ]; \
 \
  beg = strchr( cmd, '/' ); \
  beg = strchr( beg + 1, '/' ); \
  end = strchr( beg + 1, '/' ); \
  strncpy( dev, beg + 1, end - beg - 1 ); \
  dev[ end - beg - 1 ] = '\0'; \
 \
  if ( Dirty ) \
    processNetDev(); \
 \
  for ( i = 0; i < MAXNETDEVS; ++i ) \
    if ( strcmp( NetDevs[ i ].name, dev ) == 0) { \
      if (f) \
         fprintf( CurrentClient, "%li\n", (long) \
               ( NetDevs[ i ].a / ( NetDevs[ i ].a##Scale * timeInterval ) ) ); \
      else \
         fprintf( CurrentClient, "%li\n", (long) NetDevs[ i ].a ); \
      return; \
    } \
 \
  fprintf( CurrentClient, "0\n" ); \
} \
 \
void printNetDev##a##Info( const char* cmd ) \
{ \
  (void)cmd; \
  fprintf( CurrentClient, "%s\t0\t0\t%s\n", c, d ); \
}

FORALL( PRINTFUNC )
FORALLWIFI( PRINTFUNC )