]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
Merge pull request #704 from ktsaou/master
[netdata.git] / src / plugin_tc.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/resource.h>
9
10 #include "avl.h"
11 #include "log.h"
12 #include "common.h"
13 #include "appconfig.h"
14 #include "rrd.h"
15 #include "popen.h"
16 #include "plugin_tc.h"
17 #include "main.h"
18
19 #define RRD_TYPE_TC                                     "tc"
20 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
21
22 // ----------------------------------------------------------------------------
23 // /sbin/tc processor
24 // this requires the script plugins.d/tc-qos-helper.sh
25
26 #define TC_LINE_MAX 1024
27
28 struct tc_class {
29         avl avl;
30
31         char *id;
32         uint32_t hash;
33
34         char *name;
35
36         char *leafid;
37         uint32_t leaf_hash;
38
39         char *parentid;
40         uint32_t parent_hash;
41
42         char hasparent;
43         char isleaf;
44         unsigned long long bytes;
45         unsigned long long packets;
46         unsigned long long dropped;
47         unsigned long long overlimits;
48         unsigned long long requeues;
49         unsigned long long lended;
50         unsigned long long borrowed;
51         unsigned long long giants;
52         unsigned long long tokens;
53         unsigned long long ctokens;
54
55         RRDDIM *rd_bytes;
56         RRDDIM *rd_packets;
57         RRDDIM *rd_dropped;
58
59         char name_updated;
60         char updated;   // updated bytes
61         int seen;               // seen in the tc list (even without bytes)
62
63         struct tc_class *next;
64         struct tc_class *prev;
65 };
66
67 struct tc_device {
68         avl avl;
69
70         char *id;
71         uint32_t hash;
72
73         char *name;
74         char *family;
75
76         char name_updated;
77         char family_updated;
78
79         char enabled;
80         char enabled_bytes;
81         char enabled_packets;
82         char enabled_dropped;
83
84         RRDSET *st_bytes;
85         RRDSET *st_packets;
86         RRDSET *st_dropped;
87
88         avl_tree classes_index;
89
90         struct tc_class *classes;
91
92         struct tc_device *next;
93         struct tc_device *prev;
94 };
95
96
97 struct tc_device *tc_device_root = NULL;
98
99 // ----------------------------------------------------------------------------
100 // tc_device index
101
102 static int tc_device_compare(void* a, void* b) {
103         if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
104         else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
105         else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
106 }
107
108 avl_tree tc_device_root_index = {
109                 NULL,
110                 tc_device_compare
111 };
112
113 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
114 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
115
116 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
117         struct tc_device tmp;
118         tmp.id = (char *)id;
119         tmp.hash = (hash)?hash:simple_hash(tmp.id);
120
121         return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
122 }
123
124
125 // ----------------------------------------------------------------------------
126 // tc_class index
127
128 static int tc_class_compare(void* a, void* b) {
129         if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
130         else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
131         else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
132 }
133
134 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
135 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
136
137 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
138         struct tc_class tmp;
139         tmp.id = (char *)id;
140         tmp.hash = (hash)?hash:simple_hash(tmp.id);
141
142         return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
143 }
144
145 // ----------------------------------------------------------------------------
146
147 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
148         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);
149
150         if(c->next) c->next->prev = c->prev;
151         if(c->prev) c->prev->next = c->next;
152         if(n->classes == c) {
153                 if(c->next) n->classes = c->next;
154                 else n->classes = c->prev;
155         }
156
157         tc_class_index_del(n, c);
158
159         if(c->id) free(c->id);
160         if(c->name) free(c->name);
161         if(c->leafid) free(c->leafid);
162         if(c->parentid) free(c->parentid);
163
164         free(c);
165 }
166
167 static inline void tc_device_classes_cleanup(struct tc_device *d) {
168         static int cleanup_every = 999;
169
170         if(unlikely(cleanup_every > 0)) {
171                 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
172                 if(cleanup_every < 0) cleanup_every = -cleanup_every;
173         }
174
175         d->name_updated = 0;
176         d->family_updated = 0;
177
178         struct tc_class *c = d->classes;
179         while(c) {
180                 if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
181                         struct tc_class *nc = c->next;
182                         tc_class_free(d, c);
183                         c = nc;
184                 }
185                 else {
186                         c->updated = 0;
187                         c->name_updated = 0;
188
189                         c = c->next;
190                 }
191         }
192 }
193
194 static inline void tc_device_commit(struct tc_device *d) {
195         static int enable_new_interfaces = -1, enable_bytes = -1, enable_packets = -1, enable_dropped = -1;
196
197         if(unlikely(enable_new_interfaces == -1)) {
198                 enable_new_interfaces = config_get_boolean_ondemand("plugin:tc", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_YES);
199                 enable_bytes          = config_get_boolean_ondemand("plugin:tc", "enable traffic charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
200                 enable_packets        = config_get_boolean_ondemand("plugin:tc", "enable packets charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
201                 enable_dropped        = config_get_boolean_ondemand("plugin:tc", "enable dropped charts for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
202         }
203
204         // we only need to add leaf classes
205         struct tc_class *c, *x;
206         unsigned long long bytes_sum = 0, packets_sum = 0, dropped_sum = 0;
207         int active_classes = 0;
208
209         // set all classes
210         for(c = d->classes ; c ; c = c->next) {
211                 c->isleaf = 1;
212                 c->hasparent = 0;
213         }
214
215         // mark the classes as leafs and parents
216         for(c = d->classes ; c ; c = c->next) {
217                 if(unlikely(!c->updated)) continue;
218
219                 for(x = d->classes ; x ; x = x->next) {
220                         if(unlikely(!x->updated)) continue;
221
222                         if(unlikely(c == x)) continue;
223
224                         if(x->parentid && (
225                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
226                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
227                                 // 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);
228                                 c->isleaf = 0;
229                                 x->hasparent = 1;
230                         }
231                 }
232         }
233
234         // debugging:
235         /*
236         for ( c = d->classes ; c ; c = c->next) {
237                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
238                 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);
239         }
240         */
241
242         // we need at least a class
243         for(c = d->classes ; c ; c = c->next) {
244                 // 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);
245                 if(!c->updated) continue;
246                 if(c->isleaf && c->hasparent) {
247                         active_classes++;
248                         bytes_sum += c->bytes;
249                         packets_sum += c->packets;
250                         dropped_sum += c->dropped;
251                 }
252         }
253
254         if(unlikely(!active_classes)) {
255                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
256                 tc_device_classes_cleanup(d);
257                 return;
258         }
259
260         if(unlikely(d->enabled == -1)) {
261                 char var_name[CONFIG_MAX_NAME + 1];
262                 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
263                 d->enabled         = config_get_boolean_ondemand("plugin:tc", var_name, enable_new_interfaces);
264
265                 snprintfz(var_name, CONFIG_MAX_NAME, "traffic chart for %s", d->id);
266                 d->enabled_bytes   = config_get_boolean_ondemand("plugin:tc", var_name, enable_bytes);
267
268                 snprintfz(var_name, CONFIG_MAX_NAME, "packets chart for %s", d->id);
269                 d->enabled_packets = config_get_boolean_ondemand("plugin:tc", var_name, enable_packets);
270
271                 snprintfz(var_name, CONFIG_MAX_NAME, "dropped packets chart for %s", d->id);
272                 d->enabled_dropped = config_get_boolean_ondemand("plugin:tc", var_name, enable_dropped);
273         }
274
275         if(likely(d->enabled)) {
276                 // --------------------------------------------------------------------
277                 // bytes
278
279                 if(d->enabled_bytes == CONFIG_ONDEMAND_YES || (d->enabled_bytes == CONFIG_ONDEMAND_ONDEMAND && bytes_sum)) {
280                         d->enabled_bytes = CONFIG_ONDEMAND_YES;
281
282                         if(unlikely(!d->st_bytes)) {
283                                 d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
284                                 if(unlikely(!d->st_bytes)) {
285                                         debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
286                                         d->st_bytes = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, RRD_TYPE_TC ".qos", "Class Usage", "kilobits/s", 7000, rrd_update_every, RRDSET_TYPE_STACKED);
287                                 }
288                         }
289                         else {
290                                 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
291                                 rrdset_next_plugins(d->st_bytes);
292
293                                 if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
294                                         rrdset_set_name(d->st_bytes, d->name);
295                                         d->name_updated = 0;
296                                 }
297
298                                 // FIXME
299                                 // update the family
300                         }
301
302                         for(c = d->classes ; c ; c = c->next) {
303                                 if(unlikely(!c->updated)) continue;
304
305                                 if(c->isleaf && c->hasparent) {
306                                         c->seen++;
307
308                                         if(unlikely(!c->rd_bytes)) {
309                                                 c->rd_bytes = rrddim_find(d->st_bytes, c->id);
310                                                 if(unlikely(!c->rd_bytes)) {
311                                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
312
313                                                         // new class, we have to add it
314                                                         c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
315                                                 }
316                                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
317                                         }
318
319                                         rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
320
321                                         // if it has a name, different to the id
322                                         if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
323                                                 // update the rrd dimension with the new name
324                                                 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
325                                                 rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
326                                         }
327                                 }
328                         }
329                         rrdset_done(d->st_bytes);
330                 }
331
332                 // --------------------------------------------------------------------
333                 // packets
334                 
335                 if(d->enabled_packets == CONFIG_ONDEMAND_YES || (d->enabled_packets == CONFIG_ONDEMAND_ONDEMAND && packets_sum)) {
336                         d->enabled_packets = CONFIG_ONDEMAND_YES;
337
338                         if(unlikely(!d->st_packets)) {
339                                 char id[RRD_ID_LENGTH_MAX + 1];
340                                 char name[RRD_ID_LENGTH_MAX + 1];
341                                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
342                                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
343
344                                 d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
345                                 if(unlikely(!d->st_packets)) {
346                                         debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
347                                         d->st_packets = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_packets", "Class Packets", "packets/s", 7010, rrd_update_every, RRDSET_TYPE_STACKED);
348                                 }
349                         }
350                         else {
351                                 debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
352                                 rrdset_next_plugins(d->st_packets);
353
354                                 // FIXME
355                                 // update the family
356                         }
357
358                         for(c = d->classes ; c ; c = c->next) {
359                                 if(unlikely(!c->updated)) continue;
360
361                                 if(c->isleaf && c->hasparent) {
362                                         if(unlikely(!c->rd_packets)) {
363                                                 c->rd_packets = rrddim_find(d->st_packets, c->id);
364                                                 if(unlikely(!c->rd_packets)) {
365                                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
366
367                                                         // new class, we have to add it
368                                                         c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
369                                                 }
370                                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
371                                         }
372
373                                         rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
374
375                                         // if it has a name, different to the id
376                                         if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
377                                                 // update the rrd dimension with the new name
378                                                 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
379                                                 rrddim_set_name(d->st_packets, c->rd_packets, c->name);
380                                         }
381                                 }
382                         }
383                         rrdset_done(d->st_packets);
384                 }
385
386                 // --------------------------------------------------------------------
387                 // dropped
388                 
389                 if(d->enabled_dropped == CONFIG_ONDEMAND_YES || (d->enabled_dropped == CONFIG_ONDEMAND_ONDEMAND && dropped_sum)) {
390                         d->enabled_dropped = CONFIG_ONDEMAND_YES;
391                         
392                         if(unlikely(!d->st_dropped)) {
393                                 char id[RRD_ID_LENGTH_MAX + 1];
394                                 char name[RRD_ID_LENGTH_MAX + 1];
395                                 snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
396                                 snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
397
398                                 d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
399                                 if(unlikely(!d->st_dropped)) {
400                                         debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
401                                         d->st_dropped = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_dropped", "Class Dropped Packets", "packets/s", 7020, rrd_update_every, RRDSET_TYPE_STACKED);
402                                 }
403                         }
404                         else {
405                                 debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
406                                 rrdset_next_plugins(d->st_dropped);
407
408                                 // FIXME
409                                 // update the family
410                         }
411
412                         for(c = d->classes ; c ; c = c->next) {
413                                 if(unlikely(!c->updated)) continue;
414
415                                 if(c->isleaf && c->hasparent) {
416                                         if(unlikely(!c->rd_dropped)) {
417                                                 c->rd_dropped = rrddim_find(d->st_dropped, c->id);
418                                                 if(unlikely(!c->rd_dropped)) {
419                                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
420
421                                                         // new class, we have to add it
422                                                         c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
423                                                 }
424                                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
425                                         }
426
427                                         rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
428
429                                         // if it has a name, different to the id
430                                         if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
431                                                 // update the rrd dimension with the new name
432                                                 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
433                                                 rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
434                                         }
435                                 }
436                         }
437                         rrdset_done(d->st_dropped);
438                 }
439         }
440
441         tc_device_classes_cleanup(d);
442 }
443
444 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
445 {
446         struct tc_class *c = tc_class_index_find(d, id, 0);
447         if(likely(c)) {
448                 if(likely(c->name))
449                         free(c->name);
450
451                 c->name = NULL;
452
453                 if(likely(name && *name && strcmp(c->id, name) != 0)) {
454                         debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
455                         c->name = strdup(name);
456                         if(likely(c->name))
457                                 c->name_updated = 1;
458                 }
459         }
460 }
461
462 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
463         if(likely(d->name))
464                 free(d->name);
465
466         d->name = NULL;
467
468         if(likely(name && *name && strcmp(d->id, name) != 0)) {
469                 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
470                 d->name = strdup(name);
471                 if(likely(d->name))
472                         d->name_updated = 1;
473         }
474 }
475
476 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
477         if(likely(d->family))
478                 free(d->family);
479
480         d->family = NULL;
481
482         if(likely(family && *family && strcmp(d->id, family) != 0)) {
483                 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
484                 d->family = strdup(family);
485                 if(likely(d->family))
486                         d->family_updated = 1;
487         }
488         // no need for null termination - it is already null
489 }
490
491 static inline struct tc_device *tc_device_create(char *id)
492 {
493         struct tc_device *d = tc_device_index_find(id, 0);
494
495         if(!d) {
496                 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
497
498                 d = calloc(1, sizeof(struct tc_device));
499                 if(!d) {
500                         fatal("Cannot allocate memory for tc_device %s", id);
501                         return NULL;
502                 }
503
504                 d->id = strdup(id);
505                 d->hash = simple_hash(d->id);
506                 d->enabled = -1;
507
508                 avl_init(&d->classes_index, tc_class_compare);
509                 tc_device_index_add(d);
510
511                 if(!tc_device_root) {
512                         tc_device_root = d;
513                 }
514                 else {
515                         d->next = tc_device_root;
516                         tc_device_root->prev = d;
517                         tc_device_root = d;
518                 }
519         }
520
521         return(d);
522 }
523
524 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
525 {
526         struct tc_class *c = tc_class_index_find(n, id, 0);
527
528         if(!c) {
529                 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
530
531                 c = calloc(1, sizeof(struct tc_class));
532                 if(!c) {
533                         fatal("Cannot allocate memory for tc class");
534                         return NULL;
535                 }
536
537                 if(n->classes) n->classes->prev = c;
538                 c->next = n->classes;
539                 n->classes = c;
540
541                 c->id = strdup(id);
542                 if(!c->id) {
543                         free(c);
544                         return NULL;
545                 }
546                 c->hash = simple_hash(c->id);
547
548                 if(parentid && *parentid) {
549                         c->parentid = strdup(parentid);
550                         c->parent_hash = simple_hash(c->parentid);
551                 }
552
553                 if(leafid && *leafid) {
554                         c->leafid = strdup(leafid);
555                         c->leaf_hash = simple_hash(c->leafid);
556                 }
557
558                 tc_class_index_add(n, c);
559         }
560
561         c->seen = 1;
562
563         return(c);
564 }
565
566 static inline void tc_device_free(struct tc_device *n)
567 {
568         if(n->next) n->next->prev = n->prev;
569         if(n->prev) n->prev->next = n->next;
570         if(tc_device_root == n) {
571                 if(n->next) tc_device_root = n->next;
572                 else tc_device_root = n->prev;
573         }
574
575         tc_device_index_del(n);
576
577         while(n->classes) tc_class_free(n, n->classes);
578
579         if(n->id) free(n->id);
580         if(n->name) free(n->name);
581         if(n->family) free(n->family);
582
583         free(n);
584 }
585
586 static inline void tc_device_free_all()
587 {
588         while(tc_device_root)
589                 tc_device_free(tc_device_root);
590 }
591
592 #define MAX_WORDS 20
593
594 static inline int tc_space(char c) {
595         switch(c) {
596         case ' ':
597         case '\t':
598         case '\r':
599         case '\n':
600                 return 1;
601
602         default:
603                 return 0;
604         }
605 }
606
607 static inline void tc_split_words(char *str, char **words, int max_words) {
608         char *s = str;
609         int i = 0;
610
611         // skip all white space
612         while(tc_space(*s)) s++;
613
614         // store the first word
615         words[i++] = s;
616
617         // while we have something
618         while(*s) {
619                 // if it is a space
620                 if(unlikely(tc_space(*s))) {
621
622                         // terminate the word
623                         *s++ = '\0';
624
625                         // skip all white space
626                         while(tc_space(*s)) s++;
627
628                         // if we reached the end, stop
629                         if(!*s) break;
630
631                         // store the next word
632                         if(i < max_words) words[i++] = s;
633                         else break;
634                 }
635                 else s++;
636         }
637
638         // terminate the words
639         while(i < max_words) words[i++] = NULL;
640 }
641
642 pid_t tc_child_pid = 0;
643 void *tc_main(void *ptr) {
644         (void)ptr;
645
646         info("TC thread created with task id %d", gettid());
647
648         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
649                 error("Cannot set pthread cancel type to DEFERRED.");
650
651         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
652                 error("Cannot set pthread cancel state to ENABLE.");
653
654         struct rusage thread;
655         RRDSET *stcpu = NULL, *sttime = NULL;
656
657         char buffer[TC_LINE_MAX+1] = "";
658         char *words[MAX_WORDS] = { NULL };
659
660         uint32_t BEGIN_HASH = simple_hash("BEGIN");
661         uint32_t END_HASH = simple_hash("END");
662         uint32_t CLASS_HASH = simple_hash("class");
663         uint32_t SENT_HASH = simple_hash("Sent");
664         uint32_t LENDED_HASH = simple_hash("lended:");
665         uint32_t TOKENS_HASH = simple_hash("tokens:");
666         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
667         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
668         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
669         uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
670 #ifdef DETACH_PLUGINS_FROM_NETDATA
671         uint32_t MYPID_HASH = simple_hash("MYPID");
672 #endif
673         uint32_t first_hash;
674
675         for(;1;) {
676                 if(unlikely(netdata_exit)) break;
677
678                 FILE *fp;
679                 struct tc_device *device = NULL;
680                 struct tc_class *class = NULL;
681
682                 snprintfz(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);
683                 debug(D_TC_LOOP, "executing '%s'", buffer);
684
685                 fp = mypopen(buffer, &tc_child_pid);
686                 if(unlikely(!fp)) {
687                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
688                         pthread_exit(NULL);
689                         return NULL;
690                 }
691
692                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
693                         if(unlikely(netdata_exit)) break;
694
695                         buffer[TC_LINE_MAX] = '\0';
696                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
697
698                         tc_split_words(buffer, words, MAX_WORDS);
699
700                         if(unlikely(!words[0] || !*words[0])) {
701                                 // debug(D_TC_LOOP, "empty line");
702                                 continue;
703                         }
704                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
705
706                         first_hash = simple_hash(words[0]);
707
708                         if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
709                                 // 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]);
710
711                                 // words[1] : class type
712                                 // words[2] : N:XX
713                                 // words[3] : parent or root
714                                 if(likely(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0))) {
715                                         //char *type     = words[1];  // the class: htb, fq_codel, etc
716
717                                         // we are only interested for HTB classes
718                                         //if(strcmp(type, "htb") != 0) continue;
719
720                                         char *id       = words[2];      // the class major:minor
721                                         char *parent   = words[3];      // 'parent' or 'root'
722                                         char *parentid = words[4];      // the parent's id
723                                         char *leaf     = words[5];      // 'leaf'
724                                         char *leafid   = words[6];      // leafid
725
726                                         if(strcmp(parent, "root") == 0) {
727                                                 parentid = NULL;
728                                                 leafid = NULL;
729                                         }
730                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
731                                                 leafid = NULL;
732
733                                         char leafbuf[20 + 1] = "";
734                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
735                                                 strncpyz(leafbuf, leafid, 20 - 1);
736                                                 strcat(leafbuf, "1");
737                                                 leafid = leafbuf;
738                                         }
739
740                                         class = tc_class_add(device, id, parentid, leafid);
741                                 }
742                                 else {
743                                         // clear the last class
744                                         class = NULL;
745                                 }
746                         }
747                         else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
748                                 // debug(D_TC_LOOP, "END line");
749
750                                 if(likely(device)) {
751                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
752                                                 error("Cannot set pthread cancel state to DISABLE.");
753
754                                         tc_device_commit(device);
755                                         // tc_device_free(device);
756
757                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
758                                                 error("Cannot set pthread cancel state to ENABLE.");
759                                 }
760
761                                 device = NULL;
762                                 class = NULL;
763                         }
764                         else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
765                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
766
767                                 if(likely(words[1] && *words[1])) {
768                                         device = tc_device_create(words[1]);
769                                 }
770                                 else {
771                                         // tc_device_free(device);
772                                         device = NULL;
773                                 }
774
775                                 class = NULL;
776                         }
777                         else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
778                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
779                                 if(likely(words[1] && *words[1])) {
780                                         class->bytes = strtoull(words[1], NULL, 10);
781                                         class->updated = 1;
782                                 }
783                                 else {
784                                         class->updated = 0;
785                                 }
786
787                                 if(likely(words[3] && *words[3]))
788                                         class->packets = strtoull(words[3], NULL, 10);
789
790                                 if(likely(words[6] && *words[6]))
791                                         class->dropped = strtoull(words[6], NULL, 10);
792
793                                 if(likely(words[8] && *words[8]))
794                                         class->overlimits = strtoull(words[8], NULL, 10);
795
796                                 if(likely(words[10] && *words[10]))
797                                         class->requeues = strtoull(words[8], NULL, 10);
798                         }
799                         else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
800                                 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
801                                 if(likely(words[1] && *words[1]))
802                                         class->lended = strtoull(words[1], NULL, 10);
803
804                                 if(likely(words[3] && *words[3]))
805                                         class->borrowed = strtoull(words[3], NULL, 10);
806
807                                 if(likely(words[5] && *words[5]))
808                                         class->giants = strtoull(words[5], NULL, 10);
809                         }
810                         else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
811                                 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
812                                 if(likely(words[1] && *words[1]))
813                                         class->tokens = strtoull(words[1], NULL, 10);
814
815                                 if(likely(words[3] && *words[3]))
816                                         class->ctokens = strtoull(words[3], NULL, 10);
817                         }
818                         else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
819                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
820                                 if(likely(words[1] && *words[1]))
821                                         tc_device_set_device_name(device, words[1]);
822                         }
823                         else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
824                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
825                                 if(likely(words[1] && *words[1]))
826                                         tc_device_set_device_family(device, words[1]);
827                         }
828                         else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
829                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
830                                 char *id    = words[1];
831                                 char *path  = words[2];
832                                 if(likely(id && *id && path && *path))
833                                         tc_device_set_class_name(device, id, path);
834                         }
835                         else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
836                                 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
837                                 getrusage(RUSAGE_THREAD, &thread);
838
839                                 if(unlikely(!stcpu)) stcpu = rrdset_find("netdata.plugin_tc_cpu");
840                                 if(unlikely(!stcpu)) {
841                                         stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
842                                         rrddim_add(stcpu, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
843                                         rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
844                                 }
845                                 else rrdset_next(stcpu);
846
847                                 rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
848                                 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
849                                 rrdset_done(stcpu);
850
851                                 if(unlikely(!sttime)) stcpu = rrdset_find("netdata.plugin_tc_time");
852                                 if(unlikely(!sttime)) {
853                                         sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
854                                         rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
855                                 }
856                                 else rrdset_next(sttime);
857
858                                 rrddim_set(sttime, "run_time", atoll(words[1]));
859                                 rrdset_done(sttime);
860
861                         }
862 #ifdef DETACH_PLUGINS_FROM_NETDATA
863                         else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
864                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
865                                 char *id = words[1];
866                                 pid_t pid = atol(id);
867
868                                 if(likely(pid)) tc_child_pid = pid;
869
870                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
871                         }
872 #endif
873                         //else {
874                         //      debug(D_TC_LOOP, "IGNORED line");
875                         //}
876                 }
877
878                 // fgets() failed or loop broke
879                 int code = mypclose(fp, tc_child_pid);
880                 tc_child_pid = 0;
881
882                 if(unlikely(device)) {
883                         // tc_device_free(device);
884                         device = NULL;
885                         class = NULL;
886                 }
887
888                 if(unlikely(netdata_exit)) {
889                         tc_device_free_all();
890                         pthread_exit(NULL);
891                         return NULL;
892                 }
893
894                 if(code == 1 || code == 127) {
895                         // 1 = DISABLE
896                         // 127 = cannot even run it
897                         error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
898
899                         tc_device_free_all();
900                         pthread_exit(NULL);
901                         return NULL;
902                 }
903
904                 sleep((unsigned int) rrd_update_every);
905         }
906
907         pthread_exit(NULL);
908         return NULL;
909 }