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 interface_s {
82 char name[PORT_NAME_SIZE_MAX]; /* Interface 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]; /* Statistics for interface */
88 struct interface_s *next; /* Next interface for associated port */
91 typedef struct port_s {
92 char name[PORT_NAME_SIZE_MAX]; /* Port name */
93 char port_uuid[UUID_SIZE]; /* Port table _uuid */
94 struct bridge_list_s *br; /* Pointer to bridge */
95 struct interface_s *iface; /* Pointer to first interface */
96 struct port_s *next; /* Next port */
99 typedef struct bridge_list_s {
100 char *name; /* Bridge name */
101 struct bridge_list_s *next; /* Next bridge*/
104 #define cnt_str(x) [x] = #x
106 static const char *const iface_counter_table[IFACE_COUNTER_COUNT] = {
112 cnt_str(rx_frame_err),
113 cnt_str(rx_over_err),
119 cnt_str(rx_1_to_64_packets),
120 cnt_str(rx_65_to_127_packets),
121 cnt_str(rx_128_to_255_packets),
122 cnt_str(rx_256_to_511_packets),
123 cnt_str(rx_512_to_1023_packets),
124 cnt_str(rx_1024_to_1522_packets),
125 cnt_str(rx_1523_to_max_packets),
126 cnt_str(tx_1_to_64_packets),
127 cnt_str(tx_65_to_127_packets),
128 cnt_str(tx_128_to_255_packets),
129 cnt_str(tx_256_to_511_packets),
130 cnt_str(tx_512_to_1023_packets),
131 cnt_str(tx_1024_to_1522_packets),
132 cnt_str(tx_1523_to_max_packets),
133 cnt_str(tx_multicast_packets),
134 cnt_str(rx_broadcast_packets),
135 cnt_str(tx_broadcast_packets),
136 cnt_str(rx_undersized_errors),
137 cnt_str(rx_oversize_errors),
138 cnt_str(rx_fragmented_errors),
139 cnt_str(rx_jabber_errors),
144 /* Entry into the list of network bridges */
145 static bridge_list_t *g_bridge_list_head;
147 /* Entry into the list of monitored network bridges */
148 static bridge_list_t *g_monitored_bridge_list_head;
150 /* entry into the list of network bridges */
151 static port_list_t *g_port_list_head;
153 /* lock for statistics cache */
154 static pthread_mutex_t g_stats_lock;
157 static ovs_db_t *g_ovs_db;
159 /* OVS stats configuration data */
160 struct ovs_stats_config_s {
161 char ovs_db_node[OVS_DB_ADDR_NODE_SIZE]; /* OVS DB node */
162 char ovs_db_serv[OVS_DB_ADDR_SERVICE_SIZE]; /* OVS DB service */
163 char ovs_db_unix[OVS_DB_ADDR_UNIX_SIZE]; /* OVS DB unix socket path */
165 typedef struct ovs_stats_config_s ovs_stats_config_t;
167 static ovs_stats_config_t ovs_stats_cfg = {
168 .ovs_db_node = "localhost", /* use default OVS DB node */
169 .ovs_db_serv = "6640", /* use default OVS DB service */
172 /* flag indicating whether or not to publish individual interface statistics */
173 static bool interface_stats = false;
175 static iface_counter ovs_stats_counter_name_to_type(const char *counter) {
176 iface_counter index = not_supported;
179 return not_supported;
181 for (int i = 0; i < IFACE_COUNTER_COUNT; i++) {
182 if (strncmp(iface_counter_table[i], counter,
183 strlen(iface_counter_table[i])) == 0) {
191 static void ovs_stats_submit_one(const char *dev, const char *type,
192 const char *type_instance, derive_t value,
194 /* if counter is less than 0 - skip it*/
197 value_list_t vl = VALUE_LIST_INIT;
199 vl.values = &(value_t){.derive = value};
203 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
204 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
205 sstrncpy(vl.type, type, sizeof(vl.type));
207 if (type_instance != NULL)
208 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
210 plugin_dispatch_values(&vl);
213 static void ovs_stats_submit_two(const char *dev, const char *type,
214 const char *type_instance, derive_t rx,
215 derive_t tx, meta_data_t *meta) {
216 /* if counter is less than 0 - skip it*/
217 if (rx < 0 || tx < 0)
219 value_list_t vl = VALUE_LIST_INIT;
220 value_t values[] = {{.derive = rx}, {.derive = tx}};
223 vl.values_len = STATIC_ARRAY_SIZE(values);
226 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
227 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
228 sstrncpy(vl.type, type, sizeof(vl.type));
230 if (type_instance != NULL)
231 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
233 plugin_dispatch_values(&vl);
236 static void ovs_stats_submit_interfaces(bridge_list_t *bridge,
238 char devname[PORT_NAME_SIZE_MAX * 2];
240 for (interface_list_t *iface = port->iface; iface != NULL;
241 iface = iface->next) {
242 meta_data_t *meta = meta_data_create();
244 meta_data_add_string(meta, "uuid", iface->iface_uuid);
246 if (strlen(iface->ex_vm_id))
247 meta_data_add_string(meta, "vm-uuid", iface->ex_vm_id);
249 if (strlen(iface->ex_iface_id))
250 meta_data_add_string(meta, "iface-id", iface->ex_iface_id);
252 snprintf(devname, sizeof(devname), "%s.%s.%s", bridge->name, port->name,
254 ovs_stats_submit_one(devname, "if_collisions", NULL,
255 iface->stats[collisions], meta);
256 ovs_stats_submit_two(devname, "if_dropped", NULL, iface->stats[rx_dropped],
257 iface->stats[tx_dropped], meta);
258 ovs_stats_submit_two(devname, "if_errors", NULL, iface->stats[rx_errors],
259 iface->stats[tx_errors], meta);
260 ovs_stats_submit_two(devname, "if_packets", NULL, iface->stats[rx_packets],
261 iface->stats[tx_packets], meta);
262 ovs_stats_submit_one(devname, "if_rx_errors", "crc",
263 iface->stats[rx_crc_err], meta);
264 ovs_stats_submit_one(devname, "if_rx_errors", "frame",
265 iface->stats[rx_frame_err], meta);
266 ovs_stats_submit_one(devname, "if_rx_errors", "over",
267 iface->stats[rx_over_err], meta);
268 ovs_stats_submit_one(devname, "if_rx_octets", NULL, iface->stats[rx_bytes],
270 ovs_stats_submit_one(devname, "if_tx_octets", NULL, iface->stats[tx_bytes],
272 ovs_stats_submit_two(devname, "if_packets", "1_to_64_packets",
273 iface->stats[rx_1_to_64_packets],
274 iface->stats[tx_1_to_64_packets], meta);
275 ovs_stats_submit_two(devname, "if_packets", "65_to_127_packets",
276 iface->stats[rx_65_to_127_packets],
277 iface->stats[tx_65_to_127_packets], meta);
278 ovs_stats_submit_two(devname, "if_packets", "128_to_255_packets",
279 iface->stats[rx_128_to_255_packets],
280 iface->stats[tx_128_to_255_packets], meta);
281 ovs_stats_submit_two(devname, "if_packets", "256_to_511_packets",
282 iface->stats[rx_256_to_511_packets],
283 iface->stats[tx_256_to_511_packets], meta);
284 ovs_stats_submit_two(devname, "if_packets", "512_to_1023_packets",
285 iface->stats[rx_512_to_1023_packets],
286 iface->stats[tx_512_to_1023_packets], meta);
287 ovs_stats_submit_two(devname, "if_packets", "1024_to_1522_packets",
288 iface->stats[rx_1024_to_1522_packets],
289 iface->stats[tx_1024_to_1522_packets], meta);
290 ovs_stats_submit_two(devname, "if_packets", "1523_to_max_packets",
291 iface->stats[rx_1523_to_max_packets],
292 iface->stats[tx_1523_to_max_packets], meta);
293 ovs_stats_submit_two(devname, "if_packets", "broadcast_packets",
294 iface->stats[rx_broadcast_packets],
295 iface->stats[tx_broadcast_packets], meta);
296 ovs_stats_submit_one(devname, "if_multicast", "tx_multicast_packets",
297 iface->stats[tx_multicast_packets], meta);
298 ovs_stats_submit_one(devname, "if_rx_errors", "rx_undersized_errors",
299 iface->stats[rx_undersized_errors], meta);
300 ovs_stats_submit_one(devname, "if_rx_errors", "rx_oversize_errors",
301 iface->stats[rx_oversize_errors], meta);
302 ovs_stats_submit_one(devname, "if_rx_errors", "rx_fragmented_errors",
303 iface->stats[rx_fragmented_errors], meta);
304 ovs_stats_submit_one(devname, "if_rx_errors", "rx_jabber_errors",
305 iface->stats[rx_jabber_errors], meta);
307 meta_data_destroy(meta);
311 static int ovs_stats_get_port_stat_value(port_list_t *port,
312 iface_counter index) {
318 for (interface_list_t *iface = port->iface; iface != NULL;
319 iface = iface->next) {
320 value = value + iface->stats[index];
326 static void ovs_stats_submit_port(bridge_list_t *bridge, port_list_t *port) {
327 char devname[PORT_NAME_SIZE_MAX * 2];
329 meta_data_t *meta = meta_data_create();
331 char key_str[DATA_MAX_NAME_LEN];
334 for (interface_list_t *iface = port->iface; iface != NULL;
335 iface = iface->next) {
336 snprintf(key_str, sizeof(key_str), "uuid%d", i);
337 meta_data_add_string(meta, key_str, iface->iface_uuid);
339 if (strlen(iface->ex_vm_id)) {
340 snprintf(key_str, sizeof(key_str), "vm-uuid%d", i);
341 meta_data_add_string(meta, key_str, iface->ex_vm_id);
344 if (strlen(iface->ex_iface_id)) {
345 snprintf(key_str, sizeof(key_str), "iface-id%d", i);
346 meta_data_add_string(meta, key_str, iface->ex_iface_id);
352 snprintf(devname, sizeof(devname), "%s.%s", bridge->name, port->name);
353 ovs_stats_submit_one(devname, "if_collisions", NULL,
354 ovs_stats_get_port_stat_value(port, collisions), meta);
355 ovs_stats_submit_two(devname, "if_dropped", NULL,
356 ovs_stats_get_port_stat_value(port, rx_dropped),
357 ovs_stats_get_port_stat_value(port, tx_dropped), meta);
358 ovs_stats_submit_two(devname, "if_errors", NULL,
359 ovs_stats_get_port_stat_value(port, rx_errors),
360 ovs_stats_get_port_stat_value(port, tx_errors), meta);
361 ovs_stats_submit_two(devname, "if_packets", NULL,
362 ovs_stats_get_port_stat_value(port, rx_packets),
363 ovs_stats_get_port_stat_value(port, tx_packets), meta);
364 ovs_stats_submit_one(devname, "if_rx_errors", "crc",
365 ovs_stats_get_port_stat_value(port, rx_crc_err), meta);
366 ovs_stats_submit_one(devname, "if_rx_errors", "frame",
367 ovs_stats_get_port_stat_value(port, rx_frame_err), meta);
368 ovs_stats_submit_one(devname, "if_rx_errors", "over",
369 ovs_stats_get_port_stat_value(port, rx_over_err), meta);
370 ovs_stats_submit_one(devname, "if_rx_octets", NULL,
371 ovs_stats_get_port_stat_value(port, rx_bytes), meta);
372 ovs_stats_submit_one(devname, "if_tx_octets", NULL,
373 ovs_stats_get_port_stat_value(port, tx_bytes), meta);
374 ovs_stats_submit_two(devname, "if_packets", "1_to_64_packets",
375 ovs_stats_get_port_stat_value(port, rx_1_to_64_packets),
376 ovs_stats_get_port_stat_value(port, tx_1_to_64_packets),
378 ovs_stats_submit_two(
379 devname, "if_packets", "65_to_127_packets",
380 ovs_stats_get_port_stat_value(port, rx_65_to_127_packets),
381 ovs_stats_get_port_stat_value(port, tx_65_to_127_packets), meta);
382 ovs_stats_submit_two(
383 devname, "if_packets", "128_to_255_packets",
384 ovs_stats_get_port_stat_value(port, rx_128_to_255_packets),
385 ovs_stats_get_port_stat_value(port, tx_128_to_255_packets), meta);
386 ovs_stats_submit_two(
387 devname, "if_packets", "256_to_511_packets",
388 ovs_stats_get_port_stat_value(port, rx_256_to_511_packets),
389 ovs_stats_get_port_stat_value(port, tx_256_to_511_packets), meta);
390 ovs_stats_submit_two(
391 devname, "if_packets", "512_to_1023_packets",
392 ovs_stats_get_port_stat_value(port, rx_512_to_1023_packets),
393 ovs_stats_get_port_stat_value(port, tx_512_to_1023_packets), meta);
394 ovs_stats_submit_two(
395 devname, "if_packets", "1024_to_1522_packets",
396 ovs_stats_get_port_stat_value(port, rx_1024_to_1522_packets),
397 ovs_stats_get_port_stat_value(port, tx_1024_to_1522_packets), meta);
398 ovs_stats_submit_two(
399 devname, "if_packets", "1523_to_max_packets",
400 ovs_stats_get_port_stat_value(port, rx_1523_to_max_packets),
401 ovs_stats_get_port_stat_value(port, tx_1523_to_max_packets), meta);
402 ovs_stats_submit_two(
403 devname, "if_packets", "broadcast_packets",
404 ovs_stats_get_port_stat_value(port, rx_broadcast_packets),
405 ovs_stats_get_port_stat_value(port, tx_broadcast_packets), meta);
406 ovs_stats_submit_one(
407 devname, "if_multicast", "tx_multicast_packets",
408 ovs_stats_get_port_stat_value(port, tx_multicast_packets), meta);
409 ovs_stats_submit_one(
410 devname, "if_rx_errors", "rx_undersized_errors",
411 ovs_stats_get_port_stat_value(port, rx_undersized_errors), meta);
412 ovs_stats_submit_one(devname, "if_rx_errors", "rx_oversize_errors",
413 ovs_stats_get_port_stat_value(port, rx_oversize_errors),
415 ovs_stats_submit_one(
416 devname, "if_rx_errors", "rx_fragmented_errors",
417 ovs_stats_get_port_stat_value(port, rx_fragmented_errors), meta);
418 ovs_stats_submit_one(devname, "if_rx_errors", "rx_jabber_errors",
419 ovs_stats_get_port_stat_value(port, rx_jabber_errors),
422 meta_data_destroy(meta);
425 static port_list_t *ovs_stats_get_port(const char *uuid) {
429 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
430 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0)
436 static port_list_t *ovs_stats_get_port_by_interface_uuid(const char *uuid) {
440 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
441 for (interface_list_t *iface = port->iface; iface != NULL;
442 iface = iface->next) {
443 if (strncmp(iface->iface_uuid, uuid, strlen(uuid)) == 0)
450 static interface_list_t *ovs_stats_get_port_interface(port_list_t *port,
452 if (port == NULL || uuid == NULL)
455 for (interface_list_t *iface = port->iface; iface != NULL;
456 iface = iface->next) {
457 if (strncmp(iface->iface_uuid, uuid, strlen(uuid)) == 0)
463 static interface_list_t *ovs_stats_get_interface(const char *uuid) {
467 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
468 for (interface_list_t *iface = port->iface; iface != NULL;
469 iface = iface->next) {
470 if (strncmp(iface->iface_uuid, uuid, strlen(uuid)) == 0)
477 static interface_list_t *ovs_stats_new_port_interface(port_list_t *port,
482 interface_list_t *iface = ovs_stats_get_port_interface(port, uuid);
485 iface = (interface_list_t *)calloc(1, sizeof(interface_list_t));
487 ERROR("%s: Error allocating interface", plugin_name);
490 memset(iface->stats, -1, sizeof(int64_t[IFACE_COUNTER_COUNT]));
491 sstrncpy(iface->iface_uuid, uuid, sizeof(iface->iface_uuid));
492 sstrncpy(iface->port_uuid, port->port_uuid, sizeof(iface->port_uuid));
493 pthread_mutex_lock(&g_stats_lock);
494 interface_list_t *iface_head = port->iface;
495 iface->next = iface_head;
497 pthread_mutex_unlock(&g_stats_lock);
502 /* Create or get port by port uuid */
503 static port_list_t *ovs_stats_new_port(bridge_list_t *bridge,
508 port_list_t *port = ovs_stats_get_port(uuid);
511 port = (port_list_t *)calloc(1, sizeof(port_list_t));
513 ERROR("%s: Error allocating port", plugin_name);
516 sstrncpy(port->port_uuid, uuid, sizeof(port->port_uuid));
517 pthread_mutex_lock(&g_stats_lock);
518 port->next = g_port_list_head;
519 g_port_list_head = port;
520 pthread_mutex_unlock(&g_stats_lock);
522 if (bridge != NULL) {
523 pthread_mutex_lock(&g_stats_lock);
525 pthread_mutex_unlock(&g_stats_lock);
530 /* Get bridge by name*/
531 static bridge_list_t *ovs_stats_get_bridge(bridge_list_t *head,
536 for (bridge_list_t *bridge = head; bridge != NULL; bridge = bridge->next) {
537 if ((strncmp(bridge->name, name, strlen(bridge->name)) == 0) &&
538 strlen(name) == strlen(bridge->name))
545 static int ovs_stats_del_bridge(yajl_val bridge) {
546 const char *old[] = {"old", NULL};
547 const char *name[] = {"name", NULL};
549 if (!bridge || !YAJL_IS_OBJECT(bridge)) {
550 WARNING("%s: Incorrect data for deleting bridge", plugin_name);
554 yajl_val row = yajl_tree_get(bridge, old, yajl_t_object);
555 if (!row || !YAJL_IS_OBJECT(row))
558 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
559 if (!br_name || !YAJL_IS_STRING(br_name))
562 bridge_list_t *prev_br = g_bridge_list_head;
563 for (bridge_list_t *br = g_bridge_list_head; br != NULL;
564 prev_br = br, br = br->next) {
565 if ((strncmp(br->name, br_name->u.string, strlen(br->name)) == 0) &&
566 strlen(br->name) == strlen(br_name->u.string)) {
567 if (br == g_bridge_list_head)
568 g_bridge_list_head = br->next;
570 prev_br->next = br->next;
579 /* Update Bridge. Create bridge ports*/
580 static int ovs_stats_update_bridge(yajl_val bridge) {
581 const char *new[] = {"new", NULL};
582 const char *name[] = {"name", NULL};
583 const char *ports[] = {"ports", NULL};
584 bridge_list_t *br = NULL;
586 if (!bridge || !YAJL_IS_OBJECT(bridge))
589 yajl_val row = yajl_tree_get(bridge, new, yajl_t_object);
590 if (!row || !YAJL_IS_OBJECT(row))
593 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
594 if (br_name && YAJL_IS_STRING(br_name)) {
595 br = ovs_stats_get_bridge(g_bridge_list_head, YAJL_GET_STRING(br_name));
596 pthread_mutex_lock(&g_stats_lock);
598 br = calloc(1, sizeof(*br));
600 pthread_mutex_unlock(&g_stats_lock);
601 ERROR("%s: calloc(%zu) failed.", plugin_name, sizeof(*br));
605 char *tmp = YAJL_GET_STRING(br_name);
607 br->name = strdup(tmp);
609 if (br->name == NULL) {
611 pthread_mutex_unlock(&g_stats_lock);
612 ERROR("%s: strdup failed.", plugin_name);
616 br->next = g_bridge_list_head;
617 g_bridge_list_head = br;
619 pthread_mutex_unlock(&g_stats_lock);
622 yajl_val br_ports = yajl_tree_get(row, ports, yajl_t_array);
623 if (!br_ports || !YAJL_IS_ARRAY(br_ports))
626 char *tmp = YAJL_GET_STRING(br_ports->u.array.values[0]);
627 if (tmp != NULL && strcmp("set", tmp) == 0) {
628 yajl_val *array = YAJL_GET_ARRAY(br_ports)->values;
629 size_t array_len = YAJL_GET_ARRAY(br_ports)->len;
630 if (array != NULL && array_len > 0 && YAJL_IS_ARRAY(array[1])) {
631 if (YAJL_GET_ARRAY(array[1]) == NULL)
634 yajl_val *ports_arr = YAJL_GET_ARRAY(array[1])->values;
635 size_t ports_num = YAJL_GET_ARRAY(array[1])->len;
636 for (size_t i = 0; i < ports_num && ports_arr != NULL; i++) {
637 tmp = YAJL_GET_STRING(ports_arr[i]->u.array.values[1]);
639 ovs_stats_new_port(br, tmp);
645 ovs_stats_new_port(br, YAJL_GET_STRING(br_ports->u.array.values[1]));
651 ERROR("Incorrect JSON Bridge data");
655 /* Handle JSON with Bridge Table change event */
656 static void ovs_stats_bridge_table_change_cb(yajl_val jupdates) {
657 /* Bridge Table update example JSON data
660 "bb1f8965-5775-46d9-b820-236ca8edbedc": {
668 "117f1a07-7ef0-458a-865c-ec7fbb85bc01"
672 "12fd8bdc-e950-4281-aaa9-46e185658f79"
681 const char *path[] = {"Bridge", NULL};
682 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
684 if (!bridges || !YAJL_IS_OBJECT(bridges))
687 for (size_t i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
688 yajl_val bridge = YAJL_GET_OBJECT(bridges)->values[i];
689 ovs_stats_update_bridge(bridge);
693 /* Handle Bridge Table delete event */
694 static void ovs_stats_bridge_table_delete_cb(yajl_val jupdates) {
695 const char *path[] = {"Bridge", NULL};
696 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
697 if (!bridges || !YAJL_IS_OBJECT(bridges))
700 pthread_mutex_lock(&g_stats_lock);
701 for (size_t i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
702 yajl_val bridge = YAJL_GET_OBJECT(bridges)->values[i];
703 ovs_stats_del_bridge(bridge);
705 pthread_mutex_unlock(&g_stats_lock);
708 /* Handle JSON with Bridge table initial values */
709 static void ovs_stats_bridge_table_result_cb(yajl_val jresult,
711 if (YAJL_IS_NULL(jerror))
712 ovs_stats_bridge_table_change_cb(jresult);
714 ERROR("%s: Error received from OvSDB. Table: Bridge", plugin_name);
718 /* Update port name and interface UUID(s)*/
719 static int ovs_stats_update_port(const char *uuid, yajl_val port) {
720 const char *new[] = {"new", NULL};
721 const char *name[] = {"name", NULL};
723 if (!port || !YAJL_IS_OBJECT(port)) {
724 ERROR("Incorrect JSON Port data");
728 yajl_val row = yajl_tree_get(port, new, yajl_t_object);
729 if (!row || !YAJL_IS_OBJECT(row))
732 yajl_val port_name = yajl_tree_get(row, name, yajl_t_string);
733 if (!port_name || !YAJL_IS_STRING(port_name))
736 /* Create or get port by port uuid */
737 port_list_t *portentry = ovs_stats_new_port(NULL, uuid);
741 pthread_mutex_lock(&g_stats_lock);
742 sstrncpy(portentry->name, YAJL_GET_STRING(port_name),
743 sizeof(portentry->name));
744 pthread_mutex_unlock(&g_stats_lock);
746 yajl_val ifaces_root = ovs_utils_get_value_by_key(row, "interfaces");
747 char *ifaces_root_key =
748 YAJL_GET_STRING(YAJL_GET_ARRAY(ifaces_root)->values[0]);
750 if (strcmp("set", ifaces_root_key) == 0) {
751 // ifaces_root is ["set", [[ "uuid", "<some_uuid>" ], [ "uuid",
752 // "<another_uuid>" ], ... ]]
753 yajl_val ifaces_list = YAJL_GET_ARRAY(ifaces_root)->values[1];
755 // ifaces_list is [[ "uuid", "<some_uuid>" ], [ "uuid",
756 // "<another_uuid>" ], ... ]]
757 for (int i = 0; i < YAJL_GET_ARRAY(ifaces_list)->len; i++) {
758 yajl_val iface_tuple = YAJL_GET_ARRAY(ifaces_list)->values[i];
760 // iface_tuple is [ "uuid", "<some_uuid>" ]
761 char *iface_uuid_str =
762 YAJL_GET_STRING(YAJL_GET_ARRAY(iface_tuple)->values[1]);
764 // Also checks if interface already registered
765 ovs_stats_new_port_interface(portentry, iface_uuid_str);
768 // ifaces_root is [ "uuid", "<some_uuid>" ]
769 char *iface_uuid_str =
770 YAJL_GET_STRING(YAJL_GET_ARRAY(ifaces_root)->values[1]);
772 // Also checks if interface already registered
773 ovs_stats_new_port_interface(portentry, iface_uuid_str);
779 /* Delete port from global port list */
780 static int ovs_stats_del_port(const char *uuid) {
781 port_list_t *prev_port = g_port_list_head;
782 for (port_list_t *port = g_port_list_head; port != NULL;
783 prev_port = port, port = port->next) {
784 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0) {
785 if (port == g_port_list_head)
786 g_port_list_head = port->next;
788 prev_port->next = port->next;
790 for (interface_list_t *iface = port->iface; iface != NULL;
791 iface = port->iface) {
792 interface_list_t *del = iface;
793 port->iface = iface->next;
804 /* Handle JSON with Port Table change event */
805 static void ovs_stats_port_table_change_cb(yajl_val jupdates) {
806 /* Port Table update example JSON data
809 "ab107d6f-28a1-4257-b1cc-5b742821db8a": {
814 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
821 const char *path[] = {"Port", NULL};
822 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
823 if (!ports || !YAJL_IS_OBJECT(ports))
826 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
827 yajl_val port = YAJL_GET_OBJECT(ports)->values[i];
828 ovs_stats_update_port(YAJL_GET_OBJECT(ports)->keys[i], port);
833 /* Handle JSON with Port table initial values */
834 static void ovs_stats_port_table_result_cb(yajl_val jresult, yajl_val jerror) {
835 if (YAJL_IS_NULL(jerror))
836 ovs_stats_port_table_change_cb(jresult);
838 ERROR("%s: Error received from OvSDB. Table: Port", plugin_name);
842 /* Handle Port Table delete event */
843 static void ovs_stats_port_table_delete_cb(yajl_val jupdates) {
844 const char *path[] = {"Port", NULL};
845 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
846 if (!ports || !YAJL_IS_OBJECT(ports))
849 pthread_mutex_lock(&g_stats_lock);
850 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
851 ovs_stats_del_port(YAJL_GET_OBJECT(ports)->keys[i]);
853 pthread_mutex_unlock(&g_stats_lock);
857 /* Update interface statistics */
858 static int ovs_stats_update_iface_stats(interface_list_t *iface,
861 if (!stats || !YAJL_IS_ARRAY(stats))
864 for (size_t i = 0; i < YAJL_GET_ARRAY(stats)->len; i++) {
865 yajl_val stat = YAJL_GET_ARRAY(stats)->values[i];
866 if (!YAJL_IS_ARRAY(stat))
869 char *counter_name = YAJL_GET_STRING(YAJL_GET_ARRAY(stat)->values[0]);
870 iface_counter counter_index = ovs_stats_counter_name_to_type(counter_name);
871 int64_t counter_value = YAJL_GET_INTEGER(YAJL_GET_ARRAY(stat)->values[1]);
872 if (counter_index == not_supported)
875 iface->stats[counter_index] = counter_value;
881 /* Update interface external_ids */
882 static int ovs_stats_update_iface_ext_ids(interface_list_t *iface,
885 if (ext_ids && YAJL_IS_ARRAY(ext_ids)) {
886 for (size_t i = 0; i < YAJL_GET_ARRAY(ext_ids)->len; i++) {
887 yajl_val ext_id = YAJL_GET_ARRAY(ext_ids)->values[i];
888 if (!YAJL_IS_ARRAY(ext_id))
891 char *key = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[0]);
892 char *value = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[1]);
894 if (strncmp(key, "iface-id", strlen(key)) == 0) {
895 sstrncpy(iface->ex_iface_id, value, sizeof(iface->ex_iface_id));
896 } else if (strncmp(key, "vm-uuid", strlen(key)) == 0) {
897 sstrncpy(iface->ex_vm_id, value, sizeof(iface->ex_vm_id));
906 /* Get interface statistic and external_ids */
907 static int ovs_stats_update_iface(yajl_val iface_obj) {
908 if (!iface_obj || !YAJL_IS_OBJECT(iface_obj)) {
909 ERROR("ovs_stats plugin: incorrect JSON interface data");
913 yajl_val row = ovs_utils_get_value_by_key(iface_obj, "new");
914 if (!row || !YAJL_IS_OBJECT(row))
917 yajl_val iface_name = ovs_utils_get_value_by_key(row, "name");
918 if (!iface_name || !YAJL_IS_STRING(iface_name))
921 yajl_val iface_uuid = ovs_utils_get_value_by_key(row, "_uuid");
922 if (!iface_uuid || !YAJL_IS_ARRAY(iface_uuid) ||
923 YAJL_GET_ARRAY(iface_uuid)->len != 2)
926 char *iface_uuid_str = YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]);
927 if (iface_uuid_str == NULL) {
928 ERROR("ovs_stats plugin: incorrect JSON interface data");
932 interface_list_t *iface = ovs_stats_get_interface(iface_uuid_str);
936 sstrncpy(iface->name, YAJL_GET_STRING(iface_name), sizeof(iface->name));
938 yajl_val iface_stats = ovs_utils_get_value_by_key(row, "statistics");
939 yajl_val iface_ext_ids = ovs_utils_get_value_by_key(row, "external_ids");
958 Check that statistics is an array with 2 elements
961 if (iface_stats && YAJL_IS_ARRAY(iface_stats) &&
962 YAJL_GET_ARRAY(iface_stats)->len == 2)
963 ovs_stats_update_iface_stats(iface, YAJL_GET_ARRAY(iface_stats)->values[1]);
965 if (iface_ext_ids && YAJL_IS_ARRAY(iface_ext_ids))
966 ovs_stats_update_iface_ext_ids(iface,
967 YAJL_GET_ARRAY(iface_ext_ids)->values[1]);
972 /* Delete interface */
973 static int ovs_stats_del_interface(const char *uuid) {
974 port_list_t *port = ovs_stats_get_port_by_interface_uuid(uuid);
979 interface_list_t *prev_iface = NULL;
981 for (interface_list_t *iface = port->iface; iface != NULL;
982 iface = port->iface) {
983 if (strncmp(iface->iface_uuid, uuid, strlen(iface->iface_uuid))) {
985 interface_list_t *del = iface;
987 if (prev_iface == NULL)
988 port->iface = iface->next;
990 prev_iface->next = iface->next;
1002 /* Handle JSON with Interface Table change event */
1003 static void ovs_stats_interface_table_change_cb(yajl_val jupdates) {
1004 /* Interface Table update example JSON data
1007 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6": {
1030 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
1041 "a61b7e2b-6951-488a-b4c6-6e91343960b2"
1054 const char *path[] = {"Interface", NULL};
1055 yajl_val interfaces = yajl_tree_get(jupdates, path, yajl_t_object);
1056 if (!interfaces || !YAJL_IS_OBJECT(interfaces))
1059 pthread_mutex_lock(&g_stats_lock);
1060 for (size_t i = 0; i < YAJL_GET_OBJECT(interfaces)->len; i++) {
1061 ovs_stats_update_iface(YAJL_GET_OBJECT(interfaces)->values[i]);
1063 pthread_mutex_unlock(&g_stats_lock);
1068 /* Handle JSON with Interface table initial values */
1069 static void ovs_stats_interface_table_result_cb(yajl_val jresult,
1071 if (YAJL_IS_NULL(jerror))
1072 ovs_stats_interface_table_change_cb(jresult);
1074 ERROR("%s: Error received from OvSDB. Table: Interface", plugin_name);
1078 /* Handle Interface Table delete event */
1079 static void ovs_stats_interface_table_delete_cb(yajl_val jupdates) {
1080 const char *path[] = {"Interface", NULL};
1081 yajl_val interfaces = yajl_tree_get(jupdates, path, yajl_t_object);
1082 if (!interfaces || !YAJL_IS_OBJECT(interfaces))
1085 pthread_mutex_lock(&g_stats_lock);
1086 for (size_t i = 0; i < YAJL_GET_OBJECT(interfaces)->len; i++) {
1087 ovs_stats_del_interface(YAJL_GET_OBJECT(interfaces)->keys[i]);
1089 pthread_mutex_unlock(&g_stats_lock);
1094 /* Setup OVS DB table callbacks */
1095 static void ovs_stats_initialize(ovs_db_t *pdb) {
1096 const char *bridge_columns[] = {"name", "ports", NULL};
1097 const char *port_columns[] = {"name", "interfaces", NULL};
1098 const char *interface_columns[] = {"name", "statistics", "_uuid",
1099 "external_ids", NULL};
1101 /* subscribe to a tables */
1102 ovs_db_table_cb_register(
1103 pdb, "Bridge", bridge_columns, ovs_stats_bridge_table_change_cb,
1104 ovs_stats_bridge_table_result_cb,
1105 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
1106 OVS_DB_TABLE_CB_FLAG_MODIFY);
1108 ovs_db_table_cb_register(pdb, "Bridge", bridge_columns,
1109 ovs_stats_bridge_table_delete_cb, NULL,
1110 OVS_DB_TABLE_CB_FLAG_DELETE);
1112 ovs_db_table_cb_register(
1113 pdb, "Port", port_columns, ovs_stats_port_table_change_cb,
1114 ovs_stats_port_table_result_cb,
1115 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
1116 OVS_DB_TABLE_CB_FLAG_MODIFY);
1118 ovs_db_table_cb_register(pdb, "Port", port_columns,
1119 ovs_stats_port_table_delete_cb, NULL,
1120 OVS_DB_TABLE_CB_FLAG_DELETE);
1122 ovs_db_table_cb_register(
1123 pdb, "Interface", interface_columns, ovs_stats_interface_table_change_cb,
1124 ovs_stats_interface_table_result_cb,
1125 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
1126 OVS_DB_TABLE_CB_FLAG_MODIFY);
1128 ovs_db_table_cb_register(pdb, "Interface", interface_columns,
1129 ovs_stats_interface_table_delete_cb, NULL,
1130 OVS_DB_TABLE_CB_FLAG_DELETE);
1133 /* Check if bridge is configured to be monitored in config file */
1134 static int ovs_stats_is_monitored_bridge(const char *br_name) {
1135 /* if no bridges are configured, return true */
1136 if (g_monitored_bridge_list_head == NULL)
1139 /* check if given bridge exists */
1140 if (ovs_stats_get_bridge(g_monitored_bridge_list_head, br_name) != NULL)
1146 /* Delete all ports from port list */
1147 static void ovs_stats_free_port_list(port_list_t *head) {
1148 for (port_list_t *i = head; i != NULL;) {
1149 port_list_t *del = i;
1151 for (interface_list_t *iface = i->iface; iface != NULL; iface = i->iface) {
1152 interface_list_t *del2 = iface;
1153 i->iface = iface->next;
1162 /* Delete all bridges from bridge list */
1163 static void ovs_stats_free_bridge_list(bridge_list_t *head) {
1164 for (bridge_list_t *i = head; i != NULL;) {
1165 bridge_list_t *del = i;
1172 /* Handle OVSDB lost connection callback */
1173 static void ovs_stats_conn_terminate() {
1174 WARNING("Lost connection to OVSDB server");
1175 pthread_mutex_lock(&g_stats_lock);
1176 ovs_stats_free_bridge_list(g_bridge_list_head);
1177 g_bridge_list_head = NULL;
1178 ovs_stats_free_port_list(g_port_list_head);
1179 g_port_list_head = NULL;
1180 pthread_mutex_unlock(&g_stats_lock);
1183 /* Parse plugin configuration file and store the config
1184 * in allocated memory. Returns negative value in case of error.
1186 static int ovs_stats_plugin_config(oconfig_item_t *ci) {
1187 bridge_list_t *bridge;
1189 for (int i = 0; i < ci->children_num; i++) {
1190 oconfig_item_t *child = ci->children + i;
1191 if (strcasecmp("Address", child->key) == 0) {
1192 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_node,
1193 OVS_DB_ADDR_NODE_SIZE) != 0) {
1194 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1197 } else if (strcasecmp("Port", child->key) == 0) {
1198 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_serv,
1199 OVS_DB_ADDR_SERVICE_SIZE) != 0) {
1200 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1203 } else if (strcasecmp("Socket", child->key) == 0) {
1204 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_unix,
1205 OVS_DB_ADDR_UNIX_SIZE) != 0) {
1206 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1209 } else if (strcasecmp("Bridges", child->key) == 0) {
1210 for (int j = 0; j < child->values_num; j++) {
1211 /* check value type */
1212 if (child->values[j].type != OCONFIG_TYPE_STRING) {
1213 ERROR("%s: Wrong bridge name [idx=%d]. "
1214 "Bridge name should be string",
1219 char const *br_name = child->values[j].value.string;
1220 if ((bridge = ovs_stats_get_bridge(g_monitored_bridge_list_head,
1221 br_name)) == NULL) {
1222 if ((bridge = calloc(1, sizeof(bridge_list_t))) == NULL) {
1223 ERROR("%s: Error allocating memory for bridge", plugin_name);
1226 char *br_name_dup = strdup(br_name);
1227 if (br_name_dup == NULL) {
1228 ERROR("%s: strdup() copy bridge name fail", plugin_name);
1233 pthread_mutex_lock(&g_stats_lock);
1234 /* store bridge name */
1235 bridge->name = br_name_dup;
1236 bridge->next = g_monitored_bridge_list_head;
1237 g_monitored_bridge_list_head = bridge;
1238 pthread_mutex_unlock(&g_stats_lock);
1239 DEBUG("%s: found monitored interface \"%s\"", plugin_name, br_name);
1243 } else if (strcasecmp("InterfaceStats", child->key) == 0) {
1244 if (cf_util_get_boolean(child, &interface_stats) != 0) {
1245 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1249 WARNING("%s: option '%s' not allowed here", plugin_name, child->key);
1256 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
1260 /* Initialize OvS Stats plugin*/
1261 static int ovs_stats_plugin_init(void) {
1262 ovs_db_callback_t cb = {.post_conn_init = ovs_stats_initialize,
1263 .post_conn_terminate = ovs_stats_conn_terminate};
1265 INFO("%s: Connecting to OVS DB using address=%s, service=%s, unix=%s",
1266 plugin_name, ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
1267 ovs_stats_cfg.ovs_db_unix);
1268 /* connect to OvS DB */
1270 ovs_db_init(ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
1271 ovs_stats_cfg.ovs_db_unix, &cb)) == NULL) {
1272 ERROR("%s: plugin: failed to connect to OvS DB server", plugin_name);
1275 int err = pthread_mutex_init(&g_stats_lock, NULL);
1277 ERROR("%s: plugin: failed to initialize cache lock", plugin_name);
1278 ovs_db_destroy(g_ovs_db);
1284 /* OvS stats read callback. Read bridge/port information and submit it*/
1285 static int ovs_stats_plugin_read(__attribute__((unused)) user_data_t *ud) {
1286 bridge_list_t *bridge;
1289 pthread_mutex_lock(&g_stats_lock);
1290 for (bridge = g_bridge_list_head; bridge != NULL; bridge = bridge->next) {
1291 if (!ovs_stats_is_monitored_bridge(bridge->name))
1294 for (port = g_port_list_head; port != NULL; port = port->next) {
1295 if (port->br != bridge)
1298 if (strlen(port->name) == 0)
1299 /* Skip port w/o name. This is possible when read callback
1300 * is called after Interface Table update callback but before
1301 * Port table Update callback. Will add this port on next read */
1304 ovs_stats_submit_port(bridge, port);
1306 if (interface_stats)
1307 ovs_stats_submit_interfaces(bridge, port);
1310 pthread_mutex_unlock(&g_stats_lock);
1314 /* Shutdown OvS Stats plugin */
1315 static int ovs_stats_plugin_shutdown(void) {
1316 DEBUG("OvS Statistics plugin shutting down");
1317 ovs_db_destroy(g_ovs_db);
1318 pthread_mutex_lock(&g_stats_lock);
1319 ovs_stats_free_bridge_list(g_bridge_list_head);
1320 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
1321 ovs_stats_free_port_list(g_port_list_head);
1322 pthread_mutex_unlock(&g_stats_lock);
1323 pthread_mutex_destroy(&g_stats_lock);
1327 /* Register OvS Stats plugin callbacks */
1328 void module_register(void) {
1329 plugin_register_complex_config(plugin_name, ovs_stats_plugin_config);
1330 plugin_register_init(plugin_name, ovs_stats_plugin_init);
1331 plugin_register_complex_read(NULL, plugin_name, ovs_stats_plugin_read, 0,
1333 plugin_register_shutdown(plugin_name, ovs_stats_plugin_shutdown);