12 #include "plugin_tc.h"
14 #define RRD_TYPE_TC "tc"
15 #define RRD_TYPE_TC_LEN strlen(RRD_TYPE_TC)
17 // ----------------------------------------------------------------------------
19 // this requires the script plugins.d/tc-qos-helper.sh
21 #define TC_LINE_MAX 1024
39 unsigned long long bytes;
41 char updated; // updated bytes
42 char seen; // seen in the tc list (even without bytes)
44 struct tc_class *next;
45 struct tc_class *prev;
57 avl_tree classes_index;
59 struct tc_class *classes;
63 // ----------------------------------------------------------------------------
66 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
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);
74 avl_tree tc_device_root_index = {
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))
82 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
83 struct tc_device *result = NULL, tmp;
85 tmp.hash = (hash)?hash:simple_hash(tmp.id);
87 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
92 // ----------------------------------------------------------------------------
95 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
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);
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))
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;
109 tmp.hash = (hash)?hash:simple_hash(tmp.id);
111 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
115 // ----------------------------------------------------------------------------
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);
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;
124 tc_class_index_del(n, c);
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);
134 static void tc_device_classes_cleanup(struct tc_device *d) {
135 static int cleanup_every = 999;
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;
143 struct tc_class *c = d->classes;
145 if(c->seen < cleanup_every) {
146 struct tc_class *nc = c->next;
159 static void tc_device_commit(struct tc_device *d)
161 static int enable_new_interfaces = -1;
163 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
165 // we only need to add leaf classes
166 struct tc_class *c, *x;
169 for(c = d->classes ; c ; c = c->next) {
174 // mark the classes as leafs and parents
175 for(c = d->classes ; c ; c = c->next) {
176 if(!c->updated) continue;
178 for(x = d->classes ; x ; x = x->next) {
179 if(!x->updated) continue;
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);
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);
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;
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);
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);
218 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
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);
222 for(c = d->classes ; c ; c = c->next) {
223 if(!c->updated) continue;
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);
230 debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
231 rrdset_next_plugins(st);
233 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
236 for(c = d->classes ; c ; c = c->next) {
237 if(!c->updated) continue;
239 if(c->isleaf && c->hasparent) {
240 RRDDIM *rd = rrddim_find(st, c->id);
243 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
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);
248 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
250 rrddim_set_by_pointer(st, rd, c->bytes);
252 // if it has a name, different to the id
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);
266 tc_device_classes_cleanup(d);
269 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
271 struct tc_class *c = tc_class_index_find(d, id, 0);
273 if(c->name) free(c->name);
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);
283 static void tc_device_set_device_name(struct tc_device *d, char *name) {
284 if(d->name) free(d->name);
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);
293 static void tc_device_set_device_family(struct tc_device *d, char *family) {
294 if(d->family) free(d->family);
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);
301 // no need for null termination - it is already null
304 static struct tc_device *tc_device_create(char *id)
306 struct tc_device *d = tc_device_index_find(id, 0);
309 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
311 d = calloc(1, sizeof(struct tc_device));
313 fatal("Cannot allocate memory for tc_device %s", id);
318 d->hash = simple_hash(d->id);
320 d->classes_index.root = NULL;
321 d->classes_index.compar = tc_class_compare;
323 tc_device_index_add(d);
329 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
331 struct tc_class *c = tc_class_index_find(n, id, 0);
334 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
336 c = calloc(1, sizeof(struct tc_class));
338 fatal("Cannot allocate memory for tc class");
342 if(n->classes) n->classes->prev = c;
343 c->next = n->classes;
351 c->hash = simple_hash(c->id);
353 if(parentid && *parentid) {
354 c->parentid = strdup(parentid);
355 c->parent_hash = simple_hash(c->parentid);
358 if(leafid && *leafid) {
359 c->leafid = strdup(leafid);
360 c->leaf_hash = simple_hash(c->leafid);
363 tc_class_index_add(n, c);
371 static void tc_device_free(struct tc_device *n)
373 tc_device_index_del(n);
375 while(n->classes) tc_class_free(n, n->classes);
377 if(n->id) free(n->id);
378 if(n->name) free(n->name);
379 if(n->family) free(n->family);
387 static inline int tc_space(char c) {
400 static void tc_split_words(char *str, char **words, int max_words) {
404 // skip all white space
405 while(tc_space(*s)) s++;
407 // store the first word
410 // while we have something
415 // terminate the word
418 // skip all white space
419 while(tc_space(*s)) s++;
421 // if we reached the end, stop
424 // store the next word
425 if(i < max_words) words[i++] = s;
431 // terminate the words
432 while(i < max_words) words[i++] = NULL;
435 pid_t tc_child_pid = 0;
436 void *tc_main(void *ptr)
440 info("TC thread created with task id %d", gettid());
442 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
443 error("Cannot set pthread cancel type to DEFERRED.");
445 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
446 error("Cannot set pthread cancel state to ENABLE.");
448 char buffer[TC_LINE_MAX+1] = "";
449 char *words[MAX_WORDS] = { NULL };
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");
465 struct tc_device *device = NULL;
466 struct tc_class *class = NULL;
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);
473 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
477 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
478 buffer[TC_LINE_MAX] = '\0';
479 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
481 tc_split_words(buffer, words, MAX_WORDS);
482 if(!words[0] || !*words[0]) {
483 // debug(D_TC_LOOP, "empty line");
486 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
488 first_hash = simple_hash(words[0]);
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]);
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
501 if(strcmp(parent, "root") == 0) {
505 else if(!leaf || strcmp(leaf, "leaf") != 0)
508 char leafbuf[20 + 1] = "";
509 if(leafid && leafid[strlen(leafid) - 1] == ':') {
510 strncpy(leafbuf, leafid, 20 - 1);
511 strcat(leafbuf, "1");
515 class = tc_class_add(device, id, parentid, leafid);
518 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
519 // debug(D_TC_LOOP, "END line");
522 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
523 error("Cannot set pthread cancel state to DISABLE.");
525 tc_device_commit(device);
526 // tc_device_free(device);
530 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
531 error("Cannot set pthread cancel state to ENABLE.");
534 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
535 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
538 // tc_device_free(device);
543 if(words[1] && *words[1]) {
544 device = tc_device_create(words[1]);
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]);
554 else class->bytes = 0;
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]);
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]);
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]);
567 char *path = words[2];
568 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
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]);
574 pid_t pid = atol(id);
576 if(pid) tc_child_pid = pid;
578 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
582 // debug(D_TC_LOOP, "IGNORED line");
585 mypclose(fp, tc_child_pid);
589 // tc_device_free(device);
594 sleep(rrd_update_every);