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>
40 //#include <linux/if.h>
41 //#include <linux/if_link.h>
43 typedef struct ir_ignorelist_s
48 struct ir_ignorelist_s *next;
51 static int ir_ignorelist_invert = 1;
52 static ir_ignorelist_t *ir_ignorelist_head = NULL;
54 static struct mnl_socket *nl;
56 static char **iflist = NULL;
57 static size_t iflist_len = 0;
59 static const char *config_keys[] =
68 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
70 static int add_ignorelist (const char *dev, const char *type,
73 ir_ignorelist_t *entry;
75 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
79 memset (entry, '\0', sizeof (ir_ignorelist_t));
81 if (strcasecmp (dev, "All") != 0)
83 entry->device = strdup (dev);
84 if (entry->device == NULL)
91 entry->type = strdup (type);
92 if (entry->type == NULL)
94 sfree (entry->device);
101 entry->inst = strdup (inst);
102 if (entry->inst == NULL)
105 sfree (entry->device);
111 entry->next = ir_ignorelist_head;
112 ir_ignorelist_head = entry;
115 } /* int add_ignorelist */
118 * Checks wether a data set should be ignored. Returns `true' is the value
119 * should be ignored, `false' otherwise.
121 static int check_ignorelist (const char *dev,
122 const char *type, const char *type_instance)
126 assert ((dev != NULL) && (type != NULL));
128 if (ir_ignorelist_head == NULL)
129 return (ir_ignorelist_invert ? 0 : 1);
131 for (i = ir_ignorelist_head; i != NULL; i = i->next)
133 /* i->device == NULL => match all devices */
134 if ((i->device != NULL)
135 && (strcasecmp (i->device, dev) != 0))
138 if (strcasecmp (i->type, type) != 0)
141 if ((i->inst != NULL) && (type_instance != NULL)
142 && (strcasecmp (i->inst, type_instance) != 0))
145 DEBUG ("netlink plugin: check_ignorelist: "
146 "(dev = %s; type = %s; inst = %s) matched "
147 "(dev = %s; type = %s; inst = %s)",
149 type_instance == NULL ? "(nil)" : type_instance,
150 i->device == NULL ? "(nil)" : i->device,
152 i->inst == NULL ? "(nil)" : i->inst);
154 return (ir_ignorelist_invert ? 0 : 1);
157 return (ir_ignorelist_invert);
158 } /* int check_ignorelist */
160 static void submit_one (const char *dev, const char *type,
161 const char *type_instance, derive_t value)
164 value_list_t vl = VALUE_LIST_INIT;
166 values[0].derive = value;
170 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
171 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
172 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
173 sstrncpy (vl.type, type, sizeof (vl.type));
175 if (type_instance != NULL)
176 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
178 plugin_dispatch_values (&vl);
179 } /* void submit_one */
181 static void submit_two (const char *dev, const char *type,
182 const char *type_instance,
183 derive_t rx, derive_t tx)
186 value_list_t vl = VALUE_LIST_INIT;
188 values[0].derive = rx;
189 values[1].derive = tx;
193 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
194 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
195 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
196 sstrncpy (vl.type, type, sizeof (vl.type));
198 if (type_instance != NULL)
199 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
201 plugin_dispatch_values (&vl);
202 } /* void submit_two */
204 static int update_iflist(struct ifinfomsg *msg)
206 /* Update the `iflist'. It's used to know which interfaces exist and query
207 * them later for qdiscs and classes. */
208 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
212 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
215 ERROR ("netlink plugin: update_iflist: realloc failed.");
219 memset (temp + iflist_len, '\0',
220 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
222 iflist_len = msg->ifi_index + 1;
224 if ((iflist[msg->ifi_index] == NULL)
225 || (strcmp (iflist[msg->ifi_index], dev) != 0))
227 sfree (iflist[msg->ifi_index]);
228 iflist[msg->ifi_index] = strdup (dev);
235 static void check_ignorelist_and_submit(const char *dev,
236 struct rtnl_link_stats *stats)
239 if (check_ignorelist (dev, "interface", NULL) == 0)
241 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
242 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
243 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
247 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
250 if (check_ignorelist (dev, "if_detail", NULL) == 0)
252 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
253 submit_one (dev, "if_multicast", NULL, stats->multicast);
254 submit_one (dev, "if_collisions", NULL, stats->collisions);
256 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
257 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
258 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
259 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
260 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
261 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
263 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
264 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
265 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
266 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
267 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
271 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
276 static int link_filter_cb (const struct nlmsghdr *nlh,
277 void __attribute__((unused)) *args)
279 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
281 struct rtnl_link_stats *stats = NULL;
282 const char *dev = NULL;
284 if (nlh->nlmsg_type != RTM_NEWLINK)
286 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
291 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
293 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
296 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
298 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
302 dev = mnl_attr_get_str (attr);
303 if (update_iflist (ifm) < 0)
310 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
314 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
316 if (mnl_attr_get_type (attr) != IFLA_STATS)
319 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof(*stats)) < 0)
321 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
324 stats = mnl_attr_get_payload(attr);
326 check_ignorelist_and_submit(dev, stats);
332 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
337 } /* int link_filter_cb */
339 static int qos_attr_cb (const struct nlattr *attr, void *data)
341 struct gnet_stats_basic *bs = *(struct gnet_stats_basic **)data;
343 /* skip unsupported attribute in user-space */
344 if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
347 if (mnl_attr_get_type(attr) == TCA_STATS_BASIC)
349 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*bs)) < 0)
351 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
354 bs = mnl_attr_get_payload(attr);
361 static int qos_filter_cb (constr struct nlmsghdr *nlh, void *args)
363 struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
364 int wanted_ifindex = *((int *) args);
367 const char *kind = NULL;
368 struct gnet_stats_basic *bs = NULL;
369 struct tc_stats *ts = NULL;
371 /* char *type_instance; */
373 char tc_inst[DATA_MAX_NAME_LEN];
375 if (tm->tcm_ifindex != wanted_ifindex)
377 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
379 tc_type, msg->tcm_ifindex, wanted_ifindex);
383 if (nlh->nlmsg_type == RTM_NEWQDISC)
385 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
387 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
391 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
396 if ((tm->tcm_ifindex >= 0)
397 && ((size_t) tm->tcm_ifindex >= iflist_len))
399 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
400 ">= iflist_len = %zu",
401 tm->tcm_ifindex, iflist_len);
405 dev = iflist[msg->tcm_ifindex];
408 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
413 mnl_attr_for_each (attr, nlh, sizeof (*tm))
415 if (mnl_attr_get_type(attr) != TCA_KIND)
418 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
420 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
424 kind = mnl_attr_get_str(attr);
430 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
435 uint32_t numberic_id;
437 numberic_id = tm->tcm_handle;
438 if (strcmp (tc_type, "filter") == 0)
439 numberic_id = tm->tcm_parent;
441 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
444 numberic_id & 0x0000FFFF);
447 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
448 tc_type, dev, msg->tcm_ifindex);
450 if (check_ignorelist (dev, tc_type, tc_inst))
454 mnl_attr_for_each (attr, nlh, sizeof (*tm))
456 if (mnl_attr_get_type(attr) != TCA_STATS2)
459 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
461 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
465 mnl_attr_parse_nested(attr, qos_attr_cb, &bs);
472 char type_instance[DATA_MAX_NAME_LEN];
474 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
477 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
478 submit_one (dev, "ipt_packets", type_instance, bs->packets);
480 #endif /* TCA_STATS2 */
483 mnl_attr_for_each (attr, nlh, sizeof (*tm))
485 if (mnl_attr_get_type(attr) != TCA_STATS)
488 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
490 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
493 ts = mnl_attr_get_payload(attr);
497 if (bs == NULL && ts != NULL)
499 char type_instance[DATA_MAX_NAME_LEN];
501 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
504 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
505 submit_one (dev, "ipt_packets", type_instance, ts->packets);
507 #endif /* TCA_STATS */
509 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
510 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
515 } /* int qos_filter_cb */
517 static int ir_config (const char *key, const char *value)
524 new_val = strdup (value);
528 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
529 if ((fields_num < 1) || (fields_num > 8))
535 if ((strcasecmp (key, "Interface") == 0)
536 || (strcasecmp (key, "VerboseInterface") == 0))
540 ERROR ("netlink plugin: Invalid number of fields for option "
541 "`%s'. Got %i, expected 1.", key, fields_num);
546 add_ignorelist (fields[0], "interface", NULL);
547 if (strcasecmp (key, "VerboseInterface") == 0)
548 add_ignorelist (fields[0], "if_detail", NULL);
552 else if ((strcasecmp (key, "QDisc") == 0)
553 || (strcasecmp (key, "Class") == 0)
554 || (strcasecmp (key, "Filter") == 0))
556 if ((fields_num < 1) || (fields_num > 2))
558 ERROR ("netlink plugin: Invalid number of fields for option "
559 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
564 add_ignorelist (fields[0], key,
565 (fields_num == 2) ? fields[1] : NULL);
569 else if (strcasecmp (key, "IgnoreSelected") == 0)
573 ERROR ("netlink plugin: Invalid number of fields for option "
574 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
579 if (IS_TRUE (fields[0]))
580 ir_ignorelist_invert = 0;
582 ir_ignorelist_invert = 1;
590 } /* int ir_config */
592 static int ir_init (void)
594 nl = mnl_socket_open(NETLINK_ROUTE);
597 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
601 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
603 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
610 static int ir_read (void)
612 char buf[MNL_SOCKET_BUFFER_SIZE];
613 struct nlmsghdr *nlh;
616 unsigned int seq, portid;
618 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
619 static const char *type_name[] = { "qdisc", "class", "filter" };
621 portid = mnl_socket_get_portid(nl);
623 nlh = mnl_nlmsg_put_header (buf);
624 nlh->nlmsg_type = RTM_GETLINK;
625 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
626 nlh->nlmsg_seq = seq = time (NULL);
627 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
628 rt->rtgen_family = AF_PACKET;
630 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
632 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
636 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
639 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
640 if (ret <= MNL_CB_STOP)
642 ret = mnl_socket_recvfrom (nl, buf, sizeof(buf));
646 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
650 /* `link_filter_cb' will update `iflist' which is used here to iterate
651 * over all interfaces. */
652 for (ifindex = 0; (size_t) ifindex < iflist_len; ifindex++)
658 if (iflist[ifindex] == NULL)
661 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
663 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
665 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
666 "== TRUE", iflist[ifindex], type_name[type_index]);
670 DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
671 type_name[type_index], iflist[ifindex], ifindex);
673 nlh = mnl_nlmsg_put_header (buf);
674 nlh->nlmsg_type = type_id[type_index];
675 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
676 nlh->nlmsg_seq = seq = time (NULL);
677 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
678 tm->tcm_family = AF_PACKET;
679 tm->tcm_ifindex = ifindex;
681 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
683 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
687 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
690 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
691 if (ret <= MNL_CB_STOP)
693 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
697 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
701 } /* for (type_index) */
702 } /* for (if_index) */
707 static int ir_shutdown (void)
711 mnl_socket_close(nl);
716 } /* int ir_shutdown */
718 void module_register (void)
720 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
721 plugin_register_init ("netlink", ir_init);
722 plugin_register_read ("netlink", ir_read);
723 plugin_register_shutdown ("netlink", ir_shutdown);
724 } /* void module_register */
727 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :