dpdkstat: use portable format strings
[collectd.git] / src / dpdkstat.c
index 145f96b..3ab269d 100644 (file)
@@ -25,6 +25,7 @@
  * Authors:
  *   Maryam Tahhan <maryam.tahhan@intel.com>
  *   Harry van Haaren <harry.van.haaren@intel.com>
+ *   Taras Chornyi <tarasx.chornyi@intel.com>
  */
 
 #include "collectd.h"
 #include "utils_time.h"
 
 #include <getopt.h>
+#include <poll.h>
 #include <semaphore.h>
 #include <sys/mman.h>
 #include <sys/queue.h>
-#include <poll.h>
 
+#include <rte_atomic.h>
+#include <rte_branch_prediction.h>
+#include <rte_common.h>
 #include <rte_config.h>
+#include <rte_debug.h>
+#include <rte_debug.h>
 #include <rte_eal.h>
 #include <rte_ethdev.h>
-#include <rte_common.h>
-#include <rte_debug.h>
+#include <rte_launch.h>
+#include <rte_lcore.h>
+#include <rte_log.h>
 #include <rte_malloc.h>
 #include <rte_memory.h>
 #include <rte_memzone.h>
-#include <rte_launch.h>
-#include <rte_tailq.h>
-#include <rte_lcore.h>
 #include <rte_per_lcore.h>
-#include <rte_debug.h>
-#include <rte_log.h>
-#include <rte_atomic.h>
-#include <rte_branch_prediction.h>
 #include <rte_string_fns.h>
+#include <rte_tailq.h>
+#include <rte_version.h>
 
 #define DPDK_DEFAULT_RTE_CONFIG "/var/run/.rte_config"
 #define DPDK_MAX_ARGC 8
 #define RESET 1
 #define NO_RESET 0
 
+#define RTE_VERSION_16_07 RTE_VERSION_NUM(16, 7, 0, 16)
+
+#if RTE_VERSION < RTE_VERSION_16_07
+#define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value
+#define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xstats[index].name
+#define DPDK_STATS_CTX_GET_XSTAT_SIZE sizeof(struct rte_eth_xstats)
+#define DPDK_STATS_CTX_INIT(ctx)                                               \
+  do {                                                                         \
+    ctx->xstats = (struct rte_eth_xstats *)&ctx->raw_data[0];                  \
+  } while (0)
+#else
+#define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value
+#define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xnames[index].name
+#define DPDK_STATS_CTX_GET_XSTAT_SIZE                                          \
+  (sizeof(struct rte_eth_xstat) + sizeof(struct rte_eth_xstat_name))
+#define DPDK_STATS_CTX_INIT(ctx)                                               \
+  do {                                                                         \
+    ctx->xstats = (struct rte_eth_xstat *)&ctx->raw_data[0];                   \
+    ctx->xnames =                                                              \
+        (struct rte_eth_xstat_name *)&ctx                                      \
+            ->raw_data[ctx->num_xstats * sizeof(struct rte_eth_xstat)];        \
+  } while (0)
+#endif
+
 enum DPDK_HELPER_ACTION {
   DPDK_HELPER_ACTION_COUNT_STATS,
   DPDK_HELPER_ACTION_SEND_STATS,
@@ -105,7 +131,13 @@ struct dpdk_config_s {
   cdtime_t port_read_time[RTE_MAX_ETHPORTS];
   uint32_t num_stats_in_port[RTE_MAX_ETHPORTS];
   struct rte_eth_link link_status[RTE_MAX_ETHPORTS];
+#if RTE_VERSION < RTE_VERSION_16_07
   struct rte_eth_xstats *xstats;
+#else
+  struct rte_eth_xstat *xstats;
+  struct rte_eth_xstat_name *xnames;
+#endif
+  char *raw_data;
   /* rte_eth_xstats from here on until the end of the SHM */
 };
 typedef struct dpdk_config_s dpdk_config_t;
@@ -127,8 +159,8 @@ static int dpdk_shm_init(size_t size);
 static void dpdk_config_init_default(void) {
   g_configuration->interval = plugin_get_interval();
   if (g_configuration->interval == cf_get_default_interval())
-    WARNING("dpdkstat: No time interval was configured, default value %lu ms "
-            "is set",
+    WARNING("dpdkstat: No time interval was configured, default value %" PRIu64
+            " ms is set",
             CDTIME_T_TO_MS(g_configuration->interval));
   /* Default is all ports enabled */
   g_configuration->enabled_port_mask = ~0;
@@ -288,7 +320,7 @@ static int dpdk_re_init_shm() {
 
   size_t shm_xstats_size =
       sizeof(dpdk_config_t) +
-      (sizeof(struct rte_eth_xstats) * g_configuration->num_xstats);
+      (DPDK_STATS_CTX_GET_XSTAT_SIZE * g_configuration->num_xstats);
   DEBUG("=== SHM new size for %" PRIu32 " xstats", g_configuration->num_xstats);
 
   int err = dpdk_shm_cleanup();
@@ -307,7 +339,8 @@ static int dpdk_re_init_shm() {
 
   memcpy(g_configuration, &temp_config, sizeof(dpdk_config_t));
   g_configuration->collectd_reinit_shm = 0;
-  g_configuration->xstats = (struct rte_eth_xstats *)(g_configuration + 1);
+  g_configuration->raw_data = (char *)(g_configuration + 1);
+  DPDK_STATS_CTX_INIT(g_configuration);
   return 0;
 }
 
@@ -380,7 +413,7 @@ static int dpdk_helper_spawn(enum DPDK_HELPER_ACTION action) {
   if (pid > 0) {
     close(g_configuration->helper_pipes[1]);
     g_configuration->helper_pid = pid;
-    DEBUG("dpdkstat: helper pid %lu", (long)g_configuration->helper_pid);
+    DEBUG("dpdkstat: helper pid %li", (long)g_configuration->helper_pid);
     /* Kick helper once its alive to have it start processing */
     sem_post(&g_configuration->sema_helper_get_stats);
   } else if (pid == 0) {
@@ -447,12 +480,12 @@ static int dpdk_helper_run(void) {
 
   while (1) {
     /* sem_timedwait() to avoid blocking forever */
-    struct timespec ts;
     cdtime_t now = cdtime();
     cdtime_t safety_period = MS_TO_CDTIME_T(1500);
-    CDTIME_T_TO_TIMESPEC(now + safety_period + g_configuration->interval * 2,
-                         &ts);
-    int ret = sem_timedwait(&g_configuration->sema_helper_get_stats, &ts);
+    int ret =
+        sem_timedwait(&g_configuration->sema_helper_get_stats,
+                      &CDTIME_T_TO_TIMESPEC(now + safety_period +
+                                            g_configuration->interval * 2));
 
     if (ret == -1 && errno == ETIMEDOUT) {
       ERROR("dpdkstat-helper: sem timedwait()"
@@ -504,7 +537,11 @@ static int dpdk_helper_run(void) {
         continue;
 
       if (g_configuration->helper_action == DPDK_HELPER_ACTION_COUNT_STATS) {
+#if RTE_VERSION >= RTE_VERSION_16_07
+        len = rte_eth_xstats_get_names(i, NULL, 0);
+#else
         len = rte_eth_xstats_get(i, NULL, 0);
+#endif
         if (len < 0) {
           ERROR("dpdkstat-helper: Cannot get xstats count on port %" PRIu8, i);
           break;
@@ -525,6 +562,15 @@ static int dpdk_helper_run(void) {
                 i, len);
           break;
         }
+#if RTE_VERSION >= RTE_VERSION_16_07
+        ret = rte_eth_xstats_get_names(i, g_configuration->xnames + num_xstats,
+                                       len);
+        if (ret < 0 || ret != len) {
+          ERROR("dpdkstat-helper: Error reading xstat names (port=%d; len=%d)",
+                i, len);
+          break;
+        }
+#endif
         num_xstats += g_configuration->num_stats_in_port[enabled_port_count];
         enabled_port_count++;
       }
@@ -551,80 +597,83 @@ static int dpdk_helper_run(void) {
   return 0;
 }
 
-static void dpdk_submit_xstats(const char *dev_name,
-                               const struct rte_eth_xstats *xstats,
+static void dpdk_submit_xstats(const char *dev_name, int count,
                                uint32_t counters, cdtime_t port_read_time) {
   for (uint32_t j = 0; j < counters; j++) {
-    value_list_t dpdkstat_vl = VALUE_LIST_INIT;
+    value_list_t vl = VALUE_LIST_INIT;
+    char *counter_name;
     char *type_end;
 
-    dpdkstat_vl.values = &(value_t){.derive = (derive_t)xstats[j].value};
-    dpdkstat_vl.values_len = 1; /* Submit stats one at a time */
-    dpdkstat_vl.time = port_read_time;
-    sstrncpy(dpdkstat_vl.host, hostname_g, sizeof(dpdkstat_vl.host));
-    sstrncpy(dpdkstat_vl.plugin, "dpdkstat", sizeof(dpdkstat_vl.plugin));
-    sstrncpy(dpdkstat_vl.plugin_instance, dev_name,
-             sizeof(dpdkstat_vl.plugin_instance));
+    vl.values = &(value_t){.derive = (derive_t)DPDK_STATS_XSTAT_GET_VALUE(
+                               g_configuration, count + j)};
+    vl.values_len = 1; /* Submit stats one at a time */
+    vl.time = port_read_time;
+    sstrncpy(vl.plugin, "dpdkstat", sizeof(vl.plugin));
+    sstrncpy(vl.plugin_instance, dev_name, sizeof(vl.plugin_instance));
+    counter_name = DPDK_STATS_XSTAT_GET_NAME(g_configuration, count + j);
+    if (counter_name == NULL) {
+      WARNING("dpdkstat: Failed to get counter name.");
+      return;
+    }
 
-    type_end = strrchr(xstats[j].name, '_');
+    type_end = strrchr(counter_name, '_');
 
     if ((type_end != NULL) &&
-        (strncmp(xstats[j].name, "rx_", strlen("rx_")) == 0)) {
+        (strncmp(counter_name, "rx_", strlen("rx_")) == 0)) {
       if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_rx_errors", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_rx_errors", sizeof(vl.type));
       } else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_rx_dropped", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_rx_dropped", sizeof(vl.type));
       } else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_rx_octets", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_rx_octets", sizeof(vl.type));
       } else if (strncmp(type_end, "_packets", strlen("_packets")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_rx_packets", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_rx_packets", sizeof(vl.type));
       } else if (strncmp(type_end, "_placement", strlen("_placement")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_rx_errors", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_rx_errors", sizeof(vl.type));
       } else if (strncmp(type_end, "_buff", strlen("_buff")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_rx_errors", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_rx_errors", sizeof(vl.type));
       } else {
         /* Does not fit obvious type: use a more generic one */
-        sstrncpy(dpdkstat_vl.type, "derive", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "derive", sizeof(vl.type));
       }
 
     } else if ((type_end != NULL) &&
-               (strncmp(xstats[j].name, "tx_", strlen("tx_"))) == 0) {
+               (strncmp(counter_name, "tx_", strlen("tx_"))) == 0) {
       if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_tx_errors", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_tx_errors", sizeof(vl.type));
       } else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_tx_dropped", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_tx_dropped", sizeof(vl.type));
       } else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_tx_octets", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_tx_octets", sizeof(vl.type));
       } else if (strncmp(type_end, "_packets", strlen("_packets")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "if_tx_packets", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "if_tx_packets", sizeof(vl.type));
       } else {
         /* Does not fit obvious type: use a more generic one */
-        sstrncpy(dpdkstat_vl.type, "derive", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "derive", sizeof(vl.type));
       }
     } else if ((type_end != NULL) &&
-               (strncmp(xstats[j].name, "flow_", strlen("flow_"))) == 0) {
+               (strncmp(counter_name, "flow_", strlen("flow_"))) == 0) {
 
       if (strncmp(type_end, "_filters", strlen("_filters")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "operations", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "operations", sizeof(vl.type));
       } else if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "errors", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "errors", sizeof(vl.type));
       } else if (strncmp(type_end, "_filters", strlen("_filters")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "filter_result", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "filter_result", sizeof(vl.type));
       }
     } else if ((type_end != NULL) &&
-               (strncmp(xstats[j].name, "mac_", strlen("mac_"))) == 0) {
+               (strncmp(counter_name, "mac_", strlen("mac_"))) == 0) {
       if (strncmp(type_end, "_errors", strlen("_errors")) == 0) {
-        sstrncpy(dpdkstat_vl.type, "errors", sizeof(dpdkstat_vl.type));
+        sstrncpy(vl.type, "errors", sizeof(vl.type));
       }
     } else {
       /* Does not fit obvious type, or strrchr error:
        *   use a more generic type */
-      sstrncpy(dpdkstat_vl.type, "derive", sizeof(dpdkstat_vl.type));
+      sstrncpy(vl.type, "derive", sizeof(vl.type));
     }
 
-    sstrncpy(dpdkstat_vl.type_instance, xstats[j].name,
-             sizeof(dpdkstat_vl.type_instance));
-    plugin_dispatch_values(&dpdkstat_vl);
+    sstrncpy(vl.type_instance, counter_name, sizeof(vl.type_instance));
+    plugin_dispatch_values(&vl);
   }
 }
 
@@ -701,10 +750,9 @@ static int dpdk_read(user_data_t *ud) {
   /* Kick helper process through SHM */
   sem_post(&g_configuration->sema_helper_get_stats);
 
-  struct timespec ts;
   cdtime_t now = cdtime();
-  CDTIME_T_TO_TIMESPEC(now + g_configuration->interval, &ts);
-  ret = sem_timedwait(&g_configuration->sema_stats_in_shm, &ts);
+  ret = sem_timedwait(&g_configuration->sema_stats_in_shm,
+                      &CDTIME_T_TO_TIMESPEC(now + g_configuration->interval));
   if (ret == -1) {
     if (errno == ETIMEDOUT)
       DEBUG(
@@ -731,9 +779,7 @@ static int dpdk_read(user_data_t *ud) {
                 g_configuration->port_name[i]);
     else
       ssnprintf(dev_name, sizeof(dev_name), "port.%" PRIu32, port_num);
-    struct rte_eth_xstats *xstats = g_configuration->xstats + count;
-
-    dpdk_submit_xstats(dev_name, xstats, counters_num, port_read_time);
+    dpdk_submit_xstats(dev_name, count, counters_num, port_read_time);
     count += counters_num;
     port_num++;
   } /* for each port */