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;
43 struct tc_class *next;
55 avl_tree classes_index;
57 struct tc_class *classes;
61 // ----------------------------------------------------------------------------
64 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
66 static int tc_device_compare(void* a, void* b) {
67 if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
68 else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
69 else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
72 avl_tree tc_device_root_index = {
77 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
78 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
80 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
81 struct tc_device *result = NULL, tmp;
83 tmp.hash = (hash)?hash:simple_hash(tmp.id);
85 avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
90 // ----------------------------------------------------------------------------
93 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
95 static int tc_class_compare(void* a, void* b) {
96 if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
97 else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
98 else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
101 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
102 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
104 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
105 struct tc_class *result = NULL, tmp;
107 tmp.hash = (hash)?hash:simple_hash(tmp.id);
109 avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
113 // ----------------------------------------------------------------------------
115 static void tc_device_commit(struct tc_device *d)
117 static int enable_new_interfaces = -1;
119 if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
121 // we only need to add leaf classes
122 struct tc_class *c, *x;
124 // set them all as leafs without parent
125 for(c = d->classes ; c ; c = c->next) {
130 // mark the classes as leafs and parents
131 for(c = d->classes ; c ; c = c->next) {
132 if(!c->updated) continue;
134 for(x = d->classes ; x ; x = x->next) {
135 if(!x->updated) continue;
138 ( c->hash == x->parent_hash && strcmp(c->id, x->parentid) == 0) ||
139 (c->leafid && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
140 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has leaf the class '%s' (parentid: '%s').", d->name, c->name, c->leafid, x->name, x->parentid);
149 for ( c = d->classes ; c ; c = c->next) {
150 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
151 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);
156 for(c = d->classes ; c ; c = c->next) {
157 if(!c->updated) continue;
158 if(c->isleaf && c->hasparent) break;
161 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
165 char var_name[CONFIG_MAX_NAME + 1];
166 snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
167 if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
168 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
170 debug(D_TC_LOOP, "TC: Committing new TC device '%s'", d->name?d->name:d->id);
172 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);
174 for(c = d->classes ; c ; c = c->next) {
175 if(!c->updated) continue;
177 if(c->isleaf && c->hasparent)
178 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
182 rrdset_next_plugins(st);
184 if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
187 for(c = d->classes ; c ; c = c->next) {
188 if(!c->updated) continue;
190 if(c->isleaf && c->hasparent) {
191 if(rrddim_set(st, c->id, c->bytes) != 0) {
193 // new class, we have to add it
194 rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
195 rrddim_set(st, c->id, c->bytes);
198 // if it has a name, different to the id
200 // update the rrd dimension with the new name
201 RRDDIM *rd = rrddim_find(st, c->id);
202 if(rd) rrddim_set_name(st, rd, c->name);
215 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
217 struct tc_class *c = tc_class_index_find(d, id, 0);
219 if(c->name) free(c->name);
222 if(name && *name && strcmp(c->id, name) != 0) c->name = strdup(name);
226 static void tc_device_set_device_name(struct tc_device *d, char *name) {
227 if(d->name) free(d->name);
230 if(name && *name && strcmp(d->id, name) != 0) d->name = strdup(name);
233 static void tc_device_set_device_family(struct tc_device *d, char *family) {
234 if(d->family) free(d->family);
237 if(family && *family && strcmp(d->id, family) != 0) d->family = strdup(family);
238 // no need for null termination - it is already null
241 static struct tc_device *tc_device_create(char *id)
243 struct tc_device *d = tc_device_index_find(id, 0);
247 d = calloc(1, sizeof(struct tc_device));
249 fatal("Cannot allocate memory for tc_device %s", id);
254 d->hash = simple_hash(d->id);
256 d->classes_index.root = NULL;
257 d->classes_index.compar = tc_class_compare;
259 tc_device_index_add(d);
265 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
267 // fprintf(stderr, "Adding class id '%s', parentid '%s', leafid '%s'\n", id, parentid, leafid);
269 struct tc_class *c = tc_class_index_find(n, id, 0);
271 c = calloc(1, sizeof(struct tc_class));
273 fatal("Cannot allocate memory for tc class");
277 c->next = n->classes;
285 c->hash = simple_hash(c->id);
287 if(parentid && *parentid) {
288 c->parentid = strdup(parentid);
289 c->parent_hash = simple_hash(c->parentid);
292 if(leafid && *leafid) {
293 c->leafid = strdup(leafid);
294 c->leaf_hash = simple_hash(c->leafid);
297 tc_class_index_add(n, c);
303 static void tc_class_free_all(struct tc_device *n, struct tc_class *c)
305 if(c->next) tc_class_free_all(n, c->next);
306 tc_class_index_del(n, c);
308 if(c->id) free(c->id);
309 if(c->name) free(c->name);
310 if(c->leafid) free(c->leafid);
311 if(c->parentid) free(c->parentid);
316 static void tc_device_free(struct tc_device *n)
318 tc_device_index_del(n);
319 if(n->classes) tc_class_free_all(n, n->classes);
321 if(n->id) free(n->id);
322 if(n->name) free(n->name);
323 if(n->family) free(n->family);
331 int tc_space(char c) {
344 void tc_split_words(char *str, char **words, int max_words) {
348 // skip all white space
349 while(tc_space(*s)) s++;
351 // store the first word
354 // while we have something
359 // terminate the word
362 // skip all white space
363 while(tc_space(*s)) s++;
365 // if we reached the end, stop
368 // store the next word
369 if(i < max_words) words[i++] = s;
375 // terminate the words
376 while(i < max_words) words[i++] = NULL;
379 pid_t tc_child_pid = 0;
380 void *tc_main(void *ptr)
384 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
385 error("Cannot set pthread cancel type to DEFERRED.");
387 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
388 error("Cannot set pthread cancel state to ENABLE.");
390 char buffer[TC_LINE_MAX+1] = "";
391 char *words[MAX_WORDS] = { NULL };
393 uint32_t BEGIN_HASH = simple_hash("BEGIN");
394 uint32_t END_HASH = simple_hash("END");
395 uint32_t CLASS_HASH = simple_hash("class");
396 uint32_t SENT_HASH = simple_hash("Sent");
397 uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
398 uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
399 uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
400 #ifdef DETACH_PLUGINS_FROM_NETDATA
401 uint32_t MYPID_HASH = simple_hash("MYPID");
407 struct tc_device *device = NULL;
408 struct tc_class *class = NULL;
410 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);
411 debug(D_TC_LOOP, "executing '%s'", buffer);
412 // fp = popen(buffer, "r");
413 fp = mypopen(buffer, &tc_child_pid);
415 error("TC: Cannot popen(\"%s\", \"r\").", buffer);
419 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
420 buffer[TC_LINE_MAX] = '\0';
421 // debug(D_TC_LOOP, "TC: read '%s'", buffer);
423 tc_split_words(buffer, words, MAX_WORDS);
424 if(!words[0] || !*words[0]) {
425 // debug(D_TC_LOOP, "empty line");
428 // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
430 first_hash = simple_hash(words[0]);
432 if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
433 // 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]);
435 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
436 // char *type = words[1]; // the class: htb, fq_codel, etc
437 char *id = words[2]; // the class major:minor
438 char *parent = words[3]; // 'parent' or 'root'
439 char *parentid = words[4]; // the parent's id
440 char *leaf = words[5]; // 'leaf'
441 char *leafid = words[6]; // leafid
443 if(strcmp(parent, "root") == 0) {
447 else if(!leaf || strcmp(leaf, "leaf") != 0)
450 char leafbuf[20 + 1] = "";
451 if(leafid && leafid[strlen(leafid) - 1] == ':') {
452 strncpy(leafbuf, leafid, 20 - 1);
453 strcat(leafbuf, "1");
457 class = tc_class_add(device, id, parentid, leafid);
460 else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
461 // debug(D_TC_LOOP, "END line");
464 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
465 error("Cannot set pthread cancel state to DISABLE.");
467 tc_device_commit(device);
468 // tc_device_free(device);
472 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
473 error("Cannot set pthread cancel state to ENABLE.");
476 else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
477 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
480 // tc_device_free(device);
485 if(words[1] && *words[1]) {
486 device = tc_device_create(words[1]);
490 else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
491 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
492 if(words[1] && *words[1]) {
493 class->bytes = atoll(words[1]);
496 else class->bytes = 0;
498 else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
499 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
500 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
502 else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
503 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
504 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
506 else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
507 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
509 char *path = words[2];
510 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
512 #ifdef DETACH_PLUGINS_FROM_NETDATA
513 else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
514 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
516 pid_t pid = atol(id);
518 if(pid) tc_child_pid = pid;
520 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
524 // debug(D_TC_LOOP, "IGNORED line");
530 // tc_device_free(device);
535 sleep(rrd_update_every);