Treewide: replace ssnprintf with snprintf
[collectd.git] / src / dpdkevents.c
index 62e28b9..3941f1b 100644 (file)
  *   Krzysztof Matczak <krzysztofx@intel.com>
  */
 
+#include "collectd.h"
+
 #include "common.h"
 #include "plugin.h"
+
 #include "semaphore.h"
 #include "sys/mman.h"
 #include "utils_dpdk.h"
 #include "utils_time.h"
-#include "collectd.h"
 
 #include <rte_config.h>
 #include <rte_eal.h>
@@ -51,8 +53,6 @@
 #define KEEPALIVE_PLUGIN_INSTANCE "keepalive"
 #define RTE_KEEPALIVE_SHM_NAME "/dpdk_keepalive_shm_name"
 
-#define RTE_VERSION_16_07 RTE_VERSION_NUM(16, 7, 0, 16)
-
 typedef struct dpdk_keepalive_shm_s {
   sem_t core_died;
   enum rte_keepalive_state core_state[RTE_KEEPALIVE_MAXCORES];
@@ -231,7 +231,7 @@ static int dpdk_events_link_status_config(dpdk_events_ctx_t *ec,
     if (strcasecmp("PortName", child->key) == 0) {
       while (!(ec->config.link_status.enabled_port_mask & (1 << port_num)))
         port_num++;
-      ssnprintf(ec->config.link_status.port_name[port_num], DATA_MAX_NAME_LEN,
+      snprintf(ec->config.link_status.port_name[port_num], DATA_MAX_NAME_LEN,
                 "%s", child->values[0].value.string);
       DEBUG(DPDK_EVENTS_PLUGIN ": LinkStatus:Port %d Name: %s", port_num,
             ec->config.link_status.port_name[port_num]);
@@ -256,7 +256,7 @@ static int dpdk_events_keep_alive_config(dpdk_events_ctx_t *ec,
             (int)child->values[0].value.boolean);
     } else if (strcasecmp("LCoreMask", child->key) == 0) {
       char lcore_mask[DATA_MAX_NAME_LEN];
-      ssnprintf(lcore_mask, sizeof(lcore_mask), "%s",
+      snprintf(lcore_mask, sizeof(lcore_mask), "%s",
                 child->values[0].value.string);
       ec->config.keep_alive.lcore_mask =
           str_to_uint128(lcore_mask, strlen(lcore_mask));
@@ -264,7 +264,7 @@ static int dpdk_events_keep_alive_config(dpdk_events_ctx_t *ec,
             ec->config.keep_alive.lcore_mask.high,
             ec->config.keep_alive.lcore_mask.low);
     } else if (strcasecmp("KeepAliveShmName", child->key) == 0) {
-      ssnprintf(ec->config.keep_alive.shm_name,
+      snprintf(ec->config.keep_alive.shm_name,
                 sizeof(ec->config.keep_alive.shm_name), "%s",
                 child->values[0].value.string);
       DEBUG(DPDK_EVENTS_PLUGIN ": KeepAlive:KeepAliveShmName %s",
@@ -304,6 +304,12 @@ static int dpdk_events_config(oconfig_item_t *ci) {
     }
   }
 
+  if (!ec->config.keep_alive.enabled && !ec->config.link_status.enabled) {
+    ERROR(DPDK_EVENTS_PLUGIN ": At least one type of events should be "
+                             "configured for collecting. Plugin misconfigured");
+    return -1;
+  }
+
   return ret;
 }
 
@@ -350,28 +356,27 @@ int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
   }
 
   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
-
+  int ret = 0;
   if (ec->config.link_status.enabled)
-    dpdk_helper_link_status_get(phc);
+    ret = dpdk_helper_link_status_get(phc);
 
-  return 0;
+  return ret;
 }
 
 static void dpdk_events_notification_dispatch(int severity,
-                                              char *plugin_instance,
-                                              cdtime_t time, char *msg) {
-  notification_t n = {0};
-  n.severity = severity;
-  n.time = time;
+                                              const char *plugin_instance,
+                                              cdtime_t time, const char *msg) {
+  notification_t n = {
+      .severity = severity, .time = time, .plugin = DPDK_EVENTS_PLUGIN};
   sstrncpy(n.host, hostname_g, sizeof(n.host));
-  sstrncpy(n.plugin, DPDK_EVENTS_PLUGIN, sizeof(n.plugin));
   sstrncpy(n.plugin_instance, plugin_instance, sizeof(n.plugin_instance));
   sstrncpy(n.message, msg, sizeof(n.message));
   plugin_dispatch_notification(&n);
 }
 
-static void dpdk_events_gauge_submit(char *plugin_instance, char *type,
-                                     gauge_t value, cdtime_t time) {
+static void dpdk_events_gauge_submit(const char *plugin_instance,
+                                     const char *type_instance, gauge_t value,
+                                     cdtime_t time) {
   value_list_t vl = {.values = &(value_t){.gauge = value},
                      .values_len = 1,
                      .time = time,
@@ -380,7 +385,7 @@ static void dpdk_events_gauge_submit(char *plugin_instance, char *type,
                      .meta = NULL};
   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
-  sstrncpy(vl.type_instance, type, sizeof(vl.type_instance));
+  sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
   plugin_dispatch_values(&vl);
 }
 
@@ -400,16 +405,16 @@ static int dpdk_events_link_status_dispatch(dpdk_helper_ctx_t *phc) {
 
         char dev_name[DATA_MAX_NAME_LEN];
         if (ec->config.link_status.port_name[i][0] != 0) {
-          ssnprintf(dev_name, sizeof(dev_name), "%s",
+          snprintf(dev_name, sizeof(dev_name), "%s",
                     ec->config.link_status.port_name[i]);
         } else {
-          ssnprintf(dev_name, sizeof(dev_name), "port.%d", i);
+          snprintf(dev_name, sizeof(dev_name), "port.%d", i);
         }
 
         if (ec->config.link_status.notify) {
           int sev = ec->link_info[i].link_status ? NOTIF_OKAY : NOTIF_WARNING;
           char msg[DATA_MAX_NAME_LEN];
-          ssnprintf(msg, sizeof(msg), "Link Status: %s",
+          snprintf(msg, sizeof(msg), "Link Status: %s",
                     ec->link_info[i].link_status ? "UP" : "DOWN");
           dpdk_events_notification_dispatch(sev, dev_name,
                                             ec->link_info[i].read_time, msg);
@@ -426,7 +431,7 @@ static int dpdk_events_link_status_dispatch(dpdk_helper_ctx_t *phc) {
   return 0;
 }
 
-static int dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
+static void dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(phc);
 
   /* dispatch Keep Alive values to collectd */
@@ -446,7 +451,7 @@ static int dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
     }
 
     char core_name[DATA_MAX_NAME_LEN];
-    ssnprintf(core_name, sizeof(core_name), "lcore%u", i);
+    snprintf(core_name, sizeof(core_name), "lcore%u", i);
 
     if (!ec->config.keep_alive.send_updated ||
         (ec->core_info[i].lcore_state !=
@@ -454,7 +459,6 @@ static int dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
       ec->core_info[i].lcore_state = ec->config.keep_alive.shm->core_state[i];
       ec->core_info[i].read_time = cdtime();
 
-#if RTE_VERSION >= RTE_VERSION_16_07
       if (ec->config.keep_alive.notify) {
         char msg[DATA_MAX_NAME_LEN];
         int sev;
@@ -462,34 +466,34 @@ static int dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
         switch (ec->config.keep_alive.shm->core_state[i]) {
         case RTE_KA_STATE_ALIVE:
           sev = NOTIF_OKAY;
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: ALIVE", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: ALIVE", i);
           break;
         case RTE_KA_STATE_MISSING:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: MISSING", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: MISSING", i);
           sev = NOTIF_WARNING;
           break;
         case RTE_KA_STATE_DEAD:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DEAD", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DEAD", i);
           sev = NOTIF_FAILURE;
           break;
         case RTE_KA_STATE_UNUSED:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNUSED", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNUSED", i);
           sev = NOTIF_OKAY;
           break;
         case RTE_KA_STATE_GONE:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: GONE", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: GONE", i);
           sev = NOTIF_FAILURE;
           break;
         case RTE_KA_STATE_DOZING:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DOZING", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: DOZING", i);
           sev = NOTIF_OKAY;
           break;
         case RTE_KA_STATE_SLEEP:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: SLEEP", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: SLEEP", i);
           sev = NOTIF_OKAY;
           break;
         default:
-          ssnprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNKNOWN", i);
+          snprintf(msg, sizeof(msg), "lcore %u Keep Alive Status: UNKNOWN", i);
           sev = NOTIF_FAILURE;
         }
 
@@ -500,15 +504,8 @@ static int dpdk_events_keep_alive_dispatch(dpdk_helper_ctx_t *phc) {
                                  ec->config.keep_alive.shm->core_state[i],
                                  ec->core_info[i].read_time);
       }
-#else
-      dpdk_events_gauge_submit(KEEPALIVE_PLUGIN_INSTANCE, core_name,
-                               ec->config.keep_alive.shm->core_state[i],
-                               ec->core_info[i].read_time);
-#endif /* #if RTE_VERSION >= RTE_VERSION_16_07 */
     }
   }
-
-  return 0;
 }
 
 static int dpdk_events_read(user_data_t *ud) {
@@ -516,16 +513,11 @@ static int dpdk_events_read(user_data_t *ud) {
 
   if (g_hc == NULL) {
     ERROR(DPDK_EVENTS_PLUGIN ": plugin not initialized.");
-    return -EINVAL;
+    return -1;
   }
 
   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
 
-  if (!ec->config.keep_alive.enabled && !ec->config.link_status.enabled) {
-    /* nothing to do */
-    return 0;
-  }
-
   if (ec->config.link_status.enabled) {
     int cmd_res = 0;
     int ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_EVENTS, &cmd_res,
@@ -564,7 +556,7 @@ static int dpdk_events_init(void) {
 
 static int dpdk_events_shutdown(void) {
   DPDK_EVENTS_TRACE();
-  int ret = 0;
+  int ret;
 
   dpdk_events_ctx_t *ec = DPDK_EVENTS_CTX_GET(g_hc);
   if (ec->config.keep_alive.enabled) {