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 of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Taras Chornyi <tarasx.chornyi@intel.com>
30 #include "utils_ovs.h" /* OvS helpers */
33 static const char plugin_name[] = "ovs_stats";
35 typedef enum iface_counter {
51 rx_128_to_255_packets,
52 rx_256_to_511_packets,
53 rx_512_to_1023_packets,
54 rx_1024_to_1522_packets,
55 rx_1523_to_max_packets,
58 tx_128_to_255_packets,
59 tx_256_to_511_packets,
60 tx_512_to_1023_packets,
61 tx_1024_to_1522_packets,
62 tx_1523_to_max_packets,
73 #define IFACE_COUNTER_MAX (__iface_counter_max - 1)
74 #define IFACE_COUNTER_COUNT (__iface_counter_max)
75 #define PORT_NAME_SIZE_MAX 255
78 typedef struct port_s {
79 char name[PORT_NAME_SIZE_MAX]; /* Port name */
80 char port_uuid[UUID_SIZE]; /* Port table _uuid */
81 char iface_uuid[UUID_SIZE]; /* Interface table uuid */
82 char ex_iface_id[UUID_SIZE]; /* External iface id */
83 char ex_vm_id[UUID_SIZE]; /* External vm id */
84 int64_t stats[IFACE_COUNTER_COUNT]; /* Port statistics */
85 struct bridge_list_s *br; /* Pointer to bridge */
86 struct port_s *next; /* Next port */
89 typedef struct bridge_list_s {
90 char *name; /* Bridge name */
91 struct bridge_list_s *next; /* Next bridge*/
94 static const char *const iface_counter_table[IFACE_COUNTER_COUNT] = {
95 [collisions] = "collisions",
96 [rx_bytes] = "rx_bytes",
97 [rx_crc_err] = "rx_crc_err",
98 [rx_dropped] = "rx_dropped",
99 [rx_errors] = "rx_errors",
100 [rx_frame_err] = "rx_frame_err",
101 [rx_over_err] = "rx_over_err",
102 [rx_packets] = "rx_packets",
103 [tx_bytes] = "tx_bytes",
104 [tx_dropped] = "tx_dropped",
105 [tx_errors] = "tx_errors",
106 [tx_packets] = "tx_packets",
107 [rx_1_to_64_packets] = "rx_1_to_64_packets",
108 [rx_65_to_127_packets] = "rx_65_to_127_packets",
109 [rx_128_to_255_packets] = "rx_128_to_255_packets",
110 [rx_256_to_511_packets] = "rx_256_to_511_packets",
111 [rx_512_to_1023_packets] = "rx_512_to_1023_packets",
112 [rx_1024_to_1522_packets] = "rx_1024_to_1518_packets",
113 [rx_1523_to_max_packets] = "rx_1523_to_max_packets",
114 [tx_1_to_64_packets] = "tx_1_to_64_packets",
115 [tx_65_to_127_packets] = "tx_65_to_127_packets",
116 [tx_128_to_255_packets] = "tx_128_to_255_packets",
117 [tx_256_to_511_packets] = "tx_256_to_511_packets",
118 [tx_512_to_1023_packets] = "tx_512_to_1023_packets",
119 [tx_1024_to_1522_packets] = "tx_1024_to_1518_packets",
120 [tx_1523_to_max_packets] = "tx_1523_to_max_packets",
121 [tx_multicast_packets] = "tx_multicast_packets",
122 [rx_broadcast_packets] = "rx_broadcast_packets",
123 [tx_broadcast_packets] = "tx_broadcast_packets",
124 [rx_undersized_errors] = "rx_undersized_errors",
125 [rx_oversize_errors] = "rx_oversize_errors",
126 [rx_fragmented_errors] = "rx_fragmented_errors",
127 [rx_jabber_errors] = "rx_jabber_errors",
130 /* Entry into the list of network bridges */
131 static bridge_list_t *g_bridge_list_head;
133 /* Entry into the list of monitored network bridges */
134 static bridge_list_t *g_monitored_bridge_list_head;
136 /* entry into the list of network bridges */
137 static port_list_t *g_port_list_head;
139 /* lock for statistics cache */
140 static pthread_mutex_t g_stats_lock;
143 static ovs_db_t *g_ovs_db;
145 /* OVS stats configuration data */
146 struct ovs_stats_config_s {
147 char ovs_db_node[OVS_DB_ADDR_NODE_SIZE]; /* OVS DB node */
148 char ovs_db_serv[OVS_DB_ADDR_SERVICE_SIZE]; /* OVS DB service */
149 char ovs_db_unix[OVS_DB_ADDR_UNIX_SIZE]; /* OVS DB unix socket path */
151 typedef struct ovs_stats_config_s ovs_stats_config_t;
153 static ovs_stats_config_t ovs_stats_cfg = {
154 .ovs_db_node = "localhost", /* use default OVS DB node */
155 .ovs_db_serv = "6640", /* use default OVS DB service */
158 static iface_counter ovs_stats_counter_name_to_type(const char *counter) {
159 iface_counter index = not_supported;
162 return not_supported;
164 for (int i = 0; i < IFACE_COUNTER_COUNT; i++) {
165 if (strncmp(iface_counter_table[i], counter,
166 strlen(iface_counter_table[i])) == 0) {
174 static void ovs_stats_submit_one(const char *dev, const char *type,
175 const char *type_instance, derive_t value,
177 /* if counter is less than 0 - skip it*/
180 value_list_t vl = VALUE_LIST_INIT;
182 vl.values = &(value_t){.derive = value};
186 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
187 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
188 sstrncpy(vl.type, type, sizeof(vl.type));
190 if (type_instance != NULL)
191 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
193 plugin_dispatch_values(&vl);
196 static void ovs_stats_submit_two(const char *dev, const char *type,
197 const char *type_instance, derive_t rx,
198 derive_t tx, meta_data_t *meta) {
199 /* if counter is less than 0 - skip it*/
200 if (rx < 0 || tx < 0)
202 value_list_t vl = VALUE_LIST_INIT;
203 value_t values[] = {{.derive = rx}, {.derive = tx}};
206 vl.values_len = STATIC_ARRAY_SIZE(values);
209 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
210 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
211 sstrncpy(vl.type, type, sizeof(vl.type));
213 if (type_instance != NULL)
214 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
216 plugin_dispatch_values(&vl);
219 static port_list_t *ovs_stats_get_port(const char *uuid) {
223 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
224 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0)
230 static port_list_t *ovs_stats_get_port_by_name(const char *name) {
234 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next)
235 if ((strncmp(port->name, name, strlen(port->name)) == 0) &&
236 strlen(name) == strlen(port->name))
241 /* Create or get port by port uuid */
242 static port_list_t *ovs_stats_new_port(bridge_list_t *bridge,
244 port_list_t *port = ovs_stats_get_port(uuid);
247 port = (port_list_t *)calloc(1, sizeof(port_list_t));
249 ERROR("%s: Error allocating port", plugin_name);
252 memset(port->stats, -1, sizeof(int64_t[IFACE_COUNTER_COUNT]));
253 sstrncpy(port->port_uuid, uuid, sizeof(port->port_uuid));
254 pthread_mutex_lock(&g_stats_lock);
255 port->next = g_port_list_head;
256 g_port_list_head = port;
257 pthread_mutex_unlock(&g_stats_lock);
259 if (bridge != NULL) {
260 pthread_mutex_lock(&g_stats_lock);
262 pthread_mutex_unlock(&g_stats_lock);
267 /* Get bridge by name*/
268 static bridge_list_t *ovs_stats_get_bridge(bridge_list_t *head,
273 for (bridge_list_t *bridge = head; bridge != NULL; bridge = bridge->next) {
274 if ((strncmp(bridge->name, name, strlen(bridge->name)) == 0) &&
275 strlen(name) == strlen(bridge->name))
282 static int ovs_stats_del_bridge(yajl_val bridge) {
283 const char *old[] = {"old", NULL};
284 const char *name[] = {"name", NULL};
288 if (bridge && YAJL_IS_OBJECT(bridge)) {
289 row = yajl_tree_get(bridge, old, yajl_t_object);
290 if (row && YAJL_IS_OBJECT(row)) {
291 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
292 if (br_name && YAJL_IS_STRING(br_name)) {
293 bridge_list_t *prev_br = g_bridge_list_head;
294 for (bridge_list_t *br = g_bridge_list_head; br != NULL;
295 prev_br = br, br = br->next) {
296 if ((strncmp(br->name, br_name->u.string, strlen(br->name)) == 0) &&
297 strlen(br->name) == strlen(br_name->u.string)) {
298 if (br == g_bridge_list_head)
299 g_bridge_list_head = br->next;
301 prev_br->next = br->next;
310 WARNING("%s: Incorrect data for deleting bridge", plugin_name);
314 /* Update Bridge. Create bridge ports*/
315 static int ovs_stats_update_bridge(yajl_val bridge) {
316 const char *new[] = {"new", NULL};
317 const char *name[] = {"name", NULL};
318 const char *ports[] = {"ports", NULL};
319 bridge_list_t *br = NULL;
321 if (bridge && YAJL_IS_OBJECT(bridge)) {
322 yajl_val row = yajl_tree_get(bridge, new, yajl_t_object);
323 if (row && YAJL_IS_OBJECT(row)) {
324 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
325 yajl_val br_ports = yajl_tree_get(row, ports, yajl_t_array);
326 if (br_name && YAJL_IS_STRING(br_name)) {
327 br = ovs_stats_get_bridge(g_bridge_list_head, YAJL_GET_STRING(br_name));
328 pthread_mutex_lock(&g_stats_lock);
330 br = (bridge_list_t *)calloc(1, sizeof(bridge_list_t));
332 ERROR("%s: Error allocating memory for bridge", plugin_name);
335 char *tmp = YAJL_GET_STRING(br_name);
338 br->name = strdup(tmp);
339 if (br->name == NULL) {
341 pthread_mutex_unlock(&g_stats_lock);
344 br->next = g_bridge_list_head;
345 g_bridge_list_head = br;
347 pthread_mutex_unlock(&g_stats_lock);
349 if (br_ports && YAJL_IS_ARRAY(br_ports)) {
350 char *tmp = YAJL_GET_STRING(br_ports->u.array.values[0]);
351 if (tmp != NULL && strcmp("set", tmp) == 0) {
352 yajl_val *ports_arr =
353 YAJL_GET_ARRAY(br_ports->u.array.values[1])->values;
354 size_t ports_num = YAJL_GET_ARRAY(br_ports->u.array.values[1])->len;
356 for (int i = 0; i < ports_num; i++)
358 br, YAJL_GET_STRING(ports_arr[i]->u.array.values[1]));
360 ovs_stats_new_port(br, YAJL_GET_STRING(br_ports->u.array.values[1]));
364 ERROR("Incorrect JSON Bridge data");
370 /* Handle JSON with Bridge Table change event */
371 static void ovs_stats_bridge_table_change_cb(yajl_val jupdates) {
372 /* Bridge Table update example JSON data
375 "bb1f8965-5775-46d9-b820-236ca8edbedc": {
383 "117f1a07-7ef0-458a-865c-ec7fbb85bc01"
387 "12fd8bdc-e950-4281-aaa9-46e185658f79"
396 const char *path[] = {"Bridge", NULL};
398 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
400 if (bridges && YAJL_IS_OBJECT(bridges)) {
401 for (int i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
402 yajl_val bridge = YAJL_GET_OBJECT(bridges)->values[i];
403 ovs_stats_update_bridge(bridge);
408 /* Handle Bridge Table delete event */
409 static void ovs_stats_bridge_table_delete_cb(yajl_val jupdates) {
410 const char *path[] = {"Bridge", NULL};
411 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
413 if (bridges && YAJL_IS_OBJECT(bridges)) {
414 pthread_mutex_lock(&g_stats_lock);
415 for (int i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
416 bridge = YAJL_GET_OBJECT(bridges)->values[i];
417 ovs_stats_del_bridge(bridge);
419 pthread_mutex_unlock(&g_stats_lock);
424 /* Handle JSON with Bridge table initial values */
425 static void ovs_stats_bridge_table_result_cb(yajl_val jresult,
427 if (YAJL_IS_NULL(jerror))
428 ovs_stats_bridge_table_change_cb(jresult);
430 ERROR("%s: Error received from OvSDB. Table: Bridge", plugin_name);
434 /* Update port name */
435 static int ovs_stats_update_port(const char *uuid, yajl_val port) {
436 const char *new[] = {"new", NULL};
437 const char *name[] = {"name", NULL};
439 port_list_t *portentry = NULL;
440 if (port && YAJL_IS_OBJECT(port)) {
441 row = yajl_tree_get(port, new, yajl_t_object);
442 if (row && YAJL_IS_OBJECT(row)) {
443 yajl_val port_name = yajl_tree_get(row, name, yajl_t_string);
444 if (port_name && YAJL_IS_STRING(port_name)) {
445 portentry = ovs_stats_get_port(uuid);
446 if (portentry == NULL)
447 portentry = ovs_stats_new_port(NULL, uuid);
449 pthread_mutex_lock(&g_stats_lock);
450 sstrncpy(portentry->name, YAJL_GET_STRING(port_name),
451 sizeof(portentry->name));
452 pthread_mutex_unlock(&g_stats_lock);
457 ERROR("Incorrect JSON Port data");
463 /* Delete port from global port list */
464 static int ovs_stats_del_port(const char *uuid) {
465 port_list_t *prev_port = g_port_list_head;
466 for (port_list_t *port = g_port_list_head; port != NULL;
467 prev_port = port, port = port->next) {
468 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0) {
469 if (port == g_port_list_head)
470 g_port_list_head = port->next;
472 prev_port->next = port->next;
480 /* Handle JSON with Port Table change event */
481 static void ovs_stats_port_table_change_cb(yajl_val jupdates) {
482 /* Port Table update example JSON data
485 "ab107d6f-28a1-4257-b1cc-5b742821db8a": {
490 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
497 const char *path[] = {"Port", NULL};
498 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
500 if (ports && YAJL_IS_OBJECT(ports)) {
501 for (int i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
502 port = YAJL_GET_OBJECT(ports)->values[i];
503 ovs_stats_update_port(YAJL_GET_OBJECT(ports)->keys[i], port);
509 /* Handle JSON with Port table initial values */
510 static void ovs_stats_port_table_result_cb(yajl_val jresult, yajl_val jerror) {
511 if (YAJL_IS_NULL(jerror))
512 ovs_stats_port_table_change_cb(jresult);
514 ERROR("%s: Error received from OvSDB. Table: Port", plugin_name);
518 /* Handle Port Table delete event */
519 static void ovs_stats_port_table_delete_cb(yajl_val jupdates) {
520 const char *path[] = {"Port", NULL};
521 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
522 pthread_mutex_lock(&g_stats_lock);
523 if (ports && YAJL_IS_OBJECT(ports))
524 for (int i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
525 ovs_stats_del_port(YAJL_GET_OBJECT(ports)->keys[i]);
527 pthread_mutex_unlock(&g_stats_lock);
531 /* Update interface statistics */
532 static int ovs_stats_update_iface_stats(port_list_t *port, yajl_val stats) {
534 iface_counter counter_index = 0;
535 char *counter_name = NULL;
536 int64_t counter_value = 0;
537 if (stats && YAJL_IS_ARRAY(stats))
538 for (int i = 0; i < YAJL_GET_ARRAY(stats)->len; i++) {
539 stat = YAJL_GET_ARRAY(stats)->values[i];
540 counter_name = YAJL_GET_STRING(YAJL_GET_ARRAY(stat)->values[0]);
541 counter_index = ovs_stats_counter_name_to_type(counter_name);
542 counter_value = YAJL_GET_INTEGER(YAJL_GET_ARRAY(stat)->values[1]);
543 if (counter_index == not_supported)
545 port->stats[counter_index] = counter_value;
551 /* Update interface external_ids */
552 static int ovs_stats_update_iface_ext_ids(port_list_t *port, yajl_val ext_ids) {
557 if (ext_ids && YAJL_IS_ARRAY(ext_ids))
558 for (int i = 0; i < YAJL_GET_ARRAY(ext_ids)->len; i++) {
559 ext_id = YAJL_GET_ARRAY(ext_ids)->values[i];
560 key = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[0]);
561 value = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[1]);
563 if (strncmp(key, "iface-id", strlen(key)) == 0)
564 sstrncpy(port->ex_iface_id, value, sizeof(port->ex_iface_id));
565 else if (strncmp(key, "vm-uuid", strlen(key)) == 0)
566 sstrncpy(port->ex_vm_id, value, sizeof(port->ex_vm_id));
573 /* Get interface statistic and external_ids */
574 static int ovs_stats_update_iface(yajl_val iface) {
576 port_list_t *port = NULL;
577 if (iface && YAJL_IS_OBJECT(iface)) {
578 row = ovs_utils_get_value_by_key(iface, "new");
579 if (row && YAJL_IS_OBJECT(row)) {
580 yajl_val iface_name = ovs_utils_get_value_by_key(row, "name");
581 yajl_val iface_stats = ovs_utils_get_value_by_key(row, "statistics");
582 yajl_val iface_ext_ids = ovs_utils_get_value_by_key(row, "external_ids");
583 yajl_val iface_uuid = ovs_utils_get_value_by_key(row, "_uuid");
584 if (iface_name && YAJL_IS_STRING(iface_name)) {
585 port = ovs_stats_get_port_by_name(YAJL_GET_STRING(iface_name));
606 Check that statistics is an array with 2 elements
608 if (iface_stats && YAJL_IS_ARRAY(iface_stats) &&
609 YAJL_GET_ARRAY(iface_stats)->len == 2)
610 ovs_stats_update_iface_stats(port,
611 YAJL_GET_ARRAY(iface_stats)->values[1]);
612 if (iface_ext_ids && YAJL_IS_ARRAY(iface_ext_ids))
613 ovs_stats_update_iface_ext_ids(
614 port, YAJL_GET_ARRAY(iface_ext_ids)->values[1]);
615 if (iface_uuid && YAJL_IS_ARRAY(iface_uuid) &&
616 YAJL_GET_ARRAY(iface_uuid)->len == 2)
617 sstrncpy(port->iface_uuid,
618 YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]),
619 sizeof(port->iface_uuid));
622 ERROR("Incorrect JSON Port data");
628 /* Handle JSON with Interface Table change event */
629 static void ovs_stats_interface_table_change_cb(yajl_val jupdates) {
630 /* Interface Table update example JSON data
633 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6": {
656 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
667 "a61b7e2b-6951-488a-b4c6-6e91343960b2"
680 const char *path[] = {"Interface", NULL};
681 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
682 pthread_mutex_lock(&g_stats_lock);
683 if (ports && YAJL_IS_OBJECT(ports))
684 for (int i = 0; i < YAJL_GET_OBJECT(ports)->len; i++)
685 ovs_stats_update_iface(YAJL_GET_OBJECT(ports)->values[i]);
686 pthread_mutex_unlock(&g_stats_lock);
690 /* Handle JSON with Interface table initial values */
691 static void ovs_stats_interface_table_result_cb(yajl_val jresult,
693 if (YAJL_IS_NULL(jerror))
694 ovs_stats_interface_table_change_cb(jresult);
696 ERROR("%s: Error received from OvSDB. Table: Interface", plugin_name);
700 /* Setup OVS DB table callbacks */
701 static void ovs_stats_initialize(ovs_db_t *pdb) {
702 const char *bridge_columns[] = {"name", "ports", NULL};
703 const char *port_columns[] = {"name", "interfaces", NULL};
704 const char *interface_columns[] = {"name", "statistics", "_uuid",
705 "external_ids", NULL};
707 /* subscribe to a tables */
708 ovs_db_table_cb_register(pdb, "Bridge", bridge_columns,
709 ovs_stats_bridge_table_change_cb,
710 ovs_stats_bridge_table_result_cb,
711 OVS_DB_TABLE_CB_FLAG_INITIAL |
712 OVS_DB_TABLE_CB_FLAG_INSERT |
713 OVS_DB_TABLE_CB_FLAG_MODIFY);
715 ovs_db_table_cb_register(pdb, "Bridge", bridge_columns,
716 ovs_stats_bridge_table_delete_cb, NULL,
717 OVS_DB_TABLE_CB_FLAG_DELETE);
719 ovs_db_table_cb_register(pdb, "Port", port_columns,
720 ovs_stats_port_table_change_cb,
721 ovs_stats_port_table_result_cb,
722 OVS_DB_TABLE_CB_FLAG_INITIAL |
723 OVS_DB_TABLE_CB_FLAG_INSERT |
724 OVS_DB_TABLE_CB_FLAG_MODIFY);
726 ovs_db_table_cb_register(pdb, "Port", port_columns,
727 ovs_stats_port_table_delete_cb, NULL,
728 OVS_DB_TABLE_CB_FLAG_DELETE);
730 ovs_db_table_cb_register(pdb, "Interface", interface_columns,
731 ovs_stats_interface_table_change_cb,
732 ovs_stats_interface_table_result_cb,
733 OVS_DB_TABLE_CB_FLAG_INITIAL |
734 OVS_DB_TABLE_CB_FLAG_INSERT |
735 OVS_DB_TABLE_CB_FLAG_MODIFY);
738 /* Check if bridge is configured to be monitored in config file */
739 static int ovs_stats_is_monitored_bridge(const char *br_name) {
740 /* if no bridges are configured, return true */
741 if (g_monitored_bridge_list_head == NULL)
744 /* check if given bridge exists */
745 if (ovs_stats_get_bridge(g_monitored_bridge_list_head, br_name) != NULL)
751 /* Delete all ports from port list */
752 static void ovs_stats_free_port_list(port_list_t *head) {
753 for (port_list_t *i = head; i != NULL;) {
754 port_list_t *del = i;
760 /* Delete all bridges from bridge list */
761 static void ovs_stats_free_bridge_list(bridge_list_t *head) {
762 for (bridge_list_t *i = head; i != NULL;) {
763 bridge_list_t *del = i;
770 /* Handle OVSDB lost connection callback */
771 static void ovs_stats_conn_terminate() {
772 WARNING("Lost connection to OVSDB server");
773 pthread_mutex_lock(&g_stats_lock);
774 ovs_stats_free_bridge_list(g_bridge_list_head);
775 g_bridge_list_head = NULL;
776 ovs_stats_free_port_list(g_port_list_head);
777 g_port_list_head = NULL;
778 pthread_mutex_unlock(&g_stats_lock);
781 /* Parse plugin configuration file and store the config
782 * in allocated memory. Returns negative value in case of error.
784 static int ovs_stats_plugin_config(oconfig_item_t *ci) {
785 bridge_list_t *bridge;
788 for (int i = 0; i < ci->children_num; i++) {
789 oconfig_item_t *child = ci->children + i;
790 if (strcasecmp("Address", child->key) == 0) {
791 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_node,
792 OVS_DB_ADDR_NODE_SIZE) != 0) {
793 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
796 } else if (strcasecmp("Port", child->key) == 0) {
797 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_serv,
798 OVS_DB_ADDR_SERVICE_SIZE) != 0) {
799 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
802 } else if (strcasecmp("Socket", child->key) == 0) {
803 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_unix,
804 OVS_DB_ADDR_UNIX_SIZE) != 0) {
805 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
808 } else if (strcasecmp("Bridges", child->key) == 0) {
809 for (int j = 0; j < child->values_num; j++) {
810 /* check value type */
811 if (child->values[j].type != OCONFIG_TYPE_STRING) {
812 ERROR("%s: Wrong bridge name [idx=%d]. "
813 "Bridge name should be string",
818 if ((br_name = strdup(child->values[j].value.string)) == NULL) {
819 ERROR("%s: strdup() copy bridge name fail", plugin_name);
822 if ((bridge = ovs_stats_get_bridge(g_monitored_bridge_list_head,
824 if ((bridge = calloc(1, sizeof(bridge_list_t))) == NULL) {
825 ERROR("%s: Error allocating memory for bridge", plugin_name);
828 pthread_mutex_lock(&g_stats_lock);
829 /* store bridge name */
830 bridge->name = br_name;
831 bridge->next = g_monitored_bridge_list_head;
832 g_monitored_bridge_list_head = bridge;
833 pthread_mutex_unlock(&g_stats_lock);
834 DEBUG("%s: found monitored interface \"%s\"", plugin_name, br_name);
839 WARNING("%s: option '%s' not allowed here", plugin_name, child->key);
846 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
850 /* Initialize OvS Stats plugin*/
851 static int ovs_stats_plugin_init(void) {
852 ovs_db_callback_t cb = {.post_conn_init = ovs_stats_initialize,
853 .post_conn_terminate = ovs_stats_conn_terminate};
855 INFO("%s: Connecting to OVS DB using address=%s, service=%s, unix=%s",
856 plugin_name, ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
857 ovs_stats_cfg.ovs_db_unix);
858 /* connect to OvS DB */
859 if ((g_ovs_db = ovs_db_init (ovs_stats_cfg.ovs_db_node,
860 ovs_stats_cfg.ovs_db_serv,
861 ovs_stats_cfg.ovs_db_unix, &cb)) == NULL) {
862 ERROR("%s: plugin: failed to connect to OvS DB server", plugin_name);
865 int err = pthread_mutex_init(&g_stats_lock, NULL);
867 ERROR("%s: plugin: failed to initialize cache lock", plugin_name);
868 ovs_db_destroy(g_ovs_db);
874 /* OvS stats read callback. Read bridge/port information and submit it*/
875 static int ovs_stats_plugin_read(__attribute__((unused)) user_data_t *ud) {
876 bridge_list_t *bridge;
878 char devname[PORT_NAME_SIZE_MAX];
880 pthread_mutex_lock(&g_stats_lock);
881 for (bridge = g_bridge_list_head; bridge != NULL; bridge = bridge->next) {
882 if (ovs_stats_is_monitored_bridge(bridge->name)) {
883 for (port = g_port_list_head; port != NULL; port = port->next)
884 if (port->br == bridge) {
885 if (strlen(port->name) == 0)
886 /* Skip port w/o name. This is possible when read callback
887 * is called after Interface Table update callback but before
888 * Port table Update callback. Will add this port on next read */
890 meta_data_t *meta = meta_data_create();
892 meta_data_add_string(meta, "uuid", port->iface_uuid);
893 if (strlen(port->ex_vm_id))
894 meta_data_add_string(meta, "vm-uuid", port->ex_vm_id);
895 if (strlen(port->ex_iface_id))
896 meta_data_add_string(meta, "iface-id", port->ex_iface_id);
898 snprintf(devname, sizeof(devname), "%s.%s", bridge->name, port->name);
899 ovs_stats_submit_one(devname, "if_collisions", NULL,
900 port->stats[collisions], meta);
901 ovs_stats_submit_two(devname, "if_dropped", NULL,
902 port->stats[rx_dropped], port->stats[tx_dropped],
904 ovs_stats_submit_two(devname, "if_errors", NULL,
905 port->stats[rx_errors], port->stats[tx_errors],
907 ovs_stats_submit_two(devname, "if_packets", NULL,
908 port->stats[rx_packets], port->stats[tx_packets],
910 ovs_stats_submit_one(devname, "if_rx_errors", "crc",
911 port->stats[rx_crc_err], meta);
912 ovs_stats_submit_one(devname, "if_rx_errors", "frame",
913 port->stats[rx_frame_err], meta);
914 ovs_stats_submit_one(devname, "if_rx_errors", "over",
915 port->stats[rx_over_err], meta);
916 ovs_stats_submit_one(devname, "if_rx_octets", NULL,
917 port->stats[rx_bytes], meta);
918 ovs_stats_submit_one(devname, "if_tx_octets", NULL,
919 port->stats[tx_bytes], meta);
920 ovs_stats_submit_two(devname, "if_packets", "1_to_64_packets",
921 port->stats[rx_1_to_64_packets],
922 port->stats[tx_1_to_64_packets], meta);
923 ovs_stats_submit_two(devname, "if_packets", "65_to_127_packets",
924 port->stats[rx_65_to_127_packets],
925 port->stats[tx_65_to_127_packets], meta);
926 ovs_stats_submit_two(devname, "if_packets", "128_to_255_packets",
927 port->stats[rx_128_to_255_packets],
928 port->stats[tx_128_to_255_packets], meta);
929 ovs_stats_submit_two(devname, "if_packets", "256_to_511_packets",
930 port->stats[rx_256_to_511_packets],
931 port->stats[tx_256_to_511_packets], meta);
932 ovs_stats_submit_two(devname, "if_packets", "512_to_1023_packets",
933 port->stats[rx_512_to_1023_packets],
934 port->stats[tx_512_to_1023_packets], meta);
935 ovs_stats_submit_two(devname, "if_packets", "1024_to_1518_packets",
936 port->stats[rx_1024_to_1522_packets],
937 port->stats[tx_1024_to_1522_packets], meta);
938 ovs_stats_submit_two(devname, "if_packets", "1523_to_max_packets",
939 port->stats[rx_1523_to_max_packets],
940 port->stats[tx_1523_to_max_packets], meta);
941 ovs_stats_submit_two(devname, "if_packets", "broadcast_packets",
942 port->stats[rx_broadcast_packets],
943 port->stats[tx_broadcast_packets], meta);
944 ovs_stats_submit_one(devname, "if_multicast", "tx_multicast_packets",
945 port->stats[tx_multicast_packets], meta);
946 ovs_stats_submit_one(devname, "if_rx_errors", "rx_undersized_errors",
947 port->stats[rx_undersized_errors], meta);
948 ovs_stats_submit_one(devname, "if_rx_errors", "rx_oversize_errors",
949 port->stats[rx_oversize_errors], meta);
950 ovs_stats_submit_one(devname, "if_rx_errors", "rx_fragmented_errors",
951 port->stats[rx_fragmented_errors], meta);
952 ovs_stats_submit_one(devname, "if_rx_errors", "rx_jabber_errors",
953 port->stats[rx_jabber_errors], meta);
955 meta_data_destroy(meta);
960 pthread_mutex_unlock(&g_stats_lock);
964 /* Shutdown OvS Stats plugin */
965 static int ovs_stats_plugin_shutdown(void) {
966 pthread_mutex_lock(&g_stats_lock);
967 DEBUG("OvS Statistics plugin shutting down");
968 ovs_db_destroy(g_ovs_db);
969 ovs_stats_free_bridge_list(g_bridge_list_head);
970 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
971 ovs_stats_free_port_list(g_port_list_head);
972 pthread_mutex_unlock(&g_stats_lock);
973 pthread_mutex_destroy(&g_stats_lock);
977 /* Register OvS Stats plugin callbacks */
978 void module_register(void) {
979 plugin_register_complex_config(plugin_name, ovs_stats_plugin_config);
980 plugin_register_init(plugin_name, ovs_stats_plugin_init);
981 plugin_register_complex_read(NULL, plugin_name, ovs_stats_plugin_read, 0,
983 plugin_register_shutdown(plugin_name, ovs_stats_plugin_shutdown);