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.host, hostname_g, sizeof (vl.host));
202 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
203 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
204 sstrncpy (vl.type, type, sizeof (vl.type));
206 if (type_instance != NULL)
207 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
209 plugin_dispatch_values (&vl);
210 } /* void submit_one */
212 static void submit_two (const char *dev, const char *type,
213 const char *type_instance,
214 derive_t rx, derive_t tx)
216 value_list_t vl = VALUE_LIST_INIT;
223 vl.values_len = STATIC_ARRAY_SIZE (values);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
226 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
227 sstrncpy (vl.type, type, sizeof (vl.type));
229 if (type_instance != NULL)
230 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
232 plugin_dispatch_values (&vl);
233 } /* void submit_two */
235 static int update_iflist (struct ifinfomsg *msg, const char *dev)
237 /* Update the `iflist'. It's used to know which interfaces exist and query
238 * them later for qdiscs and classes. */
239 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
243 temp = realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
246 ERROR ("netlink plugin: update_iflist: realloc failed.");
250 memset (temp + iflist_len, '\0',
251 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
253 iflist_len = msg->ifi_index + 1;
255 if ((iflist[msg->ifi_index] == NULL)
256 || (strcmp (iflist[msg->ifi_index], dev) != 0))
258 sfree (iflist[msg->ifi_index]);
259 iflist[msg->ifi_index] = strdup (dev);
263 } /* int update_iflist */
265 static void check_ignorelist_and_submit (const char *dev,
266 struct ir_link_stats_storage_s *stats)
269 if (check_ignorelist (dev, "interface", NULL) == 0)
271 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
272 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
273 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
277 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
280 if (check_ignorelist (dev, "if_detail", NULL) == 0)
282 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
283 submit_one (dev, "if_multicast", NULL, stats->multicast);
284 submit_one (dev, "if_collisions", NULL, stats->collisions);
286 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
287 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
288 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
289 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
290 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
291 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
293 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
294 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
295 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
296 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
297 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
301 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
304 } /* void check_ignorelist_and_submit */
306 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
307 (dst_stats)->value_name = (src_stats)->value_name
309 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats) \
310 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_packets); \
311 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_packets); \
312 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_bytes); \
313 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_bytes); \
314 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_errors); \
315 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_errors); \
316 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_dropped); \
317 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_dropped); \
318 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, multicast); \
319 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, collisions); \
320 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_length_errors); \
321 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_over_errors); \
322 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_crc_errors); \
323 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_frame_errors); \
324 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_fifo_errors); \
325 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_missed_errors); \
326 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_aborted_errors); \
327 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_carrier_errors); \
328 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_fifo_errors); \
329 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_heartbeat_errors); \
330 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_window_errors)
332 #ifdef HAVE_RTNL_LINK_STATS64
333 static void check_ignorelist_and_submit64 (const char *dev,
334 struct rtnl_link_stats64 *stats)
336 struct ir_link_stats_storage_s s;
338 COPY_RTNL_LINK_STATS (&s, stats);
340 check_ignorelist_and_submit (dev, &s);
344 static void check_ignorelist_and_submit32 (const char *dev,
345 struct rtnl_link_stats *stats)
347 struct ir_link_stats_storage_s s;
349 COPY_RTNL_LINK_STATS(&s, stats);
351 check_ignorelist_and_submit (dev, &s);
354 static int link_filter_cb (const struct nlmsghdr *nlh,
355 void *args __attribute__((unused)))
357 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
359 const char *dev = NULL;
360 union ir_link_stats_u stats;
362 if (nlh->nlmsg_type != RTM_NEWLINK)
364 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
369 /* Scan attribute list for device name. */
370 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
372 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
375 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
377 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
381 dev = mnl_attr_get_str (attr);
382 if (update_iflist (ifm, dev) < 0)
389 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
392 #ifdef HAVE_RTNL_LINK_STATS64
393 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
395 if (mnl_attr_get_type (attr) != IFLA_STATS64)
398 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats64)) < 0)
400 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 failed.");
403 stats.stats64 = mnl_attr_get_payload (attr);
405 check_ignorelist_and_submit64 (dev, stats.stats64);
410 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
412 if (mnl_attr_get_type (attr) != IFLA_STATS)
415 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats32)) < 0)
417 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
420 stats.stats32 = mnl_attr_get_payload (attr);
422 check_ignorelist_and_submit32 (dev, stats.stats32);
427 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
430 } /* int link_filter_cb */
433 static int qos_attr_cb (const struct nlattr *attr, void *data)
435 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
437 /* skip unsupported attribute in user-space */
438 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
441 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
443 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
445 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
448 *bs = mnl_attr_get_payload (attr);
456 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
458 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
461 int wanted_ifindex = *((int *) args);
464 const char *kind = NULL;
466 /* char *type_instance; */
468 char tc_inst[DATA_MAX_NAME_LEN];
470 _Bool stats_submitted = 0;
472 if (nlh->nlmsg_type == RTM_NEWQDISC)
474 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
476 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
480 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
485 if (tm->tcm_ifindex != wanted_ifindex)
487 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
489 tc_type, tm->tcm_ifindex, wanted_ifindex);
493 if ((tm->tcm_ifindex >= 0)
494 && ((size_t) tm->tcm_ifindex >= iflist_len))
496 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
497 ">= iflist_len = %zu",
498 tm->tcm_ifindex, iflist_len);
502 dev = iflist[tm->tcm_ifindex];
505 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
510 mnl_attr_for_each (attr, nlh, sizeof (*tm))
512 if (mnl_attr_get_type (attr) != TCA_KIND)
515 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
517 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
521 kind = mnl_attr_get_str (attr);
527 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
532 uint32_t numberic_id;
534 numberic_id = tm->tcm_handle;
535 if (strcmp (tc_type, "filter") == 0)
536 numberic_id = tm->tcm_parent;
538 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
541 numberic_id & 0x0000FFFF);
544 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
545 tc_type, dev, tm->tcm_ifindex);
547 if (check_ignorelist (dev, tc_type, tc_inst))
551 mnl_attr_for_each (attr, nlh, sizeof (*tm))
553 struct gnet_stats_basic *bs = NULL;
555 if (mnl_attr_get_type (attr) != TCA_STATS2)
558 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
560 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
564 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
568 char type_instance[DATA_MAX_NAME_LEN];
572 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
575 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
576 submit_one (dev, "ipt_packets", type_instance, bs->packets);
581 #endif /* TCA_STATS2 */
584 mnl_attr_for_each (attr, nlh, sizeof (*tm))
586 struct tc_stats *ts = NULL;
588 if (mnl_attr_get_type (attr) != TCA_STATS)
591 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
593 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
596 ts = mnl_attr_get_payload (attr);
598 if (!stats_submitted && ts != NULL)
600 char type_instance[DATA_MAX_NAME_LEN];
602 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
605 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
606 submit_one (dev, "ipt_packets", type_instance, ts->packets);
612 #endif /* TCA_STATS */
614 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
615 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
620 } /* int qos_filter_cb */
622 static int ir_config (const char *key, const char *value)
629 new_val = strdup (value);
633 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
634 if ((fields_num < 1) || (fields_num > 8))
640 if ((strcasecmp (key, "Interface") == 0)
641 || (strcasecmp (key, "VerboseInterface") == 0))
645 ERROR ("netlink plugin: Invalid number of fields for option "
646 "`%s'. Got %i, expected 1.", key, fields_num);
651 add_ignorelist (fields[0], "interface", NULL);
652 if (strcasecmp (key, "VerboseInterface") == 0)
653 add_ignorelist (fields[0], "if_detail", NULL);
657 else if ((strcasecmp (key, "QDisc") == 0)
658 || (strcasecmp (key, "Class") == 0)
659 || (strcasecmp (key, "Filter") == 0))
661 if ((fields_num < 1) || (fields_num > 2))
663 ERROR ("netlink plugin: Invalid number of fields for option "
664 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
669 add_ignorelist (fields[0], key,
670 (fields_num == 2) ? fields[1] : NULL);
674 else if (strcasecmp (key, "IgnoreSelected") == 0)
678 ERROR ("netlink plugin: Invalid number of fields for option "
679 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
684 if (IS_TRUE (fields[0]))
685 ir_ignorelist_invert = 0;
687 ir_ignorelist_invert = 1;
695 } /* int ir_config */
697 static int ir_init (void)
699 nl = mnl_socket_open (NETLINK_ROUTE);
702 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
706 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
708 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
715 static int ir_read (void)
717 char buf[MNL_SOCKET_BUFFER_SIZE];
718 struct nlmsghdr *nlh;
721 unsigned int seq, portid;
723 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
724 static const char *type_name[] = { "qdisc", "class", "filter" };
726 portid = mnl_socket_get_portid (nl);
728 nlh = mnl_nlmsg_put_header (buf);
729 nlh->nlmsg_type = RTM_GETLINK;
730 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
731 nlh->nlmsg_seq = seq = time (NULL);
732 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
733 rt->rtgen_family = AF_PACKET;
735 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
737 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
741 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
744 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
745 if (ret <= MNL_CB_STOP)
747 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
751 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
755 /* `link_filter_cb' will update `iflist' which is used here to iterate
756 * over all interfaces. */
757 for (size_t ifindex = 1; ifindex < iflist_len; ifindex++)
761 if (iflist[ifindex] == NULL)
764 for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
766 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
768 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
769 "== TRUE", iflist[ifindex], type_name[type_index]);
773 DEBUG ("netlink plugin: ir_read: querying %s from %s (%zu).",
774 type_name[type_index], iflist[ifindex], ifindex);
776 nlh = mnl_nlmsg_put_header (buf);
777 nlh->nlmsg_type = type_id[type_index];
778 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
779 nlh->nlmsg_seq = seq = time (NULL);
780 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
781 tm->tcm_family = AF_PACKET;
782 tm->tcm_ifindex = ifindex;
784 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
786 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
790 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
793 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
794 if (ret <= MNL_CB_STOP)
796 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
800 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
804 } /* for (type_index) */
805 } /* for (if_index) */
810 static int ir_shutdown (void)
814 mnl_socket_close (nl);
819 } /* int ir_shutdown */
821 void module_register (void)
823 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
824 plugin_register_init ("netlink", ir_init);
825 plugin_register_read ("netlink", ir_read);
826 plugin_register_shutdown ("netlink", ir_shutdown);
827 } /* void module_register */
830 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :