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 iface_uuid[UUID_SIZE]; /* Interface table uuid */
84 char ex_iface_id[UUID_SIZE]; /* External iface id */
85 char ex_vm_id[UUID_SIZE]; /* External vm id */
86 int64_t stats[IFACE_COUNTER_COUNT]; /* Statistics for interface */
87 struct interface_s *next; /* Next interface for associated port */
90 typedef struct port_s {
91 char name[PORT_NAME_SIZE_MAX]; /* Port name */
92 char port_uuid[UUID_SIZE]; /* Port table _uuid */
93 struct bridge_list_s *br; /* Pointer to bridge */
94 struct interface_s *iface; /* Pointer to first interface */
95 struct port_s *next; /* Next port */
98 typedef struct bridge_list_s {
99 char *name; /* Bridge name */
100 struct bridge_list_s *next; /* Next bridge*/
103 #define cnt_str(x) [x] = #x
105 static const char *const iface_counter_table[IFACE_COUNTER_COUNT] = {
111 cnt_str(rx_frame_err),
112 cnt_str(rx_over_err),
118 cnt_str(rx_1_to_64_packets),
119 cnt_str(rx_65_to_127_packets),
120 cnt_str(rx_128_to_255_packets),
121 cnt_str(rx_256_to_511_packets),
122 cnt_str(rx_512_to_1023_packets),
123 cnt_str(rx_1024_to_1522_packets),
124 cnt_str(rx_1523_to_max_packets),
125 cnt_str(tx_1_to_64_packets),
126 cnt_str(tx_65_to_127_packets),
127 cnt_str(tx_128_to_255_packets),
128 cnt_str(tx_256_to_511_packets),
129 cnt_str(tx_512_to_1023_packets),
130 cnt_str(tx_1024_to_1522_packets),
131 cnt_str(tx_1523_to_max_packets),
132 cnt_str(tx_multicast_packets),
133 cnt_str(rx_broadcast_packets),
134 cnt_str(tx_broadcast_packets),
135 cnt_str(rx_undersized_errors),
136 cnt_str(rx_oversize_errors),
137 cnt_str(rx_fragmented_errors),
138 cnt_str(rx_jabber_errors),
143 /* Entry into the list of network bridges */
144 static bridge_list_t *g_bridge_list_head;
146 /* Entry into the list of monitored network bridges */
147 static bridge_list_t *g_monitored_bridge_list_head;
149 /* entry into the list of network bridges */
150 static port_list_t *g_port_list_head;
152 /* lock for statistics cache */
153 static pthread_mutex_t g_stats_lock;
156 static ovs_db_t *g_ovs_db;
158 /* OVS stats configuration data */
159 struct ovs_stats_config_s {
160 char ovs_db_node[OVS_DB_ADDR_NODE_SIZE]; /* OVS DB node */
161 char ovs_db_serv[OVS_DB_ADDR_SERVICE_SIZE]; /* OVS DB service */
162 char ovs_db_unix[OVS_DB_ADDR_UNIX_SIZE]; /* OVS DB unix socket path */
164 typedef struct ovs_stats_config_s ovs_stats_config_t;
166 static ovs_stats_config_t ovs_stats_cfg = {
167 .ovs_db_node = "localhost", /* use default OVS DB node */
168 .ovs_db_serv = "6640", /* use default OVS DB service */
171 /* flag indicating whether or not to publish individual interface statistics */
172 static bool interface_stats = false;
174 static iface_counter ovs_stats_counter_name_to_type(const char *counter) {
175 iface_counter index = not_supported;
178 return not_supported;
180 for (int i = 0; i < IFACE_COUNTER_COUNT; i++) {
181 if (strncmp(iface_counter_table[i], counter,
182 strlen(iface_counter_table[i])) == 0) {
190 static void ovs_stats_submit_one(const char *dev, const char *type,
191 const char *type_instance, derive_t value,
193 /* if counter is less than 0 - skip it*/
196 value_list_t vl = VALUE_LIST_INIT;
198 vl.values = &(value_t){.derive = value};
202 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
203 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
204 sstrncpy(vl.type, type, sizeof(vl.type));
206 if (type_instance != NULL)
207 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
209 plugin_dispatch_values(&vl);
212 static void ovs_stats_submit_two(const char *dev, const char *type,
213 const char *type_instance, derive_t rx,
214 derive_t tx, meta_data_t *meta) {
215 /* if counter is less than 0 - skip it*/
216 if (rx < 0 || tx < 0)
218 value_list_t vl = VALUE_LIST_INIT;
219 value_t values[] = {{.derive = rx}, {.derive = tx}};
222 vl.values_len = STATIC_ARRAY_SIZE(values);
225 sstrncpy(vl.plugin, plugin_name, sizeof(vl.plugin));
226 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
227 sstrncpy(vl.type, type, sizeof(vl.type));
229 if (type_instance != NULL)
230 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
232 plugin_dispatch_values(&vl);
235 static void ovs_stats_submit_interfaces(bridge_list_t *bridge,
237 char devname[PORT_NAME_SIZE_MAX * 2];
239 for (interface_list_t *iface = port->iface; iface != NULL;
240 iface = iface->next) {
241 meta_data_t *meta = meta_data_create();
243 meta_data_add_string(meta, "uuid", iface->iface_uuid);
245 if (strlen(iface->ex_vm_id))
246 meta_data_add_string(meta, "vm-uuid", iface->ex_vm_id);
248 if (strlen(iface->ex_iface_id))
249 meta_data_add_string(meta, "iface-id", iface->ex_iface_id);
251 snprintf(devname, sizeof(devname), "%s.%s.%s", bridge->name, port->name,
253 ovs_stats_submit_one(devname, "if_collisions", NULL,
254 iface->stats[collisions], meta);
255 ovs_stats_submit_two(devname, "if_dropped", NULL, iface->stats[rx_dropped],
256 iface->stats[tx_dropped], meta);
257 ovs_stats_submit_two(devname, "if_errors", NULL, iface->stats[rx_errors],
258 iface->stats[tx_errors], meta);
259 ovs_stats_submit_two(devname, "if_packets", NULL, iface->stats[rx_packets],
260 iface->stats[tx_packets], meta);
261 ovs_stats_submit_one(devname, "if_rx_errors", "crc",
262 iface->stats[rx_crc_err], meta);
263 ovs_stats_submit_one(devname, "if_rx_errors", "frame",
264 iface->stats[rx_frame_err], meta);
265 ovs_stats_submit_one(devname, "if_rx_errors", "over",
266 iface->stats[rx_over_err], meta);
267 ovs_stats_submit_one(devname, "if_rx_octets", NULL, iface->stats[rx_bytes],
269 ovs_stats_submit_one(devname, "if_tx_octets", NULL, iface->stats[tx_bytes],
271 ovs_stats_submit_two(devname, "if_packets", "1_to_64_packets",
272 iface->stats[rx_1_to_64_packets],
273 iface->stats[tx_1_to_64_packets], meta);
274 ovs_stats_submit_two(devname, "if_packets", "65_to_127_packets",
275 iface->stats[rx_65_to_127_packets],
276 iface->stats[tx_65_to_127_packets], meta);
277 ovs_stats_submit_two(devname, "if_packets", "128_to_255_packets",
278 iface->stats[rx_128_to_255_packets],
279 iface->stats[tx_128_to_255_packets], meta);
280 ovs_stats_submit_two(devname, "if_packets", "256_to_511_packets",
281 iface->stats[rx_256_to_511_packets],
282 iface->stats[tx_256_to_511_packets], meta);
283 ovs_stats_submit_two(devname, "if_packets", "512_to_1023_packets",
284 iface->stats[rx_512_to_1023_packets],
285 iface->stats[tx_512_to_1023_packets], meta);
286 ovs_stats_submit_two(devname, "if_packets", "1024_to_1522_packets",
287 iface->stats[rx_1024_to_1522_packets],
288 iface->stats[tx_1024_to_1522_packets], meta);
289 ovs_stats_submit_two(devname, "if_packets", "1523_to_max_packets",
290 iface->stats[rx_1523_to_max_packets],
291 iface->stats[tx_1523_to_max_packets], meta);
292 ovs_stats_submit_two(devname, "if_packets", "broadcast_packets",
293 iface->stats[rx_broadcast_packets],
294 iface->stats[tx_broadcast_packets], meta);
295 ovs_stats_submit_one(devname, "if_multicast", "tx_multicast_packets",
296 iface->stats[tx_multicast_packets], meta);
297 ovs_stats_submit_one(devname, "if_rx_errors", "rx_undersized_errors",
298 iface->stats[rx_undersized_errors], meta);
299 ovs_stats_submit_one(devname, "if_rx_errors", "rx_oversize_errors",
300 iface->stats[rx_oversize_errors], meta);
301 ovs_stats_submit_one(devname, "if_rx_errors", "rx_fragmented_errors",
302 iface->stats[rx_fragmented_errors], meta);
303 ovs_stats_submit_one(devname, "if_rx_errors", "rx_jabber_errors",
304 iface->stats[rx_jabber_errors], meta);
306 meta_data_destroy(meta);
310 static int ovs_stats_get_port_stat_value(port_list_t *port,
311 iface_counter index) {
317 for (interface_list_t *iface = port->iface; iface != NULL;
318 iface = iface->next) {
319 value = value + iface->stats[index];
325 static void ovs_stats_submit_port(bridge_list_t *bridge, port_list_t *port) {
326 char devname[PORT_NAME_SIZE_MAX * 2];
328 meta_data_t *meta = meta_data_create();
330 char key_str[DATA_MAX_NAME_LEN];
333 for (interface_list_t *iface = port->iface; iface != NULL;
334 iface = iface->next) {
335 snprintf(key_str, sizeof(key_str), "uuid%d", i);
336 meta_data_add_string(meta, key_str, iface->iface_uuid);
338 if (strlen(iface->ex_vm_id)) {
339 snprintf(key_str, sizeof(key_str), "vm-uuid%d", i);
340 meta_data_add_string(meta, key_str, iface->ex_vm_id);
343 if (strlen(iface->ex_iface_id)) {
344 snprintf(key_str, sizeof(key_str), "iface-id%d", i);
345 meta_data_add_string(meta, key_str, iface->ex_iface_id);
351 snprintf(devname, sizeof(devname), "%s.%s", bridge->name, port->name);
352 ovs_stats_submit_one(devname, "if_collisions", NULL,
353 ovs_stats_get_port_stat_value(port, collisions), meta);
354 ovs_stats_submit_two(devname, "if_dropped", NULL,
355 ovs_stats_get_port_stat_value(port, rx_dropped),
356 ovs_stats_get_port_stat_value(port, tx_dropped), meta);
357 ovs_stats_submit_two(devname, "if_errors", NULL,
358 ovs_stats_get_port_stat_value(port, rx_errors),
359 ovs_stats_get_port_stat_value(port, tx_errors), meta);
360 ovs_stats_submit_two(devname, "if_packets", NULL,
361 ovs_stats_get_port_stat_value(port, rx_packets),
362 ovs_stats_get_port_stat_value(port, tx_packets), meta);
363 ovs_stats_submit_one(devname, "if_rx_errors", "crc",
364 ovs_stats_get_port_stat_value(port, rx_crc_err), meta);
365 ovs_stats_submit_one(devname, "if_rx_errors", "frame",
366 ovs_stats_get_port_stat_value(port, rx_frame_err), meta);
367 ovs_stats_submit_one(devname, "if_rx_errors", "over",
368 ovs_stats_get_port_stat_value(port, rx_over_err), meta);
369 ovs_stats_submit_one(devname, "if_rx_octets", NULL,
370 ovs_stats_get_port_stat_value(port, rx_bytes), meta);
371 ovs_stats_submit_one(devname, "if_tx_octets", NULL,
372 ovs_stats_get_port_stat_value(port, tx_bytes), meta);
373 ovs_stats_submit_two(devname, "if_packets", "1_to_64_packets",
374 ovs_stats_get_port_stat_value(port, rx_1_to_64_packets),
375 ovs_stats_get_port_stat_value(port, tx_1_to_64_packets),
377 ovs_stats_submit_two(
378 devname, "if_packets", "65_to_127_packets",
379 ovs_stats_get_port_stat_value(port, rx_65_to_127_packets),
380 ovs_stats_get_port_stat_value(port, tx_65_to_127_packets), meta);
381 ovs_stats_submit_two(
382 devname, "if_packets", "128_to_255_packets",
383 ovs_stats_get_port_stat_value(port, rx_128_to_255_packets),
384 ovs_stats_get_port_stat_value(port, tx_128_to_255_packets), meta);
385 ovs_stats_submit_two(
386 devname, "if_packets", "256_to_511_packets",
387 ovs_stats_get_port_stat_value(port, rx_256_to_511_packets),
388 ovs_stats_get_port_stat_value(port, tx_256_to_511_packets), meta);
389 ovs_stats_submit_two(
390 devname, "if_packets", "512_to_1023_packets",
391 ovs_stats_get_port_stat_value(port, rx_512_to_1023_packets),
392 ovs_stats_get_port_stat_value(port, tx_512_to_1023_packets), meta);
393 ovs_stats_submit_two(
394 devname, "if_packets", "1024_to_1522_packets",
395 ovs_stats_get_port_stat_value(port, rx_1024_to_1522_packets),
396 ovs_stats_get_port_stat_value(port, tx_1024_to_1522_packets), meta);
397 ovs_stats_submit_two(
398 devname, "if_packets", "1523_to_max_packets",
399 ovs_stats_get_port_stat_value(port, rx_1523_to_max_packets),
400 ovs_stats_get_port_stat_value(port, tx_1523_to_max_packets), meta);
401 ovs_stats_submit_two(
402 devname, "if_packets", "broadcast_packets",
403 ovs_stats_get_port_stat_value(port, rx_broadcast_packets),
404 ovs_stats_get_port_stat_value(port, tx_broadcast_packets), meta);
405 ovs_stats_submit_one(
406 devname, "if_multicast", "tx_multicast_packets",
407 ovs_stats_get_port_stat_value(port, tx_multicast_packets), meta);
408 ovs_stats_submit_one(
409 devname, "if_rx_errors", "rx_undersized_errors",
410 ovs_stats_get_port_stat_value(port, rx_undersized_errors), meta);
411 ovs_stats_submit_one(devname, "if_rx_errors", "rx_oversize_errors",
412 ovs_stats_get_port_stat_value(port, rx_oversize_errors),
414 ovs_stats_submit_one(
415 devname, "if_rx_errors", "rx_fragmented_errors",
416 ovs_stats_get_port_stat_value(port, rx_fragmented_errors), meta);
417 ovs_stats_submit_one(devname, "if_rx_errors", "rx_jabber_errors",
418 ovs_stats_get_port_stat_value(port, rx_jabber_errors),
421 meta_data_destroy(meta);
424 static port_list_t *ovs_stats_get_port(const char *uuid) {
428 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
429 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0)
435 static port_list_t *ovs_stats_get_port_by_interface_uuid(const char *uuid) {
439 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
440 for (interface_list_t *iface = port->iface; iface != NULL;
441 iface = iface->next) {
442 if (strncmp(iface->iface_uuid, uuid, strlen(uuid)) == 0)
449 static interface_list_t *ovs_stats_get_port_interface(port_list_t *port,
451 if (port == NULL || uuid == NULL)
454 for (interface_list_t *iface = port->iface; iface != NULL;
455 iface = iface->next) {
456 if (strncmp(iface->iface_uuid, uuid, strlen(uuid)) == 0)
462 static interface_list_t *ovs_stats_get_interface(const char *uuid) {
466 for (port_list_t *port = g_port_list_head; port != NULL; port = port->next) {
467 for (interface_list_t *iface = port->iface; iface != NULL;
468 iface = iface->next) {
469 if (strncmp(iface->iface_uuid, uuid, strlen(uuid)) == 0)
476 static interface_list_t *ovs_stats_new_port_interface(port_list_t *port,
481 interface_list_t *iface = ovs_stats_get_port_interface(port, uuid);
484 iface = (interface_list_t *)calloc(1, sizeof(interface_list_t));
486 ERROR("%s: Error allocating interface", plugin_name);
489 memset(iface->stats, -1, sizeof(int64_t[IFACE_COUNTER_COUNT]));
490 sstrncpy(iface->iface_uuid, uuid, sizeof(iface->iface_uuid));
491 pthread_mutex_lock(&g_stats_lock);
492 interface_list_t *iface_head = port->iface;
493 iface->next = iface_head;
495 pthread_mutex_unlock(&g_stats_lock);
500 /* Create or get port by port uuid */
501 static port_list_t *ovs_stats_new_port(bridge_list_t *bridge,
506 port_list_t *port = ovs_stats_get_port(uuid);
509 port = (port_list_t *)calloc(1, sizeof(port_list_t));
511 ERROR("%s: Error allocating port", plugin_name);
514 sstrncpy(port->port_uuid, uuid, sizeof(port->port_uuid));
515 pthread_mutex_lock(&g_stats_lock);
516 port->next = g_port_list_head;
517 g_port_list_head = port;
518 pthread_mutex_unlock(&g_stats_lock);
520 if (bridge != NULL) {
521 pthread_mutex_lock(&g_stats_lock);
523 pthread_mutex_unlock(&g_stats_lock);
528 /* Get bridge by name*/
529 static bridge_list_t *ovs_stats_get_bridge(bridge_list_t *head,
534 for (bridge_list_t *bridge = head; bridge != NULL; bridge = bridge->next) {
535 if ((strncmp(bridge->name, name, strlen(bridge->name)) == 0) &&
536 strlen(name) == strlen(bridge->name))
543 static int ovs_stats_del_bridge(yajl_val bridge) {
544 const char *old[] = {"old", NULL};
545 const char *name[] = {"name", NULL};
547 if (!bridge || !YAJL_IS_OBJECT(bridge)) {
548 WARNING("%s: Incorrect data for deleting bridge", plugin_name);
552 yajl_val row = yajl_tree_get(bridge, old, yajl_t_object);
553 if (!row || !YAJL_IS_OBJECT(row))
556 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
557 if (!br_name || !YAJL_IS_STRING(br_name))
560 bridge_list_t *prev_br = g_bridge_list_head;
561 for (bridge_list_t *br = g_bridge_list_head; br != NULL;
562 prev_br = br, br = br->next) {
563 if ((strncmp(br->name, br_name->u.string, strlen(br->name)) == 0) &&
564 strlen(br->name) == strlen(br_name->u.string)) {
565 if (br == g_bridge_list_head)
566 g_bridge_list_head = br->next;
568 prev_br->next = br->next;
577 /* Update Bridge. Create bridge ports*/
578 static int ovs_stats_update_bridge(yajl_val bridge) {
579 const char *new[] = {"new", NULL};
580 const char *name[] = {"name", NULL};
581 const char *ports[] = {"ports", NULL};
582 bridge_list_t *br = NULL;
584 if (!bridge || !YAJL_IS_OBJECT(bridge))
587 yajl_val row = yajl_tree_get(bridge, new, yajl_t_object);
588 if (!row || !YAJL_IS_OBJECT(row))
591 yajl_val br_name = yajl_tree_get(row, name, yajl_t_string);
592 if (br_name && YAJL_IS_STRING(br_name)) {
593 br = ovs_stats_get_bridge(g_bridge_list_head, YAJL_GET_STRING(br_name));
594 pthread_mutex_lock(&g_stats_lock);
596 br = calloc(1, sizeof(*br));
598 pthread_mutex_unlock(&g_stats_lock);
599 ERROR("%s: calloc(%zu) failed.", plugin_name, sizeof(*br));
603 char *tmp = YAJL_GET_STRING(br_name);
605 br->name = strdup(tmp);
607 if (br->name == NULL) {
609 pthread_mutex_unlock(&g_stats_lock);
610 ERROR("%s: strdup failed.", plugin_name);
614 br->next = g_bridge_list_head;
615 g_bridge_list_head = br;
617 pthread_mutex_unlock(&g_stats_lock);
620 yajl_val br_ports = yajl_tree_get(row, ports, yajl_t_array);
621 if (!br_ports || !YAJL_IS_ARRAY(br_ports))
624 char *tmp = YAJL_GET_STRING(br_ports->u.array.values[0]);
625 if (tmp != NULL && strcmp("set", tmp) == 0) {
626 yajl_val *array = YAJL_GET_ARRAY(br_ports)->values;
627 size_t array_len = YAJL_GET_ARRAY(br_ports)->len;
628 if (array != NULL && array_len > 0 && YAJL_IS_ARRAY(array[1])) {
629 if (YAJL_GET_ARRAY(array[1]) == NULL)
632 yajl_val *ports_arr = YAJL_GET_ARRAY(array[1])->values;
633 size_t ports_num = YAJL_GET_ARRAY(array[1])->len;
634 for (size_t i = 0; i < ports_num && ports_arr != NULL; i++) {
635 tmp = YAJL_GET_STRING(ports_arr[i]->u.array.values[1]);
637 ovs_stats_new_port(br, tmp);
643 ovs_stats_new_port(br, YAJL_GET_STRING(br_ports->u.array.values[1]));
649 ERROR("Incorrect JSON Bridge data");
653 /* Handle JSON with Bridge Table change event */
654 static void ovs_stats_bridge_table_change_cb(yajl_val jupdates) {
655 /* Bridge Table update example JSON data
658 "bb1f8965-5775-46d9-b820-236ca8edbedc": {
666 "117f1a07-7ef0-458a-865c-ec7fbb85bc01"
670 "12fd8bdc-e950-4281-aaa9-46e185658f79"
679 const char *path[] = {"Bridge", NULL};
680 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
682 if (!bridges || !YAJL_IS_OBJECT(bridges))
685 for (size_t i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
686 yajl_val bridge = YAJL_GET_OBJECT(bridges)->values[i];
687 ovs_stats_update_bridge(bridge);
691 /* Handle Bridge Table delete event */
692 static void ovs_stats_bridge_table_delete_cb(yajl_val jupdates) {
693 const char *path[] = {"Bridge", NULL};
694 yajl_val bridges = yajl_tree_get(jupdates, path, yajl_t_object);
695 if (!bridges || !YAJL_IS_OBJECT(bridges))
698 pthread_mutex_lock(&g_stats_lock);
699 for (size_t i = 0; i < YAJL_GET_OBJECT(bridges)->len; i++) {
700 yajl_val bridge = YAJL_GET_OBJECT(bridges)->values[i];
701 ovs_stats_del_bridge(bridge);
703 pthread_mutex_unlock(&g_stats_lock);
706 /* Handle JSON with Bridge table initial values */
707 static void ovs_stats_bridge_table_result_cb(yajl_val jresult,
709 if (YAJL_IS_NULL(jerror))
710 ovs_stats_bridge_table_change_cb(jresult);
712 ERROR("%s: Error received from OvSDB. Table: Bridge", plugin_name);
716 /* Update port name and interface UUID(s)*/
717 static int ovs_stats_update_port(const char *uuid, yajl_val port) {
718 const char *new[] = {"new", NULL};
719 const char *name[] = {"name", NULL};
721 if (!port || !YAJL_IS_OBJECT(port)) {
722 ERROR("Incorrect JSON Port data");
726 yajl_val row = yajl_tree_get(port, new, yajl_t_object);
727 if (!row || !YAJL_IS_OBJECT(row))
730 yajl_val port_name = yajl_tree_get(row, name, yajl_t_string);
731 if (!port_name || !YAJL_IS_STRING(port_name))
734 /* Create or get port by port uuid */
735 port_list_t *portentry = ovs_stats_new_port(NULL, uuid);
739 pthread_mutex_lock(&g_stats_lock);
740 sstrncpy(portentry->name, YAJL_GET_STRING(port_name),
741 sizeof(portentry->name));
742 pthread_mutex_unlock(&g_stats_lock);
744 yajl_val ifaces_root = ovs_utils_get_value_by_key(row, "interfaces");
745 char *ifaces_root_key =
746 YAJL_GET_STRING(YAJL_GET_ARRAY(ifaces_root)->values[0]);
748 if (strcmp("set", ifaces_root_key) == 0) {
749 // ifaces_root is ["set", [[ "uuid", "<some_uuid>" ], [ "uuid",
750 // "<another_uuid>" ], ... ]]
751 yajl_val ifaces_list = YAJL_GET_ARRAY(ifaces_root)->values[1];
753 // ifaces_list is [[ "uuid", "<some_uuid>" ], [ "uuid",
754 // "<another_uuid>" ], ... ]]
755 for (int i = 0; i < YAJL_GET_ARRAY(ifaces_list)->len; i++) {
756 yajl_val iface_tuple = YAJL_GET_ARRAY(ifaces_list)->values[i];
758 // iface_tuple is [ "uuid", "<some_uuid>" ]
759 char *iface_uuid_str =
760 YAJL_GET_STRING(YAJL_GET_ARRAY(iface_tuple)->values[1]);
762 // Also checks if interface already registered
763 ovs_stats_new_port_interface(portentry, iface_uuid_str);
766 // ifaces_root is [ "uuid", "<some_uuid>" ]
767 char *iface_uuid_str =
768 YAJL_GET_STRING(YAJL_GET_ARRAY(ifaces_root)->values[1]);
770 // Also checks if interface already registered
771 ovs_stats_new_port_interface(portentry, iface_uuid_str);
777 /* Delete port from global port list */
778 static int ovs_stats_del_port(const char *uuid) {
779 port_list_t *prev_port = g_port_list_head;
780 for (port_list_t *port = g_port_list_head; port != NULL;
781 prev_port = port, port = port->next) {
782 if (strncmp(port->port_uuid, uuid, strlen(port->port_uuid)) == 0) {
783 if (port == g_port_list_head)
784 g_port_list_head = port->next;
786 prev_port->next = port->next;
788 for (interface_list_t *iface = port->iface; iface != NULL;
789 iface = port->iface) {
790 interface_list_t *del = iface;
791 port->iface = iface->next;
802 /* Handle JSON with Port Table change event */
803 static void ovs_stats_port_table_change_cb(yajl_val jupdates) {
804 /* Port Table update example JSON data
807 "ab107d6f-28a1-4257-b1cc-5b742821db8a": {
812 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
819 const char *path[] = {"Port", NULL};
820 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
821 if (!ports || !YAJL_IS_OBJECT(ports))
824 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
825 yajl_val port = YAJL_GET_OBJECT(ports)->values[i];
826 ovs_stats_update_port(YAJL_GET_OBJECT(ports)->keys[i], port);
831 /* Handle JSON with Port table initial values */
832 static void ovs_stats_port_table_result_cb(yajl_val jresult, yajl_val jerror) {
833 if (YAJL_IS_NULL(jerror))
834 ovs_stats_port_table_change_cb(jresult);
836 ERROR("%s: Error received from OvSDB. Table: Port", plugin_name);
840 /* Handle Port Table delete event */
841 static void ovs_stats_port_table_delete_cb(yajl_val jupdates) {
842 const char *path[] = {"Port", NULL};
843 yajl_val ports = yajl_tree_get(jupdates, path, yajl_t_object);
844 if (!ports || !YAJL_IS_OBJECT(ports))
847 pthread_mutex_lock(&g_stats_lock);
848 for (size_t i = 0; i < YAJL_GET_OBJECT(ports)->len; i++) {
849 ovs_stats_del_port(YAJL_GET_OBJECT(ports)->keys[i]);
851 pthread_mutex_unlock(&g_stats_lock);
855 /* Update interface statistics */
856 static int ovs_stats_update_iface_stats(interface_list_t *iface,
859 if (!stats || !YAJL_IS_ARRAY(stats))
862 for (size_t i = 0; i < YAJL_GET_ARRAY(stats)->len; i++) {
863 yajl_val stat = YAJL_GET_ARRAY(stats)->values[i];
864 if (!YAJL_IS_ARRAY(stat))
867 char *counter_name = YAJL_GET_STRING(YAJL_GET_ARRAY(stat)->values[0]);
868 iface_counter counter_index = ovs_stats_counter_name_to_type(counter_name);
869 int64_t counter_value = YAJL_GET_INTEGER(YAJL_GET_ARRAY(stat)->values[1]);
870 if (counter_index == not_supported)
873 iface->stats[counter_index] = counter_value;
879 /* Update interface external_ids */
880 static int ovs_stats_update_iface_ext_ids(interface_list_t *iface,
883 if (ext_ids && YAJL_IS_ARRAY(ext_ids)) {
884 for (size_t i = 0; i < YAJL_GET_ARRAY(ext_ids)->len; i++) {
885 yajl_val ext_id = YAJL_GET_ARRAY(ext_ids)->values[i];
886 if (!YAJL_IS_ARRAY(ext_id))
889 char *key = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[0]);
890 char *value = YAJL_GET_STRING(YAJL_GET_ARRAY(ext_id)->values[1]);
892 if (strncmp(key, "iface-id", strlen(key)) == 0) {
893 sstrncpy(iface->ex_iface_id, value, sizeof(iface->ex_iface_id));
894 } else if (strncmp(key, "vm-uuid", strlen(key)) == 0) {
895 sstrncpy(iface->ex_vm_id, value, sizeof(iface->ex_vm_id));
904 /* Get interface statistic and external_ids */
905 static int ovs_stats_update_iface(yajl_val iface_obj) {
906 if (!iface_obj || !YAJL_IS_OBJECT(iface_obj)) {
907 ERROR("ovs_stats plugin: incorrect JSON interface data");
911 yajl_val row = ovs_utils_get_value_by_key(iface_obj, "new");
912 if (!row || !YAJL_IS_OBJECT(row))
915 yajl_val iface_name = ovs_utils_get_value_by_key(row, "name");
916 if (!iface_name || !YAJL_IS_STRING(iface_name))
919 yajl_val iface_uuid = ovs_utils_get_value_by_key(row, "_uuid");
920 if (!iface_uuid || !YAJL_IS_ARRAY(iface_uuid) ||
921 YAJL_GET_ARRAY(iface_uuid)->len != 2)
924 char *iface_uuid_str = YAJL_GET_STRING(YAJL_GET_ARRAY(iface_uuid)->values[1]);
925 if (iface_uuid_str == NULL) {
926 ERROR("ovs_stats plugin: incorrect JSON interface data");
930 interface_list_t *iface = ovs_stats_get_interface(iface_uuid_str);
934 sstrncpy(iface->name, YAJL_GET_STRING(iface_name), sizeof(iface->name));
936 yajl_val iface_stats = ovs_utils_get_value_by_key(row, "statistics");
937 yajl_val iface_ext_ids = ovs_utils_get_value_by_key(row, "external_ids");
956 Check that statistics is an array with 2 elements
959 if (iface_stats && YAJL_IS_ARRAY(iface_stats) &&
960 YAJL_GET_ARRAY(iface_stats)->len == 2)
961 ovs_stats_update_iface_stats(iface, YAJL_GET_ARRAY(iface_stats)->values[1]);
963 if (iface_ext_ids && YAJL_IS_ARRAY(iface_ext_ids))
964 ovs_stats_update_iface_ext_ids(iface,
965 YAJL_GET_ARRAY(iface_ext_ids)->values[1]);
970 /* Delete interface */
971 static int ovs_stats_del_interface(const char *uuid) {
972 port_list_t *port = ovs_stats_get_port_by_interface_uuid(uuid);
977 interface_list_t *prev_iface = NULL;
979 for (interface_list_t *iface = port->iface; iface != NULL;
980 iface = port->iface) {
981 if (strncmp(iface->iface_uuid, uuid, strlen(iface->iface_uuid))) {
983 interface_list_t *del = iface;
985 if (prev_iface == NULL)
986 port->iface = iface->next;
988 prev_iface->next = iface->next;
1000 /* Handle JSON with Interface Table change event */
1001 static void ovs_stats_interface_table_change_cb(yajl_val jupdates) {
1002 /* Interface Table update example JSON data
1005 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6": {
1028 "33a289a0-1d34-4e46-a3c2-3e4066fbecc6"
1039 "a61b7e2b-6951-488a-b4c6-6e91343960b2"
1052 const char *path[] = {"Interface", NULL};
1053 yajl_val interfaces = yajl_tree_get(jupdates, path, yajl_t_object);
1054 if (!interfaces || !YAJL_IS_OBJECT(interfaces))
1057 pthread_mutex_lock(&g_stats_lock);
1058 for (size_t i = 0; i < YAJL_GET_OBJECT(interfaces)->len; i++) {
1059 ovs_stats_update_iface(YAJL_GET_OBJECT(interfaces)->values[i]);
1061 pthread_mutex_unlock(&g_stats_lock);
1066 /* Handle JSON with Interface table initial values */
1067 static void ovs_stats_interface_table_result_cb(yajl_val jresult,
1069 if (YAJL_IS_NULL(jerror))
1070 ovs_stats_interface_table_change_cb(jresult);
1072 ERROR("%s: Error received from OvSDB. Table: Interface", plugin_name);
1076 /* Handle Interface Table delete event */
1077 static void ovs_stats_interface_table_delete_cb(yajl_val jupdates) {
1078 const char *path[] = {"Interface", NULL};
1079 yajl_val interfaces = yajl_tree_get(jupdates, path, yajl_t_object);
1080 if (!interfaces || !YAJL_IS_OBJECT(interfaces))
1083 pthread_mutex_lock(&g_stats_lock);
1084 for (size_t i = 0; i < YAJL_GET_OBJECT(interfaces)->len; i++) {
1085 ovs_stats_del_interface(YAJL_GET_OBJECT(interfaces)->keys[i]);
1087 pthread_mutex_unlock(&g_stats_lock);
1092 /* Setup OVS DB table callbacks */
1093 static void ovs_stats_initialize(ovs_db_t *pdb) {
1094 const char *bridge_columns[] = {"name", "ports", NULL};
1095 const char *port_columns[] = {"name", "interfaces", NULL};
1096 const char *interface_columns[] = {"name", "statistics", "_uuid",
1097 "external_ids", NULL};
1099 /* subscribe to a tables */
1100 ovs_db_table_cb_register(
1101 pdb, "Bridge", bridge_columns, ovs_stats_bridge_table_change_cb,
1102 ovs_stats_bridge_table_result_cb,
1103 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
1104 OVS_DB_TABLE_CB_FLAG_MODIFY);
1106 ovs_db_table_cb_register(pdb, "Bridge", bridge_columns,
1107 ovs_stats_bridge_table_delete_cb, NULL,
1108 OVS_DB_TABLE_CB_FLAG_DELETE);
1110 ovs_db_table_cb_register(
1111 pdb, "Port", port_columns, ovs_stats_port_table_change_cb,
1112 ovs_stats_port_table_result_cb,
1113 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
1114 OVS_DB_TABLE_CB_FLAG_MODIFY);
1116 ovs_db_table_cb_register(pdb, "Port", port_columns,
1117 ovs_stats_port_table_delete_cb, NULL,
1118 OVS_DB_TABLE_CB_FLAG_DELETE);
1120 ovs_db_table_cb_register(
1121 pdb, "Interface", interface_columns, ovs_stats_interface_table_change_cb,
1122 ovs_stats_interface_table_result_cb,
1123 OVS_DB_TABLE_CB_FLAG_INITIAL | OVS_DB_TABLE_CB_FLAG_INSERT |
1124 OVS_DB_TABLE_CB_FLAG_MODIFY);
1126 ovs_db_table_cb_register(pdb, "Interface", interface_columns,
1127 ovs_stats_interface_table_delete_cb, NULL,
1128 OVS_DB_TABLE_CB_FLAG_DELETE);
1131 /* Check if bridge is configured to be monitored in config file */
1132 static int ovs_stats_is_monitored_bridge(const char *br_name) {
1133 /* if no bridges are configured, return true */
1134 if (g_monitored_bridge_list_head == NULL)
1137 /* check if given bridge exists */
1138 if (ovs_stats_get_bridge(g_monitored_bridge_list_head, br_name) != NULL)
1144 /* Delete all ports from port list */
1145 static void ovs_stats_free_port_list(port_list_t *head) {
1146 for (port_list_t *i = head; i != NULL;) {
1147 port_list_t *del = i;
1149 for (interface_list_t *iface = i->iface; iface != NULL; iface = i->iface) {
1150 interface_list_t *del2 = iface;
1151 i->iface = iface->next;
1160 /* Delete all bridges from bridge list */
1161 static void ovs_stats_free_bridge_list(bridge_list_t *head) {
1162 for (bridge_list_t *i = head; i != NULL;) {
1163 bridge_list_t *del = i;
1170 /* Handle OVSDB lost connection callback */
1171 static void ovs_stats_conn_terminate() {
1172 WARNING("Lost connection to OVSDB server");
1173 pthread_mutex_lock(&g_stats_lock);
1174 ovs_stats_free_bridge_list(g_bridge_list_head);
1175 g_bridge_list_head = NULL;
1176 ovs_stats_free_port_list(g_port_list_head);
1177 g_port_list_head = NULL;
1178 pthread_mutex_unlock(&g_stats_lock);
1181 /* Parse plugin configuration file and store the config
1182 * in allocated memory. Returns negative value in case of error.
1184 static int ovs_stats_plugin_config(oconfig_item_t *ci) {
1185 bridge_list_t *bridge;
1187 for (int i = 0; i < ci->children_num; i++) {
1188 oconfig_item_t *child = ci->children + i;
1189 if (strcasecmp("Address", child->key) == 0) {
1190 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_node,
1191 OVS_DB_ADDR_NODE_SIZE) != 0) {
1192 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1195 } else if (strcasecmp("Port", child->key) == 0) {
1196 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_serv,
1197 OVS_DB_ADDR_SERVICE_SIZE) != 0) {
1198 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1201 } else if (strcasecmp("Socket", child->key) == 0) {
1202 if (cf_util_get_string_buffer(child, ovs_stats_cfg.ovs_db_unix,
1203 OVS_DB_ADDR_UNIX_SIZE) != 0) {
1204 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1207 } else if (strcasecmp("Bridges", child->key) == 0) {
1208 for (int j = 0; j < child->values_num; j++) {
1209 /* check value type */
1210 if (child->values[j].type != OCONFIG_TYPE_STRING) {
1211 ERROR("%s: Wrong bridge name [idx=%d]. "
1212 "Bridge name should be string",
1217 char const *br_name = child->values[j].value.string;
1218 if ((bridge = ovs_stats_get_bridge(g_monitored_bridge_list_head,
1219 br_name)) == NULL) {
1220 if ((bridge = calloc(1, sizeof(bridge_list_t))) == NULL) {
1221 ERROR("%s: Error allocating memory for bridge", plugin_name);
1224 char *br_name_dup = strdup(br_name);
1225 if (br_name_dup == NULL) {
1226 ERROR("%s: strdup() copy bridge name fail", plugin_name);
1231 pthread_mutex_lock(&g_stats_lock);
1232 /* store bridge name */
1233 bridge->name = br_name_dup;
1234 bridge->next = g_monitored_bridge_list_head;
1235 g_monitored_bridge_list_head = bridge;
1236 pthread_mutex_unlock(&g_stats_lock);
1237 DEBUG("%s: found monitored interface \"%s\"", plugin_name, br_name);
1241 } else if (strcasecmp("InterfaceStats", child->key) == 0) {
1242 if (cf_util_get_boolean(child, &interface_stats) != 0) {
1243 ERROR("%s: parse '%s' option failed", plugin_name, child->key);
1247 WARNING("%s: option '%s' not allowed here", plugin_name, child->key);
1254 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
1258 /* Initialize OvS Stats plugin*/
1259 static int ovs_stats_plugin_init(void) {
1260 ovs_db_callback_t cb = {.post_conn_init = ovs_stats_initialize,
1261 .post_conn_terminate = ovs_stats_conn_terminate};
1263 INFO("%s: Connecting to OVS DB using address=%s, service=%s, unix=%s",
1264 plugin_name, ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
1265 ovs_stats_cfg.ovs_db_unix);
1266 /* connect to OvS DB */
1268 ovs_db_init(ovs_stats_cfg.ovs_db_node, ovs_stats_cfg.ovs_db_serv,
1269 ovs_stats_cfg.ovs_db_unix, &cb)) == NULL) {
1270 ERROR("%s: plugin: failed to connect to OvS DB server", plugin_name);
1273 int err = pthread_mutex_init(&g_stats_lock, NULL);
1275 ERROR("%s: plugin: failed to initialize cache lock", plugin_name);
1276 ovs_db_destroy(g_ovs_db);
1282 /* OvS stats read callback. Read bridge/port information and submit it*/
1283 static int ovs_stats_plugin_read(__attribute__((unused)) user_data_t *ud) {
1284 bridge_list_t *bridge;
1287 pthread_mutex_lock(&g_stats_lock);
1288 for (bridge = g_bridge_list_head; bridge != NULL; bridge = bridge->next) {
1289 if (!ovs_stats_is_monitored_bridge(bridge->name))
1292 for (port = g_port_list_head; port != NULL; port = port->next) {
1293 if (port->br != bridge)
1296 if (strlen(port->name) == 0)
1297 /* Skip port w/o name. This is possible when read callback
1298 * is called after Interface Table update callback but before
1299 * Port table Update callback. Will add this port on next read */
1302 ovs_stats_submit_port(bridge, port);
1304 if (interface_stats)
1305 ovs_stats_submit_interfaces(bridge, port);
1308 pthread_mutex_unlock(&g_stats_lock);
1312 /* Shutdown OvS Stats plugin */
1313 static int ovs_stats_plugin_shutdown(void) {
1314 DEBUG("OvS Statistics plugin shutting down");
1315 ovs_db_destroy(g_ovs_db);
1316 pthread_mutex_lock(&g_stats_lock);
1317 ovs_stats_free_bridge_list(g_bridge_list_head);
1318 ovs_stats_free_bridge_list(g_monitored_bridge_list_head);
1319 ovs_stats_free_port_list(g_port_list_head);
1320 pthread_mutex_unlock(&g_stats_lock);
1321 pthread_mutex_destroy(&g_stats_lock);
1325 /* Register OvS Stats plugin callbacks */
1326 void module_register(void) {
1327 plugin_register_complex_config(plugin_name, ovs_stats_plugin_config);
1328 plugin_register_init(plugin_name, ovs_stats_plugin_init);
1329 plugin_register_complex_read(NULL, plugin_name, ovs_stats_plugin_read, 0,
1331 plugin_register_shutdown(plugin_name, ovs_stats_plugin_shutdown);