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