2 * collectd - src/ovs_stats.c
4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is furnished to
13 * so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
17 * copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * Taras Chornyi <tarasx.chornyi@intel.com>
33 #include "utils_ovs.h" /* OvS helpers */
36 static const char plugin_name[] = "ovs_stats";
38 typedef enum iface_counter {
54 rx_128_to_255_packets,
55 rx_256_to_511_packets,
56 rx_512_to_1023_packets,
57 rx_1024_to_1522_packets,
58 rx_1523_to_max_packets,
61 tx_128_to_255_packets,
62 tx_256_to_511_packets,
63 tx_512_to_1023_packets,
64 tx_1024_to_1522_packets,
65 tx_1523_to_max_packets,
76 #define IFACE_COUNTER_MAX (__iface_counter_max - 1)
77 #define IFACE_COUNTER_COUNT (__iface_counter_max)
78 #define PORT_NAME_SIZE_MAX 255
81 typedef struct port_s {
82 char name[PORT_NAME_SIZE_MAX]; /* Port name */
83 char port_uuid[UUID_SIZE]; /* Port table _uuid */
84 char iface_uuid[UUID_SIZE]; /* Interface table uuid */
85 char ex_iface_id[UUID_SIZE]; /* External iface id */
86 char ex_vm_id[UUID_SIZE]; /* External vm id */
87 int64_t stats[IFACE_COUNTER_COUNT]; /* Port statistics */
88 struct bridge_list_s *br; /* Pointer to bridge */
89 struct port_s *next; /* Next port */
92 typedef struct bridge_list_s {
93 char *name; /* Bridge name */
94 struct bridge_list_s *next; /* Next bridge*/
97 static const char *const iface_counter_table[IFACE_COUNTER_COUNT] = {
98 [collisions] = "collisions",
99 [rx_bytes] = "rx_bytes",
100 [rx_crc_err] = "rx_crc_err",
101 [rx_dropped] = "rx_dropped",
102 [rx_errors] = "rx_errors",
103 [rx_frame_err] = "rx_frame_err",
104 [rx_over_err] = "rx_over_err",
105 [rx_packets] = "rx_packets",
106 [tx_bytes] = "tx_bytes",
107 [tx_dropped] = "tx_dropped",
108 [tx_errors] = "tx_errors",
109 [tx_packets] = "tx_packets",
110 [rx_1_to_64_packets] = "rx_1_to_64_packets",
111 [rx_65_to_127_packets] = "rx_65_to_127_packets",
112 [rx_128_to_255_packets] = "rx_128_to_255_packets",
113 [rx_256_to_511_packets] = "rx_256_to_511_packets",
114 [rx_512_to_1023_packets] = "rx_512_to_1023_packets",
115 [rx_1024_to_1522_packets] = "rx_1024_to_1518_packets",
116 [rx_1523_to_max_packets] = "rx_1523_to_max_packets",
117 [tx_1_to_64_packets] = "tx_1_to_64_packets",
118 [tx_65_to_127_packets] = "tx_65_to_127_packets",
119 [tx_128_to_255_packets] = "tx_128_to_255_packets",
120 [tx_256_to_511_packets] = "tx_256_to_511_packets",
121 [tx_512_to_1023_packets] = "tx_512_to_1023_packets",
122 [tx_1024_to_1522_packets] = "tx_1024_to_1518_packets",
123 [tx_1523_to_max_packets] = "tx_1523_to_max_packets",
124 [tx_multicast_packets] = "tx_multicast_packets",
125 [rx_broadcast_packets] = "rx_broadcast_packets",
126 [tx_broadcast_packets] = "tx_broadcast_packets",
127 [rx_undersized_errors] = "rx_undersized_errors",
128 [rx_oversize_errors] = "rx_oversize_errors",
129 [rx_fragmented_errors] = "rx_fragmented_errors",
130 [rx_jabber_errors] = "rx_jabber_errors",
133 /* Entry into the list of network bridges */
134 static bridge_list_t *g_bridge_list_head;
136 /* Entry into the list of monitored network bridges */
137 static bridge_list_t *g_monitored_bridge_list_head;
139 /* entry into the list of network bridges */
140 static port_list_t *g_port_list_head;
142 /* lock for statistics cache */
143 static pthread_mutex_t g_stats_lock;
146 static ovs_db_t *g_ovs_db;
148 /* OVS stats configuration data */
149 struct ovs_stats_config_s {
150 char ovs_db_node[OVS_DB_ADDR_NODE_SIZE]; /* OVS DB node */
151 char ovs_db_serv[OVS_DB_ADDR_SERVICE_SIZE]; /* OVS DB service */
152 char ovs_db_unix[OVS_DB_ADDR_UNIX_SIZE]; /* OVS DB unix socket path */
154 typedef struct ovs_stats_config_s ovs_stats_config_t;
156 static ovs_stats_config_t ovs_stats_cfg = {
157 .ovs_db_node = "localhost", /* use default OVS DB node */
158 .ovs_db_serv = "6640", /* use default OVS DB service */
161 static iface_counter ovs_stats_counter_name_to_type(const char *counter) {
162 iface_counter index = not_supported;
165 return not_supported;
167 for (int i = 0; i < IFACE_COUNTER_COUNT; i++) {
168 if (strncmp(iface_counter_table[i], counter,
169 strlen(iface_counter_table[i])) == 0) {
177 static void ovs_stats_submit_one(const char *dev, const char *type,
178 const char *type_instance, derive_t value,
180 /* if counter is less than 0 - skip it*/
183 value_list_t vl = VALUE_LIST_INIT;
185 vl.values = &(value_t){.derive = value};
189 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
190 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
191 sstrncpy(vl.type, type, sizeof(vl.type));
193 if (type_instance != NULL)
194 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
196 plugin_dispatch_values(&vl);
199 static void ovs_stats_submit_two(const char *dev, const char *type,
200 const char *type_instance, derive_t rx,
201 derive_t tx, meta_data_t *meta) {
202 /* if counter is less than 0 - skip it*/
203 if (rx < 0 || tx < 0)
205 value_list_t vl = VALUE_LIST_INIT;
206 value_t values[] = {{.derive = rx}, {.derive = tx}};
209 vl.values_len = STATIC_ARRAY_SIZE(values);
212 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
213 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
214 sstrncpy(vl.type, type, sizeof(vl.type));
216 if (type_instance != NULL)
217 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
219 plugin_dispatch_values(&vl);
222 static port_list_t *ovs_stats_get_port(const char *uuid) {
226 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
227 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0)
233 static port_list_t *ovs_stats_get_port_by_name(const char *name) {
237 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next)
238 if ((strncmp(port->name, name, strlen(port->name)) == 0) &&
239 strlen(name) == strlen(port->name))
244 /* Create or get port by port uuid */
245 static port_list_t *ovs_stats_new_port(bridge_list_t *bridge,
250 port_list_t *port = ovs_stats_get_port(uuid);
253 port = (port_list_t *)calloc(1, sizeof(port_list_t));
255 ERROR("%s: Error allocating port", plugin_name);
258 memset(port->stats, -1, sizeof(int64_t[IFACE_COUNTER_COUNT]));
259 sstrncpy(port->port_uuid, uuid, sizeof(port->port_uuid));
260 pthread_mutex_lock(&g_stats_lock);
261 port->next = g_port_list_head;
262 g_port_list_head = port;
263 pthread_mutex_unlock(&g_stats_lock);
265 if (bridge != NULL) {
266 pthread_mutex_lock(&g_stats_lock);
268 pthread_mutex_unlock(&g_stats_lock);
273 /* Get bridge by name*/
274 static bridge_list_t *ovs_stats_get_bridge(bridge_list_t *head,
279 for (bridge_list_t *bridge = head; bridge != NULL; bridge = bridge->next) {
280 if ((strncmp(bridge->name, name, strlen(bridge->name)) == 0) &&
281 strlen(name) == strlen(bridge->name))
288 static int ovs_stats_del_bridge(yajl_val bridge) {
289 const char *old[] = {"old", NULL};
290 const char *name[] = {"name", NULL};
294 if (bridge && YAJL_IS_OBJECT(bridge)) {
295 row = yajl_tree_get(bridge, old, yajl_t_object);
296 if (row && YAJL_IS_OBJECT(row)) {
297 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
298 if (br_name && YAJL_IS_STRING(br_name)) {
299 bridge_list_t *prev_br = g_bridge_list_head;
300 for (bridge_list_t *br = g_bridge_list_head; br != NULL;
301 prev_br = br, br = br->next) {
302 if ((strncmp(br->name, br_name->u.string, strlen(br->name)) == 0) &&
303 strlen(br->name) == strlen(br_name->u.string)) {
304 if (br == g_bridge_list_head)
305 g_bridge_list_head = br->next;
307 prev_br->next = br->next;
316 WARNING("%s: Incorrect data for deleting bridge", plugin_name);
320 /* Update Bridge. Create bridge ports*/
321 static int ovs_stats_update_bridge(yajl_val bridge) {
322 const char *new[] = {"new", NULL};
323 const char *name[] = {"name", NULL};
324 const char *ports[] = {"ports", NULL};
325 bridge_list_t *br = NULL;
327 if (bridge && YAJL_IS_OBJECT(bridge)) {
328 yajl_val row = yajl_tree_get(bridge, new, yajl_t_object);
329 if (row && YAJL_IS_OBJECT(row)) {
330 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
331 yajl_val br_ports = yajl_tree_get(row, ports, yajl_t_array);
332 if (br_name && YAJL_IS_STRING(br_name)) {
333 br = ovs_stats_get_bridge(g_bridge_list_head, YAJL_GET_STRING(br_name));
334 pthread_mutex_lock(&g_stats_lock);
336 br = calloc(1, sizeof(*br));
338 pthread_mutex_unlock(&g_stats_lock);
339 ERROR("%s: calloc(%zu) failed.", plugin_name, sizeof(*br));
342 char *tmp = YAJL_GET_STRING(br_name);
345 br->name = strdup(tmp);
346 if (br->name == NULL) {
348 pthread_mutex_unlock(&g_stats_lock);
349 ERROR("%s: strdup failed.", plugin_name);
352 br->next = g_bridge_list_head;
353 g_bridge_list_head = br;
355 pthread_mutex_unlock(&g_stats_lock);
357 if (br_ports && YAJL_IS_ARRAY(br_ports)) {
358 char *tmp = YAJL_GET_STRING(br_ports->u.array.values[0]);
359 if (tmp != NULL && strcmp("set", tmp) == 0) {
360 yajl_val *array = YAJL_GET_ARRAY(br_ports)->values;
361 size_t array_len = YAJL_GET_ARRAY(br_ports)->len;
362 if (array != NULL && array_len > 0 && YAJL_IS_ARRAY(array[1])) {
363 if (YAJL_GET_ARRAY(array[1]) == NULL)
366 yajl_val *ports_arr = YAJL_GET_ARRAY(array[1])->values;
367 size_t ports_num = YAJL_GET_ARRAY(array[1])->len;
368 for (size_t i = 0; i < ports_num && ports_arr != NULL; i++) {
369 tmp = YAJL_GET_STRING(ports_arr[i]->u.array.values[1]);
371 ovs_stats_new_port(br, tmp);
378 ovs_stats_new_port(br, YAJL_GET_STRING(br_ports->u.array.values[1]));
388 ERROR("Incorrect JSON Bridge data");
392 /* Handle JSON with Bridge Table change event */
393 static void ovs_stats_bridge_table_change_cb(yajl_val jupdates) {
394 /* Bridge Table update example JSON data
397 "bb1f8965-5775-46d9-b820-236ca8edbedc": {
405 "117f1a07-7ef0-458a-865c-ec7fbb85bc01"
409 "12fd8bdc-e950-4281-aaa9-46e185658f79"
418 const char *path[] = {"Bridge", NULL};
420 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
422 if (bridges && YAJL_IS_OBJECT(bridges)) {
423 for (size_t i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
424 yajl_val bridge = YAJL_GET_OBJECT(bridges)->values[i];
425 ovs_stats_update_bridge(bridge);
430 /* Handle Bridge Table delete event */
431 static void ovs_stats_bridge_table_delete_cb(yajl_val jupdates) {
432 const char *path[] = {"Bridge", NULL};
433 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
435 if (bridges && YAJL_IS_OBJECT(bridges)) {
436 pthread_mutex_lock(&g_stats_lock);
437 for (size_t i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
438 bridge = YAJL_GET_OBJECT(bridges)->values[i];
439 ovs_stats_del_bridge(bridge);
441 pthread_mutex_unlock(&g_stats_lock);
446 /* Handle JSON with Bridge table initial values */
447 static void ovs_stats_bridge_table_result_cb(yajl_val jresult,
449 if (YAJL_IS_NULL(jerror))
450 ovs_stats_bridge_table_change_cb(jresult);
452 ERROR("%s: Error received from OvSDB. Table: Bridge", plugin_name);
456 /* Update port name */
457 static int ovs_stats_update_port(const char *uuid, yajl_val port) {
458 const char *new[] = {"new", NULL};
459 const char *name[] = {"name", NULL};
461 port_list_t *portentry = NULL;
462 if (port && YAJL_IS_OBJECT(port)) {
463 row = yajl_tree_get(port, new, yajl_t_object);
464 if (row && YAJL_IS_OBJECT(row)) {
465 yajl_val port_name = yajl_tree_get(row, name, yajl_t_string);
466 if (port_name && YAJL_IS_STRING(port_name)) {
467 portentry = ovs_stats_get_port(uuid);
468 if (portentry == NULL)
469 portentry = ovs_stats_new_port(NULL, uuid);
471 pthread_mutex_lock(&g_stats_lock);
472 sstrncpy(portentry->name, YAJL_GET_STRING(port_name),
473 sizeof(portentry->name));
474 pthread_mutex_unlock(&g_stats_lock);
479 ERROR("Incorrect JSON Port data");
485 /* Delete port from global port list */
486 static int ovs_stats_del_port(const char *uuid) {
487 port_list_t *prev_port = g_port_list_head;
488 for (port_list_t *port = g_port_list_head; port != NULL;
489 prev_port = port, port = port->next) {
490 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0) {
491 if (port == g_port_list_head)
492 g_port_list_head = port->next;
494 prev_port->next = port->next;
502 /* Handle JSON with Port Table change event */
503 static void ovs_stats_port_table_change_cb(yajl_val jupdates) {
504 /* Port Table update example JSON data
507 "ab107d6f-28a1-4257-b1cc-5b742821db8a": {
512 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
519 const char *path[] = {"Port", NULL};
520 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
522 if (ports && YAJL_IS_OBJECT(ports)) {
523 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
524 port = YAJL_GET_OBJECT(ports)->values[i];
525 ovs_stats_update_port(YAJL_GET_OBJECT(ports)->keys[i], port);
531 /* Handle JSON with Port table initial values */
532 static void ovs_stats_port_table_result_cb(yajl_val jresult, yajl_val jerror) {
533 if (YAJL_IS_NULL(jerror))
534 ovs_stats_port_table_change_cb(jresult);
536 ERROR("%s: Error received from OvSDB. Table: Port", plugin_name);
540 /* Handle Port Table delete event */
541 static void ovs_stats_port_table_delete_cb(yajl_val jupdates) {
542 const char *path[] = {"Port", NULL};
543 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
544 pthread_mutex_lock(&g_stats_lock);
545 if (ports && YAJL_IS_OBJECT(ports))
546 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
547 ovs_stats_del_port(YAJL_GET_OBJECT(ports)->keys[i]);
549 pthread_mutex_unlock(&g_stats_lock);
553 /* Update interface statistics */
554 static int ovs_stats_update_iface_stats(port_list_t *port, yajl_val stats) {
556 iface_counter counter_index = 0;
557 char *counter_name = NULL;
558 int64_t counter_value = 0;
559 if (stats && YAJL_IS_ARRAY(stats))
560 for (size_t i = 0; i < YAJL_GET_ARRAY(stats)->len; i++) {
561 stat = YAJL_GET_ARRAY(stats)->values[i];
562 if (!YAJL_IS_ARRAY(stat))
564 counter_name = YAJL_GET_STRING(YAJL_GET_ARRAY(stat)->values[0]);
565 counter_index = ovs_stats_counter_name_to_type(counter_name);
566 counter_value = YAJL_GET_INTEGER(YAJL_GET_ARRAY(stat)->values[1]);
567 if (counter_index == not_supported)
569 port->stats[counter_index] = counter_value;
575 /* Update interface external_ids */
576 static int ovs_stats_update_iface_ext_ids(port_list_t *port, yajl_val ext_ids) {
581 if (ext_ids && YAJL_IS_ARRAY(ext_ids))
582 for (size_t i = 0; i < YAJL_GET_ARRAY(ext_ids)->len; i++) {
583 ext_id = YAJL_GET_ARRAY(ext_ids)->values[i];
584 if (!YAJL_IS_ARRAY(ext_id))
586 key = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[0]);
587 value = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[1]);
589 if (strncmp(key, "iface-id", strlen(key)) == 0)
590 sstrncpy(port->ex_iface_id, value, sizeof(port->ex_iface_id));
591 else if (strncmp(key, "vm-uuid", strlen(key)) == 0)
592 sstrncpy(port->ex_vm_id, value, sizeof(port->ex_vm_id));
599 /* Get interface statistic and external_ids */
600 static int ovs_stats_update_iface(yajl_val iface) {
601 if (!iface || !YAJL_IS_OBJECT(iface)) {
602 ERROR("ovs_stats plugin: incorrect JSON port data");
606 yajl_val row = ovs_utils_get_value_by_key(iface, "new");
607 if (!row || !YAJL_IS_OBJECT(row))
610 yajl_val iface_name = ovs_utils_get_value_by_key(row, "name");
611 if (!iface_name || !YAJL_IS_STRING(iface_name))
614 port_list_t *port = ovs_stats_get_port_by_name(YAJL_GET_STRING(iface_name));
618 yajl_val iface_stats = ovs_utils_get_value_by_key(row, "statistics");
619 yajl_val iface_ext_ids = ovs_utils_get_value_by_key(row, "external_ids");
620 yajl_val iface_uuid = ovs_utils_get_value_by_key(row, "_uuid");
638 Check that statistics is an array with 2 elements
640 if (iface_stats && YAJL_IS_ARRAY(iface_stats) &&
641 YAJL_GET_ARRAY(iface_stats)->len == 2)
642 ovs_stats_update_iface_stats(port, YAJL_GET_ARRAY(iface_stats)->values[1]);
643 if (iface_ext_ids && YAJL_IS_ARRAY(iface_ext_ids))
644 ovs_stats_update_iface_ext_ids(port,
645 YAJL_GET_ARRAY(iface_ext_ids)->values[1]);
646 if (iface_uuid && YAJL_IS_ARRAY(iface_uuid) &&
647 YAJL_GET_ARRAY(iface_uuid)->len == 2 &&
648 YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]) != NULL)
649 sstrncpy(port->iface_uuid,
650 YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]),
651 sizeof(port->iface_uuid));
653 ERROR("ovs_stats plugin: incorrect JSON interface data");
660 /* Handle JSON with Interface Table change event */
661 static void ovs_stats_interface_table_change_cb(yajl_val jupdates) {
662 /* Interface Table update example JSON data
665 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6": {
688 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
699 "a61b7e2b-6951-488a-b4c6-6e91343960b2"
712 const char *path[] = {"Interface", NULL};
713 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
714 pthread_mutex_lock(&g_stats_lock);
715 if (ports && YAJL_IS_OBJECT(ports))
716 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++)
717 ovs_stats_update_iface(YAJL_GET_OBJECT(ports)->values[i]);
718 pthread_mutex_unlock(&g_stats_lock);
722 /* Handle JSON with Interface table initial values */
723 static void ovs_stats_interface_table_result_cb(yajl_val jresult,
725 if (YAJL_IS_NULL(jerror))
726 ovs_stats_interface_table_change_cb(jresult);
728 ERROR("%s: Error received from OvSDB. Table: Interface", plugin_name);
732 /* Setup OVS DB table callbacks */
733 static void ovs_stats_initialize(ovs_db_t *pdb) {
734 const char *bridge_columns[] = {"name", "ports", NULL};
735 const char *port_columns[] = {"name", "interfaces", NULL};
736 const char *interface_columns[] = {"name", "statistics", "_uuid",
737 "external_ids", NULL};
739 /* subscribe to a tables */
740 ovs_db_table_cb_register(
741 pdb, "Bridge", bridge_columns, ovs_stats_bridge_table_change_cb,
742 ovs_stats_bridge_table_result_cb,
743 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
744 OVS_DB_TABLE_CB_FLAG_MODIFY);
746 ovs_db_table_cb_register(pdb, "Bridge", bridge_columns,
747 ovs_stats_bridge_table_delete_cb, NULL,
748 OVS_DB_TABLE_CB_FLAG_DELETE);
750 ovs_db_table_cb_register(
751 pdb, "Port", port_columns, ovs_stats_port_table_change_cb,
752 ovs_stats_port_table_result_cb,
753 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
754 OVS_DB_TABLE_CB_FLAG_MODIFY);
756 ovs_db_table_cb_register(pdb, "Port", port_columns,
757 ovs_stats_port_table_delete_cb, NULL,
758 OVS_DB_TABLE_CB_FLAG_DELETE);
760 ovs_db_table_cb_register(
761 pdb, "Interface", interface_columns, ovs_stats_interface_table_change_cb,
762 ovs_stats_interface_table_result_cb,
763 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
764 OVS_DB_TABLE_CB_FLAG_MODIFY);
767 /* Check if bridge is configured to be monitored in config file */
768 static int ovs_stats_is_monitored_bridge(const char *br_name) {
769 /* if no bridges are configured, return true */
770 if (g_monitored_bridge_list_head == NULL)
773 /* check if given bridge exists */
774 if (ovs_stats_get_bridge(g_monitored_bridge_list_head, br_name) != NULL)
780 /* Delete all ports from port list */
781 static void ovs_stats_free_port_list(port_list_t *head) {
782 for (port_list_t *i = head; i != NULL;) {
783 port_list_t *del = i;
789 /* Delete all bridges from bridge list */
790 static void ovs_stats_free_bridge_list(bridge_list_t *head) {
791 for (bridge_list_t *i = head; i != NULL;) {
792 bridge_list_t *del = i;
799 /* Handle OVSDB lost connection callback */
800 static void ovs_stats_conn_terminate() {
801 WARNING("Lost connection to OVSDB server");
802 pthread_mutex_lock(&g_stats_lock);
803 ovs_stats_free_bridge_list(g_bridge_list_head);
804 g_bridge_list_head = NULL;
805 ovs_stats_free_port_list(g_port_list_head);
806 g_port_list_head = NULL;
807 pthread_mutex_unlock(&g_stats_lock);
810 /* Parse plugin configuration file and store the config
811 * in allocated memory. Returns negative value in case of error.
813 static int ovs_stats_plugin_config(oconfig_item_t *ci) {
814 bridge_list_t *bridge;
816 for (int i = 0; i < ci->children_num; i++) {
817 oconfig_item_t *child = ci->children + i;
818 if (strcasecmp("Address", child->key) == 0) {
819 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_node,
820 OVS_DB_ADDR_NODE_SIZE) != 0) {
821 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
824 } else if (strcasecmp("Port", child->key) == 0) {
825 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_serv,
826 OVS_DB_ADDR_SERVICE_SIZE) != 0) {
827 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
830 } else if (strcasecmp("Socket", child->key) == 0) {
831 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_unix,
832 OVS_DB_ADDR_UNIX_SIZE) != 0) {
833 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
836 } else if (strcasecmp("Bridges", child->key) == 0) {
837 for (int j = 0; j < child->values_num; j++) {
838 /* check value type */
839 if (child->values[j].type != OCONFIG_TYPE_STRING) {
840 ERROR("%s: Wrong bridge name [idx=%d]. "
841 "Bridge name should be string",
846 char const *br_name = child->values[j].value.string;
847 if ((bridge = ovs_stats_get_bridge(g_monitored_bridge_list_head,
849 if ((bridge = calloc(1, sizeof(bridge_list_t))) == NULL) {
850 ERROR("%s: Error allocating memory for bridge", plugin_name);
853 char *br_name_dup = strdup(br_name);
854 if (br_name_dup == NULL) {
855 ERROR("%s: strdup() copy bridge name fail", plugin_name);
860 pthread_mutex_lock(&g_stats_lock);
861 /* store bridge name */
862 bridge->name = br_name_dup;
863 bridge->next = g_monitored_bridge_list_head;
864 g_monitored_bridge_list_head = bridge;
865 pthread_mutex_unlock(&g_stats_lock);
866 DEBUG("%s: found monitored interface \"%s\"", plugin_name, br_name);
871 WARNING("%s: option '%s' not allowed here", plugin_name, child->key);
878 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
882 /* Initialize OvS Stats plugin*/
883 static int ovs_stats_plugin_init(void) {
884 ovs_db_callback_t cb = {.post_conn_init = ovs_stats_initialize,
885 .post_conn_terminate = ovs_stats_conn_terminate};
887 INFO("%s: Connecting to OVS DB using address=%s, service=%s, unix=%s",
888 plugin_name, ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
889 ovs_stats_cfg.ovs_db_unix);
890 /* connect to OvS DB */
892 ovs_db_init(ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
893 ovs_stats_cfg.ovs_db_unix, &cb)) == NULL) {
894 ERROR("%s: plugin: failed to connect to OvS DB server", plugin_name);
897 int err = pthread_mutex_init(&g_stats_lock, NULL);
899 ERROR("%s: plugin: failed to initialize cache lock", plugin_name);
900 ovs_db_destroy(g_ovs_db);
906 /* OvS stats read callback. Read bridge/port information and submit it*/
907 static int ovs_stats_plugin_read(__attribute__((unused)) user_data_t *ud) {
908 bridge_list_t *bridge;
910 char devname[PORT_NAME_SIZE_MAX * 2];
912 pthread_mutex_lock(&g_stats_lock);
913 for (bridge = g_bridge_list_head; bridge != NULL; bridge = bridge->next) {
914 if (ovs_stats_is_monitored_bridge(bridge->name)) {
915 for (port = g_port_list_head; port != NULL; port = port->next)
916 if (port->br == bridge) {
917 if (strlen(port->name) == 0)
918 /* Skip port w/o name. This is possible when read callback
919 * is called after Interface Table update callback but before
920 * Port table Update callback. Will add this port on next read */
922 meta_data_t *meta = meta_data_create();
924 meta_data_add_string(meta, "uuid", port->iface_uuid);
925 if (strlen(port->ex_vm_id))
926 meta_data_add_string(meta, "vm-uuid", port->ex_vm_id);
927 if (strlen(port->ex_iface_id))
928 meta_data_add_string(meta, "iface-id", port->ex_iface_id);
930 snprintf(devname, sizeof(devname), "%s.%s", bridge->name, port->name);
931 ovs_stats_submit_one(devname, "if_collisions", NULL,
932 port->stats[collisions], meta);
933 ovs_stats_submit_two(devname, "if_dropped", NULL,
934 port->stats[rx_dropped], port->stats[tx_dropped],
936 ovs_stats_submit_two(devname, "if_errors", NULL,
937 port->stats[rx_errors], port->stats[tx_errors],
939 ovs_stats_submit_two(devname, "if_packets", NULL,
940 port->stats[rx_packets], port->stats[tx_packets],
942 ovs_stats_submit_one(devname, "if_rx_errors", "crc",
943 port->stats[rx_crc_err], meta);
944 ovs_stats_submit_one(devname, "if_rx_errors", "frame",
945 port->stats[rx_frame_err], meta);
946 ovs_stats_submit_one(devname, "if_rx_errors", "over",
947 port->stats[rx_over_err], meta);
948 ovs_stats_submit_one(devname, "if_rx_octets", NULL,
949 port->stats[rx_bytes], meta);
950 ovs_stats_submit_one(devname, "if_tx_octets", NULL,
951 port->stats[tx_bytes], meta);
952 ovs_stats_submit_two(devname, "if_packets", "1_to_64_packets",
953 port->stats[rx_1_to_64_packets],
954 port->stats[tx_1_to_64_packets], meta);
955 ovs_stats_submit_two(devname, "if_packets", "65_to_127_packets",
956 port->stats[rx_65_to_127_packets],
957 port->stats[tx_65_to_127_packets], meta);
958 ovs_stats_submit_two(devname, "if_packets", "128_to_255_packets",
959 port->stats[rx_128_to_255_packets],
960 port->stats[tx_128_to_255_packets], meta);
961 ovs_stats_submit_two(devname, "if_packets", "256_to_511_packets",
962 port->stats[rx_256_to_511_packets],
963 port->stats[tx_256_to_511_packets], meta);
964 ovs_stats_submit_two(devname, "if_packets", "512_to_1023_packets",
965 port->stats[rx_512_to_1023_packets],
966 port->stats[tx_512_to_1023_packets], meta);
967 ovs_stats_submit_two(devname, "if_packets", "1024_to_1518_packets",
968 port->stats[rx_1024_to_1522_packets],
969 port->stats[tx_1024_to_1522_packets], meta);
970 ovs_stats_submit_two(devname, "if_packets", "1523_to_max_packets",
971 port->stats[rx_1523_to_max_packets],
972 port->stats[tx_1523_to_max_packets], meta);
973 ovs_stats_submit_two(devname, "if_packets", "broadcast_packets",
974 port->stats[rx_broadcast_packets],
975 port->stats[tx_broadcast_packets], meta);
976 ovs_stats_submit_one(devname, "if_multicast", "tx_multicast_packets",
977 port->stats[tx_multicast_packets], meta);
978 ovs_stats_submit_one(devname, "if_rx_errors", "rx_undersized_errors",
979 port->stats[rx_undersized_errors], meta);
980 ovs_stats_submit_one(devname, "if_rx_errors", "rx_oversize_errors",
981 port->stats[rx_oversize_errors], meta);
982 ovs_stats_submit_one(devname, "if_rx_errors", "rx_fragmented_errors",
983 port->stats[rx_fragmented_errors], meta);
984 ovs_stats_submit_one(devname, "if_rx_errors", "rx_jabber_errors",
985 port->stats[rx_jabber_errors], meta);
987 meta_data_destroy(meta);
992 pthread_mutex_unlock(&g_stats_lock);
996 /* Shutdown OvS Stats plugin */
997 static int ovs_stats_plugin_shutdown(void) {
998 DEBUG("OvS Statistics plugin shutting down");
999 ovs_db_destroy(g_ovs_db);
1000 pthread_mutex_lock(&g_stats_lock);
1001 ovs_stats_free_bridge_list(g_bridge_list_head);
1002 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
1003 ovs_stats_free_port_list(g_port_list_head);
1004 pthread_mutex_unlock(&g_stats_lock);
1005 pthread_mutex_destroy(&g_stats_lock);
1009 /* Register OvS Stats plugin callbacks */
1010 void module_register(void) {
1011 plugin_register_complex_config(plugin_name, ovs_stats_plugin_config);
1012 plugin_register_init(plugin_name, ovs_stats_plugin_init);
1013 plugin_register_complex_read(NULL, plugin_name, ovs_stats_plugin_read, 0,
1015 plugin_register_shutdown(plugin_name, ovs_stats_plugin_shutdown);