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 **/
28 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
32 #include <asm/types.h>
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
43 #include <libmnl/libmnl.h>
45 struct ir_link_stats_storage_s {
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;
54 uint64_t rx_dropped;
55 uint64_t tx_dropped;
56 uint64_t multicast;
57 uint64_t collisions;
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;
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 };
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 };
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;
88 static int ir_ignorelist_invert = 1;
89 static ir_ignorelist_t *ir_ignorelist_head = NULL;
91 static struct mnl_socket *nl;
93 static char **iflist = NULL;
94 static size_t iflist_len = 0;
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);
107 static int add_ignorelist (const char *dev, const char *type,
108 const char *inst)
109 {
110 ir_ignorelist_t *entry;
112 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
113 if (entry == NULL)
114 return (-1);
116 memset (entry, '\0', sizeof (ir_ignorelist_t));
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 }
128 entry->type = strdup (type);
129 if (entry->type == NULL)
130 {
131 sfree (entry->device);
132 sfree (entry);
133 return (-1);
134 }
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 }
148 entry->next = ir_ignorelist_head;
149 ir_ignorelist_head = entry;
151 return (0);
152 } /* int add_ignorelist */
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;
163 assert ((dev != NULL) && (type != NULL));
165 if (ir_ignorelist_head == NULL)
166 return (ir_ignorelist_invert ? 0 : 1);
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;
175 if (strcasecmp (i->type, type) != 0)
176 continue;
178 if ((i->inst != NULL) && (type_instance != NULL)
179 && (strcasecmp (i->inst, type_instance) != 0))
180 continue;
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);
191 return (ir_ignorelist_invert ? 0 : 1);
192 } /* for i */
194 return (ir_ignorelist_invert);
195 } /* int check_ignorelist */
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;
203 values[0].derive = value;
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));
212 if (type_instance != NULL)
213 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
215 plugin_dispatch_values (&vl);
216 } /* void submit_one */
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;
225 values[0].derive = rx;
226 values[1].derive = tx;
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));
235 if (type_instance != NULL)
236 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
238 plugin_dispatch_values (&vl);
239 } /* void submit_two */
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;
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 }
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 }
268 return (0);
269 } /* int update_iflist */
271 static void check_ignorelist_and_submit (const char *dev,
272 struct ir_link_stats_storage_s *stats)
273 {
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 }
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);
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);
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 }
310 } /* void check_ignorelist_and_submit */
312 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
313 (dst_stats)->value_name = (src_stats)->value_name
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)
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;
344 COPY_RTNL_LINK_STATS (&s, stats);
346 check_ignorelist_and_submit (dev, &s);
347 }
348 #endif
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;
355 COPY_RTNL_LINK_STATS(&s, stats);
357 check_ignorelist_and_submit (dev, &s);
358 }
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;
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 }
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;
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 }
387 dev = mnl_attr_get_str (attr);
388 if (update_iflist (ifm, dev) < 0)
389 return MNL_CB_ERROR;
390 break;
391 }
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;
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);
411 check_ignorelist_and_submit64 (dev, stats.stats64);
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;
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);
428 check_ignorelist_and_submit32 (dev, stats.stats32);
430 return MNL_CB_OK;
431 }
433 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
434 return MNL_CB_OK;
436 } /* int link_filter_cb */
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;
443 /* skip unsupported attribute in user-space */
444 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
445 return MNL_CB_OK;
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 }
458 return MNL_CB_OK;
459 } /* qos_attr_cb */
460 #endif
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;
467 int wanted_ifindex = *((int *) args);
469 const char *dev;
470 const char *kind = NULL;
472 /* char *type_instance; */
473 char *tc_type;
474 char tc_inst[DATA_MAX_NAME_LEN];
476 _Bool stats_submitted = 0;
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 }
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 }
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 }
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 }
516 mnl_attr_for_each (attr, nlh, sizeof (*tm))
517 {
518 if (mnl_attr_get_type (attr) != TCA_KIND)
519 continue;
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 }
527 kind = mnl_attr_get_str (attr);
528 break;
529 }
531 if (kind == NULL)
532 {
533 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
534 return (-1);
535 }
537 { /* The ID */
538 uint32_t numberic_id;
540 numberic_id = tm->tcm_handle;
541 if (strcmp (tc_type, "filter") == 0)
542 numberic_id = tm->tcm_parent;
544 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
545 kind,
546 numberic_id >> 16,
547 numberic_id & 0x0000FFFF);
548 }
550 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
551 tc_type, dev, tm->tcm_ifindex);
553 if (check_ignorelist (dev, tc_type, tc_inst))
554 return MNL_CB_OK;
556 #if HAVE_TCA_STATS2
557 mnl_attr_for_each (attr, nlh, sizeof (*tm))
558 {
559 struct gnet_stats_basic *bs = NULL;
561 if (mnl_attr_get_type (attr) != TCA_STATS2)
562 continue;
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 }
570 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
572 if (bs != NULL)
573 {
574 char type_instance[DATA_MAX_NAME_LEN];
576 stats_submitted = 1;
578 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
579 tc_type, tc_inst);
581 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
582 submit_one (dev, "ipt_packets", type_instance, bs->packets);
583 }
585 break;
586 }
587 #endif /* TCA_STATS2 */
589 #if HAVE_TCA_STATS
590 mnl_attr_for_each (attr, nlh, sizeof (*tm))
591 {
592 struct tc_stats *ts = NULL;
594 if (mnl_attr_get_type (attr) != TCA_STATS)
595 continue;
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);
604 if (!stats_submitted && ts != NULL)
605 {
606 char type_instance[DATA_MAX_NAME_LEN];
608 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
609 tc_type, tc_inst);
611 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
612 submit_one (dev, "ipt_packets", type_instance, ts->packets);
613 }
615 break;
616 }
618 #endif /* TCA_STATS */
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
625 return MNL_CB_OK;
626 } /* int qos_filter_cb */
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;
635 new_val = strdup (value);
636 if (new_val == NULL)
637 return (-1);
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 }
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 }
698 sfree (new_val);
700 return (status);
701 } /* int ir_config */
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 }
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 }
718 return (0);
719 } /* int ir_init */
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;
729 size_t ifindex;
731 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
732 static const char *type_name[] = { "qdisc", "class", "filter" };
734 portid = mnl_socket_get_portid (nl);
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;
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 }
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 }
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;
770 if (iflist[ifindex] == NULL)
771 continue;
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 }
782 DEBUG ("netlink plugin: ir_read: querying %s from %s (%zu).",
783 type_name[type_index], iflist[ifindex], ifindex);
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;
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 }
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 }
813 } /* for (type_index) */
814 } /* for (if_index) */
816 return (0);
817 } /* int ir_read */
819 static int ir_shutdown (void)
820 {
821 if (nl)
822 {
823 mnl_socket_close (nl);
824 nl = NULL;
825 }
827 return (0);
828 } /* int ir_shutdown */
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 */
838 /*
839 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
840 */