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 {
59 uint64_t rx_nohandler;
61 uint64_t rx_length_errors;
62 uint64_t rx_over_errors;
63 uint64_t rx_crc_errors;
64 uint64_t rx_frame_errors;
65 uint64_t rx_fifo_errors;
66 uint64_t rx_missed_errors;
68 uint64_t tx_aborted_errors;
69 uint64_t tx_carrier_errors;
70 uint64_t tx_fifo_errors;
71 uint64_t tx_heartbeat_errors;
72 uint64_t tx_window_errors;
75 union ir_link_stats_u {
76 struct rtnl_link_stats *stats32;
77 #ifdef HAVE_RTNL_LINK_STATS64
78 struct rtnl_link_stats64 *stats64;
82 typedef struct ir_ignorelist_s {
86 struct ir_ignorelist_s *next;
90 struct gnet_stats_basic *bs;
91 struct gnet_stats_queue *qs;
94 static int ir_ignorelist_invert = 1;
95 static ir_ignorelist_t *ir_ignorelist_head;
97 static struct mnl_socket *nl;
100 static size_t iflist_len;
102 static const char *config_keys[] = {"Interface", "VerboseInterface",
104 "Filter", "IgnoreSelected"};
105 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
107 static int add_ignorelist(const char *dev, const char *type, const char *inst) {
108 ir_ignorelist_t *entry;
110 entry = calloc(1, sizeof(*entry));
114 if (strcasecmp(dev, "All") != 0) {
115 entry->device = strdup(dev);
116 if (entry->device == NULL) {
122 entry->type = strdup(type);
123 if (entry->type == NULL) {
124 sfree(entry->device);
130 entry->inst = strdup(inst);
131 if (entry->inst == NULL) {
133 sfree(entry->device);
139 entry->next = ir_ignorelist_head;
140 ir_ignorelist_head = entry;
143 } /* int add_ignorelist */
146 * Checks wether a data set should be ignored. Returns `true' is the value
147 * should be ignored, `false' otherwise.
149 static int check_ignorelist(const char *dev, const char *type,
150 const char *type_instance) {
151 assert((dev != NULL) && (type != NULL));
153 if (ir_ignorelist_head == NULL)
154 return ir_ignorelist_invert ? 0 : 1;
156 for (ir_ignorelist_t *i = ir_ignorelist_head; i != NULL; i = i->next) {
157 /* i->device == NULL => match all devices */
158 if ((i->device != NULL) && (strcasecmp(i->device, dev) != 0))
161 if (strcasecmp(i->type, type) != 0)
164 if ((i->inst != NULL) && (type_instance != NULL) &&
165 (strcasecmp(i->inst, type_instance) != 0))
168 DEBUG("netlink plugin: check_ignorelist: "
169 "(dev = %s; type = %s; inst = %s) matched "
170 "(dev = %s; type = %s; inst = %s)",
171 dev, type, type_instance == NULL ? "(nil)" : type_instance,
172 i->device == NULL ? "(nil)" : i->device, i->type,
173 i->inst == NULL ? "(nil)" : i->inst);
175 return ir_ignorelist_invert ? 0 : 1;
178 return ir_ignorelist_invert;
179 } /* int check_ignorelist */
181 static void submit_one(const char *dev, const char *type,
182 const char *type_instance, derive_t value) {
183 value_list_t vl = VALUE_LIST_INIT;
185 vl.values = &(value_t){.derive = value};
187 sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
188 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
189 sstrncpy(vl.type, type, sizeof(vl.type));
191 if (type_instance != NULL)
192 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
194 plugin_dispatch_values(&vl);
195 } /* void submit_one */
197 static void submit_two(const char *dev, const char *type,
198 const char *type_instance, derive_t rx, derive_t tx) {
199 value_list_t vl = VALUE_LIST_INIT;
201 {.derive = rx}, {.derive = tx},
205 vl.values_len = STATIC_ARRAY_SIZE(values);
206 sstrncpy(vl.plugin, "netlink", sizeof(vl.plugin));
207 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
208 sstrncpy(vl.type, type, sizeof(vl.type));
210 if (type_instance != NULL)
211 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
213 plugin_dispatch_values(&vl);
214 } /* void submit_two */
216 static int update_iflist(struct ifinfomsg *msg, const char *dev) {
217 /* Update the `iflist'. It's used to know which interfaces exist and query
218 * them later for qdiscs and classes. */
219 if ((msg->ifi_index >= 0) && ((size_t)msg->ifi_index >= iflist_len)) {
222 temp = realloc(iflist, (msg->ifi_index + 1) * sizeof(char *));
224 ERROR("netlink plugin: update_iflist: realloc failed.");
228 memset(temp + iflist_len, '\0',
229 (msg->ifi_index + 1 - iflist_len) * sizeof(char *));
231 iflist_len = msg->ifi_index + 1;
233 if ((iflist[msg->ifi_index] == NULL) ||
234 (strcmp(iflist[msg->ifi_index], dev) != 0)) {
235 sfree(iflist[msg->ifi_index]);
236 iflist[msg->ifi_index] = strdup(dev);
240 } /* int update_iflist */
242 static void check_ignorelist_and_submit(const char *dev,
243 struct ir_link_stats_storage_s *stats) {
245 if (check_ignorelist(dev, "interface", NULL) == 0) {
246 submit_two(dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
247 submit_two(dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
248 submit_two(dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
250 DEBUG("netlink plugin: Ignoring %s/interface.", dev);
253 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);
257 #if defined(HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER) || \
258 defined(HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER)
259 submit_one(dev, "if_rx_nohandler", NULL, stats->rx_nohandler);
262 submit_one(dev, "if_rx_errors", "length", stats->rx_length_errors);
263 submit_one(dev, "if_rx_errors", "over", stats->rx_over_errors);
264 submit_one(dev, "if_rx_errors", "crc", stats->rx_crc_errors);
265 submit_one(dev, "if_rx_errors", "frame", stats->rx_frame_errors);
266 submit_one(dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
267 submit_one(dev, "if_rx_errors", "missed", stats->rx_missed_errors);
269 submit_one(dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
270 submit_one(dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
271 submit_one(dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
272 submit_one(dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
273 submit_one(dev, "if_tx_errors", "window", stats->tx_window_errors);
275 DEBUG("netlink plugin: Ignoring %s/if_detail.", dev);
278 } /* void check_ignorelist_and_submit */
280 #define COPY_RTNL_LINK_VALUE(dst_stats, src_stats, value_name) \
281 (dst_stats)->value_name = (src_stats)->value_name
283 #define COPY_RTNL_LINK_STATS(dst_stats, src_stats) \
284 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_packets); \
285 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_packets); \
286 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_bytes); \
287 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_bytes); \
288 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_errors); \
289 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_errors); \
290 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_dropped); \
291 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_dropped); \
292 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, multicast); \
293 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, collisions); \
294 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_length_errors); \
295 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_over_errors); \
296 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_crc_errors); \
297 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_frame_errors); \
298 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_fifo_errors); \
299 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, rx_missed_errors); \
300 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_aborted_errors); \
301 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_carrier_errors); \
302 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_fifo_errors); \
303 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_heartbeat_errors); \
304 COPY_RTNL_LINK_VALUE(dst_stats, src_stats, tx_window_errors)
306 #ifdef HAVE_RTNL_LINK_STATS64
307 static void check_ignorelist_and_submit64(const char *dev,
308 struct rtnl_link_stats64 *stats) {
309 struct ir_link_stats_storage_s s;
311 COPY_RTNL_LINK_STATS(&s, stats);
312 #ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER
313 COPY_RTNL_LINK_VALUE(&s, stats, rx_nohandler);
316 check_ignorelist_and_submit(dev, &s);
320 static void check_ignorelist_and_submit32(const char *dev,
321 struct rtnl_link_stats *stats) {
322 struct ir_link_stats_storage_s s;
324 COPY_RTNL_LINK_STATS(&s, stats);
325 #ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER
326 COPY_RTNL_LINK_VALUE(&s, stats, rx_nohandler);
329 check_ignorelist_and_submit(dev, &s);
332 static int link_filter_cb(const struct nlmsghdr *nlh,
333 void *args __attribute__((unused))) {
334 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
336 const char *dev = NULL;
337 union ir_link_stats_u stats;
339 if (nlh->nlmsg_type != RTM_NEWLINK) {
340 ERROR("netlink plugin: link_filter_cb: Don't know how to handle type %i.",
345 /* Scan attribute list for device name. */
346 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
347 if (mnl_attr_get_type(attr) != IFLA_IFNAME)
350 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
351 ERROR("netlink plugin: link_filter_cb: IFLA_IFNAME mnl_attr_validate "
356 dev = mnl_attr_get_str(attr);
357 if (update_iflist(ifm, dev) < 0)
363 ERROR("netlink plugin: link_filter_cb: dev == NULL");
366 #ifdef HAVE_RTNL_LINK_STATS64
367 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
368 if (mnl_attr_get_type(attr) != IFLA_STATS64)
371 uint16_t attr_len = mnl_attr_get_payload_len(attr);
372 if (attr_len < sizeof(*stats.stats64)) {
373 ERROR("netlink plugin: link_filter_cb: IFLA_STATS64 attribute has "
374 "insufficient data.");
377 stats.stats64 = mnl_attr_get_payload(attr);
379 check_ignorelist_and_submit64(dev, stats.stats64);
384 mnl_attr_for_each(attr, nlh, sizeof(*ifm)) {
385 if (mnl_attr_get_type(attr) != IFLA_STATS)
388 uint16_t attr_len = mnl_attr_get_payload_len(attr);
389 if (attr_len < sizeof(*stats.stats32)) {
390 ERROR("netlink plugin: link_filter_cb: IFLA_STATS attribute has "
391 "insufficient data.");
394 stats.stats32 = mnl_attr_get_payload(attr);
396 check_ignorelist_and_submit32(dev, stats.stats32);
401 DEBUG("netlink plugin: link_filter: No statistics for interface %s.", dev);
404 } /* int link_filter_cb */
407 static int qos_attr_cb(const struct nlattr *attr, void *data) {
408 struct qos_stats *q_stats = (struct qos_stats *)data;
410 /* skip unsupported attribute in user-space */
411 if (mnl_attr_type_valid(attr, TCA_STATS_MAX) < 0)
414 if (mnl_attr_get_type(attr) == TCA_STATS_BASIC) {
415 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*q_stats->bs)) < 0) {
416 ERROR("netlink plugin: qos_attr_cb: TCA_STATS_BASIC mnl_attr_validate2 "
421 q_stats->bs = mnl_attr_get_payload(attr);
425 if (mnl_attr_get_type(attr) == TCA_STATS_QUEUE) {
426 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*q_stats->qs)) < 0) {
427 ERROR("netlink plugin: qos_attr_cb: TCA_STATS_QUEUE mnl_attr_validate2 "
431 q_stats->qs = mnl_attr_get_payload(attr);
439 static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
440 struct tcmsg *tm = mnl_nlmsg_get_payload(nlh);
443 int wanted_ifindex = *((int *)args);
446 const char *kind = NULL;
448 /* char *type_instance; */
450 char tc_inst[DATA_MAX_NAME_LEN];
452 bool stats_submitted = false;
454 if (nlh->nlmsg_type == RTM_NEWQDISC)
456 else if (nlh->nlmsg_type == RTM_NEWTCLASS)
458 else if (nlh->nlmsg_type == RTM_NEWTFILTER)
461 ERROR("netlink plugin: qos_filter_cb: Don't know how to handle type %i.",
466 if (tm->tcm_ifindex != wanted_ifindex) {
467 DEBUG("netlink plugin: qos_filter_cb: Got %s for interface #%i, "
469 tc_type, tm->tcm_ifindex, wanted_ifindex);
473 if ((tm->tcm_ifindex >= 0) && ((size_t)tm->tcm_ifindex >= iflist_len)) {
474 ERROR("netlink plugin: qos_filter_cb: tm->tcm_ifindex = %i "
475 ">= iflist_len = %" PRIsz,
476 tm->tcm_ifindex, iflist_len);
480 dev = iflist[tm->tcm_ifindex];
482 ERROR("netlink plugin: qos_filter_cb: iflist[%i] == NULL", tm->tcm_ifindex);
486 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
487 if (mnl_attr_get_type(attr) != TCA_KIND)
490 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
492 "netlink plugin: qos_filter_cb: TCA_KIND mnl_attr_validate failed.");
496 kind = mnl_attr_get_str(attr);
501 ERROR("netlink plugin: qos_filter_cb: kind == NULL");
506 uint32_t numberic_id;
508 numberic_id = tm->tcm_handle;
509 if (strcmp(tc_type, "filter") == 0)
510 numberic_id = tm->tcm_parent;
512 snprintf(tc_inst, sizeof(tc_inst), "%s-%x:%x", kind, numberic_id >> 16,
513 numberic_id & 0x0000FFFF);
516 DEBUG("netlink plugin: qos_filter_cb: got %s for %s (%i).", tc_type, dev,
519 if (check_ignorelist(dev, tc_type, tc_inst))
523 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
524 struct qos_stats q_stats;
526 memset(&q_stats, 0x0, sizeof(q_stats));
528 if (mnl_attr_get_type(attr) != TCA_STATS2)
531 if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
532 ERROR("netlink plugin: qos_filter_cb: TCA_STATS2 mnl_attr_validate "
537 mnl_attr_parse_nested(attr, qos_attr_cb, &q_stats);
539 if (q_stats.bs != NULL || q_stats.qs != NULL) {
540 char type_instance[DATA_MAX_NAME_LEN];
542 stats_submitted = true;
544 int r = snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
546 if ((size_t)r >= sizeof(type_instance)) {
547 ERROR("netlink plugin: type_instance truncated to %zu bytes, need %d",
548 sizeof(type_instance), r);
552 if (q_stats.bs != NULL) {
553 submit_one(dev, "ipt_bytes", type_instance, q_stats.bs->bytes);
554 submit_one(dev, "ipt_packets", type_instance, q_stats.bs->packets);
556 if (q_stats.qs != NULL) {
557 submit_one(dev, "if_tx_dropped", type_instance, q_stats.qs->drops);
563 #endif /* TCA_STATS2 */
566 mnl_attr_for_each(attr, nlh, sizeof(*tm)) {
567 struct tc_stats *ts = NULL;
569 if (mnl_attr_get_type(attr) != TCA_STATS)
572 if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(*ts)) < 0) {
573 ERROR("netlink plugin: qos_filter_cb: TCA_STATS mnl_attr_validate2 "
578 ts = mnl_attr_get_payload(attr);
580 if (!stats_submitted && ts != NULL) {
581 char type_instance[DATA_MAX_NAME_LEN];
583 int r = snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
585 if ((size_t)r >= sizeof(type_instance)) {
586 ERROR("netlink plugin: type_instance truncated to %zu bytes, need %d",
587 sizeof(type_instance), r);
591 submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
592 submit_one(dev, "ipt_packets", type_instance, ts->packets);
598 #endif /* TCA_STATS */
600 #if !(HAVE_TCA_STATS && HAVE_TCA_STATS2)
601 DEBUG("netlink plugin: qos_filter_cb: Have neither TCA_STATS2 nor "
606 } /* int qos_filter_cb */
608 static int ir_config(const char *key, const char *value) {
614 new_val = strdup(value);
618 fields_num = strsplit(new_val, fields, STATIC_ARRAY_SIZE(fields));
619 if ((fields_num < 1) || (fields_num > 8)) {
624 if ((strcasecmp(key, "Interface") == 0) ||
625 (strcasecmp(key, "VerboseInterface") == 0)) {
626 if (fields_num != 1) {
627 ERROR("netlink plugin: Invalid number of fields for option "
628 "`%s'. Got %i, expected 1.",
632 add_ignorelist(fields[0], "interface", NULL);
633 if (strcasecmp(key, "VerboseInterface") == 0)
634 add_ignorelist(fields[0], "if_detail", NULL);
637 } else if ((strcasecmp(key, "QDisc") == 0) ||
638 (strcasecmp(key, "Class") == 0) ||
639 (strcasecmp(key, "Filter") == 0)) {
640 if ((fields_num < 1) || (fields_num > 2)) {
641 ERROR("netlink plugin: Invalid number of fields for option "
642 "`%s'. Got %i, expected 1 or 2.",
646 add_ignorelist(fields[0], key, (fields_num == 2) ? fields[1] : NULL);
649 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
650 if (fields_num != 1) {
651 ERROR("netlink plugin: Invalid number of fields for option "
652 "`IgnoreSelected'. Got %i, expected 1.",
656 if (IS_TRUE(fields[0]))
657 ir_ignorelist_invert = 0;
659 ir_ignorelist_invert = 1;
667 } /* int ir_config */
669 static int ir_init(void) {
670 nl = mnl_socket_open(NETLINK_ROUTE);
672 ERROR("netlink plugin: ir_init: mnl_socket_open failed.");
676 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
677 ERROR("netlink plugin: ir_init: mnl_socket_bind failed.");
684 static int ir_read(void) {
685 char buf[MNL_SOCKET_BUFFER_SIZE];
686 struct nlmsghdr *nlh;
689 unsigned int seq, portid;
691 static const int type_id[] = {RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER};
692 static const char *type_name[] = {"qdisc", "class", "filter"};
694 portid = mnl_socket_get_portid(nl);
696 nlh = mnl_nlmsg_put_header(buf);
697 nlh->nlmsg_type = RTM_GETLINK;
698 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
699 nlh->nlmsg_seq = seq = time(NULL);
700 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(*rt));
701 rt->rtgen_family = AF_PACKET;
703 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
704 ERROR("netlink plugin: ir_read: rtnl_wilddump_request failed.");
708 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
710 ret = mnl_cb_run(buf, ret, seq, portid, link_filter_cb, NULL);
711 if (ret <= MNL_CB_STOP)
713 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
716 ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s", STRERRNO);
720 /* `link_filter_cb' will update `iflist' which is used here to iterate
721 * over all interfaces. */
722 for (size_t ifindex = 1; ifindex < iflist_len; ifindex++) {
725 if (iflist[ifindex] == NULL)
728 for (size_t type_index = 0; type_index < STATIC_ARRAY_SIZE(type_id);
730 if (check_ignorelist(iflist[ifindex], type_name[type_index], NULL)) {
731 DEBUG("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
733 iflist[ifindex], type_name[type_index]);
737 DEBUG("netlink plugin: ir_read: querying %s from %s (%" PRIsz ").",
738 type_name[type_index], iflist[ifindex], ifindex);
740 nlh = mnl_nlmsg_put_header(buf);
741 nlh->nlmsg_type = type_id[type_index];
742 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
743 nlh->nlmsg_seq = seq = time(NULL);
744 tm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tm));
745 tm->tcm_family = AF_PACKET;
746 tm->tcm_ifindex = ifindex;
748 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
749 ERROR("netlink plugin: ir_read: mnl_socket_sendto failed.");
753 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
755 ret = mnl_cb_run(buf, ret, seq, portid, qos_filter_cb, &ifindex);
756 if (ret <= MNL_CB_STOP)
758 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
761 ERROR("netlink plugin: ir_read: mnl_socket_recvfrom failed: %s",
765 } /* for (type_index) */
766 } /* for (if_index) */
771 static int ir_shutdown(void) {
773 mnl_socket_close(nl);
778 } /* int ir_shutdown */
780 void module_register(void) {
781 plugin_register_config("netlink", ir_config, config_keys, config_keys_num);
782 plugin_register_init("netlink", ir_init);
783 plugin_register_read("netlink", ir_read);
784 plugin_register_shutdown("netlink", ir_shutdown);
785 } /* void module_register */