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 = calloc (1, sizeof (*entry));
116 if (strcasecmp (dev, "All") != 0)
118 entry->device = strdup (dev);
119 if (entry->device == NULL)
126 entry->type = strdup (type);
127 if (entry->type == NULL)
129 sfree (entry->device);
136 entry->inst = strdup (inst);
137 if (entry->inst == NULL)
140 sfree (entry->device);
146 entry->next = ir_ignorelist_head;
147 ir_ignorelist_head = entry;
150 } /* int add_ignorelist */
153 * Checks wether a data set should be ignored. Returns `true' is the value
154 * should be ignored, `false' otherwise.
156 static int check_ignorelist (const char *dev,
157 const char *type, const char *type_instance)
161 assert ((dev != NULL) && (type != NULL));
163 if (ir_ignorelist_head == NULL)
164 return (ir_ignorelist_invert ? 0 : 1);
166 for (i = ir_ignorelist_head; i != NULL; i = i->next)
168 /* i->device == NULL => match all devices */
169 if ((i->device != NULL)
170 && (strcasecmp (i->device, dev) != 0))
173 if (strcasecmp (i->type, type) != 0)
176 if ((i->inst != NULL) && (type_instance != NULL)
177 && (strcasecmp (i->inst, type_instance) != 0))
180 DEBUG ("netlink plugin: check_ignorelist: "
181 "(dev = %s; type = %s; inst = %s) matched "
182 "(dev = %s; type = %s; inst = %s)",
184 type_instance == NULL ? "(nil)" : type_instance,
185 i->device == NULL ? "(nil)" : i->device,
187 i->inst == NULL ? "(nil)" : i->inst);
189 return (ir_ignorelist_invert ? 0 : 1);
192 return (ir_ignorelist_invert);
193 } /* int check_ignorelist */
195 static void submit_one (const char *dev, const char *type,
196 const char *type_instance, derive_t value)
199 value_list_t vl = VALUE_LIST_INIT;
201 values[0].derive = value;
205 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
206 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
207 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
208 sstrncpy (vl.type, type, sizeof (vl.type));
210 if (type_instance != NULL)
211 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
213 plugin_dispatch_values (&vl);
214 } /* void submit_one */
216 static void submit_two (const char *dev, const char *type,
217 const char *type_instance,
218 derive_t rx, derive_t tx)
221 value_list_t vl = VALUE_LIST_INIT;
223 values[0].derive = rx;
224 values[1].derive = tx;
228 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
229 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
230 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
231 sstrncpy (vl.type, type, sizeof (vl.type));
233 if (type_instance != NULL)
234 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
236 plugin_dispatch_values (&vl);
237 } /* void submit_two */
239 static int update_iflist (struct ifinfomsg *msg, const char *dev)
241 /* Update the `iflist'. It's used to know which interfaces exist and query
242 * them later for qdiscs and classes. */
243 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
247 temp = realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
250 ERROR ("netlink plugin: update_iflist: realloc failed.");
254 memset (temp + iflist_len, '\0',
255 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
257 iflist_len = msg->ifi_index + 1;
259 if ((iflist[msg->ifi_index] == NULL)
260 || (strcmp (iflist[msg->ifi_index], dev) != 0))
262 sfree (iflist[msg->ifi_index]);
263 iflist[msg->ifi_index] = strdup (dev);
267 } /* int update_iflist */
269 static void check_ignorelist_and_submit (const char *dev,
270 struct ir_link_stats_storage_s *stats)
273 if (check_ignorelist (dev, "interface", NULL) == 0)
275 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
276 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
277 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
281 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
284 if (check_ignorelist (dev, "if_detail", NULL) == 0)
286 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
287 submit_one (dev, "if_multicast", NULL, stats->multicast);
288 submit_one (dev, "if_collisions", NULL, stats->collisions);
290 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
291 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
292 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
293 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
294 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
295 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
297 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
298 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
299 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
300 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
301 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
305 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
308 } /* void check_ignorelist_and_submit */
310 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
311 (dst_stats)->value_name = (src_stats)->value_name
313 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats) \
314 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_packets); \
315 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_packets); \
316 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_bytes); \
317 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_bytes); \
318 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_errors); \
319 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_errors); \
320 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_dropped); \
321 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_dropped); \
322 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, multicast); \
323 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, collisions); \
324 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_length_errors); \
325 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_over_errors); \
326 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_crc_errors); \
327 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_frame_errors); \
328 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_fifo_errors); \
329 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, rx_missed_errors); \
330 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_aborted_errors); \
331 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_carrier_errors); \
332 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_fifo_errors); \
333 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_heartbeat_errors); \
334 COPY_RTNL_LINK_VALUE (dst_stats, src_stats, tx_window_errors)
336 #ifdef HAVE_RTNL_LINK_STATS64
337 static void check_ignorelist_and_submit64 (const char *dev,
338 struct rtnl_link_stats64 *stats)
340 struct ir_link_stats_storage_s s;
342 COPY_RTNL_LINK_STATS (&s, stats);
344 check_ignorelist_and_submit (dev, &s);
348 static void check_ignorelist_and_submit32 (const char *dev,
349 struct rtnl_link_stats *stats)
351 struct ir_link_stats_storage_s s;
353 COPY_RTNL_LINK_STATS(&s, stats);
355 check_ignorelist_and_submit (dev, &s);
358 static int link_filter_cb (const struct nlmsghdr *nlh,
359 void *args __attribute__((unused)))
361 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
363 const char *dev = NULL;
364 union ir_link_stats_u stats;
366 if (nlh->nlmsg_type != RTM_NEWLINK)
368 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
373 /* Scan attribute list for device name. */
374 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
376 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
379 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
381 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
385 dev = mnl_attr_get_str (attr);
386 if (update_iflist (ifm, dev) < 0)
393 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
396 #ifdef HAVE_RTNL_LINK_STATS64
397 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
399 if (mnl_attr_get_type (attr) != IFLA_STATS64)
402 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats64)) < 0)
404 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 failed.");
407 stats.stats64 = mnl_attr_get_payload (attr);
409 check_ignorelist_and_submit64 (dev, stats.stats64);
414 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
416 if (mnl_attr_get_type (attr) != IFLA_STATS)
419 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats.stats32)) < 0)
421 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
424 stats.stats32 = mnl_attr_get_payload (attr);
426 check_ignorelist_and_submit32 (dev, stats.stats32);
431 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
434 } /* int link_filter_cb */
437 static int qos_attr_cb (const struct nlattr *attr, void *data)
439 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
441 /* skip unsupported attribute in user-space */
442 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
445 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
447 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
449 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
452 *bs = mnl_attr_get_payload (attr);
460 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
462 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
465 int wanted_ifindex = *((int *) args);
468 const char *kind = NULL;
470 /* char *type_instance; */
472 char tc_inst[DATA_MAX_NAME_LEN];
474 _Bool stats_submitted = 0;
476 if (nlh->nlmsg_type == RTM_NEWQDISC)
478 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
480 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
484 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
489 if (tm->tcm_ifindex != wanted_ifindex)
491 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
493 tc_type, tm->tcm_ifindex, wanted_ifindex);
497 if ((tm->tcm_ifindex >= 0)
498 && ((size_t) tm->tcm_ifindex >= iflist_len))
500 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
501 ">= iflist_len = %zu",
502 tm->tcm_ifindex, iflist_len);
506 dev = iflist[tm->tcm_ifindex];
509 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
514 mnl_attr_for_each (attr, nlh, sizeof (*tm))
516 if (mnl_attr_get_type (attr) != TCA_KIND)
519 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
521 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
525 kind = mnl_attr_get_str (attr);
531 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
536 uint32_t numberic_id;
538 numberic_id = tm->tcm_handle;
539 if (strcmp (tc_type, "filter") == 0)
540 numberic_id = tm->tcm_parent;
542 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
545 numberic_id & 0x0000FFFF);
548 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
549 tc_type, dev, tm->tcm_ifindex);
551 if (check_ignorelist (dev, tc_type, tc_inst))
555 mnl_attr_for_each (attr, nlh, sizeof (*tm))
557 struct gnet_stats_basic *bs = NULL;
559 if (mnl_attr_get_type (attr) != TCA_STATS2)
562 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
564 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
568 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
572 char type_instance[DATA_MAX_NAME_LEN];
576 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
579 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
580 submit_one (dev, "ipt_packets", type_instance, bs->packets);
585 #endif /* TCA_STATS2 */
588 mnl_attr_for_each (attr, nlh, sizeof (*tm))
590 struct tc_stats *ts = NULL;
592 if (mnl_attr_get_type (attr) != TCA_STATS)
595 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
597 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
600 ts = mnl_attr_get_payload (attr);
602 if (!stats_submitted && ts != NULL)
604 char type_instance[DATA_MAX_NAME_LEN];
606 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
609 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
610 submit_one (dev, "ipt_packets", type_instance, ts->packets);
616 #endif /* TCA_STATS */
618 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
619 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
624 } /* int qos_filter_cb */
626 static int ir_config (const char *key, const char *value)
633 new_val = strdup (value);
637 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
638 if ((fields_num < 1) || (fields_num > 8))
644 if ((strcasecmp (key, "Interface") == 0)
645 || (strcasecmp (key, "VerboseInterface") == 0))
649 ERROR ("netlink plugin: Invalid number of fields for option "
650 "`%s'. Got %i, expected 1.", key, fields_num);
655 add_ignorelist (fields[0], "interface", NULL);
656 if (strcasecmp (key, "VerboseInterface") == 0)
657 add_ignorelist (fields[0], "if_detail", NULL);
661 else if ((strcasecmp (key, "QDisc") == 0)
662 || (strcasecmp (key, "Class") == 0)
663 || (strcasecmp (key, "Filter") == 0))
665 if ((fields_num < 1) || (fields_num > 2))
667 ERROR ("netlink plugin: Invalid number of fields for option "
668 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
673 add_ignorelist (fields[0], key,
674 (fields_num == 2) ? fields[1] : NULL);
678 else if (strcasecmp (key, "IgnoreSelected") == 0)
682 ERROR ("netlink plugin: Invalid number of fields for option "
683 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
688 if (IS_TRUE (fields[0]))
689 ir_ignorelist_invert = 0;
691 ir_ignorelist_invert = 1;
699 } /* int ir_config */
701 static int ir_init (void)
703 nl = mnl_socket_open (NETLINK_ROUTE);
706 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
710 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
712 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
719 static int ir_read (void)
721 char buf[MNL_SOCKET_BUFFER_SIZE];
722 struct nlmsghdr *nlh;
725 unsigned int seq, portid;
729 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
730 static const char *type_name[] = { "qdisc", "class", "filter" };
732 portid = mnl_socket_get_portid (nl);
734 nlh = mnl_nlmsg_put_header (buf);
735 nlh->nlmsg_type = RTM_GETLINK;
736 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
737 nlh->nlmsg_seq = seq = time (NULL);
738 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
739 rt->rtgen_family = AF_PACKET;
741 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
743 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
747 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
750 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
751 if (ret <= MNL_CB_STOP)
753 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
757 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
761 /* `link_filter_cb' will update `iflist' which is used here to iterate
762 * over all interfaces. */
763 for (ifindex = 1; ifindex < iflist_len; ifindex++)
768 if (iflist[ifindex] == NULL)
771 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
773 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
775 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
776 "== TRUE", iflist[ifindex], type_name[type_index]);
780 DEBUG ("netlink plugin: ir_read: querying %s from %s (%zu).",
781 type_name[type_index], iflist[ifindex], ifindex);
783 nlh = mnl_nlmsg_put_header (buf);
784 nlh->nlmsg_type = type_id[type_index];
785 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
786 nlh->nlmsg_seq = seq = time (NULL);
787 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
788 tm->tcm_family = AF_PACKET;
789 tm->tcm_ifindex = ifindex;
791 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
793 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
797 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
800 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
801 if (ret <= MNL_CB_STOP)
803 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
807 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
811 } /* for (type_index) */
812 } /* for (if_index) */
817 static int ir_shutdown (void)
821 mnl_socket_close (nl);
826 } /* int ir_shutdown */
828 void module_register (void)
830 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
831 plugin_register_init ("netlink", ir_init);
832 plugin_register_read ("netlink", ir_read);
833 plugin_register_shutdown ("netlink", ir_shutdown);
834 } /* void module_register */
837 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :