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