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"
30 #include "common.h"
31 #include "plugin.h"
33 #include <asm/types.h>
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
44 #include <libmnl/libmnl.h>
46 struct ir_link_stats_storage_s {
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;
55 uint64_t rx_dropped;
56 uint64_t tx_dropped;
57 uint64_t multicast;
58 uint64_t collisions;
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;
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 };
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 };
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;
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[] = {"Interface", "VerboseInterface",
97 "QDisc", "Class",
98 "Filter", "IgnoreSelected"};
99 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
101 static int add_ignorelist(const char *dev, const char *type, const char *inst) {
102 ir_ignorelist_t *entry;
104 entry = calloc(1, sizeof(*entry));
105 if (entry == NULL)
106 return (-1);
108 if (strcasecmp(dev, "All") != 0) {
109 entry->device = strdup(dev);
110 if (entry->device == NULL) {
111 sfree(entry);
112 return (-1);
113 }
114 }
116 entry->type = strdup(type);
117 if (entry->type == NULL) {
118 sfree(entry->device);
119 sfree(entry);
120 return (-1);
121 }
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 }
133 entry->next = ir_ignorelist_head;
134 ir_ignorelist_head = entry;
136 return (0);
137 } /* int add_ignorelist */
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));
147 if (ir_ignorelist_head == NULL)
148 return (ir_ignorelist_invert ? 0 : 1);
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;
155 if (strcasecmp(i->type, type) != 0)
156 continue;
158 if ((i->inst != NULL) && (type_instance != NULL) &&
159 (strcasecmp(i->inst, type_instance) != 0))
160 continue;
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);
169 return (ir_ignorelist_invert ? 0 : 1);
170 } /* for i */
172 return (ir_ignorelist_invert);
173 } /* int check_ignorelist */
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;
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));
185 if (type_instance != NULL)
186 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
188 plugin_dispatch_values(&vl);
189 } /* void submit_one */
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 };
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));
204 if (type_instance != NULL)
205 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
207 plugin_dispatch_values(&vl);
208 } /* void submit_two */
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;
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 }
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 }
233 return (0);
234 } /* int update_iflist */
236 static void check_ignorelist_and_submit(const char *dev,
237 struct ir_link_stats_storage_s *stats) {
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 }
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);
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);
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 }
268 } /* void check_ignorelist_and_submit */
270 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
271 (dst_stats)->value_name = (src_stats)->value_name
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)
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;
301 COPY_RTNL_LINK_STATS(&s, stats);
303 check_ignorelist_and_submit(dev, &s);
304 }
305 #endif
307 static void check_ignorelist_and_submit32(const char *dev,
308 struct rtnl_link_stats *stats) {
309 struct ir_link_stats_storage_s s;
311 COPY_RTNL_LINK_STATS(&s, stats);
313 check_ignorelist_and_submit(dev, &s);
314 }
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;
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 }
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;
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 }
340 dev = mnl_attr_get_str(attr);
341 if (update_iflist(ifm, dev) < 0)
342 return MNL_CB_ERROR;
343 break;
344 }
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;
355 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats64)) < 0) {
356 char errbuf[1024];
357 ERROR("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 "
358 "failed: %s",
359 sstrerror(errno, errbuf, sizeof(errbuf)));
360 return MNL_CB_ERROR;
361 }
362 stats.stats64 = mnl_attr_get_payload(attr);
364 check_ignorelist_and_submit64(dev, stats.stats64);
366 return MNL_CB_OK;
367 }
368 #endif
369 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
370 if (mnl_attr_get_type(attr) != IFLA_STATS)
371 continue;
373 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats32)) < 0) {
374 char errbuf[1024];
375 ERROR("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 "
376 "failed: %s",
377 sstrerror(errno, errbuf, sizeof(errbuf)));
378 return MNL_CB_ERROR;
379 }
380 stats.stats32 = mnl_attr_get_payload(attr);
382 check_ignorelist_and_submit32(dev, stats.stats32);
384 return MNL_CB_OK;
385 }
387 DEBUG("netlink plugin: link_filter: No statistics for interface %s.", dev);
388 return MNL_CB_OK;
390 } /* int link_filter_cb */
392 #if HAVE_TCA_STATS2
393 static int qos_attr_cb(const struct nlattr *attr, void *data) {
394 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
396 /* skip unsupported attribute in user-space */
397 if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
398 return MNL_CB_OK;
400 if (mnl_attr_get_type(attr) == TCA_STATS_BASIC) {
401 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(**bs)) < 0) {
402 char errbuf[1024];
403 ERROR("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 "
404 "failed: %s",
405 sstrerror(errno, errbuf, sizeof(errbuf)));
406 return MNL_CB_ERROR;
407 }
408 *bs = mnl_attr_get_payload(attr);
409 return MNL_CB_STOP;
410 }
412 return MNL_CB_OK;
413 } /* qos_attr_cb */
414 #endif
416 static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
417 struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
418 struct nlattr *attr;
420 int wanted_ifindex = *((int *)args);
422 const char *dev;
423 const char *kind = NULL;
425 /* char *type_instance; */
426 const char *tc_type;
427 char tc_inst[DATA_MAX_NAME_LEN];
429 _Bool stats_submitted = 0;
431 if (nlh->nlmsg_type == RTM_NEWQDISC)
432 tc_type = "qdisc";
433 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
434 tc_type = "class";
435 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
436 tc_type = "filter";
437 else {
438 ERROR("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
439 nlh->nlmsg_type);
440 return MNL_CB_ERROR;
441 }
443 if (tm->tcm_ifindex != wanted_ifindex) {
444 DEBUG("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
445 "but expected #%i.",
446 tc_type, tm->tcm_ifindex, wanted_ifindex);
447 return MNL_CB_OK;
448 }
450 if ((tm->tcm_ifindex >= 0) && ((size_t)tm->tcm_ifindex >= iflist_len)) {
451 ERROR("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
452 ">= iflist_len = %zu",
453 tm->tcm_ifindex, iflist_len);
454 return MNL_CB_ERROR;
455 }
457 dev = iflist[tm->tcm_ifindex];
458 if (dev == NULL) {
459 ERROR("netlink plugin: qos_filter_cb: iflist[%i] == NULL", tm->tcm_ifindex);
460 return MNL_CB_ERROR;
461 }
463 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
464 if (mnl_attr_get_type(attr) != TCA_KIND)
465 continue;
467 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
468 ERROR(
469 "netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
470 return MNL_CB_ERROR;
471 }
473 kind = mnl_attr_get_str(attr);
474 break;
475 }
477 if (kind == NULL) {
478 ERROR("netlink plugin: qos_filter_cb: kind == NULL");
479 return (-1);
480 }
482 { /* The ID */
483 uint32_t numberic_id;
485 numberic_id = tm->tcm_handle;
486 if (strcmp(tc_type, "filter") == 0)
487 numberic_id = tm->tcm_parent;
489 ssnprintf(tc_inst, sizeof(tc_inst), "%s-%x:%x", kind, numberic_id >> 16,
490 numberic_id & 0x0000FFFF);
491 }
493 DEBUG("netlink plugin: qos_filter_cb: got %s for %s (%i).", tc_type, dev,
494 tm->tcm_ifindex);
496 if (check_ignorelist(dev, tc_type, tc_inst))
497 return MNL_CB_OK;
499 #if HAVE_TCA_STATS2
500 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
501 struct gnet_stats_basic *bs = NULL;
503 if (mnl_attr_get_type(attr) != TCA_STATS2)
504 continue;
506 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
507 ERROR("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate "
508 "failed.");
509 return MNL_CB_ERROR;
510 }
512 mnl_attr_parse_nested(attr, qos_attr_cb, &bs);
514 if (bs != NULL) {
515 char type_instance[DATA_MAX_NAME_LEN];
517 stats_submitted = 1;
519 ssnprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
520 tc_inst);
522 submit_one(dev, "ipt_bytes", type_instance, bs->bytes);
523 submit_one(dev, "ipt_packets", type_instance, bs->packets);
524 }
526 break;
527 }
528 #endif /* TCA_STATS2 */
530 #if HAVE_TCA_STATS
531 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
532 struct tc_stats *ts = NULL;
534 if (mnl_attr_get_type(attr) != TCA_STATS)
535 continue;
537 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*ts)) < 0) {
538 char errbuf[1024];
539 ERROR("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 "
540 "failed: %s",
541 sstrerror(errno, errbuf, sizeof(errbuf)));
542 return MNL_CB_ERROR;
543 }
544 ts = mnl_attr_get_payload(attr);
546 if (!stats_submitted && ts != NULL) {
547 char type_instance[DATA_MAX_NAME_LEN];
549 ssnprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
550 tc_inst);
552 submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
553 submit_one(dev, "ipt_packets", type_instance, ts->packets);
554 }
556 break;
557 }
559 #endif /* TCA_STATS */
561 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
562 DEBUG("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
563 "TCA_STATS.");
564 #endif
566 return MNL_CB_OK;
567 } /* int qos_filter_cb */
569 static int ir_config(const char *key, const char *value) {
570 char *new_val;
571 char *fields[8];
572 int fields_num;
573 int status = 1;
575 new_val = strdup(value);
576 if (new_val == NULL)
577 return (-1);
579 fields_num = strsplit(new_val, fields, STATIC_ARRAY_SIZE(fields));
580 if ((fields_num < 1) || (fields_num > 8)) {
581 sfree(new_val);
582 return (-1);
583 }
585 if ((strcasecmp(key, "Interface") == 0) ||
586 (strcasecmp(key, "VerboseInterface") == 0)) {
587 if (fields_num != 1) {
588 ERROR("netlink plugin: Invalid number of fields for option "
589 "`%s'. Got %i, expected 1.",
590 key, fields_num);
591 status = -1;
592 } else {
593 add_ignorelist(fields[0], "interface", NULL);
594 if (strcasecmp(key, "VerboseInterface") == 0)
595 add_ignorelist(fields[0], "if_detail", NULL);
596 status = 0;
597 }
598 } else if ((strcasecmp(key, "QDisc") == 0) ||
599 (strcasecmp(key, "Class") == 0) ||
600 (strcasecmp(key, "Filter") == 0)) {
601 if ((fields_num < 1) || (fields_num > 2)) {
602 ERROR("netlink plugin: Invalid number of fields for option "
603 "`%s'. Got %i, expected 1 or 2.",
604 key, fields_num);
605 return (-1);
606 } else {
607 add_ignorelist(fields[0], key, (fields_num == 2) ? fields[1] : NULL);
608 status = 0;
609 }
610 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
611 if (fields_num != 1) {
612 ERROR("netlink plugin: Invalid number of fields for option "
613 "`IgnoreSelected'. Got %i, expected 1.",
614 fields_num);
615 status = -1;
616 } else {
617 if (IS_TRUE(fields[0]))
618 ir_ignorelist_invert = 0;
619 else
620 ir_ignorelist_invert = 1;
621 status = 0;
622 }
623 }
625 sfree(new_val);
627 return (status);
628 } /* int ir_config */
630 static int ir_init(void) {
631 nl = mnl_socket_open(NETLINK_ROUTE);
632 if (nl == NULL) {
633 ERROR("netlink plugin: ir_init: mnl_socket_open failed.");
634 return (-1);
635 }
637 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
638 ERROR("netlink plugin: ir_init: mnl_socket_bind failed.");
639 return (-1);
640 }
642 return (0);
643 } /* int ir_init */
645 static int ir_read(void) {
646 char buf[MNL_SOCKET_BUFFER_SIZE];
647 struct nlmsghdr *nlh;
648 struct rtgenmsg *rt;
649 int ret;
650 unsigned int seq, portid;
652 static const int type_id[] = {RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER};
653 static const char *type_name[] = {"qdisc", "class", "filter"};
655 portid = mnl_socket_get_portid(nl);
657 nlh = mnl_nlmsg_put_header(buf);
658 nlh->nlmsg_type = RTM_GETLINK;
659 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
660 nlh->nlmsg_seq = seq = time(NULL);
661 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(*rt));
662 rt->rtgen_family = AF_PACKET;
664 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
665 ERROR("netlink plugin: ir_read: rtnl_wilddump_request failed.");
666 return (-1);
667 }
669 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
670 while (ret > 0) {
671 ret = mnl_cb_run(buf, ret, seq, portid, link_filter_cb, NULL);
672 if (ret <= MNL_CB_STOP)
673 break;
674 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
675 }
676 if (ret < 0) {
677 char errbuf[1024];
678 ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s",
679 sstrerror(errno, errbuf, sizeof(errbuf)));
680 return (-1);
681 }
683 /* `link_filter_cb' will update `iflist' which is used here to iterate
684 * over all interfaces. */
685 for (size_t ifindex = 1; ifindex < iflist_len; ifindex++) {
686 struct tcmsg *tm;
688 if (iflist[ifindex] == NULL)
689 continue;
691 for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE(type_id);
692 type_index++) {
693 if (check_ignorelist(iflist[ifindex], type_name[type_index], NULL)) {
694 DEBUG("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
695 "== TRUE",
696 iflist[ifindex], type_name[type_index]);
697 continue;
698 }
700 DEBUG("netlink plugin: ir_read: querying %s from %s (%zu).",
701 type_name[type_index], iflist[ifindex], ifindex);
703 nlh = mnl_nlmsg_put_header(buf);
704 nlh->nlmsg_type = type_id[type_index];
705 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
706 nlh->nlmsg_seq = seq = time(NULL);
707 tm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tm));
708 tm->tcm_family = AF_PACKET;
709 tm->tcm_ifindex = ifindex;
711 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
712 ERROR("netlink plugin: ir_read: mnl_socket_sendto failed.");
713 continue;
714 }
716 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
717 while (ret > 0) {
718 ret = mnl_cb_run(buf, ret, seq, portid, qos_filter_cb, &ifindex);
719 if (ret <= MNL_CB_STOP)
720 break;
721 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
722 }
723 if (ret < 0) {
724 char errbuf[1024];
725 ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s",
726 sstrerror(errno, errbuf, sizeof(errbuf)));
727 continue;
728 }
729 } /* for (type_index) */
730 } /* for (if_index) */
732 return (0);
733 } /* int ir_read */
735 static int ir_shutdown(void) {
736 if (nl) {
737 mnl_socket_close(nl);
738 nl = NULL;
739 }
741 return (0);
742 } /* int ir_shutdown */
744 void module_register(void) {
745 plugin_register_config("netlink", ir_config, config_keys, config_keys_num);
746 plugin_register_init("netlink", ir_init);
747 plugin_register_read("netlink", ir_read);
748 plugin_register_shutdown("netlink", ir_shutdown);
749 } /* void module_register */
751 /*
752 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
753 */