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>
32 #include <asm/types.h>
33 #include <sys/socket.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 = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
117 memset (entry, '\0', sizeof (ir_ignorelist_t));
119 if (strcasecmp (dev, "All") != 0)
121 entry->device = strdup (dev);
122 if (entry->device == NULL)
129 entry->type = strdup (type);
130 if (entry->type == NULL)
132 sfree (entry->device);
139 entry->inst = strdup (inst);
140 if (entry->inst == NULL)
143 sfree (entry->device);
149 entry->next = ir_ignorelist_head;
150 ir_ignorelist_head = entry;
153 } /* int add_ignorelist */
156 * Checks wether a data set should be ignored. Returns `true' is the value
157 * should be ignored, `false' otherwise.
159 static int check_ignorelist (const char *dev,
160 const char *type, const char *type_instance)
164 assert ((dev != NULL) && (type != NULL));
166 if (ir_ignorelist_head == NULL)
167 return (ir_ignorelist_invert ? 0 : 1);
169 for (i = ir_ignorelist_head; i != NULL; i = i->next)
171 /* i->device == NULL => match all devices */
172 if ((i->device != NULL)
173 && (strcasecmp (i->device, dev) != 0))
176 if (strcasecmp (i->type, type) != 0)
179 if ((i->inst != NULL) && (type_instance != NULL)
180 && (strcasecmp (i->inst, type_instance) != 0))
183 DEBUG ("netlink plugin: check_ignorelist: "
184 "(dev = %s; type = %s; inst = %s) matched "
185 "(dev = %s; type = %s; inst = %s)",
187 type_instance == NULL ? "(nil)" : type_instance,
188 i->device == NULL ? "(nil)" : i->device,
190 i->inst == NULL ? "(nil)" : i->inst);
192 return (ir_ignorelist_invert ? 0 : 1);
195 return (ir_ignorelist_invert);
196 } /* int check_ignorelist */
198 static void submit_one (const char *dev, const char *type,
199 const char *type_instance, derive_t value)
202 value_list_t vl = VALUE_LIST_INIT;
204 values[0].derive = value;
208 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
209 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
210 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
211 sstrncpy (vl.type, type, sizeof (vl.type));
213 if (type_instance != NULL)
214 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
216 plugin_dispatch_values (&vl);
217 } /* void submit_one */
219 static void submit_two (const char *dev, const char *type,
220 const char *type_instance,
221 derive_t rx, derive_t tx)
224 value_list_t vl = VALUE_LIST_INIT;
226 values[0].derive = rx;
227 values[1].derive = tx;
231 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
232 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
233 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
234 sstrncpy (vl.type, type, sizeof (vl.type));
236 if (type_instance != NULL)
237 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
239 plugin_dispatch_values (&vl);
240 } /* void submit_two */
242 static int update_iflist (struct ifinfomsg *msg, const char *dev)
244 /* Update the `iflist'. It's used to know which interfaces exist and query
245 * them later for qdiscs and classes. */
246 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
250 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
253 ERROR ("netlink plugin: update_iflist: realloc failed.");
257 memset (temp + iflist_len, '\0',
258 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
260 iflist_len = msg->ifi_index + 1;
262 if ((iflist[msg->ifi_index] == NULL)
263 || (strcmp (iflist[msg->ifi_index], dev) != 0))
265 sfree (iflist[msg->ifi_index]);
266 iflist[msg->ifi_index] = strdup (dev);
270 } /* int update_iflist */
272 static void check_ignorelist_and_submit (const char *dev,
273 struct ir_link_stats_storage_s *stats)
276 if (check_ignorelist (dev, "interface", NULL) == 0)
278 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
279 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
280 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
284 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
287 if (check_ignorelist (dev, "if_detail", NULL) == 0)
289 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
290 submit_one (dev, "if_multicast", NULL, stats->multicast);
291 submit_one (dev, "if_collisions", NULL, stats->collisions);
293 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
294 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
295 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
296 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
297 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
298 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
300 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
301 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
302 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
303 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
304 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
308 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
311 } /* void check_ignorelist_and_submit */
313 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
314 (dst_stats)->value_name = (src_stats)->value_name
316 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats) \
317 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_packets); \
318 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_packets); \
319 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_bytes); \
320 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_bytes); \
321 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_errors); \
322 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_errors); \
323 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_dropped); \
324 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_dropped); \
325 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, multicast); \
326 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, collisions); \
327 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_length_errors); \
328 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_over_errors); \
329 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_crc_errors); \
330 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_frame_errors); \
331 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_fifo_errors); \
332 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_missed_errors); \
333 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_aborted_errors); \
334 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_carrier_errors); \
335 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_fifo_errors); \
336 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_heartbeat_errors); \
337 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_window_errors)
339 #ifdef HAVE_RTNL_LINK_STATS64
340 static void check_ignorelist_and_submit64 (const char *dev,
341 struct rtnl_link_stats64 *stats)
343 struct ir_link_stats_storage_s s;
345 COPY_RTNL_LINK_STATS (&s, stats);
347 check_ignorelist_and_submit (dev, &s);
351 static void check_ignorelist_and_submit32 (const char *dev,
352 struct rtnl_link_stats *stats)
354 struct ir_link_stats_storage_s s;
356 COPY_RTNL_LINK_STATS(&s, stats);
358 check_ignorelist_and_submit (dev, &s);
361 static int link_filter_cb (const struct nlmsghdr *nlh,
362 void *args __attribute__((unused)))
364 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
366 const char *dev = NULL;
367 union ir_link_stats_u stats;
369 if (nlh->nlmsg_type != RTM_NEWLINK)
371 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
376 /* Scan attribute list for device name. */
377 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
379 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
382 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
384 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
388 dev = mnl_attr_get_str (attr);
389 if (update_iflist (ifm, dev) < 0)
396 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
399 #ifdef HAVE_RTNL_LINK_STATS64
400 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
402 if (mnl_attr_get_type (attr) != IFLA_STATS64)
405 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats64)) < 0)
407 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 failed.");
410 stats.stats64 = mnl_attr_get_payload (attr);
412 check_ignorelist_and_submit64 (dev, stats.stats64);
417 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
419 if (mnl_attr_get_type (attr) != IFLA_STATS)
422 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats32)) < 0)
424 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
427 stats.stats32 = mnl_attr_get_payload (attr);
429 check_ignorelist_and_submit32 (dev, stats.stats32);
434 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
437 } /* int link_filter_cb */
440 static int qos_attr_cb (const struct nlattr *attr, void *data)
442 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
444 /* skip unsupported attribute in user-space */
445 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
448 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
450 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
452 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
455 *bs = mnl_attr_get_payload (attr);
463 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
465 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
468 int wanted_ifindex = *((int *) args);
471 const char *kind = NULL;
473 /* char *type_instance; */
475 char tc_inst[DATA_MAX_NAME_LEN];
477 _Bool stats_submitted = 0;
479 if (nlh->nlmsg_type == RTM_NEWQDISC)
481 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
483 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
487 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
492 if (tm->tcm_ifindex != wanted_ifindex)
494 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
496 tc_type, tm->tcm_ifindex, wanted_ifindex);
500 if ((tm->tcm_ifindex >= 0)
501 && ((size_t) tm->tcm_ifindex >= iflist_len))
503 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
504 ">= iflist_len = %zu",
505 tm->tcm_ifindex, iflist_len);
509 dev = iflist[tm->tcm_ifindex];
512 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
517 mnl_attr_for_each (attr, nlh, sizeof (*tm))
519 if (mnl_attr_get_type (attr) != TCA_KIND)
522 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
524 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
528 kind = mnl_attr_get_str (attr);
534 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
539 uint32_t numberic_id;
541 numberic_id = tm->tcm_handle;
542 if (strcmp (tc_type, "filter") == 0)
543 numberic_id = tm->tcm_parent;
545 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
548 numberic_id & 0x0000FFFF);
551 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
552 tc_type, dev, tm->tcm_ifindex);
554 if (check_ignorelist (dev, tc_type, tc_inst))
558 mnl_attr_for_each (attr, nlh, sizeof (*tm))
560 struct gnet_stats_basic *bs = NULL;
562 if (mnl_attr_get_type (attr) != TCA_STATS2)
565 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
567 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
571 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
575 char type_instance[DATA_MAX_NAME_LEN];
579 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
582 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
583 submit_one (dev, "ipt_packets", type_instance, bs->packets);
588 #endif /* TCA_STATS2 */
591 mnl_attr_for_each (attr, nlh, sizeof (*tm))
593 struct tc_stats *ts = NULL;
595 if (mnl_attr_get_type (attr) != TCA_STATS)
598 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
600 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
603 ts = mnl_attr_get_payload (attr);
605 if (!stats_submitted && ts != NULL)
607 char type_instance[DATA_MAX_NAME_LEN];
609 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
612 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
613 submit_one (dev, "ipt_packets", type_instance, ts->packets);
619 #endif /* TCA_STATS */
621 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
622 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
627 } /* int qos_filter_cb */
629 static int ir_config (const char *key, const char *value)
636 new_val = strdup (value);
640 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
641 if ((fields_num < 1) || (fields_num > 8))
647 if ((strcasecmp (key, "Interface") == 0)
648 || (strcasecmp (key, "VerboseInterface") == 0))
652 ERROR ("netlink plugin: Invalid number of fields for option "
653 "`%s'. Got %i, expected 1.", key, fields_num);
658 add_ignorelist (fields[0], "interface", NULL);
659 if (strcasecmp (key, "VerboseInterface") == 0)
660 add_ignorelist (fields[0], "if_detail", NULL);
664 else if ((strcasecmp (key, "QDisc") == 0)
665 || (strcasecmp (key, "Class") == 0)
666 || (strcasecmp (key, "Filter") == 0))
668 if ((fields_num < 1) || (fields_num > 2))
670 ERROR ("netlink plugin: Invalid number of fields for option "
671 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
676 add_ignorelist (fields[0], key,
677 (fields_num == 2) ? fields[1] : NULL);
681 else if (strcasecmp (key, "IgnoreSelected") == 0)
685 ERROR ("netlink plugin: Invalid number of fields for option "
686 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
691 if (IS_TRUE (fields[0]))
692 ir_ignorelist_invert = 0;
694 ir_ignorelist_invert = 1;
702 } /* int ir_config */
704 static int ir_init (void)
706 nl = mnl_socket_open (NETLINK_ROUTE);
709 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
713 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
715 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
722 static int ir_read (void)
724 char buf[MNL_SOCKET_BUFFER_SIZE];
725 struct nlmsghdr *nlh;
728 unsigned int seq, portid;
732 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
733 static const char *type_name[] = { "qdisc", "class", "filter" };
735 portid = mnl_socket_get_portid (nl);
737 nlh = mnl_nlmsg_put_header (buf);
738 nlh->nlmsg_type = RTM_GETLINK;
739 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
740 nlh->nlmsg_seq = seq = time (NULL);
741 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
742 rt->rtgen_family = AF_PACKET;
744 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
746 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
750 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
753 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
754 if (ret <= MNL_CB_STOP)
756 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
760 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
764 /* `link_filter_cb' will update `iflist' which is used here to iterate
765 * over all interfaces. */
766 for (ifindex = 1; ifindex < iflist_len; ifindex++)
771 if (iflist[ifindex] == NULL)
774 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
776 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
778 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
779 "== TRUE", iflist[ifindex], type_name[type_index]);
783 DEBUG ("netlink plugin: ir_read: querying %s from %s (%zu).",
784 type_name[type_index], iflist[ifindex], ifindex);
786 nlh = mnl_nlmsg_put_header (buf);
787 nlh->nlmsg_type = type_id[type_index];
788 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
789 nlh->nlmsg_seq = seq = time (NULL);
790 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
791 tm->tcm_family = AF_PACKET;
792 tm->tcm_ifindex = ifindex;
794 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
796 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
800 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
803 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
804 if (ret <= MNL_CB_STOP)
806 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
810 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
814 } /* for (type_index) */
815 } /* for (if_index) */
820 static int ir_shutdown (void)
824 mnl_socket_close (nl);
829 } /* int ir_shutdown */
831 void module_register (void)
833 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
834 plugin_register_init ("netlink", ir_init);
835 plugin_register_read ("netlink", ir_read);
836 plugin_register_shutdown ("netlink", ir_shutdown);
837 } /* void module_register */
840 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :