X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fovs_stats.c;h=20b0dd1f0174fbd31104f837c82572f339bc4843;hb=71d8519096a413dec86660da706865e82b202036;hp=7d775284fbea7b38e3b71798aa8a6786adf78ec2;hpb=3e0dd5070e385b864c771902bfa4c6331c5710b4;p=collectd.git diff --git a/src/ovs_stats.c b/src/ovs_stats.c index 7d775284..20b0dd1f 100644 --- a/src/ovs_stats.c +++ b/src/ovs_stats.c @@ -3,14 +3,17 @@ * * Copyright(c) 2016 Intel Corporation. All rights reserved. * - * Permission is hereby granted, free of charge, to any person obtaining a copy of + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do + * of the Software, and to permit persons to whom the Software is furnished to + * do * so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all + * The above copyright notice and this permission notice shall be included in + * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR @@ -308,7 +311,7 @@ static int ovs_stats_del_bridge(yajl_val bridge) { } } else WARNING("%s: Incorrect data for deleting bridge", plugin_name); - return (0); + return 0; } /* Update Bridge. Create bridge ports*/ @@ -330,7 +333,7 @@ static int ovs_stats_update_bridge(yajl_val bridge) { br = (bridge_list_t *)calloc(1, sizeof(bridge_list_t)); if (!br) { ERROR("%s: Error allocating memory for bridge", plugin_name); - return (-1); + return -1; } char *tmp = YAJL_GET_STRING(br_name); @@ -339,7 +342,7 @@ static int ovs_stats_update_bridge(yajl_val bridge) { if (br->name == NULL) { sfree(br); pthread_mutex_unlock(&g_stats_lock); - return (-1); + return -1; } br->next = g_bridge_list_head; g_bridge_list_head = br; @@ -349,22 +352,24 @@ static int ovs_stats_update_bridge(yajl_val bridge) { if (br_ports && YAJL_IS_ARRAY(br_ports)) { char *tmp = YAJL_GET_STRING(br_ports->u.array.values[0]); if (tmp != NULL && strcmp("set", tmp) == 0) { - yajl_val *ports_arr = - YAJL_GET_ARRAY(br_ports->u.array.values[1])->values; - size_t ports_num = YAJL_GET_ARRAY(br_ports->u.array.values[1])->len; - - for (size_t i = 0; i < ports_num; i++) - ovs_stats_new_port( - br, YAJL_GET_STRING(ports_arr[i]->u.array.values[1])); + yajl_val *array = YAJL_GET_ARRAY(br_ports)->values; + size_t array_len = YAJL_GET_ARRAY(br_ports)->len; + if (array != NULL && array_len > 0 && YAJL_IS_ARRAY(array[1])) { + yajl_val *ports_arr = YAJL_GET_ARRAY(array[1])->values; + size_t ports_num = YAJL_GET_ARRAY(array[1])->len; + for (size_t i = 0; i < ports_num && ports_arr != NULL; i++) + ovs_stats_new_port( + br, YAJL_GET_STRING(ports_arr[i]->u.array.values[1])); + } } else ovs_stats_new_port(br, YAJL_GET_STRING(br_ports->u.array.values[1])); } } } else { ERROR("Incorrect JSON Bridge data"); - return (-1); + return -1; } - return (0); + return 0; } /* Handle JSON with Bridge Table change event */ @@ -455,9 +460,9 @@ static int ovs_stats_update_port(const char *uuid, yajl_val port) { } } else { ERROR("Incorrect JSON Port data"); - return (-1); + return -1; } - return (0); + return 0; } /* Delete port from global port list */ @@ -474,7 +479,7 @@ static int ovs_stats_del_port(const char *uuid) { break; } } - return (0); + return 0; } /* Handle JSON with Port Table change event */ @@ -537,6 +542,8 @@ static int ovs_stats_update_iface_stats(port_list_t *port, yajl_val stats) { if (stats && YAJL_IS_ARRAY(stats)) for (size_t i = 0; i < YAJL_GET_ARRAY(stats)->len; i++) { stat = YAJL_GET_ARRAY(stats)->values[i]; + if (!YAJL_IS_ARRAY(stat)) + return -1; counter_name = YAJL_GET_STRING(YAJL_GET_ARRAY(stat)->values[0]); counter_index = ovs_stats_counter_name_to_type(counter_name); counter_value = YAJL_GET_INTEGER(YAJL_GET_ARRAY(stat)->values[1]); @@ -545,7 +552,7 @@ static int ovs_stats_update_iface_stats(port_list_t *port, yajl_val stats) { port->stats[counter_index] = counter_value; } - return (0); + return 0; } /* Update interface external_ids */ @@ -557,6 +564,8 @@ static int ovs_stats_update_iface_ext_ids(port_list_t *port, yajl_val ext_ids) { if (ext_ids && YAJL_IS_ARRAY(ext_ids)) for (size_t i = 0; i < YAJL_GET_ARRAY(ext_ids)->len; i++) { ext_id = YAJL_GET_ARRAY(ext_ids)->values[i]; + if (!YAJL_IS_ARRAY(ext_id)) + return -1; key = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[0]); value = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[1]); if (key && value) { @@ -567,7 +576,7 @@ static int ovs_stats_update_iface_ext_ids(port_list_t *port, yajl_val ext_ids) { } } - return (0); + return 0; } /* Get interface statistic and external_ids */ @@ -584,7 +593,7 @@ static int ovs_stats_update_iface(yajl_val iface) { if (iface_name && YAJL_IS_STRING(iface_name)) { port = ovs_stats_get_port_by_name(YAJL_GET_STRING(iface_name)); if (port == NULL) - return (0); + return 0; } /* * { @@ -620,9 +629,9 @@ static int ovs_stats_update_iface(yajl_val iface) { } } else { ERROR("Incorrect JSON Port data"); - return (-1); + return -1; } - return (0); + return 0; } /* Handle JSON with Interface Table change event */ @@ -705,33 +714,30 @@ static void ovs_stats_initialize(ovs_db_t *pdb) { "external_ids", NULL}; /* subscribe to a tables */ - ovs_db_table_cb_register(pdb, "Bridge", bridge_columns, - ovs_stats_bridge_table_change_cb, + ovs_db_table_cb_register( + pdb, "Bridge", bridge_columns, ovs_stats_bridge_table_change_cb, ovs_stats_bridge_table_result_cb, - OVS_DB_TABLE_CB_FLAG_INITIAL | - OVS_DB_TABLE_CB_FLAG_INSERT | + OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT | OVS_DB_TABLE_CB_FLAG_MODIFY); ovs_db_table_cb_register(pdb, "Bridge", bridge_columns, ovs_stats_bridge_table_delete_cb, NULL, OVS_DB_TABLE_CB_FLAG_DELETE); - ovs_db_table_cb_register(pdb, "Port", port_columns, - ovs_stats_port_table_change_cb, + ovs_db_table_cb_register( + pdb, "Port", port_columns, ovs_stats_port_table_change_cb, ovs_stats_port_table_result_cb, - OVS_DB_TABLE_CB_FLAG_INITIAL | - OVS_DB_TABLE_CB_FLAG_INSERT | + OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT | OVS_DB_TABLE_CB_FLAG_MODIFY); ovs_db_table_cb_register(pdb, "Port", port_columns, ovs_stats_port_table_delete_cb, NULL, OVS_DB_TABLE_CB_FLAG_DELETE); - ovs_db_table_cb_register(pdb, "Interface", interface_columns, - ovs_stats_interface_table_change_cb, + ovs_db_table_cb_register( + pdb, "Interface", interface_columns, ovs_stats_interface_table_change_cb, ovs_stats_interface_table_result_cb, - OVS_DB_TABLE_CB_FLAG_INITIAL | - OVS_DB_TABLE_CB_FLAG_INSERT | + OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT | OVS_DB_TABLE_CB_FLAG_MODIFY); } @@ -739,11 +745,11 @@ static void ovs_stats_initialize(ovs_db_t *pdb) { static int ovs_stats_is_monitored_bridge(const char *br_name) { /* if no bridges are configured, return true */ if (g_monitored_bridge_list_head == NULL) - return (1); + return 1; /* check if given bridge exists */ if (ovs_stats_get_bridge(g_monitored_bridge_list_head, br_name) != NULL) - return (1); + return 1; return 0; } @@ -791,19 +797,19 @@ static int ovs_stats_plugin_config(oconfig_item_t *ci) { if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_node, OVS_DB_ADDR_NODE_SIZE) != 0) { ERROR("%s: parse '%s' option failed", plugin_name, child->key); - return (-1); + return -1; } } else if (strcasecmp("Port", child->key) == 0) { if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_serv, OVS_DB_ADDR_SERVICE_SIZE) != 0) { ERROR("%s: parse '%s' option failed", plugin_name, child->key); - return (-1); + return -1; } } else if (strcasecmp("Socket", child->key) == 0) { if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_unix, OVS_DB_ADDR_UNIX_SIZE) != 0) { ERROR("%s: parse '%s' option failed", plugin_name, child->key); - return (-1); + return -1; } } else if (strcasecmp("Bridges", child->key) == 0) { for (int j = 0; j < child->values_num; j++) { @@ -840,11 +846,11 @@ static int ovs_stats_plugin_config(oconfig_item_t *ci) { goto cleanup_fail; } } - return (0); + return 0; cleanup_fail: ovs_stats_free_bridge_list(g_monitored_bridge_list_head); - return (-1); + return -1; } /* Initialize OvS Stats plugin*/ @@ -856,26 +862,26 @@ static int ovs_stats_plugin_init(void) { plugin_name, ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv, ovs_stats_cfg.ovs_db_unix); /* connect to OvS DB */ - if ((g_ovs_db = ovs_db_init (ovs_stats_cfg.ovs_db_node, - ovs_stats_cfg.ovs_db_serv, + if ((g_ovs_db = + ovs_db_init(ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv, ovs_stats_cfg.ovs_db_unix, &cb)) == NULL) { ERROR("%s: plugin: failed to connect to OvS DB server", plugin_name); - return (-1); + return -1; } int err = pthread_mutex_init(&g_stats_lock, NULL); if (err < 0) { ERROR("%s: plugin: failed to initialize cache lock", plugin_name); ovs_db_destroy(g_ovs_db); - return (-1); + return -1; } - return (0); + return 0; } /* OvS stats read callback. Read bridge/port information and submit it*/ static int ovs_stats_plugin_read(__attribute__((unused)) user_data_t *ud) { bridge_list_t *bridge; port_list_t *port; - char devname[PORT_NAME_SIZE_MAX]; + char devname[PORT_NAME_SIZE_MAX * 2]; pthread_mutex_lock(&g_stats_lock); for (bridge = g_bridge_list_head; bridge != NULL; bridge = bridge->next) { @@ -958,7 +964,7 @@ static int ovs_stats_plugin_read(__attribute__((unused)) user_data_t *ud) { continue; } pthread_mutex_unlock(&g_stats_lock); - return (0); + return 0; } /* Shutdown OvS Stats plugin */ @@ -971,7 +977,7 @@ static int ovs_stats_plugin_shutdown(void) { ovs_stats_free_port_list(g_port_list_head); pthread_mutex_unlock(&g_stats_lock); pthread_mutex_destroy(&g_stats_lock); - return (0); + return 0; } /* Register OvS Stats plugin callbacks */