Merge remote-tracking branch 'origin/collectd-5.8'
[collectd.git] / src / netlink.c
1 /**
2  * collectd - src/netlink.c
3  * Copyright (C) 2007-2010  Florian octo Forster
4  * Copyright (C) 2008-2012  Sebastian Harl
5  * Copyright (C) 2013       Andreas Henriksson
6  * Copyright (C) 2013       Marc Fournier
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; only version 2 of the License is applicable.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Florian octo Forster <octo at collectd.org>
23  *   Sebastian Harl <sh at tokkee.org>
24  *   Andreas Henriksson <andreas at fatal.se>
25  *   Marc Fournier <marc.fournier at camptocamp.com>
26  **/
27
28 #include "collectd.h"
29
30 #include "common.h"
31 #include "plugin.h"
32
33 #include <asm/types.h>
34
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
37 #if HAVE_LINUX_GEN_STATS_H
38 #include <linux/gen_stats.h>
39 #endif
40 #if HAVE_LINUX_PKT_SCHED_H
41 #include <linux/pkt_sched.h>
42 #endif
43
44 #include <libmnl/libmnl.h>
45
46 struct ir_link_stats_storage_s {
47
48   uint64_t rx_packets;
49   uint64_t tx_packets;
50   uint64_t rx_bytes;
51   uint64_t tx_bytes;
52   uint64_t rx_errors;
53   uint64_t tx_errors;
54
55   uint64_t rx_dropped;
56   uint64_t tx_dropped;
57   uint64_t multicast;
58   uint64_t collisions;
59
60   uint64_t rx_length_errors;
61   uint64_t rx_over_errors;
62   uint64_t rx_crc_errors;
63   uint64_t rx_frame_errors;
64   uint64_t rx_fifo_errors;
65   uint64_t rx_missed_errors;
66
67   uint64_t tx_aborted_errors;
68   uint64_t tx_carrier_errors;
69   uint64_t tx_fifo_errors;
70   uint64_t tx_heartbeat_errors;
71   uint64_t tx_window_errors;
72 };
73
74 union ir_link_stats_u {
75   struct rtnl_link_stats *stats32;
76 #ifdef HAVE_RTNL_LINK_STATS64
77   struct rtnl_link_stats64 *stats64;
78 #endif
79 };
80
81 typedef struct ir_ignorelist_s {
82   char *device;
83   char *type;
84   char *inst;
85   struct ir_ignorelist_s *next;
86 } ir_ignorelist_t;
87
88 struct qos_stats {
89   struct gnet_stats_basic *bs;
90   struct gnet_stats_queue *qs;
91 };
92
93 static int ir_ignorelist_invert = 1;
94 static ir_ignorelist_t *ir_ignorelist_head = NULL;
95
96 static struct mnl_socket *nl;
97
98 static char **iflist = NULL;
99 static size_t iflist_len = 0;
100
101 static const char *config_keys[] = {"Interface", "VerboseInterface",
102                                     "QDisc",     "Class",
103                                     "Filter",    "IgnoreSelected"};
104 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
105
106 static int add_ignorelist(const char *dev, const char *type, const char *inst) {
107   ir_ignorelist_t *entry;
108
109   entry = calloc(1, sizeof(*entry));
110   if (entry == NULL)
111     return -1;
112
113   if (strcasecmp(dev, "All") != 0) {
114     entry->device = strdup(dev);
115     if (entry->device == NULL) {
116       sfree(entry);
117       return -1;
118     }
119   }
120
121   entry->type = strdup(type);
122   if (entry->type == NULL) {
123     sfree(entry->device);
124     sfree(entry);
125     return -1;
126   }
127
128   if (inst != NULL) {
129     entry->inst = strdup(inst);
130     if (entry->inst == NULL) {
131       sfree(entry->type);
132       sfree(entry->device);
133       sfree(entry);
134       return -1;
135     }
136   }
137
138   entry->next = ir_ignorelist_head;
139   ir_ignorelist_head = entry;
140
141   return 0;
142 } /* int add_ignorelist */
143
144 /*
145  * Checks wether a data set should be ignored. Returns `true' is the value
146  * should be ignored, `false' otherwise.
147  */
148 static int check_ignorelist(const char *dev, const char *type,
149                             const char *type_instance) {
150   assert((dev != NULL) && (type != NULL));
151
152   if (ir_ignorelist_head == NULL)
153     return ir_ignorelist_invert ? 0 : 1;
154
155   for (ir_ignorelist_t *i = ir_ignorelist_head; i != NULL; i = i->next) {
156     /* i->device == NULL  =>  match all devices */
157     if ((i->device != NULL) && (strcasecmp(i->device, dev) != 0))
158       continue;
159
160     if (strcasecmp(i->type, type) != 0)
161       continue;
162
163     if ((i->inst != NULL) && (type_instance != NULL) &&
164         (strcasecmp(i->inst, type_instance) != 0))
165       continue;
166
167     DEBUG("netlink plugin: check_ignorelist: "
168           "(dev = %s; type = %s; inst = %s) matched "
169           "(dev = %s; type = %s; inst = %s)",
170           dev, type, type_instance == NULL ? "(nil)" : type_instance,
171           i->device == NULL ? "(nil)" : i->device, i->type,
172           i->inst == NULL ? "(nil)" : i->inst);
173
174     return ir_ignorelist_invert ? 0 : 1;
175   } /* for i */
176
177   return ir_ignorelist_invert;
178 } /* int check_ignorelist */
179
180 static void submit_one(const char *dev, const char *type,
181                        const char *type_instance, derive_t value) {
182   value_list_t vl = VALUE_LIST_INIT;
183
184   vl.values = &(value_t){.derive = value};
185   vl.values_len = 1;
186   sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
187   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
188   sstrncpy(vl.type, type, sizeof(vl.type));
189
190   if (type_instance != NULL)
191     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
192
193   plugin_dispatch_values(&vl);
194 } /* void submit_one */
195
196 static void submit_two(const char *dev, const char *type,
197                        const char *type_instance, derive_t rx, derive_t tx) {
198   value_list_t vl = VALUE_LIST_INIT;
199   value_t values[] = {
200       {.derive = rx}, {.derive = tx},
201   };
202
203   vl.values = values;
204   vl.values_len = STATIC_ARRAY_SIZE(values);
205   sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
206   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
207   sstrncpy(vl.type, type, sizeof(vl.type));
208
209   if (type_instance != NULL)
210     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
211
212   plugin_dispatch_values(&vl);
213 } /* void submit_two */
214
215 static int update_iflist(struct ifinfomsg *msg, const char *dev) {
216   /* Update the `iflist'. It's used to know which interfaces exist and query
217    * them later for qdiscs and classes. */
218   if ((msg->ifi_index >= 0) && ((size_t)msg->ifi_index >= iflist_len)) {
219     char **temp;
220
221     temp = realloc(iflist, (msg->ifi_index + 1) * sizeof(char *));
222     if (temp == NULL) {
223       ERROR("netlink plugin: update_iflist: realloc failed.");
224       return -1;
225     }
226
227     memset(temp + iflist_len, '\0',
228            (msg->ifi_index + 1 - iflist_len) * sizeof(char *));
229     iflist = temp;
230     iflist_len = msg->ifi_index + 1;
231   }
232   if ((iflist[msg->ifi_index] == NULL) ||
233       (strcmp(iflist[msg->ifi_index], dev) != 0)) {
234     sfree(iflist[msg->ifi_index]);
235     iflist[msg->ifi_index] = strdup(dev);
236   }
237
238   return 0;
239 } /* int update_iflist */
240
241 static void check_ignorelist_and_submit(const char *dev,
242                                         struct ir_link_stats_storage_s *stats) {
243
244   if (check_ignorelist(dev, "interface", NULL) == 0) {
245     submit_two(dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
246     submit_two(dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
247     submit_two(dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
248   } else {
249     DEBUG("netlink plugin: Ignoring %s/interface.", dev);
250   }
251
252   if (check_ignorelist(dev, "if_detail", NULL) == 0) {
253     submit_two(dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
254     submit_one(dev, "if_multicast", NULL, stats->multicast);
255     submit_one(dev, "if_collisions", NULL, stats->collisions);
256
257     submit_one(dev, "if_rx_errors", "length", stats->rx_length_errors);
258     submit_one(dev, "if_rx_errors", "over", stats->rx_over_errors);
259     submit_one(dev, "if_rx_errors", "crc", stats->rx_crc_errors);
260     submit_one(dev, "if_rx_errors", "frame", stats->rx_frame_errors);
261     submit_one(dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
262     submit_one(dev, "if_rx_errors", "missed", stats->rx_missed_errors);
263
264     submit_one(dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
265     submit_one(dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
266     submit_one(dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
267     submit_one(dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
268     submit_one(dev, "if_tx_errors", "window", stats->tx_window_errors);
269   } else {
270     DEBUG("netlink plugin: Ignoring %s/if_detail.", dev);
271   }
272
273 } /* void check_ignorelist_and_submit */
274
275 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name)                 \
276   (dst_stats)->value_name = (src_stats)->value_name
277
278 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats)                             \
279   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_packets);                      \
280   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_packets);                      \
281   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_bytes);                        \
282   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_bytes);                        \
283   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_errors);                       \
284   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_errors);                       \
285   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_dropped);                      \
286   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_dropped);                      \
287   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, multicast);                       \
288   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, collisions);                      \
289   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_length_errors);                \
290   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_over_errors);                  \
291   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_crc_errors);                   \
292   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_frame_errors);                 \
293   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_fifo_errors);                  \
294   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_missed_errors);                \
295   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_aborted_errors);               \
296   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_carrier_errors);               \
297   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_fifo_errors);                  \
298   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_heartbeat_errors);             \
299   COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_window_errors)
300
301 #ifdef HAVE_RTNL_LINK_STATS64
302 static void check_ignorelist_and_submit64(const char *dev,
303                                           struct rtnl_link_stats64 *stats) {
304   struct ir_link_stats_storage_s s;
305
306   COPY_RTNL_LINK_STATS(&s, stats);
307
308   check_ignorelist_and_submit(dev, &s);
309 }
310 #endif
311
312 static void check_ignorelist_and_submit32(const char *dev,
313                                           struct rtnl_link_stats *stats) {
314   struct ir_link_stats_storage_s s;
315
316   COPY_RTNL_LINK_STATS(&s, stats);
317
318   check_ignorelist_and_submit(dev, &s);
319 }
320
321 static int link_filter_cb(const struct nlmsghdr *nlh,
322                           void *args __attribute__((unused))) {
323   struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
324   struct nlattr *attr;
325   const char *dev = NULL;
326   union ir_link_stats_u stats;
327
328   if (nlh->nlmsg_type != RTM_NEWLINK) {
329     ERROR("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
330           nlh->nlmsg_type);
331     return MNL_CB_ERROR;
332   }
333
334   /* Scan attribute list for device name. */
335   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
336     if (mnl_attr_get_type(attr) != IFLA_IFNAME)
337       continue;
338
339     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
340       ERROR("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate "
341             "failed.");
342       return MNL_CB_ERROR;
343     }
344
345     dev = mnl_attr_get_str(attr);
346     if (update_iflist(ifm, dev) < 0)
347       return MNL_CB_ERROR;
348     break;
349   }
350
351   if (dev == NULL) {
352     ERROR("netlink plugin: link_filter_cb: dev == NULL");
353     return MNL_CB_ERROR;
354   }
355 #ifdef HAVE_RTNL_LINK_STATS64
356   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
357     if (mnl_attr_get_type(attr) != IFLA_STATS64)
358       continue;
359
360     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats64)) < 0) {
361       ERROR("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 "
362             "failed: %s",
363             STRERRNO);
364       return MNL_CB_ERROR;
365     }
366     stats.stats64 = mnl_attr_get_payload(attr);
367
368     check_ignorelist_and_submit64(dev, stats.stats64);
369
370     return MNL_CB_OK;
371   }
372 #endif
373   mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
374     if (mnl_attr_get_type(attr) != IFLA_STATS)
375       continue;
376
377     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats32)) < 0) {
378       ERROR("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 "
379             "failed: %s",
380             STRERRNO);
381       return MNL_CB_ERROR;
382     }
383     stats.stats32 = mnl_attr_get_payload(attr);
384
385     check_ignorelist_and_submit32(dev, stats.stats32);
386
387     return MNL_CB_OK;
388   }
389
390   DEBUG("netlink plugin: link_filter: No statistics for interface %s.", dev);
391   return MNL_CB_OK;
392
393 } /* int link_filter_cb */
394
395 #if HAVE_TCA_STATS2
396 static int qos_attr_cb(const struct nlattr *attr, void *data) {
397   struct qos_stats *q_stats = (struct qos_stats *)data;
398
399   /* skip unsupported attribute in user-space */
400   if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
401     return MNL_CB_OK;
402
403   if (mnl_attr_get_type(attr) == TCA_STATS_BASIC) {
404     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*q_stats->bs)) < 0) {
405       ERROR("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 "
406             "failed: %s",
407             STRERRNO);
408       return MNL_CB_ERROR;
409     }
410     q_stats->bs = mnl_attr_get_payload(attr);
411     return MNL_CB_OK;
412   }
413
414   if (mnl_attr_get_type(attr) == TCA_STATS_QUEUE) {
415     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*q_stats->qs)) < 0) {
416       ERROR("netlink plugin: qos_attr_cb: TCA_STATS_QUEUE mnl_attr_validate2 "
417             "failed.");
418       return MNL_CB_ERROR;
419     }
420     q_stats->qs = mnl_attr_get_payload(attr);
421     return MNL_CB_OK;
422   }
423
424   return MNL_CB_OK;
425 } /* qos_attr_cb */
426 #endif
427
428 static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
429   struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
430   struct nlattr *attr;
431
432   int wanted_ifindex = *((int *)args);
433
434   const char *dev;
435   const char *kind = NULL;
436
437   /* char *type_instance; */
438   const char *tc_type;
439   char tc_inst[DATA_MAX_NAME_LEN];
440
441   _Bool stats_submitted = 0;
442
443   if (nlh->nlmsg_type == RTM_NEWQDISC)
444     tc_type = "qdisc";
445   else if (nlh->nlmsg_type == RTM_NEWTCLASS)
446     tc_type = "class";
447   else if (nlh->nlmsg_type == RTM_NEWTFILTER)
448     tc_type = "filter";
449   else {
450     ERROR("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
451           nlh->nlmsg_type);
452     return MNL_CB_ERROR;
453   }
454
455   if (tm->tcm_ifindex != wanted_ifindex) {
456     DEBUG("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
457           "but expected #%i.",
458           tc_type, tm->tcm_ifindex, wanted_ifindex);
459     return MNL_CB_OK;
460   }
461
462   if ((tm->tcm_ifindex >= 0) && ((size_t)tm->tcm_ifindex >= iflist_len)) {
463     ERROR("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
464           ">= iflist_len = %" PRIsz,
465           tm->tcm_ifindex, iflist_len);
466     return MNL_CB_ERROR;
467   }
468
469   dev = iflist[tm->tcm_ifindex];
470   if (dev == NULL) {
471     ERROR("netlink plugin: qos_filter_cb: iflist[%i] == NULL", tm->tcm_ifindex);
472     return MNL_CB_ERROR;
473   }
474
475   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
476     if (mnl_attr_get_type(attr) != TCA_KIND)
477       continue;
478
479     if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
480       ERROR(
481           "netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
482       return MNL_CB_ERROR;
483     }
484
485     kind = mnl_attr_get_str(attr);
486     break;
487   }
488
489   if (kind == NULL) {
490     ERROR("netlink plugin: qos_filter_cb: kind == NULL");
491     return -1;
492   }
493
494   { /* The ID */
495     uint32_t numberic_id;
496
497     numberic_id = tm->tcm_handle;
498     if (strcmp(tc_type, "filter") == 0)
499       numberic_id = tm->tcm_parent;
500
501     snprintf(tc_inst, sizeof(tc_inst), "%s-%x:%x", kind, numberic_id >> 16,
502              numberic_id & 0x0000FFFF);
503   }
504
505   DEBUG("netlink plugin: qos_filter_cb: got %s for %s (%i).", tc_type, dev,
506         tm->tcm_ifindex);
507
508   if (check_ignorelist(dev, tc_type, tc_inst))
509     return MNL_CB_OK;
510
511 #if HAVE_TCA_STATS2
512   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
513     struct qos_stats q_stats;
514
515     memset(&q_stats, 0x0, sizeof(q_stats));
516
517     if (mnl_attr_get_type(attr) != TCA_STATS2)
518       continue;
519
520     if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
521       ERROR("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate "
522             "failed.");
523       return MNL_CB_ERROR;
524     }
525
526     mnl_attr_parse_nested(attr, qos_attr_cb, &q_stats);
527
528     if (q_stats.bs != NULL || q_stats.qs != NULL) {
529       char type_instance[DATA_MAX_NAME_LEN];
530
531       stats_submitted = 1;
532
533       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
534
535       if (q_stats.bs != NULL) {
536         submit_one(dev, "ipt_bytes", type_instance, q_stats.bs->bytes);
537         submit_one(dev, "ipt_packets", type_instance, q_stats.bs->packets);
538       }
539       if (q_stats.qs != NULL) {
540         submit_one(dev, "if_tx_dropped", type_instance, q_stats.qs->drops);
541       }
542     }
543
544     break;
545   }
546 #endif /* TCA_STATS2 */
547
548 #if HAVE_TCA_STATS
549   mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
550     struct tc_stats *ts = NULL;
551
552     if (mnl_attr_get_type(attr) != TCA_STATS)
553       continue;
554
555     if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*ts)) < 0) {
556       ERROR("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 "
557             "failed: %s",
558             STRERRNO);
559       return MNL_CB_ERROR;
560     }
561     ts = mnl_attr_get_payload(attr);
562
563     if (!stats_submitted && ts != NULL) {
564       char type_instance[DATA_MAX_NAME_LEN];
565
566       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
567
568       submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
569       submit_one(dev, "ipt_packets", type_instance, ts->packets);
570     }
571
572     break;
573   }
574
575 #endif /* TCA_STATS */
576
577 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
578   DEBUG("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
579         "TCA_STATS.");
580 #endif
581
582   return MNL_CB_OK;
583 } /* int qos_filter_cb */
584
585 static int ir_config(const char *key, const char *value) {
586   char *new_val;
587   char *fields[8];
588   int fields_num;
589   int status = 1;
590
591   new_val = strdup(value);
592   if (new_val == NULL)
593     return -1;
594
595   fields_num = strsplit(new_val, fields, STATIC_ARRAY_SIZE(fields));
596   if ((fields_num < 1) || (fields_num > 8)) {
597     sfree(new_val);
598     return -1;
599   }
600
601   if ((strcasecmp(key, "Interface") == 0) ||
602       (strcasecmp(key, "VerboseInterface") == 0)) {
603     if (fields_num != 1) {
604       ERROR("netlink plugin: Invalid number of fields for option "
605             "`%s'. Got %i, expected 1.",
606             key, fields_num);
607       status = -1;
608     } else {
609       add_ignorelist(fields[0], "interface", NULL);
610       if (strcasecmp(key, "VerboseInterface") == 0)
611         add_ignorelist(fields[0], "if_detail", NULL);
612       status = 0;
613     }
614   } else if ((strcasecmp(key, "QDisc") == 0) ||
615              (strcasecmp(key, "Class") == 0) ||
616              (strcasecmp(key, "Filter") == 0)) {
617     if ((fields_num < 1) || (fields_num > 2)) {
618       ERROR("netlink plugin: Invalid number of fields for option "
619             "`%s'. Got %i, expected 1 or 2.",
620             key, fields_num);
621       return -1;
622     } else {
623       add_ignorelist(fields[0], key, (fields_num == 2) ? fields[1] : NULL);
624       status = 0;
625     }
626   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
627     if (fields_num != 1) {
628       ERROR("netlink plugin: Invalid number of fields for option "
629             "`IgnoreSelected'. Got %i, expected 1.",
630             fields_num);
631       status = -1;
632     } else {
633       if (IS_TRUE(fields[0]))
634         ir_ignorelist_invert = 0;
635       else
636         ir_ignorelist_invert = 1;
637       status = 0;
638     }
639   }
640
641   sfree(new_val);
642
643   return status;
644 } /* int ir_config */
645
646 static int ir_init(void) {
647   nl = mnl_socket_open(NETLINK_ROUTE);
648   if (nl == NULL) {
649     ERROR("netlink plugin: ir_init: mnl_socket_open failed.");
650     return -1;
651   }
652
653   if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
654     ERROR("netlink plugin: ir_init: mnl_socket_bind failed.");
655     return -1;
656   }
657
658   return 0;
659 } /* int ir_init */
660
661 static int ir_read(void) {
662   char buf[MNL_SOCKET_BUFFER_SIZE];
663   struct nlmsghdr *nlh;
664   struct rtgenmsg *rt;
665   int ret;
666   unsigned int seq, portid;
667
668   static const int type_id[] = {RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER};
669   static const char *type_name[] = {"qdisc", "class", "filter"};
670
671   portid = mnl_socket_get_portid(nl);
672
673   nlh = mnl_nlmsg_put_header(buf);
674   nlh->nlmsg_type = RTM_GETLINK;
675   nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
676   nlh->nlmsg_seq = seq = time(NULL);
677   rt = mnl_nlmsg_put_extra_header(nlh, sizeof(*rt));
678   rt->rtgen_family = AF_PACKET;
679
680   if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
681     ERROR("netlink plugin: ir_read: rtnl_wilddump_request failed.");
682     return -1;
683   }
684
685   ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
686   while (ret > 0) {
687     ret = mnl_cb_run(buf, ret, seq, portid, link_filter_cb, NULL);
688     if (ret <= MNL_CB_STOP)
689       break;
690     ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
691   }
692   if (ret < 0) {
693     ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s", STRERRNO);
694     return (-1);
695   }
696
697   /* `link_filter_cb' will update `iflist' which is used here to iterate
698    * over all interfaces. */
699   for (size_t ifindex = 1; ifindex < iflist_len; ifindex++) {
700     struct tcmsg *tm;
701
702     if (iflist[ifindex] == NULL)
703       continue;
704
705     for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE(type_id);
706          type_index++) {
707       if (check_ignorelist(iflist[ifindex], type_name[type_index], NULL)) {
708         DEBUG("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
709               "== TRUE",
710               iflist[ifindex], type_name[type_index]);
711         continue;
712       }
713
714       DEBUG("netlink plugin: ir_read: querying %s from %s (%" PRIsz ").",
715             type_name[type_index], iflist[ifindex], ifindex);
716
717       nlh = mnl_nlmsg_put_header(buf);
718       nlh->nlmsg_type = type_id[type_index];
719       nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
720       nlh->nlmsg_seq = seq = time(NULL);
721       tm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tm));
722       tm->tcm_family = AF_PACKET;
723       tm->tcm_ifindex = ifindex;
724
725       if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
726         ERROR("netlink plugin: ir_read: mnl_socket_sendto failed.");
727         continue;
728       }
729
730       ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
731       while (ret > 0) {
732         ret = mnl_cb_run(buf, ret, seq, portid, qos_filter_cb, &ifindex);
733         if (ret <= MNL_CB_STOP)
734           break;
735         ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
736       }
737       if (ret < 0) {
738         ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s",
739               STRERRNO);
740         continue;
741       }
742     } /* for (type_index) */
743   }   /* for (if_index) */
744
745   return 0;
746 } /* int ir_read */
747
748 static int ir_shutdown(void) {
749   if (nl) {
750     mnl_socket_close(nl);
751     nl = NULL;
752   }
753
754   return 0;
755 } /* int ir_shutdown */
756
757 void module_register(void) {
758   plugin_register_config("netlink", ir_config, config_keys, config_keys_num);
759   plugin_register_init("netlink", ir_init);
760   plugin_register_read("netlink", ir_read);
761   plugin_register_shutdown("netlink", ir_shutdown);
762 } /* void module_register */