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