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>
38 #include <libmnl/libmnl.h>
40 typedef struct ir_ignorelist_s
45 struct ir_ignorelist_s *next;
48 static int ir_ignorelist_invert = 1;
49 static ir_ignorelist_t *ir_ignorelist_head = NULL;
51 static struct mnl_socket *nl;
53 static char **iflist = NULL;
54 static size_t iflist_len = 0;
56 static const char *config_keys[] =
65 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
67 static int add_ignorelist (const char *dev, const char *type,
70 ir_ignorelist_t *entry;
72 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
76 memset (entry, '\0', sizeof (ir_ignorelist_t));
78 if (strcasecmp (dev, "All") != 0)
80 entry->device = strdup (dev);
81 if (entry->device == NULL)
88 entry->type = strdup (type);
89 if (entry->type == NULL)
91 sfree (entry->device);
98 entry->inst = strdup (inst);
99 if (entry->inst == NULL)
102 sfree (entry->device);
108 entry->next = ir_ignorelist_head;
109 ir_ignorelist_head = entry;
112 } /* int add_ignorelist */
115 * Checks wether a data set should be ignored. Returns `true' is the value
116 * should be ignored, `false' otherwise.
118 static int check_ignorelist (const char *dev,
119 const char *type, const char *type_instance)
123 assert ((dev != NULL) && (type != NULL));
125 if (ir_ignorelist_head == NULL)
126 return (ir_ignorelist_invert ? 0 : 1);
128 for (i = ir_ignorelist_head; i != NULL; i = i->next)
130 /* i->device == NULL => match all devices */
131 if ((i->device != NULL)
132 && (strcasecmp (i->device, dev) != 0))
135 if (strcasecmp (i->type, type) != 0)
138 if ((i->inst != NULL) && (type_instance != NULL)
139 && (strcasecmp (i->inst, type_instance) != 0))
142 DEBUG ("netlink plugin: check_ignorelist: "
143 "(dev = %s; type = %s; inst = %s) matched "
144 "(dev = %s; type = %s; inst = %s)",
146 type_instance == NULL ? "(nil)" : type_instance,
147 i->device == NULL ? "(nil)" : i->device,
149 i->inst == NULL ? "(nil)" : i->inst);
151 return (ir_ignorelist_invert ? 0 : 1);
154 return (ir_ignorelist_invert);
155 } /* int check_ignorelist */
157 static void submit_one (const char *dev, const char *type,
158 const char *type_instance, derive_t value)
161 value_list_t vl = VALUE_LIST_INIT;
163 values[0].derive = value;
167 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
168 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
169 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
170 sstrncpy (vl.type, type, sizeof (vl.type));
172 if (type_instance != NULL)
173 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
175 plugin_dispatch_values (&vl);
176 } /* void submit_one */
178 static void submit_two (const char *dev, const char *type,
179 const char *type_instance,
180 derive_t rx, derive_t tx)
183 value_list_t vl = VALUE_LIST_INIT;
185 values[0].derive = rx;
186 values[1].derive = tx;
190 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
191 sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
192 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
193 sstrncpy (vl.type, type, sizeof (vl.type));
195 if (type_instance != NULL)
196 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
198 plugin_dispatch_values (&vl);
199 } /* void submit_two */
201 static int update_iflist (struct ifinfomsg *msg, const char *dev)
203 /* Update the `iflist'. It's used to know which interfaces exist and query
204 * them later for qdiscs and classes. */
205 if ((msg->ifi_index >= 0) && ((size_t) msg->ifi_index >= iflist_len))
209 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
212 ERROR ("netlink plugin: update_iflist: realloc failed.");
216 memset (temp + iflist_len, '\0',
217 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
219 iflist_len = msg->ifi_index + 1;
221 if ((iflist[msg->ifi_index] == NULL)
222 || (strcmp (iflist[msg->ifi_index], dev) != 0))
224 sfree (iflist[msg->ifi_index]);
225 iflist[msg->ifi_index] = strdup (dev);
229 } /* int update_iflist */
231 static void check_ignorelist_and_submit (const char *dev,
232 struct rtnl_link_stats *stats)
235 if (check_ignorelist (dev, "interface", NULL) == 0)
237 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
238 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
239 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
243 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
246 if (check_ignorelist (dev, "if_detail", NULL) == 0)
248 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
249 submit_one (dev, "if_multicast", NULL, stats->multicast);
250 submit_one (dev, "if_collisions", NULL, stats->collisions);
252 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
253 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
254 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
255 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
256 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
257 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
259 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
260 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
261 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
262 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
263 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
267 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
270 } /* void check_ignorelist_and_submit */
272 static int link_filter_cb (const struct nlmsghdr *nlh,
273 void *args __attribute__((unused)))
275 struct ifinfomsg *ifm = mnl_nlmsg_get_payload (nlh);
277 struct rtnl_link_stats *stats = NULL;
278 const char *dev = NULL;
280 if (nlh->nlmsg_type != RTM_NEWLINK)
282 ERROR ("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
287 /* Scan attribute list for device name. */
288 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
290 if (mnl_attr_get_type (attr) != IFLA_IFNAME)
293 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
295 ERROR ("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate failed.");
299 dev = mnl_attr_get_str (attr);
300 if (update_iflist (ifm, dev) < 0)
307 ERROR ("netlink plugin: link_filter_cb: dev == NULL");
311 mnl_attr_for_each (attr, nlh, sizeof (*ifm))
313 if (mnl_attr_get_type (attr) != IFLA_STATS)
316 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*stats)) < 0)
318 ERROR ("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 failed.");
321 stats = mnl_attr_get_payload (attr);
323 check_ignorelist_and_submit (dev, stats);
329 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
334 } /* int link_filter_cb */
337 static int qos_attr_cb (const struct nlattr *attr, void *data)
339 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
341 /* skip unsupported attribute in user-space */
342 if (mnl_attr_type_valid (attr, TCA_STATS_MAX) < 0)
345 if (mnl_attr_get_type (attr) == TCA_STATS_BASIC)
347 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (**bs)) < 0)
349 ERROR ("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 failed.");
352 *bs = mnl_attr_get_payload (attr);
360 static int qos_filter_cb (const struct nlmsghdr *nlh, void *args)
362 struct tcmsg *tm = mnl_nlmsg_get_payload (nlh);
365 int wanted_ifindex = *((int *) args);
368 const char *kind = NULL;
370 /* char *type_instance; */
372 char tc_inst[DATA_MAX_NAME_LEN];
374 _Bool stats_submitted = 0;
376 if (nlh->nlmsg_type == RTM_NEWQDISC)
378 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
380 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
384 ERROR ("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
389 if (tm->tcm_ifindex != wanted_ifindex)
391 DEBUG ("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
393 tc_type, tm->tcm_ifindex, wanted_ifindex);
397 if ((tm->tcm_ifindex >= 0)
398 && ((size_t) tm->tcm_ifindex >= iflist_len))
400 ERROR ("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
401 ">= iflist_len = %zu",
402 tm->tcm_ifindex, iflist_len);
406 dev = iflist[tm->tcm_ifindex];
409 ERROR ("netlink plugin: qos_filter_cb: iflist[%i] == NULL",
414 mnl_attr_for_each (attr, nlh, sizeof (*tm))
416 if (mnl_attr_get_type (attr) != TCA_KIND)
419 if (mnl_attr_validate (attr, MNL_TYPE_STRING) < 0)
421 ERROR ("netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
425 kind = mnl_attr_get_str (attr);
431 ERROR ("netlink plugin: qos_filter_cb: kind == NULL");
436 uint32_t numberic_id;
438 numberic_id = tm->tcm_handle;
439 if (strcmp (tc_type, "filter") == 0)
440 numberic_id = tm->tcm_parent;
442 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
445 numberic_id & 0x0000FFFF);
448 DEBUG ("netlink plugin: qos_filter_cb: got %s for %s (%i).",
449 tc_type, dev, tm->tcm_ifindex);
451 if (check_ignorelist (dev, tc_type, tc_inst))
455 mnl_attr_for_each (attr, nlh, sizeof (*tm))
457 struct gnet_stats_basic *bs = NULL;
459 if (mnl_attr_get_type (attr) != TCA_STATS2)
462 if (mnl_attr_validate (attr, MNL_TYPE_NESTED) < 0)
464 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate failed.");
468 mnl_attr_parse_nested (attr, qos_attr_cb, &bs);
472 char type_instance[DATA_MAX_NAME_LEN];
476 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
479 submit_one (dev, "ipt_bytes", type_instance, bs->bytes);
480 submit_one (dev, "ipt_packets", type_instance, bs->packets);
485 #endif /* TCA_STATS2 */
488 mnl_attr_for_each (attr, nlh, sizeof (*tm))
490 struct tc_stats *ts = NULL;
492 if (mnl_attr_get_type (attr) != TCA_STATS)
495 if (mnl_attr_validate2 (attr, MNL_TYPE_UNSPEC, sizeof (*ts)) < 0)
497 ERROR ("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 failed.");
500 ts = mnl_attr_get_payload (attr);
502 if (!stats_submitted && ts != NULL)
504 char type_instance[DATA_MAX_NAME_LEN];
506 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
509 submit_one (dev, "ipt_bytes", type_instance, ts->bytes);
510 submit_one (dev, "ipt_packets", type_instance, ts->packets);
516 #endif /* TCA_STATS */
518 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
519 DEBUG ("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
524 } /* int qos_filter_cb */
526 static int ir_config (const char *key, const char *value)
533 new_val = strdup (value);
537 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
538 if ((fields_num < 1) || (fields_num > 8))
544 if ((strcasecmp (key, "Interface") == 0)
545 || (strcasecmp (key, "VerboseInterface") == 0))
549 ERROR ("netlink plugin: Invalid number of fields for option "
550 "`%s'. Got %i, expected 1.", key, fields_num);
555 add_ignorelist (fields[0], "interface", NULL);
556 if (strcasecmp (key, "VerboseInterface") == 0)
557 add_ignorelist (fields[0], "if_detail", NULL);
561 else if ((strcasecmp (key, "QDisc") == 0)
562 || (strcasecmp (key, "Class") == 0)
563 || (strcasecmp (key, "Filter") == 0))
565 if ((fields_num < 1) || (fields_num > 2))
567 ERROR ("netlink plugin: Invalid number of fields for option "
568 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
573 add_ignorelist (fields[0], key,
574 (fields_num == 2) ? fields[1] : NULL);
578 else if (strcasecmp (key, "IgnoreSelected") == 0)
582 ERROR ("netlink plugin: Invalid number of fields for option "
583 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
588 if (IS_TRUE (fields[0]))
589 ir_ignorelist_invert = 0;
591 ir_ignorelist_invert = 1;
599 } /* int ir_config */
601 static int ir_init (void)
603 nl = mnl_socket_open (NETLINK_ROUTE);
606 ERROR ("netlink plugin: ir_init: mnl_socket_open failed.");
610 if (mnl_socket_bind (nl, 0, MNL_SOCKET_AUTOPID) < 0)
612 ERROR ("netlink plugin: ir_init: mnl_socket_bind failed.");
619 static int ir_read (void)
621 char buf[MNL_SOCKET_BUFFER_SIZE];
622 struct nlmsghdr *nlh;
625 unsigned int seq, portid;
629 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
630 static const char *type_name[] = { "qdisc", "class", "filter" };
632 portid = mnl_socket_get_portid (nl);
634 nlh = mnl_nlmsg_put_header (buf);
635 nlh->nlmsg_type = RTM_GETLINK;
636 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
637 nlh->nlmsg_seq = seq = time (NULL);
638 rt = mnl_nlmsg_put_extra_header (nlh, sizeof (*rt));
639 rt->rtgen_family = AF_PACKET;
641 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
643 ERROR ("netlink plugin: ir_read: rtnl_wilddump_request failed.");
647 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
650 ret = mnl_cb_run (buf, ret, seq, portid, link_filter_cb, NULL);
651 if (ret <= MNL_CB_STOP)
653 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
657 ERROR ("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
661 /* `link_filter_cb' will update `iflist' which is used here to iterate
662 * over all interfaces. */
663 for (ifindex = 1; ifindex < iflist_len; ifindex++)
668 if (iflist[ifindex] == NULL)
671 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
673 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
675 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
676 "== TRUE", iflist[ifindex], type_name[type_index]);
680 DEBUG ("netlink plugin: ir_read: querying %s from %s (%lu).",
681 type_name[type_index], iflist[ifindex], ifindex);
683 nlh = mnl_nlmsg_put_header (buf);
684 nlh->nlmsg_type = type_id[type_index];
685 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
686 nlh->nlmsg_seq = seq = time (NULL);
687 tm = mnl_nlmsg_put_extra_header (nlh, sizeof (*tm));
688 tm->tcm_family = AF_PACKET;
689 tm->tcm_ifindex = ifindex;
691 if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) < 0)
693 ERROR ("netlink plugin: ir_read: mnl_socket_sendto failed.");
697 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
700 ret = mnl_cb_run (buf, ret, seq, portid, qos_filter_cb, &ifindex);
701 if (ret <= MNL_CB_STOP)
703 ret = mnl_socket_recvfrom (nl, buf, sizeof (buf));
707 ERROR ("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
711 } /* for (type_index) */
712 } /* for (if_index) */
717 static int ir_shutdown (void)
721 mnl_socket_close (nl);
726 } /* int ir_shutdown */
728 void module_register (void)
730 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
731 plugin_register_init ("netlink", ir_init);
732 plugin_register_read ("netlink", ir_read);
733 plugin_register_shutdown ("netlink", ir_shutdown);
734 } /* void module_register */
737 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :