]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
3edcc200951d4a45cbc5196165a8948ec831a173
[netdata.git] / src / plugin_tc.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5
6 #include "avl.h"
7 #include "log.h"
8 #include "common.h"
9 #include "config.h"
10 #include "rrd.h"
11 #include "popen.h"
12 #include "plugin_tc.h"
13
14 #define RRD_TYPE_TC                                     "tc"
15 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
16
17 // ----------------------------------------------------------------------------
18 // /sbin/tc processor
19 // this requires the script plugins.d/tc-qos-helper.sh
20
21 #define TC_LINE_MAX 1024
22
23 struct tc_class {
24         avl avl;
25
26         char *id;
27         uint32_t hash;
28
29         char *name;
30
31         char *leafid;
32         uint32_t leaf_hash;
33
34         char *parentid;
35         uint32_t parent_hash;
36
37         char hasparent;
38         char isleaf;
39         unsigned long long bytes;
40
41         char updated;   // updated bytes
42         char seen;              // seen in the tc list (even without bytes)
43
44         struct tc_class *next;
45         struct tc_class *prev;
46 };
47
48 struct tc_device {
49         avl avl;
50
51         char *id;
52         uint32_t hash;
53
54         char *name;
55         char *family;
56
57         avl_tree classes_index;
58
59         struct tc_class *classes;
60 };
61
62
63 // ----------------------------------------------------------------------------
64 // tc_device index
65
66 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
67
68 static int tc_device_compare(void* a, void* b) {
69         if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
70         else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
71         else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
72 }
73
74 avl_tree tc_device_root_index = {
75                 NULL,
76                 tc_device_compare
77 };
78
79 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
80 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
81
82 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
83         struct tc_device *result = NULL, tmp;
84         tmp.id = (char *)id;
85         tmp.hash = (hash)?hash:simple_hash(tmp.id);
86
87         avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
88         return result;
89 }
90
91
92 // ----------------------------------------------------------------------------
93 // tc_class index
94
95 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
96
97 static int tc_class_compare(void* a, void* b) {
98         if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
99         else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
100         else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
101 }
102
103 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
104 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
105
106 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
107         struct tc_class *result = NULL, tmp;
108         tmp.id = (char *)id;
109         tmp.hash = (hash)?hash:simple_hash(tmp.id);
110
111         avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
112         return result;
113 }
114
115 // ----------------------------------------------------------------------------
116
117 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
118         debug(D_TC_LOOP, "Removing from device '%s' class '%s', parentid '%s', leafid '%s', seen=%d", n->id, c->id, c->parentid?c->parentid:"", c->leafid?c->leafid:"", c->seen);
119
120         if(c->next) c->next->prev = c->prev;
121         if(c->prev) c->prev->next = c->next;
122         if(n->classes == c) n->classes = c->next;
123
124         tc_class_index_del(n, c);
125
126         if(c->id) free(c->id);
127         if(c->name) free(c->name);
128         if(c->leafid) free(c->leafid);
129         if(c->parentid) free(c->parentid);
130
131         free(c);
132 }
133
134 static void tc_device_classes_cleanup(struct tc_device *d) {
135         static int cleanup_every = 999;
136
137         if(cleanup_every > 0) {
138                 cleanup_every = -config_get_number("plugin:tc", "cleanup unused classes every", 60);
139                 if(cleanup_every > 0) cleanup_every = -cleanup_every;
140                 if(cleanup_every == 0) cleanup_every = -1;
141         }
142
143         struct tc_class *c = d->classes;
144         while(c) {
145                 if(c->seen < cleanup_every) {
146                         struct tc_class *nc = c->next;
147                         tc_class_free(d, c);
148                         c = nc;
149                 }
150                 else c = c->next;
151
152                 if(c) {
153                         c->updated = 0;
154                         c->seen--;
155                 }
156         }
157 }
158
159 static void tc_device_commit(struct tc_device *d)
160 {
161         static int enable_new_interfaces = -1;
162
163         if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
164         
165         // we only need to add leaf classes
166         struct tc_class *c, *x;
167
168         // set all classes
169         for(c = d->classes ; c ; c = c->next) {
170                 c->isleaf = 1;
171                 c->hasparent = 0;
172         }
173
174         // mark the classes as leafs and parents
175         for(c = d->classes ; c ; c = c->next) {
176                 if(!c->updated) continue;
177
178                 for(x = d->classes ; x ; x = x->next) {
179                         if(!x->updated) continue;
180
181                         if(c == x) continue;
182
183                         if(x->parentid && (
184                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
185                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
186                                 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has as leaf class '%s' (parentid: '%s').", d->name?d->name:d->id, c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->name?x->name:x->id, x->parentid?x->parentid:x->id);
187                                 c->isleaf = 0;
188                                 x->hasparent = 1;
189                         }
190                 }
191         }
192         
193         // debugging:
194         /*
195         for ( c = d->classes ; c ; c = c->next) {
196                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
197                 else debug(D_TC_LOOP, "TC: Device %s, class %s, IGNORE (isleaf: %d, hasparent: %d, parent: %s)", d->name, c->id, c->isleaf, c->hasparent, c->parentid);
198         }
199         */
200
201         // we need at least a class
202         for(c = d->classes ; c ; c = c->next) {
203                 // debug(D_TC_LOOP, "TC: Device '%s', class '%s', isLeaf=%d, HasParent=%d, Seen=%d", d->name?d->name:d->id, c->name?c->name:c->id, c->isleaf, c->hasparent, c->seen);
204                 if(!c->updated) continue;
205                 if(c->isleaf && c->hasparent) break;
206         }
207         if(!c) {
208                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
209                 tc_device_classes_cleanup(d);
210                 return;
211         }
212
213         char var_name[CONFIG_MAX_NAME + 1];
214         snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
215         if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
216                 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
217                 if(!st) {
218                         debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
219
220                         st = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, "Class Usage", "kilobits/s", 1000, rrd_update_every, RRDSET_TYPE_STACKED);
221
222                         for(c = d->classes ; c ; c = c->next) {
223                                 if(!c->updated) continue;
224
225                                 if(c->isleaf && c->hasparent)
226                                         rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
227                         }
228                 }
229                 else {
230                         debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
231                         rrdset_next_plugins(st);
232
233                         if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
234                 }
235
236                 for(c = d->classes ; c ; c = c->next) {
237                         if(!c->updated) continue;
238
239                         if(c->isleaf && c->hasparent) {
240                                 RRDDIM *rd = rrddim_find(st, c->id);
241
242                                 if(!rd) {
243                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
244                                         
245                                         // new class, we have to add it
246                                         rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
247                                 }
248                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
249
250                                 rrddim_set_by_pointer(st, rd, c->bytes);
251
252                                 // if it has a name, different to the id
253                                 if(c->name) {
254                                         // update the rrd dimension with the new name
255                                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
256                                         rrddim_set_name(st, rd, c->name);
257
258                                         free(c->name);
259                                         c->name = NULL;
260                                 }
261                         }
262                 }
263                 rrdset_done(st);
264         }
265
266         tc_device_classes_cleanup(d);
267 }
268
269 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
270 {
271         struct tc_class *c = tc_class_index_find(d, id, 0);
272         if(c) {
273                 if(c->name) free(c->name);
274                 c->name = NULL;
275
276                 if(name && *name && strcmp(c->id, name) != 0) {
277                         debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
278                         c->name = strdup(name);
279                 }
280         }
281 }
282
283 static void tc_device_set_device_name(struct tc_device *d, char *name) {
284         if(d->name) free(d->name);
285         d->name = NULL;
286
287         if(name && *name && strcmp(d->id, name) != 0) {
288                 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
289                 d->name = strdup(name);
290         }
291 }
292
293 static void tc_device_set_device_family(struct tc_device *d, char *family) {
294         if(d->family) free(d->family);
295         d->family = NULL;
296
297         if(family && *family && strcmp(d->id, family) != 0) {
298                 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
299                 d->family = strdup(family);
300         }
301         // no need for null termination - it is already null
302 }
303
304 static struct tc_device *tc_device_create(char *id)
305 {
306         struct tc_device *d = tc_device_index_find(id, 0);
307
308         if(!d) {
309                 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
310
311                 d = calloc(1, sizeof(struct tc_device));
312                 if(!d) {
313                         fatal("Cannot allocate memory for tc_device %s", id);
314                         return NULL;
315                 }
316
317                 d->id = strdup(id);
318                 d->hash = simple_hash(d->id);
319
320                 d->classes_index.root = NULL;
321                 d->classes_index.compar = tc_class_compare;
322
323                 tc_device_index_add(d);
324         }
325
326         return(d);
327 }
328
329 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
330 {
331         struct tc_class *c = tc_class_index_find(n, id, 0);
332
333         if(!c) {
334                 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
335
336                 c = calloc(1, sizeof(struct tc_class));
337                 if(!c) {
338                         fatal("Cannot allocate memory for tc class");
339                         return NULL;
340                 }
341
342                 if(n->classes) n->classes->prev = c;
343                 c->next = n->classes;
344                 n->classes = c;
345
346                 c->id = strdup(id);
347                 if(!c->id) {
348                         free(c);
349                         return NULL;
350                 }
351                 c->hash = simple_hash(c->id);
352
353                 if(parentid && *parentid) {
354                         c->parentid = strdup(parentid);
355                         c->parent_hash = simple_hash(c->parentid);
356                 }
357
358                 if(leafid && *leafid) {
359                         c->leafid = strdup(leafid);
360                         c->leaf_hash = simple_hash(c->leafid);
361                 }
362
363                 tc_class_index_add(n, c);
364         }
365
366         c->seen = 1;
367
368         return(c);
369 }
370 /*
371 static void tc_device_free(struct tc_device *n)
372 {
373         tc_device_index_del(n);
374
375         while(n->classes) tc_class_free(n, n->classes);
376
377         if(n->id) free(n->id);
378         if(n->name) free(n->name);
379         if(n->family) free(n->family);
380
381         free(n);
382 }
383 */
384
385 #define MAX_WORDS 20
386
387 static inline int tc_space(char c) {
388         switch(c) {
389         case ' ':
390         case '\t':
391         case '\r':
392         case '\n':
393                 return 1;
394
395         default:
396                 return 0;
397         }
398 }
399
400 static void tc_split_words(char *str, char **words, int max_words) {
401         char *s = str;
402         int i = 0;
403
404         // skip all white space
405         while(tc_space(*s)) s++;
406
407         // store the first word
408         words[i++] = s;
409
410         // while we have something
411         while(*s) {
412                 // if it is a space
413                 if(tc_space(*s)) {
414
415                         // terminate the word
416                         *s++ = '\0';
417
418                         // skip all white space
419                         while(tc_space(*s)) s++;
420
421                         // if we reached the end, stop
422                         if(!*s) break;
423
424                         // store the next word
425                         if(i < max_words) words[i++] = s;
426                         else break;
427                 }
428                 else s++;
429         }
430
431         // terminate the words
432         while(i < max_words) words[i++] = NULL;
433 }
434
435 pid_t tc_child_pid = 0;
436 void *tc_main(void *ptr)
437 {
438         if(ptr) { ; }
439
440         info("TC thread created with task id %d", gettid());
441
442         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
443                 error("Cannot set pthread cancel type to DEFERRED.");
444
445         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
446                 error("Cannot set pthread cancel state to ENABLE.");
447
448         char buffer[TC_LINE_MAX+1] = "";
449         char *words[MAX_WORDS] = { NULL };
450
451         uint32_t BEGIN_HASH = simple_hash("BEGIN");
452         uint32_t END_HASH = simple_hash("END");
453         uint32_t CLASS_HASH = simple_hash("class");
454         uint32_t SENT_HASH = simple_hash("Sent");
455         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
456         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
457         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
458 #ifdef DETACH_PLUGINS_FROM_NETDATA
459         uint32_t MYPID_HASH = simple_hash("MYPID");
460 #endif
461         uint32_t first_hash;
462
463         for(;1;) {
464                 FILE *fp;
465                 struct tc_device *device = NULL;
466                 struct tc_class *class = NULL;
467
468                 snprintf(buffer, TC_LINE_MAX, "exec %s %d", config_get("plugin:tc", "script to run to get tc values", PLUGINS_DIR "/tc-qos-helper.sh"), rrd_update_every);
469                 debug(D_TC_LOOP, "executing '%s'", buffer);
470                 // fp = popen(buffer, "r");
471                 fp = mypopen(buffer, &tc_child_pid);
472                 if(!fp) {
473                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
474                         return NULL;
475                 }
476
477                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
478                         buffer[TC_LINE_MAX] = '\0';
479                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
480
481                         tc_split_words(buffer, words, MAX_WORDS);
482                         if(!words[0] || !*words[0]) {
483                                 // debug(D_TC_LOOP, "empty line");
484                                 continue;
485                         }
486                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
487
488                         first_hash = simple_hash(words[0]);
489
490                         if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
491                                 // debug(D_TC_LOOP, "CLASS line on class id='%s', parent='%s', parentid='%s', leaf='%s', leafid='%s'", words[2], words[3], words[4], words[5], words[6]);
492
493                                 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
494                                         // char *type     = words[1];  // the class: htb, fq_codel, etc
495                                         char *id       = words[2];      // the class major:minor
496                                         char *parent   = words[3];      // 'parent' or 'root'
497                                         char *parentid = words[4];      // the parent's id
498                                         char *leaf     = words[5];      // 'leaf'
499                                         char *leafid   = words[6];      // leafid
500
501                                         if(strcmp(parent, "root") == 0) {
502                                                 parentid = NULL;
503                                                 leafid = NULL;
504                                         }
505                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
506                                                 leafid = NULL;
507
508                                         char leafbuf[20 + 1] = "";
509                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
510                                                 strncpy(leafbuf, leafid, 20 - 1);
511                                                 strcat(leafbuf, "1");
512                                                 leafid = leafbuf;
513                                         }
514
515                                         class = tc_class_add(device, id, parentid, leafid);
516                                 }
517                         }
518                         else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
519                                 // debug(D_TC_LOOP, "END line");
520
521                                 if(device) {
522                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
523                                                 error("Cannot set pthread cancel state to DISABLE.");
524
525                                         tc_device_commit(device);
526                                         // tc_device_free(device);
527                                         device = NULL;
528                                         class = NULL;
529
530                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
531                                                 error("Cannot set pthread cancel state to ENABLE.");
532                                 }
533                         }
534                         else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
535                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
536
537                                 if(device) {
538                                         // tc_device_free(device);
539                                         device = NULL;
540                                         class = NULL;
541                                 }
542
543                                 if(words[1] && *words[1]) {
544                                         device = tc_device_create(words[1]);
545                                         class = NULL;
546                                 }
547                         }
548                         else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
549                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
550                                 if(words[1] && *words[1]) {
551                                         class->bytes = atoll(words[1]);
552                                         class->updated = 1;
553                                 }
554                                 else class->bytes = 0;
555                         }
556                         else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
557                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
558                                 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
559                         }
560                         else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
561                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
562                                 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
563                         }
564                         else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
565                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
566                                 char *id    = words[1];
567                                 char *path  = words[2];
568                                 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
569                         }
570 #ifdef DETACH_PLUGINS_FROM_NETDATA
571                         else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
572                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
573                                 char *id = words[1];
574                                 pid_t pid = atol(id);
575
576                                 if(pid) tc_child_pid = pid;
577
578                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
579                         }
580 #endif
581                         //else {
582                         //      debug(D_TC_LOOP, "IGNORED line");
583                         //}
584                 }
585                 mypclose(fp, tc_child_pid);
586                 tc_child_pid = 0;
587
588                 if(device) {
589                         // tc_device_free(device);
590                         device = NULL;
591                         class = NULL;
592                 }
593
594                 sleep(rrd_update_every);
595         }
596
597         return NULL;
598 }
599