2 * collectd - src/netlink.c
3 * Copyright (C) 2007-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
26 #include <asm/types.h>
27 #include <sys/socket.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #if HAVE_LINUX_GEN_STATS_H
32 # include <linux/gen_stats.h>
34 #if HAVE_LINUX_PKT_SCHED_H
35 # include <linux/pkt_sched.h>
39 #include <libmnl/libmnl.h>
41 typedef struct ir_ignorelist_s
46 struct ir_ignorelist_s *next;
49 static int ir_ignorelist_invert = 1;
50 static ir_ignorelist_t *ir_ignorelist_head = NULL;
52 static struct mnl_socket *nl;
54 static char **iflist = NULL;
55 static size_t iflist_len = 0;
57 static const char *config_keys[] =
66 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
68 static int add_ignorelist (const char *dev, const char *type,
71 ir_ignorelist_t *entry;
73 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
77 memset (entry, '\0', sizeof (ir_ignorelist_t));
79 if (strcasecmp (dev, "All") != 0)
81 entry->device = strdup (dev);
82 if (entry->device == NULL)
89 entry->type = strdup (type);
90 if (entry->type == NULL)
92 sfree (entry->device);
99 entry->inst = strdup (inst);
100 if (entry->inst == NULL)
103 sfree (entry->device);
109 entry->next = ir_ignorelist_head;
110 ir_ignorelist_head = entry;
113 } /* int add_ignorelist */
116 * Checks wether a data set should be ignored. Returns `true' is the value
117 * should be ignored, `false' otherwise.
119 static int check_ignorelist (const char *dev,
120 const char *type, const char *type_instance)
124 assert ((dev != NULL) && (type != NULL));
126 if (ir_ignorelist_head == NULL)
127 return (ir_ignorelist_invert ? 0 : 1);
129 for (i = ir_ignorelist_head; i != NULL; i = i->next)
131 /* i->device == NULL => match all devices */
132 if ((i->device != NULL)
133 && (strcasecmp (i->device, dev) != 0))
136 if (strcasecmp (i->type, type) != 0)
139 if ((i->inst != NULL) && (type_instance != NULL)
140 && (strcasecmp (i->inst, type_instance) != 0))
143 DEBUG ("netlink plugin: check_ignorelist: "
144 "(dev = %s; type = %s; inst = %s) matched "
145 "(dev = %s; type = %s; inst = %s)",
147 type_instance == NULL ? "(nil)" : type_instance,
148 i->device == NULL ? "(nil)" : i->device,
150 i->inst == NULL ? "(nil)" : i->inst);
152 return (ir_ignorelist_invert ? 0 : 1);
155 return (ir_ignorelist_invert);
156 } /* int check_ignorelist */
158 static void submit_one (const char *dev, const char *type,
159 const char *type_instance, derive_t value)
162 value_list_t vl = VALUE_LIST_INIT;
164 values[0].derive = value;
168 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
169 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
170 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
171 sstrncpy (vl.type, type, sizeof (vl.type));
173 if (type_instance != NULL)
174 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
176 plugin_dispatch_values (&vl);
177 } /* void submit_one */
179 static void submit_two (const char *dev, const char *type,
180 const char *type_instance,
181 derive_t rx, derive_t tx)
184 value_list_t vl = VALUE_LIST_INIT;
186 values[0].derive = rx;
187 values[1].derive = tx;
191 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
192 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
193 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
194 sstrncpy (vl.type, type, sizeof (vl.type));
196 if (type_instance != NULL)
197 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
199 plugin_dispatch_values (&vl);
200 } /* void submit_two */
202 static int update_iflist (struct ifinfomsg *msg, const char *dev)
204 /* Update the `iflist'. It's used to know which interfaces exist and query
205 * them later for qdiscs and classes. */
206 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
210 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
213 ERROR ("netlink plugin: update_iflist: realloc failed.");
217 memset (temp + iflist_len, '\0',
218 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
220 iflist_len = msg->ifi_index + 1;
222 if ((iflist[msg->ifi_index] == NULL)
223 || (strcmp (iflist[msg->ifi_index], dev) != 0))
225 sfree (iflist[msg->ifi_index]);
226 iflist[msg->ifi_index] = strdup (dev);
230 } /* int update_iflist */
232 static void check_ignorelist_and_submit (const char *dev,
233 struct rtnl_link_stats *stats)
236 if (check_ignorelist (dev, "interface", NULL) == 0)
238 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
239 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
240 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
244 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
247 if (check_ignorelist (dev, "if_detail", NULL) == 0)
249 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
250 submit_one (dev, "if_multicast", NULL, stats->multicast);
251 submit_one (dev, "if_collisions", NULL, stats->collisions);
253 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
254 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
255 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
256 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
257 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
258 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
260 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
261 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
262 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
263 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
264 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
268 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
271 } /* void check_ignorelist_and_submit */
273 static int link_filter_cb (const struct nlmsghdr *nlh,
274 void *args __attribute__((unused)))
276 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
278 struct rtnl_link_stats *stats = NULL;
279 const char *dev = NULL;
281 if (nlh->nlmsg_type != RTM_NEWLINK)
283 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
288 /* Scan attribute list for device name. */
289 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
291 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
294 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
296 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
300 dev = mnl_attr_get_str (attr);
301 if (update_iflist (ifm, dev) < 0)
308 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
312 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
314 if (mnl_attr_get_type (attr) != IFLA_STATS)
317 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats)) < 0)
319 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
322 stats = mnl_attr_get_payload (attr);
324 check_ignorelist_and_submit (dev, stats);
330 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
335 } /* int link_filter_cb */
338 static int qos_attr_cb (const struct nlattr *attr, void *data)
340 struct gnet_stats_basic *bs = *(struct gnet_stats_basic **)data;
342 /* skip unsupported attribute in user-space */
343 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
346 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
348 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*bs)) < 0)
350 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
353 bs = mnl_attr_get_payload (attr);
361 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
363 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
366 int wanted_ifindex = *((int *) args);
369 const char *kind = NULL;
371 /* char *type_instance; */
373 char tc_inst[DATA_MAX_NAME_LEN];
375 _Bool stats_submitted = 0;
377 if (nlh->nlmsg_type == RTM_NEWQDISC)
379 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
381 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
385 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
390 if (tm->tcm_ifindex != wanted_ifindex)
392 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
394 tc_type, tm->tcm_ifindex, wanted_ifindex);
398 if ((tm->tcm_ifindex >= 0)
399 && ((size_t) tm->tcm_ifindex >= iflist_len))
401 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
402 ">= iflist_len = %zu",
403 tm->tcm_ifindex, iflist_len);
407 dev = iflist[tm->tcm_ifindex];
410 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
415 mnl_attr_for_each (attr, nlh, sizeof (*tm))
417 if (mnl_attr_get_type (attr) != TCA_KIND)
420 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
422 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
426 kind = mnl_attr_get_str (attr);
432 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
437 uint32_t numberic_id;
439 numberic_id = tm->tcm_handle;
440 if (strcmp (tc_type, "filter") == 0)
441 numberic_id = tm->tcm_parent;
443 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
446 numberic_id & 0x0000FFFF);
449 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
450 tc_type, dev, tm->tcm_ifindex);
452 if (check_ignorelist (dev, tc_type, tc_inst))
456 mnl_attr_for_each (attr, nlh, sizeof (*tm))
458 struct gnet_stats_basic *bs = NULL;
460 if (mnl_attr_get_type (attr) != TCA_STATS2)
463 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
465 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
469 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
473 char type_instance[DATA_MAX_NAME_LEN];
477 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
480 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
481 submit_one (dev, "ipt_packets", type_instance, bs->packets);
486 #endif /* TCA_STATS2 */
489 mnl_attr_for_each (attr, nlh, sizeof (*tm))
491 struct tc_stats *ts = NULL;
493 if (mnl_attr_get_type (attr) != TCA_STATS)
496 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
498 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
501 ts = mnl_attr_get_payload (attr);
503 if (!stats_submitted && ts != NULL)
505 char type_instance[DATA_MAX_NAME_LEN];
507 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
510 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
511 submit_one (dev, "ipt_packets", type_instance, ts->packets);
517 #endif /* TCA_STATS */
519 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
520 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
525 } /* int qos_filter_cb */
527 static int ir_config (const char *key, const char *value)
534 new_val = strdup (value);
538 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
539 if ((fields_num < 1) || (fields_num > 8))
545 if ((strcasecmp (key, "Interface") == 0)
546 || (strcasecmp (key, "VerboseInterface") == 0))
550 ERROR ("netlink plugin: Invalid number of fields for option "
551 "`%s'. Got %i, expected 1.", key, fields_num);
556 add_ignorelist (fields[0], "interface", NULL);
557 if (strcasecmp (key, "VerboseInterface") == 0)
558 add_ignorelist (fields[0], "if_detail", NULL);
562 else if ((strcasecmp (key, "QDisc") == 0)
563 || (strcasecmp (key, "Class") == 0)
564 || (strcasecmp (key, "Filter") == 0))
566 if ((fields_num < 1) || (fields_num > 2))
568 ERROR ("netlink plugin: Invalid number of fields for option "
569 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
574 add_ignorelist (fields[0], key,
575 (fields_num == 2) ? fields[1] : NULL);
579 else if (strcasecmp (key, "IgnoreSelected") == 0)
583 ERROR ("netlink plugin: Invalid number of fields for option "
584 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
589 if (IS_TRUE (fields[0]))
590 ir_ignorelist_invert = 0;
592 ir_ignorelist_invert = 1;
600 } /* int ir_config */
602 static int ir_init (void)
604 nl = mnl_socket_open (NETLINK_ROUTE);
607 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
611 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
613 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
620 static int ir_read (void)
622 char buf[MNL_SOCKET_BUFFER_SIZE];
623 struct nlmsghdr *nlh;
626 unsigned int seq, portid;
630 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
631 static const char *type_name[] = { "qdisc", "class", "filter" };
633 portid = mnl_socket_get_portid (nl);
635 nlh = mnl_nlmsg_put_header (buf);
636 nlh->nlmsg_type = RTM_GETLINK;
637 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
638 nlh->nlmsg_seq = seq = time (NULL);
639 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
640 rt->rtgen_family = AF_PACKET;
642 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
644 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
648 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
651 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
652 if (ret <= MNL_CB_STOP)
654 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
658 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
662 /* `link_filter_cb' will update `iflist' which is used here to iterate
663 * over all interfaces. */
664 for (ifindex = 1; ifindex < iflist_len; ifindex++)
669 if (iflist[ifindex] == NULL)
672 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
674 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
676 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
677 "== TRUE", iflist[ifindex], type_name[type_index]);
681 DEBUG ("netlink plugin: ir_read: querying %s from %s (%lu).",
682 type_name[type_index], iflist[ifindex], ifindex);
684 nlh = mnl_nlmsg_put_header (buf);
685 nlh->nlmsg_type = type_id[type_index];
686 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
687 nlh->nlmsg_seq = seq = time (NULL);
688 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
689 tm->tcm_family = AF_PACKET;
690 tm->tcm_ifindex = ifindex;
692 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
694 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
698 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
701 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
702 if (ret <= MNL_CB_STOP)
704 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
708 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
712 } /* for (type_index) */
713 } /* for (if_index) */
718 static int ir_shutdown (void)
722 mnl_socket_close (nl);
727 } /* int ir_shutdown */
729 void module_register (void)
731 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
732 plugin_register_init ("netlink", ir_init);
733 plugin_register_read ("netlink", ir_read);
734 plugin_register_shutdown ("netlink", ir_shutdown);
735 } /* void module_register */
738 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :