X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnetwork.c;h=8615753807445755cab2b4a49c87b73e9e4ea893;hb=eeae8f7fd3b1bbad7fbcfef7bde35a7b20148c87;hp=0e416bd5dd1714fd39e592b8622267e293727948;hpb=27aec36864d1882bf628b9c71c233d70e994a202;p=collectd.git diff --git a/src/network.c b/src/network.c index 0e416bd5..86157538 100644 --- a/src/network.c +++ b/src/network.c @@ -1,22 +1,25 @@ /** * collectd - src/network.c * Copyright (C) 2005-2009 Florian octo Forster + * Copyright (C) 2009 Aman Gupta * * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; only version 2.1 of the License is + * applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: * Florian octo Forster + * Aman Gupta **/ #define _BSD_SOURCE /* For struct ip_mreq */ @@ -52,11 +55,9 @@ #if HAVE_LIBGCRYPT # include +GCRY_THREAD_OPTION_PTHREAD_IMPL; #endif -/* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */ -/* #define BUFF_SIZE 1452 */ - #ifndef IPV6_ADD_MEMBERSHIP # ifdef IPV6_JOIN_GROUP # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP @@ -65,9 +66,6 @@ # endif #endif /* !IP_ADD_MEMBERSHIP */ -/* Buffer size to allocate. */ -#define BUFF_SIZE 1024 - /* * Maximum size required for encryption / signing: * @@ -245,7 +243,7 @@ typedef struct part_encryption_aes256_s part_encryption_aes256_t; struct receive_list_entry_s { - char data[BUFF_SIZE]; + char *data; int data_len; int fd; struct receive_list_entry_s *next; @@ -256,7 +254,9 @@ typedef struct receive_list_entry_s receive_list_entry_t; * Private variables */ static int network_config_ttl = 0; +static size_t network_config_packet_size = 1024; static int network_config_forward = 0; +static int network_config_stats = 0; static sockent_t *sending_sockets = NULL; @@ -264,6 +264,7 @@ static receive_list_entry_t *receive_list_head = NULL; static receive_list_entry_t *receive_list_tail = NULL; static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER; +static uint64_t receive_list_length = 0; static sockent_t *listen_sockets = NULL; static struct pollfd *listen_sockets_pollfd = NULL; @@ -278,12 +279,28 @@ static int dispatch_thread_running = 0; static pthread_t dispatch_thread_id; /* Buffer in which to-be-sent network packets are constructed. */ -static char send_buffer[BUFF_SIZE]; +static char *send_buffer; static char *send_buffer_ptr; static int send_buffer_fill; static value_list_t send_buffer_vl = VALUE_LIST_STATIC; static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER; +/* XXX: These counters are incremented from one place only. The spot in which + * the values are incremented is either only reachable by one thread (the + * dispatch thread, for example) or locked by some lock (send_buffer_lock for + * example). Only if neither is true, the stats_lock is acquired. The counters + * are always read without holding a lock in the hope that writing 8 bytes to + * memory is an atomic operation. */ +static uint64_t stats_octets_rx = 0; +static uint64_t stats_octets_tx = 0; +static uint64_t stats_packets_rx = 0; +static uint64_t stats_packets_tx = 0; +static uint64_t stats_values_dispatched = 0; +static uint64_t stats_values_not_dispatched = 0; +static uint64_t stats_values_sent = 0; +static uint64_t stats_values_not_sent = 0; +static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER; + /* * Private functions */ @@ -342,12 +359,13 @@ static int network_dispatch_values (value_list_t *vl) /* {{{ */ if (!check_receive_okay (vl)) { #if COLLECT_DEBUG - char name[6*DATA_MAX_NAME_LEN]; - FORMAT_VL (name, sizeof (name), vl); - name[sizeof (name) - 1] = 0; - DEBUG ("network plugin: network_dispatch_values: " - "NOT dispatching %s.", name); + char name[6*DATA_MAX_NAME_LEN]; + FORMAT_VL (name, sizeof (name), vl); + name[sizeof (name) - 1] = 0; + DEBUG ("network plugin: network_dispatch_values: " + "NOT dispatching %s.", name); #endif + stats_values_not_dispatched++; return (0); } @@ -370,6 +388,7 @@ static int network_dispatch_values (value_list_t *vl) /* {{{ */ } plugin_dispatch_values (vl); + stats_values_dispatched++; meta_data_destroy (vl->meta); vl->meta = NULL; @@ -1431,6 +1450,10 @@ static int parse_packet (sockent_t *se, /* {{{ */ } } /* while (buffer_size > sizeof (part_header_t)) */ + if (status == 0 && buffer_size > 0) + WARNING ("network plugin: parse_packet: Received truncated " + "packet, try increasing `MaxPacketSize'"); + return (status); } /* }}} int parse_packet */ @@ -1959,13 +1982,14 @@ static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */ /* Lock and wait for more data to come in */ pthread_mutex_lock (&receive_list_lock); while ((listen_loop == 0) - && (receive_list_head == NULL)) + && (receive_list_head == NULL)) pthread_cond_wait (&receive_list_cond, &receive_list_lock); /* Remove the head entry and unlock */ ent = receive_list_head; if (ent != NULL) receive_list_head = ent->next; + receive_list_length--; pthread_mutex_unlock (&receive_list_lock); /* Check whether we are supposed to exit. We do NOT check `listen_loop' @@ -1991,14 +2015,16 @@ static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */ if (se == NULL) { - ERROR ("network plugin: Got packet from FD %i, but can't " - "find an appropriate socket entry.", - ent->fd); - sfree (ent); - continue; + ERROR ("network plugin: Got packet from FD %i, but can't " + "find an appropriate socket entry.", + ent->fd); + sfree (ent->data); + sfree (ent); + continue; } parse_packet (se, ent->data, ent->data_len, /* flags = */ 0); + sfree (ent->data); sfree (ent); } /* while (42) */ @@ -2007,7 +2033,7 @@ static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */ static int network_receive (void) /* {{{ */ { - char buffer[BUFF_SIZE]; + char buffer[network_config_packet_size]; int buffer_len; int i; @@ -2015,11 +2041,13 @@ static int network_receive (void) /* {{{ */ receive_list_entry_t *private_list_head; receive_list_entry_t *private_list_tail; + uint64_t private_list_length; assert (listen_sockets_num > 0); private_list_head = NULL; private_list_tail = NULL; + private_list_length = 0; while (listen_loop == 0) { @@ -2056,6 +2084,9 @@ static int network_receive (void) /* {{{ */ return (-1); } + stats_octets_rx += ((uint64_t) buffer_len); + stats_packets_rx++; + /* TODO: Possible performance enhancement: Do not free * these entries in the dispatch thread but put them in * another list, so we don't have to allocate more and @@ -2067,14 +2098,16 @@ static int network_receive (void) /* {{{ */ return (-1); } memset (ent, 0, sizeof (receive_list_entry_t)); + ent->data = malloc (network_config_packet_size); + if (ent->data == NULL) + { + sfree (ent); + ERROR ("network plugin: malloc failed."); + return (-1); + } ent->fd = listen_sockets_pollfd[i].fd; ent->next = NULL; - /* Hopefully this be optimized out by the compiler. It - * might help prevent stupid bugs in the future though. - */ - assert (sizeof (ent->data) == sizeof (buffer)); - memcpy (ent->data, buffer, buffer_len); ent->data_len = buffer_len; @@ -2083,22 +2116,28 @@ static int network_receive (void) /* {{{ */ else private_list_tail->next = ent; private_list_tail = ent; + private_list_length++; /* Do not block here. Blocking here has led to * insufficient performance in the past. */ if (pthread_mutex_trylock (&receive_list_lock) == 0) { + assert (((receive_list_head == NULL) && (receive_list_length == 0)) + || ((receive_list_head != NULL) && (receive_list_length != 0))); + if (receive_list_head == NULL) receive_list_head = private_list_head; else receive_list_tail->next = private_list_head; receive_list_tail = private_list_tail; - - private_list_head = NULL; - private_list_tail = NULL; + receive_list_length += private_list_length; pthread_cond_signal (&receive_list_cond); pthread_mutex_unlock (&receive_list_lock); + + private_list_head = NULL; + private_list_tail = NULL; + private_list_length = 0; } } /* for (listen_sockets_pollfd) */ } /* while (listen_loop == 0) */ @@ -2113,9 +2152,11 @@ static int network_receive (void) /* {{{ */ else receive_list_tail->next = private_list_head; receive_list_tail = private_list_tail; + receive_list_length += private_list_length; private_list_head = NULL; private_list_tail = NULL; + private_list_length = 0; pthread_cond_signal (&receive_list_cond); pthread_mutex_unlock (&receive_list_lock); @@ -2131,7 +2172,7 @@ static void *receive_thread (void __attribute__((unused)) *arg) static void network_init_buffer (void) { - memset (send_buffer, 0, sizeof (send_buffer)); + memset (send_buffer, 0, network_config_packet_size); send_buffer_ptr = send_buffer; send_buffer_fill = 0; @@ -2424,6 +2465,10 @@ static void flush_buffer (void) send_buffer_fill); network_send_buffer (send_buffer, (size_t) send_buffer_fill); + + stats_octets_tx += ((uint64_t) send_buffer_fill); + stats_packets_tx++; + network_init_buffer (); } @@ -2441,6 +2486,11 @@ static int network_write (const data_set_t *ds, const value_list_t *vl, DEBUG ("network plugin: network_write: " "NOT sending %s.", name); #endif + /* Counter is not protected by another lock and may be reached by + * multiple threads */ + pthread_mutex_lock (&stats_lock); + stats_values_not_sent++; + pthread_mutex_unlock (&stats_lock); return (0); } @@ -2450,7 +2500,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl, pthread_mutex_lock (&send_buffer_lock); status = add_to_buffer (send_buffer_ptr, - sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE), + network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE), &send_buffer_vl, ds, vl); if (status >= 0) @@ -2458,13 +2508,15 @@ static int network_write (const data_set_t *ds, const value_list_t *vl, /* status == bytes added to the buffer */ send_buffer_fill += status; send_buffer_ptr += status; + + stats_values_sent++; } else { flush_buffer (); status = add_to_buffer (send_buffer_ptr, - sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE), + network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE), &send_buffer_vl, ds, vl); @@ -2472,6 +2524,8 @@ static int network_write (const data_set_t *ds, const value_list_t *vl, { send_buffer_fill += status; send_buffer_ptr += status; + + stats_values_sent++; } } @@ -2480,7 +2534,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl, ERROR ("network plugin: Unable to append to the " "buffer for some weird reason"); } - else if ((sizeof (send_buffer) - send_buffer_fill) < 15) + else if ((network_config_packet_size - send_buffer_fill) < 15) { flush_buffer (); } @@ -2513,13 +2567,9 @@ static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */ { char *str = ci->values[0].value.string; - if ((strcasecmp ("true", str) == 0) - || (strcasecmp ("yes", str) == 0) - || (strcasecmp ("on", str) == 0)) + if (IS_TRUE (str)) *retval = 1; - else if ((strcasecmp ("false", str) == 0) - || (strcasecmp ("no", str) == 0) - || (strcasecmp ("off", str) == 0)) + else if (IS_FALSE (str)) *retval = 0; else { @@ -2551,6 +2601,24 @@ static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */ return (0); } /* }}} int network_config_set_ttl */ +static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */ +{ + int tmp; + if ((ci->values_num != 1) + || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) + { + WARNING ("network plugin: The `MaxPacketSize' config option needs exactly " + "one numeric argument."); + return (-1); + } + + tmp = (int) ci->values[0].value.number; + if ((tmp >= 1024) && (tmp <= 65535)) + network_config_packet_size = tmp; + + return (0); +} /* }}} int network_config_set_buffer_size */ + #if HAVE_LIBGCRYPT static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */ char **ret_string) @@ -2774,8 +2842,12 @@ static int network_config (oconfig_item_t *ci) /* {{{ */ network_config_add_server (child); else if (strcasecmp ("TimeToLive", child->key) == 0) network_config_set_ttl (child); + else if (strcasecmp ("MaxPacketSize", child->key) == 0) + network_config_set_buffer_size (child); else if (strcasecmp ("Forward", child->key) == 0) network_config_set_boolean (child, &network_config_forward); + else if (strcasecmp ("ReportStats", child->key) == 0) + network_config_set_boolean (child, &network_config_stats); else if (strcasecmp ("CacheFlush", child->key) == 0) /* no op for backwards compatibility only */; else @@ -2791,7 +2863,7 @@ static int network_config (oconfig_item_t *ci) /* {{{ */ static int network_notification (const notification_t *n, user_data_t __attribute__((unused)) *user_data) { - char buffer[BUFF_SIZE]; + char buffer[network_config_packet_size]; char *buffer_ptr = buffer; int buffer_free = sizeof (buffer); int status; @@ -2890,6 +2962,8 @@ static int network_shutdown (void) if (send_buffer_fill > 0) flush_buffer (); + sfree (send_buffer); + /* TODO: Close `sending_sockets' */ plugin_unregister_config ("network"); @@ -2900,6 +2974,83 @@ static int network_shutdown (void) return (0); } /* int network_shutdown */ +static int network_stats_read (void) /* {{{ */ +{ + uint64_t copy_octets_rx; + uint64_t copy_octets_tx; + uint64_t copy_packets_rx; + uint64_t copy_packets_tx; + uint64_t copy_values_dispatched; + uint64_t copy_values_not_dispatched; + uint64_t copy_values_sent; + uint64_t copy_values_not_sent; + uint64_t copy_receive_list_length; + value_list_t vl = VALUE_LIST_INIT; + value_t values[2]; + + copy_octets_rx = stats_octets_rx; + copy_octets_tx = stats_octets_tx; + copy_packets_rx = stats_packets_rx; + copy_packets_tx = stats_packets_tx; + copy_values_dispatched = stats_values_dispatched; + copy_values_not_dispatched = stats_values_not_dispatched; + copy_values_sent = stats_values_sent; + copy_values_not_sent = stats_values_not_sent; + copy_receive_list_length = receive_list_length; + + /* Initialize `vl' */ + vl.values = values; + vl.values_len = 2; + vl.time = 0; + vl.interval = interval_g; + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "network", sizeof (vl.plugin)); + + /* Octets received / sent */ + vl.values[0].counter = (counter_t) copy_octets_rx; + vl.values[1].counter = (counter_t) copy_octets_tx; + sstrncpy (vl.type, "if_octets", sizeof (vl.type)); + plugin_dispatch_values (&vl); + + /* Packets received / send */ + vl.values[0].counter = (counter_t) copy_packets_rx; + vl.values[1].counter = (counter_t) copy_packets_tx; + sstrncpy (vl.type, "if_packets", sizeof (vl.type)); + plugin_dispatch_values (&vl); + + /* Values (not) dispatched and (not) send */ + sstrncpy (vl.type, "total_values", sizeof (vl.type)); + vl.values_len = 1; + + vl.values[0].derive = (derive_t) copy_values_dispatched; + sstrncpy (vl.type_instance, "dispatch-accepted", + sizeof (vl.type_instance)); + plugin_dispatch_values (&vl); + + vl.values[0].derive = (derive_t) copy_values_not_dispatched; + sstrncpy (vl.type_instance, "dispatch-rejected", + sizeof (vl.type_instance)); + plugin_dispatch_values (&vl); + + vl.values[0].derive = (derive_t) copy_values_sent; + sstrncpy (vl.type_instance, "send-accepted", + sizeof (vl.type_instance)); + plugin_dispatch_values (&vl); + + vl.values[0].derive = (derive_t) copy_values_not_sent; + sstrncpy (vl.type_instance, "send-rejected", + sizeof (vl.type_instance)); + plugin_dispatch_values (&vl); + + /* Receive queue length */ + vl.values[0].gauge = (gauge_t) copy_receive_list_length; + sstrncpy (vl.type, "queue_length", sizeof (vl.type)); + vl.type_instance[0] = 0; + plugin_dispatch_values (&vl); + + return (0); +} /* }}} int network_stats_read */ + static int network_init (void) { static _Bool have_init = false; @@ -2910,8 +3061,23 @@ static int network_init (void) return (0); have_init = true; +#if HAVE_LIBGCRYPT + gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0); + gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); +#endif + + if (network_config_stats != 0) + plugin_register_read ("network", network_stats_read); + plugin_register_shutdown ("network", network_shutdown); + send_buffer = malloc (network_config_packet_size); + if (send_buffer == NULL) + { + ERROR ("network plugin: malloc failed."); + return (-1); + } network_init_buffer (); /* setup socket(s) and so on */