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