*
* 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
}
} else
WARNING("%s: Incorrect data for deleting bridge", plugin_name);
- return (0);
+ return 0;
}
/* Update Bridge. Create bridge ports*/
br = ovs_stats_get_bridge(g_bridge_list_head, YAJL_GET_STRING(br_name));
pthread_mutex_lock(&g_stats_lock);
if (br == NULL) {
- br = (bridge_list_t *)calloc(1, sizeof(bridge_list_t));
+ br = calloc(1, sizeof(*br));
if (!br) {
- ERROR("%s: Error allocating memory for bridge", plugin_name);
- return (-1);
+ pthread_mutex_unlock(&g_stats_lock);
+ ERROR("%s: calloc(%zu) failed.", plugin_name, sizeof(*br));
+ return -1;
}
char *tmp = YAJL_GET_STRING(br_name);
if (br->name == NULL) {
sfree(br);
pthread_mutex_unlock(&g_stats_lock);
- return (-1);
+ ERROR("%s: strdup failed.", plugin_name);
+ return -1;
}
br->next = g_bridge_list_head;
g_bridge_list_head = br;
}
} else {
ERROR("Incorrect JSON Bridge data");
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
}
/* Handle JSON with Bridge Table change event */
}
} else {
ERROR("Incorrect JSON Port data");
- return (-1);
+ return -1;
}
- return (0);
+ return 0;
}
/* Delete port from global port list */
break;
}
}
- return (0);
+ return 0;
}
/* Handle JSON with Port Table change event */
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);
+ 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]);
port->stats[counter_index] = counter_value;
}
- return (0);
+ return 0;
}
/* Update interface external_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);
+ 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) {
}
}
- return (0);
+ return 0;
}
/* Get interface statistic and external_ids */
static int ovs_stats_update_iface(yajl_val iface) {
- yajl_val row;
- port_list_t *port = NULL;
- if (iface && YAJL_IS_OBJECT(iface)) {
- row = ovs_utils_get_value_by_key(iface, "new");
- if (row && YAJL_IS_OBJECT(row)) {
- yajl_val iface_name = ovs_utils_get_value_by_key(row, "name");
- yajl_val iface_stats = ovs_utils_get_value_by_key(row, "statistics");
- yajl_val iface_ext_ids = ovs_utils_get_value_by_key(row, "external_ids");
- yajl_val iface_uuid = ovs_utils_get_value_by_key(row, "_uuid");
- 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);
- }
- /*
- * {
- "statistics": [
- "map",
- [
- [
- "collisions",
- 0
- ],
- . . .
- [
- "tx_packets",
- 0
- ]
- ]
- ]
- }
- Check that statistics is an array with 2 elements
- */
- if (iface_stats && YAJL_IS_ARRAY(iface_stats) &&
- YAJL_GET_ARRAY(iface_stats)->len == 2)
- ovs_stats_update_iface_stats(port,
- YAJL_GET_ARRAY(iface_stats)->values[1]);
- if (iface_ext_ids && YAJL_IS_ARRAY(iface_ext_ids))
- ovs_stats_update_iface_ext_ids(
- port, YAJL_GET_ARRAY(iface_ext_ids)->values[1]);
- if (iface_uuid && YAJL_IS_ARRAY(iface_uuid) &&
- YAJL_GET_ARRAY(iface_uuid)->len == 2)
- sstrncpy(port->iface_uuid,
- YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]),
- sizeof(port->iface_uuid));
- }
- } else {
- ERROR("Incorrect JSON Port data");
- return (-1);
+ if (!iface || !YAJL_IS_OBJECT(iface)) {
+ ERROR("ovs_stats plugin: incorrect JSON port data");
+ return -1;
}
- return (0);
+
+ yajl_val row = ovs_utils_get_value_by_key(iface, "new");
+ if (!row || !YAJL_IS_OBJECT(row))
+ return 0;
+
+ yajl_val iface_name = ovs_utils_get_value_by_key(row, "name");
+ if (!iface_name || !YAJL_IS_STRING(iface_name))
+ return 0;
+
+ port_list_t *port = ovs_stats_get_port_by_name(YAJL_GET_STRING(iface_name));
+ if (port == NULL)
+ return 0;
+
+ yajl_val iface_stats = ovs_utils_get_value_by_key(row, "statistics");
+ yajl_val iface_ext_ids = ovs_utils_get_value_by_key(row, "external_ids");
+ yajl_val iface_uuid = ovs_utils_get_value_by_key(row, "_uuid");
+ /*
+ * {
+ "statistics": [
+ "map",
+ [
+ [
+ "collisions",
+ 0
+ ],
+ . . .
+ [
+ "tx_packets",
+ 0
+ ]
+ ]
+ ]
+ }
+ Check that statistics is an array with 2 elements
+ */
+ if (iface_stats && YAJL_IS_ARRAY(iface_stats) &&
+ YAJL_GET_ARRAY(iface_stats)->len == 2)
+ ovs_stats_update_iface_stats(port, YAJL_GET_ARRAY(iface_stats)->values[1]);
+ if (iface_ext_ids && YAJL_IS_ARRAY(iface_ext_ids))
+ ovs_stats_update_iface_ext_ids(port,
+ YAJL_GET_ARRAY(iface_ext_ids)->values[1]);
+ if (iface_uuid && YAJL_IS_ARRAY(iface_uuid) &&
+ YAJL_GET_ARRAY(iface_uuid)->len == 2)
+ sstrncpy(port->iface_uuid,
+ YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]),
+ sizeof(port->iface_uuid));
+
+ return 0;
}
/* Handle JSON with Interface Table change event */
"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);
}
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;
}
*/
static int ovs_stats_plugin_config(oconfig_item_t *ci) {
bridge_list_t *bridge;
- char *br_name;
for (int i = 0; i < ci->children_num; i++) {
oconfig_item_t *child = ci->children + i;
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++) {
goto cleanup_fail;
}
/* get value */
- if ((br_name = strdup(child->values[j].value.string)) == NULL) {
- ERROR("%s: strdup() copy bridge name fail", plugin_name);
- goto cleanup_fail;
- }
+ char const *br_name = child->values[j].value.string;
if ((bridge = ovs_stats_get_bridge(g_monitored_bridge_list_head,
br_name)) == NULL) {
if ((bridge = calloc(1, sizeof(bridge_list_t))) == NULL) {
ERROR("%s: Error allocating memory for bridge", plugin_name);
goto cleanup_fail;
} else {
+ char *br_name_dup = strdup(br_name);
+ if (br_name_dup == NULL) {
+ ERROR("%s: strdup() copy bridge name fail", plugin_name);
+ goto cleanup_fail;
+ }
+
pthread_mutex_lock(&g_stats_lock);
/* store bridge name */
- bridge->name = br_name;
+ bridge->name = br_name_dup;
bridge->next = g_monitored_bridge_list_head;
g_monitored_bridge_list_head = bridge;
pthread_mutex_unlock(&g_stats_lock);
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*/
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*/
continue;
}
pthread_mutex_unlock(&g_stats_lock);
- return (0);
+ return 0;
}
/* Shutdown OvS Stats plugin */
static int ovs_stats_plugin_shutdown(void) {
- pthread_mutex_lock(&g_stats_lock);
DEBUG("OvS Statistics plugin shutting down");
ovs_db_destroy(g_ovs_db);
+ pthread_mutex_lock(&g_stats_lock);
ovs_stats_free_bridge_list(g_bridge_list_head);
ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
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 */