12 #include "appconfig.h"
15 #include "plugin_tc.h"
17 #define RRD_TYPE_TC "tc"
18 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
20 // ----------------------------------------------------------------------------
22 // this requires the script plugins.d/tc-qos-helper.sh
24 #define TC_LINE_MAX 1024
42 unsigned long long bytes;
44 char updated; // updated bytes
45 char seen; // seen in the tc list (even without bytes)
47 struct tc_class *next;
48 struct tc_class *prev;
60 avl_tree classes_index;
62 struct tc_class *classes;
66 // ----------------------------------------------------------------------------
69 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
71 static int tc_device_compare(void* a, void* b) {
72 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
73 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
74 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
77 avl_tree tc_device_root_index = {
82 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
83 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
85 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
86 struct tc_device *result = NULL, tmp;
88 tmp.hash = (hash)?hash:simple_hash(tmp.id);
90 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
95 // ----------------------------------------------------------------------------
98 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
100 static int tc_class_compare(void* a, void* b) {
101 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
102 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
103 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
106 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
107 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
109 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
110 struct tc_class *result = NULL, tmp;
112 tmp.hash = (hash)?hash:simple_hash(tmp.id);
114 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
118 // ----------------------------------------------------------------------------
120 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
121 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);
123 if(c->next) c->next->prev = c->prev;
124 if(c->prev) c->prev->next = c->next;
125 if(n->classes == c) n->classes = c->next;
127 tc_class_index_del(n, c);
129 if(c->id) free(c->id);
130 if(c->name) free(c->name);
131 if(c->leafid) free(c->leafid);
132 if(c->parentid) free(c->parentid);
137 static void tc_device_classes_cleanup(struct tc_device *d) {
138 static int cleanup_every = 999;
140 if(cleanup_every > 0) {
141 cleanup_every = -config_get_number("plugin:tc", "cleanup unused classes every", 60);
142 if(cleanup_every > 0) cleanup_every = -cleanup_every;
143 if(cleanup_every == 0) cleanup_every = -1;
146 struct tc_class *c = d->classes;
148 if(c->seen < cleanup_every) {
149 struct tc_class *nc = c->next;
162 static void tc_device_commit(struct tc_device *d)
164 static int enable_new_interfaces = -1;
166 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
168 // we only need to add leaf classes
169 struct tc_class *c, *x;
172 for(c = d->classes ; c ; c = c->next) {
177 // mark the classes as leafs and parents
178 for(c = d->classes ; c ; c = c->next) {
179 if(!c->updated) continue;
181 for(x = d->classes ; x ; x = x->next) {
182 if(!x->updated) continue;
187 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
188 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
189 // 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);
198 for ( c = d->classes ; c ; c = c->next) {
199 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
200 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);
204 // we need at least a class
205 for(c = d->classes ; c ; c = c->next) {
206 // 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);
207 if(!c->updated) continue;
208 if(c->isleaf && c->hasparent) break;
211 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
212 tc_device_classes_cleanup(d);
216 char var_name[CONFIG_MAX_NAME + 1];
217 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
218 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
219 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
221 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
223 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);
225 for(c = d->classes ; c ; c = c->next) {
226 if(!c->updated) continue;
228 if(c->isleaf && c->hasparent)
229 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
233 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
234 rrdset_next_plugins(st);
236 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
239 for(c = d->classes ; c ; c = c->next) {
240 if(!c->updated) continue;
242 if(c->isleaf && c->hasparent) {
243 RRDDIM *rd = rrddim_find(st, c->id);
246 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
248 // new class, we have to add it
249 rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
251 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
253 rrddim_set_by_pointer(st, rd, c->bytes);
255 // if it has a name, different to the id
257 // update the rrd dimension with the new name
258 debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
259 rrddim_set_name(st, rd, c->name);
269 tc_device_classes_cleanup(d);
272 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
274 struct tc_class *c = tc_class_index_find(d, id, 0);
276 if(c->name) free(c->name);
279 if(name && *name && strcmp(c->id, name) != 0) {
280 debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
281 c->name = strdup(name);
286 static void tc_device_set_device_name(struct tc_device *d, char *name) {
287 if(d->name) free(d->name);
290 if(name && *name && strcmp(d->id, name) != 0) {
291 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
292 d->name = strdup(name);
296 static void tc_device_set_device_family(struct tc_device *d, char *family) {
297 if(d->family) free(d->family);
300 if(family && *family && strcmp(d->id, family) != 0) {
301 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
302 d->family = strdup(family);
304 // no need for null termination - it is already null
307 static struct tc_device *tc_device_create(char *id)
309 struct tc_device *d = tc_device_index_find(id, 0);
312 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
314 d = calloc(1, sizeof(struct tc_device));
316 fatal("Cannot allocate memory for tc_device %s", id);
321 d->hash = simple_hash(d->id);
323 d->classes_index.root = NULL;
324 d->classes_index.compar = tc_class_compare;
326 tc_device_index_add(d);
332 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
334 struct tc_class *c = tc_class_index_find(n, id, 0);
337 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
339 c = calloc(1, sizeof(struct tc_class));
341 fatal("Cannot allocate memory for tc class");
345 if(n->classes) n->classes->prev = c;
346 c->next = n->classes;
354 c->hash = simple_hash(c->id);
356 if(parentid && *parentid) {
357 c->parentid = strdup(parentid);
358 c->parent_hash = simple_hash(c->parentid);
361 if(leafid && *leafid) {
362 c->leafid = strdup(leafid);
363 c->leaf_hash = simple_hash(c->leafid);
366 tc_class_index_add(n, c);
374 static void tc_device_free(struct tc_device *n)
376 tc_device_index_del(n);
378 while(n->classes) tc_class_free(n, n->classes);
380 if(n->id) free(n->id);
381 if(n->name) free(n->name);
382 if(n->family) free(n->family);
390 static inline int tc_space(char c) {
403 static void tc_split_words(char *str, char **words, int max_words) {
407 // skip all white space
408 while(tc_space(*s)) s++;
410 // store the first word
413 // while we have something
418 // terminate the word
421 // skip all white space
422 while(tc_space(*s)) s++;
424 // if we reached the end, stop
427 // store the next word
428 if(i < max_words) words[i++] = s;
434 // terminate the words
435 while(i < max_words) words[i++] = NULL;
438 pid_t tc_child_pid = 0;
439 void *tc_main(void *ptr)
443 info("TC thread created with task id %d", gettid());
445 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
446 error("Cannot set pthread cancel type to DEFERRED.");
448 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
449 error("Cannot set pthread cancel state to ENABLE.");
451 char buffer[TC_LINE_MAX+1] = "";
452 char *words[MAX_WORDS] = { NULL };
454 uint32_t BEGIN_HASH = simple_hash("BEGIN");
455 uint32_t END_HASH = simple_hash("END");
456 uint32_t CLASS_HASH = simple_hash("class");
457 uint32_t SENT_HASH = simple_hash("Sent");
458 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
459 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
460 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
461 #ifdef DETACH_PLUGINS_FROM_NETDATA
462 uint32_t MYPID_HASH = simple_hash("MYPID");
468 struct tc_device *device = NULL;
469 struct tc_class *class = NULL;
471 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);
472 debug(D_TC_LOOP, "executing '%s'", buffer);
473 // fp = popen(buffer, "r");
474 fp = mypopen(buffer, &tc_child_pid);
476 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
480 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
481 buffer[TC_LINE_MAX] = '\0';
482 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
484 tc_split_words(buffer, words, MAX_WORDS);
485 if(!words[0] || !*words[0]) {
486 // debug(D_TC_LOOP, "empty line");
489 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
491 first_hash = simple_hash(words[0]);
493 if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
494 // 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]);
496 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
497 // char *type = words[1]; // the class: htb, fq_codel, etc
498 char *id = words[2]; // the class major:minor
499 char *parent = words[3]; // 'parent' or 'root'
500 char *parentid = words[4]; // the parent's id
501 char *leaf = words[5]; // 'leaf'
502 char *leafid = words[6]; // leafid
504 if(strcmp(parent, "root") == 0) {
508 else if(!leaf || strcmp(leaf, "leaf") != 0)
511 char leafbuf[20 + 1] = "";
512 if(leafid && leafid[strlen(leafid) - 1] == ':') {
513 strncpy(leafbuf, leafid, 20 - 1);
514 strcat(leafbuf, "1");
518 class = tc_class_add(device, id, parentid, leafid);
521 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
522 // debug(D_TC_LOOP, "END line");
525 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
526 error("Cannot set pthread cancel state to DISABLE.");
528 tc_device_commit(device);
529 // tc_device_free(device);
533 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
534 error("Cannot set pthread cancel state to ENABLE.");
537 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
538 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
541 // tc_device_free(device);
546 if(words[1] && *words[1]) {
547 device = tc_device_create(words[1]);
551 else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
552 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
553 if(words[1] && *words[1]) {
554 class->bytes = atoll(words[1]);
557 else class->bytes = 0;
559 else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
560 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
561 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
563 else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
564 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
565 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
567 else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
568 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
570 char *path = words[2];
571 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
573 #ifdef DETACH_PLUGINS_FROM_NETDATA
574 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
575 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
577 pid_t pid = atol(id);
579 if(pid) tc_child_pid = pid;
581 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
585 // debug(D_TC_LOOP, "IGNORED line");
588 mypclose(fp, tc_child_pid);
592 // tc_device_free(device);
597 sleep(rrd_update_every);