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
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.
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.
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
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>
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>
40 #if HAVE_LINUX_PKT_SCHED_H
41 # include <linux/pkt_sched.h>
44 #include <libmnl/libmnl.h>
46 struct ir_link_stats_storage_s {
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;
74 union ir_link_stats_u {
75 struct rtnl_link_stats *stats32;
76 #ifdef HAVE_RTNL_LINK_STATS64
77 struct rtnl_link_stats64 *stats64;
81 typedef struct ir_ignorelist_s
86 struct ir_ignorelist_s *next;
89 static int ir_ignorelist_invert = 1;
90 static ir_ignorelist_t *ir_ignorelist_head = NULL;
92 static struct mnl_socket *nl;
94 static char **iflist = NULL;
95 static size_t iflist_len = 0;
97 static const char *config_keys[] =
106 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
108 static int add_ignorelist (const char *dev, const char *type,
111 ir_ignorelist_t *entry;
113 entry = calloc (1, sizeof (*entry));
117 if (strcasecmp (dev, "All") != 0)
119 entry->device = strdup (dev);
120 if (entry->device == NULL)
127 entry->type = strdup (type);
128 if (entry->type == NULL)
130 sfree (entry->device);
137 entry->inst = strdup (inst);
138 if (entry->inst == NULL)
141 sfree (entry->device);
147 entry->next = ir_ignorelist_head;
148 ir_ignorelist_head = entry;
151 } /* int add_ignorelist */
154 * Checks wether a data set should be ignored. Returns `true' is the value
155 * should be ignored, `false' otherwise.
157 static int check_ignorelist (const char *dev,
158 const char *type, const char *type_instance)
160 assert ((dev != NULL) && (type != NULL));
162 if (ir_ignorelist_head == NULL)
163 return (ir_ignorelist_invert ? 0 : 1);
165 for (ir_ignorelist_t *i = ir_ignorelist_head; i != NULL; i = i->next)
167 /* i->device == NULL => match all devices */
168 if ((i->device != NULL)
169 && (strcasecmp (i->device, dev) != 0))
172 if (strcasecmp (i->type, type) != 0)
175 if ((i->inst != NULL) && (type_instance != NULL)
176 && (strcasecmp (i->inst, type_instance) != 0))
179 DEBUG ("netlink plugin: check_ignorelist: "
180 "(dev = %s; type = %s; inst = %s) matched "
181 "(dev = %s; type = %s; inst = %s)",
183 type_instance == NULL ? "(nil)" : type_instance,
184 i->device == NULL ? "(nil)" : i->device,
186 i->inst == NULL ? "(nil)" : i->inst);
188 return (ir_ignorelist_invert ? 0 : 1);
191 return (ir_ignorelist_invert);
192 } /* int check_ignorelist */
194 static void submit_one (const char *dev, const char *type,
195 const char *type_instance, derive_t value)
197 value_list_t vl = VALUE_LIST_INIT;
199 vl.values = &(value_t) { .derive = value };
201 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
202 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
203 sstrncpy (vl.type, type, sizeof (vl.type));
205 if (type_instance != NULL)
206 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
208 plugin_dispatch_values (&vl);
209 } /* void submit_one */
211 static void submit_two (const char *dev, const char *type,
212 const char *type_instance,
213 derive_t rx, derive_t tx)
215 value_list_t vl = VALUE_LIST_INIT;
222 vl.values_len = STATIC_ARRAY_SIZE (values);
223 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
224 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
225 sstrncpy (vl.type, type, sizeof (vl.type));
227 if (type_instance != NULL)
228 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
230 plugin_dispatch_values (&vl);
231 } /* void submit_two */
233 static int update_iflist (struct ifinfomsg *msg, const char *dev)
235 /* Update the `iflist'. It's used to know which interfaces exist and query
236 * them later for qdiscs and classes. */
237 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
241 temp = realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
244 ERROR ("netlink plugin: update_iflist: realloc failed.");
248 memset (temp + iflist_len, '\0',
249 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
251 iflist_len = msg->ifi_index + 1;
253 if ((iflist[msg->ifi_index] == NULL)
254 || (strcmp (iflist[msg->ifi_index], dev) != 0))
256 sfree (iflist[msg->ifi_index]);
257 iflist[msg->ifi_index] = strdup (dev);
261 } /* int update_iflist */
263 static void check_ignorelist_and_submit (const char *dev,
264 struct ir_link_stats_storage_s *stats)
267 if (check_ignorelist (dev, "interface", NULL) == 0)
269 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
270 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
271 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
275 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
278 if (check_ignorelist (dev, "if_detail", NULL) == 0)
280 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
281 submit_one (dev, "if_multicast", NULL, stats->multicast);
282 submit_one (dev, "if_collisions", NULL, stats->collisions);
284 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
285 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
286 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
287 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
288 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
289 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
291 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
292 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
293 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
294 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
295 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
299 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
302 } /* void check_ignorelist_and_submit */
304 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
305 (dst_stats)->value_name = (src_stats)->value_name
307 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats) \
308 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_packets); \
309 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_packets); \
310 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_bytes); \
311 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_bytes); \
312 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_errors); \
313 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_errors); \
314 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_dropped); \
315 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_dropped); \
316 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, multicast); \
317 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, collisions); \
318 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_length_errors); \
319 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_over_errors); \
320 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_crc_errors); \
321 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_frame_errors); \
322 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_fifo_errors); \
323 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_missed_errors); \
324 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_aborted_errors); \
325 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_carrier_errors); \
326 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_fifo_errors); \
327 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_heartbeat_errors); \
328 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_window_errors)
330 #ifdef HAVE_RTNL_LINK_STATS64
331 static void check_ignorelist_and_submit64 (const char *dev,
332 struct rtnl_link_stats64 *stats)
334 struct ir_link_stats_storage_s s;
336 COPY_RTNL_LINK_STATS (&s, stats);
338 check_ignorelist_and_submit (dev, &s);
342 static void check_ignorelist_and_submit32 (const char *dev,
343 struct rtnl_link_stats *stats)
345 struct ir_link_stats_storage_s s;
347 COPY_RTNL_LINK_STATS(&s, stats);
349 check_ignorelist_and_submit (dev, &s);
352 static int link_filter_cb (const struct nlmsghdr *nlh,
353 void *args __attribute__((unused)))
355 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
357 const char *dev = NULL;
358 union ir_link_stats_u stats;
360 if (nlh->nlmsg_type != RTM_NEWLINK)
362 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
367 /* Scan attribute list for device name. */
368 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
370 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
373 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
375 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
379 dev = mnl_attr_get_str (attr);
380 if (update_iflist (ifm, dev) < 0)
387 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
390 #ifdef HAVE_RTNL_LINK_STATS64
391 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
393 if (mnl_attr_get_type (attr) != IFLA_STATS64)
396 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats64)) < 0)
398 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 failed.");
401 stats.stats64 = mnl_attr_get_payload (attr);
403 check_ignorelist_and_submit64 (dev, stats.stats64);
408 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
410 if (mnl_attr_get_type (attr) != IFLA_STATS)
413 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats32)) < 0)
415 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
418 stats.stats32 = mnl_attr_get_payload (attr);
420 check_ignorelist_and_submit32 (dev, stats.stats32);
425 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
428 } /* int link_filter_cb */
431 static int qos_attr_cb (const struct nlattr *attr, void *data)
433 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
435 /* skip unsupported attribute in user-space */
436 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
439 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
441 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
443 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
446 *bs = mnl_attr_get_payload (attr);
454 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
456 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
459 int wanted_ifindex = *((int *) args);
462 const char *kind = NULL;
464 /* char *type_instance; */
466 char tc_inst[DATA_MAX_NAME_LEN];
468 _Bool stats_submitted = 0;
470 if (nlh->nlmsg_type == RTM_NEWQDISC)
472 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
474 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
478 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
483 if (tm->tcm_ifindex != wanted_ifindex)
485 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
487 tc_type, tm->tcm_ifindex, wanted_ifindex);
491 if ((tm->tcm_ifindex >= 0)
492 && ((size_t) tm->tcm_ifindex >= iflist_len))
494 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
495 ">= iflist_len = %zu",
496 tm->tcm_ifindex, iflist_len);
500 dev = iflist[tm->tcm_ifindex];
503 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
508 mnl_attr_for_each (attr, nlh, sizeof (*tm))
510 if (mnl_attr_get_type (attr) != TCA_KIND)
513 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
515 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
519 kind = mnl_attr_get_str (attr);
525 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
530 uint32_t numberic_id;
532 numberic_id = tm->tcm_handle;
533 if (strcmp (tc_type, "filter") == 0)
534 numberic_id = tm->tcm_parent;
536 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
539 numberic_id & 0x0000FFFF);
542 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
543 tc_type, dev, tm->tcm_ifindex);
545 if (check_ignorelist (dev, tc_type, tc_inst))
549 mnl_attr_for_each (attr, nlh, sizeof (*tm))
551 struct gnet_stats_basic *bs = NULL;
553 if (mnl_attr_get_type (attr) != TCA_STATS2)
556 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
558 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
562 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
566 char type_instance[DATA_MAX_NAME_LEN];
570 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
573 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
574 submit_one (dev, "ipt_packets", type_instance, bs->packets);
579 #endif /* TCA_STATS2 */
582 mnl_attr_for_each (attr, nlh, sizeof (*tm))
584 struct tc_stats *ts = NULL;
586 if (mnl_attr_get_type (attr) != TCA_STATS)
589 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
591 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
594 ts = mnl_attr_get_payload (attr);
596 if (!stats_submitted && ts != NULL)
598 char type_instance[DATA_MAX_NAME_LEN];
600 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
603 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
604 submit_one (dev, "ipt_packets", type_instance, ts->packets);
610 #endif /* TCA_STATS */
612 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
613 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
618 } /* int qos_filter_cb */
620 static int ir_config (const char *key, const char *value)
627 new_val = strdup (value);
631 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
632 if ((fields_num < 1) || (fields_num > 8))
638 if ((strcasecmp (key, "Interface") == 0)
639 || (strcasecmp (key, "VerboseInterface") == 0))
643 ERROR ("netlink plugin: Invalid number of fields for option "
644 "`%s'. Got %i, expected 1.", key, fields_num);
649 add_ignorelist (fields[0], "interface", NULL);
650 if (strcasecmp (key, "VerboseInterface") == 0)
651 add_ignorelist (fields[0], "if_detail", NULL);
655 else if ((strcasecmp (key, "QDisc") == 0)
656 || (strcasecmp (key, "Class") == 0)
657 || (strcasecmp (key, "Filter") == 0))
659 if ((fields_num < 1) || (fields_num > 2))
661 ERROR ("netlink plugin: Invalid number of fields for option "
662 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
667 add_ignorelist (fields[0], key,
668 (fields_num == 2) ? fields[1] : NULL);
672 else if (strcasecmp (key, "IgnoreSelected") == 0)
676 ERROR ("netlink plugin: Invalid number of fields for option "
677 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
682 if (IS_TRUE (fields[0]))
683 ir_ignorelist_invert = 0;
685 ir_ignorelist_invert = 1;
693 } /* int ir_config */
695 static int ir_init (void)
697 nl = mnl_socket_open (NETLINK_ROUTE);
700 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
704 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
706 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
713 static int ir_read (void)
715 char buf[MNL_SOCKET_BUFFER_SIZE];
716 struct nlmsghdr *nlh;
719 unsigned int seq, portid;
721 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
722 static const char *type_name[] = { "qdisc", "class", "filter" };
724 portid = mnl_socket_get_portid (nl);
726 nlh = mnl_nlmsg_put_header (buf);
727 nlh->nlmsg_type = RTM_GETLINK;
728 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
729 nlh->nlmsg_seq = seq = time (NULL);
730 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
731 rt->rtgen_family = AF_PACKET;
733 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
735 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
739 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
742 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
743 if (ret <= MNL_CB_STOP)
745 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
749 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
753 /* `link_filter_cb' will update `iflist' which is used here to iterate
754 * over all interfaces. */
755 for (size_t ifindex = 1; ifindex < iflist_len; ifindex++)
759 if (iflist[ifindex] == NULL)
762 for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
764 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
766 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
767 "== TRUE", iflist[ifindex], type_name[type_index]);
771 DEBUG ("netlink plugin: ir_read: querying %s from %s (%zu).",
772 type_name[type_index], iflist[ifindex], ifindex);
774 nlh = mnl_nlmsg_put_header (buf);
775 nlh->nlmsg_type = type_id[type_index];
776 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
777 nlh->nlmsg_seq = seq = time (NULL);
778 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
779 tm->tcm_family = AF_PACKET;
780 tm->tcm_ifindex = ifindex;
782 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
784 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
788 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
791 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
792 if (ret <= MNL_CB_STOP)
794 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
798 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
802 } /* for (type_index) */
803 } /* for (if_index) */
808 static int ir_shutdown (void)
812 mnl_socket_close (nl);
817 } /* int ir_shutdown */
819 void module_register (void)
821 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
822 plugin_register_init ("netlink", ir_init);
823 plugin_register_read ("netlink", ir_read);
824 plugin_register_shutdown ("netlink", ir_shutdown);
825 } /* void module_register */
828 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :