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>
34 #include <linux/netlink.h>
35 #include <linux/rtnetlink.h>
36 #if HAVE_LINUX_GEN_STATS_H
37 # include <linux/gen_stats.h>
39 #if HAVE_LINUX_PKT_SCHED_H
40 # include <linux/pkt_sched.h>
43 #include <libmnl/libmnl.h>
45 struct ir_link_stats_storage_s {
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;
73 union ir_link_stats_u {
74 struct rtnl_link_stats *stats32;
75 #ifdef HAVE_RTNL_LINK_STATS64
76 struct rtnl_link_stats64 *stats64;
80 typedef struct ir_ignorelist_s
85 struct ir_ignorelist_s *next;
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[] =
105 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
107 static int add_ignorelist (const char *dev, const char *type,
110 ir_ignorelist_t *entry;
112 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
116 memset (entry, '\0', sizeof (ir_ignorelist_t));
118 if (strcasecmp (dev, "All") != 0)
120 entry->device = strdup (dev);
121 if (entry->device == NULL)
128 entry->type = strdup (type);
129 if (entry->type == NULL)
131 sfree (entry->device);
138 entry->inst = strdup (inst);
139 if (entry->inst == NULL)
142 sfree (entry->device);
148 entry->next = ir_ignorelist_head;
149 ir_ignorelist_head = entry;
152 } /* int add_ignorelist */
155 * Checks wether a data set should be ignored. Returns `true' is the value
156 * should be ignored, `false' otherwise.
158 static int check_ignorelist (const char *dev,
159 const char *type, const char *type_instance)
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)
170 /* i->device == NULL => match all devices */
171 if ((i->device != NULL)
172 && (strcasecmp (i->device, dev) != 0))
175 if (strcasecmp (i->type, type) != 0)
178 if ((i->inst != NULL) && (type_instance != NULL)
179 && (strcasecmp (i->inst, type_instance) != 0))
182 DEBUG ("netlink plugin: check_ignorelist: "
183 "(dev = %s; type = %s; inst = %s) matched "
184 "(dev = %s; type = %s; inst = %s)",
186 type_instance == NULL ? "(nil)" : type_instance,
187 i->device == NULL ? "(nil)" : i->device,
189 i->inst == NULL ? "(nil)" : i->inst);
191 return (ir_ignorelist_invert ? 0 : 1);
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)
201 value_list_t vl = VALUE_LIST_INIT;
203 values[0].derive = value;
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)
223 value_list_t vl = VALUE_LIST_INIT;
225 values[0].derive = rx;
226 values[1].derive = tx;
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)
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))
249 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
252 ERROR ("netlink plugin: update_iflist: realloc failed.");
256 memset (temp + iflist_len, '\0',
257 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
259 iflist_len = msg->ifi_index + 1;
261 if ((iflist[msg->ifi_index] == NULL)
262 || (strcmp (iflist[msg->ifi_index], dev) != 0))
264 sfree (iflist[msg->ifi_index]);
265 iflist[msg->ifi_index] = strdup (dev);
269 } /* int update_iflist */
271 static void check_ignorelist_and_submit (const char *dev,
272 struct ir_link_stats_storage_s *stats)
275 if (check_ignorelist (dev, "interface", NULL) == 0)
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);
283 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
286 if (check_ignorelist (dev, "if_detail", NULL) == 0)
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);
307 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
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)
342 struct ir_link_stats_storage_s s;
344 COPY_RTNL_LINK_STATS (&s, stats);
346 check_ignorelist_and_submit (dev, &s);
350 static void check_ignorelist_and_submit32 (const char *dev,
351 struct rtnl_link_stats *stats)
353 struct ir_link_stats_storage_s s;
355 COPY_RTNL_LINK_STATS(&s, stats);
357 check_ignorelist_and_submit (dev, &s);
360 static int link_filter_cb (const struct nlmsghdr *nlh,
361 void *args __attribute__((unused)))
363 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
365 const char *dev = NULL;
366 union ir_link_stats_u stats;
368 if (nlh->nlmsg_type != RTM_NEWLINK)
370 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
375 /* Scan attribute list for device name. */
376 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
378 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
381 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
383 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
387 dev = mnl_attr_get_str (attr);
388 if (update_iflist (ifm, dev) < 0)
395 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
398 #ifdef HAVE_RTNL_LINK_STATS64
399 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
401 if (mnl_attr_get_type (attr) != IFLA_STATS64)
404 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats64)) < 0)
406 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 failed.");
409 stats.stats64 = mnl_attr_get_payload (attr);
411 check_ignorelist_and_submit64 (dev, stats.stats64);
416 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
418 if (mnl_attr_get_type (attr) != IFLA_STATS)
421 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats32)) < 0)
423 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
426 stats.stats32 = mnl_attr_get_payload (attr);
428 check_ignorelist_and_submit32 (dev, stats.stats32);
433 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
436 } /* int link_filter_cb */
439 static int qos_attr_cb (const struct nlattr *attr, void *data)
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)
447 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
449 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
451 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
454 *bs = mnl_attr_get_payload (attr);
462 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
464 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
467 int wanted_ifindex = *((int *) args);
470 const char *kind = NULL;
472 /* char *type_instance; */
474 char tc_inst[DATA_MAX_NAME_LEN];
476 _Bool stats_submitted = 0;
478 if (nlh->nlmsg_type == RTM_NEWQDISC)
480 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
482 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
486 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
491 if (tm->tcm_ifindex != wanted_ifindex)
493 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
495 tc_type, tm->tcm_ifindex, wanted_ifindex);
499 if ((tm->tcm_ifindex >= 0)
500 && ((size_t) tm->tcm_ifindex >= iflist_len))
502 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
503 ">= iflist_len = %zu",
504 tm->tcm_ifindex, iflist_len);
508 dev = iflist[tm->tcm_ifindex];
511 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
516 mnl_attr_for_each (attr, nlh, sizeof (*tm))
518 if (mnl_attr_get_type (attr) != TCA_KIND)
521 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
523 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
527 kind = mnl_attr_get_str (attr);
533 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
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",
547 numberic_id & 0x0000FFFF);
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))
557 mnl_attr_for_each (attr, nlh, sizeof (*tm))
559 struct gnet_stats_basic *bs = NULL;
561 if (mnl_attr_get_type (attr) != TCA_STATS2)
564 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
566 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
570 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
574 char type_instance[DATA_MAX_NAME_LEN];
578 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
581 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
582 submit_one (dev, "ipt_packets", type_instance, bs->packets);
587 #endif /* TCA_STATS2 */
590 mnl_attr_for_each (attr, nlh, sizeof (*tm))
592 struct tc_stats *ts = NULL;
594 if (mnl_attr_get_type (attr) != TCA_STATS)
597 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
599 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
602 ts = mnl_attr_get_payload (attr);
604 if (!stats_submitted && ts != NULL)
606 char type_instance[DATA_MAX_NAME_LEN];
608 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
611 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
612 submit_one (dev, "ipt_packets", type_instance, ts->packets);
618 #endif /* TCA_STATS */
620 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
621 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
626 } /* int qos_filter_cb */
628 static int ir_config (const char *key, const char *value)
635 new_val = strdup (value);
639 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
640 if ((fields_num < 1) || (fields_num > 8))
646 if ((strcasecmp (key, "Interface") == 0)
647 || (strcasecmp (key, "VerboseInterface") == 0))
651 ERROR ("netlink plugin: Invalid number of fields for option "
652 "`%s'. Got %i, expected 1.", key, fields_num);
657 add_ignorelist (fields[0], "interface", NULL);
658 if (strcasecmp (key, "VerboseInterface") == 0)
659 add_ignorelist (fields[0], "if_detail", NULL);
663 else if ((strcasecmp (key, "QDisc") == 0)
664 || (strcasecmp (key, "Class") == 0)
665 || (strcasecmp (key, "Filter") == 0))
667 if ((fields_num < 1) || (fields_num > 2))
669 ERROR ("netlink plugin: Invalid number of fields for option "
670 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
675 add_ignorelist (fields[0], key,
676 (fields_num == 2) ? fields[1] : NULL);
680 else if (strcasecmp (key, "IgnoreSelected") == 0)
684 ERROR ("netlink plugin: Invalid number of fields for option "
685 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
690 if (IS_TRUE (fields[0]))
691 ir_ignorelist_invert = 0;
693 ir_ignorelist_invert = 1;
701 } /* int ir_config */
703 static int ir_init (void)
705 nl = mnl_socket_open (NETLINK_ROUTE);
708 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
712 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
714 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
721 static int ir_read (void)
723 char buf[MNL_SOCKET_BUFFER_SIZE];
724 struct nlmsghdr *nlh;
727 unsigned int seq, portid;
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)
745 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
749 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
752 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
753 if (ret <= MNL_CB_STOP)
755 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
759 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
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++)
770 if (iflist[ifindex] == NULL)
773 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
775 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
777 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
778 "== TRUE", iflist[ifindex], type_name[type_index]);
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)
795 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
799 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
802 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
803 if (ret <= MNL_CB_STOP)
805 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
809 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
813 } /* for (type_index) */
814 } /* for (if_index) */
819 static int ir_shutdown (void)
823 mnl_socket_close (nl);
828 } /* int ir_shutdown */
830 void module_register (void)
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 */
839 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :