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 typedef struct ir_ignorelist_s
51 struct ir_ignorelist_s *next;
54 static int ir_ignorelist_invert = 1;
55 static ir_ignorelist_t *ir_ignorelist_head = NULL;
57 static struct mnl_socket *nl;
59 static char **iflist = NULL;
60 static size_t iflist_len = 0;
62 static const char *config_keys[] =
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static int add_ignorelist (const char *dev, const char *type,
76 ir_ignorelist_t *entry;
78 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
82 memset (entry, '\0', sizeof (ir_ignorelist_t));
84 if (strcasecmp (dev, "All") != 0)
86 entry->device = strdup (dev);
87 if (entry->device == NULL)
94 entry->type = strdup (type);
95 if (entry->type == NULL)
97 sfree (entry->device);
104 entry->inst = strdup (inst);
105 if (entry->inst == NULL)
108 sfree (entry->device);
114 entry->next = ir_ignorelist_head;
115 ir_ignorelist_head = entry;
118 } /* int add_ignorelist */
121 * Checks wether a data set should be ignored. Returns `true' is the value
122 * should be ignored, `false' otherwise.
124 static int check_ignorelist (const char *dev,
125 const char *type, const char *type_instance)
129 assert ((dev != NULL) && (type != NULL));
131 if (ir_ignorelist_head == NULL)
132 return (ir_ignorelist_invert ? 0 : 1);
134 for (i = ir_ignorelist_head; i != NULL; i = i->next)
136 /* i->device == NULL => match all devices */
137 if ((i->device != NULL)
138 && (strcasecmp (i->device, dev) != 0))
141 if (strcasecmp (i->type, type) != 0)
144 if ((i->inst != NULL) && (type_instance != NULL)
145 && (strcasecmp (i->inst, type_instance) != 0))
148 DEBUG ("netlink plugin: check_ignorelist: "
149 "(dev = %s; type = %s; inst = %s) matched "
150 "(dev = %s; type = %s; inst = %s)",
152 type_instance == NULL ? "(nil)" : type_instance,
153 i->device == NULL ? "(nil)" : i->device,
155 i->inst == NULL ? "(nil)" : i->inst);
157 return (ir_ignorelist_invert ? 0 : 1);
160 return (ir_ignorelist_invert);
161 } /* int check_ignorelist */
163 static void submit_one (const char *dev, const char *type,
164 const char *type_instance, derive_t value)
167 value_list_t vl = VALUE_LIST_INIT;
169 values[0].derive = value;
173 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
174 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
175 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
176 sstrncpy (vl.type, type, sizeof (vl.type));
178 if (type_instance != NULL)
179 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
181 plugin_dispatch_values (&vl);
182 } /* void submit_one */
184 static void submit_two (const char *dev, const char *type,
185 const char *type_instance,
186 derive_t rx, derive_t tx)
189 value_list_t vl = VALUE_LIST_INIT;
191 values[0].derive = rx;
192 values[1].derive = tx;
196 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
197 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
198 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
199 sstrncpy (vl.type, type, sizeof (vl.type));
201 if (type_instance != NULL)
202 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
204 plugin_dispatch_values (&vl);
205 } /* void submit_two */
207 static int update_iflist (struct ifinfomsg *msg, const char *dev)
209 /* Update the `iflist'. It's used to know which interfaces exist and query
210 * them later for qdiscs and classes. */
211 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
215 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
218 ERROR ("netlink plugin: update_iflist: realloc failed.");
222 memset (temp + iflist_len, '\0',
223 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
225 iflist_len = msg->ifi_index + 1;
227 if ((iflist[msg->ifi_index] == NULL)
228 || (strcmp (iflist[msg->ifi_index], dev) != 0))
230 sfree (iflist[msg->ifi_index]);
231 iflist[msg->ifi_index] = strdup (dev);
235 } /* int update_iflist */
237 static void check_ignorelist_and_submit (const char *dev,
238 struct rtnl_link_stats *stats)
241 if (check_ignorelist (dev, "interface", NULL) == 0)
243 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
244 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
245 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
249 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
252 if (check_ignorelist (dev, "if_detail", NULL) == 0)
254 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
255 submit_one (dev, "if_multicast", NULL, stats->multicast);
256 submit_one (dev, "if_collisions", NULL, stats->collisions);
258 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
259 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
260 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
261 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
262 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
263 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
265 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
266 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
267 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
268 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
269 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
273 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
276 } /* void check_ignorelist_and_submit */
278 static int link_filter_cb (const struct nlmsghdr *nlh,
279 void *args __attribute__((unused)))
281 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
283 struct rtnl_link_stats *stats = NULL;
284 const char *dev = NULL;
286 if (nlh->nlmsg_type != RTM_NEWLINK)
288 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
293 /* Scan attribute list for device name. */
294 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
296 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
299 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
301 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
305 dev = mnl_attr_get_str (attr);
306 if (update_iflist (ifm, dev) < 0)
313 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
317 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
319 if (mnl_attr_get_type (attr) != IFLA_STATS)
322 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats)) < 0)
324 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
327 stats = mnl_attr_get_payload (attr);
329 check_ignorelist_and_submit (dev, stats);
335 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
340 } /* int link_filter_cb */
343 static int qos_attr_cb (const struct nlattr *attr, void *data)
345 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
347 /* skip unsupported attribute in user-space */
348 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
351 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
353 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
355 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
358 *bs = mnl_attr_get_payload (attr);
366 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
368 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
371 int wanted_ifindex = *((int *) args);
374 const char *kind = NULL;
376 /* char *type_instance; */
378 char tc_inst[DATA_MAX_NAME_LEN];
380 _Bool stats_submitted = 0;
382 if (nlh->nlmsg_type == RTM_NEWQDISC)
384 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
386 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
390 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
395 if (tm->tcm_ifindex != wanted_ifindex)
397 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
399 tc_type, tm->tcm_ifindex, wanted_ifindex);
403 if ((tm->tcm_ifindex >= 0)
404 && ((size_t) tm->tcm_ifindex >= iflist_len))
406 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
407 ">= iflist_len = %zu",
408 tm->tcm_ifindex, iflist_len);
412 dev = iflist[tm->tcm_ifindex];
415 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
420 mnl_attr_for_each (attr, nlh, sizeof (*tm))
422 if (mnl_attr_get_type (attr) != TCA_KIND)
425 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
427 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
431 kind = mnl_attr_get_str (attr);
437 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
442 uint32_t numberic_id;
444 numberic_id = tm->tcm_handle;
445 if (strcmp (tc_type, "filter") == 0)
446 numberic_id = tm->tcm_parent;
448 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
451 numberic_id & 0x0000FFFF);
454 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
455 tc_type, dev, tm->tcm_ifindex);
457 if (check_ignorelist (dev, tc_type, tc_inst))
461 mnl_attr_for_each (attr, nlh, sizeof (*tm))
463 struct gnet_stats_basic *bs = NULL;
465 if (mnl_attr_get_type (attr) != TCA_STATS2)
468 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
470 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
474 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
478 char type_instance[DATA_MAX_NAME_LEN];
482 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
485 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
486 submit_one (dev, "ipt_packets", type_instance, bs->packets);
491 #endif /* TCA_STATS2 */
494 mnl_attr_for_each (attr, nlh, sizeof (*tm))
496 struct tc_stats *ts = NULL;
498 if (mnl_attr_get_type (attr) != TCA_STATS)
501 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
503 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
506 ts = mnl_attr_get_payload (attr);
508 if (!stats_submitted && ts != NULL)
510 char type_instance[DATA_MAX_NAME_LEN];
512 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
515 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
516 submit_one (dev, "ipt_packets", type_instance, ts->packets);
522 #endif /* TCA_STATS */
524 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
525 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
530 } /* int qos_filter_cb */
532 static int ir_config (const char *key, const char *value)
539 new_val = strdup (value);
543 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
544 if ((fields_num < 1) || (fields_num > 8))
550 if ((strcasecmp (key, "Interface") == 0)
551 || (strcasecmp (key, "VerboseInterface") == 0))
555 ERROR ("netlink plugin: Invalid number of fields for option "
556 "`%s'. Got %i, expected 1.", key, fields_num);
561 add_ignorelist (fields[0], "interface", NULL);
562 if (strcasecmp (key, "VerboseInterface") == 0)
563 add_ignorelist (fields[0], "if_detail", NULL);
567 else if ((strcasecmp (key, "QDisc") == 0)
568 || (strcasecmp (key, "Class") == 0)
569 || (strcasecmp (key, "Filter") == 0))
571 if ((fields_num < 1) || (fields_num > 2))
573 ERROR ("netlink plugin: Invalid number of fields for option "
574 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
579 add_ignorelist (fields[0], key,
580 (fields_num == 2) ? fields[1] : NULL);
584 else if (strcasecmp (key, "IgnoreSelected") == 0)
588 ERROR ("netlink plugin: Invalid number of fields for option "
589 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
594 if (IS_TRUE (fields[0]))
595 ir_ignorelist_invert = 0;
597 ir_ignorelist_invert = 1;
605 } /* int ir_config */
607 static int ir_init (void)
609 nl = mnl_socket_open (NETLINK_ROUTE);
612 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
616 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
618 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
625 static int ir_read (void)
627 char buf[MNL_SOCKET_BUFFER_SIZE];
628 struct nlmsghdr *nlh;
631 unsigned int seq, portid;
635 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
636 static const char *type_name[] = { "qdisc", "class", "filter" };
638 portid = mnl_socket_get_portid (nl);
640 nlh = mnl_nlmsg_put_header (buf);
641 nlh->nlmsg_type = RTM_GETLINK;
642 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
643 nlh->nlmsg_seq = seq = time (NULL);
644 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
645 rt->rtgen_family = AF_PACKET;
647 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
649 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
653 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
656 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
657 if (ret <= MNL_CB_STOP)
659 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
663 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
667 /* `link_filter_cb' will update `iflist' which is used here to iterate
668 * over all interfaces. */
669 for (ifindex = 1; ifindex < iflist_len; ifindex++)
674 if (iflist[ifindex] == NULL)
677 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
679 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
681 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
682 "== TRUE", iflist[ifindex], type_name[type_index]);
686 DEBUG ("netlink plugin: ir_read: querying %s from %s (%lu).",
687 type_name[type_index], iflist[ifindex], ifindex);
689 nlh = mnl_nlmsg_put_header (buf);
690 nlh->nlmsg_type = type_id[type_index];
691 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
692 nlh->nlmsg_seq = seq = time (NULL);
693 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
694 tm->tcm_family = AF_PACKET;
695 tm->tcm_ifindex = ifindex;
697 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
699 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
703 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
706 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
707 if (ret <= MNL_CB_STOP)
709 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
713 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
717 } /* for (type_index) */
718 } /* for (if_index) */
723 static int ir_shutdown (void)
727 mnl_socket_close (nl);
732 } /* int ir_shutdown */
734 void module_register (void)
736 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
737 plugin_register_init ("netlink", ir_init);
738 plugin_register_read ("netlink", ir_read);
739 plugin_register_shutdown ("netlink", ir_shutdown);
740 } /* void module_register */
743 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :