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