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>
33 #include <asm/types.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 struct ir_link_stats_storage_s {
60 uint64_t rx_length_errors;
61 uint64_t rx_over_errors;
62 uint64_t rx_crc_errors;
63 uint64_t rx_frame_errors;
64 uint64_t rx_fifo_errors;
65 uint64_t rx_missed_errors;
67 uint64_t tx_aborted_errors;
68 uint64_t tx_carrier_errors;
69 uint64_t tx_fifo_errors;
70 uint64_t tx_heartbeat_errors;
71 uint64_t tx_window_errors;
74 union ir_link_stats_u {
75 struct rtnl_link_stats *stats32;
76 #ifdef HAVE_RTNL_LINK_STATS64
77 struct rtnl_link_stats64 *stats64;
81 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[] = {"Interface", "VerboseInterface",
98 "Filter", "IgnoreSelected"};
99 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
101 static int add_ignorelist(const char *dev, const char *type, const char *inst) {
102 ir_ignorelist_t *entry;
104 entry = calloc(1, sizeof(*entry));
108 if (strcasecmp(dev, "All") != 0) {
109 entry->device = strdup(dev);
110 if (entry->device == NULL) {
116 entry->type = strdup(type);
117 if (entry->type == NULL) {
118 sfree(entry->device);
124 entry->inst = strdup(inst);
125 if (entry->inst == NULL) {
127 sfree(entry->device);
133 entry->next = ir_ignorelist_head;
134 ir_ignorelist_head = entry;
137 } /* int add_ignorelist */
140 * Checks wether a data set should be ignored. Returns `true' is the value
141 * should be ignored, `false' otherwise.
143 static int check_ignorelist(const char *dev, const char *type,
144 const char *type_instance) {
145 assert((dev != NULL) && (type != NULL));
147 if (ir_ignorelist_head == NULL)
148 return (ir_ignorelist_invert ? 0 : 1);
150 for (ir_ignorelist_t *i = ir_ignorelist_head; i != NULL; i = i->next) {
151 /* i->device == NULL => match all devices */
152 if ((i->device != NULL) && (strcasecmp(i->device, dev) != 0))
155 if (strcasecmp(i->type, type) != 0)
158 if ((i->inst != NULL) && (type_instance != NULL) &&
159 (strcasecmp(i->inst, type_instance) != 0))
162 DEBUG("netlink plugin: check_ignorelist: "
163 "(dev = %s; type = %s; inst = %s) matched "
164 "(dev = %s; type = %s; inst = %s)",
165 dev, type, type_instance == NULL ? "(nil)" : type_instance,
166 i->device == NULL ? "(nil)" : i->device, i->type,
167 i->inst == NULL ? "(nil)" : i->inst);
169 return (ir_ignorelist_invert ? 0 : 1);
172 return (ir_ignorelist_invert);
173 } /* int check_ignorelist */
175 static void submit_one(const char *dev, const char *type,
176 const char *type_instance, derive_t value) {
177 value_list_t vl = VALUE_LIST_INIT;
179 vl.values = &(value_t){.derive = value};
181 sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
182 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
183 sstrncpy(vl.type, type, sizeof(vl.type));
185 if (type_instance != NULL)
186 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
188 plugin_dispatch_values(&vl);
189 } /* void submit_one */
191 static void submit_two(const char *dev, const char *type,
192 const char *type_instance, derive_t rx, derive_t tx) {
193 value_list_t vl = VALUE_LIST_INIT;
195 {.derive = rx}, {.derive = tx},
199 vl.values_len = STATIC_ARRAY_SIZE(values);
200 sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
201 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
202 sstrncpy(vl.type, type, sizeof(vl.type));
204 if (type_instance != NULL)
205 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
207 plugin_dispatch_values(&vl);
208 } /* void submit_two */
210 static int update_iflist(struct ifinfomsg *msg, const char *dev) {
211 /* Update the `iflist'. It's used to know which interfaces exist and query
212 * them later for qdiscs and classes. */
213 if ((msg->ifi_index >= 0) && ((size_t)msg->ifi_index >= iflist_len)) {
216 temp = 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)) {
229 sfree(iflist[msg->ifi_index]);
230 iflist[msg->ifi_index] = strdup(dev);
234 } /* int update_iflist */
236 static void check_ignorelist_and_submit(const char *dev,
237 struct ir_link_stats_storage_s *stats) {
239 if (check_ignorelist(dev, "interface", NULL) == 0) {
240 submit_two(dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
241 submit_two(dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
242 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) {
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);
265 DEBUG("netlink plugin: Ignoring %s/if_detail.", dev);
268 } /* void check_ignorelist_and_submit */
270 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
271 (dst_stats)->value_name = (src_stats)->value_name
273 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats) \
274 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_packets); \
275 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_packets); \
276 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_bytes); \
277 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_bytes); \
278 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_errors); \
279 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_errors); \
280 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_dropped); \
281 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_dropped); \
282 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, multicast); \
283 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, collisions); \
284 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_length_errors); \
285 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_over_errors); \
286 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_crc_errors); \
287 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_frame_errors); \
288 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_fifo_errors); \
289 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_missed_errors); \
290 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_aborted_errors); \
291 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_carrier_errors); \
292 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_fifo_errors); \
293 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_heartbeat_errors); \
294 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_window_errors)
296 #ifdef HAVE_RTNL_LINK_STATS64
297 static void check_ignorelist_and_submit64(const char *dev,
298 struct rtnl_link_stats64 *stats) {
299 struct ir_link_stats_storage_s s;
301 COPY_RTNL_LINK_STATS(&s, stats);
303 check_ignorelist_and_submit(dev, &s);
307 static void check_ignorelist_and_submit32(const char *dev,
308 struct rtnl_link_stats *stats) {
309 struct ir_link_stats_storage_s s;
311 COPY_RTNL_LINK_STATS(&s, stats);
313 check_ignorelist_and_submit(dev, &s);
316 static int link_filter_cb(const struct nlmsghdr *nlh,
317 void *args __attribute__((unused))) {
318 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
320 const char *dev = NULL;
321 union ir_link_stats_u stats;
323 if (nlh->nlmsg_type != RTM_NEWLINK) {
324 ERROR("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
329 /* Scan attribute list for device name. */
330 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
331 if (mnl_attr_get_type(attr) != IFLA_IFNAME)
334 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
335 ERROR("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate "
340 dev = mnl_attr_get_str(attr);
341 if (update_iflist(ifm, dev) < 0)
347 ERROR("netlink plugin: link_filter_cb: dev == NULL");
350 #ifdef HAVE_RTNL_LINK_STATS64
351 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
352 if (mnl_attr_get_type(attr) != IFLA_STATS64)
355 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats64)) < 0) {
356 ERROR("netlink plugin: link_filter_cb: IFLA_STATS64 mnl_attr_validate2 "
360 stats.stats64 = mnl_attr_get_payload(attr);
362 check_ignorelist_and_submit64(dev, stats.stats64);
367 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
368 if (mnl_attr_get_type(attr) != IFLA_STATS)
371 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*stats.stats32)) < 0) {
372 ERROR("netlink plugin: link_filter_cb: IFLA_STATS mnl_attr_validate2 "
376 stats.stats32 = mnl_attr_get_payload(attr);
378 check_ignorelist_and_submit32(dev, stats.stats32);
383 DEBUG("netlink plugin: link_filter: No statistics for interface %s.", dev);
386 } /* int link_filter_cb */
389 static int qos_attr_cb(const struct nlattr *attr, void *data) {
390 struct gnet_stats_basic **bs = (struct gnet_stats_basic **)data;
392 /* skip unsupported attribute in user-space */
393 if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
396 if (mnl_attr_get_type(attr) == TCA_STATS_BASIC) {
397 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(**bs)) < 0) {
398 ERROR("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 "
402 *bs = mnl_attr_get_payload(attr);
410 static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
411 struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
414 int wanted_ifindex = *((int *)args);
417 const char *kind = NULL;
419 /* char *type_instance; */
421 char tc_inst[DATA_MAX_NAME_LEN];
423 _Bool stats_submitted = 0;
425 if (nlh->nlmsg_type == RTM_NEWQDISC)
427 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
429 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
432 ERROR("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
437 if (tm->tcm_ifindex != wanted_ifindex) {
438 DEBUG("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
440 tc_type, tm->tcm_ifindex, wanted_ifindex);
444 if ((tm->tcm_ifindex >= 0) && ((size_t)tm->tcm_ifindex >= iflist_len)) {
445 ERROR("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
446 ">= iflist_len = %zu",
447 tm->tcm_ifindex, iflist_len);
451 dev = iflist[tm->tcm_ifindex];
453 ERROR("netlink plugin: qos_filter_cb: iflist[%i] == NULL", tm->tcm_ifindex);
457 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
458 if (mnl_attr_get_type(attr) != TCA_KIND)
461 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
463 "netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
467 kind = mnl_attr_get_str(attr);
472 ERROR("netlink plugin: qos_filter_cb: kind == NULL");
477 uint32_t numberic_id;
479 numberic_id = tm->tcm_handle;
480 if (strcmp(tc_type, "filter") == 0)
481 numberic_id = tm->tcm_parent;
483 ssnprintf(tc_inst, sizeof(tc_inst), "%s-%x:%x", kind, numberic_id >> 16,
484 numberic_id & 0x0000FFFF);
487 DEBUG("netlink plugin: qos_filter_cb: got %s for %s (%i).", tc_type, dev,
490 if (check_ignorelist(dev, tc_type, tc_inst))
494 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
495 struct gnet_stats_basic *bs = NULL;
497 if (mnl_attr_get_type(attr) != TCA_STATS2)
500 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
501 ERROR("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate "
506 mnl_attr_parse_nested(attr, qos_attr_cb, &bs);
509 char type_instance[DATA_MAX_NAME_LEN];
513 ssnprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
516 submit_one(dev, "ipt_bytes", type_instance, bs->bytes);
517 submit_one(dev, "ipt_packets", type_instance, bs->packets);
522 #endif /* TCA_STATS2 */
525 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
526 struct tc_stats *ts = NULL;
528 if (mnl_attr_get_type(attr) != TCA_STATS)
531 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*ts)) < 0) {
532 ERROR("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 "
536 ts = mnl_attr_get_payload(attr);
538 if (!stats_submitted && ts != NULL) {
539 char type_instance[DATA_MAX_NAME_LEN];
541 ssnprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
544 submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
545 submit_one(dev, "ipt_packets", type_instance, ts->packets);
551 #endif /* TCA_STATS */
553 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
554 DEBUG("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
559 } /* int qos_filter_cb */
561 static int ir_config(const char *key, const char *value) {
567 new_val = strdup(value);
571 fields_num = strsplit(new_val, fields, STATIC_ARRAY_SIZE(fields));
572 if ((fields_num < 1) || (fields_num > 8)) {
577 if ((strcasecmp(key, "Interface") == 0) ||
578 (strcasecmp(key, "VerboseInterface") == 0)) {
579 if (fields_num != 1) {
580 ERROR("netlink plugin: Invalid number of fields for option "
581 "`%s'. Got %i, expected 1.",
585 add_ignorelist(fields[0], "interface", NULL);
586 if (strcasecmp(key, "VerboseInterface") == 0)
587 add_ignorelist(fields[0], "if_detail", NULL);
590 } else if ((strcasecmp(key, "QDisc") == 0) ||
591 (strcasecmp(key, "Class") == 0) ||
592 (strcasecmp(key, "Filter") == 0)) {
593 if ((fields_num < 1) || (fields_num > 2)) {
594 ERROR("netlink plugin: Invalid number of fields for option "
595 "`%s'. Got %i, expected 1 or 2.",
599 add_ignorelist(fields[0], key, (fields_num == 2) ? fields[1] : NULL);
602 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
603 if (fields_num != 1) {
604 ERROR("netlink plugin: Invalid number of fields for option "
605 "`IgnoreSelected'. Got %i, expected 1.",
609 if (IS_TRUE(fields[0]))
610 ir_ignorelist_invert = 0;
612 ir_ignorelist_invert = 1;
620 } /* int ir_config */
622 static int ir_init(void) {
623 nl = mnl_socket_open(NETLINK_ROUTE);
625 ERROR("netlink plugin: ir_init: mnl_socket_open failed.");
629 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
630 ERROR("netlink plugin: ir_init: mnl_socket_bind failed.");
637 static int ir_read(void) {
638 char buf[MNL_SOCKET_BUFFER_SIZE];
639 struct nlmsghdr *nlh;
642 unsigned int seq, portid;
644 static const int type_id[] = {RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER};
645 static const char *type_name[] = {"qdisc", "class", "filter"};
647 portid = mnl_socket_get_portid(nl);
649 nlh = mnl_nlmsg_put_header(buf);
650 nlh->nlmsg_type = RTM_GETLINK;
651 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
652 nlh->nlmsg_seq = seq = time(NULL);
653 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(*rt));
654 rt->rtgen_family = AF_PACKET;
656 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
657 ERROR("netlink plugin: ir_read: rtnl_wilddump_request failed.");
661 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
663 ret = mnl_cb_run(buf, ret, seq, portid, link_filter_cb, NULL);
664 if (ret <= MNL_CB_STOP)
666 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
669 ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed.");
673 /* `link_filter_cb' will update `iflist' which is used here to iterate
674 * over all interfaces. */
675 for (size_t ifindex = 1; ifindex < iflist_len; ifindex++) {
678 if (iflist[ifindex] == NULL)
681 for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE(type_id);
683 if (check_ignorelist(iflist[ifindex], type_name[type_index], NULL)) {
684 DEBUG("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
686 iflist[ifindex], type_name[type_index]);
690 DEBUG("netlink plugin: ir_read: querying %s from %s (%zu).",
691 type_name[type_index], iflist[ifindex], ifindex);
693 nlh = mnl_nlmsg_put_header(buf);
694 nlh->nlmsg_type = type_id[type_index];
695 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
696 nlh->nlmsg_seq = seq = time(NULL);
697 tm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tm));
698 tm->tcm_family = AF_PACKET;
699 tm->tcm_ifindex = ifindex;
701 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
702 ERROR("netlink plugin: ir_read: mnl_socket_sendto failed.");
706 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
708 ret = mnl_cb_run(buf, ret, seq, portid, qos_filter_cb, &ifindex);
709 if (ret <= MNL_CB_STOP)
711 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
714 ERROR("netlink plugin: ir_read:mnl_socket_recvfrom failed.");
718 } /* for (type_index) */
719 } /* for (if_index) */
724 static int ir_shutdown(void) {
726 mnl_socket_close(nl);
731 } /* int ir_shutdown */
733 void module_register(void) {
734 plugin_register_config("netlink", ir_config, config_keys, config_keys_num);
735 plugin_register_init("netlink", ir_init);
736 plugin_register_read("netlink", ir_read);
737 plugin_register_shutdown("netlink", ir_shutdown);
738 } /* void module_register */